]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/scsi/libfc/fc_fcp.c
59a4408b27b5b4d4bd3eb248e66442a9c7c98ba7
[net-next-2.6.git] / drivers / scsi / libfc / fc_fcp.c
1 /*
2  * Copyright(c) 2007 Intel Corporation. All rights reserved.
3  * Copyright(c) 2008 Red Hat, Inc.  All rights reserved.
4  * Copyright(c) 2008 Mike Christie
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Maintained at www.Open-FCoE.org
20  */
21
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/spinlock.h>
27 #include <linux/scatterlist.h>
28 #include <linux/err.h>
29 #include <linux/crc32.h>
30
31 #include <scsi/scsi_tcq.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
37 #include <scsi/fc/fc_fc2.h>
38
39 #include <scsi/libfc.h>
40 #include <scsi/fc_encode.h>
41
42 MODULE_AUTHOR("Open-FCoE.org");
43 MODULE_DESCRIPTION("libfc");
44 MODULE_LICENSE("GPL v2");
45
46 unsigned int fc_debug_logging;
47 module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
48 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
49
50 static struct kmem_cache *scsi_pkt_cachep;
51
52 /* SRB state definitions */
53 #define FC_SRB_FREE             0               /* cmd is free */
54 #define FC_SRB_CMD_SENT         (1 << 0)        /* cmd has been sent */
55 #define FC_SRB_RCV_STATUS       (1 << 1)        /* response has arrived */
56 #define FC_SRB_ABORT_PENDING    (1 << 2)        /* cmd abort sent to device */
57 #define FC_SRB_ABORTED          (1 << 3)        /* abort acknowleged */
58 #define FC_SRB_DISCONTIG        (1 << 4)        /* non-sequential data recvd */
59 #define FC_SRB_COMPL            (1 << 5)        /* fc_io_compl has been run */
60 #define FC_SRB_FCP_PROCESSING_TMO (1 << 6)      /* timer function processing */
61 #define FC_SRB_NOMEM            (1 << 7)        /* dropped to out of mem */
62
63 #define FC_SRB_READ             (1 << 1)
64 #define FC_SRB_WRITE            (1 << 0)
65
66 /*
67  * The SCp.ptr should be tested and set under the host lock. NULL indicates
68  * that the command has been retruned to the scsi layer.
69  */
70 #define CMD_SP(Cmnd)                ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
71 #define CMD_ENTRY_STATUS(Cmnd)      ((Cmnd)->SCp.have_data_in)
72 #define CMD_COMPL_STATUS(Cmnd)      ((Cmnd)->SCp.this_residual)
73 #define CMD_SCSI_STATUS(Cmnd)       ((Cmnd)->SCp.Status)
74 #define CMD_RESID_LEN(Cmnd)         ((Cmnd)->SCp.buffers_residual)
75
76 struct fc_fcp_internal {
77         mempool_t       *scsi_pkt_pool;
78         struct list_head scsi_pkt_queue;
79         u8              throttled;
80 };
81
82 #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
83
84 /*
85  * function prototypes
86  * FC scsi I/O related functions
87  */
88 static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
89 static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
90 static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
91 static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
92 static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
93 static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp);
94 static void fc_timeout_error(struct fc_fcp_pkt *);
95 static void fc_fcp_timeout(unsigned long data);
96 static void fc_fcp_rec(struct fc_fcp_pkt *);
97 static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
98 static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
99 static void fc_io_compl(struct fc_fcp_pkt *);
100
101 static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
102 static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
103 static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
104
105 /*
106  * command status codes
107  */
108 #define FC_COMPLETE             0
109 #define FC_CMD_ABORTED          1
110 #define FC_CMD_RESET            2
111 #define FC_CMD_PLOGO            3
112 #define FC_SNS_RCV              4
113 #define FC_TRANS_ERR            5
114 #define FC_DATA_OVRRUN          6
115 #define FC_DATA_UNDRUN          7
116 #define FC_ERROR                8
117 #define FC_HRD_ERROR            9
118 #define FC_CMD_TIME_OUT         10
119
120 /*
121  * Error recovery timeout values.
122  */
123 #define FC_SCSI_ER_TIMEOUT      (10 * HZ)
124 #define FC_SCSI_TM_TOV          (10 * HZ)
125 #define FC_SCSI_REC_TOV         (2 * HZ)
126 #define FC_HOST_RESET_TIMEOUT   (30 * HZ)
127
128 #define FC_MAX_ERROR_CNT        5
129 #define FC_MAX_RECOV_RETRY      3
130
131 #define FC_FCP_DFLT_QUEUE_DEPTH 32
132
133 /**
134  * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
135  * @lp:         fc lport struct
136  * @gfp:        gfp flags for allocation
137  *
138  * This is used by upper layer scsi driver.
139  * Return Value : scsi_pkt structure or null on allocation failure.
140  * Context      : call from process context. no locking required.
141  */
142 static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp)
143 {
144         struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
145         struct fc_fcp_pkt *fsp;
146
147         fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
148         if (fsp) {
149                 memset(fsp, 0, sizeof(*fsp));
150                 fsp->lp = lp;
151                 atomic_set(&fsp->ref_cnt, 1);
152                 init_timer(&fsp->timer);
153                 INIT_LIST_HEAD(&fsp->list);
154                 spin_lock_init(&fsp->scsi_pkt_lock);
155         }
156         return fsp;
157 }
158
159 /**
160  * fc_fcp_pkt_release() - release hold on scsi_pkt packet
161  * @fsp:        fcp packet struct
162  *
163  * This is used by upper layer scsi driver.
164  * Context      : call from process  and interrupt context.
165  *                no locking required
166  */
167 static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
168 {
169         if (atomic_dec_and_test(&fsp->ref_cnt)) {
170                 struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
171
172                 mempool_free(fsp, si->scsi_pkt_pool);
173         }
174 }
175
176 static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
177 {
178         atomic_inc(&fsp->ref_cnt);
179 }
180
181 /**
182  * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
183  * @seq:                exchange sequence
184  * @fsp:        fcp packet struct
185  *
186  * Release hold on scsi_pkt packet set to keep scsi_pkt
187  * till EM layer exch resource is not freed.
188  * Context      : called from from EM layer.
189  *                no locking required
190  */
191 static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
192 {
193         fc_fcp_pkt_release(fsp);
194 }
195
196 /**
197  * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
198  * @fsp:        fcp packet
199  *
200  * We should only return error if we return a command to scsi-ml before
201  * getting a response. This can happen in cases where we send a abort, but
202  * do not wait for the response and the abort and command can be passing
203  * each other on the wire/network-layer.
204  *
205  * Note: this function locks the packet and gets a reference to allow
206  * callers to call the completion function while the lock is held and
207  * not have to worry about the packets refcount.
208  *
209  * TODO: Maybe we should just have callers grab/release the lock and
210  * have a function that they call to verify the fsp and grab a ref if
211  * needed.
212  */
213 static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
214 {
215         spin_lock_bh(&fsp->scsi_pkt_lock);
216         if (fsp->state & FC_SRB_COMPL) {
217                 spin_unlock_bh(&fsp->scsi_pkt_lock);
218                 return -EPERM;
219         }
220
221         fc_fcp_pkt_hold(fsp);
222         return 0;
223 }
224
225 static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
226 {
227         spin_unlock_bh(&fsp->scsi_pkt_lock);
228         fc_fcp_pkt_release(fsp);
229 }
230
231 static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
232 {
233         if (!(fsp->state & FC_SRB_COMPL))
234                 mod_timer(&fsp->timer, jiffies + delay);
235 }
236
237 static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
238 {
239         if (!fsp->seq_ptr)
240                 return -EINVAL;
241
242         fsp->state |= FC_SRB_ABORT_PENDING;
243         return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
244 }
245
246 /*
247  * Retry command.
248  * An abort isn't needed.
249  */
250 static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
251 {
252         if (fsp->seq_ptr) {
253                 fsp->lp->tt.exch_done(fsp->seq_ptr);
254                 fsp->seq_ptr = NULL;
255         }
256
257         fsp->state &= ~FC_SRB_ABORT_PENDING;
258         fsp->io_status = 0;
259         fsp->status_code = FC_ERROR;
260         fc_fcp_complete_locked(fsp);
261 }
262
263 /*
264  * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
265  * transfer for a read I/O indicated by the fc_fcp_pkt.
266  * @fsp: ptr to the fc_fcp_pkt
267  *
268  * This is called in exch_seq_send() when we have a newly allocated
269  * exchange with a valid exchange id to setup ddp.
270  *
271  * returns: none
272  */
273 void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
274 {
275         struct fc_lport *lp;
276
277         if (!fsp)
278                 return;
279
280         lp = fsp->lp;
281         if ((fsp->req_flags & FC_SRB_READ) &&
282             (lp->lro_enabled) && (lp->tt.ddp_setup)) {
283                 if (lp->tt.ddp_setup(lp, xid, scsi_sglist(fsp->cmd),
284                                      scsi_sg_count(fsp->cmd)))
285                         fsp->xfer_ddp = xid;
286         }
287 }
288 EXPORT_SYMBOL(fc_fcp_ddp_setup);
289
290 /*
291  * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
292  * DDP related resources for this I/O if it is initialized
293  * as a ddp transfer
294  * @fsp: ptr to the fc_fcp_pkt
295  *
296  * returns: none
297  */
298 static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
299 {
300         struct fc_lport *lp;
301
302         if (!fsp)
303                 return;
304
305         lp = fsp->lp;
306         if (fsp->xfer_ddp && lp->tt.ddp_done) {
307                 fsp->xfer_len = lp->tt.ddp_done(lp, fsp->xfer_ddp);
308                 fsp->xfer_ddp = 0;
309         }
310 }
311
312
313 /*
314  * Receive SCSI data from target.
315  * Called after receiving solicited data.
316  */
317 static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
318 {
319         struct scsi_cmnd *sc = fsp->cmd;
320         struct fc_lport *lp = fsp->lp;
321         struct fcoe_dev_stats *stats;
322         struct fc_frame_header *fh;
323         size_t start_offset;
324         size_t offset;
325         u32 crc;
326         u32 copy_len = 0;
327         size_t len;
328         void *buf;
329         struct scatterlist *sg;
330         size_t remaining;
331
332         fh = fc_frame_header_get(fp);
333         offset = ntohl(fh->fh_parm_offset);
334         start_offset = offset;
335         len = fr_len(fp) - sizeof(*fh);
336         buf = fc_frame_payload_get(fp, 0);
337
338         /* if this I/O is ddped, update xfer len */
339         fc_fcp_ddp_done(fsp);
340
341         if (offset + len > fsp->data_len) {
342                 /* this should never happen */
343                 if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
344                     fc_frame_crc_check(fp))
345                         goto crc_err;
346                 FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
347                            "data_len %x\n", len, offset, fsp->data_len);
348                 fc_fcp_retry_cmd(fsp);
349                 return;
350         }
351         if (offset != fsp->xfer_len)
352                 fsp->state |= FC_SRB_DISCONTIG;
353
354         crc = 0;
355         if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
356                 crc = crc32(~0, (u8 *) fh, sizeof(*fh));
357
358         sg = scsi_sglist(sc);
359         remaining = len;
360
361         while (remaining > 0 && sg) {
362                 size_t off;
363                 void *page_addr;
364                 size_t sg_bytes;
365
366                 if (offset >= sg->length) {
367                         offset -= sg->length;
368                         sg = sg_next(sg);
369                         continue;
370                 }
371                 sg_bytes = min(remaining, sg->length - offset);
372
373                 /*
374                  * The scatterlist item may be bigger than PAGE_SIZE,
375                  * but we are limited to mapping PAGE_SIZE at a time.
376                  */
377                 off = offset + sg->offset;
378                 sg_bytes = min(sg_bytes, (size_t)
379                                (PAGE_SIZE - (off & ~PAGE_MASK)));
380                 page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
381                                         KM_SOFTIRQ0);
382                 if (!page_addr)
383                         break;          /* XXX panic? */
384
385                 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
386                         crc = crc32(crc, buf, sg_bytes);
387                 memcpy((char *)page_addr + (off & ~PAGE_MASK), buf,
388                        sg_bytes);
389
390                 kunmap_atomic(page_addr, KM_SOFTIRQ0);
391                 buf += sg_bytes;
392                 offset += sg_bytes;
393                 remaining -= sg_bytes;
394                 copy_len += sg_bytes;
395         }
396
397         if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
398                 buf = fc_frame_payload_get(fp, 0);
399                 if (len % 4) {
400                         crc = crc32(crc, buf + len, 4 - (len % 4));
401                         len += 4 - (len % 4);
402                 }
403
404                 if (~crc != le32_to_cpu(fr_crc(fp))) {
405 crc_err:
406                         stats = fc_lport_get_stats(lp);
407                         stats->ErrorFrames++;
408                         /* FIXME - per cpu count, not total count! */
409                         if (stats->InvalidCRCCount++ < 5)
410                                 printk(KERN_WARNING "libfc: CRC error on data "
411                                        "frame for port (%6x)\n",
412                                        fc_host_port_id(lp->host));
413                         /*
414                          * Assume the frame is total garbage.
415                          * We may have copied it over the good part
416                          * of the buffer.
417                          * If so, we need to retry the entire operation.
418                          * Otherwise, ignore it.
419                          */
420                         if (fsp->state & FC_SRB_DISCONTIG)
421                                 fc_fcp_retry_cmd(fsp);
422                         return;
423                 }
424         }
425
426         if (fsp->xfer_contig_end == start_offset)
427                 fsp->xfer_contig_end += copy_len;
428         fsp->xfer_len += copy_len;
429
430         /*
431          * In the very rare event that this data arrived after the response
432          * and completes the transfer, call the completion handler.
433          */
434         if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
435             fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
436                 fc_fcp_complete_locked(fsp);
437 }
438
439 /**
440  * fc_fcp_send_data() -  Send SCSI data to target.
441  * @fsp: ptr to fc_fcp_pkt
442  * @sp: ptr to this sequence
443  * @offset: starting offset for this data request
444  * @seq_blen: the burst length for this data request
445  *
446  * Called after receiving a Transfer Ready data descriptor.
447  * if LLD is capable of seq offload then send down seq_blen
448  * size of data in single frame, otherwise send multiple FC
449  * frames of max FC frame payload supported by target port.
450  *
451  * Returns : 0 for success.
452  */
453 static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
454                             size_t offset, size_t seq_blen)
455 {
456         struct fc_exch *ep;
457         struct scsi_cmnd *sc;
458         struct scatterlist *sg;
459         struct fc_frame *fp = NULL;
460         struct fc_lport *lp = fsp->lp;
461         size_t remaining;
462         size_t t_blen;
463         size_t tlen;
464         size_t sg_bytes;
465         size_t frame_offset, fh_parm_offset;
466         int error;
467         void *data = NULL;
468         void *page_addr;
469         int using_sg = lp->sg_supp;
470         u32 f_ctl;
471
472         WARN_ON(seq_blen <= 0);
473         if (unlikely(offset + seq_blen > fsp->data_len)) {
474                 /* this should never happen */
475                 FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
476                            "offset %zx\n", seq_blen, offset);
477                 fc_fcp_send_abort(fsp);
478                 return 0;
479         } else if (offset != fsp->xfer_len) {
480                 /* Out of Order Data Request - no problem, but unexpected. */
481                 FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
482                            "seq_blen %zx offset %zx\n", seq_blen, offset);
483         }
484
485         /*
486          * if LLD is capable of seq_offload then set transport
487          * burst length (t_blen) to seq_blen, otherwise set t_blen
488          * to max FC frame payload previously set in fsp->max_payload.
489          */
490         t_blen = fsp->max_payload;
491         if (lp->seq_offload) {
492                 t_blen = min(seq_blen, (size_t)lp->lso_max);
493                 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
494                            fsp, seq_blen, lp->lso_max, t_blen);
495         }
496
497         WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
498         if (t_blen > 512)
499                 t_blen &= ~(512 - 1);   /* round down to block size */
500         WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);   /* won't go below 256 */
501         sc = fsp->cmd;
502
503         remaining = seq_blen;
504         fh_parm_offset = frame_offset = offset;
505         tlen = 0;
506         seq = lp->tt.seq_start_next(seq);
507         f_ctl = FC_FC_REL_OFF;
508         WARN_ON(!seq);
509
510         sg = scsi_sglist(sc);
511
512         while (remaining > 0 && sg) {
513                 if (offset >= sg->length) {
514                         offset -= sg->length;
515                         sg = sg_next(sg);
516                         continue;
517                 }
518                 if (!fp) {
519                         tlen = min(t_blen, remaining);
520
521                         /*
522                          * TODO.  Temporary workaround.  fc_seq_send() can't
523                          * handle odd lengths in non-linear skbs.
524                          * This will be the final fragment only.
525                          */
526                         if (tlen % 4)
527                                 using_sg = 0;
528                         if (using_sg) {
529                                 fp = _fc_frame_alloc(lp, 0);
530                                 if (!fp)
531                                         return -ENOMEM;
532                         } else {
533                                 fp = fc_frame_alloc(lp, tlen);
534                                 if (!fp)
535                                         return -ENOMEM;
536
537                                 data = (void *)(fr_hdr(fp)) +
538                                         sizeof(struct fc_frame_header);
539                         }
540                         fh_parm_offset = frame_offset;
541                         fr_max_payload(fp) = fsp->max_payload;
542                 }
543                 sg_bytes = min(tlen, sg->length - offset);
544                 if (using_sg) {
545                         get_page(sg_page(sg));
546                         skb_fill_page_desc(fp_skb(fp),
547                                            skb_shinfo(fp_skb(fp))->nr_frags,
548                                            sg_page(sg), sg->offset + offset,
549                                            sg_bytes);
550                         fp_skb(fp)->data_len += sg_bytes;
551                         fr_len(fp) += sg_bytes;
552                         fp_skb(fp)->truesize += PAGE_SIZE;
553                 } else {
554                         size_t off = offset + sg->offset;
555
556                         /*
557                          * The scatterlist item may be bigger than PAGE_SIZE,
558                          * but we must not cross pages inside the kmap.
559                          */
560                         sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
561                                                            (off & ~PAGE_MASK)));
562                         page_addr = kmap_atomic(sg_page(sg) +
563                                                 (off >> PAGE_SHIFT),
564                                                 KM_SOFTIRQ0);
565                         memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
566                                sg_bytes);
567                         kunmap_atomic(page_addr, KM_SOFTIRQ0);
568                         data += sg_bytes;
569                 }
570                 offset += sg_bytes;
571                 frame_offset += sg_bytes;
572                 tlen -= sg_bytes;
573                 remaining -= sg_bytes;
574
575                 if (tlen)
576                         continue;
577
578                 /*
579                  * Send sequence with transfer sequence initiative in case
580                  * this is last FCP frame of the sequence.
581                  */
582                 if (remaining == 0)
583                         f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
584
585                 ep = fc_seq_exch(seq);
586                 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
587                                FC_TYPE_FCP, f_ctl, fh_parm_offset);
588
589                 /*
590                  * send fragment using for a sequence.
591                  */
592                 error = lp->tt.seq_send(lp, seq, fp);
593                 if (error) {
594                         WARN_ON(1);             /* send error should be rare */
595                         fc_fcp_retry_cmd(fsp);
596                         return 0;
597                 }
598                 fp = NULL;
599         }
600         fsp->xfer_len += seq_blen;      /* premature count? */
601         return 0;
602 }
603
604 static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
605 {
606         int ba_done = 1;
607         struct fc_ba_rjt *brp;
608         struct fc_frame_header *fh;
609
610         fh = fc_frame_header_get(fp);
611         switch (fh->fh_r_ctl) {
612         case FC_RCTL_BA_ACC:
613                 break;
614         case FC_RCTL_BA_RJT:
615                 brp = fc_frame_payload_get(fp, sizeof(*brp));
616                 if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
617                         break;
618                 /* fall thru */
619         default:
620                 /*
621                  * we will let the command timeout
622                  * and scsi-ml recover in this case,
623                  * therefore cleared the ba_done flag.
624                  */
625                 ba_done = 0;
626         }
627
628         if (ba_done) {
629                 fsp->state |= FC_SRB_ABORTED;
630                 fsp->state &= ~FC_SRB_ABORT_PENDING;
631
632                 if (fsp->wait_for_comp)
633                         complete(&fsp->tm_done);
634                 else
635                         fc_fcp_complete_locked(fsp);
636         }
637 }
638
639 /**
640  * fc_fcp_reduce_can_queue() - drop can_queue
641  * @lp: lport to drop queueing for
642  *
643  * If we are getting memory allocation failures, then we may
644  * be trying to execute too many commands. We let the running
645  * commands complete or timeout, then try again with a reduced
646  * can_queue. Eventually we will hit the point where we run
647  * on all reserved structs.
648  */
649 static void fc_fcp_reduce_can_queue(struct fc_lport *lp)
650 {
651         struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
652         unsigned long flags;
653         int can_queue;
654
655         spin_lock_irqsave(lp->host->host_lock, flags);
656         if (si->throttled)
657                 goto done;
658         si->throttled = 1;
659
660         can_queue = lp->host->can_queue;
661         can_queue >>= 1;
662         if (!can_queue)
663                 can_queue = 1;
664         lp->host->can_queue = can_queue;
665         shost_printk(KERN_ERR, lp->host, "libfc: Could not allocate frame.\n"
666                      "Reducing can_queue to %d.\n", can_queue);
667 done:
668         spin_unlock_irqrestore(lp->host->host_lock, flags);
669 }
670
671 /**
672  * fc_fcp_recv() - Reveive FCP frames
673  * @seq: The sequence the frame is on
674  * @fp: The FC frame
675  * @arg: The related FCP packet
676  *
677  * Return   : None
678  * Context  : called from Soft IRQ context
679  *            can not called holding list lock
680  */
681 static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
682 {
683         struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
684         struct fc_lport *lport = fsp->lp;
685         struct fc_frame_header *fh;
686         struct fcp_txrdy *dd;
687         u8 r_ctl;
688         int rc = 0;
689
690         if (IS_ERR(fp))
691                 goto errout;
692
693         fh = fc_frame_header_get(fp);
694         r_ctl = fh->fh_r_ctl;
695
696         if (!(lport->state & LPORT_ST_READY))
697                 goto out;
698         if (fc_fcp_lock_pkt(fsp))
699                 goto out;
700         fsp->last_pkt_time = jiffies;
701
702         if (fh->fh_type == FC_TYPE_BLS) {
703                 fc_fcp_abts_resp(fsp, fp);
704                 goto unlock;
705         }
706
707         if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
708                 goto unlock;
709
710         if (r_ctl == FC_RCTL_DD_DATA_DESC) {
711                 /*
712                  * received XFER RDY from the target
713                  * need to send data to the target
714                  */
715                 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
716                 dd = fc_frame_payload_get(fp, sizeof(*dd));
717                 WARN_ON(!dd);
718
719                 rc = fc_fcp_send_data(fsp, seq,
720                                       (size_t) ntohl(dd->ft_data_ro),
721                                       (size_t) ntohl(dd->ft_burst_len));
722                 if (!rc)
723                         seq->rec_data = fsp->xfer_len;
724                 else if (rc == -ENOMEM)
725                         fsp->state |= FC_SRB_NOMEM;
726         } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
727                 /*
728                  * received a DATA frame
729                  * next we will copy the data to the system buffer
730                  */
731                 WARN_ON(fr_len(fp) < sizeof(*fh));      /* len may be 0 */
732                 fc_fcp_recv_data(fsp, fp);
733                 seq->rec_data = fsp->xfer_contig_end;
734         } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
735                 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
736
737                 fc_fcp_resp(fsp, fp);
738         } else {
739                 FC_FCP_DBG(fsp, "unexpected frame.  r_ctl %x\n", r_ctl);
740         }
741 unlock:
742         fc_fcp_unlock_pkt(fsp);
743 out:
744         fc_frame_free(fp);
745 errout:
746         if (IS_ERR(fp))
747                 fc_fcp_error(fsp, fp);
748         else if (rc == -ENOMEM)
749                 fc_fcp_reduce_can_queue(lport);
750 }
751
752 static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
753 {
754         struct fc_frame_header *fh;
755         struct fcp_resp *fc_rp;
756         struct fcp_resp_ext *rp_ex;
757         struct fcp_resp_rsp_info *fc_rp_info;
758         u32 plen;
759         u32 expected_len;
760         u32 respl = 0;
761         u32 snsl = 0;
762         u8 flags = 0;
763
764         plen = fr_len(fp);
765         fh = (struct fc_frame_header *)fr_hdr(fp);
766         if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
767                 goto len_err;
768         plen -= sizeof(*fh);
769         fc_rp = (struct fcp_resp *)(fh + 1);
770         fsp->cdb_status = fc_rp->fr_status;
771         flags = fc_rp->fr_flags;
772         fsp->scsi_comp_flags = flags;
773         expected_len = fsp->data_len;
774
775         /* if ddp, update xfer len */
776         fc_fcp_ddp_done(fsp);
777
778         if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
779                 rp_ex = (void *)(fc_rp + 1);
780                 if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
781                         if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
782                                 goto len_err;
783                         fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
784                         if (flags & FCP_RSP_LEN_VAL) {
785                                 respl = ntohl(rp_ex->fr_rsp_len);
786                                 if (respl != sizeof(*fc_rp_info))
787                                         goto len_err;
788                                 if (fsp->wait_for_comp) {
789                                         /* Abuse cdb_status for rsp code */
790                                         fsp->cdb_status = fc_rp_info->rsp_code;
791                                         complete(&fsp->tm_done);
792                                         /*
793                                          * tmfs will not have any scsi cmd so
794                                          * exit here
795                                          */
796                                         return;
797                                 } else
798                                         goto err;
799                         }
800                         if (flags & FCP_SNS_LEN_VAL) {
801                                 snsl = ntohl(rp_ex->fr_sns_len);
802                                 if (snsl > SCSI_SENSE_BUFFERSIZE)
803                                         snsl = SCSI_SENSE_BUFFERSIZE;
804                                 memcpy(fsp->cmd->sense_buffer,
805                                        (char *)fc_rp_info + respl, snsl);
806                         }
807                 }
808                 if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
809                         if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
810                                 goto len_err;
811                         if (flags & FCP_RESID_UNDER) {
812                                 fsp->scsi_resid = ntohl(rp_ex->fr_resid);
813                                 /*
814                                  * The cmnd->underflow is the minimum number of
815                                  * bytes that must be transfered for this
816                                  * command.  Provided a sense condition is not
817                                  * present, make sure the actual amount
818                                  * transferred is at least the underflow value
819                                  * or fail.
820                                  */
821                                 if (!(flags & FCP_SNS_LEN_VAL) &&
822                                     (fc_rp->fr_status == 0) &&
823                                     (scsi_bufflen(fsp->cmd) -
824                                      fsp->scsi_resid) < fsp->cmd->underflow)
825                                         goto err;
826                                 expected_len -= fsp->scsi_resid;
827                         } else {
828                                 fsp->status_code = FC_ERROR;
829                         }
830                 }
831         }
832         fsp->state |= FC_SRB_RCV_STATUS;
833
834         /*
835          * Check for missing or extra data frames.
836          */
837         if (unlikely(fsp->xfer_len != expected_len)) {
838                 if (fsp->xfer_len < expected_len) {
839                         /*
840                          * Some data may be queued locally,
841                          * Wait a at least one jiffy to see if it is delivered.
842                          * If this expires without data, we may do SRR.
843                          */
844                         fc_fcp_timer_set(fsp, 2);
845                         return;
846                 }
847                 fsp->status_code = FC_DATA_OVRRUN;
848                 FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
849                            "len %x, data len %x\n",
850                            fsp->rport->port_id,
851                            fsp->xfer_len, expected_len, fsp->data_len);
852         }
853         fc_fcp_complete_locked(fsp);
854         return;
855
856 len_err:
857         FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
858                    "snsl %u\n", flags, fr_len(fp), respl, snsl);
859 err:
860         fsp->status_code = FC_ERROR;
861         fc_fcp_complete_locked(fsp);
862 }
863
864 /**
865  * fc_fcp_complete_locked() - complete processing of a fcp packet
866  * @fsp:        fcp packet
867  *
868  * This function may sleep if a timer is pending. The packet lock must be
869  * held, and the host lock must not be held.
870  */
871 static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
872 {
873         struct fc_lport *lp = fsp->lp;
874         struct fc_seq *seq;
875         struct fc_exch *ep;
876         u32 f_ctl;
877
878         if (fsp->state & FC_SRB_ABORT_PENDING)
879                 return;
880
881         if (fsp->state & FC_SRB_ABORTED) {
882                 if (!fsp->status_code)
883                         fsp->status_code = FC_CMD_ABORTED;
884         } else {
885                 /*
886                  * Test for transport underrun, independent of response
887                  * underrun status.
888                  */
889                 if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
890                     (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
891                      fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
892                         fsp->status_code = FC_DATA_UNDRUN;
893                         fsp->io_status = 0;
894                 }
895         }
896
897         seq = fsp->seq_ptr;
898         if (seq) {
899                 fsp->seq_ptr = NULL;
900                 if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
901                         struct fc_frame *conf_frame;
902                         struct fc_seq *csp;
903
904                         csp = lp->tt.seq_start_next(seq);
905                         conf_frame = fc_frame_alloc(fsp->lp, 0);
906                         if (conf_frame) {
907                                 f_ctl = FC_FC_SEQ_INIT;
908                                 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
909                                 ep = fc_seq_exch(seq);
910                                 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
911                                                ep->did, ep->sid,
912                                                FC_TYPE_FCP, f_ctl, 0);
913                                 lp->tt.seq_send(lp, csp, conf_frame);
914                         }
915                 }
916                 lp->tt.exch_done(seq);
917         }
918         fc_io_compl(fsp);
919 }
920
921 static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
922 {
923         struct fc_lport *lp = fsp->lp;
924
925         if (fsp->seq_ptr) {
926                 lp->tt.exch_done(fsp->seq_ptr);
927                 fsp->seq_ptr = NULL;
928         }
929         fsp->status_code = error;
930 }
931
932 /**
933  * fc_fcp_cleanup_each_cmd() - Cleanup active commads
934  * @lp:         logical port
935  * @id:         target id
936  * @lun:        lun
937  * @error:      fsp status code
938  *
939  * If lun or id is -1, they are ignored.
940  */
941 static void fc_fcp_cleanup_each_cmd(struct fc_lport *lp, unsigned int id,
942                                     unsigned int lun, int error)
943 {
944         struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
945         struct fc_fcp_pkt *fsp;
946         struct scsi_cmnd *sc_cmd;
947         unsigned long flags;
948
949         spin_lock_irqsave(lp->host->host_lock, flags);
950 restart:
951         list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
952                 sc_cmd = fsp->cmd;
953                 if (id != -1 && scmd_id(sc_cmd) != id)
954                         continue;
955
956                 if (lun != -1 && sc_cmd->device->lun != lun)
957                         continue;
958
959                 fc_fcp_pkt_hold(fsp);
960                 spin_unlock_irqrestore(lp->host->host_lock, flags);
961
962                 if (!fc_fcp_lock_pkt(fsp)) {
963                         fc_fcp_cleanup_cmd(fsp, error);
964                         fc_io_compl(fsp);
965                         fc_fcp_unlock_pkt(fsp);
966                 }
967
968                 fc_fcp_pkt_release(fsp);
969                 spin_lock_irqsave(lp->host->host_lock, flags);
970                 /*
971                  * while we dropped the lock multiple pkts could
972                  * have been released, so we have to start over.
973                  */
974                 goto restart;
975         }
976         spin_unlock_irqrestore(lp->host->host_lock, flags);
977 }
978
979 static void fc_fcp_abort_io(struct fc_lport *lp)
980 {
981         fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_HRD_ERROR);
982 }
983
984 /**
985  * fc_fcp_pkt_send() - send a fcp packet to the lower level.
986  * @lp:         fc lport
987  * @fsp:        fc packet.
988  *
989  * This is called by upper layer protocol.
990  * Return   : zero for success and -1 for failure
991  * Context  : called from queuecommand which can be called from process
992  *            or scsi soft irq.
993  * Locks    : called with the host lock and irqs disabled.
994  */
995 static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
996 {
997         struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
998         int rc;
999
1000         fsp->cmd->SCp.ptr = (char *)fsp;
1001         fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1002         fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
1003
1004         int_to_scsilun(fsp->cmd->device->lun,
1005                        (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1006         memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
1007         list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1008
1009         spin_unlock_irq(lp->host->host_lock);
1010         rc = lp->tt.fcp_cmd_send(lp, fsp, fc_fcp_recv);
1011         spin_lock_irq(lp->host->host_lock);
1012         if (rc)
1013                 list_del(&fsp->list);
1014
1015         return rc;
1016 }
1017
1018 static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1019                            void (*resp)(struct fc_seq *,
1020                                         struct fc_frame *fp,
1021                                         void *arg))
1022 {
1023         struct fc_frame *fp;
1024         struct fc_seq *seq;
1025         struct fc_rport *rport;
1026         struct fc_rport_libfc_priv *rp;
1027         const size_t len = sizeof(fsp->cdb_cmd);
1028         int rc = 0;
1029
1030         if (fc_fcp_lock_pkt(fsp))
1031                 return 0;
1032
1033         fp = fc_frame_alloc(lp, sizeof(fsp->cdb_cmd));
1034         if (!fp) {
1035                 rc = -1;
1036                 goto unlock;
1037         }
1038
1039         memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
1040         fr_fsp(fp) = fsp;
1041         rport = fsp->rport;
1042         fsp->max_payload = rport->maxframe_size;
1043         rp = rport->dd_data;
1044
1045         fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
1046                        fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
1047                        FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1048
1049         seq = lp->tt.exch_seq_send(lp, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
1050         if (!seq) {
1051                 fc_frame_free(fp);
1052                 rc = -1;
1053                 goto unlock;
1054         }
1055         fsp->last_pkt_time = jiffies;
1056         fsp->seq_ptr = seq;
1057         fc_fcp_pkt_hold(fsp);   /* hold for fc_fcp_pkt_destroy */
1058
1059         setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
1060         fc_fcp_timer_set(fsp,
1061                          (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
1062                          FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
1063 unlock:
1064         fc_fcp_unlock_pkt(fsp);
1065         return rc;
1066 }
1067
1068 /*
1069  * transport error handler
1070  */
1071 static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1072 {
1073         int error = PTR_ERR(fp);
1074
1075         if (fc_fcp_lock_pkt(fsp))
1076                 return;
1077
1078         if (error == -FC_EX_CLOSED) {
1079                 fc_fcp_retry_cmd(fsp);
1080                 goto unlock;
1081         }
1082
1083         /*
1084          * clear abort pending, because the lower layer
1085          * decided to force completion.
1086          */
1087         fsp->state &= ~FC_SRB_ABORT_PENDING;
1088         fsp->status_code = FC_CMD_PLOGO;
1089         fc_fcp_complete_locked(fsp);
1090 unlock:
1091         fc_fcp_unlock_pkt(fsp);
1092 }
1093
1094 /*
1095  * Scsi abort handler- calls to send an abort
1096  * and then wait for abort completion
1097  */
1098 static int fc_fcp_pkt_abort(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
1099 {
1100         int rc = FAILED;
1101
1102         if (fc_fcp_send_abort(fsp))
1103                 return FAILED;
1104
1105         init_completion(&fsp->tm_done);
1106         fsp->wait_for_comp = 1;
1107
1108         spin_unlock_bh(&fsp->scsi_pkt_lock);
1109         rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1110         spin_lock_bh(&fsp->scsi_pkt_lock);
1111         fsp->wait_for_comp = 0;
1112
1113         if (!rc) {
1114                 FC_FCP_DBG(fsp, "target abort cmd  failed\n");
1115                 rc = FAILED;
1116         } else if (fsp->state & FC_SRB_ABORTED) {
1117                 FC_FCP_DBG(fsp, "target abort cmd  passed\n");
1118                 rc = SUCCESS;
1119                 fc_fcp_complete_locked(fsp);
1120         }
1121
1122         return rc;
1123 }
1124
1125 /*
1126  * Retry LUN reset after resource allocation failed.
1127  */
1128 static void fc_lun_reset_send(unsigned long data)
1129 {
1130         struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1131         struct fc_lport *lp = fsp->lp;
1132         if (lp->tt.fcp_cmd_send(lp, fsp, fc_tm_done)) {
1133                 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
1134                         return;
1135                 if (fc_fcp_lock_pkt(fsp))
1136                         return;
1137                 setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
1138                 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1139                 fc_fcp_unlock_pkt(fsp);
1140         }
1141 }
1142
1143 /*
1144  * Scsi device reset handler- send a LUN RESET to the device
1145  * and wait for reset reply
1146  */
1147 static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1148                         unsigned int id, unsigned int lun)
1149 {
1150         int rc;
1151
1152         fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1153         fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
1154         int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1155
1156         fsp->wait_for_comp = 1;
1157         init_completion(&fsp->tm_done);
1158
1159         fc_lun_reset_send((unsigned long)fsp);
1160
1161         /*
1162          * wait for completion of reset
1163          * after that make sure all commands are terminated
1164          */
1165         rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1166
1167         spin_lock_bh(&fsp->scsi_pkt_lock);
1168         fsp->state |= FC_SRB_COMPL;
1169         spin_unlock_bh(&fsp->scsi_pkt_lock);
1170
1171         del_timer_sync(&fsp->timer);
1172
1173         spin_lock_bh(&fsp->scsi_pkt_lock);
1174         if (fsp->seq_ptr) {
1175                 lp->tt.exch_done(fsp->seq_ptr);
1176                 fsp->seq_ptr = NULL;
1177         }
1178         fsp->wait_for_comp = 0;
1179         spin_unlock_bh(&fsp->scsi_pkt_lock);
1180
1181         if (!rc) {
1182                 FC_SCSI_DBG(lp, "lun reset failed\n");
1183                 return FAILED;
1184         }
1185
1186         /* cdb_status holds the tmf's rsp code */
1187         if (fsp->cdb_status != FCP_TMF_CMPL)
1188                 return FAILED;
1189
1190         FC_SCSI_DBG(lp, "lun reset to lun %u completed\n", lun);
1191         fc_fcp_cleanup_each_cmd(lp, id, lun, FC_CMD_ABORTED);
1192         return SUCCESS;
1193 }
1194
1195 /*
1196  * Task Managment response handler
1197  */
1198 static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1199 {
1200         struct fc_fcp_pkt *fsp = arg;
1201         struct fc_frame_header *fh;
1202
1203         if (IS_ERR(fp)) {
1204                 /*
1205                  * If there is an error just let it timeout or wait
1206                  * for TMF to be aborted if it timedout.
1207                  *
1208                  * scsi-eh will escalate for when either happens.
1209                  */
1210                 return;
1211         }
1212
1213         if (fc_fcp_lock_pkt(fsp))
1214                 return;
1215
1216         /*
1217          * raced with eh timeout handler.
1218          */
1219         if (!fsp->seq_ptr || !fsp->wait_for_comp) {
1220                 spin_unlock_bh(&fsp->scsi_pkt_lock);
1221                 return;
1222         }
1223
1224         fh = fc_frame_header_get(fp);
1225         if (fh->fh_type != FC_TYPE_BLS)
1226                 fc_fcp_resp(fsp, fp);
1227         fsp->seq_ptr = NULL;
1228         fsp->lp->tt.exch_done(seq);
1229         fc_frame_free(fp);
1230         fc_fcp_unlock_pkt(fsp);
1231 }
1232
1233 static void fc_fcp_cleanup(struct fc_lport *lp)
1234 {
1235         fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_ERROR);
1236 }
1237
1238 /*
1239  * fc_fcp_timeout: called by OS timer function.
1240  *
1241  * The timer has been inactivated and must be reactivated if desired
1242  * using fc_fcp_timer_set().
1243  *
1244  * Algorithm:
1245  *
1246  * If REC is supported, just issue it, and return.  The REC exchange will
1247  * complete or time out, and recovery can continue at that point.
1248  *
1249  * Otherwise, if the response has been received without all the data,
1250  * it has been ER_TIMEOUT since the response was received.
1251  *
1252  * If the response has not been received,
1253  * we see if data was received recently.  If it has been, we continue waiting,
1254  * otherwise, we abort the command.
1255  */
1256 static void fc_fcp_timeout(unsigned long data)
1257 {
1258         struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1259         struct fc_rport *rport = fsp->rport;
1260         struct fc_rport_libfc_priv *rp = rport->dd_data;
1261
1262         if (fc_fcp_lock_pkt(fsp))
1263                 return;
1264
1265         if (fsp->cdb_cmd.fc_tm_flags)
1266                 goto unlock;
1267
1268         fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
1269
1270         if (rp->flags & FC_RP_FLAGS_REC_SUPPORTED)
1271                 fc_fcp_rec(fsp);
1272         else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
1273                                jiffies))
1274                 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1275         else if (fsp->state & FC_SRB_RCV_STATUS)
1276                 fc_fcp_complete_locked(fsp);
1277         else
1278                 fc_timeout_error(fsp);
1279         fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
1280 unlock:
1281         fc_fcp_unlock_pkt(fsp);
1282 }
1283
1284 /*
1285  * Send a REC ELS request
1286  */
1287 static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
1288 {
1289         struct fc_lport *lp;
1290         struct fc_frame *fp;
1291         struct fc_rport *rport;
1292         struct fc_rport_libfc_priv *rp;
1293
1294         lp = fsp->lp;
1295         rport = fsp->rport;
1296         rp = rport->dd_data;
1297         if (!fsp->seq_ptr || rp->rp_state != RPORT_ST_READY) {
1298                 fsp->status_code = FC_HRD_ERROR;
1299                 fsp->io_status = 0;
1300                 fc_fcp_complete_locked(fsp);
1301                 return;
1302         }
1303         fp = fc_frame_alloc(lp, sizeof(struct fc_els_rec));
1304         if (!fp)
1305                 goto retry;
1306
1307         fr_seq(fp) = fsp->seq_ptr;
1308         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
1309                        fc_host_port_id(rp->local_port->host), FC_TYPE_ELS,
1310                        FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1311         if (lp->tt.elsct_send(lp, rport->port_id, fp, ELS_REC, fc_fcp_rec_resp,
1312                               fsp, jiffies_to_msecs(FC_SCSI_REC_TOV))) {
1313                 fc_fcp_pkt_hold(fsp);           /* hold while REC outstanding */
1314                 return;
1315         }
1316         fc_frame_free(fp);
1317 retry:
1318         if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1319                 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1320         else
1321                 fc_timeout_error(fsp);
1322 }
1323
1324 /*
1325  * Receive handler for REC ELS frame
1326  * if it is a reject then let the scsi layer to handle
1327  * the timeout. if it is a LS_ACC then if the io was not completed
1328  * then set the timeout and return otherwise complete the exchange
1329  * and tell the scsi layer to restart the I/O.
1330  */
1331 static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1332 {
1333         struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
1334         struct fc_els_rec_acc *recp;
1335         struct fc_els_ls_rjt *rjt;
1336         u32 e_stat;
1337         u8 opcode;
1338         u32 offset;
1339         enum dma_data_direction data_dir;
1340         enum fc_rctl r_ctl;
1341         struct fc_rport_libfc_priv *rp;
1342
1343         if (IS_ERR(fp)) {
1344                 fc_fcp_rec_error(fsp, fp);
1345                 return;
1346         }
1347
1348         if (fc_fcp_lock_pkt(fsp))
1349                 goto out;
1350
1351         fsp->recov_retry = 0;
1352         opcode = fc_frame_payload_op(fp);
1353         if (opcode == ELS_LS_RJT) {
1354                 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1355                 switch (rjt->er_reason) {
1356                 default:
1357                         FC_FCP_DBG(fsp, "device %x unexpected REC reject "
1358                                    "reason %d expl %d\n",
1359                                    fsp->rport->port_id, rjt->er_reason,
1360                                    rjt->er_explan);
1361                         /* fall through */
1362                 case ELS_RJT_UNSUP:
1363                         FC_FCP_DBG(fsp, "device does not support REC\n");
1364                         rp = fsp->rport->dd_data;
1365                         /*
1366                          * if we do not spport RECs or got some bogus
1367                          * reason then resetup timer so we check for
1368                          * making progress.
1369                          */
1370                         rp->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
1371                         fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1372                         break;
1373                 case ELS_RJT_LOGIC:
1374                 case ELS_RJT_UNAB:
1375                         /*
1376                          * If no data transfer, the command frame got dropped
1377                          * so we just retry.  If data was transferred, we
1378                          * lost the response but the target has no record,
1379                          * so we abort and retry.
1380                          */
1381                         if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
1382                             fsp->xfer_len == 0) {
1383                                 fc_fcp_retry_cmd(fsp);
1384                                 break;
1385                         }
1386                         fc_timeout_error(fsp);
1387                         break;
1388                 }
1389         } else if (opcode == ELS_LS_ACC) {
1390                 if (fsp->state & FC_SRB_ABORTED)
1391                         goto unlock_out;
1392
1393                 data_dir = fsp->cmd->sc_data_direction;
1394                 recp = fc_frame_payload_get(fp, sizeof(*recp));
1395                 offset = ntohl(recp->reca_fc4value);
1396                 e_stat = ntohl(recp->reca_e_stat);
1397
1398                 if (e_stat & ESB_ST_COMPLETE) {
1399
1400                         /*
1401                          * The exchange is complete.
1402                          *
1403                          * For output, we must've lost the response.
1404                          * For input, all data must've been sent.
1405                          * We lost may have lost the response
1406                          * (and a confirmation was requested) and maybe
1407                          * some data.
1408                          *
1409                          * If all data received, send SRR
1410                          * asking for response.  If partial data received,
1411                          * or gaps, SRR requests data at start of gap.
1412                          * Recovery via SRR relies on in-order-delivery.
1413                          */
1414                         if (data_dir == DMA_TO_DEVICE) {
1415                                 r_ctl = FC_RCTL_DD_CMD_STATUS;
1416                         } else if (fsp->xfer_contig_end == offset) {
1417                                 r_ctl = FC_RCTL_DD_CMD_STATUS;
1418                         } else {
1419                                 offset = fsp->xfer_contig_end;
1420                                 r_ctl = FC_RCTL_DD_SOL_DATA;
1421                         }
1422                         fc_fcp_srr(fsp, r_ctl, offset);
1423                 } else if (e_stat & ESB_ST_SEQ_INIT) {
1424
1425                         /*
1426                          * The remote port has the initiative, so just
1427                          * keep waiting for it to complete.
1428                          */
1429                         fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1430                 } else {
1431
1432                         /*
1433                          * The exchange is incomplete, we have seq. initiative.
1434                          * Lost response with requested confirmation,
1435                          * lost confirmation, lost transfer ready or
1436                          * lost write data.
1437                          *
1438                          * For output, if not all data was received, ask
1439                          * for transfer ready to be repeated.
1440                          *
1441                          * If we received or sent all the data, send SRR to
1442                          * request response.
1443                          *
1444                          * If we lost a response, we may have lost some read
1445                          * data as well.
1446                          */
1447                         r_ctl = FC_RCTL_DD_SOL_DATA;
1448                         if (data_dir == DMA_TO_DEVICE) {
1449                                 r_ctl = FC_RCTL_DD_CMD_STATUS;
1450                                 if (offset < fsp->data_len)
1451                                         r_ctl = FC_RCTL_DD_DATA_DESC;
1452                         } else if (offset == fsp->xfer_contig_end) {
1453                                 r_ctl = FC_RCTL_DD_CMD_STATUS;
1454                         } else if (fsp->xfer_contig_end < offset) {
1455                                 offset = fsp->xfer_contig_end;
1456                         }
1457                         fc_fcp_srr(fsp, r_ctl, offset);
1458                 }
1459         }
1460 unlock_out:
1461         fc_fcp_unlock_pkt(fsp);
1462 out:
1463         fc_fcp_pkt_release(fsp);        /* drop hold for outstanding REC */
1464         fc_frame_free(fp);
1465 }
1466
1467 /*
1468  * Handle error response or timeout for REC exchange.
1469  */
1470 static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1471 {
1472         int error = PTR_ERR(fp);
1473
1474         if (fc_fcp_lock_pkt(fsp))
1475                 goto out;
1476
1477         switch (error) {
1478         case -FC_EX_CLOSED:
1479                 fc_fcp_retry_cmd(fsp);
1480                 break;
1481
1482         default:
1483                 FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
1484                            fsp, fsp->rport->port_id, error);
1485                 fsp->status_code = FC_CMD_PLOGO;
1486                 /* fall through */
1487
1488         case -FC_EX_TIMEOUT:
1489                 /*
1490                  * Assume REC or LS_ACC was lost.
1491                  * The exchange manager will have aborted REC, so retry.
1492                  */
1493                 FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
1494                            fsp->rport->port_id, error, fsp->recov_retry,
1495                            FC_MAX_RECOV_RETRY);
1496                 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1497                         fc_fcp_rec(fsp);
1498                 else
1499                         fc_timeout_error(fsp);
1500                 break;
1501         }
1502         fc_fcp_unlock_pkt(fsp);
1503 out:
1504         fc_fcp_pkt_release(fsp);        /* drop hold for outstanding REC */
1505 }
1506
1507 /*
1508  * Time out error routine:
1509  * abort's the I/O close the exchange and
1510  * send completion notification to scsi layer
1511  */
1512 static void fc_timeout_error(struct fc_fcp_pkt *fsp)
1513 {
1514         fsp->status_code = FC_CMD_TIME_OUT;
1515         fsp->cdb_status = 0;
1516         fsp->io_status = 0;
1517         /*
1518          * if this fails then we let the scsi command timer fire and
1519          * scsi-ml escalate.
1520          */
1521         fc_fcp_send_abort(fsp);
1522 }
1523
1524 /*
1525  * Sequence retransmission request.
1526  * This is called after receiving status but insufficient data, or
1527  * when expecting status but the request has timed out.
1528  */
1529 static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1530 {
1531         struct fc_lport *lp = fsp->lp;
1532         struct fc_rport *rport;
1533         struct fc_rport_libfc_priv *rp;
1534         struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1535         struct fc_seq *seq;
1536         struct fcp_srr *srr;
1537         struct fc_frame *fp;
1538         u8 cdb_op;
1539
1540         rport = fsp->rport;
1541         rp = rport->dd_data;
1542         cdb_op = fsp->cdb_cmd.fc_cdb[0];
1543
1544         if (!(rp->flags & FC_RP_FLAGS_RETRY) || rp->rp_state != RPORT_ST_READY)
1545                 goto retry;                     /* shouldn't happen */
1546         fp = fc_frame_alloc(lp, sizeof(*srr));
1547         if (!fp)
1548                 goto retry;
1549
1550         srr = fc_frame_payload_get(fp, sizeof(*srr));
1551         memset(srr, 0, sizeof(*srr));
1552         srr->srr_op = ELS_SRR;
1553         srr->srr_ox_id = htons(ep->oxid);
1554         srr->srr_rx_id = htons(ep->rxid);
1555         srr->srr_r_ctl = r_ctl;
1556         srr->srr_rel_off = htonl(offset);
1557
1558         fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
1559                        fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
1560                        FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1561
1562         seq = lp->tt.exch_seq_send(lp, fp, fc_fcp_srr_resp, NULL,
1563                                    fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
1564         if (!seq) {
1565                 fc_frame_free(fp);
1566                 goto retry;
1567         }
1568         fsp->recov_seq = seq;
1569         fsp->xfer_len = offset;
1570         fsp->xfer_contig_end = offset;
1571         fsp->state &= ~FC_SRB_RCV_STATUS;
1572         fc_fcp_pkt_hold(fsp);           /* hold for outstanding SRR */
1573         return;
1574 retry:
1575         fc_fcp_retry_cmd(fsp);
1576 }
1577
1578 /*
1579  * Handle response from SRR.
1580  */
1581 static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1582 {
1583         struct fc_fcp_pkt *fsp = arg;
1584         struct fc_frame_header *fh;
1585
1586         if (IS_ERR(fp)) {
1587                 fc_fcp_srr_error(fsp, fp);
1588                 return;
1589         }
1590
1591         if (fc_fcp_lock_pkt(fsp))
1592                 goto out;
1593
1594         fh = fc_frame_header_get(fp);
1595         /*
1596          * BUG? fc_fcp_srr_error calls exch_done which would release
1597          * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
1598          * then fc_exch_timeout would be sending an abort. The exch_done
1599          * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1600          * an abort response though.
1601          */
1602         if (fh->fh_type == FC_TYPE_BLS) {
1603                 fc_fcp_unlock_pkt(fsp);
1604                 return;
1605         }
1606
1607         fsp->recov_seq = NULL;
1608         switch (fc_frame_payload_op(fp)) {
1609         case ELS_LS_ACC:
1610                 fsp->recov_retry = 0;
1611                 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1612                 break;
1613         case ELS_LS_RJT:
1614         default:
1615                 fc_timeout_error(fsp);
1616                 break;
1617         }
1618         fc_fcp_unlock_pkt(fsp);
1619         fsp->lp->tt.exch_done(seq);
1620 out:
1621         fc_frame_free(fp);
1622         fc_fcp_pkt_release(fsp);        /* drop hold for outstanding SRR */
1623 }
1624
1625 static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1626 {
1627         if (fc_fcp_lock_pkt(fsp))
1628                 goto out;
1629         fsp->lp->tt.exch_done(fsp->recov_seq);
1630         fsp->recov_seq = NULL;
1631         switch (PTR_ERR(fp)) {
1632         case -FC_EX_TIMEOUT:
1633                 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1634                         fc_fcp_rec(fsp);
1635                 else
1636                         fc_timeout_error(fsp);
1637                 break;
1638         case -FC_EX_CLOSED:                     /* e.g., link failure */
1639                 /* fall through */
1640         default:
1641                 fc_fcp_retry_cmd(fsp);
1642                 break;
1643         }
1644         fc_fcp_unlock_pkt(fsp);
1645 out:
1646         fc_fcp_pkt_release(fsp);        /* drop hold for outstanding SRR */
1647 }
1648
1649 static inline int fc_fcp_lport_queue_ready(struct fc_lport *lp)
1650 {
1651         /* lock ? */
1652         return (lp->state == LPORT_ST_READY) && lp->link_up && !lp->qfull;
1653 }
1654
1655 /**
1656  * fc_queuecommand - The queuecommand function of the scsi template
1657  * @cmd:        struct scsi_cmnd to be executed
1658  * @done:       Callback function to be called when cmd is completed
1659  *
1660  * this is the i/o strategy routine, called by the scsi layer
1661  * this routine is called with holding the host_lock.
1662  */
1663 int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1664 {
1665         struct fc_lport *lp;
1666         struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1667         struct fc_fcp_pkt *fsp;
1668         struct fc_rport_libfc_priv *rp;
1669         int rval;
1670         int rc = 0;
1671         struct fcoe_dev_stats *stats;
1672
1673         lp = shost_priv(sc_cmd->device->host);
1674
1675         rval = fc_remote_port_chkready(rport);
1676         if (rval) {
1677                 sc_cmd->result = rval;
1678                 done(sc_cmd);
1679                 goto out;
1680         }
1681
1682         if (!*(struct fc_remote_port **)rport->dd_data) {
1683                 /*
1684                  * rport is transitioning from blocked/deleted to
1685                  * online
1686                  */
1687                 sc_cmd->result = DID_IMM_RETRY << 16;
1688                 done(sc_cmd);
1689                 goto out;
1690         }
1691
1692         rp = rport->dd_data;
1693
1694         if (!fc_fcp_lport_queue_ready(lp)) {
1695                 rc = SCSI_MLQUEUE_HOST_BUSY;
1696                 goto out;
1697         }
1698
1699         fsp = fc_fcp_pkt_alloc(lp, GFP_ATOMIC);
1700         if (fsp == NULL) {
1701                 rc = SCSI_MLQUEUE_HOST_BUSY;
1702                 goto out;
1703         }
1704
1705         /*
1706          * build the libfc request pkt
1707          */
1708         fsp->cmd = sc_cmd;      /* save the cmd */
1709         fsp->lp = lp;           /* save the softc ptr */
1710         fsp->rport = rport;     /* set the remote port ptr */
1711         sc_cmd->scsi_done = done;
1712
1713         /*
1714          * set up the transfer length
1715          */
1716         fsp->data_len = scsi_bufflen(sc_cmd);
1717         fsp->xfer_len = 0;
1718
1719         /*
1720          * setup the data direction
1721          */
1722         stats = fc_lport_get_stats(lp);
1723         if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1724                 fsp->req_flags = FC_SRB_READ;
1725                 stats->InputRequests++;
1726                 stats->InputMegabytes = fsp->data_len;
1727         } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1728                 fsp->req_flags = FC_SRB_WRITE;
1729                 stats->OutputRequests++;
1730                 stats->OutputMegabytes = fsp->data_len;
1731         } else {
1732                 fsp->req_flags = 0;
1733                 stats->ControlRequests++;
1734         }
1735
1736         fsp->tgt_flags = rp->flags;
1737
1738         init_timer(&fsp->timer);
1739         fsp->timer.data = (unsigned long)fsp;
1740
1741         /*
1742          * send it to the lower layer
1743          * if we get -1 return then put the request in the pending
1744          * queue.
1745          */
1746         rval = fc_fcp_pkt_send(lp, fsp);
1747         if (rval != 0) {
1748                 fsp->state = FC_SRB_FREE;
1749                 fc_fcp_pkt_release(fsp);
1750                 rc = SCSI_MLQUEUE_HOST_BUSY;
1751         }
1752 out:
1753         return rc;
1754 }
1755 EXPORT_SYMBOL(fc_queuecommand);
1756
1757 /**
1758  * fc_io_compl() -  Handle responses for completed commands
1759  * @fsp:        scsi packet
1760  *
1761  * Translates a error to a Linux SCSI error.
1762  *
1763  * The fcp packet lock must be held when calling.
1764  */
1765 static void fc_io_compl(struct fc_fcp_pkt *fsp)
1766 {
1767         struct fc_fcp_internal *si;
1768         struct scsi_cmnd *sc_cmd;
1769         struct fc_lport *lp;
1770         unsigned long flags;
1771
1772         /* release outstanding ddp context */
1773         fc_fcp_ddp_done(fsp);
1774
1775         fsp->state |= FC_SRB_COMPL;
1776         if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
1777                 spin_unlock_bh(&fsp->scsi_pkt_lock);
1778                 del_timer_sync(&fsp->timer);
1779                 spin_lock_bh(&fsp->scsi_pkt_lock);
1780         }
1781
1782         lp = fsp->lp;
1783         si = fc_get_scsi_internal(lp);
1784         spin_lock_irqsave(lp->host->host_lock, flags);
1785         if (!fsp->cmd) {
1786                 spin_unlock_irqrestore(lp->host->host_lock, flags);
1787                 return;
1788         }
1789
1790         /*
1791          * if a command timed out while we had to try and throttle IO
1792          * and it is now getting cleaned up, then we are about to
1793          * try again so clear the throttled flag incase we get more
1794          * time outs.
1795          */
1796         if (si->throttled && fsp->state & FC_SRB_NOMEM)
1797                 si->throttled = 0;
1798
1799         sc_cmd = fsp->cmd;
1800         fsp->cmd = NULL;
1801
1802         if (!sc_cmd->SCp.ptr) {
1803                 spin_unlock_irqrestore(lp->host->host_lock, flags);
1804                 return;
1805         }
1806
1807         CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
1808         switch (fsp->status_code) {
1809         case FC_COMPLETE:
1810                 if (fsp->cdb_status == 0) {
1811                         /*
1812                          * good I/O status
1813                          */
1814                         sc_cmd->result = DID_OK << 16;
1815                         if (fsp->scsi_resid)
1816                                 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1817                 } else if (fsp->cdb_status == QUEUE_FULL) {
1818                         struct scsi_device *tmp_sdev;
1819                         struct scsi_device *sdev = sc_cmd->device;
1820
1821                         shost_for_each_device(tmp_sdev, sdev->host) {
1822                                 if (tmp_sdev->id != sdev->id)
1823                                         continue;
1824
1825                                 if (tmp_sdev->queue_depth > 1) {
1826                                         scsi_track_queue_full(tmp_sdev,
1827                                                               tmp_sdev->
1828                                                               queue_depth - 1);
1829                                 }
1830                         }
1831                         sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
1832                 } else {
1833                         /*
1834                          * transport level I/O was ok but scsi
1835                          * has non zero status
1836                          */
1837                         sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
1838                 }
1839                 break;
1840         case FC_ERROR:
1841                 sc_cmd->result = DID_ERROR << 16;
1842                 break;
1843         case FC_DATA_UNDRUN:
1844                 if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
1845                         /*
1846                          * scsi status is good but transport level
1847                          * underrun.
1848                          */
1849                         sc_cmd->result = DID_OK << 16;
1850                 } else {
1851                         /*
1852                          * scsi got underrun, this is an error
1853                          */
1854                         CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1855                         sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1856                 }
1857                 break;
1858         case FC_DATA_OVRRUN:
1859                 /*
1860                  * overrun is an error
1861                  */
1862                 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1863                 break;
1864         case FC_CMD_ABORTED:
1865                 sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
1866                 break;
1867         case FC_CMD_TIME_OUT:
1868                 sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
1869                 break;
1870         case FC_CMD_RESET:
1871                 sc_cmd->result = (DID_RESET << 16);
1872                 break;
1873         case FC_HRD_ERROR:
1874                 sc_cmd->result = (DID_NO_CONNECT << 16);
1875                 break;
1876         default:
1877                 sc_cmd->result = (DID_ERROR << 16);
1878                 break;
1879         }
1880
1881         list_del(&fsp->list);
1882         sc_cmd->SCp.ptr = NULL;
1883         sc_cmd->scsi_done(sc_cmd);
1884         spin_unlock_irqrestore(lp->host->host_lock, flags);
1885
1886         /* release ref from initial allocation in queue command */
1887         fc_fcp_pkt_release(fsp);
1888 }
1889
1890 /**
1891  * fc_fcp_complete() - complete processing of a fcp packet
1892  * @fsp:        fcp packet
1893  *
1894  * This function may sleep if a fsp timer is pending.
1895  * The host lock must not be held by caller.
1896  */
1897 void fc_fcp_complete(struct fc_fcp_pkt *fsp)
1898 {
1899         if (fc_fcp_lock_pkt(fsp))
1900                 return;
1901
1902         fc_fcp_complete_locked(fsp);
1903         fc_fcp_unlock_pkt(fsp);
1904 }
1905 EXPORT_SYMBOL(fc_fcp_complete);
1906
1907 /**
1908  * fc_eh_abort() - Abort a command
1909  * @sc_cmd:     scsi command to abort
1910  *
1911  * From scsi host template.
1912  * send ABTS to the target device  and wait for the response
1913  * sc_cmd is the pointer to the command to be aborted.
1914  */
1915 int fc_eh_abort(struct scsi_cmnd *sc_cmd)
1916 {
1917         struct fc_fcp_pkt *fsp;
1918         struct fc_lport *lp;
1919         int rc = FAILED;
1920         unsigned long flags;
1921
1922         lp = shost_priv(sc_cmd->device->host);
1923         if (lp->state != LPORT_ST_READY)
1924                 return rc;
1925         else if (!lp->link_up)
1926                 return rc;
1927
1928         spin_lock_irqsave(lp->host->host_lock, flags);
1929         fsp = CMD_SP(sc_cmd);
1930         if (!fsp) {
1931                 /* command completed while scsi eh was setting up */
1932                 spin_unlock_irqrestore(lp->host->host_lock, flags);
1933                 return SUCCESS;
1934         }
1935         /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
1936         fc_fcp_pkt_hold(fsp);
1937         spin_unlock_irqrestore(lp->host->host_lock, flags);
1938
1939         if (fc_fcp_lock_pkt(fsp)) {
1940                 /* completed while we were waiting for timer to be deleted */
1941                 rc = SUCCESS;
1942                 goto release_pkt;
1943         }
1944
1945         rc = fc_fcp_pkt_abort(lp, fsp);
1946         fc_fcp_unlock_pkt(fsp);
1947
1948 release_pkt:
1949         fc_fcp_pkt_release(fsp);
1950         return rc;
1951 }
1952 EXPORT_SYMBOL(fc_eh_abort);
1953
1954 /**
1955  * fc_eh_device_reset() Reset a single LUN
1956  * @sc_cmd:     scsi command
1957  *
1958  * Set from scsi host template to send tm cmd to the target and wait for the
1959  * response.
1960  */
1961 int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1962 {
1963         struct fc_lport *lp;
1964         struct fc_fcp_pkt *fsp;
1965         struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1966         int rc = FAILED;
1967         struct fc_rport_libfc_priv *rp;
1968         int rval;
1969
1970         rval = fc_remote_port_chkready(rport);
1971         if (rval)
1972                 goto out;
1973
1974         rp = rport->dd_data;
1975         lp = shost_priv(sc_cmd->device->host);
1976
1977         if (lp->state != LPORT_ST_READY)
1978                 return rc;
1979
1980         FC_SCSI_DBG(lp, "Resetting rport (%6x)\n", rport->port_id);
1981
1982         fsp = fc_fcp_pkt_alloc(lp, GFP_NOIO);
1983         if (fsp == NULL) {
1984                 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
1985                 sc_cmd->result = DID_NO_CONNECT << 16;
1986                 goto out;
1987         }
1988
1989         /*
1990          * Build the libfc request pkt. Do not set the scsi cmnd, because
1991          * the sc passed in is not setup for execution like when sent
1992          * through the queuecommand callout.
1993          */
1994         fsp->lp = lp;           /* save the softc ptr */
1995         fsp->rport = rport;     /* set the remote port ptr */
1996
1997         /*
1998          * flush outstanding commands
1999          */
2000         rc = fc_lun_reset(lp, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
2001         fsp->state = FC_SRB_FREE;
2002         fc_fcp_pkt_release(fsp);
2003
2004 out:
2005         return rc;
2006 }
2007 EXPORT_SYMBOL(fc_eh_device_reset);
2008
2009 /**
2010  * fc_eh_host_reset() - The reset function will reset the ports on the host.
2011  * @sc_cmd:     scsi command
2012  */
2013 int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
2014 {
2015         struct Scsi_Host *shost = sc_cmd->device->host;
2016         struct fc_lport *lp = shost_priv(shost);
2017         unsigned long wait_tmo;
2018
2019         FC_SCSI_DBG(lp, "Resetting host\n");
2020
2021         lp->tt.lport_reset(lp);
2022         wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
2023         while (!fc_fcp_lport_queue_ready(lp) && time_before(jiffies, wait_tmo))
2024                 msleep(1000);
2025
2026         if (fc_fcp_lport_queue_ready(lp)) {
2027                 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
2028                              "on port (%6x)\n", fc_host_port_id(lp->host));
2029                 return SUCCESS;
2030         } else {
2031                 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
2032                              "port (%6x) is not ready.\n",
2033                              fc_host_port_id(lp->host));
2034                 return FAILED;
2035         }
2036 }
2037 EXPORT_SYMBOL(fc_eh_host_reset);
2038
2039 /**
2040  * fc_slave_alloc() - configure queue depth
2041  * @sdev:       scsi device
2042  *
2043  * Configures queue depth based on host's cmd_per_len. If not set
2044  * then we use the libfc default.
2045  */
2046 int fc_slave_alloc(struct scsi_device *sdev)
2047 {
2048         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2049         int queue_depth;
2050
2051         if (!rport || fc_remote_port_chkready(rport))
2052                 return -ENXIO;
2053
2054         if (sdev->tagged_supported) {
2055                 if (sdev->host->hostt->cmd_per_lun)
2056                         queue_depth = sdev->host->hostt->cmd_per_lun;
2057                 else
2058                         queue_depth = FC_FCP_DFLT_QUEUE_DEPTH;
2059                 scsi_activate_tcq(sdev, queue_depth);
2060         }
2061         return 0;
2062 }
2063 EXPORT_SYMBOL(fc_slave_alloc);
2064
2065 int fc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2066 {
2067         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2068         return sdev->queue_depth;
2069 }
2070 EXPORT_SYMBOL(fc_change_queue_depth);
2071
2072 int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
2073 {
2074         if (sdev->tagged_supported) {
2075                 scsi_set_tag_type(sdev, tag_type);
2076                 if (tag_type)
2077                         scsi_activate_tcq(sdev, sdev->queue_depth);
2078                 else
2079                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
2080         } else
2081                 tag_type = 0;
2082
2083         return tag_type;
2084 }
2085 EXPORT_SYMBOL(fc_change_queue_type);
2086
2087 void fc_fcp_destroy(struct fc_lport *lp)
2088 {
2089         struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
2090
2091         if (!list_empty(&si->scsi_pkt_queue))
2092                 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
2093                        "port (%6x)\n", fc_host_port_id(lp->host));
2094
2095         mempool_destroy(si->scsi_pkt_pool);
2096         kfree(si);
2097         lp->scsi_priv = NULL;
2098 }
2099 EXPORT_SYMBOL(fc_fcp_destroy);
2100
2101 int fc_fcp_init(struct fc_lport *lp)
2102 {
2103         int rc;
2104         struct fc_fcp_internal *si;
2105
2106         if (!lp->tt.fcp_cmd_send)
2107                 lp->tt.fcp_cmd_send = fc_fcp_cmd_send;
2108
2109         if (!lp->tt.fcp_cleanup)
2110                 lp->tt.fcp_cleanup = fc_fcp_cleanup;
2111
2112         if (!lp->tt.fcp_abort_io)
2113                 lp->tt.fcp_abort_io = fc_fcp_abort_io;
2114
2115         si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
2116         if (!si)
2117                 return -ENOMEM;
2118         lp->scsi_priv = si;
2119         INIT_LIST_HEAD(&si->scsi_pkt_queue);
2120
2121         si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
2122         if (!si->scsi_pkt_pool) {
2123                 rc = -ENOMEM;
2124                 goto free_internal;
2125         }
2126         return 0;
2127
2128 free_internal:
2129         kfree(si);
2130         return rc;
2131 }
2132 EXPORT_SYMBOL(fc_fcp_init);
2133
2134 static int __init libfc_init(void)
2135 {
2136         int rc;
2137
2138         scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2139                                             sizeof(struct fc_fcp_pkt),
2140                                             0, SLAB_HWCACHE_ALIGN, NULL);
2141         if (scsi_pkt_cachep == NULL) {
2142                 printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2143                        "module load failed!");
2144                 return -ENOMEM;
2145         }
2146
2147         rc = fc_setup_exch_mgr();
2148         if (rc)
2149                 goto destroy_pkt_cache;
2150
2151         rc = fc_setup_rport();
2152         if (rc)
2153                 goto destroy_em;
2154
2155         return rc;
2156 destroy_em:
2157         fc_destroy_exch_mgr();
2158 destroy_pkt_cache:
2159         kmem_cache_destroy(scsi_pkt_cachep);
2160         return rc;
2161 }
2162
2163 static void __exit libfc_exit(void)
2164 {
2165         kmem_cache_destroy(scsi_pkt_cachep);
2166         fc_destroy_exch_mgr();
2167         fc_destroy_rport();
2168 }
2169
2170 module_init(libfc_init);
2171 module_exit(libfc_exit);