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);
152 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id);
156 /* Show some statistics about the card */
157 static ssize_t twa_show_stats(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 struct Scsi_Host *host = class_to_shost(dev);
161 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
162 unsigned long flags = 0;
165 spin_lock_irqsave(tw_dev->host->host_lock, flags);
166 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
167 "Current commands posted: %4d\n"
168 "Max commands posted: %4d\n"
169 "Current pending commands: %4d\n"
170 "Max pending commands: %4d\n"
171 "Last sgl length: %4d\n"
172 "Max sgl length: %4d\n"
173 "Last sector count: %4d\n"
174 "Max sector count: %4d\n"
175 "SCSI Host Resets: %4d\n"
178 tw_dev->posted_request_count,
179 tw_dev->max_posted_request_count,
180 tw_dev->pending_request_count,
181 tw_dev->max_pending_request_count,
183 tw_dev->max_sgl_entries,
184 tw_dev->sector_count,
185 tw_dev->max_sector_count,
188 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
190 } /* End twa_show_stats() */
192 /* Create sysfs 'stats' entry */
193 static struct device_attribute twa_host_stats_attr = {
198 .show = twa_show_stats
201 /* Host attributes initializer */
202 static struct device_attribute *twa_host_attrs[] = {
203 &twa_host_stats_attr,
207 /* File operations struct for character device */
208 static const struct file_operations twa_fops = {
209 .owner = THIS_MODULE,
210 .unlocked_ioctl = twa_chrdev_ioctl,
211 .open = twa_chrdev_open,
213 .llseek = noop_llseek,
216 /* This function will complete an aen request from the isr */
217 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
219 TW_Command_Full *full_command_packet;
220 TW_Command *command_packet;
221 TW_Command_Apache_Header *header;
225 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
226 tw_dev->posted_request_count--;
227 aen = le16_to_cpu(header->status_block.error);
228 full_command_packet = tw_dev->command_packet_virt[request_id];
229 command_packet = &full_command_packet->command.oldcommand;
231 /* First check for internal completion of set param for time sync */
232 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
233 /* Keep reading the queue in case there are more aen's */
234 if (twa_aen_read_queue(tw_dev, request_id))
243 case TW_AEN_QUEUE_EMPTY:
244 /* Quit reading the queue if this is the last one */
246 case TW_AEN_SYNC_TIME_WITH_HOST:
247 twa_aen_sync_time(tw_dev, request_id);
251 twa_aen_queue_event(tw_dev, header);
253 /* If there are more aen's, keep reading the queue */
254 if (twa_aen_read_queue(tw_dev, request_id))
263 tw_dev->state[request_id] = TW_S_COMPLETED;
264 twa_free_request_id(tw_dev, request_id);
265 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
268 } /* End twa_aen_complete() */
270 /* This function will drain aen queue */
271 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
274 char cdb[TW_MAX_CDB_LEN];
275 TW_SG_Entry sglist[1];
276 int finished = 0, count = 0;
277 TW_Command_Full *full_command_packet;
278 TW_Command_Apache_Header *header;
280 int first_reset = 0, queue = 0, retval = 1;
287 full_command_packet = tw_dev->command_packet_virt[request_id];
288 memset(full_command_packet, 0, sizeof(TW_Command_Full));
291 memset(&cdb, 0, TW_MAX_CDB_LEN);
292 cdb[0] = REQUEST_SENSE; /* opcode */
293 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
295 /* Initialize sglist */
296 memset(&sglist, 0, sizeof(TW_SG_Entry));
297 sglist[0].length = TW_SECTOR_SIZE;
298 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
300 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
301 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
305 /* Mark internal command */
306 tw_dev->srb[request_id] = NULL;
309 /* Send command to the board */
310 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
311 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
315 /* Now poll for completion */
316 if (twa_poll_response(tw_dev, request_id, 30)) {
317 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
318 tw_dev->posted_request_count--;
322 tw_dev->posted_request_count--;
323 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
324 aen = le16_to_cpu(header->status_block.error);
329 case TW_AEN_QUEUE_EMPTY:
330 if (first_reset != 1)
335 case TW_AEN_SOFT_RESET:
336 if (first_reset == 0)
341 case TW_AEN_SYNC_TIME_WITH_HOST:
347 /* Now queue an event info */
349 twa_aen_queue_event(tw_dev, header);
350 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
352 if (count == TW_MAX_AEN_DRAIN)
357 tw_dev->state[request_id] = TW_S_INITIAL;
359 } /* End twa_aen_drain_queue() */
361 /* This function will queue an event */
362 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
373 /* Fill out event info */
374 event = tw_dev->event_queue[tw_dev->error_index];
376 /* Check for clobber */
379 sprintf(host, " scsi%d:", tw_dev->host->host_no);
380 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
381 tw_dev->aen_clobber = 1;
384 aen = le16_to_cpu(header->status_block.error);
385 memset(event, 0, sizeof(TW_Event));
387 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
388 do_gettimeofday(&time);
389 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
390 event->time_stamp_sec = local_time;
391 event->aen_code = aen;
392 event->retrieved = TW_AEN_NOT_RETRIEVED;
393 event->sequence_id = tw_dev->error_sequence_id;
394 tw_dev->error_sequence_id++;
396 /* Check for embedded error string */
397 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
399 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
400 event->parameter_len = strlen(header->err_specific_desc);
401 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
402 if (event->severity != TW_AEN_SEVERITY_DEBUG)
403 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
405 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
406 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
407 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
408 header->err_specific_desc);
412 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
413 tw_dev->event_queue_wrapped = 1;
414 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
415 } /* End twa_aen_queue_event() */
417 /* This function will read the aen queue from the isr */
418 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
420 char cdb[TW_MAX_CDB_LEN];
421 TW_SG_Entry sglist[1];
422 TW_Command_Full *full_command_packet;
425 full_command_packet = tw_dev->command_packet_virt[request_id];
426 memset(full_command_packet, 0, sizeof(TW_Command_Full));
429 memset(&cdb, 0, TW_MAX_CDB_LEN);
430 cdb[0] = REQUEST_SENSE; /* opcode */
431 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
433 /* Initialize sglist */
434 memset(&sglist, 0, sizeof(TW_SG_Entry));
435 sglist[0].length = TW_SECTOR_SIZE;
436 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
438 /* Mark internal command */
439 tw_dev->srb[request_id] = NULL;
441 /* Now post the command packet */
442 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
443 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
449 } /* End twa_aen_read_queue() */
451 /* This function will look up an AEN severity string */
452 static char *twa_aen_severity_lookup(unsigned char severity_code)
456 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
457 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
460 retval = twa_aen_severity_table[severity_code];
463 } /* End twa_aen_severity_lookup() */
465 /* This function will sync firmware time with the host time */
466 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
470 TW_Command_Full *full_command_packet;
471 TW_Command *command_packet;
472 TW_Param_Apache *param;
475 /* Fill out the command packet */
476 full_command_packet = tw_dev->command_packet_virt[request_id];
477 memset(full_command_packet, 0, sizeof(TW_Command_Full));
478 command_packet = &full_command_packet->command.oldcommand;
479 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
480 command_packet->request_id = request_id;
481 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
482 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
483 command_packet->size = TW_COMMAND_SIZE;
484 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
486 /* Setup the param */
487 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
488 memset(param, 0, TW_SECTOR_SIZE);
489 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
490 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
491 param->parameter_size_bytes = cpu_to_le16(4);
493 /* Convert system time in UTC to local time seconds since last
495 do_gettimeofday(&utc);
496 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
497 schedulertime = local_time - (3 * 86400);
498 schedulertime = cpu_to_le32(schedulertime % 604800);
500 memcpy(param->data, &schedulertime, sizeof(u32));
502 /* Mark internal command */
503 tw_dev->srb[request_id] = NULL;
505 /* Now post the command */
506 twa_post_command_packet(tw_dev, request_id, 1);
507 } /* End twa_aen_sync_time() */
509 /* This function will allocate memory and check if it is correctly aligned */
510 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
513 dma_addr_t dma_handle;
514 unsigned long *cpu_addr;
517 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
519 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
523 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
524 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
525 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
529 memset(cpu_addr, 0, size*TW_Q_LENGTH);
531 for (i = 0; i < TW_Q_LENGTH; i++) {
534 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
535 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
538 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
539 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
546 } /* End twa_allocate_memory() */
548 /* This function will check the status register for unexpected bits */
549 static int twa_check_bits(u32 status_reg_value)
553 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
555 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
561 } /* End twa_check_bits() */
563 /* This function will check the srl and decide if we are compatible */
564 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
567 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
568 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
569 u32 init_connect_result = 0;
571 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
572 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
573 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
574 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
575 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
576 &fw_on_ctlr_build, &init_connect_result)) {
577 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
581 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
582 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
583 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
585 /* Try base mode compatibility */
586 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
587 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
588 TW_EXTENDED_INIT_CONNECT,
589 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
590 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
591 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
592 &fw_on_ctlr_branch, &fw_on_ctlr_build,
593 &init_connect_result)) {
594 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
597 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
598 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
599 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
601 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
605 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
606 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
607 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
610 /* Load rest of compatibility struct */
611 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_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 tw_dev->state[request_id] = TW_S_COMPLETED;
1343 twa_free_request_id(tw_dev, request_id);
1344 tw_dev->posted_request_count--;
1345 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1346 twa_unmap_scsi_data(tw_dev, request_id);
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 perform a pci-dma mapping for a scatter gather list */
1405 static int twa_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
1408 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1410 use_sg = scsi_dma_map(cmd);
1413 else if (use_sg < 0) {
1414 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to map scatter gather list");
1418 cmd->SCp.phase = TW_PHASE_SGLIST;
1419 cmd->SCp.have_data_in = use_sg;
1422 } /* End twa_map_scsi_sg_data() */
1424 /* This function will poll for a response interrupt of a request */
1425 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1427 int retval = 1, found = 0, response_request_id;
1428 TW_Response_Queue response_queue;
1429 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1431 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1432 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1433 response_request_id = TW_RESID_OUT(response_queue.response_id);
1434 if (request_id != response_request_id) {
1435 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1438 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1439 if (full_command_packet->command.newcommand.status != 0) {
1441 twa_fill_sense(tw_dev, request_id, 0, 0);
1446 if (full_command_packet->command.oldcommand.status != 0) {
1448 twa_fill_sense(tw_dev, request_id, 0, 0);
1459 } /* End twa_poll_response() */
1461 /* This function will poll the status register for a flag */
1462 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1464 u32 status_reg_value;
1465 unsigned long before;
1468 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1471 if (twa_check_bits(status_reg_value))
1472 twa_decode_bits(tw_dev, status_reg_value);
1474 while ((status_reg_value & flag) != flag) {
1475 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1477 if (twa_check_bits(status_reg_value))
1478 twa_decode_bits(tw_dev, status_reg_value);
1480 if (time_after(jiffies, before + HZ * seconds))
1488 } /* End twa_poll_status() */
1490 /* This function will poll the status register for disappearance of a flag */
1491 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1493 u32 status_reg_value;
1494 unsigned long before;
1497 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1500 if (twa_check_bits(status_reg_value))
1501 twa_decode_bits(tw_dev, status_reg_value);
1503 while ((status_reg_value & flag) != 0) {
1504 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1505 if (twa_check_bits(status_reg_value))
1506 twa_decode_bits(tw_dev, status_reg_value);
1508 if (time_after(jiffies, before + HZ * seconds))
1516 } /* End twa_poll_status_gone() */
1518 /* This function will attempt to post a command packet to the board */
1519 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1521 u32 status_reg_value;
1522 dma_addr_t command_que_value;
1525 command_que_value = tw_dev->command_packet_phys[request_id];
1527 /* For 9650SE write low 4 bytes first */
1528 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1529 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1530 command_que_value += TW_COMMAND_OFFSET;
1531 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1534 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1536 if (twa_check_bits(status_reg_value))
1537 twa_decode_bits(tw_dev, status_reg_value);
1539 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1541 /* Only pend internal driver commands */
1543 retval = SCSI_MLQUEUE_HOST_BUSY;
1547 /* Couldn't post the command packet, so we do it later */
1548 if (tw_dev->state[request_id] != TW_S_PENDING) {
1549 tw_dev->state[request_id] = TW_S_PENDING;
1550 tw_dev->pending_request_count++;
1551 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1552 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1554 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1555 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1557 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1560 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1561 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1562 /* Now write upper 4 bytes */
1563 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1565 if (sizeof(dma_addr_t) > 4) {
1566 command_que_value += TW_COMMAND_OFFSET;
1567 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1568 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1570 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1573 tw_dev->state[request_id] = TW_S_POSTED;
1574 tw_dev->posted_request_count++;
1575 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1576 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1582 } /* End twa_post_command_packet() */
1584 /* This function will reset a device extension */
1585 static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1589 unsigned long flags = 0;
1591 set_bit(TW_IN_RESET, &tw_dev->flags);
1592 TW_DISABLE_INTERRUPTS(tw_dev);
1593 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1594 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1596 /* Abort all requests that are in progress */
1597 for (i = 0; i < TW_Q_LENGTH; i++) {
1598 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1599 (tw_dev->state[i] != TW_S_INITIAL) &&
1600 (tw_dev->state[i] != TW_S_COMPLETED)) {
1601 if (tw_dev->srb[i]) {
1602 tw_dev->srb[i]->result = (DID_RESET << 16);
1603 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1604 twa_unmap_scsi_data(tw_dev, i);
1609 /* Reset queues and counts */
1610 for (i = 0; i < TW_Q_LENGTH; i++) {
1611 tw_dev->free_queue[i] = i;
1612 tw_dev->state[i] = TW_S_INITIAL;
1614 tw_dev->free_head = TW_Q_START;
1615 tw_dev->free_tail = TW_Q_START;
1616 tw_dev->posted_request_count = 0;
1617 tw_dev->pending_request_count = 0;
1618 tw_dev->pending_head = TW_Q_START;
1619 tw_dev->pending_tail = TW_Q_START;
1620 tw_dev->reset_print = 0;
1622 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1624 if (twa_reset_sequence(tw_dev, 1))
1627 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1628 clear_bit(TW_IN_RESET, &tw_dev->flags);
1629 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1634 } /* End twa_reset_device_extension() */
1636 /* This function will reset a controller */
1637 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1639 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1641 while (tries < TW_MAX_RESET_TRIES) {
1642 if (do_soft_reset) {
1643 TW_SOFT_RESET(tw_dev);
1644 /* Clear pchip/response queue on 9550SX */
1645 if (twa_empty_response_queue_large(tw_dev)) {
1646 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1653 /* Make sure controller is in a good state */
1654 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1655 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1661 /* Empty response queue */
1662 if (twa_empty_response_queue(tw_dev)) {
1663 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1671 /* Check for compatibility/flash */
1672 if (twa_check_srl(tw_dev, &flashed)) {
1673 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1684 /* Drain the AEN queue */
1685 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1686 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1692 /* If we got here, controller is in a good state */
1698 } /* End twa_reset_sequence() */
1700 /* This funciton returns unit geometry in cylinders/heads/sectors */
1701 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1703 int heads, sectors, cylinders;
1704 TW_Device_Extension *tw_dev;
1706 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1708 if (capacity >= 0x200000) {
1711 cylinders = sector_div(capacity, heads * sectors);
1715 cylinders = sector_div(capacity, heads * sectors);
1720 geom[2] = cylinders;
1723 } /* End twa_scsi_biosparam() */
1725 /* This is the new scsi eh reset function */
1726 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1728 TW_Device_Extension *tw_dev = NULL;
1729 int retval = FAILED;
1731 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1733 tw_dev->num_resets++;
1735 sdev_printk(KERN_WARNING, SCpnt->device,
1736 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1737 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1739 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1740 mutex_lock(&tw_dev->ioctl_lock);
1742 /* Now reset the card and some of the device extension data */
1743 if (twa_reset_device_extension(tw_dev)) {
1744 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1750 mutex_unlock(&tw_dev->ioctl_lock);
1752 } /* End twa_scsi_eh_reset() */
1754 /* This is the main scsi queue function to handle scsi opcodes */
1755 static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1757 int request_id, retval;
1758 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1760 /* If we are resetting due to timed out ioctl, report as busy */
1761 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1762 retval = SCSI_MLQUEUE_HOST_BUSY;
1766 /* Check if this FW supports luns */
1767 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1768 SCpnt->result = (DID_BAD_TARGET << 16);
1774 /* Save done function into scsi_cmnd struct */
1775 SCpnt->scsi_done = done;
1777 /* Get a free request id */
1778 twa_get_request_id(tw_dev, &request_id);
1780 /* Save the scsi command for use by the ISR */
1781 tw_dev->srb[request_id] = SCpnt;
1783 /* Initialize phase to zero */
1784 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1786 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1788 case SCSI_MLQUEUE_HOST_BUSY:
1789 twa_free_request_id(tw_dev, request_id);
1790 twa_unmap_scsi_data(tw_dev, request_id);
1793 tw_dev->state[request_id] = TW_S_COMPLETED;
1794 twa_free_request_id(tw_dev, request_id);
1795 twa_unmap_scsi_data(tw_dev, request_id);
1796 SCpnt->result = (DID_ERROR << 16);
1802 } /* End twa_scsi_queue() */
1804 static DEF_SCSI_QCMD(twa_scsi_queue)
1806 /* This function hands scsi cdb's to the firmware */
1807 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1809 TW_Command_Full *full_command_packet;
1810 TW_Command_Apache *command_packet;
1811 u32 num_sectors = 0x0;
1813 struct scsi_cmnd *srb = NULL;
1814 struct scatterlist *sglist = NULL, *sg;
1817 if (tw_dev->srb[request_id]) {
1818 srb = tw_dev->srb[request_id];
1819 if (scsi_sglist(srb))
1820 sglist = scsi_sglist(srb);
1823 /* Initialize command packet */
1824 full_command_packet = tw_dev->command_packet_virt[request_id];
1825 full_command_packet->header.header_desc.size_header = 128;
1826 full_command_packet->header.status_block.error = 0;
1827 full_command_packet->header.status_block.severity__reserved = 0;
1829 command_packet = &full_command_packet->command.newcommand;
1830 command_packet->status = 0;
1831 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1833 /* We forced 16 byte cdb use earlier */
1835 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1837 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1840 command_packet->unit = srb->device->id;
1841 command_packet->request_id__lunl =
1842 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1844 command_packet->request_id__lunl =
1845 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1846 command_packet->unit = 0;
1849 command_packet->sgl_offset = 16;
1852 /* Map sglist from scsi layer to cmd packet */
1854 if (scsi_sg_count(srb)) {
1855 if ((scsi_sg_count(srb) == 1) &&
1856 (scsi_bufflen(srb) < TW_MIN_SGL_LENGTH)) {
1857 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1858 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1859 scsi_sg_copy_to_buffer(srb,
1860 tw_dev->generic_buffer_virt[request_id],
1862 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1863 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1865 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1869 scsi_for_each_sg(srb, sg, sg_count, i) {
1870 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1871 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
1872 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1873 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1878 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1881 /* Internal cdb post */
1882 for (i = 0; i < use_sg; i++) {
1883 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1884 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1885 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1886 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1890 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1894 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1895 num_sectors = (u32)srb->cmnd[4];
1897 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1898 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1901 /* Update sector statistic */
1902 tw_dev->sector_count = num_sectors;
1903 if (tw_dev->sector_count > tw_dev->max_sector_count)
1904 tw_dev->max_sector_count = tw_dev->sector_count;
1906 /* Update SG statistics */
1908 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1909 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1910 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1913 /* Now post the command to the board */
1915 retval = twa_post_command_packet(tw_dev, request_id, 0);
1917 twa_post_command_packet(tw_dev, request_id, 1);
1922 } /* End twa_scsiop_execute_scsi() */
1924 /* This function completes an execute scsi operation */
1925 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1927 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1929 if (scsi_bufflen(cmd) < TW_MIN_SGL_LENGTH &&
1930 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1931 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
1932 if (scsi_sg_count(cmd) == 1) {
1933 void *buf = tw_dev->generic_buffer_virt[request_id];
1935 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
1938 } /* End twa_scsiop_execute_scsi_complete() */
1940 /* This function tells the controller to shut down */
1941 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1943 /* Disable interrupts */
1944 TW_DISABLE_INTERRUPTS(tw_dev);
1946 /* Free up the IRQ */
1947 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1949 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1951 /* Tell the card we are shutting down */
1952 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1953 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1955 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1958 /* Clear all interrupts just before exit */
1959 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1960 } /* End __twa_shutdown() */
1962 /* Wrapper for __twa_shutdown */
1963 static void twa_shutdown(struct pci_dev *pdev)
1965 struct Scsi_Host *host = pci_get_drvdata(pdev);
1966 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1968 __twa_shutdown(tw_dev);
1969 } /* End twa_shutdown() */
1971 /* This function will look up a string */
1972 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1976 for (index = 0; ((code != table[index].code) &&
1977 (table[index].text != (char *)0)); index++);
1978 return(table[index].text);
1979 } /* End twa_string_lookup() */
1981 /* This function will perform a pci-dma unmap */
1982 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1984 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1986 if (cmd->SCp.phase == TW_PHASE_SGLIST)
1987 scsi_dma_unmap(cmd);
1988 } /* End twa_unmap_scsi_data() */
1990 /* This function gets called when a disk is coming on-line */
1991 static int twa_slave_configure(struct scsi_device *sdev)
1993 /* Force 60 second timeout */
1994 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1997 } /* End twa_slave_configure() */
1999 /* scsi_host_template initializer */
2000 static struct scsi_host_template driver_template = {
2001 .module = THIS_MODULE,
2002 .name = "3ware 9000 Storage Controller",
2003 .queuecommand = twa_scsi_queue,
2004 .eh_host_reset_handler = twa_scsi_eh_reset,
2005 .bios_param = twa_scsi_biosparam,
2006 .change_queue_depth = scsi_change_queue_depth,
2007 .can_queue = TW_Q_LENGTH-2,
2008 .slave_configure = twa_slave_configure,
2010 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
2011 .max_sectors = TW_MAX_SECTORS,
2012 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
2013 .use_clustering = ENABLE_CLUSTERING,
2014 .shost_attrs = twa_host_attrs,
2019 /* This function will probe and initialize a card */
2020 static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2022 struct Scsi_Host *host = NULL;
2023 TW_Device_Extension *tw_dev;
2024 unsigned long mem_addr, mem_len;
2025 int retval = -ENODEV;
2027 retval = pci_enable_device(pdev);
2029 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2030 goto out_disable_device;
2033 pci_set_master(pdev);
2034 pci_try_set_mwi(pdev);
2036 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2037 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2038 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2039 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2040 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2042 goto out_disable_device;
2045 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2047 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2049 goto out_disable_device;
2051 tw_dev = (TW_Device_Extension *)host->hostdata;
2053 /* Save values to device extension */
2054 tw_dev->host = host;
2055 tw_dev->tw_pci_dev = pdev;
2057 if (twa_initialize_device_extension(tw_dev)) {
2058 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2059 goto out_free_device_extension;
2062 /* Request IO regions */
2063 retval = pci_request_regions(pdev, "3w-9xxx");
2065 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2066 goto out_free_device_extension;
2069 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
2070 mem_addr = pci_resource_start(pdev, 1);
2071 mem_len = pci_resource_len(pdev, 1);
2073 mem_addr = pci_resource_start(pdev, 2);
2074 mem_len = pci_resource_len(pdev, 2);
2077 /* Save base address */
2078 tw_dev->base_addr = ioremap(mem_addr, mem_len);
2079 if (!tw_dev->base_addr) {
2080 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2081 goto out_release_mem_region;
2084 /* Disable interrupts on the card */
2085 TW_DISABLE_INTERRUPTS(tw_dev);
2087 /* Initialize the card */
2088 if (twa_reset_sequence(tw_dev, 0))
2091 /* Set host specific parameters */
2092 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2093 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
2094 host->max_id = TW_MAX_UNITS_9650SE;
2096 host->max_id = TW_MAX_UNITS;
2098 host->max_cmd_len = TW_MAX_CDB_LEN;
2100 /* Channels aren't supported by adapter */
2101 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
2102 host->max_channel = 0;
2104 /* Register the card with the kernel SCSI layer */
2105 retval = scsi_add_host(host, &pdev->dev);
2107 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2111 pci_set_drvdata(pdev, host);
2113 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2114 host->host_no, mem_addr, pdev->irq);
2115 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2117 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2118 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2119 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2120 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2121 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2122 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
2124 /* Try to enable MSI */
2125 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2126 !pci_enable_msi(pdev))
2127 set_bit(TW_USING_MSI, &tw_dev->flags);
2129 /* Now setup the interrupt handler */
2130 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2132 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2133 goto out_remove_host;
2136 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2137 twa_device_extension_count++;
2139 /* Re-enable interrupts on the card */
2140 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2142 /* Finally, scan the host */
2143 scsi_scan_host(host);
2145 if (twa_major == -1) {
2146 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2147 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2152 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2153 pci_disable_msi(pdev);
2154 scsi_remove_host(host);
2156 iounmap(tw_dev->base_addr);
2157 out_release_mem_region:
2158 pci_release_regions(pdev);
2159 out_free_device_extension:
2160 twa_free_device_extension(tw_dev);
2161 scsi_host_put(host);
2163 pci_disable_device(pdev);
2166 } /* End twa_probe() */
2168 /* This function is called to remove a device */
2169 static void twa_remove(struct pci_dev *pdev)
2171 struct Scsi_Host *host = pci_get_drvdata(pdev);
2172 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2174 scsi_remove_host(tw_dev->host);
2176 /* Unregister character device */
2177 if (twa_major >= 0) {
2178 unregister_chrdev(twa_major, "twa");
2182 /* Shutdown the card */
2183 __twa_shutdown(tw_dev);
2185 /* Disable MSI if enabled */
2186 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2187 pci_disable_msi(pdev);
2189 /* Free IO remapping */
2190 iounmap(tw_dev->base_addr);
2192 /* Free up the mem region */
2193 pci_release_regions(pdev);
2195 /* Free up device extension resources */
2196 twa_free_device_extension(tw_dev);
2198 scsi_host_put(tw_dev->host);
2199 pci_disable_device(pdev);
2200 twa_device_extension_count--;
2201 } /* End twa_remove() */
2204 /* This function is called on PCI suspend */
2205 static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
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: Suspending host %d.\n", tw_dev->host->host_no);
2212 TW_DISABLE_INTERRUPTS(tw_dev);
2213 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2215 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2216 pci_disable_msi(pdev);
2218 /* Tell the card we are shutting down */
2219 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2220 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2222 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2224 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2226 pci_save_state(pdev);
2227 pci_disable_device(pdev);
2228 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2231 } /* End twa_suspend() */
2233 /* This function is called on PCI resume */
2234 static int twa_resume(struct pci_dev *pdev)
2237 struct Scsi_Host *host = pci_get_drvdata(pdev);
2238 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2240 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2241 pci_set_power_state(pdev, PCI_D0);
2242 pci_enable_wake(pdev, PCI_D0, 0);
2243 pci_restore_state(pdev);
2245 retval = pci_enable_device(pdev);
2247 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2251 pci_set_master(pdev);
2252 pci_try_set_mwi(pdev);
2254 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2255 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2256 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2257 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2258 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2260 goto out_disable_device;
2263 /* Initialize the card */
2264 if (twa_reset_sequence(tw_dev, 0)) {
2266 goto out_disable_device;
2269 /* Now setup the interrupt handler */
2270 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2272 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2274 goto out_disable_device;
2277 /* Now enable MSI if enabled */
2278 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2279 pci_enable_msi(pdev);
2281 /* Re-enable interrupts on the card */
2282 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2284 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2288 scsi_remove_host(host);
2289 pci_disable_device(pdev);
2292 } /* End twa_resume() */
2295 /* PCI Devices supported by this driver */
2296 static struct pci_device_id twa_pci_tbl[] = {
2297 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2298 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2299 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2300 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2301 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2302 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2303 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2304 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2307 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2309 /* pci_driver initializer */
2310 static struct pci_driver twa_driver = {
2312 .id_table = twa_pci_tbl,
2314 .remove = twa_remove,
2316 .suspend = twa_suspend,
2317 .resume = twa_resume,
2319 .shutdown = twa_shutdown
2322 /* This function is called on driver initialization */
2323 static int __init twa_init(void)
2325 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2327 return pci_register_driver(&twa_driver);
2328 } /* End twa_init() */
2330 /* This function is called on driver exit */
2331 static void __exit twa_exit(void)
2333 pci_unregister_driver(&twa_driver);
2334 } /* End twa_exit() */
2336 module_init(twa_init);
2337 module_exit(twa_exit);