tfc_cmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 se_mem *mem;
  58. if (!(ft_debug_logging & FT_DEBUG_IO))
  59. return;
  60. se_cmd = &cmd->se_cmd;
  61. printk(KERN_INFO "%s: cmd %p state %d sess %p seq %p se_cmd %p\n",
  62. caller, cmd, cmd->state, cmd->sess, cmd->seq, se_cmd);
  63. printk(KERN_INFO "%s: cmd %p cdb %p\n",
  64. caller, cmd, cmd->cdb);
  65. printk(KERN_INFO "%s: cmd %p lun %d\n", caller, cmd, cmd->lun);
  66. printk(KERN_INFO "%s: cmd %p se_num %u buf %p len %u se_cmd_flags <0x%x>\n",
  67. caller, cmd, se_cmd->t_tasks_se_num,
  68. se_cmd->t_task_buf, se_cmd->data_length, se_cmd->se_cmd_flags);
  69. list_for_each_entry(mem, &se_cmd->t_mem_list, se_list)
  70. printk(KERN_INFO "%s: cmd %p mem %p page %p "
  71. "len 0x%x off 0x%x\n",
  72. caller, cmd, mem,
  73. mem->se_page, mem->se_len, mem->se_off);
  74. sp = cmd->seq;
  75. if (sp) {
  76. ep = fc_seq_exch(sp);
  77. printk(KERN_INFO "%s: cmd %p sid %x did %x "
  78. "ox_id %x rx_id %x seq_id %x e_stat %x\n",
  79. caller, cmd, ep->sid, ep->did, ep->oxid, ep->rxid,
  80. sp->id, ep->esb_stat);
  81. }
  82. print_hex_dump(KERN_INFO, "ft_dump_cmd ", DUMP_PREFIX_NONE,
  83. 16, 4, cmd->cdb, MAX_COMMAND_SIZE, 0);
  84. }
  85. static void ft_queue_cmd(struct ft_sess *sess, struct ft_cmd *cmd)
  86. {
  87. struct se_queue_obj *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. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  93. atomic_inc(&qobj->queue_cnt);
  94. wake_up_interruptible(&qobj->thread_wq);
  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. * Map se_mem list to scatterlist, so that
  225. * DDP can be setup. DDP setup function require
  226. * scatterlist. se_mem_list is internal to
  227. * TCM/LIO target
  228. */
  229. transport_do_task_sg_chain(se_cmd);
  230. cmd->sg = se_cmd->t_tasks_sg_chained;
  231. cmd->sg_cnt =
  232. se_cmd->t_tasks_sg_chained_no;
  233. }
  234. if (cmd->sg && lport->tt.ddp_setup(lport, ep->xid,
  235. cmd->sg, cmd->sg_cnt))
  236. cmd->was_ddp_setup = 1;
  237. }
  238. }
  239. lport->tt.seq_send(lport, cmd->seq, fp);
  240. return 0;
  241. }
  242. u32 ft_get_task_tag(struct se_cmd *se_cmd)
  243. {
  244. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  245. return fc_seq_exch(cmd->seq)->rxid;
  246. }
  247. int ft_get_cmd_state(struct se_cmd *se_cmd)
  248. {
  249. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  250. return cmd->state;
  251. }
  252. int ft_is_state_remove(struct se_cmd *se_cmd)
  253. {
  254. return 0; /* XXX TBD */
  255. }
  256. /*
  257. * FC sequence response handler for follow-on sequences (data) and aborts.
  258. */
  259. static void ft_recv_seq(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  260. {
  261. struct ft_cmd *cmd = arg;
  262. struct fc_frame_header *fh;
  263. if (IS_ERR(fp)) {
  264. /* XXX need to find cmd if queued */
  265. cmd->se_cmd.t_state = TRANSPORT_REMOVE;
  266. cmd->seq = NULL;
  267. transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
  268. return;
  269. }
  270. fh = fc_frame_header_get(fp);
  271. switch (fh->fh_r_ctl) {
  272. case FC_RCTL_DD_SOL_DATA: /* write data */
  273. ft_recv_write_data(cmd, fp);
  274. break;
  275. case FC_RCTL_DD_UNSOL_CTL: /* command */
  276. case FC_RCTL_DD_SOL_CTL: /* transfer ready */
  277. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  278. default:
  279. printk(KERN_INFO "%s: unhandled frame r_ctl %x\n",
  280. __func__, fh->fh_r_ctl);
  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. FT_IO_DBG("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. * Always frees the cmd and associated state.
  329. */
  330. static void ft_send_resp_code(struct ft_cmd *cmd, 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. ft_free_cmd(cmd);
  335. }
  336. /*
  337. * Handle Task Management Request.
  338. */
  339. static void ft_send_tm(struct ft_cmd *cmd)
  340. {
  341. struct se_tmr_req *tmr;
  342. struct fcp_cmnd *fcp;
  343. struct ft_sess *sess;
  344. u8 tm_func;
  345. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  346. switch (fcp->fc_tm_flags) {
  347. case FCP_TMF_LUN_RESET:
  348. tm_func = TMR_LUN_RESET;
  349. break;
  350. case FCP_TMF_TGT_RESET:
  351. tm_func = TMR_TARGET_WARM_RESET;
  352. break;
  353. case FCP_TMF_CLR_TASK_SET:
  354. tm_func = TMR_CLEAR_TASK_SET;
  355. break;
  356. case FCP_TMF_ABT_TASK_SET:
  357. tm_func = TMR_ABORT_TASK_SET;
  358. break;
  359. case FCP_TMF_CLR_ACA:
  360. tm_func = TMR_CLEAR_ACA;
  361. break;
  362. default:
  363. /*
  364. * FCP4r01 indicates having a combination of
  365. * tm_flags set is invalid.
  366. */
  367. FT_TM_DBG("invalid FCP tm_flags %x\n", fcp->fc_tm_flags);
  368. ft_send_resp_code(cmd, FCP_CMND_FIELDS_INVALID);
  369. return;
  370. }
  371. FT_TM_DBG("alloc tm cmd fn %d\n", tm_func);
  372. tmr = core_tmr_alloc_req(&cmd->se_cmd, cmd, tm_func);
  373. if (!tmr) {
  374. FT_TM_DBG("alloc failed\n");
  375. ft_send_resp_code(cmd, FCP_TMF_FAILED);
  376. return;
  377. }
  378. cmd->se_cmd.se_tmr_req = tmr;
  379. switch (fcp->fc_tm_flags) {
  380. case FCP_TMF_LUN_RESET:
  381. cmd->lun = scsilun_to_int((struct scsi_lun *)fcp->fc_lun);
  382. if (transport_lookup_tmr_lun(&cmd->se_cmd, cmd->lun) < 0) {
  383. /*
  384. * Make sure to clean up newly allocated TMR request
  385. * since "unable to handle TMR request because failed
  386. * to get to LUN"
  387. */
  388. FT_TM_DBG("Failed to get LUN for TMR func %d, "
  389. "se_cmd %p, unpacked_lun %d\n",
  390. tm_func, &cmd->se_cmd, cmd->lun);
  391. ft_dump_cmd(cmd, __func__);
  392. sess = cmd->sess;
  393. transport_send_check_condition_and_sense(&cmd->se_cmd,
  394. cmd->se_cmd.scsi_sense_reason, 0);
  395. transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
  396. ft_sess_put(sess);
  397. return;
  398. }
  399. break;
  400. case FCP_TMF_TGT_RESET:
  401. case FCP_TMF_CLR_TASK_SET:
  402. case FCP_TMF_ABT_TASK_SET:
  403. case FCP_TMF_CLR_ACA:
  404. break;
  405. default:
  406. return;
  407. }
  408. transport_generic_handle_tmr(&cmd->se_cmd);
  409. }
  410. /*
  411. * Send status from completed task management request.
  412. */
  413. int ft_queue_tm_resp(struct se_cmd *se_cmd)
  414. {
  415. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  416. struct se_tmr_req *tmr = se_cmd->se_tmr_req;
  417. enum fcp_resp_rsp_codes code;
  418. switch (tmr->response) {
  419. case TMR_FUNCTION_COMPLETE:
  420. code = FCP_TMF_CMPL;
  421. break;
  422. case TMR_LUN_DOES_NOT_EXIST:
  423. code = FCP_TMF_INVALID_LUN;
  424. break;
  425. case TMR_FUNCTION_REJECTED:
  426. code = FCP_TMF_REJECTED;
  427. break;
  428. case TMR_TASK_DOES_NOT_EXIST:
  429. case TMR_TASK_STILL_ALLEGIANT:
  430. case TMR_TASK_FAILOVER_NOT_SUPPORTED:
  431. case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
  432. case TMR_FUNCTION_AUTHORIZATION_FAILED:
  433. default:
  434. code = FCP_TMF_FAILED;
  435. break;
  436. }
  437. FT_TM_DBG("tmr fn %d resp %d fcp code %d\n",
  438. tmr->function, tmr->response, code);
  439. ft_send_resp_code(cmd, code);
  440. return 0;
  441. }
  442. /*
  443. * Handle incoming FCP command.
  444. */
  445. static void ft_recv_cmd(struct ft_sess *sess, struct fc_frame *fp)
  446. {
  447. struct ft_cmd *cmd;
  448. struct fc_lport *lport = sess->tport->lport;
  449. cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
  450. if (!cmd)
  451. goto busy;
  452. cmd->sess = sess;
  453. cmd->seq = lport->tt.seq_assign(lport, fp);
  454. if (!cmd->seq) {
  455. kfree(cmd);
  456. goto busy;
  457. }
  458. cmd->req_frame = fp; /* hold frame during cmd */
  459. ft_queue_cmd(sess, cmd);
  460. return;
  461. busy:
  462. FT_IO_DBG("cmd or seq allocation failure - sending BUSY\n");
  463. ft_send_resp_status(lport, fp, SAM_STAT_BUSY, 0);
  464. fc_frame_free(fp);
  465. ft_sess_put(sess); /* undo get from lookup */
  466. }
  467. /*
  468. * Handle incoming FCP frame.
  469. * Caller has verified that the frame is type FCP.
  470. */
  471. void ft_recv_req(struct ft_sess *sess, struct fc_frame *fp)
  472. {
  473. struct fc_frame_header *fh = fc_frame_header_get(fp);
  474. switch (fh->fh_r_ctl) {
  475. case FC_RCTL_DD_UNSOL_CMD: /* command */
  476. ft_recv_cmd(sess, fp);
  477. break;
  478. case FC_RCTL_DD_SOL_DATA: /* write data */
  479. case FC_RCTL_DD_UNSOL_CTL:
  480. case FC_RCTL_DD_SOL_CTL:
  481. case FC_RCTL_DD_DATA_DESC: /* transfer ready */
  482. case FC_RCTL_ELS4_REQ: /* SRR, perhaps */
  483. default:
  484. printk(KERN_INFO "%s: unhandled frame r_ctl %x\n",
  485. __func__, fh->fh_r_ctl);
  486. fc_frame_free(fp);
  487. ft_sess_put(sess); /* undo get from lookup */
  488. break;
  489. }
  490. }
  491. /*
  492. * Send new command to target.
  493. */
  494. static void ft_send_cmd(struct ft_cmd *cmd)
  495. {
  496. struct fc_frame_header *fh = fc_frame_header_get(cmd->req_frame);
  497. struct se_cmd *se_cmd;
  498. struct fcp_cmnd *fcp;
  499. int data_dir;
  500. u32 data_len;
  501. int task_attr;
  502. int ret;
  503. fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
  504. if (!fcp)
  505. goto err;
  506. if (fcp->fc_flags & FCP_CFL_LEN_MASK)
  507. goto err; /* not handling longer CDBs yet */
  508. if (fcp->fc_tm_flags) {
  509. task_attr = FCP_PTA_SIMPLE;
  510. data_dir = DMA_NONE;
  511. data_len = 0;
  512. } else {
  513. switch (fcp->fc_flags & (FCP_CFL_RDDATA | FCP_CFL_WRDATA)) {
  514. case 0:
  515. data_dir = DMA_NONE;
  516. break;
  517. case FCP_CFL_RDDATA:
  518. data_dir = DMA_FROM_DEVICE;
  519. break;
  520. case FCP_CFL_WRDATA:
  521. data_dir = DMA_TO_DEVICE;
  522. break;
  523. case FCP_CFL_WRDATA | FCP_CFL_RDDATA:
  524. goto err; /* TBD not supported by tcm_fc yet */
  525. }
  526. /*
  527. * Locate the SAM Task Attr from fc_pri_ta
  528. */
  529. switch (fcp->fc_pri_ta & FCP_PTA_MASK) {
  530. case FCP_PTA_HEADQ:
  531. task_attr = MSG_HEAD_TAG;
  532. break;
  533. case FCP_PTA_ORDERED:
  534. task_attr = MSG_ORDERED_TAG;
  535. break;
  536. case FCP_PTA_ACA:
  537. task_attr = MSG_ACA_TAG;
  538. break;
  539. case FCP_PTA_SIMPLE: /* Fallthrough */
  540. default:
  541. task_attr = MSG_SIMPLE_TAG;
  542. }
  543. task_attr = fcp->fc_pri_ta & FCP_PTA_MASK;
  544. data_len = ntohl(fcp->fc_dl);
  545. cmd->cdb = fcp->fc_cdb;
  546. }
  547. se_cmd = &cmd->se_cmd;
  548. /*
  549. * Initialize struct se_cmd descriptor from target_core_mod
  550. * infrastructure
  551. */
  552. transport_init_se_cmd(se_cmd, &ft_configfs->tf_ops, cmd->sess->se_sess,
  553. data_len, data_dir, task_attr,
  554. &cmd->ft_sense_buffer[0]);
  555. /*
  556. * Check for FCP task management flags
  557. */
  558. if (fcp->fc_tm_flags) {
  559. ft_send_tm(cmd);
  560. return;
  561. }
  562. fc_seq_exch(cmd->seq)->lp->tt.seq_set_resp(cmd->seq, ft_recv_seq, cmd);
  563. cmd->lun = scsilun_to_int((struct scsi_lun *)fcp->fc_lun);
  564. ret = transport_lookup_cmd_lun(&cmd->se_cmd, cmd->lun);
  565. if (ret < 0) {
  566. ft_dump_cmd(cmd, __func__);
  567. transport_send_check_condition_and_sense(&cmd->se_cmd,
  568. cmd->se_cmd.scsi_sense_reason, 0);
  569. return;
  570. }
  571. ret = transport_generic_allocate_tasks(se_cmd, cmd->cdb);
  572. FT_IO_DBG("r_ctl %x alloc task ret %d\n", fh->fh_r_ctl, ret);
  573. ft_dump_cmd(cmd, __func__);
  574. if (ret == -ENOMEM) {
  575. transport_send_check_condition_and_sense(se_cmd,
  576. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  577. transport_generic_free_cmd(se_cmd, 0, 0);
  578. return;
  579. }
  580. if (ret == -EINVAL) {
  581. if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
  582. ft_queue_status(se_cmd);
  583. else
  584. transport_send_check_condition_and_sense(se_cmd,
  585. se_cmd->scsi_sense_reason, 0);
  586. transport_generic_free_cmd(se_cmd, 0, 0);
  587. return;
  588. }
  589. transport_generic_handle_cdb(se_cmd);
  590. return;
  591. err:
  592. ft_send_resp_code(cmd, FCP_CMND_FIELDS_INVALID);
  593. }
  594. /*
  595. * Handle request in the command thread.
  596. */
  597. static void ft_exec_req(struct ft_cmd *cmd)
  598. {
  599. FT_IO_DBG("cmd state %x\n", cmd->state);
  600. switch (cmd->state) {
  601. case FC_CMD_ST_NEW:
  602. ft_send_cmd(cmd);
  603. break;
  604. default:
  605. break;
  606. }
  607. }
  608. /*
  609. * Processing thread.
  610. * Currently one thread per tpg.
  611. */
  612. int ft_thread(void *arg)
  613. {
  614. struct ft_tpg *tpg = arg;
  615. struct se_queue_obj *qobj = &tpg->qobj;
  616. struct ft_cmd *cmd;
  617. int ret;
  618. set_user_nice(current, -20);
  619. while (!kthread_should_stop()) {
  620. ret = wait_event_interruptible(qobj->thread_wq,
  621. atomic_read(&qobj->queue_cnt) || kthread_should_stop());
  622. if (ret < 0 || kthread_should_stop())
  623. goto out;
  624. cmd = ft_dequeue_cmd(qobj);
  625. if (cmd)
  626. ft_exec_req(cmd);
  627. }
  628. out:
  629. return 0;
  630. }