qla_os.c 160 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * QLogic Fibre Channel HBA Driver
3
 * Copyright (c)  2003-2014 QLogic Corporation
Linus Torvalds's avatar
Linus Torvalds committed
4
 *
5
 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds's avatar
Linus Torvalds committed
6
7
8
9
10
11
 */
#include "qla_def.h"

#include <linux/moduleparam.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
12
#include <linux/kthread.h>
13
#include <linux/mutex.h>
14
#include <linux/kobject.h>
15
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
16
17
18
19
20
#include <scsi/scsi_tcq.h>
#include <scsi/scsicam.h>
#include <scsi/scsi_transport.h>
#include <scsi/scsi_transport_fc.h>

21
22
#include "qla_target.h"

Linus Torvalds's avatar
Linus Torvalds committed
23
24
25
26
27
/*
 * Driver version
 */
char qla2x00_version_str[40];

28
29
static int apidev_major;

Linus Torvalds's avatar
Linus Torvalds committed
30
31
32
/*
 * SRB allocation cache
 */
33
static struct kmem_cache *srb_cachep;
Linus Torvalds's avatar
Linus Torvalds committed
34

35
36
37
38
/*
 * CT6 CTX allocation cache
 */
static struct kmem_cache *ctx_cachep;
39
40
41
42
/*
 * error level for logging
 */
int ql_errlev = ql_log_all;
43

44
static int ql2xenableclass2;
45
46
47
48
49
module_param(ql2xenableclass2, int, S_IRUGO|S_IRUSR);
MODULE_PARM_DESC(ql2xenableclass2,
		"Specify if Class 2 operations are supported from the very "
		"beginning. Default is 0 - class 2 not supported.");

50

Linus Torvalds's avatar
Linus Torvalds committed
51
int ql2xlogintimeout = 20;
52
module_param(ql2xlogintimeout, int, S_IRUGO);
Linus Torvalds's avatar
Linus Torvalds committed
53
54
55
MODULE_PARM_DESC(ql2xlogintimeout,
		"Login timeout value in seconds.");

56
int qlport_down_retry;
57
module_param(qlport_down_retry, int, S_IRUGO);
Linus Torvalds's avatar
Linus Torvalds committed
58
MODULE_PARM_DESC(qlport_down_retry,
59
		"Maximum number of command retries to a port that returns "
Linus Torvalds's avatar
Linus Torvalds committed
60
61
62
63
64
65
		"a PORT-DOWN status.");

int ql2xplogiabsentdevice;
module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(ql2xplogiabsentdevice,
		"Option to enable PLOGI to devices that are not present after "
66
		"a Fabric scan.  This is needed for several broken switches. "
Linus Torvalds's avatar
Linus Torvalds committed
67
68
69
		"Default is 0 - no PLOGI. 1 - perfom PLOGI.");

int ql2xloginretrycount = 0;
70
module_param(ql2xloginretrycount, int, S_IRUGO);
Linus Torvalds's avatar
Linus Torvalds committed
71
72
73
MODULE_PARM_DESC(ql2xloginretrycount,
		"Specify an alternate value for the NVRAM login retry count.");

74
int ql2xallocfwdump = 1;
75
module_param(ql2xallocfwdump, int, S_IRUGO);
76
77
78
79
80
MODULE_PARM_DESC(ql2xallocfwdump,
		"Option to enable allocation of memory for a firmware dump "
		"during HBA initialization.  Memory allocation requirements "
		"vary by ISP type.  Default is 1 - allocate memory.");

81
int ql2xextended_error_logging;
82
module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
83
MODULE_PARM_DESC(ql2xextended_error_logging,
84
85
86
87
88
89
90
91
92
		"Option to enable extended error logging,\n"
		"\t\tDefault is 0 - no logging.  0x40000000 - Module Init & Probe.\n"
		"\t\t0x20000000 - Mailbox Cmnds. 0x10000000 - Device Discovery.\n"
		"\t\t0x08000000 - IO tracing.    0x04000000 - DPC Thread.\n"
		"\t\t0x02000000 - Async events.  0x01000000 - Timer routines.\n"
		"\t\t0x00800000 - User space.    0x00400000 - Task Management.\n"
		"\t\t0x00200000 - AER/EEH.       0x00100000 - Multi Q.\n"
		"\t\t0x00080000 - P3P Specific.  0x00040000 - Virtual Port.\n"
		"\t\t0x00020000 - Buffer Dump.   0x00010000 - Misc.\n"
93
94
		"\t\t0x00008000 - Verbose.       0x00004000 - Target.\n"
		"\t\t0x00002000 - Target Mgmt.   0x00001000 - Target TMF.\n"
95
		"\t\t0x7fffffff - For enabling all logs, can be too many logs.\n"
96
97
98
		"\t\t0x1e400000 - Preferred value for capturing essential "
		"debug information (equivalent to old "
		"ql2xextended_error_logging=1).\n"
99
		"\t\tDo LOGICAL OR of the value to enable more than one level");
100

