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 /* This function will set a devices queue depth */
192 static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth,
195 if (reason != SCSI_QDEPTH_DEFAULT)
198 if (queue_depth > TW_Q_LENGTH-2)
199 queue_depth = TW_Q_LENGTH-2;
200 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
202 } /* End twa_change_queue_depth() */
204 /* Create sysfs 'stats' entry */
205 static struct device_attribute twa_host_stats_attr = {
210 .show = twa_show_stats
213 /* Host attributes initializer */
214 static struct device_attribute *twa_host_attrs[] = {
215 &twa_host_stats_attr,
219 /* File operations struct for character device */
220 static const struct file_operations twa_fops = {
221 .owner = THIS_MODULE,
222 .unlocked_ioctl = twa_chrdev_ioctl,
223 .open = twa_chrdev_open,
225 .llseek = noop_llseek,
229 * The controllers use an inline buffer instead of a mapped SGL for small,
230 * single entry buffers. Note that we treat a zero-length transfer like
233 static bool twa_command_mapped(struct scsi_cmnd *cmd)
235 return scsi_sg_count(cmd) != 1 ||
236 scsi_bufflen(cmd) >= TW_MIN_SGL_LENGTH;
239 /* This function will complete an aen request from the isr */
240 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
242 TW_Command_Full *full_command_packet;
243 TW_Command *command_packet;
244 TW_Command_Apache_Header *header;
248 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
249 tw_dev->posted_request_count--;
250 aen = le16_to_cpu(header->status_block.error);
251 full_command_packet = tw_dev->command_packet_virt[request_id];
252 command_packet = &full_command_packet->command.oldcommand;
254 /* First check for internal completion of set param for time sync */
255 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
256 /* Keep reading the queue in case there are more aen's */
257 if (twa_aen_read_queue(tw_dev, request_id))
266 case TW_AEN_QUEUE_EMPTY:
267 /* Quit reading the queue if this is the last one */
269 case TW_AEN_SYNC_TIME_WITH_HOST:
270 twa_aen_sync_time(tw_dev, request_id);
274 twa_aen_queue_event(tw_dev, header);
276 /* If there are more aen's, keep reading the queue */
277 if (twa_aen_read_queue(tw_dev, request_id))
286 tw_dev->state[request_id] = TW_S_COMPLETED;
287 twa_free_request_id(tw_dev, request_id);
288 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
291 } /* End twa_aen_complete() */
293 /* This function will drain aen queue */
294 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
297 char cdb[TW_MAX_CDB_LEN];
298 TW_SG_Entry sglist[1];
299 int finished = 0, count = 0;
300 TW_Command_Full *full_command_packet;
301 TW_Command_Apache_Header *header;
303 int first_reset = 0, queue = 0, retval = 1;
310 full_command_packet = tw_dev->command_packet_virt[request_id];
311 memset(full_command_packet, 0, sizeof(TW_Command_Full));
314 memset(&cdb, 0, TW_MAX_CDB_LEN);
315 cdb[0] = REQUEST_SENSE; /* opcode */
316 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
318 /* Initialize sglist */
319 memset(&sglist, 0, sizeof(TW_SG_Entry));
320 sglist[0].length = TW_SECTOR_SIZE;
321 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
323 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
324 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
328 /* Mark internal command */
329 tw_dev->srb[request_id] = NULL;
332 /* Send command to the board */
333 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
334 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
338 /* Now poll for completion */
339 if (twa_poll_response(tw_dev, request_id, 30)) {
340 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
341 tw_dev->posted_request_count--;
345 tw_dev->posted_request_count--;
346 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
347 aen = le16_to_cpu(header->status_block.error);
352 case TW_AEN_QUEUE_EMPTY:
353 if (first_reset != 1)
358 case TW_AEN_SOFT_RESET:
359 if (first_reset == 0)
364 case TW_AEN_SYNC_TIME_WITH_HOST:
370 /* Now queue an event info */
372 twa_aen_queue_event(tw_dev, header);
373 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
375 if (count == TW_MAX_AEN_DRAIN)
380 tw_dev->state[request_id] = TW_S_INITIAL;
382 } /* End twa_aen_drain_queue() */
384 /* This function will queue an event */
385 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
396 /* Fill out event info */
397 event = tw_dev->event_queue[tw_dev->error_index];
399 /* Check for clobber */
402 sprintf(host, " scsi%d:", tw_dev->host->host_no);
403 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
404 tw_dev->aen_clobber = 1;
407 aen = le16_to_cpu(header->status_block.error);
408 memset(event, 0, sizeof(TW_Event));
410 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
411 do_gettimeofday(&time);
412 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
413 event->time_stamp_sec = local_time;
414 event->aen_code = aen;
415 event->retrieved = TW_AEN_NOT_RETRIEVED;
416 event->sequence_id = tw_dev->error_sequence_id;
417 tw_dev->error_sequence_id++;
419 /* Check for embedded error string */
420 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
422 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
423 event->parameter_len = strlen(header->err_specific_desc);
424 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
425 if (event->severity != TW_AEN_SEVERITY_DEBUG)
426 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
428 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
429 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
430 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
431 header->err_specific_desc);
435 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
436 tw_dev->event_queue_wrapped = 1;
437 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
438 } /* End twa_aen_queue_event() */
440 /* This function will read the aen queue from the isr */
441 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
443 char cdb[TW_MAX_CDB_LEN];
444 TW_SG_Entry sglist[1];
445 TW_Command_Full *full_command_packet;
448 full_command_packet = tw_dev->command_packet_virt[request_id];
449 memset(full_command_packet, 0, sizeof(TW_Command_Full));
452 memset(&cdb, 0, TW_MAX_CDB_LEN);
453 cdb[0] = REQUEST_SENSE; /* opcode */
454 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
456 /* Initialize sglist */
457 memset(&sglist, 0, sizeof(TW_SG_Entry));
458 sglist[0].length = TW_SECTOR_SIZE;
459 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
461 /* Mark internal command */
462 tw_dev->srb[request_id] = NULL;
464 /* Now post the command packet */
465 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
466 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
472 } /* End twa_aen_read_queue() */
474 /* This function will look up an AEN severity string */
475 static char *twa_aen_severity_lookup(unsigned char severity_code)
479 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
480 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
483 retval = twa_aen_severity_table[severity_code];
486 } /* End twa_aen_severity_lookup() */
488 /* This function will sync firmware time with the host time */
489 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
493 TW_Command_Full *full_command_packet;
494 TW_Command *command_packet;
495 TW_Param_Apache *param;
498 /* Fill out the command packet */
499 full_command_packet = tw_dev->command_packet_virt[request_id];
500 memset(full_command_packet, 0, sizeof(TW_Command_Full));
501 command_packet = &full_command_packet->command.oldcommand;
502 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
503 command_packet->request_id = request_id;
504 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
505 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
506 command_packet->size = TW_COMMAND_SIZE;
507 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
509 /* Setup the param */
510 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
511 memset(param, 0, TW_SECTOR_SIZE);
512 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
513 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
514 param->parameter_size_bytes = cpu_to_le16(4);
516 /* Convert system time in UTC to local time seconds since last
518 do_gettimeofday(&utc);
519 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
520 schedulertime = local_time - (3 * 86400);
521 schedulertime = cpu_to_le32(schedulertime % 604800);
523 memcpy(param->data, &schedulertime, sizeof(u32));
525 /* Mark internal command */
526 tw_dev->srb[request_id] = NULL;
528 /* Now post the command */
529 twa_post_command_packet(tw_dev, request_id, 1);
530 } /* End twa_aen_sync_time() */
532 /* This function will allocate memory and check if it is correctly aligned */
533 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
536 dma_addr_t dma_handle;
537 unsigned long *cpu_addr;
540 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
542 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
546 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
547 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
548 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
552 memset(cpu_addr, 0, size*TW_Q_LENGTH);
554 for (i = 0; i < TW_Q_LENGTH; i++) {
557 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
558 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
561 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
562 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
569 } /* End twa_allocate_memory() */
571 /* This function will check the status register for unexpected bits */
572 static int twa_check_bits(u32 status_reg_value)
576 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
578 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
584 } /* End twa_check_bits() */
586 /* This function will check the srl and decide if we are compatible */
587 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
590 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
591 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
592 u32 init_connect_result = 0;
594 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
595 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
596 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
597 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
598 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
599 &fw_on_ctlr_build, &init_connect_result)) {
600 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
604 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
605 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
606 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
608 /* Try base mode compatibility */
609 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
610 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
611 TW_EXTENDED_INIT_CONNECT,
612 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
613 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
614 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
615 &fw_on_ctlr_branch, &fw_on_ctlr_build,
616 &init_connect_result)) {
617 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
620 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
621 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
622 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
624 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
628 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
629 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
630 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
633 /* Load rest of compatibility struct */
634 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
635 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
636 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
637 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
638 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
639 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
640 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
641 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
642 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
643 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
648 } /* End twa_check_srl() */
650 /* This function handles ioctl for the character device */
651 static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
653 struct inode *inode = file->f_path.dentry->d_inode;
655 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
656 dma_addr_t dma_handle;
658 unsigned int sequence_id = 0;
659 unsigned char event_index, start_index;
660 TW_Ioctl_Driver_Command driver_command;
661 TW_Ioctl_Buf_Apache *tw_ioctl;
663 TW_Command_Full *full_command_packet;
664 TW_Compatibility_Info *tw_compat_info;
666 struct timeval current_time;
668 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
669 int retval = TW_IOCTL_ERROR_OS_EFAULT;
670 void __user *argp = (void __user *)arg;
672 mutex_lock(&twa_chrdev_mutex);
674 /* Only let one of these through at a time */
675 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
676 retval = TW_IOCTL_ERROR_OS_EINTR;
680 /* First copy down the driver command */
681 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
684 /* Check data buffer size */
685 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
686 retval = TW_IOCTL_ERROR_OS_EINVAL;
690 /* Hardware can only do multiple of 512 byte transfers */
691 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
693 /* Now allocate ioctl buf memory */
694 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);
696 retval = TW_IOCTL_ERROR_OS_ENOMEM;
700 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
702 /* Now copy down the entire ioctl */
703 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
706 /* See which ioctl we are doing */
708 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
709 spin_lock_irqsave(tw_dev->host->host_lock, flags);
710 twa_get_request_id(tw_dev, &request_id);
712 /* Flag internal command */
713 tw_dev->srb[request_id] = NULL;
715 /* Flag chrdev ioctl */
716 tw_dev->chrdev_request_id = request_id;
718 full_command_packet = &tw_ioctl->firmware_command;
720 /* Load request id and sglist for both command types */
721 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
723 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
725 /* Now post the command packet to the controller */
726 twa_post_command_packet(tw_dev, request_id, 1);
727 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
729 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
731 /* Now wait for command to complete */
732 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
734 /* We timed out, and didn't get an interrupt */
735 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
736 /* Now we need to reset the board */
737 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
738 tw_dev->host->host_no, TW_DRIVER, 0x37,
740 retval = TW_IOCTL_ERROR_OS_EIO;
741 twa_reset_device_extension(tw_dev);
745 /* Now copy in the command packet response */
746 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
748 /* Now complete the io */
749 spin_lock_irqsave(tw_dev->host->host_lock, flags);
750 tw_dev->posted_request_count--;
751 tw_dev->state[request_id] = TW_S_COMPLETED;
752 twa_free_request_id(tw_dev, request_id);
753 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
755 case TW_IOCTL_GET_COMPATIBILITY_INFO:
756 tw_ioctl->driver_command.status = 0;
757 /* Copy compatibility struct into ioctl data buffer */
758 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
759 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
761 case TW_IOCTL_GET_LAST_EVENT:
762 if (tw_dev->event_queue_wrapped) {
763 if (tw_dev->aen_clobber) {
764 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
765 tw_dev->aen_clobber = 0;
767 tw_ioctl->driver_command.status = 0;
769 if (!tw_dev->error_index) {
770 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
773 tw_ioctl->driver_command.status = 0;
775 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
776 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
777 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
779 case TW_IOCTL_GET_FIRST_EVENT:
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 tw_ioctl->driver_command.status = 0;
786 event_index = tw_dev->error_index;
788 if (!tw_dev->error_index) {
789 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
792 tw_ioctl->driver_command.status = 0;
795 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
796 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
798 case TW_IOCTL_GET_NEXT_EVENT:
799 event = (TW_Event *)tw_ioctl->data_buffer;
800 sequence_id = event->sequence_id;
801 tw_ioctl->driver_command.status = 0;
803 if (tw_dev->event_queue_wrapped) {
804 if (tw_dev->aen_clobber) {
805 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
806 tw_dev->aen_clobber = 0;
808 start_index = tw_dev->error_index;
810 if (!tw_dev->error_index) {
811 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
816 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
818 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
819 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
820 tw_dev->aen_clobber = 1;
821 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
824 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
825 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
827 case TW_IOCTL_GET_PREVIOUS_EVENT:
828 event = (TW_Event *)tw_ioctl->data_buffer;
829 sequence_id = event->sequence_id;
830 tw_ioctl->driver_command.status = 0;
832 if (tw_dev->event_queue_wrapped) {
833 if (tw_dev->aen_clobber) {
834 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
835 tw_dev->aen_clobber = 0;
837 start_index = tw_dev->error_index;
839 if (!tw_dev->error_index) {
840 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
845 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
847 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
848 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
849 tw_dev->aen_clobber = 1;
850 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
853 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
854 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
856 case TW_IOCTL_GET_LOCK:
857 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
858 do_gettimeofday(¤t_time);
859 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
861 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
862 tw_dev->ioctl_sem_lock = 1;
863 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
864 tw_ioctl->driver_command.status = 0;
865 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
867 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
868 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
871 case TW_IOCTL_RELEASE_LOCK:
872 if (tw_dev->ioctl_sem_lock == 1) {
873 tw_dev->ioctl_sem_lock = 0;
874 tw_ioctl->driver_command.status = 0;
876 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
880 retval = TW_IOCTL_ERROR_OS_ENOTTY;
884 /* Now copy the entire response to userspace */
885 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
888 /* Now free ioctl buf memory */
889 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
891 mutex_unlock(&tw_dev->ioctl_lock);
893 mutex_unlock(&twa_chrdev_mutex);
895 } /* End twa_chrdev_ioctl() */
897 /* This function handles open for the character device */
898 /* NOTE that this function will race with remove. */
899 static int twa_chrdev_open(struct inode *inode, struct file *file)
901 unsigned int minor_number;
902 int retval = TW_IOCTL_ERROR_OS_ENODEV;
904 minor_number = iminor(inode);
905 if (minor_number >= twa_device_extension_count)
910 } /* End twa_chrdev_open() */
912 /* This function will print readable messages from status register errors */
913 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
917 /* Check for various error conditions and handle them appropriately */
918 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
919 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
920 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
923 if (status_reg_value & TW_STATUS_PCI_ABORT) {
924 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
925 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
926 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
929 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
930 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
931 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
932 (!test_bit(TW_IN_RESET, &tw_dev->flags)))
933 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
934 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
937 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
938 if (tw_dev->reset_print == 0) {
939 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
940 tw_dev->reset_print = 1;
947 } /* End twa_decode_bits() */
949 /* This function will empty the response queue */
950 static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
952 u32 status_reg_value, response_que_value;
953 int count = 0, retval = 1;
955 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
957 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
958 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
959 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
962 if (count == TW_MAX_RESPONSE_DRAIN)
968 } /* End twa_empty_response_queue() */
970 /* This function will clear the pchip/response queue on 9550SX */
971 static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
973 u32 response_que_value = 0;
974 unsigned long before;
977 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
979 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
980 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
982 if (time_after(jiffies, before + HZ * 30))
985 /* P-chip settle time */
992 } /* End twa_empty_response_queue_large() */
994 /* This function passes sense keys from firmware to scsi layer */
995 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
997 TW_Command_Full *full_command_packet;
998 unsigned short error;
1002 full_command_packet = tw_dev->command_packet_virt[request_id];
1004 /* Check for embedded error string */
1005 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
1007 /* Don't print error for Logical unit not supported during rollcall */
1008 error = le16_to_cpu(full_command_packet->header.status_block.error);
1009 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
1011 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1012 tw_dev->host->host_no,
1013 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1014 full_command_packet->header.status_block.error,
1015 error_str[0] == '\0' ?
1016 twa_string_lookup(twa_error_table,
1017 full_command_packet->header.status_block.error) : error_str,
1018 full_command_packet->header.err_specific_desc);
1020 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1021 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1022 full_command_packet->header.status_block.error,
1023 error_str[0] == '\0' ?
1024 twa_string_lookup(twa_error_table,
1025 full_command_packet->header.status_block.error) : error_str,
1026 full_command_packet->header.err_specific_desc);
1030 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1031 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1032 retval = TW_ISR_DONT_RESULT;
1038 } /* End twa_fill_sense() */
1040 /* This function will free up device extension resources */
1041 static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1043 if (tw_dev->command_packet_virt[0])
1044 pci_free_consistent(tw_dev->tw_pci_dev,
1045 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1046 tw_dev->command_packet_virt[0],
1047 tw_dev->command_packet_phys[0]);
1049 if (tw_dev->generic_buffer_virt[0])
1050 pci_free_consistent(tw_dev->tw_pci_dev,
1051 TW_SECTOR_SIZE*TW_Q_LENGTH,
1052 tw_dev->generic_buffer_virt[0],
1053 tw_dev->generic_buffer_phys[0]);
1055 kfree(tw_dev->event_queue[0]);
1056 } /* End twa_free_device_extension() */
1058 /* This function will free a request id */
1059 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1061 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1062 tw_dev->state[request_id] = TW_S_FINISHED;
1063 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1064 } /* End twa_free_request_id() */
1066 /* This function will get parameter table entries from the firmware */
1067 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1069 TW_Command_Full *full_command_packet;
1070 TW_Command *command_packet;
1071 TW_Param_Apache *param;
1072 void *retval = NULL;
1074 /* Setup the command packet */
1075 full_command_packet = tw_dev->command_packet_virt[request_id];
1076 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1077 command_packet = &full_command_packet->command.oldcommand;
1079 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1080 command_packet->size = TW_COMMAND_SIZE;
1081 command_packet->request_id = request_id;
1082 command_packet->byte6_offset.block_count = cpu_to_le16(1);
1084 /* Now setup the param */
1085 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1086 memset(param, 0, TW_SECTOR_SIZE);
1087 param->table_id = cpu_to_le16(table_id | 0x8000);
1088 param->parameter_id = cpu_to_le16(parameter_id);
1089 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
1091 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1092 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1094 /* Post the command packet to the board */
1095 twa_post_command_packet(tw_dev, request_id, 1);
1097 /* Poll for completion */
1098 if (twa_poll_response(tw_dev, request_id, 30))
1099 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1101 retval = (void *)&(param->data[0]);
1103 tw_dev->posted_request_count--;
1104 tw_dev->state[request_id] = TW_S_INITIAL;
1107 } /* End twa_get_param() */
1109 /* This function will assign an available request id */
1110 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1112 *request_id = tw_dev->free_queue[tw_dev->free_head];
1113 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1114 tw_dev->state[*request_id] = TW_S_STARTED;
1115 } /* End twa_get_request_id() */
1117 /* This function will send an initconnection command to controller */
1118 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1119 u32 set_features, unsigned short current_fw_srl,
1120 unsigned short current_fw_arch_id,
1121 unsigned short current_fw_branch,
1122 unsigned short current_fw_build,
1123 unsigned short *fw_on_ctlr_srl,
1124 unsigned short *fw_on_ctlr_arch_id,
1125 unsigned short *fw_on_ctlr_branch,
1126 unsigned short *fw_on_ctlr_build,
1127 u32 *init_connect_result)
1129 TW_Command_Full *full_command_packet;
1130 TW_Initconnect *tw_initconnect;
1131 int request_id = 0, retval = 1;
1133 /* Initialize InitConnection command packet */
1134 full_command_packet = tw_dev->command_packet_virt[request_id];
1135 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1136 full_command_packet->header.header_desc.size_header = 128;
1138 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1139 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1140 tw_initconnect->request_id = request_id;
1141 tw_initconnect->message_credits = cpu_to_le16(message_credits);
1142 tw_initconnect->features = set_features;
1144 /* Turn on 64-bit sgl support if we need to */
1145 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1147 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1149 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1150 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1151 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1152 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1153 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1154 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1156 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1158 /* Send command packet to the board */
1159 twa_post_command_packet(tw_dev, request_id, 1);
1161 /* Poll for completion */
1162 if (twa_poll_response(tw_dev, request_id, 30)) {
1163 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1165 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1166 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1167 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1168 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1169 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1170 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1175 tw_dev->posted_request_count--;
1176 tw_dev->state[request_id] = TW_S_INITIAL;
1179 } /* End twa_initconnection() */
1181 /* This function will initialize the fields of a device extension */
1182 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1186 /* Initialize command packet buffers */
1187 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1188 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1192 /* Initialize generic buffer */
1193 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1194 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1198 /* Allocate event info space */
1199 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1200 if (!tw_dev->event_queue[0]) {
1201 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1206 for (i = 0; i < TW_Q_LENGTH; i++) {
1207 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1208 tw_dev->free_queue[i] = i;
1209 tw_dev->state[i] = TW_S_INITIAL;
1212 tw_dev->pending_head = TW_Q_START;
1213 tw_dev->pending_tail = TW_Q_START;
1214 tw_dev->free_head = TW_Q_START;
1215 tw_dev->free_tail = TW_Q_START;
1216 tw_dev->error_sequence_id = 1;
1217 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1219 mutex_init(&tw_dev->ioctl_lock);
1220 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1225 } /* End twa_initialize_device_extension() */
1227 /* This function is the interrupt service routine */
1228 static irqreturn_t twa_interrupt(int irq, void *dev_instance)
1230 int request_id, error = 0;
1231 u32 status_reg_value;
1232 TW_Response_Queue response_que;
1233 TW_Command_Full *full_command_packet;
1234 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1237 /* Get the per adapter lock */
1238 spin_lock(tw_dev->host->host_lock);
1240 /* Read the registers */
1241 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1243 /* Check if this is our interrupt, otherwise bail */
1244 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1245 goto twa_interrupt_bail;
1249 /* If we are resetting, bail */
1250 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1251 goto twa_interrupt_bail;
1253 /* Check controller for errors */
1254 if (twa_check_bits(status_reg_value)) {
1255 if (twa_decode_bits(tw_dev, status_reg_value)) {
1256 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1257 goto twa_interrupt_bail;
1261 /* Handle host interrupt */
1262 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1263 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1265 /* Handle attention interrupt */
1266 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1267 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1268 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1269 twa_get_request_id(tw_dev, &request_id);
1271 error = twa_aen_read_queue(tw_dev, request_id);
1273 tw_dev->state[request_id] = TW_S_COMPLETED;
1274 twa_free_request_id(tw_dev, request_id);
1275 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1280 /* Handle command interrupt */
1281 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1282 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1283 /* Drain as many pending commands as we can */
1284 while (tw_dev->pending_request_count > 0) {
1285 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1286 if (tw_dev->state[request_id] != TW_S_PENDING) {
1287 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1288 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1289 goto twa_interrupt_bail;
1291 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1292 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1293 tw_dev->pending_request_count--;
1295 /* If we get here, we will continue re-posting on the next command interrupt */
1301 /* Handle response interrupt */
1302 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1304 /* Drain the response queue from the board */
1305 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1306 /* Complete the response */
1307 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1308 request_id = TW_RESID_OUT(response_que.response_id);
1309 full_command_packet = tw_dev->command_packet_virt[request_id];
1311 /* Check for command packet errors */
1312 if (full_command_packet->command.newcommand.status != 0) {
1313 if (tw_dev->srb[request_id] != NULL) {
1314 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1316 /* Skip ioctl error prints */
1317 if (request_id != tw_dev->chrdev_request_id) {
1318 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1323 /* Check for correct state */
1324 if (tw_dev->state[request_id] != TW_S_POSTED) {
1325 if (tw_dev->srb[request_id] != NULL) {
1326 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1327 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1328 goto twa_interrupt_bail;
1332 /* Check for internal command completion */
1333 if (tw_dev->srb[request_id] == NULL) {
1334 if (request_id != tw_dev->chrdev_request_id) {
1335 if (twa_aen_complete(tw_dev, request_id))
1336 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1338 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1339 wake_up(&tw_dev->ioctl_wqueue);
1342 struct scsi_cmnd *cmd;
1344 cmd = tw_dev->srb[request_id];
1346 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1347 /* If no error command was a success */
1349 cmd->result = (DID_OK << 16);
1352 /* If error, command failed */
1354 /* Ask for a host reset */
1355 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1358 /* Report residual bytes for single sgl */
1359 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1360 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1361 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1364 /* Now complete the io */
1365 if (twa_command_mapped(cmd))
1366 scsi_dma_unmap(cmd);
1367 cmd->scsi_done(cmd);
1368 tw_dev->state[request_id] = TW_S_COMPLETED;
1369 twa_free_request_id(tw_dev, request_id);
1370 tw_dev->posted_request_count--;
1373 /* Check for valid status after each drain */
1374 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1375 if (twa_check_bits(status_reg_value)) {
1376 if (twa_decode_bits(tw_dev, status_reg_value)) {
1377 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1378 goto twa_interrupt_bail;
1385 spin_unlock(tw_dev->host->host_lock);
1386 return IRQ_RETVAL(handled);
1387 } /* End twa_interrupt() */
1389 /* This function will load the request id and various sgls for ioctls */
1390 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)
1392 TW_Command *oldcommand;
1393 TW_Command_Apache *newcommand;
1395 unsigned int pae = 0;
1397 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1400 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1401 newcommand = &full_command_packet->command.newcommand;
1402 newcommand->request_id__lunl =
1403 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
1405 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1406 newcommand->sg_list[0].length = cpu_to_le32(length);
1408 newcommand->sgl_entries__lunh =
1409 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
1411 oldcommand = &full_command_packet->command.oldcommand;
1412 oldcommand->request_id = request_id;
1414 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1415 /* Load the sg list */
1416 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1417 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1419 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1420 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1421 sgl->length = cpu_to_le32(length);
1423 oldcommand->size += pae;
1426 } /* End twa_load_sgl() */
1428 /* This function will poll for a response interrupt of a request */
1429 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1431 int retval = 1, found = 0, response_request_id;
1432 TW_Response_Queue response_queue;
1433 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1435 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1436 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1437 response_request_id = TW_RESID_OUT(response_queue.response_id);
1438 if (request_id != response_request_id) {
1439 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1442 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1443 if (full_command_packet->command.newcommand.status != 0) {
1445 twa_fill_sense(tw_dev, request_id, 0, 0);
1450 if (full_command_packet->command.oldcommand.status != 0) {
1452 twa_fill_sense(tw_dev, request_id, 0, 0);
1463 } /* End twa_poll_response() */
1465 /* This function will poll the status register for a flag */
1466 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1468 u32 status_reg_value;
1469 unsigned long before;
1472 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1475 if (twa_check_bits(status_reg_value))
1476 twa_decode_bits(tw_dev, status_reg_value);
1478 while ((status_reg_value & flag) != flag) {
1479 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1481 if (twa_check_bits(status_reg_value))
1482 twa_decode_bits(tw_dev, status_reg_value);
1484 if (time_after(jiffies, before + HZ * seconds))
1492 } /* End twa_poll_status() */
1494 /* This function will poll the status register for disappearance of a flag */
1495 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1497 u32 status_reg_value;
1498 unsigned long before;
1501 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1504 if (twa_check_bits(status_reg_value))
1505 twa_decode_bits(tw_dev, status_reg_value);
1507 while ((status_reg_value & flag) != 0) {
1508 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1509 if (twa_check_bits(status_reg_value))
1510 twa_decode_bits(tw_dev, status_reg_value);
1512 if (time_after(jiffies, before + HZ * seconds))
1520 } /* End twa_poll_status_gone() */
1522 /* This function will attempt to post a command packet to the board */
1523 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1525 u32 status_reg_value;
1526 dma_addr_t command_que_value;
1529 command_que_value = tw_dev->command_packet_phys[request_id];
1531 /* For 9650SE write low 4 bytes first */
1532 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1533 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1534 command_que_value += TW_COMMAND_OFFSET;
1535 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1538 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1540 if (twa_check_bits(status_reg_value))
1541 twa_decode_bits(tw_dev, status_reg_value);
1543 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1545 /* Only pend internal driver commands */
1547 retval = SCSI_MLQUEUE_HOST_BUSY;
1551 /* Couldn't post the command packet, so we do it later */
1552 if (tw_dev->state[request_id] != TW_S_PENDING) {
1553 tw_dev->state[request_id] = TW_S_PENDING;
1554 tw_dev->pending_request_count++;
1555 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1556 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1558 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1559 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1561 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1564 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1565 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1566 /* Now write upper 4 bytes */
1567 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1569 if (sizeof(dma_addr_t) > 4) {
1570 command_que_value += TW_COMMAND_OFFSET;
1571 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1572 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1574 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1577 tw_dev->state[request_id] = TW_S_POSTED;
1578 tw_dev->posted_request_count++;
1579 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1580 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1586 } /* End twa_post_command_packet() */
1588 /* This function will reset a device extension */
1589 static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1593 unsigned long flags = 0;
1595 set_bit(TW_IN_RESET, &tw_dev->flags);
1596 TW_DISABLE_INTERRUPTS(tw_dev);
1597 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1598 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1600 /* Abort all requests that are in progress */
1601 for (i = 0; i < TW_Q_LENGTH; i++) {
1602 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1603 (tw_dev->state[i] != TW_S_INITIAL) &&
1604 (tw_dev->state[i] != TW_S_COMPLETED)) {
1605 if (tw_dev->srb[i]) {
1606 struct scsi_cmnd *cmd = tw_dev->srb[i];
1608 cmd->result = (DID_RESET << 16);
1609 if (twa_command_mapped(cmd))
1610 scsi_dma_unmap(cmd);
1611 cmd->scsi_done(cmd);
1616 /* Reset queues and counts */
1617 for (i = 0; i < TW_Q_LENGTH; i++) {
1618 tw_dev->free_queue[i] = i;
1619 tw_dev->state[i] = TW_S_INITIAL;
1621 tw_dev->free_head = TW_Q_START;
1622 tw_dev->free_tail = TW_Q_START;
1623 tw_dev->posted_request_count = 0;
1624 tw_dev->pending_request_count = 0;
1625 tw_dev->pending_head = TW_Q_START;
1626 tw_dev->pending_tail = TW_Q_START;
1627 tw_dev->reset_print = 0;
1629 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1631 if (twa_reset_sequence(tw_dev, 1))
1634 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1635 clear_bit(TW_IN_RESET, &tw_dev->flags);
1636 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1641 } /* End twa_reset_device_extension() */
1643 /* This function will reset a controller */
1644 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1646 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1648 while (tries < TW_MAX_RESET_TRIES) {
1649 if (do_soft_reset) {
1650 TW_SOFT_RESET(tw_dev);
1651 /* Clear pchip/response queue on 9550SX */
1652 if (twa_empty_response_queue_large(tw_dev)) {
1653 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1660 /* Make sure controller is in a good state */
1661 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1662 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1668 /* Empty response queue */
1669 if (twa_empty_response_queue(tw_dev)) {
1670 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1678 /* Check for compatibility/flash */
1679 if (twa_check_srl(tw_dev, &flashed)) {
1680 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1691 /* Drain the AEN queue */
1692 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1693 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1699 /* If we got here, controller is in a good state */
1705 } /* End twa_reset_sequence() */
1707 /* This funciton returns unit geometry in cylinders/heads/sectors */
1708 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1710 int heads, sectors, cylinders;
1711 TW_Device_Extension *tw_dev;
1713 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1715 if (capacity >= 0x200000) {
1718 cylinders = sector_div(capacity, heads * sectors);
1722 cylinders = sector_div(capacity, heads * sectors);
1727 geom[2] = cylinders;
1730 } /* End twa_scsi_biosparam() */
1732 /* This is the new scsi eh reset function */
1733 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1735 TW_Device_Extension *tw_dev = NULL;
1736 int retval = FAILED;
1738 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1740 tw_dev->num_resets++;
1742 sdev_printk(KERN_WARNING, SCpnt->device,
1743 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1744 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1746 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1747 mutex_lock(&tw_dev->ioctl_lock);
1749 /* Now reset the card and some of the device extension data */
1750 if (twa_reset_device_extension(tw_dev)) {
1751 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1757 mutex_unlock(&tw_dev->ioctl_lock);
1759 } /* End twa_scsi_eh_reset() */
1761 /* This is the main scsi queue function to handle scsi opcodes */
1762 static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1764 int request_id, retval;
1765 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1767 /* If we are resetting due to timed out ioctl, report as busy */
1768 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1769 retval = SCSI_MLQUEUE_HOST_BUSY;
1773 /* Check if this FW supports luns */
1774 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1775 SCpnt->result = (DID_BAD_TARGET << 16);
1781 /* Save done function into scsi_cmnd struct */
1782 SCpnt->scsi_done = done;
1784 /* Get a free request id */
1785 twa_get_request_id(tw_dev, &request_id);
1787 /* Save the scsi command for use by the ISR */
1788 tw_dev->srb[request_id] = SCpnt;
1790 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1792 case SCSI_MLQUEUE_HOST_BUSY:
1793 if (twa_command_mapped(SCpnt))
1794 scsi_dma_unmap(SCpnt);
1795 twa_free_request_id(tw_dev, request_id);
1798 SCpnt->result = (DID_ERROR << 16);
1799 if (twa_command_mapped(SCpnt))
1800 scsi_dma_unmap(SCpnt);
1802 tw_dev->state[request_id] = TW_S_COMPLETED;
1803 twa_free_request_id(tw_dev, request_id);
1808 } /* End twa_scsi_queue() */
1810 static DEF_SCSI_QCMD(twa_scsi_queue)
1812 /* This function hands scsi cdb's to the firmware */
1813 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1815 TW_Command_Full *full_command_packet;
1816 TW_Command_Apache *command_packet;
1817 u32 num_sectors = 0x0;
1819 struct scsi_cmnd *srb = NULL;
1820 struct scatterlist *sglist = NULL, *sg;
1823 if (tw_dev->srb[request_id]) {
1824 srb = tw_dev->srb[request_id];
1825 if (scsi_sglist(srb))
1826 sglist = scsi_sglist(srb);
1829 /* Initialize command packet */
1830 full_command_packet = tw_dev->command_packet_virt[request_id];
1831 full_command_packet->header.header_desc.size_header = 128;
1832 full_command_packet->header.status_block.error = 0;
1833 full_command_packet->header.status_block.severity__reserved = 0;
1835 command_packet = &full_command_packet->command.newcommand;
1836 command_packet->status = 0;
1837 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1839 /* We forced 16 byte cdb use earlier */
1841 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1843 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1846 command_packet->unit = srb->device->id;
1847 command_packet->request_id__lunl =
1848 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1850 command_packet->request_id__lunl =
1851 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1852 command_packet->unit = 0;
1855 command_packet->sgl_offset = 16;
1858 /* Map sglist from scsi layer to cmd packet */
1860 if (scsi_sg_count(srb)) {
1861 if (!twa_command_mapped(srb)) {
1862 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1863 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1864 scsi_sg_copy_to_buffer(srb,
1865 tw_dev->generic_buffer_virt[request_id],
1867 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1868 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1870 sg_count = scsi_dma_map(srb);
1874 scsi_for_each_sg(srb, sg, sg_count, i) {
1875 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1876 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
1877 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1878 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1883 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1886 /* Internal cdb post */
1887 for (i = 0; i < use_sg; i++) {
1888 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1889 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1890 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1891 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1895 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1899 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1900 num_sectors = (u32)srb->cmnd[4];
1902 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1903 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1906 /* Update sector statistic */
1907 tw_dev->sector_count = num_sectors;
1908 if (tw_dev->sector_count > tw_dev->max_sector_count)
1909 tw_dev->max_sector_count = tw_dev->sector_count;
1911 /* Update SG statistics */
1913 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1914 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1915 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1918 /* Now post the command to the board */
1920 retval = twa_post_command_packet(tw_dev, request_id, 0);
1922 twa_post_command_packet(tw_dev, request_id, 1);
1927 } /* End twa_scsiop_execute_scsi() */
1929 /* This function completes an execute scsi operation */
1930 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1932 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1934 if (!twa_command_mapped(cmd) &&
1935 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1936 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
1937 if (scsi_sg_count(cmd) == 1) {
1938 void *buf = tw_dev->generic_buffer_virt[request_id];
1940 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
1943 } /* End twa_scsiop_execute_scsi_complete() */
1945 /* This function tells the controller to shut down */
1946 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1948 /* Disable interrupts */
1949 TW_DISABLE_INTERRUPTS(tw_dev);
1951 /* Free up the IRQ */
1952 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1954 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1956 /* Tell the card we are shutting down */
1957 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1958 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1960 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1963 /* Clear all interrupts just before exit */
1964 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1965 } /* End __twa_shutdown() */
1967 /* Wrapper for __twa_shutdown */
1968 static void twa_shutdown(struct pci_dev *pdev)
1970 struct Scsi_Host *host = pci_get_drvdata(pdev);
1971 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1973 __twa_shutdown(tw_dev);
1974 } /* End twa_shutdown() */
1976 /* This function will look up a string */
1977 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1981 for (index = 0; ((code != table[index].code) &&
1982 (table[index].text != (char *)0)); index++);
1983 return(table[index].text);
1984 } /* End twa_string_lookup() */
1986 /* This function gets called when a disk is coming on-line */
1987 static int twa_slave_configure(struct scsi_device *sdev)
1989 /* Force 60 second timeout */
1990 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1993 } /* End twa_slave_configure() */
1995 /* scsi_host_template initializer */
1996 static struct scsi_host_template driver_template = {
1997 .module = THIS_MODULE,
1998 .name = "3ware 9000 Storage Controller",
1999 .queuecommand = twa_scsi_queue,
2000 .eh_host_reset_handler = twa_scsi_eh_reset,
2001 .bios_param = twa_scsi_biosparam,
2002 .change_queue_depth = twa_change_queue_depth,
2003 .can_queue = TW_Q_LENGTH-2,
2004 .slave_configure = twa_slave_configure,
2006 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
2007 .max_sectors = TW_MAX_SECTORS,
2008 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
2009 .use_clustering = ENABLE_CLUSTERING,
2010 .shost_attrs = twa_host_attrs,
2014 /* This function will probe and initialize a card */
2015 static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2017 struct Scsi_Host *host = NULL;
2018 TW_Device_Extension *tw_dev;
2019 unsigned long mem_addr, mem_len;
2020 int retval = -ENODEV;
2022 retval = pci_enable_device(pdev);
2024 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2025 goto out_disable_device;
2028 pci_set_master(pdev);
2029 pci_try_set_mwi(pdev);
2031 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2032 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2033 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2034 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2035 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2037 goto out_disable_device;
2040 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2042 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2044 goto out_disable_device;
2046 tw_dev = (TW_Device_Extension *)host->hostdata;
2048 /* Save values to device extension */
2049 tw_dev->host = host;
2050 tw_dev->tw_pci_dev = pdev;
2052 if (twa_initialize_device_extension(tw_dev)) {
2053 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2054 goto out_free_device_extension;
2057 /* Request IO regions */
2058 retval = pci_request_regions(pdev, "3w-9xxx");
2060 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2061 goto out_free_device_extension;
2064 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
2065 mem_addr = pci_resource_start(pdev, 1);
2066 mem_len = pci_resource_len(pdev, 1);
2068 mem_addr = pci_resource_start(pdev, 2);
2069 mem_len = pci_resource_len(pdev, 2);
2072 /* Save base address */
2073 tw_dev->base_addr = ioremap(mem_addr, mem_len);
2074 if (!tw_dev->base_addr) {
2075 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2076 goto out_release_mem_region;
2079 /* Disable interrupts on the card */
2080 TW_DISABLE_INTERRUPTS(tw_dev);
2082 /* Initialize the card */
2083 if (twa_reset_sequence(tw_dev, 0))
2086 /* Set host specific parameters */
2087 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2088 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
2089 host->max_id = TW_MAX_UNITS_9650SE;
2091 host->max_id = TW_MAX_UNITS;
2093 host->max_cmd_len = TW_MAX_CDB_LEN;
2095 /* Channels aren't supported by adapter */
2096 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
2097 host->max_channel = 0;
2099 /* Register the card with the kernel SCSI layer */
2100 retval = scsi_add_host(host, &pdev->dev);
2102 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2106 pci_set_drvdata(pdev, host);
2108 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2109 host->host_no, mem_addr, pdev->irq);
2110 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2112 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2113 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2114 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2115 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2116 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2117 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
2119 /* Try to enable MSI */
2120 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2121 !pci_enable_msi(pdev))
2122 set_bit(TW_USING_MSI, &tw_dev->flags);
2124 /* Now setup the interrupt handler */
2125 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2127 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2128 goto out_remove_host;
2131 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2132 twa_device_extension_count++;
2134 /* Re-enable interrupts on the card */
2135 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2137 /* Finally, scan the host */
2138 scsi_scan_host(host);
2140 if (twa_major == -1) {
2141 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2142 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2147 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2148 pci_disable_msi(pdev);
2149 scsi_remove_host(host);
2151 iounmap(tw_dev->base_addr);
2152 out_release_mem_region:
2153 pci_release_regions(pdev);
2154 out_free_device_extension:
2155 twa_free_device_extension(tw_dev);
2156 scsi_host_put(host);
2158 pci_disable_device(pdev);
2161 } /* End twa_probe() */
2163 /* This function is called to remove a device */
2164 static void twa_remove(struct pci_dev *pdev)
2166 struct Scsi_Host *host = pci_get_drvdata(pdev);
2167 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2169 scsi_remove_host(tw_dev->host);
2171 /* Unregister character device */
2172 if (twa_major >= 0) {
2173 unregister_chrdev(twa_major, "twa");
2177 /* Shutdown the card */
2178 __twa_shutdown(tw_dev);
2180 /* Disable MSI if enabled */
2181 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2182 pci_disable_msi(pdev);
2184 /* Free IO remapping */
2185 iounmap(tw_dev->base_addr);
2187 /* Free up the mem region */
2188 pci_release_regions(pdev);
2190 /* Free up device extension resources */
2191 twa_free_device_extension(tw_dev);
2193 scsi_host_put(tw_dev->host);
2194 pci_disable_device(pdev);
2195 twa_device_extension_count--;
2196 } /* End twa_remove() */
2199 /* This function is called on PCI suspend */
2200 static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
2202 struct Scsi_Host *host = pci_get_drvdata(pdev);
2203 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2205 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2207 TW_DISABLE_INTERRUPTS(tw_dev);
2208 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2210 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2211 pci_disable_msi(pdev);
2213 /* Tell the card we are shutting down */
2214 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2215 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2217 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2219 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2221 pci_save_state(pdev);
2222 pci_disable_device(pdev);
2223 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2226 } /* End twa_suspend() */
2228 /* This function is called on PCI resume */
2229 static int twa_resume(struct pci_dev *pdev)
2232 struct Scsi_Host *host = pci_get_drvdata(pdev);
2233 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2235 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2236 pci_set_power_state(pdev, PCI_D0);
2237 pci_enable_wake(pdev, PCI_D0, 0);
2238 pci_restore_state(pdev);
2240 retval = pci_enable_device(pdev);
2242 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2246 pci_set_master(pdev);
2247 pci_try_set_mwi(pdev);
2249 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2250 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2251 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2252 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2253 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2255 goto out_disable_device;
2258 /* Initialize the card */
2259 if (twa_reset_sequence(tw_dev, 0)) {
2261 goto out_disable_device;
2264 /* Now setup the interrupt handler */
2265 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2267 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2269 goto out_disable_device;
2272 /* Now enable MSI if enabled */
2273 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2274 pci_enable_msi(pdev);
2276 /* Re-enable interrupts on the card */
2277 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2279 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2283 scsi_remove_host(host);
2284 pci_disable_device(pdev);
2287 } /* End twa_resume() */
2290 /* PCI Devices supported by this driver */
2291 static struct pci_device_id twa_pci_tbl[] __devinitdata = {
2292 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2293 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2294 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2295 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2296 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2297 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2298 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2299 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2302 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2304 /* pci_driver initializer */
2305 static struct pci_driver twa_driver = {
2307 .id_table = twa_pci_tbl,
2309 .remove = twa_remove,
2311 .suspend = twa_suspend,
2312 .resume = twa_resume,
2314 .shutdown = twa_shutdown
2317 /* This function is called on driver initialization */
2318 static int __init twa_init(void)
2320 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2322 return pci_register_driver(&twa_driver);
2323 } /* End twa_init() */
2325 /* This function is called on driver exit */
2326 static void __exit twa_exit(void)
2328 pci_unregister_driver(&twa_driver);
2329 } /* End twa_exit() */
2331 module_init(twa_init);
2332 module_exit(twa_exit);