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