101
int ql2xshiftctondsd = 6;
102
module_param(ql2xshiftctondsd, int, S_IRUGO);
103
104
105
106
MODULE_PARM_DESC(ql2xshiftctondsd,
		"Set to control shifting of command type processing "
		"based on total number of SG elements.");

107
int ql2xfdmienable=1;
108
module_param(ql2xfdmienable, int, S_IRUGO|S_IWUSR);
109
MODULE_PARM_DESC(ql2xfdmienable,
110
111
		"Enables FDMI registrations. "
		"0 - no FDMI. Default is 1 - perform FDMI.");
112

113
114
#define MAX_Q_DEPTH	32
static int ql2xmaxqdepth = MAX_Q_DEPTH;
115
116
module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(ql2xmaxqdepth,
117
118
		"Maximum queue depth to set for each LUN. "
		"Default is 32.");
119

120
121
int ql2xenabledif = 2;
module_param(ql2xenabledif, int, S_IRUGO);
122
MODULE_PARM_DESC(ql2xenabledif,
123
124
125
126
127
		" Enable T10-CRC-DIF:\n"
		" Default is 2.\n"
		"  0 -- No DIF Support\n"
		"  1 -- Enable DIF for all types\n"
		"  2 -- Enable DIF for all types, except Type 0.\n");
128

