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