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