tfc_cmd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 <linux/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. /*
  197. * FC sequence response handler for follow-on sequences (data) and aborts.
  198. */
  199. static void ft_recv_seq(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  200. {
  201. struct ft_cmd *cmd = arg;
  202. struct fc_frame_header *fh;
  203. if (IS_ERR(fp)) {
  204. /* XXX need to find cmd if queued */
  205. cmd->seq = NULL;
  206. cmd->aborted = true;
  207. return;
  208. }
  209. fh = fc_frame_header_get(fp);
  210. switch (fh->fh_r_ctl) {
  211. case FC_RCTL_DD_SOL_DATA: /* write data */
  212. ft_recv_write_data(cmd, fp);
  213. break;
  214. case FC_RCTL_DD_UNSOL_CTL: /* command */
  215. case FC_RCTL_DD_SOL_CTL: /* transfer ready */
  216. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  217. default:
  218. pr_debug("%s: unhandled frame r_ctl %x\n",
  219. __func__, fh->fh_r_ctl);
  220. ft_invl_hw_context(cmd);
  221. fc_frame_free(fp);
  222. transport_generic_free_cmd(&cmd->se_cmd, 0);
  223. break;
  224. }
  225. }
  226. /*
  227. * Send a FCP response including SCSI status and optional FCP rsp_code.
  228. * status is SAM_STAT_GOOD (zero) iff code is valid.
  229. * This is used in error cases, such as allocation failures.
  230. */
  231. static void ft_send_resp_status(struct fc_lport *lport,
  232. const struct fc_frame *rx_fp,
  233. u32 status, enum fcp_resp_rsp_codes code)
  234. {
  235. struct fc_frame *fp;
  236. struct fc_seq *sp;
  237. const struct fc_frame_header *fh;
  238. size_t len;
  239. struct fcp_resp_with_ext *fcp;
  240. struct fcp_resp_rsp_info *info;
  241. fh = fc_frame_header_get(rx_fp);
  242. pr_debug("FCP error response: did %x oxid %x status %x code %x\n",
  243. ntoh24(fh->fh_s_id), ntohs(fh->fh_ox_id), status, code);
  244. len = sizeof(*fcp);
  245. if (status == SAM_STAT_GOOD)
  246. len += sizeof(*info);
  247. fp = fc_frame_alloc(lport, len);
  248. if (!fp)
  249. return;
  250. fcp = fc_frame_payload_get(fp, len);
  251. memset(fcp, 0, len);
  252. fcp->resp.fr_status = status;
  253. if (status == SAM_STAT_GOOD) {
  254. fcp->ext.fr_rsp_len = htonl(sizeof(*info));
  255. fcp->resp.fr_flags |= FCP_RSP_LEN_VAL;
  256. info = (struct fcp_resp_rsp_info *)(fcp + 1);
  257. info->rsp_code = code;
  258. }
  259. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_DD_CMD_STATUS, 0);
  260. sp = fr_seq(fp);
  261. if (sp) {
  262. fc_seq_send(lport, sp, fp);
  263. fc_exch_done(sp);
  264. } else {
  265. lport->tt.frame_send(lport, fp);
  266. }
  267. }
  268. /*
  269. * Send error or task management response.
  270. */
  271. static void ft_send_resp_code(struct ft_cmd *cmd,
  272. enum fcp_resp_rsp_codes code)
  273. {
  274. ft_send_resp_status(cmd->sess->tport->lport,
  275. cmd->req_frame, SAM_STAT_GOOD, code);
  276. }
  277. /*
  278. * Send error or task management response.
  279. * Always frees the cmd and associated state.
  280. */
  281. static void ft_send_resp_code_and_free(struct ft_cmd *cmd,
  282. enum fcp_resp_rsp_codes code)
  283. {
  284. ft_send_resp_code(cmd, code);
  285. ft_free_cmd(cmd);
  286. }
  287. /*
  288. * Handle Task Management Request.
  289. */
  290. static void ft_send_tm(struct ft_cmd *cmd)
  291. {
  292. struct fcp_cmnd *fcp;
  293. int rc;
  294. u8 tm_func;
  295. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  296. switch (fcp->fc_tm_flags) {
  297. case FCP_TMF_LUN_RESET:
  298. tm_func = TMR_LUN_RESET;
  299. break;
  300. case FCP_TMF_TGT_RESET:
  301. tm_func = TMR_TARGET_WARM_RESET;
  302. break;
  303. case FCP_TMF_CLR_TASK_SET:
  304. tm_func = TMR_CLEAR_TASK_SET;
  305. break;
  306. case FCP_TMF_ABT_TASK_SET:
  307. tm_func = TMR_ABORT_TASK_SET;
  308. break;
  309. case FCP_TMF_CLR_ACA:
  310. tm_func = TMR_CLEAR_ACA;
  311. break;
  312. default:
  313. /*
  314. * FCP4r01 indicates having a combination of
  315. * tm_flags set is invalid.
  316. */
  317. pr_debug("invalid FCP tm_flags %x\n", fcp->fc_tm_flags);
  318. ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
  319. return;
  320. }
  321. /* FIXME: Add referenced task tag for ABORT_TASK */
  322. rc = target_submit_tmr(&cmd->se_cmd, cmd->sess->se_sess,
  323. &cmd->ft_sense_buffer[0], scsilun_to_int(&fcp->fc_lun),
  324. cmd, tm_func, GFP_KERNEL, 0, TARGET_SCF_ACK_KREF);
  325. if (rc < 0)
  326. ft_send_resp_code_and_free(cmd, FCP_TMF_FAILED);
  327. }
  328. /*
  329. * Send status from completed task management request.
  330. */
  331. void ft_queue_tm_resp(struct se_cmd *se_cmd)
  332. {
  333. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  334. struct se_tmr_req *tmr = se_cmd->se_tmr_req;
  335. enum fcp_resp_rsp_codes code;
  336. if (cmd->aborted)
  337. return;
  338. switch (tmr->response) {
  339. case TMR_FUNCTION_COMPLETE:
  340. code = FCP_TMF_CMPL;
  341. break;
  342. case TMR_LUN_DOES_NOT_EXIST:
  343. code = FCP_TMF_INVALID_LUN;
  344. break;
  345. case TMR_FUNCTION_REJECTED:
  346. code = FCP_TMF_REJECTED;
  347. break;
  348. case TMR_TASK_DOES_NOT_EXIST:
  349. case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
  350. default:
  351. code = FCP_TMF_FAILED;
  352. break;
  353. }
  354. pr_debug("tmr fn %d resp %d fcp code %d\n",
  355. tmr->function, tmr->response, code);
  356. ft_send_resp_code(cmd, code);
  357. /*
  358. * Drop the extra ACK_KREF reference taken by target_submit_tmr()
  359. * ahead of ft_check_stop_free() -> transport_generic_free_cmd()
  360. * final se_cmd->cmd_kref put.
  361. */
  362. target_put_sess_cmd(&cmd->se_cmd);
  363. }
  364. void ft_aborted_task(struct se_cmd *se_cmd)
  365. {
  366. return;
  367. }
  368. static void ft_send_work(struct work_struct *work);
  369. /*
  370. * Handle incoming FCP command.
  371. */
  372. static void ft_recv_cmd(struct ft_sess *sess, struct fc_frame *fp)
  373. {
  374. struct ft_cmd *cmd;
  375. struct fc_lport *lport = sess->tport->lport;
  376. struct se_session *se_sess = sess->se_sess;
  377. int tag, cpu;
  378. tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
  379. if (tag < 0)
  380. goto busy;
  381. cmd = &((struct ft_cmd *)se_sess->sess_cmd_map)[tag];
  382. memset(cmd, 0, sizeof(struct ft_cmd));
  383. cmd->se_cmd.map_tag = tag;
  384. cmd->se_cmd.map_cpu = cpu;
  385. cmd->sess = sess;
  386. cmd->seq = fc_seq_assign(lport, fp);
  387. if (!cmd->seq) {
  388. target_free_tag(se_sess, &cmd->se_cmd);
  389. goto busy;
  390. }
  391. cmd->req_frame = fp; /* hold frame during cmd */
  392. INIT_WORK(&cmd->work, ft_send_work);
  393. queue_work(sess->tport->tpg->workqueue, &cmd->work);
  394. return;
  395. busy:
  396. pr_debug("cmd or seq allocation failure - sending BUSY\n");
  397. ft_send_resp_status(lport, fp, SAM_STAT_BUSY, 0);
  398. fc_frame_free(fp);
  399. ft_sess_put(sess); /* undo get from lookup */
  400. }
  401. /*
  402. * Handle incoming FCP frame.
  403. * Caller has verified that the frame is type FCP.
  404. */
  405. void ft_recv_req(struct ft_sess *sess, struct fc_frame *fp)
  406. {
  407. struct fc_frame_header *fh = fc_frame_header_get(fp);
  408. switch (fh->fh_r_ctl) {
  409. case FC_RCTL_DD_UNSOL_CMD: /* command */
  410. ft_recv_cmd(sess, fp);
  411. break;
  412. case FC_RCTL_DD_SOL_DATA: /* write data */
  413. case FC_RCTL_DD_UNSOL_CTL:
  414. case FC_RCTL_DD_SOL_CTL:
  415. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  416. case FC_RCTL_ELS4_REQ: /* SRR, perhaps */
  417. default:
  418. pr_debug("%s: unhandled frame r_ctl %x\n",
  419. __func__, fh->fh_r_ctl);
  420. fc_frame_free(fp);
  421. ft_sess_put(sess); /* undo get from lookup */
  422. break;
  423. }
  424. }
  425. /*
  426. * Send new command to target.
  427. */
  428. static void ft_send_work(struct work_struct *work)
  429. {
  430. struct ft_cmd *cmd = container_of(work, struct ft_cmd, work);
  431. struct fc_frame_header *fh = fc_frame_header_get(cmd->req_frame);
  432. struct fcp_cmnd *fcp;
  433. int data_dir = 0;
  434. int task_attr;
  435. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  436. if (!fcp)
  437. goto err;
  438. if (fcp->fc_flags & FCP_CFL_LEN_MASK)
  439. goto err; /* not handling longer CDBs yet */
  440. /*
  441. * Check for FCP task management flags
  442. */
  443. if (fcp->fc_tm_flags) {
  444. ft_send_tm(cmd);
  445. return;
  446. }
  447. switch (fcp->fc_flags & (FCP_CFL_RDDATA | FCP_CFL_WRDATA)) {
  448. case 0:
  449. data_dir = DMA_NONE;
  450. break;
  451. case FCP_CFL_RDDATA:
  452. data_dir = DMA_FROM_DEVICE;
  453. break;
  454. case FCP_CFL_WRDATA:
  455. data_dir = DMA_TO_DEVICE;
  456. break;
  457. case FCP_CFL_WRDATA | FCP_CFL_RDDATA:
  458. goto err; /* TBD not supported by tcm_fc yet */
  459. }
  460. /*
  461. * Locate the SAM Task Attr from fc_pri_ta
  462. */
  463. switch (fcp->fc_pri_ta & FCP_PTA_MASK) {
  464. case FCP_PTA_HEADQ:
  465. task_attr = TCM_HEAD_TAG;
  466. break;
  467. case FCP_PTA_ORDERED:
  468. task_attr = TCM_ORDERED_TAG;
  469. break;
  470. case FCP_PTA_ACA:
  471. task_attr = TCM_ACA_TAG;
  472. break;
  473. case FCP_PTA_SIMPLE:
  474. default:
  475. task_attr = TCM_SIMPLE_TAG;
  476. }
  477. fc_seq_set_resp(cmd->seq, ft_recv_seq, cmd);
  478. cmd->se_cmd.tag = fc_seq_exch(cmd->seq)->rxid;
  479. /*
  480. * Use a single se_cmd->cmd_kref as we expect to release se_cmd
  481. * directly from ft_check_stop_free callback in response path.
  482. */
  483. if (target_init_cmd(&cmd->se_cmd, cmd->sess->se_sess,
  484. &cmd->ft_sense_buffer[0],
  485. scsilun_to_int(&fcp->fc_lun), ntohl(fcp->fc_dl),
  486. task_attr, data_dir, TARGET_SCF_ACK_KREF))
  487. goto err;
  488. if (target_submit_prep(&cmd->se_cmd, fcp->fc_cdb, NULL, 0, NULL, 0,
  489. NULL, 0, GFP_KERNEL))
  490. return;
  491. target_submit(&cmd->se_cmd);
  492. pr_debug("r_ctl %x target_submit_cmd %p\n", fh->fh_r_ctl, cmd);
  493. return;
  494. err:
  495. ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
  496. }