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