tfc_cmd.c 18 KB

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