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