84 #include <linux/module.h>
85 #include <linux/reboot.h>
89 #include <linux/errno.h>
90 #include <linux/types.h>
92 #include <linux/pci.h>
93 #include <linux/time.h>
95 #include <linux/slab.h>
98 #include <asm/uaccess.h>
99 #include <scsi/scsi.h>
106 #define TW_DRIVER_VERSION "2.26.02.014"
109 static unsigned int twa_device_extension_count;
110 static int twa_major = -1;
119 static int use_msi = 0;
126 static char *twa_aen_severity_lookup(
unsigned char severity_code);
128 static long twa_chrdev_ioctl(
struct file *
file,
unsigned int cmd,
unsigned long arg);
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);
162 unsigned long flags = 0;
167 "Current commands posted: %4d\n"
168 "Max commands posted: %4d\n"
169 "Current pending commands: %4d\n"
170 "Max pending commands: %4d\n"
171 "Last sgl length: %4d\n"
172 "Max sgl length: %4d\n"
173 "Last sector count: %4d\n"
174 "Max sector count: %4d\n"
175 "SCSI Host Resets: %4d\n"
188 spin_unlock_irqrestore(tw_dev->
host->host_lock,
flags);
211 .show = twa_show_stats
216 &twa_host_stats_attr,
223 .unlocked_ioctl = twa_chrdev_ioctl,
224 .open = twa_chrdev_open,
247 if (twa_aen_read_queue(tw_dev, request_id))
260 twa_aen_sync_time(tw_dev, request_id);
264 twa_aen_queue_event(tw_dev, header);
267 if (twa_aen_read_queue(tw_dev, request_id))
277 twa_free_request_id(tw_dev, request_id);
293 int first_reset = 0,
queue = 0, retval = 1;
323 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
329 if (twa_poll_response(tw_dev, request_id, 30)) {
343 if (first_reset != 1)
349 if (first_reset == 0)
362 twa_aen_queue_event(tw_dev, header);
402 local_time = (
u32)(
time.tv_sec - (
sys_tz.tz_minuteswest * 60));
403 event->time_stamp_sec = local_time;
404 event->aen_code = aen;
420 error_str[0] ==
'\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
455 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
465 static char *twa_aen_severity_lookup(
unsigned char severity_code)
473 retval = twa_aen_severity_table[severity_code];
491 command_packet = &full_command_packet->command.oldcommand;
509 local_time = (
u32)(utc.tv_sec - (
sys_tz.tz_minuteswest * 60));
510 schedulertime = local_time - (3 * 86400);
511 schedulertime =
cpu_to_le32(schedulertime % 604800);
519 twa_post_command_packet(tw_dev, request_id, 1);
562 static int twa_check_bits(
u32 status_reg_value)
580 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
581 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
582 u32 init_connect_result = 0;
588 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
589 &fw_on_ctlr_build, &init_connect_result)) {
604 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
605 &fw_on_ctlr_branch, &fw_on_ctlr_build,
606 &init_connect_result)) {
610 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
641 static long twa_chrdev_ioctl(
struct file *
file,
unsigned int cmd,
unsigned long arg)
645 unsigned long *
cpu_addr, data_buffer_length_adjusted = 0,
flags = 0;
648 unsigned int sequence_id = 0;
649 unsigned char event_index, start_index;
681 data_buffer_length_adjusted = (driver_command.
buffer_length + 511) & ~511;
700 twa_get_request_id(tw_dev, &request_id);
711 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
716 twa_post_command_packet(tw_dev, request_id, 1);
717 spin_unlock_irqrestore(tw_dev->
host->host_lock,
flags);
727 printk(
KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
731 twa_reset_device_extension(tw_dev);
742 twa_free_request_id(tw_dev, request_id);
743 spin_unlock_irqrestore(tw_dev->
host->host_lock,
flags);
806 event_index = (start_index + sequence_id - tw_dev->
event_queue[start_index]->
sequence_id + 1) % TW_Q_LENGTH;
835 event_index = (start_index + sequence_id - tw_dev->
event_queue[start_index]->
sequence_id - 1) % TW_Q_LENGTH;
849 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
889 static int twa_chrdev_open(
struct inode *inode,
struct file *file)
891 unsigned int minor_number;
894 minor_number = iminor(inode);
895 if (minor_number >= twa_device_extension_count)
942 u32 status_reg_value, response_que_value;
943 int count = 0, retval = 1;
963 u32 response_que_value = 0;
964 unsigned long before;
985 static int twa_fill_sense(
TW_Device_Extension *tw_dev,
int request_id,
int copy_sense,
int print_host)
988 unsigned short error;
1002 tw_dev->
host->host_no,
1005 error_str[0] ==
'\0' ?
1006 twa_string_lookup(twa_error_table,
1013 error_str[0] ==
'\0' ?
1014 twa_string_lookup(twa_error_table,
1057 static void *twa_get_param(
TW_Device_Extension *tw_dev,
int request_id,
int table_id,
int parameter_id,
int parameter_size_bytes)
1062 void *retval =
NULL;
1085 twa_post_command_packet(tw_dev, request_id, 1);
1088 if (twa_poll_response(tw_dev, request_id, 30))
1091 retval = (
void *)&(param->
data[0]);
1093 tw_dev->posted_request_count--;
1102 *request_id = tw_dev->free_queue[tw_dev->free_head];
1103 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1109 u32 set_features,
unsigned short current_fw_srl,
1110 unsigned short current_fw_arch_id,
1111 unsigned short current_fw_branch,
1112 unsigned short current_fw_build,
1113 unsigned short *fw_on_ctlr_srl,
1114 unsigned short *fw_on_ctlr_arch_id,
1115 unsigned short *fw_on_ctlr_branch,
1116 unsigned short *fw_on_ctlr_build,
1117 u32 *init_connect_result)
1121 int request_id = 0, retval = 1;
1132 tw_initconnect->
features = set_features;
1149 twa_post_command_packet(tw_dev, request_id, 1);
1152 if (twa_poll_response(tw_dev, request_id, 30)) {
1155 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1218 static irqreturn_t twa_interrupt(
int irq,
void *dev_instance)
1221 u32 status_reg_value;
1228 spin_lock(tw_dev->
host->host_lock);
1235 goto twa_interrupt_bail;
1241 goto twa_interrupt_bail;
1244 if (twa_check_bits(status_reg_value)) {
1245 if (twa_decode_bits(tw_dev, status_reg_value)) {
1247 goto twa_interrupt_bail;
1259 twa_get_request_id(tw_dev, &request_id);
1261 error = twa_aen_read_queue(tw_dev, request_id);
1264 twa_free_request_id(tw_dev, request_id);
1279 goto twa_interrupt_bail;
1281 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1295 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1303 if (tw_dev->
srb[request_id] !=
NULL) {
1304 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1308 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1315 if (tw_dev->
srb[request_id] !=
NULL) {
1318 goto twa_interrupt_bail;
1323 if (tw_dev->
srb[request_id] ==
NULL) {
1325 if (twa_aen_complete(tw_dev, request_id))
1336 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1356 twa_free_request_id(tw_dev, request_id);
1359 twa_unmap_scsi_data(tw_dev, request_id);
1364 if (twa_check_bits(status_reg_value)) {
1365 if (twa_decode_bits(tw_dev, status_reg_value)) {
1367 goto twa_interrupt_bail;
1374 spin_unlock(tw_dev->
host->host_lock);
1384 unsigned int pae = 0;
1386 if ((
sizeof(
long) < 8) && (
sizeof(
dma_addr_t) > 4))
1412 oldcommand->
size += pae;
1426 else if (use_sg < 0) {
1432 cmd->
SCp.have_data_in = use_sg;
1440 int retval = 1, found = 0, response_request_id;
1444 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1447 if (request_id != response_request_id) {
1454 twa_fill_sense(tw_dev, request_id, 0, 0);
1461 twa_fill_sense(tw_dev, request_id, 0, 0);
1477 u32 status_reg_value;
1478 unsigned long before;
1484 if (twa_check_bits(status_reg_value))
1485 twa_decode_bits(tw_dev, status_reg_value);
1487 while ((status_reg_value & flag) != flag) {
1490 if (twa_check_bits(status_reg_value))
1491 twa_decode_bits(tw_dev, status_reg_value);
1493 if (
time_after(jiffies, before + HZ * seconds))
1506 u32 status_reg_value;
1507 unsigned long before;
1513 if (twa_check_bits(status_reg_value))
1514 twa_decode_bits(tw_dev, status_reg_value);
1516 while ((status_reg_value & flag) != 0) {
1518 if (twa_check_bits(status_reg_value))
1519 twa_decode_bits(tw_dev, status_reg_value);
1521 if (
time_after(jiffies, before + HZ * seconds))
1532 static int twa_post_command_packet(
TW_Device_Extension *tw_dev,
int request_id,
char internal)
1534 u32 status_reg_value;
1549 if (twa_check_bits(status_reg_value))
1550 twa_decode_bits(tw_dev, status_reg_value);
1602 unsigned long flags = 0;
1614 if (tw_dev->
srb[i]) {
1616 tw_dev->
srb[
i]->scsi_done(tw_dev->
srb[i]);
1617 twa_unmap_scsi_data(tw_dev, i);
1635 spin_unlock_irqrestore(tw_dev->
host->host_lock, flags);
1637 if (twa_reset_sequence(tw_dev, 1))
1652 int tries = 0, retval = 1, flashed = 0, do_soft_reset =
soft_reset;
1655 if (do_soft_reset) {
1658 if (twa_empty_response_queue_large(tw_dev)) {
1675 if (twa_empty_response_queue(tw_dev)) {
1685 if (twa_check_srl(tw_dev, &flashed)) {
1698 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1721 if (capacity >= 0x200000) {
1724 cylinders =
sector_div(capacity, heads * sectors);
1728 cylinders =
sector_div(capacity, heads * sectors);
1739 static int twa_scsi_eh_reset(
struct scsi_cmnd *SCpnt)
1749 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1756 if (twa_reset_device_extension(tw_dev)) {
1791 twa_get_request_id(tw_dev, &request_id);
1799 retval = twa_scsiop_execute_scsi(tw_dev, request_id,
NULL, 0,
NULL);
1802 twa_free_request_id(tw_dev, request_id);
1803 twa_unmap_scsi_data(tw_dev, request_id);
1807 twa_free_request_id(tw_dev, request_id);
1808 twa_unmap_scsi_data(tw_dev, request_id);
1824 u32 num_sectors = 0x0;
1830 if (tw_dev->srb[request_id]) {
1832 if (scsi_sglist(srb))
1833 sglist = scsi_sglist(srb);
1837 full_command_packet = tw_dev->command_packet_virt[
request_id];
1843 command_packet->
status = 0;
1859 command_packet->
unit = 0;
1867 if (scsi_sg_count(srb)) {
1868 if ((scsi_sg_count(srb) == 1) &&
1872 scsi_sg_copy_to_buffer(srb,
1873 tw_dev->generic_buffer_virt[request_id],
1878 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1886 TW_PRINTK(tw_dev->host,
TW_DRIVER, 0x2e,
"Found unaligned sgl address during execute scsi");
1895 for (i = 0; i < use_sg; i++) {
1899 TW_PRINTK(tw_dev->host,
TW_DRIVER, 0x2f,
"Found unaligned sgl address during internal post");
1908 num_sectors = (
u32)srb->
cmnd[4];
1915 tw_dev->sector_count = num_sectors;
1916 if (tw_dev->sector_count > tw_dev->max_sector_count)
1917 tw_dev->max_sector_count = tw_dev->sector_count;
1921 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1922 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1923 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1928 retval = twa_post_command_packet(tw_dev, request_id, 0);
1930 twa_post_command_packet(tw_dev, request_id, 1);
1945 if (scsi_sg_count(cmd) == 1) {
1965 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0,
NULL,
NULL,
NULL,
NULL,
NULL)) {
1976 static void twa_shutdown(
struct pci_dev *pdev)
1978 struct Scsi_Host *host = pci_get_drvdata(pdev);
1981 __twa_shutdown(tw_dev);
1989 for (index = 0; ((code != table[
index].
code) &&
1990 (table[index].
text != (
char *)0)); index++);
1991 return(table[index].
text);
2004 static int twa_slave_configure(
struct scsi_device *sdev)
2015 .name =
"3ware 9000 Storage Controller",
2016 .queuecommand = twa_scsi_queue,
2017 .eh_host_reset_handler = twa_scsi_eh_reset,
2018 .bios_param = twa_scsi_biosparam,
2019 .change_queue_depth = twa_change_queue_depth,
2020 .can_queue = TW_Q_LENGTH-2,
2021 .slave_configure = twa_slave_configure,
2027 .shost_attrs = twa_host_attrs,
2036 unsigned long mem_addr, mem_len;
2042 goto out_disable_device;
2049 || pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(64)))
2051 || pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(32))) {
2054 goto out_disable_device;
2061 goto out_disable_device;
2066 tw_dev->
host = host;
2069 if (twa_initialize_device_extension(tw_dev)) {
2071 goto out_free_device_extension;
2078 goto out_free_device_extension;
2093 goto out_release_mem_region;
2100 if (twa_reset_sequence(tw_dev, 0))
2117 retval = scsi_add_host(host, &pdev->
dev);
2123 pci_set_drvdata(pdev, host);
2125 printk(
KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2138 !pci_enable_msi(pdev))
2145 goto out_remove_host;
2148 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2149 twa_device_extension_count++;
2157 if (twa_major == -1) {
2158 if ((twa_major = register_chrdev (0,
"twa", &twa_fops)) < 0)
2169 out_release_mem_region:
2171 out_free_device_extension:
2172 twa_free_device_extension(tw_dev);
2181 static void twa_remove(
struct pci_dev *pdev)
2183 struct Scsi_Host *host = pci_get_drvdata(pdev);
2189 if (twa_major >= 0) {
2190 unregister_chrdev(twa_major,
"twa");
2195 __twa_shutdown(tw_dev);
2208 twa_free_device_extension(tw_dev);
2212 twa_device_extension_count--;
2219 struct Scsi_Host *host = pci_get_drvdata(pdev);
2231 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0,
NULL,
NULL,
NULL,
NULL,
NULL)) {
2246 static int twa_resume(
struct pci_dev *pdev)
2249 struct Scsi_Host *host = pci_get_drvdata(pdev);
2254 pci_enable_wake(pdev,
PCI_D0, 0);
2267 || pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(64)))
2269 || pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(32))) {
2272 goto out_disable_device;
2276 if (twa_reset_sequence(tw_dev, 0)) {
2278 goto out_disable_device;
2286 goto out_disable_device;
2291 pci_enable_msi(pdev);
2324 .id_table = twa_pci_tbl,
2326 .remove = twa_remove,
2328 .suspend = twa_suspend,
2329 .resume = twa_resume,
2331 .shutdown = twa_shutdown
2335 static int __init twa_init(
void)
2339 return pci_register_driver(&twa_driver);
2343 static void __exit twa_exit(
void)