2 3w-sas.c -- LSI 3ware SAS/SATA-RAID Controller device driver for Linux.
4 Written By: Adam Radford <linuxraid@lsi.com>
6 Copyright (C) 2009 LSI Corporation.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
18 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
19 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
20 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
21 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
22 solely responsible for determining the appropriateness of using and
23 distributing the Program and assumes all risks associated with its
24 exercise of rights under this Agreement, including but not limited to
25 the risks and costs of program errors, damage to or loss of data,
26 programs or equipment, and unavailability or interruption of operations.
28 DISCLAIMER OF LIABILITY
29 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
30 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
35 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37 You should have received a copy of the GNU General Public License
38 along with this program; if not, write to the Free Software
39 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 Controllers supported by this driver:
43 LSI 3ware 9750 6Gb/s SAS/SATA-RAID
45 Bugs/Comments/Suggestions should be mailed to:
48 For more information, goto:
53 3.26.02.000 - Initial driver release.
56 #include <linux/module.h>
57 #include <linux/reboot.h>
58 #include <linux/spinlock.h>
59 #include <linux/interrupt.h>
60 #include <linux/moduleparam.h>
61 #include <linux/errno.h>
62 #include <linux/types.h>
63 #include <linux/delay.h>
64 #include <linux/pci.h>
65 #include <linux/time.h>
66 #include <linux/mutex.h>
67 #include <linux/smp_lock.h>
68 #include <linux/slab.h>
71 #include <asm/uaccess.h>
72 #include <scsi/scsi.h>
73 #include <scsi/scsi_host.h>
74 #include <scsi/scsi_tcq.h>
75 #include <scsi/scsi_cmnd.h>
79 #define TW_DRIVER_VERSION "3.26.02.000"
80 static TW_Device_Extension *twl_device_extension_list[TW_MAX_SLOT];
81 static unsigned int twl_device_extension_count;
82 static int twl_major = -1;
83 extern struct timezone sys_tz;
85 /* Module parameters */
86 MODULE_AUTHOR ("LSI");
87 MODULE_DESCRIPTION ("LSI 3ware SAS/SATA-RAID Linux Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(TW_DRIVER_VERSION);
92 module_param(use_msi, int, S_IRUGO);
93 MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
95 /* Function prototypes */
96 static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset);
100 /* This function returns AENs through sysfs */
101 static ssize_t twl_sysfs_aen_read(struct file *filp, struct kobject *kobj,
102 struct bin_attribute *bin_attr,
103 char *outbuf, loff_t offset, size_t count)
105 struct device *dev = container_of(kobj, struct device, kobj);
106 struct Scsi_Host *shost = class_to_shost(dev);
107 TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
108 unsigned long flags = 0;
111 if (!capable(CAP_SYS_ADMIN))
114 spin_lock_irqsave(tw_dev->host->host_lock, flags);
115 ret = memory_read_from_buffer(outbuf, count, &offset, tw_dev->event_queue[0], sizeof(TW_Event) * TW_Q_LENGTH);
116 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
119 } /* End twl_sysfs_aen_read() */
121 /* aen_read sysfs attribute initializer */
122 static struct bin_attribute twl_sysfs_aen_read_attr = {
124 .name = "3ware_aen_read",
128 .read = twl_sysfs_aen_read
131 /* This function returns driver compatibility info through sysfs */
132 static ssize_t twl_sysfs_compat_info(struct file *filp, struct kobject *kobj,
133 struct bin_attribute *bin_attr,
134 char *outbuf, loff_t offset, size_t count)
136 struct device *dev = container_of(kobj, struct device, kobj);
137 struct Scsi_Host *shost = class_to_shost(dev);
138 TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
139 unsigned long flags = 0;
142 if (!capable(CAP_SYS_ADMIN))
145 spin_lock_irqsave(tw_dev->host->host_lock, flags);
146 ret = memory_read_from_buffer(outbuf, count, &offset, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
147 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
150 } /* End twl_sysfs_compat_info() */
152 /* compat_info sysfs attribute initializer */
153 static struct bin_attribute twl_sysfs_compat_info_attr = {
155 .name = "3ware_compat_info",
159 .read = twl_sysfs_compat_info
162 /* Show some statistics about the card */
163 static ssize_t twl_show_stats(struct device *dev,
164 struct device_attribute *attr, char *buf)
166 struct Scsi_Host *host = class_to_shost(dev);
167 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
168 unsigned long flags = 0;
171 spin_lock_irqsave(tw_dev->host->host_lock, flags);
172 len = snprintf(buf, PAGE_SIZE, "3w-sas Driver version: %s\n"
173 "Current commands posted: %4d\n"
174 "Max commands posted: %4d\n"
175 "Last sgl length: %4d\n"
176 "Max sgl length: %4d\n"
177 "Last sector count: %4d\n"
178 "Max sector count: %4d\n"
179 "SCSI Host Resets: %4d\n"
182 tw_dev->posted_request_count,
183 tw_dev->max_posted_request_count,
185 tw_dev->max_sgl_entries,
186 tw_dev->sector_count,
187 tw_dev->max_sector_count,
190 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
192 } /* End twl_show_stats() */
194 /* This function will set a devices queue depth */
195 static int twl_change_queue_depth(struct scsi_device *sdev, int queue_depth,
198 if (reason != SCSI_QDEPTH_DEFAULT)
201 if (queue_depth > TW_Q_LENGTH-2)
202 queue_depth = TW_Q_LENGTH-2;
203 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
205 } /* End twl_change_queue_depth() */
207 /* stats sysfs attribute initializer */
208 static struct device_attribute twl_host_stats_attr = {
210 .name = "3ware_stats",
213 .show = twl_show_stats
216 /* Host attributes initializer */
217 static struct device_attribute *twl_host_attrs[] = {
218 &twl_host_stats_attr,
222 /* This function will look up an AEN severity string */
223 static char *twl_aen_severity_lookup(unsigned char severity_code)
227 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
228 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
231 retval = twl_aen_severity_table[severity_code];
234 } /* End twl_aen_severity_lookup() */
236 /* This function will queue an event */
237 static void twl_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
248 /* Fill out event info */
249 event = tw_dev->event_queue[tw_dev->error_index];
253 sprintf(host, " scsi%d:", tw_dev->host->host_no);
255 aen = le16_to_cpu(header->status_block.error);
256 memset(event, 0, sizeof(TW_Event));
258 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
259 do_gettimeofday(&time);
260 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
261 event->time_stamp_sec = local_time;
262 event->aen_code = aen;
263 event->retrieved = TW_AEN_NOT_RETRIEVED;
264 event->sequence_id = tw_dev->error_sequence_id;
265 tw_dev->error_sequence_id++;
267 /* Check for embedded error string */
268 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
270 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
271 event->parameter_len = strlen(header->err_specific_desc);
272 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + 1 + strlen(error_str));
273 if (event->severity != TW_AEN_SEVERITY_DEBUG)
274 printk(KERN_WARNING "3w-sas:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
276 twl_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
277 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen, error_str,
278 header->err_specific_desc);
282 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
283 } /* End twl_aen_queue_event() */
285 /* This function will attempt to post a command packet to the board */
286 static int twl_post_command_packet(TW_Device_Extension *tw_dev, int request_id)
288 dma_addr_t command_que_value;
290 command_que_value = tw_dev->command_packet_phys[request_id];
291 command_que_value += TW_COMMAND_OFFSET;
293 /* First write upper 4 bytes */
294 writel((u32)((u64)command_que_value >> 32), TWL_HIBQPH_REG_ADDR(tw_dev));
295 /* Then the lower 4 bytes */
296 writel((u32)(command_que_value | TWL_PULL_MODE), TWL_HIBQPL_REG_ADDR(tw_dev));
298 tw_dev->state[request_id] = TW_S_POSTED;
299 tw_dev->posted_request_count++;
300 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count)
301 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
304 } /* End twl_post_command_packet() */
306 /* This function will perform a pci-dma mapping for a scatter gather list */
307 static int twl_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
310 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
312 use_sg = scsi_dma_map(cmd);
315 else if (use_sg < 0) {
316 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Failed to map scatter gather list");
320 cmd->SCp.phase = TW_PHASE_SGLIST;
321 cmd->SCp.have_data_in = use_sg;
324 } /* End twl_map_scsi_sg_data() */
326 /* This function hands scsi cdb's to the firmware */
327 static int twl_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry_ISO *sglistarg)
329 TW_Command_Full *full_command_packet;
330 TW_Command_Apache *command_packet;
332 struct scsi_cmnd *srb = NULL;
333 struct scatterlist *sglist = NULL, *sg;
336 if (tw_dev->srb[request_id]) {
337 srb = tw_dev->srb[request_id];
338 if (scsi_sglist(srb))
339 sglist = scsi_sglist(srb);
342 /* Initialize command packet */
343 full_command_packet = tw_dev->command_packet_virt[request_id];
344 full_command_packet->header.header_desc.size_header = 128;
345 full_command_packet->header.status_block.error = 0;
346 full_command_packet->header.status_block.severity__reserved = 0;
348 command_packet = &full_command_packet->command.newcommand;
349 command_packet->status = 0;
350 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
352 /* We forced 16 byte cdb use earlier */
354 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
356 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
359 command_packet->unit = srb->device->id;
360 command_packet->request_id__lunl =
361 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
363 command_packet->request_id__lunl =
364 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
365 command_packet->unit = 0;
368 command_packet->sgl_offset = 16;
371 /* Map sglist from scsi layer to cmd packet */
372 if (scsi_sg_count(srb)) {
373 sg_count = twl_map_scsi_sg_data(tw_dev, request_id);
377 scsi_for_each_sg(srb, sg, sg_count, i) {
378 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
379 command_packet->sg_list[i].length = TW_CPU_TO_SGL(sg_dma_len(sg));
381 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
384 /* Internal cdb post */
385 for (i = 0; i < use_sg; i++) {
386 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
387 command_packet->sg_list[i].length = TW_CPU_TO_SGL(sglistarg[i].length);
389 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
392 /* Update some stats */
394 tw_dev->sector_count = scsi_bufflen(srb) / 512;
395 if (tw_dev->sector_count > tw_dev->max_sector_count)
396 tw_dev->max_sector_count = tw_dev->sector_count;
397 tw_dev->sgl_entries = scsi_sg_count(srb);
398 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
399 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
402 /* Now post the command to the board */
403 retval = twl_post_command_packet(tw_dev, request_id);
407 } /* End twl_scsiop_execute_scsi() */
409 /* This function will read the aen queue from the isr */
410 static int twl_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
412 char cdb[TW_MAX_CDB_LEN];
413 TW_SG_Entry_ISO sglist[1];
414 TW_Command_Full *full_command_packet;
417 full_command_packet = tw_dev->command_packet_virt[request_id];
418 memset(full_command_packet, 0, sizeof(TW_Command_Full));
421 memset(&cdb, 0, TW_MAX_CDB_LEN);
422 cdb[0] = REQUEST_SENSE; /* opcode */
423 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
425 /* Initialize sglist */
426 memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
427 sglist[0].length = TW_SECTOR_SIZE;
428 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
430 /* Mark internal command */
431 tw_dev->srb[request_id] = NULL;
433 /* Now post the command packet */
434 if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
435 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Post failed while reading AEN queue");
441 } /* End twl_aen_read_queue() */
443 /* This function will sync firmware time with the host time */
444 static void twl_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
448 TW_Command_Full *full_command_packet;
449 TW_Command *command_packet;
450 TW_Param_Apache *param;
453 /* Fill out the command packet */
454 full_command_packet = tw_dev->command_packet_virt[request_id];
455 memset(full_command_packet, 0, sizeof(TW_Command_Full));
456 command_packet = &full_command_packet->command.oldcommand;
457 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
458 command_packet->request_id = request_id;
459 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
460 command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
461 command_packet->size = TW_COMMAND_SIZE;
462 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
464 /* Setup the param */
465 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
466 memset(param, 0, TW_SECTOR_SIZE);
467 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
468 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
469 param->parameter_size_bytes = cpu_to_le16(4);
471 /* Convert system time in UTC to local time seconds since last
473 do_gettimeofday(&utc);
474 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
475 schedulertime = local_time - (3 * 86400);
476 schedulertime = cpu_to_le32(schedulertime % 604800);
478 memcpy(param->data, &schedulertime, sizeof(u32));
480 /* Mark internal command */
481 tw_dev->srb[request_id] = NULL;
483 /* Now post the command */
484 twl_post_command_packet(tw_dev, request_id);
485 } /* End twl_aen_sync_time() */
487 /* This function will assign an available request id */
488 static void twl_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
490 *request_id = tw_dev->free_queue[tw_dev->free_head];
491 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
492 tw_dev->state[*request_id] = TW_S_STARTED;
493 } /* End twl_get_request_id() */
495 /* This function will free a request id */
496 static void twl_free_request_id(TW_Device_Extension *tw_dev, int request_id)
498 tw_dev->free_queue[tw_dev->free_tail] = request_id;
499 tw_dev->state[request_id] = TW_S_FINISHED;
500 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
501 } /* End twl_free_request_id() */
503 /* This function will complete an aen request from the isr */
504 static int twl_aen_complete(TW_Device_Extension *tw_dev, int request_id)
506 TW_Command_Full *full_command_packet;
507 TW_Command *command_packet;
508 TW_Command_Apache_Header *header;
512 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
513 tw_dev->posted_request_count--;
514 aen = le16_to_cpu(header->status_block.error);
515 full_command_packet = tw_dev->command_packet_virt[request_id];
516 command_packet = &full_command_packet->command.oldcommand;
518 /* First check for internal completion of set param for time sync */
519 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
520 /* Keep reading the queue in case there are more aen's */
521 if (twl_aen_read_queue(tw_dev, request_id))
530 case TW_AEN_QUEUE_EMPTY:
531 /* Quit reading the queue if this is the last one */
533 case TW_AEN_SYNC_TIME_WITH_HOST:
534 twl_aen_sync_time(tw_dev, request_id);
538 twl_aen_queue_event(tw_dev, header);
540 /* If there are more aen's, keep reading the queue */
541 if (twl_aen_read_queue(tw_dev, request_id))
550 tw_dev->state[request_id] = TW_S_COMPLETED;
551 twl_free_request_id(tw_dev, request_id);
552 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
555 } /* End twl_aen_complete() */
557 /* This function will poll for a response */
558 static int twl_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
560 unsigned long before;
570 if (sizeof(dma_addr_t) > 4) {
571 regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
572 regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
573 mfa = ((u64)regh << 32) | regl;
575 mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
579 if (TW_RESID_OUT(response) == request_id)
582 if (time_after(jiffies, before + HZ * seconds))
590 } /* End twl_poll_response() */
592 /* This function will drain the aen queue */
593 static int twl_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
596 char cdb[TW_MAX_CDB_LEN];
597 TW_SG_Entry_ISO sglist[1];
598 int finished = 0, count = 0;
599 TW_Command_Full *full_command_packet;
600 TW_Command_Apache_Header *header;
602 int first_reset = 0, queue = 0, retval = 1;
609 full_command_packet = tw_dev->command_packet_virt[request_id];
610 memset(full_command_packet, 0, sizeof(TW_Command_Full));
613 memset(&cdb, 0, TW_MAX_CDB_LEN);
614 cdb[0] = REQUEST_SENSE; /* opcode */
615 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
617 /* Initialize sglist */
618 memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
619 sglist[0].length = TW_SECTOR_SIZE;
620 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
622 /* Mark internal command */
623 tw_dev->srb[request_id] = NULL;
626 /* Send command to the board */
627 if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
628 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "Error posting request sense");
632 /* Now poll for completion */
633 if (twl_poll_response(tw_dev, request_id, 30)) {
634 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "No valid response while draining AEN queue");
635 tw_dev->posted_request_count--;
639 tw_dev->posted_request_count--;
640 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
641 aen = le16_to_cpu(header->status_block.error);
646 case TW_AEN_QUEUE_EMPTY:
647 if (first_reset != 1)
652 case TW_AEN_SOFT_RESET:
653 if (first_reset == 0)
658 case TW_AEN_SYNC_TIME_WITH_HOST:
664 /* Now queue an event info */
666 twl_aen_queue_event(tw_dev, header);
667 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
669 if (count == TW_MAX_AEN_DRAIN)
674 tw_dev->state[request_id] = TW_S_INITIAL;
676 } /* End twl_aen_drain_queue() */
678 /* This function will allocate memory and check if it is correctly aligned */
679 static int twl_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
682 dma_addr_t dma_handle;
683 unsigned long *cpu_addr;
686 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
688 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
692 memset(cpu_addr, 0, size*TW_Q_LENGTH);
694 for (i = 0; i < TW_Q_LENGTH; i++) {
697 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
698 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
701 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
702 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
705 tw_dev->sense_buffer_phys[i] = dma_handle+(i*size);
706 tw_dev->sense_buffer_virt[i] = (TW_Command_Apache_Header *)((unsigned char *)cpu_addr + (i*size));
713 } /* End twl_allocate_memory() */
715 /* This function will load the request id and various sgls for ioctls */
716 static void twl_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
718 TW_Command *oldcommand;
719 TW_Command_Apache *newcommand;
720 TW_SG_Entry_ISO *sgl;
721 unsigned int pae = 0;
723 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
726 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
727 newcommand = &full_command_packet->command.newcommand;
728 newcommand->request_id__lunl =
729 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
731 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
732 newcommand->sg_list[0].length = TW_CPU_TO_SGL(length);
734 newcommand->sgl_entries__lunh =
735 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
737 oldcommand = &full_command_packet->command.oldcommand;
738 oldcommand->request_id = request_id;
740 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
741 /* Load the sg list */
742 sgl = (TW_SG_Entry_ISO *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry_ISO)/4) + pae + (sizeof(dma_addr_t) > 4 ? 1 : 0));
743 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
744 sgl->length = TW_CPU_TO_SGL(length);
745 oldcommand->size += pae;
746 oldcommand->size += sizeof(dma_addr_t) > 4 ? 1 : 0;
749 } /* End twl_load_sgl() */
751 /* This function handles ioctl for the character device
752 This interface is used by smartmontools open source software */
753 static long twl_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
756 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
757 dma_addr_t dma_handle;
759 TW_Ioctl_Driver_Command driver_command;
760 struct inode *inode = file->f_dentry->d_inode;
761 TW_Ioctl_Buf_Apache *tw_ioctl;
762 TW_Command_Full *full_command_packet;
763 TW_Device_Extension *tw_dev = twl_device_extension_list[iminor(inode)];
764 int retval = -EFAULT;
765 void __user *argp = (void __user *)arg;
769 /* Only let one of these through at a time */
770 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
775 /* First copy down the driver command */
776 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
779 /* Check data buffer size */
780 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
785 /* Hardware can only do multiple of 512 byte transfers */
786 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
788 /* Now allocate ioctl buf memory */
789 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);
795 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
797 /* Now copy down the entire ioctl */
798 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
801 /* See which ioctl we are doing */
803 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
804 spin_lock_irqsave(tw_dev->host->host_lock, flags);
805 twl_get_request_id(tw_dev, &request_id);
807 /* Flag internal command */
808 tw_dev->srb[request_id] = NULL;
810 /* Flag chrdev ioctl */
811 tw_dev->chrdev_request_id = request_id;
813 full_command_packet = (TW_Command_Full *)&tw_ioctl->firmware_command;
815 /* Load request id and sglist for both command types */
816 twl_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
818 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
820 /* Now post the command packet to the controller */
821 twl_post_command_packet(tw_dev, request_id);
822 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
824 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
826 /* Now wait for command to complete */
827 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
829 /* We timed out, and didn't get an interrupt */
830 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
831 /* Now we need to reset the board */
832 printk(KERN_WARNING "3w-sas: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
833 tw_dev->host->host_no, TW_DRIVER, 0x6,
836 twl_reset_device_extension(tw_dev, 1);
840 /* Now copy in the command packet response */
841 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
843 /* Now complete the io */
844 spin_lock_irqsave(tw_dev->host->host_lock, flags);
845 tw_dev->posted_request_count--;
846 tw_dev->state[request_id] = TW_S_COMPLETED;
847 twl_free_request_id(tw_dev, request_id);
848 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
855 /* Now copy the entire response to userspace */
856 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
859 /* Now free ioctl buf memory */
860 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
862 mutex_unlock(&tw_dev->ioctl_lock);
866 } /* End twl_chrdev_ioctl() */
868 /* This function handles open for the character device */
869 static int twl_chrdev_open(struct inode *inode, struct file *file)
871 unsigned int minor_number;
872 int retval = -ENODEV;
874 if (!capable(CAP_SYS_ADMIN)) {
880 minor_number = iminor(inode);
881 if (minor_number >= twl_device_extension_count)
886 } /* End twl_chrdev_open() */
888 /* File operations struct for character device */
889 static const struct file_operations twl_fops = {
890 .owner = THIS_MODULE,
891 .unlocked_ioctl = twl_chrdev_ioctl,
892 .open = twl_chrdev_open,
894 .llseek = noop_llseek,
897 /* This function passes sense data from firmware to scsi layer */
898 static int twl_fill_sense(TW_Device_Extension *tw_dev, int i, int request_id, int copy_sense, int print_host)
900 TW_Command_Apache_Header *header;
901 TW_Command_Full *full_command_packet;
902 unsigned short error;
906 header = tw_dev->sense_buffer_virt[i];
907 full_command_packet = tw_dev->command_packet_virt[request_id];
909 /* Get embedded firmware error string */
910 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc) + 1]);
912 /* Don't print error for Logical unit not supported during rollcall */
913 error = le16_to_cpu(header->status_block.error);
914 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE) && (error != TW_ERROR_INVALID_FIELD_IN_CDB)) {
916 printk(KERN_WARNING "3w-sas: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
917 tw_dev->host->host_no,
918 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
919 header->status_block.error,
921 header->err_specific_desc);
923 printk(KERN_WARNING "3w-sas: ERROR: (0x%02X:0x%04X): %s:%s.\n",
924 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
925 header->status_block.error,
927 header->err_specific_desc);
931 memcpy(tw_dev->srb[request_id]->sense_buffer, header->sense_data, TW_SENSE_DATA_LENGTH);
932 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
937 } /* End twl_fill_sense() */
939 /* This function will free up device extension resources */
940 static void twl_free_device_extension(TW_Device_Extension *tw_dev)
942 if (tw_dev->command_packet_virt[0])
943 pci_free_consistent(tw_dev->tw_pci_dev,
944 sizeof(TW_Command_Full)*TW_Q_LENGTH,
945 tw_dev->command_packet_virt[0],
946 tw_dev->command_packet_phys[0]);
948 if (tw_dev->generic_buffer_virt[0])
949 pci_free_consistent(tw_dev->tw_pci_dev,
950 TW_SECTOR_SIZE*TW_Q_LENGTH,
951 tw_dev->generic_buffer_virt[0],
952 tw_dev->generic_buffer_phys[0]);
954 if (tw_dev->sense_buffer_virt[0])
955 pci_free_consistent(tw_dev->tw_pci_dev,
956 sizeof(TW_Command_Apache_Header)*
958 tw_dev->sense_buffer_virt[0],
959 tw_dev->sense_buffer_phys[0]);
961 kfree(tw_dev->event_queue[0]);
962 } /* End twl_free_device_extension() */
964 /* This function will get parameter table entries from the firmware */
965 static void *twl_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
967 TW_Command_Full *full_command_packet;
968 TW_Command *command_packet;
969 TW_Param_Apache *param;
972 /* Setup the command packet */
973 full_command_packet = tw_dev->command_packet_virt[request_id];
974 memset(full_command_packet, 0, sizeof(TW_Command_Full));
975 command_packet = &full_command_packet->command.oldcommand;
977 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
978 command_packet->size = TW_COMMAND_SIZE;
979 command_packet->request_id = request_id;
980 command_packet->byte6_offset.block_count = cpu_to_le16(1);
982 /* Now setup the param */
983 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
984 memset(param, 0, TW_SECTOR_SIZE);
985 param->table_id = cpu_to_le16(table_id | 0x8000);
986 param->parameter_id = cpu_to_le16(parameter_id);
987 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
989 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
990 command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
992 /* Post the command packet to the board */
993 twl_post_command_packet(tw_dev, request_id);
995 /* Poll for completion */
996 if (twl_poll_response(tw_dev, request_id, 30))
997 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "No valid response during get param")
999 retval = (void *)&(param->data[0]);
1001 tw_dev->posted_request_count--;
1002 tw_dev->state[request_id] = TW_S_INITIAL;
1005 } /* End twl_get_param() */
1007 /* This function will send an initconnection command to controller */
1008 static int twl_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1009 u32 set_features, unsigned short current_fw_srl,
1010 unsigned short current_fw_arch_id,
1011 unsigned short current_fw_branch,
1012 unsigned short current_fw_build,
1013 unsigned short *fw_on_ctlr_srl,
1014 unsigned short *fw_on_ctlr_arch_id,
1015 unsigned short *fw_on_ctlr_branch,
1016 unsigned short *fw_on_ctlr_build,
1017 u32 *init_connect_result)
1019 TW_Command_Full *full_command_packet;
1020 TW_Initconnect *tw_initconnect;
1021 int request_id = 0, retval = 1;
1023 /* Initialize InitConnection command packet */
1024 full_command_packet = tw_dev->command_packet_virt[request_id];
1025 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1026 full_command_packet->header.header_desc.size_header = 128;
1028 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1029 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1030 tw_initconnect->request_id = request_id;
1031 tw_initconnect->message_credits = cpu_to_le16(message_credits);
1032 tw_initconnect->features = set_features;
1034 /* Turn on 64-bit sgl support if we need to */
1035 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1037 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1039 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1040 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1041 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1042 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1043 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1044 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1046 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1048 /* Send command packet to the board */
1049 twl_post_command_packet(tw_dev, request_id);
1051 /* Poll for completion */
1052 if (twl_poll_response(tw_dev, request_id, 30)) {
1053 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x8, "No valid response during init connection");
1055 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1056 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1057 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1058 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1059 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1060 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1065 tw_dev->posted_request_count--;
1066 tw_dev->state[request_id] = TW_S_INITIAL;
1069 } /* End twl_initconnection() */
1071 /* This function will initialize the fields of a device extension */
1072 static int twl_initialize_device_extension(TW_Device_Extension *tw_dev)
1076 /* Initialize command packet buffers */
1077 if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1078 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x9, "Command packet memory allocation failed");
1082 /* Initialize generic buffer */
1083 if (twl_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1084 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Generic memory allocation failed");
1088 /* Allocate sense buffers */
1089 if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Apache_Header), 2)) {
1090 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xb, "Sense buffer allocation failed");
1094 /* Allocate event info space */
1095 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1096 if (!tw_dev->event_queue[0]) {
1097 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "Event info memory allocation failed");
1101 for (i = 0; i < TW_Q_LENGTH; i++) {
1102 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1103 tw_dev->free_queue[i] = i;
1104 tw_dev->state[i] = TW_S_INITIAL;
1107 tw_dev->free_head = TW_Q_START;
1108 tw_dev->free_tail = TW_Q_START;
1109 tw_dev->error_sequence_id = 1;
1110 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1112 mutex_init(&tw_dev->ioctl_lock);
1113 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1118 } /* End twl_initialize_device_extension() */
1120 /* This function will perform a pci-dma unmap */
1121 static void twl_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1123 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1125 if (cmd->SCp.phase == TW_PHASE_SGLIST)
1126 scsi_dma_unmap(cmd);
1127 } /* End twl_unmap_scsi_data() */
1129 /* This function will handle attention interrupts */
1130 static int twl_handle_attention_interrupt(TW_Device_Extension *tw_dev)
1133 u32 request_id, doorbell;
1135 /* Read doorbell status */
1136 doorbell = readl(TWL_HOBDB_REG_ADDR(tw_dev));
1138 /* Check for controller errors */
1139 if (doorbell & TWL_DOORBELL_CONTROLLER_ERROR) {
1140 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "Microcontroller Error: clearing");
1144 /* Check if we need to perform an AEN drain */
1145 if (doorbell & TWL_DOORBELL_ATTENTION_INTERRUPT) {
1146 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1147 twl_get_request_id(tw_dev, &request_id);
1148 if (twl_aen_read_queue(tw_dev, request_id)) {
1149 tw_dev->state[request_id] = TW_S_COMPLETED;
1150 twl_free_request_id(tw_dev, request_id);
1151 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1158 /* Clear doorbell interrupt */
1159 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1161 /* Make sure the clear was flushed by reading it back */
1162 readl(TWL_HOBDBC_REG_ADDR(tw_dev));
1165 } /* End twl_handle_attention_interrupt() */
1167 /* Interrupt service routine */
1168 static irqreturn_t twl_interrupt(int irq, void *dev_instance)
1170 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1171 int i, handled = 0, error = 0;
1173 u32 reg, regl, regh, response, request_id = 0;
1174 struct scsi_cmnd *cmd;
1175 TW_Command_Full *full_command_packet;
1177 spin_lock(tw_dev->host->host_lock);
1179 /* Read host interrupt status */
1180 reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1182 /* Check if this is our interrupt, otherwise bail */
1183 if (!(reg & TWL_HISTATUS_VALID_INTERRUPT))
1184 goto twl_interrupt_bail;
1188 /* If we are resetting, bail */
1189 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1190 goto twl_interrupt_bail;
1192 /* Attention interrupt */
1193 if (reg & TWL_HISTATUS_ATTENTION_INTERRUPT) {
1194 if (twl_handle_attention_interrupt(tw_dev)) {
1195 TWL_MASK_INTERRUPTS(tw_dev);
1196 goto twl_interrupt_bail;
1200 /* Response interrupt */
1201 while (reg & TWL_HISTATUS_RESPONSE_INTERRUPT) {
1202 if (sizeof(dma_addr_t) > 4) {
1203 regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
1204 regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1205 mfa = ((u64)regh << 32) | regl;
1207 mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1210 response = (u32)mfa;
1212 /* Check for command packet error */
1213 if (!TW_NOTMFA_OUT(response)) {
1214 for (i=0;i<TW_Q_LENGTH;i++) {
1215 if (tw_dev->sense_buffer_phys[i] == mfa) {
1216 request_id = le16_to_cpu(tw_dev->sense_buffer_virt[i]->header_desc.request_id);
1217 if (tw_dev->srb[request_id] != NULL)
1218 error = twl_fill_sense(tw_dev, i, request_id, 1, 1);
1220 /* Skip ioctl error prints */
1221 if (request_id != tw_dev->chrdev_request_id)
1222 error = twl_fill_sense(tw_dev, i, request_id, 0, 1);
1224 memcpy(tw_dev->command_packet_virt[request_id], tw_dev->sense_buffer_virt[i], sizeof(TW_Command_Apache_Header));
1227 /* Now re-post the sense buffer */
1228 writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1229 writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1234 request_id = TW_RESID_OUT(response);
1236 full_command_packet = tw_dev->command_packet_virt[request_id];
1238 /* Check for correct state */
1239 if (tw_dev->state[request_id] != TW_S_POSTED) {
1240 if (tw_dev->srb[request_id] != NULL) {
1241 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Received a request id that wasn't posted");
1242 TWL_MASK_INTERRUPTS(tw_dev);
1243 goto twl_interrupt_bail;
1247 /* Check for internal command completion */
1248 if (tw_dev->srb[request_id] == NULL) {
1249 if (request_id != tw_dev->chrdev_request_id) {
1250 if (twl_aen_complete(tw_dev, request_id))
1251 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xf, "Error completing AEN during attention interrupt");
1253 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1254 wake_up(&tw_dev->ioctl_wqueue);
1257 cmd = tw_dev->srb[request_id];
1260 cmd->result = (DID_OK << 16);
1262 /* Report residual bytes for single sgl */
1263 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1264 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1265 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1268 /* Now complete the io */
1269 tw_dev->state[request_id] = TW_S_COMPLETED;
1270 twl_free_request_id(tw_dev, request_id);
1271 tw_dev->posted_request_count--;
1272 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1273 twl_unmap_scsi_data(tw_dev, request_id);
1276 /* Check for another response interrupt */
1277 reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1281 spin_unlock(tw_dev->host->host_lock);
1282 return IRQ_RETVAL(handled);
1283 } /* End twl_interrupt() */
1285 /* This function will poll for a register change */
1286 static int twl_poll_register(TW_Device_Extension *tw_dev, void *reg, u32 value, u32 result, int seconds)
1288 unsigned long before;
1292 reg_value = readl(reg);
1295 while ((reg_value & value) != result) {
1296 reg_value = readl(reg);
1297 if (time_after(jiffies, before + HZ * seconds))
1304 } /* End twl_poll_register() */
1306 /* This function will reset a controller */
1307 static int twl_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1312 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
1313 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
1314 u32 init_connect_result = 0;
1316 int do_soft_reset = soft_reset;
1318 while (tries < TW_MAX_RESET_TRIES) {
1319 /* Do a soft reset if one is needed */
1320 if (do_soft_reset) {
1321 TWL_SOFT_RESET(tw_dev);
1323 /* Make sure controller is in a good state */
1324 if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, 0x0, 30)) {
1325 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Controller never went non-ready during reset sequence");
1329 if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, TWL_CONTROLLER_READY, 60)) {
1330 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x11, "Controller not ready during reset sequence");
1337 if (twl_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
1338 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
1339 TW_9750_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
1340 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
1341 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1342 &fw_on_ctlr_build, &init_connect_result)) {
1343 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x12, "Initconnection failed while checking SRL");
1349 /* Load sense buffers */
1350 while (i < TW_Q_LENGTH) {
1351 writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1352 writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1354 /* Check status for over-run after each write */
1355 status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1356 if (!(status & TWL_STATUS_OVERRUN_SUBMIT))
1360 /* Now check status */
1361 status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1363 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "Bad controller status after loading sense buffers");
1369 /* Drain the AEN queue */
1370 if (twl_aen_drain_queue(tw_dev, soft_reset)) {
1371 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x14, "AEN drain failed during reset sequence");
1377 /* Load rest of compatibility struct */
1378 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
1379 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
1380 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
1381 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
1382 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
1383 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
1384 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
1385 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
1386 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
1387 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
1389 /* If we got here, controller is in a good state */
1395 } /* End twl_reset_sequence() */
1397 /* This function will reset a device extension */
1398 static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset)
1400 int i = 0, retval = 1;
1401 unsigned long flags = 0;
1403 /* Block SCSI requests while we are resetting */
1405 scsi_block_requests(tw_dev->host);
1407 set_bit(TW_IN_RESET, &tw_dev->flags);
1408 TWL_MASK_INTERRUPTS(tw_dev);
1409 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1411 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1413 /* Abort all requests that are in progress */
1414 for (i = 0; i < TW_Q_LENGTH; i++) {
1415 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1416 (tw_dev->state[i] != TW_S_INITIAL) &&
1417 (tw_dev->state[i] != TW_S_COMPLETED)) {
1418 if (tw_dev->srb[i]) {
1419 tw_dev->srb[i]->result = (DID_RESET << 16);
1420 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1421 twl_unmap_scsi_data(tw_dev, i);
1426 /* Reset queues and counts */
1427 for (i = 0; i < TW_Q_LENGTH; i++) {
1428 tw_dev->free_queue[i] = i;
1429 tw_dev->state[i] = TW_S_INITIAL;
1431 tw_dev->free_head = TW_Q_START;
1432 tw_dev->free_tail = TW_Q_START;
1433 tw_dev->posted_request_count = 0;
1435 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1437 if (twl_reset_sequence(tw_dev, 1))
1440 TWL_UNMASK_INTERRUPTS(tw_dev);
1442 clear_bit(TW_IN_RESET, &tw_dev->flags);
1443 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1448 scsi_unblock_requests(tw_dev->host);
1450 } /* End twl_reset_device_extension() */
1452 /* This funciton returns unit geometry in cylinders/heads/sectors */
1453 static int twl_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1456 TW_Device_Extension *tw_dev;
1458 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1460 if (capacity >= 0x200000) {
1470 geom[2] = sector_div(capacity, heads * sectors); /* cylinders */
1473 } /* End twl_scsi_biosparam() */
1475 /* This is the new scsi eh reset function */
1476 static int twl_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1478 TW_Device_Extension *tw_dev = NULL;
1479 int retval = FAILED;
1481 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1483 tw_dev->num_resets++;
1485 sdev_printk(KERN_WARNING, SCpnt->device,
1486 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1487 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1489 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1490 mutex_lock(&tw_dev->ioctl_lock);
1492 /* Now reset the card and some of the device extension data */
1493 if (twl_reset_device_extension(tw_dev, 0)) {
1494 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "Controller reset failed during scsi host reset");
1500 mutex_unlock(&tw_dev->ioctl_lock);
1502 } /* End twl_scsi_eh_reset() */
1504 /* This is the main scsi queue function to handle scsi opcodes */
1505 static int twl_scsi_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1507 int request_id, retval;
1508 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1510 /* If we are resetting due to timed out ioctl, report as busy */
1511 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1512 retval = SCSI_MLQUEUE_HOST_BUSY;
1516 /* Save done function into scsi_cmnd struct */
1517 SCpnt->scsi_done = done;
1519 /* Get a free request id */
1520 twl_get_request_id(tw_dev, &request_id);
1522 /* Save the scsi command for use by the ISR */
1523 tw_dev->srb[request_id] = SCpnt;
1525 /* Initialize phase to zero */
1526 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1528 retval = twl_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1530 tw_dev->state[request_id] = TW_S_COMPLETED;
1531 twl_free_request_id(tw_dev, request_id);
1532 SCpnt->result = (DID_ERROR << 16);
1538 } /* End twl_scsi_queue() */
1540 /* This function tells the controller to shut down */
1541 static void __twl_shutdown(TW_Device_Extension *tw_dev)
1543 /* Disable interrupts */
1544 TWL_MASK_INTERRUPTS(tw_dev);
1546 /* Free up the IRQ */
1547 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1549 printk(KERN_WARNING "3w-sas: Shutting down host %d.\n", tw_dev->host->host_no);
1551 /* Tell the card we are shutting down */
1552 if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1553 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Connection shutdown failed");
1555 printk(KERN_WARNING "3w-sas: Shutdown complete.\n");
1558 /* Clear doorbell interrupt just before exit */
1559 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1560 } /* End __twl_shutdown() */
1562 /* Wrapper for __twl_shutdown */
1563 static void twl_shutdown(struct pci_dev *pdev)
1565 struct Scsi_Host *host = pci_get_drvdata(pdev);
1566 TW_Device_Extension *tw_dev;
1571 tw_dev = (TW_Device_Extension *)host->hostdata;
1574 __twl_shutdown(tw_dev);
1575 } /* End twl_shutdown() */
1577 /* This function configures unit settings when a unit is coming on-line */
1578 static int twl_slave_configure(struct scsi_device *sdev)
1580 /* Force 60 second timeout */
1581 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1584 } /* End twl_slave_configure() */
1586 /* scsi_host_template initializer */
1587 static struct scsi_host_template driver_template = {
1588 .module = THIS_MODULE,
1590 .queuecommand = twl_scsi_queue,
1591 .eh_host_reset_handler = twl_scsi_eh_reset,
1592 .bios_param = twl_scsi_biosparam,
1593 .change_queue_depth = twl_change_queue_depth,
1594 .can_queue = TW_Q_LENGTH-2,
1595 .slave_configure = twl_slave_configure,
1597 .sg_tablesize = TW_LIBERATOR_MAX_SGL_LENGTH,
1598 .max_sectors = TW_MAX_SECTORS,
1599 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1600 .use_clustering = ENABLE_CLUSTERING,
1601 .shost_attrs = twl_host_attrs,
1605 /* This function will probe and initialize a card */
1606 static int __devinit twl_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1608 struct Scsi_Host *host = NULL;
1609 TW_Device_Extension *tw_dev;
1610 int retval = -ENODEV;
1611 int *ptr_phycount, phycount=0;
1613 retval = pci_enable_device(pdev);
1615 TW_PRINTK(host, TW_DRIVER, 0x17, "Failed to enable pci device");
1616 goto out_disable_device;
1619 pci_set_master(pdev);
1620 pci_try_set_mwi(pdev);
1622 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
1623 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1624 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1625 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1626 TW_PRINTK(host, TW_DRIVER, 0x18, "Failed to set dma mask");
1628 goto out_disable_device;
1631 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
1633 TW_PRINTK(host, TW_DRIVER, 0x19, "Failed to allocate memory for device extension");
1635 goto out_disable_device;
1637 tw_dev = shost_priv(host);
1639 /* Save values to device extension */
1640 tw_dev->host = host;
1641 tw_dev->tw_pci_dev = pdev;
1643 if (twl_initialize_device_extension(tw_dev)) {
1644 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Failed to initialize device extension");
1645 goto out_free_device_extension;
1648 /* Request IO regions */
1649 retval = pci_request_regions(pdev, "3w-sas");
1651 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Failed to get mem region");
1652 goto out_free_device_extension;
1655 /* Save base address, use region 1 */
1656 tw_dev->base_addr = pci_iomap(pdev, 1, 0);
1657 if (!tw_dev->base_addr) {
1658 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to ioremap");
1659 goto out_release_mem_region;
1662 /* Disable interrupts on the card */
1663 TWL_MASK_INTERRUPTS(tw_dev);
1665 /* Initialize the card */
1666 if (twl_reset_sequence(tw_dev, 0)) {
1667 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1d, "Controller reset failed during probe");
1671 /* Set host specific parameters */
1672 host->max_id = TW_MAX_UNITS;
1673 host->max_cmd_len = TW_MAX_CDB_LEN;
1674 host->max_lun = TW_MAX_LUNS;
1675 host->max_channel = 0;
1677 /* Register the card with the kernel SCSI layer */
1678 retval = scsi_add_host(host, &pdev->dev);
1680 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "scsi add host failed");
1684 pci_set_drvdata(pdev, host);
1686 printk(KERN_WARNING "3w-sas: scsi%d: Found an LSI 3ware %s Controller at 0x%llx, IRQ: %d.\n",
1688 (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1689 TW_PARAM_MODEL, TW_PARAM_MODEL_LENGTH),
1690 (u64)pci_resource_start(pdev, 1), pdev->irq);
1692 ptr_phycount = twl_get_param(tw_dev, 2, TW_PARAM_PHY_SUMMARY_TABLE,
1693 TW_PARAM_PHYCOUNT, TW_PARAM_PHYCOUNT_LENGTH);
1695 phycount = le32_to_cpu(*(int *)ptr_phycount);
1697 printk(KERN_WARNING "3w-sas: scsi%d: Firmware %s, BIOS %s, Phys: %d.\n",
1699 (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1700 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
1701 (char *)twl_get_param(tw_dev, 2, TW_VERSION_TABLE,
1702 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
1705 /* Try to enable MSI */
1706 if (use_msi && !pci_enable_msi(pdev))
1707 set_bit(TW_USING_MSI, &tw_dev->flags);
1709 /* Now setup the interrupt handler */
1710 retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1712 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Error requesting IRQ");
1713 goto out_remove_host;
1716 twl_device_extension_list[twl_device_extension_count] = tw_dev;
1717 twl_device_extension_count++;
1719 /* Re-enable interrupts on the card */
1720 TWL_UNMASK_INTERRUPTS(tw_dev);
1722 /* Finally, scan the host */
1723 scsi_scan_host(host);
1725 /* Add sysfs binary files */
1726 if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr))
1727 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Failed to create sysfs binary file: 3ware_aen_read");
1728 if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr))
1729 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Failed to create sysfs binary file: 3ware_compat_info");
1731 if (twl_major == -1) {
1732 if ((twl_major = register_chrdev (0, "twl", &twl_fops)) < 0)
1733 TW_PRINTK(host, TW_DRIVER, 0x22, "Failed to register character device");
1739 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1740 pci_disable_msi(pdev);
1741 scsi_remove_host(host);
1743 iounmap(tw_dev->base_addr);
1744 out_release_mem_region:
1745 pci_release_regions(pdev);
1746 out_free_device_extension:
1747 twl_free_device_extension(tw_dev);
1748 scsi_host_put(host);
1750 pci_disable_device(pdev);
1753 } /* End twl_probe() */
1755 /* This function is called to remove a device */
1756 static void twl_remove(struct pci_dev *pdev)
1758 struct Scsi_Host *host = pci_get_drvdata(pdev);
1759 TW_Device_Extension *tw_dev;
1764 tw_dev = (TW_Device_Extension *)host->hostdata;
1766 if (!tw_dev->online)
1769 /* Remove sysfs binary files */
1770 sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr);
1771 sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr);
1773 scsi_remove_host(tw_dev->host);
1775 /* Unregister character device */
1776 if (twl_major >= 0) {
1777 unregister_chrdev(twl_major, "twl");
1781 /* Shutdown the card */
1782 __twl_shutdown(tw_dev);
1784 /* Disable MSI if enabled */
1785 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1786 pci_disable_msi(pdev);
1788 /* Free IO remapping */
1789 iounmap(tw_dev->base_addr);
1791 /* Free up the mem region */
1792 pci_release_regions(pdev);
1794 /* Free up device extension resources */
1795 twl_free_device_extension(tw_dev);
1797 scsi_host_put(tw_dev->host);
1798 pci_disable_device(pdev);
1799 twl_device_extension_count--;
1800 } /* End twl_remove() */
1803 /* This function is called on PCI suspend */
1804 static int twl_suspend(struct pci_dev *pdev, pm_message_t state)
1806 struct Scsi_Host *host = pci_get_drvdata(pdev);
1807 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1809 printk(KERN_WARNING "3w-sas: Suspending host %d.\n", tw_dev->host->host_no);
1810 /* Disable interrupts */
1811 TWL_MASK_INTERRUPTS(tw_dev);
1813 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1815 /* Tell the card we are shutting down */
1816 if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1817 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x23, "Connection shutdown failed during suspend");
1819 printk(KERN_WARNING "3w-sas: Suspend complete.\n");
1822 /* Clear doorbell interrupt */
1823 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1825 pci_save_state(pdev);
1826 pci_disable_device(pdev);
1827 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1830 } /* End twl_suspend() */
1832 /* This function is called on PCI resume */
1833 static int twl_resume(struct pci_dev *pdev)
1836 struct Scsi_Host *host = pci_get_drvdata(pdev);
1837 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1839 printk(KERN_WARNING "3w-sas: Resuming host %d.\n", tw_dev->host->host_no);
1840 pci_set_power_state(pdev, PCI_D0);
1841 pci_enable_wake(pdev, PCI_D0, 0);
1842 pci_restore_state(pdev);
1844 retval = pci_enable_device(pdev);
1846 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x24, "Enable device failed during resume");
1850 pci_set_master(pdev);
1851 pci_try_set_mwi(pdev);
1853 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
1854 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1855 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1856 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1857 TW_PRINTK(host, TW_DRIVER, 0x25, "Failed to set dma mask during resume");
1859 goto out_disable_device;
1862 /* Initialize the card */
1863 if (twl_reset_sequence(tw_dev, 0)) {
1865 goto out_disable_device;
1868 /* Now setup the interrupt handler */
1869 retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1871 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Error requesting IRQ during resume");
1873 goto out_disable_device;
1876 /* Now enable MSI if enabled */
1877 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1878 pci_enable_msi(pdev);
1880 /* Re-enable interrupts on the card */
1881 TWL_UNMASK_INTERRUPTS(tw_dev);
1883 printk(KERN_WARNING "3w-sas: Resume complete.\n");
1887 scsi_remove_host(host);
1888 pci_disable_device(pdev);
1891 } /* End twl_resume() */
1894 /* PCI Devices supported by this driver */
1895 static struct pci_device_id twl_pci_tbl[] __devinitdata = {
1896 { PCI_VDEVICE(3WARE, PCI_DEVICE_ID_3WARE_9750) },
1899 MODULE_DEVICE_TABLE(pci, twl_pci_tbl);
1901 /* pci_driver initializer */
1902 static struct pci_driver twl_driver = {
1904 .id_table = twl_pci_tbl,
1906 .remove = twl_remove,
1908 .suspend = twl_suspend,
1909 .resume = twl_resume,
1911 .shutdown = twl_shutdown
1914 /* This function is called on driver initialization */
1915 static int __init twl_init(void)
1917 printk(KERN_INFO "LSI 3ware SAS/SATA-RAID Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
1919 return pci_register_driver(&twl_driver);
1920 } /* End twl_init() */
1922 /* This function is called on driver exit */
1923 static void __exit twl_exit(void)
1925 pci_unregister_driver(&twl_driver);
1926 } /* End twl_exit() */
1928 module_init(twl_init);
1929 module_exit(twl_exit);