129
int ql2xenablehba_err_chk = 2;
130
131
module_param(ql2xenablehba_err_chk, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(ql2xenablehba_err_chk,
132
		" Enable T10-CRC-DIF Error isolation by HBA:\n"
133
		" Default is 2.\n"
134
135
136
		"  0 -- Error isolation disabled\n"
		"  1 -- Error isolation enabled only for DIX Type 0\n"
		"  2 -- Error isolation enabled for all Types\n");
137

138
int ql2xiidmaenable=1;
139
module_param(ql2xiidmaenable, int, S_IRUGO);
140
141
142
143
MODULE_PARM_DESC(ql2xiidmaenable,
		"Enables iIDMA settings "
		"Default is 1 - perform iIDMA. 0 - no iIDMA.");

144
int ql2xmaxqueues = 1;
145
module_param(ql2xmaxqueues, int, S_IRUGO);
146
147
MODULE_PARM_DESC(ql2xmaxqueues,
		"Enables MQ settings "
148
149
		"Default is 1 for single queue. Set it to number "
		"of queues in MQ mode.");
150
151

int ql2xmultique_tag;
152
module_param(ql2xmultique_tag, int, S_IRUGO);
153
154
155
156
MODULE_PARM_DESC(ql2xmultique_tag,
		"Enables CPU affinity settings for the driver "
		"Default is 0 for no affinity of request and response IO. "
		"Set it to 1 to turn on the cpu affinity.");
157
158

int ql2xfwloadbin;
159
module_param(ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
160
MODULE_PARM_DESC(ql2xfwloadbin,
161
162
		"Option to specify location from which to load ISP firmware:.\n"
		" 2 -- load firmware via the request_firmware() (hotplug).\n"
163
164
165
166
		"      interface.\n"
		" 1 -- load firmware from flash.\n"
		" 0 -- use default semantics.\n");

167
int ql2xetsenable;
168
module_param(ql2xetsenable, int, S_IRUGO);
169
170
171
172
MODULE_PARM_DESC(ql2xetsenable,
		"Enables firmware ETS burst."
		"Default is 0 - skip ETS enablement.");

173
int ql2xdbwr = 1;
174
module_param(ql2xdbwr, int, S_IRUGO|S_IWUSR);
175
MODULE_PARM_DESC(ql2xdbwr,
176
177
178
		"Option to specify scheme for request queue posting.\n"
		" 0 -- Regular doorbell.\n"
		" 1 -- CAMRAM doorbell (faster).\n");
179

180
int ql2xtargetreset = 1;
181
module_param(ql2xtargetreset, int, S_IRUGO);
182
183
184
185
MODULE_PARM_DESC(ql2xtargetreset,
		 "Enable target reset."
		 "Default is 1 - use hw defaults.");

186
int ql2xgffidenable;
187
module_param(ql2xgffidenable, int, S_IRUGO);
188
189
190
MODULE_PARM_DESC(ql2xgffidenable,
		"Enables GFF_ID checks of port type. "
		"Default is 0 - Do not use GFF_ID information.");
191

192
int ql2xasynctmfenable;
193
module_param(ql2xasynctmfenable, int, S_IRUGO);
194
195
196
MODULE_PARM_DESC(ql2xasynctmfenable,
		"Enables issue of TM IOCBs asynchronously via IOCB mechanism"
		"Default is 0 - Issue TM IOCBs via mailbox mechanism.");
197
198

int ql2xdontresethba;
199
module_param(ql2xdontresethba, int, S_IRUGO|S_IWUSR);
200
MODULE_PARM_DESC(ql2xdontresethba,
201
202
203
		"Option to specify reset behaviour.\n"
		" 0 (Default) -- Reset on failure.\n"
		" 1 -- Do not reset on failure.\n");
204

205
206
uint64_t ql2xmaxlun = MAX_LUNS;
module_param(ql2xmaxlun, ullong, S_IRUGO);
207
208
209
210
MODULE_PARM_DESC(ql2xmaxlun,
		"Defines the maximum LU number to register with the SCSI "
		"midlayer. Default is 65535.");

211
212
213
214
int ql2xmdcapmask = 0x1F;
module_param(ql2xmdcapmask, int, S_IRUGO);
MODULE_PARM_DESC(ql2xmdcapmask,
		"Set the Minidump driver capture mask level. "
215
		"Default is 0x1F - Can be set to 0x3, 0x7, 0xF, 0x1F, 0x7F.");
216

217
int ql2xmdenable = 1;
218
219
220
module_param(ql2xmdenable, int, S_IRUGO);
MODULE_PARM_DESC(ql2xmdenable,
		"Enable/disable MiniDump. "
221
222
		"0 - MiniDump disabled. "
		"1 (Default) - MiniDump enabled.");
223

Linus Torvalds's avatar
Linus Torvalds committed
224
/*
225
 * SCSI host template entry points
Linus Torvalds's avatar
Linus Torvalds committed
226
227
 */
static int qla2xxx_slave_configure(struct scsi_device * device);
228
static int qla2xxx_slave_alloc(struct scsi_device *);
229
230
static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
static void qla2xxx_scan_start(struct Scsi_Host *);
231
static void qla2xxx_slave_destroy(struct scsi_device *);
Jeff Garzik's avatar
Jeff Garzik committed
232
static int qla2xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Linus Torvalds's avatar
Linus Torvalds committed
233
234
static int qla2xxx_eh_abort(struct scsi_cmnd *);
static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
235
static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
Linus Torvalds's avatar
Linus Torvalds committed
236
237
238
static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
static int qla2xxx_eh_host_reset(struct scsi_cmnd *);

239
static void qla2x00_clear_drv_active(struct qla_hw_data *);
240
static void qla2x00_free_device(scsi_qla_host_t *);
241
static void qla83xx_disable_laser(scsi_qla_host_t *vha);
242

243
struct scsi_host_template qla2xxx_driver_template = {
Linus Torvalds's avatar
Linus Torvalds committed
244
	.module			= THIS_MODULE,
245
	.name			= QLA2XXX_DRIVER_NAME,
246
	.queuecommand		= qla2xxx_queuecommand,
247
248
249

	.eh_abort_handler	= qla2xxx_eh_abort,
	.eh_device_reset_handler = qla2xxx_eh_device_reset,
250
	.eh_target_reset_handler = qla2xxx_eh_target_reset,
251
252
253
254
255
256
257
	.eh_bus_reset_handler	= qla2xxx_eh_bus_reset,
	.eh_host_reset_handler	= qla2xxx_eh_host_reset,

	.slave_configure	= qla2xxx_slave_configure,

	.slave_alloc		= qla2xxx_slave_alloc,
	.slave_destroy		= qla2xxx_slave_destroy,
258
259
	.scan_finished		= qla2xxx_scan_finished,
	.scan_start		= qla2xxx_scan_start,
260
	.change_queue_depth	= scsi_change_queue_depth,
261
262
263
264
265
266
	.this_id		= -1,
	.cmd_per_lun		= 3,
	.use_clustering		= ENABLE_CLUSTERING,
	.sg_tablesize		= SG_ALL,

	.max_sectors		= 0xFFFF,
267
	.shost_attrs		= qla2x00_host_attrs,
268
269

	.supported_mode		= MODE_INITIATOR,
270
	.track_queue_depth	= 1,
271
272
};

Linus Torvalds's avatar
Linus Torvalds committed
273
static struct scsi_transport_template *qla2xxx_transport_template = NULL;
274
struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
275
276
277
278
279
280

/* TODO Convert to inlines
 *
 * Timer routines
 */

281
__inline__ void
282
qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
Linus Torvalds's avatar
Linus Torvalds committed
283
{
284
285
286
287
288
289
	init_timer(&vha->timer);
	vha->timer.expires = jiffies + interval * HZ;
	vha->timer.data = (unsigned long)vha;
	vha->timer.function = (void (*)(unsigned long))func;
	add_timer(&vha->timer);
	vha->timer_active = 1;
Linus Torvalds's avatar
Linus Torvalds committed
290
291
292
}

static inline void
293
qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
Linus Torvalds's avatar
Linus Torvalds committed
294
{
295
	/* Currently used for 82XX only. */
296
297
298
	if (vha->device_flags & DFLG_DEV_FAILED) {
		ql_dbg(ql_dbg_timer, vha, 0x600d,
		    "Device in a failed state, returning.\n");
299
		return;
300
	}
301

302
	mod_timer(&vha->timer, jiffies + interval * HZ);
Linus Torvalds's avatar
Linus Torvalds committed
303
304
}

Adrian Bunk's avatar
Adrian Bunk committed
305
static __inline__ void
306
qla2x00_stop_timer(scsi_qla_host_t *vha)
Linus Torvalds's avatar
Linus Torvalds committed
307
{
308
309
	del_timer_sync(&vha->timer);
	vha->timer_active = 0;
Linus Torvalds's avatar
Linus Torvalds committed
310
311
312
313
314
315
}

static int qla2x00_do_dpc(void *data);

static void qla2x00_rst_aen(scsi_qla_host_t *);

316
317
static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
	struct req_que **, struct rsp_que **);
318
static void qla2x00_free_fw_dump(struct qla_hw_data *);
319
static void qla2x00_mem_free(struct qla_hw_data *);
Linus Torvalds's avatar
Linus Torvalds committed
320
321

/* -------------------------------------------------------------------------- */
322
323
static int qla2x00_alloc_queues(struct qla_hw_data *ha, struct req_que *req,
				struct rsp_que *rsp)
324
{
325
	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
326
	ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
327
328
				GFP_KERNEL);
	if (!ha->req_q_map) {
329
330
		ql_log(ql_log_fatal, vha, 0x003b,
		    "Unable to allocate memory for request queue ptrs.\n");
331
332
333
		goto fail_req_map;
	}

334
	ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
335
336
				GFP_KERNEL);
	if (!ha->rsp_q_map) {
337
338
		ql_log(ql_log_fatal, vha, 0x003c,
		    "Unable to allocate memory for response queue ptrs.\n");
339
340
		goto fail_rsp_map;
	}
