]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/qla2xxx/qla_os.c
[SCSI] qla2xxx: Fix for a multiqueue bug in CPU affinity mode
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / qla2xxx / qla_os.c
CommitLineData
1da177e4 1/*
fa90c54f 2 * QLogic Fibre Channel HBA Driver
01e58d8e 3 * Copyright (c) 2003-2008 QLogic Corporation
1da177e4 4 *
fa90c54f 5 * See LICENSE.qla2xxx for copyright and licensing details.
1da177e4
LT
6 */
7#include "qla_def.h"
8
9#include <linux/moduleparam.h>
10#include <linux/vmalloc.h>
1da177e4 11#include <linux/delay.h>
39a11240 12#include <linux/kthread.h>
e1e82b6f 13#include <linux/mutex.h>
3420d36c 14#include <linux/kobject.h>
1da177e4
LT
15
16#include <scsi/scsi_tcq.h>
17#include <scsi/scsicam.h>
18#include <scsi/scsi_transport.h>
19#include <scsi/scsi_transport_fc.h>
20
21/*
22 * Driver version
23 */
24char qla2x00_version_str[40];
25
26/*
27 * SRB allocation cache
28 */
e18b890b 29static struct kmem_cache *srb_cachep;
1da177e4 30
1da177e4
LT
31int ql2xlogintimeout = 20;
32module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
33MODULE_PARM_DESC(ql2xlogintimeout,
34 "Login timeout value in seconds.");
35
a7b61842 36int qlport_down_retry;
1da177e4
LT
37module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
38MODULE_PARM_DESC(qlport_down_retry,
900d9f98 39 "Maximum number of command retries to a port that returns "
1da177e4
LT
40 "a PORT-DOWN status.");
41
1da177e4
LT
42int ql2xplogiabsentdevice;
43module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
44MODULE_PARM_DESC(ql2xplogiabsentdevice,
45 "Option to enable PLOGI to devices that are not present after "
900d9f98 46 "a Fabric scan. This is needed for several broken switches. "
1da177e4
LT
47 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
48
1da177e4
LT
49int ql2xloginretrycount = 0;
50module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
51MODULE_PARM_DESC(ql2xloginretrycount,
52 "Specify an alternate value for the NVRAM login retry count.");
53
a7a167bf
AV
54int ql2xallocfwdump = 1;
55module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
56MODULE_PARM_DESC(ql2xallocfwdump,
57 "Option to enable allocation of memory for a firmware dump "
58 "during HBA initialization. Memory allocation requirements "
59 "vary by ISP type. Default is 1 - allocate memory.");
60
11010fec 61int ql2xextended_error_logging;
27d94035 62module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
11010fec 63MODULE_PARM_DESC(ql2xextended_error_logging,
0181944f
AV
64 "Option to enable extended error logging, "
65 "Default is 0 - no logging. 1 - log errors.");
66
1da177e4
LT
67static void qla2x00_free_device(scsi_qla_host_t *);
68
7e47e5ca 69int ql2xfdmienable=1;
cca5335c
AV
70module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
71MODULE_PARM_DESC(ql2xfdmienable,
72 "Enables FDMI registratons "
73 "Default is 0 - no FDMI. 1 - perfom FDMI.");
74
df7baa50
AV
75#define MAX_Q_DEPTH 32
76static int ql2xmaxqdepth = MAX_Q_DEPTH;
77module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
78MODULE_PARM_DESC(ql2xmaxqdepth,
79 "Maximum queue depth to report for target devices.");
80
e5896bd5
AV
81int ql2xiidmaenable=1;
82module_param(ql2xiidmaenable, int, S_IRUGO|S_IRUSR);
83MODULE_PARM_DESC(ql2xiidmaenable,
84 "Enables iIDMA settings "
85 "Default is 1 - perform iIDMA. 0 - no iIDMA.");
86
73208dfd
AC
87int ql2xmaxqueues = 1;
88module_param(ql2xmaxqueues, int, S_IRUGO|S_IRUSR);
89MODULE_PARM_DESC(ql2xmaxqueues,
90 "Enables MQ settings "
91 "Default is 1 for single queue. Set it to number \
92 of queues in MQ mode.");
68ca949c
AC
93
94int ql2xmultique_tag;
95module_param(ql2xmultique_tag, int, S_IRUGO|S_IRUSR);
96MODULE_PARM_DESC(ql2xmultique_tag,
97 "Enables CPU affinity settings for the driver "
98 "Default is 0 for no affinity of request and response IO. "
99 "Set it to 1 to turn on the cpu affinity.");
e337d907
AV
100
101int ql2xfwloadbin;
102module_param(ql2xfwloadbin, int, S_IRUGO|S_IRUSR);
103MODULE_PARM_DESC(ql2xfwloadbin,
104 "Option to specify location from which to load ISP firmware:\n"
105 " 2 -- load firmware via the request_firmware() (hotplug)\n"
106 " interface.\n"
107 " 1 -- load firmware from flash.\n"
108 " 0 -- use default semantics.\n");
109
1da177e4 110/*
fa2a1ce5 111 * SCSI host template entry points
1da177e4
LT
112 */
113static int qla2xxx_slave_configure(struct scsi_device * device);
f4f051eb 114static int qla2xxx_slave_alloc(struct scsi_device *);
1e99e33a
AV
115static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
116static void qla2xxx_scan_start(struct Scsi_Host *);
f4f051eb 117static void qla2xxx_slave_destroy(struct scsi_device *);
a5326f86 118static int qla2xxx_queuecommand(struct scsi_cmnd *cmd,
fca29703 119 void (*fn)(struct scsi_cmnd *));
1da177e4
LT
120static int qla2xxx_eh_abort(struct scsi_cmnd *);
121static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
523ec773 122static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
1da177e4
LT
123static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
124static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
1da177e4 125
e881a172 126static int qla2x00_change_queue_depth(struct scsi_device *, int, int);
ce7e4af7
AV
127static int qla2x00_change_queue_type(struct scsi_device *, int);
128
a5326f86 129struct scsi_host_template qla2xxx_driver_template = {
1da177e4 130 .module = THIS_MODULE,
cb63067a 131 .name = QLA2XXX_DRIVER_NAME,
a5326f86 132 .queuecommand = qla2xxx_queuecommand,
fca29703
AV
133
134 .eh_abort_handler = qla2xxx_eh_abort,
135 .eh_device_reset_handler = qla2xxx_eh_device_reset,
523ec773 136 .eh_target_reset_handler = qla2xxx_eh_target_reset,
fca29703
AV
137 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
138 .eh_host_reset_handler = qla2xxx_eh_host_reset,
139
140 .slave_configure = qla2xxx_slave_configure,
141
142 .slave_alloc = qla2xxx_slave_alloc,
143 .slave_destroy = qla2xxx_slave_destroy,
ed677086
AV
144 .scan_finished = qla2xxx_scan_finished,
145 .scan_start = qla2xxx_scan_start,
ce7e4af7
AV
146 .change_queue_depth = qla2x00_change_queue_depth,
147 .change_queue_type = qla2x00_change_queue_type,
fca29703
AV
148 .this_id = -1,
149 .cmd_per_lun = 3,
150 .use_clustering = ENABLE_CLUSTERING,
151 .sg_tablesize = SG_ALL,
152
153 .max_sectors = 0xFFFF,
afb046e2 154 .shost_attrs = qla2x00_host_attrs,
fca29703
AV
155};
156
1da177e4 157static struct scsi_transport_template *qla2xxx_transport_template = NULL;
2c3dfe3f 158struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
1da177e4 159
1da177e4
LT
160/* TODO Convert to inlines
161 *
162 * Timer routines
163 */
1da177e4 164
2c3dfe3f 165__inline__ void
e315cd28 166qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
1da177e4 167{
e315cd28
AC
168 init_timer(&vha->timer);
169 vha->timer.expires = jiffies + interval * HZ;
170 vha->timer.data = (unsigned long)vha;
171 vha->timer.function = (void (*)(unsigned long))func;
172 add_timer(&vha->timer);
173 vha->timer_active = 1;
1da177e4
LT
174}
175
176static inline void
e315cd28 177qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
1da177e4 178{
e315cd28 179 mod_timer(&vha->timer, jiffies + interval * HZ);
1da177e4
LT
180}
181
a824ebb3 182static __inline__ void
e315cd28 183qla2x00_stop_timer(scsi_qla_host_t *vha)
1da177e4 184{
e315cd28
AC
185 del_timer_sync(&vha->timer);
186 vha->timer_active = 0;
1da177e4
LT
187}
188
1da177e4
LT
189static int qla2x00_do_dpc(void *data);
190
191static void qla2x00_rst_aen(scsi_qla_host_t *);
192
73208dfd
AC
193static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
194 struct req_que **, struct rsp_que **);
e315cd28
AC
195static void qla2x00_mem_free(struct qla_hw_data *);
196static void qla2x00_sp_free_dma(srb_t *);
1da177e4 197
1da177e4 198/* -------------------------------------------------------------------------- */
73208dfd
AC
199static int qla2x00_alloc_queues(struct qla_hw_data *ha)
200{
2afa19a9 201 ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
73208dfd
AC
202 GFP_KERNEL);
203 if (!ha->req_q_map) {
204 qla_printk(KERN_WARNING, ha,
205 "Unable to allocate memory for request queue ptrs\n");
206 goto fail_req_map;
207 }
208
2afa19a9 209 ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
73208dfd
AC
210 GFP_KERNEL);
211 if (!ha->rsp_q_map) {
212 qla_printk(KERN_WARNING, ha,
213 "Unable to allocate memory for response queue ptrs\n");
214 goto fail_rsp_map;
215 }
216 set_bit(0, ha->rsp_qid_map);
217 set_bit(0, ha->req_qid_map);
218 return 1;
219
220fail_rsp_map:
221 kfree(ha->req_q_map);
222 ha->req_q_map = NULL;
223fail_req_map:
224 return -ENOMEM;
225}
226
2afa19a9 227static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
73208dfd 228{
73208dfd
AC
229 if (req && req->ring)
230 dma_free_coherent(&ha->pdev->dev,
231 (req->length + 1) * sizeof(request_t),
232 req->ring, req->dma);
233
234 kfree(req);
235 req = NULL;
236}
237
2afa19a9
AC
238static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
239{
240 if (rsp && rsp->ring)
241 dma_free_coherent(&ha->pdev->dev,
242 (rsp->length + 1) * sizeof(response_t),
243 rsp->ring, rsp->dma);
244
245 kfree(rsp);
246 rsp = NULL;
247}
248
73208dfd
AC
249static void qla2x00_free_queues(struct qla_hw_data *ha)
250{
251 struct req_que *req;
252 struct rsp_que *rsp;
253 int cnt;
254
2afa19a9 255 for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
73208dfd 256 req = ha->req_q_map[cnt];
2afa19a9 257 qla2x00_free_req_que(ha, req);
73208dfd 258 }
73208dfd
AC
259 kfree(ha->req_q_map);
260 ha->req_q_map = NULL;
2afa19a9
AC
261
262 for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
263 rsp = ha->rsp_q_map[cnt];
264 qla2x00_free_rsp_que(ha, rsp);
265 }
266 kfree(ha->rsp_q_map);
267 ha->rsp_q_map = NULL;
73208dfd
AC
268}
269
68ca949c
AC
270static int qla25xx_setup_mode(struct scsi_qla_host *vha)
271{
272 uint16_t options = 0;
273 int ques, req, ret;
274 struct qla_hw_data *ha = vha->hw;
275
7163ea81
AC
276 if (!(ha->fw_attributes & BIT_6)) {
277 qla_printk(KERN_INFO, ha,
278 "Firmware is not multi-queue capable\n");
279 goto fail;
280 }
68ca949c 281 if (ql2xmultique_tag) {
68ca949c
AC
282 /* create a request queue for IO */
283 options |= BIT_7;
284 req = qla25xx_create_req_que(ha, options, 0, 0, -1,
285 QLA_DEFAULT_QUE_QOS);
286 if (!req) {
287 qla_printk(KERN_WARNING, ha,
288 "Can't create request queue\n");
289 goto fail;
290 }
7163ea81 291 ha->wq = create_workqueue("qla2xxx_wq");
68ca949c
AC
292 vha->req = ha->req_q_map[req];
293 options |= BIT_1;
294 for (ques = 1; ques < ha->max_rsp_queues; ques++) {
295 ret = qla25xx_create_rsp_que(ha, options, 0, 0, req);
296 if (!ret) {
297 qla_printk(KERN_WARNING, ha,
298 "Response Queue create failed\n");
299 goto fail2;
300 }
301 }
7163ea81
AC
302 ha->flags.cpu_affinity_enabled = 1;
303
68ca949c
AC
304 DEBUG2(qla_printk(KERN_INFO, ha,
305 "CPU affinity mode enabled, no. of response"
306 " queues:%d, no. of request queues:%d\n",
307 ha->max_rsp_queues, ha->max_req_queues));
308 }
309 return 0;
310fail2:
311 qla25xx_delete_queues(vha);
7163ea81
AC
312 destroy_workqueue(ha->wq);
313 ha->wq = NULL;
68ca949c
AC
314fail:
315 ha->mqenable = 0;
7163ea81
AC
316 kfree(ha->req_q_map);
317 kfree(ha->rsp_q_map);
318 ha->max_req_queues = ha->max_rsp_queues = 1;
68ca949c
AC
319 return 1;
320}
321
1da177e4 322static char *
e315cd28 323qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
1da177e4 324{
e315cd28 325 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
326 static char *pci_bus_modes[] = {
327 "33", "66", "100", "133",
328 };
329 uint16_t pci_bus;
330
331 strcpy(str, "PCI");
332 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
333 if (pci_bus) {
334 strcat(str, "-X (");
335 strcat(str, pci_bus_modes[pci_bus]);
336 } else {
337 pci_bus = (ha->pci_attr & BIT_8) >> 8;
338 strcat(str, " (");
339 strcat(str, pci_bus_modes[pci_bus]);
340 }
341 strcat(str, " MHz)");
342
343 return (str);
344}
345
fca29703 346static char *
e315cd28 347qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
fca29703
AV
348{
349 static char *pci_bus_modes[] = { "33", "66", "100", "133", };
e315cd28 350 struct qla_hw_data *ha = vha->hw;
fca29703
AV
351 uint32_t pci_bus;
352 int pcie_reg;
353
354 pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
355 if (pcie_reg) {
356 char lwstr[6];
357 uint16_t pcie_lstat, lspeed, lwidth;
358
359 pcie_reg += 0x12;
360 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
361 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
362 lwidth = (pcie_lstat &
363 (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
364
365 strcpy(str, "PCIe (");
366 if (lspeed == 1)
c87a0d8c 367 strcat(str, "2.5GT/s ");
c3a2f0df 368 else if (lspeed == 2)
c87a0d8c 369 strcat(str, "5.0GT/s ");
fca29703
AV
370 else
371 strcat(str, "<unknown> ");
372 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
373 strcat(str, lwstr);
374
375 return str;
376 }
377
378 strcpy(str, "PCI");
379 pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
380 if (pci_bus == 0 || pci_bus == 8) {
381 strcat(str, " (");
382 strcat(str, pci_bus_modes[pci_bus >> 3]);
383 } else {
384 strcat(str, "-X ");
385 if (pci_bus & BIT_2)
386 strcat(str, "Mode 2");
387 else
388 strcat(str, "Mode 1");
389 strcat(str, " (");
390 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
391 }
392 strcat(str, " MHz)");
393
394 return str;
395}
396
e5f82ab8 397static char *
e315cd28 398qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str)
1da177e4
LT
399{
400 char un_str[10];
e315cd28 401 struct qla_hw_data *ha = vha->hw;
fa2a1ce5 402
1da177e4
LT
403 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
404 ha->fw_minor_version,
405 ha->fw_subminor_version);
406
407 if (ha->fw_attributes & BIT_9) {
408 strcat(str, "FLX");
409 return (str);
410 }
411
412 switch (ha->fw_attributes & 0xFF) {
413 case 0x7:
414 strcat(str, "EF");
415 break;
416 case 0x17:
417 strcat(str, "TP");
418 break;
419 case 0x37:
420 strcat(str, "IP");
421 break;
422 case 0x77:
423 strcat(str, "VI");
424 break;
425 default:
426 sprintf(un_str, "(%x)", ha->fw_attributes);
427 strcat(str, un_str);
428 break;
429 }
430 if (ha->fw_attributes & 0x100)
431 strcat(str, "X");
432
433 return (str);
434}
435
e5f82ab8 436static char *
e315cd28 437qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str)
fca29703 438{
e315cd28 439 struct qla_hw_data *ha = vha->hw;
f0883ac6 440
3a03eb79
AV
441 sprintf(str, "%d.%02d.%02d (%x)", ha->fw_major_version,
442 ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
fca29703 443 return str;
fca29703
AV
444}
445
446static inline srb_t *
e315cd28 447qla2x00_get_new_sp(scsi_qla_host_t *vha, fc_port_t *fcport,
fca29703
AV
448 struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
449{
450 srb_t *sp;
e315cd28 451 struct qla_hw_data *ha = vha->hw;
fca29703
AV
452
453 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
454 if (!sp)
455 return sp;
456
fca29703
AV
457 sp->fcport = fcport;
458 sp->cmd = cmd;
459 sp->flags = 0;
460 CMD_SP(cmd) = (void *)sp;
461 cmd->scsi_done = done;
cf53b069 462 sp->ctx = NULL;
fca29703
AV
463
464 return sp;
465}
466
1da177e4 467static int
a5326f86 468qla2xxx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
fca29703 469{
e315cd28 470 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
fca29703 471 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
19a7b4ae 472 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
e315cd28
AC
473 struct qla_hw_data *ha = vha->hw;
474 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
fca29703
AV
475 srb_t *sp;
476 int rval;
477
e315cd28 478 if (unlikely(pci_channel_offline(ha->pdev))) {
b9b12f73
SJ
479 if (ha->pdev->error_state == pci_channel_io_frozen)
480 cmd->result = DID_REQUEUE << 16;
481 else
482 cmd->result = DID_NO_CONNECT << 16;
14e660e6
SJ
483 goto qc24_fail_command;
484 }
485
19a7b4ae
JSEC
486 rval = fc_remote_port_chkready(rport);
487 if (rval) {
488 cmd->result = rval;
fca29703
AV
489 goto qc24_fail_command;
490 }
491
387f96b4 492 /* Close window on fcport/rport state-transitioning. */
7b594131
MC
493 if (fcport->drport)
494 goto qc24_target_busy;
387f96b4 495
fca29703
AV
496 if (atomic_read(&fcport->state) != FCS_ONLINE) {
497 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
e315cd28 498 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
fca29703
AV
499 cmd->result = DID_NO_CONNECT << 16;
500 goto qc24_fail_command;
501 }
7b594131 502 goto qc24_target_busy;
fca29703
AV
503 }
504
e315cd28 505 spin_unlock_irq(vha->host->host_lock);
fca29703 506
e315cd28 507 sp = qla2x00_get_new_sp(base_vha, fcport, cmd, done);
fca29703
AV
508 if (!sp)
509 goto qc24_host_busy_lock;
510
e315cd28 511 rval = ha->isp_ops->start_scsi(sp);
fca29703
AV
512 if (rval != QLA_SUCCESS)
513 goto qc24_host_busy_free_sp;
514
e315cd28 515 spin_lock_irq(vha->host->host_lock);
fca29703
AV
516
517 return 0;
518
519qc24_host_busy_free_sp:
e315cd28
AC
520 qla2x00_sp_free_dma(sp);
521 mempool_free(sp, ha->srb_mempool);
fca29703
AV
522
523qc24_host_busy_lock:
e315cd28 524 spin_lock_irq(vha->host->host_lock);
fca29703
AV
525 return SCSI_MLQUEUE_HOST_BUSY;
526
7b594131
MC
527qc24_target_busy:
528 return SCSI_MLQUEUE_TARGET_BUSY;
529
fca29703
AV
530qc24_fail_command:
531 done(cmd);
532
533 return 0;
534}
535
536
1da177e4
LT
537/*
538 * qla2x00_eh_wait_on_command
539 * Waits for the command to be returned by the Firmware for some
540 * max time.
541 *
542 * Input:
1da177e4 543 * cmd = Scsi Command to wait on.
1da177e4
LT
544 *
545 * Return:
546 * Not Found : 0
547 * Found : 1
548 */
549static int
e315cd28 550qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
1da177e4 551{
fe74c71f
AV
552#define ABORT_POLLING_PERIOD 1000
553#define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
f4f051eb
AV
554 unsigned long wait_iter = ABORT_WAIT_ITER;
555 int ret = QLA_SUCCESS;
1da177e4 556
d970432c 557 while (CMD_SP(cmd) && wait_iter--) {
fe74c71f 558 msleep(ABORT_POLLING_PERIOD);
f4f051eb
AV
559 }
560 if (CMD_SP(cmd))
561 ret = QLA_FUNCTION_FAILED;
1da177e4 562
f4f051eb 563 return ret;
1da177e4
LT
564}
565
566/*
567 * qla2x00_wait_for_hba_online
fa2a1ce5 568 * Wait till the HBA is online after going through
1da177e4
LT
569 * <= MAX_RETRIES_OF_ISP_ABORT or
570 * finally HBA is disabled ie marked offline
571 *
572 * Input:
573 * ha - pointer to host adapter structure
fa2a1ce5
AV
574 *
575 * Note:
1da177e4
LT
576 * Does context switching-Release SPIN_LOCK
577 * (if any) before calling this routine.
578 *
579 * Return:
580 * Success (Adapter is online) : 0
581 * Failed (Adapter is offline/disabled) : 1
582 */
854165f4 583int
e315cd28 584qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
1da177e4 585{
fca29703
AV
586 int return_status;
587 unsigned long wait_online;
e315cd28
AC
588 struct qla_hw_data *ha = vha->hw;
589 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 590
fa2a1ce5 591 wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
e315cd28
AC
592 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
593 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
594 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
595 ha->dpc_active) && time_before(jiffies, wait_online)) {
1da177e4
LT
596
597 msleep(1000);
598 }
e315cd28 599 if (base_vha->flags.online)
fa2a1ce5 600 return_status = QLA_SUCCESS;
1da177e4
LT
601 else
602 return_status = QLA_FUNCTION_FAILED;
603
1da177e4
LT
604 return (return_status);
605}
606
2533cf67
LC
607int
608qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
609{
610 int return_status;
611 unsigned long wait_reset;
612 struct qla_hw_data *ha = vha->hw;
613 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
614
615 wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
616 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
617 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
618 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
619 ha->dpc_active) && time_before(jiffies, wait_reset)) {
620
621 msleep(1000);
622
623 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
624 ha->flags.chip_reset_done)
625 break;
626 }
627 if (ha->flags.chip_reset_done)
628 return_status = QLA_SUCCESS;
629 else
630 return_status = QLA_FUNCTION_FAILED;
631
632 return return_status;
633}
634
1da177e4
LT
635/*
636 * qla2x00_wait_for_loop_ready
637 * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
fa2a1ce5 638 * to be in LOOP_READY state.
1da177e4
LT
639 * Input:
640 * ha - pointer to host adapter structure
fa2a1ce5
AV
641 *
642 * Note:
1da177e4
LT
643 * Does context switching-Release SPIN_LOCK
644 * (if any) before calling this routine.
fa2a1ce5 645 *
1da177e4
LT
646 *
647 * Return:
648 * Success (LOOP_READY) : 0
649 * Failed (LOOP_NOT_READY) : 1
650 */
fa2a1ce5 651static inline int
e315cd28 652qla2x00_wait_for_loop_ready(scsi_qla_host_t *vha)
1da177e4
LT
653{
654 int return_status = QLA_SUCCESS;
655 unsigned long loop_timeout ;
e315cd28
AC
656 struct qla_hw_data *ha = vha->hw;
657 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4
LT
658
659 /* wait for 5 min at the max for loop to be ready */
fa2a1ce5 660 loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1da177e4 661
e315cd28
AC
662 while ((!atomic_read(&base_vha->loop_down_timer) &&
663 atomic_read(&base_vha->loop_state) == LOOP_DOWN) ||
664 atomic_read(&base_vha->loop_state) != LOOP_READY) {
665 if (atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
57680080
RA
666 return_status = QLA_FUNCTION_FAILED;
667 break;
668 }
1da177e4
LT
669 msleep(1000);
670 if (time_after_eq(jiffies, loop_timeout)) {
671 return_status = QLA_FUNCTION_FAILED;
672 break;
673 }
674 }
fa2a1ce5 675 return (return_status);
1da177e4
LT
676}
677
5f3a9a20
SJ
678void
679qla2x00_abort_fcport_cmds(fc_port_t *fcport)
680{
2afa19a9 681 int cnt;
5f3a9a20
SJ
682 unsigned long flags;
683 srb_t *sp;
e315cd28
AC
684 scsi_qla_host_t *vha = fcport->vha;
685 struct qla_hw_data *ha = vha->hw;
73208dfd 686 struct req_que *req;
5f3a9a20 687
e315cd28 688 spin_lock_irqsave(&ha->hardware_lock, flags);
2afa19a9
AC
689 req = vha->req;
690 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
691 sp = req->outstanding_cmds[cnt];
692 if (!sp)
693 continue;
694 if (sp->fcport != fcport)
5f3a9a20 695 continue;
cf53b069
AV
696 if (sp->ctx)
697 continue;
5f3a9a20 698
2afa19a9
AC
699 spin_unlock_irqrestore(&ha->hardware_lock, flags);
700 if (ha->isp_ops->abort_command(sp)) {
701 DEBUG2(qla_printk(KERN_WARNING, ha,
702 "Abort failed -- %lx\n",
703 sp->cmd->serial_number));
704 } else {
705 if (qla2x00_eh_wait_on_command(sp->cmd) !=
706 QLA_SUCCESS)
5f3a9a20 707 DEBUG2(qla_printk(KERN_WARNING, ha,
2afa19a9 708 "Abort failed while waiting -- %lx\n",
73208dfd 709 sp->cmd->serial_number));
5f3a9a20 710 }
2afa19a9 711 spin_lock_irqsave(&ha->hardware_lock, flags);
5f3a9a20 712 }
e315cd28 713 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5f3a9a20
SJ
714}
715
1da177e4
LT
716/**************************************************************************
717* qla2xxx_eh_abort
718*
719* Description:
720* The abort function will abort the specified command.
721*
722* Input:
723* cmd = Linux SCSI command packet to be aborted.
724*
725* Returns:
726* Either SUCCESS or FAILED.
727*
728* Note:
2ea00202 729* Only return FAILED if command not returned by firmware.
1da177e4 730**************************************************************************/
e5f82ab8 731static int
1da177e4
LT
732qla2xxx_eh_abort(struct scsi_cmnd *cmd)
733{
e315cd28 734 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
f4f051eb 735 srb_t *sp;
17d98630 736 int ret, i;
f4f051eb
AV
737 unsigned int id, lun;
738 unsigned long serial;
18e144d3 739 unsigned long flags;
2ea00202 740 int wait = 0;
e315cd28 741 struct qla_hw_data *ha = vha->hw;
67c2e93a 742 struct req_que *req = vha->req;
17d98630 743 srb_t *spt;
1da177e4 744
65d430fa 745 fc_block_scsi_eh(cmd);
07db5183 746
f4f051eb 747 if (!CMD_SP(cmd))
2ea00202 748 return SUCCESS;
1da177e4 749
2ea00202 750 ret = SUCCESS;
1da177e4 751
f4f051eb
AV
752 id = cmd->device->id;
753 lun = cmd->device->lun;
754 serial = cmd->serial_number;
17d98630
AC
755 spt = (srb_t *) CMD_SP(cmd);
756 if (!spt)
757 return SUCCESS;
1da177e4 758
f4f051eb 759 /* Check active list for command command. */
e315cd28 760 spin_lock_irqsave(&ha->hardware_lock, flags);
17d98630
AC
761 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
762 sp = req->outstanding_cmds[i];
1da177e4 763
17d98630
AC
764 if (sp == NULL)
765 continue;
cf53b069
AV
766 if (sp->ctx)
767 continue;
17d98630
AC
768 if (sp->cmd != cmd)
769 continue;
1da177e4 770
17d98630
AC
771 DEBUG2(printk("%s(%ld): aborting sp %p from RISC."
772 " pid=%ld.\n", __func__, vha->host_no, sp, serial));
773
774 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2afa19a9 775 if (ha->isp_ops->abort_command(sp)) {
17d98630
AC
776 DEBUG2(printk("%s(%ld): abort_command "
777 "mbx failed.\n", __func__, vha->host_no));
2ac4b64f 778 ret = FAILED;
17d98630
AC
779 } else {
780 DEBUG3(printk("%s(%ld): abort_command "
781 "mbx success.\n", __func__, vha->host_no));
782 wait = 1;
73208dfd 783 }
17d98630
AC
784 spin_lock_irqsave(&ha->hardware_lock, flags);
785 break;
f4f051eb 786 }
e315cd28 787 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4 788
f4f051eb 789 /* Wait for the command to be returned. */
2ea00202 790 if (wait) {
e315cd28 791 if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
fa2a1ce5 792 qla_printk(KERN_ERR, ha,
f4f051eb 793 "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
e315cd28 794 "%x.\n", vha->host_no, id, lun, serial, ret);
2ea00202 795 ret = FAILED;
f4f051eb 796 }
1da177e4 797 }
1da177e4 798
fa2a1ce5 799 qla_printk(KERN_INFO, ha,
2ea00202 800 "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
e315cd28 801 vha->host_no, id, lun, wait, serial, ret);
1da177e4 802
f4f051eb
AV
803 return ret;
804}
1da177e4 805
523ec773
AV
806enum nexus_wait_type {
807 WAIT_HOST = 0,
808 WAIT_TARGET,
809 WAIT_LUN,
810};
811
f4f051eb 812static int
e315cd28 813qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
17d98630 814 unsigned int l, srb_t *sp, enum nexus_wait_type type)
f4f051eb 815{
17d98630 816 int cnt, match, status;
18e144d3 817 unsigned long flags;
e315cd28 818 struct qla_hw_data *ha = vha->hw;
73208dfd 819 struct req_que *req;
1da177e4 820
523ec773 821 status = QLA_SUCCESS;
17d98630
AC
822 if (!sp)
823 return status;
824
e315cd28 825 spin_lock_irqsave(&ha->hardware_lock, flags);
67c2e93a 826 req = vha->req;
17d98630
AC
827 for (cnt = 1; status == QLA_SUCCESS &&
828 cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
829 sp = req->outstanding_cmds[cnt];
830 if (!sp)
523ec773 831 continue;
cf53b069
AV
832 if (sp->ctx)
833 continue;
17d98630
AC
834 if (vha->vp_idx != sp->fcport->vha->vp_idx)
835 continue;
836 match = 0;
837 switch (type) {
838 case WAIT_HOST:
839 match = 1;
840 break;
841 case WAIT_TARGET:
842 match = sp->cmd->device->id == t;
843 break;
844 case WAIT_LUN:
845 match = (sp->cmd->device->id == t &&
846 sp->cmd->device->lun == l);
847 break;
73208dfd 848 }
17d98630
AC
849 if (!match)
850 continue;
851
852 spin_unlock_irqrestore(&ha->hardware_lock, flags);
853 status = qla2x00_eh_wait_on_command(sp->cmd);
854 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4 855 }
e315cd28 856 spin_unlock_irqrestore(&ha->hardware_lock, flags);
523ec773
AV
857
858 return status;
1da177e4
LT
859}
860
523ec773
AV
861static char *reset_errors[] = {
862 "HBA not online",
863 "HBA not ready",
864 "Task management failed",
865 "Waiting for command completions",
866};
1da177e4 867
e5f82ab8 868static int
523ec773 869__qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
2afa19a9 870 struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, unsigned int, int))
1da177e4 871{
e315cd28 872 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 873 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
523ec773 874 int err;
1da177e4 875
65d430fa 876 fc_block_scsi_eh(cmd);
07db5183 877
b0328bee 878 if (!fcport)
523ec773 879 return FAILED;
1da177e4 880
e315cd28
AC
881 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET ISSUED.\n",
882 vha->host_no, cmd->device->id, cmd->device->lun, name);
1da177e4 883
523ec773 884 err = 0;
e315cd28 885 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
523ec773
AV
886 goto eh_reset_failed;
887 err = 1;
e315cd28 888 if (qla2x00_wait_for_loop_ready(vha) != QLA_SUCCESS)
523ec773
AV
889 goto eh_reset_failed;
890 err = 2;
2afa19a9
AC
891 if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
892 != QLA_SUCCESS)
523ec773
AV
893 goto eh_reset_failed;
894 err = 3;
e315cd28 895 if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
17d98630 896 cmd->device->lun, (srb_t *) CMD_SP(cmd), type) != QLA_SUCCESS)
523ec773
AV
897 goto eh_reset_failed;
898
e315cd28
AC
899 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET SUCCEEDED.\n",
900 vha->host_no, cmd->device->id, cmd->device->lun, name);
523ec773
AV
901
902 return SUCCESS;
903
904 eh_reset_failed:
e315cd28
AC
905 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET FAILED: %s.\n"
906 , vha->host_no, cmd->device->id, cmd->device->lun, name,
523ec773
AV
907 reset_errors[err]);
908 return FAILED;
909}
1da177e4 910
523ec773
AV
911static int
912qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
913{
e315cd28
AC
914 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
915 struct qla_hw_data *ha = vha->hw;
1da177e4 916
523ec773
AV
917 return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
918 ha->isp_ops->lun_reset);
1da177e4
LT
919}
920
1da177e4 921static int
523ec773 922qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1da177e4 923{
e315cd28
AC
924 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
925 struct qla_hw_data *ha = vha->hw;
1da177e4 926
523ec773
AV
927 return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
928 ha->isp_ops->target_reset);
1da177e4
LT
929}
930
1da177e4
LT
931/**************************************************************************
932* qla2xxx_eh_bus_reset
933*
934* Description:
935* The bus reset function will reset the bus and abort any executing
936* commands.
937*
938* Input:
939* cmd = Linux SCSI command packet of the command that cause the
940* bus reset.
941*
942* Returns:
943* SUCCESS/FAILURE (defined as macro in scsi.h).
944*
945**************************************************************************/
e5f82ab8 946static int
1da177e4
LT
947qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
948{
e315cd28 949 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 950 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
2c3dfe3f 951 int ret = FAILED;
f4f051eb
AV
952 unsigned int id, lun;
953 unsigned long serial;
17d98630 954 srb_t *sp = (srb_t *) CMD_SP(cmd);
f4f051eb 955
65d430fa 956 fc_block_scsi_eh(cmd);
07db5183 957
f4f051eb
AV
958 id = cmd->device->id;
959 lun = cmd->device->lun;
960 serial = cmd->serial_number;
1da177e4 961
b0328bee 962 if (!fcport)
f4f051eb 963 return ret;
1da177e4 964
e315cd28 965 qla_printk(KERN_INFO, vha->hw,
749af3d5 966 "scsi(%ld:%d:%d): BUS RESET ISSUED.\n", vha->host_no, id, lun);
1da177e4 967
e315cd28 968 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1da177e4 969 DEBUG2(printk("%s failed:board disabled\n",__func__));
f4f051eb 970 goto eh_bus_reset_done;
1da177e4
LT
971 }
972
e315cd28
AC
973 if (qla2x00_wait_for_loop_ready(vha) == QLA_SUCCESS) {
974 if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
f4f051eb 975 ret = SUCCESS;
1da177e4 976 }
f4f051eb
AV
977 if (ret == FAILED)
978 goto eh_bus_reset_done;
1da177e4 979
9a41a62b 980 /* Flush outstanding commands. */
17d98630 981 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) !=
523ec773 982 QLA_SUCCESS)
9a41a62b 983 ret = FAILED;
1da177e4 984
f4f051eb 985eh_bus_reset_done:
e315cd28 986 qla_printk(KERN_INFO, vha->hw, "%s: reset %s\n", __func__,
f4f051eb 987 (ret == FAILED) ? "failed" : "succeded");
1da177e4 988
f4f051eb 989 return ret;
1da177e4
LT
990}
991
992/**************************************************************************
993* qla2xxx_eh_host_reset
994*
995* Description:
996* The reset function will reset the Adapter.
997*
998* Input:
999* cmd = Linux SCSI command packet of the command that cause the
1000* adapter reset.
1001*
1002* Returns:
1003* Either SUCCESS or FAILED.
1004*
1005* Note:
1006**************************************************************************/
e5f82ab8 1007static int
1da177e4
LT
1008qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1009{
e315cd28 1010 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 1011 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
e315cd28 1012 struct qla_hw_data *ha = vha->hw;
2c3dfe3f 1013 int ret = FAILED;
f4f051eb
AV
1014 unsigned int id, lun;
1015 unsigned long serial;
17d98630 1016 srb_t *sp = (srb_t *) CMD_SP(cmd);
e315cd28 1017 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 1018
65d430fa 1019 fc_block_scsi_eh(cmd);
07db5183 1020
f4f051eb
AV
1021 id = cmd->device->id;
1022 lun = cmd->device->lun;
1023 serial = cmd->serial_number;
1024
b0328bee 1025 if (!fcport)
f4f051eb 1026 return ret;
1da177e4 1027
1da177e4 1028 qla_printk(KERN_INFO, ha,
e315cd28 1029 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", vha->host_no, id, lun);
1da177e4 1030
e315cd28 1031 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
f4f051eb 1032 goto eh_host_reset_lock;
1da177e4
LT
1033
1034 /*
1035 * Fixme-may be dpc thread is active and processing
fa2a1ce5 1036 * loop_resync,so wait a while for it to
1da177e4
LT
1037 * be completed and then issue big hammer.Otherwise
1038 * it may cause I/O failure as big hammer marks the
1039 * devices as lost kicking of the port_down_timer
1040 * while dpc is stuck for the mailbox to complete.
1041 */
e315cd28
AC
1042 qla2x00_wait_for_loop_ready(vha);
1043 if (vha != base_vha) {
1044 if (qla2x00_vp_abort_isp(vha))
f4f051eb 1045 goto eh_host_reset_lock;
e315cd28 1046 } else {
68ca949c
AC
1047 if (ha->wq)
1048 flush_workqueue(ha->wq);
1049
e315cd28
AC
1050 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1051 if (qla2x00_abort_isp(base_vha)) {
1052 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1053 /* failed. schedule dpc to try */
1054 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1055
1056 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
1057 goto eh_host_reset_lock;
1058 }
1059 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
fa2a1ce5 1060 }
1da177e4 1061
e315cd28 1062 /* Waiting for command to be returned to OS.*/
17d98630 1063 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) ==
e315cd28 1064 QLA_SUCCESS)
f4f051eb 1065 ret = SUCCESS;
1da177e4 1066
f4f051eb 1067eh_host_reset_lock:
f4f051eb
AV
1068 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1069 (ret == FAILED) ? "failed" : "succeded");
1da177e4 1070
f4f051eb
AV
1071 return ret;
1072}
1da177e4
LT
1073
1074/*
1075* qla2x00_loop_reset
1076* Issue loop reset.
1077*
1078* Input:
1079* ha = adapter block pointer.
1080*
1081* Returns:
1082* 0 = success
1083*/
a4722cf2 1084int
e315cd28 1085qla2x00_loop_reset(scsi_qla_host_t *vha)
1da177e4 1086{
0c8c39af 1087 int ret;
bdf79621 1088 struct fc_port *fcport;
e315cd28 1089 struct qla_hw_data *ha = vha->hw;
1da177e4 1090
0d6e61bc 1091 if (ha->flags.enable_lip_full_login && !IS_QLA81XX(ha)) {
e315cd28 1092 ret = qla2x00_full_login_lip(vha);
0c8c39af 1093 if (ret != QLA_SUCCESS) {
749af3d5 1094 DEBUG2_3(printk("%s(%ld): failed: "
e315cd28 1095 "full_login_lip=%d.\n", __func__, vha->host_no,
0c8c39af 1096 ret));
749af3d5
AC
1097 }
1098 atomic_set(&vha->loop_state, LOOP_DOWN);
1099 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1100 qla2x00_mark_all_devices_lost(vha, 0);
1101 qla2x00_wait_for_loop_ready(vha);
0c8c39af
AV
1102 }
1103
0d6e61bc 1104 if (ha->flags.enable_lip_reset) {
e315cd28 1105 ret = qla2x00_lip_reset(vha);
0c8c39af 1106 if (ret != QLA_SUCCESS) {
749af3d5 1107 DEBUG2_3(printk("%s(%ld): failed: "
e315cd28
AC
1108 "lip_reset=%d.\n", __func__, vha->host_no, ret));
1109 } else
1110 qla2x00_wait_for_loop_ready(vha);
1da177e4
LT
1111 }
1112
0c8c39af 1113 if (ha->flags.enable_target_reset) {
e315cd28 1114 list_for_each_entry(fcport, &vha->vp_fcports, list) {
bdf79621 1115 if (fcport->port_type != FCT_TARGET)
1da177e4
LT
1116 continue;
1117
2afa19a9 1118 ret = ha->isp_ops->target_reset(fcport, 0, 0);
0c8c39af
AV
1119 if (ret != QLA_SUCCESS) {
1120 DEBUG2_3(printk("%s(%ld): bus_reset failed: "
1121 "target_reset=%d d_id=%x.\n", __func__,
e315cd28 1122 vha->host_no, ret, fcport->d_id.b24));
0c8c39af 1123 }
1da177e4
LT
1124 }
1125 }
1da177e4 1126 /* Issue marker command only when we are going to start the I/O */
e315cd28 1127 vha->marker_needed = 1;
1da177e4 1128
0c8c39af 1129 return QLA_SUCCESS;
1da177e4
LT
1130}
1131
df4bf0bb 1132void
e315cd28 1133qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
df4bf0bb 1134{
73208dfd 1135 int que, cnt;
df4bf0bb
AV
1136 unsigned long flags;
1137 srb_t *sp;
ac280b67 1138 struct srb_ctx *ctx;
e315cd28 1139 struct qla_hw_data *ha = vha->hw;
73208dfd 1140 struct req_que *req;
df4bf0bb
AV
1141
1142 spin_lock_irqsave(&ha->hardware_lock, flags);
2afa19a9 1143 for (que = 0; que < ha->max_req_queues; que++) {
29bdccbe 1144 req = ha->req_q_map[que];
73208dfd
AC
1145 if (!req)
1146 continue;
1147 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1148 sp = req->outstanding_cmds[cnt];
e612d465 1149 if (sp) {
73208dfd 1150 req->outstanding_cmds[cnt] = NULL;
ac280b67
AV
1151 if (!sp->ctx) {
1152 sp->cmd->result = res;
1153 qla2x00_sp_compl(ha, sp);
1154 } else {
1155 ctx = sp->ctx;
1156 del_timer_sync(&ctx->timer);
1157 ctx->free(sp);
1158 }
73208dfd 1159 }
df4bf0bb
AV
1160 }
1161 }
1162 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1163}
1164
f4f051eb
AV
1165static int
1166qla2xxx_slave_alloc(struct scsi_device *sdev)
1da177e4 1167{
bdf79621 1168 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1da177e4 1169
19a7b4ae 1170 if (!rport || fc_remote_port_chkready(rport))
f4f051eb 1171 return -ENXIO;
bdf79621 1172
19a7b4ae 1173 sdev->hostdata = *(fc_port_t **)rport->dd_data;
1da177e4 1174
f4f051eb
AV
1175 return 0;
1176}
1da177e4 1177
f4f051eb
AV
1178static int
1179qla2xxx_slave_configure(struct scsi_device *sdev)
1180{
e315cd28
AC
1181 scsi_qla_host_t *vha = shost_priv(sdev->host);
1182 struct qla_hw_data *ha = vha->hw;
8482e118 1183 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
8474f3a0 1184 fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2afa19a9 1185 struct req_que *req = vha->req;
8482e118 1186
f4f051eb 1187 if (sdev->tagged_supported)
73208dfd 1188 scsi_activate_tcq(sdev, req->max_q_depth);
f4f051eb 1189 else
73208dfd 1190 scsi_deactivate_tcq(sdev, req->max_q_depth);
1da177e4 1191
85821c90 1192 rport->dev_loss_tmo = ha->port_down_retry_count;
8474f3a0
SV
1193 if (sdev->type == TYPE_TAPE)
1194 fcport->flags |= FCF_TAPE_PRESENT;
8482e118 1195
f4f051eb
AV
1196 return 0;
1197}
1da177e4 1198
f4f051eb
AV
1199static void
1200qla2xxx_slave_destroy(struct scsi_device *sdev)
1201{
1202 sdev->hostdata = NULL;
1da177e4
LT
1203}
1204
c45dd305
GM
1205static void qla2x00_handle_queue_full(struct scsi_device *sdev, int qdepth)
1206{
1207 fc_port_t *fcport = (struct fc_port *) sdev->hostdata;
1208
1209 if (!scsi_track_queue_full(sdev, qdepth))
1210 return;
1211
1212 DEBUG2(qla_printk(KERN_INFO, fcport->vha->hw,
1213 "scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
1214 fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
1215 sdev->queue_depth));
1216}
1217
1218static void qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, int qdepth)
1219{
1220 fc_port_t *fcport = sdev->hostdata;
1221 struct scsi_qla_host *vha = fcport->vha;
1222 struct qla_hw_data *ha = vha->hw;
1223 struct req_que *req = NULL;
1224
1225 req = vha->req;
1226 if (!req)
1227 return;
1228
1229 if (req->max_q_depth <= sdev->queue_depth || req->max_q_depth < qdepth)
1230 return;
1231
1232 if (sdev->ordered_tags)
1233 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, qdepth);
1234 else
1235 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, qdepth);
1236
1237 DEBUG2(qla_printk(KERN_INFO, ha,
1238 "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
1239 fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
1240 sdev->queue_depth));
1241}
1242
ce7e4af7 1243static int
e881a172 1244qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
ce7e4af7 1245{
c45dd305
GM
1246 switch (reason) {
1247 case SCSI_QDEPTH_DEFAULT:
1248 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1249 break;
1250 case SCSI_QDEPTH_QFULL:
1251 qla2x00_handle_queue_full(sdev, qdepth);
1252 break;
1253 case SCSI_QDEPTH_RAMP_UP:
1254 qla2x00_adjust_sdev_qdepth_up(sdev, qdepth);
1255 break;
1256 default:
1257 return EOPNOTSUPP;
1258 }
e881a172 1259
ce7e4af7
AV
1260 return sdev->queue_depth;
1261}
1262
1263static int
1264qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1265{
1266 if (sdev->tagged_supported) {
1267 scsi_set_tag_type(sdev, tag_type);
1268 if (tag_type)
1269 scsi_activate_tcq(sdev, sdev->queue_depth);
1270 else
1271 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1272 } else
1273 tag_type = 0;
1274
1275 return tag_type;
1276}
1277
1da177e4
LT
1278/**
1279 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1280 * @ha: HA context
1281 *
1282 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1283 * supported addressing method.
1284 */
1285static void
53303c42 1286qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1da177e4 1287{
7524f9b9 1288 /* Assume a 32bit DMA mask. */
1da177e4 1289 ha->flags.enable_64bit_addressing = 0;
1da177e4 1290
6a35528a 1291 if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
7524f9b9
AV
1292 /* Any upper-dword bits set? */
1293 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
6a35528a 1294 !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
7524f9b9 1295 /* Ok, a 64bit DMA mask is applicable. */
1da177e4 1296 ha->flags.enable_64bit_addressing = 1;
fd34f556
AV
1297 ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1298 ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
7524f9b9 1299 return;
1da177e4 1300 }
1da177e4 1301 }
7524f9b9 1302
284901a9
YH
1303 dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1304 pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1da177e4
LT
1305}
1306
fd34f556 1307static void
e315cd28 1308qla2x00_enable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1309{
1310 unsigned long flags = 0;
1311 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1312
1313 spin_lock_irqsave(&ha->hardware_lock, flags);
1314 ha->interrupts_on = 1;
1315 /* enable risc and host interrupts */
1316 WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1317 RD_REG_WORD(&reg->ictrl);
1318 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1319
1320}
1321
1322static void
e315cd28 1323qla2x00_disable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1324{
1325 unsigned long flags = 0;
1326 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1327
1328 spin_lock_irqsave(&ha->hardware_lock, flags);
1329 ha->interrupts_on = 0;
1330 /* disable risc and host interrupts */
1331 WRT_REG_WORD(&reg->ictrl, 0);
1332 RD_REG_WORD(&reg->ictrl);
1333 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1334}
1335
1336static void
e315cd28 1337qla24xx_enable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1338{
1339 unsigned long flags = 0;
1340 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1341
1342 spin_lock_irqsave(&ha->hardware_lock, flags);
1343 ha->interrupts_on = 1;
1344 WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1345 RD_REG_DWORD(&reg->ictrl);
1346 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1347}
1348
1349static void
e315cd28 1350qla24xx_disable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1351{
1352 unsigned long flags = 0;
1353 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1354
124f85e6
AV
1355 if (IS_NOPOLLING_TYPE(ha))
1356 return;
fd34f556
AV
1357 spin_lock_irqsave(&ha->hardware_lock, flags);
1358 ha->interrupts_on = 0;
1359 WRT_REG_DWORD(&reg->ictrl, 0);
1360 RD_REG_DWORD(&reg->ictrl);
1361 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1362}
1363
1364static struct isp_operations qla2100_isp_ops = {
1365 .pci_config = qla2100_pci_config,
1366 .reset_chip = qla2x00_reset_chip,
1367 .chip_diag = qla2x00_chip_diag,
1368 .config_rings = qla2x00_config_rings,
1369 .reset_adapter = qla2x00_reset_adapter,
1370 .nvram_config = qla2x00_nvram_config,
1371 .update_fw_options = qla2x00_update_fw_options,
1372 .load_risc = qla2x00_load_risc,
1373 .pci_info_str = qla2x00_pci_info_str,
1374 .fw_version_str = qla2x00_fw_version_str,
1375 .intr_handler = qla2100_intr_handler,
1376 .enable_intrs = qla2x00_enable_intrs,
1377 .disable_intrs = qla2x00_disable_intrs,
1378 .abort_command = qla2x00_abort_command,
523ec773
AV
1379 .target_reset = qla2x00_abort_target,
1380 .lun_reset = qla2x00_lun_reset,
fd34f556
AV
1381 .fabric_login = qla2x00_login_fabric,
1382 .fabric_logout = qla2x00_fabric_logout,
1383 .calc_req_entries = qla2x00_calc_iocbs_32,
1384 .build_iocbs = qla2x00_build_scsi_iocbs_32,
1385 .prep_ms_iocb = qla2x00_prep_ms_iocb,
1386 .prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb,
1387 .read_nvram = qla2x00_read_nvram_data,
1388 .write_nvram = qla2x00_write_nvram_data,
1389 .fw_dump = qla2100_fw_dump,
1390 .beacon_on = NULL,
1391 .beacon_off = NULL,
1392 .beacon_blink = NULL,
1393 .read_optrom = qla2x00_read_optrom_data,
1394 .write_optrom = qla2x00_write_optrom_data,
1395 .get_flash_version = qla2x00_get_flash_version,
e315cd28 1396 .start_scsi = qla2x00_start_scsi,
fd34f556
AV
1397};
1398
1399static struct isp_operations qla2300_isp_ops = {
1400 .pci_config = qla2300_pci_config,
1401 .reset_chip = qla2x00_reset_chip,
1402 .chip_diag = qla2x00_chip_diag,
1403 .config_rings = qla2x00_config_rings,
1404 .reset_adapter = qla2x00_reset_adapter,
1405 .nvram_config = qla2x00_nvram_config,
1406 .update_fw_options = qla2x00_update_fw_options,
1407 .load_risc = qla2x00_load_risc,
1408 .pci_info_str = qla2x00_pci_info_str,
1409 .fw_version_str = qla2x00_fw_version_str,
1410 .intr_handler = qla2300_intr_handler,
1411 .enable_intrs = qla2x00_enable_intrs,
1412 .disable_intrs = qla2x00_disable_intrs,
1413 .abort_command = qla2x00_abort_command,
523ec773
AV
1414 .target_reset = qla2x00_abort_target,
1415 .lun_reset = qla2x00_lun_reset,
fd34f556
AV
1416 .fabric_login = qla2x00_login_fabric,
1417 .fabric_logout = qla2x00_fabric_logout,
1418 .calc_req_entries = qla2x00_calc_iocbs_32,
1419 .build_iocbs = qla2x00_build_scsi_iocbs_32,
1420 .prep_ms_iocb = qla2x00_prep_ms_iocb,
1421 .prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb,
1422 .read_nvram = qla2x00_read_nvram_data,
1423 .write_nvram = qla2x00_write_nvram_data,
1424 .fw_dump = qla2300_fw_dump,
1425 .beacon_on = qla2x00_beacon_on,
1426 .beacon_off = qla2x00_beacon_off,
1427 .beacon_blink = qla2x00_beacon_blink,
1428 .read_optrom = qla2x00_read_optrom_data,
1429 .write_optrom = qla2x00_write_optrom_data,
1430 .get_flash_version = qla2x00_get_flash_version,
e315cd28 1431 .start_scsi = qla2x00_start_scsi,
fd34f556
AV
1432};
1433
1434static struct isp_operations qla24xx_isp_ops = {
1435 .pci_config = qla24xx_pci_config,
1436 .reset_chip = qla24xx_reset_chip,
1437 .chip_diag = qla24xx_chip_diag,
1438 .config_rings = qla24xx_config_rings,
1439 .reset_adapter = qla24xx_reset_adapter,
1440 .nvram_config = qla24xx_nvram_config,
1441 .update_fw_options = qla24xx_update_fw_options,
1442 .load_risc = qla24xx_load_risc,
1443 .pci_info_str = qla24xx_pci_info_str,
1444 .fw_version_str = qla24xx_fw_version_str,
1445 .intr_handler = qla24xx_intr_handler,
1446 .enable_intrs = qla24xx_enable_intrs,
1447 .disable_intrs = qla24xx_disable_intrs,
1448 .abort_command = qla24xx_abort_command,
523ec773
AV
1449 .target_reset = qla24xx_abort_target,
1450 .lun_reset = qla24xx_lun_reset,
fd34f556
AV
1451 .fabric_login = qla24xx_login_fabric,
1452 .fabric_logout = qla24xx_fabric_logout,
1453 .calc_req_entries = NULL,
1454 .build_iocbs = NULL,
1455 .prep_ms_iocb = qla24xx_prep_ms_iocb,
1456 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
1457 .read_nvram = qla24xx_read_nvram_data,
1458 .write_nvram = qla24xx_write_nvram_data,
1459 .fw_dump = qla24xx_fw_dump,
1460 .beacon_on = qla24xx_beacon_on,
1461 .beacon_off = qla24xx_beacon_off,
1462 .beacon_blink = qla24xx_beacon_blink,
1463 .read_optrom = qla24xx_read_optrom_data,
1464 .write_optrom = qla24xx_write_optrom_data,
1465 .get_flash_version = qla24xx_get_flash_version,
e315cd28 1466 .start_scsi = qla24xx_start_scsi,
fd34f556
AV
1467};
1468
c3a2f0df
AV
1469static struct isp_operations qla25xx_isp_ops = {
1470 .pci_config = qla25xx_pci_config,
1471 .reset_chip = qla24xx_reset_chip,
1472 .chip_diag = qla24xx_chip_diag,
1473 .config_rings = qla24xx_config_rings,
1474 .reset_adapter = qla24xx_reset_adapter,
1475 .nvram_config = qla24xx_nvram_config,
1476 .update_fw_options = qla24xx_update_fw_options,
1477 .load_risc = qla24xx_load_risc,
1478 .pci_info_str = qla24xx_pci_info_str,
1479 .fw_version_str = qla24xx_fw_version_str,
1480 .intr_handler = qla24xx_intr_handler,
1481 .enable_intrs = qla24xx_enable_intrs,
1482 .disable_intrs = qla24xx_disable_intrs,
1483 .abort_command = qla24xx_abort_command,
523ec773
AV
1484 .target_reset = qla24xx_abort_target,
1485 .lun_reset = qla24xx_lun_reset,
c3a2f0df
AV
1486 .fabric_login = qla24xx_login_fabric,
1487 .fabric_logout = qla24xx_fabric_logout,
1488 .calc_req_entries = NULL,
1489 .build_iocbs = NULL,
1490 .prep_ms_iocb = qla24xx_prep_ms_iocb,
1491 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
1492 .read_nvram = qla25xx_read_nvram_data,
1493 .write_nvram = qla25xx_write_nvram_data,
1494 .fw_dump = qla25xx_fw_dump,
1495 .beacon_on = qla24xx_beacon_on,
1496 .beacon_off = qla24xx_beacon_off,
1497 .beacon_blink = qla24xx_beacon_blink,
338c9161 1498 .read_optrom = qla25xx_read_optrom_data,
c3a2f0df
AV
1499 .write_optrom = qla24xx_write_optrom_data,
1500 .get_flash_version = qla24xx_get_flash_version,
e315cd28 1501 .start_scsi = qla24xx_start_scsi,
c3a2f0df
AV
1502};
1503
3a03eb79
AV
1504static struct isp_operations qla81xx_isp_ops = {
1505 .pci_config = qla25xx_pci_config,
1506 .reset_chip = qla24xx_reset_chip,
1507 .chip_diag = qla24xx_chip_diag,
1508 .config_rings = qla24xx_config_rings,
1509 .reset_adapter = qla24xx_reset_adapter,
1510 .nvram_config = qla81xx_nvram_config,
1511 .update_fw_options = qla81xx_update_fw_options,
eaac30be 1512 .load_risc = qla81xx_load_risc,
3a03eb79
AV
1513 .pci_info_str = qla24xx_pci_info_str,
1514 .fw_version_str = qla24xx_fw_version_str,
1515 .intr_handler = qla24xx_intr_handler,
1516 .enable_intrs = qla24xx_enable_intrs,
1517 .disable_intrs = qla24xx_disable_intrs,
1518 .abort_command = qla24xx_abort_command,
1519 .target_reset = qla24xx_abort_target,
1520 .lun_reset = qla24xx_lun_reset,
1521 .fabric_login = qla24xx_login_fabric,
1522 .fabric_logout = qla24xx_fabric_logout,
1523 .calc_req_entries = NULL,
1524 .build_iocbs = NULL,
1525 .prep_ms_iocb = qla24xx_prep_ms_iocb,
1526 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
3d79038f
AV
1527 .read_nvram = NULL,
1528 .write_nvram = NULL,
3a03eb79
AV
1529 .fw_dump = qla81xx_fw_dump,
1530 .beacon_on = qla24xx_beacon_on,
1531 .beacon_off = qla24xx_beacon_off,
1532 .beacon_blink = qla24xx_beacon_blink,
1533 .read_optrom = qla25xx_read_optrom_data,
1534 .write_optrom = qla24xx_write_optrom_data,
1535 .get_flash_version = qla24xx_get_flash_version,
1536 .start_scsi = qla24xx_start_scsi,
3a03eb79
AV
1537};
1538
ea5b6382 1539static inline void
e315cd28 1540qla2x00_set_isp_flags(struct qla_hw_data *ha)
ea5b6382
AV
1541{
1542 ha->device_type = DT_EXTENDED_IDS;
1543 switch (ha->pdev->device) {
1544 case PCI_DEVICE_ID_QLOGIC_ISP2100:
1545 ha->device_type |= DT_ISP2100;
1546 ha->device_type &= ~DT_EXTENDED_IDS;
441d1072 1547 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
ea5b6382
AV
1548 break;
1549 case PCI_DEVICE_ID_QLOGIC_ISP2200:
1550 ha->device_type |= DT_ISP2200;
1551 ha->device_type &= ~DT_EXTENDED_IDS;
441d1072 1552 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
ea5b6382
AV
1553 break;
1554 case PCI_DEVICE_ID_QLOGIC_ISP2300:
1555 ha->device_type |= DT_ISP2300;
4a59f71d 1556 ha->device_type |= DT_ZIO_SUPPORTED;
441d1072 1557 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1558 break;
1559 case PCI_DEVICE_ID_QLOGIC_ISP2312:
1560 ha->device_type |= DT_ISP2312;
4a59f71d 1561 ha->device_type |= DT_ZIO_SUPPORTED;
441d1072 1562 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1563 break;
1564 case PCI_DEVICE_ID_QLOGIC_ISP2322:
1565 ha->device_type |= DT_ISP2322;
4a59f71d 1566 ha->device_type |= DT_ZIO_SUPPORTED;
ea5b6382
AV
1567 if (ha->pdev->subsystem_vendor == 0x1028 &&
1568 ha->pdev->subsystem_device == 0x0170)
1569 ha->device_type |= DT_OEM_001;
441d1072 1570 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1571 break;
1572 case PCI_DEVICE_ID_QLOGIC_ISP6312:
1573 ha->device_type |= DT_ISP6312;
441d1072 1574 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1575 break;
1576 case PCI_DEVICE_ID_QLOGIC_ISP6322:
1577 ha->device_type |= DT_ISP6322;
441d1072 1578 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1579 break;
1580 case PCI_DEVICE_ID_QLOGIC_ISP2422:
1581 ha->device_type |= DT_ISP2422;
4a59f71d 1582 ha->device_type |= DT_ZIO_SUPPORTED;
e428924c 1583 ha->device_type |= DT_FWI2;
c76f2c01 1584 ha->device_type |= DT_IIDMA;
441d1072 1585 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382
AV
1586 break;
1587 case PCI_DEVICE_ID_QLOGIC_ISP2432:
1588 ha->device_type |= DT_ISP2432;
4a59f71d 1589 ha->device_type |= DT_ZIO_SUPPORTED;
e428924c 1590 ha->device_type |= DT_FWI2;
c76f2c01 1591 ha->device_type |= DT_IIDMA;
441d1072 1592 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1593 break;
4d4df193
HK
1594 case PCI_DEVICE_ID_QLOGIC_ISP8432:
1595 ha->device_type |= DT_ISP8432;
1596 ha->device_type |= DT_ZIO_SUPPORTED;
1597 ha->device_type |= DT_FWI2;
1598 ha->device_type |= DT_IIDMA;
1599 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1600 break;
044cc6c8
AV
1601 case PCI_DEVICE_ID_QLOGIC_ISP5422:
1602 ha->device_type |= DT_ISP5422;
e428924c 1603 ha->device_type |= DT_FWI2;
441d1072 1604 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1605 break;
044cc6c8
AV
1606 case PCI_DEVICE_ID_QLOGIC_ISP5432:
1607 ha->device_type |= DT_ISP5432;
e428924c 1608 ha->device_type |= DT_FWI2;
441d1072 1609 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1610 break;
c3a2f0df
AV
1611 case PCI_DEVICE_ID_QLOGIC_ISP2532:
1612 ha->device_type |= DT_ISP2532;
1613 ha->device_type |= DT_ZIO_SUPPORTED;
1614 ha->device_type |= DT_FWI2;
1615 ha->device_type |= DT_IIDMA;
441d1072 1616 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1617 break;
3a03eb79
AV
1618 case PCI_DEVICE_ID_QLOGIC_ISP8001:
1619 ha->device_type |= DT_ISP8001;
1620 ha->device_type |= DT_ZIO_SUPPORTED;
1621 ha->device_type |= DT_FWI2;
1622 ha->device_type |= DT_IIDMA;
1623 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1624 break;
ea5b6382 1625 }
e5b68a61
AC
1626
1627 /* Get adapter physical port no from interrupt pin register. */
1628 pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
1629 if (ha->port_no & 1)
1630 ha->flags.port0 = 1;
1631 else
1632 ha->flags.port0 = 0;
ea5b6382
AV
1633}
1634
1da177e4 1635static int
e315cd28 1636qla2x00_iospace_config(struct qla_hw_data *ha)
1da177e4 1637{
3776541d 1638 resource_size_t pio;
73208dfd 1639 uint16_t msix;
68ca949c 1640 int cpus;
1da177e4 1641
285d0321
AV
1642 if (pci_request_selected_regions(ha->pdev, ha->bars,
1643 QLA2XXX_DRIVER_NAME)) {
1644 qla_printk(KERN_WARNING, ha,
1645 "Failed to reserve PIO/MMIO regions (%s)\n",
1646 pci_name(ha->pdev));
1647
1648 goto iospace_error_exit;
1649 }
1650 if (!(ha->bars & 1))
1651 goto skip_pio;
1652
1da177e4
LT
1653 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1654 pio = pci_resource_start(ha->pdev, 0);
3776541d
AV
1655 if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1656 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1da177e4
LT
1657 qla_printk(KERN_WARNING, ha,
1658 "Invalid PCI I/O region size (%s)...\n",
1659 pci_name(ha->pdev));
1660 pio = 0;
1661 }
1662 } else {
1663 qla_printk(KERN_WARNING, ha,
1664 "region #0 not a PIO resource (%s)...\n",
1665 pci_name(ha->pdev));
1666 pio = 0;
1667 }
285d0321 1668 ha->pio_address = pio;
1da177e4 1669
285d0321 1670skip_pio:
1da177e4 1671 /* Use MMIO operations for all accesses. */
3776541d 1672 if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
1da177e4 1673 qla_printk(KERN_ERR, ha,
3776541d 1674 "region #1 not an MMIO resource (%s), aborting\n",
1da177e4
LT
1675 pci_name(ha->pdev));
1676 goto iospace_error_exit;
1677 }
3776541d 1678 if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
1da177e4
LT
1679 qla_printk(KERN_ERR, ha,
1680 "Invalid PCI mem region size (%s), aborting\n",
1681 pci_name(ha->pdev));
1682 goto iospace_error_exit;
1683 }
1684
3776541d 1685 ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
1da177e4
LT
1686 if (!ha->iobase) {
1687 qla_printk(KERN_ERR, ha,
1688 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1689
1690 goto iospace_error_exit;
1691 }
1692
73208dfd 1693 /* Determine queue resources */
2afa19a9 1694 ha->max_req_queues = ha->max_rsp_queues = 1;
68ca949c 1695 if ((ql2xmaxqueues <= 1 || ql2xmultique_tag < 1) &&
2afa19a9 1696 (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
17d98630
AC
1697 goto mqiobase_exit;
1698 ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1699 pci_resource_len(ha->pdev, 3));
1700 if (ha->mqiobase) {
1701 /* Read MSIX vector size of the board */
1702 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1703 ha->msix_count = msix;
68ca949c
AC
1704 /* Max queues are bounded by available msix vectors */
1705 /* queue 0 uses two msix vectors */
1706 if (ql2xmultique_tag) {
1707 cpus = num_online_cpus();
27dc9c5a 1708 ha->max_rsp_queues = (ha->msix_count - 1 > cpus) ?
68ca949c
AC
1709 (cpus + 1) : (ha->msix_count - 1);
1710 ha->max_req_queues = 2;
1711 } else if (ql2xmaxqueues > 1) {
2afa19a9
AC
1712 ha->max_req_queues = ql2xmaxqueues > QLA_MQ_SIZE ?
1713 QLA_MQ_SIZE : ql2xmaxqueues;
1714 DEBUG2(qla_printk(KERN_INFO, ha, "QoS mode set, max no"
1715 " of request queues:%d\n", ha->max_req_queues));
1716 }
68ca949c
AC
1717 qla_printk(KERN_INFO, ha,
1718 "MSI-X vector count: %d\n", msix);
2afa19a9
AC
1719 } else
1720 qla_printk(KERN_INFO, ha, "BAR 3 not enabled\n");
17d98630
AC
1721
1722mqiobase_exit:
2afa19a9 1723 ha->msix_count = ha->max_rsp_queues + 1;
1da177e4
LT
1724 return (0);
1725
1726iospace_error_exit:
1727 return (-ENOMEM);
1728}
1729
1e99e33a
AV
1730static void
1731qla2xxx_scan_start(struct Scsi_Host *shost)
1732{
e315cd28 1733 scsi_qla_host_t *vha = shost_priv(shost);
1e99e33a 1734
cbc8eb67
AV
1735 if (vha->hw->flags.running_gold_fw)
1736 return;
1737
e315cd28
AC
1738 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1739 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1740 set_bit(RSCN_UPDATE, &vha->dpc_flags);
1741 set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
1e99e33a
AV
1742}
1743
1744static int
1745qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
1746{
e315cd28 1747 scsi_qla_host_t *vha = shost_priv(shost);
1e99e33a 1748
e315cd28 1749 if (!vha->host)
1e99e33a 1750 return 1;
e315cd28 1751 if (time > vha->hw->loop_reset_delay * HZ)
1e99e33a
AV
1752 return 1;
1753
e315cd28 1754 return atomic_read(&vha->loop_state) == LOOP_READY;
1e99e33a
AV
1755}
1756
1da177e4
LT
1757/*
1758 * PCI driver interface
1759 */
7ee61397
AV
1760static int __devinit
1761qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1da177e4 1762{
a1541d5a 1763 int ret = -ENODEV;
1da177e4 1764 struct Scsi_Host *host;
e315cd28
AC
1765 scsi_qla_host_t *base_vha = NULL;
1766 struct qla_hw_data *ha;
29856e28 1767 char pci_info[30];
1da177e4 1768 char fw_str[30];
5433383e 1769 struct scsi_host_template *sht;
c51da4ec 1770 int bars, max_id, mem_only = 0;
e315cd28 1771 uint16_t req_length = 0, rsp_length = 0;
73208dfd
AC
1772 struct req_que *req = NULL;
1773 struct rsp_que *rsp = NULL;
1da177e4 1774
285d0321 1775 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
a5326f86 1776 sht = &qla2xxx_driver_template;
5433383e 1777 if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
8bc69e7d 1778 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
4d4df193 1779 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
8bc69e7d 1780 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
c3a2f0df 1781 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
3a03eb79
AV
1782 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
1783 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001) {
285d0321 1784 bars = pci_select_bars(pdev, IORESOURCE_MEM);
09483916 1785 mem_only = 1;
285d0321
AV
1786 }
1787
09483916
BH
1788 if (mem_only) {
1789 if (pci_enable_device_mem(pdev))
1790 goto probe_out;
1791 } else {
1792 if (pci_enable_device(pdev))
1793 goto probe_out;
1794 }
285d0321 1795
0927678f
JB
1796 /* This may fail but that's ok */
1797 pci_enable_pcie_error_reporting(pdev);
285d0321 1798
e315cd28
AC
1799 ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
1800 if (!ha) {
1801 DEBUG(printk("Unable to allocate memory for ha\n"));
1802 goto probe_out;
1da177e4 1803 }
e315cd28 1804 ha->pdev = pdev;
1da177e4
LT
1805
1806 /* Clear our data area */
285d0321 1807 ha->bars = bars;
09483916 1808 ha->mem_only = mem_only;
df4bf0bb 1809 spin_lock_init(&ha->hardware_lock);
1da177e4 1810
ea5b6382
AV
1811 /* Set ISP-type information. */
1812 qla2x00_set_isp_flags(ha);
1da177e4
LT
1813 /* Configure PCI I/O space */
1814 ret = qla2x00_iospace_config(ha);
a1541d5a 1815 if (ret)
e315cd28 1816 goto probe_hw_failed;
1da177e4 1817
1da177e4 1818 qla_printk(KERN_INFO, ha,
5433383e
AV
1819 "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1820 ha->iobase);
1da177e4 1821
1da177e4 1822 ha->prev_topology = 0;
fca29703 1823 ha->init_cb_size = sizeof(init_cb_t);
d8b45213 1824 ha->link_data_rate = PORT_SPEED_UNKNOWN;
854165f4 1825 ha->optrom_size = OPTROM_SIZE_2300;
1da177e4 1826
abbd8870 1827 /* Assign ISP specific operations. */
e315cd28 1828 max_id = MAX_TARGETS_2200;
1da177e4 1829 if (IS_QLA2100(ha)) {
e315cd28 1830 max_id = MAX_TARGETS_2100;
1da177e4 1831 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
e315cd28
AC
1832 req_length = REQUEST_ENTRY_CNT_2100;
1833 rsp_length = RESPONSE_ENTRY_CNT_2100;
1834 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
abbd8870 1835 ha->gid_list_info_size = 4;
3a03eb79
AV
1836 ha->flash_conf_off = ~0;
1837 ha->flash_data_off = ~0;
1838 ha->nvram_conf_off = ~0;
1839 ha->nvram_data_off = ~0;
fd34f556 1840 ha->isp_ops = &qla2100_isp_ops;
1da177e4 1841 } else if (IS_QLA2200(ha)) {
1da177e4 1842 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1843 req_length = REQUEST_ENTRY_CNT_2200;
1844 rsp_length = RESPONSE_ENTRY_CNT_2100;
1845 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
abbd8870 1846 ha->gid_list_info_size = 4;
3a03eb79
AV
1847 ha->flash_conf_off = ~0;
1848 ha->flash_data_off = ~0;
1849 ha->nvram_conf_off = ~0;
1850 ha->nvram_data_off = ~0;
fd34f556 1851 ha->isp_ops = &qla2100_isp_ops;
fca29703 1852 } else if (IS_QLA23XX(ha)) {
1da177e4 1853 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1854 req_length = REQUEST_ENTRY_CNT_2200;
1855 rsp_length = RESPONSE_ENTRY_CNT_2300;
1856 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
abbd8870 1857 ha->gid_list_info_size = 6;
854165f4
AV
1858 if (IS_QLA2322(ha) || IS_QLA6322(ha))
1859 ha->optrom_size = OPTROM_SIZE_2322;
3a03eb79
AV
1860 ha->flash_conf_off = ~0;
1861 ha->flash_data_off = ~0;
1862 ha->nvram_conf_off = ~0;
1863 ha->nvram_data_off = ~0;
fd34f556 1864 ha->isp_ops = &qla2300_isp_ops;
4d4df193 1865 } else if (IS_QLA24XX_TYPE(ha)) {
fca29703 1866 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1867 req_length = REQUEST_ENTRY_CNT_24XX;
1868 rsp_length = RESPONSE_ENTRY_CNT_2300;
1869 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2c3dfe3f 1870 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
fca29703 1871 ha->gid_list_info_size = 8;
854165f4 1872 ha->optrom_size = OPTROM_SIZE_24XX;
73208dfd 1873 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
fd34f556 1874 ha->isp_ops = &qla24xx_isp_ops;
3a03eb79
AV
1875 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1876 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1877 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1878 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
c3a2f0df 1879 } else if (IS_QLA25XX(ha)) {
c3a2f0df 1880 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1881 req_length = REQUEST_ENTRY_CNT_24XX;
1882 rsp_length = RESPONSE_ENTRY_CNT_2300;
1883 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
c3a2f0df 1884 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
c3a2f0df
AV
1885 ha->gid_list_info_size = 8;
1886 ha->optrom_size = OPTROM_SIZE_25XX;
73208dfd 1887 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
c3a2f0df 1888 ha->isp_ops = &qla25xx_isp_ops;
3a03eb79
AV
1889 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1890 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1891 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1892 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1893 } else if (IS_QLA81XX(ha)) {
1894 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1895 req_length = REQUEST_ENTRY_CNT_24XX;
1896 rsp_length = RESPONSE_ENTRY_CNT_2300;
1897 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1898 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
1899 ha->gid_list_info_size = 8;
1900 ha->optrom_size = OPTROM_SIZE_81XX;
40859ae5 1901 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3a03eb79
AV
1902 ha->isp_ops = &qla81xx_isp_ops;
1903 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
1904 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
1905 ha->nvram_conf_off = ~0;
1906 ha->nvram_data_off = ~0;
1da177e4 1907 }
1da177e4 1908
6c2f527c 1909 mutex_init(&ha->vport_lock);
0b05a1f0
MB
1910 init_completion(&ha->mbx_cmd_comp);
1911 complete(&ha->mbx_cmd_comp);
1912 init_completion(&ha->mbx_intr_comp);
1da177e4 1913
2c3dfe3f 1914 set_bit(0, (unsigned long *) ha->vp_idx_map);
1da177e4 1915
53303c42 1916 qla2x00_config_dma_addressing(ha);
73208dfd 1917 ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
e315cd28 1918 if (!ret) {
1da177e4
LT
1919 qla_printk(KERN_WARNING, ha,
1920 "[ERROR] Failed to allocate memory for adapter\n");
1921
e315cd28
AC
1922 goto probe_hw_failed;
1923 }
1924
73208dfd 1925 req->max_q_depth = MAX_Q_DEPTH;
e315cd28 1926 if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
73208dfd
AC
1927 req->max_q_depth = ql2xmaxqdepth;
1928
e315cd28
AC
1929
1930 base_vha = qla2x00_create_host(sht, ha);
1931 if (!base_vha) {
1932 qla_printk(KERN_WARNING, ha,
1933 "[ERROR] Failed to allocate memory for scsi_host\n");
1934
a1541d5a 1935 ret = -ENOMEM;
6e9f21f3 1936 qla2x00_mem_free(ha);
2afa19a9
AC
1937 qla2x00_free_req_que(ha, req);
1938 qla2x00_free_rsp_que(ha, rsp);
e315cd28 1939 goto probe_hw_failed;
1da177e4
LT
1940 }
1941
e315cd28
AC
1942 pci_set_drvdata(pdev, base_vha);
1943
e315cd28 1944 host = base_vha->host;
2afa19a9 1945 base_vha->req = req;
73208dfd
AC
1946 host->can_queue = req->length + 128;
1947 if (IS_QLA2XXX_MIDTYPE(ha))
e315cd28 1948 base_vha->mgmt_svr_loop_id = 10 + base_vha->vp_idx;
73208dfd 1949 else
e315cd28
AC
1950 base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
1951 base_vha->vp_idx;
e315cd28
AC
1952 if (IS_QLA2100(ha))
1953 host->sg_tablesize = 32;
1954 host->max_id = max_id;
1955 host->this_id = 255;
1956 host->cmd_per_lun = 3;
1957 host->unique_id = host->host_no;
1958 host->max_cmd_len = MAX_CMDSZ;
1959 host->max_channel = MAX_BUSES - 1;
1960 host->max_lun = MAX_LUNS;
1961 host->transportt = qla2xxx_transport_template;
1962
73208dfd
AC
1963 /* Set up the irqs */
1964 ret = qla2x00_request_irqs(ha, rsp);
1965 if (ret)
6e9f21f3 1966 goto probe_init_failed;
73208dfd 1967 /* Alloc arrays of request and response ring ptrs */
7163ea81 1968que_init:
73208dfd
AC
1969 if (!qla2x00_alloc_queues(ha)) {
1970 qla_printk(KERN_WARNING, ha,
1971 "[ERROR] Failed to allocate memory for queue"
1972 " pointers\n");
6e9f21f3 1973 goto probe_init_failed;
73208dfd
AC
1974 }
1975 ha->rsp_q_map[0] = rsp;
1976 ha->req_q_map[0] = req;
2afa19a9
AC
1977 rsp->req = req;
1978 req->rsp = rsp;
1979 set_bit(0, ha->req_qid_map);
1980 set_bit(0, ha->rsp_qid_map);
08029990
AV
1981 /* FWI2-capable only. */
1982 req->req_q_in = &ha->iobase->isp24.req_q_in;
1983 req->req_q_out = &ha->iobase->isp24.req_q_out;
1984 rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
1985 rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
17d98630 1986 if (ha->mqenable) {
08029990
AV
1987 req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
1988 req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
1989 rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
1990 rsp->rsp_q_out = &ha->mqiobase->isp25mq.rsp_q_out;
17d98630
AC
1991 }
1992
e315cd28 1993 if (qla2x00_initialize_adapter(base_vha)) {
1da177e4
LT
1994 qla_printk(KERN_WARNING, ha,
1995 "Failed to initialize adapter\n");
1996
1997 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1998 "Adapter flags %x.\n",
e315cd28 1999 base_vha->host_no, base_vha->device_flags));
1da177e4 2000
a1541d5a 2001 ret = -ENODEV;
1da177e4
LT
2002 goto probe_failed;
2003 }
2004
7163ea81
AC
2005 if (ha->mqenable) {
2006 if (qla25xx_setup_mode(base_vha)) {
68ca949c
AC
2007 qla_printk(KERN_WARNING, ha,
2008 "Can't create queues, falling back to single"
2009 " queue mode\n");
7163ea81
AC
2010 goto que_init;
2011 }
2012 }
68ca949c 2013
cbc8eb67
AV
2014 if (ha->flags.running_gold_fw)
2015 goto skip_dpc;
2016
1da177e4
LT
2017 /*
2018 * Startup the kernel thread for this host adapter
2019 */
39a11240 2020 ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
e315cd28 2021 "%s_dpc", base_vha->host_str);
39a11240 2022 if (IS_ERR(ha->dpc_thread)) {
1da177e4
LT
2023 qla_printk(KERN_WARNING, ha,
2024 "Unable to start DPC thread!\n");
39a11240 2025 ret = PTR_ERR(ha->dpc_thread);
1da177e4
LT
2026 goto probe_failed;
2027 }
1da177e4 2028
cbc8eb67 2029skip_dpc:
e315cd28
AC
2030 list_add_tail(&base_vha->list, &ha->vp_list);
2031 base_vha->host->irq = ha->pdev->irq;
1da177e4
LT
2032
2033 /* Initialized the timer */
e315cd28 2034 qla2x00_start_timer(base_vha, qla2x00_timer, WATCH_INTERVAL);
1da177e4
LT
2035
2036 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
e315cd28 2037 base_vha->host_no, ha));
d19044c3 2038
a1541d5a
AV
2039 ret = scsi_add_host(host, &pdev->dev);
2040 if (ret)
2041 goto probe_failed;
2042
1486400f
MR
2043 base_vha->flags.init_done = 1;
2044 base_vha->flags.online = 1;
2045
048feec5
AV
2046 ha->isp_ops->enable_intrs(ha);
2047
1e99e33a
AV
2048 scsi_scan_host(host);
2049
e315cd28 2050 qla2x00_alloc_sysfs_attr(base_vha);
a1541d5a 2051
e315cd28 2052 qla2x00_init_host_attr(base_vha);
a1541d5a 2053
e315cd28 2054 qla2x00_dfs_setup(base_vha);
df613b96 2055
1da177e4
LT
2056 qla_printk(KERN_INFO, ha, "\n"
2057 " QLogic Fibre Channel HBA Driver: %s\n"
2058 " QLogic %s - %s\n"
5433383e
AV
2059 " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
2060 qla2x00_version_str, ha->model_number,
e315cd28
AC
2061 ha->model_desc ? ha->model_desc : "", pdev->device,
2062 ha->isp_ops->pci_info_str(base_vha, pci_info), pci_name(pdev),
2063 ha->flags.enable_64bit_addressing ? '+' : '-', base_vha->host_no,
2064 ha->isp_ops->fw_version_str(base_vha, fw_str));
1da177e4 2065
1da177e4
LT
2066 return 0;
2067
6e9f21f3 2068probe_init_failed:
2afa19a9
AC
2069 qla2x00_free_req_que(ha, req);
2070 qla2x00_free_rsp_que(ha, rsp);
2071 ha->max_req_queues = ha->max_rsp_queues = 0;
6e9f21f3 2072
1da177e4 2073probe_failed:
b9978769
AV
2074 if (base_vha->timer_active)
2075 qla2x00_stop_timer(base_vha);
2076 base_vha->flags.online = 0;
2077 if (ha->dpc_thread) {
2078 struct task_struct *t = ha->dpc_thread;
2079
2080 ha->dpc_thread = NULL;
2081 kthread_stop(t);
2082 }
2083
e315cd28 2084 qla2x00_free_device(base_vha);
1da177e4 2085
e315cd28 2086 scsi_host_put(base_vha->host);
1da177e4 2087
e315cd28
AC
2088probe_hw_failed:
2089 if (ha->iobase)
2090 iounmap(ha->iobase);
2091
2092 pci_release_selected_regions(ha->pdev, ha->bars);
2093 kfree(ha);
2094 ha = NULL;
1da177e4 2095
a1541d5a 2096probe_out:
e315cd28 2097 pci_disable_device(pdev);
a1541d5a 2098 return ret;
1da177e4 2099}
1da177e4 2100
4c993f76 2101static void
7ee61397 2102qla2x00_remove_one(struct pci_dev *pdev)
1da177e4 2103{
e315cd28
AC
2104 scsi_qla_host_t *base_vha, *vha, *temp;
2105 struct qla_hw_data *ha;
2106
2107 base_vha = pci_get_drvdata(pdev);
2108 ha = base_vha->hw;
2109
2110 list_for_each_entry_safe(vha, temp, &ha->vp_list, list) {
2111 if (vha && vha->fc_vport)
2112 fc_vport_terminate(vha->fc_vport);
2113 }
1da177e4 2114
e315cd28 2115 set_bit(UNLOADING, &base_vha->dpc_flags);
1da177e4 2116
b9978769
AV
2117 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2118
e315cd28 2119 qla2x00_dfs_remove(base_vha);
c795c1e4 2120
e315cd28 2121 qla84xx_put_chip(base_vha);
c795c1e4 2122
b9978769
AV
2123 /* Disable timer */
2124 if (base_vha->timer_active)
2125 qla2x00_stop_timer(base_vha);
2126
2127 base_vha->flags.online = 0;
2128
68ca949c
AC
2129 /* Flush the work queue and remove it */
2130 if (ha->wq) {
2131 flush_workqueue(ha->wq);
2132 destroy_workqueue(ha->wq);
2133 ha->wq = NULL;
2134 }
2135
b9978769
AV
2136 /* Kill the kernel thread for this host */
2137 if (ha->dpc_thread) {
2138 struct task_struct *t = ha->dpc_thread;
2139
2140 /*
2141 * qla2xxx_wake_dpc checks for ->dpc_thread
2142 * so we need to zero it out.
2143 */
2144 ha->dpc_thread = NULL;
2145 kthread_stop(t);
2146 }
2147
e315cd28 2148 qla2x00_free_sysfs_attr(base_vha);
df613b96 2149
e315cd28 2150 fc_remove_host(base_vha->host);
4d4df193 2151
e315cd28 2152 scsi_remove_host(base_vha->host);
1da177e4 2153
e315cd28 2154 qla2x00_free_device(base_vha);
bdf79621 2155
e315cd28 2156 scsi_host_put(base_vha->host);
1da177e4 2157
e315cd28
AC
2158 if (ha->iobase)
2159 iounmap(ha->iobase);
1da177e4 2160
73208dfd
AC
2161 if (ha->mqiobase)
2162 iounmap(ha->mqiobase);
2163
e315cd28
AC
2164 pci_release_selected_regions(ha->pdev, ha->bars);
2165 kfree(ha);
2166 ha = NULL;
1da177e4 2167
665db93b 2168 pci_disable_device(pdev);
1da177e4
LT
2169 pci_set_drvdata(pdev, NULL);
2170}
1da177e4
LT
2171
2172static void
e315cd28 2173qla2x00_free_device(scsi_qla_host_t *vha)
1da177e4 2174{
e315cd28 2175 struct qla_hw_data *ha = vha->hw;
1da177e4 2176
2afa19a9
AC
2177 qla25xx_delete_queues(vha);
2178
df613b96 2179 if (ha->flags.fce_enabled)
e315cd28 2180 qla2x00_disable_fce_trace(vha, NULL, NULL);
df613b96 2181
a7a167bf 2182 if (ha->eft)
e315cd28 2183 qla2x00_disable_eft_trace(vha);
a7a167bf 2184
f6ef3b18 2185 /* Stop currently executing firmware. */
e315cd28 2186 qla2x00_try_to_stop_firmware(vha);
1da177e4 2187
f6ef3b18
AV
2188 /* turn-off interrupts on the card */
2189 if (ha->interrupts_on)
fd34f556 2190 ha->isp_ops->disable_intrs(ha);
f6ef3b18 2191
e315cd28 2192 qla2x00_free_irqs(vha);
1da177e4 2193
e315cd28 2194 qla2x00_mem_free(ha);
73208dfd
AC
2195
2196 qla2x00_free_queues(ha);
1da177e4
LT
2197}
2198
d97994dc 2199static inline void
e315cd28 2200qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
d97994dc
AV
2201 int defer)
2202{
d97994dc 2203 struct fc_rport *rport;
67becc00 2204 scsi_qla_host_t *base_vha;
d97994dc
AV
2205
2206 if (!fcport->rport)
2207 return;
2208
2209 rport = fcport->rport;
2210 if (defer) {
67becc00 2211 base_vha = pci_get_drvdata(vha->hw->pdev);
e315cd28 2212 spin_lock_irq(vha->host->host_lock);
d97994dc 2213 fcport->drport = rport;
e315cd28 2214 spin_unlock_irq(vha->host->host_lock);
67becc00
AV
2215 set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2216 qla2xxx_wake_dpc(base_vha);
5f3a9a20 2217 } else
d97994dc 2218 fc_remote_port_delete(rport);
d97994dc
AV
2219}
2220
1da177e4
LT
2221/*
2222 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2223 *
2224 * Input: ha = adapter block pointer. fcport = port structure pointer.
2225 *
2226 * Return: None.
2227 *
2228 * Context:
2229 */
e315cd28 2230void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
d97994dc 2231 int do_login, int defer)
1da177e4 2232{
2c3dfe3f 2233 if (atomic_read(&fcport->state) == FCS_ONLINE &&
e315cd28
AC
2234 vha->vp_idx == fcport->vp_idx) {
2235 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2236 qla2x00_schedule_rport_del(vha, fcport, defer);
2237 }
fa2a1ce5 2238 /*
1da177e4
LT
2239 * We may need to retry the login, so don't change the state of the
2240 * port but do the retries.
2241 */
2242 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2243 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2244
2245 if (!do_login)
2246 return;
2247
2248 if (fcport->login_retry == 0) {
e315cd28
AC
2249 fcport->login_retry = vha->hw->login_retry_count;
2250 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1da177e4
LT
2251
2252 DEBUG(printk("scsi(%ld): Port login retry: "
2253 "%02x%02x%02x%02x%02x%02x%02x%02x, "
2254 "id = 0x%04x retry cnt=%d\n",
e315cd28 2255 vha->host_no,
1da177e4
LT
2256 fcport->port_name[0],
2257 fcport->port_name[1],
2258 fcport->port_name[2],
2259 fcport->port_name[3],
2260 fcport->port_name[4],
2261 fcport->port_name[5],
2262 fcport->port_name[6],
2263 fcport->port_name[7],
2264 fcport->loop_id,
2265 fcport->login_retry));
2266 }
2267}
2268
2269/*
2270 * qla2x00_mark_all_devices_lost
2271 * Updates fcport state when device goes offline.
2272 *
2273 * Input:
2274 * ha = adapter block pointer.
2275 * fcport = port structure pointer.
2276 *
2277 * Return:
2278 * None.
2279 *
2280 * Context:
2281 */
2282void
e315cd28 2283qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
1da177e4
LT
2284{
2285 fc_port_t *fcport;
2286
e315cd28 2287 list_for_each_entry(fcport, &vha->vp_fcports, list) {
0d6e61bc 2288 if (vha->vp_idx != 0 && vha->vp_idx != fcport->vp_idx)
1da177e4 2289 continue;
0d6e61bc 2290
1da177e4
LT
2291 /*
2292 * No point in marking the device as lost, if the device is
2293 * already DEAD.
2294 */
2295 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2296 continue;
e315cd28 2297 if (atomic_read(&fcport->state) == FCS_ONLINE) {
0d6e61bc
AV
2298 if (defer)
2299 qla2x00_schedule_rport_del(vha, fcport, defer);
2300 else if (vha->vp_idx == fcport->vp_idx)
2301 qla2x00_schedule_rport_del(vha, fcport, defer);
2302 }
2303 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1da177e4
LT
2304 }
2305}
2306
2307/*
2308* qla2x00_mem_alloc
2309* Allocates adapter memory.
2310*
2311* Returns:
2312* 0 = success.
e8711085 2313* !0 = failure.
1da177e4 2314*/
e8711085 2315static int
73208dfd
AC
2316qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2317 struct req_que **req, struct rsp_que **rsp)
1da177e4
LT
2318{
2319 char name[16];
1da177e4 2320
e8711085 2321 ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
e315cd28 2322 &ha->init_cb_dma, GFP_KERNEL);
e8711085 2323 if (!ha->init_cb)
e315cd28 2324 goto fail;
e8711085 2325
e315cd28
AC
2326 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2327 &ha->gid_list_dma, GFP_KERNEL);
2328 if (!ha->gid_list)
e8711085 2329 goto fail_free_init_cb;
1da177e4 2330
e8711085
AV
2331 ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2332 if (!ha->srb_mempool)
e315cd28 2333 goto fail_free_gid_list;
e8711085
AV
2334
2335 /* Get memory for cached NVRAM */
2336 ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2337 if (!ha->nvram)
2338 goto fail_free_srb_mempool;
2339
e315cd28
AC
2340 snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2341 ha->pdev->device);
2342 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2343 DMA_POOL_SIZE, 8, 0);
2344 if (!ha->s_dma_pool)
2345 goto fail_free_nvram;
2346
e8711085
AV
2347 /* Allocate memory for SNS commands */
2348 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
e315cd28 2349 /* Get consistent memory allocated for SNS commands */
e8711085 2350 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
e315cd28 2351 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
e8711085 2352 if (!ha->sns_cmd)
e315cd28 2353 goto fail_dma_pool;
e8711085 2354 } else {
e315cd28 2355 /* Get consistent memory allocated for MS IOCB */
e8711085 2356 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
e315cd28 2357 &ha->ms_iocb_dma);
e8711085 2358 if (!ha->ms_iocb)
e315cd28
AC
2359 goto fail_dma_pool;
2360 /* Get consistent memory allocated for CT SNS commands */
e8711085 2361 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
e315cd28 2362 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
e8711085
AV
2363 if (!ha->ct_sns)
2364 goto fail_free_ms_iocb;
1da177e4
LT
2365 }
2366
e315cd28 2367 /* Allocate memory for request ring */
73208dfd
AC
2368 *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2369 if (!*req) {
e315cd28
AC
2370 DEBUG(printk("Unable to allocate memory for req\n"));
2371 goto fail_req;
2372 }
73208dfd
AC
2373 (*req)->length = req_len;
2374 (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2375 ((*req)->length + 1) * sizeof(request_t),
2376 &(*req)->dma, GFP_KERNEL);
2377 if (!(*req)->ring) {
e315cd28
AC
2378 DEBUG(printk("Unable to allocate memory for req_ring\n"));
2379 goto fail_req_ring;
2380 }
2381 /* Allocate memory for response ring */
73208dfd
AC
2382 *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2383 if (!*rsp) {
2384 qla_printk(KERN_WARNING, ha,
2385 "Unable to allocate memory for rsp\n");
e315cd28
AC
2386 goto fail_rsp;
2387 }
73208dfd
AC
2388 (*rsp)->hw = ha;
2389 (*rsp)->length = rsp_len;
2390 (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2391 ((*rsp)->length + 1) * sizeof(response_t),
2392 &(*rsp)->dma, GFP_KERNEL);
2393 if (!(*rsp)->ring) {
2394 qla_printk(KERN_WARNING, ha,
2395 "Unable to allocate memory for rsp_ring\n");
e315cd28
AC
2396 goto fail_rsp_ring;
2397 }
73208dfd
AC
2398 (*req)->rsp = *rsp;
2399 (*rsp)->req = *req;
2400 /* Allocate memory for NVRAM data for vports */
2401 if (ha->nvram_npiv_size) {
2402 ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2403 ha->nvram_npiv_size, GFP_KERNEL);
2404 if (!ha->npiv_info) {
2405 qla_printk(KERN_WARNING, ha,
2406 "Unable to allocate memory for npiv info\n");
2407 goto fail_npiv_info;
2408 }
2409 } else
2410 ha->npiv_info = NULL;
e8711085 2411
b64b0e8f
AV
2412 /* Get consistent memory allocated for EX-INIT-CB. */
2413 if (IS_QLA81XX(ha)) {
2414 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2415 &ha->ex_init_cb_dma);
2416 if (!ha->ex_init_cb)
2417 goto fail_ex_init_cb;
2418 }
2419
e315cd28
AC
2420 INIT_LIST_HEAD(&ha->vp_list);
2421 return 1;
2422
b64b0e8f
AV
2423fail_ex_init_cb:
2424 kfree(ha->npiv_info);
73208dfd
AC
2425fail_npiv_info:
2426 dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2427 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2428 (*rsp)->ring = NULL;
2429 (*rsp)->dma = 0;
e315cd28 2430fail_rsp_ring:
73208dfd 2431 kfree(*rsp);
e315cd28 2432fail_rsp:
73208dfd
AC
2433 dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2434 sizeof(request_t), (*req)->ring, (*req)->dma);
2435 (*req)->ring = NULL;
2436 (*req)->dma = 0;
e315cd28 2437fail_req_ring:
73208dfd 2438 kfree(*req);
e315cd28
AC
2439fail_req:
2440 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2441 ha->ct_sns, ha->ct_sns_dma);
2442 ha->ct_sns = NULL;
2443 ha->ct_sns_dma = 0;
e8711085
AV
2444fail_free_ms_iocb:
2445 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2446 ha->ms_iocb = NULL;
2447 ha->ms_iocb_dma = 0;
e315cd28
AC
2448fail_dma_pool:
2449 dma_pool_destroy(ha->s_dma_pool);
2450 ha->s_dma_pool = NULL;
e8711085
AV
2451fail_free_nvram:
2452 kfree(ha->nvram);
2453 ha->nvram = NULL;
2454fail_free_srb_mempool:
2455 mempool_destroy(ha->srb_mempool);
2456 ha->srb_mempool = NULL;
e8711085
AV
2457fail_free_gid_list:
2458 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
e315cd28 2459 ha->gid_list_dma);
e8711085
AV
2460 ha->gid_list = NULL;
2461 ha->gid_list_dma = 0;
e315cd28
AC
2462fail_free_init_cb:
2463 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2464 ha->init_cb_dma);
2465 ha->init_cb = NULL;
2466 ha->init_cb_dma = 0;
e8711085 2467fail:
e315cd28 2468 DEBUG(printk("%s: Memory allocation failure\n", __func__));
e8711085 2469 return -ENOMEM;
1da177e4
LT
2470}
2471
2472/*
2473* qla2x00_mem_free
2474* Frees all adapter allocated memory.
2475*
2476* Input:
2477* ha = adapter block pointer.
2478*/
a824ebb3 2479static void
e315cd28 2480qla2x00_mem_free(struct qla_hw_data *ha)
1da177e4 2481{
e8711085
AV
2482 if (ha->srb_mempool)
2483 mempool_destroy(ha->srb_mempool);
1da177e4 2484
df613b96
AV
2485 if (ha->fce)
2486 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
e315cd28 2487 ha->fce_dma);
df613b96 2488
a7a167bf
AV
2489 if (ha->fw_dump) {
2490 if (ha->eft)
2491 dma_free_coherent(&ha->pdev->dev,
e315cd28 2492 ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
a7a167bf
AV
2493 vfree(ha->fw_dump);
2494 }
2495
11bbc1d8
AV
2496 if (ha->dcbx_tlv)
2497 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
2498 ha->dcbx_tlv, ha->dcbx_tlv_dma);
2499
ce0423f4
AV
2500 if (ha->xgmac_data)
2501 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
2502 ha->xgmac_data, ha->xgmac_data_dma);
2503
1da177e4
LT
2504 if (ha->sns_cmd)
2505 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
e315cd28 2506 ha->sns_cmd, ha->sns_cmd_dma);
1da177e4
LT
2507
2508 if (ha->ct_sns)
2509 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
e315cd28 2510 ha->ct_sns, ha->ct_sns_dma);
1da177e4 2511
88729e53
AV
2512 if (ha->sfp_data)
2513 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2514
ad0ecd61
JC
2515 if (ha->edc_data)
2516 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2517
1da177e4
LT
2518 if (ha->ms_iocb)
2519 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2520
b64b0e8f
AV
2521 if (ha->ex_init_cb)
2522 dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
2523
1da177e4
LT
2524 if (ha->s_dma_pool)
2525 dma_pool_destroy(ha->s_dma_pool);
2526
1da177e4
LT
2527 if (ha->gid_list)
2528 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
e315cd28 2529 ha->gid_list_dma);
1da177e4 2530
e315cd28
AC
2531 if (ha->init_cb)
2532 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
2533 ha->init_cb, ha->init_cb_dma);
2534 vfree(ha->optrom_buffer);
2535 kfree(ha->nvram);
73208dfd 2536 kfree(ha->npiv_info);
1da177e4 2537
e8711085 2538 ha->srb_mempool = NULL;
a7a167bf
AV
2539 ha->eft = NULL;
2540 ha->eft_dma = 0;
1da177e4
LT
2541 ha->sns_cmd = NULL;
2542 ha->sns_cmd_dma = 0;
2543 ha->ct_sns = NULL;
2544 ha->ct_sns_dma = 0;
2545 ha->ms_iocb = NULL;
2546 ha->ms_iocb_dma = 0;
1da177e4
LT
2547 ha->init_cb = NULL;
2548 ha->init_cb_dma = 0;
b64b0e8f
AV
2549 ha->ex_init_cb = NULL;
2550 ha->ex_init_cb_dma = 0;
1da177e4
LT
2551
2552 ha->s_dma_pool = NULL;
2553
1da177e4
LT
2554 ha->gid_list = NULL;
2555 ha->gid_list_dma = 0;
2556
e315cd28
AC
2557 ha->fw_dump = NULL;
2558 ha->fw_dumped = 0;
2559 ha->fw_dump_reading = 0;
e315cd28 2560}
1da177e4 2561
e315cd28
AC
2562struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2563 struct qla_hw_data *ha)
2564{
2565 struct Scsi_Host *host;
2566 struct scsi_qla_host *vha = NULL;
854165f4 2567
e315cd28
AC
2568 host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2569 if (host == NULL) {
2570 printk(KERN_WARNING
2571 "qla2xxx: Couldn't allocate host from scsi layer!\n");
2572 goto fail;
2573 }
2574
2575 /* Clear our data area */
2576 vha = shost_priv(host);
2577 memset(vha, 0, sizeof(scsi_qla_host_t));
2578
2579 vha->host = host;
2580 vha->host_no = host->host_no;
2581 vha->hw = ha;
2582
2583 INIT_LIST_HEAD(&vha->vp_fcports);
2584 INIT_LIST_HEAD(&vha->work_list);
2585 INIT_LIST_HEAD(&vha->list);
2586
f999f4c1
AV
2587 spin_lock_init(&vha->work_lock);
2588
e315cd28
AC
2589 sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2590 return vha;
2591
2592fail:
2593 return vha;
1da177e4
LT
2594}
2595
01ef66bb 2596static struct qla_work_evt *
f999f4c1 2597qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
0971de7f
AV
2598{
2599 struct qla_work_evt *e;
2600
f999f4c1 2601 e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
0971de7f
AV
2602 if (!e)
2603 return NULL;
2604
2605 INIT_LIST_HEAD(&e->list);
2606 e->type = type;
2607 e->flags = QLA_EVT_FLAG_FREE;
2608 return e;
2609}
2610
01ef66bb 2611static int
f999f4c1 2612qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
0971de7f 2613{
f999f4c1 2614 unsigned long flags;
0971de7f 2615
f999f4c1 2616 spin_lock_irqsave(&vha->work_lock, flags);
e315cd28 2617 list_add_tail(&e->list, &vha->work_list);
f999f4c1 2618 spin_unlock_irqrestore(&vha->work_lock, flags);
e315cd28 2619 qla2xxx_wake_dpc(vha);
f999f4c1 2620
0971de7f
AV
2621 return QLA_SUCCESS;
2622}
2623
2624int
e315cd28 2625qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
0971de7f
AV
2626 u32 data)
2627{
2628 struct qla_work_evt *e;
2629
f999f4c1 2630 e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
0971de7f
AV
2631 if (!e)
2632 return QLA_FUNCTION_FAILED;
2633
2634 e->u.aen.code = code;
2635 e->u.aen.data = data;
f999f4c1 2636 return qla2x00_post_work(vha, e);
0971de7f
AV
2637}
2638
8a659571
AV
2639int
2640qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
2641{
2642 struct qla_work_evt *e;
2643
f999f4c1 2644 e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
8a659571
AV
2645 if (!e)
2646 return QLA_FUNCTION_FAILED;
2647
2648 memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
f999f4c1 2649 return qla2x00_post_work(vha, e);
8a659571
AV
2650}
2651
ac280b67
AV
2652#define qla2x00_post_async_work(name, type) \
2653int qla2x00_post_async_##name##_work( \
2654 struct scsi_qla_host *vha, \
2655 fc_port_t *fcport, uint16_t *data) \
2656{ \
2657 struct qla_work_evt *e; \
2658 \
2659 e = qla2x00_alloc_work(vha, type); \
2660 if (!e) \
2661 return QLA_FUNCTION_FAILED; \
2662 \
2663 e->u.logio.fcport = fcport; \
2664 if (data) { \
2665 e->u.logio.data[0] = data[0]; \
2666 e->u.logio.data[1] = data[1]; \
2667 } \
2668 return qla2x00_post_work(vha, e); \
2669}
2670
2671qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
2672qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
2673qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
2674qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
2675
3420d36c
AV
2676int
2677qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
2678{
2679 struct qla_work_evt *e;
2680
2681 e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
2682 if (!e)
2683 return QLA_FUNCTION_FAILED;
2684
2685 e->u.uevent.code = code;
2686 return qla2x00_post_work(vha, e);
2687}
2688
2689static void
2690qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
2691{
2692 char event_string[40];
2693 char *envp[] = { event_string, NULL };
2694
2695 switch (code) {
2696 case QLA_UEVENT_CODE_FW_DUMP:
2697 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
2698 vha->host_no);
2699 break;
2700 default:
2701 /* do nothing */
2702 break;
2703 }
2704 kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
2705}
2706
ac280b67 2707void
e315cd28 2708qla2x00_do_work(struct scsi_qla_host *vha)
0971de7f 2709{
f999f4c1
AV
2710 struct qla_work_evt *e, *tmp;
2711 unsigned long flags;
2712 LIST_HEAD(work);
0971de7f 2713
f999f4c1
AV
2714 spin_lock_irqsave(&vha->work_lock, flags);
2715 list_splice_init(&vha->work_list, &work);
2716 spin_unlock_irqrestore(&vha->work_lock, flags);
2717
2718 list_for_each_entry_safe(e, tmp, &work, list) {
0971de7f 2719 list_del_init(&e->list);
0971de7f
AV
2720
2721 switch (e->type) {
2722 case QLA_EVT_AEN:
e315cd28 2723 fc_host_post_event(vha->host, fc_get_event_number(),
0971de7f
AV
2724 e->u.aen.code, e->u.aen.data);
2725 break;
8a659571
AV
2726 case QLA_EVT_IDC_ACK:
2727 qla81xx_idc_ack(vha, e->u.idc_ack.mb);
2728 break;
ac280b67
AV
2729 case QLA_EVT_ASYNC_LOGIN:
2730 qla2x00_async_login(vha, e->u.logio.fcport,
2731 e->u.logio.data);
2732 break;
2733 case QLA_EVT_ASYNC_LOGIN_DONE:
2734 qla2x00_async_login_done(vha, e->u.logio.fcport,
2735 e->u.logio.data);
2736 break;
2737 case QLA_EVT_ASYNC_LOGOUT:
2738 qla2x00_async_logout(vha, e->u.logio.fcport);
2739 break;
2740 case QLA_EVT_ASYNC_LOGOUT_DONE:
2741 qla2x00_async_logout_done(vha, e->u.logio.fcport,
2742 e->u.logio.data);
2743 break;
3420d36c
AV
2744 case QLA_EVT_UEVENT:
2745 qla2x00_uevent_emit(vha, e->u.uevent.code);
2746 break;
0971de7f
AV
2747 }
2748 if (e->flags & QLA_EVT_FLAG_FREE)
2749 kfree(e);
e315cd28 2750 }
e315cd28 2751}
f999f4c1 2752
e315cd28
AC
2753/* Relogins all the fcports of a vport
2754 * Context: dpc thread
2755 */
2756void qla2x00_relogin(struct scsi_qla_host *vha)
2757{
2758 fc_port_t *fcport;
c6b2fca8 2759 int status;
e315cd28
AC
2760 uint16_t next_loopid = 0;
2761 struct qla_hw_data *ha = vha->hw;
ac280b67 2762 uint16_t data[2];
e315cd28
AC
2763
2764 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2765 /*
2766 * If the port is not ONLINE then try to login
2767 * to it if we haven't run out of retries.
2768 */
2769 if (atomic_read(&fcport->state) !=
2770 FCS_ONLINE && fcport->login_retry) {
2771
ac280b67 2772 fcport->login_retry--;
e315cd28
AC
2773 if (fcport->flags & FCF_FABRIC_DEVICE) {
2774 if (fcport->flags & FCF_TAPE_PRESENT)
2775 ha->isp_ops->fabric_logout(vha,
2776 fcport->loop_id,
2777 fcport->d_id.b.domain,
2778 fcport->d_id.b.area,
2779 fcport->d_id.b.al_pa);
2780
ac280b67
AV
2781 if (IS_ALOGIO_CAPABLE(ha)) {
2782 data[0] = 0;
2783 data[1] = QLA_LOGIO_LOGIN_RETRIED;
2784 status = qla2x00_post_async_login_work(
2785 vha, fcport, data);
2786 if (status == QLA_SUCCESS)
2787 continue;
2788 /* Attempt a retry. */
2789 status = 1;
2790 } else
2791 status = qla2x00_fabric_login(vha,
2792 fcport, &next_loopid);
e315cd28
AC
2793 } else
2794 status = qla2x00_local_device_login(vha,
2795 fcport);
2796
e315cd28
AC
2797 if (status == QLA_SUCCESS) {
2798 fcport->old_loop_id = fcport->loop_id;
2799
2800 DEBUG(printk("scsi(%ld): port login OK: logged "
2801 "in ID 0x%x\n", vha->host_no, fcport->loop_id));
2802
2803 qla2x00_update_fcport(vha, fcport);
2804
2805 } else if (status == 1) {
2806 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2807 /* retry the login again */
2808 DEBUG(printk("scsi(%ld): Retrying"
2809 " %d login again loop_id 0x%x\n",
2810 vha->host_no, fcport->login_retry,
2811 fcport->loop_id));
2812 } else {
2813 fcport->login_retry = 0;
2814 }
2815
2816 if (fcport->login_retry == 0 && status != QLA_SUCCESS)
2817 fcport->loop_id = FC_NO_LOOP_ID;
2818 }
2819 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2820 break;
0971de7f 2821 }
0971de7f
AV
2822}
2823
1da177e4
LT
2824/**************************************************************************
2825* qla2x00_do_dpc
2826* This kernel thread is a task that is schedule by the interrupt handler
2827* to perform the background processing for interrupts.
2828*
2829* Notes:
2830* This task always run in the context of a kernel thread. It
2831* is kick-off by the driver's detect code and starts up
2832* up one per adapter. It immediately goes to sleep and waits for
2833* some fibre event. When either the interrupt handler or
2834* the timer routine detects a event it will one of the task
2835* bits then wake us up.
2836**************************************************************************/
2837static int
2838qla2x00_do_dpc(void *data)
2839{
2c3dfe3f 2840 int rval;
e315cd28
AC
2841 scsi_qla_host_t *base_vha;
2842 struct qla_hw_data *ha;
1da177e4 2843
e315cd28
AC
2844 ha = (struct qla_hw_data *)data;
2845 base_vha = pci_get_drvdata(ha->pdev);
1da177e4 2846
1da177e4
LT
2847 set_user_nice(current, -20);
2848
39a11240 2849 while (!kthread_should_stop()) {
1da177e4
LT
2850 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2851
39a11240
CH
2852 set_current_state(TASK_INTERRUPTIBLE);
2853 schedule();
2854 __set_current_state(TASK_RUNNING);
1da177e4
LT
2855
2856 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2857
2858 /* Initialization not yet finished. Don't do anything yet. */
e315cd28 2859 if (!base_vha->flags.init_done)
1da177e4
LT
2860 continue;
2861
e315cd28 2862 DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
1da177e4
LT
2863
2864 ha->dpc_active = 1;
2865
1da177e4 2866 if (ha->flags.mbox_busy) {
1da177e4
LT
2867 ha->dpc_active = 0;
2868 continue;
2869 }
2870
e315cd28 2871 qla2x00_do_work(base_vha);
0971de7f 2872
e315cd28
AC
2873 if (test_and_clear_bit(ISP_ABORT_NEEDED,
2874 &base_vha->dpc_flags)) {
1da177e4
LT
2875
2876 DEBUG(printk("scsi(%ld): dpc: sched "
2877 "qla2x00_abort_isp ha = %p\n",
e315cd28 2878 base_vha->host_no, ha));
1da177e4 2879 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
e315cd28 2880 &base_vha->dpc_flags))) {
1da177e4 2881
e315cd28 2882 if (qla2x00_abort_isp(base_vha)) {
1da177e4
LT
2883 /* failed. retry later */
2884 set_bit(ISP_ABORT_NEEDED,
e315cd28 2885 &base_vha->dpc_flags);
99363ef8 2886 }
e315cd28
AC
2887 clear_bit(ABORT_ISP_ACTIVE,
2888 &base_vha->dpc_flags);
99363ef8
SJ
2889 }
2890
1da177e4 2891 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
e315cd28 2892 base_vha->host_no));
1da177e4
LT
2893 }
2894
e315cd28
AC
2895 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
2896 qla2x00_update_fcports(base_vha);
2897 clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
c9c5ced9 2898 }
d97994dc 2899
e315cd28
AC
2900 if (test_and_clear_bit(RESET_MARKER_NEEDED,
2901 &base_vha->dpc_flags) &&
2902 (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
1da177e4
LT
2903
2904 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
e315cd28 2905 base_vha->host_no));
1da177e4 2906
e315cd28
AC
2907 qla2x00_rst_aen(base_vha);
2908 clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
1da177e4
LT
2909 }
2910
2911 /* Retry each device up to login retry count */
e315cd28
AC
2912 if ((test_and_clear_bit(RELOGIN_NEEDED,
2913 &base_vha->dpc_flags)) &&
2914 !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
2915 atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
1da177e4
LT
2916
2917 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
e315cd28
AC
2918 base_vha->host_no));
2919 qla2x00_relogin(base_vha);
2920
1da177e4 2921 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
e315cd28 2922 base_vha->host_no));
1da177e4
LT
2923 }
2924
e315cd28
AC
2925 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
2926 &base_vha->dpc_flags)) {
1da177e4
LT
2927
2928 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
e315cd28 2929 base_vha->host_no));
1da177e4
LT
2930
2931 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
e315cd28 2932 &base_vha->dpc_flags))) {
1da177e4 2933
e315cd28 2934 rval = qla2x00_loop_resync(base_vha);
1da177e4 2935
e315cd28
AC
2936 clear_bit(LOOP_RESYNC_ACTIVE,
2937 &base_vha->dpc_flags);
1da177e4
LT
2938 }
2939
2940 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
e315cd28 2941 base_vha->host_no));
1da177e4
LT
2942 }
2943
e315cd28
AC
2944 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
2945 atomic_read(&base_vha->loop_state) == LOOP_READY) {
2946 clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
2947 qla2xxx_flash_npiv_conf(base_vha);
272976ca
AV
2948 }
2949
1da177e4 2950 if (!ha->interrupts_on)
fd34f556 2951 ha->isp_ops->enable_intrs(ha);
1da177e4 2952
e315cd28
AC
2953 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
2954 &base_vha->dpc_flags))
2955 ha->isp_ops->beacon_blink(base_vha);
f6df144c 2956
e315cd28 2957 qla2x00_do_dpc_all_vps(base_vha);
2c3dfe3f 2958
1da177e4
LT
2959 ha->dpc_active = 0;
2960 } /* End of while(1) */
2961
e315cd28 2962 DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
1da177e4
LT
2963
2964 /*
2965 * Make sure that nobody tries to wake us up again.
2966 */
1da177e4
LT
2967 ha->dpc_active = 0;
2968
ac280b67
AV
2969 /* Cleanup any residual CTX SRBs. */
2970 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2971
39a11240
CH
2972 return 0;
2973}
2974
2975void
e315cd28 2976qla2xxx_wake_dpc(struct scsi_qla_host *vha)
39a11240 2977{
e315cd28 2978 struct qla_hw_data *ha = vha->hw;
c795c1e4
AV
2979 struct task_struct *t = ha->dpc_thread;
2980
e315cd28 2981 if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
c795c1e4 2982 wake_up_process(t);
1da177e4
LT
2983}
2984
1da177e4
LT
2985/*
2986* qla2x00_rst_aen
2987* Processes asynchronous reset.
2988*
2989* Input:
2990* ha = adapter block pointer.
2991*/
2992static void
e315cd28 2993qla2x00_rst_aen(scsi_qla_host_t *vha)
1da177e4 2994{
e315cd28
AC
2995 if (vha->flags.online && !vha->flags.reset_active &&
2996 !atomic_read(&vha->loop_down_timer) &&
2997 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
1da177e4 2998 do {
e315cd28 2999 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1da177e4
LT
3000
3001 /*
3002 * Issue marker command only when we are going to start
3003 * the I/O.
3004 */
e315cd28
AC
3005 vha->marker_needed = 1;
3006 } while (!atomic_read(&vha->loop_down_timer) &&
3007 (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
1da177e4
LT
3008 }
3009}
3010
f4f051eb 3011static void
e315cd28 3012qla2x00_sp_free_dma(srb_t *sp)
f4f051eb
AV
3013{
3014 struct scsi_cmnd *cmd = sp->cmd;
3015
3016 if (sp->flags & SRB_DMA_VALID) {
385d70b4 3017 scsi_dma_unmap(cmd);
f4f051eb
AV
3018 sp->flags &= ~SRB_DMA_VALID;
3019 }
fca29703 3020 CMD_SP(cmd) = NULL;
f4f051eb
AV
3021}
3022
3023void
73208dfd 3024qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
f4f051eb
AV
3025{
3026 struct scsi_cmnd *cmd = sp->cmd;
3027
e315cd28 3028 qla2x00_sp_free_dma(sp);
f4f051eb 3029
f4f051eb
AV
3030 mempool_free(sp, ha->srb_mempool);
3031
3032 cmd->scsi_done(cmd);
3033}
bdf79621 3034
1da177e4
LT
3035/**************************************************************************
3036* qla2x00_timer
3037*
3038* Description:
3039* One second timer
3040*
3041* Context: Interrupt
3042***************************************************************************/
2c3dfe3f 3043void
e315cd28 3044qla2x00_timer(scsi_qla_host_t *vha)
1da177e4 3045{
1da177e4
LT
3046 unsigned long cpu_flags = 0;
3047 fc_port_t *fcport;
1da177e4
LT
3048 int start_dpc = 0;
3049 int index;
3050 srb_t *sp;
f4f051eb 3051 int t;
e315cd28 3052 struct qla_hw_data *ha = vha->hw;
73208dfd 3053 struct req_que *req;
1da177e4
LT
3054 /*
3055 * Ports - Port down timer.
3056 *
3057 * Whenever, a port is in the LOST state we start decrementing its port
3058 * down timer every second until it reaches zero. Once it reaches zero
fa2a1ce5 3059 * the port it marked DEAD.
1da177e4
LT
3060 */
3061 t = 0;
e315cd28 3062 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
3063 if (fcport->port_type != FCT_TARGET)
3064 continue;
3065
3066 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3067
3068 if (atomic_read(&fcport->port_down_timer) == 0)
3069 continue;
3070
fa2a1ce5 3071 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
1da177e4 3072 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
fa2a1ce5 3073
1da177e4 3074 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
fca29703 3075 "%d remaining\n",
e315cd28 3076 vha->host_no,
1da177e4
LT
3077 t, atomic_read(&fcport->port_down_timer)));
3078 }
3079 t++;
3080 } /* End of for fcport */
3081
1da177e4
LT
3082
3083 /* Loop down handler. */
e315cd28
AC
3084 if (atomic_read(&vha->loop_down_timer) > 0 &&
3085 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3086 && vha->flags.online) {
1da177e4 3087
e315cd28
AC
3088 if (atomic_read(&vha->loop_down_timer) ==
3089 vha->loop_down_abort_time) {
1da177e4
LT
3090
3091 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3092 "queues before time expire\n",
e315cd28 3093 vha->host_no));
1da177e4 3094
e315cd28
AC
3095 if (!IS_QLA2100(ha) && vha->link_down_timeout)
3096 atomic_set(&vha->loop_state, LOOP_DEAD);
1da177e4
LT
3097
3098 /* Schedule an ISP abort to return any tape commands. */
2c3dfe3f 3099 /* NPIV - scan physical port only */
e315cd28 3100 if (!vha->vp_idx) {
2c3dfe3f
SJ
3101 spin_lock_irqsave(&ha->hardware_lock,
3102 cpu_flags);
73208dfd 3103 req = ha->req_q_map[0];
2c3dfe3f
SJ
3104 for (index = 1;
3105 index < MAX_OUTSTANDING_COMMANDS;
3106 index++) {
3107 fc_port_t *sfcp;
3108
e315cd28 3109 sp = req->outstanding_cmds[index];
2c3dfe3f
SJ
3110 if (!sp)
3111 continue;
cf53b069
AV
3112 if (sp->ctx)
3113 continue;
2c3dfe3f
SJ
3114 sfcp = sp->fcport;
3115 if (!(sfcp->flags & FCF_TAPE_PRESENT))
3116 continue;
bdf79621 3117
2c3dfe3f 3118 set_bit(ISP_ABORT_NEEDED,
e315cd28 3119 &vha->dpc_flags);
2c3dfe3f
SJ
3120 break;
3121 }
3122 spin_unlock_irqrestore(&ha->hardware_lock,
e315cd28 3123 cpu_flags);
1da177e4 3124 }
1da177e4
LT
3125 start_dpc++;
3126 }
3127
3128 /* if the loop has been down for 4 minutes, reinit adapter */
e315cd28 3129 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
0d6e61bc 3130 if (!(vha->device_flags & DFLG_NO_CABLE)) {
1da177e4
LT
3131 DEBUG(printk("scsi(%ld): Loop down - "
3132 "aborting ISP.\n",
e315cd28 3133 vha->host_no));
1da177e4
LT
3134 qla_printk(KERN_WARNING, ha,
3135 "Loop down - aborting ISP.\n");
3136
e315cd28 3137 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1da177e4
LT
3138 }
3139 }
fca29703 3140 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
e315cd28
AC
3141 vha->host_no,
3142 atomic_read(&vha->loop_down_timer)));
1da177e4
LT
3143 }
3144
f6df144c
AV
3145 /* Check if beacon LED needs to be blinked */
3146 if (ha->beacon_blink_led == 1) {
e315cd28 3147 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
f6df144c
AV
3148 start_dpc++;
3149 }
3150
550bf57d 3151 /* Process any deferred work. */
e315cd28 3152 if (!list_empty(&vha->work_list))
550bf57d
AV
3153 start_dpc++;
3154
1da177e4 3155 /* Schedule the DPC routine if needed */
e315cd28
AC
3156 if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3157 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3158 test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
1da177e4 3159 start_dpc ||
e315cd28
AC
3160 test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3161 test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
3162 test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3163 test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
3164 qla2xxx_wake_dpc(vha);
1da177e4 3165
e315cd28 3166 qla2x00_restart_timer(vha, WATCH_INTERVAL);
1da177e4
LT
3167}
3168
5433383e
AV
3169/* Firmware interface routines. */
3170
3a03eb79 3171#define FW_BLOBS 7
5433383e
AV
3172#define FW_ISP21XX 0
3173#define FW_ISP22XX 1
3174#define FW_ISP2300 2
3175#define FW_ISP2322 3
48c02fde 3176#define FW_ISP24XX 4
c3a2f0df 3177#define FW_ISP25XX 5
3a03eb79 3178#define FW_ISP81XX 6
5433383e 3179
bb8ee499
AV
3180#define FW_FILE_ISP21XX "ql2100_fw.bin"
3181#define FW_FILE_ISP22XX "ql2200_fw.bin"
3182#define FW_FILE_ISP2300 "ql2300_fw.bin"
3183#define FW_FILE_ISP2322 "ql2322_fw.bin"
3184#define FW_FILE_ISP24XX "ql2400_fw.bin"
c3a2f0df 3185#define FW_FILE_ISP25XX "ql2500_fw.bin"
3a03eb79 3186#define FW_FILE_ISP81XX "ql8100_fw.bin"
bb8ee499 3187
e1e82b6f 3188static DEFINE_MUTEX(qla_fw_lock);
5433383e
AV
3189
3190static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
bb8ee499
AV
3191 { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3192 { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3193 { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3194 { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3195 { .name = FW_FILE_ISP24XX, },
c3a2f0df 3196 { .name = FW_FILE_ISP25XX, },
3a03eb79 3197 { .name = FW_FILE_ISP81XX, },
5433383e
AV
3198};
3199
3200struct fw_blob *
e315cd28 3201qla2x00_request_firmware(scsi_qla_host_t *vha)
5433383e 3202{
e315cd28 3203 struct qla_hw_data *ha = vha->hw;
5433383e
AV
3204 struct fw_blob *blob;
3205
3206 blob = NULL;
3207 if (IS_QLA2100(ha)) {
3208 blob = &qla_fw_blobs[FW_ISP21XX];
3209 } else if (IS_QLA2200(ha)) {
3210 blob = &qla_fw_blobs[FW_ISP22XX];
48c02fde 3211 } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
5433383e 3212 blob = &qla_fw_blobs[FW_ISP2300];
48c02fde 3213 } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
5433383e 3214 blob = &qla_fw_blobs[FW_ISP2322];
4d4df193 3215 } else if (IS_QLA24XX_TYPE(ha)) {
5433383e 3216 blob = &qla_fw_blobs[FW_ISP24XX];
c3a2f0df
AV
3217 } else if (IS_QLA25XX(ha)) {
3218 blob = &qla_fw_blobs[FW_ISP25XX];
3a03eb79
AV
3219 } else if (IS_QLA81XX(ha)) {
3220 blob = &qla_fw_blobs[FW_ISP81XX];
5433383e
AV
3221 }
3222
e1e82b6f 3223 mutex_lock(&qla_fw_lock);
5433383e
AV
3224 if (blob->fw)
3225 goto out;
3226
3227 if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3228 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
e315cd28 3229 "(%s).\n", vha->host_no, blob->name));
5433383e
AV
3230 blob->fw = NULL;
3231 blob = NULL;
3232 goto out;
3233 }
3234
3235out:
e1e82b6f 3236 mutex_unlock(&qla_fw_lock);
5433383e
AV
3237 return blob;
3238}
3239
3240static void
3241qla2x00_release_firmware(void)
3242{
3243 int idx;
3244
e1e82b6f 3245 mutex_lock(&qla_fw_lock);
5433383e
AV
3246 for (idx = 0; idx < FW_BLOBS; idx++)
3247 if (qla_fw_blobs[idx].fw)
3248 release_firmware(qla_fw_blobs[idx].fw);
e1e82b6f 3249 mutex_unlock(&qla_fw_lock);
5433383e
AV
3250}
3251
14e660e6
SJ
3252static pci_ers_result_t
3253qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3254{
b9b12f73
SJ
3255 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3256
14e660e6
SJ
3257 switch (state) {
3258 case pci_channel_io_normal:
3259 return PCI_ERS_RESULT_CAN_RECOVER;
3260 case pci_channel_io_frozen:
3261 pci_disable_device(pdev);
3262 return PCI_ERS_RESULT_NEED_RESET;
3263 case pci_channel_io_perm_failure:
b9b12f73 3264 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
14e660e6
SJ
3265 return PCI_ERS_RESULT_DISCONNECT;
3266 }
3267 return PCI_ERS_RESULT_NEED_RESET;
3268}
3269
3270static pci_ers_result_t
3271qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3272{
3273 int risc_paused = 0;
3274 uint32_t stat;
3275 unsigned long flags;
e315cd28
AC
3276 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3277 struct qla_hw_data *ha = base_vha->hw;
14e660e6
SJ
3278 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3279 struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3280
3281 spin_lock_irqsave(&ha->hardware_lock, flags);
3282 if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3283 stat = RD_REG_DWORD(&reg->hccr);
3284 if (stat & HCCR_RISC_PAUSE)
3285 risc_paused = 1;
3286 } else if (IS_QLA23XX(ha)) {
3287 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3288 if (stat & HSR_RISC_PAUSED)
3289 risc_paused = 1;
3290 } else if (IS_FWI2_CAPABLE(ha)) {
3291 stat = RD_REG_DWORD(&reg24->host_status);
3292 if (stat & HSRX_RISC_PAUSED)
3293 risc_paused = 1;
3294 }
3295 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3296
3297 if (risc_paused) {
3298 qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3299 "Dumping firmware!\n");
e315cd28 3300 ha->isp_ops->fw_dump(base_vha, 0);
14e660e6
SJ
3301
3302 return PCI_ERS_RESULT_NEED_RESET;
3303 } else
3304 return PCI_ERS_RESULT_RECOVERED;
3305}
3306
3307static pci_ers_result_t
3308qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3309{
3310 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
e315cd28
AC
3311 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3312 struct qla_hw_data *ha = base_vha->hw;
09483916
BH
3313 int rc;
3314
3315 if (ha->mem_only)
3316 rc = pci_enable_device_mem(pdev);
3317 else
3318 rc = pci_enable_device(pdev);
14e660e6 3319
09483916 3320 if (rc) {
14e660e6
SJ
3321 qla_printk(KERN_WARNING, ha,
3322 "Can't re-enable PCI device after reset.\n");
3323
3324 return ret;
3325 }
3326 pci_set_master(pdev);
3327
e315cd28 3328 if (ha->isp_ops->pci_config(base_vha))
14e660e6
SJ
3329 return ret;
3330
e315cd28
AC
3331 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3332 if (qla2x00_abort_isp(base_vha) == QLA_SUCCESS)
14e660e6 3333 ret = PCI_ERS_RESULT_RECOVERED;
e315cd28 3334 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
14e660e6
SJ
3335
3336 return ret;
3337}
3338
3339static void
3340qla2xxx_pci_resume(struct pci_dev *pdev)
3341{
e315cd28
AC
3342 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3343 struct qla_hw_data *ha = base_vha->hw;
14e660e6
SJ
3344 int ret;
3345
e315cd28 3346 ret = qla2x00_wait_for_hba_online(base_vha);
14e660e6
SJ
3347 if (ret != QLA_SUCCESS) {
3348 qla_printk(KERN_ERR, ha,
3349 "the device failed to resume I/O "
3350 "from slot/link_reset");
3351 }
3352 pci_cleanup_aer_uncorrect_error_status(pdev);
3353}
3354
3355static struct pci_error_handlers qla2xxx_err_handler = {
3356 .error_detected = qla2xxx_pci_error_detected,
3357 .mmio_enabled = qla2xxx_pci_mmio_enabled,
3358 .slot_reset = qla2xxx_pci_slot_reset,
3359 .resume = qla2xxx_pci_resume,
3360};
3361
5433383e 3362static struct pci_device_id qla2xxx_pci_tbl[] = {
47f5e069
AV
3363 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3364 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3365 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3366 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3367 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3368 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3369 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3370 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3371 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
4d4df193 3372 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
47f5e069
AV
3373 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3374 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
c3a2f0df 3375 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3a03eb79 3376 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
5433383e
AV
3377 { 0 },
3378};
3379MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3380
fca29703 3381static struct pci_driver qla2xxx_pci_driver = {
cb63067a 3382 .name = QLA2XXX_DRIVER_NAME,
0a21ef1e
JB
3383 .driver = {
3384 .owner = THIS_MODULE,
3385 },
fca29703 3386 .id_table = qla2xxx_pci_tbl,
7ee61397 3387 .probe = qla2x00_probe_one,
4c993f76 3388 .remove = qla2x00_remove_one,
14e660e6 3389 .err_handler = &qla2xxx_err_handler,
fca29703
AV
3390};
3391
1da177e4
LT
3392/**
3393 * qla2x00_module_init - Module initialization.
3394 **/
3395static int __init
3396qla2x00_module_init(void)
3397{
fca29703
AV
3398 int ret = 0;
3399
1da177e4 3400 /* Allocate cache for SRBs. */
354d6b21 3401 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
20c2df83 3402 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
3403 if (srb_cachep == NULL) {
3404 printk(KERN_ERR
3405 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3406 return -ENOMEM;
3407 }
3408
3409 /* Derive version string. */
3410 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
11010fec 3411 if (ql2xextended_error_logging)
0181944f
AV
3412 strcat(qla2x00_version_str, "-debug");
3413
1c97a12a
AV
3414 qla2xxx_transport_template =
3415 fc_attach_transport(&qla2xxx_transport_functions);
2c3dfe3f
SJ
3416 if (!qla2xxx_transport_template) {
3417 kmem_cache_destroy(srb_cachep);
1da177e4 3418 return -ENODEV;
2c3dfe3f
SJ
3419 }
3420 qla2xxx_transport_vport_template =
3421 fc_attach_transport(&qla2xxx_transport_vport_functions);
3422 if (!qla2xxx_transport_vport_template) {
3423 kmem_cache_destroy(srb_cachep);
3424 fc_release_transport(qla2xxx_transport_template);
1da177e4 3425 return -ENODEV;
2c3dfe3f 3426 }
1da177e4 3427
fd9a29f0
AV
3428 printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3429 qla2x00_version_str);
7ee61397 3430 ret = pci_register_driver(&qla2xxx_pci_driver);
fca29703
AV
3431 if (ret) {
3432 kmem_cache_destroy(srb_cachep);
3433 fc_release_transport(qla2xxx_transport_template);
2c3dfe3f 3434 fc_release_transport(qla2xxx_transport_vport_template);
fca29703
AV
3435 }
3436 return ret;
1da177e4
LT
3437}
3438
3439/**
3440 * qla2x00_module_exit - Module cleanup.
3441 **/
3442static void __exit
3443qla2x00_module_exit(void)
3444{
7ee61397 3445 pci_unregister_driver(&qla2xxx_pci_driver);
5433383e 3446 qla2x00_release_firmware();
354d6b21 3447 kmem_cache_destroy(srb_cachep);
1da177e4 3448 fc_release_transport(qla2xxx_transport_template);
2c3dfe3f 3449 fc_release_transport(qla2xxx_transport_vport_template);
1da177e4
LT
3450}
3451
3452module_init(qla2x00_module_init);
3453module_exit(qla2x00_module_exit);
3454
3455MODULE_AUTHOR("QLogic Corporation");
3456MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
3457MODULE_LICENSE("GPL");
3458MODULE_VERSION(QLA2XXX_VERSION);
bb8ee499
AV
3459MODULE_FIRMWARE(FW_FILE_ISP21XX);
3460MODULE_FIRMWARE(FW_FILE_ISP22XX);
3461MODULE_FIRMWARE(FW_FILE_ISP2300);
3462MODULE_FIRMWARE(FW_FILE_ISP2322);
3463MODULE_FIRMWARE(FW_FILE_ISP24XX);
61623fc3 3464MODULE_FIRMWARE(FW_FILE_ISP25XX);
3a03eb79 3465MODULE_FIRMWARE(FW_FILE_ISP81XX);