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