341
342
343
344
345
346
	/*
	 * Make sure we record at least the request and response queue zero in
	 * case we need to free them if part of the probe fails.
	 */
	ha->rsp_q_map[0] = rsp;
	ha->req_q_map[0] = req;
347
348
349
350
351
352
353
354
355
356
357
	set_bit(0, ha->rsp_qid_map);
	set_bit(0, ha->req_qid_map);
	return 1;

fail_rsp_map:
	kfree(ha->req_q_map);
	ha->req_q_map = NULL;
fail_req_map:
	return -ENOMEM;
}

358
static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
359
{
360
361
362
363
364
365
	if (IS_QLAFX00(ha)) {
		if (req && req->ring_fx00)
			dma_free_coherent(&ha->pdev->dev,
			    (req->length_fx00 + 1) * sizeof(request_t),
			    req->ring_fx00, req->dma_fx00);
	} else if (req && req->ring)
366
367
368
369
		dma_free_coherent(&ha->pdev->dev,
		(req->length + 1) * sizeof(request_t),
		req->ring, req->dma);

370
371
372
	if (req)
		kfree(req->outstanding_cmds);

373
374
375
376
	kfree(req);
	req = NULL;
}

377
378
static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
{
379
380
381
382
383
384
	if (IS_QLAFX00(ha)) {
		if (rsp && rsp->ring)
			dma_free_coherent(&ha->pdev->dev,
			    (rsp->length_fx00 + 1) * sizeof(request_t),
			    rsp->ring_fx00, rsp->dma_fx00);
	} else if (rsp && rsp->ring) {
385
386
387
		dma_free_coherent(&ha->pdev->dev,
		(rsp->length + 1) * sizeof(response_t),
		rsp->ring, rsp->dma);
388
	}
389
390
391
392
	kfree(rsp);
	rsp = NULL;
}

393
394
395
396
397
398
static void qla2x00_free_queues(struct qla_hw_data *ha)
{
	struct req_que *req;
	struct rsp_que *rsp;
	int cnt;

399
	for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
400
401
402
		if (!test_bit(cnt, ha->req_qid_map))
			continue;

403
		req = ha->req_q_map[cnt];
404
		qla2x00_free_req_que(ha, req);
405
406
407
	}
	kfree(ha->req_q_map);
	ha->req_q_map = NULL;
408
409

	for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
410
411
412
		if (!test_bit(cnt, ha->rsp_qid_map))
			continue;

413
414
415
416
417
		rsp = ha->rsp_q_map[cnt];
		qla2x00_free_rsp_que(ha, rsp);
	}
	kfree(ha->rsp_q_map);
	ha->rsp_q_map = NULL;
418
419
}

