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