2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
4 Written By: Adam Radford <linuxraid@amcc.com>
5 Modifications By: Tom Couch <linuxraid@amcc.com>
7 Copyright (C) 2004-2008 Applied Micro Circuits Corporation.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 solely responsible for determining the appropriateness of using and
24 distributing the Program and assumes all risks associated with its
25 exercise of rights under this Agreement, including but not limited to
26 the risks and costs of program errors, damage to or loss of data,
27 programs or equipment, and unavailability or interruption of operations.
29 DISCLAIMER OF LIABILITY
30 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 You should have received a copy of the GNU General Public License
39 along with this program; if not, write to the Free Software
40 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 Bugs/Comments/Suggestions should be mailed to:
45 For more information, goto:
48 Note: This version of the driver does not contain a bundled firmware
53 2.26.02.000 - Driver cleanup for kernel submission.
54 2.26.02.001 - Replace schedule_timeout() calls with msleep().
55 2.26.02.002 - Add support for PAE mode.
57 Fix twa_remove() to free irq handler/unregister_chrdev()
58 before shutting down card.
59 Change to new 'change_queue_depth' api.
60 Fix 'handled=1' ISR usage, remove bogus IRQ check.
61 Remove un-needed eh_abort handler.
62 Add support for embedded firmware error strings.
63 2.26.02.003 - Correctly handle single sgl's with use_sg=1.
64 2.26.02.004 - Add support for 9550SX controllers.
65 2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
66 2.26.02.006 - Fix 9550SX pchip reset timeout.
67 Add big endian support.
68 2.26.02.007 - Disable local interrupts during kmap/unmap_atomic().
69 2.26.02.008 - Free irq handler in __twa_shutdown().
71 Add support for 9650SE controllers.
72 2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails.
73 2.26.02.010 - Add support for 9690SA controllers.
74 2.26.02.011 - Increase max AENs drained to 256.
75 Add MSI support and "use_msi" module parameter.
76 Fix bug in twa_get_param() on 4GB+.
77 Use pci_resource_len() for ioremap().
80 #include <linux/module.h>
81 #include <linux/reboot.h>
82 #include <linux/spinlock.h>
83 #include <linux/interrupt.h>
84 #include <linux/moduleparam.h>
85 #include <linux/errno.h>
86 #include <linux/types.h>
87 #include <linux/delay.h>
88 #include <linux/pci.h>
89 #include <linux/time.h>
90 #include <linux/mutex.h>
91 #include <linux/smp_lock.h>
94 #include <asm/uaccess.h>
95 #include <scsi/scsi.h>
96 #include <scsi/scsi_host.h>
97 #include <scsi/scsi_tcq.h>
98 #include <scsi/scsi_cmnd.h>
102 #define TW_DRIVER_VERSION "2.26.02.011"
103 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
104 static unsigned int twa_device_extension_count;
105 static int twa_major = -1;
106 extern struct timezone sys_tz;
108 /* Module parameters */
109 MODULE_AUTHOR ("AMCC");
110 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
111 MODULE_LICENSE("GPL");
112 MODULE_VERSION(TW_DRIVER_VERSION);
114 static int use_msi = 0;
115 module_param(use_msi, int, S_IRUGO);
116 MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
118 /* Function prototypes */
119 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
120 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
121 static char *twa_aen_severity_lookup(unsigned char severity_code);
122 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
123 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
124 static int twa_chrdev_open(struct inode *inode, struct file *file);
125 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
126 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
127 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
128 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
129 u32 set_features, unsigned short current_fw_srl,
130 unsigned short current_fw_arch_id,
131 unsigned short current_fw_branch,
132 unsigned short current_fw_build,
133 unsigned short *fw_on_ctlr_srl,
134 unsigned short *fw_on_ctlr_arch_id,
135 unsigned short *fw_on_ctlr_branch,
136 unsigned short *fw_on_ctlr_build,
137 u32 *init_connect_result);
138 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);
139 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
140 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
141 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
142 static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
143 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
144 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg);
145 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
146 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
147 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id);
151 /* Show some statistics about the card */
152 static ssize_t twa_show_stats(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct Scsi_Host *host = class_to_shost(dev);
156 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
157 unsigned long flags = 0;
160 spin_lock_irqsave(tw_dev->host->host_lock, flags);
161 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
162 "Current commands posted: %4d\n"
163 "Max commands posted: %4d\n"
164 "Current pending commands: %4d\n"
165 "Max pending commands: %4d\n"
166 "Last sgl length: %4d\n"
167 "Max sgl length: %4d\n"
168 "Last sector count: %4d\n"
169 "Max sector count: %4d\n"
170 "SCSI Host Resets: %4d\n"
173 tw_dev->posted_request_count,
174 tw_dev->max_posted_request_count,
175 tw_dev->pending_request_count,
176 tw_dev->max_pending_request_count,
178 tw_dev->max_sgl_entries,
179 tw_dev->sector_count,
180 tw_dev->max_sector_count,
183 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
185 } /* End twa_show_stats() */
187 /* This function will set a devices queue depth */
188 static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth)
190 if (queue_depth > TW_Q_LENGTH-2)
191 queue_depth = TW_Q_LENGTH-2;
192 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
194 } /* End twa_change_queue_depth() */
196 /* Create sysfs 'stats' entry */
197 static struct device_attribute twa_host_stats_attr = {
202 .show = twa_show_stats
205 /* Host attributes initializer */
206 static struct device_attribute *twa_host_attrs[] = {
207 &twa_host_stats_attr,
211 /* File operations struct for character device */
212 static const struct file_operations twa_fops = {
213 .owner = THIS_MODULE,
214 .ioctl = twa_chrdev_ioctl,
215 .open = twa_chrdev_open,
219 /* This function will complete an aen request from the isr */
220 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
222 TW_Command_Full *full_command_packet;
223 TW_Command *command_packet;
224 TW_Command_Apache_Header *header;
228 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
229 tw_dev->posted_request_count--;
230 aen = le16_to_cpu(header->status_block.error);
231 full_command_packet = tw_dev->command_packet_virt[request_id];
232 command_packet = &full_command_packet->command.oldcommand;
234 /* First check for internal completion of set param for time sync */
235 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
236 /* Keep reading the queue in case there are more aen's */
237 if (twa_aen_read_queue(tw_dev, request_id))
246 case TW_AEN_QUEUE_EMPTY:
247 /* Quit reading the queue if this is the last one */
249 case TW_AEN_SYNC_TIME_WITH_HOST:
250 twa_aen_sync_time(tw_dev, request_id);
254 twa_aen_queue_event(tw_dev, header);
256 /* If there are more aen's, keep reading the queue */
257 if (twa_aen_read_queue(tw_dev, request_id))
266 tw_dev->state[request_id] = TW_S_COMPLETED;
267 twa_free_request_id(tw_dev, request_id);
268 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
271 } /* End twa_aen_complete() */
273 /* This function will drain aen queue */
274 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
277 char cdb[TW_MAX_CDB_LEN];
278 TW_SG_Entry sglist[1];
279 int finished = 0, count = 0;
280 TW_Command_Full *full_command_packet;
281 TW_Command_Apache_Header *header;
283 int first_reset = 0, queue = 0, retval = 1;
290 full_command_packet = tw_dev->command_packet_virt[request_id];
291 memset(full_command_packet, 0, sizeof(TW_Command_Full));
294 memset(&cdb, 0, TW_MAX_CDB_LEN);
295 cdb[0] = REQUEST_SENSE; /* opcode */
296 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
298 /* Initialize sglist */
299 memset(&sglist, 0, sizeof(TW_SG_Entry));
300 sglist[0].length = TW_SECTOR_SIZE;
301 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
303 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
304 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
308 /* Mark internal command */
309 tw_dev->srb[request_id] = NULL;
312 /* Send command to the board */
313 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
314 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
318 /* Now poll for completion */
319 if (twa_poll_response(tw_dev, request_id, 30)) {
320 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
321 tw_dev->posted_request_count--;
325 tw_dev->posted_request_count--;
326 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
327 aen = le16_to_cpu(header->status_block.error);
332 case TW_AEN_QUEUE_EMPTY:
333 if (first_reset != 1)
338 case TW_AEN_SOFT_RESET:
339 if (first_reset == 0)
344 case TW_AEN_SYNC_TIME_WITH_HOST:
350 /* Now queue an event info */
352 twa_aen_queue_event(tw_dev, header);
353 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
355 if (count == TW_MAX_AEN_DRAIN)
360 tw_dev->state[request_id] = TW_S_INITIAL;
362 } /* End twa_aen_drain_queue() */
364 /* This function will queue an event */
365 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
376 /* Fill out event info */
377 event = tw_dev->event_queue[tw_dev->error_index];
379 /* Check for clobber */
382 sprintf(host, " scsi%d:", tw_dev->host->host_no);
383 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
384 tw_dev->aen_clobber = 1;
387 aen = le16_to_cpu(header->status_block.error);
388 memset(event, 0, sizeof(TW_Event));
390 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
391 do_gettimeofday(&time);
392 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
393 event->time_stamp_sec = local_time;
394 event->aen_code = aen;
395 event->retrieved = TW_AEN_NOT_RETRIEVED;
396 event->sequence_id = tw_dev->error_sequence_id;
397 tw_dev->error_sequence_id++;
399 /* Check for embedded error string */
400 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
402 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
403 event->parameter_len = strlen(header->err_specific_desc);
404 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
405 if (event->severity != TW_AEN_SEVERITY_DEBUG)
406 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
408 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
409 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
410 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
411 header->err_specific_desc);
415 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
416 tw_dev->event_queue_wrapped = 1;
417 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
418 } /* End twa_aen_queue_event() */
420 /* This function will read the aen queue from the isr */
421 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
423 char cdb[TW_MAX_CDB_LEN];
424 TW_SG_Entry sglist[1];
425 TW_Command_Full *full_command_packet;
428 full_command_packet = tw_dev->command_packet_virt[request_id];
429 memset(full_command_packet, 0, sizeof(TW_Command_Full));
432 memset(&cdb, 0, TW_MAX_CDB_LEN);
433 cdb[0] = REQUEST_SENSE; /* opcode */
434 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
436 /* Initialize sglist */
437 memset(&sglist, 0, sizeof(TW_SG_Entry));
438 sglist[0].length = TW_SECTOR_SIZE;
439 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
441 /* Mark internal command */
442 tw_dev->srb[request_id] = NULL;
444 /* Now post the command packet */
445 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
446 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
452 } /* End twa_aen_read_queue() */
454 /* This function will look up an AEN severity string */
455 static char *twa_aen_severity_lookup(unsigned char severity_code)
459 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
460 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
463 retval = twa_aen_severity_table[severity_code];
466 } /* End twa_aen_severity_lookup() */
468 /* This function will sync firmware time with the host time */
469 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
473 TW_Command_Full *full_command_packet;
474 TW_Command *command_packet;
475 TW_Param_Apache *param;
478 /* Fill out the command packet */
479 full_command_packet = tw_dev->command_packet_virt[request_id];
480 memset(full_command_packet, 0, sizeof(TW_Command_Full));
481 command_packet = &full_command_packet->command.oldcommand;
482 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
483 command_packet->request_id = request_id;
484 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
485 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
486 command_packet->size = TW_COMMAND_SIZE;
487 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
489 /* Setup the param */
490 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
491 memset(param, 0, TW_SECTOR_SIZE);
492 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
493 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
494 param->parameter_size_bytes = cpu_to_le16(4);
496 /* Convert system time in UTC to local time seconds since last
498 do_gettimeofday(&utc);
499 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
500 schedulertime = local_time - (3 * 86400);
501 schedulertime = cpu_to_le32(schedulertime % 604800);
503 memcpy(param->data, &schedulertime, sizeof(u32));
505 /* Mark internal command */
506 tw_dev->srb[request_id] = NULL;
508 /* Now post the command */
509 twa_post_command_packet(tw_dev, request_id, 1);
510 } /* End twa_aen_sync_time() */
512 /* This function will allocate memory and check if it is correctly aligned */
513 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
516 dma_addr_t dma_handle;
517 unsigned long *cpu_addr;
520 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
522 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
526 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
527 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
528 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
532 memset(cpu_addr, 0, size*TW_Q_LENGTH);
534 for (i = 0; i < TW_Q_LENGTH; i++) {
537 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
538 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
541 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
542 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
549 } /* End twa_allocate_memory() */
551 /* This function will check the status register for unexpected bits */
552 static int twa_check_bits(u32 status_reg_value)
556 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
558 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
564 } /* End twa_check_bits() */
566 /* This function will check the srl and decide if we are compatible */
567 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
570 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
571 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
572 u32 init_connect_result = 0;
574 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
575 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
576 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
577 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
578 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
579 &fw_on_ctlr_build, &init_connect_result)) {
580 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
584 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
585 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
586 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
588 /* Try base mode compatibility */
589 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
590 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
591 TW_EXTENDED_INIT_CONNECT,
592 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
593 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
594 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
595 &fw_on_ctlr_branch, &fw_on_ctlr_build,
596 &init_connect_result)) {
597 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
600 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
601 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
602 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
604 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
608 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
609 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
610 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
613 /* Load rest of compatibility struct */
614 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
615 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
616 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
617 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
618 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
619 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
620 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
621 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
622 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
623 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
628 } /* End twa_check_srl() */
630 /* This function handles ioctl for the character device */
631 static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
634 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
635 dma_addr_t dma_handle;
637 unsigned int sequence_id = 0;
638 unsigned char event_index, start_index;
639 TW_Ioctl_Driver_Command driver_command;
640 TW_Ioctl_Buf_Apache *tw_ioctl;
642 TW_Command_Full *full_command_packet;
643 TW_Compatibility_Info *tw_compat_info;
645 struct timeval current_time;
647 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
648 int retval = TW_IOCTL_ERROR_OS_EFAULT;
649 void __user *argp = (void __user *)arg;
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 compatiblity 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);
871 } /* End twa_chrdev_ioctl() */
873 /* This function handles open for the character device */
874 /* NOTE that this function will race with remove. */
875 static int twa_chrdev_open(struct inode *inode, struct file *file)
877 unsigned int minor_number;
878 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));
1380 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1381 newcommand->sg_list[0].length = cpu_to_le32(length);
1382 newcommand->sgl_entries__lunh =
1383 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), 1));
1385 oldcommand = &full_command_packet->command.oldcommand;
1386 oldcommand->request_id = request_id;
1388 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1389 /* Load the sg list */
1390 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1391 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1393 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1394 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1395 sgl->length = cpu_to_le32(length);
1397 oldcommand->size += pae;
1400 } /* End twa_load_sgl() */
1402 /* This function will perform a pci-dma mapping for a scatter gather list */
1403 static int twa_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
1406 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1408 use_sg = scsi_dma_map(cmd);
1411 else if (use_sg < 0) {
1412 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to map scatter gather list");
1416 cmd->SCp.phase = TW_PHASE_SGLIST;
1417 cmd->SCp.have_data_in = use_sg;
1420 } /* End twa_map_scsi_sg_data() */
1422 /* This function will poll for a response interrupt of a request */
1423 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1425 int retval = 1, found = 0, response_request_id;
1426 TW_Response_Queue response_queue;
1427 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1429 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1430 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1431 response_request_id = TW_RESID_OUT(response_queue.response_id);
1432 if (request_id != response_request_id) {
1433 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1436 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1437 if (full_command_packet->command.newcommand.status != 0) {
1439 twa_fill_sense(tw_dev, request_id, 0, 0);
1444 if (full_command_packet->command.oldcommand.status != 0) {
1446 twa_fill_sense(tw_dev, request_id, 0, 0);
1457 } /* End twa_poll_response() */
1459 /* This function will poll the status register for a flag */
1460 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1462 u32 status_reg_value;
1463 unsigned long before;
1466 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1469 if (twa_check_bits(status_reg_value))
1470 twa_decode_bits(tw_dev, status_reg_value);
1472 while ((status_reg_value & flag) != flag) {
1473 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 if (time_after(jiffies, before + HZ * seconds))
1486 } /* End twa_poll_status() */
1488 /* This function will poll the status register for disappearance of a flag */
1489 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1491 u32 status_reg_value;
1492 unsigned long before;
1495 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1498 if (twa_check_bits(status_reg_value))
1499 twa_decode_bits(tw_dev, status_reg_value);
1501 while ((status_reg_value & flag) != 0) {
1502 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1503 if (twa_check_bits(status_reg_value))
1504 twa_decode_bits(tw_dev, status_reg_value);
1506 if (time_after(jiffies, before + HZ * seconds))
1514 } /* End twa_poll_status_gone() */
1516 /* This function will attempt to post a command packet to the board */
1517 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1519 u32 status_reg_value;
1520 dma_addr_t command_que_value;
1523 command_que_value = tw_dev->command_packet_phys[request_id];
1525 /* For 9650SE write low 4 bytes first */
1526 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1527 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1528 command_que_value += TW_COMMAND_OFFSET;
1529 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1532 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1534 if (twa_check_bits(status_reg_value))
1535 twa_decode_bits(tw_dev, status_reg_value);
1537 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1539 /* Only pend internal driver commands */
1541 retval = SCSI_MLQUEUE_HOST_BUSY;
1545 /* Couldn't post the command packet, so we do it later */
1546 if (tw_dev->state[request_id] != TW_S_PENDING) {
1547 tw_dev->state[request_id] = TW_S_PENDING;
1548 tw_dev->pending_request_count++;
1549 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1550 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1552 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1553 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1555 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1558 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1559 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1560 /* Now write upper 4 bytes */
1561 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1563 if (sizeof(dma_addr_t) > 4) {
1564 command_que_value += TW_COMMAND_OFFSET;
1565 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1566 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1568 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1571 tw_dev->state[request_id] = TW_S_POSTED;
1572 tw_dev->posted_request_count++;
1573 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1574 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1580 } /* End twa_post_command_packet() */
1582 /* This function will reset a device extension */
1583 static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1587 unsigned long flags = 0;
1589 set_bit(TW_IN_RESET, &tw_dev->flags);
1590 TW_DISABLE_INTERRUPTS(tw_dev);
1591 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1592 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1594 /* Abort all requests that are in progress */
1595 for (i = 0; i < TW_Q_LENGTH; i++) {
1596 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1597 (tw_dev->state[i] != TW_S_INITIAL) &&
1598 (tw_dev->state[i] != TW_S_COMPLETED)) {
1599 if (tw_dev->srb[i]) {
1600 tw_dev->srb[i]->result = (DID_RESET << 16);
1601 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1602 twa_unmap_scsi_data(tw_dev, i);
1607 /* Reset queues and counts */
1608 for (i = 0; i < TW_Q_LENGTH; i++) {
1609 tw_dev->free_queue[i] = i;
1610 tw_dev->state[i] = TW_S_INITIAL;
1612 tw_dev->free_head = TW_Q_START;
1613 tw_dev->free_tail = TW_Q_START;
1614 tw_dev->posted_request_count = 0;
1615 tw_dev->pending_request_count = 0;
1616 tw_dev->pending_head = TW_Q_START;
1617 tw_dev->pending_tail = TW_Q_START;
1618 tw_dev->reset_print = 0;
1620 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1622 if (twa_reset_sequence(tw_dev, 1))
1625 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1626 clear_bit(TW_IN_RESET, &tw_dev->flags);
1627 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1632 } /* End twa_reset_device_extension() */
1634 /* This function will reset a controller */
1635 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1637 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1639 while (tries < TW_MAX_RESET_TRIES) {
1640 if (do_soft_reset) {
1641 TW_SOFT_RESET(tw_dev);
1642 /* Clear pchip/response queue on 9550SX */
1643 if (twa_empty_response_queue_large(tw_dev)) {
1644 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1651 /* Make sure controller is in a good state */
1652 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1653 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1659 /* Empty response queue */
1660 if (twa_empty_response_queue(tw_dev)) {
1661 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1669 /* Check for compatibility/flash */
1670 if (twa_check_srl(tw_dev, &flashed)) {
1671 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1682 /* Drain the AEN queue */
1683 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1684 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1690 /* If we got here, controller is in a good state */
1696 } /* End twa_reset_sequence() */
1698 /* This funciton returns unit geometry in cylinders/heads/sectors */
1699 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1701 int heads, sectors, cylinders;
1702 TW_Device_Extension *tw_dev;
1704 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1706 if (capacity >= 0x200000) {
1709 cylinders = sector_div(capacity, heads * sectors);
1713 cylinders = sector_div(capacity, heads * sectors);
1718 geom[2] = cylinders;
1721 } /* End twa_scsi_biosparam() */
1723 /* This is the new scsi eh reset function */
1724 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1726 TW_Device_Extension *tw_dev = NULL;
1727 int retval = FAILED;
1729 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1731 tw_dev->num_resets++;
1733 sdev_printk(KERN_WARNING, SCpnt->device,
1734 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1735 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1737 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1738 mutex_lock(&tw_dev->ioctl_lock);
1740 /* Now reset the card and some of the device extension data */
1741 if (twa_reset_device_extension(tw_dev)) {
1742 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1748 mutex_unlock(&tw_dev->ioctl_lock);
1750 } /* End twa_scsi_eh_reset() */
1752 /* This is the main scsi queue function to handle scsi opcodes */
1753 static int twa_scsi_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1755 int request_id, retval;
1756 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1758 /* If we are resetting due to timed out ioctl, report as busy */
1759 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1760 retval = SCSI_MLQUEUE_HOST_BUSY;
1764 /* Check if this FW supports luns */
1765 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1766 SCpnt->result = (DID_BAD_TARGET << 16);
1772 /* Save done function into scsi_cmnd struct */
1773 SCpnt->scsi_done = done;
1775 /* Get a free request id */
1776 twa_get_request_id(tw_dev, &request_id);
1778 /* Save the scsi command for use by the ISR */
1779 tw_dev->srb[request_id] = SCpnt;
1781 /* Initialize phase to zero */
1782 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1784 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1786 case SCSI_MLQUEUE_HOST_BUSY:
1787 twa_free_request_id(tw_dev, request_id);
1790 tw_dev->state[request_id] = TW_S_COMPLETED;
1791 twa_free_request_id(tw_dev, request_id);
1792 SCpnt->result = (DID_ERROR << 16);
1798 } /* End twa_scsi_queue() */
1800 /* This function hands scsi cdb's to the firmware */
1801 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1803 TW_Command_Full *full_command_packet;
1804 TW_Command_Apache *command_packet;
1805 u32 num_sectors = 0x0;
1807 struct scsi_cmnd *srb = NULL;
1808 struct scatterlist *sglist = NULL, *sg;
1811 if (tw_dev->srb[request_id]) {
1812 srb = tw_dev->srb[request_id];
1813 if (scsi_sglist(srb))
1814 sglist = scsi_sglist(srb);
1817 /* Initialize command packet */
1818 full_command_packet = tw_dev->command_packet_virt[request_id];
1819 full_command_packet->header.header_desc.size_header = 128;
1820 full_command_packet->header.status_block.error = 0;
1821 full_command_packet->header.status_block.severity__reserved = 0;
1823 command_packet = &full_command_packet->command.newcommand;
1824 command_packet->status = 0;
1825 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1827 /* We forced 16 byte cdb use earlier */
1829 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1831 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1834 command_packet->unit = srb->device->id;
1835 command_packet->request_id__lunl =
1836 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1838 command_packet->request_id__lunl =
1839 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1840 command_packet->unit = 0;
1843 command_packet->sgl_offset = 16;
1846 /* Map sglist from scsi layer to cmd packet */
1848 if (scsi_sg_count(srb)) {
1849 if ((scsi_sg_count(srb) == 1) &&
1850 (scsi_bufflen(srb) < TW_MIN_SGL_LENGTH)) {
1851 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1852 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1853 scsi_sg_copy_to_buffer(srb,
1854 tw_dev->generic_buffer_virt[request_id],
1856 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1857 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1859 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1863 scsi_for_each_sg(srb, sg, sg_count, i) {
1864 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1865 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
1866 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1867 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1872 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1875 /* Internal cdb post */
1876 for (i = 0; i < use_sg; i++) {
1877 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1878 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1879 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1880 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1884 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1888 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1889 num_sectors = (u32)srb->cmnd[4];
1891 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1892 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1895 /* Update sector statistic */
1896 tw_dev->sector_count = num_sectors;
1897 if (tw_dev->sector_count > tw_dev->max_sector_count)
1898 tw_dev->max_sector_count = tw_dev->sector_count;
1900 /* Update SG statistics */
1902 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1903 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1904 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1907 /* Now post the command to the board */
1909 retval = twa_post_command_packet(tw_dev, request_id, 0);
1911 twa_post_command_packet(tw_dev, request_id, 1);
1916 } /* End twa_scsiop_execute_scsi() */
1918 /* This function completes an execute scsi operation */
1919 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1921 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1923 if (scsi_bufflen(cmd) < TW_MIN_SGL_LENGTH &&
1924 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1925 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
1926 if (scsi_sg_count(cmd) == 1) {
1927 void *buf = tw_dev->generic_buffer_virt[request_id];
1929 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
1932 } /* End twa_scsiop_execute_scsi_complete() */
1934 /* This function tells the controller to shut down */
1935 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1937 /* Disable interrupts */
1938 TW_DISABLE_INTERRUPTS(tw_dev);
1940 /* Free up the IRQ */
1941 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1943 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1945 /* Tell the card we are shutting down */
1946 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1947 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1949 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1952 /* Clear all interrupts just before exit */
1953 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1954 } /* End __twa_shutdown() */
1956 /* Wrapper for __twa_shutdown */
1957 static void twa_shutdown(struct pci_dev *pdev)
1959 struct Scsi_Host *host = pci_get_drvdata(pdev);
1960 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1962 __twa_shutdown(tw_dev);
1963 } /* End twa_shutdown() */
1965 /* This function will look up a string */
1966 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1970 for (index = 0; ((code != table[index].code) &&
1971 (table[index].text != (char *)0)); index++);
1972 return(table[index].text);
1973 } /* End twa_string_lookup() */
1975 /* This function will perform a pci-dma unmap */
1976 static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1978 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1980 scsi_dma_unmap(cmd);
1981 } /* End twa_unmap_scsi_data() */
1983 /* scsi_host_template initializer */
1984 static struct scsi_host_template driver_template = {
1985 .module = THIS_MODULE,
1986 .name = "3ware 9000 Storage Controller",
1987 .queuecommand = twa_scsi_queue,
1988 .eh_host_reset_handler = twa_scsi_eh_reset,
1989 .bios_param = twa_scsi_biosparam,
1990 .change_queue_depth = twa_change_queue_depth,
1991 .can_queue = TW_Q_LENGTH-2,
1993 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
1994 .max_sectors = TW_MAX_SECTORS,
1995 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1996 .use_clustering = ENABLE_CLUSTERING,
1997 .shost_attrs = twa_host_attrs,
2001 /* This function will probe and initialize a card */
2002 static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2004 struct Scsi_Host *host = NULL;
2005 TW_Device_Extension *tw_dev;
2006 unsigned long mem_addr, mem_len;
2007 int retval = -ENODEV;
2009 retval = pci_enable_device(pdev);
2011 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2012 goto out_disable_device;
2015 pci_set_master(pdev);
2016 pci_try_set_mwi(pdev);
2018 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK)
2019 || pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
2020 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)
2021 || pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)) {
2022 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2024 goto out_disable_device;
2027 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2029 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2031 goto out_disable_device;
2033 tw_dev = (TW_Device_Extension *)host->hostdata;
2035 /* Save values to device extension */
2036 tw_dev->host = host;
2037 tw_dev->tw_pci_dev = pdev;
2039 if (twa_initialize_device_extension(tw_dev)) {
2040 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2041 goto out_free_device_extension;
2044 /* Request IO regions */
2045 retval = pci_request_regions(pdev, "3w-9xxx");
2047 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2048 goto out_free_device_extension;
2051 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
2052 mem_addr = pci_resource_start(pdev, 1);
2053 mem_len = pci_resource_len(pdev, 1);
2055 mem_addr = pci_resource_start(pdev, 2);
2056 mem_len = pci_resource_len(pdev, 2);
2059 /* Save base address */
2060 tw_dev->base_addr = ioremap(mem_addr, mem_len);
2061 if (!tw_dev->base_addr) {
2062 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2063 goto out_release_mem_region;
2066 /* Disable interrupts on the card */
2067 TW_DISABLE_INTERRUPTS(tw_dev);
2069 /* Initialize the card */
2070 if (twa_reset_sequence(tw_dev, 0))
2073 /* Set host specific parameters */
2074 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2075 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
2076 host->max_id = TW_MAX_UNITS_9650SE;
2078 host->max_id = TW_MAX_UNITS;
2080 host->max_cmd_len = TW_MAX_CDB_LEN;
2082 /* Channels aren't supported by adapter */
2083 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
2084 host->max_channel = 0;
2086 /* Register the card with the kernel SCSI layer */
2087 retval = scsi_add_host(host, &pdev->dev);
2089 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2093 pci_set_drvdata(pdev, host);
2095 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2096 host->host_no, mem_addr, pdev->irq);
2097 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2099 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2100 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2101 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2102 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2103 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2104 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
2106 /* Try to enable MSI */
2107 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2108 !pci_enable_msi(pdev))
2109 set_bit(TW_USING_MSI, &tw_dev->flags);
2111 /* Now setup the interrupt handler */
2112 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2114 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2115 goto out_remove_host;
2118 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2119 twa_device_extension_count++;
2121 /* Re-enable interrupts on the card */
2122 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2124 /* Finally, scan the host */
2125 scsi_scan_host(host);
2127 if (twa_major == -1) {
2128 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2129 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2134 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2135 pci_disable_msi(pdev);
2136 scsi_remove_host(host);
2138 iounmap(tw_dev->base_addr);
2139 out_release_mem_region:
2140 pci_release_regions(pdev);
2141 out_free_device_extension:
2142 twa_free_device_extension(tw_dev);
2143 scsi_host_put(host);
2145 pci_disable_device(pdev);
2148 } /* End twa_probe() */
2150 /* This function is called to remove a device */
2151 static void twa_remove(struct pci_dev *pdev)
2153 struct Scsi_Host *host = pci_get_drvdata(pdev);
2154 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2156 scsi_remove_host(tw_dev->host);
2158 /* Unregister character device */
2159 if (twa_major >= 0) {
2160 unregister_chrdev(twa_major, "twa");
2164 /* Shutdown the card */
2165 __twa_shutdown(tw_dev);
2167 /* Disable MSI if enabled */
2168 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2169 pci_disable_msi(pdev);
2171 /* Free IO remapping */
2172 iounmap(tw_dev->base_addr);
2174 /* Free up the mem region */
2175 pci_release_regions(pdev);
2177 /* Free up device extension resources */
2178 twa_free_device_extension(tw_dev);
2180 scsi_host_put(tw_dev->host);
2181 pci_disable_device(pdev);
2182 twa_device_extension_count--;
2183 } /* End twa_remove() */
2185 /* PCI Devices supported by this driver */
2186 static struct pci_device_id twa_pci_tbl[] __devinitdata = {
2187 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2188 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2189 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2190 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2191 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2192 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2193 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2194 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2197 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2199 /* pci_driver initializer */
2200 static struct pci_driver twa_driver = {
2202 .id_table = twa_pci_tbl,
2204 .remove = twa_remove,
2205 .shutdown = twa_shutdown
2208 /* This function is called on driver initialization */
2209 static int __init twa_init(void)
2211 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2213 return pci_register_driver(&twa_driver);
2214 } /* End twa_init() */
2216 /* This function is called on driver exit */
2217 static void __exit twa_exit(void)
2219 pci_unregister_driver(&twa_driver);
2220 } /* End twa_exit() */
2222 module_init(twa_init);
2223 module_exit(twa_exit);