420
421
422
423
424
425
static int qla25xx_setup_mode(struct scsi_qla_host *vha)
{
	uint16_t options = 0;
	int ques, req, ret;
	struct qla_hw_data *ha = vha->hw;

426
	if (!(ha->fw_attributes & BIT_6)) {
427
428
		ql_log(ql_log_warn, vha, 0x00d8,
		    "Firmware is not multi-queue capable.\n");
429
430
		goto fail;
	}
431
432
433
434
435
436
	if (ql2xmultique_tag) {
		/* create a request queue for IO */
		options |= BIT_7;
		req = qla25xx_create_req_que(ha, options, 0, 0, -1,
			QLA_DEFAULT_QUE_QOS);
		if (!req) {
437
438
			ql_log(ql_log_warn, vha, 0x00e0,
			    "Failed to create request queue.\n");
439
440
			goto fail;
		}
441
		ha->wq = alloc_workqueue("qla2xxx_wq", WQ_MEM_RECLAIM, 1);
442
443
444
445
446
		vha->req = ha->req_q_map[req];
		options |= BIT_1;
		for (ques = 1; ques < ha->max_rsp_queues; ques++) {
			ret = qla25xx_create_rsp_que(ha, options, 0, 0, req);
			if (!ret) {
447
448
				ql_log(ql_log_warn, vha, 0x00e8,
				    "Failed to create response queue.\n");
449
450
451
				goto fail2;
			}
		}
452
		ha->flags.cpu_affinity_enabled = 1;
453
		ql_dbg(ql_dbg_multiq, vha, 0xc007,
454
		    "CPU affinity mode enabled, "
455
456
457
		    "no. of response queues:%d no. of request queues:%d.\n",
		    ha->max_rsp_queues, ha->max_req_queues);
		ql_dbg(ql_dbg_init, vha, 0x00e9,
458
		    "CPU affinity mode enabled, "
459
460
		    "no. of response queues:%d no. of request queues:%d.\n",
		    ha->max_rsp_queues, ha->max_req_queues);
461
462
463
464
	}
	return 0;
fail2:
	qla25xx_delete_queues(vha);
465
466
	destroy_workqueue(ha->wq);
	ha->wq = NULL;
467
	vha->req = ha->req_q_map[0];
468
469
fail:
	ha->mqenable = 0;
470
471
472
	kfree(ha->req_q_map);
	kfree(ha->rsp_q_map);
	ha->max_req_queues = ha->max_rsp_queues = 1;
473
474
475
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
476
static char *
477
qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
Linus Torvalds's avatar
Linus Torvalds committed
478
{
479
	struct qla_hw_data *ha = vha->hw;
Linus Torvalds's avatar
Linus Torvalds committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
	static char *pci_bus_modes[] = {
		"33", "66", "100", "133",
	};
	uint16_t pci_bus;

	strcpy(str, "PCI");
	pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
	if (pci_bus) {
		strcat(str, "-X (");
		strcat(str, pci_bus_modes[pci_bus]);
	} else {
		pci_bus = (ha->pci_attr & BIT_8) >> 8;
		strcat(str, " (");
		strcat(str, pci_bus_modes[pci_bus]);
	}
	strcat(str, " MHz)");

	return (str);
}

500
static char *
501
qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
502
503
{
	static char *pci_bus_modes[] = { "33", "66", "100", "133", };
504
	struct qla_hw_data *ha = vha->hw;
505
506
	uint32_t pci_bus;

507
	if (pci_is_pcie(ha->pdev)) {
508
		char lwstr[6];
509
		uint32_t lstat, lspeed, lwidth;
510

511
512
513
		pcie_capability_read_dword(ha->pdev, PCI_EXP_LNKCAP, &lstat);
		lspeed = lstat & PCI_EXP_LNKCAP_SLS;
		lwidth = (lstat & PCI_EXP_LNKCAP_MLW) >> 4;
514
515

		strcpy(str, "PCIe (");
516
517
		switch (lspeed) {
		case 1:
518
			strcat(str, "2.5GT/s ");
519
520
			break;
		case 2:
521
			strcat(str, "5.0GT/s ");
522
523
524
525
526
			break;
		case 3:
			strcat(str, "8.0GT/s ");
			break;
		default:
527
			strcat(str, "<unknown> ");
528
529
			break;
		}
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
		snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
		strcat(str, lwstr);

		return str;
	}

	strcpy(str, "PCI");
	pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
	if (pci_bus == 0 || pci_bus == 8) {
		strcat(str, " (");
		strcat(str, pci_bus_modes[pci_bus >> 3]);
	} else {
		strcat(str, "-X ");
		if (pci_bus & BIT_2)
			strcat(str, "Mode 2");
		else
			strcat(str, "Mode 1");
		strcat(str, " (");
		strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
	}
	strcat(str, " MHz)");

	return str;
}

555
static char *
556
qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
Linus Torvalds's avatar
Linus Torvalds committed
557
558
{
	char un_str[10];
559
	struct qla_hw_data *ha = vha->hw;
560

561
562
	snprintf(str, size, "%d.%02d.%02d ", ha->fw_major_version,
	    ha->fw_minor_version, ha->fw_subminor_version);
Linus Torvalds's avatar
Linus Torvalds committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592

	if (ha->fw_attributes & BIT_9) {
		strcat(str, "FLX");
		return (str);
	}

	switch (ha->fw_attributes & 0xFF) {
	case 0x7:
		strcat(str, "EF");
		break;
	case 0x17:
		strcat(str, "TP");
		break;
	case 0x37:
		strcat(str, "IP");
		break;
	case 0x77:
		strcat(str, "VI");
		break;
	default:
		sprintf(un_str, "(%x)", ha->fw_attributes);
		strcat(str, un_str);
		break;
	}
	if (ha->fw_attributes & 0x100)
		strcat(str, "X");

	return (str);
}

593
static char *
594
qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
595
{
596
	struct qla_hw_data *ha = vha->hw;
597

598
	snprintf(str, size, "%d.%02d.%02d (%x)", ha->fw_major_version,
599
	    ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
600
601
602
	return str;
}

603
604
void
qla2x00_sp_free_dma(void *vha, void *ptr)
605
{
606
607
608
609
	srb_t *sp = (srb_t *)ptr;
	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
	struct qla_hw_data *ha = sp->fcport->vha->hw;
	void *ctx = GET_CMD_CTX_SP(sp);
610

611
612
613
	if (sp->flags & SRB_DMA_VALID) {
		scsi_dma_unmap(cmd);
		sp->flags &= ~SRB_DMA_VALID;
614
	}
615

616
617
618
619
620
621
622
623
	if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
		dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
		    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
		sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
	}

	if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
		/* List assured to be having elements */
624
		qla2x00_clean_dsd_pool(ha, sp, NULL);
625
626
627
628
629
630
631
632
633
634
635
		sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
	}

	if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
		dma_pool_free(ha->dl_dma_pool, ctx,
		    ((struct crc_context *)ctx)->crc_ctx_dma);
		sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
	}

	if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
		struct ct6_dsd *ctx1 = (struct ct6_dsd *)ctx;
