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