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