tfc_cmd.c 17 KB

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