2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
4 Written By: Adam Radford <linuxraid@lsi.com>
5 Modifications By: Tom Couch <linuxraid@lsi.com>
7 Copyright (C) 2004-2009 Applied Micro Circuits Corporation.
8 Copyright (C) 2010 LSI Corporation.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; version 2 of the License.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 solely responsible for determining the appropriateness of using and
25 distributing the Program and assumes all risks associated with its
26 exercise of rights under this Agreement, including but not limited to
27 the risks and costs of program errors, damage to or loss of data,
28 programs or equipment, and unavailability or interruption of operations.
30 DISCLAIMER OF LIABILITY
31 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43 Bugs/Comments/Suggestions should be mailed to:
46 For more information, goto:
49 Note: This version of the driver does not contain a bundled firmware
54 2.26.02.000 - Driver cleanup for kernel submission.
55 2.26.02.001 - Replace schedule_timeout() calls with msleep().
56 2.26.02.002 - Add support for PAE mode.
58 Fix twa_remove() to free irq handler/unregister_chrdev()
59 before shutting down card.
60 Change to new 'change_queue_depth' api.
61 Fix 'handled=1' ISR usage, remove bogus IRQ check.
62 Remove un-needed eh_abort handler.
63 Add support for embedded firmware error strings.
64 2.26.02.003 - Correctly handle single sgl's with use_sg=1.
65 2.26.02.004 - Add support for 9550SX controllers.
66 2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
67 2.26.02.006 - Fix 9550SX pchip reset timeout.
68 Add big endian support.
69 2.26.02.007 - Disable local interrupts during kmap/unmap_atomic().
70 2.26.02.008 - Free irq handler in __twa_shutdown().
72 Add support for 9650SE controllers.
73 2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails.
74 2.26.02.010 - Add support for 9690SA controllers.
75 2.26.02.011 - Increase max AENs drained to 256.
76 Add MSI support and "use_msi" module parameter.
77 Fix bug in twa_get_param() on 4GB+.
78 Use pci_resource_len() for ioremap().
79 2.26.02.012 - Add power management support.
80 2.26.02.013 - Fix bug in twa_load_sgl().
81 2.26.02.014 - Force 60 second timeout default.
84 #include <linux/module.h>
85 #include <linux/reboot.h>
86 #include <linux/spinlock.h>
87 #include <linux/interrupt.h>
88 #include <linux/moduleparam.h>
89 #include <linux/errno.h>
90 #include <linux/types.h>
91 #include <linux/delay.h>
92 #include <linux/pci.h>
93 #include <linux/time.h>
94 #include <linux/mutex.h>
95 #include <linux/slab.h>
98 #include <asm/uaccess.h>
99 #include <scsi/scsi.h>
100 #include <scsi/scsi_host.h>
101 #include <scsi/scsi_tcq.h>
102 #include <scsi/scsi_cmnd.h>
106 #define TW_DRIVER_VERSION "2.26.02.014"
107 static DEFINE_MUTEX(twa_chrdev_mutex);
108 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
109 static unsigned int twa_device_extension_count;
110 static int twa_major = -1;
111 extern struct timezone sys_tz;
113 /* Module parameters */
114 MODULE_AUTHOR ("LSI");
115 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
116 MODULE_LICENSE("GPL");
117 MODULE_VERSION(TW_DRIVER_VERSION);
119 static int use_msi = 0;
120 module_param(use_msi, int, S_IRUGO);
121 MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
123 /* Function prototypes */
124 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
125 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
126 static char *twa_aen_severity_lookup(unsigned char severity_code);
127 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
128 static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
129 static int twa_chrdev_open(struct inode *inode, struct file *file);
130 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
131 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
132 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
133 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
134 u32 set_features, unsigned short current_fw_srl,
135 unsigned short current_fw_arch_id,
136 unsigned short current_fw_branch,
137 unsigned short current_fw_build,
138 unsigned short *fw_on_ctlr_srl,
139 unsigned short *fw_on_ctlr_arch_id,
140 unsigned short *fw_on_ctlr_branch,
141 unsigned short *fw_on_ctlr_build,
142 u32 *init_connect_result);
143 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);
144 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
145 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
146 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
147 static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
148 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
149 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg);
150 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
151 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
155 /* Show some statistics about the card */
156 static ssize_t twa_show_stats(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 struct Scsi_Host *host = class_to_shost(dev);
160 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
161 unsigned long flags = 0;
164 spin_lock_irqsave(tw_dev->host->host_lock, flags);
165 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
166 "Current commands posted: %4d\n"
167 "Max commands posted: %4d\n"
168 "Current pending commands: %4d\n"
169 "Max pending commands: %4d\n"
170 "Last sgl length: %4d\n"
171 "Max sgl length: %4d\n"
172 "Last sector count: %4d\n"
173 "Max sector count: %4d\n"
174 "SCSI Host Resets: %4d\n"
177 tw_dev->posted_request_count,
178 tw_dev->max_posted_request_count,
179 tw_dev->pending_request_count,
180 tw_dev->max_pending_request_count,
182 tw_dev->max_sgl_entries,
183 tw_dev->sector_count,
184 tw_dev->max_sector_count,
187 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
189 } /* End twa_show_stats() */
191 /* Create sysfs 'stats' entry */
192 static struct device_attribute twa_host_stats_attr = {
197 .show = twa_show_stats
200 /* Host attributes initializer */
201 static struct device_attribute *twa_host_attrs[] = {
202 &twa_host_stats_attr,
206 /* File operations struct for character device */
207 static const struct file_operations twa_fops = {
208 .owner = THIS_MODULE,
209 .unlocked_ioctl = twa_chrdev_ioctl,
210 .open = twa_chrdev_open,
212 .llseek = noop_llseek,
215 /* This function will complete an aen request from the isr */
216 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
218 TW_Command_Full *full_command_packet;
219 TW_Command *command_packet;
220 TW_Command_Apache_Header *header;
224 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
225 tw_dev->posted_request_count--;
226 aen = le16_to_cpu(header->status_block.error);
227 full_command_packet = tw_dev->command_packet_virt[request_id];
228 command_packet = &full_command_packet->command.oldcommand;
230 /* First check for internal completion of set param for time sync */
231 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
232 /* Keep reading the queue in case there are more aen's */
233 if (twa_aen_read_queue(tw_dev, request_id))
242 case TW_AEN_QUEUE_EMPTY:
243 /* Quit reading the queue if this is the last one */
245 case TW_AEN_SYNC_TIME_WITH_HOST:
246 twa_aen_sync_time(tw_dev, request_id);
250 twa_aen_queue_event(tw_dev, header);
252 /* If there are more aen's, keep reading the queue */
253 if (twa_aen_read_queue(tw_dev, request_id))
262 tw_dev->state[request_id] = TW_S_COMPLETED;
263 twa_free_request_id(tw_dev, request_id);
264 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
267 } /* End twa_aen_complete() */
269 /* This function will drain aen queue */
270 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
273 char cdb[TW_MAX_CDB_LEN];
274 TW_SG_Entry sglist[1];
275 int finished = 0, count = 0;
276 TW_Command_Full *full_command_packet;
277 TW_Command_Apache_Header *header;
279 int first_reset = 0, queue = 0, retval = 1;
286 full_command_packet = tw_dev->command_packet_virt[request_id];
287 memset(full_command_packet, 0, sizeof(TW_Command_Full));
290 memset(&cdb, 0, TW_MAX_CDB_LEN);
291 cdb[0] = REQUEST_SENSE; /* opcode */
292 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
294 /* Initialize sglist */
295 memset(&sglist, 0, sizeof(TW_SG_Entry));
296 sglist[0].length = TW_SECTOR_SIZE;
297 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
299 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
300 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
304 /* Mark internal command */
305 tw_dev->srb[request_id] = NULL;
308 /* Send command to the board */
309 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
310 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
314 /* Now poll for completion */
315 if (twa_poll_response(tw_dev, request_id, 30)) {
316 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
317 tw_dev->posted_request_count--;
321 tw_dev->posted_request_count--;
322 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
323 aen = le16_to_cpu(header->status_block.error);
328 case TW_AEN_QUEUE_EMPTY:
329 if (first_reset != 1)
334 case TW_AEN_SOFT_RESET:
335 if (first_reset == 0)
340 case TW_AEN_SYNC_TIME_WITH_HOST:
346 /* Now queue an event info */
348 twa_aen_queue_event(tw_dev, header);
349 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
351 if (count == TW_MAX_AEN_DRAIN)
356 tw_dev->state[request_id] = TW_S_INITIAL;
358 } /* End twa_aen_drain_queue() */
360 /* This function will queue an event */
361 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
372 /* Fill out event info */
373 event = tw_dev->event_queue[tw_dev->error_index];
375 /* Check for clobber */
378 sprintf(host, " scsi%d:", tw_dev->host->host_no);
379 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
380 tw_dev->aen_clobber = 1;
383 aen = le16_to_cpu(header->status_block.error);
384 memset(event, 0, sizeof(TW_Event));
386 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
387 do_gettimeofday(&time);
388 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
389 event->time_stamp_sec = local_time;
390 event->aen_code = aen;
391 event->retrieved = TW_AEN_NOT_RETRIEVED;
392 event->sequence_id = tw_dev->error_sequence_id;
393 tw_dev->error_sequence_id++;
395 /* Check for embedded error string */
396 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
398 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
399 event->parameter_len = strlen(header->err_specific_desc);
400 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
401 if (event->severity != TW_AEN_SEVERITY_DEBUG)
402 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
404 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
405 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
406 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
407 header->err_specific_desc);
411 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
412 tw_dev->event_queue_wrapped = 1;
413 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
414 } /* End twa_aen_queue_event() */
416 /* This function will read the aen queue from the isr */
417 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
419 char cdb[TW_MAX_CDB_LEN];
420 TW_SG_Entry sglist[1];
421 TW_Command_Full *full_command_packet;
424 full_command_packet = tw_dev->command_packet_virt[request_id];
425 memset(full_command_packet, 0, sizeof(TW_Command_Full));
428 memset(&cdb, 0, TW_MAX_CDB_LEN);
429 cdb[0] = REQUEST_SENSE; /* opcode */
430 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
432 /* Initialize sglist */
433 memset(&sglist, 0, sizeof(TW_SG_Entry));
434 sglist[0].length = TW_SECTOR_SIZE;
435 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
437 /* Mark internal command */
438 tw_dev->srb[request_id] = NULL;
440 /* Now post the command packet */
441 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
442 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
448 } /* End twa_aen_read_queue() */
450 /* This function will look up an AEN severity string */
451 static char *twa_aen_severity_lookup(unsigned char severity_code)
455 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
456 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
459 retval = twa_aen_severity_table[severity_code];
462 } /* End twa_aen_severity_lookup() */
464 /* This function will sync firmware time with the host time */
465 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
469 TW_Command_Full *full_command_packet;
470 TW_Command *command_packet;
471 TW_Param_Apache *param;
474 /* Fill out the command packet */
475 full_command_packet = tw_dev->command_packet_virt[request_id];
476 memset(full_command_packet, 0, sizeof(TW_Command_Full));
477 command_packet = &full_command_packet->command.oldcommand;
478 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
479 command_packet->request_id = request_id;
480 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
481 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
482 command_packet->size = TW_COMMAND_SIZE;
483 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
485 /* Setup the param */
486 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
487 memset(param, 0, TW_SECTOR_SIZE);
488 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
489 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
490 param->parameter_size_bytes = cpu_to_le16(4);
492 /* Convert system time in UTC to local time seconds since last
494 do_gettimeofday(&utc);
495 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
496 schedulertime = local_time - (3 * 86400);
497 schedulertime = cpu_to_le32(schedulertime % 604800);
499 memcpy(param->data, &schedulertime, sizeof(u32));
501 /* Mark internal command */
502 tw_dev->srb[request_id] = NULL;
504 /* Now post the command */
505 twa_post_command_packet(tw_dev, request_id, 1);
506 } /* End twa_aen_sync_time() */
508 /* This function will allocate memory and check if it is correctly aligned */
509 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
512 dma_addr_t dma_handle;
513 unsigned long *cpu_addr;
516 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
518 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
522 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
523 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
524 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
528 memset(cpu_addr, 0, size*TW_Q_LENGTH);
530 for (i = 0; i < TW_Q_LENGTH; i++) {
533 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
534 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
537 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
538 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
545 } /* End twa_allocate_memory() */
547 /* This function will check the status register for unexpected bits */
548 static int twa_check_bits(u32 status_reg_value)
552 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
554 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
560 } /* End twa_check_bits() */
562 /* This function will check the srl and decide if we are compatible */
563 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
566 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
567 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
568 u32 init_connect_result = 0;
570 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
571 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
572 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
573 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
574 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
575 &fw_on_ctlr_build, &init_connect_result)) {
576 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
580 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
581 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
582 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
584 /* Try base mode compatibility */
585 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
586 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
587 TW_EXTENDED_INIT_CONNECT,
588 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
589 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
590 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
591 &fw_on_ctlr_branch, &fw_on_ctlr_build,
592 &init_connect_result)) {
593 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
596 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
597 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
598 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
600 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
604 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
605 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
606 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
609 /* Load rest of compatibility struct */
610 strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION,
611 sizeof(tw_dev->tw_compat_info.driver_version));
612 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
613 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
614 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
615 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
616 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
617 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
618 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
619 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
620 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
625 } /* End twa_check_srl() */
627 /* This function handles ioctl for the character device */
628 static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
630 struct inode *inode = file_inode(file);
632 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
633 dma_addr_t dma_handle;
635 unsigned int sequence_id = 0;
636 unsigned char event_index, start_index;
637 TW_Ioctl_Driver_Command driver_command;
638 TW_Ioctl_Buf_Apache *tw_ioctl;
640 TW_Command_Full *full_command_packet;
641 TW_Compatibility_Info *tw_compat_info;
643 struct timeval current_time;
645 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
646 int retval = TW_IOCTL_ERROR_OS_EFAULT;
647 void __user *argp = (void __user *)arg;
649 mutex_lock(&twa_chrdev_mutex);
651 /* Only let one of these through at a time */
652 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
653 retval = TW_IOCTL_ERROR_OS_EINTR;
657 /* First copy down the driver command */
658 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
661 /* Check data buffer size */
662 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
663 retval = TW_IOCTL_ERROR_OS_EINVAL;
667 /* Hardware can only do multiple of 512 byte transfers */
668 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
670 /* Now allocate ioctl buf memory */
671 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);
673 retval = TW_IOCTL_ERROR_OS_ENOMEM;
677 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
679 /* Now copy down the entire ioctl */
680 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
683 /* See which ioctl we are doing */
685 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
686 spin_lock_irqsave(tw_dev->host->host_lock, flags);
687 twa_get_request_id(tw_dev, &request_id);
689 /* Flag internal command */
690 tw_dev->srb[request_id] = NULL;
692 /* Flag chrdev ioctl */
693 tw_dev->chrdev_request_id = request_id;
695 full_command_packet = &tw_ioctl->firmware_command;
697 /* Load request id and sglist for both command types */
698 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
700 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
702 /* Now post the command packet to the controller */
703 twa_post_command_packet(tw_dev, request_id, 1);
704 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
706 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
708 /* Now wait for command to complete */
709 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
711 /* We timed out, and didn't get an interrupt */
712 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
713 /* Now we need to reset the board */
714 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
715 tw_dev->host->host_no, TW_DRIVER, 0x37,
717 retval = TW_IOCTL_ERROR_OS_EIO;
718 twa_reset_device_extension(tw_dev);
722 /* Now copy in the command packet response */
723 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
725 /* Now complete the io */
726 spin_lock_irqsave(tw_dev->host->host_lock, flags);
727 tw_dev->posted_request_count--;
728 tw_dev->state[request_id] = TW_S_COMPLETED;
729 twa_free_request_id(tw_dev, request_id);
730 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
732 case TW_IOCTL_GET_COMPATIBILITY_INFO:
733 tw_ioctl->driver_command.status = 0;
734 /* Copy compatibility struct into ioctl data buffer */
735 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
736 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
738 case TW_IOCTL_GET_LAST_EVENT:
739 if (tw_dev->event_queue_wrapped) {
740 if (tw_dev->aen_clobber) {
741 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
742 tw_dev->aen_clobber = 0;
744 tw_ioctl->driver_command.status = 0;
746 if (!tw_dev->error_index) {
747 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
750 tw_ioctl->driver_command.status = 0;
752 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
753 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
754 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
756 case TW_IOCTL_GET_FIRST_EVENT:
757 if (tw_dev->event_queue_wrapped) {
758 if (tw_dev->aen_clobber) {
759 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
760 tw_dev->aen_clobber = 0;
762 tw_ioctl->driver_command.status = 0;
763 event_index = tw_dev->error_index;
765 if (!tw_dev->error_index) {
766 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
769 tw_ioctl->driver_command.status = 0;
772 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
773 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
775 case TW_IOCTL_GET_NEXT_EVENT:
776 event = (TW_Event *)tw_ioctl->data_buffer;
777 sequence_id = event->sequence_id;
778 tw_ioctl->driver_command.status = 0;
780 if (tw_dev->event_queue_wrapped) {
781 if (tw_dev->aen_clobber) {
782 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
783 tw_dev->aen_clobber = 0;
785 start_index = tw_dev->error_index;
787 if (!tw_dev->error_index) {
788 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
793 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
795 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
796 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
797 tw_dev->aen_clobber = 1;
798 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
801 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
802 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
804 case TW_IOCTL_GET_PREVIOUS_EVENT:
805 event = (TW_Event *)tw_ioctl->data_buffer;
806 sequence_id = event->sequence_id;
807 tw_ioctl->driver_command.status = 0;
809 if (tw_dev->event_queue_wrapped) {
810 if (tw_dev->aen_clobber) {
811 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
812 tw_dev->aen_clobber = 0;
814 start_index = tw_dev->error_index;
816 if (!tw_dev->error_index) {
817 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
822 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
824 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
825 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
826 tw_dev->aen_clobber = 1;
827 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
830 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
831 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
833 case TW_IOCTL_GET_LOCK:
834 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
835 do_gettimeofday(¤t_time);
836 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
838 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
839 tw_dev->ioctl_sem_lock = 1;
840 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
841 tw_ioctl->driver_command.status = 0;
842 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
844 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
845 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
848 case TW_IOCTL_RELEASE_LOCK:
849 if (tw_dev->ioctl_sem_lock == 1) {
850 tw_dev->ioctl_sem_lock = 0;
851 tw_ioctl->driver_command.status = 0;
853 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
857 retval = TW_IOCTL_ERROR_OS_ENOTTY;
861 /* Now copy the entire response to userspace */
862 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
865 /* Now free ioctl buf memory */
866 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
868 mutex_unlock(&tw_dev->ioctl_lock);
870 mutex_unlock(&twa_chrdev_mutex);
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;
881 minor_number = iminor(inode);
882 if (minor_number >= twa_device_extension_count)
887 } /* End twa_chrdev_open() */
889 /* This function will print readable messages from status register errors */
890 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
894 /* Check for various error conditions and handle them appropriately */
895 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
896 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
897 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
900 if (status_reg_value & TW_STATUS_PCI_ABORT) {
901 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
902 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
903 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
906 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
907 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
908 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
909 (!test_bit(TW_IN_RESET, &tw_dev->flags)))
910 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
911 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
914 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
915 if (tw_dev->reset_print == 0) {
916 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
917 tw_dev->reset_print = 1;
924 } /* End twa_decode_bits() */
926 /* This function will empty the response queue */
927 static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
929 u32 status_reg_value, response_que_value;
930 int count = 0, retval = 1;
932 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
934 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
935 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
936 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
939 if (count == TW_MAX_RESPONSE_DRAIN)
945 } /* End twa_empty_response_queue() */
947 /* This function will clear the pchip/response queue on 9550SX */
948 static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
950 u32 response_que_value = 0;
951 unsigned long before;
954 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
956 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
957 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
959 if (time_after(jiffies, before + HZ * 30))
962 /* P-chip settle time */
969 } /* End twa_empty_response_queue_large() */
971 /* This function passes sense keys from firmware to scsi layer */
972 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
974 TW_Command_Full *full_command_packet;
975 unsigned short error;
979 full_command_packet = tw_dev->command_packet_virt[request_id];
981 /* Check for embedded error string */
982 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
984 /* Don't print error for Logical unit not supported during rollcall */
985 error = le16_to_cpu(full_command_packet->header.status_block.error);
986 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
988 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
989 tw_dev->host->host_no,
990 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
991 full_command_packet->header.status_block.error,
992 error_str[0] == '\0' ?
993 twa_string_lookup(twa_error_table,
994 full_command_packet->header.status_block.error) : error_str,
995 full_command_packet->header.err_specific_desc);
997 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
998 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
999 full_command_packet->header.status_block.error,
1000 error_str[0] == '\0' ?
1001 twa_string_lookup(twa_error_table,
1002 full_command_packet->header.status_block.error) : error_str,
1003 full_command_packet->header.err_specific_desc);
1007 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1008 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1009 retval = TW_ISR_DONT_RESULT;
1015 } /* End twa_fill_sense() */
1017 /* This function will free up device extension resources */
1018 static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1020 if (tw_dev->command_packet_virt[0])
1021 pci_free_consistent(tw_dev->tw_pci_dev,
1022 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1023 tw_dev->command_packet_virt[0],
1024 tw_dev->command_packet_phys[0]);
1026 if (tw_dev->generic_buffer_virt[0])
1027 pci_free_consistent(tw_dev->tw_pci_dev,
1028 TW_SECTOR_SIZE*TW_Q_LENGTH,
1029 tw_dev->generic_buffer_virt[0],
1030 tw_dev->generic_buffer_phys[0]);
1032 kfree(tw_dev->event_queue[0]);
1033 } /* End twa_free_device_extension() */
1035 /* This function will free a request id */
1036 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1038 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1039 tw_dev->state[request_id] = TW_S_FINISHED;
1040 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1041 } /* End twa_free_request_id() */
1043 /* This function will get parameter table entries from the firmware */
1044 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1046 TW_Command_Full *full_command_packet;
1047 TW_Command *command_packet;
1048 TW_Param_Apache *param;
1049 void *retval = NULL;
1051 /* Setup the command packet */
1052 full_command_packet = tw_dev->command_packet_virt[request_id];
1053 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1054 command_packet = &full_command_packet->command.oldcommand;
1056 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1057 command_packet->size = TW_COMMAND_SIZE;
1058 command_packet->request_id = request_id;
1059 command_packet->byte6_offset.block_count = cpu_to_le16(1);
1061 /* Now setup the param */
1062 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1063 memset(param, 0, TW_SECTOR_SIZE);
1064 param->table_id = cpu_to_le16(table_id | 0x8000);
1065 param->parameter_id = cpu_to_le16(parameter_id);
1066 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
1068 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1069 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1071 /* Post the command packet to the board */
1072 twa_post_command_packet(tw_dev, request_id, 1);
1074 /* Poll for completion */
1075 if (twa_poll_response(tw_dev, request_id, 30))
1076 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1078 retval = (void *)&(param->data[0]);
1080 tw_dev->posted_request_count--;
1081 tw_dev->state[request_id] = TW_S_INITIAL;
1084 } /* End twa_get_param() */
1086 /* This function will assign an available request id */
1087 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1089 *request_id = tw_dev->free_queue[tw_dev->free_head];
1090 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1091 tw_dev->state[*request_id] = TW_S_STARTED;
1092 } /* End twa_get_request_id() */
1094 /* This function will send an initconnection command to controller */
1095 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1096 u32 set_features, unsigned short current_fw_srl,
1097 unsigned short current_fw_arch_id,
1098 unsigned short current_fw_branch,
1099 unsigned short current_fw_build,
1100 unsigned short *fw_on_ctlr_srl,
1101 unsigned short *fw_on_ctlr_arch_id,
1102 unsigned short *fw_on_ctlr_branch,
1103 unsigned short *fw_on_ctlr_build,
1104 u32 *init_connect_result)
1106 TW_Command_Full *full_command_packet;
1107 TW_Initconnect *tw_initconnect;
1108 int request_id = 0, retval = 1;
1110 /* Initialize InitConnection command packet */
1111 full_command_packet = tw_dev->command_packet_virt[request_id];
1112 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1113 full_command_packet->header.header_desc.size_header = 128;
1115 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1116 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1117 tw_initconnect->request_id = request_id;
1118 tw_initconnect->message_credits = cpu_to_le16(message_credits);
1119 tw_initconnect->features = set_features;
1121 /* Turn on 64-bit sgl support if we need to */
1122 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1124 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1126 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1127 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1128 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1129 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1130 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1131 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1133 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1135 /* Send command packet to the board */
1136 twa_post_command_packet(tw_dev, request_id, 1);
1138 /* Poll for completion */
1139 if (twa_poll_response(tw_dev, request_id, 30)) {
1140 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1142 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1143 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1144 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1145 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1146 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1147 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1152 tw_dev->posted_request_count--;
1153 tw_dev->state[request_id] = TW_S_INITIAL;
1156 } /* End twa_initconnection() */
1158 /* This function will initialize the fields of a device extension */
1159 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1163 /* Initialize command packet buffers */
1164 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1165 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1169 /* Initialize generic buffer */
1170 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1171 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1175 /* Allocate event info space */
1176 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1177 if (!tw_dev->event_queue[0]) {
1178 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1183 for (i = 0; i < TW_Q_LENGTH; i++) {
1184 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1185 tw_dev->free_queue[i] = i;
1186 tw_dev->state[i] = TW_S_INITIAL;
1189 tw_dev->pending_head = TW_Q_START;
1190 tw_dev->pending_tail = TW_Q_START;
1191 tw_dev->free_head = TW_Q_START;
1192 tw_dev->free_tail = TW_Q_START;
1193 tw_dev->error_sequence_id = 1;
1194 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1196 mutex_init(&tw_dev->ioctl_lock);
1197 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1202 } /* End twa_initialize_device_extension() */
1204 /* This function is the interrupt service routine */
1205 static irqreturn_t twa_interrupt(int irq, void *dev_instance)
1207 int request_id, error = 0;
1208 u32 status_reg_value;
1209 TW_Response_Queue response_que;
1210 TW_Command_Full *full_command_packet;
1211 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1214 /* Get the per adapter lock */
1215 spin_lock(tw_dev->host->host_lock);
1217 /* Read the registers */
1218 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1220 /* Check if this is our interrupt, otherwise bail */
1221 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1222 goto twa_interrupt_bail;
1226 /* If we are resetting, bail */
1227 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1228 goto twa_interrupt_bail;
1230 /* Check controller for errors */
1231 if (twa_check_bits(status_reg_value)) {
1232 if (twa_decode_bits(tw_dev, status_reg_value)) {
1233 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1234 goto twa_interrupt_bail;
1238 /* Handle host interrupt */
1239 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1240 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1242 /* Handle attention interrupt */
1243 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1244 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1245 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1246 twa_get_request_id(tw_dev, &request_id);
1248 error = twa_aen_read_queue(tw_dev, request_id);
1250 tw_dev->state[request_id] = TW_S_COMPLETED;
1251 twa_free_request_id(tw_dev, request_id);
1252 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1257 /* Handle command interrupt */
1258 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1259 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1260 /* Drain as many pending commands as we can */
1261 while (tw_dev->pending_request_count > 0) {
1262 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1263 if (tw_dev->state[request_id] != TW_S_PENDING) {
1264 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1265 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1266 goto twa_interrupt_bail;
1268 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1269 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1270 tw_dev->pending_request_count--;
1272 /* If we get here, we will continue re-posting on the next command interrupt */
1278 /* Handle response interrupt */
1279 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1281 /* Drain the response queue from the board */
1282 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1283 /* Complete the response */
1284 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1285 request_id = TW_RESID_OUT(response_que.response_id);
1286 full_command_packet = tw_dev->command_packet_virt[request_id];
1288 /* Check for command packet errors */
1289 if (full_command_packet->command.newcommand.status != 0) {
1290 if (tw_dev->srb[request_id] != NULL) {
1291 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1293 /* Skip ioctl error prints */
1294 if (request_id != tw_dev->chrdev_request_id) {
1295 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1300 /* Check for correct state */
1301 if (tw_dev->state[request_id] != TW_S_POSTED) {
1302 if (tw_dev->srb[request_id] != NULL) {
1303 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1304 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1305 goto twa_interrupt_bail;
1309 /* Check for internal command completion */
1310 if (tw_dev->srb[request_id] == NULL) {
1311 if (request_id != tw_dev->chrdev_request_id) {
1312 if (twa_aen_complete(tw_dev, request_id))
1313 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1315 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1316 wake_up(&tw_dev->ioctl_wqueue);
1319 struct scsi_cmnd *cmd;
1321 cmd = tw_dev->srb[request_id];
1323 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1324 /* If no error command was a success */
1326 cmd->result = (DID_OK << 16);
1329 /* If error, command failed */
1331 /* Ask for a host reset */
1332 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1335 /* Report residual bytes for single sgl */
1336 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1337 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1338 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1341 /* Now complete the io */
1342 scsi_dma_unmap(cmd);
1343 cmd->scsi_done(cmd);
1344 tw_dev->state[request_id] = TW_S_COMPLETED;
1345 twa_free_request_id(tw_dev, request_id);
1346 tw_dev->posted_request_count--;
1349 /* Check for valid status after each drain */
1350 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1351 if (twa_check_bits(status_reg_value)) {
1352 if (twa_decode_bits(tw_dev, status_reg_value)) {
1353 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1354 goto twa_interrupt_bail;
1361 spin_unlock(tw_dev->host->host_lock);
1362 return IRQ_RETVAL(handled);
1363 } /* End twa_interrupt() */
1365 /* This function will load the request id and various sgls for ioctls */
1366 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)
1368 TW_Command *oldcommand;
1369 TW_Command_Apache *newcommand;
1371 unsigned int pae = 0;
1373 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1376 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1377 newcommand = &full_command_packet->command.newcommand;
1378 newcommand->request_id__lunl =
1379 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);
1384 newcommand->sgl_entries__lunh =
1385 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
1387 oldcommand = &full_command_packet->command.oldcommand;
1388 oldcommand->request_id = request_id;
1390 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1391 /* Load the sg list */
1392 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1393 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1395 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1396 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1397 sgl->length = cpu_to_le32(length);
1399 oldcommand->size += pae;
1402 } /* End twa_load_sgl() */
1404 /* This function will poll for a response interrupt of a request */
1405 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1407 int retval = 1, found = 0, response_request_id;
1408 TW_Response_Queue response_queue;
1409 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1411 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1412 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1413 response_request_id = TW_RESID_OUT(response_queue.response_id);
1414 if (request_id != response_request_id) {
1415 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1418 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1419 if (full_command_packet->command.newcommand.status != 0) {
1421 twa_fill_sense(tw_dev, request_id, 0, 0);
1426 if (full_command_packet->command.oldcommand.status != 0) {
1428 twa_fill_sense(tw_dev, request_id, 0, 0);
1439 } /* End twa_poll_response() */
1441 /* This function will poll the status register for a flag */
1442 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1444 u32 status_reg_value;
1445 unsigned long before;
1448 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1451 if (twa_check_bits(status_reg_value))
1452 twa_decode_bits(tw_dev, status_reg_value);
1454 while ((status_reg_value & flag) != flag) {
1455 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1457 if (twa_check_bits(status_reg_value))
1458 twa_decode_bits(tw_dev, status_reg_value);
1460 if (time_after(jiffies, before + HZ * seconds))
1468 } /* End twa_poll_status() */
1470 /* This function will poll the status register for disappearance of a flag */
1471 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1473 u32 status_reg_value;
1474 unsigned long before;
1477 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1480 if (twa_check_bits(status_reg_value))
1481 twa_decode_bits(tw_dev, status_reg_value);
1483 while ((status_reg_value & flag) != 0) {
1484 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_gone() */
1498 /* This function will attempt to post a command packet to the board */
1499 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1501 u32 status_reg_value;
1502 dma_addr_t command_que_value;
1505 command_que_value = tw_dev->command_packet_phys[request_id];
1507 /* For 9650SE write low 4 bytes first */
1508 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1509 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1510 command_que_value += TW_COMMAND_OFFSET;
1511 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1514 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1516 if (twa_check_bits(status_reg_value))
1517 twa_decode_bits(tw_dev, status_reg_value);
1519 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1521 /* Only pend internal driver commands */
1523 retval = SCSI_MLQUEUE_HOST_BUSY;
1527 /* Couldn't post the command packet, so we do it later */
1528 if (tw_dev->state[request_id] != TW_S_PENDING) {
1529 tw_dev->state[request_id] = TW_S_PENDING;
1530 tw_dev->pending_request_count++;
1531 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1532 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1534 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1535 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1537 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1540 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1541 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1542 /* Now write upper 4 bytes */
1543 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1545 if (sizeof(dma_addr_t) > 4) {
1546 command_que_value += TW_COMMAND_OFFSET;
1547 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1548 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1550 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1553 tw_dev->state[request_id] = TW_S_POSTED;
1554 tw_dev->posted_request_count++;
1555 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1556 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1562 } /* End twa_post_command_packet() */
1564 /* This function will reset a device extension */
1565 static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1569 unsigned long flags = 0;
1571 set_bit(TW_IN_RESET, &tw_dev->flags);
1572 TW_DISABLE_INTERRUPTS(tw_dev);
1573 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1574 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1576 /* Abort all requests that are in progress */
1577 for (i = 0; i < TW_Q_LENGTH; i++) {
1578 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1579 (tw_dev->state[i] != TW_S_INITIAL) &&
1580 (tw_dev->state[i] != TW_S_COMPLETED)) {
1581 if (tw_dev->srb[i]) {
1582 struct scsi_cmnd *cmd = tw_dev->srb[i];
1584 cmd->result = (DID_RESET << 16);
1585 scsi_dma_unmap(cmd);
1586 cmd->scsi_done(cmd);
1591 /* Reset queues and counts */
1592 for (i = 0; i < TW_Q_LENGTH; i++) {
1593 tw_dev->free_queue[i] = i;
1594 tw_dev->state[i] = TW_S_INITIAL;
1596 tw_dev->free_head = TW_Q_START;
1597 tw_dev->free_tail = TW_Q_START;
1598 tw_dev->posted_request_count = 0;
1599 tw_dev->pending_request_count = 0;
1600 tw_dev->pending_head = TW_Q_START;
1601 tw_dev->pending_tail = TW_Q_START;
1602 tw_dev->reset_print = 0;
1604 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1606 if (twa_reset_sequence(tw_dev, 1))
1609 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1610 clear_bit(TW_IN_RESET, &tw_dev->flags);
1611 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1616 } /* End twa_reset_device_extension() */
1618 /* This function will reset a controller */
1619 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1621 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1623 while (tries < TW_MAX_RESET_TRIES) {
1624 if (do_soft_reset) {
1625 TW_SOFT_RESET(tw_dev);
1626 /* Clear pchip/response queue on 9550SX */
1627 if (twa_empty_response_queue_large(tw_dev)) {
1628 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1635 /* Make sure controller is in a good state */
1636 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1637 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1643 /* Empty response queue */
1644 if (twa_empty_response_queue(tw_dev)) {
1645 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1653 /* Check for compatibility/flash */
1654 if (twa_check_srl(tw_dev, &flashed)) {
1655 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1666 /* Drain the AEN queue */
1667 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1668 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1674 /* If we got here, controller is in a good state */
1680 } /* End twa_reset_sequence() */
1682 /* This funciton returns unit geometry in cylinders/heads/sectors */
1683 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1685 int heads, sectors, cylinders;
1686 TW_Device_Extension *tw_dev;
1688 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1690 if (capacity >= 0x200000) {
1693 cylinders = sector_div(capacity, heads * sectors);
1697 cylinders = sector_div(capacity, heads * sectors);
1702 geom[2] = cylinders;
1705 } /* End twa_scsi_biosparam() */
1707 /* This is the new scsi eh reset function */
1708 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1710 TW_Device_Extension *tw_dev = NULL;
1711 int retval = FAILED;
1713 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1715 tw_dev->num_resets++;
1717 sdev_printk(KERN_WARNING, SCpnt->device,
1718 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1719 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1721 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1722 mutex_lock(&tw_dev->ioctl_lock);
1724 /* Now reset the card and some of the device extension data */
1725 if (twa_reset_device_extension(tw_dev)) {
1726 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1732 mutex_unlock(&tw_dev->ioctl_lock);
1734 } /* End twa_scsi_eh_reset() */
1736 /* This is the main scsi queue function to handle scsi opcodes */
1737 static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1739 int request_id, retval;
1740 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1742 /* If we are resetting due to timed out ioctl, report as busy */
1743 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1744 retval = SCSI_MLQUEUE_HOST_BUSY;
1748 /* Check if this FW supports luns */
1749 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1750 SCpnt->result = (DID_BAD_TARGET << 16);
1756 /* Save done function into scsi_cmnd struct */
1757 SCpnt->scsi_done = done;
1759 /* Get a free request id */
1760 twa_get_request_id(tw_dev, &request_id);
1762 /* Save the scsi command for use by the ISR */
1763 tw_dev->srb[request_id] = SCpnt;
1765 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1767 case SCSI_MLQUEUE_HOST_BUSY:
1768 scsi_dma_unmap(SCpnt);
1769 twa_free_request_id(tw_dev, request_id);
1772 SCpnt->result = (DID_ERROR << 16);
1773 scsi_dma_unmap(SCpnt);
1775 tw_dev->state[request_id] = TW_S_COMPLETED;
1776 twa_free_request_id(tw_dev, request_id);
1781 } /* End twa_scsi_queue() */
1783 static DEF_SCSI_QCMD(twa_scsi_queue)
1785 /* This function hands scsi cdb's to the firmware */
1786 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1788 TW_Command_Full *full_command_packet;
1789 TW_Command_Apache *command_packet;
1790 u32 num_sectors = 0x0;
1792 struct scsi_cmnd *srb = NULL;
1793 struct scatterlist *sglist = NULL, *sg;
1796 if (tw_dev->srb[request_id]) {
1797 srb = tw_dev->srb[request_id];
1798 if (scsi_sglist(srb))
1799 sglist = scsi_sglist(srb);
1802 /* Initialize command packet */
1803 full_command_packet = tw_dev->command_packet_virt[request_id];
1804 full_command_packet->header.header_desc.size_header = 128;
1805 full_command_packet->header.status_block.error = 0;
1806 full_command_packet->header.status_block.severity__reserved = 0;
1808 command_packet = &full_command_packet->command.newcommand;
1809 command_packet->status = 0;
1810 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1812 /* We forced 16 byte cdb use earlier */
1814 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1816 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1819 command_packet->unit = srb->device->id;
1820 command_packet->request_id__lunl =
1821 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1823 command_packet->request_id__lunl =
1824 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1825 command_packet->unit = 0;
1828 command_packet->sgl_offset = 16;
1831 /* Map sglist from scsi layer to cmd packet */
1833 if (scsi_sg_count(srb)) {
1834 if ((scsi_sg_count(srb) == 1) &&
1835 (scsi_bufflen(srb) < TW_MIN_SGL_LENGTH)) {
1836 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1837 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1838 scsi_sg_copy_to_buffer(srb,
1839 tw_dev->generic_buffer_virt[request_id],
1841 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1842 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1844 sg_count = scsi_dma_map(srb);
1848 scsi_for_each_sg(srb, sg, sg_count, i) {
1849 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1850 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
1851 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1852 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1857 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1860 /* Internal cdb post */
1861 for (i = 0; i < use_sg; i++) {
1862 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1863 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1864 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1865 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1869 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1873 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1874 num_sectors = (u32)srb->cmnd[4];
1876 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1877 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1880 /* Update sector statistic */
1881 tw_dev->sector_count = num_sectors;
1882 if (tw_dev->sector_count > tw_dev->max_sector_count)
1883 tw_dev->max_sector_count = tw_dev->sector_count;
1885 /* Update SG statistics */
1887 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1888 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1889 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1892 /* Now post the command to the board */
1894 retval = twa_post_command_packet(tw_dev, request_id, 0);
1896 twa_post_command_packet(tw_dev, request_id, 1);
1901 } /* End twa_scsiop_execute_scsi() */
1903 /* This function completes an execute scsi operation */
1904 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1906 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1908 if (scsi_bufflen(cmd) < TW_MIN_SGL_LENGTH &&
1909 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1910 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
1911 if (scsi_sg_count(cmd) == 1) {
1912 void *buf = tw_dev->generic_buffer_virt[request_id];
1914 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
1917 } /* End twa_scsiop_execute_scsi_complete() */
1919 /* This function tells the controller to shut down */
1920 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1922 /* Disable interrupts */
1923 TW_DISABLE_INTERRUPTS(tw_dev);
1925 /* Free up the IRQ */
1926 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1928 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1930 /* Tell the card we are shutting down */
1931 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1932 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1934 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1937 /* Clear all interrupts just before exit */
1938 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1939 } /* End __twa_shutdown() */
1941 /* Wrapper for __twa_shutdown */
1942 static void twa_shutdown(struct pci_dev *pdev)
1944 struct Scsi_Host *host = pci_get_drvdata(pdev);
1945 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1947 __twa_shutdown(tw_dev);
1948 } /* End twa_shutdown() */
1950 /* This function will look up a string */
1951 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1955 for (index = 0; ((code != table[index].code) &&
1956 (table[index].text != (char *)0)); index++);
1957 return(table[index].text);
1958 } /* End twa_string_lookup() */
1960 /* This function gets called when a disk is coming on-line */
1961 static int twa_slave_configure(struct scsi_device *sdev)
1963 /* Force 60 second timeout */
1964 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1967 } /* End twa_slave_configure() */
1969 /* scsi_host_template initializer */
1970 static struct scsi_host_template driver_template = {
1971 .module = THIS_MODULE,
1972 .name = "3ware 9000 Storage Controller",
1973 .queuecommand = twa_scsi_queue,
1974 .eh_host_reset_handler = twa_scsi_eh_reset,
1975 .bios_param = twa_scsi_biosparam,
1976 .change_queue_depth = scsi_change_queue_depth,
1977 .can_queue = TW_Q_LENGTH-2,
1978 .slave_configure = twa_slave_configure,
1980 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
1981 .max_sectors = TW_MAX_SECTORS,
1982 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1983 .use_clustering = ENABLE_CLUSTERING,
1984 .shost_attrs = twa_host_attrs,
1989 /* This function will probe and initialize a card */
1990 static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1992 struct Scsi_Host *host = NULL;
1993 TW_Device_Extension *tw_dev;
1994 unsigned long mem_addr, mem_len;
1995 int retval = -ENODEV;
1997 retval = pci_enable_device(pdev);
1999 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2000 goto out_disable_device;
2003 pci_set_master(pdev);
2004 pci_try_set_mwi(pdev);
2006 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2007 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2008 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2009 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2010 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2012 goto out_disable_device;
2015 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2017 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2019 goto out_disable_device;
2021 tw_dev = (TW_Device_Extension *)host->hostdata;
2023 /* Save values to device extension */
2024 tw_dev->host = host;
2025 tw_dev->tw_pci_dev = pdev;
2027 if (twa_initialize_device_extension(tw_dev)) {
2028 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2029 goto out_free_device_extension;
2032 /* Request IO regions */
2033 retval = pci_request_regions(pdev, "3w-9xxx");
2035 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2036 goto out_free_device_extension;
2039 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
2040 mem_addr = pci_resource_start(pdev, 1);
2041 mem_len = pci_resource_len(pdev, 1);
2043 mem_addr = pci_resource_start(pdev, 2);
2044 mem_len = pci_resource_len(pdev, 2);
2047 /* Save base address */
2048 tw_dev->base_addr = ioremap(mem_addr, mem_len);
2049 if (!tw_dev->base_addr) {
2050 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2051 goto out_release_mem_region;
2054 /* Disable interrupts on the card */
2055 TW_DISABLE_INTERRUPTS(tw_dev);
2057 /* Initialize the card */
2058 if (twa_reset_sequence(tw_dev, 0))
2061 /* Set host specific parameters */
2062 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2063 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
2064 host->max_id = TW_MAX_UNITS_9650SE;
2066 host->max_id = TW_MAX_UNITS;
2068 host->max_cmd_len = TW_MAX_CDB_LEN;
2070 /* Channels aren't supported by adapter */
2071 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
2072 host->max_channel = 0;
2074 /* Register the card with the kernel SCSI layer */
2075 retval = scsi_add_host(host, &pdev->dev);
2077 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2081 pci_set_drvdata(pdev, host);
2083 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2084 host->host_no, mem_addr, pdev->irq);
2085 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2087 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2088 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2089 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2090 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2091 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2092 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
2094 /* Try to enable MSI */
2095 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2096 !pci_enable_msi(pdev))
2097 set_bit(TW_USING_MSI, &tw_dev->flags);
2099 /* Now setup the interrupt handler */
2100 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2102 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2103 goto out_remove_host;
2106 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2107 twa_device_extension_count++;
2109 /* Re-enable interrupts on the card */
2110 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2112 /* Finally, scan the host */
2113 scsi_scan_host(host);
2115 if (twa_major == -1) {
2116 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2117 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2122 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2123 pci_disable_msi(pdev);
2124 scsi_remove_host(host);
2126 iounmap(tw_dev->base_addr);
2127 out_release_mem_region:
2128 pci_release_regions(pdev);
2129 out_free_device_extension:
2130 twa_free_device_extension(tw_dev);
2131 scsi_host_put(host);
2133 pci_disable_device(pdev);
2136 } /* End twa_probe() */
2138 /* This function is called to remove a device */
2139 static void twa_remove(struct pci_dev *pdev)
2141 struct Scsi_Host *host = pci_get_drvdata(pdev);
2142 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2144 scsi_remove_host(tw_dev->host);
2146 /* Unregister character device */
2147 if (twa_major >= 0) {
2148 unregister_chrdev(twa_major, "twa");
2152 /* Shutdown the card */
2153 __twa_shutdown(tw_dev);
2155 /* Disable MSI if enabled */
2156 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2157 pci_disable_msi(pdev);
2159 /* Free IO remapping */
2160 iounmap(tw_dev->base_addr);
2162 /* Free up the mem region */
2163 pci_release_regions(pdev);
2165 /* Free up device extension resources */
2166 twa_free_device_extension(tw_dev);
2168 scsi_host_put(tw_dev->host);
2169 pci_disable_device(pdev);
2170 twa_device_extension_count--;
2171 } /* End twa_remove() */
2174 /* This function is called on PCI suspend */
2175 static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
2177 struct Scsi_Host *host = pci_get_drvdata(pdev);
2178 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2180 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2182 TW_DISABLE_INTERRUPTS(tw_dev);
2183 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2185 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2186 pci_disable_msi(pdev);
2188 /* Tell the card we are shutting down */
2189 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2190 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2192 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2194 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2196 pci_save_state(pdev);
2197 pci_disable_device(pdev);
2198 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2201 } /* End twa_suspend() */
2203 /* This function is called on PCI resume */
2204 static int twa_resume(struct pci_dev *pdev)
2207 struct Scsi_Host *host = pci_get_drvdata(pdev);
2208 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2210 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2211 pci_set_power_state(pdev, PCI_D0);
2212 pci_enable_wake(pdev, PCI_D0, 0);
2213 pci_restore_state(pdev);
2215 retval = pci_enable_device(pdev);
2217 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2221 pci_set_master(pdev);
2222 pci_try_set_mwi(pdev);
2224 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2225 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2226 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2227 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2228 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2230 goto out_disable_device;
2233 /* Initialize the card */
2234 if (twa_reset_sequence(tw_dev, 0)) {
2236 goto out_disable_device;
2239 /* Now setup the interrupt handler */
2240 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2242 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2244 goto out_disable_device;
2247 /* Now enable MSI if enabled */
2248 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2249 pci_enable_msi(pdev);
2251 /* Re-enable interrupts on the card */
2252 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2254 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2258 scsi_remove_host(host);
2259 pci_disable_device(pdev);
2262 } /* End twa_resume() */
2265 /* PCI Devices supported by this driver */
2266 static struct pci_device_id twa_pci_tbl[] = {
2267 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2268 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2269 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2270 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2271 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2272 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2273 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2274 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2277 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2279 /* pci_driver initializer */
2280 static struct pci_driver twa_driver = {
2282 .id_table = twa_pci_tbl,
2284 .remove = twa_remove,
2286 .suspend = twa_suspend,
2287 .resume = twa_resume,
2289 .shutdown = twa_shutdown
2292 /* This function is called on driver initialization */
2293 static int __init twa_init(void)
2295 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2297 return pci_register_driver(&twa_driver);
2298 } /* End twa_init() */
2300 /* This function is called on driver exit */
2301 static void __exit twa_exit(void)
2303 pci_unregister_driver(&twa_driver);
2304 } /* End twa_exit() */
2306 module_init(twa_init);
2307 module_exit(twa_exit);