2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2004 QLogic Corporation
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
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.
21 #include <linux/moduleparam.h>
22 #include <linux/vmalloc.h>
23 #include <linux/smp_lock.h>
24 #include <linux/delay.h>
26 #include <scsi/scsi_tcq.h>
27 #include <scsi/scsicam.h>
28 #include <scsi/scsi_transport.h>
29 #include <scsi/scsi_transport_fc.h>
34 char qla2x00_version_str[40];
37 * SRB allocation cache
39 static kmem_cache_t *srb_cachep;
42 * Ioctl related information.
46 int ql2xlogintimeout = 20;
47 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
48 MODULE_PARM_DESC(ql2xlogintimeout,
49 "Login timeout value in seconds.");
51 int qlport_down_retry = 30;
52 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
53 MODULE_PARM_DESC(qlport_down_retry,
54 "Maximum number of command retries to a port that returns"
55 "a PORT-DOWN status.");
57 int ql2xplogiabsentdevice;
58 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
59 MODULE_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.");
64 int ql2xenablezio = 0;
65 module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
66 MODULE_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");
71 int ql2xintrdelaytimer = 10;
72 module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
73 MODULE_PARM_DESC(ql2xintrdelaytimer,
74 "ZIO: Waiting time for Firmware before it generates an "
75 "interrupt to the host to notify completion of request.");
77 int ql2xloginretrycount = 0;
78 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
79 MODULE_PARM_DESC(ql2xloginretrycount,
80 "Specify an alternate value for the NVRAM login retry count.");
82 static void qla2x00_free_device(scsi_qla_host_t *);
84 static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
87 * SCSI host template entry points
89 static int qla2xxx_slave_configure(struct scsi_device * device);
90 static int qla2xxx_slave_alloc(struct scsi_device *);
91 static void qla2xxx_slave_destroy(struct scsi_device *);
92 static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
93 void (*fn)(struct scsi_cmnd *));
94 static int qla2xxx_eh_abort(struct scsi_cmnd *);
95 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
96 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
97 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
98 static int qla2x00_loop_reset(scsi_qla_host_t *ha);
99 static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
101 static struct scsi_host_template qla2x00_driver_template = {
102 .module = THIS_MODULE,
104 .queuecommand = qla2x00_queuecommand,
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,
111 .slave_configure = qla2xxx_slave_configure,
113 .slave_alloc = qla2xxx_slave_alloc,
114 .slave_destroy = qla2xxx_slave_destroy,
117 .use_clustering = ENABLE_CLUSTERING,
118 .sg_tablesize = SG_ALL,
121 * The RISC allows for each command to transfer (2^32-1) bytes of data,
122 * which equates to 0x800000 sectors.
124 .max_sectors = 0xFFFF,
127 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
129 /* TODO Convert to inlines
133 #define WATCH_INTERVAL 1 /* number of seconds */
135 static void qla2x00_timer(scsi_qla_host_t *);
137 static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
138 void *, unsigned long);
139 static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
140 static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
143 qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
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;
154 qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
156 mod_timer(&ha->timer, jiffies + interval * HZ);
159 static __inline__ void
160 qla2x00_stop_timer(scsi_qla_host_t *ha)
162 del_timer_sync(&ha->timer);
163 ha->timer_active = 0;
166 static int qla2x00_do_dpc(void *data);
168 static void qla2x00_rst_aen(scsi_qla_host_t *);
170 static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
171 static void qla2x00_mem_free(scsi_qla_host_t *ha);
172 static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
173 static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
174 static srb_t *qla2x00_get_new_sp(scsi_qla_host_t *);
175 static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
176 void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
178 /* -------------------------------------------------------------------------- */
181 qla2x00_get_pci_info_str(struct scsi_qla_host *ha, char *str)
183 static char *pci_bus_modes[] = {
184 "33", "66", "100", "133",
189 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
192 strcat(str, pci_bus_modes[pci_bus]);
194 pci_bus = (ha->pci_attr & BIT_8) >> 8;
196 strcat(str, pci_bus_modes[pci_bus]);
198 strcat(str, " MHz)");
204 qla2x00_get_fw_version_str(struct scsi_qla_host *ha, char *str)
208 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
209 ha->fw_minor_version,
210 ha->fw_subminor_version);
212 if (ha->fw_attributes & BIT_9) {
217 switch (ha->fw_attributes & 0xFF) {
231 sprintf(un_str, "(%x)", ha->fw_attributes);
235 if (ha->fw_attributes & 0x100)
241 /**************************************************************************
242 * qla2x00_queuecommand
245 * Queue a command to the controller.
248 * cmd - pointer to Scsi cmd structure
249 * fn - pointer to Scsi done function
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
259 **************************************************************************/
261 qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
263 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
264 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
269 cmd->result = DID_NO_CONNECT << 16;
270 goto qc_fail_command;
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;
282 spin_unlock_irq(ha->host->host_lock);
284 /* Allocate a command packet from the "sp" pool. */
285 if ((sp = qla2x00_get_new_sp(ha)) == NULL) {
286 goto qc_host_busy_lock;
294 CMD_SP(cmd) = (void *)sp;
295 cmd->scsi_done = done;
297 rval = qla2x00_start_scsi(sp);
298 if (rval != QLA_SUCCESS)
299 goto qc_host_busy_free_sp;
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) {
306 spin_lock_irqsave(&ha->hardware_lock, flags);
307 qla2x00_process_response_queue(ha);
308 spin_unlock_irqrestore(&ha->hardware_lock, flags);
311 spin_lock_irq(ha->host->host_lock);
315 qc_host_busy_free_sp:
316 qla2x00_sp_free_dma(ha, sp);
318 mempool_free(sp, ha->srb_mempool);
321 spin_lock_irq(ha->host->host_lock);
324 return SCSI_MLQUEUE_HOST_BUSY;
333 * qla2x00_eh_wait_on_command
334 * Waits for the command to be returned by the Firmware for some
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)
348 qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
350 #define ABORT_POLLING_PERIOD HZ
351 #define ABORT_WAIT_ITER ((10 * HZ) / (ABORT_POLLING_PERIOD))
352 unsigned long wait_iter = ABORT_WAIT_ITER;
353 int ret = QLA_SUCCESS;
355 while (CMD_SP(cmd)) {
356 set_current_state(TASK_UNINTERRUPTIBLE);
357 schedule_timeout(ABORT_POLLING_PERIOD);
363 ret = QLA_FUNCTION_FAILED;
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
375 * ha - pointer to host adapter structure
378 * Does context switching-Release SPIN_LOCK
379 * (if any) before calling this routine.
382 * Success (Adapter is online) : 0
383 * Failed (Adapter is offline/disabled) : 1
386 qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
389 unsigned long wait_online;
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)) {
399 if (ha->flags.online)
400 return_status = QLA_SUCCESS;
402 return_status = QLA_FUNCTION_FAILED;
404 DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
406 return (return_status);
410 * qla2x00_wait_for_loop_ready
411 * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
412 * to be in LOOP_READY state.
414 * ha - pointer to host adapter structure
417 * Does context switching-Release SPIN_LOCK
418 * (if any) before calling this routine.
422 * Success (LOOP_READY) : 0
423 * Failed (LOOP_NOT_READY) : 1
426 qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
428 int return_status = QLA_SUCCESS;
429 unsigned long loop_timeout ;
431 /* wait for 5 min at the max for loop to be ready */
432 loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
434 while ((!atomic_read(&ha->loop_down_timer) &&
435 atomic_read(&ha->loop_state) == LOOP_DOWN) ||
436 atomic_read(&ha->loop_state) != LOOP_READY) {
438 if (time_after_eq(jiffies, loop_timeout)) {
439 return_status = QLA_FUNCTION_FAILED;
443 return (return_status);
446 /**************************************************************************
450 * The abort function will abort the specified command.
453 * cmd = Linux SCSI command packet to be aborted.
456 * Either SUCCESS or FAILED.
459 **************************************************************************/
461 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
463 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
466 unsigned int id, lun;
467 unsigned long serial;
474 id = cmd->device->id;
475 lun = cmd->device->lun;
476 serial = cmd->serial_number;
478 /* Check active list for command command. */
479 spin_unlock_irq(ha->host->host_lock);
480 spin_lock(&ha->hardware_lock);
481 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
482 sp = ha->outstanding_cmds[i];
490 DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
491 "sp->state=%x\n", __func__, ha->host_no, sp, serial,
493 DEBUG3(qla2x00_print_scsi_cmd(cmd);)
495 spin_unlock(&ha->hardware_lock);
496 if (qla2x00_abort_command(ha, sp)) {
497 DEBUG2(printk("%s(%ld): abort_command "
498 "mbx failed.\n", __func__, ha->host_no));
500 DEBUG3(printk("%s(%ld): abort_command "
501 "mbx success.\n", __func__, ha->host_no));
504 spin_lock(&ha->hardware_lock);
509 /* Wait for the command to be returned. */
510 if (ret == SUCCESS) {
511 spin_unlock(&ha->hardware_lock);
512 if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
513 qla_printk(KERN_ERR, ha,
514 "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
515 "%x.\n", ha->host_no, id, lun, serial, ret);
517 spin_lock(&ha->hardware_lock);
519 spin_lock_irq(ha->host->host_lock);
521 qla_printk(KERN_INFO, ha,
522 "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
523 id, lun, serial, ret);
528 /**************************************************************************
529 * qla2x00_eh_wait_for_pending_target_commands
532 * Waits for all the commands to come back from the specified target.
535 * ha - pointer to scsi_qla_host structure.
538 * Either SUCCESS or FAILED.
541 **************************************************************************/
543 qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
548 struct scsi_cmnd *cmd;
553 * Waiting for all commands for the designated target in the active
556 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
557 spin_lock(&ha->hardware_lock);
558 sp = ha->outstanding_cmds[cnt];
561 spin_unlock(&ha->hardware_lock);
562 if (cmd->device->id == t) {
563 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
569 spin_unlock(&ha->hardware_lock);
576 /**************************************************************************
577 * qla2xxx_eh_device_reset
580 * The device reset function will reset the target and abort any
581 * executing commands.
583 * NOTE: The use of SP is undefined within this context. Do *NOT*
584 * attempt to use this value, even if you determine it is
588 * cmd = Linux SCSI command packet of the command that cause the
592 * SUCCESS/FAILURE (defined as macro in scsi.h).
594 **************************************************************************/
596 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
598 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
599 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
602 unsigned int id, lun;
603 unsigned long serial;
607 id = cmd->device->id;
608 lun = cmd->device->lun;
609 serial = cmd->serial_number;
611 sp = (srb_t *) CMD_SP(cmd);
615 qla_printk(KERN_INFO, ha,
616 "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
618 spin_unlock_irq(ha->host->host_lock);
620 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
621 spin_lock_irq(ha->host->host_lock);
622 goto eh_dev_reset_done;
625 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
626 if (qla2x00_device_reset(ha, fcport) == 0)
629 #if defined(LOGOUT_AFTER_DEVICE_RESET)
630 if (ret == SUCCESS) {
631 if (fcport->flags & FC_FABRIC_DEVICE) {
632 qla2x00_fabric_logout(ha, fcport->loop_id);
633 qla2x00_mark_device_lost(ha, fcport);
638 DEBUG2(printk(KERN_INFO
639 "%s failed: loop not ready\n",__func__);)
643 DEBUG3(printk("%s(%ld): device reset failed\n",
644 __func__, ha->host_no));
645 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
648 goto eh_dev_reset_done;
652 * If we are coming down the EH path, wait for all commands to
653 * complete for the device.
655 if (cmd->device->host->eh_active) {
656 if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
660 DEBUG3(printk("%s(%ld): failed while waiting for "
661 "commands\n", __func__, ha->host_no));
662 qla_printk(KERN_INFO, ha,
663 "%s: failed while waiting for commands\n",
666 goto eh_dev_reset_done;
670 qla_printk(KERN_INFO, ha,
671 "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
674 spin_lock_irq(ha->host->host_lock);
679 /**************************************************************************
680 * qla2x00_eh_wait_for_pending_commands
683 * Waits for all the commands to come back from the specified host.
686 * ha - pointer to scsi_qla_host structure.
693 **************************************************************************/
695 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
700 struct scsi_cmnd *cmd;
705 * Waiting for all commands for the designated target in the active
708 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
709 spin_lock(&ha->hardware_lock);
710 sp = ha->outstanding_cmds[cnt];
713 spin_unlock(&ha->hardware_lock);
714 status = qla2x00_eh_wait_on_command(ha, cmd);
719 spin_unlock(&ha->hardware_lock);
726 /**************************************************************************
727 * qla2xxx_eh_bus_reset
730 * The bus reset function will reset the bus and abort any executing
734 * cmd = Linux SCSI command packet of the command that cause the
738 * SUCCESS/FAILURE (defined as macro in scsi.h).
740 **************************************************************************/
742 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
744 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
745 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
748 unsigned int id, lun;
749 unsigned long serial;
753 id = cmd->device->id;
754 lun = cmd->device->lun;
755 serial = cmd->serial_number;
757 sp = (srb_t *) CMD_SP(cmd);
761 qla_printk(KERN_INFO, ha,
762 "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
764 spin_unlock_irq(ha->host->host_lock);
766 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
767 DEBUG2(printk("%s failed:board disabled\n",__func__));
768 goto eh_bus_reset_done;
771 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
772 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
776 goto eh_bus_reset_done;
778 /* Waiting for our command in done_queue to be returned to OS.*/
779 if (cmd->device->host->eh_active)
780 if (!qla2x00_eh_wait_for_pending_commands(ha))
784 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
785 (ret == FAILED) ? "failed" : "succeded");
787 spin_lock_irq(ha->host->host_lock);
792 /**************************************************************************
793 * qla2xxx_eh_host_reset
796 * The reset function will reset the Adapter.
799 * cmd = Linux SCSI command packet of the command that cause the
803 * Either SUCCESS or FAILED.
806 **************************************************************************/
808 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
810 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
811 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
814 unsigned int id, lun;
815 unsigned long serial;
819 id = cmd->device->id;
820 lun = cmd->device->lun;
821 serial = cmd->serial_number;
823 sp = (srb_t *) CMD_SP(cmd);
827 qla_printk(KERN_INFO, ha,
828 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
830 spin_unlock_irq(ha->host->host_lock);
832 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
833 goto eh_host_reset_lock;
836 * Fixme-may be dpc thread is active and processing
837 * loop_resync,so wait a while for it to
838 * be completed and then issue big hammer.Otherwise
839 * it may cause I/O failure as big hammer marks the
840 * devices as lost kicking of the port_down_timer
841 * while dpc is stuck for the mailbox to complete.
843 qla2x00_wait_for_loop_ready(ha);
844 set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
845 if (qla2x00_abort_isp(ha)) {
846 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
847 /* failed. schedule dpc to try */
848 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
850 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
851 goto eh_host_reset_lock;
853 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
855 /* Waiting for our command in done_queue to be returned to OS.*/
856 if (qla2x00_eh_wait_for_pending_commands(ha))
860 spin_lock_irq(ha->host->host_lock);
862 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
863 (ret == FAILED) ? "failed" : "succeded");
873 * ha = adapter block pointer.
879 qla2x00_loop_reset(scsi_qla_host_t *ha)
881 int status = QLA_SUCCESS;
882 struct fc_port *fcport;
884 if (ha->flags.enable_lip_reset) {
885 status = qla2x00_lip_reset(ha);
888 if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
889 list_for_each_entry(fcport, &ha->fcports, list) {
890 if (fcport->port_type != FCT_TARGET)
893 status = qla2x00_target_reset(ha, fcport);
894 if (status != QLA_SUCCESS)
899 if (status == QLA_SUCCESS &&
900 ((!ha->flags.enable_target_reset &&
901 !ha->flags.enable_lip_reset) ||
902 ha->flags.enable_lip_full_login)) {
904 status = qla2x00_full_login_lip(ha);
907 /* Issue marker command only when we are going to start the I/O */
908 ha->marker_needed = 1;
912 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
917 DEBUG3(printk("%s(%ld): exiting normally.\n",
926 * qla2x00_device_reset
927 * Issue bus device reset message to the target.
930 * ha = adapter block pointer.
932 * TARGET_QUEUE_LOCK must be released.
933 * ADAPTER_STATE_LOCK must be released.
939 qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
941 /* Abort Target command will clear Reservation */
942 return qla2x00_abort_target(reset_fcport);
946 qla2xxx_slave_alloc(struct scsi_device *sdev)
948 scsi_qla_host_t *ha = to_qla_host(sdev->host);
949 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
957 list_for_each_entry(fcport, &ha->fcports, list) {
958 if (rport->port_name ==
959 be64_to_cpu(*(uint64_t *)fcport->port_name)) {
967 sdev->hostdata = fcport;
973 qla2xxx_slave_configure(struct scsi_device *sdev)
975 scsi_qla_host_t *ha = to_qla_host(sdev->host);
976 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
978 if (sdev->tagged_supported)
979 scsi_activate_tcq(sdev, 32);
981 scsi_deactivate_tcq(sdev, 32);
983 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
989 qla2xxx_slave_destroy(struct scsi_device *sdev)
991 sdev->hostdata = NULL;
995 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
998 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
999 * supported addressing method.
1002 qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1004 /* Assume 32bit DMA address */
1005 ha->flags.enable_64bit_addressing = 0;
1006 ha->calc_request_entries = qla2x00_calc_iocbs_32;
1007 ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_32;
1010 * Given the two variants pci_set_dma_mask(), allow the compiler to
1011 * assist in setting the proper dma mask.
1013 if (sizeof(dma_addr_t) > 4) {
1014 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1015 ha->flags.enable_64bit_addressing = 1;
1016 ha->calc_request_entries = qla2x00_calc_iocbs_64;
1017 ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_64;
1019 if (pci_set_consistent_dma_mask(ha->pdev,
1021 qla_printk(KERN_DEBUG, ha,
1022 "Failed to set 64 bit PCI consistent mask; "
1024 pci_set_consistent_dma_mask(ha->pdev,
1028 qla_printk(KERN_DEBUG, ha,
1029 "Failed to set 64 bit PCI DMA mask, falling back "
1030 "to 32 bit MASK.\n");
1031 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1034 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1039 qla2x00_iospace_config(scsi_qla_host_t *ha)
1041 unsigned long pio, pio_len, pio_flags;
1042 unsigned long mmio, mmio_len, mmio_flags;
1044 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1045 pio = pci_resource_start(ha->pdev, 0);
1046 pio_len = pci_resource_len(ha->pdev, 0);
1047 pio_flags = pci_resource_flags(ha->pdev, 0);
1048 if (pio_flags & IORESOURCE_IO) {
1049 if (pio_len < MIN_IOBASE_LEN) {
1050 qla_printk(KERN_WARNING, ha,
1051 "Invalid PCI I/O region size (%s)...\n",
1052 pci_name(ha->pdev));
1056 qla_printk(KERN_WARNING, ha,
1057 "region #0 not a PIO resource (%s)...\n",
1058 pci_name(ha->pdev));
1062 /* Use MMIO operations for all accesses. */
1063 mmio = pci_resource_start(ha->pdev, 1);
1064 mmio_len = pci_resource_len(ha->pdev, 1);
1065 mmio_flags = pci_resource_flags(ha->pdev, 1);
1067 if (!(mmio_flags & IORESOURCE_MEM)) {
1068 qla_printk(KERN_ERR, ha,
1069 "region #0 not an MMIO resource (%s), aborting\n",
1070 pci_name(ha->pdev));
1071 goto iospace_error_exit;
1073 if (mmio_len < MIN_IOBASE_LEN) {
1074 qla_printk(KERN_ERR, ha,
1075 "Invalid PCI mem region size (%s), aborting\n",
1076 pci_name(ha->pdev));
1077 goto iospace_error_exit;
1080 if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1081 qla_printk(KERN_WARNING, ha,
1082 "Failed to reserve PIO/MMIO regions (%s)\n",
1083 pci_name(ha->pdev));
1085 goto iospace_error_exit;
1088 ha->pio_address = pio;
1089 ha->pio_length = pio_len;
1090 ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1092 qla_printk(KERN_ERR, ha,
1093 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1095 goto iospace_error_exit;
1105 * PCI driver interface
1107 int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1110 device_reg_t __iomem *reg;
1111 struct Scsi_Host *host;
1112 scsi_qla_host_t *ha;
1113 unsigned long flags = 0;
1114 unsigned long wait_switch = 0;
1119 if (pci_enable_device(pdev))
1122 host = scsi_host_alloc(&qla2x00_driver_template,
1123 sizeof(scsi_qla_host_t));
1126 "qla2xxx: Couldn't allocate host from scsi layer!\n");
1127 goto probe_disable_device;
1130 /* Clear our data area */
1131 ha = (scsi_qla_host_t *)host->hostdata;
1132 memset(ha, 0, sizeof(scsi_qla_host_t));
1136 ha->host_no = host->host_no;
1137 ha->brd_info = brd_info;
1138 sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1140 /* Configure PCI I/O space */
1141 ret = qla2x00_iospace_config(ha);
1143 goto probe_alloc_failed;
1146 /* Sanitize the information from PCI BIOS. */
1147 host->irq = pdev->irq;
1149 qla_printk(KERN_INFO, ha,
1150 "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
1151 host->irq, ha->iobase);
1153 spin_lock_init(&ha->hardware_lock);
1155 ha->prev_topology = 0;
1156 ha->ports = MAX_BUSES;
1158 if (IS_QLA2100(ha)) {
1159 host->max_id = MAX_TARGETS_2100;
1160 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1161 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1162 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1163 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1164 host->sg_tablesize = 32;
1165 } else if (IS_QLA2200(ha)) {
1166 host->max_id = MAX_TARGETS_2200;
1167 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1168 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1169 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1170 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1171 } else /*if (IS_QLA2300(ha))*/ {
1172 host->max_id = MAX_TARGETS_2200;
1173 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1174 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1175 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1176 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1178 host->can_queue = ha->request_q_length + 128;
1180 /* load the F/W, read paramaters, and init the H/W */
1181 ha->instance = num_hosts;
1183 init_MUTEX(&ha->mbx_cmd_sem);
1184 init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1186 INIT_LIST_HEAD(&ha->list);
1187 INIT_LIST_HEAD(&ha->fcports);
1188 INIT_LIST_HEAD(&ha->rscn_fcports);
1191 * These locks are used to prevent more than one CPU
1192 * from modifying the queue at the same time. The
1193 * higher level "host_lock" will reduce most
1194 * contention for these locks.
1196 spin_lock_init(&ha->mbx_reg_lock);
1199 init_completion(&ha->dpc_inited);
1200 init_completion(&ha->dpc_exited);
1202 qla2x00_config_dma_addressing(ha);
1203 if (qla2x00_mem_alloc(ha)) {
1204 qla_printk(KERN_WARNING, ha,
1205 "[ERROR] Failed to allocate memory for adapter\n");
1207 goto probe_alloc_failed;
1210 pci_set_drvdata(pdev, ha);
1211 host->this_id = 255;
1212 host->cmd_per_lun = 3;
1213 host->unique_id = ha->instance;
1214 host->max_cmd_len = MAX_CMDSZ;
1215 host->max_channel = ha->ports - 1;
1216 host->max_lun = MAX_LUNS;
1217 host->transportt = qla2xxx_transport_template;
1218 if (scsi_add_host(host, &pdev->dev))
1219 goto probe_alloc_failed;
1221 qla2x00_alloc_sysfs_attr(ha);
1223 if (qla2x00_initialize_adapter(ha) &&
1224 !(ha->device_flags & DFLG_NO_CABLE)) {
1226 qla_printk(KERN_WARNING, ha,
1227 "Failed to initialize adapter\n");
1229 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1230 "Adapter flags %x.\n",
1231 ha->host_no, ha->device_flags));
1236 qla2x00_init_host_attr(ha);
1239 * Startup the kernel thread for this host adapter
1241 ha->dpc_should_die = 0;
1242 ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1243 if (ha->dpc_pid < 0) {
1244 qla_printk(KERN_WARNING, ha,
1245 "Unable to start DPC thread!\n");
1249 wait_for_completion(&ha->dpc_inited);
1251 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1252 ret = request_irq(host->irq, qla2100_intr_handler,
1253 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1255 ret = request_irq(host->irq, qla2300_intr_handler,
1256 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1258 qla_printk(KERN_WARNING, ha,
1259 "Failed to reserve interrupt %d already in use.\n",
1264 /* Initialized the timer */
1265 qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1267 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1272 /* Disable ISP interrupts. */
1273 qla2x00_disable_intrs(ha);
1275 /* Ensure mailbox registers are free. */
1276 spin_lock_irqsave(&ha->hardware_lock, flags);
1277 WRT_REG_WORD(®->semaphore, 0);
1278 WRT_REG_WORD(®->hccr, HCCR_CLR_RISC_INT);
1279 WRT_REG_WORD(®->hccr, HCCR_CLR_HOST_INT);
1281 /* Enable proper parity */
1282 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1285 WRT_REG_WORD(®->hccr, (HCCR_ENABLE_PARITY + 0x1));
1287 /* SRAM, Instruction RAM and GP RAM parity */
1288 WRT_REG_WORD(®->hccr, (HCCR_ENABLE_PARITY + 0x7));
1290 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1292 /* Enable chip interrupts. */
1293 qla2x00_enable_intrs(ha);
1297 * Wait around max loop_reset_delay secs for the devices to come
1298 * on-line. We don't want Linux scanning before we are ready.
1301 for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1302 time_before(jiffies,wait_switch) &&
1303 !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1304 && (ha->device_flags & SWITCH_FOUND) ;) {
1306 qla2x00_check_fabric_devices(ha);
1311 ha->flags.init_done = 1;
1314 qla_printk(KERN_INFO, ha, "\n"
1315 " QLogic Fibre Channel HBA Driver: %s\n"
1317 " %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
1318 ha->model_number, ha->model_desc ? ha->model_desc: "",
1319 ha->brd_info->isp_name, qla2x00_get_pci_info_str(ha, pci_info),
1320 pci_name(ha->pdev), ha->flags.enable_64bit_addressing ? '+': '-',
1321 ha->host_no, qla2x00_get_fw_version_str(ha, fw_str));
1323 /* Go with fc_rport registration. */
1324 list_for_each_entry(fcport, &ha->fcports, list)
1325 qla2x00_reg_remote_port(ha, fcport);
1330 fc_remove_host(ha->host);
1332 scsi_remove_host(host);
1335 qla2x00_free_device(ha);
1337 scsi_host_put(host);
1339 probe_disable_device:
1340 pci_disable_device(pdev);
1344 EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1346 void qla2x00_remove_one(struct pci_dev *pdev)
1348 scsi_qla_host_t *ha;
1350 ha = pci_get_drvdata(pdev);
1352 qla2x00_free_sysfs_attr(ha);
1354 fc_remove_host(ha->host);
1356 scsi_remove_host(ha->host);
1358 qla2x00_free_device(ha);
1360 scsi_host_put(ha->host);
1362 pci_set_drvdata(pdev, NULL);
1364 EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1367 qla2x00_free_device(scsi_qla_host_t *ha)
1371 /* Abort any outstanding IO descriptors. */
1372 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1373 qla2x00_cancel_io_descriptors(ha);
1375 /* turn-off interrupts on the card */
1376 if (ha->interrupts_on)
1377 qla2x00_disable_intrs(ha);
1380 if (ha->timer_active)
1381 qla2x00_stop_timer(ha);
1383 /* Kill the kernel thread for this host */
1384 if (ha->dpc_pid >= 0) {
1385 ha->dpc_should_die = 1;
1387 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1389 qla_printk(KERN_ERR, ha,
1390 "Unable to signal DPC thread -- (%d)\n", ret);
1392 /* TODO: SOMETHING MORE??? */
1394 wait_for_completion(&ha->dpc_exited);
1398 qla2x00_mem_free(ha);
1401 ha->flags.online = 0;
1403 /* Detach interrupts */
1405 free_irq(ha->pdev->irq, ha);
1407 /* release io space registers */
1409 iounmap(ha->iobase);
1410 pci_release_regions(ha->pdev);
1412 pci_disable_device(ha->pdev);
1416 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1418 * Input: ha = adapter block pointer. fcport = port structure pointer.
1424 void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1427 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1428 fc_remote_port_block(fcport->rport);
1430 * We may need to retry the login, so don't change the state of the
1431 * port but do the retries.
1433 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1434 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1439 if (fcport->login_retry == 0) {
1440 fcport->login_retry = ha->login_retry_count;
1441 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1443 DEBUG(printk("scsi(%ld): Port login retry: "
1444 "%02x%02x%02x%02x%02x%02x%02x%02x, "
1445 "id = 0x%04x retry cnt=%d\n",
1447 fcport->port_name[0],
1448 fcport->port_name[1],
1449 fcport->port_name[2],
1450 fcport->port_name[3],
1451 fcport->port_name[4],
1452 fcport->port_name[5],
1453 fcport->port_name[6],
1454 fcport->port_name[7],
1456 fcport->login_retry));
1461 * qla2x00_mark_all_devices_lost
1462 * Updates fcport state when device goes offline.
1465 * ha = adapter block pointer.
1466 * fcport = port structure pointer.
1474 qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
1478 list_for_each_entry(fcport, &ha->fcports, list) {
1479 if (fcport->port_type != FCT_TARGET)
1483 * No point in marking the device as lost, if the device is
1486 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1488 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1489 fc_remote_port_block(fcport->rport);
1490 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1496 * Allocates adapter memory.
1503 qla2x00_mem_alloc(scsi_qla_host_t *ha)
1511 * This will loop only once if everything goes well, else some
1512 * number of retries will be performed to get around a kernel
1513 * bug where available mem is not allocated until after a
1514 * little delay and a retry.
1516 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1517 (ha->request_q_length + 1) * sizeof(request_t),
1518 &ha->request_dma, GFP_KERNEL);
1519 if (ha->request_ring == NULL) {
1520 qla_printk(KERN_WARNING, ha,
1521 "Memory Allocation failed - request_ring\n");
1523 qla2x00_mem_free(ha);
1529 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1530 (ha->response_q_length + 1) * sizeof(response_t),
1531 &ha->response_dma, GFP_KERNEL);
1532 if (ha->response_ring == NULL) {
1533 qla_printk(KERN_WARNING, ha,
1534 "Memory Allocation failed - response_ring\n");
1536 qla2x00_mem_free(ha);
1542 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1543 &ha->gid_list_dma, GFP_KERNEL);
1544 if (ha->gid_list == NULL) {
1545 qla_printk(KERN_WARNING, ha,
1546 "Memory Allocation failed - gid_list\n");
1548 qla2x00_mem_free(ha);
1554 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1555 sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1556 if (ha->rlc_rsp == NULL) {
1557 qla_printk(KERN_WARNING, ha,
1558 "Memory Allocation failed - rlc");
1560 qla2x00_mem_free(ha);
1566 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1567 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1568 DMA_POOL_SIZE, 8, 0);
1569 if (ha->s_dma_pool == NULL) {
1570 qla_printk(KERN_WARNING, ha,
1571 "Memory Allocation failed - s_dma_pool\n");
1573 qla2x00_mem_free(ha);
1579 /* get consistent memory allocated for init control block */
1580 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1582 if (ha->init_cb == NULL) {
1583 qla_printk(KERN_WARNING, ha,
1584 "Memory Allocation failed - init_cb\n");
1586 qla2x00_mem_free(ha);
1591 memset(ha->init_cb, 0, sizeof(init_cb_t));
1593 /* Get consistent memory allocated for Get Port Database cmd */
1594 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1595 &ha->iodesc_pd_dma);
1596 if (ha->iodesc_pd == NULL) {
1598 qla_printk(KERN_WARNING, ha,
1599 "Memory Allocation failed - iodesc_pd\n");
1601 qla2x00_mem_free(ha);
1606 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1608 /* Allocate ioctl related memory. */
1609 if (qla2x00_alloc_ioctl_mem(ha)) {
1610 qla_printk(KERN_WARNING, ha,
1611 "Memory Allocation failed - ioctl_mem\n");
1613 qla2x00_mem_free(ha);
1619 if (qla2x00_allocate_sp_pool(ha)) {
1620 qla_printk(KERN_WARNING, ha,
1621 "Memory Allocation failed - "
1622 "qla2x00_allocate_sp_pool()\n");
1624 qla2x00_mem_free(ha);
1630 /* Allocate memory for SNS commands */
1631 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1632 /* Get consistent memory allocated for SNS commands */
1633 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1634 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1636 if (ha->sns_cmd == NULL) {
1638 qla_printk(KERN_WARNING, ha,
1639 "Memory Allocation failed - sns_cmd\n");
1641 qla2x00_mem_free(ha);
1646 memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1648 /* Get consistent memory allocated for MS IOCB */
1649 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1651 if (ha->ms_iocb == NULL) {
1653 qla_printk(KERN_WARNING, ha,
1654 "Memory Allocation failed - ms_iocb\n");
1656 qla2x00_mem_free(ha);
1661 memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1664 * Get consistent memory allocated for CT SNS
1667 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1668 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1670 if (ha->ct_sns == NULL) {
1672 qla_printk(KERN_WARNING, ha,
1673 "Memory Allocation failed - ct_sns\n");
1675 qla2x00_mem_free(ha);
1680 memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1683 /* Done all allocations without any error. */
1686 } while (retry-- && status != 0);
1690 "%s(): **** FAILED ****\n", __func__);
1698 * Frees all adapter allocated memory.
1701 * ha = adapter block pointer.
1704 qla2x00_mem_free(scsi_qla_host_t *ha)
1706 struct list_head *fcpl, *fcptemp;
1708 unsigned long wtime;/* max wait time if mbx cmd is busy. */
1712 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1716 /* Make sure all other threads are stopped. */
1718 while (ha->dpc_wait && wtime) {
1719 set_current_state(TASK_INTERRUPTIBLE);
1720 wtime = schedule_timeout(wtime);
1723 /* free ioctl memory */
1724 qla2x00_free_ioctl_mem(ha);
1727 qla2x00_free_sp_pool(ha);
1730 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1731 ha->sns_cmd, ha->sns_cmd_dma);
1734 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1735 ha->ct_sns, ha->ct_sns_dma);
1738 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1741 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1744 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1747 dma_pool_destroy(ha->s_dma_pool);
1750 dma_free_coherent(&ha->pdev->dev,
1751 sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1755 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1758 if (ha->response_ring)
1759 dma_free_coherent(&ha->pdev->dev,
1760 (ha->response_q_length + 1) * sizeof(response_t),
1761 ha->response_ring, ha->response_dma);
1763 if (ha->request_ring)
1764 dma_free_coherent(&ha->pdev->dev,
1765 (ha->request_q_length + 1) * sizeof(request_t),
1766 ha->request_ring, ha->request_dma);
1769 ha->sns_cmd_dma = 0;
1773 ha->ms_iocb_dma = 0;
1774 ha->iodesc_pd = NULL;
1775 ha->iodesc_pd_dma = 0;
1777 ha->init_cb_dma = 0;
1779 ha->s_dma_pool = NULL;
1782 ha->rlc_rsp_dma = 0;
1783 ha->gid_list = NULL;
1784 ha->gid_list_dma = 0;
1786 ha->response_ring = NULL;
1787 ha->response_dma = 0;
1788 ha->request_ring = NULL;
1789 ha->request_dma = 0;
1791 list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
1792 fcport = list_entry(fcpl, fc_port_t, list);
1795 list_del_init(&fcport->list);
1798 INIT_LIST_HEAD(&ha->fcports);
1801 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
1803 if (ha->fw_dump_buffer)
1804 vfree(ha->fw_dump_buffer);
1807 ha->fw_dump_reading = 0;
1808 ha->fw_dump_buffer = NULL;
1812 * qla2x00_allocate_sp_pool
1813 * This routine is called during initialization to allocate
1814 * memory for local srb_t.
1817 * ha = adapter block pointer.
1822 * Note: Sets the ref_count for non Null sp to one.
1825 qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
1830 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1831 mempool_free_slab, srb_cachep);
1832 if (ha->srb_mempool == NULL) {
1833 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
1834 rval = QLA_FUNCTION_FAILED;
1840 * This routine frees all adapter allocated memory.
1844 qla2x00_free_sp_pool( scsi_qla_host_t *ha)
1846 if (ha->srb_mempool) {
1847 mempool_destroy(ha->srb_mempool);
1848 ha->srb_mempool = NULL;
1852 /**************************************************************************
1854 * This kernel thread is a task that is schedule by the interrupt handler
1855 * to perform the background processing for interrupts.
1858 * This task always run in the context of a kernel thread. It
1859 * is kick-off by the driver's detect code and starts up
1860 * up one per adapter. It immediately goes to sleep and waits for
1861 * some fibre event. When either the interrupt handler or
1862 * the timer routine detects a event it will one of the task
1863 * bits then wake us up.
1864 **************************************************************************/
1866 qla2x00_do_dpc(void *data)
1868 DECLARE_MUTEX_LOCKED(sem);
1869 scsi_qla_host_t *ha;
1872 uint16_t next_loopid;
1874 ha = (scsi_qla_host_t *)data;
1878 daemonize("%s_dpc", ha->host_str);
1879 allow_signal(SIGHUP);
1881 ha->dpc_wait = &sem;
1883 set_user_nice(current, -20);
1887 complete(&ha->dpc_inited);
1890 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
1892 if (down_interruptible(&sem))
1895 if (ha->dpc_should_die)
1898 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
1900 /* Initialization not yet finished. Don't do anything yet. */
1901 if (!ha->flags.init_done || ha->dpc_active)
1904 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
1908 if (ha->flags.mbox_busy) {
1913 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
1915 DEBUG(printk("scsi(%ld): dpc: sched "
1916 "qla2x00_abort_isp ha = %p\n",
1918 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
1921 if (qla2x00_abort_isp(ha)) {
1922 /* failed. retry later */
1923 set_bit(ISP_ABORT_NEEDED,
1926 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
1928 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
1932 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
1933 (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
1935 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
1938 qla2x00_rst_aen(ha);
1939 clear_bit(RESET_ACTIVE, &ha->dpc_flags);
1942 /* Retry each device up to login retry count */
1943 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
1944 !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
1945 atomic_read(&ha->loop_state) != LOOP_DOWN) {
1947 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
1951 list_for_each_entry(fcport, &ha->fcports, list) {
1952 if (fcport->port_type != FCT_TARGET)
1956 * If the port is not ONLINE then try to login
1957 * to it if we haven't run out of retries.
1959 if (atomic_read(&fcport->state) != FCS_ONLINE &&
1960 fcport->login_retry) {
1962 fcport->login_retry--;
1963 if (fcport->flags & FCF_FABRIC_DEVICE) {
1966 qla2x00_fabric_logout(
1969 status = qla2x00_fabric_login(
1970 ha, fcport, &next_loopid);
1973 qla2x00_local_device_login(
1974 ha, fcport->loop_id);
1976 if (status == QLA_SUCCESS) {
1977 fcport->old_loop_id = fcport->loop_id;
1979 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
1980 ha->host_no, fcport->loop_id));
1982 fcport->port_login_retry_count =
1983 ha->port_down_retry_count * PORT_RETRY_TIME;
1984 atomic_set(&fcport->state, FCS_ONLINE);
1985 atomic_set(&fcport->port_down_timer,
1986 ha->port_down_retry_count * PORT_RETRY_TIME);
1988 fcport->login_retry = 0;
1989 } else if (status == 1) {
1990 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1991 /* retry the login again */
1992 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
1994 fcport->login_retry, fcport->loop_id));
1996 fcport->login_retry = 0;
1999 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2002 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2006 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2007 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2009 clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2010 DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2013 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2015 DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2019 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2021 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2024 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2027 qla2x00_loop_resync(ha);
2029 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2032 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2036 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2038 DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2041 qla2x00_rescan_fcports(ha);
2043 DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2048 if (!ha->interrupts_on)
2049 qla2x00_enable_intrs(ha);
2052 } /* End of while(1) */
2054 DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2057 * Make sure that nobody tries to wake us up again.
2059 ha->dpc_wait = NULL;
2062 complete_and_exit(&ha->dpc_exited, 0);
2067 * Processes asynchronous reset.
2070 * ha = adapter block pointer.
2073 qla2x00_rst_aen(scsi_qla_host_t *ha)
2075 if (ha->flags.online && !ha->flags.reset_active &&
2076 !atomic_read(&ha->loop_down_timer) &&
2077 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2079 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2082 * Issue marker command only when we are going to start
2085 ha->marker_needed = 1;
2086 } while (!atomic_read(&ha->loop_down_timer) &&
2087 (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2093 * This routine will allocate SP from the free queue
2100 qla2x00_get_new_sp(scsi_qla_host_t *ha)
2104 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2106 atomic_set(&sp->ref_count, 1);
2111 qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2113 struct scsi_cmnd *cmd = sp->cmd;
2115 if (sp->flags & SRB_DMA_VALID) {
2117 dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2118 cmd->use_sg, cmd->sc_data_direction);
2119 } else if (cmd->request_bufflen) {
2120 dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2121 cmd->request_bufflen, cmd->sc_data_direction);
2123 sp->flags &= ~SRB_DMA_VALID;
2128 qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2130 struct scsi_cmnd *cmd = sp->cmd;
2132 qla2x00_sp_free_dma(ha, sp);
2135 mempool_free(sp, ha->srb_mempool);
2137 cmd->scsi_done(cmd);
2140 /**************************************************************************
2146 * Context: Interrupt
2147 ***************************************************************************/
2149 qla2x00_timer(scsi_qla_host_t *ha)
2151 unsigned long cpu_flags = 0;
2159 * Ports - Port down timer.
2161 * Whenever, a port is in the LOST state we start decrementing its port
2162 * down timer every second until it reaches zero. Once it reaches zero
2163 * the port it marked DEAD.
2166 list_for_each_entry(fcport, &ha->fcports, list) {
2167 if (fcport->port_type != FCT_TARGET)
2170 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2172 if (atomic_read(&fcport->port_down_timer) == 0)
2175 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
2176 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2178 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2181 t, atomic_read(&fcport->port_down_timer)));
2184 } /* End of for fcport */
2187 /* Loop down handler. */
2188 if (atomic_read(&ha->loop_down_timer) > 0 &&
2189 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2191 if (atomic_read(&ha->loop_down_timer) ==
2192 ha->loop_down_abort_time) {
2194 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2195 "queues before time expire\n",
2198 if (!IS_QLA2100(ha) && ha->link_down_timeout)
2199 atomic_set(&ha->loop_state, LOOP_DEAD);
2201 /* Schedule an ISP abort to return any tape commands. */
2202 spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2203 for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2207 sp = ha->outstanding_cmds[index];
2211 if (!(sfcp->flags & FCF_TAPE_PRESENT))
2214 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2217 spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2219 set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2223 /* if the loop has been down for 4 minutes, reinit adapter */
2224 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2225 DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2226 "restarting queues.\n",
2229 set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2232 if (!(ha->device_flags & DFLG_NO_CABLE)) {
2233 DEBUG(printk("scsi(%ld): Loop down - "
2236 qla_printk(KERN_WARNING, ha,
2237 "Loop down - aborting ISP.\n");
2239 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2242 DEBUG3(printk("scsi(%ld): Loop Down - seconds remainning %d\n",
2244 atomic_read(&ha->loop_down_timer)));
2247 /* Schedule the DPC routine if needed */
2248 if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2249 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2251 test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
2252 test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
2253 test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2254 ha->dpc_wait && !ha->dpc_active) {
2259 qla2x00_restart_timer(ha, WATCH_INTERVAL);
2262 /* XXX(hch): crude hack to emulate a down_timeout() */
2264 qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2266 const unsigned int step = HZ/10;
2269 if (!down_trylock(sema))
2271 set_current_state(TASK_INTERRUPTIBLE);
2272 if (schedule_timeout(step))
2274 } while ((timeout -= step) > 0);
2280 * qla2x00_module_init - Module initialization.
2283 qla2x00_module_init(void)
2285 /* Allocate cache for SRBs. */
2286 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
2287 SLAB_HWCACHE_ALIGN, NULL, NULL);
2288 if (srb_cachep == NULL) {
2290 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2294 /* Derive version string. */
2295 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2297 strcat(qla2x00_version_str, "-debug");
2299 qla2xxx_transport_template =
2300 fc_attach_transport(&qla2xxx_transport_functions);
2301 if (!qla2xxx_transport_template)
2304 printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
2309 * qla2x00_module_exit - Module cleanup.
2312 qla2x00_module_exit(void)
2314 kmem_cache_destroy(srb_cachep);
2315 fc_release_transport(qla2xxx_transport_template);
2318 module_init(qla2x00_module_init);
2319 module_exit(qla2x00_module_exit);
2321 MODULE_AUTHOR("QLogic Corporation");
2322 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2323 MODULE_LICENSE("GPL");
2324 MODULE_VERSION(QLA2XXX_VERSION);