2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
4 Written By: Adam Radford <linuxraid@amcc.com>
5 Modifications By: Tom Couch <linuxraid@amcc.com>
7 Copyright (C) 2004-2009 Applied Micro Circuits Corporation.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 solely responsible for determining the appropriateness of using and
24 distributing the Program and assumes all risks associated with its
25 exercise of rights under this Agreement, including but not limited to
26 the risks and costs of program errors, damage to or loss of data,
27 programs or equipment, and unavailability or interruption of operations.
29 DISCLAIMER OF LIABILITY
30 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 You should have received a copy of the GNU General Public License
39 along with this program; if not, write to the Free Software
40 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 Bugs/Comments/Suggestions should be mailed to:
45 For more information, goto:
48 Note: This version of the driver does not contain a bundled firmware
53 2.26.02.000 - Driver cleanup for kernel submission.
54 2.26.02.001 - Replace schedule_timeout() calls with msleep().
55 2.26.02.002 - Add support for PAE mode.
57 Fix twa_remove() to free irq handler/unregister_chrdev()
58 before shutting down card.
59 Change to new 'change_queue_depth' api.
60 Fix 'handled=1' ISR usage, remove bogus IRQ check.
61 Remove un-needed eh_abort handler.
62 Add support for embedded firmware error strings.
63 2.26.02.003 - Correctly handle single sgl's with use_sg=1.
64 2.26.02.004 - Add support for 9550SX controllers.
65 2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
66 2.26.02.006 - Fix 9550SX pchip reset timeout.
67 Add big endian support.
68 2.26.02.007 - Disable local interrupts during kmap/unmap_atomic().
69 2.26.02.008 - Free irq handler in __twa_shutdown().
71 Add support for 9650SE controllers.
72 2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails.
73 2.26.02.010 - Add support for 9690SA controllers.
74 2.26.02.011 - Increase max AENs drained to 256.
75 Add MSI support and "use_msi" module parameter.
76 Fix bug in twa_get_param() on 4GB+.
77 Use pci_resource_len() for ioremap().
78 2.26.02.012 - Add power management support.
81 #include <linux/module.h>
82 #include <linux/reboot.h>
83 #include <linux/spinlock.h>
84 #include <linux/interrupt.h>
85 #include <linux/moduleparam.h>
86 #include <linux/errno.h>
87 #include <linux/types.h>
88 #include <linux/delay.h>
89 #include <linux/pci.h>
90 #include <linux/time.h>
91 #include <linux/mutex.h>
92 #include <linux/smp_lock.h>
95 #include <asm/uaccess.h>
96 #include <scsi/scsi.h>
97 #include <scsi/scsi_host.h>
98 #include <scsi/scsi_tcq.h>
99 #include <scsi/scsi_cmnd.h>
103 #define TW_DRIVER_VERSION "2.26.02.012"
104 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
105 static unsigned int twa_device_extension_count;
106 static int twa_major = -1;
107 extern struct timezone sys_tz;
109 /* Module parameters */
110 MODULE_AUTHOR ("AMCC");
111 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
112 MODULE_LICENSE("GPL");
113 MODULE_VERSION(TW_DRIVER_VERSION);
115 static int use_msi = 0;
116 module_param(use_msi, int, S_IRUGO);
117 MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
119 /* Function prototypes */
120 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
121 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
122 static char *twa_aen_severity_lookup(unsigned char severity_code);
123 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
124 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
125 static int twa_chrdev_open(struct inode *inode, struct file *file);
126 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
127 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
128 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
129 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
130 u32 set_features, unsigned short current_fw_srl,
131 unsigned short current_fw_arch_id,
132 unsigned short current_fw_branch,
133 unsigned short current_fw_build,
134 unsigned short *fw_on_ctlr_srl,
135 unsigned short *fw_on_ctlr_arch_id,
136 unsigned short *fw_on_ctlr_branch,
137 unsigned short *fw_on_ctlr_build,
138 u32 *init_connect_result);
139 static void twa_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length);
140 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
141 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
142 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
143 static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
144 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
145 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg);
146 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
147 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
148 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id);
152 /* Show some statistics about the card */
153 static ssize_t twa_show_stats(struct device *dev,
154 struct device_attribute *attr, char *buf)
156 struct Scsi_Host *host = class_to_shost(dev);
157 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
158 unsigned long flags = 0;
161 spin_lock_irqsave(tw_dev->host->host_lock, flags);
162 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
163 "Current commands posted: %4d\n"
164 "Max commands posted: %4d\n"
165 "Current pending commands: %4d\n"
166 "Max pending commands: %4d\n"
167 "Last sgl length: %4d\n"
168 "Max sgl length: %4d\n"
169 "Last sector count: %4d\n"
170 "Max sector count: %4d\n"
171 "SCSI Host Resets: %4d\n"
174 tw_dev->posted_request_count,
175 tw_dev->max_posted_request_count,
176 tw_dev->pending_request_count,
177 tw_dev->max_pending_request_count,
179 tw_dev->max_sgl_entries,
180 tw_dev->sector_count,
181 tw_dev->max_sector_count,
184 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
186 } /* End twa_show_stats() */
188 /* This function will set a devices queue depth */
189 static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth)
191 if (queue_depth > TW_Q_LENGTH-2)
192 queue_depth = TW_Q_LENGTH-2;
193 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
195 } /* End twa_change_queue_depth() */
197 /* Create sysfs 'stats' entry */
198 static struct device_attribute twa_host_stats_attr = {
203 .show = twa_show_stats
206 /* Host attributes initializer */
207 static struct device_attribute *twa_host_attrs[] = {
208 &twa_host_stats_attr,
212 /* File operations struct for character device */
213 static const struct file_operations twa_fops = {
214 .owner = THIS_MODULE,
215 .ioctl = twa_chrdev_ioctl,
216 .open = twa_chrdev_open,
220 /* This function will complete an aen request from the isr */
221 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
223 TW_Command_Full *full_command_packet;
224 TW_Command *command_packet;
225 TW_Command_Apache_Header *header;
229 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
230 tw_dev->posted_request_count--;
231 aen = le16_to_cpu(header->status_block.error);
232 full_command_packet = tw_dev->command_packet_virt[request_id];
233 command_packet = &full_command_packet->command.oldcommand;
235 /* First check for internal completion of set param for time sync */
236 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
237 /* Keep reading the queue in case there are more aen's */
238 if (twa_aen_read_queue(tw_dev, request_id))
247 case TW_AEN_QUEUE_EMPTY:
248 /* Quit reading the queue if this is the last one */
250 case TW_AEN_SYNC_TIME_WITH_HOST:
251 twa_aen_sync_time(tw_dev, request_id);
255 twa_aen_queue_event(tw_dev, header);
257 /* If there are more aen's, keep reading the queue */
258 if (twa_aen_read_queue(tw_dev, request_id))
267 tw_dev->state[request_id] = TW_S_COMPLETED;
268 twa_free_request_id(tw_dev, request_id);
269 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
272 } /* End twa_aen_complete() */
274 /* This function will drain aen queue */
275 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
278 char cdb[TW_MAX_CDB_LEN];
279 TW_SG_Entry sglist[1];
280 int finished = 0, count = 0;
281 TW_Command_Full *full_command_packet;
282 TW_Command_Apache_Header *header;
284 int first_reset = 0, queue = 0, retval = 1;
291 full_command_packet = tw_dev->command_packet_virt[request_id];
292 memset(full_command_packet, 0, sizeof(TW_Command_Full));
295 memset(&cdb, 0, TW_MAX_CDB_LEN);
296 cdb[0] = REQUEST_SENSE; /* opcode */
297 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
299 /* Initialize sglist */
300 memset(&sglist, 0, sizeof(TW_SG_Entry));
301 sglist[0].length = TW_SECTOR_SIZE;
302 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
304 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
305 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
309 /* Mark internal command */
310 tw_dev->srb[request_id] = NULL;
313 /* Send command to the board */
314 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
315 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
319 /* Now poll for completion */
320 if (twa_poll_response(tw_dev, request_id, 30)) {
321 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
322 tw_dev->posted_request_count--;
326 tw_dev->posted_request_count--;
327 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
328 aen = le16_to_cpu(header->status_block.error);
333 case TW_AEN_QUEUE_EMPTY:
334 if (first_reset != 1)
339 case TW_AEN_SOFT_RESET:
340 if (first_reset == 0)
345 case TW_AEN_SYNC_TIME_WITH_HOST:
351 /* Now queue an event info */
353 twa_aen_queue_event(tw_dev, header);
354 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
356 if (count == TW_MAX_AEN_DRAIN)
361 tw_dev->state[request_id] = TW_S_INITIAL;
363 } /* End twa_aen_drain_queue() */
365 /* This function will queue an event */
366 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
377 /* Fill out event info */
378 event = tw_dev->event_queue[tw_dev->error_index];
380 /* Check for clobber */
383 sprintf(host, " scsi%d:", tw_dev->host->host_no);
384 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
385 tw_dev->aen_clobber = 1;
388 aen = le16_to_cpu(header->status_block.error);
389 memset(event, 0, sizeof(TW_Event));
391 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
392 do_gettimeofday(&time);
393 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
394 event->time_stamp_sec = local_time;
395 event->aen_code = aen;
396 event->retrieved = TW_AEN_NOT_RETRIEVED;
397 event->sequence_id = tw_dev->error_sequence_id;
398 tw_dev->error_sequence_id++;
400 /* Check for embedded error string */
401 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
403 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
404 event->parameter_len = strlen(header->err_specific_desc);
405 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
406 if (event->severity != TW_AEN_SEVERITY_DEBUG)
407 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
409 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
410 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
411 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
412 header->err_specific_desc);
416 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
417 tw_dev->event_queue_wrapped = 1;
418 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
419 } /* End twa_aen_queue_event() */
421 /* This function will read the aen queue from the isr */
422 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
424 char cdb[TW_MAX_CDB_LEN];
425 TW_SG_Entry sglist[1];
426 TW_Command_Full *full_command_packet;
429 full_command_packet = tw_dev->command_packet_virt[request_id];
430 memset(full_command_packet, 0, sizeof(TW_Command_Full));
433 memset(&cdb, 0, TW_MAX_CDB_LEN);
434 cdb[0] = REQUEST_SENSE; /* opcode */
435 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
437 /* Initialize sglist */
438 memset(&sglist, 0, sizeof(TW_SG_Entry));
439 sglist[0].length = TW_SECTOR_SIZE;
440 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
442 /* Mark internal command */
443 tw_dev->srb[request_id] = NULL;
445 /* Now post the command packet */
446 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
447 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
453 } /* End twa_aen_read_queue() */
455 /* This function will look up an AEN severity string */
456 static char *twa_aen_severity_lookup(unsigned char severity_code)
460 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
461 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
464 retval = twa_aen_severity_table[severity_code];
467 } /* End twa_aen_severity_lookup() */
469 /* This function will sync firmware time with the host time */
470 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
474 TW_Command_Full *full_command_packet;
475 TW_Command *command_packet;
476 TW_Param_Apache *param;
479 /* Fill out the command packet */
480 full_command_packet = tw_dev->command_packet_virt[request_id];
481 memset(full_command_packet, 0, sizeof(TW_Command_Full));
482 command_packet = &full_command_packet->command.oldcommand;
483 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
484 command_packet->request_id = request_id;
485 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
486 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
487 command_packet->size = TW_COMMAND_SIZE;
488 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
490 /* Setup the param */
491 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
492 memset(param, 0, TW_SECTOR_SIZE);
493 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
494 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
495 param->parameter_size_bytes = cpu_to_le16(4);
497 /* Convert system time in UTC to local time seconds since last
499 do_gettimeofday(&utc);
500 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
501 schedulertime = local_time - (3 * 86400);
502 schedulertime = cpu_to_le32(schedulertime % 604800);
504 memcpy(param->data, &schedulertime, sizeof(u32));
506 /* Mark internal command */
507 tw_dev->srb[request_id] = NULL;
509 /* Now post the command */
510 twa_post_command_packet(tw_dev, request_id, 1);
511 } /* End twa_aen_sync_time() */
513 /* This function will allocate memory and check if it is correctly aligned */
514 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
517 dma_addr_t dma_handle;
518 unsigned long *cpu_addr;
521 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
523 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
527 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
528 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
529 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
533 memset(cpu_addr, 0, size*TW_Q_LENGTH);
535 for (i = 0; i < TW_Q_LENGTH; i++) {
538 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
539 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
542 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
543 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
550 } /* End twa_allocate_memory() */
552 /* This function will check the status register for unexpected bits */
553 static int twa_check_bits(u32 status_reg_value)
557 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
559 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
565 } /* End twa_check_bits() */
567 /* This function will check the srl and decide if we are compatible */
568 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
571 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
572 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
573 u32 init_connect_result = 0;
575 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
576 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
577 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
578 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
579 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
580 &fw_on_ctlr_build, &init_connect_result)) {
581 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
585 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
586 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
587 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
589 /* Try base mode compatibility */
590 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
591 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
592 TW_EXTENDED_INIT_CONNECT,
593 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
594 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
595 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
596 &fw_on_ctlr_branch, &fw_on_ctlr_build,
597 &init_connect_result)) {
598 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
601 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
602 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
603 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
605 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
609 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
610 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
611 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
614 /* Load rest of compatibility struct */
615 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
616 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
617 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
618 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
619 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
620 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
621 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
622 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
623 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
624 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
629 } /* End twa_check_srl() */
631 /* This function handles ioctl for the character device */
632 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
635 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
636 dma_addr_t dma_handle;
638 unsigned int sequence_id = 0;
639 unsigned char event_index, start_index;
640 TW_Ioctl_Driver_Command driver_command;
641 TW_Ioctl_Buf_Apache *tw_ioctl;
643 TW_Command_Full *full_command_packet;
644 TW_Compatibility_Info *tw_compat_info;
646 struct timeval current_time;
648 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
649 int retval = TW_IOCTL_ERROR_OS_EFAULT;
650 void __user *argp = (void __user *)arg;
652 /* Only let one of these through at a time */
653 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
654 retval = TW_IOCTL_ERROR_OS_EINTR;
658 /* First copy down the driver command */
659 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
662 /* Check data buffer size */
663 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
664 retval = TW_IOCTL_ERROR_OS_EINVAL;
668 /* Hardware can only do multiple of 512 byte transfers */
669 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
671 /* Now allocate ioctl buf memory */
672 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);
674 retval = TW_IOCTL_ERROR_OS_ENOMEM;
678 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
680 /* Now copy down the entire ioctl */
681 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
684 /* See which ioctl we are doing */
686 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
687 spin_lock_irqsave(tw_dev->host->host_lock, flags);
688 twa_get_request_id(tw_dev, &request_id);
690 /* Flag internal command */
691 tw_dev->srb[request_id] = NULL;
693 /* Flag chrdev ioctl */
694 tw_dev->chrdev_request_id = request_id;
696 full_command_packet = &tw_ioctl->firmware_command;
698 /* Load request id and sglist for both command types */
699 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
701 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
703 /* Now post the command packet to the controller */
704 twa_post_command_packet(tw_dev, request_id, 1);
705 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
707 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
709 /* Now wait for command to complete */
710 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
712 /* We timed out, and didn't get an interrupt */
713 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
714 /* Now we need to reset the board */
715 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
716 tw_dev->host->host_no, TW_DRIVER, 0x37,
718 retval = TW_IOCTL_ERROR_OS_EIO;
719 twa_reset_device_extension(tw_dev);
723 /* Now copy in the command packet response */
724 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
726 /* Now complete the io */
727 spin_lock_irqsave(tw_dev->host->host_lock, flags);
728 tw_dev->posted_request_count--;
729 tw_dev->state[request_id] = TW_S_COMPLETED;
730 twa_free_request_id(tw_dev, request_id);
731 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
733 case TW_IOCTL_GET_COMPATIBILITY_INFO:
734 tw_ioctl->driver_command.status = 0;
735 /* Copy compatiblity struct into ioctl data buffer */
736 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
737 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
739 case TW_IOCTL_GET_LAST_EVENT:
740 if (tw_dev->event_queue_wrapped) {
741 if (tw_dev->aen_clobber) {
742 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
743 tw_dev->aen_clobber = 0;
745 tw_ioctl->driver_command.status = 0;
747 if (!tw_dev->error_index) {
748 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
751 tw_ioctl->driver_command.status = 0;
753 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
754 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
755 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
757 case TW_IOCTL_GET_FIRST_EVENT:
758 if (tw_dev->event_queue_wrapped) {
759 if (tw_dev->aen_clobber) {
760 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
761 tw_dev->aen_clobber = 0;
763 tw_ioctl->driver_command.status = 0;
764 event_index = tw_dev->error_index;
766 if (!tw_dev->error_index) {
767 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
770 tw_ioctl->driver_command.status = 0;
773 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
774 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
776 case TW_IOCTL_GET_NEXT_EVENT:
777 event = (TW_Event *)tw_ioctl->data_buffer;
778 sequence_id = event->sequence_id;
779 tw_ioctl->driver_command.status = 0;
781 if (tw_dev->event_queue_wrapped) {
782 if (tw_dev->aen_clobber) {
783 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
784 tw_dev->aen_clobber = 0;
786 start_index = tw_dev->error_index;
788 if (!tw_dev->error_index) {
789 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
794 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
796 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
797 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
798 tw_dev->aen_clobber = 1;
799 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
802 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
803 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
805 case TW_IOCTL_GET_PREVIOUS_EVENT:
806 event = (TW_Event *)tw_ioctl->data_buffer;
807 sequence_id = event->sequence_id;
808 tw_ioctl->driver_command.status = 0;
810 if (tw_dev->event_queue_wrapped) {
811 if (tw_dev->aen_clobber) {
812 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
813 tw_dev->aen_clobber = 0;
815 start_index = tw_dev->error_index;
817 if (!tw_dev->error_index) {
818 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
823 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
825 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
826 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
827 tw_dev->aen_clobber = 1;
828 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
831 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
832 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
834 case TW_IOCTL_GET_LOCK:
835 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
836 do_gettimeofday(¤t_time);
837 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
839 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
840 tw_dev->ioctl_sem_lock = 1;
841 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
842 tw_ioctl->driver_command.status = 0;
843 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
845 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
846 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
849 case TW_IOCTL_RELEASE_LOCK:
850 if (tw_dev->ioctl_sem_lock == 1) {
851 tw_dev->ioctl_sem_lock = 0;
852 tw_ioctl->driver_command.status = 0;
854 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
858 retval = TW_IOCTL_ERROR_OS_ENOTTY;
862 /* Now copy the entire response to userspace */
863 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
866 /* Now free ioctl buf memory */
867 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
869 mutex_unlock(&tw_dev->ioctl_lock);
872 } /* End twa_chrdev_ioctl() */
874 /* This function handles open for the character device */
875 /* NOTE that this function will race with remove. */
876 static int twa_chrdev_open(struct inode *inode, struct file *file)
878 unsigned int minor_number;
879 int retval = TW_IOCTL_ERROR_OS_ENODEV;
882 minor_number = iminor(inode);
883 if (minor_number >= twa_device_extension_count)
888 } /* End twa_chrdev_open() */
890 /* This function will print readable messages from status register errors */
891 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
895 /* Check for various error conditions and handle them appropriately */
896 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
897 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
898 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
901 if (status_reg_value & TW_STATUS_PCI_ABORT) {
902 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
903 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
904 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
907 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
908 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
909 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
910 (!test_bit(TW_IN_RESET, &tw_dev->flags)))
911 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
912 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
915 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
916 if (tw_dev->reset_print == 0) {
917 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
918 tw_dev->reset_print = 1;
925 } /* End twa_decode_bits() */
927 /* This function will empty the response queue */
928 static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
930 u32 status_reg_value, response_que_value;
931 int count = 0, retval = 1;
933 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
935 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
936 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
937 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
940 if (count == TW_MAX_RESPONSE_DRAIN)
946 } /* End twa_empty_response_queue() */
948 /* This function will clear the pchip/response queue on 9550SX */
949 static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
951 u32 response_que_value = 0;
952 unsigned long before;
955 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
957 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
958 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
960 if (time_after(jiffies, before + HZ * 30))
963 /* P-chip settle time */
970 } /* End twa_empty_response_queue_large() */
972 /* This function passes sense keys from firmware to scsi layer */
973 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
975 TW_Command_Full *full_command_packet;
976 unsigned short error;
980 full_command_packet = tw_dev->command_packet_virt[request_id];
982 /* Check for embedded error string */
983 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
985 /* Don't print error for Logical unit not supported during rollcall */
986 error = le16_to_cpu(full_command_packet->header.status_block.error);
987 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
989 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
990 tw_dev->host->host_no,
991 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
992 full_command_packet->header.status_block.error,
993 error_str[0] == '\0' ?
994 twa_string_lookup(twa_error_table,
995 full_command_packet->header.status_block.error) : error_str,
996 full_command_packet->header.err_specific_desc);
998 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
999 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1000 full_command_packet->header.status_block.error,
1001 error_str[0] == '\0' ?
1002 twa_string_lookup(twa_error_table,
1003 full_command_packet->header.status_block.error) : error_str,
1004 full_command_packet->header.err_specific_desc);
1008 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1009 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1010 retval = TW_ISR_DONT_RESULT;
1016 } /* End twa_fill_sense() */
1018 /* This function will free up device extension resources */
1019 static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1021 if (tw_dev->command_packet_virt[0])
1022 pci_free_consistent(tw_dev->tw_pci_dev,
1023 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1024 tw_dev->command_packet_virt[0],
1025 tw_dev->command_packet_phys[0]);
1027 if (tw_dev->generic_buffer_virt[0])
1028 pci_free_consistent(tw_dev->tw_pci_dev,
1029 TW_SECTOR_SIZE*TW_Q_LENGTH,
1030 tw_dev->generic_buffer_virt[0],
1031 tw_dev->generic_buffer_phys[0]);
1033 kfree(tw_dev->event_queue[0]);
1034 } /* End twa_free_device_extension() */
1036 /* This function will free a request id */
1037 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1039 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1040 tw_dev->state[request_id] = TW_S_FINISHED;
1041 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1042 } /* End twa_free_request_id() */
1044 /* This function will get parameter table entries from the firmware */
1045 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1047 TW_Command_Full *full_command_packet;
1048 TW_Command *command_packet;
1049 TW_Param_Apache *param;
1050 void *retval = NULL;
1052 /* Setup the command packet */
1053 full_command_packet = tw_dev->command_packet_virt[request_id];
1054 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1055 command_packet = &full_command_packet->command.oldcommand;
1057 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1058 command_packet->size = TW_COMMAND_SIZE;
1059 command_packet->request_id = request_id;
1060 command_packet->byte6_offset.block_count = cpu_to_le16(1);
1062 /* Now setup the param */
1063 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1064 memset(param, 0, TW_SECTOR_SIZE);
1065 param->table_id = cpu_to_le16(table_id | 0x8000);
1066 param->parameter_id = cpu_to_le16(parameter_id);
1067 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
1069 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1070 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1072 /* Post the command packet to the board */
1073 twa_post_command_packet(tw_dev, request_id, 1);
1075 /* Poll for completion */
1076 if (twa_poll_response(tw_dev, request_id, 30))
1077 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1079 retval = (void *)&(param->data[0]);
1081 tw_dev->posted_request_count--;
1082 tw_dev->state[request_id] = TW_S_INITIAL;
1085 } /* End twa_get_param() */
1087 /* This function will assign an available request id */
1088 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1090 *request_id = tw_dev->free_queue[tw_dev->free_head];
1091 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1092 tw_dev->state[*request_id] = TW_S_STARTED;
1093 } /* End twa_get_request_id() */
1095 /* This function will send an initconnection command to controller */
1096 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1097 u32 set_features, unsigned short current_fw_srl,
1098 unsigned short current_fw_arch_id,
1099 unsigned short current_fw_branch,
1100 unsigned short current_fw_build,
1101 unsigned short *fw_on_ctlr_srl,
1102 unsigned short *fw_on_ctlr_arch_id,
1103 unsigned short *fw_on_ctlr_branch,
1104 unsigned short *fw_on_ctlr_build,
1105 u32 *init_connect_result)
1107 TW_Command_Full *full_command_packet;
1108 TW_Initconnect *tw_initconnect;
1109 int request_id = 0, retval = 1;
1111 /* Initialize InitConnection command packet */
1112 full_command_packet = tw_dev->command_packet_virt[request_id];
1113 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1114 full_command_packet->header.header_desc.size_header = 128;
1116 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1117 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1118 tw_initconnect->request_id = request_id;
1119 tw_initconnect->message_credits = cpu_to_le16(message_credits);
1120 tw_initconnect->features = set_features;
1122 /* Turn on 64-bit sgl support if we need to */
1123 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1125 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1127 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1128 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1129 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1130 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1131 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1132 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1134 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1136 /* Send command packet to the board */
1137 twa_post_command_packet(tw_dev, request_id, 1);
1139 /* Poll for completion */
1140 if (twa_poll_response(tw_dev, request_id, 30)) {
1141 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1143 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1144 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1145 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1146 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1147 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1148 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1153 tw_dev->posted_request_count--;
1154 tw_dev->state[request_id] = TW_S_INITIAL;
1157 } /* End twa_initconnection() */
1159 /* This function will initialize the fields of a device extension */
1160 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1164 /* Initialize command packet buffers */
1165 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1166 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1170 /* Initialize generic buffer */
1171 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1172 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1176 /* Allocate event info space */
1177 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1178 if (!tw_dev->event_queue[0]) {
1179 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1184 for (i = 0; i < TW_Q_LENGTH; i++) {
1185 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1186 tw_dev->free_queue[i] = i;
1187 tw_dev->state[i] = TW_S_INITIAL;
1190 tw_dev->pending_head = TW_Q_START;
1191 tw_dev->pending_tail = TW_Q_START;
1192 tw_dev->free_head = TW_Q_START;
1193 tw_dev->free_tail = TW_Q_START;
1194 tw_dev->error_sequence_id = 1;
1195 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1197 mutex_init(&tw_dev->ioctl_lock);
1198 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1203 } /* End twa_initialize_device_extension() */
1205 /* This function is the interrupt service routine */
1206 static irqreturn_t twa_interrupt(int irq, void *dev_instance)
1208 int request_id, error = 0;
1209 u32 status_reg_value;
1210 TW_Response_Queue response_que;
1211 TW_Command_Full *full_command_packet;
1212 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1215 /* Get the per adapter lock */
1216 spin_lock(tw_dev->host->host_lock);
1218 /* Read the registers */
1219 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1221 /* Check if this is our interrupt, otherwise bail */
1222 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1223 goto twa_interrupt_bail;
1227 /* If we are resetting, bail */
1228 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1229 goto twa_interrupt_bail;
1231 /* Check controller for errors */
1232 if (twa_check_bits(status_reg_value)) {
1233 if (twa_decode_bits(tw_dev, status_reg_value)) {
1234 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1235 goto twa_interrupt_bail;
1239 /* Handle host interrupt */
1240 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1241 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1243 /* Handle attention interrupt */
1244 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1245 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1246 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1247 twa_get_request_id(tw_dev, &request_id);
1249 error = twa_aen_read_queue(tw_dev, request_id);
1251 tw_dev->state[request_id] = TW_S_COMPLETED;
1252 twa_free_request_id(tw_dev, request_id);
1253 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1258 /* Handle command interrupt */
1259 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1260 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1261 /* Drain as many pending commands as we can */
1262 while (tw_dev->pending_request_count > 0) {
1263 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1264 if (tw_dev->state[request_id] != TW_S_PENDING) {
1265 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1266 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1267 goto twa_interrupt_bail;
1269 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1270 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1271 tw_dev->pending_request_count--;
1273 /* If we get here, we will continue re-posting on the next command interrupt */
1279 /* Handle response interrupt */
1280 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1282 /* Drain the response queue from the board */
1283 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1284 /* Complete the response */
1285 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1286 request_id = TW_RESID_OUT(response_que.response_id);
1287 full_command_packet = tw_dev->command_packet_virt[request_id];
1289 /* Check for command packet errors */
1290 if (full_command_packet->command.newcommand.status != 0) {
1291 if (tw_dev->srb[request_id] != NULL) {
1292 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1294 /* Skip ioctl error prints */
1295 if (request_id != tw_dev->chrdev_request_id) {
1296 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1301 /* Check for correct state */
1302 if (tw_dev->state[request_id] != TW_S_POSTED) {
1303 if (tw_dev->srb[request_id] != NULL) {
1304 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1305 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1306 goto twa_interrupt_bail;
1310 /* Check for internal command completion */
1311 if (tw_dev->srb[request_id] == NULL) {
1312 if (request_id != tw_dev->chrdev_request_id) {
1313 if (twa_aen_complete(tw_dev, request_id))
1314 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1316 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1317 wake_up(&tw_dev->ioctl_wqueue);
1320 struct scsi_cmnd *cmd;
1322 cmd = tw_dev->srb[request_id];
1324 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1325 /* If no error command was a success */
1327 cmd->result = (DID_OK << 16);
1330 /* If error, command failed */
1332 /* Ask for a host reset */
1333 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1336 /* Report residual bytes for single sgl */
1337 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1338 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1339 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1342 /* Now complete the io */
1343 tw_dev->state[request_id] = TW_S_COMPLETED;
1344 twa_free_request_id(tw_dev, request_id);
1345 tw_dev->posted_request_count--;
1346 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1347 twa_unmap_scsi_data(tw_dev, request_id);
1350 /* Check for valid status after each drain */
1351 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1352 if (twa_check_bits(status_reg_value)) {
1353 if (twa_decode_bits(tw_dev, status_reg_value)) {
1354 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1355 goto twa_interrupt_bail;
1362 spin_unlock(tw_dev->host->host_lock);
1363 return IRQ_RETVAL(handled);
1364 } /* End twa_interrupt() */
1366 /* This function will load the request id and various sgls for ioctls */
1367 static void twa_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
1369 TW_Command *oldcommand;
1370 TW_Command_Apache *newcommand;
1372 unsigned int pae = 0;
1374 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1377 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1378 newcommand = &full_command_packet->command.newcommand;
1379 newcommand->request_id__lunl =
1380 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
1381 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1382 newcommand->sg_list[0].length = cpu_to_le32(length);
1383 newcommand->sgl_entries__lunh =
1384 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), 1));
1386 oldcommand = &full_command_packet->command.oldcommand;
1387 oldcommand->request_id = request_id;
1389 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1390 /* Load the sg list */
1391 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1392 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1394 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1395 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1396 sgl->length = cpu_to_le32(length);
1398 oldcommand->size += pae;
1401 } /* End twa_load_sgl() */
1403 /* This function will perform a pci-dma mapping for a scatter gather list */
1404 static int twa_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
1407 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1409 use_sg = scsi_dma_map(cmd);
1412 else if (use_sg < 0) {
1413 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to map scatter gather list");
1417 cmd->SCp.phase = TW_PHASE_SGLIST;
1418 cmd->SCp.have_data_in = use_sg;
1421 } /* End twa_map_scsi_sg_data() */
1423 /* This function will poll for a response interrupt of a request */
1424 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1426 int retval = 1, found = 0, response_request_id;
1427 TW_Response_Queue response_queue;
1428 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1430 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1431 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1432 response_request_id = TW_RESID_OUT(response_queue.response_id);
1433 if (request_id != response_request_id) {
1434 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1437 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1438 if (full_command_packet->command.newcommand.status != 0) {
1440 twa_fill_sense(tw_dev, request_id, 0, 0);
1445 if (full_command_packet->command.oldcommand.status != 0) {
1447 twa_fill_sense(tw_dev, request_id, 0, 0);
1458 } /* End twa_poll_response() */
1460 /* This function will poll the status register for a flag */
1461 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1463 u32 status_reg_value;
1464 unsigned long before;
1467 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1470 if (twa_check_bits(status_reg_value))
1471 twa_decode_bits(tw_dev, status_reg_value);
1473 while ((status_reg_value & flag) != flag) {
1474 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1476 if (twa_check_bits(status_reg_value))
1477 twa_decode_bits(tw_dev, status_reg_value);
1479 if (time_after(jiffies, before + HZ * seconds))
1487 } /* End twa_poll_status() */
1489 /* This function will poll the status register for disappearance of a flag */
1490 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1492 u32 status_reg_value;
1493 unsigned long before;
1496 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1499 if (twa_check_bits(status_reg_value))
1500 twa_decode_bits(tw_dev, status_reg_value);
1502 while ((status_reg_value & flag) != 0) {
1503 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1504 if (twa_check_bits(status_reg_value))
1505 twa_decode_bits(tw_dev, status_reg_value);
1507 if (time_after(jiffies, before + HZ * seconds))
1515 } /* End twa_poll_status_gone() */
1517 /* This function will attempt to post a command packet to the board */
1518 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1520 u32 status_reg_value;
1521 dma_addr_t command_que_value;
1524 command_que_value = tw_dev->command_packet_phys[request_id];
1526 /* For 9650SE write low 4 bytes first */
1527 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1528 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1529 command_que_value += TW_COMMAND_OFFSET;
1530 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1533 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1535 if (twa_check_bits(status_reg_value))
1536 twa_decode_bits(tw_dev, status_reg_value);
1538 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1540 /* Only pend internal driver commands */
1542 retval = SCSI_MLQUEUE_HOST_BUSY;
1546 /* Couldn't post the command packet, so we do it later */
1547 if (tw_dev->state[request_id] != TW_S_PENDING) {
1548 tw_dev->state[request_id] = TW_S_PENDING;
1549 tw_dev->pending_request_count++;
1550 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1551 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1553 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1554 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1556 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1559 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1560 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1561 /* Now write upper 4 bytes */
1562 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1564 if (sizeof(dma_addr_t) > 4) {
1565 command_que_value += TW_COMMAND_OFFSET;
1566 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1567 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1569 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1572 tw_dev->state[request_id] = TW_S_POSTED;
1573 tw_dev->posted_request_count++;
1574 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1575 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1581 } /* End twa_post_command_packet() */
1583 /* This function will reset a device extension */
1584 static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1588 unsigned long flags = 0;
1590 set_bit(TW_IN_RESET, &tw_dev->flags);
1591 TW_DISABLE_INTERRUPTS(tw_dev);
1592 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1593 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1595 /* Abort all requests that are in progress */
1596 for (i = 0; i < TW_Q_LENGTH; i++) {
1597 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1598 (tw_dev->state[i] != TW_S_INITIAL) &&
1599 (tw_dev->state[i] != TW_S_COMPLETED)) {
1600 if (tw_dev->srb[i]) {
1601 tw_dev->srb[i]->result = (DID_RESET << 16);
1602 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1603 twa_unmap_scsi_data(tw_dev, i);
1608 /* Reset queues and counts */
1609 for (i = 0; i < TW_Q_LENGTH; i++) {
1610 tw_dev->free_queue[i] = i;
1611 tw_dev->state[i] = TW_S_INITIAL;
1613 tw_dev->free_head = TW_Q_START;
1614 tw_dev->free_tail = TW_Q_START;
1615 tw_dev->posted_request_count = 0;
1616 tw_dev->pending_request_count = 0;
1617 tw_dev->pending_head = TW_Q_START;
1618 tw_dev->pending_tail = TW_Q_START;
1619 tw_dev->reset_print = 0;
1621 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1623 if (twa_reset_sequence(tw_dev, 1))
1626 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1627 clear_bit(TW_IN_RESET, &tw_dev->flags);
1628 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1633 } /* End twa_reset_device_extension() */
1635 /* This function will reset a controller */
1636 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1638 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1640 while (tries < TW_MAX_RESET_TRIES) {
1641 if (do_soft_reset) {
1642 TW_SOFT_RESET(tw_dev);
1643 /* Clear pchip/response queue on 9550SX */
1644 if (twa_empty_response_queue_large(tw_dev)) {
1645 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1652 /* Make sure controller is in a good state */
1653 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1654 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1660 /* Empty response queue */
1661 if (twa_empty_response_queue(tw_dev)) {
1662 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1670 /* Check for compatibility/flash */
1671 if (twa_check_srl(tw_dev, &flashed)) {
1672 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1683 /* Drain the AEN queue */
1684 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1685 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1691 /* If we got here, controller is in a good state */
1697 } /* End twa_reset_sequence() */
1699 /* This funciton returns unit geometry in cylinders/heads/sectors */
1700 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1702 int heads, sectors, cylinders;
1703 TW_Device_Extension *tw_dev;
1705 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1707 if (capacity >= 0x200000) {
1710 cylinders = sector_div(capacity, heads * sectors);
1714 cylinders = sector_div(capacity, heads * sectors);
1719 geom[2] = cylinders;
1722 } /* End twa_scsi_biosparam() */
1724 /* This is the new scsi eh reset function */
1725 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1727 TW_Device_Extension *tw_dev = NULL;
1728 int retval = FAILED;
1730 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1732 tw_dev->num_resets++;
1734 sdev_printk(KERN_WARNING, SCpnt->device,
1735 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1736 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1738 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1739 mutex_lock(&tw_dev->ioctl_lock);
1741 /* Now reset the card and some of the device extension data */
1742 if (twa_reset_device_extension(tw_dev)) {
1743 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1749 mutex_unlock(&tw_dev->ioctl_lock);
1751 } /* End twa_scsi_eh_reset() */
1753 /* This is the main scsi queue function to handle scsi opcodes */
1754 static int twa_scsi_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1756 int request_id, retval;
1757 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1759 /* If we are resetting due to timed out ioctl, report as busy */
1760 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1761 retval = SCSI_MLQUEUE_HOST_BUSY;
1765 /* Check if this FW supports luns */
1766 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1767 SCpnt->result = (DID_BAD_TARGET << 16);
1773 /* Save done function into scsi_cmnd struct */
1774 SCpnt->scsi_done = done;
1776 /* Get a free request id */
1777 twa_get_request_id(tw_dev, &request_id);
1779 /* Save the scsi command for use by the ISR */
1780 tw_dev->srb[request_id] = SCpnt;
1782 /* Initialize phase to zero */
1783 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1785 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1787 case SCSI_MLQUEUE_HOST_BUSY:
1788 twa_free_request_id(tw_dev, request_id);
1791 tw_dev->state[request_id] = TW_S_COMPLETED;
1792 twa_free_request_id(tw_dev, request_id);
1793 SCpnt->result = (DID_ERROR << 16);
1799 } /* End twa_scsi_queue() */
1801 /* This function hands scsi cdb's to the firmware */
1802 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1804 TW_Command_Full *full_command_packet;
1805 TW_Command_Apache *command_packet;
1806 u32 num_sectors = 0x0;
1808 struct scsi_cmnd *srb = NULL;
1809 struct scatterlist *sglist = NULL, *sg;
1812 if (tw_dev->srb[request_id]) {
1813 srb = tw_dev->srb[request_id];
1814 if (scsi_sglist(srb))
1815 sglist = scsi_sglist(srb);
1818 /* Initialize command packet */
1819 full_command_packet = tw_dev->command_packet_virt[request_id];
1820 full_command_packet->header.header_desc.size_header = 128;
1821 full_command_packet->header.status_block.error = 0;
1822 full_command_packet->header.status_block.severity__reserved = 0;
1824 command_packet = &full_command_packet->command.newcommand;
1825 command_packet->status = 0;
1826 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1828 /* We forced 16 byte cdb use earlier */
1830 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1832 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1835 command_packet->unit = srb->device->id;
1836 command_packet->request_id__lunl =
1837 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1839 command_packet->request_id__lunl =
1840 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1841 command_packet->unit = 0;
1844 command_packet->sgl_offset = 16;
1847 /* Map sglist from scsi layer to cmd packet */
1849 if (scsi_sg_count(srb)) {
1850 if ((scsi_sg_count(srb) == 1) &&
1851 (scsi_bufflen(srb) < TW_MIN_SGL_LENGTH)) {
1852 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1853 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1854 scsi_sg_copy_to_buffer(srb,
1855 tw_dev->generic_buffer_virt[request_id],
1857 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1858 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1860 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1864 scsi_for_each_sg(srb, sg, sg_count, i) {
1865 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1866 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
1867 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1868 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1873 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1876 /* Internal cdb post */
1877 for (i = 0; i < use_sg; i++) {
1878 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1879 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1880 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1881 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1885 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1889 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1890 num_sectors = (u32)srb->cmnd[4];
1892 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1893 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1896 /* Update sector statistic */
1897 tw_dev->sector_count = num_sectors;
1898 if (tw_dev->sector_count > tw_dev->max_sector_count)
1899 tw_dev->max_sector_count = tw_dev->sector_count;
1901 /* Update SG statistics */
1903 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1904 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1905 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1908 /* Now post the command to the board */
1910 retval = twa_post_command_packet(tw_dev, request_id, 0);
1912 twa_post_command_packet(tw_dev, request_id, 1);
1917 } /* End twa_scsiop_execute_scsi() */
1919 /* This function completes an execute scsi operation */
1920 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1922 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1924 if (scsi_bufflen(cmd) < TW_MIN_SGL_LENGTH &&
1925 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1926 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
1927 if (scsi_sg_count(cmd) == 1) {
1928 void *buf = tw_dev->generic_buffer_virt[request_id];
1930 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
1933 } /* End twa_scsiop_execute_scsi_complete() */
1935 /* This function tells the controller to shut down */
1936 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1938 /* Disable interrupts */
1939 TW_DISABLE_INTERRUPTS(tw_dev);
1941 /* Free up the IRQ */
1942 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1944 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1946 /* Tell the card we are shutting down */
1947 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1948 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1950 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1953 /* Clear all interrupts just before exit */
1954 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1955 } /* End __twa_shutdown() */
1957 /* Wrapper for __twa_shutdown */
1958 static void twa_shutdown(struct pci_dev *pdev)
1960 struct Scsi_Host *host = pci_get_drvdata(pdev);
1961 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1963 __twa_shutdown(tw_dev);
1964 } /* End twa_shutdown() */
1966 /* This function will look up a string */
1967 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1971 for (index = 0; ((code != table[index].code) &&
1972 (table[index].text != (char *)0)); index++);
1973 return(table[index].text);
1974 } /* End twa_string_lookup() */
1976 /* This function will perform a pci-dma unmap */
1977 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1979 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1981 scsi_dma_unmap(cmd);
1982 } /* End twa_unmap_scsi_data() */
1984 /* scsi_host_template initializer */
1985 static struct scsi_host_template driver_template = {
1986 .module = THIS_MODULE,
1987 .name = "3ware 9000 Storage Controller",
1988 .queuecommand = twa_scsi_queue,
1989 .eh_host_reset_handler = twa_scsi_eh_reset,
1990 .bios_param = twa_scsi_biosparam,
1991 .change_queue_depth = twa_change_queue_depth,
1992 .can_queue = TW_Q_LENGTH-2,
1994 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
1995 .max_sectors = TW_MAX_SECTORS,
1996 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1997 .use_clustering = ENABLE_CLUSTERING,
1998 .shost_attrs = twa_host_attrs,
2002 /* This function will probe and initialize a card */
2003 static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2005 struct Scsi_Host *host = NULL;
2006 TW_Device_Extension *tw_dev;
2007 unsigned long mem_addr, mem_len;
2008 int retval = -ENODEV;
2010 retval = pci_enable_device(pdev);
2012 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2013 goto out_disable_device;
2016 pci_set_master(pdev);
2017 pci_try_set_mwi(pdev);
2019 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK)
2020 || pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
2021 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)
2022 || pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)) {
2023 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2025 goto out_disable_device;
2028 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2030 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2032 goto out_disable_device;
2034 tw_dev = (TW_Device_Extension *)host->hostdata;
2036 /* Save values to device extension */
2037 tw_dev->host = host;
2038 tw_dev->tw_pci_dev = pdev;
2040 if (twa_initialize_device_extension(tw_dev)) {
2041 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2042 goto out_free_device_extension;
2045 /* Request IO regions */
2046 retval = pci_request_regions(pdev, "3w-9xxx");
2048 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2049 goto out_free_device_extension;
2052 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
2053 mem_addr = pci_resource_start(pdev, 1);
2054 mem_len = pci_resource_len(pdev, 1);
2056 mem_addr = pci_resource_start(pdev, 2);
2057 mem_len = pci_resource_len(pdev, 2);
2060 /* Save base address */
2061 tw_dev->base_addr = ioremap(mem_addr, mem_len);
2062 if (!tw_dev->base_addr) {
2063 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2064 goto out_release_mem_region;
2067 /* Disable interrupts on the card */
2068 TW_DISABLE_INTERRUPTS(tw_dev);
2070 /* Initialize the card */
2071 if (twa_reset_sequence(tw_dev, 0))
2074 /* Set host specific parameters */
2075 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2076 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
2077 host->max_id = TW_MAX_UNITS_9650SE;
2079 host->max_id = TW_MAX_UNITS;
2081 host->max_cmd_len = TW_MAX_CDB_LEN;
2083 /* Channels aren't supported by adapter */
2084 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
2085 host->max_channel = 0;
2087 /* Register the card with the kernel SCSI layer */
2088 retval = scsi_add_host(host, &pdev->dev);
2090 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2094 pci_set_drvdata(pdev, host);
2096 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2097 host->host_no, mem_addr, pdev->irq);
2098 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2100 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2101 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2102 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2103 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2104 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2105 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
2107 /* Try to enable MSI */
2108 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2109 !pci_enable_msi(pdev))
2110 set_bit(TW_USING_MSI, &tw_dev->flags);
2112 /* Now setup the interrupt handler */
2113 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2115 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2116 goto out_remove_host;
2119 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2120 twa_device_extension_count++;
2122 /* Re-enable interrupts on the card */
2123 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2125 /* Finally, scan the host */
2126 scsi_scan_host(host);
2128 if (twa_major == -1) {
2129 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2130 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2135 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2136 pci_disable_msi(pdev);
2137 scsi_remove_host(host);
2139 iounmap(tw_dev->base_addr);
2140 out_release_mem_region:
2141 pci_release_regions(pdev);
2142 out_free_device_extension:
2143 twa_free_device_extension(tw_dev);
2144 scsi_host_put(host);
2146 pci_disable_device(pdev);
2149 } /* End twa_probe() */
2151 /* This function is called to remove a device */
2152 static void twa_remove(struct pci_dev *pdev)
2154 struct Scsi_Host *host = pci_get_drvdata(pdev);
2155 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2157 scsi_remove_host(tw_dev->host);
2159 /* Unregister character device */
2160 if (twa_major >= 0) {
2161 unregister_chrdev(twa_major, "twa");
2165 /* Shutdown the card */
2166 __twa_shutdown(tw_dev);
2168 /* Disable MSI if enabled */
2169 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2170 pci_disable_msi(pdev);
2172 /* Free IO remapping */
2173 iounmap(tw_dev->base_addr);
2175 /* Free up the mem region */
2176 pci_release_regions(pdev);
2178 /* Free up device extension resources */
2179 twa_free_device_extension(tw_dev);
2181 scsi_host_put(tw_dev->host);
2182 pci_disable_device(pdev);
2183 twa_device_extension_count--;
2184 } /* End twa_remove() */
2187 /* This function is called on PCI suspend */
2188 static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
2190 struct Scsi_Host *host = pci_get_drvdata(pdev);
2191 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2193 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2195 TW_DISABLE_INTERRUPTS(tw_dev);
2196 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2198 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2199 pci_disable_msi(pdev);
2201 /* Tell the card we are shutting down */
2202 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2203 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2205 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2207 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2209 pci_save_state(pdev);
2210 pci_disable_device(pdev);
2211 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2214 } /* End twa_suspend() */
2216 /* This function is called on PCI resume */
2217 static int twa_resume(struct pci_dev *pdev)
2220 struct Scsi_Host *host = pci_get_drvdata(pdev);
2221 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2223 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2224 pci_set_power_state(pdev, PCI_D0);
2225 pci_enable_wake(pdev, PCI_D0, 0);
2226 pci_restore_state(pdev);
2228 retval = pci_enable_device(pdev);
2230 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2234 pci_set_master(pdev);
2235 pci_try_set_mwi(pdev);
2237 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK)
2238 || pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
2239 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)
2240 || pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)) {
2241 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2243 goto out_disable_device;
2246 /* Initialize the card */
2247 if (twa_reset_sequence(tw_dev, 0)) {
2249 goto out_disable_device;
2252 /* Now setup the interrupt handler */
2253 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2255 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2257 goto out_disable_device;
2260 /* Now enable MSI if enabled */
2261 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2262 pci_enable_msi(pdev);
2264 /* Re-enable interrupts on the card */
2265 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2267 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2271 scsi_remove_host(host);
2272 pci_disable_device(pdev);
2275 } /* End twa_resume() */
2278 /* PCI Devices supported by this driver */
2279 static struct pci_device_id twa_pci_tbl[] __devinitdata = {
2280 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2281 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2282 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2283 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2284 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2285 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2286 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2287 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2290 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2292 /* pci_driver initializer */
2293 static struct pci_driver twa_driver = {
2295 .id_table = twa_pci_tbl,
2297 .remove = twa_remove,
2299 .suspend = twa_suspend,
2300 .resume = twa_resume,
2302 .shutdown = twa_shutdown
2305 /* This function is called on driver initialization */
2306 static int __init twa_init(void)
2308 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2310 return pci_register_driver(&twa_driver);
2311 } /* End twa_init() */
2313 /* This function is called on driver exit */
2314 static void __exit twa_exit(void)
2316 pci_unregister_driver(&twa_driver);
2317 } /* End twa_exit() */
2319 module_init(twa_init);
2320 module_exit(twa_exit);