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