1 /* 2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux. 3 4 Written By: Adam Radford <aradford@gmail.com> 5 Modifications By: Tom Couch 6 7 Copyright (C) 2004-2009 Applied Micro Circuits Corporation. 8 Copyright (C) 2010 LSI Corporation. 9 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. 13 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. 18 19 NO WARRANTY 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. 29 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 38 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 42 43 Bugs/Comments/Suggestions should be mailed to: 44 aradford@gmail.com 45 46 Note: This version of the driver does not contain a bundled firmware 47 image. 48 49 History 50 ------- 51 2.26.02.000 - Driver cleanup for kernel submission. 52 2.26.02.001 - Replace schedule_timeout() calls with msleep(). 53 2.26.02.002 - Add support for PAE mode. 54 Add lun support. 55 Fix twa_remove() to free irq handler/unregister_chrdev() 56 before shutting down card. 57 Change to new 'change_queue_depth' api. 58 Fix 'handled=1' ISR usage, remove bogus IRQ check. 59 Remove un-needed eh_abort handler. 60 Add support for embedded firmware error strings. 61 2.26.02.003 - Correctly handle single sgl's with use_sg=1. 62 2.26.02.004 - Add support for 9550SX controllers. 63 2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher. 64 2.26.02.006 - Fix 9550SX pchip reset timeout. 65 Add big endian support. 66 2.26.02.007 - Disable local interrupts during kmap/unmap_atomic(). 67 2.26.02.008 - Free irq handler in __twa_shutdown(). 68 Serialize reset code. 69 Add support for 9650SE controllers. 70 2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails. 71 2.26.02.010 - Add support for 9690SA controllers. 72 2.26.02.011 - Increase max AENs drained to 256. 73 Add MSI support and "use_msi" module parameter. 74 Fix bug in twa_get_param() on 4GB+. 75 Use pci_resource_len() for ioremap(). 76 2.26.02.012 - Add power management support. 77 2.26.02.013 - Fix bug in twa_load_sgl(). 78 2.26.02.014 - Force 60 second timeout default. 79 */ 80 81 #include <linux/module.h> 82 #include <linux/reboot.h> 83 #include <linux/spinlock.h> 84 #include <linux/interrupt.h> 85 #include <linux/moduleparam.h> 86 #include <linux/errno.h> 87 #include <linux/types.h> 88 #include <linux/delay.h> 89 #include <linux/pci.h> 90 #include <linux/time.h> 91 #include <linux/mutex.h> 92 #include <linux/slab.h> 93 #include <asm/io.h> 94 #include <asm/irq.h> 95 #include <linux/uaccess.h> 96 #include <scsi/scsi.h> 97 #include <scsi/scsi_host.h> 98 #include <scsi/scsi_tcq.h> 99 #include <scsi/scsi_cmnd.h> 100 #include "3w-9xxx.h" 101 102 /* Globals */ 103 #define TW_DRIVER_VERSION "2.26.02.014" 104 static DEFINE_MUTEX(twa_chrdev_mutex); 105 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT]; 106 static unsigned int twa_device_extension_count; 107 static int twa_major = -1; 108 extern struct timezone sys_tz; 109 110 /* Module parameters */ 111 MODULE_AUTHOR ("LSI"); 112 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver"); 113 MODULE_LICENSE("GPL"); 114 MODULE_VERSION(TW_DRIVER_VERSION); 115 116 static int use_msi = 0; 117 module_param(use_msi, int, S_IRUGO); 118 MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0"); 119 120 /* Function prototypes */ 121 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header); 122 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id); 123 static char *twa_aen_severity_lookup(unsigned char severity_code); 124 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id); 125 static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 126 static int twa_chrdev_open(struct inode *inode, struct file *file); 127 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host); 128 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id); 129 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id); 130 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits, 131 u32 set_features, unsigned short current_fw_srl, 132 unsigned short current_fw_arch_id, 133 unsigned short current_fw_branch, 134 unsigned short current_fw_build, 135 unsigned short *fw_on_ctlr_srl, 136 unsigned short *fw_on_ctlr_arch_id, 137 unsigned short *fw_on_ctlr_branch, 138 unsigned short *fw_on_ctlr_build, 139 u32 *init_connect_result); 140 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); 141 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds); 142 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds); 143 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal); 144 static int twa_reset_device_extension(TW_Device_Extension *tw_dev); 145 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset); 146 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, 147 unsigned char *cdb, int use_sg, 148 TW_SG_Entry *sglistarg); 149 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id); 150 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code); 151 152 /* Functions */ 153 154 /* Show some statistics about the card */ 155 static ssize_t twa_show_stats(struct device *dev, 156 struct device_attribute *attr, char *buf) 157 { 158 struct Scsi_Host *host = class_to_shost(dev); 159 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata; 160 unsigned long flags = 0; 161 ssize_t len; 162 163 spin_lock_irqsave(tw_dev->host->host_lock, flags); 164 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n" 165 "Current commands posted: %4d\n" 166 "Max commands posted: %4d\n" 167 "Current pending commands: %4d\n" 168 "Max pending commands: %4d\n" 169 "Last sgl length: %4d\n" 170 "Max sgl length: %4d\n" 171 "Last sector count: %4d\n" 172 "Max sector count: %4d\n" 173 "SCSI Host Resets: %4d\n" 174 "AEN's: %4d\n", 175 TW_DRIVER_VERSION, 176 tw_dev->posted_request_count, 177 tw_dev->max_posted_request_count, 178 tw_dev->pending_request_count, 179 tw_dev->max_pending_request_count, 180 tw_dev->sgl_entries, 181 tw_dev->max_sgl_entries, 182 tw_dev->sector_count, 183 tw_dev->max_sector_count, 184 tw_dev->num_resets, 185 tw_dev->aen_count); 186 spin_unlock_irqrestore(tw_dev->host->host_lock, flags); 187 return len; 188 } /* End twa_show_stats() */ 189 190 /* Create sysfs 'stats' entry */ 191 static struct device_attribute twa_host_stats_attr = { 192 .attr = { 193 .name = "stats", 194 .mode = S_IRUGO, 195 }, 196 .show = twa_show_stats 197 }; 198 199 /* Host attributes initializer */ 200 static struct device_attribute *twa_host_attrs[] = { 201 &twa_host_stats_attr, 202 NULL, 203 }; 204 205 /* File operations struct for character device */ 206 static const struct file_operations twa_fops = { 207 .owner = THIS_MODULE, 208 .unlocked_ioctl = twa_chrdev_ioctl, 209 .open = twa_chrdev_open, 210 .release = NULL, 211 .llseek = noop_llseek, 212 }; 213 214 /* 215 * The controllers use an inline buffer instead of a mapped SGL for small, 216 * single entry buffers. Note that we treat a zero-length transfer like 217 * a mapped SGL. 218 */ 219 static bool twa_command_mapped(struct scsi_cmnd *cmd) 220 { 221 return scsi_sg_count(cmd) != 1 || 222 scsi_bufflen(cmd) >= TW_MIN_SGL_LENGTH; 223 } 224 225 /* This function will complete an aen request from the isr */ 226 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id) 227 { 228 TW_Command_Full *full_command_packet; 229 TW_Command *command_packet; 230 TW_Command_Apache_Header *header; 231 unsigned short aen; 232 int retval = 1; 233 234 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id]; 235 tw_dev->posted_request_count--; 236 aen = le16_to_cpu(header->status_block.error); 237 full_command_packet = tw_dev->command_packet_virt[request_id]; 238 command_packet = &full_command_packet->command.oldcommand; 239 240 /* First check for internal completion of set param for time sync */ 241 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) { 242 /* Keep reading the queue in case there are more aen's */ 243 if (twa_aen_read_queue(tw_dev, request_id)) 244 goto out2; 245 else { 246 retval = 0; 247 goto out; 248 } 249 } 250 251 switch (aen) { 252 case TW_AEN_QUEUE_EMPTY: 253 /* Quit reading the queue if this is the last one */ 254 break; 255 case TW_AEN_SYNC_TIME_WITH_HOST: 256 twa_aen_sync_time(tw_dev, request_id); 257 retval = 0; 258 goto out; 259 default: 260 twa_aen_queue_event(tw_dev, header); 261 262 /* If there are more aen's, keep reading the queue */ 263 if (twa_aen_read_queue(tw_dev, request_id)) 264 goto out2; 265 else { 266 retval = 0; 267 goto out; 268 } 269 } 270 retval = 0; 271 out2: 272 tw_dev->state[request_id] = TW_S_COMPLETED; 273 twa_free_request_id(tw_dev, request_id); 274 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags); 275 out: 276 return retval; 277 } /* End twa_aen_complete() */ 278 279 /* This function will drain aen queue */ 280 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset) 281 { 282 int request_id = 0; 283 unsigned char cdb[TW_MAX_CDB_LEN]; 284 TW_SG_Entry sglist[1]; 285 int finished = 0, count = 0; 286 TW_Command_Full *full_command_packet; 287 TW_Command_Apache_Header *header; 288 unsigned short aen; 289 int first_reset = 0, queue = 0, retval = 1; 290 291 if (no_check_reset) 292 first_reset = 0; 293 else 294 first_reset = 1; 295 296 full_command_packet = tw_dev->command_packet_virt[request_id]; 297 memset(full_command_packet, 0, sizeof(TW_Command_Full)); 298 299 /* Initialize cdb */ 300 memset(&cdb, 0, TW_MAX_CDB_LEN); 301 cdb[0] = REQUEST_SENSE; /* opcode */ 302 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */ 303 304 /* Initialize sglist */ 305 memset(&sglist, 0, sizeof(TW_SG_Entry)); 306 sglist[0].length = TW_SECTOR_SIZE; 307 sglist[0].address = tw_dev->generic_buffer_phys[request_id]; 308 309 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) { 310 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain"); 311 goto out; 312 } 313 314 /* Mark internal command */ 315 tw_dev->srb[request_id] = NULL; 316 317 do { 318 /* Send command to the board */ 319 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) { 320 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense"); 321 goto out; 322 } 323 324 /* Now poll for completion */ 325 if (twa_poll_response(tw_dev, request_id, 30)) { 326 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue"); 327 tw_dev->posted_request_count--; 328 goto out; 329 } 330 331 tw_dev->posted_request_count--; 332 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id]; 333 aen = le16_to_cpu(header->status_block.error); 334 queue = 0; 335 count++; 336 337 switch (aen) { 338 case TW_AEN_QUEUE_EMPTY: 339 if (first_reset != 1) 340 goto out; 341 else 342 finished = 1; 343 break; 344 case TW_AEN_SOFT_RESET: 345 if (first_reset == 0) 346 first_reset = 1; 347 else 348 queue = 1; 349 break; 350 case TW_AEN_SYNC_TIME_WITH_HOST: 351 break; 352 default: 353 queue = 1; 354 } 355 356 /* Now queue an event info */ 357 if (queue) 358 twa_aen_queue_event(tw_dev, header); 359 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN)); 360 361 if (count == TW_MAX_AEN_DRAIN) 362 goto out; 363 364 retval = 0; 365 out: 366 tw_dev->state[request_id] = TW_S_INITIAL; 367 return retval; 368 } /* End twa_aen_drain_queue() */ 369 370 /* This function will queue an event */ 371 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header) 372 { 373 u32 local_time; 374 TW_Event *event; 375 unsigned short aen; 376 char host[16]; 377 char *error_str; 378 379 tw_dev->aen_count++; 380 381 /* Fill out event info */ 382 event = tw_dev->event_queue[tw_dev->error_index]; 383 384 /* Check for clobber */ 385 host[0] = '\0'; 386 if (tw_dev->host) { 387 sprintf(host, " scsi%d:", tw_dev->host->host_no); 388 if (event->retrieved == TW_AEN_NOT_RETRIEVED) 389 tw_dev->aen_clobber = 1; 390 } 391 392 aen = le16_to_cpu(header->status_block.error); 393 memset(event, 0, sizeof(TW_Event)); 394 395 event->severity = TW_SEV_OUT(header->status_block.severity__reserved); 396 /* event->time_stamp_sec overflows in y2106 */ 397 local_time = (u32)(ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60)); 398 event->time_stamp_sec = local_time; 399 event->aen_code = aen; 400 event->retrieved = TW_AEN_NOT_RETRIEVED; 401 event->sequence_id = tw_dev->error_sequence_id; 402 tw_dev->error_sequence_id++; 403 404 /* Check for embedded error string */ 405 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]); 406 407 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0'; 408 event->parameter_len = strlen(header->err_specific_desc); 409 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str)))); 410 if (event->severity != TW_AEN_SEVERITY_DEBUG) 411 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n", 412 host, 413 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)), 414 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen, 415 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str, 416 header->err_specific_desc); 417 else 418 tw_dev->aen_count--; 419 420 if ((tw_dev->error_index + 1) == TW_Q_LENGTH) 421 tw_dev->event_queue_wrapped = 1; 422 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH; 423 } /* End twa_aen_queue_event() */ 424 425 /* This function will read the aen queue from the isr */ 426 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id) 427 { 428 unsigned char cdb[TW_MAX_CDB_LEN]; 429 TW_SG_Entry sglist[1]; 430 TW_Command_Full *full_command_packet; 431 int retval = 1; 432 433 full_command_packet = tw_dev->command_packet_virt[request_id]; 434 memset(full_command_packet, 0, sizeof(TW_Command_Full)); 435 436 /* Initialize cdb */ 437 memset(&cdb, 0, TW_MAX_CDB_LEN); 438 cdb[0] = REQUEST_SENSE; /* opcode */ 439 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */ 440 441 /* Initialize sglist */ 442 memset(&sglist, 0, sizeof(TW_SG_Entry)); 443 sglist[0].length = TW_SECTOR_SIZE; 444 sglist[0].address = tw_dev->generic_buffer_phys[request_id]; 445 446 /* Mark internal command */ 447 tw_dev->srb[request_id] = NULL; 448 449 /* Now post the command packet */ 450 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) { 451 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue"); 452 goto out; 453 } 454 retval = 0; 455 out: 456 return retval; 457 } /* End twa_aen_read_queue() */ 458 459 /* This function will look up an AEN severity string */ 460 static char *twa_aen_severity_lookup(unsigned char severity_code) 461 { 462 char *retval = NULL; 463 464 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) || 465 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG)) 466 goto out; 467 468 retval = twa_aen_severity_table[severity_code]; 469 out: 470 return retval; 471 } /* End twa_aen_severity_lookup() */ 472 473 /* This function will sync firmware time with the host time */ 474 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id) 475 { 476 u32 schedulertime; 477 TW_Command_Full *full_command_packet; 478 TW_Command *command_packet; 479 TW_Param_Apache *param; 480 time64_t local_time; 481 482 /* Fill out the command packet */ 483 full_command_packet = tw_dev->command_packet_virt[request_id]; 484 memset(full_command_packet, 0, sizeof(TW_Command_Full)); 485 command_packet = &full_command_packet->command.oldcommand; 486 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM); 487 command_packet->request_id = request_id; 488 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]); 489 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE); 490 command_packet->size = TW_COMMAND_SIZE; 491 command_packet->byte6_offset.parameter_count = cpu_to_le16(1); 492 493 /* Setup the param */ 494 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id]; 495 memset(param, 0, TW_SECTOR_SIZE); 496 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */ 497 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */ 498 param->parameter_size_bytes = cpu_to_le16(4); 499 500 /* Convert system time in UTC to local time seconds since last 501 Sunday 12:00AM */ 502 local_time = (ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60)); 503 div_u64_rem(local_time - (3 * 86400), 604800, &schedulertime); 504 schedulertime = cpu_to_le32(schedulertime % 604800); 505 506 memcpy(param->data, &schedulertime, sizeof(u32)); 507 508 /* Mark internal command */ 509 tw_dev->srb[request_id] = NULL; 510 511 /* Now post the command */ 512 twa_post_command_packet(tw_dev, request_id, 1); 513 } /* End twa_aen_sync_time() */ 514 515 /* This function will allocate memory and check if it is correctly aligned */ 516 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which) 517 { 518 int i; 519 dma_addr_t dma_handle; 520 unsigned long *cpu_addr; 521 int retval = 1; 522 523 cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev, 524 size * TW_Q_LENGTH, &dma_handle, GFP_KERNEL); 525 if (!cpu_addr) { 526 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed"); 527 goto out; 528 } 529 530 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) { 531 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory"); 532 dma_free_coherent(&tw_dev->tw_pci_dev->dev, size * TW_Q_LENGTH, 533 cpu_addr, dma_handle); 534 goto out; 535 } 536 537 memset(cpu_addr, 0, size*TW_Q_LENGTH); 538 539 for (i = 0; i < TW_Q_LENGTH; i++) { 540 switch(which) { 541 case 0: 542 tw_dev->command_packet_phys[i] = dma_handle+(i*size); 543 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size)); 544 break; 545 case 1: 546 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size); 547 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size)); 548 break; 549 } 550 } 551 retval = 0; 552 out: 553 return retval; 554 } /* End twa_allocate_memory() */ 555 556 /* This function will check the status register for unexpected bits */ 557 static int twa_check_bits(u32 status_reg_value) 558 { 559 int retval = 1; 560 561 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS) 562 goto out; 563 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0) 564 goto out; 565 566 retval = 0; 567 out: 568 return retval; 569 } /* End twa_check_bits() */ 570 571 /* This function will check the srl and decide if we are compatible */ 572 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed) 573 { 574 int retval = 1; 575 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0; 576 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0; 577 u32 init_connect_result = 0; 578 579 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS, 580 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL, 581 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH, 582 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl, 583 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch, 584 &fw_on_ctlr_build, &init_connect_result)) { 585 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL"); 586 goto out; 587 } 588 589 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl; 590 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch; 591 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build; 592 593 /* Try base mode compatibility */ 594 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) { 595 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS, 596 TW_EXTENDED_INIT_CONNECT, 597 TW_BASE_FW_SRL, TW_9000_ARCH_ID, 598 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD, 599 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id, 600 &fw_on_ctlr_branch, &fw_on_ctlr_build, 601 &init_connect_result)) { 602 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL"); 603 goto out; 604 } 605 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) { 606 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) { 607 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware"); 608 } else { 609 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver"); 610 } 611 goto out; 612 } 613 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL; 614 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH; 615 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD; 616 } 617 618 /* Load rest of compatibility struct */ 619 strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, 620 sizeof(tw_dev->tw_compat_info.driver_version)); 621 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL; 622 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH; 623 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD; 624 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL; 625 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH; 626 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD; 627 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl; 628 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch; 629 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build; 630 631 retval = 0; 632 out: 633 return retval; 634 } /* End twa_check_srl() */ 635 636 /* This function handles ioctl for the character device */ 637 static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 638 { 639 struct inode *inode = file_inode(file); 640 long timeout; 641 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0; 642 dma_addr_t dma_handle; 643 int request_id = 0; 644 unsigned int sequence_id = 0; 645 unsigned char event_index, start_index; 646 TW_Ioctl_Driver_Command driver_command; 647 TW_Ioctl_Buf_Apache *tw_ioctl; 648 TW_Lock *tw_lock; 649 TW_Command_Full *full_command_packet; 650 TW_Compatibility_Info *tw_compat_info; 651 TW_Event *event; 652 ktime_t current_time; 653 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)]; 654 int retval = TW_IOCTL_ERROR_OS_EFAULT; 655 void __user *argp = (void __user *)arg; 656 657 mutex_lock(&twa_chrdev_mutex); 658 659 /* Only let one of these through at a time */ 660 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) { 661 retval = TW_IOCTL_ERROR_OS_EINTR; 662 goto out; 663 } 664 665 /* First copy down the driver command */ 666 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command))) 667 goto out2; 668 669 /* Check data buffer size */ 670 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) { 671 retval = TW_IOCTL_ERROR_OS_EINVAL; 672 goto out2; 673 } 674 675 /* Hardware can only do multiple of 512 byte transfers */ 676 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511; 677 678 /* Now allocate ioctl buf memory */ 679 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); 680 if (!cpu_addr) { 681 retval = TW_IOCTL_ERROR_OS_ENOMEM; 682 goto out2; 683 } 684 685 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr; 686 687 /* Now copy down the entire ioctl */ 688 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1)) 689 goto out3; 690 691 /* See which ioctl we are doing */ 692 switch (cmd) { 693 case TW_IOCTL_FIRMWARE_PASS_THROUGH: 694 spin_lock_irqsave(tw_dev->host->host_lock, flags); 695 twa_get_request_id(tw_dev, &request_id); 696 697 /* Flag internal command */ 698 tw_dev->srb[request_id] = NULL; 699 700 /* Flag chrdev ioctl */ 701 tw_dev->chrdev_request_id = request_id; 702 703 full_command_packet = &tw_ioctl->firmware_command; 704 705 /* Load request id and sglist for both command types */ 706 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted); 707 708 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full)); 709 710 /* Now post the command packet to the controller */ 711 twa_post_command_packet(tw_dev, request_id, 1); 712 spin_unlock_irqrestore(tw_dev->host->host_lock, flags); 713 714 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ; 715 716 /* Now wait for command to complete */ 717 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout); 718 719 /* We timed out, and didn't get an interrupt */ 720 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) { 721 /* Now we need to reset the board */ 722 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n", 723 tw_dev->host->host_no, TW_DRIVER, 0x37, 724 cmd); 725 retval = TW_IOCTL_ERROR_OS_EIO; 726 twa_reset_device_extension(tw_dev); 727 goto out3; 728 } 729 730 /* Now copy in the command packet response */ 731 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full)); 732 733 /* Now complete the io */ 734 spin_lock_irqsave(tw_dev->host->host_lock, flags); 735 tw_dev->posted_request_count--; 736 tw_dev->state[request_id] = TW_S_COMPLETED; 737 twa_free_request_id(tw_dev, request_id); 738 spin_unlock_irqrestore(tw_dev->host->host_lock, flags); 739 break; 740 case TW_IOCTL_GET_COMPATIBILITY_INFO: 741 tw_ioctl->driver_command.status = 0; 742 /* Copy compatibility struct into ioctl data buffer */ 743 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer; 744 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info)); 745 break; 746 case TW_IOCTL_GET_LAST_EVENT: 747 if (tw_dev->event_queue_wrapped) { 748 if (tw_dev->aen_clobber) { 749 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER; 750 tw_dev->aen_clobber = 0; 751 } else 752 tw_ioctl->driver_command.status = 0; 753 } else { 754 if (!tw_dev->error_index) { 755 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS; 756 break; 757 } 758 tw_ioctl->driver_command.status = 0; 759 } 760 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH; 761 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event)); 762 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED; 763 break; 764 case TW_IOCTL_GET_FIRST_EVENT: 765 if (tw_dev->event_queue_wrapped) { 766 if (tw_dev->aen_clobber) { 767 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER; 768 tw_dev->aen_clobber = 0; 769 } else 770 tw_ioctl->driver_command.status = 0; 771 event_index = tw_dev->error_index; 772 } else { 773 if (!tw_dev->error_index) { 774 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS; 775 break; 776 } 777 tw_ioctl->driver_command.status = 0; 778 event_index = 0; 779 } 780 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event)); 781 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED; 782 break; 783 case TW_IOCTL_GET_NEXT_EVENT: 784 event = (TW_Event *)tw_ioctl->data_buffer; 785 sequence_id = event->sequence_id; 786 tw_ioctl->driver_command.status = 0; 787 788 if (tw_dev->event_queue_wrapped) { 789 if (tw_dev->aen_clobber) { 790 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER; 791 tw_dev->aen_clobber = 0; 792 } 793 start_index = tw_dev->error_index; 794 } else { 795 if (!tw_dev->error_index) { 796 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS; 797 break; 798 } 799 start_index = 0; 800 } 801 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH; 802 803 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) { 804 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER) 805 tw_dev->aen_clobber = 1; 806 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS; 807 break; 808 } 809 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event)); 810 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED; 811 break; 812 case TW_IOCTL_GET_PREVIOUS_EVENT: 813 event = (TW_Event *)tw_ioctl->data_buffer; 814 sequence_id = event->sequence_id; 815 tw_ioctl->driver_command.status = 0; 816 817 if (tw_dev->event_queue_wrapped) { 818 if (tw_dev->aen_clobber) { 819 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER; 820 tw_dev->aen_clobber = 0; 821 } 822 start_index = tw_dev->error_index; 823 } else { 824 if (!tw_dev->error_index) { 825 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS; 826 break; 827 } 828 start_index = 0; 829 } 830 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH; 831 832 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) { 833 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER) 834 tw_dev->aen_clobber = 1; 835 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS; 836 break; 837 } 838 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event)); 839 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED; 840 break; 841 case TW_IOCTL_GET_LOCK: 842 tw_lock = (TW_Lock *)tw_ioctl->data_buffer; 843 current_time = ktime_get(); 844 845 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || 846 ktime_after(current_time, tw_dev->ioctl_time)) { 847 tw_dev->ioctl_sem_lock = 1; 848 tw_dev->ioctl_time = ktime_add_ms(current_time, tw_lock->timeout_msec); 849 tw_ioctl->driver_command.status = 0; 850 tw_lock->time_remaining_msec = tw_lock->timeout_msec; 851 } else { 852 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED; 853 tw_lock->time_remaining_msec = ktime_ms_delta(tw_dev->ioctl_time, current_time); 854 } 855 break; 856 case TW_IOCTL_RELEASE_LOCK: 857 if (tw_dev->ioctl_sem_lock == 1) { 858 tw_dev->ioctl_sem_lock = 0; 859 tw_ioctl->driver_command.status = 0; 860 } else { 861 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED; 862 } 863 break; 864 default: 865 retval = TW_IOCTL_ERROR_OS_ENOTTY; 866 goto out3; 867 } 868 869 /* Now copy the entire response to userspace */ 870 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0) 871 retval = 0; 872 out3: 873 /* Now free ioctl buf memory */ 874 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle); 875 out2: 876 mutex_unlock(&tw_dev->ioctl_lock); 877 out: 878 mutex_unlock(&twa_chrdev_mutex); 879 return retval; 880 } /* End twa_chrdev_ioctl() */ 881 882 /* This function handles open for the character device */ 883 /* NOTE that this function will race with remove. */ 884 static int twa_chrdev_open(struct inode *inode, struct file *file) 885 { 886 unsigned int minor_number; 887 int retval = TW_IOCTL_ERROR_OS_ENODEV; 888 889 if (!capable(CAP_SYS_ADMIN)) { 890 retval = -EACCES; 891 goto out; 892 } 893 894 minor_number = iminor(inode); 895 if (minor_number >= twa_device_extension_count) 896 goto out; 897 retval = 0; 898 out: 899 return retval; 900 } /* End twa_chrdev_open() */ 901 902 /* This function will print readable messages from status register errors */ 903 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value) 904 { 905 int retval = 1; 906 907 /* Check for various error conditions and handle them appropriately */ 908 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) { 909 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing"); 910 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev)); 911 } 912 913 if (status_reg_value & TW_STATUS_PCI_ABORT) { 914 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing"); 915 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev)); 916 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT); 917 } 918 919 if (status_reg_value & TW_STATUS_QUEUE_ERROR) { 920 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) && 921 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) || 922 (!test_bit(TW_IN_RESET, &tw_dev->flags))) 923 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing"); 924 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev)); 925 } 926 927 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) { 928 if (tw_dev->reset_print == 0) { 929 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing"); 930 tw_dev->reset_print = 1; 931 } 932 goto out; 933 } 934 retval = 0; 935 out: 936 return retval; 937 } /* End twa_decode_bits() */ 938 939 /* This function will empty the response queue */ 940 static int twa_empty_response_queue(TW_Device_Extension *tw_dev) 941 { 942 u32 status_reg_value; 943 int count = 0, retval = 1; 944 945 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 946 947 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) { 948 readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev)); 949 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 950 count++; 951 } 952 if (count == TW_MAX_RESPONSE_DRAIN) 953 goto out; 954 955 retval = 0; 956 out: 957 return retval; 958 } /* End twa_empty_response_queue() */ 959 960 /* This function will clear the pchip/response queue on 9550SX */ 961 static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev) 962 { 963 u32 response_que_value = 0; 964 unsigned long before; 965 int retval = 1; 966 967 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) { 968 before = jiffies; 969 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) { 970 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev)); 971 msleep(1); 972 if (time_after(jiffies, before + HZ * 30)) 973 goto out; 974 } 975 /* P-chip settle time */ 976 msleep(500); 977 retval = 0; 978 } else 979 retval = 0; 980 out: 981 return retval; 982 } /* End twa_empty_response_queue_large() */ 983 984 /* This function passes sense keys from firmware to scsi layer */ 985 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host) 986 { 987 TW_Command_Full *full_command_packet; 988 unsigned short error; 989 int retval = 1; 990 char *error_str; 991 992 full_command_packet = tw_dev->command_packet_virt[request_id]; 993 994 /* Check for embedded error string */ 995 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]); 996 997 /* Don't print error for Logical unit not supported during rollcall */ 998 error = le16_to_cpu(full_command_packet->header.status_block.error); 999 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) { 1000 if (print_host) 1001 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n", 1002 tw_dev->host->host_no, 1003 TW_MESSAGE_SOURCE_CONTROLLER_ERROR, 1004 full_command_packet->header.status_block.error, 1005 error_str[0] == '\0' ? 1006 twa_string_lookup(twa_error_table, 1007 full_command_packet->header.status_block.error) : error_str, 1008 full_command_packet->header.err_specific_desc); 1009 else 1010 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n", 1011 TW_MESSAGE_SOURCE_CONTROLLER_ERROR, 1012 full_command_packet->header.status_block.error, 1013 error_str[0] == '\0' ? 1014 twa_string_lookup(twa_error_table, 1015 full_command_packet->header.status_block.error) : error_str, 1016 full_command_packet->header.err_specific_desc); 1017 } 1018 1019 if (copy_sense) { 1020 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH); 1021 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1); 1022 retval = TW_ISR_DONT_RESULT; 1023 goto out; 1024 } 1025 retval = 0; 1026 out: 1027 return retval; 1028 } /* End twa_fill_sense() */ 1029 1030 /* This function will free up device extension resources */ 1031 static void twa_free_device_extension(TW_Device_Extension *tw_dev) 1032 { 1033 if (tw_dev->command_packet_virt[0]) 1034 dma_free_coherent(&tw_dev->tw_pci_dev->dev, 1035 sizeof(TW_Command_Full) * TW_Q_LENGTH, 1036 tw_dev->command_packet_virt[0], 1037 tw_dev->command_packet_phys[0]); 1038 1039 if (tw_dev->generic_buffer_virt[0]) 1040 dma_free_coherent(&tw_dev->tw_pci_dev->dev, 1041 TW_SECTOR_SIZE * TW_Q_LENGTH, 1042 tw_dev->generic_buffer_virt[0], 1043 tw_dev->generic_buffer_phys[0]); 1044 1045 kfree(tw_dev->event_queue[0]); 1046 } /* End twa_free_device_extension() */ 1047 1048 /* This function will free a request id */ 1049 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id) 1050 { 1051 tw_dev->free_queue[tw_dev->free_tail] = request_id; 1052 tw_dev->state[request_id] = TW_S_FINISHED; 1053 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH; 1054 } /* End twa_free_request_id() */ 1055 1056 /* This function will get parameter table entries from the firmware */ 1057 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes) 1058 { 1059 TW_Command_Full *full_command_packet; 1060 TW_Command *command_packet; 1061 TW_Param_Apache *param; 1062 void *retval = NULL; 1063 1064 /* Setup the command packet */ 1065 full_command_packet = tw_dev->command_packet_virt[request_id]; 1066 memset(full_command_packet, 0, sizeof(TW_Command_Full)); 1067 command_packet = &full_command_packet->command.oldcommand; 1068 1069 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM); 1070 command_packet->size = TW_COMMAND_SIZE; 1071 command_packet->request_id = request_id; 1072 command_packet->byte6_offset.block_count = cpu_to_le16(1); 1073 1074 /* Now setup the param */ 1075 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id]; 1076 memset(param, 0, TW_SECTOR_SIZE); 1077 param->table_id = cpu_to_le16(table_id | 0x8000); 1078 param->parameter_id = cpu_to_le16(parameter_id); 1079 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes); 1080 1081 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]); 1082 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE); 1083 1084 /* Post the command packet to the board */ 1085 twa_post_command_packet(tw_dev, request_id, 1); 1086 1087 /* Poll for completion */ 1088 if (twa_poll_response(tw_dev, request_id, 30)) 1089 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param") 1090 else 1091 retval = (void *)&(param->data[0]); 1092 1093 tw_dev->posted_request_count--; 1094 tw_dev->state[request_id] = TW_S_INITIAL; 1095 1096 return retval; 1097 } /* End twa_get_param() */ 1098 1099 /* This function will assign an available request id */ 1100 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id) 1101 { 1102 *request_id = tw_dev->free_queue[tw_dev->free_head]; 1103 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH; 1104 tw_dev->state[*request_id] = TW_S_STARTED; 1105 } /* End twa_get_request_id() */ 1106 1107 /* This function will send an initconnection command to controller */ 1108 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits, 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) 1118 { 1119 TW_Command_Full *full_command_packet; 1120 TW_Initconnect *tw_initconnect; 1121 int request_id = 0, retval = 1; 1122 1123 /* Initialize InitConnection command packet */ 1124 full_command_packet = tw_dev->command_packet_virt[request_id]; 1125 memset(full_command_packet, 0, sizeof(TW_Command_Full)); 1126 full_command_packet->header.header_desc.size_header = 128; 1127 1128 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand; 1129 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION); 1130 tw_initconnect->request_id = request_id; 1131 tw_initconnect->message_credits = cpu_to_le16(message_credits); 1132 tw_initconnect->features = set_features; 1133 1134 /* Turn on 64-bit sgl support if we need to */ 1135 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0; 1136 1137 tw_initconnect->features = cpu_to_le32(tw_initconnect->features); 1138 1139 if (set_features & TW_EXTENDED_INIT_CONNECT) { 1140 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED; 1141 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl); 1142 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id); 1143 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch); 1144 tw_initconnect->fw_build = cpu_to_le16(current_fw_build); 1145 } else 1146 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE; 1147 1148 /* Send command packet to the board */ 1149 twa_post_command_packet(tw_dev, request_id, 1); 1150 1151 /* Poll for completion */ 1152 if (twa_poll_response(tw_dev, request_id, 30)) { 1153 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection"); 1154 } else { 1155 if (set_features & TW_EXTENDED_INIT_CONNECT) { 1156 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl); 1157 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id); 1158 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch); 1159 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build); 1160 *init_connect_result = le32_to_cpu(tw_initconnect->result); 1161 } 1162 retval = 0; 1163 } 1164 1165 tw_dev->posted_request_count--; 1166 tw_dev->state[request_id] = TW_S_INITIAL; 1167 1168 return retval; 1169 } /* End twa_initconnection() */ 1170 1171 /* This function will initialize the fields of a device extension */ 1172 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev) 1173 { 1174 int i, retval = 1; 1175 1176 /* Initialize command packet buffers */ 1177 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) { 1178 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed"); 1179 goto out; 1180 } 1181 1182 /* Initialize generic buffer */ 1183 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) { 1184 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed"); 1185 goto out; 1186 } 1187 1188 /* Allocate event info space */ 1189 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL); 1190 if (!tw_dev->event_queue[0]) { 1191 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed"); 1192 goto out; 1193 } 1194 1195 1196 for (i = 0; i < TW_Q_LENGTH; i++) { 1197 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event))); 1198 tw_dev->free_queue[i] = i; 1199 tw_dev->state[i] = TW_S_INITIAL; 1200 } 1201 1202 tw_dev->pending_head = TW_Q_START; 1203 tw_dev->pending_tail = TW_Q_START; 1204 tw_dev->free_head = TW_Q_START; 1205 tw_dev->free_tail = TW_Q_START; 1206 tw_dev->error_sequence_id = 1; 1207 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE; 1208 1209 mutex_init(&tw_dev->ioctl_lock); 1210 init_waitqueue_head(&tw_dev->ioctl_wqueue); 1211 1212 retval = 0; 1213 out: 1214 return retval; 1215 } /* End twa_initialize_device_extension() */ 1216 1217 /* This function is the interrupt service routine */ 1218 static irqreturn_t twa_interrupt(int irq, void *dev_instance) 1219 { 1220 int request_id, error = 0; 1221 u32 status_reg_value; 1222 TW_Response_Queue response_que; 1223 TW_Command_Full *full_command_packet; 1224 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance; 1225 int handled = 0; 1226 1227 /* Get the per adapter lock */ 1228 spin_lock(tw_dev->host->host_lock); 1229 1230 /* Read the registers */ 1231 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 1232 1233 /* Check if this is our interrupt, otherwise bail */ 1234 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT)) 1235 goto twa_interrupt_bail; 1236 1237 handled = 1; 1238 1239 /* If we are resetting, bail */ 1240 if (test_bit(TW_IN_RESET, &tw_dev->flags)) 1241 goto twa_interrupt_bail; 1242 1243 /* Check controller for errors */ 1244 if (twa_check_bits(status_reg_value)) { 1245 if (twa_decode_bits(tw_dev, status_reg_value)) { 1246 TW_CLEAR_ALL_INTERRUPTS(tw_dev); 1247 goto twa_interrupt_bail; 1248 } 1249 } 1250 1251 /* Handle host interrupt */ 1252 if (status_reg_value & TW_STATUS_HOST_INTERRUPT) 1253 TW_CLEAR_HOST_INTERRUPT(tw_dev); 1254 1255 /* Handle attention interrupt */ 1256 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) { 1257 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev); 1258 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) { 1259 twa_get_request_id(tw_dev, &request_id); 1260 1261 error = twa_aen_read_queue(tw_dev, request_id); 1262 if (error) { 1263 tw_dev->state[request_id] = TW_S_COMPLETED; 1264 twa_free_request_id(tw_dev, request_id); 1265 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags); 1266 } 1267 } 1268 } 1269 1270 /* Handle command interrupt */ 1271 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) { 1272 TW_MASK_COMMAND_INTERRUPT(tw_dev); 1273 /* Drain as many pending commands as we can */ 1274 while (tw_dev->pending_request_count > 0) { 1275 request_id = tw_dev->pending_queue[tw_dev->pending_head]; 1276 if (tw_dev->state[request_id] != TW_S_PENDING) { 1277 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending"); 1278 TW_CLEAR_ALL_INTERRUPTS(tw_dev); 1279 goto twa_interrupt_bail; 1280 } 1281 if (twa_post_command_packet(tw_dev, request_id, 1)==0) { 1282 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH; 1283 tw_dev->pending_request_count--; 1284 } else { 1285 /* If we get here, we will continue re-posting on the next command interrupt */ 1286 break; 1287 } 1288 } 1289 } 1290 1291 /* Handle response interrupt */ 1292 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) { 1293 1294 /* Drain the response queue from the board */ 1295 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) { 1296 /* Complete the response */ 1297 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev)); 1298 request_id = TW_RESID_OUT(response_que.response_id); 1299 full_command_packet = tw_dev->command_packet_virt[request_id]; 1300 error = 0; 1301 /* Check for command packet errors */ 1302 if (full_command_packet->command.newcommand.status != 0) { 1303 if (tw_dev->srb[request_id] != NULL) { 1304 error = twa_fill_sense(tw_dev, request_id, 1, 1); 1305 } else { 1306 /* Skip ioctl error prints */ 1307 if (request_id != tw_dev->chrdev_request_id) { 1308 error = twa_fill_sense(tw_dev, request_id, 0, 1); 1309 } 1310 } 1311 } 1312 1313 /* Check for correct state */ 1314 if (tw_dev->state[request_id] != TW_S_POSTED) { 1315 if (tw_dev->srb[request_id] != NULL) { 1316 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted"); 1317 TW_CLEAR_ALL_INTERRUPTS(tw_dev); 1318 goto twa_interrupt_bail; 1319 } 1320 } 1321 1322 /* Check for internal command completion */ 1323 if (tw_dev->srb[request_id] == NULL) { 1324 if (request_id != tw_dev->chrdev_request_id) { 1325 if (twa_aen_complete(tw_dev, request_id)) 1326 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt"); 1327 } else { 1328 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE; 1329 wake_up(&tw_dev->ioctl_wqueue); 1330 } 1331 } else { 1332 struct scsi_cmnd *cmd; 1333 1334 cmd = tw_dev->srb[request_id]; 1335 1336 twa_scsiop_execute_scsi_complete(tw_dev, request_id); 1337 /* If no error command was a success */ 1338 if (error == 0) { 1339 cmd->result = (DID_OK << 16); 1340 } 1341 1342 /* If error, command failed */ 1343 if (error == 1) { 1344 /* Ask for a host reset */ 1345 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1); 1346 } 1347 1348 /* Report residual bytes for single sgl */ 1349 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) { 1350 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id])) 1351 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length); 1352 } 1353 1354 /* Now complete the io */ 1355 if (twa_command_mapped(cmd)) 1356 scsi_dma_unmap(cmd); 1357 cmd->scsi_done(cmd); 1358 tw_dev->state[request_id] = TW_S_COMPLETED; 1359 twa_free_request_id(tw_dev, request_id); 1360 tw_dev->posted_request_count--; 1361 } 1362 1363 /* Check for valid status after each drain */ 1364 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 1365 if (twa_check_bits(status_reg_value)) { 1366 if (twa_decode_bits(tw_dev, status_reg_value)) { 1367 TW_CLEAR_ALL_INTERRUPTS(tw_dev); 1368 goto twa_interrupt_bail; 1369 } 1370 } 1371 } 1372 } 1373 1374 twa_interrupt_bail: 1375 spin_unlock(tw_dev->host->host_lock); 1376 return IRQ_RETVAL(handled); 1377 } /* End twa_interrupt() */ 1378 1379 /* This function will load the request id and various sgls for ioctls */ 1380 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) 1381 { 1382 TW_Command *oldcommand; 1383 TW_Command_Apache *newcommand; 1384 TW_SG_Entry *sgl; 1385 unsigned int pae = 0; 1386 1387 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4)) 1388 pae = 1; 1389 1390 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) { 1391 newcommand = &full_command_packet->command.newcommand; 1392 newcommand->request_id__lunl = 1393 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id)); 1394 if (length) { 1395 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1); 1396 newcommand->sg_list[0].length = cpu_to_le32(length); 1397 } 1398 newcommand->sgl_entries__lunh = 1399 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0)); 1400 } else { 1401 oldcommand = &full_command_packet->command.oldcommand; 1402 oldcommand->request_id = request_id; 1403 1404 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) { 1405 /* Load the sg list */ 1406 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA) 1407 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae); 1408 else 1409 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset)); 1410 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1); 1411 sgl->length = cpu_to_le32(length); 1412 1413 oldcommand->size += pae; 1414 } 1415 } 1416 } /* End twa_load_sgl() */ 1417 1418 /* This function will poll for a response interrupt of a request */ 1419 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds) 1420 { 1421 int retval = 1, found = 0, response_request_id; 1422 TW_Response_Queue response_queue; 1423 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id]; 1424 1425 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) { 1426 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev)); 1427 response_request_id = TW_RESID_OUT(response_queue.response_id); 1428 if (request_id != response_request_id) { 1429 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response"); 1430 goto out; 1431 } 1432 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) { 1433 if (full_command_packet->command.newcommand.status != 0) { 1434 /* bad response */ 1435 twa_fill_sense(tw_dev, request_id, 0, 0); 1436 goto out; 1437 } 1438 found = 1; 1439 } else { 1440 if (full_command_packet->command.oldcommand.status != 0) { 1441 /* bad response */ 1442 twa_fill_sense(tw_dev, request_id, 0, 0); 1443 goto out; 1444 } 1445 found = 1; 1446 } 1447 } 1448 1449 if (found) 1450 retval = 0; 1451 out: 1452 return retval; 1453 } /* End twa_poll_response() */ 1454 1455 /* This function will poll the status register for a flag */ 1456 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds) 1457 { 1458 u32 status_reg_value; 1459 unsigned long before; 1460 int retval = 1; 1461 1462 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 1463 before = jiffies; 1464 1465 if (twa_check_bits(status_reg_value)) 1466 twa_decode_bits(tw_dev, status_reg_value); 1467 1468 while ((status_reg_value & flag) != flag) { 1469 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 1470 1471 if (twa_check_bits(status_reg_value)) 1472 twa_decode_bits(tw_dev, status_reg_value); 1473 1474 if (time_after(jiffies, before + HZ * seconds)) 1475 goto out; 1476 1477 msleep(50); 1478 } 1479 retval = 0; 1480 out: 1481 return retval; 1482 } /* End twa_poll_status() */ 1483 1484 /* This function will poll the status register for disappearance of a flag */ 1485 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds) 1486 { 1487 u32 status_reg_value; 1488 unsigned long before; 1489 int retval = 1; 1490 1491 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 1492 before = jiffies; 1493 1494 if (twa_check_bits(status_reg_value)) 1495 twa_decode_bits(tw_dev, status_reg_value); 1496 1497 while ((status_reg_value & flag) != 0) { 1498 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 1499 if (twa_check_bits(status_reg_value)) 1500 twa_decode_bits(tw_dev, status_reg_value); 1501 1502 if (time_after(jiffies, before + HZ * seconds)) 1503 goto out; 1504 1505 msleep(50); 1506 } 1507 retval = 0; 1508 out: 1509 return retval; 1510 } /* End twa_poll_status_gone() */ 1511 1512 /* This function will attempt to post a command packet to the board */ 1513 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal) 1514 { 1515 u32 status_reg_value; 1516 dma_addr_t command_que_value; 1517 int retval = 1; 1518 1519 command_que_value = tw_dev->command_packet_phys[request_id]; 1520 1521 /* For 9650SE write low 4 bytes first */ 1522 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) || 1523 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) { 1524 command_que_value += TW_COMMAND_OFFSET; 1525 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev)); 1526 } 1527 1528 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev)); 1529 1530 if (twa_check_bits(status_reg_value)) 1531 twa_decode_bits(tw_dev, status_reg_value); 1532 1533 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) { 1534 1535 /* Only pend internal driver commands */ 1536 if (!internal) { 1537 retval = SCSI_MLQUEUE_HOST_BUSY; 1538 goto out; 1539 } 1540 1541 /* Couldn't post the command packet, so we do it later */ 1542 if (tw_dev->state[request_id] != TW_S_PENDING) { 1543 tw_dev->state[request_id] = TW_S_PENDING; 1544 tw_dev->pending_request_count++; 1545 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) { 1546 tw_dev->max_pending_request_count = tw_dev->pending_request_count; 1547 } 1548 tw_dev->pending_queue[tw_dev->pending_tail] = request_id; 1549 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH; 1550 } 1551 TW_UNMASK_COMMAND_INTERRUPT(tw_dev); 1552 goto out; 1553 } else { 1554 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) || 1555 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) { 1556 /* Now write upper 4 bytes */ 1557 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4); 1558 } else { 1559 if (sizeof(dma_addr_t) > 4) { 1560 command_que_value += TW_COMMAND_OFFSET; 1561 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev)); 1562 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4); 1563 } else { 1564 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev)); 1565 } 1566 } 1567 tw_dev->state[request_id] = TW_S_POSTED; 1568 tw_dev->posted_request_count++; 1569 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) { 1570 tw_dev->max_posted_request_count = tw_dev->posted_request_count; 1571 } 1572 } 1573 retval = 0; 1574 out: 1575 return retval; 1576 } /* End twa_post_command_packet() */ 1577 1578 /* This function will reset a device extension */ 1579 static int twa_reset_device_extension(TW_Device_Extension *tw_dev) 1580 { 1581 int i = 0; 1582 int retval = 1; 1583 unsigned long flags = 0; 1584 1585 set_bit(TW_IN_RESET, &tw_dev->flags); 1586 TW_DISABLE_INTERRUPTS(tw_dev); 1587 TW_MASK_COMMAND_INTERRUPT(tw_dev); 1588 spin_lock_irqsave(tw_dev->host->host_lock, flags); 1589 1590 /* Abort all requests that are in progress */ 1591 for (i = 0; i < TW_Q_LENGTH; i++) { 1592 if ((tw_dev->state[i] != TW_S_FINISHED) && 1593 (tw_dev->state[i] != TW_S_INITIAL) && 1594 (tw_dev->state[i] != TW_S_COMPLETED)) { 1595 if (tw_dev->srb[i]) { 1596 struct scsi_cmnd *cmd = tw_dev->srb[i]; 1597 1598 cmd->result = (DID_RESET << 16); 1599 if (twa_command_mapped(cmd)) 1600 scsi_dma_unmap(cmd); 1601 cmd->scsi_done(cmd); 1602 } 1603 } 1604 } 1605 1606 /* Reset queues and counts */ 1607 for (i = 0; i < TW_Q_LENGTH; i++) { 1608 tw_dev->free_queue[i] = i; 1609 tw_dev->state[i] = TW_S_INITIAL; 1610 } 1611 tw_dev->free_head = TW_Q_START; 1612 tw_dev->free_tail = TW_Q_START; 1613 tw_dev->posted_request_count = 0; 1614 tw_dev->pending_request_count = 0; 1615 tw_dev->pending_head = TW_Q_START; 1616 tw_dev->pending_tail = TW_Q_START; 1617 tw_dev->reset_print = 0; 1618 1619 spin_unlock_irqrestore(tw_dev->host->host_lock, flags); 1620 1621 if (twa_reset_sequence(tw_dev, 1)) 1622 goto out; 1623 1624 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev); 1625 clear_bit(TW_IN_RESET, &tw_dev->flags); 1626 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE; 1627 1628 retval = 0; 1629 out: 1630 return retval; 1631 } /* End twa_reset_device_extension() */ 1632 1633 /* This function will reset a controller */ 1634 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset) 1635 { 1636 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset; 1637 1638 while (tries < TW_MAX_RESET_TRIES) { 1639 if (do_soft_reset) { 1640 TW_SOFT_RESET(tw_dev); 1641 /* Clear pchip/response queue on 9550SX */ 1642 if (twa_empty_response_queue_large(tw_dev)) { 1643 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence"); 1644 do_soft_reset = 1; 1645 tries++; 1646 continue; 1647 } 1648 } 1649 1650 /* Make sure controller is in a good state */ 1651 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) { 1652 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence"); 1653 do_soft_reset = 1; 1654 tries++; 1655 continue; 1656 } 1657 1658 /* Empty response queue */ 1659 if (twa_empty_response_queue(tw_dev)) { 1660 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence"); 1661 do_soft_reset = 1; 1662 tries++; 1663 continue; 1664 } 1665 1666 flashed = 0; 1667 1668 /* Check for compatibility/flash */ 1669 if (twa_check_srl(tw_dev, &flashed)) { 1670 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence"); 1671 do_soft_reset = 1; 1672 tries++; 1673 continue; 1674 } else { 1675 if (flashed) { 1676 tries++; 1677 continue; 1678 } 1679 } 1680 1681 /* Drain the AEN queue */ 1682 if (twa_aen_drain_queue(tw_dev, soft_reset)) { 1683 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence"); 1684 do_soft_reset = 1; 1685 tries++; 1686 continue; 1687 } 1688 1689 /* If we got here, controller is in a good state */ 1690 retval = 0; 1691 goto out; 1692 } 1693 out: 1694 return retval; 1695 } /* End twa_reset_sequence() */ 1696 1697 /* This funciton returns unit geometry in cylinders/heads/sectors */ 1698 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[]) 1699 { 1700 int heads, sectors, cylinders; 1701 1702 if (capacity >= 0x200000) { 1703 heads = 255; 1704 sectors = 63; 1705 cylinders = sector_div(capacity, heads * sectors); 1706 } else { 1707 heads = 64; 1708 sectors = 32; 1709 cylinders = sector_div(capacity, heads * sectors); 1710 } 1711 1712 geom[0] = heads; 1713 geom[1] = sectors; 1714 geom[2] = cylinders; 1715 1716 return 0; 1717 } /* End twa_scsi_biosparam() */ 1718 1719 /* This is the new scsi eh reset function */ 1720 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt) 1721 { 1722 TW_Device_Extension *tw_dev = NULL; 1723 int retval = FAILED; 1724 1725 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata; 1726 1727 tw_dev->num_resets++; 1728 1729 sdev_printk(KERN_WARNING, SCpnt->device, 1730 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n", 1731 TW_DRIVER, 0x2c, SCpnt->cmnd[0]); 1732 1733 /* Make sure we are not issuing an ioctl or resetting from ioctl */ 1734 mutex_lock(&tw_dev->ioctl_lock); 1735 1736 /* Now reset the card and some of the device extension data */ 1737 if (twa_reset_device_extension(tw_dev)) { 1738 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset"); 1739 goto out; 1740 } 1741 1742 retval = SUCCESS; 1743 out: 1744 mutex_unlock(&tw_dev->ioctl_lock); 1745 return retval; 1746 } /* End twa_scsi_eh_reset() */ 1747 1748 /* This is the main scsi queue function to handle scsi opcodes */ 1749 static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *)) 1750 { 1751 int request_id, retval; 1752 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata; 1753 1754 /* If we are resetting due to timed out ioctl, report as busy */ 1755 if (test_bit(TW_IN_RESET, &tw_dev->flags)) { 1756 retval = SCSI_MLQUEUE_HOST_BUSY; 1757 goto out; 1758 } 1759 1760 /* Check if this FW supports luns */ 1761 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) { 1762 SCpnt->result = (DID_BAD_TARGET << 16); 1763 done(SCpnt); 1764 retval = 0; 1765 goto out; 1766 } 1767 1768 /* Save done function into scsi_cmnd struct */ 1769 SCpnt->scsi_done = done; 1770 1771 /* Get a free request id */ 1772 twa_get_request_id(tw_dev, &request_id); 1773 1774 /* Save the scsi command for use by the ISR */ 1775 tw_dev->srb[request_id] = SCpnt; 1776 1777 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL); 1778 switch (retval) { 1779 case SCSI_MLQUEUE_HOST_BUSY: 1780 if (twa_command_mapped(SCpnt)) 1781 scsi_dma_unmap(SCpnt); 1782 twa_free_request_id(tw_dev, request_id); 1783 break; 1784 case 1: 1785 SCpnt->result = (DID_ERROR << 16); 1786 if (twa_command_mapped(SCpnt)) 1787 scsi_dma_unmap(SCpnt); 1788 done(SCpnt); 1789 tw_dev->state[request_id] = TW_S_COMPLETED; 1790 twa_free_request_id(tw_dev, request_id); 1791 retval = 0; 1792 } 1793 out: 1794 return retval; 1795 } /* End twa_scsi_queue() */ 1796 1797 static DEF_SCSI_QCMD(twa_scsi_queue) 1798 1799 /* This function hands scsi cdb's to the firmware */ 1800 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, 1801 unsigned char *cdb, int use_sg, 1802 TW_SG_Entry *sglistarg) 1803 { 1804 TW_Command_Full *full_command_packet; 1805 TW_Command_Apache *command_packet; 1806 u32 num_sectors = 0x0; 1807 int i, sg_count; 1808 struct scsi_cmnd *srb = NULL; 1809 struct scatterlist *sg; 1810 int retval = 1; 1811 1812 if (tw_dev->srb[request_id]) 1813 srb = tw_dev->srb[request_id]; 1814 1815 /* Initialize command packet */ 1816 full_command_packet = tw_dev->command_packet_virt[request_id]; 1817 full_command_packet->header.header_desc.size_header = 128; 1818 full_command_packet->header.status_block.error = 0; 1819 full_command_packet->header.status_block.severity__reserved = 0; 1820 1821 command_packet = &full_command_packet->command.newcommand; 1822 command_packet->status = 0; 1823 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI); 1824 1825 /* We forced 16 byte cdb use earlier */ 1826 if (!cdb) 1827 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN); 1828 else 1829 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN); 1830 1831 if (srb) { 1832 command_packet->unit = srb->device->id; 1833 command_packet->request_id__lunl = 1834 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id)); 1835 } else { 1836 command_packet->request_id__lunl = 1837 cpu_to_le16(TW_REQ_LUN_IN(0, request_id)); 1838 command_packet->unit = 0; 1839 } 1840 1841 command_packet->sgl_offset = 16; 1842 1843 if (!sglistarg) { 1844 /* Map sglist from scsi layer to cmd packet */ 1845 1846 if (scsi_sg_count(srb)) { 1847 if (!twa_command_mapped(srb)) { 1848 if (srb->sc_data_direction == DMA_TO_DEVICE || 1849 srb->sc_data_direction == DMA_BIDIRECTIONAL) 1850 scsi_sg_copy_to_buffer(srb, 1851 tw_dev->generic_buffer_virt[request_id], 1852 TW_SECTOR_SIZE); 1853 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]); 1854 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH); 1855 } else { 1856 sg_count = scsi_dma_map(srb); 1857 if (sg_count < 0) 1858 goto out; 1859 1860 scsi_for_each_sg(srb, sg, sg_count, i) { 1861 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg)); 1862 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg)); 1863 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) { 1864 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi"); 1865 goto out; 1866 } 1867 } 1868 } 1869 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id]))); 1870 } 1871 } else { 1872 /* Internal cdb post */ 1873 for (i = 0; i < use_sg; i++) { 1874 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address); 1875 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length); 1876 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) { 1877 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post"); 1878 goto out; 1879 } 1880 } 1881 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg)); 1882 } 1883 1884 if (srb) { 1885 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6) 1886 num_sectors = (u32)srb->cmnd[4]; 1887 1888 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) 1889 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8); 1890 } 1891 1892 /* Update sector statistic */ 1893 tw_dev->sector_count = num_sectors; 1894 if (tw_dev->sector_count > tw_dev->max_sector_count) 1895 tw_dev->max_sector_count = tw_dev->sector_count; 1896 1897 /* Update SG statistics */ 1898 if (srb) { 1899 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]); 1900 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries) 1901 tw_dev->max_sgl_entries = tw_dev->sgl_entries; 1902 } 1903 1904 /* Now post the command to the board */ 1905 if (srb) { 1906 retval = twa_post_command_packet(tw_dev, request_id, 0); 1907 } else { 1908 twa_post_command_packet(tw_dev, request_id, 1); 1909 retval = 0; 1910 } 1911 out: 1912 return retval; 1913 } /* End twa_scsiop_execute_scsi() */ 1914 1915 /* This function completes an execute scsi operation */ 1916 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id) 1917 { 1918 struct scsi_cmnd *cmd = tw_dev->srb[request_id]; 1919 1920 if (!twa_command_mapped(cmd) && 1921 (cmd->sc_data_direction == DMA_FROM_DEVICE || 1922 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) { 1923 if (scsi_sg_count(cmd) == 1) { 1924 void *buf = tw_dev->generic_buffer_virt[request_id]; 1925 1926 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE); 1927 } 1928 } 1929 } /* End twa_scsiop_execute_scsi_complete() */ 1930 1931 /* This function tells the controller to shut down */ 1932 static void __twa_shutdown(TW_Device_Extension *tw_dev) 1933 { 1934 /* Disable interrupts */ 1935 TW_DISABLE_INTERRUPTS(tw_dev); 1936 1937 /* Free up the IRQ */ 1938 free_irq(tw_dev->tw_pci_dev->irq, tw_dev); 1939 1940 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no); 1941 1942 /* Tell the card we are shutting down */ 1943 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) { 1944 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed"); 1945 } else { 1946 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n"); 1947 } 1948 1949 /* Clear all interrupts just before exit */ 1950 TW_CLEAR_ALL_INTERRUPTS(tw_dev); 1951 } /* End __twa_shutdown() */ 1952 1953 /* Wrapper for __twa_shutdown */ 1954 static void twa_shutdown(struct pci_dev *pdev) 1955 { 1956 struct Scsi_Host *host = pci_get_drvdata(pdev); 1957 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata; 1958 1959 __twa_shutdown(tw_dev); 1960 } /* End twa_shutdown() */ 1961 1962 /* This function will look up a string */ 1963 static char *twa_string_lookup(twa_message_type *table, unsigned int code) 1964 { 1965 int index; 1966 1967 for (index = 0; ((code != table[index].code) && 1968 (table[index].text != (char *)0)); index++); 1969 return(table[index].text); 1970 } /* End twa_string_lookup() */ 1971 1972 /* This function gets called when a disk is coming on-line */ 1973 static int twa_slave_configure(struct scsi_device *sdev) 1974 { 1975 /* Force 60 second timeout */ 1976 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ); 1977 1978 return 0; 1979 } /* End twa_slave_configure() */ 1980 1981 /* scsi_host_template initializer */ 1982 static struct scsi_host_template driver_template = { 1983 .module = THIS_MODULE, 1984 .name = "3ware 9000 Storage Controller", 1985 .queuecommand = twa_scsi_queue, 1986 .eh_host_reset_handler = twa_scsi_eh_reset, 1987 .bios_param = twa_scsi_biosparam, 1988 .change_queue_depth = scsi_change_queue_depth, 1989 .can_queue = TW_Q_LENGTH-2, 1990 .slave_configure = twa_slave_configure, 1991 .this_id = -1, 1992 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH, 1993 .max_sectors = TW_MAX_SECTORS, 1994 .cmd_per_lun = TW_MAX_CMDS_PER_LUN, 1995 .shost_attrs = twa_host_attrs, 1996 .emulated = 1, 1997 .no_write_same = 1, 1998 }; 1999 2000 /* This function will probe and initialize a card */ 2001 static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id) 2002 { 2003 struct Scsi_Host *host = NULL; 2004 TW_Device_Extension *tw_dev; 2005 unsigned long mem_addr, mem_len; 2006 int retval; 2007 2008 retval = pci_enable_device(pdev); 2009 if (retval) { 2010 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device"); 2011 goto out_disable_device; 2012 } 2013 2014 pci_set_master(pdev); 2015 pci_try_set_mwi(pdev); 2016 2017 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 2018 if (retval) 2019 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 2020 if (retval) { 2021 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask"); 2022 retval = -ENODEV; 2023 goto out_disable_device; 2024 } 2025 2026 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension)); 2027 if (!host) { 2028 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension"); 2029 retval = -ENOMEM; 2030 goto out_disable_device; 2031 } 2032 tw_dev = (TW_Device_Extension *)host->hostdata; 2033 2034 /* Save values to device extension */ 2035 tw_dev->host = host; 2036 tw_dev->tw_pci_dev = pdev; 2037 2038 if (twa_initialize_device_extension(tw_dev)) { 2039 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension"); 2040 retval = -ENOMEM; 2041 goto out_free_device_extension; 2042 } 2043 2044 /* Request IO regions */ 2045 retval = pci_request_regions(pdev, "3w-9xxx"); 2046 if (retval) { 2047 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region"); 2048 goto out_free_device_extension; 2049 } 2050 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); 2054 } else { 2055 mem_addr = pci_resource_start(pdev, 2); 2056 mem_len = pci_resource_len(pdev, 2); 2057 } 2058 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 retval = -ENOMEM; 2064 goto out_release_mem_region; 2065 } 2066 2067 /* Disable interrupts on the card */ 2068 TW_DISABLE_INTERRUPTS(tw_dev); 2069 2070 /* Initialize the card */ 2071 if (twa_reset_sequence(tw_dev, 0)) { 2072 retval = -ENOMEM; 2073 goto out_iounmap; 2074 } 2075 2076 /* Set host specific parameters */ 2077 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) || 2078 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA)) 2079 host->max_id = TW_MAX_UNITS_9650SE; 2080 else 2081 host->max_id = TW_MAX_UNITS; 2082 2083 host->max_cmd_len = TW_MAX_CDB_LEN; 2084 2085 /* Channels aren't supported by adapter */ 2086 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl); 2087 host->max_channel = 0; 2088 2089 /* Register the card with the kernel SCSI layer */ 2090 retval = scsi_add_host(host, &pdev->dev); 2091 if (retval) { 2092 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed"); 2093 goto out_iounmap; 2094 } 2095 2096 pci_set_drvdata(pdev, host); 2097 2098 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n", 2099 host->host_no, mem_addr, pdev->irq); 2100 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n", 2101 host->host_no, 2102 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE, 2103 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH), 2104 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE, 2105 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH), 2106 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE, 2107 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH))); 2108 2109 /* Try to enable MSI */ 2110 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) && 2111 !pci_enable_msi(pdev)) 2112 set_bit(TW_USING_MSI, &tw_dev->flags); 2113 2114 /* Now setup the interrupt handler */ 2115 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev); 2116 if (retval) { 2117 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ"); 2118 goto out_remove_host; 2119 } 2120 2121 twa_device_extension_list[twa_device_extension_count] = tw_dev; 2122 twa_device_extension_count++; 2123 2124 /* Re-enable interrupts on the card */ 2125 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev); 2126 2127 /* Finally, scan the host */ 2128 scsi_scan_host(host); 2129 2130 if (twa_major == -1) { 2131 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0) 2132 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device"); 2133 } 2134 return 0; 2135 2136 out_remove_host: 2137 if (test_bit(TW_USING_MSI, &tw_dev->flags)) 2138 pci_disable_msi(pdev); 2139 scsi_remove_host(host); 2140 out_iounmap: 2141 iounmap(tw_dev->base_addr); 2142 out_release_mem_region: 2143 pci_release_regions(pdev); 2144 out_free_device_extension: 2145 twa_free_device_extension(tw_dev); 2146 scsi_host_put(host); 2147 out_disable_device: 2148 pci_disable_device(pdev); 2149 2150 return retval; 2151 } /* End twa_probe() */ 2152 2153 /* This function is called to remove a device */ 2154 static void twa_remove(struct pci_dev *pdev) 2155 { 2156 struct Scsi_Host *host = pci_get_drvdata(pdev); 2157 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata; 2158 2159 scsi_remove_host(tw_dev->host); 2160 2161 /* Unregister character device */ 2162 if (twa_major >= 0) { 2163 unregister_chrdev(twa_major, "twa"); 2164 twa_major = -1; 2165 } 2166 2167 /* Shutdown the card */ 2168 __twa_shutdown(tw_dev); 2169 2170 /* Disable MSI if enabled */ 2171 if (test_bit(TW_USING_MSI, &tw_dev->flags)) 2172 pci_disable_msi(pdev); 2173 2174 /* Free IO remapping */ 2175 iounmap(tw_dev->base_addr); 2176 2177 /* Free up the mem region */ 2178 pci_release_regions(pdev); 2179 2180 /* Free up device extension resources */ 2181 twa_free_device_extension(tw_dev); 2182 2183 scsi_host_put(tw_dev->host); 2184 pci_disable_device(pdev); 2185 twa_device_extension_count--; 2186 } /* End twa_remove() */ 2187 2188 /* This function is called on PCI suspend */ 2189 static int __maybe_unused twa_suspend(struct device *dev) 2190 { 2191 struct pci_dev *pdev = to_pci_dev(dev); 2192 struct Scsi_Host *host = pci_get_drvdata(pdev); 2193 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata; 2194 2195 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no); 2196 2197 TW_DISABLE_INTERRUPTS(tw_dev); 2198 free_irq(tw_dev->tw_pci_dev->irq, tw_dev); 2199 2200 if (test_bit(TW_USING_MSI, &tw_dev->flags)) 2201 pci_disable_msi(pdev); 2202 2203 /* Tell the card we are shutting down */ 2204 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) { 2205 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend"); 2206 } else { 2207 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n"); 2208 } 2209 TW_CLEAR_ALL_INTERRUPTS(tw_dev); 2210 2211 return 0; 2212 } /* End twa_suspend() */ 2213 2214 /* This function is called on PCI resume */ 2215 static int __maybe_unused twa_resume(struct device *dev) 2216 { 2217 int retval = 0; 2218 struct pci_dev *pdev = to_pci_dev(dev); 2219 struct Scsi_Host *host = pci_get_drvdata(pdev); 2220 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata; 2221 2222 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no); 2223 2224 pci_try_set_mwi(pdev); 2225 2226 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 2227 if (retval) 2228 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 2229 if (retval) { 2230 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume"); 2231 retval = -ENODEV; 2232 goto out_disable_device; 2233 } 2234 2235 /* Initialize the card */ 2236 if (twa_reset_sequence(tw_dev, 0)) { 2237 retval = -ENODEV; 2238 goto out_disable_device; 2239 } 2240 2241 /* Now setup the interrupt handler */ 2242 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev); 2243 if (retval) { 2244 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume"); 2245 retval = -ENODEV; 2246 goto out_disable_device; 2247 } 2248 2249 /* Now enable MSI if enabled */ 2250 if (test_bit(TW_USING_MSI, &tw_dev->flags)) 2251 pci_enable_msi(pdev); 2252 2253 /* Re-enable interrupts on the card */ 2254 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev); 2255 2256 printk(KERN_WARNING "3w-9xxx: Resume complete.\n"); 2257 return 0; 2258 2259 out_disable_device: 2260 scsi_remove_host(host); 2261 2262 return retval; 2263 } /* End twa_resume() */ 2264 2265 /* PCI Devices supported by this driver */ 2266 static struct pci_device_id twa_pci_tbl[] = { 2267 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000, 2268 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 2269 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX, 2270 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 2271 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE, 2272 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 2273 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA, 2274 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 2275 { } 2276 }; 2277 MODULE_DEVICE_TABLE(pci, twa_pci_tbl); 2278 2279 static SIMPLE_DEV_PM_OPS(twa_pm_ops, twa_suspend, twa_resume); 2280 2281 /* pci_driver initializer */ 2282 static struct pci_driver twa_driver = { 2283 .name = "3w-9xxx", 2284 .id_table = twa_pci_tbl, 2285 .probe = twa_probe, 2286 .remove = twa_remove, 2287 .driver.pm = &twa_pm_ops, 2288 .shutdown = twa_shutdown 2289 }; 2290 2291 /* This function is called on driver initialization */ 2292 static int __init twa_init(void) 2293 { 2294 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION); 2295 2296 return pci_register_driver(&twa_driver); 2297 } /* End twa_init() */ 2298 2299 /* This function is called on driver exit */ 2300 static void __exit twa_exit(void) 2301 { 2302 pci_unregister_driver(&twa_driver); 2303 } /* End twa_exit() */ 2304 2305 module_init(twa_init); 2306 module_exit(twa_exit); 2307 2308