636

637
638
639
640
641
642
643
644
645
646
		dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
			ctx1->fcp_cmnd_dma);
		list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
		ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
		ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
		mempool_free(ctx1, ha->ctx_mempool);
		ctx1 = NULL;
	}

	CMD_SP(cmd) = NULL;
647
	qla2x00_rel_sp(sp->fcport->vha, sp);
648
649
}

650
static void
651
652
653
654
655
656
657
658
659
660
661
662
663
qla2x00_sp_compl(void *data, void *ptr, int res)
{
	struct qla_hw_data *ha = (struct qla_hw_data *)data;
	srb_t *sp = (srb_t *)ptr;
	struct scsi_cmnd *cmd = GET_CMD_SP(sp);

	cmd->result = res;

	if (atomic_read(&sp->ref_count) == 0) {
		ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3015,
		    "SP reference-count to ZERO -- sp=%p cmd=%p.\n",
		    sp, GET_CMD_SP(sp));
		if (ql2xextended_error_logging & ql_dbg_io)
664
			WARN_ON(atomic_read(&sp->ref_count) == 0);
665
666
667
668
669
670
671
		return;
	}
	if (!atomic_dec_and_test(&sp->ref_count))
		return;

	qla2x00_sp_free_dma(ha, sp);
	cmd->scsi_done(cmd);
672
673
}

674
675
676
/* If we are SP1 here, we need to still take and release the host_lock as SP1
 * does not have the changes necessary to avoid taking host->host_lock.
 */
Linus Torvalds's avatar
Linus Torvalds committed
677
static int
678
qla2xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
679
{
680
	scsi_qla_host_t *vha = shost_priv(host);
681
	fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
682
	struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
683
684
	struct qla_hw_data *ha = vha->hw;
	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
685
686
687
	srb_t *sp;
	int rval;

688
689
690
691
692
	if (unlikely(test_bit(UNLOADING, &base_vha->dpc_flags))) {
		cmd->result = DID_NO_CONNECT << 16;
		goto qc24_fail_command;
	}

693
	if (ha->flags.eeh_busy) {
694
		if (ha->flags.pci_channel_io_perm_failure) {
695
			ql_dbg(ql_dbg_aer, vha, 0x9010,
696
697
			    "PCI Channel IO permanent failure, exiting "
			    "cmd=%p.\n", cmd);
698
			cmd->result = DID_NO_CONNECT << 16;
699
		} else {
700
			ql_dbg(ql_dbg_aer, vha, 0x9011,
701
			    "EEH_Busy, Requeuing the cmd=%p.\n", cmd);
702
			cmd->result = DID_REQUEUE << 16;
703
		}
704
705
706
		goto qc24_fail_command;
	}

707
708
709
	rval = fc_remote_port_chkready(rport);
	if (rval) {
		cmd->result = rval;
710
		ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3003,
711
712
		    "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
		    cmd, rval);
713
714
715
		goto qc24_fail_command;
	}

716
717
	if (!vha->flags.difdix_supported &&
		scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
718
719
720
			ql_dbg(ql_dbg_io, vha, 0x3004,
			    "DIF Cap not reg, fail DIF capable cmd's:%p.\n",
			    cmd);
721
722
723
			cmd->result = DID_NO_CONNECT << 16;
			goto qc24_fail_command;
	}
724
725
726
727
728
729

	if (!fcport) {
		cmd->result = DID_NO_CONNECT << 16;
		goto qc24_fail_command;
	}

730
731
	if (atomic_read(&fcport->state) != FCS_ONLINE) {
		if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
732
			atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
733
734
735
736
			ql_dbg(ql_dbg_io, vha, 0x3005,
			    "Returning DNC, fcport_state=%d loop_state=%d.\n",
			    atomic_read(&fcport->state),
			    atomic_read(&base_vha->loop_state));
737
738
739
			cmd->result = DID_NO_CONNECT << 16;
			goto qc24_fail_command;
		}
