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