blob: 4fae253d4f3ded0a9453c01383f20a44450122ae [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Robert Love42e9a922008-12-09 15:10:17 -080031
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_device.h>
36#include <scsi/scsi_cmnd.h>
37
38#include <scsi/fc/fc_fc2.h>
39
40#include <scsi/libfc.h>
41#include <scsi/fc_encode.h>
42
Robert Love8866a5d2009-11-03 11:45:58 -080043#include "fc_libfc.h"
Robert Love42e9a922008-12-09 15:10:17 -080044
Randy Dunlap55204902011-01-28 16:03:57 -080045static struct kmem_cache *scsi_pkt_cachep;
Robert Love42e9a922008-12-09 15:10:17 -080046
47/* SRB state definitions */
48#define FC_SRB_FREE 0 /* cmd is free */
49#define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
50#define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
51#define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
Daniel Mack3ad2f3f2010-02-03 08:01:28 +080052#define FC_SRB_ABORTED (1 << 3) /* abort acknowledged */
Robert Love42e9a922008-12-09 15:10:17 -080053#define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
54#define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
55#define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
Robert Love42e9a922008-12-09 15:10:17 -080056
57#define FC_SRB_READ (1 << 1)
58#define FC_SRB_WRITE (1 << 0)
59
60/*
Vasu Dev8b7ac2b2010-10-08 17:12:31 -070061 * The SCp.ptr should be tested and set under the scsi_pkt_queue lock
Robert Love42e9a922008-12-09 15:10:17 -080062 */
63#define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
64#define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
65#define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
66#define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
67#define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
68
Robert Love3a3b42b2009-11-03 11:47:39 -080069/**
70 * struct fc_fcp_internal - FCP layer internal data
Chris Leechc1ecb902009-12-10 09:59:26 -080071 * @scsi_pkt_pool: Memory pool to draw FCP packets from
72 * @scsi_queue_lock: Protects the scsi_pkt_queue
Robert Love3a3b42b2009-11-03 11:47:39 -080073 * @scsi_pkt_queue: Current FCP packets
Vasu Dev84c3e1a2009-11-03 11:48:06 -080074 * @last_can_queue_ramp_down_time: ramp down time
75 * @last_can_queue_ramp_up_time: ramp up time
76 * @max_can_queue: max can_queue size
Robert Love3a3b42b2009-11-03 11:47:39 -080077 */
Robert Love42e9a922008-12-09 15:10:17 -080078struct fc_fcp_internal {
Chris Leechc1ecb902009-12-10 09:59:26 -080079 mempool_t *scsi_pkt_pool;
80 spinlock_t scsi_queue_lock;
81 struct list_head scsi_pkt_queue;
82 unsigned long last_can_queue_ramp_down_time;
83 unsigned long last_can_queue_ramp_up_time;
84 int max_can_queue;
Robert Love42e9a922008-12-09 15:10:17 -080085};
86
87#define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
88
89/*
90 * function prototypes
91 * FC scsi I/O related functions
92 */
93static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
94static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
95static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
96static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
97static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
Robert Love3a3b42b2009-11-03 11:47:39 -080098static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
john fastabendac17ea82010-11-30 16:18:39 -080099static void fc_fcp_recovery(struct fc_fcp_pkt *, u8 code);
Kees Cook1f0849d2017-10-11 12:51:53 -0700100static void fc_fcp_timeout(struct timer_list *);
Robert Love42e9a922008-12-09 15:10:17 -0800101static void fc_fcp_rec(struct fc_fcp_pkt *);
102static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
103static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
104static void fc_io_compl(struct fc_fcp_pkt *);
105
106static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
107static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
108static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
109
110/*
111 * command status codes
112 */
113#define FC_COMPLETE 0
114#define FC_CMD_ABORTED 1
115#define FC_CMD_RESET 2
116#define FC_CMD_PLOGO 3
117#define FC_SNS_RCV 4
118#define FC_TRANS_ERR 5
119#define FC_DATA_OVRRUN 6
120#define FC_DATA_UNDRUN 7
121#define FC_ERROR 8
122#define FC_HRD_ERROR 9
john fastabendac17ea82010-11-30 16:18:39 -0800123#define FC_CRC_ERROR 10
124#define FC_TIMED_OUT 11
Hannes Reineckea0452bb2016-10-13 15:10:38 +0200125#define FC_TRANS_RESET 12
Robert Love42e9a922008-12-09 15:10:17 -0800126
127/*
128 * Error recovery timeout values.
129 */
Robert Love42e9a922008-12-09 15:10:17 -0800130#define FC_SCSI_TM_TOV (10 * HZ)
Robert Love42e9a922008-12-09 15:10:17 -0800131#define FC_HOST_RESET_TIMEOUT (30 * HZ)
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800132#define FC_CAN_QUEUE_PERIOD (60 * HZ)
Robert Love42e9a922008-12-09 15:10:17 -0800133
134#define FC_MAX_ERROR_CNT 5
135#define FC_MAX_RECOV_RETRY 3
136
137#define FC_FCP_DFLT_QUEUE_DEPTH 32
138
139/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800140 * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
141 * @lport: The local port that the FCP packet is for
142 * @gfp: GFP flags for allocation
Robert Love42e9a922008-12-09 15:10:17 -0800143 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800144 * Return value: fcp_pkt structure or null on allocation failure.
145 * Context: Can be called from process context, no lock is required.
Robert Love42e9a922008-12-09 15:10:17 -0800146 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800147static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
Robert Love42e9a922008-12-09 15:10:17 -0800148{
Robert Love3a3b42b2009-11-03 11:47:39 -0800149 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800150 struct fc_fcp_pkt *fsp;
151
152 fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
153 if (fsp) {
154 memset(fsp, 0, sizeof(*fsp));
Robert Love3a3b42b2009-11-03 11:47:39 -0800155 fsp->lp = lport;
Yi Zouf56635a2011-01-28 16:03:15 -0800156 fsp->xfer_ddp = FC_XID_UNKNOWN;
Elena Reshetova22c70d12017-03-09 09:44:02 +0200157 refcount_set(&fsp->ref_cnt, 1);
Kees Cook1f0849d2017-10-11 12:51:53 -0700158 timer_setup(&fsp->timer, NULL, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800159 INIT_LIST_HEAD(&fsp->list);
160 spin_lock_init(&fsp->scsi_pkt_lock);
Vasu Dev0f02a662012-05-25 10:26:48 -0700161 } else {
162 per_cpu_ptr(lport->stats, get_cpu())->FcpPktAllocFails++;
163 put_cpu();
Robert Love42e9a922008-12-09 15:10:17 -0800164 }
165 return fsp;
166}
167
168/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800169 * fc_fcp_pkt_release() - Release hold on a fcp_pkt
170 * @fsp: The FCP packet to be released
Robert Love42e9a922008-12-09 15:10:17 -0800171 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800172 * Context: Can be called from process or interrupt context,
173 * no lock is required.
Robert Love42e9a922008-12-09 15:10:17 -0800174 */
175static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
176{
Elena Reshetova22c70d12017-03-09 09:44:02 +0200177 if (refcount_dec_and_test(&fsp->ref_cnt)) {
Robert Love42e9a922008-12-09 15:10:17 -0800178 struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
179
180 mempool_free(fsp, si->scsi_pkt_pool);
181 }
182}
183
Robert Love3a3b42b2009-11-03 11:47:39 -0800184/**
185 * fc_fcp_pkt_hold() - Hold a fcp_pkt
186 * @fsp: The FCP packet to be held
187 */
Robert Love42e9a922008-12-09 15:10:17 -0800188static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
189{
Elena Reshetova22c70d12017-03-09 09:44:02 +0200190 refcount_inc(&fsp->ref_cnt);
Robert Love42e9a922008-12-09 15:10:17 -0800191}
192
193/**
Bart Van Asschece83a4c2015-06-05 14:20:40 -0700194 * fc_fcp_pkt_destroy() - Release hold on a fcp_pkt
Robert Love3a3b42b2009-11-03 11:47:39 -0800195 * @seq: The sequence that the FCP packet is on (required by destructor API)
196 * @fsp: The FCP packet to be released
Robert Love42e9a922008-12-09 15:10:17 -0800197 *
Hannes Reinecke3afd2d12016-10-18 10:01:38 +0200198 * This routine is called by a destructor callback in the fc_exch_seq_send()
Robert Love3a3b42b2009-11-03 11:47:39 -0800199 * routine of the libfc Transport Template. The 'struct fc_seq' is a required
200 * argument even though it is not used by this routine.
201 *
202 * Context: No locking required.
Robert Love42e9a922008-12-09 15:10:17 -0800203 */
204static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
205{
206 fc_fcp_pkt_release(fsp);
207}
208
209/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800210 * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
211 * @fsp: The FCP packet to be locked and incremented
Robert Love42e9a922008-12-09 15:10:17 -0800212 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800213 * We should only return error if we return a command to SCSI-ml before
Robert Love42e9a922008-12-09 15:10:17 -0800214 * getting a response. This can happen in cases where we send a abort, but
215 * do not wait for the response and the abort and command can be passing
216 * each other on the wire/network-layer.
217 *
218 * Note: this function locks the packet and gets a reference to allow
219 * callers to call the completion function while the lock is held and
220 * not have to worry about the packets refcount.
221 *
222 * TODO: Maybe we should just have callers grab/release the lock and
223 * have a function that they call to verify the fsp and grab a ref if
224 * needed.
225 */
226static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
227{
228 spin_lock_bh(&fsp->scsi_pkt_lock);
229 if (fsp->state & FC_SRB_COMPL) {
230 spin_unlock_bh(&fsp->scsi_pkt_lock);
231 return -EPERM;
232 }
233
234 fc_fcp_pkt_hold(fsp);
235 return 0;
236}
237
Robert Love3a3b42b2009-11-03 11:47:39 -0800238/**
239 * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
240 * reference count
241 * @fsp: The FCP packet to be unlocked and decremented
242 */
Robert Love42e9a922008-12-09 15:10:17 -0800243static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
244{
245 spin_unlock_bh(&fsp->scsi_pkt_lock);
246 fc_fcp_pkt_release(fsp);
247}
248
Robert Love3a3b42b2009-11-03 11:47:39 -0800249/**
250 * fc_fcp_timer_set() - Start a timer for a fcp_pkt
251 * @fsp: The FCP packet to start a timer for
Vasu Devf2817ec2011-04-01 16:06:40 -0700252 * @delay: The timeout period in jiffies
Robert Love3a3b42b2009-11-03 11:47:39 -0800253 */
Robert Love42e9a922008-12-09 15:10:17 -0800254static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
255{
Hannes Reineckead3120c2016-10-13 15:10:53 +0200256 if (!(fsp->state & FC_SRB_COMPL)) {
Robert Love42e9a922008-12-09 15:10:17 -0800257 mod_timer(&fsp->timer, jiffies + delay);
Hannes Reineckead3120c2016-10-13 15:10:53 +0200258 fsp->timer_delay = delay;
259 }
Robert Love42e9a922008-12-09 15:10:17 -0800260}
261
Hannes Reinecked11b44e2016-10-13 15:10:51 +0200262static void fc_fcp_abort_done(struct fc_fcp_pkt *fsp)
263{
264 fsp->state |= FC_SRB_ABORTED;
265 fsp->state &= ~FC_SRB_ABORT_PENDING;
266
267 if (fsp->wait_for_comp)
268 complete(&fsp->tm_done);
269 else
270 fc_fcp_complete_locked(fsp);
271}
272
Robert Love3a3b42b2009-11-03 11:47:39 -0800273/**
274 * fc_fcp_send_abort() - Send an abort for exchanges associated with a
275 * fcp_pkt
276 * @fsp: The FCP packet to abort exchanges on
277 */
Robert Love42e9a922008-12-09 15:10:17 -0800278static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
279{
Hannes Reinecked11b44e2016-10-13 15:10:51 +0200280 int rc;
281
Robert Love42e9a922008-12-09 15:10:17 -0800282 if (!fsp->seq_ptr)
283 return -EINVAL;
284
Vasu Dev0f02a662012-05-25 10:26:48 -0700285 per_cpu_ptr(fsp->lp->stats, get_cpu())->FcpPktAborts++;
286 put_cpu();
287
Robert Love42e9a922008-12-09 15:10:17 -0800288 fsp->state |= FC_SRB_ABORT_PENDING;
Hannes Reinecke0ebaed12016-10-18 10:01:49 +0200289 rc = fc_seq_exch_abort(fsp->seq_ptr, 0);
Hannes Reinecked11b44e2016-10-13 15:10:51 +0200290 /*
Hannes Reinecke0ebaed12016-10-18 10:01:49 +0200291 * fc_seq_exch_abort() might return -ENXIO if
Hannes Reinecked11b44e2016-10-13 15:10:51 +0200292 * the sequence is already completed
293 */
294 if (rc == -ENXIO) {
295 fc_fcp_abort_done(fsp);
296 rc = 0;
297 }
298 return rc;
Robert Love42e9a922008-12-09 15:10:17 -0800299}
300
Robert Love3a3b42b2009-11-03 11:47:39 -0800301/**
302 * fc_fcp_retry_cmd() - Retry a fcp_pkt
303 * @fsp: The FCP packet to be retried
304 *
305 * Sets the status code to be FC_ERROR and then calls
306 * fc_fcp_complete_locked() which in turn calls fc_io_compl().
307 * fc_io_compl() will notify the SCSI-ml that the I/O is done.
308 * The SCSI-ml will retry the command.
Robert Love42e9a922008-12-09 15:10:17 -0800309 */
Hannes Reineckea0452bb2016-10-13 15:10:38 +0200310static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp, int status_code)
Robert Love42e9a922008-12-09 15:10:17 -0800311{
312 if (fsp->seq_ptr) {
Hannes Reinecke768c72c2016-10-18 10:01:50 +0200313 fc_exch_done(fsp->seq_ptr);
Robert Love42e9a922008-12-09 15:10:17 -0800314 fsp->seq_ptr = NULL;
315 }
316
317 fsp->state &= ~FC_SRB_ABORT_PENDING;
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -0500318 fsp->io_status = 0;
Hannes Reineckea0452bb2016-10-13 15:10:38 +0200319 fsp->status_code = status_code;
Robert Love42e9a922008-12-09 15:10:17 -0800320 fc_fcp_complete_locked(fsp);
321}
322
Robert Love3a3b42b2009-11-03 11:47:39 -0800323/**
324 * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
325 * @fsp: The FCP packet that will manage the DDP frames
326 * @xid: The XID that will be used for the DDP exchange
Yi Zoub277d2a2009-02-27 14:07:21 -0800327 */
328void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
329{
Robert Love3a3b42b2009-11-03 11:47:39 -0800330 struct fc_lport *lport;
Yi Zoub277d2a2009-02-27 14:07:21 -0800331
Robert Love3a3b42b2009-11-03 11:47:39 -0800332 lport = fsp->lp;
Yi Zoub277d2a2009-02-27 14:07:21 -0800333 if ((fsp->req_flags & FC_SRB_READ) &&
Robert Love3a3b42b2009-11-03 11:47:39 -0800334 (lport->lro_enabled) && (lport->tt.ddp_setup)) {
335 if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
336 scsi_sg_count(fsp->cmd)))
Yi Zoub277d2a2009-02-27 14:07:21 -0800337 fsp->xfer_ddp = xid;
338 }
339}
Yi Zoub277d2a2009-02-27 14:07:21 -0800340
Robert Love3a3b42b2009-11-03 11:47:39 -0800341/**
342 * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
343 * DDP related resources for a fcp_pkt
344 * @fsp: The FCP packet that DDP had been used on
Yi Zoub277d2a2009-02-27 14:07:21 -0800345 */
Yi Zou6a716a82011-05-16 16:45:40 -0700346void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
Yi Zoub277d2a2009-02-27 14:07:21 -0800347{
Robert Love3a3b42b2009-11-03 11:47:39 -0800348 struct fc_lport *lport;
Yi Zoub277d2a2009-02-27 14:07:21 -0800349
350 if (!fsp)
351 return;
352
Yi Zou5e472d02009-10-21 16:26:50 -0700353 if (fsp->xfer_ddp == FC_XID_UNKNOWN)
354 return;
355
Robert Love3a3b42b2009-11-03 11:47:39 -0800356 lport = fsp->lp;
357 if (lport->tt.ddp_done) {
358 fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
Yi Zou5e472d02009-10-21 16:26:50 -0700359 fsp->xfer_ddp = FC_XID_UNKNOWN;
Yi Zoub277d2a2009-02-27 14:07:21 -0800360 }
361}
362
Robert Love3a3b42b2009-11-03 11:47:39 -0800363/**
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800364 * fc_fcp_can_queue_ramp_up() - increases can_queue
365 * @lport: lport to ramp up can_queue
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800366 */
367static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport)
368{
369 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love9c8cce82011-04-01 16:06:14 -0700370 unsigned long flags;
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800371 int can_queue;
372
Robert Love9c8cce82011-04-01 16:06:14 -0700373 spin_lock_irqsave(lport->host->host_lock, flags);
374
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800375 if (si->last_can_queue_ramp_up_time &&
376 (time_before(jiffies, si->last_can_queue_ramp_up_time +
377 FC_CAN_QUEUE_PERIOD)))
Robert Love9c8cce82011-04-01 16:06:14 -0700378 goto unlock;
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800379
380 if (time_before(jiffies, si->last_can_queue_ramp_down_time +
381 FC_CAN_QUEUE_PERIOD))
Robert Love9c8cce82011-04-01 16:06:14 -0700382 goto unlock;
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800383
384 si->last_can_queue_ramp_up_time = jiffies;
385
386 can_queue = lport->host->can_queue << 1;
387 if (can_queue >= si->max_can_queue) {
388 can_queue = si->max_can_queue;
389 si->last_can_queue_ramp_down_time = 0;
390 }
391 lport->host->can_queue = can_queue;
392 shost_printk(KERN_ERR, lport->host, "libfc: increased "
393 "can_queue to %d.\n", can_queue);
Robert Love9c8cce82011-04-01 16:06:14 -0700394
395unlock:
396 spin_unlock_irqrestore(lport->host->host_lock, flags);
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800397}
398
399/**
Vasu Devc46be112009-11-03 11:48:00 -0800400 * fc_fcp_can_queue_ramp_down() - reduces can_queue
401 * @lport: lport to reduce can_queue
402 *
403 * If we are getting memory allocation failures, then we may
404 * be trying to execute too many commands. We let the running
405 * commands complete or timeout, then try again with a reduced
406 * can_queue. Eventually we will hit the point where we run
407 * on all reserved structs.
408 */
Hannes Reineckec1061252017-04-27 16:25:03 +0200409static bool fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
Vasu Devc46be112009-11-03 11:48:00 -0800410{
411 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love9c8cce82011-04-01 16:06:14 -0700412 unsigned long flags;
Vasu Devc46be112009-11-03 11:48:00 -0800413 int can_queue;
Hannes Reineckec1061252017-04-27 16:25:03 +0200414 bool changed = false;
Vasu Devc46be112009-11-03 11:48:00 -0800415
Robert Love9c8cce82011-04-01 16:06:14 -0700416 spin_lock_irqsave(lport->host->host_lock, flags);
417
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800418 if (si->last_can_queue_ramp_down_time &&
419 (time_before(jiffies, si->last_can_queue_ramp_down_time +
420 FC_CAN_QUEUE_PERIOD)))
Robert Love9c8cce82011-04-01 16:06:14 -0700421 goto unlock;
Vasu Dev84c3e1a2009-11-03 11:48:06 -0800422
423 si->last_can_queue_ramp_down_time = jiffies;
Vasu Devc46be112009-11-03 11:48:00 -0800424
425 can_queue = lport->host->can_queue;
426 can_queue >>= 1;
427 if (!can_queue)
428 can_queue = 1;
429 lport->host->can_queue = can_queue;
Hannes Reineckec1061252017-04-27 16:25:03 +0200430 changed = true;
Robert Love9c8cce82011-04-01 16:06:14 -0700431
432unlock:
433 spin_unlock_irqrestore(lport->host->host_lock, flags);
Hannes Reineckec1061252017-04-27 16:25:03 +0200434 return changed;
Vasu Devc46be112009-11-03 11:48:00 -0800435}
436
437/*
438 * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer.
439 * @lport: fc lport struct
440 * @len: payload length
441 *
442 * Allocates fc_frame structure and buffer but if fails to allocate
443 * then reduce can_queue.
444 */
445static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
446 size_t len)
447{
448 struct fc_frame *fp;
449
450 fp = fc_frame_alloc(lport, len);
Chris Leechc1ecb902009-12-10 09:59:26 -0800451 if (likely(fp))
452 return fp;
453
Vasu Dev0f02a662012-05-25 10:26:48 -0700454 per_cpu_ptr(lport->stats, get_cpu())->FcpFrameAllocFails++;
455 put_cpu();
Chris Leechc1ecb902009-12-10 09:59:26 -0800456 /* error case */
Chris Leechc1ecb902009-12-10 09:59:26 -0800457 fc_fcp_can_queue_ramp_down(lport);
Hannes Reinecke5d339d12016-10-13 15:10:48 +0200458 shost_printk(KERN_ERR, lport->host,
459 "libfc: Could not allocate frame, "
460 "reducing can_queue to %d.\n", lport->host->can_queue);
Chris Leechc1ecb902009-12-10 09:59:26 -0800461 return NULL;
Vasu Devc46be112009-11-03 11:48:00 -0800462}
463
464/**
Hannes Reinecke87da3b82016-10-13 15:10:54 +0200465 * get_fsp_rec_tov() - Helper function to get REC_TOV
466 * @fsp: the FCP packet
467 *
468 * Returns rec tov in jiffies as rpriv->e_d_tov + 1 second
469 */
470static inline unsigned int get_fsp_rec_tov(struct fc_fcp_pkt *fsp)
471{
472 struct fc_rport_libfc_priv *rpriv = fsp->rport->dd_data;
473 unsigned int e_d_tov = FC_DEF_E_D_TOV;
474
475 if (rpriv && rpriv->e_d_tov > e_d_tov)
476 e_d_tov = rpriv->e_d_tov;
477 return msecs_to_jiffies(e_d_tov) + HZ;
478}
479
480/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800481 * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
482 * @fsp: The FCP packet the data is on
483 * @fp: The data frame
Robert Love42e9a922008-12-09 15:10:17 -0800484 */
485static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
486{
487 struct scsi_cmnd *sc = fsp->cmd;
Robert Love3a3b42b2009-11-03 11:47:39 -0800488 struct fc_lport *lport = fsp->lp;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700489 struct fc_stats *stats;
Robert Love42e9a922008-12-09 15:10:17 -0800490 struct fc_frame_header *fh;
491 size_t start_offset;
492 size_t offset;
493 u32 crc;
494 u32 copy_len = 0;
495 size_t len;
496 void *buf;
497 struct scatterlist *sg;
Robert Love58682872009-11-03 11:47:28 -0800498 u32 nents;
john fastabendac17ea82010-11-30 16:18:39 -0800499 u8 host_bcode = FC_COMPLETE;
Robert Love42e9a922008-12-09 15:10:17 -0800500
501 fh = fc_frame_header_get(fp);
502 offset = ntohl(fh->fh_parm_offset);
503 start_offset = offset;
504 len = fr_len(fp) - sizeof(*fh);
505 buf = fc_frame_payload_get(fp, 0);
506
Vasu Dev5c12c412010-03-12 16:08:44 -0800507 /*
john fastabendac17ea82010-11-30 16:18:39 -0800508 * if this I/O is ddped then clear it and initiate recovery since data
509 * frames are expected to be placed directly in that case.
510 *
511 * Indicate error to scsi-ml because something went wrong with the
512 * ddp handling to get us here.
Vasu Dev5c12c412010-03-12 16:08:44 -0800513 */
514 if (fsp->xfer_ddp != FC_XID_UNKNOWN) {
515 fc_fcp_ddp_done(fsp);
john fastabendac17ea82010-11-30 16:18:39 -0800516 FC_FCP_DBG(fsp, "DDP I/O in fc_fcp_recv_data set ERROR\n");
517 host_bcode = FC_ERROR;
Vasu Dev5c12c412010-03-12 16:08:44 -0800518 goto err;
519 }
Robert Love42e9a922008-12-09 15:10:17 -0800520 if (offset + len > fsp->data_len) {
Robert Love34f42a02009-02-27 10:55:45 -0800521 /* this should never happen */
Robert Love42e9a922008-12-09 15:10:17 -0800522 if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
523 fc_frame_crc_check(fp))
524 goto crc_err;
Robert Love74147052009-06-10 15:31:10 -0700525 FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
526 "data_len %x\n", len, offset, fsp->data_len);
john fastabendac17ea82010-11-30 16:18:39 -0800527
528 /* Data is corrupted indicate scsi-ml should retry */
529 host_bcode = FC_DATA_OVRRUN;
Vasu Dev5c12c412010-03-12 16:08:44 -0800530 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800531 }
532 if (offset != fsp->xfer_len)
533 fsp->state |= FC_SRB_DISCONTIG;
534
Robert Love42e9a922008-12-09 15:10:17 -0800535 sg = scsi_sglist(sc);
Robert Love58682872009-11-03 11:47:28 -0800536 nents = scsi_sg_count(sc);
Robert Love42e9a922008-12-09 15:10:17 -0800537
Robert Love58682872009-11-03 11:47:28 -0800538 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
539 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
Cong Wang77dfce02011-11-25 23:14:23 +0800540 &offset, NULL);
Robert Love58682872009-11-03 11:47:28 -0800541 } else {
542 crc = crc32(~0, (u8 *) fh, sizeof(*fh));
543 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
Cong Wang77dfce02011-11-25 23:14:23 +0800544 &offset, &crc);
Robert Love42e9a922008-12-09 15:10:17 -0800545 buf = fc_frame_payload_get(fp, 0);
Robert Love58682872009-11-03 11:47:28 -0800546 if (len % 4)
Robert Love42e9a922008-12-09 15:10:17 -0800547 crc = crc32(crc, buf + len, 4 - (len % 4));
Robert Love42e9a922008-12-09 15:10:17 -0800548
549 if (~crc != le32_to_cpu(fr_crc(fp))) {
550crc_err:
Vasu Dev1bd49b42012-05-25 10:26:43 -0700551 stats = per_cpu_ptr(lport->stats, get_cpu());
Robert Love42e9a922008-12-09 15:10:17 -0800552 stats->ErrorFrames++;
Joe Eykholtf018b732010-03-12 16:08:55 -0800553 /* per cpu count, not total count, but OK for limit */
Hillf Danton688fd362011-07-27 15:10:28 -0700554 if (stats->InvalidCRCCount++ < FC_MAX_ERROR_CNT)
Robert Love74147052009-06-10 15:31:10 -0700555 printk(KERN_WARNING "libfc: CRC error on data "
Chris Leechce8b5df2010-04-09 14:23:10 -0700556 "frame for port (%6.6x)\n",
Robert Love7b2787e2010-05-07 15:18:41 -0700557 lport->port_id);
Joe Eykholtf018b732010-03-12 16:08:55 -0800558 put_cpu();
Robert Love42e9a922008-12-09 15:10:17 -0800559 /*
560 * Assume the frame is total garbage.
561 * We may have copied it over the good part
562 * of the buffer.
563 * If so, we need to retry the entire operation.
564 * Otherwise, ignore it.
565 */
john fastabendac17ea82010-11-30 16:18:39 -0800566 if (fsp->state & FC_SRB_DISCONTIG) {
567 host_bcode = FC_CRC_ERROR;
Vasu Dev5c12c412010-03-12 16:08:44 -0800568 goto err;
john fastabendac17ea82010-11-30 16:18:39 -0800569 }
Robert Love42e9a922008-12-09 15:10:17 -0800570 return;
571 }
572 }
573
574 if (fsp->xfer_contig_end == start_offset)
575 fsp->xfer_contig_end += copy_len;
576 fsp->xfer_len += copy_len;
577
578 /*
579 * In the very rare event that this data arrived after the response
580 * and completes the transfer, call the completion handler.
581 */
582 if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
Hannes Reinecke87da3b82016-10-13 15:10:54 +0200583 fsp->xfer_len == fsp->data_len - fsp->scsi_resid) {
584 FC_FCP_DBG( fsp, "complete out-of-order sequence\n" );
Robert Love42e9a922008-12-09 15:10:17 -0800585 fc_fcp_complete_locked(fsp);
Hannes Reinecke87da3b82016-10-13 15:10:54 +0200586 }
Vasu Dev5c12c412010-03-12 16:08:44 -0800587 return;
588err:
john fastabendac17ea82010-11-30 16:18:39 -0800589 fc_fcp_recovery(fsp, host_bcode);
Robert Love42e9a922008-12-09 15:10:17 -0800590}
591
Robert Love34f42a02009-02-27 10:55:45 -0800592/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800593 * fc_fcp_send_data() - Send SCSI data to a target
594 * @fsp: The FCP packet the data is on
595 * @sp: The sequence the data is to be sent on
596 * @offset: The starting offset for this data request
597 * @seq_blen: The burst length for this data request
Robert Love42e9a922008-12-09 15:10:17 -0800598 *
599 * Called after receiving a Transfer Ready data descriptor.
Robert Love3a3b42b2009-11-03 11:47:39 -0800600 * If the LLD is capable of sequence offload then send down the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100601 * seq_blen amount of data in single frame, otherwise send
Robert Love3a3b42b2009-11-03 11:47:39 -0800602 * multiple frames of the maximum frame payload supported by
603 * the target port.
Robert Love42e9a922008-12-09 15:10:17 -0800604 */
605static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
606 size_t offset, size_t seq_blen)
607{
608 struct fc_exch *ep;
609 struct scsi_cmnd *sc;
610 struct scatterlist *sg;
611 struct fc_frame *fp = NULL;
Robert Love3a3b42b2009-11-03 11:47:39 -0800612 struct fc_lport *lport = fsp->lp;
Chris Leech18fa11e2009-11-03 11:50:05 -0800613 struct page *page;
Robert Love42e9a922008-12-09 15:10:17 -0800614 size_t remaining;
615 size_t t_blen;
616 size_t tlen;
617 size_t sg_bytes;
618 size_t frame_offset, fh_parm_offset;
Chris Leech18fa11e2009-11-03 11:50:05 -0800619 size_t off;
Robert Love42e9a922008-12-09 15:10:17 -0800620 int error;
621 void *data = NULL;
622 void *page_addr;
Robert Love3a3b42b2009-11-03 11:47:39 -0800623 int using_sg = lport->sg_supp;
Robert Love42e9a922008-12-09 15:10:17 -0800624 u32 f_ctl;
625
626 WARN_ON(seq_blen <= 0);
627 if (unlikely(offset + seq_blen > fsp->data_len)) {
628 /* this should never happen */
Robert Love74147052009-06-10 15:31:10 -0700629 FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
630 "offset %zx\n", seq_blen, offset);
Robert Love42e9a922008-12-09 15:10:17 -0800631 fc_fcp_send_abort(fsp);
632 return 0;
633 } else if (offset != fsp->xfer_len) {
634 /* Out of Order Data Request - no problem, but unexpected. */
Robert Love74147052009-06-10 15:31:10 -0700635 FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
636 "seq_blen %zx offset %zx\n", seq_blen, offset);
Robert Love42e9a922008-12-09 15:10:17 -0800637 }
638
639 /*
640 * if LLD is capable of seq_offload then set transport
641 * burst length (t_blen) to seq_blen, otherwise set t_blen
642 * to max FC frame payload previously set in fsp->max_payload.
643 */
Yi Zou276d6812009-02-27 14:07:10 -0800644 t_blen = fsp->max_payload;
Robert Love3a3b42b2009-11-03 11:47:39 -0800645 if (lport->seq_offload) {
646 t_blen = min(seq_blen, (size_t)lport->lso_max);
Robert Love74147052009-06-10 15:31:10 -0700647 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
Robert Love3a3b42b2009-11-03 11:47:39 -0800648 fsp, seq_blen, lport->lso_max, t_blen);
Yi Zou276d6812009-02-27 14:07:10 -0800649 }
650
Robert Love42e9a922008-12-09 15:10:17 -0800651 if (t_blen > 512)
652 t_blen &= ~(512 - 1); /* round down to block size */
Robert Love42e9a922008-12-09 15:10:17 -0800653 sc = fsp->cmd;
654
655 remaining = seq_blen;
656 fh_parm_offset = frame_offset = offset;
657 tlen = 0;
Hannes Reineckec6865b32016-10-18 10:01:51 +0200658 seq = fc_seq_start_next(seq);
Robert Love42e9a922008-12-09 15:10:17 -0800659 f_ctl = FC_FC_REL_OFF;
660 WARN_ON(!seq);
661
Robert Love42e9a922008-12-09 15:10:17 -0800662 sg = scsi_sglist(sc);
663
664 while (remaining > 0 && sg) {
665 if (offset >= sg->length) {
666 offset -= sg->length;
667 sg = sg_next(sg);
668 continue;
669 }
670 if (!fp) {
671 tlen = min(t_blen, remaining);
672
673 /*
674 * TODO. Temporary workaround. fc_seq_send() can't
675 * handle odd lengths in non-linear skbs.
676 * This will be the final fragment only.
677 */
678 if (tlen % 4)
679 using_sg = 0;
Vasu Deva7bbc7f2009-11-03 11:47:55 -0800680 fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
681 if (!fp)
682 return -ENOMEM;
Robert Love42e9a922008-12-09 15:10:17 -0800683
Vasu Deva7bbc7f2009-11-03 11:47:55 -0800684 data = fc_frame_header_get(fp) + 1;
Robert Love42e9a922008-12-09 15:10:17 -0800685 fh_parm_offset = frame_offset;
686 fr_max_payload(fp) = fsp->max_payload;
687 }
Chris Leech18fa11e2009-11-03 11:50:05 -0800688
689 off = offset + sg->offset;
Robert Love42e9a922008-12-09 15:10:17 -0800690 sg_bytes = min(tlen, sg->length - offset);
Chris Leech18fa11e2009-11-03 11:50:05 -0800691 sg_bytes = min(sg_bytes,
692 (size_t) (PAGE_SIZE - (off & ~PAGE_MASK)));
693 page = sg_page(sg) + (off >> PAGE_SHIFT);
Robert Love42e9a922008-12-09 15:10:17 -0800694 if (using_sg) {
Chris Leech18fa11e2009-11-03 11:50:05 -0800695 get_page(page);
Robert Love42e9a922008-12-09 15:10:17 -0800696 skb_fill_page_desc(fp_skb(fp),
697 skb_shinfo(fp_skb(fp))->nr_frags,
Chris Leech18fa11e2009-11-03 11:50:05 -0800698 page, off & ~PAGE_MASK, sg_bytes);
Robert Love42e9a922008-12-09 15:10:17 -0800699 fp_skb(fp)->data_len += sg_bytes;
700 fr_len(fp) += sg_bytes;
701 fp_skb(fp)->truesize += PAGE_SIZE;
702 } else {
Robert Love42e9a922008-12-09 15:10:17 -0800703 /*
704 * The scatterlist item may be bigger than PAGE_SIZE,
705 * but we must not cross pages inside the kmap.
706 */
Cong Wang77dfce02011-11-25 23:14:23 +0800707 page_addr = kmap_atomic(page);
Robert Love42e9a922008-12-09 15:10:17 -0800708 memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
709 sg_bytes);
Cong Wang77dfce02011-11-25 23:14:23 +0800710 kunmap_atomic(page_addr);
Robert Love42e9a922008-12-09 15:10:17 -0800711 data += sg_bytes;
712 }
713 offset += sg_bytes;
714 frame_offset += sg_bytes;
715 tlen -= sg_bytes;
716 remaining -= sg_bytes;
717
Yi Zoud37322a2009-10-21 16:27:58 -0700718 if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
719 (tlen))
Robert Love42e9a922008-12-09 15:10:17 -0800720 continue;
721
722 /*
723 * Send sequence with transfer sequence initiative in case
724 * this is last FCP frame of the sequence.
725 */
726 if (remaining == 0)
727 f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
728
729 ep = fc_seq_exch(seq);
730 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
731 FC_TYPE_FCP, f_ctl, fh_parm_offset);
732
733 /*
734 * send fragment using for a sequence.
735 */
Hannes Reinecke0cac9372016-10-18 10:01:48 +0200736 error = fc_seq_send(lport, seq, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800737 if (error) {
738 WARN_ON(1); /* send error should be rare */
Yi Zou8467b962011-05-16 16:45:57 -0700739 return error;
Robert Love42e9a922008-12-09 15:10:17 -0800740 }
741 fp = NULL;
742 }
743 fsp->xfer_len += seq_blen; /* premature count? */
744 return 0;
745}
746
Robert Love3a3b42b2009-11-03 11:47:39 -0800747/**
Hillf Danton547fb122011-07-27 15:10:44 -0700748 * fc_fcp_abts_resp() - Receive an ABTS response
Robert Love3a3b42b2009-11-03 11:47:39 -0800749 * @fsp: The FCP packet that is being aborted
750 * @fp: The response frame
751 */
Robert Love42e9a922008-12-09 15:10:17 -0800752static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
753{
754 int ba_done = 1;
755 struct fc_ba_rjt *brp;
756 struct fc_frame_header *fh;
757
758 fh = fc_frame_header_get(fp);
759 switch (fh->fh_r_ctl) {
760 case FC_RCTL_BA_ACC:
761 break;
762 case FC_RCTL_BA_RJT:
763 brp = fc_frame_payload_get(fp, sizeof(*brp));
764 if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
765 break;
766 /* fall thru */
767 default:
768 /*
769 * we will let the command timeout
770 * and scsi-ml recover in this case,
771 * therefore cleared the ba_done flag.
772 */
773 ba_done = 0;
774 }
775
Hannes Reinecked11b44e2016-10-13 15:10:51 +0200776 if (ba_done)
777 fc_fcp_abort_done(fsp);
Robert Love42e9a922008-12-09 15:10:17 -0800778}
779
Robert Love34f42a02009-02-27 10:55:45 -0800780/**
Hillf Danton547fb122011-07-27 15:10:44 -0700781 * fc_fcp_recv() - Receive an FCP frame
Robert Love34f42a02009-02-27 10:55:45 -0800782 * @seq: The sequence the frame is on
Robert Love3a3b42b2009-11-03 11:47:39 -0800783 * @fp: The received frame
Robert Love34f42a02009-02-27 10:55:45 -0800784 * @arg: The related FCP packet
Robert Love42e9a922008-12-09 15:10:17 -0800785 *
Robert Love3a3b42b2009-11-03 11:47:39 -0800786 * Context: Called from Soft IRQ context. Can not be called
787 * holding the FCP packet list lock.
Robert Love42e9a922008-12-09 15:10:17 -0800788 */
789static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
790{
791 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
Robert Lovea29e7642009-04-21 16:27:41 -0700792 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800793 struct fc_frame_header *fh;
794 struct fcp_txrdy *dd;
795 u8 r_ctl;
796 int rc = 0;
797
Vasu Devc46be112009-11-03 11:48:00 -0800798 if (IS_ERR(fp)) {
799 fc_fcp_error(fsp, fp);
800 return;
801 }
Robert Love42e9a922008-12-09 15:10:17 -0800802
803 fh = fc_frame_header_get(fp);
804 r_ctl = fh->fh_r_ctl;
Robert Love42e9a922008-12-09 15:10:17 -0800805
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200806 if (lport->state != LPORT_ST_READY) {
807 FC_FCP_DBG(fsp, "lport state %d, ignoring r_ctl %x\n",
808 lport->state, r_ctl);
Robert Love42e9a922008-12-09 15:10:17 -0800809 goto out;
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200810 }
Robert Love42e9a922008-12-09 15:10:17 -0800811 if (fc_fcp_lock_pkt(fsp))
812 goto out;
Robert Love42e9a922008-12-09 15:10:17 -0800813
814 if (fh->fh_type == FC_TYPE_BLS) {
815 fc_fcp_abts_resp(fsp, fp);
816 goto unlock;
817 }
818
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200819 if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING)) {
820 FC_FCP_DBG(fsp, "command aborted, ignoring r_ctl %x\n", r_ctl);
Robert Love42e9a922008-12-09 15:10:17 -0800821 goto unlock;
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200822 }
Robert Love42e9a922008-12-09 15:10:17 -0800823
824 if (r_ctl == FC_RCTL_DD_DATA_DESC) {
825 /*
826 * received XFER RDY from the target
827 * need to send data to the target
828 */
829 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
830 dd = fc_frame_payload_get(fp, sizeof(*dd));
831 WARN_ON(!dd);
832
833 rc = fc_fcp_send_data(fsp, seq,
834 (size_t) ntohl(dd->ft_data_ro),
835 (size_t) ntohl(dd->ft_burst_len));
836 if (!rc)
837 seq->rec_data = fsp->xfer_len;
Robert Love42e9a922008-12-09 15:10:17 -0800838 } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
839 /*
840 * received a DATA frame
841 * next we will copy the data to the system buffer
842 */
843 WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
844 fc_fcp_recv_data(fsp, fp);
845 seq->rec_data = fsp->xfer_contig_end;
846 } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
847 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
848
849 fc_fcp_resp(fsp, fp);
850 } else {
Robert Love74147052009-06-10 15:31:10 -0700851 FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
Robert Love42e9a922008-12-09 15:10:17 -0800852 }
853unlock:
854 fc_fcp_unlock_pkt(fsp);
855out:
856 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -0800857}
858
Robert Love3a3b42b2009-11-03 11:47:39 -0800859/**
860 * fc_fcp_resp() - Handler for FCP responses
861 * @fsp: The FCP packet the response is for
862 * @fp: The response frame
863 */
Robert Love42e9a922008-12-09 15:10:17 -0800864static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
865{
866 struct fc_frame_header *fh;
867 struct fcp_resp *fc_rp;
868 struct fcp_resp_ext *rp_ex;
869 struct fcp_resp_rsp_info *fc_rp_info;
870 u32 plen;
871 u32 expected_len;
872 u32 respl = 0;
873 u32 snsl = 0;
874 u8 flags = 0;
875
876 plen = fr_len(fp);
877 fh = (struct fc_frame_header *)fr_hdr(fp);
878 if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
879 goto len_err;
880 plen -= sizeof(*fh);
881 fc_rp = (struct fcp_resp *)(fh + 1);
882 fsp->cdb_status = fc_rp->fr_status;
883 flags = fc_rp->fr_flags;
884 fsp->scsi_comp_flags = flags;
885 expected_len = fsp->data_len;
886
Yi Zoub277d2a2009-02-27 14:07:21 -0800887 /* if ddp, update xfer len */
888 fc_fcp_ddp_done(fsp);
889
Robert Love42e9a922008-12-09 15:10:17 -0800890 if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
891 rp_ex = (void *)(fc_rp + 1);
892 if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
893 if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
894 goto len_err;
895 fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
896 if (flags & FCP_RSP_LEN_VAL) {
897 respl = ntohl(rp_ex->fr_rsp_len);
Yi Zou3b64b182012-09-24 11:52:50 -0700898 if ((respl != FCP_RESP_RSP_INFO_LEN4) &&
899 (respl != FCP_RESP_RSP_INFO_LEN8))
Robert Love42e9a922008-12-09 15:10:17 -0800900 goto len_err;
901 if (fsp->wait_for_comp) {
902 /* Abuse cdb_status for rsp code */
903 fsp->cdb_status = fc_rp_info->rsp_code;
904 complete(&fsp->tm_done);
905 /*
906 * tmfs will not have any scsi cmd so
907 * exit here
908 */
909 return;
Yi Zou63ac4bb2010-04-09 14:23:05 -0700910 }
Robert Love42e9a922008-12-09 15:10:17 -0800911 }
912 if (flags & FCP_SNS_LEN_VAL) {
913 snsl = ntohl(rp_ex->fr_sns_len);
914 if (snsl > SCSI_SENSE_BUFFERSIZE)
915 snsl = SCSI_SENSE_BUFFERSIZE;
916 memcpy(fsp->cmd->sense_buffer,
917 (char *)fc_rp_info + respl, snsl);
918 }
919 }
920 if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
921 if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
922 goto len_err;
923 if (flags & FCP_RESID_UNDER) {
924 fsp->scsi_resid = ntohl(rp_ex->fr_resid);
925 /*
926 * The cmnd->underflow is the minimum number of
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300927 * bytes that must be transferred for this
Robert Love42e9a922008-12-09 15:10:17 -0800928 * command. Provided a sense condition is not
929 * present, make sure the actual amount
930 * transferred is at least the underflow value
931 * or fail.
932 */
933 if (!(flags & FCP_SNS_LEN_VAL) &&
934 (fc_rp->fr_status == 0) &&
935 (scsi_bufflen(fsp->cmd) -
936 fsp->scsi_resid) < fsp->cmd->underflow)
937 goto err;
938 expected_len -= fsp->scsi_resid;
939 } else {
940 fsp->status_code = FC_ERROR;
941 }
942 }
943 }
944 fsp->state |= FC_SRB_RCV_STATUS;
945
946 /*
947 * Check for missing or extra data frames.
948 */
Bart Van Assche9de99012013-08-14 15:40:49 +0000949 if (unlikely(fsp->cdb_status == SAM_STAT_GOOD &&
950 fsp->xfer_len != expected_len)) {
Robert Love42e9a922008-12-09 15:10:17 -0800951 if (fsp->xfer_len < expected_len) {
952 /*
953 * Some data may be queued locally,
954 * Wait a at least one jiffy to see if it is delivered.
955 * If this expires without data, we may do SRR.
956 */
Hannes Reineckead3120c2016-10-13 15:10:53 +0200957 if (fsp->lp->qfull) {
958 FC_FCP_DBG(fsp, "tgt %6.6x queue busy retry\n",
959 fsp->rport->port_id);
960 return;
961 }
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200962 FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx data underrun "
963 "len %x, data len %x\n",
964 fsp->rport->port_id,
965 fsp->xfer_len, expected_len, fsp->data_len);
Hannes Reinecke87da3b82016-10-13 15:10:54 +0200966 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
Robert Love42e9a922008-12-09 15:10:17 -0800967 return;
968 }
969 fsp->status_code = FC_DATA_OVRRUN;
Chris Leechce8b5df2010-04-09 14:23:10 -0700970 FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx greater than expected, "
Robert Love74147052009-06-10 15:31:10 -0700971 "len %x, data len %x\n",
972 fsp->rport->port_id,
973 fsp->xfer_len, expected_len, fsp->data_len);
Robert Love42e9a922008-12-09 15:10:17 -0800974 }
975 fc_fcp_complete_locked(fsp);
976 return;
977
978len_err:
Robert Love74147052009-06-10 15:31:10 -0700979 FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
980 "snsl %u\n", flags, fr_len(fp), respl, snsl);
Robert Love42e9a922008-12-09 15:10:17 -0800981err:
982 fsp->status_code = FC_ERROR;
983 fc_fcp_complete_locked(fsp);
984}
985
986/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800987 * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
988 * fcp_pkt lock held
989 * @fsp: The FCP packet to be completed
Robert Love42e9a922008-12-09 15:10:17 -0800990 *
991 * This function may sleep if a timer is pending. The packet lock must be
992 * held, and the host lock must not be held.
993 */
994static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
995{
Robert Love3a3b42b2009-11-03 11:47:39 -0800996 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -0800997 struct fc_seq *seq;
998 struct fc_exch *ep;
999 u32 f_ctl;
1000
1001 if (fsp->state & FC_SRB_ABORT_PENDING)
1002 return;
1003
1004 if (fsp->state & FC_SRB_ABORTED) {
1005 if (!fsp->status_code)
1006 fsp->status_code = FC_CMD_ABORTED;
1007 } else {
1008 /*
1009 * Test for transport underrun, independent of response
1010 * underrun status.
1011 */
Bart Van Assche9de99012013-08-14 15:40:49 +00001012 if (fsp->cdb_status == SAM_STAT_GOOD &&
1013 fsp->xfer_len < fsp->data_len && !fsp->io_status &&
Robert Love42e9a922008-12-09 15:10:17 -08001014 (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001015 fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
1016 FC_FCP_DBG(fsp, "data underrun, xfer %zx data %x\n",
1017 fsp->xfer_len, fsp->data_len);
Robert Love42e9a922008-12-09 15:10:17 -08001018 fsp->status_code = FC_DATA_UNDRUN;
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001019 }
Robert Love42e9a922008-12-09 15:10:17 -08001020 }
1021
1022 seq = fsp->seq_ptr;
1023 if (seq) {
1024 fsp->seq_ptr = NULL;
1025 if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
1026 struct fc_frame *conf_frame;
1027 struct fc_seq *csp;
1028
Hannes Reineckec6865b32016-10-18 10:01:51 +02001029 csp = fc_seq_start_next(seq);
Vasu Devc46be112009-11-03 11:48:00 -08001030 conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001031 if (conf_frame) {
1032 f_ctl = FC_FC_SEQ_INIT;
1033 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1034 ep = fc_seq_exch(seq);
1035 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
1036 ep->did, ep->sid,
1037 FC_TYPE_FCP, f_ctl, 0);
Hannes Reinecke0cac9372016-10-18 10:01:48 +02001038 fc_seq_send(lport, csp, conf_frame);
Robert Love42e9a922008-12-09 15:10:17 -08001039 }
1040 }
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001041 fc_exch_done(seq);
Robert Love42e9a922008-12-09 15:10:17 -08001042 }
Yi Zoud889b302010-11-30 16:19:20 -08001043 /*
1044 * Some resets driven by SCSI are not I/Os and do not have
1045 * SCSI commands associated with the requests. We should not
1046 * call I/O completion if we do not have a SCSI command.
1047 */
1048 if (fsp->cmd)
1049 fc_io_compl(fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001050}
1051
Robert Love3a3b42b2009-11-03 11:47:39 -08001052/**
1053 * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
1054 * @fsp: The FCP packet whose exchanges should be canceled
1055 * @error: The reason for the cancellation
1056 */
Robert Love42e9a922008-12-09 15:10:17 -08001057static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
1058{
Robert Love42e9a922008-12-09 15:10:17 -08001059 if (fsp->seq_ptr) {
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001060 fc_exch_done(fsp->seq_ptr);
Robert Love42e9a922008-12-09 15:10:17 -08001061 fsp->seq_ptr = NULL;
1062 }
1063 fsp->status_code = error;
1064}
1065
1066/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001067 * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
1068 * @lport: The local port whose exchanges should be canceled
1069 * @id: The target's ID
1070 * @lun: The LUN
1071 * @error: The reason for cancellation
Robert Love42e9a922008-12-09 15:10:17 -08001072 *
1073 * If lun or id is -1, they are ignored.
1074 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001075static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
Robert Love42e9a922008-12-09 15:10:17 -08001076 unsigned int lun, int error)
1077{
Robert Love3a3b42b2009-11-03 11:47:39 -08001078 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001079 struct fc_fcp_pkt *fsp;
1080 struct scsi_cmnd *sc_cmd;
1081 unsigned long flags;
1082
Chris Leechc1ecb902009-12-10 09:59:26 -08001083 spin_lock_irqsave(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001084restart:
1085 list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
1086 sc_cmd = fsp->cmd;
1087 if (id != -1 && scmd_id(sc_cmd) != id)
1088 continue;
1089
1090 if (lun != -1 && sc_cmd->device->lun != lun)
1091 continue;
1092
1093 fc_fcp_pkt_hold(fsp);
Chris Leechc1ecb902009-12-10 09:59:26 -08001094 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001095
Bart Van Assche8f2777f2015-06-05 14:20:51 -07001096 spin_lock_bh(&fsp->scsi_pkt_lock);
1097 if (!(fsp->state & FC_SRB_COMPL)) {
1098 fsp->state |= FC_SRB_COMPL;
1099 /*
1100 * TODO: dropping scsi_pkt_lock and then reacquiring
1101 * again around fc_fcp_cleanup_cmd() is required,
1102 * since fc_fcp_cleanup_cmd() calls into
1103 * fc_seq_set_resp() and that func preempts cpu using
1104 * schedule. May be schedule and related code should be
1105 * removed instead of unlocking here to avoid scheduling
1106 * while atomic bug.
1107 */
1108 spin_unlock_bh(&fsp->scsi_pkt_lock);
1109
Robert Love42e9a922008-12-09 15:10:17 -08001110 fc_fcp_cleanup_cmd(fsp, error);
Bart Van Assche8f2777f2015-06-05 14:20:51 -07001111
1112 spin_lock_bh(&fsp->scsi_pkt_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001113 fc_io_compl(fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001114 }
Bart Van Assche8f2777f2015-06-05 14:20:51 -07001115 spin_unlock_bh(&fsp->scsi_pkt_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001116
1117 fc_fcp_pkt_release(fsp);
Chris Leechc1ecb902009-12-10 09:59:26 -08001118 spin_lock_irqsave(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001119 /*
1120 * while we dropped the lock multiple pkts could
1121 * have been released, so we have to start over.
1122 */
1123 goto restart;
1124 }
Chris Leechc1ecb902009-12-10 09:59:26 -08001125 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08001126}
1127
1128/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001129 * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
1130 * @lport: The local port whose exchanges are to be aborted
Robert Love42e9a922008-12-09 15:10:17 -08001131 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001132static void fc_fcp_abort_io(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001133{
Robert Love3a3b42b2009-11-03 11:47:39 -08001134 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
1135}
1136
1137/**
1138 * fc_fcp_pkt_send() - Send a fcp_pkt
1139 * @lport: The local port to send the FCP packet on
1140 * @fsp: The FCP packet to send
1141 *
1142 * Return: Zero for success and -1 for failure
Chris Leechc1ecb902009-12-10 09:59:26 -08001143 * Locks: Called without locks held
Robert Love3a3b42b2009-11-03 11:47:39 -08001144 */
1145static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
1146{
1147 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Chris Leechc1ecb902009-12-10 09:59:26 -08001148 unsigned long flags;
Robert Love42e9a922008-12-09 15:10:17 -08001149 int rc;
1150
1151 fsp->cmd->SCp.ptr = (char *)fsp;
1152 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1153 fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
1154
Andy Grovere35fa8c2012-01-19 13:39:11 -08001155 int_to_scsilun(fsp->cmd->device->lun, &fsp->cdb_cmd.fc_lun);
Robert Love42e9a922008-12-09 15:10:17 -08001156 memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
Robert Love42e9a922008-12-09 15:10:17 -08001157
Chris Leechc1ecb902009-12-10 09:59:26 -08001158 spin_lock_irqsave(&si->scsi_queue_lock, flags);
1159 list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1160 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
Robert Love3a3b42b2009-11-03 11:47:39 -08001161 rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
Chris Leechc1ecb902009-12-10 09:59:26 -08001162 if (unlikely(rc)) {
1163 spin_lock_irqsave(&si->scsi_queue_lock, flags);
Neerav Parikhc9d24a72011-07-27 15:10:54 -07001164 fsp->cmd->SCp.ptr = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001165 list_del(&fsp->list);
Chris Leechc1ecb902009-12-10 09:59:26 -08001166 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1167 }
Robert Love42e9a922008-12-09 15:10:17 -08001168
1169 return rc;
1170}
1171
Robert Love3a3b42b2009-11-03 11:47:39 -08001172/**
1173 * fc_fcp_cmd_send() - Send a FCP command
1174 * @lport: The local port to send the command on
1175 * @fsp: The FCP packet the command is on
1176 * @resp: The handler for the response
1177 */
1178static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
Robert Love42e9a922008-12-09 15:10:17 -08001179 void (*resp)(struct fc_seq *,
1180 struct fc_frame *fp,
1181 void *arg))
1182{
1183 struct fc_frame *fp;
1184 struct fc_seq *seq;
1185 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001186 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001187 const size_t len = sizeof(fsp->cdb_cmd);
1188 int rc = 0;
1189
1190 if (fc_fcp_lock_pkt(fsp))
1191 return 0;
1192
Vasu Devc46be112009-11-03 11:48:00 -08001193 fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
Robert Love42e9a922008-12-09 15:10:17 -08001194 if (!fp) {
1195 rc = -1;
1196 goto unlock;
1197 }
1198
1199 memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
Yi Zoub277d2a2009-02-27 14:07:21 -08001200 fr_fsp(fp) = fsp;
Robert Love42e9a922008-12-09 15:10:17 -08001201 rport = fsp->rport;
1202 fsp->max_payload = rport->maxframe_size;
Robert Love3a3b42b2009-11-03 11:47:39 -08001203 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001204
1205 fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
Robert Love7b2787e2010-05-07 15:18:41 -07001206 rpriv->local_port->port_id, FC_TYPE_FCP,
Joe Eykholt24f089e2010-07-20 15:21:01 -07001207 FC_FCTL_REQ, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001208
Hannes Reinecke3afd2d12016-10-18 10:01:38 +02001209 seq = fc_exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001210 if (!seq) {
Robert Love42e9a922008-12-09 15:10:17 -08001211 rc = -1;
1212 goto unlock;
1213 }
Robert Love42e9a922008-12-09 15:10:17 -08001214 fsp->seq_ptr = seq;
1215 fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
1216
Kees Cook841b86f2017-10-23 09:40:42 +02001217 fsp->timer.function = fc_fcp_timeout;
john fastabend05fee642010-11-30 16:18:49 -08001218 if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
Vasu Devf2817ec2011-04-01 16:06:40 -07001219 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
john fastabend05fee642010-11-30 16:18:49 -08001220
Robert Love42e9a922008-12-09 15:10:17 -08001221unlock:
1222 fc_fcp_unlock_pkt(fsp);
1223 return rc;
1224}
1225
Robert Love3a3b42b2009-11-03 11:47:39 -08001226/**
1227 * fc_fcp_error() - Handler for FCP layer errors
1228 * @fsp: The FCP packet the error is on
1229 * @fp: The frame that has errored
Robert Love42e9a922008-12-09 15:10:17 -08001230 */
1231static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1232{
1233 int error = PTR_ERR(fp);
1234
1235 if (fc_fcp_lock_pkt(fsp))
1236 return;
1237
Robert Love74147052009-06-10 15:31:10 -07001238 if (error == -FC_EX_CLOSED) {
Hannes Reineckea0452bb2016-10-13 15:10:38 +02001239 fc_fcp_retry_cmd(fsp, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001240 goto unlock;
Robert Love42e9a922008-12-09 15:10:17 -08001241 }
Robert Love74147052009-06-10 15:31:10 -07001242
Robert Love42e9a922008-12-09 15:10:17 -08001243 /*
1244 * clear abort pending, because the lower layer
1245 * decided to force completion.
1246 */
1247 fsp->state &= ~FC_SRB_ABORT_PENDING;
1248 fsp->status_code = FC_CMD_PLOGO;
1249 fc_fcp_complete_locked(fsp);
1250unlock:
1251 fc_fcp_unlock_pkt(fsp);
1252}
1253
Robert Love3a3b42b2009-11-03 11:47:39 -08001254/**
1255 * fc_fcp_pkt_abort() - Abort a fcp_pkt
1256 * @fsp: The FCP packet to abort on
1257 *
1258 * Called to send an abort and then wait for abort completion
Robert Love42e9a922008-12-09 15:10:17 -08001259 */
Robert Lovec3401112009-10-21 16:27:06 -07001260static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
Robert Love42e9a922008-12-09 15:10:17 -08001261{
1262 int rc = FAILED;
Hillf Dantonfa79dbd2011-01-28 16:03:21 -08001263 unsigned long ticks_left;
Robert Love42e9a922008-12-09 15:10:17 -08001264
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001265 FC_FCP_DBG(fsp, "pkt abort state %x\n", fsp->state);
1266 if (fc_fcp_send_abort(fsp)) {
1267 FC_FCP_DBG(fsp, "failed to send abort\n");
Robert Love42e9a922008-12-09 15:10:17 -08001268 return FAILED;
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001269 }
Robert Love42e9a922008-12-09 15:10:17 -08001270
Hannes Reinecked11b44e2016-10-13 15:10:51 +02001271 if (fsp->state & FC_SRB_ABORTED) {
1272 FC_FCP_DBG(fsp, "target abort cmd completed\n");
1273 return SUCCESS;
1274 }
1275
Robert Love42e9a922008-12-09 15:10:17 -08001276 init_completion(&fsp->tm_done);
1277 fsp->wait_for_comp = 1;
1278
1279 spin_unlock_bh(&fsp->scsi_pkt_lock);
Hillf Dantonfa79dbd2011-01-28 16:03:21 -08001280 ticks_left = wait_for_completion_timeout(&fsp->tm_done,
1281 FC_SCSI_TM_TOV);
Robert Love42e9a922008-12-09 15:10:17 -08001282 spin_lock_bh(&fsp->scsi_pkt_lock);
1283 fsp->wait_for_comp = 0;
1284
Hillf Dantonfa79dbd2011-01-28 16:03:21 -08001285 if (!ticks_left) {
Robert Love74147052009-06-10 15:31:10 -07001286 FC_FCP_DBG(fsp, "target abort cmd failed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001287 } else if (fsp->state & FC_SRB_ABORTED) {
Robert Love74147052009-06-10 15:31:10 -07001288 FC_FCP_DBG(fsp, "target abort cmd passed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001289 rc = SUCCESS;
1290 fc_fcp_complete_locked(fsp);
1291 }
1292
1293 return rc;
1294}
1295
Robert Love3a3b42b2009-11-03 11:47:39 -08001296/**
1297 * fc_lun_reset_send() - Send LUN reset command
1298 * @data: The FCP packet that identifies the LUN to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001299 */
Kees Cook1f0849d2017-10-11 12:51:53 -07001300static void fc_lun_reset_send(struct timer_list *t)
Robert Love42e9a922008-12-09 15:10:17 -08001301{
Kees Cook1f0849d2017-10-11 12:51:53 -07001302 struct fc_fcp_pkt *fsp = from_timer(fsp, t, timer);
Robert Love3a3b42b2009-11-03 11:47:39 -08001303 struct fc_lport *lport = fsp->lp;
john fastabende0883a32010-11-30 16:18:44 -08001304
Robert Love3a3b42b2009-11-03 11:47:39 -08001305 if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
Robert Love42e9a922008-12-09 15:10:17 -08001306 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
1307 return;
1308 if (fc_fcp_lock_pkt(fsp))
1309 return;
Kees Cook841b86f2017-10-23 09:40:42 +02001310 fsp->timer.function = fc_lun_reset_send;
Vasu Devf2817ec2011-04-01 16:06:40 -07001311 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
Robert Love42e9a922008-12-09 15:10:17 -08001312 fc_fcp_unlock_pkt(fsp);
1313 }
1314}
1315
Robert Love3a3b42b2009-11-03 11:47:39 -08001316/**
1317 * fc_lun_reset() - Send a LUN RESET command to a device
1318 * and wait for the reply
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001319 * @lport: The local port to sent the command on
Robert Love3a3b42b2009-11-03 11:47:39 -08001320 * @fsp: The FCP packet that identifies the LUN to be reset
1321 * @id: The SCSI command ID
1322 * @lun: The LUN ID to be reset
Robert Love42e9a922008-12-09 15:10:17 -08001323 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001324static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
Robert Love42e9a922008-12-09 15:10:17 -08001325 unsigned int id, unsigned int lun)
1326{
1327 int rc;
1328
1329 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
1330 fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
Andy Grovere35fa8c2012-01-19 13:39:11 -08001331 int_to_scsilun(lun, &fsp->cdb_cmd.fc_lun);
Robert Love42e9a922008-12-09 15:10:17 -08001332
1333 fsp->wait_for_comp = 1;
1334 init_completion(&fsp->tm_done);
1335
Kees Cook1f0849d2017-10-11 12:51:53 -07001336 fc_lun_reset_send(&fsp->timer);
Robert Love42e9a922008-12-09 15:10:17 -08001337
1338 /*
1339 * wait for completion of reset
1340 * after that make sure all commands are terminated
1341 */
1342 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
1343
1344 spin_lock_bh(&fsp->scsi_pkt_lock);
1345 fsp->state |= FC_SRB_COMPL;
1346 spin_unlock_bh(&fsp->scsi_pkt_lock);
1347
1348 del_timer_sync(&fsp->timer);
1349
1350 spin_lock_bh(&fsp->scsi_pkt_lock);
1351 if (fsp->seq_ptr) {
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001352 fc_exch_done(fsp->seq_ptr);
Robert Love42e9a922008-12-09 15:10:17 -08001353 fsp->seq_ptr = NULL;
1354 }
1355 fsp->wait_for_comp = 0;
1356 spin_unlock_bh(&fsp->scsi_pkt_lock);
1357
1358 if (!rc) {
Robert Love3a3b42b2009-11-03 11:47:39 -08001359 FC_SCSI_DBG(lport, "lun reset failed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001360 return FAILED;
1361 }
1362
1363 /* cdb_status holds the tmf's rsp code */
1364 if (fsp->cdb_status != FCP_TMF_CMPL)
1365 return FAILED;
1366
Robert Love3a3b42b2009-11-03 11:47:39 -08001367 FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
1368 fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
Robert Love42e9a922008-12-09 15:10:17 -08001369 return SUCCESS;
1370}
1371
Robert Love3a3b42b2009-11-03 11:47:39 -08001372/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001373 * fc_tm_done() - Task Management response handler
Robert Love3a3b42b2009-11-03 11:47:39 -08001374 * @seq: The sequence that the response is on
1375 * @fp: The response frame
1376 * @arg: The FCP packet the response is for
Robert Love42e9a922008-12-09 15:10:17 -08001377 */
1378static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1379{
1380 struct fc_fcp_pkt *fsp = arg;
1381 struct fc_frame_header *fh;
1382
1383 if (IS_ERR(fp)) {
1384 /*
1385 * If there is an error just let it timeout or wait
1386 * for TMF to be aborted if it timedout.
1387 *
1388 * scsi-eh will escalate for when either happens.
1389 */
Dan Carpentere4a9a982011-01-28 16:03:31 -08001390 return;
Robert Love42e9a922008-12-09 15:10:17 -08001391 }
1392
1393 if (fc_fcp_lock_pkt(fsp))
Yi Zoub5fe5e92010-11-30 16:19:35 -08001394 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001395
1396 /*
1397 * raced with eh timeout handler.
1398 */
Yi Zoub5fe5e92010-11-30 16:19:35 -08001399 if (!fsp->seq_ptr || !fsp->wait_for_comp)
1400 goto out_unlock;
Robert Love42e9a922008-12-09 15:10:17 -08001401
1402 fh = fc_frame_header_get(fp);
1403 if (fh->fh_type != FC_TYPE_BLS)
1404 fc_fcp_resp(fsp, fp);
1405 fsp->seq_ptr = NULL;
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001406 fc_exch_done(seq);
Yi Zoub5fe5e92010-11-30 16:19:35 -08001407out_unlock:
Robert Love42e9a922008-12-09 15:10:17 -08001408 fc_fcp_unlock_pkt(fsp);
Yi Zoub5fe5e92010-11-30 16:19:35 -08001409out:
1410 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001411}
1412
Robert Love3a3b42b2009-11-03 11:47:39 -08001413/**
1414 * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
1415 * @lport: The local port to be cleaned up
1416 */
1417static void fc_fcp_cleanup(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001418{
Robert Love3a3b42b2009-11-03 11:47:39 -08001419 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001420}
1421
Robert Love3a3b42b2009-11-03 11:47:39 -08001422/**
1423 * fc_fcp_timeout() - Handler for fcp_pkt timeouts
1424 * @data: The FCP packet that has timed out
Robert Love42e9a922008-12-09 15:10:17 -08001425 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001426 * If REC is supported then just issue it and return. The REC exchange will
1427 * complete or time out and recovery can continue at that point. Otherwise,
1428 * if the response has been received without all the data it has been
1429 * ER_TIMEOUT since the response was received. If the response has not been
1430 * received we see if data was received recently. If it has been then we
1431 * continue waiting, otherwise, we abort the command.
Robert Love42e9a922008-12-09 15:10:17 -08001432 */
Kees Cook1f0849d2017-10-11 12:51:53 -07001433static void fc_fcp_timeout(struct timer_list *t)
Robert Love42e9a922008-12-09 15:10:17 -08001434{
Kees Cook1f0849d2017-10-11 12:51:53 -07001435 struct fc_fcp_pkt *fsp = from_timer(fsp, t, timer);
Robert Love42e9a922008-12-09 15:10:17 -08001436 struct fc_rport *rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001437 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001438
1439 if (fc_fcp_lock_pkt(fsp))
1440 return;
1441
1442 if (fsp->cdb_cmd.fc_tm_flags)
1443 goto unlock;
1444
Hannes Reineckead3120c2016-10-13 15:10:53 +02001445 if (fsp->lp->qfull) {
1446 FC_FCP_DBG(fsp, "fcp timeout, resetting timer delay %d\n",
1447 fsp->timer_delay);
Kees Cook841b86f2017-10-23 09:40:42 +02001448 fsp->timer.function = fc_fcp_timeout;
Hannes Reineckead3120c2016-10-13 15:10:53 +02001449 fc_fcp_timer_set(fsp, fsp->timer_delay);
1450 goto unlock;
1451 }
1452 FC_FCP_DBG(fsp, "fcp timeout, delay %d flags %x state %x\n",
1453 fsp->timer_delay, rpriv->flags, fsp->state);
Robert Love42e9a922008-12-09 15:10:17 -08001454 fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
1455
Vasu Dev5b97fab2012-10-09 01:43:24 +00001456 if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
Yi Zoua7523592012-07-06 10:40:31 -07001457 fc_fcp_rec(fsp);
Vasu Dev5b97fab2012-10-09 01:43:24 +00001458 else if (fsp->state & FC_SRB_RCV_STATUS)
1459 fc_fcp_complete_locked(fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001460 else
john fastabendac17ea82010-11-30 16:18:39 -08001461 fc_fcp_recovery(fsp, FC_TIMED_OUT);
Robert Love42e9a922008-12-09 15:10:17 -08001462 fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
1463unlock:
1464 fc_fcp_unlock_pkt(fsp);
1465}
1466
Robert Love3a3b42b2009-11-03 11:47:39 -08001467/**
1468 * fc_fcp_rec() - Send a REC ELS request
1469 * @fsp: The FCP packet to send the REC request on
Robert Love42e9a922008-12-09 15:10:17 -08001470 */
1471static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
1472{
Robert Love3a3b42b2009-11-03 11:47:39 -08001473 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001474 struct fc_frame *fp;
1475 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001476 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001477
Robert Love3a3b42b2009-11-03 11:47:39 -08001478 lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -08001479 rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001480 rpriv = rport->dd_data;
1481 if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
Robert Love42e9a922008-12-09 15:10:17 -08001482 fsp->status_code = FC_HRD_ERROR;
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001483 fsp->io_status = 0;
Robert Love42e9a922008-12-09 15:10:17 -08001484 fc_fcp_complete_locked(fsp);
1485 return;
1486 }
john fastabende0883a32010-11-30 16:18:44 -08001487
Vasu Devc46be112009-11-03 11:48:00 -08001488 fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
Robert Love42e9a922008-12-09 15:10:17 -08001489 if (!fp)
1490 goto retry;
1491
1492 fr_seq(fp) = fsp->seq_ptr;
1493 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
Robert Love7b2787e2010-05-07 15:18:41 -07001494 rpriv->local_port->port_id, FC_TYPE_ELS,
Joe Eykholt24f089e2010-07-20 15:21:01 -07001495 FC_FCTL_REQ, 0);
Robert Love3a3b42b2009-11-03 11:47:39 -08001496 if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
1497 fc_fcp_rec_resp, fsp,
Yi Zou3a910902010-11-30 16:19:30 -08001498 2 * lport->r_a_tov)) {
Robert Love42e9a922008-12-09 15:10:17 -08001499 fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
1500 return;
1501 }
Robert Love42e9a922008-12-09 15:10:17 -08001502retry:
1503 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
Yi Zou3a910902010-11-30 16:19:30 -08001504 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
Robert Love42e9a922008-12-09 15:10:17 -08001505 else
john fastabendac17ea82010-11-30 16:18:39 -08001506 fc_fcp_recovery(fsp, FC_TIMED_OUT);
Robert Love42e9a922008-12-09 15:10:17 -08001507}
1508
Robert Love3a3b42b2009-11-03 11:47:39 -08001509/**
1510 * fc_fcp_rec_resp() - Handler for REC ELS responses
1511 * @seq: The sequence the response is on
1512 * @fp: The response frame
1513 * @arg: The FCP packet the response is on
1514 *
1515 * If the response is a reject then the scsi layer will handle
1516 * the timeout. If the response is a LS_ACC then if the I/O was not completed
1517 * set the timeout and return. If the I/O was completed then complete the
1518 * exchange and tell the SCSI layer.
Robert Love42e9a922008-12-09 15:10:17 -08001519 */
1520static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1521{
1522 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
1523 struct fc_els_rec_acc *recp;
1524 struct fc_els_ls_rjt *rjt;
1525 u32 e_stat;
1526 u8 opcode;
1527 u32 offset;
1528 enum dma_data_direction data_dir;
1529 enum fc_rctl r_ctl;
Robert Love3a3b42b2009-11-03 11:47:39 -08001530 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001531
1532 if (IS_ERR(fp)) {
1533 fc_fcp_rec_error(fsp, fp);
1534 return;
1535 }
1536
1537 if (fc_fcp_lock_pkt(fsp))
1538 goto out;
1539
1540 fsp->recov_retry = 0;
1541 opcode = fc_frame_payload_op(fp);
1542 if (opcode == ELS_LS_RJT) {
1543 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1544 switch (rjt->er_reason) {
1545 default:
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001546 FC_FCP_DBG(fsp,
1547 "device %x invalid REC reject %d/%d\n",
Robert Love74147052009-06-10 15:31:10 -07001548 fsp->rport->port_id, rjt->er_reason,
1549 rjt->er_explan);
Robert Love42e9a922008-12-09 15:10:17 -08001550 /* fall through */
1551 case ELS_RJT_UNSUP:
Robert Love74147052009-06-10 15:31:10 -07001552 FC_FCP_DBG(fsp, "device does not support REC\n");
Robert Love3a3b42b2009-11-03 11:47:39 -08001553 rpriv = fsp->rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001554 /*
1555 * if we do not spport RECs or got some bogus
1556 * reason then resetup timer so we check for
1557 * making progress.
1558 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001559 rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
Robert Love42e9a922008-12-09 15:10:17 -08001560 break;
1561 case ELS_RJT_LOGIC:
1562 case ELS_RJT_UNAB:
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001563 FC_FCP_DBG(fsp, "device %x REC reject %d/%d\n",
1564 fsp->rport->port_id, rjt->er_reason,
1565 rjt->er_explan);
Robert Love42e9a922008-12-09 15:10:17 -08001566 /*
Hannes Reinecke9ca1e182016-10-13 15:10:50 +02001567 * If response got lost or is stuck in the
1568 * queue somewhere we have no idea if and when
1569 * the response will be received. So quarantine
1570 * the xid and retry the command.
Robert Love42e9a922008-12-09 15:10:17 -08001571 */
Hannes Reinecke9ca1e182016-10-13 15:10:50 +02001572 if (rjt->er_explan == ELS_EXPL_OXID_RXID) {
1573 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1574 ep->state |= FC_EX_QUARANTINE;
Hannes Reineckea0452bb2016-10-13 15:10:38 +02001575 fsp->state |= FC_SRB_ABORTED;
1576 fc_fcp_retry_cmd(fsp, FC_TRANS_RESET);
Robert Love42e9a922008-12-09 15:10:17 -08001577 break;
1578 }
Hannes Reineckea0452bb2016-10-13 15:10:38 +02001579 fc_fcp_recovery(fsp, FC_TRANS_RESET);
Robert Love42e9a922008-12-09 15:10:17 -08001580 break;
1581 }
1582 } else if (opcode == ELS_LS_ACC) {
1583 if (fsp->state & FC_SRB_ABORTED)
1584 goto unlock_out;
1585
1586 data_dir = fsp->cmd->sc_data_direction;
1587 recp = fc_frame_payload_get(fp, sizeof(*recp));
1588 offset = ntohl(recp->reca_fc4value);
1589 e_stat = ntohl(recp->reca_e_stat);
1590
1591 if (e_stat & ESB_ST_COMPLETE) {
1592
1593 /*
1594 * The exchange is complete.
1595 *
1596 * For output, we must've lost the response.
1597 * For input, all data must've been sent.
1598 * We lost may have lost the response
1599 * (and a confirmation was requested) and maybe
1600 * some data.
1601 *
1602 * If all data received, send SRR
1603 * asking for response. If partial data received,
1604 * or gaps, SRR requests data at start of gap.
1605 * Recovery via SRR relies on in-order-delivery.
1606 */
1607 if (data_dir == DMA_TO_DEVICE) {
1608 r_ctl = FC_RCTL_DD_CMD_STATUS;
1609 } else if (fsp->xfer_contig_end == offset) {
1610 r_ctl = FC_RCTL_DD_CMD_STATUS;
1611 } else {
1612 offset = fsp->xfer_contig_end;
1613 r_ctl = FC_RCTL_DD_SOL_DATA;
1614 }
1615 fc_fcp_srr(fsp, r_ctl, offset);
1616 } else if (e_stat & ESB_ST_SEQ_INIT) {
Robert Love42e9a922008-12-09 15:10:17 -08001617 /*
1618 * The remote port has the initiative, so just
1619 * keep waiting for it to complete.
1620 */
Vasu Devf2817ec2011-04-01 16:06:40 -07001621 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
Robert Love42e9a922008-12-09 15:10:17 -08001622 } else {
1623
1624 /*
1625 * The exchange is incomplete, we have seq. initiative.
1626 * Lost response with requested confirmation,
1627 * lost confirmation, lost transfer ready or
1628 * lost write data.
1629 *
1630 * For output, if not all data was received, ask
1631 * for transfer ready to be repeated.
1632 *
1633 * If we received or sent all the data, send SRR to
1634 * request response.
1635 *
1636 * If we lost a response, we may have lost some read
1637 * data as well.
1638 */
1639 r_ctl = FC_RCTL_DD_SOL_DATA;
1640 if (data_dir == DMA_TO_DEVICE) {
1641 r_ctl = FC_RCTL_DD_CMD_STATUS;
1642 if (offset < fsp->data_len)
1643 r_ctl = FC_RCTL_DD_DATA_DESC;
1644 } else if (offset == fsp->xfer_contig_end) {
1645 r_ctl = FC_RCTL_DD_CMD_STATUS;
1646 } else if (fsp->xfer_contig_end < offset) {
1647 offset = fsp->xfer_contig_end;
1648 }
1649 fc_fcp_srr(fsp, r_ctl, offset);
1650 }
1651 }
1652unlock_out:
1653 fc_fcp_unlock_pkt(fsp);
1654out:
1655 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1656 fc_frame_free(fp);
1657}
1658
Robert Love3a3b42b2009-11-03 11:47:39 -08001659/**
1660 * fc_fcp_rec_error() - Handler for REC errors
1661 * @fsp: The FCP packet the error is on
1662 * @fp: The REC frame
Robert Love42e9a922008-12-09 15:10:17 -08001663 */
1664static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1665{
1666 int error = PTR_ERR(fp);
1667
1668 if (fc_fcp_lock_pkt(fsp))
1669 goto out;
1670
1671 switch (error) {
1672 case -FC_EX_CLOSED:
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001673 FC_FCP_DBG(fsp, "REC %p fid %6.6x exchange closed\n",
1674 fsp, fsp->rport->port_id);
Hannes Reineckea0452bb2016-10-13 15:10:38 +02001675 fc_fcp_retry_cmd(fsp, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001676 break;
1677
1678 default:
Chris Leechce8b5df2010-04-09 14:23:10 -07001679 FC_FCP_DBG(fsp, "REC %p fid %6.6x error unexpected error %d\n",
Robert Love74147052009-06-10 15:31:10 -07001680 fsp, fsp->rport->port_id, error);
Robert Love42e9a922008-12-09 15:10:17 -08001681 fsp->status_code = FC_CMD_PLOGO;
1682 /* fall through */
1683
1684 case -FC_EX_TIMEOUT:
1685 /*
1686 * Assume REC or LS_ACC was lost.
1687 * The exchange manager will have aborted REC, so retry.
1688 */
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001689 FC_FCP_DBG(fsp, "REC %p fid %6.6x exchange timeout retry %d/%d\n",
1690 fsp, fsp->rport->port_id, fsp->recov_retry,
Robert Love74147052009-06-10 15:31:10 -07001691 FC_MAX_RECOV_RETRY);
Robert Love42e9a922008-12-09 15:10:17 -08001692 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1693 fc_fcp_rec(fsp);
1694 else
john fastabendac17ea82010-11-30 16:18:39 -08001695 fc_fcp_recovery(fsp, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001696 break;
1697 }
1698 fc_fcp_unlock_pkt(fsp);
1699out:
1700 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1701}
1702
Robert Love3a3b42b2009-11-03 11:47:39 -08001703/**
Vasu Dev5c12c412010-03-12 16:08:44 -08001704 * fc_fcp_recovery() - Handler for fcp_pkt recovery
1705 * @fsp: The FCP pkt that needs to be aborted
Robert Love42e9a922008-12-09 15:10:17 -08001706 */
john fastabendac17ea82010-11-30 16:18:39 -08001707static void fc_fcp_recovery(struct fc_fcp_pkt *fsp, u8 code)
Robert Love42e9a922008-12-09 15:10:17 -08001708{
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001709 FC_FCP_DBG(fsp, "start recovery code %x\n", code);
john fastabendac17ea82010-11-30 16:18:39 -08001710 fsp->status_code = code;
Robert Love42e9a922008-12-09 15:10:17 -08001711 fsp->cdb_status = 0;
1712 fsp->io_status = 0;
1713 /*
1714 * if this fails then we let the scsi command timer fire and
1715 * scsi-ml escalate.
1716 */
1717 fc_fcp_send_abort(fsp);
1718}
1719
Robert Love3a3b42b2009-11-03 11:47:39 -08001720/**
1721 * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
1722 * @fsp: The FCP packet the SRR is to be sent on
1723 * @r_ctl: The R_CTL field for the SRR request
Robert Love42e9a922008-12-09 15:10:17 -08001724 * This is called after receiving status but insufficient data, or
1725 * when expecting status but the request has timed out.
1726 */
1727static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1728{
Robert Love3a3b42b2009-11-03 11:47:39 -08001729 struct fc_lport *lport = fsp->lp;
Robert Love42e9a922008-12-09 15:10:17 -08001730 struct fc_rport *rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001731 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001732 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1733 struct fc_seq *seq;
1734 struct fcp_srr *srr;
1735 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001736
1737 rport = fsp->rport;
Robert Love3a3b42b2009-11-03 11:47:39 -08001738 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001739
Robert Love3a3b42b2009-11-03 11:47:39 -08001740 if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
1741 rpriv->rp_state != RPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08001742 goto retry; /* shouldn't happen */
Vasu Devc46be112009-11-03 11:48:00 -08001743 fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
Robert Love42e9a922008-12-09 15:10:17 -08001744 if (!fp)
1745 goto retry;
1746
1747 srr = fc_frame_payload_get(fp, sizeof(*srr));
1748 memset(srr, 0, sizeof(*srr));
1749 srr->srr_op = ELS_SRR;
1750 srr->srr_ox_id = htons(ep->oxid);
1751 srr->srr_rx_id = htons(ep->rxid);
1752 srr->srr_r_ctl = r_ctl;
1753 srr->srr_rel_off = htonl(offset);
1754
1755 fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
Robert Love7b2787e2010-05-07 15:18:41 -07001756 rpriv->local_port->port_id, FC_TYPE_FCP,
Joe Eykholt24f089e2010-07-20 15:21:01 -07001757 FC_FCTL_REQ, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001758
Hannes Reinecke3afd2d12016-10-18 10:01:38 +02001759 seq = fc_exch_seq_send(lport, fp, fc_fcp_srr_resp,
1760 fc_fcp_pkt_destroy,
1761 fsp, get_fsp_rec_tov(fsp));
Chris Leech8f550f92009-10-21 16:28:09 -07001762 if (!seq)
Robert Love42e9a922008-12-09 15:10:17 -08001763 goto retry;
Chris Leech8f550f92009-10-21 16:28:09 -07001764
Robert Love42e9a922008-12-09 15:10:17 -08001765 fsp->recov_seq = seq;
1766 fsp->xfer_len = offset;
1767 fsp->xfer_contig_end = offset;
1768 fsp->state &= ~FC_SRB_RCV_STATUS;
1769 fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
1770 return;
1771retry:
Hannes Reineckea0452bb2016-10-13 15:10:38 +02001772 fc_fcp_retry_cmd(fsp, FC_TRANS_RESET);
Robert Love42e9a922008-12-09 15:10:17 -08001773}
1774
Robert Love3a3b42b2009-11-03 11:47:39 -08001775/**
1776 * fc_fcp_srr_resp() - Handler for SRR response
1777 * @seq: The sequence the SRR is on
1778 * @fp: The SRR frame
1779 * @arg: The FCP packet the SRR is on
Robert Love42e9a922008-12-09 15:10:17 -08001780 */
1781static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1782{
1783 struct fc_fcp_pkt *fsp = arg;
1784 struct fc_frame_header *fh;
1785
1786 if (IS_ERR(fp)) {
1787 fc_fcp_srr_error(fsp, fp);
1788 return;
1789 }
1790
1791 if (fc_fcp_lock_pkt(fsp))
1792 goto out;
1793
1794 fh = fc_frame_header_get(fp);
1795 /*
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001796 * BUG? fc_fcp_srr_error calls fc_exch_done which would release
Robert Love42e9a922008-12-09 15:10:17 -08001797 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001798 * then fc_exch_timeout would be sending an abort. The fc_exch_done
Robert Love42e9a922008-12-09 15:10:17 -08001799 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
1800 * an abort response though.
1801 */
1802 if (fh->fh_type == FC_TYPE_BLS) {
1803 fc_fcp_unlock_pkt(fsp);
1804 return;
1805 }
1806
Robert Love42e9a922008-12-09 15:10:17 -08001807 switch (fc_frame_payload_op(fp)) {
1808 case ELS_LS_ACC:
1809 fsp->recov_retry = 0;
Vasu Devf2817ec2011-04-01 16:06:40 -07001810 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
Robert Love42e9a922008-12-09 15:10:17 -08001811 break;
1812 case ELS_LS_RJT:
1813 default:
john fastabendac17ea82010-11-30 16:18:39 -08001814 fc_fcp_recovery(fsp, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001815 break;
1816 }
1817 fc_fcp_unlock_pkt(fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001818out:
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001819 fc_exch_done(seq);
Robert Love42e9a922008-12-09 15:10:17 -08001820 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001821}
1822
Robert Love3a3b42b2009-11-03 11:47:39 -08001823/**
1824 * fc_fcp_srr_error() - Handler for SRR errors
1825 * @fsp: The FCP packet that the SRR error is on
1826 * @fp: The SRR frame
1827 */
Robert Love42e9a922008-12-09 15:10:17 -08001828static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1829{
1830 if (fc_fcp_lock_pkt(fsp))
1831 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001832 switch (PTR_ERR(fp)) {
1833 case -FC_EX_TIMEOUT:
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001834 FC_FCP_DBG(fsp, "SRR timeout, retries %d\n", fsp->recov_retry);
Robert Love42e9a922008-12-09 15:10:17 -08001835 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1836 fc_fcp_rec(fsp);
1837 else
john fastabendac17ea82010-11-30 16:18:39 -08001838 fc_fcp_recovery(fsp, FC_TIMED_OUT);
Robert Love42e9a922008-12-09 15:10:17 -08001839 break;
1840 case -FC_EX_CLOSED: /* e.g., link failure */
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001841 FC_FCP_DBG(fsp, "SRR error, exchange closed\n");
Robert Love42e9a922008-12-09 15:10:17 -08001842 /* fall through */
1843 default:
Hannes Reineckea0452bb2016-10-13 15:10:38 +02001844 fc_fcp_retry_cmd(fsp, FC_ERROR);
Robert Love42e9a922008-12-09 15:10:17 -08001845 break;
1846 }
1847 fc_fcp_unlock_pkt(fsp);
1848out:
Hannes Reinecke768c72c2016-10-18 10:01:50 +02001849 fc_exch_done(fsp->recov_seq);
Robert Love42e9a922008-12-09 15:10:17 -08001850}
1851
Robert Love3a3b42b2009-11-03 11:47:39 -08001852/**
1853 * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
1854 * @lport: The local port to be checked
1855 */
1856static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001857{
1858 /* lock ? */
Robert Love3a3b42b2009-11-03 11:47:39 -08001859 return (lport->state == LPORT_ST_READY) &&
1860 lport->link_up && !lport->qfull;
Robert Love42e9a922008-12-09 15:10:17 -08001861}
1862
1863/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001864 * fc_queuecommand() - The queuecommand function of the SCSI template
Robert Love04885b12011-01-28 16:04:29 -08001865 * @shost: The Scsi_Host that the command was issued to
Robert Love3a3b42b2009-11-03 11:47:39 -08001866 * @cmd: The scsi_cmnd to be executed
Robert Love42e9a922008-12-09 15:10:17 -08001867 *
Robert Love04885b12011-01-28 16:04:29 -08001868 * This is the i/o strategy routine, called by the SCSI layer.
Robert Love42e9a922008-12-09 15:10:17 -08001869 */
Robert Love04885b12011-01-28 16:04:29 -08001870int fc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc_cmd)
Robert Love42e9a922008-12-09 15:10:17 -08001871{
Robert Love04885b12011-01-28 16:04:29 -08001872 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -08001873 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1874 struct fc_fcp_pkt *fsp;
Robert Love3a3b42b2009-11-03 11:47:39 -08001875 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -08001876 int rval;
1877 int rc = 0;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001878 struct fc_stats *stats;
Robert Love42e9a922008-12-09 15:10:17 -08001879
Robert Love42e9a922008-12-09 15:10:17 -08001880 rval = fc_remote_port_chkready(rport);
1881 if (rval) {
1882 sc_cmd->result = rval;
Robert Love04885b12011-01-28 16:04:29 -08001883 sc_cmd->scsi_done(sc_cmd);
Mike Christiebe948fc2010-08-02 16:02:24 -05001884 return 0;
Robert Love42e9a922008-12-09 15:10:17 -08001885 }
1886
1887 if (!*(struct fc_remote_port **)rport->dd_data) {
1888 /*
1889 * rport is transitioning from blocked/deleted to
1890 * online
1891 */
1892 sc_cmd->result = DID_IMM_RETRY << 16;
Robert Love04885b12011-01-28 16:04:29 -08001893 sc_cmd->scsi_done(sc_cmd);
Robert Love42e9a922008-12-09 15:10:17 -08001894 goto out;
1895 }
1896
Robert Love3a3b42b2009-11-03 11:47:39 -08001897 rpriv = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001898
Robert Love3a3b42b2009-11-03 11:47:39 -08001899 if (!fc_fcp_lport_queue_ready(lport)) {
Hannes Reinecke5d339d12016-10-13 15:10:48 +02001900 if (lport->qfull) {
Hannes Reineckec1061252017-04-27 16:25:03 +02001901 if (fc_fcp_can_queue_ramp_down(lport))
1902 shost_printk(KERN_ERR, lport->host,
1903 "libfc: queue full, "
1904 "reducing can_queue to %d.\n",
1905 lport->host->can_queue);
Hannes Reinecke5d339d12016-10-13 15:10:48 +02001906 }
Robert Love42e9a922008-12-09 15:10:17 -08001907 rc = SCSI_MLQUEUE_HOST_BUSY;
1908 goto out;
1909 }
1910
Robert Love3a3b42b2009-11-03 11:47:39 -08001911 fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
Robert Love42e9a922008-12-09 15:10:17 -08001912 if (fsp == NULL) {
1913 rc = SCSI_MLQUEUE_HOST_BUSY;
1914 goto out;
1915 }
1916
1917 /*
1918 * build the libfc request pkt
1919 */
1920 fsp->cmd = sc_cmd; /* save the cmd */
Robert Love42e9a922008-12-09 15:10:17 -08001921 fsp->rport = rport; /* set the remote port ptr */
Robert Love42e9a922008-12-09 15:10:17 -08001922
1923 /*
1924 * set up the transfer length
1925 */
1926 fsp->data_len = scsi_bufflen(sc_cmd);
1927 fsp->xfer_len = 0;
1928
1929 /*
1930 * setup the data direction
1931 */
Vasu Dev1bd49b42012-05-25 10:26:43 -07001932 stats = per_cpu_ptr(lport->stats, get_cpu());
Robert Love42e9a922008-12-09 15:10:17 -08001933 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1934 fsp->req_flags = FC_SRB_READ;
1935 stats->InputRequests++;
Joe Eykholt5f0e3852010-11-30 16:20:18 -08001936 stats->InputBytes += fsp->data_len;
Robert Love42e9a922008-12-09 15:10:17 -08001937 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1938 fsp->req_flags = FC_SRB_WRITE;
1939 stats->OutputRequests++;
Joe Eykholt5f0e3852010-11-30 16:20:18 -08001940 stats->OutputBytes += fsp->data_len;
Robert Love42e9a922008-12-09 15:10:17 -08001941 } else {
1942 fsp->req_flags = 0;
1943 stats->ControlRequests++;
1944 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001945 put_cpu();
Robert Love42e9a922008-12-09 15:10:17 -08001946
Robert Love42e9a922008-12-09 15:10:17 -08001947 /*
1948 * send it to the lower layer
1949 * if we get -1 return then put the request in the pending
1950 * queue.
1951 */
Robert Love3a3b42b2009-11-03 11:47:39 -08001952 rval = fc_fcp_pkt_send(lport, fsp);
Robert Love42e9a922008-12-09 15:10:17 -08001953 if (rval != 0) {
1954 fsp->state = FC_SRB_FREE;
1955 fc_fcp_pkt_release(fsp);
1956 rc = SCSI_MLQUEUE_HOST_BUSY;
1957 }
1958out:
1959 return rc;
1960}
1961EXPORT_SYMBOL(fc_queuecommand);
1962
1963/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001964 * fc_io_compl() - Handle responses for completed commands
1965 * @fsp: The FCP packet that is complete
Robert Love42e9a922008-12-09 15:10:17 -08001966 *
Robert Love3a3b42b2009-11-03 11:47:39 -08001967 * Translates fcp_pkt errors to a Linux SCSI errors.
Robert Love42e9a922008-12-09 15:10:17 -08001968 * The fcp packet lock must be held when calling.
1969 */
1970static void fc_io_compl(struct fc_fcp_pkt *fsp)
1971{
1972 struct fc_fcp_internal *si;
1973 struct scsi_cmnd *sc_cmd;
Robert Love3a3b42b2009-11-03 11:47:39 -08001974 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08001975 unsigned long flags;
1976
Yi Zoub277d2a2009-02-27 14:07:21 -08001977 /* release outstanding ddp context */
1978 fc_fcp_ddp_done(fsp);
1979
Robert Love42e9a922008-12-09 15:10:17 -08001980 fsp->state |= FC_SRB_COMPL;
1981 if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
1982 spin_unlock_bh(&fsp->scsi_pkt_lock);
1983 del_timer_sync(&fsp->timer);
1984 spin_lock_bh(&fsp->scsi_pkt_lock);
1985 }
1986
Robert Love3a3b42b2009-11-03 11:47:39 -08001987 lport = fsp->lp;
1988 si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001989
1990 /*
Vasu Dev84c3e1a2009-11-03 11:48:06 -08001991 * if can_queue ramp down is done then try can_queue ramp up
1992 * since commands are completing now.
Robert Love42e9a922008-12-09 15:10:17 -08001993 */
Vasu Dev84c3e1a2009-11-03 11:48:06 -08001994 if (si->last_can_queue_ramp_down_time)
1995 fc_fcp_can_queue_ramp_up(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001996
1997 sc_cmd = fsp->cmd;
Robert Love42e9a922008-12-09 15:10:17 -08001998 CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
1999 switch (fsp->status_code) {
2000 case FC_COMPLETE:
2001 if (fsp->cdb_status == 0) {
2002 /*
2003 * good I/O status
2004 */
2005 sc_cmd->result = DID_OK << 16;
2006 if (fsp->scsi_resid)
2007 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
Robert Love42e9a922008-12-09 15:10:17 -08002008 } else {
2009 /*
2010 * transport level I/O was ok but scsi
2011 * has non zero status
2012 */
2013 sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
2014 }
2015 break;
2016 case FC_ERROR:
Robert Loveb3ef990c2010-03-12 16:08:50 -08002017 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
2018 "due to FC_ERROR\n");
Robert Love42e9a922008-12-09 15:10:17 -08002019 sc_cmd->result = DID_ERROR << 16;
2020 break;
2021 case FC_DATA_UNDRUN:
Vasu Dev26d9cab2009-02-27 10:55:07 -08002022 if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
Robert Love42e9a922008-12-09 15:10:17 -08002023 /*
2024 * scsi status is good but transport level
Vasu Dev26d9cab2009-02-27 10:55:07 -08002025 * underrun.
Robert Love42e9a922008-12-09 15:10:17 -08002026 */
Robert Loveb3ef990c2010-03-12 16:08:50 -08002027 if (fsp->state & FC_SRB_RCV_STATUS) {
2028 sc_cmd->result = DID_OK << 16;
2029 } else {
2030 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml"
2031 " due to FC_DATA_UNDRUN (trans)\n");
2032 sc_cmd->result = DID_ERROR << 16;
2033 }
Robert Love42e9a922008-12-09 15:10:17 -08002034 } else {
2035 /*
2036 * scsi got underrun, this is an error
2037 */
Robert Loveb3ef990c2010-03-12 16:08:50 -08002038 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
2039 "due to FC_DATA_UNDRUN (scsi)\n");
Robert Love42e9a922008-12-09 15:10:17 -08002040 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
2041 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
2042 }
2043 break;
2044 case FC_DATA_OVRRUN:
2045 /*
2046 * overrun is an error
2047 */
Robert Loveb3ef990c2010-03-12 16:08:50 -08002048 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
2049 "due to FC_DATA_OVRRUN\n");
Robert Love42e9a922008-12-09 15:10:17 -08002050 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
2051 break;
2052 case FC_CMD_ABORTED:
Hannes Reinecke0f4c16a2016-10-13 15:10:43 +02002053 if (host_byte(sc_cmd->result) == DID_TIME_OUT)
2054 FC_FCP_DBG(fsp, "Returning DID_TIME_OUT to scsi-ml "
2055 "due to FC_CMD_ABORTED\n");
2056 else {
2057 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
2058 "due to FC_CMD_ABORTED\n");
2059 set_host_byte(sc_cmd, DID_ERROR);
2060 }
2061 sc_cmd->result |= fsp->io_status;
Robert Love42e9a922008-12-09 15:10:17 -08002062 break;
Robert Love42e9a922008-12-09 15:10:17 -08002063 case FC_CMD_RESET:
john fastabendac17ea82010-11-30 16:18:39 -08002064 FC_FCP_DBG(fsp, "Returning DID_RESET to scsi-ml "
2065 "due to FC_CMD_RESET\n");
Robert Love42e9a922008-12-09 15:10:17 -08002066 sc_cmd->result = (DID_RESET << 16);
2067 break;
Hannes Reineckea0452bb2016-10-13 15:10:38 +02002068 case FC_TRANS_RESET:
2069 FC_FCP_DBG(fsp, "Returning DID_SOFT_ERROR to scsi-ml "
2070 "due to FC_TRANS_RESET\n");
2071 sc_cmd->result = (DID_SOFT_ERROR << 16);
2072 break;
Robert Love42e9a922008-12-09 15:10:17 -08002073 case FC_HRD_ERROR:
john fastabendac17ea82010-11-30 16:18:39 -08002074 FC_FCP_DBG(fsp, "Returning DID_NO_CONNECT to scsi-ml "
2075 "due to FC_HRD_ERROR\n");
Robert Love42e9a922008-12-09 15:10:17 -08002076 sc_cmd->result = (DID_NO_CONNECT << 16);
2077 break;
john fastabendac17ea82010-11-30 16:18:39 -08002078 case FC_CRC_ERROR:
2079 FC_FCP_DBG(fsp, "Returning DID_PARITY to scsi-ml "
2080 "due to FC_CRC_ERROR\n");
2081 sc_cmd->result = (DID_PARITY << 16);
2082 break;
2083 case FC_TIMED_OUT:
2084 FC_FCP_DBG(fsp, "Returning DID_BUS_BUSY to scsi-ml "
2085 "due to FC_TIMED_OUT\n");
2086 sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
2087 break;
Robert Love42e9a922008-12-09 15:10:17 -08002088 default:
Robert Loveb3ef990c2010-03-12 16:08:50 -08002089 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
2090 "due to unknown error\n");
Robert Love42e9a922008-12-09 15:10:17 -08002091 sc_cmd->result = (DID_ERROR << 16);
2092 break;
2093 }
2094
Vasu Dev75eee722010-10-08 17:12:15 -07002095 if (lport->state != LPORT_ST_READY && fsp->status_code != FC_COMPLETE)
2096 sc_cmd->result = (DID_TRANSPORT_DISRUPTED << 16);
Vasu Deve0d93c52010-07-20 15:19:20 -07002097
Chris Leechc1ecb902009-12-10 09:59:26 -08002098 spin_lock_irqsave(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08002099 list_del(&fsp->list);
2100 sc_cmd->SCp.ptr = NULL;
Vasu Dev8b7ac2b2010-10-08 17:12:31 -07002101 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08002102 sc_cmd->scsi_done(sc_cmd);
Robert Love42e9a922008-12-09 15:10:17 -08002103
2104 /* release ref from initial allocation in queue command */
2105 fc_fcp_pkt_release(fsp);
2106}
2107
2108/**
Robert Love34f42a02009-02-27 10:55:45 -08002109 * fc_eh_abort() - Abort a command
Robert Love3a3b42b2009-11-03 11:47:39 -08002110 * @sc_cmd: The SCSI command to abort
Robert Love42e9a922008-12-09 15:10:17 -08002111 *
Robert Love3a3b42b2009-11-03 11:47:39 -08002112 * From SCSI host template.
2113 * Send an ABTS to the target device and wait for the response.
Robert Love42e9a922008-12-09 15:10:17 -08002114 */
2115int fc_eh_abort(struct scsi_cmnd *sc_cmd)
2116{
2117 struct fc_fcp_pkt *fsp;
Robert Love3a3b42b2009-11-03 11:47:39 -08002118 struct fc_lport *lport;
Vasu Dev8b7ac2b2010-10-08 17:12:31 -07002119 struct fc_fcp_internal *si;
Robert Love42e9a922008-12-09 15:10:17 -08002120 int rc = FAILED;
2121 unsigned long flags;
Vasu Dev21cc0bd32011-08-25 12:40:57 -07002122 int rval;
2123
2124 rval = fc_block_scsi_eh(sc_cmd);
2125 if (rval)
2126 return rval;
Robert Love42e9a922008-12-09 15:10:17 -08002127
Robert Love3a3b42b2009-11-03 11:47:39 -08002128 lport = shost_priv(sc_cmd->device->host);
2129 if (lport->state != LPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08002130 return rc;
Robert Love3a3b42b2009-11-03 11:47:39 -08002131 else if (!lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -08002132 return rc;
2133
Vasu Dev8b7ac2b2010-10-08 17:12:31 -07002134 si = fc_get_scsi_internal(lport);
2135 spin_lock_irqsave(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08002136 fsp = CMD_SP(sc_cmd);
2137 if (!fsp) {
2138 /* command completed while scsi eh was setting up */
Robert Love3c2c3bf2010-11-30 16:18:23 -08002139 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08002140 return SUCCESS;
2141 }
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002142 /* grab a ref so the fsp and sc_cmd cannot be released from under us */
Robert Love42e9a922008-12-09 15:10:17 -08002143 fc_fcp_pkt_hold(fsp);
Vasu Dev8b7ac2b2010-10-08 17:12:31 -07002144 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
Robert Love42e9a922008-12-09 15:10:17 -08002145
2146 if (fc_fcp_lock_pkt(fsp)) {
2147 /* completed while we were waiting for timer to be deleted */
2148 rc = SUCCESS;
2149 goto release_pkt;
2150 }
2151
Robert Lovec3401112009-10-21 16:27:06 -07002152 rc = fc_fcp_pkt_abort(fsp);
Robert Love42e9a922008-12-09 15:10:17 -08002153 fc_fcp_unlock_pkt(fsp);
2154
2155release_pkt:
2156 fc_fcp_pkt_release(fsp);
2157 return rc;
2158}
2159EXPORT_SYMBOL(fc_eh_abort);
2160
2161/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002162 * fc_eh_device_reset() - Reset a single LUN
2163 * @sc_cmd: The SCSI command which identifies the device whose
2164 * LUN is to be reset
Robert Love42e9a922008-12-09 15:10:17 -08002165 *
Robert Love3a3b42b2009-11-03 11:47:39 -08002166 * Set from SCSI host template.
Robert Love42e9a922008-12-09 15:10:17 -08002167 */
2168int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
2169{
Robert Love3a3b42b2009-11-03 11:47:39 -08002170 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -08002171 struct fc_fcp_pkt *fsp;
2172 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
2173 int rc = FAILED;
Robert Love42e9a922008-12-09 15:10:17 -08002174 int rval;
2175
Vasu Dev21cc0bd32011-08-25 12:40:57 -07002176 rval = fc_block_scsi_eh(sc_cmd);
Robert Love42e9a922008-12-09 15:10:17 -08002177 if (rval)
Vasu Dev21cc0bd32011-08-25 12:40:57 -07002178 return rval;
Robert Love42e9a922008-12-09 15:10:17 -08002179
Robert Love3a3b42b2009-11-03 11:47:39 -08002180 lport = shost_priv(sc_cmd->device->host);
Robert Love42e9a922008-12-09 15:10:17 -08002181
Robert Love3a3b42b2009-11-03 11:47:39 -08002182 if (lport->state != LPORT_ST_READY)
Robert Love42e9a922008-12-09 15:10:17 -08002183 return rc;
2184
Chris Leechce8b5df2010-04-09 14:23:10 -07002185 FC_SCSI_DBG(lport, "Resetting rport (%6.6x)\n", rport->port_id);
Robert Love74147052009-06-10 15:31:10 -07002186
Robert Love3a3b42b2009-11-03 11:47:39 -08002187 fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
Robert Love42e9a922008-12-09 15:10:17 -08002188 if (fsp == NULL) {
Robert Love74147052009-06-10 15:31:10 -07002189 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
Robert Love42e9a922008-12-09 15:10:17 -08002190 goto out;
2191 }
2192
2193 /*
2194 * Build the libfc request pkt. Do not set the scsi cmnd, because
2195 * the sc passed in is not setup for execution like when sent
2196 * through the queuecommand callout.
2197 */
Robert Love42e9a922008-12-09 15:10:17 -08002198 fsp->rport = rport; /* set the remote port ptr */
2199
2200 /*
2201 * flush outstanding commands
2202 */
Robert Love3a3b42b2009-11-03 11:47:39 -08002203 rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
Robert Love42e9a922008-12-09 15:10:17 -08002204 fsp->state = FC_SRB_FREE;
2205 fc_fcp_pkt_release(fsp);
2206
2207out:
2208 return rc;
2209}
2210EXPORT_SYMBOL(fc_eh_device_reset);
2211
2212/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002213 * fc_eh_host_reset() - Reset a Scsi_Host.
2214 * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
Robert Love42e9a922008-12-09 15:10:17 -08002215 */
2216int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
2217{
2218 struct Scsi_Host *shost = sc_cmd->device->host;
Robert Love3a3b42b2009-11-03 11:47:39 -08002219 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -08002220 unsigned long wait_tmo;
2221
Robert Love3a3b42b2009-11-03 11:47:39 -08002222 FC_SCSI_DBG(lport, "Resetting host\n");
Robert Love74147052009-06-10 15:31:10 -07002223
Hannes Reinecke31c0a632016-10-18 10:01:36 +02002224 fc_lport_reset(lport);
Robert Love42e9a922008-12-09 15:10:17 -08002225 wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
Robert Love3a3b42b2009-11-03 11:47:39 -08002226 while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
2227 wait_tmo))
Robert Love42e9a922008-12-09 15:10:17 -08002228 msleep(1000);
2229
Robert Love3a3b42b2009-11-03 11:47:39 -08002230 if (fc_fcp_lport_queue_ready(lport)) {
Robert Love74147052009-06-10 15:31:10 -07002231 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
Robert Love7b2787e2010-05-07 15:18:41 -07002232 "on port (%6.6x)\n", lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -08002233 return SUCCESS;
2234 } else {
Robert Love74147052009-06-10 15:31:10 -07002235 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
Chris Leechce8b5df2010-04-09 14:23:10 -07002236 "port (%6.6x) is not ready.\n",
Robert Love7b2787e2010-05-07 15:18:41 -07002237 lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -08002238 return FAILED;
2239 }
2240}
2241EXPORT_SYMBOL(fc_eh_host_reset);
2242
2243/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002244 * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
2245 * @sdev: The SCSI device that identifies the SCSI host
Robert Love42e9a922008-12-09 15:10:17 -08002246 *
2247 * Configures queue depth based on host's cmd_per_len. If not set
2248 * then we use the libfc default.
2249 */
2250int fc_slave_alloc(struct scsi_device *sdev)
2251{
2252 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Robert Love42e9a922008-12-09 15:10:17 -08002253
2254 if (!rport || fc_remote_port_chkready(rport))
2255 return -ENXIO;
2256
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002257 scsi_change_queue_depth(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
Robert Love42e9a922008-12-09 15:10:17 -08002258 return 0;
2259}
2260EXPORT_SYMBOL(fc_slave_alloc);
2261
Robert Love3a3b42b2009-11-03 11:47:39 -08002262/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002263 * fc_fcp_destory() - Tear down the FCP layer for a given local port
2264 * @lport: The local port that no longer needs the FCP layer
2265 */
2266void fc_fcp_destroy(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08002267{
Robert Love3a3b42b2009-11-03 11:47:39 -08002268 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
Robert Love42e9a922008-12-09 15:10:17 -08002269
2270 if (!list_empty(&si->scsi_pkt_queue))
Robert Love74147052009-06-10 15:31:10 -07002271 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
Robert Love7b2787e2010-05-07 15:18:41 -07002272 "port (%6.6x)\n", lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -08002273
2274 mempool_destroy(si->scsi_pkt_pool);
2275 kfree(si);
Robert Love3a3b42b2009-11-03 11:47:39 -08002276 lport->scsi_priv = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08002277}
2278EXPORT_SYMBOL(fc_fcp_destroy);
2279
Randy Dunlap55204902011-01-28 16:03:57 -08002280int fc_setup_fcp(void)
Robert Love93e6d5a2009-11-03 11:46:03 -08002281{
2282 int rc = 0;
2283
2284 scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2285 sizeof(struct fc_fcp_pkt),
2286 0, SLAB_HWCACHE_ALIGN, NULL);
2287 if (!scsi_pkt_cachep) {
2288 printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2289 "module load failed!");
2290 rc = -ENOMEM;
2291 }
2292
2293 return rc;
2294}
2295
Randy Dunlap55204902011-01-28 16:03:57 -08002296void fc_destroy_fcp(void)
Robert Love93e6d5a2009-11-03 11:46:03 -08002297{
2298 if (scsi_pkt_cachep)
2299 kmem_cache_destroy(scsi_pkt_cachep);
2300}
2301
Robert Love3a3b42b2009-11-03 11:47:39 -08002302/**
2303 * fc_fcp_init() - Initialize the FCP layer for a local port
2304 * @lport: The local port to initialize the exchange layer for
2305 */
2306int fc_fcp_init(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08002307{
2308 int rc;
2309 struct fc_fcp_internal *si;
2310
Robert Love3a3b42b2009-11-03 11:47:39 -08002311 if (!lport->tt.fcp_cmd_send)
2312 lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
Robert Love42e9a922008-12-09 15:10:17 -08002313
Robert Love3a3b42b2009-11-03 11:47:39 -08002314 if (!lport->tt.fcp_cleanup)
2315 lport->tt.fcp_cleanup = fc_fcp_cleanup;
Robert Love42e9a922008-12-09 15:10:17 -08002316
Robert Love3a3b42b2009-11-03 11:47:39 -08002317 if (!lport->tt.fcp_abort_io)
2318 lport->tt.fcp_abort_io = fc_fcp_abort_io;
Robert Love42e9a922008-12-09 15:10:17 -08002319
2320 si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
2321 if (!si)
2322 return -ENOMEM;
Robert Love3a3b42b2009-11-03 11:47:39 -08002323 lport->scsi_priv = si;
Vasu Dev84c3e1a2009-11-03 11:48:06 -08002324 si->max_can_queue = lport->host->can_queue;
Robert Love42e9a922008-12-09 15:10:17 -08002325 INIT_LIST_HEAD(&si->scsi_pkt_queue);
Chris Leechc1ecb902009-12-10 09:59:26 -08002326 spin_lock_init(&si->scsi_queue_lock);
Robert Love42e9a922008-12-09 15:10:17 -08002327
2328 si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
2329 if (!si->scsi_pkt_pool) {
2330 rc = -ENOMEM;
2331 goto free_internal;
2332 }
2333 return 0;
2334
2335free_internal:
2336 kfree(si);
2337 return rc;
2338}
2339EXPORT_SYMBOL(fc_fcp_init);