740
		goto qc24_target_busy;
741
742
	}

743
744
745
746
	/*
	 * Return target busy if we've received a non-zero retry_delay_timer
	 * in a FCP_RSP.
	 */
747
748
749
	if (fcport->retry_delay_timestamp == 0) {
		/* retry delay not set */
	} else if (time_after(jiffies, fcport->retry_delay_timestamp))
750
751
752
753
		fcport->retry_delay_timestamp = 0;
	else
		goto qc24_target_busy;

754
	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
755
	if (!sp)
756
		goto qc24_host_busy;
757

758
759
760
761
762
763
764
	sp->u.scmd.cmd = cmd;
	sp->type = SRB_SCSI_CMD;
	atomic_set(&sp->ref_count, 1);
	CMD_SP(cmd) = (void *)sp;
	sp->free = qla2x00_sp_free_dma;
	sp->done = qla2x00_sp_compl;

765
	rval = ha->isp_ops->start_scsi(sp);
766
	if (rval != QLA_SUCCESS) {
767
		ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3013,
768
		    "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
769
		goto qc24_host_busy_free_sp;
770
	}
771
772
773
774

	return 0;

qc24_host_busy_free_sp:
775
	qla2x00_sp_free_dma(ha, sp);
776

777
qc24_host_busy:
778
779
	return SCSI_MLQUEUE_HOST_BUSY;

780
781
782
qc24_target_busy:
	return SCSI_MLQUEUE_TARGET_BUSY;

783
qc24_fail_command:
784
	cmd->scsi_done(cmd);
785
786
787
788

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
789
790
791
792
793
794
795
796
797
798
799
800
801
/*
 * qla2x00_eh_wait_on_command
 *    Waits for the command to be returned by the Firmware for some
 *    max time.
 *
 * Input:
 *    cmd = Scsi Command to wait on.
 *
 * Return:
 *    Not Found : 0
 *    Found : 1
 */
static int
802
qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
803
{
804
#define ABORT_POLLING_PERIOD	1000
805
#define ABORT_WAIT_ITER		((2 * 1000) / (ABORT_POLLING_PERIOD))
806
	unsigned long wait_iter = ABORT_WAIT_ITER;
807
808
	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
	struct qla_hw_data *ha = vha->hw;
809
	int ret = QLA_SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
810

811
	if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
812
813
		ql_dbg(ql_dbg_taskm, vha, 0x8005,
		    "Return:eh_wait.\n");
814
815
816
		return ret;
	}

817
	while (CMD_SP(cmd) && wait_iter--) {
818
		msleep(ABORT_POLLING_PERIOD);
819
820
821
	}
	if (CMD_SP(cmd))
		ret = QLA_FUNCTION_FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
822

823
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
824
825
826
827
}

/*
 * qla2x00_wait_for_hba_online
828
 *    Wait till the HBA is online after going through
Linus Torvalds's avatar
Linus Torvalds committed
829
830
831
832
833
 *    <= MAX_RETRIES_OF_ISP_ABORT  or
 *    finally HBA is disabled ie marked offline
 *
 * Input:
 *     ha - pointer to host adapter structure
834
835
 *
 * Note:
Linus Torvalds's avatar
Linus Torvalds committed
836
837
838
839
840
841
842
 *    Does context switching-Release SPIN_LOCK
 *    (if any) before calling this routine.
 *
 * Return:
 *    Success (Adapter is online) : 0
 *    Failed  (Adapter is offline/disabled) : 1
 */
843
int
844
qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
Linus Torvalds's avatar
Linus Torvalds committed
845
{
846
847
	int		return_status;
	unsigned long	wait_online;
848
849
	struct qla_hw_data *ha = vha->hw;
	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds's avatar
Linus Torvalds committed
850

851
	wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
852
853
854
855
	while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
	    ha->dpc_active) && time_before(jiffies, wait_online)) {
Linus Torvalds's avatar
Linus Torvalds committed
856
857
858

		msleep(1000);
	}
859
	if (base_vha->flags.online)
860
		return_status = QLA_SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
861
862
863
864
865
866
	else
		return_status = QLA_FUNCTION_FAILED;

	return (return_status);
}

867
/*
868
869
 * qla2x00_wait_for_hba_ready
 * Wait till the HBA is ready before doing driver unload
870
871
872
873
874
875
876
877
878
 *
 * Input:
 *     ha - pointer to host adapter structure
 *
 * Note:
 *    Does context switching-Release SPIN_LOCK
 *    (if any) before calling this routine.
 *
 */
879
880
static void
qla2x00_wait_for_hba_ready(scsi_qla_host_t *vha)
881
882
883
{
	struct qla_hw_data *ha = vha->hw;

884
885
886
887
	while (((qla2x00_reset_active(vha)) || ha->dpc_active ||
	    ha->flags.mbox_busy) ||
		test_bit(FX00_RESET_RECOVERY, &vha->dpc_flags) ||
		test_bit(FX00_TARGET_SCAN, &vha->dpc_flags))
888
889
890
		msleep(1000);
}

