2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
4 Written By: Adam Radford <linuxraid@amcc.com>
6 Copyright (C) 2004-2005 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().
54 2.26.02.002 - Add support for PAE mode.
56 Fix twa_remove() to free irq handler/unregister_chrdev()
57 before shutting down card.
58 Change to new 'change_queue_depth' api.
59 Fix 'handled=1' ISR usage, remove bogus IRQ check.
60 Remove un-needed eh_abort handler.
61 Add support for embedded firmware error strings.
62 2.26.02.003 - Correctly handle single sgl's with use_sg=1.
63 2.26.02.004 - Add support for 9550SX controllers.
64 2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
67 #include <linux/module.h>
68 #include <linux/reboot.h>
69 #include <linux/spinlock.h>
70 #include <linux/interrupt.h>
71 #include <linux/moduleparam.h>
72 #include <linux/errno.h>
73 #include <linux/types.h>
74 #include <linux/delay.h>
75 #include <linux/pci.h>
76 #include <linux/time.h>
77 #include <linux/mutex.h>
80 #include <asm/uaccess.h>
81 #include <scsi/scsi.h>
82 #include <scsi/scsi_host.h>
83 #include <scsi/scsi_tcq.h>
84 #include <scsi/scsi_cmnd.h>
88 #define TW_DRIVER_VERSION "2.26.02.005"
89 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
90 static unsigned int twa_device_extension_count;
91 static int twa_major = -1;
92 extern struct timezone sys_tz;
94 /* Module parameters */
95 MODULE_AUTHOR ("AMCC");
96 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
97 MODULE_LICENSE("GPL");
98 MODULE_VERSION(TW_DRIVER_VERSION);
100 /* Function prototypes */
101 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
102 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
103 static char *twa_aen_severity_lookup(unsigned char severity_code);
104 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
105 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
106 static int twa_chrdev_open(struct inode *inode, struct file *file);
107 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
108 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
109 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
110 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
111 u32 set_features, unsigned short current_fw_srl,
112 unsigned short current_fw_arch_id,
113 unsigned short current_fw_branch,
114 unsigned short current_fw_build,
115 unsigned short *fw_on_ctlr_srl,
116 unsigned short *fw_on_ctlr_arch_id,
117 unsigned short *fw_on_ctlr_branch,
118 unsigned short *fw_on_ctlr_build,
119 u32 *init_connect_result);
120 static void twa_load_sgl(TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length);
121 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
122 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
123 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
124 static int twa_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset);
125 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
126 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg);
127 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
128 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
129 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id);
133 /* Show some statistics about the card */
134 static ssize_t twa_show_stats(struct class_device *class_dev, char *buf)
136 struct Scsi_Host *host = class_to_shost(class_dev);
137 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
138 unsigned long flags = 0;
141 spin_lock_irqsave(tw_dev->host->host_lock, flags);
142 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
143 "Current commands posted: %4d\n"
144 "Max commands posted: %4d\n"
145 "Current pending commands: %4d\n"
146 "Max pending commands: %4d\n"
147 "Last sgl length: %4d\n"
148 "Max sgl length: %4d\n"
149 "Last sector count: %4d\n"
150 "Max sector count: %4d\n"
151 "SCSI Host Resets: %4d\n"
154 tw_dev->posted_request_count,
155 tw_dev->max_posted_request_count,
156 tw_dev->pending_request_count,
157 tw_dev->max_pending_request_count,
159 tw_dev->max_sgl_entries,
160 tw_dev->sector_count,
161 tw_dev->max_sector_count,
164 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
166 } /* End twa_show_stats() */
168 /* This function will set a devices queue depth */
169 static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth)
171 if (queue_depth > TW_Q_LENGTH-2)
172 queue_depth = TW_Q_LENGTH-2;
173 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
175 } /* End twa_change_queue_depth() */
177 /* Create sysfs 'stats' entry */
178 static struct class_device_attribute twa_host_stats_attr = {
183 .show = twa_show_stats
186 /* Host attributes initializer */
187 static struct class_device_attribute *twa_host_attrs[] = {
188 &twa_host_stats_attr,
192 /* File operations struct for character device */
193 static struct file_operations twa_fops = {
194 .owner = THIS_MODULE,
195 .ioctl = twa_chrdev_ioctl,
196 .open = twa_chrdev_open,
200 /* This function will complete an aen request from the isr */
201 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
203 TW_Command_Full *full_command_packet;
204 TW_Command *command_packet;
205 TW_Command_Apache_Header *header;
209 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
210 tw_dev->posted_request_count--;
211 aen = header->status_block.error;
212 full_command_packet = tw_dev->command_packet_virt[request_id];
213 command_packet = &full_command_packet->command.oldcommand;
215 /* First check for internal completion of set param for time sync */
216 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
217 /* Keep reading the queue in case there are more aen's */
218 if (twa_aen_read_queue(tw_dev, request_id))
227 case TW_AEN_QUEUE_EMPTY:
228 /* Quit reading the queue if this is the last one */
230 case TW_AEN_SYNC_TIME_WITH_HOST:
231 twa_aen_sync_time(tw_dev, request_id);
235 twa_aen_queue_event(tw_dev, header);
237 /* If there are more aen's, keep reading the queue */
238 if (twa_aen_read_queue(tw_dev, request_id))
247 tw_dev->state[request_id] = TW_S_COMPLETED;
248 twa_free_request_id(tw_dev, request_id);
249 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
252 } /* End twa_aen_complete() */
254 /* This function will drain aen queue */
255 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
258 char cdb[TW_MAX_CDB_LEN];
259 TW_SG_Entry sglist[1];
260 int finished = 0, count = 0;
261 TW_Command_Full *full_command_packet;
262 TW_Command_Apache_Header *header;
264 int first_reset = 0, queue = 0, retval = 1;
271 full_command_packet = tw_dev->command_packet_virt[request_id];
272 memset(full_command_packet, 0, sizeof(TW_Command_Full));
275 memset(&cdb, 0, TW_MAX_CDB_LEN);
276 cdb[0] = REQUEST_SENSE; /* opcode */
277 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
279 /* Initialize sglist */
280 memset(&sglist, 0, sizeof(TW_SG_Entry));
281 sglist[0].length = TW_SECTOR_SIZE;
282 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
284 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
285 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
289 /* Mark internal command */
290 tw_dev->srb[request_id] = NULL;
293 /* Send command to the board */
294 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
295 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
299 /* Now poll for completion */
300 if (twa_poll_response(tw_dev, request_id, 30)) {
301 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
302 tw_dev->posted_request_count--;
306 tw_dev->posted_request_count--;
307 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
308 aen = header->status_block.error;
313 case TW_AEN_QUEUE_EMPTY:
314 if (first_reset != 1)
319 case TW_AEN_SOFT_RESET:
320 if (first_reset == 0)
325 case TW_AEN_SYNC_TIME_WITH_HOST:
331 /* Now queue an event info */
333 twa_aen_queue_event(tw_dev, header);
334 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
336 if (count == TW_MAX_AEN_DRAIN)
341 tw_dev->state[request_id] = TW_S_INITIAL;
343 } /* End twa_aen_drain_queue() */
345 /* This function will queue an event */
346 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
357 /* Fill out event info */
358 event = tw_dev->event_queue[tw_dev->error_index];
360 /* Check for clobber */
363 sprintf(host, " scsi%d:", tw_dev->host->host_no);
364 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
365 tw_dev->aen_clobber = 1;
368 aen = header->status_block.error;
369 memset(event, 0, sizeof(TW_Event));
371 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
372 do_gettimeofday(&time);
373 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
374 event->time_stamp_sec = local_time;
375 event->aen_code = aen;
376 event->retrieved = TW_AEN_NOT_RETRIEVED;
377 event->sequence_id = tw_dev->error_sequence_id;
378 tw_dev->error_sequence_id++;
380 /* Check for embedded error string */
381 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
383 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
384 event->parameter_len = strlen(header->err_specific_desc);
385 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len);
386 if (event->severity != TW_AEN_SEVERITY_DEBUG)
387 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
389 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
390 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
391 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
392 header->err_specific_desc);
396 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
397 tw_dev->event_queue_wrapped = 1;
398 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
399 } /* End twa_aen_queue_event() */
401 /* This function will read the aen queue from the isr */
402 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
404 char cdb[TW_MAX_CDB_LEN];
405 TW_SG_Entry sglist[1];
406 TW_Command_Full *full_command_packet;
409 full_command_packet = tw_dev->command_packet_virt[request_id];
410 memset(full_command_packet, 0, sizeof(TW_Command_Full));
413 memset(&cdb, 0, TW_MAX_CDB_LEN);
414 cdb[0] = REQUEST_SENSE; /* opcode */
415 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
417 /* Initialize sglist */
418 memset(&sglist, 0, sizeof(TW_SG_Entry));
419 sglist[0].length = TW_SECTOR_SIZE;
420 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
422 /* Mark internal command */
423 tw_dev->srb[request_id] = NULL;
425 /* Now post the command packet */
426 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
427 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
433 } /* End twa_aen_read_queue() */
435 /* This function will look up an AEN severity string */
436 static char *twa_aen_severity_lookup(unsigned char severity_code)
440 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
441 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
444 retval = twa_aen_severity_table[severity_code];
447 } /* End twa_aen_severity_lookup() */
449 /* This function will sync firmware time with the host time */
450 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
454 TW_Command_Full *full_command_packet;
455 TW_Command *command_packet;
456 TW_Param_Apache *param;
459 /* Fill out the command packet */
460 full_command_packet = tw_dev->command_packet_virt[request_id];
461 memset(full_command_packet, 0, sizeof(TW_Command_Full));
462 command_packet = &full_command_packet->command.oldcommand;
463 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
464 command_packet->request_id = request_id;
465 command_packet->byte8_offset.param.sgl[0].address = tw_dev->generic_buffer_phys[request_id];
466 command_packet->byte8_offset.param.sgl[0].length = TW_SECTOR_SIZE;
467 command_packet->size = TW_COMMAND_SIZE;
468 command_packet->byte6_offset.parameter_count = 1;
470 /* Setup the param */
471 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
472 memset(param, 0, TW_SECTOR_SIZE);
473 param->table_id = TW_TIMEKEEP_TABLE | 0x8000; /* Controller time keep table */
474 param->parameter_id = 0x3; /* SchedulerTime */
475 param->parameter_size_bytes = 4;
477 /* Convert system time in UTC to local time seconds since last
479 do_gettimeofday(&utc);
480 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
481 schedulertime = local_time - (3 * 86400);
482 schedulertime = schedulertime % 604800;
484 memcpy(param->data, &schedulertime, sizeof(u32));
486 /* Mark internal command */
487 tw_dev->srb[request_id] = NULL;
489 /* Now post the command */
490 twa_post_command_packet(tw_dev, request_id, 1);
491 } /* End twa_aen_sync_time() */
493 /* This function will allocate memory and check if it is correctly aligned */
494 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
497 dma_addr_t dma_handle;
498 unsigned long *cpu_addr;
501 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
503 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
507 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
508 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
509 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
513 memset(cpu_addr, 0, size*TW_Q_LENGTH);
515 for (i = 0; i < TW_Q_LENGTH; i++) {
518 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
519 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
522 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
523 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
530 } /* End twa_allocate_memory() */
532 /* This function will check the status register for unexpected bits */
533 static int twa_check_bits(u32 status_reg_value)
537 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
539 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
545 } /* End twa_check_bits() */
547 /* This function will check the srl and decide if we are compatible */
548 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
551 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
552 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
553 u32 init_connect_result = 0;
555 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
556 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
557 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
558 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
559 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
560 &fw_on_ctlr_build, &init_connect_result)) {
561 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
565 tw_dev->working_srl = fw_on_ctlr_srl;
566 tw_dev->working_branch = fw_on_ctlr_branch;
567 tw_dev->working_build = fw_on_ctlr_build;
569 /* Try base mode compatibility */
570 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
571 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
572 TW_EXTENDED_INIT_CONNECT,
573 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
574 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
575 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
576 &fw_on_ctlr_branch, &fw_on_ctlr_build,
577 &init_connect_result)) {
578 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
581 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
582 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
583 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
585 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
589 tw_dev->working_srl = TW_BASE_FW_SRL;
590 tw_dev->working_branch = TW_BASE_FW_BRANCH;
591 tw_dev->working_build = TW_BASE_FW_BUILD;
596 } /* End twa_check_srl() */
598 /* This function handles ioctl for the character device */
599 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
602 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
603 dma_addr_t dma_handle;
605 unsigned int sequence_id = 0;
606 unsigned char event_index, start_index;
607 TW_Ioctl_Driver_Command driver_command;
608 TW_Ioctl_Buf_Apache *tw_ioctl;
610 TW_Command_Full *full_command_packet;
611 TW_Compatibility_Info *tw_compat_info;
613 struct timeval current_time;
615 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
616 int retval = TW_IOCTL_ERROR_OS_EFAULT;
617 void __user *argp = (void __user *)arg;
619 /* Only let one of these through at a time */
620 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
621 retval = TW_IOCTL_ERROR_OS_EINTR;
625 /* First copy down the driver command */
626 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
629 /* Check data buffer size */
630 if (driver_command.buffer_length > TW_MAX_SECTORS * 512) {
631 retval = TW_IOCTL_ERROR_OS_EINVAL;
635 /* Hardware can only do multiple of 512 byte transfers */
636 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
638 /* Now allocate ioctl buf memory */
639 cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, &dma_handle, GFP_KERNEL);
641 retval = TW_IOCTL_ERROR_OS_ENOMEM;
645 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
647 /* Now copy down the entire ioctl */
648 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
651 /* See which ioctl we are doing */
653 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
654 spin_lock_irqsave(tw_dev->host->host_lock, flags);
655 twa_get_request_id(tw_dev, &request_id);
657 /* Flag internal command */
658 tw_dev->srb[request_id] = NULL;
660 /* Flag chrdev ioctl */
661 tw_dev->chrdev_request_id = request_id;
663 full_command_packet = &tw_ioctl->firmware_command;
665 /* Load request id and sglist for both command types */
666 twa_load_sgl(full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
668 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
670 /* Now post the command packet to the controller */
671 twa_post_command_packet(tw_dev, request_id, 1);
672 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
674 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
676 /* Now wait for command to complete */
677 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
679 /* See if we reset while waiting for the ioctl to complete */
680 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
681 clear_bit(TW_IN_RESET, &tw_dev->flags);
682 retval = TW_IOCTL_ERROR_OS_ERESTARTSYS;
686 /* We timed out, and didn't get an interrupt */
687 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
688 /* Now we need to reset the board */
689 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
690 tw_dev->host->host_no, TW_DRIVER, 0xc,
692 retval = TW_IOCTL_ERROR_OS_EIO;
693 spin_lock_irqsave(tw_dev->host->host_lock, flags);
694 tw_dev->state[request_id] = TW_S_COMPLETED;
695 twa_free_request_id(tw_dev, request_id);
696 tw_dev->posted_request_count--;
697 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
698 twa_reset_device_extension(tw_dev, 1);
702 /* Now copy in the command packet response */
703 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
705 /* Now complete the io */
706 spin_lock_irqsave(tw_dev->host->host_lock, flags);
707 tw_dev->posted_request_count--;
708 tw_dev->state[request_id] = TW_S_COMPLETED;
709 twa_free_request_id(tw_dev, request_id);
710 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
712 case TW_IOCTL_GET_COMPATIBILITY_INFO:
713 tw_ioctl->driver_command.status = 0;
714 /* Copy compatiblity struct into ioctl data buffer */
715 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
716 strncpy(tw_compat_info->driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
717 tw_compat_info->working_srl = tw_dev->working_srl;
718 tw_compat_info->working_branch = tw_dev->working_branch;
719 tw_compat_info->working_build = tw_dev->working_build;
720 tw_compat_info->driver_srl_high = TW_CURRENT_DRIVER_SRL;
721 tw_compat_info->driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
722 tw_compat_info->driver_build_high = TW_CURRENT_DRIVER_BUILD;
723 tw_compat_info->driver_srl_low = TW_BASE_FW_SRL;
724 tw_compat_info->driver_branch_low = TW_BASE_FW_BRANCH;
725 tw_compat_info->driver_build_low = TW_BASE_FW_BUILD;
727 case TW_IOCTL_GET_LAST_EVENT:
728 if (tw_dev->event_queue_wrapped) {
729 if (tw_dev->aen_clobber) {
730 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
731 tw_dev->aen_clobber = 0;
733 tw_ioctl->driver_command.status = 0;
735 if (!tw_dev->error_index) {
736 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
739 tw_ioctl->driver_command.status = 0;
741 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
742 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
743 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
745 case TW_IOCTL_GET_FIRST_EVENT:
746 if (tw_dev->event_queue_wrapped) {
747 if (tw_dev->aen_clobber) {
748 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
749 tw_dev->aen_clobber = 0;
751 tw_ioctl->driver_command.status = 0;
752 event_index = tw_dev->error_index;
754 if (!tw_dev->error_index) {
755 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
758 tw_ioctl->driver_command.status = 0;
761 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
762 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
764 case TW_IOCTL_GET_NEXT_EVENT:
765 event = (TW_Event *)tw_ioctl->data_buffer;
766 sequence_id = event->sequence_id;
767 tw_ioctl->driver_command.status = 0;
769 if (tw_dev->event_queue_wrapped) {
770 if (tw_dev->aen_clobber) {
771 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
772 tw_dev->aen_clobber = 0;
774 start_index = tw_dev->error_index;
776 if (!tw_dev->error_index) {
777 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
782 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
784 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
785 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
786 tw_dev->aen_clobber = 1;
787 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
790 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
791 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
793 case TW_IOCTL_GET_PREVIOUS_EVENT:
794 event = (TW_Event *)tw_ioctl->data_buffer;
795 sequence_id = event->sequence_id;
796 tw_ioctl->driver_command.status = 0;
798 if (tw_dev->event_queue_wrapped) {
799 if (tw_dev->aen_clobber) {
800 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
801 tw_dev->aen_clobber = 0;
803 start_index = tw_dev->error_index;
805 if (!tw_dev->error_index) {
806 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
811 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
813 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
814 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
815 tw_dev->aen_clobber = 1;
816 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
819 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
820 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
822 case TW_IOCTL_GET_LOCK:
823 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
824 do_gettimeofday(¤t_time);
825 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
827 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
828 tw_dev->ioctl_sem_lock = 1;
829 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
830 tw_ioctl->driver_command.status = 0;
831 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
833 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
834 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
837 case TW_IOCTL_RELEASE_LOCK:
838 if (tw_dev->ioctl_sem_lock == 1) {
839 tw_dev->ioctl_sem_lock = 0;
840 tw_ioctl->driver_command.status = 0;
842 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
846 retval = TW_IOCTL_ERROR_OS_ENOTTY;
850 /* Now copy the entire response to userspace */
851 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
854 /* Now free ioctl buf memory */
855 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
857 mutex_unlock(&tw_dev->ioctl_lock);
860 } /* End twa_chrdev_ioctl() */
862 /* This function handles open for the character device */
863 static int twa_chrdev_open(struct inode *inode, struct file *file)
865 unsigned int minor_number;
866 int retval = TW_IOCTL_ERROR_OS_ENODEV;
868 minor_number = iminor(inode);
869 if (minor_number >= twa_device_extension_count)
874 } /* End twa_chrdev_open() */
876 /* This function will print readable messages from status register errors */
877 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
881 /* Check for various error conditions and handle them appropriately */
882 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
883 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
884 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
887 if (status_reg_value & TW_STATUS_PCI_ABORT) {
888 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
889 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
890 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
893 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
894 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
895 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
898 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
899 if (tw_dev->reset_print == 0) {
900 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
901 tw_dev->reset_print = 1;
908 } /* End twa_decode_bits() */
910 /* This function will empty the response queue */
911 static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
913 u32 status_reg_value, response_que_value;
914 int count = 0, retval = 1;
916 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
918 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
919 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
920 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
923 if (count == TW_MAX_RESPONSE_DRAIN)
929 } /* End twa_empty_response_queue() */
931 /* This function will clear the pchip/response queue on 9550SX */
932 static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
934 u32 status_reg_value, response_que_value;
935 int count = 0, retval = 1;
937 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9550SX) {
938 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
940 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
941 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
942 if ((response_que_value & TW_9550SX_DRAIN_COMPLETED) == TW_9550SX_DRAIN_COMPLETED) {
943 /* P-chip settle time */
948 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
951 if (count == TW_MAX_RESPONSE_DRAIN)
959 } /* End twa_empty_response_queue_large() */
961 /* This function passes sense keys from firmware to scsi layer */
962 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
964 TW_Command_Full *full_command_packet;
965 unsigned short error;
969 full_command_packet = tw_dev->command_packet_virt[request_id];
971 /* Check for embedded error string */
972 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
974 /* Don't print error for Logical unit not supported during rollcall */
975 error = full_command_packet->header.status_block.error;
976 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
978 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
979 tw_dev->host->host_no,
980 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
981 full_command_packet->header.status_block.error,
982 error_str[0] == '\0' ?
983 twa_string_lookup(twa_error_table,
984 full_command_packet->header.status_block.error) : error_str,
985 full_command_packet->header.err_specific_desc);
987 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
988 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
989 full_command_packet->header.status_block.error,
990 error_str[0] == '\0' ?
991 twa_string_lookup(twa_error_table,
992 full_command_packet->header.status_block.error) : error_str,
993 full_command_packet->header.err_specific_desc);
997 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
998 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
999 retval = TW_ISR_DONT_RESULT;
1005 } /* End twa_fill_sense() */
1007 /* This function will free up device extension resources */
1008 static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1010 if (tw_dev->command_packet_virt[0])
1011 pci_free_consistent(tw_dev->tw_pci_dev,
1012 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1013 tw_dev->command_packet_virt[0],
1014 tw_dev->command_packet_phys[0]);
1016 if (tw_dev->generic_buffer_virt[0])
1017 pci_free_consistent(tw_dev->tw_pci_dev,
1018 TW_SECTOR_SIZE*TW_Q_LENGTH,
1019 tw_dev->generic_buffer_virt[0],
1020 tw_dev->generic_buffer_phys[0]);
1022 kfree(tw_dev->event_queue[0]);
1023 } /* End twa_free_device_extension() */
1025 /* This function will free a request id */
1026 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1028 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1029 tw_dev->state[request_id] = TW_S_FINISHED;
1030 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1031 } /* End twa_free_request_id() */
1033 /* This function will get parameter table entires from the firmware */
1034 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1036 TW_Command_Full *full_command_packet;
1037 TW_Command *command_packet;
1038 TW_Param_Apache *param;
1039 unsigned long param_value;
1040 void *retval = NULL;
1042 /* Setup the command packet */
1043 full_command_packet = tw_dev->command_packet_virt[request_id];
1044 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1045 command_packet = &full_command_packet->command.oldcommand;
1047 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1048 command_packet->size = TW_COMMAND_SIZE;
1049 command_packet->request_id = request_id;
1050 command_packet->byte6_offset.block_count = 1;
1052 /* Now setup the param */
1053 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1054 memset(param, 0, TW_SECTOR_SIZE);
1055 param->table_id = table_id | 0x8000;
1056 param->parameter_id = parameter_id;
1057 param->parameter_size_bytes = parameter_size_bytes;
1058 param_value = tw_dev->generic_buffer_phys[request_id];
1060 command_packet->byte8_offset.param.sgl[0].address = param_value;
1061 command_packet->byte8_offset.param.sgl[0].length = TW_SECTOR_SIZE;
1063 /* Post the command packet to the board */
1064 twa_post_command_packet(tw_dev, request_id, 1);
1066 /* Poll for completion */
1067 if (twa_poll_response(tw_dev, request_id, 30))
1068 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1070 retval = (void *)&(param->data[0]);
1072 tw_dev->posted_request_count--;
1073 tw_dev->state[request_id] = TW_S_INITIAL;
1076 } /* End twa_get_param() */
1078 /* This function will assign an available request id */
1079 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1081 *request_id = tw_dev->free_queue[tw_dev->free_head];
1082 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1083 tw_dev->state[*request_id] = TW_S_STARTED;
1084 } /* End twa_get_request_id() */
1086 /* This function will send an initconnection command to controller */
1087 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1088 u32 set_features, unsigned short current_fw_srl,
1089 unsigned short current_fw_arch_id,
1090 unsigned short current_fw_branch,
1091 unsigned short current_fw_build,
1092 unsigned short *fw_on_ctlr_srl,
1093 unsigned short *fw_on_ctlr_arch_id,
1094 unsigned short *fw_on_ctlr_branch,
1095 unsigned short *fw_on_ctlr_build,
1096 u32 *init_connect_result)
1098 TW_Command_Full *full_command_packet;
1099 TW_Initconnect *tw_initconnect;
1100 int request_id = 0, retval = 1;
1102 /* Initialize InitConnection command packet */
1103 full_command_packet = tw_dev->command_packet_virt[request_id];
1104 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1105 full_command_packet->header.header_desc.size_header = 128;
1107 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1108 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1109 tw_initconnect->request_id = request_id;
1110 tw_initconnect->message_credits = message_credits;
1111 tw_initconnect->features = set_features;
1113 /* Turn on 64-bit sgl support if we need to */
1114 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1116 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1117 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1118 tw_initconnect->fw_srl = current_fw_srl;
1119 tw_initconnect->fw_arch_id = current_fw_arch_id;
1120 tw_initconnect->fw_branch = current_fw_branch;
1121 tw_initconnect->fw_build = current_fw_build;
1123 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1125 /* Send command packet to the board */
1126 twa_post_command_packet(tw_dev, request_id, 1);
1128 /* Poll for completion */
1129 if (twa_poll_response(tw_dev, request_id, 30)) {
1130 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1132 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1133 *fw_on_ctlr_srl = tw_initconnect->fw_srl;
1134 *fw_on_ctlr_arch_id = tw_initconnect->fw_arch_id;
1135 *fw_on_ctlr_branch = tw_initconnect->fw_branch;
1136 *fw_on_ctlr_build = tw_initconnect->fw_build;
1137 *init_connect_result = tw_initconnect->result;
1142 tw_dev->posted_request_count--;
1143 tw_dev->state[request_id] = TW_S_INITIAL;
1146 } /* End twa_initconnection() */
1148 /* This function will initialize the fields of a device extension */
1149 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1153 /* Initialize command packet buffers */
1154 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1155 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1159 /* Initialize generic buffer */
1160 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1161 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1165 /* Allocate event info space */
1166 tw_dev->event_queue[0] = kmalloc(sizeof(TW_Event) * TW_Q_LENGTH, GFP_KERNEL);
1167 if (!tw_dev->event_queue[0]) {
1168 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1172 memset(tw_dev->event_queue[0], 0, sizeof(TW_Event) * TW_Q_LENGTH);
1174 for (i = 0; i < TW_Q_LENGTH; i++) {
1175 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1176 tw_dev->free_queue[i] = i;
1177 tw_dev->state[i] = TW_S_INITIAL;
1180 tw_dev->pending_head = TW_Q_START;
1181 tw_dev->pending_tail = TW_Q_START;
1182 tw_dev->free_head = TW_Q_START;
1183 tw_dev->free_tail = TW_Q_START;
1184 tw_dev->error_sequence_id = 1;
1185 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1187 mutex_init(&tw_dev->ioctl_lock);
1188 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1193 } /* End twa_initialize_device_extension() */
1195 /* This function is the interrupt service routine */
1196 static irqreturn_t twa_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1198 int request_id, error = 0;
1199 u32 status_reg_value;
1200 TW_Response_Queue response_que;
1201 TW_Command_Full *full_command_packet;
1202 TW_Command *command_packet;
1203 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1206 /* Get the per adapter lock */
1207 spin_lock(tw_dev->host->host_lock);
1209 /* Read the registers */
1210 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1212 /* Check if this is our interrupt, otherwise bail */
1213 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1214 goto twa_interrupt_bail;
1218 /* Check controller for errors */
1219 if (twa_check_bits(status_reg_value)) {
1220 if (twa_decode_bits(tw_dev, status_reg_value)) {
1221 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1222 goto twa_interrupt_bail;
1226 /* Handle host interrupt */
1227 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1228 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1230 /* Handle attention interrupt */
1231 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1232 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1233 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1234 twa_get_request_id(tw_dev, &request_id);
1236 error = twa_aen_read_queue(tw_dev, request_id);
1238 tw_dev->state[request_id] = TW_S_COMPLETED;
1239 twa_free_request_id(tw_dev, request_id);
1240 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1245 /* Handle command interrupt */
1246 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1247 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1248 /* Drain as many pending commands as we can */
1249 while (tw_dev->pending_request_count > 0) {
1250 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1251 if (tw_dev->state[request_id] != TW_S_PENDING) {
1252 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1253 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1254 goto twa_interrupt_bail;
1256 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1257 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1258 tw_dev->pending_request_count--;
1260 /* If we get here, we will continue re-posting on the next command interrupt */
1266 /* Handle response interrupt */
1267 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1269 /* Drain the response queue from the board */
1270 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1271 /* Complete the response */
1272 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1273 request_id = TW_RESID_OUT(response_que.response_id);
1274 full_command_packet = tw_dev->command_packet_virt[request_id];
1276 command_packet = &full_command_packet->command.oldcommand;
1277 /* Check for command packet errors */
1278 if (full_command_packet->command.newcommand.status != 0) {
1279 if (tw_dev->srb[request_id] != 0) {
1280 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1282 /* Skip ioctl error prints */
1283 if (request_id != tw_dev->chrdev_request_id) {
1284 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1289 /* Check for correct state */
1290 if (tw_dev->state[request_id] != TW_S_POSTED) {
1291 if (tw_dev->srb[request_id] != 0) {
1292 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1293 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1294 goto twa_interrupt_bail;
1298 /* Check for internal command completion */
1299 if (tw_dev->srb[request_id] == 0) {
1300 if (request_id != tw_dev->chrdev_request_id) {
1301 if (twa_aen_complete(tw_dev, request_id))
1302 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1304 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1305 wake_up(&tw_dev->ioctl_wqueue);
1308 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1309 /* If no error command was a success */
1311 tw_dev->srb[request_id]->result = (DID_OK << 16);
1314 /* If error, command failed */
1316 /* Ask for a host reset */
1317 tw_dev->srb[request_id]->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1320 /* Report residual bytes for single sgl */
1321 if ((tw_dev->srb[request_id]->use_sg <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1322 if (full_command_packet->command.newcommand.sg_list[0].length < tw_dev->srb[request_id]->request_bufflen)
1323 tw_dev->srb[request_id]->resid = tw_dev->srb[request_id]->request_bufflen - full_command_packet->command.newcommand.sg_list[0].length;
1326 /* Now complete the io */
1327 tw_dev->state[request_id] = TW_S_COMPLETED;
1328 twa_free_request_id(tw_dev, request_id);
1329 tw_dev->posted_request_count--;
1330 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1331 twa_unmap_scsi_data(tw_dev, request_id);
1334 /* Check for valid status after each drain */
1335 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1336 if (twa_check_bits(status_reg_value)) {
1337 if (twa_decode_bits(tw_dev, status_reg_value)) {
1338 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1339 goto twa_interrupt_bail;
1346 spin_unlock(tw_dev->host->host_lock);
1347 return IRQ_RETVAL(handled);
1348 } /* End twa_interrupt() */
1350 /* This function will load the request id and various sgls for ioctls */
1351 static void twa_load_sgl(TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
1353 TW_Command *oldcommand;
1354 TW_Command_Apache *newcommand;
1357 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1358 newcommand = &full_command_packet->command.newcommand;
1359 newcommand->request_id__lunl =
1360 TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id);
1361 newcommand->sg_list[0].address = dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1;
1362 newcommand->sg_list[0].length = length;
1363 newcommand->sgl_entries__lunh =
1364 TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), 1);
1366 oldcommand = &full_command_packet->command.oldcommand;
1367 oldcommand->request_id = request_id;
1369 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1370 /* Load the sg list */
1371 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1372 sgl->address = dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1;
1373 sgl->length = length;
1375 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1376 oldcommand->size += 1;
1379 } /* End twa_load_sgl() */
1381 /* This function will perform a pci-dma mapping for a scatter gather list */
1382 static int twa_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
1385 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1386 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1389 if (cmd->use_sg == 0)
1392 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, DMA_BIDIRECTIONAL);
1395 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to map scatter gather list");
1399 cmd->SCp.phase = TW_PHASE_SGLIST;
1400 cmd->SCp.have_data_in = use_sg;
1404 } /* End twa_map_scsi_sg_data() */
1406 /* This function will perform a pci-dma map for a single buffer */
1407 static dma_addr_t twa_map_scsi_single_data(TW_Device_Extension *tw_dev, int request_id)
1410 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1411 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1412 dma_addr_t retval = 0;
1414 if (cmd->request_bufflen == 0) {
1419 mapping = pci_map_single(pdev, cmd->request_buffer, cmd->request_bufflen, DMA_BIDIRECTIONAL);
1422 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1d, "Failed to map page");
1426 cmd->SCp.phase = TW_PHASE_SINGLE;
1427 cmd->SCp.have_data_in = mapping;
1431 } /* End twa_map_scsi_single_data() */
1433 /* This function will poll for a response interrupt of a request */
1434 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1436 int retval = 1, found = 0, response_request_id;
1437 TW_Response_Queue response_queue;
1438 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1440 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1441 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1442 response_request_id = TW_RESID_OUT(response_queue.response_id);
1443 if (request_id != response_request_id) {
1444 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1447 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1448 if (full_command_packet->command.newcommand.status != 0) {
1450 twa_fill_sense(tw_dev, request_id, 0, 0);
1455 if (full_command_packet->command.oldcommand.status != 0) {
1457 twa_fill_sense(tw_dev, request_id, 0, 0);
1468 } /* End twa_poll_response() */
1470 /* This function will poll the status register for a flag */
1471 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1473 u32 status_reg_value;
1474 unsigned long before;
1477 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1480 if (twa_check_bits(status_reg_value))
1481 twa_decode_bits(tw_dev, status_reg_value);
1483 while ((status_reg_value & flag) != flag) {
1484 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1486 if (twa_check_bits(status_reg_value))
1487 twa_decode_bits(tw_dev, status_reg_value);
1489 if (time_after(jiffies, before + HZ * seconds))
1497 } /* End twa_poll_status() */
1499 /* This function will poll the status register for disappearance of a flag */
1500 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1502 u32 status_reg_value;
1503 unsigned long before;
1506 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1509 if (twa_check_bits(status_reg_value))
1510 twa_decode_bits(tw_dev, status_reg_value);
1512 while ((status_reg_value & flag) != 0) {
1513 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1514 if (twa_check_bits(status_reg_value))
1515 twa_decode_bits(tw_dev, status_reg_value);
1517 if (time_after(jiffies, before + HZ * seconds))
1525 } /* End twa_poll_status_gone() */
1527 /* This function will attempt to post a command packet to the board */
1528 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1530 u32 status_reg_value;
1531 dma_addr_t command_que_value;
1534 command_que_value = tw_dev->command_packet_phys[request_id];
1535 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1537 if (twa_check_bits(status_reg_value))
1538 twa_decode_bits(tw_dev, status_reg_value);
1540 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1542 /* Only pend internal driver commands */
1544 retval = SCSI_MLQUEUE_HOST_BUSY;
1548 /* Couldn't post the command packet, so we do it later */
1549 if (tw_dev->state[request_id] != TW_S_PENDING) {
1550 tw_dev->state[request_id] = TW_S_PENDING;
1551 tw_dev->pending_request_count++;
1552 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1553 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1555 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1556 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1558 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1561 /* We successfully posted the command packet */
1562 if (sizeof(dma_addr_t) > 4) {
1563 command_que_value += TW_COMMAND_OFFSET;
1564 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1565 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1567 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1569 tw_dev->state[request_id] = TW_S_POSTED;
1570 tw_dev->posted_request_count++;
1571 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1572 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1578 } /* End twa_post_command_packet() */
1580 /* This function will reset a device extension */
1581 static int twa_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset)
1585 unsigned long flags = 0;
1587 set_bit(TW_IN_RESET, &tw_dev->flags);
1588 TW_DISABLE_INTERRUPTS(tw_dev);
1589 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1590 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1592 /* Abort all requests that are in progress */
1593 for (i = 0; i < TW_Q_LENGTH; i++) {
1594 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1595 (tw_dev->state[i] != TW_S_INITIAL) &&
1596 (tw_dev->state[i] != TW_S_COMPLETED)) {
1597 if (tw_dev->srb[i]) {
1598 tw_dev->srb[i]->result = (DID_RESET << 16);
1599 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1600 twa_unmap_scsi_data(tw_dev, i);
1605 /* Reset queues and counts */
1606 for (i = 0; i < TW_Q_LENGTH; i++) {
1607 tw_dev->free_queue[i] = i;
1608 tw_dev->state[i] = TW_S_INITIAL;
1610 tw_dev->free_head = TW_Q_START;
1611 tw_dev->free_tail = TW_Q_START;
1612 tw_dev->posted_request_count = 0;
1613 tw_dev->pending_request_count = 0;
1614 tw_dev->pending_head = TW_Q_START;
1615 tw_dev->pending_tail = TW_Q_START;
1616 tw_dev->reset_print = 0;
1618 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1620 if (twa_reset_sequence(tw_dev, 1))
1623 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1625 /* Wake up any ioctl that was pending before the reset */
1626 if ((tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE) || (ioctl_reset)) {
1627 clear_bit(TW_IN_RESET, &tw_dev->flags);
1629 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1630 wake_up(&tw_dev->ioctl_wqueue);
1635 } /* End twa_reset_device_extension() */
1637 /* This function will reset a controller */
1638 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1640 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1642 while (tries < TW_MAX_RESET_TRIES) {
1643 if (do_soft_reset) {
1644 TW_SOFT_RESET(tw_dev);
1645 /* Clear pchip/response queue on 9550SX */
1646 if (twa_empty_response_queue_large(tw_dev)) {
1647 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1654 /* Make sure controller is in a good state */
1655 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1656 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1662 /* Empty response queue */
1663 if (twa_empty_response_queue(tw_dev)) {
1664 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1672 /* Check for compatibility/flash */
1673 if (twa_check_srl(tw_dev, &flashed)) {
1674 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1685 /* Drain the AEN queue */
1686 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1687 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1693 /* If we got here, controller is in a good state */
1699 } /* End twa_reset_sequence() */
1701 /* This funciton returns unit geometry in cylinders/heads/sectors */
1702 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1704 int heads, sectors, cylinders;
1705 TW_Device_Extension *tw_dev;
1707 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1709 if (capacity >= 0x200000) {
1712 cylinders = sector_div(capacity, heads * sectors);
1716 cylinders = sector_div(capacity, heads * sectors);
1721 geom[2] = cylinders;
1724 } /* End twa_scsi_biosparam() */
1726 /* This is the new scsi eh reset function */
1727 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1729 TW_Device_Extension *tw_dev = NULL;
1730 int retval = FAILED;
1732 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1734 tw_dev->num_resets++;
1736 sdev_printk(KERN_WARNING, SCpnt->device,
1737 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1738 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1740 /* Now reset the card and some of the device extension data */
1741 if (twa_reset_device_extension(tw_dev, 0)) {
1742 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1749 } /* End twa_scsi_eh_reset() */
1751 /* This is the main scsi queue function to handle scsi opcodes */
1752 static int twa_scsi_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1754 int request_id, retval;
1755 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1757 /* Check if this FW supports luns */
1758 if ((SCpnt->device->lun != 0) && (tw_dev->working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1759 SCpnt->result = (DID_BAD_TARGET << 16);
1765 /* Save done function into scsi_cmnd struct */
1766 SCpnt->scsi_done = done;
1768 /* Get a free request id */
1769 twa_get_request_id(tw_dev, &request_id);
1771 /* Save the scsi command for use by the ISR */
1772 tw_dev->srb[request_id] = SCpnt;
1774 /* Initialize phase to zero */
1775 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1777 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1779 case SCSI_MLQUEUE_HOST_BUSY:
1780 twa_free_request_id(tw_dev, request_id);
1783 tw_dev->state[request_id] = TW_S_COMPLETED;
1784 twa_free_request_id(tw_dev, request_id);
1785 SCpnt->result = (DID_ERROR << 16);
1791 } /* End twa_scsi_queue() */
1793 /* This function hands scsi cdb's to the firmware */
1794 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1796 TW_Command_Full *full_command_packet;
1797 TW_Command_Apache *command_packet;
1798 u32 num_sectors = 0x0;
1800 struct scsi_cmnd *srb = NULL;
1801 struct scatterlist *sglist = NULL;
1802 dma_addr_t buffaddr = 0x0;
1805 if (tw_dev->srb[request_id]) {
1806 if (tw_dev->srb[request_id]->request_buffer) {
1807 sglist = (struct scatterlist *)tw_dev->srb[request_id]->request_buffer;
1809 srb = tw_dev->srb[request_id];
1812 /* Initialize command packet */
1813 full_command_packet = tw_dev->command_packet_virt[request_id];
1814 full_command_packet->header.header_desc.size_header = 128;
1815 full_command_packet->header.status_block.error = 0;
1816 full_command_packet->header.status_block.severity__reserved = 0;
1818 command_packet = &full_command_packet->command.newcommand;
1819 command_packet->status = 0;
1820 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1822 /* We forced 16 byte cdb use earlier */
1824 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1826 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1829 command_packet->unit = srb->device->id;
1830 command_packet->request_id__lunl =
1831 TW_REQ_LUN_IN(srb->device->lun, request_id);
1833 command_packet->request_id__lunl =
1834 TW_REQ_LUN_IN(0, request_id);
1835 command_packet->unit = 0;
1838 command_packet->sgl_offset = 16;
1841 /* Map sglist from scsi layer to cmd packet */
1842 if (tw_dev->srb[request_id]->use_sg == 0) {
1843 if (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH) {
1844 command_packet->sg_list[0].address = tw_dev->generic_buffer_phys[request_id];
1845 command_packet->sg_list[0].length = TW_MIN_SGL_LENGTH;
1846 if (tw_dev->srb[request_id]->sc_data_direction == DMA_TO_DEVICE || tw_dev->srb[request_id]->sc_data_direction == DMA_BIDIRECTIONAL)
1847 memcpy(tw_dev->generic_buffer_virt[request_id], tw_dev->srb[request_id]->request_buffer, tw_dev->srb[request_id]->request_bufflen);
1849 buffaddr = twa_map_scsi_single_data(tw_dev, request_id);
1853 command_packet->sg_list[0].address = buffaddr;
1854 command_packet->sg_list[0].length = tw_dev->srb[request_id]->request_bufflen;
1856 command_packet->sgl_entries__lunh = TW_REQ_LUN_IN((srb->device->lun >> 4), 1);
1858 if (command_packet->sg_list[0].address & TW_ALIGNMENT_9000_SGL) {
1859 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2d, "Found unaligned address during execute scsi");
1864 if (tw_dev->srb[request_id]->use_sg > 0) {
1865 if ((tw_dev->srb[request_id]->use_sg == 1) && (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH)) {
1866 if (tw_dev->srb[request_id]->sc_data_direction == DMA_TO_DEVICE || tw_dev->srb[request_id]->sc_data_direction == DMA_BIDIRECTIONAL) {
1867 struct scatterlist *sg = (struct scatterlist *)tw_dev->srb[request_id]->request_buffer;
1868 char *buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
1869 memcpy(tw_dev->generic_buffer_virt[request_id], buf, sg->length);
1870 kunmap_atomic(buf - sg->offset, KM_IRQ0);
1872 command_packet->sg_list[0].address = tw_dev->generic_buffer_phys[request_id];
1873 command_packet->sg_list[0].length = TW_MIN_SGL_LENGTH;
1875 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1879 for (i = 0; i < sg_count; i++) {
1880 command_packet->sg_list[i].address = sg_dma_address(&sglist[i]);
1881 command_packet->sg_list[i].length = sg_dma_len(&sglist[i]);
1882 if (command_packet->sg_list[i].address & TW_ALIGNMENT_9000_SGL) {
1883 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1888 command_packet->sgl_entries__lunh = TW_REQ_LUN_IN((srb->device->lun >> 4), tw_dev->srb[request_id]->use_sg);
1891 /* Internal cdb post */
1892 for (i = 0; i < use_sg; i++) {
1893 command_packet->sg_list[i].address = sglistarg[i].address;
1894 command_packet->sg_list[i].length = sglistarg[i].length;
1895 if (command_packet->sg_list[i].address & TW_ALIGNMENT_9000_SGL) {
1896 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1900 command_packet->sgl_entries__lunh = TW_REQ_LUN_IN(0, use_sg);
1904 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1905 num_sectors = (u32)srb->cmnd[4];
1907 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1908 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1911 /* Update sector statistic */
1912 tw_dev->sector_count = num_sectors;
1913 if (tw_dev->sector_count > tw_dev->max_sector_count)
1914 tw_dev->max_sector_count = tw_dev->sector_count;
1916 /* Update SG statistics */
1918 tw_dev->sgl_entries = tw_dev->srb[request_id]->use_sg;
1919 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1920 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1923 /* Now post the command to the board */
1925 retval = twa_post_command_packet(tw_dev, request_id, 0);
1927 twa_post_command_packet(tw_dev, request_id, 1);
1932 } /* End twa_scsiop_execute_scsi() */
1934 /* This function completes an execute scsi operation */
1935 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1937 if (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH &&
1938 (tw_dev->srb[request_id]->sc_data_direction == DMA_FROM_DEVICE ||
1939 tw_dev->srb[request_id]->sc_data_direction == DMA_BIDIRECTIONAL)) {
1940 if (tw_dev->srb[request_id]->use_sg == 0) {
1941 memcpy(tw_dev->srb[request_id]->request_buffer,
1942 tw_dev->generic_buffer_virt[request_id],
1943 tw_dev->srb[request_id]->request_bufflen);
1945 if (tw_dev->srb[request_id]->use_sg == 1) {
1946 struct scatterlist *sg = (struct scatterlist *)tw_dev->srb[request_id]->request_buffer;
1947 char *buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
1948 memcpy(buf, tw_dev->generic_buffer_virt[request_id], sg->length);
1949 kunmap_atomic(buf - sg->offset, KM_IRQ0);
1952 } /* End twa_scsiop_execute_scsi_complete() */
1954 /* This function tells the controller to shut down */
1955 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1957 /* Disable interrupts */
1958 TW_DISABLE_INTERRUPTS(tw_dev);
1960 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1962 /* Tell the card we are shutting down */
1963 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1964 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1966 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1969 /* Clear all interrupts just before exit */
1970 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1971 } /* End __twa_shutdown() */
1973 /* Wrapper for __twa_shutdown */
1974 static void twa_shutdown(struct pci_dev *pdev)
1976 struct Scsi_Host *host = pci_get_drvdata(pdev);
1977 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1979 __twa_shutdown(tw_dev);
1980 } /* End twa_shutdown() */
1982 /* This function will look up a string */
1983 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1987 for (index = 0; ((code != table[index].code) &&
1988 (table[index].text != (char *)0)); index++);
1989 return(table[index].text);
1990 } /* End twa_string_lookup() */
1992 /* This function will perform a pci-dma unmap */
1993 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1995 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1996 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1998 switch(cmd->SCp.phase) {
1999 case TW_PHASE_SINGLE:
2000 pci_unmap_single(pdev, cmd->SCp.have_data_in, cmd->request_bufflen, DMA_BIDIRECTIONAL);
2002 case TW_PHASE_SGLIST:
2003 pci_unmap_sg(pdev, cmd->request_buffer, cmd->use_sg, DMA_BIDIRECTIONAL);
2006 } /* End twa_unmap_scsi_data() */
2008 /* scsi_host_template initializer */
2009 static struct scsi_host_template driver_template = {
2010 .module = THIS_MODULE,
2011 .name = "3ware 9000 Storage Controller",
2012 .queuecommand = twa_scsi_queue,
2013 .eh_host_reset_handler = twa_scsi_eh_reset,
2014 .bios_param = twa_scsi_biosparam,
2015 .change_queue_depth = twa_change_queue_depth,
2016 .can_queue = TW_Q_LENGTH-2,
2018 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
2019 .max_sectors = TW_MAX_SECTORS,
2020 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
2021 .use_clustering = ENABLE_CLUSTERING,
2022 .shost_attrs = twa_host_attrs,
2026 /* This function will probe and initialize a card */
2027 static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2029 struct Scsi_Host *host = NULL;
2030 TW_Device_Extension *tw_dev;
2032 int retval = -ENODEV;
2034 retval = pci_enable_device(pdev);
2036 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2037 goto out_disable_device;
2040 pci_set_master(pdev);
2042 retval = pci_set_dma_mask(pdev, sizeof(dma_addr_t) > 4 ? DMA_64BIT_MASK : DMA_32BIT_MASK);
2044 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2045 goto out_disable_device;
2048 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2050 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2052 goto out_disable_device;
2054 tw_dev = (TW_Device_Extension *)host->hostdata;
2056 memset(tw_dev, 0, sizeof(TW_Device_Extension));
2058 /* Save values to device extension */
2059 tw_dev->host = host;
2060 tw_dev->tw_pci_dev = pdev;
2062 if (twa_initialize_device_extension(tw_dev)) {
2063 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2064 goto out_free_device_extension;
2067 /* Request IO regions */
2068 retval = pci_request_regions(pdev, "3w-9xxx");
2070 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2071 goto out_free_device_extension;
2074 if (pdev->device == PCI_DEVICE_ID_3WARE_9000)
2075 mem_addr = pci_resource_start(pdev, 1);
2077 mem_addr = pci_resource_start(pdev, 2);
2079 /* Save base address */
2080 tw_dev->base_addr = ioremap(mem_addr, PAGE_SIZE);
2081 if (!tw_dev->base_addr) {
2082 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2083 goto out_release_mem_region;
2086 /* Disable interrupts on the card */
2087 TW_DISABLE_INTERRUPTS(tw_dev);
2089 /* Initialize the card */
2090 if (twa_reset_sequence(tw_dev, 0))
2091 goto out_release_mem_region;
2093 /* Set host specific parameters */
2094 host->max_id = TW_MAX_UNITS;
2095 host->max_cmd_len = TW_MAX_CDB_LEN;
2097 /* Channels aren't supported by adapter */
2098 host->max_lun = TW_MAX_LUNS(tw_dev->working_srl);
2099 host->max_channel = 0;
2101 /* Register the card with the kernel SCSI layer */
2102 retval = scsi_add_host(host, &pdev->dev);
2104 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2105 goto out_release_mem_region;
2108 pci_set_drvdata(pdev, host);
2110 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%x, IRQ: %d.\n",
2111 host->host_no, mem_addr, pdev->irq);
2112 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2114 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2115 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2116 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2117 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2118 *(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2119 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH));
2121 /* Now setup the interrupt handler */
2122 retval = request_irq(pdev->irq, twa_interrupt, SA_SHIRQ, "3w-9xxx", tw_dev);
2124 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2125 goto out_remove_host;
2128 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2129 twa_device_extension_count++;
2131 /* Re-enable interrupts on the card */
2132 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2134 /* Finally, scan the host */
2135 scsi_scan_host(host);
2137 if (twa_major == -1) {
2138 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2139 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2144 scsi_remove_host(host);
2145 out_release_mem_region:
2146 pci_release_regions(pdev);
2147 out_free_device_extension:
2148 twa_free_device_extension(tw_dev);
2149 scsi_host_put(host);
2151 pci_disable_device(pdev);
2154 } /* End twa_probe() */
2156 /* This function is called to remove a device */
2157 static void twa_remove(struct pci_dev *pdev)
2159 struct Scsi_Host *host = pci_get_drvdata(pdev);
2160 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2162 scsi_remove_host(tw_dev->host);
2164 /* Unregister character device */
2165 if (twa_major >= 0) {
2166 unregister_chrdev(twa_major, "twa");
2170 /* Free up the IRQ */
2171 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2173 /* Shutdown the card */
2174 __twa_shutdown(tw_dev);
2176 /* Free up the mem region */
2177 pci_release_regions(pdev);
2179 /* Free up device extension resources */
2180 twa_free_device_extension(tw_dev);
2182 scsi_host_put(tw_dev->host);
2183 pci_disable_device(pdev);
2184 twa_device_extension_count--;
2185 } /* End twa_remove() */
2187 /* PCI Devices supported by this driver */
2188 static struct pci_device_id twa_pci_tbl[] __devinitdata = {
2189 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2190 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2191 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2192 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2195 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2197 /* pci_driver initializer */
2198 static struct pci_driver twa_driver = {
2200 .id_table = twa_pci_tbl,
2202 .remove = twa_remove,
2203 .shutdown = twa_shutdown
2206 /* This function is called on driver initialization */
2207 static int __init twa_init(void)
2209 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2211 return pci_module_init(&twa_driver);
2212 } /* End twa_init() */
2214 /* This function is called on driver exit */
2215 static void __exit twa_exit(void)
2217 pci_unregister_driver(&twa_driver);
2218 } /* End twa_exit() */
2220 module_init(twa_init);
2221 module_exit(twa_exit);