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