blob: 970b54f653b70f15017638d14fb7ff037c17cd40 [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
Robert Love3a3b42b2009-11-03 11:47:39 -080070/**
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 */
Robert Love42e9a922008-12-09 15:10:17 -080076struct fc_fcp_internal {
Robert Love3a3b42b2009-11-03 11:47:39 -080077 mempool_t *scsi_pkt_pool;
Robert Love42e9a922008-12-09 15:10:17 -080078 struct list_head scsi_pkt_queue;
Robert Love3a3b42b2009-11-03 11:47:39 -080079 u8 throttled;
Robert Love42e9a922008-12-09 15:10:17 -080080};
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 *);
Robert Love3a3b42b2009-11-03 11:47:39 -080093static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080094static void fc_timeout_error(struct fc_fcp_pkt *);
Robert Love3a3b42b2009-11-03 11:47:39 -080095static void fc_fcp_timeout(unsigned long);
Robert Love42e9a922008-12-09 15:10:17 -080096static 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/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800134 * 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
Robert Love42e9a922008-12-09 15:10:17 -0800137 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800138 * Return value: fcp_pkt structure or null on allocation failure.
139 * Context: Can be called from process context, no lock is required.
Robert Love42e9a922008-12-09 15:10:17 -0800140 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800141static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
Robert Love42e9a922008-12-09 15:10:17 -0800142{
Robert Love3a3b42b2009-11-03 11:47:39 -0800143 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800144 struct fc_fcp_pkt *fsp;
145
146 fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
147 if (fsp) {
148 memset(fsp, 0, sizeof(*fsp));
Robert Love3a3b42b2009-11-03 11:47:39 -0800149 fsp->lp = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800150 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/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800159 * fc_fcp_pkt_release() - Release hold on a fcp_pkt
160 * @fsp: The FCP packet to be released
Robert Love42e9a922008-12-09 15:10:17 -0800161 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800162 * Context: Can be called from process or interrupt context,
163 * no lock is required.
Robert Love42e9a922008-12-09 15:10:17 -0800164 */
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
Robert Love3a3b42b2009-11-03 11:47:39 -0800174/**
175 * fc_fcp_pkt_hold() - Hold a fcp_pkt
176 * @fsp: The FCP packet to be held
177 */
Robert Love42e9a922008-12-09 15:10:17 -0800178static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
179{
180 atomic_inc(&fsp->ref_cnt);
181}
182
183/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800184 * 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
Robert Love42e9a922008-12-09 15:10:17 -0800187 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800188 * 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.
191 *
192 * Context: No locking required.
Robert Love42e9a922008-12-09 15:10:17 -0800193 */
194static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
195{
196 fc_fcp_pkt_release(fsp);
197}
198
199/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800200 * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
201 * @fsp: The FCP packet to be locked and incremented
Robert Love42e9a922008-12-09 15:10:17 -0800202 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800203 * We should only return error if we return a command to SCSI-ml before
Robert Love42e9a922008-12-09 15:10:17 -0800204 * 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
Robert Love3a3b42b2009-11-03 11:47:39 -0800228/**
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 */
Robert Love42e9a922008-12-09 15:10:17 -0800233static 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
Robert Love3a3b42b2009-11-03 11:47:39 -0800239/**
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 */
Robert Love42e9a922008-12-09 15:10:17 -0800244static 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
Robert Love3a3b42b2009-11-03 11:47:39 -0800250/**
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 */
Robert Love42e9a922008-12-09 15:10:17 -0800255static 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
Robert Love3a3b42b2009-11-03 11:47:39 -0800264/**
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.
Robert Love42e9a922008-12-09 15:10:17 -0800272 */
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;
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -0500281 fsp->io_status = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800282 fsp->status_code = FC_ERROR;
283 fc_fcp_complete_locked(fsp);
284}
285
Robert Love3a3b42b2009-11-03 11:47:39 -0800286/**
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
Yi Zoub277d2a2009-02-27 14:07:21 -0800290 */
291void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
292{
Robert Love3a3b42b2009-11-03 11:47:39 -0800293 struct fc_lport *lport;
Yi Zoub277d2a2009-02-27 14:07:21 -0800294
295 if (!fsp)
296 return;
297
Robert Love3a3b42b2009-11-03 11:47:39 -0800298 lport = fsp->lp;
Yi Zoub277d2a2009-02-27 14:07:21 -0800299 if ((fsp->req_flags & FC_SRB_READ) &&
Robert Love3a3b42b2009-11-03 11:47:39 -0800300 (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)))
Yi Zoub277d2a2009-02-27 14:07:21 -0800303 fsp->xfer_ddp = xid;
304 }
305}
Yi Zoub277d2a2009-02-27 14:07:21 -0800306
Robert Love3a3b42b2009-11-03 11:47:39 -0800307/**
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
Yi Zoub277d2a2009-02-27 14:07:21 -0800311 */
312static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
313{
Robert Love3a3b42b2009-11-03 11:47:39 -0800314 struct fc_lport *lport;
Yi Zoub277d2a2009-02-27 14:07:21 -0800315
316 if (!fsp)
317 return;
318
Yi Zou5e472d02009-10-21 16:26:50 -0700319 if (fsp->xfer_ddp == FC_XID_UNKNOWN)
320 return;
321
Robert Love3a3b42b2009-11-03 11:47:39 -0800322 lport = fsp->lp;
323 if (lport->tt.ddp_done) {
324 fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
Yi Zou5e472d02009-10-21 16:26:50 -0700325 fsp->xfer_ddp = FC_XID_UNKNOWN;
Yi Zoub277d2a2009-02-27 14:07:21 -0800326 }
327}
328
Robert Love3a3b42b2009-11-03 11:47:39 -0800329/**
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
Robert Love42e9a922008-12-09 15:10:17 -0800333 */
334static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
335{
336 struct scsi_cmnd *sc = fsp->cmd;
Robert Love3a3b42b2009-11-03 11:47:39 -0800337 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800338 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;
Robert Love58682872009-11-03 11:47:28 -0800347 u32 nents;
Robert Love42e9a922008-12-09 15:10:17 -0800348
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
Yi Zoub277d2a2009-02-27 14:07:21 -0800355 /* if this I/O is ddped, update xfer len */
356 fc_fcp_ddp_done(fsp);
357
Robert Love42e9a922008-12-09 15:10:17 -0800358 if (offset + len > fsp->data_len) {
Robert Love34f42a02009-02-27 10:55:45 -0800359 /* this should never happen */
Robert Love42e9a922008-12-09 15:10:17 -0800360 if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
361 fc_frame_crc_check(fp))
362 goto crc_err;
Robert Love74147052009-06-10 15:31:10 -0700363 FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
364 "data_len %x\n", len, offset, fsp->data_len);
Robert Love42e9a922008-12-09 15:10:17 -0800365 fc_fcp_retry_cmd(fsp);
366 return;
367 }
368 if (offset != fsp->xfer_len)
369 fsp->state |= FC_SRB_DISCONTIG;
370
Robert Love42e9a922008-12-09 15:10:17 -0800371 sg = scsi_sglist(sc);
Robert Love58682872009-11-03 11:47:28 -0800372 nents = scsi_sg_count(sc);
Robert Love42e9a922008-12-09 15:10:17 -0800373
Robert Love58682872009-11-03 11:47:28 -0800374 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);
Robert Love42e9a922008-12-09 15:10:17 -0800381 buf = fc_frame_payload_get(fp, 0);
Robert Love58682872009-11-03 11:47:28 -0800382 if (len % 4)
Robert Love42e9a922008-12-09 15:10:17 -0800383 crc = crc32(crc, buf + len, 4 - (len % 4));
Robert Love42e9a922008-12-09 15:10:17 -0800384
385 if (~crc != le32_to_cpu(fr_crc(fp))) {
386crc_err:
Robert Love3a3b42b2009-11-03 11:47:39 -0800387 stats = fc_lport_get_stats(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800388 stats->ErrorFrames++;
Robert Love582b45b2009-03-31 15:51:50 -0700389 /* FIXME - per cpu count, not total count! */
Robert Love42e9a922008-12-09 15:10:17 -0800390 if (stats->InvalidCRCCount++ < 5)
Robert Love74147052009-06-10 15:31:10 -0700391 printk(KERN_WARNING "libfc: CRC error on data "
392 "frame for port (%6x)\n",
Robert Love3a3b42b2009-11-03 11:47:39 -0800393 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800394 /*
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
Robert Love34f42a02009-02-27 10:55:45 -0800420/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800421 * 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
Robert Love42e9a922008-12-09 15:10:17 -0800426 *
427 * Called after receiving a Transfer Ready data descriptor.
Robert Love3a3b42b2009-11-03 11:47:39 -0800428 * 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.
Robert Love42e9a922008-12-09 15:10:17 -0800432 */
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;
Robert Love3a3b42b2009-11-03 11:47:39 -0800440 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800441 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;
Robert Love3a3b42b2009-11-03 11:47:39 -0800449 int using_sg = lport->sg_supp;
Robert Love42e9a922008-12-09 15:10:17 -0800450 u32 f_ctl;
451
452 WARN_ON(seq_blen <= 0);
453 if (unlikely(offset + seq_blen > fsp->data_len)) {
454 /* this should never happen */
Robert Love74147052009-06-10 15:31:10 -0700455 FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
456 "offset %zx\n", seq_blen, offset);
Robert Love42e9a922008-12-09 15:10:17 -0800457 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. */
Robert Love74147052009-06-10 15:31:10 -0700461 FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
462 "seq_blen %zx offset %zx\n", seq_blen, offset);
Robert Love42e9a922008-12-09 15:10:17 -0800463 }
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 */
Yi Zou276d6812009-02-27 14:07:10 -0800470 t_blen = fsp->max_payload;
Robert Love3a3b42b2009-11-03 11:47:39 -0800471 if (lport->seq_offload) {
472 t_blen = min(seq_blen, (size_t)lport->lso_max);
Robert Love74147052009-06-10 15:31:10 -0700473 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
Robert Love3a3b42b2009-11-03 11:47:39 -0800474 fsp, seq_blen, lport->lso_max, t_blen);
Yi Zou276d6812009-02-27 14:07:10 -0800475 }
476
Robert Love42e9a922008-12-09 15:10:17 -0800477 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;
Robert Love3a3b42b2009-11-03 11:47:39 -0800486 seq = lport->tt.seq_start_next(seq);
Robert Love42e9a922008-12-09 15:10:17 -0800487 f_ctl = FC_FC_REL_OFF;
488 WARN_ON(!seq);
489
Robert Love42e9a922008-12-09 15:10:17 -0800490 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;
508 if (using_sg) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800509 fp = _fc_frame_alloc(lport, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800510 if (!fp)
511 return -ENOMEM;
512 } else {
Robert Love3a3b42b2009-11-03 11:47:39 -0800513 fp = fc_frame_alloc(lport, tlen);
Robert Love42e9a922008-12-09 15:10:17 -0800514 if (!fp)
515 return -ENOMEM;
516
517 data = (void *)(fr_hdr(fp)) +
518 sizeof(struct fc_frame_header);
519 }
520 fh_parm_offset = frame_offset;
521 fr_max_payload(fp) = fsp->max_payload;
522 }
523 sg_bytes = min(tlen, sg->length - offset);
524 if (using_sg) {
Robert Love42e9a922008-12-09 15:10:17 -0800525 get_page(sg_page(sg));
526 skb_fill_page_desc(fp_skb(fp),
527 skb_shinfo(fp_skb(fp))->nr_frags,
528 sg_page(sg), sg->offset + offset,
529 sg_bytes);
530 fp_skb(fp)->data_len += sg_bytes;
531 fr_len(fp) += sg_bytes;
532 fp_skb(fp)->truesize += PAGE_SIZE;
533 } else {
534 size_t off = offset + sg->offset;
535
536 /*
537 * The scatterlist item may be bigger than PAGE_SIZE,
538 * but we must not cross pages inside the kmap.
539 */
540 sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
541 (off & ~PAGE_MASK)));
542 page_addr = kmap_atomic(sg_page(sg) +
543 (off >> PAGE_SHIFT),
544 KM_SOFTIRQ0);
545 memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
546 sg_bytes);
547 kunmap_atomic(page_addr, KM_SOFTIRQ0);
548 data += sg_bytes;
549 }
550 offset += sg_bytes;
551 frame_offset += sg_bytes;
552 tlen -= sg_bytes;
553 remaining -= sg_bytes;
554
Yi Zoud37322a2009-10-21 16:27:58 -0700555 if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
556 (tlen))
Robert Love42e9a922008-12-09 15:10:17 -0800557 continue;
558
559 /*
560 * Send sequence with transfer sequence initiative in case
561 * this is last FCP frame of the sequence.
562 */
563 if (remaining == 0)
564 f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
565
566 ep = fc_seq_exch(seq);
567 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
568 FC_TYPE_FCP, f_ctl, fh_parm_offset);
569
570 /*
571 * send fragment using for a sequence.
572 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800573 error = lport->tt.seq_send(lport, seq, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800574 if (error) {
575 WARN_ON(1); /* send error should be rare */
576 fc_fcp_retry_cmd(fsp);
577 return 0;
578 }
579 fp = NULL;
580 }
581 fsp->xfer_len += seq_blen; /* premature count? */
582 return 0;
583}
584
Robert Love3a3b42b2009-11-03 11:47:39 -0800585/**
586 * fc_fcp_abts_resp() - Send an ABTS response
587 * @fsp: The FCP packet that is being aborted
588 * @fp: The response frame
589 */
Robert Love42e9a922008-12-09 15:10:17 -0800590static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
591{
592 int ba_done = 1;
593 struct fc_ba_rjt *brp;
594 struct fc_frame_header *fh;
595
596 fh = fc_frame_header_get(fp);
597 switch (fh->fh_r_ctl) {
598 case FC_RCTL_BA_ACC:
599 break;
600 case FC_RCTL_BA_RJT:
601 brp = fc_frame_payload_get(fp, sizeof(*brp));
602 if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
603 break;
604 /* fall thru */
605 default:
606 /*
607 * we will let the command timeout
608 * and scsi-ml recover in this case,
609 * therefore cleared the ba_done flag.
610 */
611 ba_done = 0;
612 }
613
614 if (ba_done) {
615 fsp->state |= FC_SRB_ABORTED;
616 fsp->state &= ~FC_SRB_ABORT_PENDING;
617
618 if (fsp->wait_for_comp)
619 complete(&fsp->tm_done);
620 else
621 fc_fcp_complete_locked(fsp);
622 }
623}
624
Robert Love34f42a02009-02-27 10:55:45 -0800625/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800626 * fc_fcp_reduce_can_queue() - Reduce the can_queue value for a local port
627 * @lport: The local port to reduce can_queue on
Robert Love42e9a922008-12-09 15:10:17 -0800628 *
629 * If we are getting memory allocation failures, then we may
630 * be trying to execute too many commands. We let the running
631 * commands complete or timeout, then try again with a reduced
632 * can_queue. Eventually we will hit the point where we run
633 * on all reserved structs.
634 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800635static void fc_fcp_reduce_can_queue(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800636{
Robert Love3a3b42b2009-11-03 11:47:39 -0800637 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800638 unsigned long flags;
639 int can_queue;
640
Robert Love3a3b42b2009-11-03 11:47:39 -0800641 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800642 if (si->throttled)
643 goto done;
644 si->throttled = 1;
645
Robert Love3a3b42b2009-11-03 11:47:39 -0800646 can_queue = lport->host->can_queue;
Robert Love42e9a922008-12-09 15:10:17 -0800647 can_queue >>= 1;
648 if (!can_queue)
649 can_queue = 1;
Robert Love3a3b42b2009-11-03 11:47:39 -0800650 lport->host->can_queue = can_queue;
651 shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
Robert Love42e9a922008-12-09 15:10:17 -0800652 "Reducing can_queue to %d.\n", can_queue);
653done:
Robert Love3a3b42b2009-11-03 11:47:39 -0800654 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800655}
656
Robert Love34f42a02009-02-27 10:55:45 -0800657/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800658 * fc_fcp_recv() - Reveive an FCP frame
Robert Love34f42a02009-02-27 10:55:45 -0800659 * @seq: The sequence the frame is on
Robert Love3a3b42b2009-11-03 11:47:39 -0800660 * @fp: The received frame
Robert Love34f42a02009-02-27 10:55:45 -0800661 * @arg: The related FCP packet
Robert Love42e9a922008-12-09 15:10:17 -0800662 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800663 * Context: Called from Soft IRQ context. Can not be called
664 * holding the FCP packet list lock.
Robert Love42e9a922008-12-09 15:10:17 -0800665 */
666static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
667{
668 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
Robert Lovea29e7642009-04-21 16:27:41 -0700669 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800670 struct fc_frame_header *fh;
671 struct fcp_txrdy *dd;
672 u8 r_ctl;
673 int rc = 0;
674
675 if (IS_ERR(fp))
676 goto errout;
677
678 fh = fc_frame_header_get(fp);
679 r_ctl = fh->fh_r_ctl;
Robert Love42e9a922008-12-09 15:10:17 -0800680
Robert Lovea29e7642009-04-21 16:27:41 -0700681 if (!(lport->state & LPORT_ST_READY))
Robert Love42e9a922008-12-09 15:10:17 -0800682 goto out;
683 if (fc_fcp_lock_pkt(fsp))
684 goto out;
685 fsp->last_pkt_time = jiffies;
686
687 if (fh->fh_type == FC_TYPE_BLS) {
688 fc_fcp_abts_resp(fsp, fp);
689 goto unlock;
690 }
691
692 if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
693 goto unlock;
694
695 if (r_ctl == FC_RCTL_DD_DATA_DESC) {
696 /*
697 * received XFER RDY from the target
698 * need to send data to the target
699 */
700 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
701 dd = fc_frame_payload_get(fp, sizeof(*dd));
702 WARN_ON(!dd);
703
704 rc = fc_fcp_send_data(fsp, seq,
705 (size_t) ntohl(dd->ft_data_ro),
706 (size_t) ntohl(dd->ft_burst_len));
707 if (!rc)
708 seq->rec_data = fsp->xfer_len;
709 else if (rc == -ENOMEM)
710 fsp->state |= FC_SRB_NOMEM;
711 } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
712 /*
713 * received a DATA frame
714 * next we will copy the data to the system buffer
715 */
716 WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
717 fc_fcp_recv_data(fsp, fp);
718 seq->rec_data = fsp->xfer_contig_end;
719 } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
720 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
721
722 fc_fcp_resp(fsp, fp);
723 } else {
Robert Love74147052009-06-10 15:31:10 -0700724 FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
Robert Love42e9a922008-12-09 15:10:17 -0800725 }
726unlock:
727 fc_fcp_unlock_pkt(fsp);
728out:
729 fc_frame_free(fp);
730errout:
731 if (IS_ERR(fp))
732 fc_fcp_error(fsp, fp);
733 else if (rc == -ENOMEM)
Robert Lovea29e7642009-04-21 16:27:41 -0700734 fc_fcp_reduce_can_queue(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800735}
736
Robert Love3a3b42b2009-11-03 11:47:39 -0800737/**
738 * fc_fcp_resp() - Handler for FCP responses
739 * @fsp: The FCP packet the response is for
740 * @fp: The response frame
741 */
Robert Love42e9a922008-12-09 15:10:17 -0800742static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
743{
744 struct fc_frame_header *fh;
745 struct fcp_resp *fc_rp;
746 struct fcp_resp_ext *rp_ex;
747 struct fcp_resp_rsp_info *fc_rp_info;
748 u32 plen;
749 u32 expected_len;
750 u32 respl = 0;
751 u32 snsl = 0;
752 u8 flags = 0;
753
754 plen = fr_len(fp);
755 fh = (struct fc_frame_header *)fr_hdr(fp);
756 if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
757 goto len_err;
758 plen -= sizeof(*fh);
759 fc_rp = (struct fcp_resp *)(fh + 1);
760 fsp->cdb_status = fc_rp->fr_status;
761 flags = fc_rp->fr_flags;
762 fsp->scsi_comp_flags = flags;
763 expected_len = fsp->data_len;
764
Yi Zoub277d2a2009-02-27 14:07:21 -0800765 /* if ddp, update xfer len */
766 fc_fcp_ddp_done(fsp);
767
Robert Love42e9a922008-12-09 15:10:17 -0800768 if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
769 rp_ex = (void *)(fc_rp + 1);
770 if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
771 if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
772 goto len_err;
773 fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
774 if (flags & FCP_RSP_LEN_VAL) {
775 respl = ntohl(rp_ex->fr_rsp_len);
776 if (respl != sizeof(*fc_rp_info))
777 goto len_err;
778 if (fsp->wait_for_comp) {
779 /* Abuse cdb_status for rsp code */
780 fsp->cdb_status = fc_rp_info->rsp_code;
781 complete(&fsp->tm_done);
782 /*
783 * tmfs will not have any scsi cmd so
784 * exit here
785 */
786 return;
787 } else
788 goto err;
789 }
790 if (flags & FCP_SNS_LEN_VAL) {
791 snsl = ntohl(rp_ex->fr_sns_len);
792 if (snsl > SCSI_SENSE_BUFFERSIZE)
793 snsl = SCSI_SENSE_BUFFERSIZE;
794 memcpy(fsp->cmd->sense_buffer,
795 (char *)fc_rp_info + respl, snsl);
796 }
797 }
798 if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
799 if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
800 goto len_err;
801 if (flags & FCP_RESID_UNDER) {
802 fsp->scsi_resid = ntohl(rp_ex->fr_resid);
803 /*
804 * The cmnd->underflow is the minimum number of
805 * bytes that must be transfered for this
806 * command. Provided a sense condition is not
807 * present, make sure the actual amount
808 * transferred is at least the underflow value
809 * or fail.
810 */
811 if (!(flags & FCP_SNS_LEN_VAL) &&
812 (fc_rp->fr_status == 0) &&
813 (scsi_bufflen(fsp->cmd) -
814 fsp->scsi_resid) < fsp->cmd->underflow)
815 goto err;
816 expected_len -= fsp->scsi_resid;
817 } else {
818 fsp->status_code = FC_ERROR;
819 }
820 }
821 }
822 fsp->state |= FC_SRB_RCV_STATUS;
823
824 /*
825 * Check for missing or extra data frames.
826 */
827 if (unlikely(fsp->xfer_len != expected_len)) {
828 if (fsp->xfer_len < expected_len) {
829 /*
830 * Some data may be queued locally,
831 * Wait a at least one jiffy to see if it is delivered.
832 * If this expires without data, we may do SRR.
833 */
834 fc_fcp_timer_set(fsp, 2);
835 return;
836 }
837 fsp->status_code = FC_DATA_OVRRUN;
Robert Love74147052009-06-10 15:31:10 -0700838 FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
839 "len %x, data len %x\n",
840 fsp->rport->port_id,
841 fsp->xfer_len, expected_len, fsp->data_len);
Robert Love42e9a922008-12-09 15:10:17 -0800842 }
843 fc_fcp_complete_locked(fsp);
844 return;
845
846len_err:
Robert Love74147052009-06-10 15:31:10 -0700847 FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
848 "snsl %u\n", flags, fr_len(fp), respl, snsl);
Robert Love42e9a922008-12-09 15:10:17 -0800849err:
850 fsp->status_code = FC_ERROR;
851 fc_fcp_complete_locked(fsp);
852}
853
854/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800855 * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
856 * fcp_pkt lock held
857 * @fsp: The FCP packet to be completed
Robert Love42e9a922008-12-09 15:10:17 -0800858 *
859 * This function may sleep if a timer is pending. The packet lock must be
860 * held, and the host lock must not be held.
861 */
862static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
863{
Robert Love3a3b42b2009-11-03 11:47:39 -0800864 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800865 struct fc_seq *seq;
866 struct fc_exch *ep;
867 u32 f_ctl;
868
869 if (fsp->state & FC_SRB_ABORT_PENDING)
870 return;
871
872 if (fsp->state & FC_SRB_ABORTED) {
873 if (!fsp->status_code)
874 fsp->status_code = FC_CMD_ABORTED;
875 } else {
876 /*
877 * Test for transport underrun, independent of response
878 * underrun status.
879 */
880 if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
881 (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
882 fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
883 fsp->status_code = FC_DATA_UNDRUN;
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -0500884 fsp->io_status = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800885 }
886 }
887
888 seq = fsp->seq_ptr;
889 if (seq) {
890 fsp->seq_ptr = NULL;
891 if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
892 struct fc_frame *conf_frame;
893 struct fc_seq *csp;
894
Robert Love3a3b42b2009-11-03 11:47:39 -0800895 csp = lport->tt.seq_start_next(seq);
Robert Love42e9a922008-12-09 15:10:17 -0800896 conf_frame = fc_frame_alloc(fsp->lp, 0);
897 if (conf_frame) {
898 f_ctl = FC_FC_SEQ_INIT;
899 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
900 ep = fc_seq_exch(seq);
901 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
902 ep->did, ep->sid,
903 FC_TYPE_FCP, f_ctl, 0);
Robert Love3a3b42b2009-11-03 11:47:39 -0800904 lport->tt.seq_send(lport, csp, conf_frame);
Robert Love42e9a922008-12-09 15:10:17 -0800905 }
906 }
Robert Love3a3b42b2009-11-03 11:47:39 -0800907 lport->tt.exch_done(seq);
Robert Love42e9a922008-12-09 15:10:17 -0800908 }
909 fc_io_compl(fsp);
910}
911
Robert Love3a3b42b2009-11-03 11:47:39 -0800912/**
913 * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
914 * @fsp: The FCP packet whose exchanges should be canceled
915 * @error: The reason for the cancellation
916 */
Robert Love42e9a922008-12-09 15:10:17 -0800917static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
918{
Robert Love3a3b42b2009-11-03 11:47:39 -0800919 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800920
921 if (fsp->seq_ptr) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800922 lport->tt.exch_done(fsp->seq_ptr);
Robert Love42e9a922008-12-09 15:10:17 -0800923 fsp->seq_ptr = NULL;
924 }
925 fsp->status_code = error;
926}
927
928/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800929 * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
930 * @lport: The local port whose exchanges should be canceled
931 * @id: The target's ID
932 * @lun: The LUN
933 * @error: The reason for cancellation
Robert Love42e9a922008-12-09 15:10:17 -0800934 *
935 * If lun or id is -1, they are ignored.
936 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800937static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
Robert Love42e9a922008-12-09 15:10:17 -0800938 unsigned int lun, int error)
939{
Robert Love3a3b42b2009-11-03 11:47:39 -0800940 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800941 struct fc_fcp_pkt *fsp;
942 struct scsi_cmnd *sc_cmd;
943 unsigned long flags;
944
Robert Love3a3b42b2009-11-03 11:47:39 -0800945 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800946restart:
947 list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
948 sc_cmd = fsp->cmd;
949 if (id != -1 && scmd_id(sc_cmd) != id)
950 continue;
951
952 if (lun != -1 && sc_cmd->device->lun != lun)
953 continue;
954
955 fc_fcp_pkt_hold(fsp);
Robert Love3a3b42b2009-11-03 11:47:39 -0800956 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800957
958 if (!fc_fcp_lock_pkt(fsp)) {
959 fc_fcp_cleanup_cmd(fsp, error);
960 fc_io_compl(fsp);
961 fc_fcp_unlock_pkt(fsp);
962 }
963
964 fc_fcp_pkt_release(fsp);
Robert Love3a3b42b2009-11-03 11:47:39 -0800965 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800966 /*
967 * while we dropped the lock multiple pkts could
968 * have been released, so we have to start over.
969 */
970 goto restart;
971 }
Robert Love3a3b42b2009-11-03 11:47:39 -0800972 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800973}
974
975/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800976 * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
977 * @lport: The local port whose exchanges are to be aborted
Robert Love42e9a922008-12-09 15:10:17 -0800978 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800979static void fc_fcp_abort_io(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800980{
Robert Love3a3b42b2009-11-03 11:47:39 -0800981 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
982}
983
984/**
985 * fc_fcp_pkt_send() - Send a fcp_pkt
986 * @lport: The local port to send the FCP packet on
987 * @fsp: The FCP packet to send
988 *
989 * Return: Zero for success and -1 for failure
990 * Locks: Called with the host lock and irqs disabled.
991 */
992static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
993{
994 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800995 int rc;
996
997 fsp->cmd->SCp.ptr = (char *)fsp;
998 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
999 fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
1000
1001 int_to_scsilun(fsp->cmd->device->lun,
1002 (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1003 memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
1004 list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1005
Robert Love3a3b42b2009-11-03 11:47:39 -08001006 spin_unlock_irq(lport->host->host_lock);
1007 rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
1008 spin_lock_irq(lport->host->host_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001009 if (rc)
1010 list_del(&fsp->list);
1011
1012 return rc;
1013}
1014
Robert Love3a3b42b2009-11-03 11:47:39 -08001015/**
1016 * fc_fcp_cmd_send() - Send a FCP command
1017 * @lport: The local port to send the command on
1018 * @fsp: The FCP packet the command is on
1019 * @resp: The handler for the response
1020 */
1021static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
Robert Love42e9a922008-12-09 15:10:17 -08001022 void (*resp)(struct fc_seq *,
1023 struct fc_frame *fp,
1024 void *arg))
1025{
1026 struct fc_frame *fp;
1027 struct fc_seq *seq;
1028 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001029 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001030 const size_t len = sizeof(fsp->cdb_cmd);
1031 int rc = 0;
1032
1033 if (fc_fcp_lock_pkt(fsp))
1034 return 0;
1035
Robert Love3a3b42b2009-11-03 11:47:39 -08001036 fp = fc_frame_alloc(lport, sizeof(fsp->cdb_cmd));
Robert Love42e9a922008-12-09 15:10:17 -08001037 if (!fp) {
1038 rc = -1;
1039 goto unlock;
1040 }
1041
1042 memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
Yi Zoub277d2a2009-02-27 14:07:21 -08001043 fr_fsp(fp) = fsp;
Robert Love42e9a922008-12-09 15:10:17 -08001044 rport = fsp->rport;
1045 fsp->max_payload = rport->maxframe_size;
Robert Love3a3b42b2009-11-03 11:47:39 -08001046 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001047
1048 fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
Robert Love3a3b42b2009-11-03 11:47:39 -08001049 fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
Robert Love42e9a922008-12-09 15:10:17 -08001050 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1051
Robert Love3a3b42b2009-11-03 11:47:39 -08001052 seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
1053 fsp, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001054 if (!seq) {
Robert Love42e9a922008-12-09 15:10:17 -08001055 rc = -1;
1056 goto unlock;
1057 }
1058 fsp->last_pkt_time = jiffies;
1059 fsp->seq_ptr = seq;
1060 fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
1061
1062 setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
1063 fc_fcp_timer_set(fsp,
1064 (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
1065 FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
1066unlock:
1067 fc_fcp_unlock_pkt(fsp);
1068 return rc;
1069}
1070
Robert Love3a3b42b2009-11-03 11:47:39 -08001071/**
1072 * fc_fcp_error() - Handler for FCP layer errors
1073 * @fsp: The FCP packet the error is on
1074 * @fp: The frame that has errored
Robert Love42e9a922008-12-09 15:10:17 -08001075 */
1076static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1077{
1078 int error = PTR_ERR(fp);
1079
1080 if (fc_fcp_lock_pkt(fsp))
1081 return;
1082
Robert Love74147052009-06-10 15:31:10 -07001083 if (error == -FC_EX_CLOSED) {
Robert Love42e9a922008-12-09 15:10:17 -08001084 fc_fcp_retry_cmd(fsp);
1085 goto unlock;
Robert Love42e9a922008-12-09 15:10:17 -08001086 }
Robert Love74147052009-06-10 15:31:10 -07001087
Robert Love42e9a922008-12-09 15:10:17 -08001088 /*
1089 * clear abort pending, because the lower layer
1090 * decided to force completion.
1091 */
1092 fsp->state &= ~FC_SRB_ABORT_PENDING;
1093 fsp->status_code = FC_CMD_PLOGO;
1094 fc_fcp_complete_locked(fsp);
1095unlock:
1096 fc_fcp_unlock_pkt(fsp);
1097}
1098
Robert Love3a3b42b2009-11-03 11:47:39 -08001099/**
1100 * fc_fcp_pkt_abort() - Abort a fcp_pkt
1101 * @fsp: The FCP packet to abort on
1102 *
1103 * Called to send an abort and then wait for abort completion
Robert Love42e9a922008-12-09 15:10:17 -08001104 */
Robert Lovec3401112009-10-21 16:27:06 -07001105static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
Robert Love42e9a922008-12-09 15:10:17 -08001106{
1107 int rc = FAILED;
1108
1109 if (fc_fcp_send_abort(fsp))
1110 return FAILED;
1111
1112 init_completion(&fsp->tm_done);
1113 fsp->wait_for_comp = 1;
1114
1115 spin_unlock_bh(&fsp->scsi_pkt_lock);
1116 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1117 spin_lock_bh(&fsp->scsi_pkt_lock);
1118 fsp->wait_for_comp = 0;
1119
1120 if (!rc) {
Robert Love74147052009-06-10 15:31:10 -07001121 FC_FCP_DBG(fsp, "target abort cmd failed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001122 rc = FAILED;
1123 } else if (fsp->state & FC_SRB_ABORTED) {
Robert Love74147052009-06-10 15:31:10 -07001124 FC_FCP_DBG(fsp, "target abort cmd passed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001125 rc = SUCCESS;
1126 fc_fcp_complete_locked(fsp);
1127 }
1128
1129 return rc;
1130}
1131
Robert Love3a3b42b2009-11-03 11:47:39 -08001132/**
1133 * fc_lun_reset_send() - Send LUN reset command
1134 * @data: The FCP packet that identifies the LUN to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001135 */
1136static void fc_lun_reset_send(unsigned long data)
1137{
1138 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
Robert Love3a3b42b2009-11-03 11:47:39 -08001139 struct fc_lport *lport = fsp->lp;
1140 if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
Robert Love42e9a922008-12-09 15:10:17 -08001141 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
1142 return;
1143 if (fc_fcp_lock_pkt(fsp))
1144 return;
1145 setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
1146 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1147 fc_fcp_unlock_pkt(fsp);
1148 }
1149}
1150
Robert Love3a3b42b2009-11-03 11:47:39 -08001151/**
1152 * fc_lun_reset() - Send a LUN RESET command to a device
1153 * and wait for the reply
1154 * @lport: The local port to sent the comand on
1155 * @fsp: The FCP packet that identifies the LUN to be reset
1156 * @id: The SCSI command ID
1157 * @lun: The LUN ID to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001158 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001159static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
Robert Love42e9a922008-12-09 15:10:17 -08001160 unsigned int id, unsigned int lun)
1161{
1162 int rc;
1163
1164 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1165 fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
1166 int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1167
1168 fsp->wait_for_comp = 1;
1169 init_completion(&fsp->tm_done);
1170
1171 fc_lun_reset_send((unsigned long)fsp);
1172
1173 /*
1174 * wait for completion of reset
1175 * after that make sure all commands are terminated
1176 */
1177 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1178
1179 spin_lock_bh(&fsp->scsi_pkt_lock);
1180 fsp->state |= FC_SRB_COMPL;
1181 spin_unlock_bh(&fsp->scsi_pkt_lock);
1182
1183 del_timer_sync(&fsp->timer);
1184
1185 spin_lock_bh(&fsp->scsi_pkt_lock);
1186 if (fsp->seq_ptr) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001187 lport->tt.exch_done(fsp->seq_ptr);
Robert Love42e9a922008-12-09 15:10:17 -08001188 fsp->seq_ptr = NULL;
1189 }
1190 fsp->wait_for_comp = 0;
1191 spin_unlock_bh(&fsp->scsi_pkt_lock);
1192
1193 if (!rc) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001194 FC_SCSI_DBG(lport, "lun reset failed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001195 return FAILED;
1196 }
1197
1198 /* cdb_status holds the tmf's rsp code */
1199 if (fsp->cdb_status != FCP_TMF_CMPL)
1200 return FAILED;
1201
Robert Love3a3b42b2009-11-03 11:47:39 -08001202 FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
1203 fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
Robert Love42e9a922008-12-09 15:10:17 -08001204 return SUCCESS;
1205}
1206
Robert Love3a3b42b2009-11-03 11:47:39 -08001207/**
1208 * fc_tm_done() - Task Managment response handler
1209 * @seq: The sequence that the response is on
1210 * @fp: The response frame
1211 * @arg: The FCP packet the response is for
Robert Love42e9a922008-12-09 15:10:17 -08001212 */
1213static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1214{
1215 struct fc_fcp_pkt *fsp = arg;
1216 struct fc_frame_header *fh;
1217
1218 if (IS_ERR(fp)) {
1219 /*
1220 * If there is an error just let it timeout or wait
1221 * for TMF to be aborted if it timedout.
1222 *
1223 * scsi-eh will escalate for when either happens.
1224 */
1225 return;
1226 }
1227
1228 if (fc_fcp_lock_pkt(fsp))
1229 return;
1230
1231 /*
1232 * raced with eh timeout handler.
1233 */
1234 if (!fsp->seq_ptr || !fsp->wait_for_comp) {
1235 spin_unlock_bh(&fsp->scsi_pkt_lock);
1236 return;
1237 }
1238
1239 fh = fc_frame_header_get(fp);
1240 if (fh->fh_type != FC_TYPE_BLS)
1241 fc_fcp_resp(fsp, fp);
1242 fsp->seq_ptr = NULL;
1243 fsp->lp->tt.exch_done(seq);
1244 fc_frame_free(fp);
1245 fc_fcp_unlock_pkt(fsp);
1246}
1247
Robert Love3a3b42b2009-11-03 11:47:39 -08001248/**
1249 * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
1250 * @lport: The local port to be cleaned up
1251 */
1252static void fc_fcp_cleanup(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001253{
Robert Love3a3b42b2009-11-03 11:47:39 -08001254 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001255}
1256
Robert Love3a3b42b2009-11-03 11:47:39 -08001257/**
1258 * fc_fcp_timeout() - Handler for fcp_pkt timeouts
1259 * @data: The FCP packet that has timed out
Robert Love42e9a922008-12-09 15:10:17 -08001260 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001261 * If REC is supported then just issue it and return. The REC exchange will
1262 * complete or time out and recovery can continue at that point. Otherwise,
1263 * if the response has been received without all the data it has been
1264 * ER_TIMEOUT since the response was received. If the response has not been
1265 * received we see if data was received recently. If it has been then we
1266 * continue waiting, otherwise, we abort the command.
Robert Love42e9a922008-12-09 15:10:17 -08001267 */
1268static void fc_fcp_timeout(unsigned long data)
1269{
1270 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1271 struct fc_rport *rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001272 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001273
1274 if (fc_fcp_lock_pkt(fsp))
1275 return;
1276
1277 if (fsp->cdb_cmd.fc_tm_flags)
1278 goto unlock;
1279
1280 fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
1281
Robert Love3a3b42b2009-11-03 11:47:39 -08001282 if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
Robert Love42e9a922008-12-09 15:10:17 -08001283 fc_fcp_rec(fsp);
1284 else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
1285 jiffies))
1286 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1287 else if (fsp->state & FC_SRB_RCV_STATUS)
1288 fc_fcp_complete_locked(fsp);
1289 else
1290 fc_timeout_error(fsp);
1291 fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
1292unlock:
1293 fc_fcp_unlock_pkt(fsp);
1294}
1295
Robert Love3a3b42b2009-11-03 11:47:39 -08001296/**
1297 * fc_fcp_rec() - Send a REC ELS request
1298 * @fsp: The FCP packet to send the REC request on
Robert Love42e9a922008-12-09 15:10:17 -08001299 */
1300static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
1301{
Robert Love3a3b42b2009-11-03 11:47:39 -08001302 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001303 struct fc_frame *fp;
1304 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001305 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001306
Robert Love3a3b42b2009-11-03 11:47:39 -08001307 lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -08001308 rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001309 rpriv = rport->dd_data;
1310 if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
Robert Love42e9a922008-12-09 15:10:17 -08001311 fsp->status_code = FC_HRD_ERROR;
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001312 fsp->io_status = 0;
Robert Love42e9a922008-12-09 15:10:17 -08001313 fc_fcp_complete_locked(fsp);
1314 return;
1315 }
Robert Love3a3b42b2009-11-03 11:47:39 -08001316 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rec));
Robert Love42e9a922008-12-09 15:10:17 -08001317 if (!fp)
1318 goto retry;
1319
1320 fr_seq(fp) = fsp->seq_ptr;
1321 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
Robert Love3a3b42b2009-11-03 11:47:39 -08001322 fc_host_port_id(rpriv->local_port->host), FC_TYPE_ELS,
Robert Love42e9a922008-12-09 15:10:17 -08001323 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
Robert Love3a3b42b2009-11-03 11:47:39 -08001324 if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
1325 fc_fcp_rec_resp, fsp,
1326 jiffies_to_msecs(FC_SCSI_REC_TOV))) {
Robert Love42e9a922008-12-09 15:10:17 -08001327 fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
1328 return;
1329 }
Robert Love42e9a922008-12-09 15:10:17 -08001330retry:
1331 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1332 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1333 else
1334 fc_timeout_error(fsp);
1335}
1336
Robert Love3a3b42b2009-11-03 11:47:39 -08001337/**
1338 * fc_fcp_rec_resp() - Handler for REC ELS responses
1339 * @seq: The sequence the response is on
1340 * @fp: The response frame
1341 * @arg: The FCP packet the response is on
1342 *
1343 * If the response is a reject then the scsi layer will handle
1344 * the timeout. If the response is a LS_ACC then if the I/O was not completed
1345 * set the timeout and return. If the I/O was completed then complete the
1346 * exchange and tell the SCSI layer.
Robert Love42e9a922008-12-09 15:10:17 -08001347 */
1348static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1349{
1350 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
1351 struct fc_els_rec_acc *recp;
1352 struct fc_els_ls_rjt *rjt;
1353 u32 e_stat;
1354 u8 opcode;
1355 u32 offset;
1356 enum dma_data_direction data_dir;
1357 enum fc_rctl r_ctl;
Robert Love3a3b42b2009-11-03 11:47:39 -08001358 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001359
1360 if (IS_ERR(fp)) {
1361 fc_fcp_rec_error(fsp, fp);
1362 return;
1363 }
1364
1365 if (fc_fcp_lock_pkt(fsp))
1366 goto out;
1367
1368 fsp->recov_retry = 0;
1369 opcode = fc_frame_payload_op(fp);
1370 if (opcode == ELS_LS_RJT) {
1371 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1372 switch (rjt->er_reason) {
1373 default:
Robert Love74147052009-06-10 15:31:10 -07001374 FC_FCP_DBG(fsp, "device %x unexpected REC reject "
1375 "reason %d expl %d\n",
1376 fsp->rport->port_id, rjt->er_reason,
1377 rjt->er_explan);
Robert Love42e9a922008-12-09 15:10:17 -08001378 /* fall through */
1379 case ELS_RJT_UNSUP:
Robert Love74147052009-06-10 15:31:10 -07001380 FC_FCP_DBG(fsp, "device does not support REC\n");
Robert Love3a3b42b2009-11-03 11:47:39 -08001381 rpriv = fsp->rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001382 /*
1383 * if we do not spport RECs or got some bogus
1384 * reason then resetup timer so we check for
1385 * making progress.
1386 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001387 rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
Robert Love42e9a922008-12-09 15:10:17 -08001388 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1389 break;
1390 case ELS_RJT_LOGIC:
1391 case ELS_RJT_UNAB:
1392 /*
1393 * If no data transfer, the command frame got dropped
1394 * so we just retry. If data was transferred, we
1395 * lost the response but the target has no record,
1396 * so we abort and retry.
1397 */
1398 if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
1399 fsp->xfer_len == 0) {
1400 fc_fcp_retry_cmd(fsp);
1401 break;
1402 }
1403 fc_timeout_error(fsp);
1404 break;
1405 }
1406 } else if (opcode == ELS_LS_ACC) {
1407 if (fsp->state & FC_SRB_ABORTED)
1408 goto unlock_out;
1409
1410 data_dir = fsp->cmd->sc_data_direction;
1411 recp = fc_frame_payload_get(fp, sizeof(*recp));
1412 offset = ntohl(recp->reca_fc4value);
1413 e_stat = ntohl(recp->reca_e_stat);
1414
1415 if (e_stat & ESB_ST_COMPLETE) {
1416
1417 /*
1418 * The exchange is complete.
1419 *
1420 * For output, we must've lost the response.
1421 * For input, all data must've been sent.
1422 * We lost may have lost the response
1423 * (and a confirmation was requested) and maybe
1424 * some data.
1425 *
1426 * If all data received, send SRR
1427 * asking for response. If partial data received,
1428 * or gaps, SRR requests data at start of gap.
1429 * Recovery via SRR relies on in-order-delivery.
1430 */
1431 if (data_dir == DMA_TO_DEVICE) {
1432 r_ctl = FC_RCTL_DD_CMD_STATUS;
1433 } else if (fsp->xfer_contig_end == offset) {
1434 r_ctl = FC_RCTL_DD_CMD_STATUS;
1435 } else {
1436 offset = fsp->xfer_contig_end;
1437 r_ctl = FC_RCTL_DD_SOL_DATA;
1438 }
1439 fc_fcp_srr(fsp, r_ctl, offset);
1440 } else if (e_stat & ESB_ST_SEQ_INIT) {
1441
1442 /*
1443 * The remote port has the initiative, so just
1444 * keep waiting for it to complete.
1445 */
1446 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1447 } else {
1448
1449 /*
1450 * The exchange is incomplete, we have seq. initiative.
1451 * Lost response with requested confirmation,
1452 * lost confirmation, lost transfer ready or
1453 * lost write data.
1454 *
1455 * For output, if not all data was received, ask
1456 * for transfer ready to be repeated.
1457 *
1458 * If we received or sent all the data, send SRR to
1459 * request response.
1460 *
1461 * If we lost a response, we may have lost some read
1462 * data as well.
1463 */
1464 r_ctl = FC_RCTL_DD_SOL_DATA;
1465 if (data_dir == DMA_TO_DEVICE) {
1466 r_ctl = FC_RCTL_DD_CMD_STATUS;
1467 if (offset < fsp->data_len)
1468 r_ctl = FC_RCTL_DD_DATA_DESC;
1469 } else if (offset == fsp->xfer_contig_end) {
1470 r_ctl = FC_RCTL_DD_CMD_STATUS;
1471 } else if (fsp->xfer_contig_end < offset) {
1472 offset = fsp->xfer_contig_end;
1473 }
1474 fc_fcp_srr(fsp, r_ctl, offset);
1475 }
1476 }
1477unlock_out:
1478 fc_fcp_unlock_pkt(fsp);
1479out:
1480 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1481 fc_frame_free(fp);
1482}
1483
Robert Love3a3b42b2009-11-03 11:47:39 -08001484/**
1485 * fc_fcp_rec_error() - Handler for REC errors
1486 * @fsp: The FCP packet the error is on
1487 * @fp: The REC frame
Robert Love42e9a922008-12-09 15:10:17 -08001488 */
1489static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1490{
1491 int error = PTR_ERR(fp);
1492
1493 if (fc_fcp_lock_pkt(fsp))
1494 goto out;
1495
1496 switch (error) {
1497 case -FC_EX_CLOSED:
1498 fc_fcp_retry_cmd(fsp);
1499 break;
1500
1501 default:
Robert Love74147052009-06-10 15:31:10 -07001502 FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
1503 fsp, fsp->rport->port_id, error);
Robert Love42e9a922008-12-09 15:10:17 -08001504 fsp->status_code = FC_CMD_PLOGO;
1505 /* fall through */
1506
1507 case -FC_EX_TIMEOUT:
1508 /*
1509 * Assume REC or LS_ACC was lost.
1510 * The exchange manager will have aborted REC, so retry.
1511 */
Robert Love74147052009-06-10 15:31:10 -07001512 FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
1513 fsp->rport->port_id, error, fsp->recov_retry,
1514 FC_MAX_RECOV_RETRY);
Robert Love42e9a922008-12-09 15:10:17 -08001515 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1516 fc_fcp_rec(fsp);
1517 else
1518 fc_timeout_error(fsp);
1519 break;
1520 }
1521 fc_fcp_unlock_pkt(fsp);
1522out:
1523 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1524}
1525
Robert Love3a3b42b2009-11-03 11:47:39 -08001526/**
1527 * fc_timeout_error() - Handler for fcp_pkt timeouts
1528 * @fsp: The FCP packt that has timed out
Robert Love42e9a922008-12-09 15:10:17 -08001529 */
1530static void fc_timeout_error(struct fc_fcp_pkt *fsp)
1531{
1532 fsp->status_code = FC_CMD_TIME_OUT;
1533 fsp->cdb_status = 0;
1534 fsp->io_status = 0;
1535 /*
1536 * if this fails then we let the scsi command timer fire and
1537 * scsi-ml escalate.
1538 */
1539 fc_fcp_send_abort(fsp);
1540}
1541
Robert Love3a3b42b2009-11-03 11:47:39 -08001542/**
1543 * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
1544 * @fsp: The FCP packet the SRR is to be sent on
1545 * @r_ctl: The R_CTL field for the SRR request
Robert Love42e9a922008-12-09 15:10:17 -08001546 * This is called after receiving status but insufficient data, or
1547 * when expecting status but the request has timed out.
1548 */
1549static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1550{
Robert Love3a3b42b2009-11-03 11:47:39 -08001551 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -08001552 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001553 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001554 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1555 struct fc_seq *seq;
1556 struct fcp_srr *srr;
1557 struct fc_frame *fp;
1558 u8 cdb_op;
1559
1560 rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001561 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001562 cdb_op = fsp->cdb_cmd.fc_cdb[0];
1563
Robert Love3a3b42b2009-11-03 11:47:39 -08001564 if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
1565 rpriv->rp_state != RPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08001566 goto retry; /* shouldn't happen */
Robert Love3a3b42b2009-11-03 11:47:39 -08001567 fp = fc_frame_alloc(lport, sizeof(*srr));
Robert Love42e9a922008-12-09 15:10:17 -08001568 if (!fp)
1569 goto retry;
1570
1571 srr = fc_frame_payload_get(fp, sizeof(*srr));
1572 memset(srr, 0, sizeof(*srr));
1573 srr->srr_op = ELS_SRR;
1574 srr->srr_ox_id = htons(ep->oxid);
1575 srr->srr_rx_id = htons(ep->rxid);
1576 srr->srr_r_ctl = r_ctl;
1577 srr->srr_rel_off = htonl(offset);
1578
1579 fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
Robert Love3a3b42b2009-11-03 11:47:39 -08001580 fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
Robert Love42e9a922008-12-09 15:10:17 -08001581 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1582
Robert Love3a3b42b2009-11-03 11:47:39 -08001583 seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
1584 fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
Chris Leech8f550f92009-10-21 16:28:09 -07001585 if (!seq)
Robert Love42e9a922008-12-09 15:10:17 -08001586 goto retry;
Chris Leech8f550f92009-10-21 16:28:09 -07001587
Robert Love42e9a922008-12-09 15:10:17 -08001588 fsp->recov_seq = seq;
1589 fsp->xfer_len = offset;
1590 fsp->xfer_contig_end = offset;
1591 fsp->state &= ~FC_SRB_RCV_STATUS;
1592 fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
1593 return;
1594retry:
1595 fc_fcp_retry_cmd(fsp);
1596}
1597
Robert Love3a3b42b2009-11-03 11:47:39 -08001598/**
1599 * fc_fcp_srr_resp() - Handler for SRR response
1600 * @seq: The sequence the SRR is on
1601 * @fp: The SRR frame
1602 * @arg: The FCP packet the SRR is on
Robert Love42e9a922008-12-09 15:10:17 -08001603 */
1604static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1605{
1606 struct fc_fcp_pkt *fsp = arg;
1607 struct fc_frame_header *fh;
1608
1609 if (IS_ERR(fp)) {
1610 fc_fcp_srr_error(fsp, fp);
1611 return;
1612 }
1613
1614 if (fc_fcp_lock_pkt(fsp))
1615 goto out;
1616
1617 fh = fc_frame_header_get(fp);
1618 /*
1619 * BUG? fc_fcp_srr_error calls exch_done which would release
1620 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
1621 * then fc_exch_timeout would be sending an abort. The exch_done
1622 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1623 * an abort response though.
1624 */
1625 if (fh->fh_type == FC_TYPE_BLS) {
1626 fc_fcp_unlock_pkt(fsp);
1627 return;
1628 }
1629
1630 fsp->recov_seq = NULL;
1631 switch (fc_frame_payload_op(fp)) {
1632 case ELS_LS_ACC:
1633 fsp->recov_retry = 0;
1634 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1635 break;
1636 case ELS_LS_RJT:
1637 default:
1638 fc_timeout_error(fsp);
1639 break;
1640 }
1641 fc_fcp_unlock_pkt(fsp);
1642 fsp->lp->tt.exch_done(seq);
1643out:
1644 fc_frame_free(fp);
1645 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1646}
1647
Robert Love3a3b42b2009-11-03 11:47:39 -08001648/**
1649 * fc_fcp_srr_error() - Handler for SRR errors
1650 * @fsp: The FCP packet that the SRR error is on
1651 * @fp: The SRR frame
1652 */
Robert Love42e9a922008-12-09 15:10:17 -08001653static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1654{
1655 if (fc_fcp_lock_pkt(fsp))
1656 goto out;
1657 fsp->lp->tt.exch_done(fsp->recov_seq);
1658 fsp->recov_seq = NULL;
1659 switch (PTR_ERR(fp)) {
1660 case -FC_EX_TIMEOUT:
1661 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1662 fc_fcp_rec(fsp);
1663 else
1664 fc_timeout_error(fsp);
1665 break;
1666 case -FC_EX_CLOSED: /* e.g., link failure */
1667 /* fall through */
1668 default:
1669 fc_fcp_retry_cmd(fsp);
1670 break;
1671 }
1672 fc_fcp_unlock_pkt(fsp);
1673out:
1674 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1675}
1676
Robert Love3a3b42b2009-11-03 11:47:39 -08001677/**
1678 * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
1679 * @lport: The local port to be checked
1680 */
1681static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001682{
1683 /* lock ? */
Robert Love3a3b42b2009-11-03 11:47:39 -08001684 return (lport->state == LPORT_ST_READY) &&
1685 lport->link_up && !lport->qfull;
Robert Love42e9a922008-12-09 15:10:17 -08001686}
1687
1688/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001689 * fc_queuecommand() - The queuecommand function of the SCSI template
1690 * @cmd: The scsi_cmnd to be executed
1691 * @done: The callback function to be called when the scsi_cmnd is complete
Robert Love42e9a922008-12-09 15:10:17 -08001692 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001693 * This is the i/o strategy routine, called by the SCSI layer. This routine
1694 * is called with the host_lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001695 */
1696int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1697{
Robert Love3a3b42b2009-11-03 11:47:39 -08001698 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001699 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1700 struct fc_fcp_pkt *fsp;
Robert Love3a3b42b2009-11-03 11:47:39 -08001701 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001702 int rval;
1703 int rc = 0;
1704 struct fcoe_dev_stats *stats;
1705
Robert Love3a3b42b2009-11-03 11:47:39 -08001706 lport = shost_priv(sc_cmd->device->host);
Robert Love42e9a922008-12-09 15:10:17 -08001707
1708 rval = fc_remote_port_chkready(rport);
1709 if (rval) {
1710 sc_cmd->result = rval;
1711 done(sc_cmd);
1712 goto out;
1713 }
1714
1715 if (!*(struct fc_remote_port **)rport->dd_data) {
1716 /*
1717 * rport is transitioning from blocked/deleted to
1718 * online
1719 */
1720 sc_cmd->result = DID_IMM_RETRY << 16;
1721 done(sc_cmd);
1722 goto out;
1723 }
1724
Robert Love3a3b42b2009-11-03 11:47:39 -08001725 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001726
Robert Love3a3b42b2009-11-03 11:47:39 -08001727 if (!fc_fcp_lport_queue_ready(lport)) {
Robert Love42e9a922008-12-09 15:10:17 -08001728 rc = SCSI_MLQUEUE_HOST_BUSY;
1729 goto out;
1730 }
1731
Robert Love3a3b42b2009-11-03 11:47:39 -08001732 fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
Robert Love42e9a922008-12-09 15:10:17 -08001733 if (fsp == NULL) {
1734 rc = SCSI_MLQUEUE_HOST_BUSY;
1735 goto out;
1736 }
1737
1738 /*
1739 * build the libfc request pkt
1740 */
1741 fsp->cmd = sc_cmd; /* save the cmd */
Robert Love3a3b42b2009-11-03 11:47:39 -08001742 fsp->lp = lport; /* save the softc ptr */
Robert Love42e9a922008-12-09 15:10:17 -08001743 fsp->rport = rport; /* set the remote port ptr */
Yi Zou5e472d02009-10-21 16:26:50 -07001744 fsp->xfer_ddp = FC_XID_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -08001745 sc_cmd->scsi_done = done;
1746
1747 /*
1748 * set up the transfer length
1749 */
1750 fsp->data_len = scsi_bufflen(sc_cmd);
1751 fsp->xfer_len = 0;
1752
1753 /*
1754 * setup the data direction
1755 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001756 stats = fc_lport_get_stats(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001757 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1758 fsp->req_flags = FC_SRB_READ;
1759 stats->InputRequests++;
1760 stats->InputMegabytes = fsp->data_len;
1761 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1762 fsp->req_flags = FC_SRB_WRITE;
1763 stats->OutputRequests++;
1764 stats->OutputMegabytes = fsp->data_len;
1765 } else {
1766 fsp->req_flags = 0;
1767 stats->ControlRequests++;
1768 }
1769
Robert Love3a3b42b2009-11-03 11:47:39 -08001770 fsp->tgt_flags = rpriv->flags;
Robert Love42e9a922008-12-09 15:10:17 -08001771
1772 init_timer(&fsp->timer);
1773 fsp->timer.data = (unsigned long)fsp;
1774
1775 /*
1776 * send it to the lower layer
1777 * if we get -1 return then put the request in the pending
1778 * queue.
1779 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001780 rval = fc_fcp_pkt_send(lport, fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001781 if (rval != 0) {
1782 fsp->state = FC_SRB_FREE;
1783 fc_fcp_pkt_release(fsp);
1784 rc = SCSI_MLQUEUE_HOST_BUSY;
1785 }
1786out:
1787 return rc;
1788}
1789EXPORT_SYMBOL(fc_queuecommand);
1790
1791/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001792 * fc_io_compl() - Handle responses for completed commands
1793 * @fsp: The FCP packet that is complete
Robert Love42e9a922008-12-09 15:10:17 -08001794 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001795 * Translates fcp_pkt errors to a Linux SCSI errors.
Robert Love42e9a922008-12-09 15:10:17 -08001796 * The fcp packet lock must be held when calling.
1797 */
1798static void fc_io_compl(struct fc_fcp_pkt *fsp)
1799{
1800 struct fc_fcp_internal *si;
1801 struct scsi_cmnd *sc_cmd;
Robert Love3a3b42b2009-11-03 11:47:39 -08001802 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001803 unsigned long flags;
1804
Yi Zoub277d2a2009-02-27 14:07:21 -08001805 /* release outstanding ddp context */
1806 fc_fcp_ddp_done(fsp);
1807
Robert Love42e9a922008-12-09 15:10:17 -08001808 fsp->state |= FC_SRB_COMPL;
1809 if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
1810 spin_unlock_bh(&fsp->scsi_pkt_lock);
1811 del_timer_sync(&fsp->timer);
1812 spin_lock_bh(&fsp->scsi_pkt_lock);
1813 }
1814
Robert Love3a3b42b2009-11-03 11:47:39 -08001815 lport = fsp->lp;
1816 si = fc_get_scsi_internal(lport);
1817 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001818 if (!fsp->cmd) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001819 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001820 return;
1821 }
1822
1823 /*
1824 * if a command timed out while we had to try and throttle IO
1825 * and it is now getting cleaned up, then we are about to
1826 * try again so clear the throttled flag incase we get more
1827 * time outs.
1828 */
1829 if (si->throttled && fsp->state & FC_SRB_NOMEM)
1830 si->throttled = 0;
1831
1832 sc_cmd = fsp->cmd;
1833 fsp->cmd = NULL;
1834
1835 if (!sc_cmd->SCp.ptr) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001836 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001837 return;
1838 }
1839
1840 CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
1841 switch (fsp->status_code) {
1842 case FC_COMPLETE:
1843 if (fsp->cdb_status == 0) {
1844 /*
1845 * good I/O status
1846 */
1847 sc_cmd->result = DID_OK << 16;
1848 if (fsp->scsi_resid)
1849 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
Robert Love42e9a922008-12-09 15:10:17 -08001850 } else {
1851 /*
1852 * transport level I/O was ok but scsi
1853 * has non zero status
1854 */
1855 sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
1856 }
1857 break;
1858 case FC_ERROR:
1859 sc_cmd->result = DID_ERROR << 16;
1860 break;
1861 case FC_DATA_UNDRUN:
Vasu Dev26d9cab2009-02-27 10:55:07 -08001862 if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
Robert Love42e9a922008-12-09 15:10:17 -08001863 /*
1864 * scsi status is good but transport level
Vasu Dev26d9cab2009-02-27 10:55:07 -08001865 * underrun.
Robert Love42e9a922008-12-09 15:10:17 -08001866 */
Yi Zou4347fa62009-10-21 16:27:12 -07001867 sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
1868 DID_OK : DID_ERROR) << 16;
Robert Love42e9a922008-12-09 15:10:17 -08001869 } else {
1870 /*
1871 * scsi got underrun, this is an error
1872 */
1873 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1874 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1875 }
1876 break;
1877 case FC_DATA_OVRRUN:
1878 /*
1879 * overrun is an error
1880 */
1881 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1882 break;
1883 case FC_CMD_ABORTED:
Mike Christied5e60542009-05-06 10:52:23 -07001884 sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
Robert Love42e9a922008-12-09 15:10:17 -08001885 break;
1886 case FC_CMD_TIME_OUT:
1887 sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
1888 break;
1889 case FC_CMD_RESET:
1890 sc_cmd->result = (DID_RESET << 16);
1891 break;
1892 case FC_HRD_ERROR:
1893 sc_cmd->result = (DID_NO_CONNECT << 16);
1894 break;
1895 default:
1896 sc_cmd->result = (DID_ERROR << 16);
1897 break;
1898 }
1899
1900 list_del(&fsp->list);
1901 sc_cmd->SCp.ptr = NULL;
1902 sc_cmd->scsi_done(sc_cmd);
Robert Love3a3b42b2009-11-03 11:47:39 -08001903 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001904
1905 /* release ref from initial allocation in queue command */
1906 fc_fcp_pkt_release(fsp);
1907}
1908
1909/**
Robert Love34f42a02009-02-27 10:55:45 -08001910 * fc_eh_abort() - Abort a command
Robert Love3a3b42b2009-11-03 11:47:39 -08001911 * @sc_cmd: The SCSI command to abort
Robert Love42e9a922008-12-09 15:10:17 -08001912 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001913 * From SCSI host template.
1914 * Send an ABTS to the target device and wait for the response.
Robert Love42e9a922008-12-09 15:10:17 -08001915 */
1916int fc_eh_abort(struct scsi_cmnd *sc_cmd)
1917{
1918 struct fc_fcp_pkt *fsp;
Robert Love3a3b42b2009-11-03 11:47:39 -08001919 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001920 int rc = FAILED;
1921 unsigned long flags;
1922
Robert Love3a3b42b2009-11-03 11:47:39 -08001923 lport = shost_priv(sc_cmd->device->host);
1924 if (lport->state != LPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08001925 return rc;
Robert Love3a3b42b2009-11-03 11:47:39 -08001926 else if (!lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -08001927 return rc;
1928
Robert Love3a3b42b2009-11-03 11:47:39 -08001929 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001930 fsp = CMD_SP(sc_cmd);
1931 if (!fsp) {
1932 /* command completed while scsi eh was setting up */
Robert Love3a3b42b2009-11-03 11:47:39 -08001933 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001934 return SUCCESS;
1935 }
1936 /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
1937 fc_fcp_pkt_hold(fsp);
Robert Love3a3b42b2009-11-03 11:47:39 -08001938 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001939
1940 if (fc_fcp_lock_pkt(fsp)) {
1941 /* completed while we were waiting for timer to be deleted */
1942 rc = SUCCESS;
1943 goto release_pkt;
1944 }
1945
Robert Lovec3401112009-10-21 16:27:06 -07001946 rc = fc_fcp_pkt_abort(fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001947 fc_fcp_unlock_pkt(fsp);
1948
1949release_pkt:
1950 fc_fcp_pkt_release(fsp);
1951 return rc;
1952}
1953EXPORT_SYMBOL(fc_eh_abort);
1954
1955/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001956 * fc_eh_device_reset() - Reset a single LUN
1957 * @sc_cmd: The SCSI command which identifies the device whose
1958 * LUN is to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001959 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001960 * Set from SCSI host template.
Robert Love42e9a922008-12-09 15:10:17 -08001961 */
1962int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1963{
Robert Love3a3b42b2009-11-03 11:47:39 -08001964 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001965 struct fc_fcp_pkt *fsp;
1966 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1967 int rc = FAILED;
Robert Love42e9a922008-12-09 15:10:17 -08001968 int rval;
1969
1970 rval = fc_remote_port_chkready(rport);
1971 if (rval)
1972 goto out;
1973
Robert Love3a3b42b2009-11-03 11:47:39 -08001974 lport = shost_priv(sc_cmd->device->host);
Robert Love42e9a922008-12-09 15:10:17 -08001975
Robert Love3a3b42b2009-11-03 11:47:39 -08001976 if (lport->state != LPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08001977 return rc;
1978
Robert Love3a3b42b2009-11-03 11:47:39 -08001979 FC_SCSI_DBG(lport, "Resetting rport (%6x)\n", rport->port_id);
Robert Love74147052009-06-10 15:31:10 -07001980
Robert Love3a3b42b2009-11-03 11:47:39 -08001981 fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
Robert Love42e9a922008-12-09 15:10:17 -08001982 if (fsp == NULL) {
Robert Love74147052009-06-10 15:31:10 -07001983 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
Robert Love42e9a922008-12-09 15:10:17 -08001984 sc_cmd->result = DID_NO_CONNECT << 16;
1985 goto out;
1986 }
1987
1988 /*
1989 * Build the libfc request pkt. Do not set the scsi cmnd, because
1990 * the sc passed in is not setup for execution like when sent
1991 * through the queuecommand callout.
1992 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001993 fsp->lp = lport; /* save the softc ptr */
Robert Love42e9a922008-12-09 15:10:17 -08001994 fsp->rport = rport; /* set the remote port ptr */
1995
1996 /*
1997 * flush outstanding commands
1998 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001999 rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
Robert Love42e9a922008-12-09 15:10:17 -08002000 fsp->state = FC_SRB_FREE;
2001 fc_fcp_pkt_release(fsp);
2002
2003out:
2004 return rc;
2005}
2006EXPORT_SYMBOL(fc_eh_device_reset);
2007
2008/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002009 * fc_eh_host_reset() - Reset a Scsi_Host.
2010 * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
Robert Love42e9a922008-12-09 15:10:17 -08002011 */
2012int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
2013{
2014 struct Scsi_Host *shost = sc_cmd->device->host;
Robert Love3a3b42b2009-11-03 11:47:39 -08002015 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -08002016 unsigned long wait_tmo;
2017
Robert Love3a3b42b2009-11-03 11:47:39 -08002018 FC_SCSI_DBG(lport, "Resetting host\n");
Robert Love74147052009-06-10 15:31:10 -07002019
Robert Love3a3b42b2009-11-03 11:47:39 -08002020 lport->tt.lport_reset(lport);
Robert Love42e9a922008-12-09 15:10:17 -08002021 wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
Robert Love3a3b42b2009-11-03 11:47:39 -08002022 while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
2023 wait_tmo))
Robert Love42e9a922008-12-09 15:10:17 -08002024 msleep(1000);
2025
Robert Love3a3b42b2009-11-03 11:47:39 -08002026 if (fc_fcp_lport_queue_ready(lport)) {
Robert Love74147052009-06-10 15:31:10 -07002027 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
Robert Love3a3b42b2009-11-03 11:47:39 -08002028 "on port (%6x)\n", fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -08002029 return SUCCESS;
2030 } else {
Robert Love74147052009-06-10 15:31:10 -07002031 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
2032 "port (%6x) is not ready.\n",
Robert Love3a3b42b2009-11-03 11:47:39 -08002033 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -08002034 return FAILED;
2035 }
2036}
2037EXPORT_SYMBOL(fc_eh_host_reset);
2038
2039/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002040 * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
2041 * @sdev: The SCSI device that identifies the SCSI host
Robert Love42e9a922008-12-09 15:10:17 -08002042 *
2043 * Configures queue depth based on host's cmd_per_len. If not set
2044 * then we use the libfc default.
2045 */
2046int fc_slave_alloc(struct scsi_device *sdev)
2047{
2048 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Robert Love42e9a922008-12-09 15:10:17 -08002049
2050 if (!rport || fc_remote_port_chkready(rport))
2051 return -ENXIO;
2052
Vasu Dev14caf442009-10-15 17:46:55 -07002053 if (sdev->tagged_supported)
2054 scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
2055 else
2056 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2057 FC_FCP_DFLT_QUEUE_DEPTH);
2058
Robert Love42e9a922008-12-09 15:10:17 -08002059 return 0;
2060}
2061EXPORT_SYMBOL(fc_slave_alloc);
2062
Robert Love3a3b42b2009-11-03 11:47:39 -08002063/**
2064 * fc_change_queue_depth() - Change a device's queue depth
2065 * @sdev: The SCSI device whose queue depth is to change
2066 * @qdepth: The new queue depth
2067 * @reason: The resason for the change
2068 */
Mike Christiee881a172009-10-15 17:46:39 -07002069int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
Robert Love42e9a922008-12-09 15:10:17 -08002070{
Mike Christie5c208482009-10-15 17:46:50 -07002071 switch (reason) {
2072 case SCSI_QDEPTH_DEFAULT:
2073 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2074 break;
2075 case SCSI_QDEPTH_QFULL:
2076 scsi_track_queue_full(sdev, qdepth);
2077 break;
Vasu Dev229b8d72009-10-15 17:47:06 -07002078 case SCSI_QDEPTH_RAMP_UP:
2079 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2080 break;
Mike Christie5c208482009-10-15 17:46:50 -07002081 default:
Mike Christiee881a172009-10-15 17:46:39 -07002082 return -EOPNOTSUPP;
Mike Christie5c208482009-10-15 17:46:50 -07002083 }
Robert Love42e9a922008-12-09 15:10:17 -08002084 return sdev->queue_depth;
2085}
2086EXPORT_SYMBOL(fc_change_queue_depth);
2087
Robert Love3a3b42b2009-11-03 11:47:39 -08002088/**
2089 * fc_change_queue_type() - Change a device's queue type
2090 * @sdev: The SCSI device whose queue depth is to change
2091 * @tag_type: Identifier for queue type
2092 */
Robert Love42e9a922008-12-09 15:10:17 -08002093int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
2094{
2095 if (sdev->tagged_supported) {
2096 scsi_set_tag_type(sdev, tag_type);
2097 if (tag_type)
2098 scsi_activate_tcq(sdev, sdev->queue_depth);
2099 else
2100 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2101 } else
2102 tag_type = 0;
2103
2104 return tag_type;
2105}
2106EXPORT_SYMBOL(fc_change_queue_type);
2107
Robert Love3a3b42b2009-11-03 11:47:39 -08002108/**
2109 * fc_fcp_destory() - Tear down the FCP layer for a given local port
2110 * @lport: The local port that no longer needs the FCP layer
2111 */
2112void fc_fcp_destroy(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08002113{
Robert Love3a3b42b2009-11-03 11:47:39 -08002114 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -08002115
2116 if (!list_empty(&si->scsi_pkt_queue))
Robert Love74147052009-06-10 15:31:10 -07002117 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
Robert Love3a3b42b2009-11-03 11:47:39 -08002118 "port (%6x)\n", fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -08002119
2120 mempool_destroy(si->scsi_pkt_pool);
2121 kfree(si);
Robert Love3a3b42b2009-11-03 11:47:39 -08002122 lport->scsi_priv = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08002123}
2124EXPORT_SYMBOL(fc_fcp_destroy);
2125
Robert Love93e6d5a2009-11-03 11:46:03 -08002126int fc_setup_fcp()
2127{
2128 int rc = 0;
2129
2130 scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2131 sizeof(struct fc_fcp_pkt),
2132 0, SLAB_HWCACHE_ALIGN, NULL);
2133 if (!scsi_pkt_cachep) {
2134 printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2135 "module load failed!");
2136 rc = -ENOMEM;
2137 }
2138
2139 return rc;
2140}
2141
2142void fc_destroy_fcp()
2143{
2144 if (scsi_pkt_cachep)
2145 kmem_cache_destroy(scsi_pkt_cachep);
2146}
2147
Robert Love3a3b42b2009-11-03 11:47:39 -08002148/**
2149 * fc_fcp_init() - Initialize the FCP layer for a local port
2150 * @lport: The local port to initialize the exchange layer for
2151 */
2152int fc_fcp_init(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08002153{
2154 int rc;
2155 struct fc_fcp_internal *si;
2156
Robert Love3a3b42b2009-11-03 11:47:39 -08002157 if (!lport->tt.fcp_cmd_send)
2158 lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
Robert Love42e9a922008-12-09 15:10:17 -08002159
Robert Love3a3b42b2009-11-03 11:47:39 -08002160 if (!lport->tt.fcp_cleanup)
2161 lport->tt.fcp_cleanup = fc_fcp_cleanup;
Robert Love42e9a922008-12-09 15:10:17 -08002162
Robert Love3a3b42b2009-11-03 11:47:39 -08002163 if (!lport->tt.fcp_abort_io)
2164 lport->tt.fcp_abort_io = fc_fcp_abort_io;
Robert Love42e9a922008-12-09 15:10:17 -08002165
2166 si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
2167 if (!si)
2168 return -ENOMEM;
Robert Love3a3b42b2009-11-03 11:47:39 -08002169 lport->scsi_priv = si;
Robert Love42e9a922008-12-09 15:10:17 -08002170 INIT_LIST_HEAD(&si->scsi_pkt_queue);
2171
2172 si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
2173 if (!si->scsi_pkt_pool) {
2174 rc = -ENOMEM;
2175 goto free_internal;
2176 }
2177 return 0;
2178
2179free_internal:
2180 kfree(si);
2181 return rc;
2182}
2183EXPORT_SYMBOL(fc_fcp_init);