d4c95cc2ae546d6ff733f629407726ca56b9ccba
[cascardo/linux.git] / drivers / target / tcm_fc / tfc_cmd.c
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
18 /* XXX TBD some includes may be extraneous */
19
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <generated/utsrelease.h>
23 #include <linux/utsname.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/kthread.h>
27 #include <linux/types.h>
28 #include <linux/string.h>
29 #include <linux/configfs.h>
30 #include <linux/ctype.h>
31 #include <linux/hash.h>
32 #include <asm/unaligned.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_cmnd.h>
37 #include <scsi/scsi_tcq.h>
38 #include <scsi/libfc.h>
39 #include <scsi/fc_encode.h>
40
41 #include <target/target_core_base.h>
42 #include <target/target_core_fabric.h>
43 #include <target/target_core_configfs.h>
44 #include <target/configfs_macros.h>
45
46 #include "tcm_fc.h"
47
48 /*
49  * Dump cmd state for debugging.
50  */
51 void ft_dump_cmd(struct ft_cmd *cmd, const char *caller)
52 {
53         struct fc_exch *ep;
54         struct fc_seq *sp;
55         struct se_cmd *se_cmd;
56         struct scatterlist *sg;
57         int count;
58
59         se_cmd = &cmd->se_cmd;
60         pr_debug("%s: cmd %p sess %p seq %p se_cmd %p\n",
61                 caller, cmd, cmd->sess, cmd->seq, se_cmd);
62
63         pr_debug("%s: cmd %p data_nents %u len %u se_cmd_flags <0x%x>\n",
64                 caller, cmd, se_cmd->t_data_nents,
65                se_cmd->data_length, se_cmd->se_cmd_flags);
66
67         for_each_sg(se_cmd->t_data_sg, sg, se_cmd->t_data_nents, count)
68                 pr_debug("%s: cmd %p sg %p page %p "
69                         "len 0x%x off 0x%x\n",
70                         caller, cmd, sg,
71                         sg_page(sg), sg->length, sg->offset);
72
73         sp = cmd->seq;
74         if (sp) {
75                 ep = fc_seq_exch(sp);
76                 pr_debug("%s: cmd %p sid %x did %x "
77                         "ox_id %x rx_id %x seq_id %x e_stat %x\n",
78                         caller, cmd, ep->sid, ep->did, ep->oxid, ep->rxid,
79                         sp->id, ep->esb_stat);
80         }
81 }
82
83 static void ft_free_cmd(struct ft_cmd *cmd)
84 {
85         struct fc_frame *fp;
86         struct fc_lport *lport;
87
88         if (!cmd)
89                 return;
90         fp = cmd->req_frame;
91         lport = fr_dev(fp);
92         if (fr_seq(fp))
93                 lport->tt.seq_release(fr_seq(fp));
94         fc_frame_free(fp);
95         ft_sess_put(cmd->sess); /* undo get from lookup at recv */
96         kfree(cmd);
97 }
98
99 void ft_release_cmd(struct se_cmd *se_cmd)
100 {
101         struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
102
103         ft_free_cmd(cmd);
104 }
105
106 int ft_check_stop_free(struct se_cmd *se_cmd)
107 {
108         transport_generic_free_cmd(se_cmd, 0);
109         return 1;
110 }
111
112 /*
113  * Send response.
114  */
115 int ft_queue_status(struct se_cmd *se_cmd)
116 {
117         struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
118         struct fc_frame *fp;
119         struct fcp_resp_with_ext *fcp;
120         struct fc_lport *lport;
121         struct fc_exch *ep;
122         size_t len;
123
124         ft_dump_cmd(cmd, __func__);
125         ep = fc_seq_exch(cmd->seq);
126         lport = ep->lp;
127         len = sizeof(*fcp) + se_cmd->scsi_sense_length;
128         fp = fc_frame_alloc(lport, len);
129         if (!fp) {
130                 /* XXX shouldn't just drop it - requeue and retry? */
131                 return 0;
132         }
133         fcp = fc_frame_payload_get(fp, len);
134         memset(fcp, 0, len);
135         fcp->resp.fr_status = se_cmd->scsi_status;
136
137         len = se_cmd->scsi_sense_length;
138         if (len) {
139                 fcp->resp.fr_flags |= FCP_SNS_LEN_VAL;
140                 fcp->ext.fr_sns_len = htonl(len);
141                 memcpy((fcp + 1), se_cmd->sense_buffer, len);
142         }
143
144         /*
145          * Test underflow and overflow with one mask.  Usually both are off.
146          * Bidirectional commands are not handled yet.
147          */
148         if (se_cmd->se_cmd_flags & (SCF_OVERFLOW_BIT | SCF_UNDERFLOW_BIT)) {
149                 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT)
150                         fcp->resp.fr_flags |= FCP_RESID_OVER;
151                 else
152                         fcp->resp.fr_flags |= FCP_RESID_UNDER;
153                 fcp->ext.fr_resid = cpu_to_be32(se_cmd->residual_count);
154         }
155
156         /*
157          * Send response.
158          */
159         cmd->seq = lport->tt.seq_start_next(cmd->seq);
160         fc_fill_fc_hdr(fp, FC_RCTL_DD_CMD_STATUS, ep->did, ep->sid, FC_TYPE_FCP,
161                        FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ, 0);
162
163         lport->tt.seq_send(lport, cmd->seq, fp);
164         lport->tt.exch_done(cmd->seq);
165         return 0;
166 }
167
168 int ft_write_pending_status(struct se_cmd *se_cmd)
169 {
170         struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
171
172         return cmd->write_data_len != se_cmd->data_length;
173 }
174
175 /*
176  * Send TX_RDY (transfer ready).
177  */
178 int ft_write_pending(struct se_cmd *se_cmd)
179 {
180         struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
181         struct fc_frame *fp;
182         struct fcp_txrdy *txrdy;
183         struct fc_lport *lport;
184         struct fc_exch *ep;
185         struct fc_frame_header *fh;
186         u32 f_ctl;
187
188         ft_dump_cmd(cmd, __func__);
189
190         ep = fc_seq_exch(cmd->seq);
191         lport = ep->lp;
192         fp = fc_frame_alloc(lport, sizeof(*txrdy));
193         if (!fp)
194                 return -ENOMEM; /* Signal QUEUE_FULL */
195
196         txrdy = fc_frame_payload_get(fp, sizeof(*txrdy));
197         memset(txrdy, 0, sizeof(*txrdy));
198         txrdy->ft_burst_len = htonl(se_cmd->data_length);
199
200         cmd->seq = lport->tt.seq_start_next(cmd->seq);
201         fc_fill_fc_hdr(fp, FC_RCTL_DD_DATA_DESC, ep->did, ep->sid, FC_TYPE_FCP,
202                        FC_FC_EX_CTX | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
203
204         fh = fc_frame_header_get(fp);
205         f_ctl = ntoh24(fh->fh_f_ctl);
206
207         /* Only if it is 'Exchange Responder' */
208         if (f_ctl & FC_FC_EX_CTX) {
209                 /* Target is 'exchange responder' and sending XFER_READY
210                  * to 'exchange initiator (initiator)'
211                  */
212                 if ((ep->xid <= lport->lro_xid) &&
213                     (fh->fh_r_ctl == FC_RCTL_DD_DATA_DESC)) {
214                         if (se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
215                                 /*
216                                  * cmd may have been broken up into multiple
217                                  * tasks. Link their sgs together so we can
218                                  * operate on them all at once.
219                                  */
220                                 transport_do_task_sg_chain(se_cmd);
221                                 cmd->sg = se_cmd->t_tasks_sg_chained;
222                                 cmd->sg_cnt =
223                                         se_cmd->t_tasks_sg_chained_no;
224                         }
225                         if (cmd->sg && lport->tt.ddp_target(lport, ep->xid,
226                                                             cmd->sg,
227                                                             cmd->sg_cnt))
228                                 cmd->was_ddp_setup = 1;
229                 }
230         }
231         lport->tt.seq_send(lport, cmd->seq, fp);
232         return 0;
233 }
234
235 u32 ft_get_task_tag(struct se_cmd *se_cmd)
236 {
237         struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
238
239         return fc_seq_exch(cmd->seq)->rxid;
240 }
241
242 int ft_get_cmd_state(struct se_cmd *se_cmd)
243 {
244         return 0;
245 }
246
247 int ft_is_state_remove(struct se_cmd *se_cmd)
248 {
249         return 0;       /* XXX TBD */
250 }
251
252 /*
253  * FC sequence response handler for follow-on sequences (data) and aborts.
254  */
255 static void ft_recv_seq(struct fc_seq *sp, struct fc_frame *fp, void *arg)
256 {
257         struct ft_cmd *cmd = arg;
258         struct fc_frame_header *fh;
259
260         if (IS_ERR(fp)) {
261                 /* XXX need to find cmd if queued */
262                 cmd->seq = NULL;
263                 transport_generic_free_cmd(&cmd->se_cmd, 0);
264                 return;
265         }
266
267         fh = fc_frame_header_get(fp);
268
269         switch (fh->fh_r_ctl) {
270         case FC_RCTL_DD_SOL_DATA:       /* write data */
271                 ft_recv_write_data(cmd, fp);
272                 break;
273         case FC_RCTL_DD_UNSOL_CTL:      /* command */
274         case FC_RCTL_DD_SOL_CTL:        /* transfer ready */
275         case FC_RCTL_DD_DATA_DESC:      /* transfer ready */
276         default:
277                 pr_debug("%s: unhandled frame r_ctl %x\n",
278                        __func__, fh->fh_r_ctl);
279                 ft_invl_hw_context(cmd);
280                 fc_frame_free(fp);
281                 transport_generic_free_cmd(&cmd->se_cmd, 0);
282                 break;
283         }
284 }
285
286 /*
287  * Send a FCP response including SCSI status and optional FCP rsp_code.
288  * status is SAM_STAT_GOOD (zero) iff code is valid.
289  * This is used in error cases, such as allocation failures.
290  */
291 static void ft_send_resp_status(struct fc_lport *lport,
292                                 const struct fc_frame *rx_fp,
293                                 u32 status, enum fcp_resp_rsp_codes code)
294 {
295         struct fc_frame *fp;
296         struct fc_seq *sp;
297         const struct fc_frame_header *fh;
298         size_t len;
299         struct fcp_resp_with_ext *fcp;
300         struct fcp_resp_rsp_info *info;
301
302         fh = fc_frame_header_get(rx_fp);
303         pr_debug("FCP error response: did %x oxid %x status %x code %x\n",
304                   ntoh24(fh->fh_s_id), ntohs(fh->fh_ox_id), status, code);
305         len = sizeof(*fcp);
306         if (status == SAM_STAT_GOOD)
307                 len += sizeof(*info);
308         fp = fc_frame_alloc(lport, len);
309         if (!fp)
310                 return;
311         fcp = fc_frame_payload_get(fp, len);
312         memset(fcp, 0, len);
313         fcp->resp.fr_status = status;
314         if (status == SAM_STAT_GOOD) {
315                 fcp->ext.fr_rsp_len = htonl(sizeof(*info));
316                 fcp->resp.fr_flags |= FCP_RSP_LEN_VAL;
317                 info = (struct fcp_resp_rsp_info *)(fcp + 1);
318                 info->rsp_code = code;
319         }
320
321         fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_DD_CMD_STATUS, 0);
322         sp = fr_seq(fp);
323         if (sp)
324                 lport->tt.seq_send(lport, sp, fp);
325         else
326                 lport->tt.frame_send(lport, fp);
327 }
328
329 /*
330  * Send error or task management response.
331  */
332 static void ft_send_resp_code(struct ft_cmd *cmd,
333                               enum fcp_resp_rsp_codes code)
334 {
335         ft_send_resp_status(cmd->sess->tport->lport,
336                             cmd->req_frame, SAM_STAT_GOOD, code);
337 }
338
339
340 /*
341  * Send error or task management response.
342  * Always frees the cmd and associated state.
343  */
344 static void ft_send_resp_code_and_free(struct ft_cmd *cmd,
345                                       enum fcp_resp_rsp_codes code)
346 {
347         ft_send_resp_code(cmd, code);
348         ft_free_cmd(cmd);
349 }
350
351 /*
352  * Handle Task Management Request.
353  */
354 static void ft_send_tm(struct ft_cmd *cmd)
355 {
356         struct fcp_cmnd *fcp;
357         struct ft_sess *sess;
358         int rc;
359         u8 tm_func;
360
361         fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
362
363         switch (fcp->fc_tm_flags) {
364         case FCP_TMF_LUN_RESET:
365                 tm_func = TMR_LUN_RESET;
366                 break;
367         case FCP_TMF_TGT_RESET:
368                 tm_func = TMR_TARGET_WARM_RESET;
369                 break;
370         case FCP_TMF_CLR_TASK_SET:
371                 tm_func = TMR_CLEAR_TASK_SET;
372                 break;
373         case FCP_TMF_ABT_TASK_SET:
374                 tm_func = TMR_ABORT_TASK_SET;
375                 break;
376         case FCP_TMF_CLR_ACA:
377                 tm_func = TMR_CLEAR_ACA;
378                 break;
379         default:
380                 /*
381                  * FCP4r01 indicates having a combination of
382                  * tm_flags set is invalid.
383                  */
384                 pr_debug("invalid FCP tm_flags %x\n", fcp->fc_tm_flags);
385                 ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
386                 return;
387         }
388
389         transport_init_se_cmd(&cmd->se_cmd, &ft_configfs->tf_ops,
390                         cmd->sess->se_sess, 0, DMA_NONE, 0,
391                         &cmd->ft_sense_buffer[0]);
392         target_get_sess_cmd(cmd->sess->se_sess, &cmd->se_cmd, false);
393
394         pr_debug("alloc tm cmd fn %d\n", tm_func);
395         rc = core_tmr_alloc_req(&cmd->se_cmd, cmd, tm_func, GFP_KERNEL);
396         if (rc < 0) {
397                 pr_debug("alloc failed\n");
398                 ft_send_resp_code_and_free(cmd, FCP_TMF_FAILED);
399                 return;
400         }
401
402         switch (fcp->fc_tm_flags) {
403         case FCP_TMF_LUN_RESET:
404                 if (transport_lookup_tmr_lun(&cmd->se_cmd, scsilun_to_int(&fcp->fc_lun)) < 0) {
405                         /*
406                          * Make sure to clean up newly allocated TMR request
407                          * since "unable to  handle TMR request because failed
408                          * to get to LUN"
409                          */
410                         pr_debug("Failed to get LUN for TMR func %d, "
411                                   "se_cmd %p, unpacked_lun %d\n",
412                                   tm_func, &cmd->se_cmd, scsilun_to_int(&fcp->fc_lun));
413                         ft_dump_cmd(cmd, __func__);
414                         sess = cmd->sess;
415                         transport_send_check_condition_and_sense(&cmd->se_cmd,
416                                 cmd->se_cmd.scsi_sense_reason, 0);
417                         ft_sess_put(sess);
418                         return;
419                 }
420                 break;
421         case FCP_TMF_TGT_RESET:
422         case FCP_TMF_CLR_TASK_SET:
423         case FCP_TMF_ABT_TASK_SET:
424         case FCP_TMF_CLR_ACA:
425                 break;
426         default:
427                 return;
428         }
429         transport_generic_handle_tmr(&cmd->se_cmd);
430 }
431
432 /*
433  * Send status from completed task management request.
434  */
435 int ft_queue_tm_resp(struct se_cmd *se_cmd)
436 {
437         struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
438         struct se_tmr_req *tmr = se_cmd->se_tmr_req;
439         enum fcp_resp_rsp_codes code;
440
441         switch (tmr->response) {
442         case TMR_FUNCTION_COMPLETE:
443                 code = FCP_TMF_CMPL;
444                 break;
445         case TMR_LUN_DOES_NOT_EXIST:
446                 code = FCP_TMF_INVALID_LUN;
447                 break;
448         case TMR_FUNCTION_REJECTED:
449                 code = FCP_TMF_REJECTED;
450                 break;
451         case TMR_TASK_DOES_NOT_EXIST:
452         case TMR_TASK_STILL_ALLEGIANT:
453         case TMR_TASK_FAILOVER_NOT_SUPPORTED:
454         case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
455         case TMR_FUNCTION_AUTHORIZATION_FAILED:
456         default:
457                 code = FCP_TMF_FAILED;
458                 break;
459         }
460         pr_debug("tmr fn %d resp %d fcp code %d\n",
461                   tmr->function, tmr->response, code);
462         ft_send_resp_code(cmd, code);
463         return 0;
464 }
465
466 static void ft_send_work(struct work_struct *work);
467
468 /*
469  * Handle incoming FCP command.
470  */
471 static void ft_recv_cmd(struct ft_sess *sess, struct fc_frame *fp)
472 {
473         struct ft_cmd *cmd;
474         struct fc_lport *lport = sess->tport->lport;
475
476         cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
477         if (!cmd)
478                 goto busy;
479         cmd->sess = sess;
480         cmd->seq = lport->tt.seq_assign(lport, fp);
481         if (!cmd->seq) {
482                 kfree(cmd);
483                 goto busy;
484         }
485         cmd->req_frame = fp;            /* hold frame during cmd */
486
487         INIT_WORK(&cmd->work, ft_send_work);
488         queue_work(sess->tport->tpg->workqueue, &cmd->work);
489         return;
490
491 busy:
492         pr_debug("cmd or seq allocation failure - sending BUSY\n");
493         ft_send_resp_status(lport, fp, SAM_STAT_BUSY, 0);
494         fc_frame_free(fp);
495         ft_sess_put(sess);              /* undo get from lookup */
496 }
497
498
499 /*
500  * Handle incoming FCP frame.
501  * Caller has verified that the frame is type FCP.
502  */
503 void ft_recv_req(struct ft_sess *sess, struct fc_frame *fp)
504 {
505         struct fc_frame_header *fh = fc_frame_header_get(fp);
506
507         switch (fh->fh_r_ctl) {
508         case FC_RCTL_DD_UNSOL_CMD:      /* command */
509                 ft_recv_cmd(sess, fp);
510                 break;
511         case FC_RCTL_DD_SOL_DATA:       /* write data */
512         case FC_RCTL_DD_UNSOL_CTL:
513         case FC_RCTL_DD_SOL_CTL:
514         case FC_RCTL_DD_DATA_DESC:      /* transfer ready */
515         case FC_RCTL_ELS4_REQ:          /* SRR, perhaps */
516         default:
517                 pr_debug("%s: unhandled frame r_ctl %x\n",
518                        __func__, fh->fh_r_ctl);
519                 fc_frame_free(fp);
520                 ft_sess_put(sess);      /* undo get from lookup */
521                 break;
522         }
523 }
524
525 /*
526  * Send new command to target.
527  */
528 static void ft_send_work(struct work_struct *work)
529 {
530         struct ft_cmd *cmd = container_of(work, struct ft_cmd, work);
531         struct fc_frame_header *fh = fc_frame_header_get(cmd->req_frame);
532         struct fcp_cmnd *fcp;
533         int data_dir = 0;
534         int task_attr;
535
536         fcp = fc_frame_payload_get(cmd->req_frame, sizeof(*fcp));
537         if (!fcp)
538                 goto err;
539
540         if (fcp->fc_flags & FCP_CFL_LEN_MASK)
541                 goto err;               /* not handling longer CDBs yet */
542
543         /*
544          * Check for FCP task management flags
545          */
546         if (fcp->fc_tm_flags) {
547                 ft_send_tm(cmd);
548                 return;
549         }
550
551         switch (fcp->fc_flags & (FCP_CFL_RDDATA | FCP_CFL_WRDATA)) {
552         case 0:
553                 data_dir = DMA_NONE;
554                 break;
555         case FCP_CFL_RDDATA:
556                 data_dir = DMA_FROM_DEVICE;
557                 break;
558         case FCP_CFL_WRDATA:
559                 data_dir = DMA_TO_DEVICE;
560                 break;
561         case FCP_CFL_WRDATA | FCP_CFL_RDDATA:
562                 goto err;       /* TBD not supported by tcm_fc yet */
563         }
564         /*
565          * Locate the SAM Task Attr from fc_pri_ta
566          */
567         switch (fcp->fc_pri_ta & FCP_PTA_MASK) {
568         case FCP_PTA_HEADQ:
569                 task_attr = MSG_HEAD_TAG;
570                 break;
571         case FCP_PTA_ORDERED:
572                 task_attr = MSG_ORDERED_TAG;
573                 break;
574         case FCP_PTA_ACA:
575                 task_attr = MSG_ACA_TAG;
576                 break;
577         case FCP_PTA_SIMPLE: /* Fallthrough */
578         default:
579                 task_attr = MSG_SIMPLE_TAG;
580         }
581
582         fc_seq_exch(cmd->seq)->lp->tt.seq_set_resp(cmd->seq, ft_recv_seq, cmd);
583         /*
584          * Use a single se_cmd->cmd_kref as we expect to release se_cmd
585          * directly from ft_check_stop_free callback in response path.
586          */
587         target_submit_cmd(&cmd->se_cmd, cmd->sess->se_sess, fcp->fc_cdb,
588                         &cmd->ft_sense_buffer[0], scsilun_to_int(&fcp->fc_lun),
589                         ntohl(fcp->fc_dl), task_attr, data_dir, 0);
590         pr_debug("r_ctl %x alloc target_submit_cmd\n", fh->fh_r_ctl);
591         return;
592
593 err:
594         ft_send_resp_code_and_free(cmd, FCP_CMND_FIELDS_INVALID);
595 }