tfc_cmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Copyright (c) 2010 Cisco Systems, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. /* XXX TBD some includes may be extraneous */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/version.h>
  21. #include <generated/utsrelease.h>
  22. #include <linux/utsname.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/kthread.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/configfs.h>
  29. #include <linux/ctype.h>
  30. #include <linux/hash.h>
  31. #include <asm/unaligned.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_device.h>
  35. #include <scsi/scsi_cmnd.h>
  36. #include <scsi/scsi_tcq.h>
  37. #include <scsi/libfc.h>
  38. #include <scsi/fc_encode.h>
  39. #include <target/target_core_base.h>
  40. #include <target/target_core_transport.h>
  41. #include <target/target_core_fabric_ops.h>
  42. #include <target/target_core_device.h>
  43. #include <target/target_core_tpg.h>
  44. #include <target/target_core_configfs.h>
  45. #include <target/target_core_tmr.h>
  46. #include <target/configfs_macros.h>
  47. #include "tcm_fc.h"
  48. /*
  49. * Dump cmd state for debugging.
  50. */
  51. void ft_dump_cmd(struct ft_cmd *cmd, const char *caller)
  52. {
  53. struct fc_exch *ep;
  54. struct fc_seq *sp;
  55. struct se_cmd *se_cmd;
  56. struct scatterlist *sg;
  57. int count;
  58. se_cmd = &cmd->se_cmd;
  59. pr_debug("%s: cmd %p state %d sess %p seq %p se_cmd %p\n",
  60. caller, cmd, cmd->state, cmd->sess, cmd->seq, se_cmd);
  61. pr_debug("%s: cmd %p cdb %p\n",
  62. caller, cmd, cmd->cdb);
  63. pr_debug("%s: cmd %p lun %d\n", caller, cmd, cmd->lun);
  64. pr_debug("%s: cmd %p data_nents %u len %u se_cmd_flags <0x%x>\n",
  65. caller, cmd, se_cmd->t_data_nents,
  66. se_cmd->data_length, se_cmd->se_cmd_flags);
  67. for_each_sg(se_cmd->t_data_sg, sg, se_cmd->t_data_nents, count)
  68. pr_debug("%s: cmd %p sg %p page %p "
  69. "len 0x%x off 0x%x\n",
  70. caller, cmd, sg,
  71. sg_page(sg), sg->length, sg->offset);
  72. sp = cmd->seq;
  73. if (sp) {
  74. ep = fc_seq_exch(sp);
  75. pr_debug("%s: cmd %p sid %x did %x "
  76. "ox_id %x rx_id %x seq_id %x e_stat %x\n",
  77. caller, cmd, ep->sid, ep->did, ep->oxid, ep->rxid,
  78. sp->id, ep->esb_stat);
  79. }
  80. print_hex_dump(KERN_INFO, "ft_dump_cmd ", DUMP_PREFIX_NONE,
  81. 16, 4, cmd->cdb, MAX_COMMAND_SIZE, 0);
  82. }
  83. static void ft_queue_cmd(struct ft_sess *sess, struct ft_cmd *cmd)
  84. {
  85. struct ft_tpg *tpg = sess->tport->tpg;
  86. struct se_queue_obj *qobj = &tpg->qobj;
  87. unsigned long flags;
  88. qobj = &sess->tport->tpg->qobj;
  89. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  90. list_add_tail(&cmd->se_req.qr_list, &qobj->qobj_list);
  91. atomic_inc(&qobj->queue_cnt);
  92. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  93. wake_up_process(tpg->thread);
  94. }
  95. static struct ft_cmd *ft_dequeue_cmd(struct se_queue_obj *qobj)
  96. {
  97. unsigned long flags;
  98. struct se_queue_req *qr;
  99. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  100. if (list_empty(&qobj->qobj_list)) {
  101. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  102. return NULL;
  103. }
  104. qr = list_first_entry(&qobj->qobj_list, struct se_queue_req, qr_list);
  105. list_del(&qr->qr_list);
  106. atomic_dec(&qobj->queue_cnt);
  107. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  108. return container_of(qr, struct ft_cmd, se_req);
  109. }
  110. static void ft_free_cmd(struct ft_cmd *cmd)
  111. {
  112. struct fc_frame *fp;
  113. struct fc_lport *lport;
  114. if (!cmd)
  115. return;
  116. fp = cmd->req_frame;
  117. lport = fr_dev(fp);
  118. if (fr_seq(fp))
  119. lport->tt.seq_release(fr_seq(fp));
  120. fc_frame_free(fp);
  121. ft_sess_put(cmd->sess); /* undo get from lookup at recv */
  122. kfree(cmd);
  123. }
  124. void ft_release_cmd(struct se_cmd *se_cmd)
  125. {
  126. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  127. ft_free_cmd(cmd);
  128. }
  129. void ft_check_stop_free(struct se_cmd *se_cmd)
  130. {
  131. transport_generic_free_cmd(se_cmd, 0, 0);
  132. }
  133. /*
  134. * Send response.
  135. */
  136. int ft_queue_status(struct se_cmd *se_cmd)
  137. {
  138. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  139. struct fc_frame *fp;
  140. struct fcp_resp_with_ext *fcp;
  141. struct fc_lport *lport;
  142. struct fc_exch *ep;
  143. size_t len;
  144. ft_dump_cmd(cmd, __func__);
  145. ep = fc_seq_exch(cmd->seq);
  146. lport = ep->lp;
  147. len = sizeof(*fcp) + se_cmd->scsi_sense_length;
  148. fp = fc_frame_alloc(lport, len);
  149. if (!fp) {
  150. /* XXX shouldn't just drop it - requeue and retry? */
  151. return 0;
  152. }
  153. fcp = fc_frame_payload_get(fp, len);
  154. memset(fcp, 0, len);
  155. fcp->resp.fr_status = se_cmd->scsi_status;
  156. len = se_cmd->scsi_sense_length;
  157. if (len) {
  158. fcp->resp.fr_flags |= FCP_SNS_LEN_VAL;
  159. fcp->ext.fr_sns_len = htonl(len);
  160. memcpy((fcp + 1), se_cmd->sense_buffer, len);
  161. }
  162. /*
  163. * Test underflow and overflow with one mask. Usually both are off.
  164. * Bidirectional commands are not handled yet.
  165. */
  166. if (se_cmd->se_cmd_flags & (SCF_OVERFLOW_BIT | SCF_UNDERFLOW_BIT)) {
  167. if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT)
  168. fcp->resp.fr_flags |= FCP_RESID_OVER;
  169. else
  170. fcp->resp.fr_flags |= FCP_RESID_UNDER;
  171. fcp->ext.fr_resid = cpu_to_be32(se_cmd->residual_count);
  172. }
  173. /*
  174. * Send response.
  175. */
  176. cmd->seq = lport->tt.seq_start_next(cmd->seq);
  177. fc_fill_fc_hdr(fp, FC_RCTL_DD_CMD_STATUS, ep->did, ep->sid, FC_TYPE_FCP,
  178. FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ, 0);
  179. lport->tt.seq_send(lport, cmd->seq, fp);
  180. lport->tt.exch_done(cmd->seq);
  181. return 0;
  182. }
  183. int ft_write_pending_status(struct se_cmd *se_cmd)
  184. {
  185. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  186. return cmd->write_data_len != se_cmd->data_length;
  187. }
  188. /*
  189. * Send TX_RDY (transfer ready).
  190. */
  191. int ft_write_pending(struct se_cmd *se_cmd)
  192. {
  193. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  194. struct fc_frame *fp;
  195. struct fcp_txrdy *txrdy;
  196. struct fc_lport *lport;
  197. struct fc_exch *ep;
  198. struct fc_frame_header *fh;
  199. u32 f_ctl;
  200. ft_dump_cmd(cmd, __func__);
  201. ep = fc_seq_exch(cmd->seq);
  202. lport = ep->lp;
  203. fp = fc_frame_alloc(lport, sizeof(*txrdy));
  204. if (!fp)
  205. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  206. txrdy = fc_frame_payload_get(fp, sizeof(*txrdy));
  207. memset(txrdy, 0, sizeof(*txrdy));
  208. txrdy->ft_burst_len = htonl(se_cmd->data_length);
  209. cmd->seq = lport->tt.seq_start_next(cmd->seq);
  210. fc_fill_fc_hdr(fp, FC_RCTL_DD_DATA_DESC, ep->did, ep->sid, FC_TYPE_FCP,
  211. FC_FC_EX_CTX | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  212. fh = fc_frame_header_get(fp);
  213. f_ctl = ntoh24(fh->fh_f_ctl);
  214. /* Only if it is 'Exchange Responder' */
  215. if (f_ctl & FC_FC_EX_CTX) {
  216. /* Target is 'exchange responder' and sending XFER_READY
  217. * to 'exchange initiator (initiator)'
  218. */
  219. if ((ep->xid <= lport->lro_xid) &&
  220. (fh->fh_r_ctl == FC_RCTL_DD_DATA_DESC)) {
  221. if (se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  222. /*
  223. * cmd may have been broken up into multiple
  224. * tasks. Link their sgs together so we can
  225. * operate on them all at once.
  226. */
  227. transport_do_task_sg_chain(se_cmd);
  228. cmd->sg = se_cmd->t_tasks_sg_chained;
  229. cmd->sg_cnt =
  230. se_cmd->t_tasks_sg_chained_no;
  231. }
  232. if (cmd->sg && lport->tt.ddp_target(lport, ep->xid,
  233. cmd->sg,
  234. cmd->sg_cnt))
  235. cmd->was_ddp_setup = 1;
  236. }
  237. }
  238. lport->tt.seq_send(lport, cmd->seq, fp);
  239. return 0;
  240. }
  241. u32 ft_get_task_tag(struct se_cmd *se_cmd)
  242. {
  243. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  244. return fc_seq_exch(cmd->seq)->rxid;
  245. }
  246. int ft_get_cmd_state(struct se_cmd *se_cmd)
  247. {
  248. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  249. return cmd->state;
  250. }
  251. int ft_is_state_remove(struct se_cmd *se_cmd)
  252. {
  253. return 0; /* XXX TBD */
  254. }
  255. /*
  256. * FC sequence response handler for follow-on sequences (data) and aborts.
  257. */
  258. static void ft_recv_seq(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  259. {
  260. struct ft_cmd *cmd = arg;
  261. struct fc_frame_header *fh;
  262. if (IS_ERR(fp)) {
  263. /* XXX need to find cmd if queued */
  264. cmd->se_cmd.t_state = TRANSPORT_REMOVE;
  265. cmd->seq = NULL;
  266. transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
  267. return;
  268. }
  269. fh = fc_frame_header_get(fp);
  270. switch (fh->fh_r_ctl) {
  271. case FC_RCTL_DD_SOL_DATA: /* write data */
  272. ft_recv_write_data(cmd, fp);
  273. break;
  274. case FC_RCTL_DD_UNSOL_CTL: /* command */
  275. case FC_RCTL_DD_SOL_CTL: /* transfer ready */
  276. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  277. default:
  278. pr_debug("%s: unhandled frame r_ctl %x\n",
  279. __func__, fh->fh_r_ctl);
  280. ft_invl_hw_context(cmd);
  281. fc_frame_free(fp);
  282. transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
  283. break;
  284. }
  285. }
  286. /*
  287. * Send a FCP response including SCSI status and optional FCP rsp_code.
  288. * status is SAM_STAT_GOOD (zero) iff code is valid.
  289. * This is used in error cases, such as allocation failures.
  290. */
  291. static void ft_send_resp_status(struct fc_lport *lport,
  292. const struct fc_frame *rx_fp,
  293. u32 status, enum fcp_resp_rsp_codes code)
  294. {
  295. struct fc_frame *fp;
  296. struct fc_seq *sp;
  297. const struct fc_frame_header *fh;
  298. size_t len;
  299. struct fcp_resp_with_ext *fcp;
  300. struct fcp_resp_rsp_info *info;
  301. fh = fc_frame_header_get(rx_fp);
  302. pr_debug("FCP error response: did %x oxid %x status %x code %x\n",
  303. ntoh24(fh->fh_s_id), ntohs(fh->fh_ox_id), status, code);
  304. len = sizeof(*fcp);
  305. if (status == SAM_STAT_GOOD)
  306. len += sizeof(*info);
  307. fp = fc_frame_alloc(lport, len);
  308. if (!fp)
  309. return;
  310. fcp = fc_frame_payload_get(fp, len);
  311. memset(fcp, 0, len);
  312. fcp->resp.fr_status = status;
  313. if (status == SAM_STAT_GOOD) {
  314. fcp->ext.fr_rsp_len = htonl(sizeof(*info));
  315. fcp->resp.fr_flags |= FCP_RSP_LEN_VAL;
  316. info = (struct fcp_resp_rsp_info *)(fcp + 1);
  317. info->rsp_code = code;
  318. }
  319. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_DD_CMD_STATUS, 0);
  320. sp = fr_seq(fp);
  321. if (sp)
  322. lport->tt.seq_send(lport, sp, fp);
  323. else
  324. lport->tt.frame_send(lport, fp);
  325. }
  326. /*
  327. * Send error or task management response.
  328. */
  329. static void ft_send_resp_code(struct ft_cmd *cmd,
  330. enum fcp_resp_rsp_codes code)
  331. {
  332. ft_send_resp_status(cmd->sess->tport->lport,
  333. cmd->req_frame, SAM_STAT_GOOD, code);
  334. }
  335. /*
  336. * Send error or task management response.
  337. * Always frees the cmd and associated state.
  338. */
  339. static void ft_send_resp_code_and_free(struct ft_cmd *cmd,
  340. enum fcp_resp_rsp_codes code)
  341. {
  342. ft_send_resp_code(cmd, code);
  343. ft_free_cmd(cmd);
  344. }
  345. /*
  346. * Handle Task Management Request.
  347. */
  348. static void ft_send_tm(struct ft_cmd *cmd)
  349. {
  350. struct se_tmr_req *tmr;
  351. struct fcp_cmnd *fcp;
  352. struct ft_sess *sess;
  353. u8 tm_func;
  354. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  355. switch (fcp->fc_tm_flags) {
  356. case FCP_TMF_LUN_RESET:
  357. tm_func = TMR_LUN_RESET;
  358. break;
  359. case FCP_TMF_TGT_RESET:
  360. tm_func = TMR_TARGET_WARM_RESET;
  361. break;
  362. case FCP_TMF_CLR_TASK_SET:
  363. tm_func = TMR_CLEAR_TASK_SET;
  364. break;
  365. case FCP_TMF_ABT_TASK_SET:
  366. tm_func = TMR_ABORT_TASK_SET;
  367. break;
  368. case FCP_TMF_CLR_ACA:
  369. tm_func = TMR_CLEAR_ACA;
  370. break;
  371. default:
  372. /*
  373. * FCP4r01 indicates having a combination of
  374. * tm_flags set is invalid.
  375. */
  376. pr_debug("invalid FCP tm_flags %x\n", fcp->fc_tm_flags);
  377. ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
  378. return;
  379. }
  380. pr_debug("alloc tm cmd fn %d\n", tm_func);
  381. tmr = core_tmr_alloc_req(&cmd->se_cmd, cmd, tm_func);
  382. if (!tmr) {
  383. pr_debug("alloc failed\n");
  384. ft_send_resp_code_and_free(cmd, FCP_TMF_FAILED);
  385. return;
  386. }
  387. cmd->se_cmd.se_tmr_req = tmr;
  388. switch (fcp->fc_tm_flags) {
  389. case FCP_TMF_LUN_RESET:
  390. cmd->lun = scsilun_to_int((struct scsi_lun *)fcp->fc_lun);
  391. if (transport_lookup_tmr_lun(&cmd->se_cmd, cmd->lun) < 0) {
  392. /*
  393. * Make sure to clean up newly allocated TMR request
  394. * since "unable to handle TMR request because failed
  395. * to get to LUN"
  396. */
  397. pr_debug("Failed to get LUN for TMR func %d, "
  398. "se_cmd %p, unpacked_lun %d\n",
  399. tm_func, &cmd->se_cmd, cmd->lun);
  400. ft_dump_cmd(cmd, __func__);
  401. sess = cmd->sess;
  402. transport_send_check_condition_and_sense(&cmd->se_cmd,
  403. cmd->se_cmd.scsi_sense_reason, 0);
  404. transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
  405. ft_sess_put(sess);
  406. return;
  407. }
  408. break;
  409. case FCP_TMF_TGT_RESET:
  410. case FCP_TMF_CLR_TASK_SET:
  411. case FCP_TMF_ABT_TASK_SET:
  412. case FCP_TMF_CLR_ACA:
  413. break;
  414. default:
  415. return;
  416. }
  417. transport_generic_handle_tmr(&cmd->se_cmd);
  418. }
  419. /*
  420. * Send status from completed task management request.
  421. */
  422. int ft_queue_tm_resp(struct se_cmd *se_cmd)
  423. {
  424. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  425. struct se_tmr_req *tmr = se_cmd->se_tmr_req;
  426. enum fcp_resp_rsp_codes code;
  427. switch (tmr->response) {
  428. case TMR_FUNCTION_COMPLETE:
  429. code = FCP_TMF_CMPL;
  430. break;
  431. case TMR_LUN_DOES_NOT_EXIST:
  432. code = FCP_TMF_INVALID_LUN;
  433. break;
  434. case TMR_FUNCTION_REJECTED:
  435. code = FCP_TMF_REJECTED;
  436. break;
  437. case TMR_TASK_DOES_NOT_EXIST:
  438. case TMR_TASK_STILL_ALLEGIANT:
  439. case TMR_TASK_FAILOVER_NOT_SUPPORTED:
  440. case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
  441. case TMR_FUNCTION_AUTHORIZATION_FAILED:
  442. default:
  443. code = FCP_TMF_FAILED;
  444. break;
  445. }
  446. pr_debug("tmr fn %d resp %d fcp code %d\n",
  447. tmr->function, tmr->response, code);
  448. ft_send_resp_code(cmd, code);
  449. return 0;
  450. }
  451. /*
  452. * Handle incoming FCP command.
  453. */
  454. static void ft_recv_cmd(struct ft_sess *sess, struct fc_frame *fp)
  455. {
  456. struct ft_cmd *cmd;
  457. struct fc_lport *lport = sess->tport->lport;
  458. cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
  459. if (!cmd)
  460. goto busy;
  461. cmd->sess = sess;
  462. cmd->seq = lport->tt.seq_assign(lport, fp);
  463. if (!cmd->seq) {
  464. kfree(cmd);
  465. goto busy;
  466. }
  467. cmd->req_frame = fp; /* hold frame during cmd */
  468. ft_queue_cmd(sess, cmd);
  469. return;
  470. busy:
  471. pr_debug("cmd or seq allocation failure - sending BUSY\n");
  472. ft_send_resp_status(lport, fp, SAM_STAT_BUSY, 0);
  473. fc_frame_free(fp);
  474. ft_sess_put(sess); /* undo get from lookup */
  475. }
  476. /*
  477. * Handle incoming FCP frame.
  478. * Caller has verified that the frame is type FCP.
  479. */
  480. void ft_recv_req(struct ft_sess *sess, struct fc_frame *fp)
  481. {
  482. struct fc_frame_header *fh = fc_frame_header_get(fp);
  483. switch (fh->fh_r_ctl) {
  484. case FC_RCTL_DD_UNSOL_CMD: /* command */
  485. ft_recv_cmd(sess, fp);
  486. break;
  487. case FC_RCTL_DD_SOL_DATA: /* write data */
  488. case FC_RCTL_DD_UNSOL_CTL:
  489. case FC_RCTL_DD_SOL_CTL:
  490. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  491. case FC_RCTL_ELS4_REQ: /* SRR, perhaps */
  492. default:
  493. pr_debug("%s: unhandled frame r_ctl %x\n",
  494. __func__, fh->fh_r_ctl);
  495. fc_frame_free(fp);
  496. ft_sess_put(sess); /* undo get from lookup */
  497. break;
  498. }
  499. }
  500. /*
  501. * Send new command to target.
  502. */
  503. static void ft_send_cmd(struct ft_cmd *cmd)
  504. {
  505. struct fc_frame_header *fh = fc_frame_header_get(cmd->req_frame);
  506. struct se_cmd *se_cmd;
  507. struct fcp_cmnd *fcp;
  508. int data_dir;
  509. u32 data_len;
  510. int task_attr;
  511. int ret;
  512. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  513. if (!fcp)
  514. goto err;
  515. if (fcp->fc_flags & FCP_CFL_LEN_MASK)
  516. goto err; /* not handling longer CDBs yet */
  517. if (fcp->fc_tm_flags) {
  518. task_attr = FCP_PTA_SIMPLE;
  519. data_dir = DMA_NONE;
  520. data_len = 0;
  521. } else {
  522. switch (fcp->fc_flags & (FCP_CFL_RDDATA | FCP_CFL_WRDATA)) {
  523. case 0:
  524. data_dir = DMA_NONE;
  525. break;
  526. case FCP_CFL_RDDATA:
  527. data_dir = DMA_FROM_DEVICE;
  528. break;
  529. case FCP_CFL_WRDATA:
  530. data_dir = DMA_TO_DEVICE;
  531. break;
  532. case FCP_CFL_WRDATA | FCP_CFL_RDDATA:
  533. goto err; /* TBD not supported by tcm_fc yet */
  534. }
  535. /*
  536. * Locate the SAM Task Attr from fc_pri_ta
  537. */
  538. switch (fcp->fc_pri_ta & FCP_PTA_MASK) {
  539. case FCP_PTA_HEADQ:
  540. task_attr = MSG_HEAD_TAG;
  541. break;
  542. case FCP_PTA_ORDERED:
  543. task_attr = MSG_ORDERED_TAG;
  544. break;
  545. case FCP_PTA_ACA:
  546. task_attr = MSG_ACA_TAG;
  547. break;
  548. case FCP_PTA_SIMPLE: /* Fallthrough */
  549. default:
  550. task_attr = MSG_SIMPLE_TAG;
  551. }
  552. task_attr = fcp->fc_pri_ta & FCP_PTA_MASK;
  553. data_len = ntohl(fcp->fc_dl);
  554. cmd->cdb = fcp->fc_cdb;
  555. }
  556. se_cmd = &cmd->se_cmd;
  557. /*
  558. * Initialize struct se_cmd descriptor from target_core_mod
  559. * infrastructure
  560. */
  561. transport_init_se_cmd(se_cmd, &ft_configfs->tf_ops, cmd->sess->se_sess,
  562. data_len, data_dir, task_attr,
  563. &cmd->ft_sense_buffer[0]);
  564. /*
  565. * Check for FCP task management flags
  566. */
  567. if (fcp->fc_tm_flags) {
  568. ft_send_tm(cmd);
  569. return;
  570. }
  571. fc_seq_exch(cmd->seq)->lp->tt.seq_set_resp(cmd->seq, ft_recv_seq, cmd);
  572. cmd->lun = scsilun_to_int((struct scsi_lun *)fcp->fc_lun);
  573. ret = transport_lookup_cmd_lun(&cmd->se_cmd, cmd->lun);
  574. if (ret < 0) {
  575. ft_dump_cmd(cmd, __func__);
  576. transport_send_check_condition_and_sense(&cmd->se_cmd,
  577. cmd->se_cmd.scsi_sense_reason, 0);
  578. return;
  579. }
  580. ret = transport_generic_allocate_tasks(se_cmd, cmd->cdb);
  581. pr_debug("r_ctl %x alloc task ret %d\n", fh->fh_r_ctl, ret);
  582. ft_dump_cmd(cmd, __func__);
  583. if (ret == -ENOMEM) {
  584. transport_send_check_condition_and_sense(se_cmd,
  585. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  586. transport_generic_free_cmd(se_cmd, 0, 0);
  587. return;
  588. }
  589. if (ret == -EINVAL) {
  590. if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
  591. ft_queue_status(se_cmd);
  592. else
  593. transport_send_check_condition_and_sense(se_cmd,
  594. se_cmd->scsi_sense_reason, 0);
  595. transport_generic_free_cmd(se_cmd, 0, 0);
  596. return;
  597. }
  598. transport_generic_handle_cdb(se_cmd);
  599. return;
  600. err:
  601. ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
  602. }
  603. /*
  604. * Handle request in the command thread.
  605. */
  606. static void ft_exec_req(struct ft_cmd *cmd)
  607. {
  608. pr_debug("cmd state %x\n", cmd->state);
  609. switch (cmd->state) {
  610. case FC_CMD_ST_NEW:
  611. ft_send_cmd(cmd);
  612. break;
  613. default:
  614. break;
  615. }
  616. }
  617. /*
  618. * Processing thread.
  619. * Currently one thread per tpg.
  620. */
  621. int ft_thread(void *arg)
  622. {
  623. struct ft_tpg *tpg = arg;
  624. struct se_queue_obj *qobj = &tpg->qobj;
  625. struct ft_cmd *cmd;
  626. while (!kthread_should_stop()) {
  627. schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT);
  628. if (kthread_should_stop())
  629. goto out;
  630. cmd = ft_dequeue_cmd(qobj);
  631. if (cmd)
  632. ft_exec_req(cmd);
  633. }
  634. out:
  635. return 0;
  636. }