tfc_cmd.c 17 KB

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