blob: 1017f56bbbccefa9952c85890e677b4756018856 [file] [log] [blame]
Kiran Patil3699d922011-04-18 16:24:14 -07001/*
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
18/* XXX TBD some includes may be extraneous */
19
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/version.h>
23#include <generated/utsrelease.h>
24#include <linux/utsname.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/kthread.h>
28#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/configfs.h>
31#include <linux/ctype.h>
32#include <linux/hash.h>
33#include <asm/unaligned.h>
34#include <scsi/scsi.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_device.h>
37#include <scsi/scsi_cmnd.h>
Nicholas Bellinger61db1802011-05-19 20:19:14 -070038#include <scsi/scsi_tcq.h>
Kiran Patil3699d922011-04-18 16:24:14 -070039#include <scsi/libfc.h>
40#include <scsi/fc_encode.h>
41
42#include <target/target_core_base.h>
43#include <target/target_core_transport.h>
44#include <target/target_core_fabric_ops.h>
45#include <target/target_core_device.h>
46#include <target/target_core_tpg.h>
47#include <target/target_core_configfs.h>
48#include <target/target_core_base.h>
49#include <target/target_core_tmr.h>
50#include <target/configfs_macros.h>
51
52#include "tcm_fc.h"
53
54/*
55 * Dump cmd state for debugging.
56 */
57void ft_dump_cmd(struct ft_cmd *cmd, const char *caller)
58{
59 struct fc_exch *ep;
60 struct fc_seq *sp;
61 struct se_cmd *se_cmd;
62 struct se_mem *mem;
Kiran Patil3699d922011-04-18 16:24:14 -070063
64 if (!(ft_debug_logging & FT_DEBUG_IO))
65 return;
66
67 se_cmd = &cmd->se_cmd;
68 printk(KERN_INFO "%s: cmd %p state %d sess %p seq %p se_cmd %p\n",
69 caller, cmd, cmd->state, cmd->sess, cmd->seq, se_cmd);
70 printk(KERN_INFO "%s: cmd %p cdb %p\n",
71 caller, cmd, cmd->cdb);
72 printk(KERN_INFO "%s: cmd %p lun %d\n", caller, cmd, cmd->lun);
73
Andy Grover05d1c7c2011-07-20 19:13:28 +000074 printk(KERN_INFO "%s: cmd %p se_num %u len %u se_cmd_flags <0x%x>\n",
Andy Grovera1d8b492011-05-02 17:12:10 -070075 caller, cmd, se_cmd->t_tasks_se_num,
Andy Grover05d1c7c2011-07-20 19:13:28 +000076 se_cmd->data_length, se_cmd->se_cmd_flags);
Andy Grover5951146d2011-07-19 10:26:37 +000077
Andy Grovera1d8b492011-05-02 17:12:10 -070078 list_for_each_entry(mem, &se_cmd->t_mem_list, se_list)
Andy Grover5951146d2011-07-19 10:26:37 +000079 printk(KERN_INFO "%s: cmd %p mem %p page %p "
80 "len 0x%x off 0x%x\n",
81 caller, cmd, mem,
82 mem->se_page, mem->se_len, mem->se_off);
Kiran Patil3699d922011-04-18 16:24:14 -070083 sp = cmd->seq;
84 if (sp) {
85 ep = fc_seq_exch(sp);
86 printk(KERN_INFO "%s: cmd %p sid %x did %x "
87 "ox_id %x rx_id %x seq_id %x e_stat %x\n",
88 caller, cmd, ep->sid, ep->did, ep->oxid, ep->rxid,
89 sp->id, ep->esb_stat);
90 }
91 print_hex_dump(KERN_INFO, "ft_dump_cmd ", DUMP_PREFIX_NONE,
92 16, 4, cmd->cdb, MAX_COMMAND_SIZE, 0);
93}
94
Kiran Patil3699d922011-04-18 16:24:14 -070095static void ft_queue_cmd(struct ft_sess *sess, struct ft_cmd *cmd)
96{
Nicholas Bellinger2be18142011-05-27 13:58:48 -070097 struct ft_tpg *tpg = sess->tport->tpg;
98 struct se_queue_obj *qobj = &tpg->qobj;
Kiran Patil3699d922011-04-18 16:24:14 -070099 unsigned long flags;
100
101 qobj = &sess->tport->tpg->qobj;
102 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
103 list_add_tail(&cmd->se_req.qr_list, &qobj->qobj_list);
Kiran Patil3699d922011-04-18 16:24:14 -0700104 atomic_inc(&qobj->queue_cnt);
Nicholas Bellinger2be18142011-05-27 13:58:48 -0700105 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
106
107 wake_up_process(tpg->thread);
Kiran Patil3699d922011-04-18 16:24:14 -0700108}
109
110static struct ft_cmd *ft_dequeue_cmd(struct se_queue_obj *qobj)
111{
112 unsigned long flags;
113 struct se_queue_req *qr;
114
115 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
116 if (list_empty(&qobj->qobj_list)) {
117 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
118 return NULL;
119 }
120 qr = list_first_entry(&qobj->qobj_list, struct se_queue_req, qr_list);
121 list_del(&qr->qr_list);
122 atomic_dec(&qobj->queue_cnt);
123 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
124 return container_of(qr, struct ft_cmd, se_req);
125}
126
127static void ft_free_cmd(struct ft_cmd *cmd)
128{
129 struct fc_frame *fp;
130 struct fc_lport *lport;
131
132 if (!cmd)
133 return;
134 fp = cmd->req_frame;
135 lport = fr_dev(fp);
136 if (fr_seq(fp))
137 lport->tt.seq_release(fr_seq(fp));
138 fc_frame_free(fp);
139 ft_sess_put(cmd->sess); /* undo get from lookup at recv */
140 kfree(cmd);
141}
142
143void ft_release_cmd(struct se_cmd *se_cmd)
144{
145 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
146
147 ft_free_cmd(cmd);
148}
149
150void ft_check_stop_free(struct se_cmd *se_cmd)
151{
Christoph Hellwig35462972011-05-31 23:56:57 -0400152 transport_generic_free_cmd(se_cmd, 0, 0);
Kiran Patil3699d922011-04-18 16:24:14 -0700153}
154
155/*
156 * Send response.
157 */
158int ft_queue_status(struct se_cmd *se_cmd)
159{
160 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
161 struct fc_frame *fp;
162 struct fcp_resp_with_ext *fcp;
163 struct fc_lport *lport;
164 struct fc_exch *ep;
165 size_t len;
166
167 ft_dump_cmd(cmd, __func__);
168 ep = fc_seq_exch(cmd->seq);
169 lport = ep->lp;
170 len = sizeof(*fcp) + se_cmd->scsi_sense_length;
171 fp = fc_frame_alloc(lport, len);
172 if (!fp) {
173 /* XXX shouldn't just drop it - requeue and retry? */
174 return 0;
175 }
176 fcp = fc_frame_payload_get(fp, len);
177 memset(fcp, 0, len);
178 fcp->resp.fr_status = se_cmd->scsi_status;
179
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 /*
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 /*
200 * Send response.
201 */
202 cmd->seq = lport->tt.seq_start_next(cmd->seq);
203 fc_fill_fc_hdr(fp, FC_RCTL_DD_CMD_STATUS, ep->did, ep->sid, FC_TYPE_FCP,
204 FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ, 0);
205
206 lport->tt.seq_send(lport, cmd->seq, fp);
207 lport->tt.exch_done(cmd->seq);
208 return 0;
209}
210
211int ft_write_pending_status(struct se_cmd *se_cmd)
212{
213 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
214
215 return cmd->write_data_len != se_cmd->data_length;
216}
217
218/*
219 * Send TX_RDY (transfer ready).
220 */
221int ft_write_pending(struct se_cmd *se_cmd)
222{
223 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
224 struct fc_frame *fp;
225 struct fcp_txrdy *txrdy;
226 struct fc_lport *lport;
227 struct fc_exch *ep;
228 struct fc_frame_header *fh;
229 u32 f_ctl;
230
231 ft_dump_cmd(cmd, __func__);
232
233 ep = fc_seq_exch(cmd->seq);
234 lport = ep->lp;
235 fp = fc_frame_alloc(lport, sizeof(*txrdy));
236 if (!fp)
237 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
238
239 txrdy = fc_frame_payload_get(fp, sizeof(*txrdy));
240 memset(txrdy, 0, sizeof(*txrdy));
241 txrdy->ft_burst_len = htonl(se_cmd->data_length);
242
243 cmd->seq = lport->tt.seq_start_next(cmd->seq);
244 fc_fill_fc_hdr(fp, FC_RCTL_DD_DATA_DESC, ep->did, ep->sid, FC_TYPE_FCP,
245 FC_FC_EX_CTX | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
246
247 fh = fc_frame_header_get(fp);
248 f_ctl = ntoh24(fh->fh_f_ctl);
249
250 /* Only if it is 'Exchange Responder' */
251 if (f_ctl & FC_FC_EX_CTX) {
252 /* Target is 'exchange responder' and sending XFER_READY
253 * to 'exchange initiator (initiator)'
254 */
255 if ((ep->xid <= lport->lro_xid) &&
256 (fh->fh_r_ctl == FC_RCTL_DD_DATA_DESC)) {
257 if (se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
258 /*
259 * Map se_mem list to scatterlist, so that
260 * DDP can be setup. DDP setup function require
261 * scatterlist. se_mem_list is internal to
262 * TCM/LIO target
263 */
264 transport_do_task_sg_chain(se_cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700265 cmd->sg = se_cmd->t_tasks_sg_chained;
Kiran Patil3699d922011-04-18 16:24:14 -0700266 cmd->sg_cnt =
Andy Grovera1d8b492011-05-02 17:12:10 -0700267 se_cmd->t_tasks_sg_chained_no;
Kiran Patil3699d922011-04-18 16:24:14 -0700268 }
269 if (cmd->sg && lport->tt.ddp_setup(lport, ep->xid,
270 cmd->sg, cmd->sg_cnt))
271 cmd->was_ddp_setup = 1;
272 }
273 }
274 lport->tt.seq_send(lport, cmd->seq, fp);
275 return 0;
276}
277
278u32 ft_get_task_tag(struct se_cmd *se_cmd)
279{
280 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
281
282 return fc_seq_exch(cmd->seq)->rxid;
283}
284
285int ft_get_cmd_state(struct se_cmd *se_cmd)
286{
287 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
288
289 return cmd->state;
290}
291
292int ft_is_state_remove(struct se_cmd *se_cmd)
293{
294 return 0; /* XXX TBD */
295}
296
Kiran Patil3699d922011-04-18 16:24:14 -0700297/*
298 * FC sequence response handler for follow-on sequences (data) and aborts.
299 */
300static void ft_recv_seq(struct fc_seq *sp, struct fc_frame *fp, void *arg)
301{
302 struct ft_cmd *cmd = arg;
303 struct fc_frame_header *fh;
304
305 if (IS_ERR(fp)) {
306 /* XXX need to find cmd if queued */
307 cmd->se_cmd.t_state = TRANSPORT_REMOVE;
308 cmd->seq = NULL;
Christoph Hellwig35462972011-05-31 23:56:57 -0400309 transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
Kiran Patil3699d922011-04-18 16:24:14 -0700310 return;
311 }
312
313 fh = fc_frame_header_get(fp);
314
315 switch (fh->fh_r_ctl) {
316 case FC_RCTL_DD_SOL_DATA: /* write data */
317 ft_recv_write_data(cmd, fp);
318 break;
319 case FC_RCTL_DD_UNSOL_CTL: /* command */
320 case FC_RCTL_DD_SOL_CTL: /* transfer ready */
321 case FC_RCTL_DD_DATA_DESC: /* transfer ready */
322 default:
323 printk(KERN_INFO "%s: unhandled frame r_ctl %x\n",
324 __func__, fh->fh_r_ctl);
325 fc_frame_free(fp);
Christoph Hellwig35462972011-05-31 23:56:57 -0400326 transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
Kiran Patil3699d922011-04-18 16:24:14 -0700327 break;
328 }
329}
330
331/*
332 * Send a FCP response including SCSI status and optional FCP rsp_code.
333 * status is SAM_STAT_GOOD (zero) iff code is valid.
334 * This is used in error cases, such as allocation failures.
335 */
336static void ft_send_resp_status(struct fc_lport *lport,
337 const struct fc_frame *rx_fp,
338 u32 status, enum fcp_resp_rsp_codes code)
339{
340 struct fc_frame *fp;
341 struct fc_seq *sp;
342 const struct fc_frame_header *fh;
343 size_t len;
344 struct fcp_resp_with_ext *fcp;
345 struct fcp_resp_rsp_info *info;
346
347 fh = fc_frame_header_get(rx_fp);
348 FT_IO_DBG("FCP error response: did %x oxid %x status %x code %x\n",
349 ntoh24(fh->fh_s_id), ntohs(fh->fh_ox_id), status, code);
350 len = sizeof(*fcp);
351 if (status == SAM_STAT_GOOD)
352 len += sizeof(*info);
353 fp = fc_frame_alloc(lport, len);
354 if (!fp)
355 return;
356 fcp = fc_frame_payload_get(fp, len);
357 memset(fcp, 0, len);
358 fcp->resp.fr_status = status;
359 if (status == SAM_STAT_GOOD) {
360 fcp->ext.fr_rsp_len = htonl(sizeof(*info));
361 fcp->resp.fr_flags |= FCP_RSP_LEN_VAL;
362 info = (struct fcp_resp_rsp_info *)(fcp + 1);
363 info->rsp_code = code;
364 }
365
366 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_DD_CMD_STATUS, 0);
367 sp = fr_seq(fp);
368 if (sp)
369 lport->tt.seq_send(lport, sp, fp);
370 else
371 lport->tt.frame_send(lport, fp);
372}
373
374/*
375 * Send error or task management response.
376 * Always frees the cmd and associated state.
377 */
378static void ft_send_resp_code(struct ft_cmd *cmd, enum fcp_resp_rsp_codes code)
379{
380 ft_send_resp_status(cmd->sess->tport->lport,
381 cmd->req_frame, SAM_STAT_GOOD, code);
382 ft_free_cmd(cmd);
383}
384
385/*
386 * Handle Task Management Request.
387 */
388static void ft_send_tm(struct ft_cmd *cmd)
389{
390 struct se_tmr_req *tmr;
391 struct fcp_cmnd *fcp;
Kiran Patil61db95272011-06-22 16:30:22 -0700392 struct ft_sess *sess;
Kiran Patil3699d922011-04-18 16:24:14 -0700393 u8 tm_func;
394
395 fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
396
397 switch (fcp->fc_tm_flags) {
398 case FCP_TMF_LUN_RESET:
399 tm_func = TMR_LUN_RESET;
Kiran Patil3699d922011-04-18 16:24:14 -0700400 break;
401 case FCP_TMF_TGT_RESET:
402 tm_func = TMR_TARGET_WARM_RESET;
403 break;
404 case FCP_TMF_CLR_TASK_SET:
405 tm_func = TMR_CLEAR_TASK_SET;
406 break;
407 case FCP_TMF_ABT_TASK_SET:
408 tm_func = TMR_ABORT_TASK_SET;
409 break;
410 case FCP_TMF_CLR_ACA:
411 tm_func = TMR_CLEAR_ACA;
412 break;
413 default:
414 /*
415 * FCP4r01 indicates having a combination of
416 * tm_flags set is invalid.
417 */
418 FT_TM_DBG("invalid FCP tm_flags %x\n", fcp->fc_tm_flags);
419 ft_send_resp_code(cmd, FCP_CMND_FIELDS_INVALID);
420 return;
421 }
422
423 FT_TM_DBG("alloc tm cmd fn %d\n", tm_func);
424 tmr = core_tmr_alloc_req(&cmd->se_cmd, cmd, tm_func);
425 if (!tmr) {
426 FT_TM_DBG("alloc failed\n");
427 ft_send_resp_code(cmd, FCP_TMF_FAILED);
428 return;
429 }
430 cmd->se_cmd.se_tmr_req = tmr;
Kiran Patil61db95272011-06-22 16:30:22 -0700431
432 switch (fcp->fc_tm_flags) {
433 case FCP_TMF_LUN_RESET:
434 cmd->lun = scsilun_to_int((struct scsi_lun *)fcp->fc_lun);
Andy Grover5951146d2011-07-19 10:26:37 +0000435 if (transport_lookup_tmr_lun(&cmd->se_cmd, cmd->lun) < 0) {
Kiran Patil61db95272011-06-22 16:30:22 -0700436 /*
437 * Make sure to clean up newly allocated TMR request
438 * since "unable to handle TMR request because failed
439 * to get to LUN"
440 */
441 FT_TM_DBG("Failed to get LUN for TMR func %d, "
442 "se_cmd %p, unpacked_lun %d\n",
443 tm_func, &cmd->se_cmd, cmd->lun);
444 ft_dump_cmd(cmd, __func__);
445 sess = cmd->sess;
446 transport_send_check_condition_and_sense(&cmd->se_cmd,
447 cmd->se_cmd.scsi_sense_reason, 0);
Christoph Hellwig35462972011-05-31 23:56:57 -0400448 transport_generic_free_cmd(&cmd->se_cmd, 0, 0);
Kiran Patil61db95272011-06-22 16:30:22 -0700449 ft_sess_put(sess);
450 return;
451 }
452 break;
453 case FCP_TMF_TGT_RESET:
454 case FCP_TMF_CLR_TASK_SET:
455 case FCP_TMF_ABT_TASK_SET:
456 case FCP_TMF_CLR_ACA:
457 break;
458 default:
459 return;
460 }
Kiran Patil3699d922011-04-18 16:24:14 -0700461 transport_generic_handle_tmr(&cmd->se_cmd);
462}
463
464/*
465 * Send status from completed task management request.
466 */
467int ft_queue_tm_resp(struct se_cmd *se_cmd)
468{
469 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
470 struct se_tmr_req *tmr = se_cmd->se_tmr_req;
471 enum fcp_resp_rsp_codes code;
472
473 switch (tmr->response) {
474 case TMR_FUNCTION_COMPLETE:
475 code = FCP_TMF_CMPL;
476 break;
477 case TMR_LUN_DOES_NOT_EXIST:
478 code = FCP_TMF_INVALID_LUN;
479 break;
480 case TMR_FUNCTION_REJECTED:
481 code = FCP_TMF_REJECTED;
482 break;
483 case TMR_TASK_DOES_NOT_EXIST:
484 case TMR_TASK_STILL_ALLEGIANT:
485 case TMR_TASK_FAILOVER_NOT_SUPPORTED:
486 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
487 case TMR_FUNCTION_AUTHORIZATION_FAILED:
488 default:
489 code = FCP_TMF_FAILED;
490 break;
491 }
492 FT_TM_DBG("tmr fn %d resp %d fcp code %d\n",
493 tmr->function, tmr->response, code);
494 ft_send_resp_code(cmd, code);
495 return 0;
496}
497
498/*
499 * Handle incoming FCP command.
500 */
501static void ft_recv_cmd(struct ft_sess *sess, struct fc_frame *fp)
502{
503 struct ft_cmd *cmd;
504 struct fc_lport *lport = sess->tport->lport;
505
506 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
507 if (!cmd)
508 goto busy;
509 cmd->sess = sess;
510 cmd->seq = lport->tt.seq_assign(lport, fp);
511 if (!cmd->seq) {
512 kfree(cmd);
513 goto busy;
514 }
515 cmd->req_frame = fp; /* hold frame during cmd */
516 ft_queue_cmd(sess, cmd);
517 return;
518
519busy:
520 FT_IO_DBG("cmd or seq allocation failure - sending BUSY\n");
521 ft_send_resp_status(lport, fp, SAM_STAT_BUSY, 0);
522 fc_frame_free(fp);
523 ft_sess_put(sess); /* undo get from lookup */
524}
525
526
527/*
528 * Handle incoming FCP frame.
529 * Caller has verified that the frame is type FCP.
530 */
531void ft_recv_req(struct ft_sess *sess, struct fc_frame *fp)
532{
533 struct fc_frame_header *fh = fc_frame_header_get(fp);
534
535 switch (fh->fh_r_ctl) {
536 case FC_RCTL_DD_UNSOL_CMD: /* command */
537 ft_recv_cmd(sess, fp);
538 break;
539 case FC_RCTL_DD_SOL_DATA: /* write data */
540 case FC_RCTL_DD_UNSOL_CTL:
541 case FC_RCTL_DD_SOL_CTL:
542 case FC_RCTL_DD_DATA_DESC: /* transfer ready */
543 case FC_RCTL_ELS4_REQ: /* SRR, perhaps */
544 default:
545 printk(KERN_INFO "%s: unhandled frame r_ctl %x\n",
546 __func__, fh->fh_r_ctl);
547 fc_frame_free(fp);
548 ft_sess_put(sess); /* undo get from lookup */
549 break;
550 }
551}
552
553/*
554 * Send new command to target.
555 */
556static void ft_send_cmd(struct ft_cmd *cmd)
557{
558 struct fc_frame_header *fh = fc_frame_header_get(cmd->req_frame);
559 struct se_cmd *se_cmd;
560 struct fcp_cmnd *fcp;
561 int data_dir;
562 u32 data_len;
563 int task_attr;
564 int ret;
565
566 fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
567 if (!fcp)
568 goto err;
569
570 if (fcp->fc_flags & FCP_CFL_LEN_MASK)
571 goto err; /* not handling longer CDBs yet */
572
573 if (fcp->fc_tm_flags) {
574 task_attr = FCP_PTA_SIMPLE;
575 data_dir = DMA_NONE;
576 data_len = 0;
577 } else {
578 switch (fcp->fc_flags & (FCP_CFL_RDDATA | FCP_CFL_WRDATA)) {
579 case 0:
580 data_dir = DMA_NONE;
581 break;
582 case FCP_CFL_RDDATA:
583 data_dir = DMA_FROM_DEVICE;
584 break;
585 case FCP_CFL_WRDATA:
586 data_dir = DMA_TO_DEVICE;
587 break;
588 case FCP_CFL_WRDATA | FCP_CFL_RDDATA:
589 goto err; /* TBD not supported by tcm_fc yet */
590 }
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700591 /*
592 * Locate the SAM Task Attr from fc_pri_ta
593 */
594 switch (fcp->fc_pri_ta & FCP_PTA_MASK) {
595 case FCP_PTA_HEADQ:
596 task_attr = MSG_HEAD_TAG;
597 break;
598 case FCP_PTA_ORDERED:
599 task_attr = MSG_ORDERED_TAG;
600 break;
601 case FCP_PTA_ACA:
602 task_attr = MSG_ACA_TAG;
603 break;
604 case FCP_PTA_SIMPLE: /* Fallthrough */
605 default:
606 task_attr = MSG_SIMPLE_TAG;
607 }
Kiran Patil3699d922011-04-18 16:24:14 -0700608
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700609
Kiran Patil3699d922011-04-18 16:24:14 -0700610 task_attr = fcp->fc_pri_ta & FCP_PTA_MASK;
611 data_len = ntohl(fcp->fc_dl);
612 cmd->cdb = fcp->fc_cdb;
613 }
614
615 se_cmd = &cmd->se_cmd;
616 /*
617 * Initialize struct se_cmd descriptor from target_core_mod
618 * infrastructure
619 */
620 transport_init_se_cmd(se_cmd, &ft_configfs->tf_ops, cmd->sess->se_sess,
621 data_len, data_dir, task_attr,
622 &cmd->ft_sense_buffer[0]);
623 /*
624 * Check for FCP task management flags
625 */
626 if (fcp->fc_tm_flags) {
627 ft_send_tm(cmd);
628 return;
629 }
630
631 fc_seq_exch(cmd->seq)->lp->tt.seq_set_resp(cmd->seq, ft_recv_seq, cmd);
632
Kiran Patil61db95272011-06-22 16:30:22 -0700633 cmd->lun = scsilun_to_int((struct scsi_lun *)fcp->fc_lun);
Andy Grover5951146d2011-07-19 10:26:37 +0000634 ret = transport_lookup_cmd_lun(&cmd->se_cmd, cmd->lun);
Kiran Patil3699d922011-04-18 16:24:14 -0700635 if (ret < 0) {
636 ft_dump_cmd(cmd, __func__);
637 transport_send_check_condition_and_sense(&cmd->se_cmd,
638 cmd->se_cmd.scsi_sense_reason, 0);
639 return;
640 }
641
642 ret = transport_generic_allocate_tasks(se_cmd, cmd->cdb);
643
644 FT_IO_DBG("r_ctl %x alloc task ret %d\n", fh->fh_r_ctl, ret);
645 ft_dump_cmd(cmd, __func__);
646
Andy Grover5951146d2011-07-19 10:26:37 +0000647 if (ret == -ENOMEM) {
Kiran Patil3699d922011-04-18 16:24:14 -0700648 transport_send_check_condition_and_sense(se_cmd,
649 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Christoph Hellwig35462972011-05-31 23:56:57 -0400650 transport_generic_free_cmd(se_cmd, 0, 0);
Kiran Patil3699d922011-04-18 16:24:14 -0700651 return;
652 }
Andy Grover5951146d2011-07-19 10:26:37 +0000653 if (ret == -EINVAL) {
Kiran Patil3699d922011-04-18 16:24:14 -0700654 if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
655 ft_queue_status(se_cmd);
656 else
657 transport_send_check_condition_and_sense(se_cmd,
658 se_cmd->scsi_sense_reason, 0);
Christoph Hellwig35462972011-05-31 23:56:57 -0400659 transport_generic_free_cmd(se_cmd, 0, 0);
Kiran Patil3699d922011-04-18 16:24:14 -0700660 return;
661 }
662 transport_generic_handle_cdb(se_cmd);
663 return;
664
665err:
666 ft_send_resp_code(cmd, FCP_CMND_FIELDS_INVALID);
Kiran Patil3699d922011-04-18 16:24:14 -0700667}
668
669/*
670 * Handle request in the command thread.
671 */
672static void ft_exec_req(struct ft_cmd *cmd)
673{
674 FT_IO_DBG("cmd state %x\n", cmd->state);
675 switch (cmd->state) {
676 case FC_CMD_ST_NEW:
677 ft_send_cmd(cmd);
678 break;
679 default:
680 break;
681 }
682}
683
684/*
685 * Processing thread.
686 * Currently one thread per tpg.
687 */
688int ft_thread(void *arg)
689{
690 struct ft_tpg *tpg = arg;
691 struct se_queue_obj *qobj = &tpg->qobj;
692 struct ft_cmd *cmd;
Kiran Patil3699d922011-04-18 16:24:14 -0700693
694 while (!kthread_should_stop()) {
Nicholas Bellinger2be18142011-05-27 13:58:48 -0700695 schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT);
696 if (kthread_should_stop())
Kiran Patil3699d922011-04-18 16:24:14 -0700697 goto out;
Nicholas Bellinger2be18142011-05-27 13:58:48 -0700698
Kiran Patil3699d922011-04-18 16:24:14 -0700699 cmd = ft_dequeue_cmd(qobj);
700 if (cmd)
701 ft_exec_req(cmd);
702 }
703
704out:
705 return 0;
706}