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