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