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