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