891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
int
qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
{
	int		return_status;
	unsigned long	wait_reset;
	struct qla_hw_data *ha = vha->hw;
	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);

	wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
	while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
	    ha->dpc_active) && time_before(jiffies, wait_reset)) {

		msleep(1000);

		if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
		    ha->flags.chip_reset_done)
			break;
	}
	if (ha->flags.chip_reset_done)
		return_status = QLA_SUCCESS;
	else
		return_status = QLA_FUNCTION_FAILED;

	return return_status;
}

919
920
921
922
923
924
static void
sp_get(struct srb *sp)
{
	atomic_inc(&sp->ref_count);
}

Linus Torvalds's avatar
Linus Torvalds committed
925
926
927
928
929
930
931
932
933
934
935
936
937
/**************************************************************************
* qla2xxx_eh_abort
*
* Description:
*    The abort function will abort the specified command.
*
* Input:
*    cmd = Linux SCSI command packet to be aborted.
*
* Returns:
*    Either SUCCESS or FAILED.
*
* Note:
938
*    Only return FAILED if command not returned by firmware.
Linus Torvalds's avatar
Linus Torvalds committed
939
**************************************************************************/
940
static int
Linus Torvalds's avatar
Linus Torvalds committed
941
942
qla2xxx_eh_abort(struct scsi_cmnd *cmd)
{
943
	scsi_qla_host_t *vha = shost_priv(cmd->device->host);
944
	srb_t *sp;
945
	int ret;
Hannes Reinecke's avatar
Hannes Reinecke committed
946
947
	unsigned int id;
	uint64_t lun;
948
	unsigned long flags;
949
	int rval, wait = 0;
950
	struct qla_hw_data *ha = vha->hw;
Linus Torvalds's avatar
Linus Torvalds committed
951

952
	if (!CMD_SP(cmd))
953
		return SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
954

955
956
957
958
959
	ret = fc_block_scsi_eh(cmd);
	if (ret != 0)
		return ret;
	ret = SUCCESS;

960
961
	id = cmd->device->id;
	lun = cmd->device->lun;
Linus Torvalds's avatar
Linus Torvalds committed
962

963
	spin_lock_irqsave(&ha->hardware_lock, flags);
964
965
966
967
968
	sp = (srb_t *) CMD_SP(cmd);
	if (!sp) {
		spin_unlock_irqrestore(&ha->hardware_lock, flags);
		return SUCCESS;
	}
Linus Torvalds's avatar
Linus Torvalds committed
969

970
	ql_dbg(ql_dbg_taskm, vha, 0x8002,
971
972
	    "Aborting from RISC nexus=%ld:%d:%llu sp=%p cmd=%p handle=%x\n",
	    vha->host_no, id, lun, sp, cmd, sp->handle);
973

974
975
	/* Get a reference to the sp and drop the lock.*/
	sp_get(sp);
976

977
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
978
979
	rval = ha->isp_ops->abort_command(sp);
	if (rval) {
980
		if (rval == QLA_FUNCTION_PARAMETER_ERROR)
981
			ret = SUCCESS;
982
		else
983
984
			ret = FAILED;

985
		ql_dbg(ql_dbg_taskm, vha, 0x8003,
986
		    "Abort command mbx failed cmd=%p, rval=%x.\n", cmd, rval);
987
	} else {
988
		ql_dbg(ql_dbg_taskm, vha, 0x8004,
989
		    "Abort command mbx success cmd=%p.\n", cmd);
990
991
		wait = 1;
	}
992
993

	spin_lock_irqsave(&ha->hardware_lock, flags);
994
	sp->done(ha, sp, 0);
995
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
996

997
998
999
1000
	/* Did the command return during mailbox execution? */
	if (ret == FAILED && !CMD_SP(cmd))
		ret = SUCCESS;

1001
	/* Wait for the command to be returned. */
1002
	if (wait) {
1003
		if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
1004
			ql_log(ql_log_warn, vha, 0x8006,
1005
			    "Abort handler timed out cmd=%p.\n", cmd);
1006
			ret = FAILED;
1007
		}
Linus Torvalds's avatar
Linus Torvalds committed
1008
1009
	}

1010
	ql_log(ql_log_info, vha, 0x801c,
Hannes Reinecke's avatar
Hannes Reinecke committed
1011
	    "Abort command issued nexus=%ld:%d:%llu --  %d %x.\n",
1012
	    vha->host_no, id, lun, wait, ret);
Linus Torvalds's avatar
Linus Torvalds committed
1013

1014
1015
	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
1016

1017
int
1018
qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
Hannes Reinecke's avatar
Hannes Reinecke committed
1019
	uint64_t l, enum nexus_wait_type type)
1020
{
1021
	int cnt, match, status;
1022
	unsigned long flags;
1023
	struct qla_hw_data *ha = vha->hw;