tfc_cmd.c 18 KB

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