tfc_cmd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010 Cisco Systems, Inc.
  4. */
  5. /* XXX TBD some includes may be extraneous */
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/utsname.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/kthread.h>
  12. #include <linux/types.h>
  13. #include <linux/string.h>
  14. #include <linux/configfs.h>
  15. #include <linux/ctype.h>
  16. #include <linux/hash.h>
  17. #include <asm/unaligned.h>
  18. #include <scsi/scsi_tcq.h>
  19. #include <scsi/libfc.h>
  20. #include <target/target_core_base.h>
  21. #include <target/target_core_fabric.h>
  22. #include "tcm_fc.h"
  23. /*
  24. * Dump cmd state for debugging.
  25. */
  26. static void _ft_dump_cmd(struct ft_cmd *cmd, const char *caller)
  27. {
  28. struct fc_exch *ep;
  29. struct fc_seq *sp;
  30. struct se_cmd *se_cmd;
  31. struct scatterlist *sg;
  32. int count;
  33. se_cmd = &cmd->se_cmd;
  34. pr_debug("%s: cmd %p sess %p seq %p se_cmd %p\n",
  35. caller, cmd, cmd->sess, cmd->seq, se_cmd);
  36. pr_debug("%s: cmd %p data_nents %u len %u se_cmd_flags <0x%x>\n",
  37. caller, cmd, se_cmd->t_data_nents,
  38. se_cmd->data_length, se_cmd->se_cmd_flags);
  39. for_each_sg(se_cmd->t_data_sg, sg, se_cmd->t_data_nents, count)
  40. pr_debug("%s: cmd %p sg %p page %p "
  41. "len 0x%x off 0x%x\n",
  42. caller, cmd, sg,
  43. sg_page(sg), sg->length, sg->offset);
  44. sp = cmd->seq;
  45. if (sp) {
  46. ep = fc_seq_exch(sp);
  47. pr_debug("%s: cmd %p sid %x did %x "
  48. "ox_id %x rx_id %x seq_id %x e_stat %x\n",
  49. caller, cmd, ep->sid, ep->did, ep->oxid, ep->rxid,
  50. sp->id, ep->esb_stat);
  51. }
  52. }
  53. void ft_dump_cmd(struct ft_cmd *cmd, const char *caller)
  54. {
  55. if (unlikely(ft_debug_logging))
  56. _ft_dump_cmd(cmd, caller);
  57. }
  58. static void ft_free_cmd(struct ft_cmd *cmd)
  59. {
  60. struct fc_frame *fp;
  61. struct ft_sess *sess;
  62. if (!cmd)
  63. return;
  64. sess = cmd->sess;
  65. fp = cmd->req_frame;
  66. if (fr_seq(fp))
  67. fc_seq_release(fr_seq(fp));
  68. fc_frame_free(fp);
  69. target_free_tag(sess->se_sess, &cmd->se_cmd);
  70. ft_sess_put(sess); /* undo get from lookup at recv */
  71. }
  72. void ft_release_cmd(struct se_cmd *se_cmd)
  73. {
  74. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  75. ft_free_cmd(cmd);
  76. }
  77. int ft_check_stop_free(struct se_cmd *se_cmd)
  78. {
  79. return transport_generic_free_cmd(se_cmd, 0);
  80. }
  81. /*
  82. * Send response.
  83. */
  84. int ft_queue_status(struct se_cmd *se_cmd)
  85. {
  86. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  87. struct fc_frame *fp;
  88. struct fcp_resp_with_ext *fcp;
  89. struct fc_lport *lport;
  90. struct fc_exch *ep;
  91. size_t len;
  92. int rc;
  93. if (cmd->aborted)
  94. return 0;
  95. ft_dump_cmd(cmd, __func__);
  96. ep = fc_seq_exch(cmd->seq);
  97. lport = ep->lp;
  98. len = sizeof(*fcp) + se_cmd->scsi_sense_length;
  99. fp = fc_frame_alloc(lport, len);
  100. if (!fp) {
  101. se_cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
  102. return -ENOMEM;
  103. }
  104. fcp = fc_frame_payload_get(fp, len);
  105. memset(fcp, 0, len);
  106. fcp->resp.fr_status = se_cmd->scsi_status;
  107. len = se_cmd->scsi_sense_length;
  108. if (len) {
  109. fcp->resp.fr_flags |= FCP_SNS_LEN_VAL;
  110. fcp->ext.fr_sns_len = htonl(len);
  111. memcpy((fcp + 1), se_cmd->sense_buffer, len);
  112. }
  113. /*
  114. * Test underflow and overflow with one mask. Usually both are off.
  115. * Bidirectional commands are not handled yet.
  116. */
  117. if (se_cmd->se_cmd_flags & (SCF_OVERFLOW_BIT | SCF_UNDERFLOW_BIT)) {
  118. if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT)
  119. fcp->resp.fr_flags |= FCP_RESID_OVER;
  120. else
  121. fcp->resp.fr_flags |= FCP_RESID_UNDER;
  122. fcp->ext.fr_resid = cpu_to_be32(se_cmd->residual_count);
  123. }
  124. /*
  125. * Send response.
  126. */
  127. cmd->seq = fc_seq_start_next(cmd->seq);
  128. fc_fill_fc_hdr(fp, FC_RCTL_DD_CMD_STATUS, ep->did, ep->sid, FC_TYPE_FCP,
  129. FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ, 0);
  130. rc = fc_seq_send(lport, cmd->seq, fp);
  131. if (rc) {
  132. pr_info_ratelimited("%s: Failed to send response frame %p, "
  133. "xid <0x%x>\n", __func__, fp, ep->xid);
  134. /*
  135. * Generate a TASK_SET_FULL status to notify the initiator
  136. * to reduce it's queue_depth after the se_cmd response has
  137. * been re-queued by target-core.
  138. */
  139. se_cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
  140. return -ENOMEM;
  141. }
  142. fc_exch_done(cmd->seq);
  143. /*
  144. * Drop the extra ACK_KREF reference taken by target_submit_cmd()
  145. * ahead of ft_check_stop_free() -> transport_generic_free_cmd()
  146. * final se_cmd->cmd_kref put.
  147. */
  148. target_put_sess_cmd(&cmd->se_cmd);
  149. return 0;
  150. }
  151. /*
  152. * Send TX_RDY (transfer ready).
  153. */
  154. int ft_write_pending(struct se_cmd *se_cmd)
  155. {
  156. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  157. struct fc_frame *fp;
  158. struct fcp_txrdy *txrdy;
  159. struct fc_lport *lport;
  160. struct fc_exch *ep;
  161. struct fc_frame_header *fh;
  162. u32 f_ctl;
  163. ft_dump_cmd(cmd, __func__);
  164. if (cmd->aborted)
  165. return 0;
  166. ep = fc_seq_exch(cmd->seq);
  167. lport = ep->lp;
  168. fp = fc_frame_alloc(lport, sizeof(*txrdy));
  169. if (!fp)
  170. return -ENOMEM; /* Signal QUEUE_FULL */
  171. txrdy = fc_frame_payload_get(fp, sizeof(*txrdy));
  172. memset(txrdy, 0, sizeof(*txrdy));
  173. txrdy->ft_burst_len = htonl(se_cmd->data_length);
  174. cmd->seq = fc_seq_start_next(cmd->seq);
  175. fc_fill_fc_hdr(fp, FC_RCTL_DD_DATA_DESC, ep->did, ep->sid, FC_TYPE_FCP,
  176. FC_FC_EX_CTX | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  177. fh = fc_frame_header_get(fp);
  178. f_ctl = ntoh24(fh->fh_f_ctl);
  179. /* Only if it is 'Exchange Responder' */
  180. if (f_ctl & FC_FC_EX_CTX) {
  181. /* Target is 'exchange responder' and sending XFER_READY
  182. * to 'exchange initiator (initiator)'
  183. */
  184. if ((ep->xid <= lport->lro_xid) &&
  185. (fh->fh_r_ctl == FC_RCTL_DD_DATA_DESC)) {
  186. if ((se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
  187. lport->tt.ddp_target(lport, ep->xid,
  188. se_cmd->t_data_sg,
  189. se_cmd->t_data_nents))
  190. cmd->was_ddp_setup = 1;
  191. }
  192. }
  193. fc_seq_send(lport, cmd->seq, fp);
  194. return 0;
  195. }
  196. int ft_get_cmd_state(struct se_cmd *se_cmd)
  197. {
  198. return 0;
  199. }
  200. /*
  201. * FC sequence response handler for follow-on sequences (data) and aborts.
  202. */
  203. static void ft_recv_seq(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  204. {
  205. struct ft_cmd *cmd = arg;
  206. struct fc_frame_header *fh;
  207. if (IS_ERR(fp)) {
  208. /* XXX need to find cmd if queued */
  209. cmd->seq = NULL;
  210. cmd->aborted = true;
  211. return;
  212. }
  213. fh = fc_frame_header_get(fp);
  214. switch (fh->fh_r_ctl) {
  215. case FC_RCTL_DD_SOL_DATA: /* write data */
  216. ft_recv_write_data(cmd, fp);
  217. break;
  218. case FC_RCTL_DD_UNSOL_CTL: /* command */
  219. case FC_RCTL_DD_SOL_CTL: /* transfer ready */
  220. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  221. default:
  222. pr_debug("%s: unhandled frame r_ctl %x\n",
  223. __func__, fh->fh_r_ctl);
  224. ft_invl_hw_context(cmd);
  225. fc_frame_free(fp);
  226. transport_generic_free_cmd(&cmd->se_cmd, 0);
  227. break;
  228. }
  229. }
  230. /*
  231. * Send a FCP response including SCSI status and optional FCP rsp_code.
  232. * status is SAM_STAT_GOOD (zero) iff code is valid.
  233. * This is used in error cases, such as allocation failures.
  234. */
  235. static void ft_send_resp_status(struct fc_lport *lport,
  236. const struct fc_frame *rx_fp,
  237. u32 status, enum fcp_resp_rsp_codes code)
  238. {
  239. struct fc_frame *fp;
  240. struct fc_seq *sp;
  241. const struct fc_frame_header *fh;
  242. size_t len;
  243. struct fcp_resp_with_ext *fcp;
  244. struct fcp_resp_rsp_info *info;
  245. fh = fc_frame_header_get(rx_fp);
  246. pr_debug("FCP error response: did %x oxid %x status %x code %x\n",
  247. ntoh24(fh->fh_s_id), ntohs(fh->fh_ox_id), status, code);
  248. len = sizeof(*fcp);
  249. if (status == SAM_STAT_GOOD)
  250. len += sizeof(*info);
  251. fp = fc_frame_alloc(lport, len);
  252. if (!fp)
  253. return;
  254. fcp = fc_frame_payload_get(fp, len);
  255. memset(fcp, 0, len);
  256. fcp->resp.fr_status = status;
  257. if (status == SAM_STAT_GOOD) {
  258. fcp->ext.fr_rsp_len = htonl(sizeof(*info));
  259. fcp->resp.fr_flags |= FCP_RSP_LEN_VAL;
  260. info = (struct fcp_resp_rsp_info *)(fcp + 1);
  261. info->rsp_code = code;
  262. }
  263. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_DD_CMD_STATUS, 0);
  264. sp = fr_seq(fp);
  265. if (sp) {
  266. fc_seq_send(lport, sp, fp);
  267. fc_exch_done(sp);
  268. } else {
  269. lport->tt.frame_send(lport, fp);
  270. }
  271. }
  272. /*
  273. * Send error or task management response.
  274. */
  275. static void ft_send_resp_code(struct ft_cmd *cmd,
  276. enum fcp_resp_rsp_codes code)
  277. {
  278. ft_send_resp_status(cmd->sess->tport->lport,
  279. cmd->req_frame, SAM_STAT_GOOD, code);
  280. }
  281. /*
  282. * Send error or task management response.
  283. * Always frees the cmd and associated state.
  284. */
  285. static void ft_send_resp_code_and_free(struct ft_cmd *cmd,
  286. enum fcp_resp_rsp_codes code)
  287. {
  288. ft_send_resp_code(cmd, code);
  289. ft_free_cmd(cmd);
  290. }
  291. /*
  292. * Handle Task Management Request.
  293. */
  294. static void ft_send_tm(struct ft_cmd *cmd)
  295. {
  296. struct fcp_cmnd *fcp;
  297. int rc;
  298. u8 tm_func;
  299. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  300. switch (fcp->fc_tm_flags) {
  301. case FCP_TMF_LUN_RESET:
  302. tm_func = TMR_LUN_RESET;
  303. break;
  304. case FCP_TMF_TGT_RESET:
  305. tm_func = TMR_TARGET_WARM_RESET;
  306. break;
  307. case FCP_TMF_CLR_TASK_SET:
  308. tm_func = TMR_CLEAR_TASK_SET;
  309. break;
  310. case FCP_TMF_ABT_TASK_SET:
  311. tm_func = TMR_ABORT_TASK_SET;
  312. break;
  313. case FCP_TMF_CLR_ACA:
  314. tm_func = TMR_CLEAR_ACA;
  315. break;
  316. default:
  317. /*
  318. * FCP4r01 indicates having a combination of
  319. * tm_flags set is invalid.
  320. */
  321. pr_debug("invalid FCP tm_flags %x\n", fcp->fc_tm_flags);
  322. ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
  323. return;
  324. }
  325. /* FIXME: Add referenced task tag for ABORT_TASK */
  326. rc = target_submit_tmr(&cmd->se_cmd, cmd->sess->se_sess,
  327. &cmd->ft_sense_buffer[0], scsilun_to_int(&fcp->fc_lun),
  328. cmd, tm_func, GFP_KERNEL, 0, TARGET_SCF_ACK_KREF);
  329. if (rc < 0)
  330. ft_send_resp_code_and_free(cmd, FCP_TMF_FAILED);
  331. }
  332. /*
  333. * Send status from completed task management request.
  334. */
  335. void ft_queue_tm_resp(struct se_cmd *se_cmd)
  336. {
  337. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  338. struct se_tmr_req *tmr = se_cmd->se_tmr_req;
  339. enum fcp_resp_rsp_codes code;
  340. if (cmd->aborted)
  341. return;
  342. switch (tmr->response) {
  343. case TMR_FUNCTION_COMPLETE:
  344. code = FCP_TMF_CMPL;
  345. break;
  346. case TMR_LUN_DOES_NOT_EXIST:
  347. code = FCP_TMF_INVALID_LUN;
  348. break;
  349. case TMR_FUNCTION_REJECTED:
  350. code = FCP_TMF_REJECTED;
  351. break;
  352. case TMR_TASK_DOES_NOT_EXIST:
  353. case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
  354. default:
  355. code = FCP_TMF_FAILED;
  356. break;
  357. }
  358. pr_debug("tmr fn %d resp %d fcp code %d\n",
  359. tmr->function, tmr->response, code);
  360. ft_send_resp_code(cmd, code);
  361. /*
  362. * Drop the extra ACK_KREF reference taken by target_submit_tmr()
  363. * ahead of ft_check_stop_free() -> transport_generic_free_cmd()
  364. * final se_cmd->cmd_kref put.
  365. */
  366. target_put_sess_cmd(&cmd->se_cmd);
  367. }
  368. void ft_aborted_task(struct se_cmd *se_cmd)
  369. {
  370. return;
  371. }
  372. static void ft_send_work(struct work_struct *work);
  373. /*
  374. * Handle incoming FCP command.
  375. */
  376. static void ft_recv_cmd(struct ft_sess *sess, struct fc_frame *fp)
  377. {
  378. struct ft_cmd *cmd;
  379. struct fc_lport *lport = sess->tport->lport;
  380. struct se_session *se_sess = sess->se_sess;
  381. int tag, cpu;
  382. tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
  383. if (tag < 0)
  384. goto busy;
  385. cmd = &((struct ft_cmd *)se_sess->sess_cmd_map)[tag];
  386. memset(cmd, 0, sizeof(struct ft_cmd));
  387. cmd->se_cmd.map_tag = tag;
  388. cmd->se_cmd.map_cpu = cpu;
  389. cmd->sess = sess;
  390. cmd->seq = fc_seq_assign(lport, fp);
  391. if (!cmd->seq) {
  392. target_free_tag(se_sess, &cmd->se_cmd);
  393. goto busy;
  394. }
  395. cmd->req_frame = fp; /* hold frame during cmd */
  396. INIT_WORK(&cmd->work, ft_send_work);
  397. queue_work(sess->tport->tpg->workqueue, &cmd->work);
  398. return;
  399. busy:
  400. pr_debug("cmd or seq allocation failure - sending BUSY\n");
  401. ft_send_resp_status(lport, fp, SAM_STAT_BUSY, 0);
  402. fc_frame_free(fp);
  403. ft_sess_put(sess); /* undo get from lookup */
  404. }
  405. /*
  406. * Handle incoming FCP frame.
  407. * Caller has verified that the frame is type FCP.
  408. */
  409. void ft_recv_req(struct ft_sess *sess, struct fc_frame *fp)
  410. {
  411. struct fc_frame_header *fh = fc_frame_header_get(fp);
  412. switch (fh->fh_r_ctl) {
  413. case FC_RCTL_DD_UNSOL_CMD: /* command */
  414. ft_recv_cmd(sess, fp);
  415. break;
  416. case FC_RCTL_DD_SOL_DATA: /* write data */
  417. case FC_RCTL_DD_UNSOL_CTL:
  418. case FC_RCTL_DD_SOL_CTL:
  419. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  420. case FC_RCTL_ELS4_REQ: /* SRR, perhaps */
  421. default:
  422. pr_debug("%s: unhandled frame r_ctl %x\n",
  423. __func__, fh->fh_r_ctl);
  424. fc_frame_free(fp);
  425. ft_sess_put(sess); /* undo get from lookup */
  426. break;
  427. }
  428. }
  429. /*
  430. * Send new command to target.
  431. */
  432. static void ft_send_work(struct work_struct *work)
  433. {
  434. struct ft_cmd *cmd = container_of(work, struct ft_cmd, work);
  435. struct fc_frame_header *fh = fc_frame_header_get(cmd->req_frame);
  436. struct fcp_cmnd *fcp;
  437. int data_dir = 0;
  438. int task_attr;
  439. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  440. if (!fcp)
  441. goto err;
  442. if (fcp->fc_flags & FCP_CFL_LEN_MASK)
  443. goto err; /* not handling longer CDBs yet */
  444. /*
  445. * Check for FCP task management flags
  446. */
  447. if (fcp->fc_tm_flags) {
  448. ft_send_tm(cmd);
  449. return;
  450. }
  451. switch (fcp->fc_flags & (FCP_CFL_RDDATA | FCP_CFL_WRDATA)) {
  452. case 0:
  453. data_dir = DMA_NONE;
  454. break;
  455. case FCP_CFL_RDDATA:
  456. data_dir = DMA_FROM_DEVICE;
  457. break;
  458. case FCP_CFL_WRDATA:
  459. data_dir = DMA_TO_DEVICE;
  460. break;
  461. case FCP_CFL_WRDATA | FCP_CFL_RDDATA:
  462. goto err; /* TBD not supported by tcm_fc yet */
  463. }
  464. /*
  465. * Locate the SAM Task Attr from fc_pri_ta
  466. */
  467. switch (fcp->fc_pri_ta & FCP_PTA_MASK) {
  468. case FCP_PTA_HEADQ:
  469. task_attr = TCM_HEAD_TAG;
  470. break;
  471. case FCP_PTA_ORDERED:
  472. task_attr = TCM_ORDERED_TAG;
  473. break;
  474. case FCP_PTA_ACA:
  475. task_attr = TCM_ACA_TAG;
  476. break;
  477. case FCP_PTA_SIMPLE:
  478. default:
  479. task_attr = TCM_SIMPLE_TAG;
  480. }
  481. fc_seq_set_resp(cmd->seq, ft_recv_seq, cmd);
  482. cmd->se_cmd.tag = fc_seq_exch(cmd->seq)->rxid;
  483. /*
  484. * Use a single se_cmd->cmd_kref as we expect to release se_cmd
  485. * directly from ft_check_stop_free callback in response path.
  486. */
  487. if (target_init_cmd(&cmd->se_cmd, cmd->sess->se_sess,
  488. &cmd->ft_sense_buffer[0],
  489. scsilun_to_int(&fcp->fc_lun), ntohl(fcp->fc_dl),
  490. task_attr, data_dir, TARGET_SCF_ACK_KREF))
  491. goto err;
  492. if (target_submit_prep(&cmd->se_cmd, fcp->fc_cdb, NULL, 0, NULL, 0,
  493. NULL, 0, GFP_KERNEL))
  494. return;
  495. target_submit(&cmd->se_cmd);
  496. pr_debug("r_ctl %x target_submit_cmd %p\n", fh->fh_r_ctl, cmd);
  497. return;
  498. err:
  499. ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
  500. }