2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
4 Written By: Adam Radford <linuxraid@amcc.com>
6 Copyright (C) 2004 Applied Micro Circuits Corporation.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
18 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
19 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
20 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
21 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
22 solely responsible for determining the appropriateness of using and
23 distributing the Program and assumes all risks associated with its
24 exercise of rights under this Agreement, including but not limited to
25 the risks and costs of program errors, damage to or loss of data,
26 programs or equipment, and unavailability or interruption of operations.
28 DISCLAIMER OF LIABILITY
29 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
30 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
35 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37 You should have received a copy of the GNU General Public License
38 along with this program; if not, write to the Free Software
39 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 Bugs/Comments/Suggestions should be mailed to:
44 For more information, goto:
47 Note: This version of the driver does not contain a bundled firmware
52 2.26.02.000 - Driver cleanup for kernel submission.
53 2.26.02.001 - Replace schedule_timeout() calls with msleep().
56 #include <linux/module.h>
57 #include <linux/reboot.h>
58 #include <linux/spinlock.h>
59 #include <linux/interrupt.h>
60 #include <linux/moduleparam.h>
61 #include <linux/errno.h>
62 #include <linux/types.h>
63 #include <linux/delay.h>
64 #include <linux/pci.h>
65 #include <linux/time.h>
68 #include <asm/uaccess.h>
69 #include <scsi/scsi.h>
70 #include <scsi/scsi_host.h>
71 #include <scsi/scsi_tcq.h>
72 #include <scsi/scsi_cmnd.h>
76 static const char *twa_driver_version="2.26.02.001";
77 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
78 static unsigned int twa_device_extension_count;
79 static int twa_major = -1;
80 extern struct timezone sys_tz;
82 /* Module parameters */
83 MODULE_AUTHOR ("AMCC");
84 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
85 MODULE_LICENSE("GPL");
87 /* Function prototypes */
88 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
89 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
90 static char *twa_aen_severity_lookup(unsigned char severity_code);
91 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
92 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
93 static int twa_chrdev_open(struct inode *inode, struct file *file);
94 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
95 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
96 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
97 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
98 u32 set_features, unsigned short current_fw_srl,
99 unsigned short current_fw_arch_id,
100 unsigned short current_fw_branch,
101 unsigned short current_fw_build,
102 unsigned short *fw_on_ctlr_srl,
103 unsigned short *fw_on_ctlr_arch_id,
104 unsigned short *fw_on_ctlr_branch,
105 unsigned short *fw_on_ctlr_build,
106 u32 *init_connect_result);
107 static void twa_load_sgl(TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length);
108 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
109 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
110 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
111 static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
112 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
113 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Apache *sglistarg);
114 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
115 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
116 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id);
120 /* Show some statistics about the card */
121 static ssize_t twa_show_stats(struct class_device *class_dev, char *buf)
123 struct Scsi_Host *host = class_to_shost(class_dev);
124 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
125 unsigned long flags = 0;
128 spin_lock_irqsave(tw_dev->host->host_lock, flags);
129 len = snprintf(buf, PAGE_SIZE, "Driver version: %s\n"
130 "Current commands posted: %4d\n"
131 "Max commands posted: %4d\n"
132 "Current pending commands: %4d\n"
133 "Max pending commands: %4d\n"
134 "Last sgl length: %4d\n"
135 "Max sgl length: %4d\n"
136 "Last sector count: %4d\n"
137 "Max sector count: %4d\n"
138 "SCSI Host Resets: %4d\n"
139 "SCSI Aborts/Timeouts: %4d\n"
142 tw_dev->posted_request_count,
143 tw_dev->max_posted_request_count,
144 tw_dev->pending_request_count,
145 tw_dev->max_pending_request_count,
147 tw_dev->max_sgl_entries,
148 tw_dev->sector_count,
149 tw_dev->max_sector_count,
153 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
155 } /* End twa_show_stats() */
157 /* This function will set a devices queue depth */
158 static ssize_t twa_store_queue_depth(struct device *dev, const char *buf, size_t count)
161 struct scsi_device *sdev = to_scsi_device(dev);
163 queue_depth = simple_strtoul(buf, NULL, 0);
164 if (queue_depth > TW_Q_LENGTH-2)
166 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
169 } /* End twa_store_queue_depth() */
171 /* Create sysfs 'queue_depth' entry */
172 static struct device_attribute twa_queue_depth_attr = {
174 .name = "queue_depth",
175 .mode = S_IRUSR | S_IWUSR,
177 .store = twa_store_queue_depth
180 /* Device attributes initializer */
181 static struct device_attribute *twa_dev_attrs[] = {
182 &twa_queue_depth_attr,
186 /* Create sysfs 'stats' entry */
187 static struct class_device_attribute twa_host_stats_attr = {
192 .show = twa_show_stats
195 /* Host attributes initializer */
196 static struct class_device_attribute *twa_host_attrs[] = {
197 &twa_host_stats_attr,
201 /* File operations struct for character device */
202 static struct file_operations twa_fops = {
203 .owner = THIS_MODULE,
204 .ioctl = twa_chrdev_ioctl,
205 .open = twa_chrdev_open,
209 /* This function will complete an aen request from the isr */
210 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
212 TW_Command_Full *full_command_packet;
213 TW_Command *command_packet;
214 TW_Command_Apache_Header *header;
218 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
219 tw_dev->posted_request_count--;
220 aen = header->status_block.error;
221 full_command_packet = tw_dev->command_packet_virt[request_id];
222 command_packet = &full_command_packet->command.oldcommand;
224 /* First check for internal completion of set param for time sync */
225 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
226 /* Keep reading the queue in case there are more aen's */
227 if (twa_aen_read_queue(tw_dev, request_id))
236 case TW_AEN_QUEUE_EMPTY:
237 /* Quit reading the queue if this is the last one */
239 case TW_AEN_SYNC_TIME_WITH_HOST:
240 twa_aen_sync_time(tw_dev, request_id);
244 twa_aen_queue_event(tw_dev, header);
246 /* If there are more aen's, keep reading the queue */
247 if (twa_aen_read_queue(tw_dev, request_id))
256 tw_dev->state[request_id] = TW_S_COMPLETED;
257 twa_free_request_id(tw_dev, request_id);
258 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
261 } /* End twa_aen_complete() */
263 /* This function will drain aen queue */
264 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
267 char cdb[TW_MAX_CDB_LEN];
268 TW_SG_Apache sglist[1];
269 int finished = 0, count = 0;
270 TW_Command_Full *full_command_packet;
271 TW_Command_Apache_Header *header;
273 int first_reset = 0, queue = 0, retval = 1;
280 full_command_packet = tw_dev->command_packet_virt[request_id];
281 memset(full_command_packet, 0, sizeof(TW_Command_Full));
284 memset(&cdb, 0, TW_MAX_CDB_LEN);
285 cdb[0] = REQUEST_SENSE; /* opcode */
286 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
288 /* Initialize sglist */
289 memset(&sglist, 0, sizeof(TW_SG_Apache));
290 sglist[0].length = TW_SECTOR_SIZE;
291 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
293 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
294 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
298 /* Mark internal command */
299 tw_dev->srb[request_id] = NULL;
302 /* Send command to the board */
303 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
304 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
308 /* Now poll for completion */
309 if (twa_poll_response(tw_dev, request_id, 30)) {
310 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
311 tw_dev->posted_request_count--;
315 tw_dev->posted_request_count--;
316 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
317 aen = header->status_block.error;
322 case TW_AEN_QUEUE_EMPTY:
323 if (first_reset != 1)
328 case TW_AEN_SOFT_RESET:
329 if (first_reset == 0)
334 case TW_AEN_SYNC_TIME_WITH_HOST:
340 /* Now queue an event info */
342 twa_aen_queue_event(tw_dev, header);
343 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
345 if (count == TW_MAX_AEN_DRAIN)
350 tw_dev->state[request_id] = TW_S_INITIAL;
352 } /* End twa_aen_drain_queue() */
354 /* This function will queue an event */
355 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
365 /* Fill out event info */
366 event = tw_dev->event_queue[tw_dev->error_index];
368 /* Check for clobber */
371 sprintf(host, " scsi%d:", tw_dev->host->host_no);
372 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
373 tw_dev->aen_clobber = 1;
376 aen = header->status_block.error;
377 memset(event, 0, sizeof(TW_Event));
379 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
380 do_gettimeofday(&time);
381 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
382 event->time_stamp_sec = local_time;
383 event->aen_code = aen;
384 event->retrieved = TW_AEN_NOT_RETRIEVED;
385 event->sequence_id = tw_dev->error_sequence_id;
386 tw_dev->error_sequence_id++;
388 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
389 event->parameter_len = strlen(header->err_specific_desc);
390 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len);
391 if (event->severity != TW_AEN_SEVERITY_DEBUG)
392 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
394 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
395 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
396 twa_string_lookup(twa_aen_table, aen),
397 header->err_specific_desc);
401 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
402 tw_dev->event_queue_wrapped = 1;
403 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
404 } /* End twa_aen_queue_event() */
406 /* This function will read the aen queue from the isr */
407 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
409 char cdb[TW_MAX_CDB_LEN];
410 TW_SG_Apache sglist[1];
411 TW_Command_Full *full_command_packet;
414 full_command_packet = tw_dev->command_packet_virt[request_id];
415 memset(full_command_packet, 0, sizeof(TW_Command_Full));
418 memset(&cdb, 0, TW_MAX_CDB_LEN);
419 cdb[0] = REQUEST_SENSE; /* opcode */
420 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
422 /* Initialize sglist */
423 memset(&sglist, 0, sizeof(TW_SG_Apache));
424 sglist[0].length = TW_SECTOR_SIZE;
425 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
427 /* Mark internal command */
428 tw_dev->srb[request_id] = NULL;
430 /* Now post the command packet */
431 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
432 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
438 } /* End twa_aen_read_queue() */
440 /* This function will look up an AEN severity string */
441 static char *twa_aen_severity_lookup(unsigned char severity_code)
445 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
446 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
449 retval = twa_aen_severity_table[severity_code];
452 } /* End twa_aen_severity_lookup() */
454 /* This function will sync firmware time with the host time */
455 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
459 TW_Command_Full *full_command_packet;
460 TW_Command *command_packet;
461 TW_Param_Apache *param;
464 /* Fill out the command packet */
465 full_command_packet = tw_dev->command_packet_virt[request_id];
466 memset(full_command_packet, 0, sizeof(TW_Command_Full));
467 command_packet = &full_command_packet->command.oldcommand;
468 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
469 command_packet->request_id = request_id;
470 command_packet->byte8_offset.param.sgl[0].address = tw_dev->generic_buffer_phys[request_id];
471 command_packet->byte8_offset.param.sgl[0].length = TW_SECTOR_SIZE;
472 command_packet->size = TW_COMMAND_SIZE;
473 command_packet->byte6_offset.parameter_count = 1;
475 /* Setup the param */
476 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
477 memset(param, 0, TW_SECTOR_SIZE);
478 param->table_id = TW_TIMEKEEP_TABLE | 0x8000; /* Controller time keep table */
479 param->parameter_id = 0x3; /* SchedulerTime */
480 param->parameter_size_bytes = 4;
482 /* Convert system time in UTC to local time seconds since last
484 do_gettimeofday(&utc);
485 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
486 schedulertime = local_time - (3 * 86400);
487 schedulertime = schedulertime % 604800;
489 memcpy(param->data, &schedulertime, sizeof(u32));
491 /* Mark internal command */
492 tw_dev->srb[request_id] = NULL;
494 /* Now post the command */
495 twa_post_command_packet(tw_dev, request_id, 1);
496 } /* End twa_aen_sync_time() */
498 /* This function will allocate memory and check if it is correctly aligned */
499 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
502 dma_addr_t dma_handle;
503 unsigned long *cpu_addr;
506 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
508 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
512 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
513 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
514 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
518 memset(cpu_addr, 0, size*TW_Q_LENGTH);
520 for (i = 0; i < TW_Q_LENGTH; i++) {
523 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
524 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
527 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
528 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
535 } /* End twa_allocate_memory() */
537 /* This function will check the status register for unexpected bits */
538 static int twa_check_bits(u32 status_reg_value)
542 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
544 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
550 } /* End twa_check_bits() */
552 /* This function will check the srl and decide if we are compatible */
553 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
556 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
557 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
558 u32 init_connect_result = 0;
560 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
561 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_FW_SRL,
562 TW_9000_ARCH_ID, TW_CURRENT_FW_BRANCH,
563 TW_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
564 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
565 &fw_on_ctlr_build, &init_connect_result)) {
566 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
570 tw_dev->working_srl = TW_CURRENT_FW_SRL;
571 tw_dev->working_branch = TW_CURRENT_FW_BRANCH;
572 tw_dev->working_build = TW_CURRENT_FW_BUILD;
574 /* Try base mode compatibility */
575 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
576 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
577 TW_EXTENDED_INIT_CONNECT,
578 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
579 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
580 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
581 &fw_on_ctlr_branch, &fw_on_ctlr_build,
582 &init_connect_result)) {
583 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
586 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
587 if (TW_CURRENT_FW_SRL > fw_on_ctlr_srl) {
588 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
590 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
594 tw_dev->working_srl = TW_BASE_FW_SRL;
595 tw_dev->working_branch = TW_BASE_FW_BRANCH;
596 tw_dev->working_build = TW_BASE_FW_BUILD;
601 } /* End twa_check_srl() */
603 /* This function handles ioctl for the character device */
604 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
607 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
608 dma_addr_t dma_handle;
610 unsigned int sequence_id = 0;
611 unsigned char event_index, start_index;
612 TW_Ioctl_Driver_Command driver_command;
613 TW_Ioctl_Buf_Apache *tw_ioctl;
615 TW_Command_Full *full_command_packet;
616 TW_Compatibility_Info *tw_compat_info;
618 struct timeval current_time;
620 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
621 int retval = TW_IOCTL_ERROR_OS_EFAULT;
623 /* Only let one of these through at a time */
624 if (down_interruptible(&tw_dev->ioctl_sem)) {
625 retval = TW_IOCTL_ERROR_OS_EINTR;
629 /* First copy down the driver command */
630 if (copy_from_user(&driver_command, (void *)arg, sizeof(TW_Ioctl_Driver_Command)))
633 /* Check data buffer size */
634 if (driver_command.buffer_length > TW_MAX_SECTORS * 512) {
635 retval = TW_IOCTL_ERROR_OS_EINVAL;
639 /* Hardware can only do multiple of 512 byte transfers */
640 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
642 /* Now allocate ioctl buf memory */
643 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, &dma_handle);
645 retval = TW_IOCTL_ERROR_OS_ENOMEM;
649 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
651 /* Now copy down the entire ioctl */
652 if (copy_from_user(tw_ioctl, (void *)arg, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
655 /* See which ioctl we are doing */
657 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
658 spin_lock_irqsave(tw_dev->host->host_lock, flags);
659 twa_get_request_id(tw_dev, &request_id);
661 /* Flag internal command */
662 tw_dev->srb[request_id] = 0;
664 /* Flag chrdev ioctl */
665 tw_dev->chrdev_request_id = request_id;
667 full_command_packet = &tw_ioctl->firmware_command;
669 /* Load request id and sglist for both command types */
670 twa_load_sgl(full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
672 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
674 /* Now post the command packet to the controller */
675 twa_post_command_packet(tw_dev, request_id, 1);
676 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
678 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
680 /* Now wait for command to complete */
681 timeout = wait_event_interruptible_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
683 /* Check if we timed out, got a signal, or didn't get
685 if ((timeout <= 0) && (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE)) {
686 /* Now we need to reset the board */
687 if (timeout == TW_IOCTL_ERROR_OS_ERESTARTSYS) {
690 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
691 tw_dev->host->host_no, TW_DRIVER, 0xc,
693 retval = TW_IOCTL_ERROR_OS_EIO;
695 spin_lock_irqsave(tw_dev->host->host_lock, flags);
696 tw_dev->state[request_id] = TW_S_COMPLETED;
697 twa_free_request_id(tw_dev, request_id);
698 tw_dev->posted_request_count--;
699 twa_reset_device_extension(tw_dev);
700 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
704 /* Now copy in the command packet response */
705 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
707 /* Now complete the io */
708 spin_lock_irqsave(tw_dev->host->host_lock, flags);
709 tw_dev->posted_request_count--;
710 tw_dev->state[request_id] = TW_S_COMPLETED;
711 twa_free_request_id(tw_dev, request_id);
712 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
714 case TW_IOCTL_GET_COMPATIBILITY_INFO:
715 tw_ioctl->driver_command.status = 0;
716 /* Copy compatiblity struct into ioctl data buffer */
717 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
718 strncpy(tw_compat_info->driver_version, twa_driver_version, strlen(twa_driver_version));
719 tw_compat_info->working_srl = tw_dev->working_srl;
720 tw_compat_info->working_branch = tw_dev->working_branch;
721 tw_compat_info->working_build = tw_dev->working_build;
723 case TW_IOCTL_GET_LAST_EVENT:
724 if (tw_dev->event_queue_wrapped) {
725 if (tw_dev->aen_clobber) {
726 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
727 tw_dev->aen_clobber = 0;
729 tw_ioctl->driver_command.status = 0;
731 if (!tw_dev->error_index) {
732 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
735 tw_ioctl->driver_command.status = 0;
737 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
738 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
739 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
741 case TW_IOCTL_GET_FIRST_EVENT:
742 if (tw_dev->event_queue_wrapped) {
743 if (tw_dev->aen_clobber) {
744 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
745 tw_dev->aen_clobber = 0;
747 tw_ioctl->driver_command.status = 0;
748 event_index = tw_dev->error_index;
750 if (!tw_dev->error_index) {
751 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
754 tw_ioctl->driver_command.status = 0;
757 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
758 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
760 case TW_IOCTL_GET_NEXT_EVENT:
761 event = (TW_Event *)tw_ioctl->data_buffer;
762 sequence_id = event->sequence_id;
763 tw_ioctl->driver_command.status = 0;
765 if (tw_dev->event_queue_wrapped) {
766 if (tw_dev->aen_clobber) {
767 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
768 tw_dev->aen_clobber = 0;
770 start_index = tw_dev->error_index;
772 if (!tw_dev->error_index) {
773 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
778 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
780 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
781 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
782 tw_dev->aen_clobber = 1;
783 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
786 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
787 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
789 case TW_IOCTL_GET_PREVIOUS_EVENT:
790 event = (TW_Event *)tw_ioctl->data_buffer;
791 sequence_id = event->sequence_id;
792 tw_ioctl->driver_command.status = 0;
794 if (tw_dev->event_queue_wrapped) {
795 if (tw_dev->aen_clobber) {
796 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
797 tw_dev->aen_clobber = 0;
799 start_index = tw_dev->error_index;
801 if (!tw_dev->error_index) {
802 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
807 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
809 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
810 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
811 tw_dev->aen_clobber = 1;
812 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
815 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
816 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
818 case TW_IOCTL_GET_LOCK:
819 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
820 do_gettimeofday(¤t_time);
821 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
823 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
824 tw_dev->ioctl_sem_lock = 1;
825 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
826 tw_ioctl->driver_command.status = 0;
827 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
829 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
830 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
833 case TW_IOCTL_RELEASE_LOCK:
834 if (tw_dev->ioctl_sem_lock == 1) {
835 tw_dev->ioctl_sem_lock = 0;
836 tw_ioctl->driver_command.status = 0;
838 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
842 retval = TW_IOCTL_ERROR_OS_ENOTTY;
846 /* Now copy the entire response to userspace */
847 if (copy_to_user((void *)arg, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
850 /* Now free ioctl buf memory */
851 pci_free_consistent(tw_dev->tw_pci_dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
853 up(&tw_dev->ioctl_sem);
856 } /* End twa_chrdev_ioctl() */
858 /* This function handles open for the character device */
859 static int twa_chrdev_open(struct inode *inode, struct file *file)
861 unsigned int minor_number;
862 int retval = TW_IOCTL_ERROR_OS_ENODEV;
864 minor_number = iminor(inode);
865 if (minor_number >= twa_device_extension_count)
870 } /* End twa_chrdev_open() */
872 /* This function will print readable messages from status register errors */
873 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
877 /* Check for various error conditions and handle them appropriately */
878 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
879 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
880 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
883 if (status_reg_value & TW_STATUS_PCI_ABORT) {
884 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
885 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
886 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
889 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
890 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
891 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
894 if (status_reg_value & TW_STATUS_SBUF_WRITE_ERROR) {
895 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xf, "SBUF Write Error: clearing");
896 writel(TW_CONTROL_CLEAR_SBUF_WRITE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
899 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
900 if (tw_dev->reset_print == 0) {
901 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
902 tw_dev->reset_print = 1;
909 } /* End twa_decode_bits() */
911 /* This function will empty the response queue */
912 static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
914 u32 status_reg_value, response_que_value;
915 int count = 0, retval = 1;
917 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
919 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
920 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
921 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
924 if (count == TW_MAX_RESPONSE_DRAIN)
930 } /* End twa_empty_response_queue() */
932 /* This function passes sense keys from firmware to scsi layer */
933 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
935 TW_Command_Full *full_command_packet;
936 unsigned short error;
939 full_command_packet = tw_dev->command_packet_virt[request_id];
940 /* Don't print error for Logical unit not supported during rollcall */
941 error = full_command_packet->header.status_block.error;
942 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
944 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
945 tw_dev->host->host_no,
946 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
947 full_command_packet->header.status_block.error,
948 twa_string_lookup(twa_error_table,
949 full_command_packet->header.status_block.error),
950 full_command_packet->header.err_specific_desc);
952 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
953 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
954 full_command_packet->header.status_block.error,
955 twa_string_lookup(twa_error_table,
956 full_command_packet->header.status_block.error),
957 full_command_packet->header.err_specific_desc);
961 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
962 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
963 retval = TW_ISR_DONT_RESULT;
969 } /* End twa_fill_sense() */
971 /* This function will free up device extension resources */
972 static void twa_free_device_extension(TW_Device_Extension *tw_dev)
974 if (tw_dev->command_packet_virt[0])
975 pci_free_consistent(tw_dev->tw_pci_dev,
976 sizeof(TW_Command_Full)*TW_Q_LENGTH,
977 tw_dev->command_packet_virt[0],
978 tw_dev->command_packet_phys[0]);
980 if (tw_dev->generic_buffer_virt[0])
981 pci_free_consistent(tw_dev->tw_pci_dev,
982 TW_SECTOR_SIZE*TW_Q_LENGTH,
983 tw_dev->generic_buffer_virt[0],
984 tw_dev->generic_buffer_phys[0]);
986 if (tw_dev->event_queue[0])
987 kfree(tw_dev->event_queue[0]);
988 } /* End twa_free_device_extension() */
990 /* This function will free a request id */
991 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
993 tw_dev->free_queue[tw_dev->free_tail] = request_id;
994 tw_dev->state[request_id] = TW_S_FINISHED;
995 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
996 } /* End twa_free_request_id() */
998 /* This function will get parameter table entires from the firmware */
999 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1001 TW_Command_Full *full_command_packet;
1002 TW_Command *command_packet;
1003 TW_Param_Apache *param;
1004 unsigned long param_value;
1005 void *retval = NULL;
1007 /* Setup the command packet */
1008 full_command_packet = tw_dev->command_packet_virt[request_id];
1009 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1010 command_packet = &full_command_packet->command.oldcommand;
1012 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1013 command_packet->size = TW_COMMAND_SIZE;
1014 command_packet->request_id = request_id;
1015 command_packet->byte6_offset.block_count = 1;
1017 /* Now setup the param */
1018 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1019 memset(param, 0, TW_SECTOR_SIZE);
1020 param->table_id = table_id | 0x8000;
1021 param->parameter_id = parameter_id;
1022 param->parameter_size_bytes = parameter_size_bytes;
1023 param_value = tw_dev->generic_buffer_phys[request_id];
1025 command_packet->byte8_offset.param.sgl[0].address = param_value;
1026 command_packet->byte8_offset.param.sgl[0].length = TW_SECTOR_SIZE;
1028 /* Post the command packet to the board */
1029 twa_post_command_packet(tw_dev, request_id, 1);
1031 /* Poll for completion */
1032 if (twa_poll_response(tw_dev, request_id, 30))
1033 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1035 retval = (void *)&(param->data[0]);
1037 tw_dev->posted_request_count--;
1038 tw_dev->state[request_id] = TW_S_INITIAL;
1041 } /* End twa_get_param() */
1043 /* This function will assign an available request id */
1044 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1046 *request_id = tw_dev->free_queue[tw_dev->free_head];
1047 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1048 tw_dev->state[*request_id] = TW_S_STARTED;
1049 } /* End twa_get_request_id() */
1051 /* This function will send an initconnection command to controller */
1052 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1053 u32 set_features, unsigned short current_fw_srl,
1054 unsigned short current_fw_arch_id,
1055 unsigned short current_fw_branch,
1056 unsigned short current_fw_build,
1057 unsigned short *fw_on_ctlr_srl,
1058 unsigned short *fw_on_ctlr_arch_id,
1059 unsigned short *fw_on_ctlr_branch,
1060 unsigned short *fw_on_ctlr_build,
1061 u32 *init_connect_result)
1063 TW_Command_Full *full_command_packet;
1064 TW_Initconnect *tw_initconnect;
1065 int request_id = 0, retval = 1;
1067 /* Initialize InitConnection command packet */
1068 full_command_packet = tw_dev->command_packet_virt[request_id];
1069 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1070 full_command_packet->header.header_desc.size_header = 128;
1072 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1073 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1074 tw_initconnect->request_id = request_id;
1075 tw_initconnect->message_credits = message_credits;
1076 tw_initconnect->features = set_features;
1077 #if BITS_PER_LONG > 32
1078 /* Turn on 64-bit sgl support */
1079 tw_initconnect->features |= 1;
1082 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1083 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1084 tw_initconnect->fw_srl = current_fw_srl;
1085 tw_initconnect->fw_arch_id = current_fw_arch_id;
1086 tw_initconnect->fw_branch = current_fw_branch;
1087 tw_initconnect->fw_build = current_fw_build;
1089 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1091 /* Send command packet to the board */
1092 twa_post_command_packet(tw_dev, request_id, 1);
1094 /* Poll for completion */
1095 if (twa_poll_response(tw_dev, request_id, 30)) {
1096 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1098 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1099 *fw_on_ctlr_srl = tw_initconnect->fw_srl;
1100 *fw_on_ctlr_arch_id = tw_initconnect->fw_arch_id;
1101 *fw_on_ctlr_branch = tw_initconnect->fw_branch;
1102 *fw_on_ctlr_build = tw_initconnect->fw_build;
1103 *init_connect_result = tw_initconnect->result;
1108 tw_dev->posted_request_count--;
1109 tw_dev->state[request_id] = TW_S_INITIAL;
1112 } /* End twa_initconnection() */
1114 /* This function will initialize the fields of a device extension */
1115 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1119 /* Initialize command packet buffers */
1120 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1121 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1125 /* Initialize generic buffer */
1126 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1127 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1131 /* Allocate event info space */
1132 tw_dev->event_queue[0] = kmalloc(sizeof(TW_Event) * TW_Q_LENGTH, GFP_KERNEL);
1133 if (!tw_dev->event_queue[0]) {
1134 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1138 memset(tw_dev->event_queue[0], 0, sizeof(TW_Event) * TW_Q_LENGTH);
1140 for (i = 0; i < TW_Q_LENGTH; i++) {
1141 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1142 tw_dev->free_queue[i] = i;
1143 tw_dev->state[i] = TW_S_INITIAL;
1146 tw_dev->pending_head = TW_Q_START;
1147 tw_dev->pending_tail = TW_Q_START;
1148 tw_dev->free_head = TW_Q_START;
1149 tw_dev->free_tail = TW_Q_START;
1150 tw_dev->error_sequence_id = 1;
1151 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1153 init_MUTEX(&tw_dev->ioctl_sem);
1154 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1159 } /* End twa_initialize_device_extension() */
1161 /* This function is the interrupt service routine */
1162 static irqreturn_t twa_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1164 int request_id, error = 0;
1165 u32 status_reg_value;
1166 TW_Response_Queue response_que;
1167 TW_Command_Full *full_command_packet;
1168 TW_Command *command_packet;
1169 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1172 /* Get the per adapter lock */
1173 spin_lock(tw_dev->host->host_lock);
1175 /* See if the interrupt matches this instance */
1176 if (tw_dev->tw_pci_dev->irq == (unsigned int)irq) {
1180 /* Read the registers */
1181 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1183 /* Check if this is our interrupt, otherwise bail */
1184 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1185 goto twa_interrupt_bail;
1187 /* Check controller for errors */
1188 if (twa_check_bits(status_reg_value)) {
1189 if (twa_decode_bits(tw_dev, status_reg_value)) {
1190 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1191 goto twa_interrupt_bail;
1195 /* Handle host interrupt */
1196 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1197 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1199 /* Handle attention interrupt */
1200 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1201 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1202 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1203 twa_get_request_id(tw_dev, &request_id);
1205 error = twa_aen_read_queue(tw_dev, request_id);
1207 tw_dev->state[request_id] = TW_S_COMPLETED;
1208 twa_free_request_id(tw_dev, request_id);
1209 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1214 /* Handle command interrupt */
1215 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1216 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1217 /* Drain as many pending commands as we can */
1218 while (tw_dev->pending_request_count > 0) {
1219 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1220 if (tw_dev->state[request_id] != TW_S_PENDING) {
1221 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1222 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1223 goto twa_interrupt_bail;
1225 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1226 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1227 tw_dev->pending_request_count--;
1229 /* If we get here, we will continue re-posting on the next command interrupt */
1235 /* Handle response interrupt */
1236 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1238 /* Drain the response queue from the board */
1239 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1240 /* Complete the response */
1241 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1242 request_id = TW_RESID_OUT(response_que.response_id);
1243 full_command_packet = tw_dev->command_packet_virt[request_id];
1245 command_packet = &full_command_packet->command.oldcommand;
1246 /* Check for command packet errors */
1247 if (full_command_packet->command.newcommand.status != 0) {
1248 if (tw_dev->srb[request_id] != 0) {
1249 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1251 /* Skip ioctl error prints */
1252 if (request_id != tw_dev->chrdev_request_id) {
1253 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1258 /* Check for correct state */
1259 if (tw_dev->state[request_id] != TW_S_POSTED) {
1260 if (tw_dev->srb[request_id] != 0) {
1261 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1262 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1263 goto twa_interrupt_bail;
1267 /* Check for internal command completion */
1268 if (tw_dev->srb[request_id] == 0) {
1269 if (request_id != tw_dev->chrdev_request_id) {
1270 if (twa_aen_complete(tw_dev, request_id))
1271 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1273 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1274 wake_up(&tw_dev->ioctl_wqueue);
1277 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1278 /* If no error command was a success */
1280 tw_dev->srb[request_id]->result = (DID_OK << 16);
1283 /* If error, command failed */
1285 /* Ask for a host reset */
1286 tw_dev->srb[request_id]->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1289 /* Now complete the io */
1290 tw_dev->state[request_id] = TW_S_COMPLETED;
1291 twa_free_request_id(tw_dev, request_id);
1292 tw_dev->posted_request_count--;
1293 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1294 twa_unmap_scsi_data(tw_dev, request_id);
1297 /* Check for valid status after each drain */
1298 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1299 if (twa_check_bits(status_reg_value)) {
1300 if (twa_decode_bits(tw_dev, status_reg_value)) {
1301 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1302 goto twa_interrupt_bail;
1309 spin_unlock(tw_dev->host->host_lock);
1310 return IRQ_RETVAL(handled);
1311 } /* End twa_interrupt() */
1313 /* This function will load the request id and various sgls for ioctls */
1314 static void twa_load_sgl(TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
1316 TW_Command *oldcommand;
1317 TW_Command_Apache *newcommand;
1320 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1321 newcommand = &full_command_packet->command.newcommand;
1322 newcommand->request_id = request_id;
1323 newcommand->sg_list[0].address = dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1;
1324 newcommand->sg_list[0].length = length;
1326 oldcommand = &full_command_packet->command.oldcommand;
1327 oldcommand->request_id = request_id;
1329 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1330 /* Load the sg list */
1331 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1332 sgl->address = dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1;
1333 sgl->length = length;
1336 } /* End twa_load_sgl() */
1338 /* This function will perform a pci-dma mapping for a scatter gather list */
1339 static int twa_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
1342 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1343 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1346 if (cmd->use_sg == 0)
1349 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, DMA_BIDIRECTIONAL);
1352 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to map scatter gather list");
1356 cmd->SCp.phase = TW_PHASE_SGLIST;
1357 cmd->SCp.have_data_in = use_sg;
1361 } /* End twa_map_scsi_sg_data() */
1363 /* This function will perform a pci-dma map for a single buffer */
1364 static dma_addr_t twa_map_scsi_single_data(TW_Device_Extension *tw_dev, int request_id)
1367 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1368 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1371 if (cmd->request_bufflen == 0) {
1376 mapping = pci_map_single(pdev, cmd->request_buffer, cmd->request_bufflen, DMA_BIDIRECTIONAL);
1379 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1d, "Failed to map page");
1383 cmd->SCp.phase = TW_PHASE_SINGLE;
1384 cmd->SCp.have_data_in = mapping;
1388 } /* End twa_map_scsi_single_data() */
1390 /* This function will poll for a response interrupt of a request */
1391 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1393 int retval = 1, found = 0, response_request_id;
1394 TW_Response_Queue response_queue;
1395 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1397 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1398 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1399 response_request_id = TW_RESID_OUT(response_queue.response_id);
1400 if (request_id != response_request_id) {
1401 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1404 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1405 if (full_command_packet->command.newcommand.status != 0) {
1407 twa_fill_sense(tw_dev, request_id, 0, 0);
1412 if (full_command_packet->command.oldcommand.status != 0) {
1414 twa_fill_sense(tw_dev, request_id, 0, 0);
1425 } /* End twa_poll_response() */
1427 /* This function will poll the status register for a flag */
1428 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1430 u32 status_reg_value;
1431 unsigned long before;
1434 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1437 if (twa_check_bits(status_reg_value))
1438 twa_decode_bits(tw_dev, status_reg_value);
1440 while ((status_reg_value & flag) != flag) {
1441 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1443 if (twa_check_bits(status_reg_value))
1444 twa_decode_bits(tw_dev, status_reg_value);
1446 if (time_after(jiffies, before + HZ * seconds))
1454 } /* End twa_poll_status() */
1456 /* This function will poll the status register for disappearance of a flag */
1457 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1459 u32 status_reg_value;
1460 unsigned long before;
1463 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1466 if (twa_check_bits(status_reg_value))
1467 twa_decode_bits(tw_dev, status_reg_value);
1469 while ((status_reg_value & flag) != 0) {
1470 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1471 if (twa_check_bits(status_reg_value))
1472 twa_decode_bits(tw_dev, status_reg_value);
1474 if (time_after(jiffies, before + HZ * seconds))
1482 } /* End twa_poll_status_gone() */
1484 /* This function will attempt to post a command packet to the board */
1485 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1487 u32 status_reg_value;
1488 unsigned long command_que_value;
1491 command_que_value = tw_dev->command_packet_phys[request_id];
1492 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1494 if (twa_check_bits(status_reg_value))
1495 twa_decode_bits(tw_dev, status_reg_value);
1497 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1499 /* Only pend internal driver commands */
1501 retval = SCSI_MLQUEUE_HOST_BUSY;
1505 /* Couldn't post the command packet, so we do it later */
1506 if (tw_dev->state[request_id] != TW_S_PENDING) {
1507 tw_dev->state[request_id] = TW_S_PENDING;
1508 tw_dev->pending_request_count++;
1509 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1510 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1512 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1513 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1515 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1518 /* We successfully posted the command packet */
1519 #if BITS_PER_LONG > 32
1520 writeq(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1522 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1524 tw_dev->state[request_id] = TW_S_POSTED;
1525 tw_dev->posted_request_count++;
1526 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1527 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1533 } /* End twa_post_command_packet() */
1535 /* This function will reset a device extension */
1536 static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1541 /* Abort all requests that are in progress */
1542 for (i = 0; i < TW_Q_LENGTH; i++) {
1543 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1544 (tw_dev->state[i] != TW_S_INITIAL) &&
1545 (tw_dev->state[i] != TW_S_COMPLETED)) {
1546 if (tw_dev->srb[i]) {
1547 tw_dev->srb[i]->result = (DID_RESET << 16);
1548 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1549 twa_unmap_scsi_data(tw_dev, i);
1554 /* Reset queues and counts */
1555 for (i = 0; i < TW_Q_LENGTH; i++) {
1556 tw_dev->free_queue[i] = i;
1557 tw_dev->state[i] = TW_S_INITIAL;
1559 tw_dev->free_head = TW_Q_START;
1560 tw_dev->free_tail = TW_Q_START;
1561 tw_dev->posted_request_count = 0;
1562 tw_dev->pending_request_count = 0;
1563 tw_dev->pending_head = TW_Q_START;
1564 tw_dev->pending_tail = TW_Q_START;
1565 tw_dev->reset_print = 0;
1566 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1569 TW_DISABLE_INTERRUPTS(tw_dev);
1571 if (twa_reset_sequence(tw_dev, 1))
1574 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1579 } /* End twa_reset_device_extension() */
1581 /* This function will reset a controller */
1582 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1584 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1586 while (tries < TW_MAX_RESET_TRIES) {
1588 TW_SOFT_RESET(tw_dev);
1590 /* Make sure controller is in a good state */
1591 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 30)) {
1592 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1598 /* Empty response queue */
1599 if (twa_empty_response_queue(tw_dev)) {
1600 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1608 /* Check for compatibility/flash */
1609 if (twa_check_srl(tw_dev, &flashed)) {
1610 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1621 /* Drain the AEN queue */
1622 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1623 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1629 /* If we got here, controller is in a good state */
1635 } /* End twa_reset_sequence() */
1637 /* This funciton returns unit geometry in cylinders/heads/sectors */
1638 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1640 int heads, sectors, cylinders;
1641 TW_Device_Extension *tw_dev;
1643 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1645 if (capacity >= 0x200000) {
1648 cylinders = sector_div(capacity, heads * sectors);
1652 cylinders = sector_div(capacity, heads * sectors);
1657 geom[2] = cylinders;
1660 } /* End twa_scsi_biosparam() */
1662 /* This is the new scsi eh abort function */
1663 static int twa_scsi_eh_abort(struct scsi_cmnd *SCpnt)
1666 TW_Device_Extension *tw_dev = NULL;
1667 int retval = FAILED;
1669 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1671 spin_unlock_irq(tw_dev->host->host_lock);
1673 tw_dev->num_aborts++;
1675 /* If we find any IO's in process, we have to reset the card */
1676 for (i = 0; i < TW_Q_LENGTH; i++) {
1677 if ((tw_dev->state[i] != TW_S_FINISHED) && (tw_dev->state[i] != TW_S_INITIAL)) {
1678 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Unit #%d: Command (0x%x) timed out, resetting card.\n",
1679 tw_dev->host->host_no, TW_DRIVER, 0x2c,
1680 SCpnt->device->id, SCpnt->cmnd[0]);
1681 if (twa_reset_device_extension(tw_dev)) {
1682 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2a, "Controller reset failed during scsi abort");
1690 spin_lock_irq(tw_dev->host->host_lock);
1692 } /* End twa_scsi_eh_abort() */
1694 /* This is the new scsi eh reset function */
1695 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1697 TW_Device_Extension *tw_dev = NULL;
1698 int retval = FAILED;
1700 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1702 spin_unlock_irq(tw_dev->host->host_lock);
1704 tw_dev->num_resets++;
1706 printk(KERN_WARNING "3w-9xxx: scsi%d: SCSI host reset started.\n", tw_dev->host->host_no);
1708 /* Now reset the card and some of the device extension data */
1709 if (twa_reset_device_extension(tw_dev)) {
1710 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1713 printk(KERN_WARNING "3w-9xxx: scsi%d: SCSI host reset succeeded.\n", tw_dev->host->host_no);
1716 spin_lock_irq(tw_dev->host->host_lock);
1718 } /* End twa_scsi_eh_reset() */
1720 /* This is the main scsi queue function to handle scsi opcodes */
1721 static int twa_scsi_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1723 int request_id, retval;
1724 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1726 /* Save done function into scsi_cmnd struct */
1727 SCpnt->scsi_done = done;
1729 /* Get a free request id */
1730 twa_get_request_id(tw_dev, &request_id);
1732 /* Save the scsi command for use by the ISR */
1733 tw_dev->srb[request_id] = SCpnt;
1735 /* Initialize phase to zero */
1736 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1738 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1740 case SCSI_MLQUEUE_HOST_BUSY:
1741 twa_free_request_id(tw_dev, request_id);
1744 tw_dev->state[request_id] = TW_S_COMPLETED;
1745 twa_free_request_id(tw_dev, request_id);
1746 SCpnt->result = (DID_ERROR << 16);
1751 } /* End twa_scsi_queue() */
1753 /* This function hands scsi cdb's to the firmware */
1754 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Apache *sglistarg)
1756 TW_Command_Full *full_command_packet;
1757 TW_Command_Apache *command_packet;
1758 u32 num_sectors = 0x0;
1760 struct scsi_cmnd *srb = NULL;
1761 struct scatterlist *sglist = NULL;
1765 if (tw_dev->srb[request_id]) {
1766 if (tw_dev->srb[request_id]->request_buffer) {
1767 sglist = (struct scatterlist *)tw_dev->srb[request_id]->request_buffer;
1769 srb = tw_dev->srb[request_id];
1772 /* Initialize command packet */
1773 full_command_packet = tw_dev->command_packet_virt[request_id];
1774 full_command_packet->header.header_desc.size_header = 128;
1775 full_command_packet->header.status_block.error = 0;
1776 full_command_packet->header.status_block.severity__reserved = 0;
1778 command_packet = &full_command_packet->command.newcommand;
1779 command_packet->status = 0;
1780 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1782 /* We forced 16 byte cdb use earlier */
1784 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1786 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1789 command_packet->unit = srb->device->id;
1791 command_packet->unit = 0;
1793 command_packet->request_id = request_id;
1794 command_packet->sgl_offset = 16;
1797 /* Map sglist from scsi layer to cmd packet */
1798 if (tw_dev->srb[request_id]->use_sg == 0) {
1799 if (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH) {
1800 command_packet->sg_list[0].address = tw_dev->generic_buffer_phys[request_id];
1801 command_packet->sg_list[0].length = TW_MIN_SGL_LENGTH;
1803 buffaddr = twa_map_scsi_single_data(tw_dev, request_id);
1807 command_packet->sg_list[0].address = buffaddr;
1808 command_packet->sg_list[0].length = tw_dev->srb[request_id]->request_bufflen;
1810 command_packet->sgl_entries = 1;
1812 if (command_packet->sg_list[0].address & TW_ALIGNMENT_9000_SGL) {
1813 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2d, "Found unaligned address during execute scsi");
1818 if (tw_dev->srb[request_id]->use_sg > 0) {
1819 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1823 for (i = 0; i < sg_count; i++) {
1824 command_packet->sg_list[i].address = sg_dma_address(&sglist[i]);
1825 command_packet->sg_list[i].length = sg_dma_len(&sglist[i]);
1826 if (command_packet->sg_list[i].address & TW_ALIGNMENT_9000_SGL) {
1827 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1831 command_packet->sgl_entries = tw_dev->srb[request_id]->use_sg;
1834 /* Internal cdb post */
1835 for (i = 0; i < use_sg; i++) {
1836 command_packet->sg_list[i].address = sglistarg[i].address;
1837 command_packet->sg_list[i].length = sglistarg[i].length;
1838 if (command_packet->sg_list[i].address & TW_ALIGNMENT_9000_SGL) {
1839 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1843 command_packet->sgl_entries = use_sg;
1847 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1848 num_sectors = (u32)srb->cmnd[4];
1850 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1851 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1854 /* Update sector statistic */
1855 tw_dev->sector_count = num_sectors;
1856 if (tw_dev->sector_count > tw_dev->max_sector_count)
1857 tw_dev->max_sector_count = tw_dev->sector_count;
1859 /* Update SG statistics */
1861 tw_dev->sgl_entries = tw_dev->srb[request_id]->use_sg;
1862 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1863 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1866 /* Now post the command to the board */
1868 retval = twa_post_command_packet(tw_dev, request_id, 0);
1870 twa_post_command_packet(tw_dev, request_id, 1);
1875 } /* End twa_scsiop_execute_scsi() */
1877 /* This function completes an execute scsi operation */
1878 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1880 /* Copy the response if too small */
1881 if ((tw_dev->srb[request_id]->request_buffer) && (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH)) {
1882 memcpy(tw_dev->srb[request_id]->request_buffer,
1883 tw_dev->generic_buffer_virt[request_id],
1884 tw_dev->srb[request_id]->request_bufflen);
1886 } /* End twa_scsiop_execute_scsi_complete() */
1888 /* This function tells the controller to shut down */
1889 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1891 /* Disable interrupts */
1892 TW_DISABLE_INTERRUPTS(tw_dev);
1894 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1896 /* Tell the card we are shutting down */
1897 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1898 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1900 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1903 /* Clear all interrupts just before exit */
1904 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1905 } /* End __twa_shutdown() */
1907 /* Wrapper for __twa_shutdown */
1908 static void twa_shutdown(struct device *dev)
1910 struct Scsi_Host *host = pci_get_drvdata(to_pci_dev(dev));
1911 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1913 __twa_shutdown(tw_dev);
1914 } /* End twa_shutdown() */
1916 /* This function will look up a string */
1917 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1921 for (index = 0; ((code != table[index].code) &&
1922 (table[index].text != (char *)0)); index++);
1923 return(table[index].text);
1924 } /* End twa_string_lookup() */
1926 /* This function will perform a pci-dma unmap */
1927 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1929 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1930 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1932 switch(cmd->SCp.phase) {
1933 case TW_PHASE_SINGLE:
1934 pci_unmap_single(pdev, cmd->SCp.have_data_in, cmd->request_bufflen, DMA_BIDIRECTIONAL);
1936 case TW_PHASE_SGLIST:
1937 pci_unmap_sg(pdev, cmd->request_buffer, cmd->use_sg, DMA_BIDIRECTIONAL);
1940 } /* End twa_unmap_scsi_data() */
1942 /* scsi_host_template initializer */
1943 static struct scsi_host_template driver_template = {
1944 .module = THIS_MODULE,
1945 .name = "3ware 9000 Storage Controller",
1946 .queuecommand = twa_scsi_queue,
1947 .eh_abort_handler = twa_scsi_eh_abort,
1948 .eh_host_reset_handler = twa_scsi_eh_reset,
1949 .bios_param = twa_scsi_biosparam,
1950 .can_queue = TW_Q_LENGTH-2,
1952 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
1953 .max_sectors = TW_MAX_SECTORS,
1954 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1955 .use_clustering = ENABLE_CLUSTERING,
1956 .shost_attrs = twa_host_attrs,
1957 .sdev_attrs = twa_dev_attrs,
1961 /* This function will probe and initialize a card */
1962 static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1964 struct Scsi_Host *host = NULL;
1965 TW_Device_Extension *tw_dev;
1967 int retval = -ENODEV;
1969 retval = pci_enable_device(pdev);
1971 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
1972 goto out_disable_device;
1975 pci_set_master(pdev);
1977 retval = pci_set_dma_mask(pdev, TW_DMA_MASK);
1979 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
1980 goto out_disable_device;
1983 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
1985 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
1987 goto out_disable_device;
1989 tw_dev = (TW_Device_Extension *)host->hostdata;
1991 memset(tw_dev, 0, sizeof(TW_Device_Extension));
1993 /* Save values to device extension */
1994 tw_dev->host = host;
1995 tw_dev->tw_pci_dev = pdev;
1997 if (twa_initialize_device_extension(tw_dev)) {
1998 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
1999 goto out_free_device_extension;
2002 /* Request IO regions */
2003 retval = pci_request_regions(pdev, "3w-9xxx");
2005 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2006 goto out_free_device_extension;
2009 mem_addr = pci_resource_start(pdev, 1);
2011 /* Save base address */
2012 tw_dev->base_addr = ioremap(mem_addr, PAGE_SIZE);
2013 if (!tw_dev->base_addr) {
2014 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2015 goto out_release_mem_region;
2018 /* Disable interrupts on the card */
2019 TW_DISABLE_INTERRUPTS(tw_dev);
2021 /* Initialize the card */
2022 if (twa_reset_sequence(tw_dev, 0))
2023 goto out_release_mem_region;
2025 /* Set host specific parameters */
2026 host->max_id = TW_MAX_UNITS;
2027 host->max_cmd_len = TW_MAX_CDB_LEN;
2029 /* Luns and channels aren't supported by adapter */
2031 host->max_channel = 0;
2033 /* Register the card with the kernel SCSI layer */
2034 retval = scsi_add_host(host, &pdev->dev);
2036 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2037 goto out_release_mem_region;
2040 pci_set_drvdata(pdev, host);
2042 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%x, IRQ: %d.\n",
2043 host->host_no, mem_addr, pdev->irq);
2044 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2046 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2047 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2048 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2049 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2050 *(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2051 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH));
2053 /* Now setup the interrupt handler */
2054 retval = request_irq(pdev->irq, twa_interrupt, SA_SHIRQ, "3w-9xxx", tw_dev);
2056 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2057 goto out_remove_host;
2060 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2061 twa_device_extension_count++;
2063 /* Re-enable interrupts on the card */
2064 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2066 /* Finally, scan the host */
2067 scsi_scan_host(host);
2069 if (twa_major == -1) {
2070 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2071 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2076 scsi_remove_host(host);
2077 out_release_mem_region:
2078 pci_release_regions(pdev);
2079 out_free_device_extension:
2080 twa_free_device_extension(tw_dev);
2081 scsi_host_put(host);
2083 pci_disable_device(pdev);
2086 } /* End twa_probe() */
2088 /* This function is called to remove a device */
2089 static void twa_remove(struct pci_dev *pdev)
2091 struct Scsi_Host *host = pci_get_drvdata(pdev);
2092 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2094 scsi_remove_host(tw_dev->host);
2096 __twa_shutdown(tw_dev);
2098 /* Free up the IRQ */
2099 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2101 /* Free up the mem region */
2102 pci_release_regions(pdev);
2104 /* Free up device extension resources */
2105 twa_free_device_extension(tw_dev);
2107 /* Unregister character device */
2108 if (twa_major >= 0) {
2109 unregister_chrdev(twa_major, "twa");
2113 scsi_host_put(tw_dev->host);
2114 pci_disable_device(pdev);
2115 twa_device_extension_count--;
2116 } /* End twa_remove() */
2118 /* PCI Devices supported by this driver */
2119 static struct pci_device_id twa_pci_tbl[] __devinitdata = {
2120 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2124 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2126 /* pci_driver initializer */
2127 static struct pci_driver twa_driver = {
2129 .id_table = twa_pci_tbl,
2131 .remove = twa_remove,
2133 .shutdown = twa_shutdown
2137 /* This function is called on driver initialization */
2138 static int __init twa_init(void)
2140 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", twa_driver_version);
2142 return pci_module_init(&twa_driver);
2143 } /* End twa_init() */
2145 /* This function is called on driver exit */
2146 static void __exit twa_exit(void)
2148 pci_unregister_driver(&twa_driver);
2149 } /* End twa_exit() */
2151 module_init(twa_init);
2152 module_exit(twa_exit);