tfc_cmd.c 18 KB

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