[SCSI] qla2xxx: Use dma_get_required_mask() in determining the 'ideal' DMA mask.
[linux-block.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 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 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 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 361 srb_t *sp;
362 int rval;
1da177e4 363
bdf79621 364 if (!fcport) {
f4f051eb 365 cmd->result = DID_NO_CONNECT << 16;
366 goto qc_fail_command;
367 }
1da177e4 368
f4f051eb 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 384 rval = qla2x00_start_scsi(sp);
385 if (rval != QLA_SUCCESS)
386 goto qc_host_busy_free_sp;
1da177e4 387
f4f051eb 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 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 402qc_host_busy_free_sp:
403 qla2x00_sp_free_dma(ha, sp);
f4f051eb 404 mempool_free(sp, ha->srb_mempool);
1da177e4 405
f4f051eb 406qc_host_busy_lock:
407 spin_lock_irq(ha->host->host_lock);
1da177e4 408
f4f051eb 409qc_host_busy:
410 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4 411
f4f051eb 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 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 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 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 610 if (!CMD_SP(cmd))
611 return FAILED;
1da177e4 612
f4f051eb 613 ret = FAILED;
1da177e4 614
f4f051eb 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 621 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
622 sp = ha->outstanding_cmds[i];
1da177e4 623
f4f051eb 624 if (sp == NULL)
625 continue;
1da177e4 626
f4f051eb 627 if (sp->cmd != cmd)
628 continue;
1da177e4 629
f4f051eb 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 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 646 break;
647 }
18e144d3 648 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4 649
f4f051eb 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 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 660 "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
661 id, lun, serial, ret);
1da177e4 662
f4f051eb 663 return ret;
664}
1da177e4 665
f4f051eb 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 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 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 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 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 761 if (qla2x00_device_reset(ha, fcport) == 0)
762 ret = SUCCESS;
1da177e4
LT
763
764#if defined(LOGOUT_AFTER_DEVICE_RESET)
f4f051eb 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 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 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 904 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
905 ret = SUCCESS;
1da177e4 906 }
f4f051eb 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 942 srb_t *sp;
943 int ret;
944 unsigned int id, lun;
945 unsigned long serial;
1da177e4 946
f4f051eb 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 984 if (qla2x00_eh_wait_for_pending_commands(ha))
985 ret = SUCCESS;
1da177e4 986
f4f051eb 987eh_host_reset_lock:
f4f051eb 988 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
989 (ret == FAILED) ? "failed" : "succeded");
1da177e4 990
f4f051eb 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 1015 list_for_each_entry(fcport, &ha->fcports, list) {
1016 if (fcport->port_type != FCT_TARGET)
1da177e4
LT
1017 continue;
1018
bdf79621 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 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 1081 return 0;
1082}
1da177e4 1083
f4f051eb 1084static int
1085qla2xxx_slave_configure(struct scsi_device *sdev)
1086{
8482e118 1087 scsi_qla_host_t *ha = to_qla_host(sdev->host);
1088 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1089
f4f051eb 1090 if (sdev->tagged_supported)
1091 scsi_activate_tcq(sdev, 32);
1092 else
1093 scsi_deactivate_tcq(sdev, 32);
1da177e4 1094
8482e118 1095 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1096
f4f051eb 1097 return 0;
1098}
1da177e4 1099
f4f051eb 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{
7524f9b9 1116 /* Assume a 32bit DMA mask. */
1da177e4 1117 ha->flags.enable_64bit_addressing = 0;
1da177e4 1118
7524f9b9
AV
1119 if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
1120 /* Any upper-dword bits set? */
1121 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1122 !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1123 /* Ok, a 64bit DMA mask is applicable. */
1da177e4 1124 ha->flags.enable_64bit_addressing = 1;
abbd8870
AV
1125 ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
1126 ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
7524f9b9 1127 return;
1da177e4 1128 }
1da177e4 1129 }
7524f9b9
AV
1130
1131 dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
1132 pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
1da177e4
LT
1133}
1134
1135static int
1136qla2x00_iospace_config(scsi_qla_host_t *ha)
1137{
1138 unsigned long pio, pio_len, pio_flags;
1139 unsigned long mmio, mmio_len, mmio_flags;
1140
1141 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1142 pio = pci_resource_start(ha->pdev, 0);
1143 pio_len = pci_resource_len(ha->pdev, 0);
1144 pio_flags = pci_resource_flags(ha->pdev, 0);
1145 if (pio_flags & IORESOURCE_IO) {
1146 if (pio_len < MIN_IOBASE_LEN) {
1147 qla_printk(KERN_WARNING, ha,
1148 "Invalid PCI I/O region size (%s)...\n",
1149 pci_name(ha->pdev));
1150 pio = 0;
1151 }
1152 } else {
1153 qla_printk(KERN_WARNING, ha,
1154 "region #0 not a PIO resource (%s)...\n",
1155 pci_name(ha->pdev));
1156 pio = 0;
1157 }
1158
1159 /* Use MMIO operations for all accesses. */
1160 mmio = pci_resource_start(ha->pdev, 1);
1161 mmio_len = pci_resource_len(ha->pdev, 1);
1162 mmio_flags = pci_resource_flags(ha->pdev, 1);
1163
1164 if (!(mmio_flags & IORESOURCE_MEM)) {
1165 qla_printk(KERN_ERR, ha,
1166 "region #0 not an MMIO resource (%s), aborting\n",
1167 pci_name(ha->pdev));
1168 goto iospace_error_exit;
1169 }
1170 if (mmio_len < MIN_IOBASE_LEN) {
1171 qla_printk(KERN_ERR, ha,
1172 "Invalid PCI mem region size (%s), aborting\n",
1173 pci_name(ha->pdev));
1174 goto iospace_error_exit;
1175 }
1176
1177 if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1178 qla_printk(KERN_WARNING, ha,
1179 "Failed to reserve PIO/MMIO regions (%s)\n",
1180 pci_name(ha->pdev));
1181
1182 goto iospace_error_exit;
1183 }
1184
1185 ha->pio_address = pio;
1186 ha->pio_length = pio_len;
1187 ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1188 if (!ha->iobase) {
1189 qla_printk(KERN_ERR, ha,
1190 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1191
1192 goto iospace_error_exit;
1193 }
1194
1195 return (0);
1196
1197iospace_error_exit:
1198 return (-ENOMEM);
1199}
1200
abbd8870
AV
1201static void
1202qla2x00_enable_intrs(scsi_qla_host_t *ha)
1203{
1204 unsigned long flags = 0;
3d71644c 1205 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
abbd8870
AV
1206
1207 spin_lock_irqsave(&ha->hardware_lock, flags);
1208 ha->interrupts_on = 1;
1209 /* enable risc and host interrupts */
1210 WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1211 RD_REG_WORD(&reg->ictrl);
1212 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1213
1214}
1215
1216static void
1217qla2x00_disable_intrs(scsi_qla_host_t *ha)
1218{
1219 unsigned long flags = 0;
3d71644c 1220 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
abbd8870
AV
1221
1222 spin_lock_irqsave(&ha->hardware_lock, flags);
1223 ha->interrupts_on = 0;
1224 /* disable risc and host interrupts */
1225 WRT_REG_WORD(&reg->ictrl, 0);
1226 RD_REG_WORD(&reg->ictrl);
1227 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1228}
1229
fca29703
AV
1230static void
1231qla24xx_enable_intrs(scsi_qla_host_t *ha)
1232{
1233 unsigned long flags = 0;
1234 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1235
1236 spin_lock_irqsave(&ha->hardware_lock, flags);
1237 ha->interrupts_on = 1;
1238 WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1239 RD_REG_DWORD(&reg->ictrl);
1240 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1241}
1242
1243static void
1244qla24xx_disable_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 = 0;
1251 WRT_REG_DWORD(&reg->ictrl, 0);
1252 RD_REG_DWORD(&reg->ictrl);
1253 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1254}
1255
1da177e4
LT
1256/*
1257 * PCI driver interface
1258 */
1259int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1260{
a1541d5a 1261 int ret = -ENODEV;
fca29703 1262 device_reg_t __iomem *reg;
1da177e4
LT
1263 struct Scsi_Host *host;
1264 scsi_qla_host_t *ha;
1265 unsigned long flags = 0;
1266 unsigned long wait_switch = 0;
1267 char pci_info[20];
1268 char fw_str[30];
8482e118 1269 fc_port_t *fcport;
1da177e4
LT
1270
1271 if (pci_enable_device(pdev))
a1541d5a 1272 goto probe_out;
1da177e4 1273
fca29703
AV
1274 host = scsi_host_alloc(brd_info->sht ? brd_info->sht:
1275 &qla2x00_driver_template, sizeof(scsi_qla_host_t));
1da177e4
LT
1276 if (host == NULL) {
1277 printk(KERN_WARNING
1278 "qla2xxx: Couldn't allocate host from scsi layer!\n");
1279 goto probe_disable_device;
1280 }
1281
1282 /* Clear our data area */
1283 ha = (scsi_qla_host_t *)host->hostdata;
1284 memset(ha, 0, sizeof(scsi_qla_host_t));
1285
1286 ha->pdev = pdev;
1287 ha->host = host;
1288 ha->host_no = host->host_no;
1289 ha->brd_info = brd_info;
1290 sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1291
1292 /* Configure PCI I/O space */
1293 ret = qla2x00_iospace_config(ha);
a1541d5a
AV
1294 if (ret)
1295 goto probe_failed;
1da177e4 1296
1da177e4
LT
1297 qla_printk(KERN_INFO, ha,
1298 "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
fca29703 1299 pdev->irq, ha->iobase);
1da177e4
LT
1300
1301 spin_lock_init(&ha->hardware_lock);
1302
1da177e4
LT
1303 ha->prev_topology = 0;
1304 ha->ports = MAX_BUSES;
fca29703 1305 ha->init_cb_size = sizeof(init_cb_t);
1da177e4 1306
abbd8870
AV
1307 /* Assign ISP specific operations. */
1308 ha->isp_ops.pci_config = qla2100_pci_config;
1309 ha->isp_ops.reset_chip = qla2x00_reset_chip;
1310 ha->isp_ops.chip_diag = qla2x00_chip_diag;
1311 ha->isp_ops.config_rings = qla2x00_config_rings;
1312 ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
1313 ha->isp_ops.nvram_config = qla2x00_nvram_config;
1314 ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
0107109e 1315 ha->isp_ops.load_risc = qla2x00_load_risc;
abbd8870
AV
1316 ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
1317 ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
1318 ha->isp_ops.intr_handler = qla2100_intr_handler;
1319 ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
1320 ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
1321 ha->isp_ops.abort_command = qla2x00_abort_command;
1322 ha->isp_ops.abort_target = qla2x00_abort_target;
1323 ha->isp_ops.fabric_login = qla2x00_login_fabric;
1324 ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
1325 ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
1326 ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
1327 ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
459c5378
AV
1328 ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
1329 ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
abbd8870
AV
1330 ha->isp_ops.fw_dump = qla2100_fw_dump;
1331 ha->isp_ops.ascii_fw_dump = qla2100_ascii_fw_dump;
1da177e4 1332 if (IS_QLA2100(ha)) {
354d6b21 1333 host->max_id = MAX_TARGETS_2100;
1da177e4
LT
1334 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1335 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1336 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1337 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1338 host->sg_tablesize = 32;
abbd8870 1339 ha->gid_list_info_size = 4;
1da177e4 1340 } else if (IS_QLA2200(ha)) {
354d6b21 1341 host->max_id = MAX_TARGETS_2200;
1da177e4
LT
1342 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1343 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1344 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1345 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
abbd8870 1346 ha->gid_list_info_size = 4;
fca29703 1347 } else if (IS_QLA23XX(ha)) {
354d6b21 1348 host->max_id = MAX_TARGETS_2200;
1da177e4
LT
1349 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1350 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1351 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1352 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
abbd8870
AV
1353 ha->isp_ops.pci_config = qla2300_pci_config;
1354 ha->isp_ops.intr_handler = qla2300_intr_handler;
1355 ha->isp_ops.fw_dump = qla2300_fw_dump;
1356 ha->isp_ops.ascii_fw_dump = qla2300_ascii_fw_dump;
1357 ha->gid_list_info_size = 6;
fca29703
AV
1358 } else if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1359 host->max_id = MAX_TARGETS_2200;
1360 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1361 ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
1362 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1363 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1364 ha->init_cb_size = sizeof(struct init_cb_24xx);
1365 ha->isp_ops.pci_config = qla24xx_pci_config;
1366 ha->isp_ops.reset_chip = qla24xx_reset_chip;
1367 ha->isp_ops.chip_diag = qla24xx_chip_diag;
1368 ha->isp_ops.config_rings = qla24xx_config_rings;
1369 ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
1370 ha->isp_ops.nvram_config = qla24xx_nvram_config;
1371 ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
1372 ha->isp_ops.load_risc = qla24xx_load_risc_flash;
1373 if (ql2xfwloadbin)
1374 ha->isp_ops.load_risc = qla24xx_load_risc_hotplug;
1375 ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
1376 ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
1377 ha->isp_ops.intr_handler = qla24xx_intr_handler;
1378 ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
1379 ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
1380 ha->isp_ops.abort_command = qla24xx_abort_command;
1381 ha->isp_ops.abort_target = qla24xx_abort_target;
1382 ha->isp_ops.fabric_login = qla24xx_login_fabric;
1383 ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
1384 ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
1385 ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
1386 ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
1387 ha->isp_ops.fw_dump = qla24xx_fw_dump;
1388 ha->isp_ops.ascii_fw_dump = qla24xx_ascii_fw_dump;
1389 ha->gid_list_info_size = 8;
1da177e4
LT
1390 }
1391 host->can_queue = ha->request_q_length + 128;
1392
1393 /* load the F/W, read paramaters, and init the H/W */
1394 ha->instance = num_hosts;
1395
1396 init_MUTEX(&ha->mbx_cmd_sem);
1397 init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1398
1399 INIT_LIST_HEAD(&ha->list);
1400 INIT_LIST_HEAD(&ha->fcports);
1401 INIT_LIST_HEAD(&ha->rscn_fcports);
1da177e4
LT
1402
1403 /*
1404 * These locks are used to prevent more than one CPU
1405 * from modifying the queue at the same time. The
1406 * higher level "host_lock" will reduce most
1407 * contention for these locks.
1408 */
1409 spin_lock_init(&ha->mbx_reg_lock);
1da177e4
LT
1410
1411 ha->dpc_pid = -1;
1412 init_completion(&ha->dpc_inited);
1413 init_completion(&ha->dpc_exited);
1414
1415 qla2x00_config_dma_addressing(ha);
1416 if (qla2x00_mem_alloc(ha)) {
1417 qla_printk(KERN_WARNING, ha,
1418 "[ERROR] Failed to allocate memory for adapter\n");
1419
a1541d5a
AV
1420 ret = -ENOMEM;
1421 goto probe_failed;
1da177e4
LT
1422 }
1423
1424 if (qla2x00_initialize_adapter(ha) &&
1425 !(ha->device_flags & DFLG_NO_CABLE)) {
1426
1427 qla_printk(KERN_WARNING, ha,
1428 "Failed to initialize adapter\n");
1429
1430 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1431 "Adapter flags %x.\n",
1432 ha->host_no, ha->device_flags));
1433
a1541d5a 1434 ret = -ENODEV;
1da177e4
LT
1435 goto probe_failed;
1436 }
1437
1438 /*
1439 * Startup the kernel thread for this host adapter
1440 */
1441 ha->dpc_should_die = 0;
1442 ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1443 if (ha->dpc_pid < 0) {
1444 qla_printk(KERN_WARNING, ha,
1445 "Unable to start DPC thread!\n");
1446
a1541d5a 1447 ret = -ENODEV;
1da177e4
LT
1448 goto probe_failed;
1449 }
1450 wait_for_completion(&ha->dpc_inited);
1451
a1541d5a
AV
1452 host->this_id = 255;
1453 host->cmd_per_lun = 3;
1454 host->unique_id = ha->instance;
1455 host->max_cmd_len = MAX_CMDSZ;
1456 host->max_channel = ha->ports - 1;
1457 host->max_lun = MAX_LUNS;
1458 host->transportt = qla2xxx_transport_template;
1459
fca29703 1460 ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
abbd8870 1461 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
a1541d5a 1462 if (ret) {
1da177e4
LT
1463 qla_printk(KERN_WARNING, ha,
1464 "Failed to reserve interrupt %d already in use.\n",
fca29703 1465 pdev->irq);
1da177e4
LT
1466 goto probe_failed;
1467 }
fca29703 1468 host->irq = pdev->irq;
1da177e4
LT
1469
1470 /* Initialized the timer */
1471 qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1472
1473 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1474 ha->host_no, ha));
1475
abbd8870 1476 ha->isp_ops.disable_intrs(ha);
1da177e4 1477
1da177e4 1478 spin_lock_irqsave(&ha->hardware_lock, flags);
fca29703
AV
1479 reg = ha->iobase;
1480 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1481 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
1482 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
1483 } else {
1484 WRT_REG_WORD(&reg->isp.semaphore, 0);
1485 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
1486 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
1487
1488 /* Enable proper parity */
1489 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1490 if (IS_QLA2300(ha))
1491 /* SRAM parity */
1492 WRT_REG_WORD(&reg->isp.hccr,
1493 (HCCR_ENABLE_PARITY + 0x1));
1494 else
1495 /* SRAM, Instruction RAM and GP RAM parity */
1496 WRT_REG_WORD(&reg->isp.hccr,
1497 (HCCR_ENABLE_PARITY + 0x7));
1498 }
1da177e4
LT
1499 }
1500 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1501
abbd8870 1502 ha->isp_ops.enable_intrs(ha);
1da177e4
LT
1503
1504 /* v2.19.5b6 */
1505 /*
1506 * Wait around max loop_reset_delay secs for the devices to come
1507 * on-line. We don't want Linux scanning before we are ready.
1508 *
1509 */
1510 for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1511 time_before(jiffies,wait_switch) &&
1512 !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1513 && (ha->device_flags & SWITCH_FOUND) ;) {
1514
1515 qla2x00_check_fabric_devices(ha);
1516
1517 msleep(10);
1518 }
1519
a1541d5a 1520 pci_set_drvdata(pdev, ha);
1da177e4
LT
1521 ha->flags.init_done = 1;
1522 num_hosts++;
1523
a1541d5a
AV
1524 ret = scsi_add_host(host, &pdev->dev);
1525 if (ret)
1526 goto probe_failed;
1527
1528 qla2x00_alloc_sysfs_attr(ha);
1529
1530 qla2x00_init_host_attr(ha);
1531
1da177e4
LT
1532 qla_printk(KERN_INFO, ha, "\n"
1533 " QLogic Fibre Channel HBA Driver: %s\n"
1534 " QLogic %s - %s\n"
1535 " %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
1536 ha->model_number, ha->model_desc ? ha->model_desc: "",
abbd8870 1537 ha->brd_info->isp_name, ha->isp_ops.pci_info_str(ha, pci_info),
fca29703 1538 pci_name(pdev), ha->flags.enable_64bit_addressing ? '+': '-',
abbd8870 1539 ha->host_no, ha->isp_ops.fw_version_str(ha, fw_str));
1da177e4 1540
8482e118 1541 /* Go with fc_rport registration. */
1542 list_for_each_entry(fcport, &ha->fcports, list)
1543 qla2x00_reg_remote_port(ha, fcport);
1da177e4
LT
1544
1545 return 0;
1546
1547probe_failed:
bdf79621 1548 fc_remove_host(ha->host);
1549
1da177e4
LT
1550 qla2x00_free_device(ha);
1551
1552 scsi_host_put(host);
1553
1554probe_disable_device:
1555 pci_disable_device(pdev);
1556
a1541d5a
AV
1557probe_out:
1558 return ret;
1da177e4
LT
1559}
1560EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1561
1562void qla2x00_remove_one(struct pci_dev *pdev)
1563{
1564 scsi_qla_host_t *ha;
1565
1566 ha = pci_get_drvdata(pdev);
1567
8482e118 1568 qla2x00_free_sysfs_attr(ha);
1da177e4 1569
bdf79621 1570 fc_remove_host(ha->host);
1571
1da177e4
LT
1572 scsi_remove_host(ha->host);
1573
1574 qla2x00_free_device(ha);
1575
1576 scsi_host_put(ha->host);
1577
1578 pci_set_drvdata(pdev, NULL);
1579}
1580EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1581
1582static void
1583qla2x00_free_device(scsi_qla_host_t *ha)
1584{
1585 int ret;
1586
1587 /* Abort any outstanding IO descriptors. */
1588 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1589 qla2x00_cancel_io_descriptors(ha);
1590
1591 /* turn-off interrupts on the card */
1592 if (ha->interrupts_on)
abbd8870 1593 ha->isp_ops.disable_intrs(ha);
1da177e4
LT
1594
1595 /* Disable timer */
1596 if (ha->timer_active)
1597 qla2x00_stop_timer(ha);
1598
1599 /* Kill the kernel thread for this host */
1600 if (ha->dpc_pid >= 0) {
1601 ha->dpc_should_die = 1;
1602 wmb();
1603 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1604 if (ret) {
1605 qla_printk(KERN_ERR, ha,
1606 "Unable to signal DPC thread -- (%d)\n", ret);
1607
1608 /* TODO: SOMETHING MORE??? */
1609 } else {
1610 wait_for_completion(&ha->dpc_exited);
1611 }
1612 }
1613
1614 qla2x00_mem_free(ha);
1615
1616
1617 ha->flags.online = 0;
1618
1619 /* Detach interrupts */
1620 if (ha->pdev->irq)
1621 free_irq(ha->pdev->irq, ha);
1622
1623 /* release io space registers */
1624 if (ha->iobase)
1625 iounmap(ha->iobase);
1626 pci_release_regions(ha->pdev);
1627
1628 pci_disable_device(ha->pdev);
1629}
1630
1da177e4
LT
1631/*
1632 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1633 *
1634 * Input: ha = adapter block pointer. fcport = port structure pointer.
1635 *
1636 * Return: None.
1637 *
1638 * Context:
1639 */
1640void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1641 int do_login)
1642{
8482e118 1643 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1644 fc_remote_port_block(fcport->rport);
fa2a1ce5 1645 /*
1da177e4
LT
1646 * We may need to retry the login, so don't change the state of the
1647 * port but do the retries.
1648 */
1649 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1650 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1651
1652 if (!do_login)
1653 return;
1654
1655 if (fcport->login_retry == 0) {
1656 fcport->login_retry = ha->login_retry_count;
1657 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1658
1659 DEBUG(printk("scsi(%ld): Port login retry: "
1660 "%02x%02x%02x%02x%02x%02x%02x%02x, "
1661 "id = 0x%04x retry cnt=%d\n",
1662 ha->host_no,
1663 fcport->port_name[0],
1664 fcport->port_name[1],
1665 fcport->port_name[2],
1666 fcport->port_name[3],
1667 fcport->port_name[4],
1668 fcport->port_name[5],
1669 fcport->port_name[6],
1670 fcport->port_name[7],
1671 fcport->loop_id,
1672 fcport->login_retry));
1673 }
1674}
1675
1676/*
1677 * qla2x00_mark_all_devices_lost
1678 * Updates fcport state when device goes offline.
1679 *
1680 * Input:
1681 * ha = adapter block pointer.
1682 * fcport = port structure pointer.
1683 *
1684 * Return:
1685 * None.
1686 *
1687 * Context:
1688 */
1689void
fa2a1ce5 1690qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
1da177e4
LT
1691{
1692 fc_port_t *fcport;
1693
1694 list_for_each_entry(fcport, &ha->fcports, list) {
1695 if (fcport->port_type != FCT_TARGET)
1696 continue;
1697
1698 /*
1699 * No point in marking the device as lost, if the device is
1700 * already DEAD.
1701 */
1702 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1703 continue;
8482e118 1704 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1705 fc_remote_port_block(fcport->rport);
1da177e4
LT
1706 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1707 }
1708}
1709
1710/*
1711* qla2x00_mem_alloc
1712* Allocates adapter memory.
1713*
1714* Returns:
1715* 0 = success.
1716* 1 = failure.
1717*/
1718static uint8_t
1719qla2x00_mem_alloc(scsi_qla_host_t *ha)
1720{
1721 char name[16];
1722 uint8_t status = 1;
1723 int retry= 10;
1724
1725 do {
1726 /*
1727 * This will loop only once if everything goes well, else some
1728 * number of retries will be performed to get around a kernel
1729 * bug where available mem is not allocated until after a
1730 * little delay and a retry.
1731 */
1732 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1733 (ha->request_q_length + 1) * sizeof(request_t),
1734 &ha->request_dma, GFP_KERNEL);
1735 if (ha->request_ring == NULL) {
1736 qla_printk(KERN_WARNING, ha,
1737 "Memory Allocation failed - request_ring\n");
1738
1739 qla2x00_mem_free(ha);
1740 msleep(100);
1741
1742 continue;
1743 }
1744
1745 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1746 (ha->response_q_length + 1) * sizeof(response_t),
1747 &ha->response_dma, GFP_KERNEL);
1748 if (ha->response_ring == NULL) {
1749 qla_printk(KERN_WARNING, ha,
1750 "Memory Allocation failed - response_ring\n");
1751
1752 qla2x00_mem_free(ha);
1753 msleep(100);
1754
1755 continue;
1756 }
1757
1758 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1759 &ha->gid_list_dma, GFP_KERNEL);
1760 if (ha->gid_list == NULL) {
1761 qla_printk(KERN_WARNING, ha,
1762 "Memory Allocation failed - gid_list\n");
1763
1764 qla2x00_mem_free(ha);
1765 msleep(100);
1766
1767 continue;
1768 }
1769
1770 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1771 sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1772 if (ha->rlc_rsp == NULL) {
1773 qla_printk(KERN_WARNING, ha,
1774 "Memory Allocation failed - rlc");
1775
1776 qla2x00_mem_free(ha);
1777 msleep(100);
1778
1779 continue;
1780 }
1781
1782 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1783 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1784 DMA_POOL_SIZE, 8, 0);
1785 if (ha->s_dma_pool == NULL) {
1786 qla_printk(KERN_WARNING, ha,
1787 "Memory Allocation failed - s_dma_pool\n");
1788
1789 qla2x00_mem_free(ha);
1790 msleep(100);
1791
1792 continue;
1793 }
1794
1795 /* get consistent memory allocated for init control block */
1796 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1797 &ha->init_cb_dma);
1798 if (ha->init_cb == NULL) {
1799 qla_printk(KERN_WARNING, ha,
1800 "Memory Allocation failed - init_cb\n");
1801
1802 qla2x00_mem_free(ha);
1803 msleep(100);
1804
1805 continue;
1806 }
fca29703 1807 memset(ha->init_cb, 0, ha->init_cb_size);
1da177e4
LT
1808
1809 /* Get consistent memory allocated for Get Port Database cmd */
1810 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1811 &ha->iodesc_pd_dma);
1812 if (ha->iodesc_pd == NULL) {
1813 /* error */
1814 qla_printk(KERN_WARNING, ha,
1815 "Memory Allocation failed - iodesc_pd\n");
1816
1817 qla2x00_mem_free(ha);
1818 msleep(100);
1819
1820 continue;
1821 }
1822 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1823
1824 /* Allocate ioctl related memory. */
1825 if (qla2x00_alloc_ioctl_mem(ha)) {
1826 qla_printk(KERN_WARNING, ha,
1827 "Memory Allocation failed - ioctl_mem\n");
1828
1829 qla2x00_mem_free(ha);
1830 msleep(100);
1831
1832 continue;
1833 }
1834
1835 if (qla2x00_allocate_sp_pool(ha)) {
1836 qla_printk(KERN_WARNING, ha,
1837 "Memory Allocation failed - "
1838 "qla2x00_allocate_sp_pool()\n");
1839
1840 qla2x00_mem_free(ha);
1841 msleep(100);
1842
1843 continue;
1844 }
1845
1846 /* Allocate memory for SNS commands */
1847 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1848 /* Get consistent memory allocated for SNS commands */
1849 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1850 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1851 GFP_KERNEL);
1852 if (ha->sns_cmd == NULL) {
1853 /* error */
1854 qla_printk(KERN_WARNING, ha,
1855 "Memory Allocation failed - sns_cmd\n");
1856
1857 qla2x00_mem_free(ha);
1858 msleep(100);
1859
1860 continue;
1861 }
1862 memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1863 } else {
1864 /* Get consistent memory allocated for MS IOCB */
1865 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1866 &ha->ms_iocb_dma);
1867 if (ha->ms_iocb == NULL) {
1868 /* error */
1869 qla_printk(KERN_WARNING, ha,
1870 "Memory Allocation failed - ms_iocb\n");
1871
1872 qla2x00_mem_free(ha);
1873 msleep(100);
1874
1875 continue;
1876 }
1877 memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1878
1879 /*
1880 * Get consistent memory allocated for CT SNS
1881 * commands
1882 */
1883 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1884 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1885 GFP_KERNEL);
1886 if (ha->ct_sns == NULL) {
1887 /* error */
1888 qla_printk(KERN_WARNING, ha,
1889 "Memory Allocation failed - ct_sns\n");
1890
1891 qla2x00_mem_free(ha);
1892 msleep(100);
1893
1894 continue;
1895 }
1896 memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1897 }
1898
1899 /* Done all allocations without any error. */
1900 status = 0;
1901
1902 } while (retry-- && status != 0);
1903
1904 if (status) {
1905 printk(KERN_WARNING
1906 "%s(): **** FAILED ****\n", __func__);
1907 }
1908
1909 return(status);
1910}
1911
1912/*
1913* qla2x00_mem_free
1914* Frees all adapter allocated memory.
1915*
1916* Input:
1917* ha = adapter block pointer.
1918*/
1919static void
1920qla2x00_mem_free(scsi_qla_host_t *ha)
1921{
1da177e4
LT
1922 struct list_head *fcpl, *fcptemp;
1923 fc_port_t *fcport;
1da177e4
LT
1924 unsigned long wtime;/* max wait time if mbx cmd is busy. */
1925
1926 if (ha == NULL) {
1927 /* error */
1928 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1929 return;
1930 }
1931
1da177e4
LT
1932 /* Make sure all other threads are stopped. */
1933 wtime = 60 * HZ;
1934 while (ha->dpc_wait && wtime) {
1935 set_current_state(TASK_INTERRUPTIBLE);
1936 wtime = schedule_timeout(wtime);
1937 }
1938
1939 /* free ioctl memory */
1940 qla2x00_free_ioctl_mem(ha);
1941
1942 /* free sp pool */
1943 qla2x00_free_sp_pool(ha);
1944
1945 if (ha->sns_cmd)
1946 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1947 ha->sns_cmd, ha->sns_cmd_dma);
1948
1949 if (ha->ct_sns)
1950 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1951 ha->ct_sns, ha->ct_sns_dma);
1952
1953 if (ha->ms_iocb)
1954 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1955
1956 if (ha->iodesc_pd)
1957 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1958
1959 if (ha->init_cb)
1960 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1961
1962 if (ha->s_dma_pool)
1963 dma_pool_destroy(ha->s_dma_pool);
1964
1965 if (ha->rlc_rsp)
1966 dma_free_coherent(&ha->pdev->dev,
1967 sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1968 ha->rlc_rsp_dma);
1969
1970 if (ha->gid_list)
1971 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1972 ha->gid_list_dma);
1973
1974 if (ha->response_ring)
1975 dma_free_coherent(&ha->pdev->dev,
1976 (ha->response_q_length + 1) * sizeof(response_t),
1977 ha->response_ring, ha->response_dma);
1978
1979 if (ha->request_ring)
1980 dma_free_coherent(&ha->pdev->dev,
1981 (ha->request_q_length + 1) * sizeof(request_t),
1982 ha->request_ring, ha->request_dma);
1983
1984 ha->sns_cmd = NULL;
1985 ha->sns_cmd_dma = 0;
1986 ha->ct_sns = NULL;
1987 ha->ct_sns_dma = 0;
1988 ha->ms_iocb = NULL;
1989 ha->ms_iocb_dma = 0;
1990 ha->iodesc_pd = NULL;
1991 ha->iodesc_pd_dma = 0;
1992 ha->init_cb = NULL;
1993 ha->init_cb_dma = 0;
1994
1995 ha->s_dma_pool = NULL;
1996
1997 ha->rlc_rsp = NULL;
1998 ha->rlc_rsp_dma = 0;
1999 ha->gid_list = NULL;
2000 ha->gid_list_dma = 0;
2001
2002 ha->response_ring = NULL;
2003 ha->response_dma = 0;
2004 ha->request_ring = NULL;
2005 ha->request_dma = 0;
2006
2007 list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
2008 fcport = list_entry(fcpl, fc_port_t, list);
2009
1da177e4
LT
2010 /* fc ports */
2011 list_del_init(&fcport->list);
2012 kfree(fcport);
2013 }
2014 INIT_LIST_HEAD(&ha->fcports);
2015
2016 if (ha->fw_dump)
2017 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
2018
fca29703
AV
2019 vfree(ha->fw_dump24);
2020
2021 vfree(ha->fw_dump_buffer);
1da177e4
LT
2022
2023 ha->fw_dump = NULL;
fca29703
AV
2024 ha->fw_dump24 = NULL;
2025 ha->fw_dumped = 0;
1da177e4
LT
2026 ha->fw_dump_reading = 0;
2027 ha->fw_dump_buffer = NULL;
2028}
2029
2030/*
2031 * qla2x00_allocate_sp_pool
2032 * This routine is called during initialization to allocate
2033 * memory for local srb_t.
2034 *
2035 * Input:
2036 * ha = adapter block pointer.
2037 *
2038 * Context:
2039 * Kernel context.
fa2a1ce5 2040 *
1da177e4
LT
2041 * Note: Sets the ref_count for non Null sp to one.
2042 */
2043static int
fa2a1ce5 2044qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
1da177e4
LT
2045{
2046 int rval;
2047
2048 rval = QLA_SUCCESS;
2049 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2050 mempool_free_slab, srb_cachep);
2051 if (ha->srb_mempool == NULL) {
2052 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
2053 rval = QLA_FUNCTION_FAILED;
2054 }
2055 return (rval);
2056}
2057
2058/*
2059 * This routine frees all adapter allocated memory.
fa2a1ce5 2060 *
1da177e4
LT
2061 */
2062static void
fa2a1ce5 2063qla2x00_free_sp_pool( scsi_qla_host_t *ha)
1da177e4
LT
2064{
2065 if (ha->srb_mempool) {
2066 mempool_destroy(ha->srb_mempool);
2067 ha->srb_mempool = NULL;
2068 }
2069}
2070
2071/**************************************************************************
2072* qla2x00_do_dpc
2073* This kernel thread is a task that is schedule by the interrupt handler
2074* to perform the background processing for interrupts.
2075*
2076* Notes:
2077* This task always run in the context of a kernel thread. It
2078* is kick-off by the driver's detect code and starts up
2079* up one per adapter. It immediately goes to sleep and waits for
2080* some fibre event. When either the interrupt handler or
2081* the timer routine detects a event it will one of the task
2082* bits then wake us up.
2083**************************************************************************/
2084static int
2085qla2x00_do_dpc(void *data)
2086{
2087 DECLARE_MUTEX_LOCKED(sem);
2088 scsi_qla_host_t *ha;
2089 fc_port_t *fcport;
1da177e4 2090 uint8_t status;
1da177e4 2091 uint16_t next_loopid;
1da177e4
LT
2092
2093 ha = (scsi_qla_host_t *)data;
2094
2095 lock_kernel();
2096
2097 daemonize("%s_dpc", ha->host_str);
2098 allow_signal(SIGHUP);
2099
2100 ha->dpc_wait = &sem;
2101
2102 set_user_nice(current, -20);
2103
2104 unlock_kernel();
2105
2106 complete(&ha->dpc_inited);
2107
2108 while (1) {
2109 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2110
2111 if (down_interruptible(&sem))
2112 break;
2113
2114 if (ha->dpc_should_die)
2115 break;
2116
2117 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2118
2119 /* Initialization not yet finished. Don't do anything yet. */
2120 if (!ha->flags.init_done || ha->dpc_active)
2121 continue;
2122
2123 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
2124
2125 ha->dpc_active = 1;
2126
1da177e4 2127 if (ha->flags.mbox_busy) {
1da177e4
LT
2128 ha->dpc_active = 0;
2129 continue;
2130 }
2131
2132 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2133
2134 DEBUG(printk("scsi(%ld): dpc: sched "
2135 "qla2x00_abort_isp ha = %p\n",
2136 ha->host_no, ha));
2137 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2138 &ha->dpc_flags))) {
2139
2140 if (qla2x00_abort_isp(ha)) {
2141 /* failed. retry later */
2142 set_bit(ISP_ABORT_NEEDED,
2143 &ha->dpc_flags);
2144 }
2145 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
2146 }
2147 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2148 ha->host_no));
2149 }
2150
2151 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
2152 (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
2153
2154 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2155 ha->host_no));
2156
2157 qla2x00_rst_aen(ha);
2158 clear_bit(RESET_ACTIVE, &ha->dpc_flags);
2159 }
2160
2161 /* Retry each device up to login retry count */
2162 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2163 !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
2164 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2165
2166 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2167 ha->host_no));
2168
2169 next_loopid = 0;
2170 list_for_each_entry(fcport, &ha->fcports, list) {
2171 if (fcport->port_type != FCT_TARGET)
2172 continue;
2173
2174 /*
2175 * If the port is not ONLINE then try to login
2176 * to it if we haven't run out of retries.
2177 */
2178 if (atomic_read(&fcport->state) != FCS_ONLINE &&
2179 fcport->login_retry) {
2180
2181 fcport->login_retry--;
2182 if (fcport->flags & FCF_FABRIC_DEVICE) {
2183 if (fcport->flags &
2184 FCF_TAPE_PRESENT)
abbd8870 2185 ha->isp_ops.fabric_logout(
1c7c6357
AV
2186 ha, fcport->loop_id,
2187 fcport->d_id.b.domain,
2188 fcport->d_id.b.area,
2189 fcport->d_id.b.al_pa);
1da177e4
LT
2190 status = qla2x00_fabric_login(
2191 ha, fcport, &next_loopid);
2192 } else
2193 status =
2194 qla2x00_local_device_login(
2195 ha, fcport->loop_id);
2196
2197 if (status == QLA_SUCCESS) {
2198 fcport->old_loop_id = fcport->loop_id;
2199
2200 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
2201 ha->host_no, fcport->loop_id));
fa2a1ce5 2202
1da177e4
LT
2203 fcport->port_login_retry_count =
2204 ha->port_down_retry_count * PORT_RETRY_TIME;
2205 atomic_set(&fcport->state, FCS_ONLINE);
2206 atomic_set(&fcport->port_down_timer,
2207 ha->port_down_retry_count * PORT_RETRY_TIME);
2208
2209 fcport->login_retry = 0;
2210 } else if (status == 1) {
2211 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
2212 /* retry the login again */
2213 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
2214 ha->host_no,
2215 fcport->login_retry, fcport->loop_id));
2216 } else {
2217 fcport->login_retry = 0;
2218 }
2219 }
2220 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2221 break;
2222 }
2223 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2224 ha->host_no));
2225 }
2226
2227 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2228 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2229
2230 clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2231 DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2232 ha->host_no));
fa2a1ce5 2233
1da177e4
LT
2234 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2235
2236 DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2237 ha->host_no));
2238 }
2239
2240 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2241
2242 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2243 ha->host_no));
2244
2245 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2246 &ha->dpc_flags))) {
2247
2248 qla2x00_loop_resync(ha);
2249
2250 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2251 }
2252
2253 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2254 ha->host_no));
2255 }
2256
1da177e4
LT
2257 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2258
2259 DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2260 ha->host_no));
2261
2262 qla2x00_rescan_fcports(ha);
2263
2264 DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2265 "end.\n",
2266 ha->host_no));
2267 }
2268
1da177e4 2269 if (!ha->interrupts_on)
abbd8870 2270 ha->isp_ops.enable_intrs(ha);
1da177e4 2271
1da177e4
LT
2272 ha->dpc_active = 0;
2273 } /* End of while(1) */
2274
2275 DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2276
2277 /*
2278 * Make sure that nobody tries to wake us up again.
2279 */
2280 ha->dpc_wait = NULL;
2281 ha->dpc_active = 0;
2282
2283 complete_and_exit(&ha->dpc_exited, 0);
2284}
2285
1da177e4
LT
2286/*
2287* qla2x00_rst_aen
2288* Processes asynchronous reset.
2289*
2290* Input:
2291* ha = adapter block pointer.
2292*/
2293static void
fa2a1ce5 2294qla2x00_rst_aen(scsi_qla_host_t *ha)
1da177e4
LT
2295{
2296 if (ha->flags.online && !ha->flags.reset_active &&
2297 !atomic_read(&ha->loop_down_timer) &&
2298 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2299 do {
2300 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2301
2302 /*
2303 * Issue marker command only when we are going to start
2304 * the I/O.
2305 */
2306 ha->marker_needed = 1;
2307 } while (!atomic_read(&ha->loop_down_timer) &&
2308 (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2309 }
2310}
2311
f4f051eb 2312static void
2313qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2314{
2315 struct scsi_cmnd *cmd = sp->cmd;
2316
2317 if (sp->flags & SRB_DMA_VALID) {
2318 if (cmd->use_sg) {
2319 dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2320 cmd->use_sg, cmd->sc_data_direction);
2321 } else if (cmd->request_bufflen) {
2322 dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2323 cmd->request_bufflen, cmd->sc_data_direction);
2324 }
2325 sp->flags &= ~SRB_DMA_VALID;
2326 }
fca29703 2327 CMD_SP(cmd) = NULL;
f4f051eb 2328}
2329
2330void
2331qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2332{
2333 struct scsi_cmnd *cmd = sp->cmd;
2334
2335 qla2x00_sp_free_dma(ha, sp);
2336
f4f051eb 2337 mempool_free(sp, ha->srb_mempool);
2338
2339 cmd->scsi_done(cmd);
2340}
bdf79621 2341
1da177e4
LT
2342/**************************************************************************
2343* qla2x00_timer
2344*
2345* Description:
2346* One second timer
2347*
2348* Context: Interrupt
2349***************************************************************************/
2350static void
2351qla2x00_timer(scsi_qla_host_t *ha)
2352{
1da177e4
LT
2353 unsigned long cpu_flags = 0;
2354 fc_port_t *fcport;
1da177e4
LT
2355 int start_dpc = 0;
2356 int index;
2357 srb_t *sp;
f4f051eb 2358 int t;
1da177e4
LT
2359
2360 /*
2361 * Ports - Port down timer.
2362 *
2363 * Whenever, a port is in the LOST state we start decrementing its port
2364 * down timer every second until it reaches zero. Once it reaches zero
fa2a1ce5 2365 * the port it marked DEAD.
1da177e4
LT
2366 */
2367 t = 0;
2368 list_for_each_entry(fcport, &ha->fcports, list) {
2369 if (fcport->port_type != FCT_TARGET)
2370 continue;
2371
2372 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2373
2374 if (atomic_read(&fcport->port_down_timer) == 0)
2375 continue;
2376
fa2a1ce5 2377 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
1da177e4 2378 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
fa2a1ce5 2379
1da177e4 2380 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
fca29703 2381 "%d remaining\n",
1da177e4
LT
2382 ha->host_no,
2383 t, atomic_read(&fcport->port_down_timer)));
2384 }
2385 t++;
2386 } /* End of for fcport */
2387
1da177e4
LT
2388
2389 /* Loop down handler. */
2390 if (atomic_read(&ha->loop_down_timer) > 0 &&
2391 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2392
2393 if (atomic_read(&ha->loop_down_timer) ==
2394 ha->loop_down_abort_time) {
2395
2396 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2397 "queues before time expire\n",
2398 ha->host_no));
2399
2400 if (!IS_QLA2100(ha) && ha->link_down_timeout)
fa2a1ce5 2401 atomic_set(&ha->loop_state, LOOP_DEAD);
1da177e4
LT
2402
2403 /* Schedule an ISP abort to return any tape commands. */
2404 spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2405 for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2406 index++) {
bdf79621 2407 fc_port_t *sfcp;
2408
1da177e4
LT
2409 sp = ha->outstanding_cmds[index];
2410 if (!sp)
2411 continue;
bdf79621 2412 sfcp = sp->fcport;
2413 if (!(sfcp->flags & FCF_TAPE_PRESENT))
1da177e4
LT
2414 continue;
2415
2416 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2417 break;
2418 }
2419 spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2420
2421 set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2422 start_dpc++;
2423 }
2424
2425 /* if the loop has been down for 4 minutes, reinit adapter */
2426 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2427 DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2428 "restarting queues.\n",
2429 ha->host_no));
2430
2431 set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2432 start_dpc++;
2433
2434 if (!(ha->device_flags & DFLG_NO_CABLE)) {
2435 DEBUG(printk("scsi(%ld): Loop down - "
2436 "aborting ISP.\n",
2437 ha->host_no));
2438 qla_printk(KERN_WARNING, ha,
2439 "Loop down - aborting ISP.\n");
2440
2441 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2442 }
2443 }
fca29703 2444 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
1da177e4
LT
2445 ha->host_no,
2446 atomic_read(&ha->loop_down_timer)));
2447 }
2448
1da177e4
LT
2449 /* Schedule the DPC routine if needed */
2450 if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2451 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2452 start_dpc ||
2453 test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
f4f051eb 2454 test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
1da177e4
LT
2455 test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2456 ha->dpc_wait && !ha->dpc_active) {
2457
2458 up(ha->dpc_wait);
2459 }
2460
2461 qla2x00_restart_timer(ha, WATCH_INTERVAL);
2462}
2463
1da177e4
LT
2464/* XXX(hch): crude hack to emulate a down_timeout() */
2465int
2466qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2467{
2468 const unsigned int step = HZ/10;
2469
2470 do {
2471 if (!down_trylock(sema))
2472 return 0;
2473 set_current_state(TASK_INTERRUPTIBLE);
2474 if (schedule_timeout(step))
2475 break;
2476 } while ((timeout -= step) > 0);
2477
2478 return -ETIMEDOUT;
2479}
2480
fca29703
AV
2481static struct qla_board_info qla_board_tbl[] = {
2482 {
2483 .drv_name = "qla2400",
2484 .isp_name = "ISP2422",
2485 .fw_fname = "ql2400_fw.bin",
2486 .sht = &qla24xx_driver_template,
2487 },
2488 {
2489 .drv_name = "qla2400",
2490 .isp_name = "ISP2432",
2491 .fw_fname = "ql2400_fw.bin",
2492 .sht = &qla24xx_driver_template,
2493 },
2494};
2495
2496static struct pci_device_id qla2xxx_pci_tbl[] = {
2497 {
2498 .vendor = PCI_VENDOR_ID_QLOGIC,
2499 .device = PCI_DEVICE_ID_QLOGIC_ISP2422,
2500 .subvendor = PCI_ANY_ID,
2501 .subdevice = PCI_ANY_ID,
2502 .driver_data = (unsigned long)&qla_board_tbl[0],
2503 },
2504 {
2505 .vendor = PCI_VENDOR_ID_QLOGIC,
2506 .device = PCI_DEVICE_ID_QLOGIC_ISP2432,
2507 .subvendor = PCI_ANY_ID,
2508 .subdevice = PCI_ANY_ID,
2509 .driver_data = (unsigned long)&qla_board_tbl[1],
2510 },
2511 {0, 0},
2512};
2513MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
2514
2515static int __devinit
2516qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2517{
2518 return qla2x00_probe_one(pdev,
2519 (struct qla_board_info *)id->driver_data);
2520}
2521
2522static void __devexit
2523qla2xxx_remove_one(struct pci_dev *pdev)
2524{
2525 qla2x00_remove_one(pdev);
2526}
2527
2528static struct pci_driver qla2xxx_pci_driver = {
2529 .name = "qla2xxx",
2530 .id_table = qla2xxx_pci_tbl,
2531 .probe = qla2xxx_probe_one,
2532 .remove = __devexit_p(qla2xxx_remove_one),
2533};
2534
1da177e4
LT
2535/**
2536 * qla2x00_module_init - Module initialization.
2537 **/
2538static int __init
2539qla2x00_module_init(void)
2540{
fca29703
AV
2541 int ret = 0;
2542
1da177e4 2543 /* Allocate cache for SRBs. */
354d6b21 2544 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
1da177e4
LT
2545 SLAB_HWCACHE_ALIGN, NULL, NULL);
2546 if (srb_cachep == NULL) {
2547 printk(KERN_ERR
2548 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2549 return -ENOMEM;
2550 }
2551
2552 /* Derive version string. */
2553 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2554#if DEBUG_QLA2100
2555 strcat(qla2x00_version_str, "-debug");
2556#endif
1c97a12a
AV
2557 qla2xxx_transport_template =
2558 fc_attach_transport(&qla2xxx_transport_functions);
1da177e4
LT
2559 if (!qla2xxx_transport_template)
2560 return -ENODEV;
2561
2562 printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
fca29703
AV
2563 ret = pci_module_init(&qla2xxx_pci_driver);
2564 if (ret) {
2565 kmem_cache_destroy(srb_cachep);
2566 fc_release_transport(qla2xxx_transport_template);
2567 }
2568 return ret;
1da177e4
LT
2569}
2570
2571/**
2572 * qla2x00_module_exit - Module cleanup.
2573 **/
2574static void __exit
2575qla2x00_module_exit(void)
2576{
fca29703 2577 pci_unregister_driver(&qla2xxx_pci_driver);
354d6b21 2578 kmem_cache_destroy(srb_cachep);
1da177e4
LT
2579 fc_release_transport(qla2xxx_transport_template);
2580}
2581
2582module_init(qla2x00_module_init);
2583module_exit(qla2x00_module_exit);
2584
2585MODULE_AUTHOR("QLogic Corporation");
2586MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2587MODULE_LICENSE("GPL");
2588MODULE_VERSION(QLA2XXX_VERSION);