blob: ac5c148d0182e910e1ebf554c84be3e434f0f50d [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/**
Vasu Devc46be112009-11-03 11:48:00 -0800330 * fc_fcp_can_queue_ramp_down() - reduces can_queue
331 * @lport: lport to reduce can_queue
332 *
333 * If we are getting memory allocation failures, then we may
334 * be trying to execute too many commands. We let the running
335 * commands complete or timeout, then try again with a reduced
336 * can_queue. Eventually we will hit the point where we run
337 * on all reserved structs.
338 */
339static void fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
340{
341 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
342 unsigned long flags;
343 int can_queue;
344
345 spin_lock_irqsave(lport->host->host_lock, flags);
346 if (si->throttled)
347 goto done;
348 si->throttled = 1;
349
350 can_queue = lport->host->can_queue;
351 can_queue >>= 1;
352 if (!can_queue)
353 can_queue = 1;
354 lport->host->can_queue = can_queue;
355 shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
356 "Reducing can_queue to %d.\n", can_queue);
357done:
358 spin_unlock_irqrestore(lport->host->host_lock, flags);
359}
360
361/*
362 * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer.
363 * @lport: fc lport struct
364 * @len: payload length
365 *
366 * Allocates fc_frame structure and buffer but if fails to allocate
367 * then reduce can_queue.
368 */
369static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
370 size_t len)
371{
372 struct fc_frame *fp;
373
374 fp = fc_frame_alloc(lport, len);
375 if (!fp)
376 fc_fcp_can_queue_ramp_down(lport);
377 return fp;
378}
379
380/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800381 * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
382 * @fsp: The FCP packet the data is on
383 * @fp: The data frame
Robert Love42e9a922008-12-09 15:10:17 -0800384 */
385static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
386{
387 struct scsi_cmnd *sc = fsp->cmd;
Robert Love3a3b42b2009-11-03 11:47:39 -0800388 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800389 struct fcoe_dev_stats *stats;
390 struct fc_frame_header *fh;
391 size_t start_offset;
392 size_t offset;
393 u32 crc;
394 u32 copy_len = 0;
395 size_t len;
396 void *buf;
397 struct scatterlist *sg;
Robert Love58682872009-11-03 11:47:28 -0800398 u32 nents;
Robert Love42e9a922008-12-09 15:10:17 -0800399
400 fh = fc_frame_header_get(fp);
401 offset = ntohl(fh->fh_parm_offset);
402 start_offset = offset;
403 len = fr_len(fp) - sizeof(*fh);
404 buf = fc_frame_payload_get(fp, 0);
405
Yi Zoub277d2a2009-02-27 14:07:21 -0800406 /* if this I/O is ddped, update xfer len */
407 fc_fcp_ddp_done(fsp);
408
Robert Love42e9a922008-12-09 15:10:17 -0800409 if (offset + len > fsp->data_len) {
Robert Love34f42a02009-02-27 10:55:45 -0800410 /* this should never happen */
Robert Love42e9a922008-12-09 15:10:17 -0800411 if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
412 fc_frame_crc_check(fp))
413 goto crc_err;
Robert Love74147052009-06-10 15:31:10 -0700414 FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
415 "data_len %x\n", len, offset, fsp->data_len);
Robert Love42e9a922008-12-09 15:10:17 -0800416 fc_fcp_retry_cmd(fsp);
417 return;
418 }
419 if (offset != fsp->xfer_len)
420 fsp->state |= FC_SRB_DISCONTIG;
421
Robert Love42e9a922008-12-09 15:10:17 -0800422 sg = scsi_sglist(sc);
Robert Love58682872009-11-03 11:47:28 -0800423 nents = scsi_sg_count(sc);
Robert Love42e9a922008-12-09 15:10:17 -0800424
Robert Love58682872009-11-03 11:47:28 -0800425 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
426 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
427 &offset, KM_SOFTIRQ0, NULL);
428 } else {
429 crc = crc32(~0, (u8 *) fh, sizeof(*fh));
430 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
431 &offset, KM_SOFTIRQ0, &crc);
Robert Love42e9a922008-12-09 15:10:17 -0800432 buf = fc_frame_payload_get(fp, 0);
Robert Love58682872009-11-03 11:47:28 -0800433 if (len % 4)
Robert Love42e9a922008-12-09 15:10:17 -0800434 crc = crc32(crc, buf + len, 4 - (len % 4));
Robert Love42e9a922008-12-09 15:10:17 -0800435
436 if (~crc != le32_to_cpu(fr_crc(fp))) {
437crc_err:
Robert Love3a3b42b2009-11-03 11:47:39 -0800438 stats = fc_lport_get_stats(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800439 stats->ErrorFrames++;
Robert Love582b45b2009-03-31 15:51:50 -0700440 /* FIXME - per cpu count, not total count! */
Robert Love42e9a922008-12-09 15:10:17 -0800441 if (stats->InvalidCRCCount++ < 5)
Robert Love74147052009-06-10 15:31:10 -0700442 printk(KERN_WARNING "libfc: CRC error on data "
443 "frame for port (%6x)\n",
Robert Love3a3b42b2009-11-03 11:47:39 -0800444 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800445 /*
446 * Assume the frame is total garbage.
447 * We may have copied it over the good part
448 * of the buffer.
449 * If so, we need to retry the entire operation.
450 * Otherwise, ignore it.
451 */
452 if (fsp->state & FC_SRB_DISCONTIG)
453 fc_fcp_retry_cmd(fsp);
454 return;
455 }
456 }
457
458 if (fsp->xfer_contig_end == start_offset)
459 fsp->xfer_contig_end += copy_len;
460 fsp->xfer_len += copy_len;
461
462 /*
463 * In the very rare event that this data arrived after the response
464 * and completes the transfer, call the completion handler.
465 */
466 if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
467 fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
468 fc_fcp_complete_locked(fsp);
469}
470
Robert Love34f42a02009-02-27 10:55:45 -0800471/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800472 * fc_fcp_send_data() - Send SCSI data to a target
473 * @fsp: The FCP packet the data is on
474 * @sp: The sequence the data is to be sent on
475 * @offset: The starting offset for this data request
476 * @seq_blen: The burst length for this data request
Robert Love42e9a922008-12-09 15:10:17 -0800477 *
478 * Called after receiving a Transfer Ready data descriptor.
Robert Love3a3b42b2009-11-03 11:47:39 -0800479 * If the LLD is capable of sequence offload then send down the
480 * seq_blen ammount of data in single frame, otherwise send
481 * multiple frames of the maximum frame payload supported by
482 * the target port.
Robert Love42e9a922008-12-09 15:10:17 -0800483 */
484static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
485 size_t offset, size_t seq_blen)
486{
487 struct fc_exch *ep;
488 struct scsi_cmnd *sc;
489 struct scatterlist *sg;
490 struct fc_frame *fp = NULL;
Robert Love3a3b42b2009-11-03 11:47:39 -0800491 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800492 size_t remaining;
493 size_t t_blen;
494 size_t tlen;
495 size_t sg_bytes;
496 size_t frame_offset, fh_parm_offset;
497 int error;
498 void *data = NULL;
499 void *page_addr;
Robert Love3a3b42b2009-11-03 11:47:39 -0800500 int using_sg = lport->sg_supp;
Robert Love42e9a922008-12-09 15:10:17 -0800501 u32 f_ctl;
502
503 WARN_ON(seq_blen <= 0);
504 if (unlikely(offset + seq_blen > fsp->data_len)) {
505 /* this should never happen */
Robert Love74147052009-06-10 15:31:10 -0700506 FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
507 "offset %zx\n", seq_blen, offset);
Robert Love42e9a922008-12-09 15:10:17 -0800508 fc_fcp_send_abort(fsp);
509 return 0;
510 } else if (offset != fsp->xfer_len) {
511 /* Out of Order Data Request - no problem, but unexpected. */
Robert Love74147052009-06-10 15:31:10 -0700512 FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
513 "seq_blen %zx offset %zx\n", seq_blen, offset);
Robert Love42e9a922008-12-09 15:10:17 -0800514 }
515
516 /*
517 * if LLD is capable of seq_offload then set transport
518 * burst length (t_blen) to seq_blen, otherwise set t_blen
519 * to max FC frame payload previously set in fsp->max_payload.
520 */
Yi Zou276d6812009-02-27 14:07:10 -0800521 t_blen = fsp->max_payload;
Robert Love3a3b42b2009-11-03 11:47:39 -0800522 if (lport->seq_offload) {
523 t_blen = min(seq_blen, (size_t)lport->lso_max);
Robert Love74147052009-06-10 15:31:10 -0700524 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
Robert Love3a3b42b2009-11-03 11:47:39 -0800525 fsp, seq_blen, lport->lso_max, t_blen);
Yi Zou276d6812009-02-27 14:07:10 -0800526 }
527
Robert Love42e9a922008-12-09 15:10:17 -0800528 WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
529 if (t_blen > 512)
530 t_blen &= ~(512 - 1); /* round down to block size */
531 WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
532 sc = fsp->cmd;
533
534 remaining = seq_blen;
535 fh_parm_offset = frame_offset = offset;
536 tlen = 0;
Robert Love3a3b42b2009-11-03 11:47:39 -0800537 seq = lport->tt.seq_start_next(seq);
Robert Love42e9a922008-12-09 15:10:17 -0800538 f_ctl = FC_FC_REL_OFF;
539 WARN_ON(!seq);
540
Robert Love42e9a922008-12-09 15:10:17 -0800541 sg = scsi_sglist(sc);
542
543 while (remaining > 0 && sg) {
544 if (offset >= sg->length) {
545 offset -= sg->length;
546 sg = sg_next(sg);
547 continue;
548 }
549 if (!fp) {
550 tlen = min(t_blen, remaining);
551
552 /*
553 * TODO. Temporary workaround. fc_seq_send() can't
554 * handle odd lengths in non-linear skbs.
555 * This will be the final fragment only.
556 */
557 if (tlen % 4)
558 using_sg = 0;
Vasu Deva7bbc7f2009-11-03 11:47:55 -0800559 fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
560 if (!fp)
561 return -ENOMEM;
Robert Love42e9a922008-12-09 15:10:17 -0800562
Vasu Deva7bbc7f2009-11-03 11:47:55 -0800563 data = fc_frame_header_get(fp) + 1;
Robert Love42e9a922008-12-09 15:10:17 -0800564 fh_parm_offset = frame_offset;
565 fr_max_payload(fp) = fsp->max_payload;
566 }
567 sg_bytes = min(tlen, sg->length - offset);
568 if (using_sg) {
Robert Love42e9a922008-12-09 15:10:17 -0800569 get_page(sg_page(sg));
570 skb_fill_page_desc(fp_skb(fp),
571 skb_shinfo(fp_skb(fp))->nr_frags,
572 sg_page(sg), sg->offset + offset,
573 sg_bytes);
574 fp_skb(fp)->data_len += sg_bytes;
575 fr_len(fp) += sg_bytes;
576 fp_skb(fp)->truesize += PAGE_SIZE;
577 } else {
578 size_t off = offset + sg->offset;
579
580 /*
581 * The scatterlist item may be bigger than PAGE_SIZE,
582 * but we must not cross pages inside the kmap.
583 */
584 sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
585 (off & ~PAGE_MASK)));
586 page_addr = kmap_atomic(sg_page(sg) +
587 (off >> PAGE_SHIFT),
588 KM_SOFTIRQ0);
589 memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
590 sg_bytes);
591 kunmap_atomic(page_addr, KM_SOFTIRQ0);
592 data += sg_bytes;
593 }
594 offset += sg_bytes;
595 frame_offset += sg_bytes;
596 tlen -= sg_bytes;
597 remaining -= sg_bytes;
598
Yi Zoud37322a2009-10-21 16:27:58 -0700599 if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
600 (tlen))
Robert Love42e9a922008-12-09 15:10:17 -0800601 continue;
602
603 /*
604 * Send sequence with transfer sequence initiative in case
605 * this is last FCP frame of the sequence.
606 */
607 if (remaining == 0)
608 f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
609
610 ep = fc_seq_exch(seq);
611 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
612 FC_TYPE_FCP, f_ctl, fh_parm_offset);
613
614 /*
615 * send fragment using for a sequence.
616 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800617 error = lport->tt.seq_send(lport, seq, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800618 if (error) {
619 WARN_ON(1); /* send error should be rare */
620 fc_fcp_retry_cmd(fsp);
621 return 0;
622 }
623 fp = NULL;
624 }
625 fsp->xfer_len += seq_blen; /* premature count? */
626 return 0;
627}
628
Robert Love3a3b42b2009-11-03 11:47:39 -0800629/**
630 * fc_fcp_abts_resp() - Send an ABTS response
631 * @fsp: The FCP packet that is being aborted
632 * @fp: The response frame
633 */
Robert Love42e9a922008-12-09 15:10:17 -0800634static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
635{
636 int ba_done = 1;
637 struct fc_ba_rjt *brp;
638 struct fc_frame_header *fh;
639
640 fh = fc_frame_header_get(fp);
641 switch (fh->fh_r_ctl) {
642 case FC_RCTL_BA_ACC:
643 break;
644 case FC_RCTL_BA_RJT:
645 brp = fc_frame_payload_get(fp, sizeof(*brp));
646 if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
647 break;
648 /* fall thru */
649 default:
650 /*
651 * we will let the command timeout
652 * and scsi-ml recover in this case,
653 * therefore cleared the ba_done flag.
654 */
655 ba_done = 0;
656 }
657
658 if (ba_done) {
659 fsp->state |= FC_SRB_ABORTED;
660 fsp->state &= ~FC_SRB_ABORT_PENDING;
661
662 if (fsp->wait_for_comp)
663 complete(&fsp->tm_done);
664 else
665 fc_fcp_complete_locked(fsp);
666 }
667}
668
Robert Love34f42a02009-02-27 10:55:45 -0800669/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800670 * fc_fcp_recv() - Reveive an FCP frame
Robert Love34f42a02009-02-27 10:55:45 -0800671 * @seq: The sequence the frame is on
Robert Love3a3b42b2009-11-03 11:47:39 -0800672 * @fp: The received frame
Robert Love34f42a02009-02-27 10:55:45 -0800673 * @arg: The related FCP packet
Robert Love42e9a922008-12-09 15:10:17 -0800674 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800675 * Context: Called from Soft IRQ context. Can not be called
676 * holding the FCP packet list lock.
Robert Love42e9a922008-12-09 15:10:17 -0800677 */
678static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
679{
680 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
Robert Lovea29e7642009-04-21 16:27:41 -0700681 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800682 struct fc_frame_header *fh;
683 struct fcp_txrdy *dd;
684 u8 r_ctl;
685 int rc = 0;
686
Vasu Devc46be112009-11-03 11:48:00 -0800687 if (IS_ERR(fp)) {
688 fc_fcp_error(fsp, fp);
689 return;
690 }
Robert Love42e9a922008-12-09 15:10:17 -0800691
692 fh = fc_frame_header_get(fp);
693 r_ctl = fh->fh_r_ctl;
Robert Love42e9a922008-12-09 15:10:17 -0800694
Robert Lovea29e7642009-04-21 16:27:41 -0700695 if (!(lport->state & LPORT_ST_READY))
Robert Love42e9a922008-12-09 15:10:17 -0800696 goto out;
697 if (fc_fcp_lock_pkt(fsp))
698 goto out;
699 fsp->last_pkt_time = jiffies;
700
701 if (fh->fh_type == FC_TYPE_BLS) {
702 fc_fcp_abts_resp(fsp, fp);
703 goto unlock;
704 }
705
706 if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
707 goto unlock;
708
709 if (r_ctl == FC_RCTL_DD_DATA_DESC) {
710 /*
711 * received XFER RDY from the target
712 * need to send data to the target
713 */
714 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
715 dd = fc_frame_payload_get(fp, sizeof(*dd));
716 WARN_ON(!dd);
717
718 rc = fc_fcp_send_data(fsp, seq,
719 (size_t) ntohl(dd->ft_data_ro),
720 (size_t) ntohl(dd->ft_burst_len));
721 if (!rc)
722 seq->rec_data = fsp->xfer_len;
723 else if (rc == -ENOMEM)
724 fsp->state |= FC_SRB_NOMEM;
725 } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
726 /*
727 * received a DATA frame
728 * next we will copy the data to the system buffer
729 */
730 WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
731 fc_fcp_recv_data(fsp, fp);
732 seq->rec_data = fsp->xfer_contig_end;
733 } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
734 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
735
736 fc_fcp_resp(fsp, fp);
737 } else {
Robert Love74147052009-06-10 15:31:10 -0700738 FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
Robert Love42e9a922008-12-09 15:10:17 -0800739 }
740unlock:
741 fc_fcp_unlock_pkt(fsp);
742out:
743 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -0800744}
745
Robert Love3a3b42b2009-11-03 11:47:39 -0800746/**
747 * fc_fcp_resp() - Handler for FCP responses
748 * @fsp: The FCP packet the response is for
749 * @fp: The response frame
750 */
Robert Love42e9a922008-12-09 15:10:17 -0800751static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
752{
753 struct fc_frame_header *fh;
754 struct fcp_resp *fc_rp;
755 struct fcp_resp_ext *rp_ex;
756 struct fcp_resp_rsp_info *fc_rp_info;
757 u32 plen;
758 u32 expected_len;
759 u32 respl = 0;
760 u32 snsl = 0;
761 u8 flags = 0;
762
763 plen = fr_len(fp);
764 fh = (struct fc_frame_header *)fr_hdr(fp);
765 if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
766 goto len_err;
767 plen -= sizeof(*fh);
768 fc_rp = (struct fcp_resp *)(fh + 1);
769 fsp->cdb_status = fc_rp->fr_status;
770 flags = fc_rp->fr_flags;
771 fsp->scsi_comp_flags = flags;
772 expected_len = fsp->data_len;
773
Yi Zoub277d2a2009-02-27 14:07:21 -0800774 /* if ddp, update xfer len */
775 fc_fcp_ddp_done(fsp);
776
Robert Love42e9a922008-12-09 15:10:17 -0800777 if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
778 rp_ex = (void *)(fc_rp + 1);
779 if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
780 if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
781 goto len_err;
782 fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
783 if (flags & FCP_RSP_LEN_VAL) {
784 respl = ntohl(rp_ex->fr_rsp_len);
785 if (respl != sizeof(*fc_rp_info))
786 goto len_err;
787 if (fsp->wait_for_comp) {
788 /* Abuse cdb_status for rsp code */
789 fsp->cdb_status = fc_rp_info->rsp_code;
790 complete(&fsp->tm_done);
791 /*
792 * tmfs will not have any scsi cmd so
793 * exit here
794 */
795 return;
796 } else
797 goto err;
798 }
799 if (flags & FCP_SNS_LEN_VAL) {
800 snsl = ntohl(rp_ex->fr_sns_len);
801 if (snsl > SCSI_SENSE_BUFFERSIZE)
802 snsl = SCSI_SENSE_BUFFERSIZE;
803 memcpy(fsp->cmd->sense_buffer,
804 (char *)fc_rp_info + respl, snsl);
805 }
806 }
807 if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
808 if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
809 goto len_err;
810 if (flags & FCP_RESID_UNDER) {
811 fsp->scsi_resid = ntohl(rp_ex->fr_resid);
812 /*
813 * The cmnd->underflow is the minimum number of
814 * bytes that must be transfered for this
815 * command. Provided a sense condition is not
816 * present, make sure the actual amount
817 * transferred is at least the underflow value
818 * or fail.
819 */
820 if (!(flags & FCP_SNS_LEN_VAL) &&
821 (fc_rp->fr_status == 0) &&
822 (scsi_bufflen(fsp->cmd) -
823 fsp->scsi_resid) < fsp->cmd->underflow)
824 goto err;
825 expected_len -= fsp->scsi_resid;
826 } else {
827 fsp->status_code = FC_ERROR;
828 }
829 }
830 }
831 fsp->state |= FC_SRB_RCV_STATUS;
832
833 /*
834 * Check for missing or extra data frames.
835 */
836 if (unlikely(fsp->xfer_len != expected_len)) {
837 if (fsp->xfer_len < expected_len) {
838 /*
839 * Some data may be queued locally,
840 * Wait a at least one jiffy to see if it is delivered.
841 * If this expires without data, we may do SRR.
842 */
843 fc_fcp_timer_set(fsp, 2);
844 return;
845 }
846 fsp->status_code = FC_DATA_OVRRUN;
Robert Love74147052009-06-10 15:31:10 -0700847 FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
848 "len %x, data len %x\n",
849 fsp->rport->port_id,
850 fsp->xfer_len, expected_len, fsp->data_len);
Robert Love42e9a922008-12-09 15:10:17 -0800851 }
852 fc_fcp_complete_locked(fsp);
853 return;
854
855len_err:
Robert Love74147052009-06-10 15:31:10 -0700856 FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
857 "snsl %u\n", flags, fr_len(fp), respl, snsl);
Robert Love42e9a922008-12-09 15:10:17 -0800858err:
859 fsp->status_code = FC_ERROR;
860 fc_fcp_complete_locked(fsp);
861}
862
863/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800864 * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
865 * fcp_pkt lock held
866 * @fsp: The FCP packet to be completed
Robert Love42e9a922008-12-09 15:10:17 -0800867 *
868 * This function may sleep if a timer is pending. The packet lock must be
869 * held, and the host lock must not be held.
870 */
871static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
872{
Robert Love3a3b42b2009-11-03 11:47:39 -0800873 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800874 struct fc_seq *seq;
875 struct fc_exch *ep;
876 u32 f_ctl;
877
878 if (fsp->state & FC_SRB_ABORT_PENDING)
879 return;
880
881 if (fsp->state & FC_SRB_ABORTED) {
882 if (!fsp->status_code)
883 fsp->status_code = FC_CMD_ABORTED;
884 } else {
885 /*
886 * Test for transport underrun, independent of response
887 * underrun status.
888 */
889 if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
890 (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
891 fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
892 fsp->status_code = FC_DATA_UNDRUN;
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -0500893 fsp->io_status = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800894 }
895 }
896
897 seq = fsp->seq_ptr;
898 if (seq) {
899 fsp->seq_ptr = NULL;
900 if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
901 struct fc_frame *conf_frame;
902 struct fc_seq *csp;
903
Robert Love3a3b42b2009-11-03 11:47:39 -0800904 csp = lport->tt.seq_start_next(seq);
Vasu Devc46be112009-11-03 11:48:00 -0800905 conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800906 if (conf_frame) {
907 f_ctl = FC_FC_SEQ_INIT;
908 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
909 ep = fc_seq_exch(seq);
910 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
911 ep->did, ep->sid,
912 FC_TYPE_FCP, f_ctl, 0);
Robert Love3a3b42b2009-11-03 11:47:39 -0800913 lport->tt.seq_send(lport, csp, conf_frame);
Robert Love42e9a922008-12-09 15:10:17 -0800914 }
915 }
Robert Love3a3b42b2009-11-03 11:47:39 -0800916 lport->tt.exch_done(seq);
Robert Love42e9a922008-12-09 15:10:17 -0800917 }
918 fc_io_compl(fsp);
919}
920
Robert Love3a3b42b2009-11-03 11:47:39 -0800921/**
922 * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
923 * @fsp: The FCP packet whose exchanges should be canceled
924 * @error: The reason for the cancellation
925 */
Robert Love42e9a922008-12-09 15:10:17 -0800926static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
927{
Robert Love3a3b42b2009-11-03 11:47:39 -0800928 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800929
930 if (fsp->seq_ptr) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800931 lport->tt.exch_done(fsp->seq_ptr);
Robert Love42e9a922008-12-09 15:10:17 -0800932 fsp->seq_ptr = NULL;
933 }
934 fsp->status_code = error;
935}
936
937/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800938 * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
939 * @lport: The local port whose exchanges should be canceled
940 * @id: The target's ID
941 * @lun: The LUN
942 * @error: The reason for cancellation
Robert Love42e9a922008-12-09 15:10:17 -0800943 *
944 * If lun or id is -1, they are ignored.
945 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800946static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
Robert Love42e9a922008-12-09 15:10:17 -0800947 unsigned int lun, int error)
948{
Robert Love3a3b42b2009-11-03 11:47:39 -0800949 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800950 struct fc_fcp_pkt *fsp;
951 struct scsi_cmnd *sc_cmd;
952 unsigned long flags;
953
Robert Love3a3b42b2009-11-03 11:47:39 -0800954 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800955restart:
956 list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
957 sc_cmd = fsp->cmd;
958 if (id != -1 && scmd_id(sc_cmd) != id)
959 continue;
960
961 if (lun != -1 && sc_cmd->device->lun != lun)
962 continue;
963
964 fc_fcp_pkt_hold(fsp);
Robert Love3a3b42b2009-11-03 11:47:39 -0800965 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800966
967 if (!fc_fcp_lock_pkt(fsp)) {
968 fc_fcp_cleanup_cmd(fsp, error);
969 fc_io_compl(fsp);
970 fc_fcp_unlock_pkt(fsp);
971 }
972
973 fc_fcp_pkt_release(fsp);
Robert Love3a3b42b2009-11-03 11:47:39 -0800974 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800975 /*
976 * while we dropped the lock multiple pkts could
977 * have been released, so we have to start over.
978 */
979 goto restart;
980 }
Robert Love3a3b42b2009-11-03 11:47:39 -0800981 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -0800982}
983
984/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800985 * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
986 * @lport: The local port whose exchanges are to be aborted
Robert Love42e9a922008-12-09 15:10:17 -0800987 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800988static void fc_fcp_abort_io(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800989{
Robert Love3a3b42b2009-11-03 11:47:39 -0800990 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
991}
992
993/**
994 * fc_fcp_pkt_send() - Send a fcp_pkt
995 * @lport: The local port to send the FCP packet on
996 * @fsp: The FCP packet to send
997 *
998 * Return: Zero for success and -1 for failure
999 * Locks: Called with the host lock and irqs disabled.
1000 */
1001static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
1002{
1003 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001004 int rc;
1005
1006 fsp->cmd->SCp.ptr = (char *)fsp;
1007 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1008 fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
1009
1010 int_to_scsilun(fsp->cmd->device->lun,
1011 (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1012 memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
1013 list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1014
Robert Love3a3b42b2009-11-03 11:47:39 -08001015 spin_unlock_irq(lport->host->host_lock);
1016 rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
1017 spin_lock_irq(lport->host->host_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001018 if (rc)
1019 list_del(&fsp->list);
1020
1021 return rc;
1022}
1023
Robert Love3a3b42b2009-11-03 11:47:39 -08001024/**
1025 * fc_fcp_cmd_send() - Send a FCP command
1026 * @lport: The local port to send the command on
1027 * @fsp: The FCP packet the command is on
1028 * @resp: The handler for the response
1029 */
1030static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
Robert Love42e9a922008-12-09 15:10:17 -08001031 void (*resp)(struct fc_seq *,
1032 struct fc_frame *fp,
1033 void *arg))
1034{
1035 struct fc_frame *fp;
1036 struct fc_seq *seq;
1037 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001038 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001039 const size_t len = sizeof(fsp->cdb_cmd);
1040 int rc = 0;
1041
1042 if (fc_fcp_lock_pkt(fsp))
1043 return 0;
1044
Vasu Devc46be112009-11-03 11:48:00 -08001045 fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
Robert Love42e9a922008-12-09 15:10:17 -08001046 if (!fp) {
1047 rc = -1;
1048 goto unlock;
1049 }
1050
1051 memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
Yi Zoub277d2a2009-02-27 14:07:21 -08001052 fr_fsp(fp) = fsp;
Robert Love42e9a922008-12-09 15:10:17 -08001053 rport = fsp->rport;
1054 fsp->max_payload = rport->maxframe_size;
Robert Love3a3b42b2009-11-03 11:47:39 -08001055 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001056
1057 fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
Robert Love3a3b42b2009-11-03 11:47:39 -08001058 fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
Robert Love42e9a922008-12-09 15:10:17 -08001059 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1060
Robert Love3a3b42b2009-11-03 11:47:39 -08001061 seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
1062 fsp, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001063 if (!seq) {
Robert Love42e9a922008-12-09 15:10:17 -08001064 rc = -1;
1065 goto unlock;
1066 }
1067 fsp->last_pkt_time = jiffies;
1068 fsp->seq_ptr = seq;
1069 fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
1070
1071 setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
1072 fc_fcp_timer_set(fsp,
1073 (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
1074 FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
1075unlock:
1076 fc_fcp_unlock_pkt(fsp);
1077 return rc;
1078}
1079
Robert Love3a3b42b2009-11-03 11:47:39 -08001080/**
1081 * fc_fcp_error() - Handler for FCP layer errors
1082 * @fsp: The FCP packet the error is on
1083 * @fp: The frame that has errored
Robert Love42e9a922008-12-09 15:10:17 -08001084 */
1085static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1086{
1087 int error = PTR_ERR(fp);
1088
1089 if (fc_fcp_lock_pkt(fsp))
1090 return;
1091
Robert Love74147052009-06-10 15:31:10 -07001092 if (error == -FC_EX_CLOSED) {
Robert Love42e9a922008-12-09 15:10:17 -08001093 fc_fcp_retry_cmd(fsp);
1094 goto unlock;
Robert Love42e9a922008-12-09 15:10:17 -08001095 }
Robert Love74147052009-06-10 15:31:10 -07001096
Robert Love42e9a922008-12-09 15:10:17 -08001097 /*
1098 * clear abort pending, because the lower layer
1099 * decided to force completion.
1100 */
1101 fsp->state &= ~FC_SRB_ABORT_PENDING;
1102 fsp->status_code = FC_CMD_PLOGO;
1103 fc_fcp_complete_locked(fsp);
1104unlock:
1105 fc_fcp_unlock_pkt(fsp);
1106}
1107
Robert Love3a3b42b2009-11-03 11:47:39 -08001108/**
1109 * fc_fcp_pkt_abort() - Abort a fcp_pkt
1110 * @fsp: The FCP packet to abort on
1111 *
1112 * Called to send an abort and then wait for abort completion
Robert Love42e9a922008-12-09 15:10:17 -08001113 */
Robert Lovec3401112009-10-21 16:27:06 -07001114static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
Robert Love42e9a922008-12-09 15:10:17 -08001115{
1116 int rc = FAILED;
1117
1118 if (fc_fcp_send_abort(fsp))
1119 return FAILED;
1120
1121 init_completion(&fsp->tm_done);
1122 fsp->wait_for_comp = 1;
1123
1124 spin_unlock_bh(&fsp->scsi_pkt_lock);
1125 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1126 spin_lock_bh(&fsp->scsi_pkt_lock);
1127 fsp->wait_for_comp = 0;
1128
1129 if (!rc) {
Robert Love74147052009-06-10 15:31:10 -07001130 FC_FCP_DBG(fsp, "target abort cmd failed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001131 rc = FAILED;
1132 } else if (fsp->state & FC_SRB_ABORTED) {
Robert Love74147052009-06-10 15:31:10 -07001133 FC_FCP_DBG(fsp, "target abort cmd passed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001134 rc = SUCCESS;
1135 fc_fcp_complete_locked(fsp);
1136 }
1137
1138 return rc;
1139}
1140
Robert Love3a3b42b2009-11-03 11:47:39 -08001141/**
1142 * fc_lun_reset_send() - Send LUN reset command
1143 * @data: The FCP packet that identifies the LUN to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001144 */
1145static void fc_lun_reset_send(unsigned long data)
1146{
1147 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
Robert Love3a3b42b2009-11-03 11:47:39 -08001148 struct fc_lport *lport = fsp->lp;
1149 if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
Robert Love42e9a922008-12-09 15:10:17 -08001150 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
1151 return;
1152 if (fc_fcp_lock_pkt(fsp))
1153 return;
1154 setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
1155 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1156 fc_fcp_unlock_pkt(fsp);
1157 }
1158}
1159
Robert Love3a3b42b2009-11-03 11:47:39 -08001160/**
1161 * fc_lun_reset() - Send a LUN RESET command to a device
1162 * and wait for the reply
1163 * @lport: The local port to sent the comand on
1164 * @fsp: The FCP packet that identifies the LUN to be reset
1165 * @id: The SCSI command ID
1166 * @lun: The LUN ID to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001167 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001168static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
Robert Love42e9a922008-12-09 15:10:17 -08001169 unsigned int id, unsigned int lun)
1170{
1171 int rc;
1172
1173 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1174 fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
1175 int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1176
1177 fsp->wait_for_comp = 1;
1178 init_completion(&fsp->tm_done);
1179
1180 fc_lun_reset_send((unsigned long)fsp);
1181
1182 /*
1183 * wait for completion of reset
1184 * after that make sure all commands are terminated
1185 */
1186 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1187
1188 spin_lock_bh(&fsp->scsi_pkt_lock);
1189 fsp->state |= FC_SRB_COMPL;
1190 spin_unlock_bh(&fsp->scsi_pkt_lock);
1191
1192 del_timer_sync(&fsp->timer);
1193
1194 spin_lock_bh(&fsp->scsi_pkt_lock);
1195 if (fsp->seq_ptr) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001196 lport->tt.exch_done(fsp->seq_ptr);
Robert Love42e9a922008-12-09 15:10:17 -08001197 fsp->seq_ptr = NULL;
1198 }
1199 fsp->wait_for_comp = 0;
1200 spin_unlock_bh(&fsp->scsi_pkt_lock);
1201
1202 if (!rc) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001203 FC_SCSI_DBG(lport, "lun reset failed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001204 return FAILED;
1205 }
1206
1207 /* cdb_status holds the tmf's rsp code */
1208 if (fsp->cdb_status != FCP_TMF_CMPL)
1209 return FAILED;
1210
Robert Love3a3b42b2009-11-03 11:47:39 -08001211 FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
1212 fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
Robert Love42e9a922008-12-09 15:10:17 -08001213 return SUCCESS;
1214}
1215
Robert Love3a3b42b2009-11-03 11:47:39 -08001216/**
1217 * fc_tm_done() - Task Managment response handler
1218 * @seq: The sequence that the response is on
1219 * @fp: The response frame
1220 * @arg: The FCP packet the response is for
Robert Love42e9a922008-12-09 15:10:17 -08001221 */
1222static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1223{
1224 struct fc_fcp_pkt *fsp = arg;
1225 struct fc_frame_header *fh;
1226
1227 if (IS_ERR(fp)) {
1228 /*
1229 * If there is an error just let it timeout or wait
1230 * for TMF to be aborted if it timedout.
1231 *
1232 * scsi-eh will escalate for when either happens.
1233 */
1234 return;
1235 }
1236
1237 if (fc_fcp_lock_pkt(fsp))
1238 return;
1239
1240 /*
1241 * raced with eh timeout handler.
1242 */
1243 if (!fsp->seq_ptr || !fsp->wait_for_comp) {
1244 spin_unlock_bh(&fsp->scsi_pkt_lock);
1245 return;
1246 }
1247
1248 fh = fc_frame_header_get(fp);
1249 if (fh->fh_type != FC_TYPE_BLS)
1250 fc_fcp_resp(fsp, fp);
1251 fsp->seq_ptr = NULL;
1252 fsp->lp->tt.exch_done(seq);
1253 fc_frame_free(fp);
1254 fc_fcp_unlock_pkt(fsp);
1255}
1256
Robert Love3a3b42b2009-11-03 11:47:39 -08001257/**
1258 * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
1259 * @lport: The local port to be cleaned up
1260 */
1261static void fc_fcp_cleanup(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001262{
Robert Love3a3b42b2009-11-03 11:47:39 -08001263 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001264}
1265
Robert Love3a3b42b2009-11-03 11:47:39 -08001266/**
1267 * fc_fcp_timeout() - Handler for fcp_pkt timeouts
1268 * @data: The FCP packet that has timed out
Robert Love42e9a922008-12-09 15:10:17 -08001269 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001270 * If REC is supported then just issue it and return. The REC exchange will
1271 * complete or time out and recovery can continue at that point. Otherwise,
1272 * if the response has been received without all the data it has been
1273 * ER_TIMEOUT since the response was received. If the response has not been
1274 * received we see if data was received recently. If it has been then we
1275 * continue waiting, otherwise, we abort the command.
Robert Love42e9a922008-12-09 15:10:17 -08001276 */
1277static void fc_fcp_timeout(unsigned long data)
1278{
1279 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1280 struct fc_rport *rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001281 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001282
1283 if (fc_fcp_lock_pkt(fsp))
1284 return;
1285
1286 if (fsp->cdb_cmd.fc_tm_flags)
1287 goto unlock;
1288
1289 fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
1290
Robert Love3a3b42b2009-11-03 11:47:39 -08001291 if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
Robert Love42e9a922008-12-09 15:10:17 -08001292 fc_fcp_rec(fsp);
1293 else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
1294 jiffies))
1295 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1296 else if (fsp->state & FC_SRB_RCV_STATUS)
1297 fc_fcp_complete_locked(fsp);
1298 else
1299 fc_timeout_error(fsp);
1300 fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
1301unlock:
1302 fc_fcp_unlock_pkt(fsp);
1303}
1304
Robert Love3a3b42b2009-11-03 11:47:39 -08001305/**
1306 * fc_fcp_rec() - Send a REC ELS request
1307 * @fsp: The FCP packet to send the REC request on
Robert Love42e9a922008-12-09 15:10:17 -08001308 */
1309static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
1310{
Robert Love3a3b42b2009-11-03 11:47:39 -08001311 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001312 struct fc_frame *fp;
1313 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001314 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001315
Robert Love3a3b42b2009-11-03 11:47:39 -08001316 lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -08001317 rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001318 rpriv = rport->dd_data;
1319 if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
Robert Love42e9a922008-12-09 15:10:17 -08001320 fsp->status_code = FC_HRD_ERROR;
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001321 fsp->io_status = 0;
Robert Love42e9a922008-12-09 15:10:17 -08001322 fc_fcp_complete_locked(fsp);
1323 return;
1324 }
Vasu Devc46be112009-11-03 11:48:00 -08001325 fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
Robert Love42e9a922008-12-09 15:10:17 -08001326 if (!fp)
1327 goto retry;
1328
1329 fr_seq(fp) = fsp->seq_ptr;
1330 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
Robert Love3a3b42b2009-11-03 11:47:39 -08001331 fc_host_port_id(rpriv->local_port->host), FC_TYPE_ELS,
Robert Love42e9a922008-12-09 15:10:17 -08001332 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
Robert Love3a3b42b2009-11-03 11:47:39 -08001333 if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
1334 fc_fcp_rec_resp, fsp,
1335 jiffies_to_msecs(FC_SCSI_REC_TOV))) {
Robert Love42e9a922008-12-09 15:10:17 -08001336 fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
1337 return;
1338 }
Robert Love42e9a922008-12-09 15:10:17 -08001339retry:
1340 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1341 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1342 else
1343 fc_timeout_error(fsp);
1344}
1345
Robert Love3a3b42b2009-11-03 11:47:39 -08001346/**
1347 * fc_fcp_rec_resp() - Handler for REC ELS responses
1348 * @seq: The sequence the response is on
1349 * @fp: The response frame
1350 * @arg: The FCP packet the response is on
1351 *
1352 * If the response is a reject then the scsi layer will handle
1353 * the timeout. If the response is a LS_ACC then if the I/O was not completed
1354 * set the timeout and return. If the I/O was completed then complete the
1355 * exchange and tell the SCSI layer.
Robert Love42e9a922008-12-09 15:10:17 -08001356 */
1357static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1358{
1359 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
1360 struct fc_els_rec_acc *recp;
1361 struct fc_els_ls_rjt *rjt;
1362 u32 e_stat;
1363 u8 opcode;
1364 u32 offset;
1365 enum dma_data_direction data_dir;
1366 enum fc_rctl r_ctl;
Robert Love3a3b42b2009-11-03 11:47:39 -08001367 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001368
1369 if (IS_ERR(fp)) {
1370 fc_fcp_rec_error(fsp, fp);
1371 return;
1372 }
1373
1374 if (fc_fcp_lock_pkt(fsp))
1375 goto out;
1376
1377 fsp->recov_retry = 0;
1378 opcode = fc_frame_payload_op(fp);
1379 if (opcode == ELS_LS_RJT) {
1380 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1381 switch (rjt->er_reason) {
1382 default:
Robert Love74147052009-06-10 15:31:10 -07001383 FC_FCP_DBG(fsp, "device %x unexpected REC reject "
1384 "reason %d expl %d\n",
1385 fsp->rport->port_id, rjt->er_reason,
1386 rjt->er_explan);
Robert Love42e9a922008-12-09 15:10:17 -08001387 /* fall through */
1388 case ELS_RJT_UNSUP:
Robert Love74147052009-06-10 15:31:10 -07001389 FC_FCP_DBG(fsp, "device does not support REC\n");
Robert Love3a3b42b2009-11-03 11:47:39 -08001390 rpriv = fsp->rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001391 /*
1392 * if we do not spport RECs or got some bogus
1393 * reason then resetup timer so we check for
1394 * making progress.
1395 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001396 rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
Robert Love42e9a922008-12-09 15:10:17 -08001397 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1398 break;
1399 case ELS_RJT_LOGIC:
1400 case ELS_RJT_UNAB:
1401 /*
1402 * If no data transfer, the command frame got dropped
1403 * so we just retry. If data was transferred, we
1404 * lost the response but the target has no record,
1405 * so we abort and retry.
1406 */
1407 if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
1408 fsp->xfer_len == 0) {
1409 fc_fcp_retry_cmd(fsp);
1410 break;
1411 }
1412 fc_timeout_error(fsp);
1413 break;
1414 }
1415 } else if (opcode == ELS_LS_ACC) {
1416 if (fsp->state & FC_SRB_ABORTED)
1417 goto unlock_out;
1418
1419 data_dir = fsp->cmd->sc_data_direction;
1420 recp = fc_frame_payload_get(fp, sizeof(*recp));
1421 offset = ntohl(recp->reca_fc4value);
1422 e_stat = ntohl(recp->reca_e_stat);
1423
1424 if (e_stat & ESB_ST_COMPLETE) {
1425
1426 /*
1427 * The exchange is complete.
1428 *
1429 * For output, we must've lost the response.
1430 * For input, all data must've been sent.
1431 * We lost may have lost the response
1432 * (and a confirmation was requested) and maybe
1433 * some data.
1434 *
1435 * If all data received, send SRR
1436 * asking for response. If partial data received,
1437 * or gaps, SRR requests data at start of gap.
1438 * Recovery via SRR relies on in-order-delivery.
1439 */
1440 if (data_dir == DMA_TO_DEVICE) {
1441 r_ctl = FC_RCTL_DD_CMD_STATUS;
1442 } else if (fsp->xfer_contig_end == offset) {
1443 r_ctl = FC_RCTL_DD_CMD_STATUS;
1444 } else {
1445 offset = fsp->xfer_contig_end;
1446 r_ctl = FC_RCTL_DD_SOL_DATA;
1447 }
1448 fc_fcp_srr(fsp, r_ctl, offset);
1449 } else if (e_stat & ESB_ST_SEQ_INIT) {
1450
1451 /*
1452 * The remote port has the initiative, so just
1453 * keep waiting for it to complete.
1454 */
1455 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1456 } else {
1457
1458 /*
1459 * The exchange is incomplete, we have seq. initiative.
1460 * Lost response with requested confirmation,
1461 * lost confirmation, lost transfer ready or
1462 * lost write data.
1463 *
1464 * For output, if not all data was received, ask
1465 * for transfer ready to be repeated.
1466 *
1467 * If we received or sent all the data, send SRR to
1468 * request response.
1469 *
1470 * If we lost a response, we may have lost some read
1471 * data as well.
1472 */
1473 r_ctl = FC_RCTL_DD_SOL_DATA;
1474 if (data_dir == DMA_TO_DEVICE) {
1475 r_ctl = FC_RCTL_DD_CMD_STATUS;
1476 if (offset < fsp->data_len)
1477 r_ctl = FC_RCTL_DD_DATA_DESC;
1478 } else if (offset == fsp->xfer_contig_end) {
1479 r_ctl = FC_RCTL_DD_CMD_STATUS;
1480 } else if (fsp->xfer_contig_end < offset) {
1481 offset = fsp->xfer_contig_end;
1482 }
1483 fc_fcp_srr(fsp, r_ctl, offset);
1484 }
1485 }
1486unlock_out:
1487 fc_fcp_unlock_pkt(fsp);
1488out:
1489 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1490 fc_frame_free(fp);
1491}
1492
Robert Love3a3b42b2009-11-03 11:47:39 -08001493/**
1494 * fc_fcp_rec_error() - Handler for REC errors
1495 * @fsp: The FCP packet the error is on
1496 * @fp: The REC frame
Robert Love42e9a922008-12-09 15:10:17 -08001497 */
1498static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1499{
1500 int error = PTR_ERR(fp);
1501
1502 if (fc_fcp_lock_pkt(fsp))
1503 goto out;
1504
1505 switch (error) {
1506 case -FC_EX_CLOSED:
1507 fc_fcp_retry_cmd(fsp);
1508 break;
1509
1510 default:
Robert Love74147052009-06-10 15:31:10 -07001511 FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
1512 fsp, fsp->rport->port_id, error);
Robert Love42e9a922008-12-09 15:10:17 -08001513 fsp->status_code = FC_CMD_PLOGO;
1514 /* fall through */
1515
1516 case -FC_EX_TIMEOUT:
1517 /*
1518 * Assume REC or LS_ACC was lost.
1519 * The exchange manager will have aborted REC, so retry.
1520 */
Robert Love74147052009-06-10 15:31:10 -07001521 FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
1522 fsp->rport->port_id, error, fsp->recov_retry,
1523 FC_MAX_RECOV_RETRY);
Robert Love42e9a922008-12-09 15:10:17 -08001524 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1525 fc_fcp_rec(fsp);
1526 else
1527 fc_timeout_error(fsp);
1528 break;
1529 }
1530 fc_fcp_unlock_pkt(fsp);
1531out:
1532 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1533}
1534
Robert Love3a3b42b2009-11-03 11:47:39 -08001535/**
1536 * fc_timeout_error() - Handler for fcp_pkt timeouts
1537 * @fsp: The FCP packt that has timed out
Robert Love42e9a922008-12-09 15:10:17 -08001538 */
1539static void fc_timeout_error(struct fc_fcp_pkt *fsp)
1540{
1541 fsp->status_code = FC_CMD_TIME_OUT;
1542 fsp->cdb_status = 0;
1543 fsp->io_status = 0;
1544 /*
1545 * if this fails then we let the scsi command timer fire and
1546 * scsi-ml escalate.
1547 */
1548 fc_fcp_send_abort(fsp);
1549}
1550
Robert Love3a3b42b2009-11-03 11:47:39 -08001551/**
1552 * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
1553 * @fsp: The FCP packet the SRR is to be sent on
1554 * @r_ctl: The R_CTL field for the SRR request
Robert Love42e9a922008-12-09 15:10:17 -08001555 * This is called after receiving status but insufficient data, or
1556 * when expecting status but the request has timed out.
1557 */
1558static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1559{
Robert Love3a3b42b2009-11-03 11:47:39 -08001560 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -08001561 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001562 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001563 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1564 struct fc_seq *seq;
1565 struct fcp_srr *srr;
1566 struct fc_frame *fp;
1567 u8 cdb_op;
1568
1569 rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001570 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001571 cdb_op = fsp->cdb_cmd.fc_cdb[0];
1572
Robert Love3a3b42b2009-11-03 11:47:39 -08001573 if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
1574 rpriv->rp_state != RPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08001575 goto retry; /* shouldn't happen */
Vasu Devc46be112009-11-03 11:48:00 -08001576 fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
Robert Love42e9a922008-12-09 15:10:17 -08001577 if (!fp)
1578 goto retry;
1579
1580 srr = fc_frame_payload_get(fp, sizeof(*srr));
1581 memset(srr, 0, sizeof(*srr));
1582 srr->srr_op = ELS_SRR;
1583 srr->srr_ox_id = htons(ep->oxid);
1584 srr->srr_rx_id = htons(ep->rxid);
1585 srr->srr_r_ctl = r_ctl;
1586 srr->srr_rel_off = htonl(offset);
1587
1588 fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
Robert Love3a3b42b2009-11-03 11:47:39 -08001589 fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
Robert Love42e9a922008-12-09 15:10:17 -08001590 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1591
Robert Love3a3b42b2009-11-03 11:47:39 -08001592 seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
1593 fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
Chris Leech8f550f92009-10-21 16:28:09 -07001594 if (!seq)
Robert Love42e9a922008-12-09 15:10:17 -08001595 goto retry;
Chris Leech8f550f92009-10-21 16:28:09 -07001596
Robert Love42e9a922008-12-09 15:10:17 -08001597 fsp->recov_seq = seq;
1598 fsp->xfer_len = offset;
1599 fsp->xfer_contig_end = offset;
1600 fsp->state &= ~FC_SRB_RCV_STATUS;
1601 fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
1602 return;
1603retry:
1604 fc_fcp_retry_cmd(fsp);
1605}
1606
Robert Love3a3b42b2009-11-03 11:47:39 -08001607/**
1608 * fc_fcp_srr_resp() - Handler for SRR response
1609 * @seq: The sequence the SRR is on
1610 * @fp: The SRR frame
1611 * @arg: The FCP packet the SRR is on
Robert Love42e9a922008-12-09 15:10:17 -08001612 */
1613static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1614{
1615 struct fc_fcp_pkt *fsp = arg;
1616 struct fc_frame_header *fh;
1617
1618 if (IS_ERR(fp)) {
1619 fc_fcp_srr_error(fsp, fp);
1620 return;
1621 }
1622
1623 if (fc_fcp_lock_pkt(fsp))
1624 goto out;
1625
1626 fh = fc_frame_header_get(fp);
1627 /*
1628 * BUG? fc_fcp_srr_error calls exch_done which would release
1629 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
1630 * then fc_exch_timeout would be sending an abort. The exch_done
1631 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1632 * an abort response though.
1633 */
1634 if (fh->fh_type == FC_TYPE_BLS) {
1635 fc_fcp_unlock_pkt(fsp);
1636 return;
1637 }
1638
1639 fsp->recov_seq = NULL;
1640 switch (fc_frame_payload_op(fp)) {
1641 case ELS_LS_ACC:
1642 fsp->recov_retry = 0;
1643 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
1644 break;
1645 case ELS_LS_RJT:
1646 default:
1647 fc_timeout_error(fsp);
1648 break;
1649 }
1650 fc_fcp_unlock_pkt(fsp);
1651 fsp->lp->tt.exch_done(seq);
1652out:
1653 fc_frame_free(fp);
1654 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1655}
1656
Robert Love3a3b42b2009-11-03 11:47:39 -08001657/**
1658 * fc_fcp_srr_error() - Handler for SRR errors
1659 * @fsp: The FCP packet that the SRR error is on
1660 * @fp: The SRR frame
1661 */
Robert Love42e9a922008-12-09 15:10:17 -08001662static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1663{
1664 if (fc_fcp_lock_pkt(fsp))
1665 goto out;
1666 fsp->lp->tt.exch_done(fsp->recov_seq);
1667 fsp->recov_seq = NULL;
1668 switch (PTR_ERR(fp)) {
1669 case -FC_EX_TIMEOUT:
1670 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1671 fc_fcp_rec(fsp);
1672 else
1673 fc_timeout_error(fsp);
1674 break;
1675 case -FC_EX_CLOSED: /* e.g., link failure */
1676 /* fall through */
1677 default:
1678 fc_fcp_retry_cmd(fsp);
1679 break;
1680 }
1681 fc_fcp_unlock_pkt(fsp);
1682out:
1683 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1684}
1685
Robert Love3a3b42b2009-11-03 11:47:39 -08001686/**
1687 * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
1688 * @lport: The local port to be checked
1689 */
1690static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001691{
1692 /* lock ? */
Robert Love3a3b42b2009-11-03 11:47:39 -08001693 return (lport->state == LPORT_ST_READY) &&
1694 lport->link_up && !lport->qfull;
Robert Love42e9a922008-12-09 15:10:17 -08001695}
1696
1697/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001698 * fc_queuecommand() - The queuecommand function of the SCSI template
1699 * @cmd: The scsi_cmnd to be executed
1700 * @done: The callback function to be called when the scsi_cmnd is complete
Robert Love42e9a922008-12-09 15:10:17 -08001701 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001702 * This is the i/o strategy routine, called by the SCSI layer. This routine
1703 * is called with the host_lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001704 */
1705int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1706{
Robert Love3a3b42b2009-11-03 11:47:39 -08001707 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001708 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1709 struct fc_fcp_pkt *fsp;
Robert Love3a3b42b2009-11-03 11:47:39 -08001710 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001711 int rval;
1712 int rc = 0;
1713 struct fcoe_dev_stats *stats;
1714
Robert Love3a3b42b2009-11-03 11:47:39 -08001715 lport = shost_priv(sc_cmd->device->host);
Robert Love42e9a922008-12-09 15:10:17 -08001716
1717 rval = fc_remote_port_chkready(rport);
1718 if (rval) {
1719 sc_cmd->result = rval;
1720 done(sc_cmd);
1721 goto out;
1722 }
1723
1724 if (!*(struct fc_remote_port **)rport->dd_data) {
1725 /*
1726 * rport is transitioning from blocked/deleted to
1727 * online
1728 */
1729 sc_cmd->result = DID_IMM_RETRY << 16;
1730 done(sc_cmd);
1731 goto out;
1732 }
1733
Robert Love3a3b42b2009-11-03 11:47:39 -08001734 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001735
Robert Love3a3b42b2009-11-03 11:47:39 -08001736 if (!fc_fcp_lport_queue_ready(lport)) {
Robert Love42e9a922008-12-09 15:10:17 -08001737 rc = SCSI_MLQUEUE_HOST_BUSY;
1738 goto out;
1739 }
1740
Robert Love3a3b42b2009-11-03 11:47:39 -08001741 fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
Robert Love42e9a922008-12-09 15:10:17 -08001742 if (fsp == NULL) {
1743 rc = SCSI_MLQUEUE_HOST_BUSY;
1744 goto out;
1745 }
1746
1747 /*
1748 * build the libfc request pkt
1749 */
1750 fsp->cmd = sc_cmd; /* save the cmd */
Robert Love3a3b42b2009-11-03 11:47:39 -08001751 fsp->lp = lport; /* save the softc ptr */
Robert Love42e9a922008-12-09 15:10:17 -08001752 fsp->rport = rport; /* set the remote port ptr */
Yi Zou5e472d02009-10-21 16:26:50 -07001753 fsp->xfer_ddp = FC_XID_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -08001754 sc_cmd->scsi_done = done;
1755
1756 /*
1757 * set up the transfer length
1758 */
1759 fsp->data_len = scsi_bufflen(sc_cmd);
1760 fsp->xfer_len = 0;
1761
1762 /*
1763 * setup the data direction
1764 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001765 stats = fc_lport_get_stats(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001766 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1767 fsp->req_flags = FC_SRB_READ;
1768 stats->InputRequests++;
1769 stats->InputMegabytes = fsp->data_len;
1770 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1771 fsp->req_flags = FC_SRB_WRITE;
1772 stats->OutputRequests++;
1773 stats->OutputMegabytes = fsp->data_len;
1774 } else {
1775 fsp->req_flags = 0;
1776 stats->ControlRequests++;
1777 }
1778
Robert Love3a3b42b2009-11-03 11:47:39 -08001779 fsp->tgt_flags = rpriv->flags;
Robert Love42e9a922008-12-09 15:10:17 -08001780
1781 init_timer(&fsp->timer);
1782 fsp->timer.data = (unsigned long)fsp;
1783
1784 /*
1785 * send it to the lower layer
1786 * if we get -1 return then put the request in the pending
1787 * queue.
1788 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001789 rval = fc_fcp_pkt_send(lport, fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001790 if (rval != 0) {
1791 fsp->state = FC_SRB_FREE;
1792 fc_fcp_pkt_release(fsp);
1793 rc = SCSI_MLQUEUE_HOST_BUSY;
1794 }
1795out:
1796 return rc;
1797}
1798EXPORT_SYMBOL(fc_queuecommand);
1799
1800/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001801 * fc_io_compl() - Handle responses for completed commands
1802 * @fsp: The FCP packet that is complete
Robert Love42e9a922008-12-09 15:10:17 -08001803 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001804 * Translates fcp_pkt errors to a Linux SCSI errors.
Robert Love42e9a922008-12-09 15:10:17 -08001805 * The fcp packet lock must be held when calling.
1806 */
1807static void fc_io_compl(struct fc_fcp_pkt *fsp)
1808{
1809 struct fc_fcp_internal *si;
1810 struct scsi_cmnd *sc_cmd;
Robert Love3a3b42b2009-11-03 11:47:39 -08001811 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001812 unsigned long flags;
1813
Yi Zoub277d2a2009-02-27 14:07:21 -08001814 /* release outstanding ddp context */
1815 fc_fcp_ddp_done(fsp);
1816
Robert Love42e9a922008-12-09 15:10:17 -08001817 fsp->state |= FC_SRB_COMPL;
1818 if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
1819 spin_unlock_bh(&fsp->scsi_pkt_lock);
1820 del_timer_sync(&fsp->timer);
1821 spin_lock_bh(&fsp->scsi_pkt_lock);
1822 }
1823
Robert Love3a3b42b2009-11-03 11:47:39 -08001824 lport = fsp->lp;
1825 si = fc_get_scsi_internal(lport);
1826 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001827 if (!fsp->cmd) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001828 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001829 return;
1830 }
1831
1832 /*
1833 * if a command timed out while we had to try and throttle IO
1834 * and it is now getting cleaned up, then we are about to
1835 * try again so clear the throttled flag incase we get more
1836 * time outs.
1837 */
1838 if (si->throttled && fsp->state & FC_SRB_NOMEM)
1839 si->throttled = 0;
1840
1841 sc_cmd = fsp->cmd;
1842 fsp->cmd = NULL;
1843
1844 if (!sc_cmd->SCp.ptr) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001845 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001846 return;
1847 }
1848
1849 CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
1850 switch (fsp->status_code) {
1851 case FC_COMPLETE:
1852 if (fsp->cdb_status == 0) {
1853 /*
1854 * good I/O status
1855 */
1856 sc_cmd->result = DID_OK << 16;
1857 if (fsp->scsi_resid)
1858 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
Robert Love42e9a922008-12-09 15:10:17 -08001859 } else {
1860 /*
1861 * transport level I/O was ok but scsi
1862 * has non zero status
1863 */
1864 sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
1865 }
1866 break;
1867 case FC_ERROR:
1868 sc_cmd->result = DID_ERROR << 16;
1869 break;
1870 case FC_DATA_UNDRUN:
Vasu Dev26d9cab2009-02-27 10:55:07 -08001871 if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
Robert Love42e9a922008-12-09 15:10:17 -08001872 /*
1873 * scsi status is good but transport level
Vasu Dev26d9cab2009-02-27 10:55:07 -08001874 * underrun.
Robert Love42e9a922008-12-09 15:10:17 -08001875 */
Yi Zou4347fa62009-10-21 16:27:12 -07001876 sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
1877 DID_OK : DID_ERROR) << 16;
Robert Love42e9a922008-12-09 15:10:17 -08001878 } else {
1879 /*
1880 * scsi got underrun, this is an error
1881 */
1882 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1883 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1884 }
1885 break;
1886 case FC_DATA_OVRRUN:
1887 /*
1888 * overrun is an error
1889 */
1890 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
1891 break;
1892 case FC_CMD_ABORTED:
Mike Christied5e60542009-05-06 10:52:23 -07001893 sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
Robert Love42e9a922008-12-09 15:10:17 -08001894 break;
1895 case FC_CMD_TIME_OUT:
1896 sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
1897 break;
1898 case FC_CMD_RESET:
1899 sc_cmd->result = (DID_RESET << 16);
1900 break;
1901 case FC_HRD_ERROR:
1902 sc_cmd->result = (DID_NO_CONNECT << 16);
1903 break;
1904 default:
1905 sc_cmd->result = (DID_ERROR << 16);
1906 break;
1907 }
1908
1909 list_del(&fsp->list);
1910 sc_cmd->SCp.ptr = NULL;
1911 sc_cmd->scsi_done(sc_cmd);
Robert Love3a3b42b2009-11-03 11:47:39 -08001912 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001913
1914 /* release ref from initial allocation in queue command */
1915 fc_fcp_pkt_release(fsp);
1916}
1917
1918/**
Robert Love34f42a02009-02-27 10:55:45 -08001919 * fc_eh_abort() - Abort a command
Robert Love3a3b42b2009-11-03 11:47:39 -08001920 * @sc_cmd: The SCSI command to abort
Robert Love42e9a922008-12-09 15:10:17 -08001921 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001922 * From SCSI host template.
1923 * Send an ABTS to the target device and wait for the response.
Robert Love42e9a922008-12-09 15:10:17 -08001924 */
1925int fc_eh_abort(struct scsi_cmnd *sc_cmd)
1926{
1927 struct fc_fcp_pkt *fsp;
Robert Love3a3b42b2009-11-03 11:47:39 -08001928 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001929 int rc = FAILED;
1930 unsigned long flags;
1931
Robert Love3a3b42b2009-11-03 11:47:39 -08001932 lport = shost_priv(sc_cmd->device->host);
1933 if (lport->state != LPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08001934 return rc;
Robert Love3a3b42b2009-11-03 11:47:39 -08001935 else if (!lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -08001936 return rc;
1937
Robert Love3a3b42b2009-11-03 11:47:39 -08001938 spin_lock_irqsave(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001939 fsp = CMD_SP(sc_cmd);
1940 if (!fsp) {
1941 /* command completed while scsi eh was setting up */
Robert Love3a3b42b2009-11-03 11:47:39 -08001942 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001943 return SUCCESS;
1944 }
1945 /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
1946 fc_fcp_pkt_hold(fsp);
Robert Love3a3b42b2009-11-03 11:47:39 -08001947 spin_unlock_irqrestore(lport->host->host_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001948
1949 if (fc_fcp_lock_pkt(fsp)) {
1950 /* completed while we were waiting for timer to be deleted */
1951 rc = SUCCESS;
1952 goto release_pkt;
1953 }
1954
Robert Lovec3401112009-10-21 16:27:06 -07001955 rc = fc_fcp_pkt_abort(fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001956 fc_fcp_unlock_pkt(fsp);
1957
1958release_pkt:
1959 fc_fcp_pkt_release(fsp);
1960 return rc;
1961}
1962EXPORT_SYMBOL(fc_eh_abort);
1963
1964/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001965 * fc_eh_device_reset() - Reset a single LUN
1966 * @sc_cmd: The SCSI command which identifies the device whose
1967 * LUN is to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001968 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001969 * Set from SCSI host template.
Robert Love42e9a922008-12-09 15:10:17 -08001970 */
1971int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1972{
Robert Love3a3b42b2009-11-03 11:47:39 -08001973 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001974 struct fc_fcp_pkt *fsp;
1975 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1976 int rc = FAILED;
Robert Love42e9a922008-12-09 15:10:17 -08001977 int rval;
1978
1979 rval = fc_remote_port_chkready(rport);
1980 if (rval)
1981 goto out;
1982
Robert Love3a3b42b2009-11-03 11:47:39 -08001983 lport = shost_priv(sc_cmd->device->host);
Robert Love42e9a922008-12-09 15:10:17 -08001984
Robert Love3a3b42b2009-11-03 11:47:39 -08001985 if (lport->state != LPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08001986 return rc;
1987
Robert Love3a3b42b2009-11-03 11:47:39 -08001988 FC_SCSI_DBG(lport, "Resetting rport (%6x)\n", rport->port_id);
Robert Love74147052009-06-10 15:31:10 -07001989
Robert Love3a3b42b2009-11-03 11:47:39 -08001990 fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
Robert Love42e9a922008-12-09 15:10:17 -08001991 if (fsp == NULL) {
Robert Love74147052009-06-10 15:31:10 -07001992 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
Robert Love42e9a922008-12-09 15:10:17 -08001993 sc_cmd->result = DID_NO_CONNECT << 16;
1994 goto out;
1995 }
1996
1997 /*
1998 * Build the libfc request pkt. Do not set the scsi cmnd, because
1999 * the sc passed in is not setup for execution like when sent
2000 * through the queuecommand callout.
2001 */
Robert Love3a3b42b2009-11-03 11:47:39 -08002002 fsp->lp = lport; /* save the softc ptr */
Robert Love42e9a922008-12-09 15:10:17 -08002003 fsp->rport = rport; /* set the remote port ptr */
2004
2005 /*
2006 * flush outstanding commands
2007 */
Robert Love3a3b42b2009-11-03 11:47:39 -08002008 rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
Robert Love42e9a922008-12-09 15:10:17 -08002009 fsp->state = FC_SRB_FREE;
2010 fc_fcp_pkt_release(fsp);
2011
2012out:
2013 return rc;
2014}
2015EXPORT_SYMBOL(fc_eh_device_reset);
2016
2017/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002018 * fc_eh_host_reset() - Reset a Scsi_Host.
2019 * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
Robert Love42e9a922008-12-09 15:10:17 -08002020 */
2021int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
2022{
2023 struct Scsi_Host *shost = sc_cmd->device->host;
Robert Love3a3b42b2009-11-03 11:47:39 -08002024 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -08002025 unsigned long wait_tmo;
2026
Robert Love3a3b42b2009-11-03 11:47:39 -08002027 FC_SCSI_DBG(lport, "Resetting host\n");
Robert Love74147052009-06-10 15:31:10 -07002028
Robert Love3a3b42b2009-11-03 11:47:39 -08002029 lport->tt.lport_reset(lport);
Robert Love42e9a922008-12-09 15:10:17 -08002030 wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
Robert Love3a3b42b2009-11-03 11:47:39 -08002031 while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
2032 wait_tmo))
Robert Love42e9a922008-12-09 15:10:17 -08002033 msleep(1000);
2034
Robert Love3a3b42b2009-11-03 11:47:39 -08002035 if (fc_fcp_lport_queue_ready(lport)) {
Robert Love74147052009-06-10 15:31:10 -07002036 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
Robert Love3a3b42b2009-11-03 11:47:39 -08002037 "on port (%6x)\n", fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -08002038 return SUCCESS;
2039 } else {
Robert Love74147052009-06-10 15:31:10 -07002040 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
2041 "port (%6x) is not ready.\n",
Robert Love3a3b42b2009-11-03 11:47:39 -08002042 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -08002043 return FAILED;
2044 }
2045}
2046EXPORT_SYMBOL(fc_eh_host_reset);
2047
2048/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002049 * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
2050 * @sdev: The SCSI device that identifies the SCSI host
Robert Love42e9a922008-12-09 15:10:17 -08002051 *
2052 * Configures queue depth based on host's cmd_per_len. If not set
2053 * then we use the libfc default.
2054 */
2055int fc_slave_alloc(struct scsi_device *sdev)
2056{
2057 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Robert Love42e9a922008-12-09 15:10:17 -08002058
2059 if (!rport || fc_remote_port_chkready(rport))
2060 return -ENXIO;
2061
Vasu Dev14caf442009-10-15 17:46:55 -07002062 if (sdev->tagged_supported)
2063 scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
2064 else
2065 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2066 FC_FCP_DFLT_QUEUE_DEPTH);
2067
Robert Love42e9a922008-12-09 15:10:17 -08002068 return 0;
2069}
2070EXPORT_SYMBOL(fc_slave_alloc);
2071
Robert Love3a3b42b2009-11-03 11:47:39 -08002072/**
2073 * fc_change_queue_depth() - Change a device's queue depth
2074 * @sdev: The SCSI device whose queue depth is to change
2075 * @qdepth: The new queue depth
2076 * @reason: The resason for the change
2077 */
Mike Christiee881a172009-10-15 17:46:39 -07002078int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
Robert Love42e9a922008-12-09 15:10:17 -08002079{
Mike Christie5c208482009-10-15 17:46:50 -07002080 switch (reason) {
2081 case SCSI_QDEPTH_DEFAULT:
2082 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2083 break;
2084 case SCSI_QDEPTH_QFULL:
2085 scsi_track_queue_full(sdev, qdepth);
2086 break;
Vasu Dev229b8d72009-10-15 17:47:06 -07002087 case SCSI_QDEPTH_RAMP_UP:
2088 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2089 break;
Mike Christie5c208482009-10-15 17:46:50 -07002090 default:
Mike Christiee881a172009-10-15 17:46:39 -07002091 return -EOPNOTSUPP;
Mike Christie5c208482009-10-15 17:46:50 -07002092 }
Robert Love42e9a922008-12-09 15:10:17 -08002093 return sdev->queue_depth;
2094}
2095EXPORT_SYMBOL(fc_change_queue_depth);
2096
Robert Love3a3b42b2009-11-03 11:47:39 -08002097/**
2098 * fc_change_queue_type() - Change a device's queue type
2099 * @sdev: The SCSI device whose queue depth is to change
2100 * @tag_type: Identifier for queue type
2101 */
Robert Love42e9a922008-12-09 15:10:17 -08002102int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
2103{
2104 if (sdev->tagged_supported) {
2105 scsi_set_tag_type(sdev, tag_type);
2106 if (tag_type)
2107 scsi_activate_tcq(sdev, sdev->queue_depth);
2108 else
2109 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2110 } else
2111 tag_type = 0;
2112
2113 return tag_type;
2114}
2115EXPORT_SYMBOL(fc_change_queue_type);
2116
Robert Love3a3b42b2009-11-03 11:47:39 -08002117/**
2118 * fc_fcp_destory() - Tear down the FCP layer for a given local port
2119 * @lport: The local port that no longer needs the FCP layer
2120 */
2121void fc_fcp_destroy(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08002122{
Robert Love3a3b42b2009-11-03 11:47:39 -08002123 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -08002124
2125 if (!list_empty(&si->scsi_pkt_queue))
Robert Love74147052009-06-10 15:31:10 -07002126 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
Robert Love3a3b42b2009-11-03 11:47:39 -08002127 "port (%6x)\n", fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -08002128
2129 mempool_destroy(si->scsi_pkt_pool);
2130 kfree(si);
Robert Love3a3b42b2009-11-03 11:47:39 -08002131 lport->scsi_priv = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08002132}
2133EXPORT_SYMBOL(fc_fcp_destroy);
2134
Robert Love93e6d5a2009-11-03 11:46:03 -08002135int fc_setup_fcp()
2136{
2137 int rc = 0;
2138
2139 scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2140 sizeof(struct fc_fcp_pkt),
2141 0, SLAB_HWCACHE_ALIGN, NULL);
2142 if (!scsi_pkt_cachep) {
2143 printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2144 "module load failed!");
2145 rc = -ENOMEM;
2146 }
2147
2148 return rc;
2149}
2150
2151void fc_destroy_fcp()
2152{
2153 if (scsi_pkt_cachep)
2154 kmem_cache_destroy(scsi_pkt_cachep);
2155}
2156
Robert Love3a3b42b2009-11-03 11:47:39 -08002157/**
2158 * fc_fcp_init() - Initialize the FCP layer for a local port
2159 * @lport: The local port to initialize the exchange layer for
2160 */
2161int fc_fcp_init(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08002162{
2163 int rc;
2164 struct fc_fcp_internal *si;
2165
Robert Love3a3b42b2009-11-03 11:47:39 -08002166 if (!lport->tt.fcp_cmd_send)
2167 lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
Robert Love42e9a922008-12-09 15:10:17 -08002168
Robert Love3a3b42b2009-11-03 11:47:39 -08002169 if (!lport->tt.fcp_cleanup)
2170 lport->tt.fcp_cleanup = fc_fcp_cleanup;
Robert Love42e9a922008-12-09 15:10:17 -08002171
Robert Love3a3b42b2009-11-03 11:47:39 -08002172 if (!lport->tt.fcp_abort_io)
2173 lport->tt.fcp_abort_io = fc_fcp_abort_io;
Robert Love42e9a922008-12-09 15:10:17 -08002174
2175 si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
2176 if (!si)
2177 return -ENOMEM;
Robert Love3a3b42b2009-11-03 11:47:39 -08002178 lport->scsi_priv = si;
Robert Love42e9a922008-12-09 15:10:17 -08002179 INIT_LIST_HEAD(&si->scsi_pkt_queue);
2180
2181 si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
2182 if (!si->scsi_pkt_pool) {
2183 rc = -ENOMEM;
2184 goto free_internal;
2185 }
2186 return 0;
2187
2188free_internal:
2189 kfree(si);
2190 return rc;
2191}
2192EXPORT_SYMBOL(fc_fcp_init);