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.
66 #include <linux/module.h>
67 #include <linux/reboot.h>
68 #include <linux/spinlock.h>
69 #include <linux/interrupt.h>
70 #include <linux/moduleparam.h>
71 #include <linux/errno.h>
72 #include <linux/types.h>
73 #include <linux/delay.h>
74 #include <linux/pci.h>
75 #include <linux/time.h>
78 #include <asm/uaccess.h>
79 #include <scsi/scsi.h>
80 #include <scsi/scsi_host.h>
81 #include <scsi/scsi_tcq.h>
82 #include <scsi/scsi_cmnd.h>
86 #define TW_DRIVER_VERSION "2.26.02.004"
87 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
88 static unsigned int twa_device_extension_count;
89 static int twa_major = -1;
90 extern struct timezone sys_tz;
92 /* Module parameters */
93 MODULE_AUTHOR ("AMCC");
94 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(TW_DRIVER_VERSION);
98 /* Function prototypes */
99 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
100 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
101 static char *twa_aen_severity_lookup(unsigned char severity_code);
102 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
103 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
104 static int twa_chrdev_open(struct inode *inode, struct file *file);
105 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
106 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
107 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
108 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
109 u32 set_features, unsigned short current_fw_srl,
110 unsigned short current_fw_arch_id,
111 unsigned short current_fw_branch,
112 unsigned short current_fw_build,
113 unsigned short *fw_on_ctlr_srl,
114 unsigned short *fw_on_ctlr_arch_id,
115 unsigned short *fw_on_ctlr_branch,
116 unsigned short *fw_on_ctlr_build,
117 u32 *init_connect_result);
118 static void twa_load_sgl(TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length);
119 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
120 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
121 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
122 static int twa_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset);
123 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
124 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg);
125 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
126 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
127 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id);
131 /* Show some statistics about the card */
132 static ssize_t twa_show_stats(struct class_device *class_dev, char *buf)
134 struct Scsi_Host *host = class_to_shost(class_dev);
135 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
136 unsigned long flags = 0;
139 spin_lock_irqsave(tw_dev->host->host_lock, flags);
140 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
141 "Current commands posted: %4d\n"
142 "Max commands posted: %4d\n"
143 "Current pending commands: %4d\n"
144 "Max pending commands: %4d\n"
145 "Last sgl length: %4d\n"
146 "Max sgl length: %4d\n"
147 "Last sector count: %4d\n"
148 "Max sector count: %4d\n"
149 "SCSI Host Resets: %4d\n"
152 tw_dev->posted_request_count,
153 tw_dev->max_posted_request_count,
154 tw_dev->pending_request_count,
155 tw_dev->max_pending_request_count,
157 tw_dev->max_sgl_entries,
158 tw_dev->sector_count,
159 tw_dev->max_sector_count,
162 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
164 } /* End twa_show_stats() */
166 /* This function will set a devices queue depth */
167 static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth)
169 if (queue_depth > TW_Q_LENGTH-2)
170 queue_depth = TW_Q_LENGTH-2;
171 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
173 } /* End twa_change_queue_depth() */
175 /* Create sysfs 'stats' entry */
176 static struct class_device_attribute twa_host_stats_attr = {
181 .show = twa_show_stats
184 /* Host attributes initializer */
185 static struct class_device_attribute *twa_host_attrs[] = {
186 &twa_host_stats_attr,
190 /* File operations struct for character device */
191 static struct file_operations twa_fops = {
192 .owner = THIS_MODULE,
193 .ioctl = twa_chrdev_ioctl,
194 .open = twa_chrdev_open,
198 /* This function will complete an aen request from the isr */
199 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
201 TW_Command_Full *full_command_packet;
202 TW_Command *command_packet;
203 TW_Command_Apache_Header *header;
207 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
208 tw_dev->posted_request_count--;
209 aen = header->status_block.error;
210 full_command_packet = tw_dev->command_packet_virt[request_id];
211 command_packet = &full_command_packet->command.oldcommand;
213 /* First check for internal completion of set param for time sync */
214 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
215 /* Keep reading the queue in case there are more aen's */
216 if (twa_aen_read_queue(tw_dev, request_id))
225 case TW_AEN_QUEUE_EMPTY:
226 /* Quit reading the queue if this is the last one */
228 case TW_AEN_SYNC_TIME_WITH_HOST:
229 twa_aen_sync_time(tw_dev, request_id);
233 twa_aen_queue_event(tw_dev, header);
235 /* If there are more aen's, keep reading the queue */
236 if (twa_aen_read_queue(tw_dev, request_id))
245 tw_dev->state[request_id] = TW_S_COMPLETED;
246 twa_free_request_id(tw_dev, request_id);
247 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
250 } /* End twa_aen_complete() */
252 /* This function will drain aen queue */
253 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
256 char cdb[TW_MAX_CDB_LEN];
257 TW_SG_Entry sglist[1];
258 int finished = 0, count = 0;
259 TW_Command_Full *full_command_packet;
260 TW_Command_Apache_Header *header;
262 int first_reset = 0, queue = 0, retval = 1;
269 full_command_packet = tw_dev->command_packet_virt[request_id];
270 memset(full_command_packet, 0, sizeof(TW_Command_Full));
273 memset(&cdb, 0, TW_MAX_CDB_LEN);
274 cdb[0] = REQUEST_SENSE; /* opcode */
275 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
277 /* Initialize sglist */
278 memset(&sglist, 0, sizeof(TW_SG_Entry));
279 sglist[0].length = TW_SECTOR_SIZE;
280 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
282 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
283 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
287 /* Mark internal command */
288 tw_dev->srb[request_id] = NULL;
291 /* Send command to the board */
292 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
293 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
297 /* Now poll for completion */
298 if (twa_poll_response(tw_dev, request_id, 30)) {
299 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
300 tw_dev->posted_request_count--;
304 tw_dev->posted_request_count--;
305 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
306 aen = header->status_block.error;
311 case TW_AEN_QUEUE_EMPTY:
312 if (first_reset != 1)
317 case TW_AEN_SOFT_RESET:
318 if (first_reset == 0)
323 case TW_AEN_SYNC_TIME_WITH_HOST:
329 /* Now queue an event info */
331 twa_aen_queue_event(tw_dev, header);
332 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
334 if (count == TW_MAX_AEN_DRAIN)
339 tw_dev->state[request_id] = TW_S_INITIAL;
341 } /* End twa_aen_drain_queue() */
343 /* This function will queue an event */
344 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
355 /* Fill out event info */
356 event = tw_dev->event_queue[tw_dev->error_index];
358 /* Check for clobber */
361 sprintf(host, " scsi%d:", tw_dev->host->host_no);
362 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
363 tw_dev->aen_clobber = 1;
366 aen = header->status_block.error;
367 memset(event, 0, sizeof(TW_Event));
369 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
370 do_gettimeofday(&time);
371 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
372 event->time_stamp_sec = local_time;
373 event->aen_code = aen;
374 event->retrieved = TW_AEN_NOT_RETRIEVED;
375 event->sequence_id = tw_dev->error_sequence_id;
376 tw_dev->error_sequence_id++;
378 /* Check for embedded error string */
379 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
381 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
382 event->parameter_len = strlen(header->err_specific_desc);
383 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len);
384 if (event->severity != TW_AEN_SEVERITY_DEBUG)
385 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
387 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
388 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
389 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
390 header->err_specific_desc);
394 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
395 tw_dev->event_queue_wrapped = 1;
396 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
397 } /* End twa_aen_queue_event() */
399 /* This function will read the aen queue from the isr */
400 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
402 char cdb[TW_MAX_CDB_LEN];
403 TW_SG_Entry sglist[1];
404 TW_Command_Full *full_command_packet;
407 full_command_packet = tw_dev->command_packet_virt[request_id];
408 memset(full_command_packet, 0, sizeof(TW_Command_Full));
411 memset(&cdb, 0, TW_MAX_CDB_LEN);
412 cdb[0] = REQUEST_SENSE; /* opcode */
413 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
415 /* Initialize sglist */
416 memset(&sglist, 0, sizeof(TW_SG_Entry));
417 sglist[0].length = TW_SECTOR_SIZE;
418 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
420 /* Mark internal command */
421 tw_dev->srb[request_id] = NULL;
423 /* Now post the command packet */
424 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
425 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
431 } /* End twa_aen_read_queue() */
433 /* This function will look up an AEN severity string */
434 static char *twa_aen_severity_lookup(unsigned char severity_code)
438 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
439 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
442 retval = twa_aen_severity_table[severity_code];
445 } /* End twa_aen_severity_lookup() */
447 /* This function will sync firmware time with the host time */
448 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
452 TW_Command_Full *full_command_packet;
453 TW_Command *command_packet;
454 TW_Param_Apache *param;
457 /* Fill out the command packet */
458 full_command_packet = tw_dev->command_packet_virt[request_id];
459 memset(full_command_packet, 0, sizeof(TW_Command_Full));
460 command_packet = &full_command_packet->command.oldcommand;
461 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
462 command_packet->request_id = request_id;
463 command_packet->byte8_offset.param.sgl[0].address = tw_dev->generic_buffer_phys[request_id];
464 command_packet->byte8_offset.param.sgl[0].length = TW_SECTOR_SIZE;
465 command_packet->size = TW_COMMAND_SIZE;
466 command_packet->byte6_offset.parameter_count = 1;
468 /* Setup the param */
469 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
470 memset(param, 0, TW_SECTOR_SIZE);
471 param->table_id = TW_TIMEKEEP_TABLE | 0x8000; /* Controller time keep table */
472 param->parameter_id = 0x3; /* SchedulerTime */
473 param->parameter_size_bytes = 4;
475 /* Convert system time in UTC to local time seconds since last
477 do_gettimeofday(&utc);
478 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
479 schedulertime = local_time - (3 * 86400);
480 schedulertime = schedulertime % 604800;
482 memcpy(param->data, &schedulertime, sizeof(u32));
484 /* Mark internal command */
485 tw_dev->srb[request_id] = NULL;
487 /* Now post the command */
488 twa_post_command_packet(tw_dev, request_id, 1);
489 } /* End twa_aen_sync_time() */
491 /* This function will allocate memory and check if it is correctly aligned */
492 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
495 dma_addr_t dma_handle;
496 unsigned long *cpu_addr;
499 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
501 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
505 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
506 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
507 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
511 memset(cpu_addr, 0, size*TW_Q_LENGTH);
513 for (i = 0; i < TW_Q_LENGTH; i++) {
516 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
517 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
520 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
521 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
528 } /* End twa_allocate_memory() */
530 /* This function will check the status register for unexpected bits */
531 static int twa_check_bits(u32 status_reg_value)
535 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
537 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
543 } /* End twa_check_bits() */
545 /* This function will check the srl and decide if we are compatible */
546 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
549 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
550 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
551 u32 init_connect_result = 0;
553 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
554 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
555 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
556 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
557 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
558 &fw_on_ctlr_build, &init_connect_result)) {
559 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
563 tw_dev->working_srl = fw_on_ctlr_srl;
564 tw_dev->working_branch = fw_on_ctlr_branch;
565 tw_dev->working_build = fw_on_ctlr_build;
567 /* Try base mode compatibility */
568 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
569 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
570 TW_EXTENDED_INIT_CONNECT,
571 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
572 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
573 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
574 &fw_on_ctlr_branch, &fw_on_ctlr_build,
575 &init_connect_result)) {
576 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
579 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
580 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
581 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
583 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
587 tw_dev->working_srl = TW_BASE_FW_SRL;
588 tw_dev->working_branch = TW_BASE_FW_BRANCH;
589 tw_dev->working_build = TW_BASE_FW_BUILD;
594 } /* End twa_check_srl() */
596 /* This function handles ioctl for the character device */
597 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
600 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
601 dma_addr_t dma_handle;
603 unsigned int sequence_id = 0;
604 unsigned char event_index, start_index;
605 TW_Ioctl_Driver_Command driver_command;
606 TW_Ioctl_Buf_Apache *tw_ioctl;
608 TW_Command_Full *full_command_packet;
609 TW_Compatibility_Info *tw_compat_info;
611 struct timeval current_time;
613 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
614 int retval = TW_IOCTL_ERROR_OS_EFAULT;
615 void __user *argp = (void __user *)arg;
617 /* Only let one of these through at a time */
618 if (down_interruptible(&tw_dev->ioctl_sem)) {
619 retval = TW_IOCTL_ERROR_OS_EINTR;
623 /* First copy down the driver command */
624 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
627 /* Check data buffer size */
628 if (driver_command.buffer_length > TW_MAX_SECTORS * 512) {
629 retval = TW_IOCTL_ERROR_OS_EINVAL;
633 /* Hardware can only do multiple of 512 byte transfers */
634 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
636 /* Now allocate ioctl buf memory */
637 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);
639 retval = TW_IOCTL_ERROR_OS_ENOMEM;
643 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
645 /* Now copy down the entire ioctl */
646 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
649 /* See which ioctl we are doing */
651 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
652 spin_lock_irqsave(tw_dev->host->host_lock, flags);
653 twa_get_request_id(tw_dev, &request_id);
655 /* Flag internal command */
656 tw_dev->srb[request_id] = NULL;
658 /* Flag chrdev ioctl */
659 tw_dev->chrdev_request_id = request_id;
661 full_command_packet = &tw_ioctl->firmware_command;
663 /* Load request id and sglist for both command types */
664 twa_load_sgl(full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
666 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
668 /* Now post the command packet to the controller */
669 twa_post_command_packet(tw_dev, request_id, 1);
670 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
672 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
674 /* Now wait for command to complete */
675 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
677 /* See if we reset while waiting for the ioctl to complete */
678 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
679 clear_bit(TW_IN_RESET, &tw_dev->flags);
680 retval = TW_IOCTL_ERROR_OS_ERESTARTSYS;
684 /* We timed out, and didn't get an interrupt */
685 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
686 /* Now we need to reset the board */
687 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
688 tw_dev->host->host_no, TW_DRIVER, 0xc,
690 retval = TW_IOCTL_ERROR_OS_EIO;
691 spin_lock_irqsave(tw_dev->host->host_lock, flags);
692 tw_dev->state[request_id] = TW_S_COMPLETED;
693 twa_free_request_id(tw_dev, request_id);
694 tw_dev->posted_request_count--;
695 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
696 twa_reset_device_extension(tw_dev, 1);
700 /* Now copy in the command packet response */
701 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
703 /* Now complete the io */
704 spin_lock_irqsave(tw_dev->host->host_lock, flags);
705 tw_dev->posted_request_count--;
706 tw_dev->state[request_id] = TW_S_COMPLETED;
707 twa_free_request_id(tw_dev, request_id);
708 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
710 case TW_IOCTL_GET_COMPATIBILITY_INFO:
711 tw_ioctl->driver_command.status = 0;
712 /* Copy compatiblity struct into ioctl data buffer */
713 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
714 strncpy(tw_compat_info->driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
715 tw_compat_info->working_srl = tw_dev->working_srl;
716 tw_compat_info->working_branch = tw_dev->working_branch;
717 tw_compat_info->working_build = tw_dev->working_build;
718 tw_compat_info->driver_srl_high = TW_CURRENT_DRIVER_SRL;
719 tw_compat_info->driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
720 tw_compat_info->driver_build_high = TW_CURRENT_DRIVER_BUILD;
721 tw_compat_info->driver_srl_low = TW_BASE_FW_SRL;
722 tw_compat_info->driver_branch_low = TW_BASE_FW_BRANCH;
723 tw_compat_info->driver_build_low = TW_BASE_FW_BUILD;
725 case TW_IOCTL_GET_LAST_EVENT:
726 if (tw_dev->event_queue_wrapped) {
727 if (tw_dev->aen_clobber) {
728 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
729 tw_dev->aen_clobber = 0;
731 tw_ioctl->driver_command.status = 0;
733 if (!tw_dev->error_index) {
734 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
737 tw_ioctl->driver_command.status = 0;
739 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
740 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
741 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
743 case TW_IOCTL_GET_FIRST_EVENT:
744 if (tw_dev->event_queue_wrapped) {
745 if (tw_dev->aen_clobber) {
746 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
747 tw_dev->aen_clobber = 0;
749 tw_ioctl->driver_command.status = 0;
750 event_index = tw_dev->error_index;
752 if (!tw_dev->error_index) {
753 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
756 tw_ioctl->driver_command.status = 0;
759 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
760 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
762 case TW_IOCTL_GET_NEXT_EVENT:
763 event = (TW_Event *)tw_ioctl->data_buffer;
764 sequence_id = event->sequence_id;
765 tw_ioctl->driver_command.status = 0;
767 if (tw_dev->event_queue_wrapped) {
768 if (tw_dev->aen_clobber) {
769 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
770 tw_dev->aen_clobber = 0;
772 start_index = tw_dev->error_index;
774 if (!tw_dev->error_index) {
775 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
780 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
782 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
783 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
784 tw_dev->aen_clobber = 1;
785 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
788 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
789 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
791 case TW_IOCTL_GET_PREVIOUS_EVENT:
792 event = (TW_Event *)tw_ioctl->data_buffer;
793 sequence_id = event->sequence_id;
794 tw_ioctl->driver_command.status = 0;
796 if (tw_dev->event_queue_wrapped) {
797 if (tw_dev->aen_clobber) {
798 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
799 tw_dev->aen_clobber = 0;
801 start_index = tw_dev->error_index;
803 if (!tw_dev->error_index) {
804 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
809 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
811 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
812 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
813 tw_dev->aen_clobber = 1;
814 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
817 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
818 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
820 case TW_IOCTL_GET_LOCK:
821 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
822 do_gettimeofday(¤t_time);
823 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
825 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
826 tw_dev->ioctl_sem_lock = 1;
827 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
828 tw_ioctl->driver_command.status = 0;
829 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
831 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
832 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
835 case TW_IOCTL_RELEASE_LOCK:
836 if (tw_dev->ioctl_sem_lock == 1) {
837 tw_dev->ioctl_sem_lock = 0;
838 tw_ioctl->driver_command.status = 0;
840 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
844 retval = TW_IOCTL_ERROR_OS_ENOTTY;
848 /* Now copy the entire response to userspace */
849 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
852 /* Now free ioctl buf memory */
853 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
855 up(&tw_dev->ioctl_sem);
858 } /* End twa_chrdev_ioctl() */
860 /* This function handles open for the character device */
861 static int twa_chrdev_open(struct inode *inode, struct file *file)
863 unsigned int minor_number;
864 int retval = TW_IOCTL_ERROR_OS_ENODEV;
866 minor_number = iminor(inode);
867 if (minor_number >= twa_device_extension_count)
872 } /* End twa_chrdev_open() */
874 /* This function will print readable messages from status register errors */
875 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
879 /* Check for various error conditions and handle them appropriately */
880 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
881 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
882 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
885 if (status_reg_value & TW_STATUS_PCI_ABORT) {
886 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
887 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
888 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
891 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
892 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
893 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
896 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
897 if (tw_dev->reset_print == 0) {
898 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
899 tw_dev->reset_print = 1;
906 } /* End twa_decode_bits() */
908 /* This function will empty the response queue */
909 static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
911 u32 status_reg_value, response_que_value;
912 int count = 0, retval = 1;
914 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
916 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
917 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
918 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
921 if (count == TW_MAX_RESPONSE_DRAIN)
927 } /* End twa_empty_response_queue() */
929 /* This function will clear the pchip/response queue on 9550SX */
930 static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
932 u32 status_reg_value, response_que_value;
933 int count = 0, retval = 1;
935 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9550SX) {
936 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
938 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
939 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
940 if ((response_que_value & TW_9550SX_DRAIN_COMPLETED) == TW_9550SX_DRAIN_COMPLETED) {
941 /* P-chip settle time */
946 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
949 if (count == TW_MAX_RESPONSE_DRAIN)
957 } /* End twa_empty_response_queue_large() */
959 /* This function passes sense keys from firmware to scsi layer */
960 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
962 TW_Command_Full *full_command_packet;
963 unsigned short error;
967 full_command_packet = tw_dev->command_packet_virt[request_id];
969 /* Check for embedded error string */
970 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
972 /* Don't print error for Logical unit not supported during rollcall */
973 error = full_command_packet->header.status_block.error;
974 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
976 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
977 tw_dev->host->host_no,
978 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
979 full_command_packet->header.status_block.error,
980 error_str[0] == '\0' ?
981 twa_string_lookup(twa_error_table,
982 full_command_packet->header.status_block.error) : error_str,
983 full_command_packet->header.err_specific_desc);
985 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
986 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
987 full_command_packet->header.status_block.error,
988 error_str[0] == '\0' ?
989 twa_string_lookup(twa_error_table,
990 full_command_packet->header.status_block.error) : error_str,
991 full_command_packet->header.err_specific_desc);
995 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
996 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
997 retval = TW_ISR_DONT_RESULT;
1003 } /* End twa_fill_sense() */
1005 /* This function will free up device extension resources */
1006 static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1008 if (tw_dev->command_packet_virt[0])
1009 pci_free_consistent(tw_dev->tw_pci_dev,
1010 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1011 tw_dev->command_packet_virt[0],
1012 tw_dev->command_packet_phys[0]);
1014 if (tw_dev->generic_buffer_virt[0])
1015 pci_free_consistent(tw_dev->tw_pci_dev,
1016 TW_SECTOR_SIZE*TW_Q_LENGTH,
1017 tw_dev->generic_buffer_virt[0],
1018 tw_dev->generic_buffer_phys[0]);
1020 if (tw_dev->event_queue[0])
1021 kfree(tw_dev->event_queue[0]);
1022 } /* End twa_free_device_extension() */
1024 /* This function will free a request id */
1025 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1027 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1028 tw_dev->state[request_id] = TW_S_FINISHED;
1029 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1030 } /* End twa_free_request_id() */
1032 /* This function will get parameter table entires from the firmware */
1033 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1035 TW_Command_Full *full_command_packet;
1036 TW_Command *command_packet;
1037 TW_Param_Apache *param;
1038 unsigned long param_value;
1039 void *retval = NULL;
1041 /* Setup the command packet */
1042 full_command_packet = tw_dev->command_packet_virt[request_id];
1043 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1044 command_packet = &full_command_packet->command.oldcommand;
1046 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1047 command_packet->size = TW_COMMAND_SIZE;
1048 command_packet->request_id = request_id;
1049 command_packet->byte6_offset.block_count = 1;
1051 /* Now setup the param */
1052 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1053 memset(param, 0, TW_SECTOR_SIZE);
1054 param->table_id = table_id | 0x8000;
1055 param->parameter_id = parameter_id;
1056 param->parameter_size_bytes = parameter_size_bytes;
1057 param_value = tw_dev->generic_buffer_phys[request_id];
1059 command_packet->byte8_offset.param.sgl[0].address = param_value;
1060 command_packet->byte8_offset.param.sgl[0].length = TW_SECTOR_SIZE;
1062 /* Post the command packet to the board */
1063 twa_post_command_packet(tw_dev, request_id, 1);
1065 /* Poll for completion */
1066 if (twa_poll_response(tw_dev, request_id, 30))
1067 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1069 retval = (void *)&(param->data[0]);
1071 tw_dev->posted_request_count--;
1072 tw_dev->state[request_id] = TW_S_INITIAL;
1075 } /* End twa_get_param() */
1077 /* This function will assign an available request id */
1078 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1080 *request_id = tw_dev->free_queue[tw_dev->free_head];
1081 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1082 tw_dev->state[*request_id] = TW_S_STARTED;
1083 } /* End twa_get_request_id() */
1085 /* This function will send an initconnection command to controller */
1086 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1087 u32 set_features, unsigned short current_fw_srl,
1088 unsigned short current_fw_arch_id,
1089 unsigned short current_fw_branch,
1090 unsigned short current_fw_build,
1091 unsigned short *fw_on_ctlr_srl,
1092 unsigned short *fw_on_ctlr_arch_id,
1093 unsigned short *fw_on_ctlr_branch,
1094 unsigned short *fw_on_ctlr_build,
1095 u32 *init_connect_result)
1097 TW_Command_Full *full_command_packet;
1098 TW_Initconnect *tw_initconnect;
1099 int request_id = 0, retval = 1;
1101 /* Initialize InitConnection command packet */
1102 full_command_packet = tw_dev->command_packet_virt[request_id];
1103 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1104 full_command_packet->header.header_desc.size_header = 128;
1106 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1107 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1108 tw_initconnect->request_id = request_id;
1109 tw_initconnect->message_credits = message_credits;
1110 tw_initconnect->features = set_features;
1112 /* Turn on 64-bit sgl support if we need to */
1113 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1115 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1116 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1117 tw_initconnect->fw_srl = current_fw_srl;
1118 tw_initconnect->fw_arch_id = current_fw_arch_id;
1119 tw_initconnect->fw_branch = current_fw_branch;
1120 tw_initconnect->fw_build = current_fw_build;
1122 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1124 /* Send command packet to the board */
1125 twa_post_command_packet(tw_dev, request_id, 1);
1127 /* Poll for completion */
1128 if (twa_poll_response(tw_dev, request_id, 30)) {
1129 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1131 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1132 *fw_on_ctlr_srl = tw_initconnect->fw_srl;
1133 *fw_on_ctlr_arch_id = tw_initconnect->fw_arch_id;
1134 *fw_on_ctlr_branch = tw_initconnect->fw_branch;
1135 *fw_on_ctlr_build = tw_initconnect->fw_build;
1136 *init_connect_result = tw_initconnect->result;
1141 tw_dev->posted_request_count--;
1142 tw_dev->state[request_id] = TW_S_INITIAL;
1145 } /* End twa_initconnection() */
1147 /* This function will initialize the fields of a device extension */
1148 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1152 /* Initialize command packet buffers */
1153 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1154 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1158 /* Initialize generic buffer */
1159 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1160 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1164 /* Allocate event info space */
1165 tw_dev->event_queue[0] = kmalloc(sizeof(TW_Event) * TW_Q_LENGTH, GFP_KERNEL);
1166 if (!tw_dev->event_queue[0]) {
1167 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1171 memset(tw_dev->event_queue[0], 0, sizeof(TW_Event) * TW_Q_LENGTH);
1173 for (i = 0; i < TW_Q_LENGTH; i++) {
1174 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1175 tw_dev->free_queue[i] = i;
1176 tw_dev->state[i] = TW_S_INITIAL;
1179 tw_dev->pending_head = TW_Q_START;
1180 tw_dev->pending_tail = TW_Q_START;
1181 tw_dev->free_head = TW_Q_START;
1182 tw_dev->free_tail = TW_Q_START;
1183 tw_dev->error_sequence_id = 1;
1184 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1186 init_MUTEX(&tw_dev->ioctl_sem);
1187 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1192 } /* End twa_initialize_device_extension() */
1194 /* This function is the interrupt service routine */
1195 static irqreturn_t twa_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1197 int request_id, error = 0;
1198 u32 status_reg_value;
1199 TW_Response_Queue response_que;
1200 TW_Command_Full *full_command_packet;
1201 TW_Command *command_packet;
1202 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1205 /* Get the per adapter lock */
1206 spin_lock(tw_dev->host->host_lock);
1208 /* Read the registers */
1209 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1211 /* Check if this is our interrupt, otherwise bail */
1212 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1213 goto twa_interrupt_bail;
1217 /* Check controller for errors */
1218 if (twa_check_bits(status_reg_value)) {
1219 if (twa_decode_bits(tw_dev, status_reg_value)) {
1220 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1221 goto twa_interrupt_bail;
1225 /* Handle host interrupt */
1226 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1227 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1229 /* Handle attention interrupt */
1230 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1231 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1232 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1233 twa_get_request_id(tw_dev, &request_id);
1235 error = twa_aen_read_queue(tw_dev, request_id);
1237 tw_dev->state[request_id] = TW_S_COMPLETED;
1238 twa_free_request_id(tw_dev, request_id);
1239 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1244 /* Handle command interrupt */
1245 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1246 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1247 /* Drain as many pending commands as we can */
1248 while (tw_dev->pending_request_count > 0) {
1249 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1250 if (tw_dev->state[request_id] != TW_S_PENDING) {
1251 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1252 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1253 goto twa_interrupt_bail;
1255 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1256 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1257 tw_dev->pending_request_count--;
1259 /* If we get here, we will continue re-posting on the next command interrupt */
1265 /* Handle response interrupt */
1266 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1268 /* Drain the response queue from the board */
1269 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1270 /* Complete the response */
1271 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1272 request_id = TW_RESID_OUT(response_que.response_id);
1273 full_command_packet = tw_dev->command_packet_virt[request_id];
1275 command_packet = &full_command_packet->command.oldcommand;
1276 /* Check for command packet errors */
1277 if (full_command_packet->command.newcommand.status != 0) {
1278 if (tw_dev->srb[request_id] != 0) {
1279 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1281 /* Skip ioctl error prints */
1282 if (request_id != tw_dev->chrdev_request_id) {
1283 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1288 /* Check for correct state */
1289 if (tw_dev->state[request_id] != TW_S_POSTED) {
1290 if (tw_dev->srb[request_id] != 0) {
1291 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1292 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1293 goto twa_interrupt_bail;
1297 /* Check for internal command completion */
1298 if (tw_dev->srb[request_id] == 0) {
1299 if (request_id != tw_dev->chrdev_request_id) {
1300 if (twa_aen_complete(tw_dev, request_id))
1301 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1303 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1304 wake_up(&tw_dev->ioctl_wqueue);
1307 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1308 /* If no error command was a success */
1310 tw_dev->srb[request_id]->result = (DID_OK << 16);
1313 /* If error, command failed */
1315 /* Ask for a host reset */
1316 tw_dev->srb[request_id]->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1319 /* Report residual bytes for single sgl */
1320 if ((tw_dev->srb[request_id]->use_sg <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1321 if (full_command_packet->command.newcommand.sg_list[0].length < tw_dev->srb[request_id]->request_bufflen)
1322 tw_dev->srb[request_id]->resid = tw_dev->srb[request_id]->request_bufflen - full_command_packet->command.newcommand.sg_list[0].length;
1325 /* Now complete the io */
1326 tw_dev->state[request_id] = TW_S_COMPLETED;
1327 twa_free_request_id(tw_dev, request_id);
1328 tw_dev->posted_request_count--;
1329 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1330 twa_unmap_scsi_data(tw_dev, request_id);
1333 /* Check for valid status after each drain */
1334 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1335 if (twa_check_bits(status_reg_value)) {
1336 if (twa_decode_bits(tw_dev, status_reg_value)) {
1337 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1338 goto twa_interrupt_bail;
1345 spin_unlock(tw_dev->host->host_lock);
1346 return IRQ_RETVAL(handled);
1347 } /* End twa_interrupt() */
1349 /* This function will load the request id and various sgls for ioctls */
1350 static void twa_load_sgl(TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
1352 TW_Command *oldcommand;
1353 TW_Command_Apache *newcommand;
1356 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1357 newcommand = &full_command_packet->command.newcommand;
1358 newcommand->request_id__lunl =
1359 TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id);
1360 newcommand->sg_list[0].address = dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1;
1361 newcommand->sg_list[0].length = length;
1362 newcommand->sgl_entries__lunh =
1363 TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), 1);
1365 oldcommand = &full_command_packet->command.oldcommand;
1366 oldcommand->request_id = request_id;
1368 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1369 /* Load the sg list */
1370 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1371 sgl->address = dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1;
1372 sgl->length = length;
1374 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1375 oldcommand->size += 1;
1378 } /* End twa_load_sgl() */
1380 /* This function will perform a pci-dma mapping for a scatter gather list */
1381 static int twa_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
1384 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1385 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1388 if (cmd->use_sg == 0)
1391 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, DMA_BIDIRECTIONAL);
1394 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to map scatter gather list");
1398 cmd->SCp.phase = TW_PHASE_SGLIST;
1399 cmd->SCp.have_data_in = use_sg;
1403 } /* End twa_map_scsi_sg_data() */
1405 /* This function will perform a pci-dma map for a single buffer */
1406 static dma_addr_t twa_map_scsi_single_data(TW_Device_Extension *tw_dev, int request_id)
1409 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1410 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1413 if (cmd->request_bufflen == 0) {
1418 mapping = pci_map_single(pdev, cmd->request_buffer, cmd->request_bufflen, DMA_BIDIRECTIONAL);
1421 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1d, "Failed to map page");
1425 cmd->SCp.phase = TW_PHASE_SINGLE;
1426 cmd->SCp.have_data_in = mapping;
1430 } /* End twa_map_scsi_single_data() */
1432 /* This function will poll for a response interrupt of a request */
1433 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1435 int retval = 1, found = 0, response_request_id;
1436 TW_Response_Queue response_queue;
1437 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1439 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1440 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1441 response_request_id = TW_RESID_OUT(response_queue.response_id);
1442 if (request_id != response_request_id) {
1443 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1446 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1447 if (full_command_packet->command.newcommand.status != 0) {
1449 twa_fill_sense(tw_dev, request_id, 0, 0);
1454 if (full_command_packet->command.oldcommand.status != 0) {
1456 twa_fill_sense(tw_dev, request_id, 0, 0);
1467 } /* End twa_poll_response() */
1469 /* This function will poll the status register for a flag */
1470 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1472 u32 status_reg_value;
1473 unsigned long before;
1476 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1479 if (twa_check_bits(status_reg_value))
1480 twa_decode_bits(tw_dev, status_reg_value);
1482 while ((status_reg_value & flag) != flag) {
1483 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1485 if (twa_check_bits(status_reg_value))
1486 twa_decode_bits(tw_dev, status_reg_value);
1488 if (time_after(jiffies, before + HZ * seconds))
1496 } /* End twa_poll_status() */
1498 /* This function will poll the status register for disappearance of a flag */
1499 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1501 u32 status_reg_value;
1502 unsigned long before;
1505 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1508 if (twa_check_bits(status_reg_value))
1509 twa_decode_bits(tw_dev, status_reg_value);
1511 while ((status_reg_value & flag) != 0) {
1512 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1513 if (twa_check_bits(status_reg_value))
1514 twa_decode_bits(tw_dev, status_reg_value);
1516 if (time_after(jiffies, before + HZ * seconds))
1524 } /* End twa_poll_status_gone() */
1526 /* This function will attempt to post a command packet to the board */
1527 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1529 u32 status_reg_value;
1530 dma_addr_t command_que_value;
1533 command_que_value = tw_dev->command_packet_phys[request_id];
1534 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1536 if (twa_check_bits(status_reg_value))
1537 twa_decode_bits(tw_dev, status_reg_value);
1539 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1541 /* Only pend internal driver commands */
1543 retval = SCSI_MLQUEUE_HOST_BUSY;
1547 /* Couldn't post the command packet, so we do it later */
1548 if (tw_dev->state[request_id] != TW_S_PENDING) {
1549 tw_dev->state[request_id] = TW_S_PENDING;
1550 tw_dev->pending_request_count++;
1551 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1552 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1554 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1555 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1557 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1560 /* We successfully posted the command packet */
1561 if (sizeof(dma_addr_t) > 4) {
1562 command_que_value += TW_COMMAND_OFFSET;
1563 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1564 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1566 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1568 tw_dev->state[request_id] = TW_S_POSTED;
1569 tw_dev->posted_request_count++;
1570 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1571 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1577 } /* End twa_post_command_packet() */
1579 /* This function will reset a device extension */
1580 static int twa_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset)
1584 unsigned long flags = 0;
1586 set_bit(TW_IN_RESET, &tw_dev->flags);
1587 TW_DISABLE_INTERRUPTS(tw_dev);
1588 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1589 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1591 /* Abort all requests that are in progress */
1592 for (i = 0; i < TW_Q_LENGTH; i++) {
1593 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1594 (tw_dev->state[i] != TW_S_INITIAL) &&
1595 (tw_dev->state[i] != TW_S_COMPLETED)) {
1596 if (tw_dev->srb[i]) {
1597 tw_dev->srb[i]->result = (DID_RESET << 16);
1598 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1599 twa_unmap_scsi_data(tw_dev, i);
1604 /* Reset queues and counts */
1605 for (i = 0; i < TW_Q_LENGTH; i++) {
1606 tw_dev->free_queue[i] = i;
1607 tw_dev->state[i] = TW_S_INITIAL;
1609 tw_dev->free_head = TW_Q_START;
1610 tw_dev->free_tail = TW_Q_START;
1611 tw_dev->posted_request_count = 0;
1612 tw_dev->pending_request_count = 0;
1613 tw_dev->pending_head = TW_Q_START;
1614 tw_dev->pending_tail = TW_Q_START;
1615 tw_dev->reset_print = 0;
1617 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1619 if (twa_reset_sequence(tw_dev, 1))
1622 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1624 /* Wake up any ioctl that was pending before the reset */
1625 if ((tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE) || (ioctl_reset)) {
1626 clear_bit(TW_IN_RESET, &tw_dev->flags);
1628 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1629 wake_up(&tw_dev->ioctl_wqueue);
1634 } /* End twa_reset_device_extension() */
1636 /* This function will reset a controller */
1637 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1639 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1641 while (tries < TW_MAX_RESET_TRIES) {
1642 if (do_soft_reset) {
1643 TW_SOFT_RESET(tw_dev);
1644 /* Clear pchip/response queue on 9550SX */
1645 if (twa_empty_response_queue_large(tw_dev)) {
1646 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1653 /* Make sure controller is in a good state */
1654 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1655 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1661 /* Empty response queue */
1662 if (twa_empty_response_queue(tw_dev)) {
1663 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1671 /* Check for compatibility/flash */
1672 if (twa_check_srl(tw_dev, &flashed)) {
1673 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1684 /* Drain the AEN queue */
1685 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1686 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1692 /* If we got here, controller is in a good state */
1698 } /* End twa_reset_sequence() */
1700 /* This funciton returns unit geometry in cylinders/heads/sectors */
1701 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1703 int heads, sectors, cylinders;
1704 TW_Device_Extension *tw_dev;
1706 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1708 if (capacity >= 0x200000) {
1711 cylinders = sector_div(capacity, heads * sectors);
1715 cylinders = sector_div(capacity, heads * sectors);
1720 geom[2] = cylinders;
1723 } /* End twa_scsi_biosparam() */
1725 /* This is the new scsi eh reset function */
1726 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1728 TW_Device_Extension *tw_dev = NULL;
1729 int retval = FAILED;
1731 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1733 tw_dev->num_resets++;
1735 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Unit #%d: Command (0x%x) timed out, resetting card.\n", tw_dev->host->host_no, TW_DRIVER, 0x2c, SCpnt->device->id, SCpnt->cmnd[0]);
1737 /* Now reset the card and some of the device extension data */
1738 if (twa_reset_device_extension(tw_dev, 0)) {
1739 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1746 } /* End twa_scsi_eh_reset() */
1748 /* This is the main scsi queue function to handle scsi opcodes */
1749 static int twa_scsi_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1751 int request_id, retval;
1752 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1754 /* Check if this FW supports luns */
1755 if ((SCpnt->device->lun != 0) && (tw_dev->working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1756 SCpnt->result = (DID_BAD_TARGET << 16);
1762 /* Save done function into scsi_cmnd struct */
1763 SCpnt->scsi_done = done;
1765 /* Get a free request id */
1766 twa_get_request_id(tw_dev, &request_id);
1768 /* Save the scsi command for use by the ISR */
1769 tw_dev->srb[request_id] = SCpnt;
1771 /* Initialize phase to zero */
1772 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1774 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1776 case SCSI_MLQUEUE_HOST_BUSY:
1777 twa_free_request_id(tw_dev, request_id);
1780 tw_dev->state[request_id] = TW_S_COMPLETED;
1781 twa_free_request_id(tw_dev, request_id);
1782 SCpnt->result = (DID_ERROR << 16);
1788 } /* End twa_scsi_queue() */
1790 /* This function hands scsi cdb's to the firmware */
1791 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1793 TW_Command_Full *full_command_packet;
1794 TW_Command_Apache *command_packet;
1795 u32 num_sectors = 0x0;
1797 struct scsi_cmnd *srb = NULL;
1798 struct scatterlist *sglist = NULL;
1802 if (tw_dev->srb[request_id]) {
1803 if (tw_dev->srb[request_id]->request_buffer) {
1804 sglist = (struct scatterlist *)tw_dev->srb[request_id]->request_buffer;
1806 srb = tw_dev->srb[request_id];
1809 /* Initialize command packet */
1810 full_command_packet = tw_dev->command_packet_virt[request_id];
1811 full_command_packet->header.header_desc.size_header = 128;
1812 full_command_packet->header.status_block.error = 0;
1813 full_command_packet->header.status_block.severity__reserved = 0;
1815 command_packet = &full_command_packet->command.newcommand;
1816 command_packet->status = 0;
1817 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1819 /* We forced 16 byte cdb use earlier */
1821 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1823 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1826 command_packet->unit = srb->device->id;
1827 command_packet->request_id__lunl =
1828 TW_REQ_LUN_IN(srb->device->lun, request_id);
1830 command_packet->request_id__lunl =
1831 TW_REQ_LUN_IN(0, request_id);
1832 command_packet->unit = 0;
1835 command_packet->sgl_offset = 16;
1838 /* Map sglist from scsi layer to cmd packet */
1839 if (tw_dev->srb[request_id]->use_sg == 0) {
1840 if (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH) {
1841 command_packet->sg_list[0].address = tw_dev->generic_buffer_phys[request_id];
1842 command_packet->sg_list[0].length = TW_MIN_SGL_LENGTH;
1843 if (tw_dev->srb[request_id]->sc_data_direction == DMA_TO_DEVICE || tw_dev->srb[request_id]->sc_data_direction == DMA_BIDIRECTIONAL)
1844 memcpy(tw_dev->generic_buffer_virt[request_id], tw_dev->srb[request_id]->request_buffer, tw_dev->srb[request_id]->request_bufflen);
1846 buffaddr = twa_map_scsi_single_data(tw_dev, request_id);
1850 command_packet->sg_list[0].address = buffaddr;
1851 command_packet->sg_list[0].length = tw_dev->srb[request_id]->request_bufflen;
1853 command_packet->sgl_entries__lunh = TW_REQ_LUN_IN((srb->device->lun >> 4), 1);
1855 if (command_packet->sg_list[0].address & TW_ALIGNMENT_9000_SGL) {
1856 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2d, "Found unaligned address during execute scsi");
1861 if (tw_dev->srb[request_id]->use_sg > 0) {
1862 if ((tw_dev->srb[request_id]->use_sg == 1) && (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH)) {
1863 if (tw_dev->srb[request_id]->sc_data_direction == DMA_TO_DEVICE || tw_dev->srb[request_id]->sc_data_direction == DMA_BIDIRECTIONAL) {
1864 struct scatterlist *sg = (struct scatterlist *)tw_dev->srb[request_id]->request_buffer;
1865 char *buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
1866 memcpy(tw_dev->generic_buffer_virt[request_id], buf, sg->length);
1867 kunmap_atomic(buf - sg->offset, KM_IRQ0);
1869 command_packet->sg_list[0].address = tw_dev->generic_buffer_phys[request_id];
1870 command_packet->sg_list[0].length = TW_MIN_SGL_LENGTH;
1872 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1876 for (i = 0; i < sg_count; i++) {
1877 command_packet->sg_list[i].address = sg_dma_address(&sglist[i]);
1878 command_packet->sg_list[i].length = sg_dma_len(&sglist[i]);
1879 if (command_packet->sg_list[i].address & TW_ALIGNMENT_9000_SGL) {
1880 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1885 command_packet->sgl_entries__lunh = TW_REQ_LUN_IN((srb->device->lun >> 4), tw_dev->srb[request_id]->use_sg);
1888 /* Internal cdb post */
1889 for (i = 0; i < use_sg; i++) {
1890 command_packet->sg_list[i].address = sglistarg[i].address;
1891 command_packet->sg_list[i].length = sglistarg[i].length;
1892 if (command_packet->sg_list[i].address & TW_ALIGNMENT_9000_SGL) {
1893 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1897 command_packet->sgl_entries__lunh = TW_REQ_LUN_IN(0, use_sg);
1901 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1902 num_sectors = (u32)srb->cmnd[4];
1904 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1905 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1908 /* Update sector statistic */
1909 tw_dev->sector_count = num_sectors;
1910 if (tw_dev->sector_count > tw_dev->max_sector_count)
1911 tw_dev->max_sector_count = tw_dev->sector_count;
1913 /* Update SG statistics */
1915 tw_dev->sgl_entries = tw_dev->srb[request_id]->use_sg;
1916 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1917 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1920 /* Now post the command to the board */
1922 retval = twa_post_command_packet(tw_dev, request_id, 0);
1924 twa_post_command_packet(tw_dev, request_id, 1);
1929 } /* End twa_scsiop_execute_scsi() */
1931 /* This function completes an execute scsi operation */
1932 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1934 if (tw_dev->srb[request_id]->request_bufflen < TW_MIN_SGL_LENGTH &&
1935 (tw_dev->srb[request_id]->sc_data_direction == DMA_FROM_DEVICE ||
1936 tw_dev->srb[request_id]->sc_data_direction == DMA_BIDIRECTIONAL)) {
1937 if (tw_dev->srb[request_id]->use_sg == 0) {
1938 memcpy(tw_dev->srb[request_id]->request_buffer,
1939 tw_dev->generic_buffer_virt[request_id],
1940 tw_dev->srb[request_id]->request_bufflen);
1942 if (tw_dev->srb[request_id]->use_sg == 1) {
1943 struct scatterlist *sg = (struct scatterlist *)tw_dev->srb[request_id]->request_buffer;
1944 char *buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
1945 memcpy(buf, tw_dev->generic_buffer_virt[request_id], sg->length);
1946 kunmap_atomic(buf - sg->offset, KM_IRQ0);
1949 } /* End twa_scsiop_execute_scsi_complete() */
1951 /* This function tells the controller to shut down */
1952 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1954 /* Disable interrupts */
1955 TW_DISABLE_INTERRUPTS(tw_dev);
1957 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1959 /* Tell the card we are shutting down */
1960 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1961 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1963 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1966 /* Clear all interrupts just before exit */
1967 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1968 } /* End __twa_shutdown() */
1970 /* Wrapper for __twa_shutdown */
1971 static void twa_shutdown(struct pci_dev *pdev)
1973 struct Scsi_Host *host = pci_get_drvdata(pdev);
1974 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1976 __twa_shutdown(tw_dev);
1977 } /* End twa_shutdown() */
1979 /* This function will look up a string */
1980 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1984 for (index = 0; ((code != table[index].code) &&
1985 (table[index].text != (char *)0)); index++);
1986 return(table[index].text);
1987 } /* End twa_string_lookup() */
1989 /* This function will perform a pci-dma unmap */
1990 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1992 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1993 struct pci_dev *pdev = tw_dev->tw_pci_dev;
1995 switch(cmd->SCp.phase) {
1996 case TW_PHASE_SINGLE:
1997 pci_unmap_single(pdev, cmd->SCp.have_data_in, cmd->request_bufflen, DMA_BIDIRECTIONAL);
1999 case TW_PHASE_SGLIST:
2000 pci_unmap_sg(pdev, cmd->request_buffer, cmd->use_sg, DMA_BIDIRECTIONAL);
2003 } /* End twa_unmap_scsi_data() */
2005 /* scsi_host_template initializer */
2006 static struct scsi_host_template driver_template = {
2007 .module = THIS_MODULE,
2008 .name = "3ware 9000 Storage Controller",
2009 .queuecommand = twa_scsi_queue,
2010 .eh_host_reset_handler = twa_scsi_eh_reset,
2011 .bios_param = twa_scsi_biosparam,
2012 .change_queue_depth = twa_change_queue_depth,
2013 .can_queue = TW_Q_LENGTH-2,
2015 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
2016 .max_sectors = TW_MAX_SECTORS,
2017 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
2018 .use_clustering = ENABLE_CLUSTERING,
2019 .shost_attrs = twa_host_attrs,
2023 /* This function will probe and initialize a card */
2024 static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2026 struct Scsi_Host *host = NULL;
2027 TW_Device_Extension *tw_dev;
2029 int retval = -ENODEV;
2031 retval = pci_enable_device(pdev);
2033 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2034 goto out_disable_device;
2037 pci_set_master(pdev);
2039 retval = pci_set_dma_mask(pdev, sizeof(dma_addr_t) > 4 ? DMA_64BIT_MASK : DMA_32BIT_MASK);
2041 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2042 goto out_disable_device;
2045 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2047 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2049 goto out_disable_device;
2051 tw_dev = (TW_Device_Extension *)host->hostdata;
2053 memset(tw_dev, 0, sizeof(TW_Device_Extension));
2055 /* Save values to device extension */
2056 tw_dev->host = host;
2057 tw_dev->tw_pci_dev = pdev;
2059 if (twa_initialize_device_extension(tw_dev)) {
2060 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2061 goto out_free_device_extension;
2064 /* Request IO regions */
2065 retval = pci_request_regions(pdev, "3w-9xxx");
2067 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2068 goto out_free_device_extension;
2071 if (pdev->device == PCI_DEVICE_ID_3WARE_9000)
2072 mem_addr = pci_resource_start(pdev, 1);
2074 mem_addr = pci_resource_start(pdev, 2);
2076 /* Save base address */
2077 tw_dev->base_addr = ioremap(mem_addr, PAGE_SIZE);
2078 if (!tw_dev->base_addr) {
2079 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2080 goto out_release_mem_region;
2083 /* Disable interrupts on the card */
2084 TW_DISABLE_INTERRUPTS(tw_dev);
2086 /* Initialize the card */
2087 if (twa_reset_sequence(tw_dev, 0))
2088 goto out_release_mem_region;
2090 /* Set host specific parameters */
2091 host->max_id = TW_MAX_UNITS;
2092 host->max_cmd_len = TW_MAX_CDB_LEN;
2094 /* Channels aren't supported by adapter */
2095 host->max_lun = TW_MAX_LUNS(tw_dev->working_srl);
2096 host->max_channel = 0;
2098 /* Register the card with the kernel SCSI layer */
2099 retval = scsi_add_host(host, &pdev->dev);
2101 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2102 goto out_release_mem_region;
2105 pci_set_drvdata(pdev, host);
2107 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%x, IRQ: %d.\n",
2108 host->host_no, mem_addr, pdev->irq);
2109 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2111 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2112 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2113 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2114 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2115 *(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2116 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH));
2118 /* Now setup the interrupt handler */
2119 retval = request_irq(pdev->irq, twa_interrupt, SA_SHIRQ, "3w-9xxx", tw_dev);
2121 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2122 goto out_remove_host;
2125 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2126 twa_device_extension_count++;
2128 /* Re-enable interrupts on the card */
2129 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2131 /* Finally, scan the host */
2132 scsi_scan_host(host);
2134 if (twa_major == -1) {
2135 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2136 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2141 scsi_remove_host(host);
2142 out_release_mem_region:
2143 pci_release_regions(pdev);
2144 out_free_device_extension:
2145 twa_free_device_extension(tw_dev);
2146 scsi_host_put(host);
2148 pci_disable_device(pdev);
2151 } /* End twa_probe() */
2153 /* This function is called to remove a device */
2154 static void twa_remove(struct pci_dev *pdev)
2156 struct Scsi_Host *host = pci_get_drvdata(pdev);
2157 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2159 scsi_remove_host(tw_dev->host);
2161 /* Unregister character device */
2162 if (twa_major >= 0) {
2163 unregister_chrdev(twa_major, "twa");
2167 /* Free up the IRQ */
2168 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2170 /* Shutdown the card */
2171 __twa_shutdown(tw_dev);
2173 /* Free up the mem region */
2174 pci_release_regions(pdev);
2176 /* Free up device extension resources */
2177 twa_free_device_extension(tw_dev);
2179 scsi_host_put(tw_dev->host);
2180 pci_disable_device(pdev);
2181 twa_device_extension_count--;
2182 } /* End twa_remove() */
2184 /* PCI Devices supported by this driver */
2185 static struct pci_device_id twa_pci_tbl[] __devinitdata = {
2186 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2187 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2188 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2189 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2192 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2194 /* pci_driver initializer */
2195 static struct pci_driver twa_driver = {
2197 .id_table = twa_pci_tbl,
2199 .remove = twa_remove,
2200 .shutdown = twa_shutdown
2203 /* This function is called on driver initialization */
2204 static int __init twa_init(void)
2206 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2208 return pci_module_init(&twa_driver);
2209 } /* End twa_init() */
2211 /* This function is called on driver exit */
2212 static void __exit twa_exit(void)
2214 pci_unregister_driver(&twa_driver);
2215 } /* End twa_exit() */
2217 module_init(twa_init);
2218 module_exit(twa_exit);