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/slab.h>
70 #include <asm/uaccess.h>
71 #include <scsi/scsi.h>
72 #include <scsi/scsi_host.h>
73 #include <scsi/scsi_tcq.h>
74 #include <scsi/scsi_cmnd.h>
78 #define TW_DRIVER_VERSION "3.26.02.000"
79 static DEFINE_MUTEX(twl_chrdev_mutex
);
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 scsi_adjust_queue_depth(sdev
, queue_depth
);
203 } /* End twl_change_queue_depth() */
205 /* stats sysfs attribute initializer */
206 static struct device_attribute twl_host_stats_attr
= {
208 .name
= "3ware_stats",
211 .show
= twl_show_stats
214 /* Host attributes initializer */
215 static struct device_attribute
*twl_host_attrs
[] = {
216 &twl_host_stats_attr
,
220 /* This function will look up an AEN severity string */
221 static char *twl_aen_severity_lookup(unsigned char severity_code
)
225 if ((severity_code
< (unsigned char) TW_AEN_SEVERITY_ERROR
) ||
226 (severity_code
> (unsigned char) TW_AEN_SEVERITY_DEBUG
))
229 retval
= twl_aen_severity_table
[severity_code
];
232 } /* End twl_aen_severity_lookup() */
234 /* This function will queue an event */
235 static void twl_aen_queue_event(TW_Device_Extension
*tw_dev
, TW_Command_Apache_Header
*header
)
246 /* Fill out event info */
247 event
= tw_dev
->event_queue
[tw_dev
->error_index
];
251 sprintf(host
, " scsi%d:", tw_dev
->host
->host_no
);
253 aen
= le16_to_cpu(header
->status_block
.error
);
254 memset(event
, 0, sizeof(TW_Event
));
256 event
->severity
= TW_SEV_OUT(header
->status_block
.severity__reserved
);
257 do_gettimeofday(&time
);
258 local_time
= (u32
)(time
.tv_sec
- (sys_tz
.tz_minuteswest
* 60));
259 event
->time_stamp_sec
= local_time
;
260 event
->aen_code
= aen
;
261 event
->retrieved
= TW_AEN_NOT_RETRIEVED
;
262 event
->sequence_id
= tw_dev
->error_sequence_id
;
263 tw_dev
->error_sequence_id
++;
265 /* Check for embedded error string */
266 error_str
= &(header
->err_specific_desc
[strlen(header
->err_specific_desc
)+1]);
268 header
->err_specific_desc
[sizeof(header
->err_specific_desc
) - 1] = '\0';
269 event
->parameter_len
= strlen(header
->err_specific_desc
);
270 memcpy(event
->parameter_data
, header
->err_specific_desc
, event
->parameter_len
+ 1 + strlen(error_str
));
271 if (event
->severity
!= TW_AEN_SEVERITY_DEBUG
)
272 printk(KERN_WARNING
"3w-sas:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
274 twl_aen_severity_lookup(TW_SEV_OUT(header
->status_block
.severity__reserved
)),
275 TW_MESSAGE_SOURCE_CONTROLLER_EVENT
, aen
, error_str
,
276 header
->err_specific_desc
);
280 tw_dev
->error_index
= (tw_dev
->error_index
+ 1 ) % TW_Q_LENGTH
;
281 } /* End twl_aen_queue_event() */
283 /* This function will attempt to post a command packet to the board */
284 static int twl_post_command_packet(TW_Device_Extension
*tw_dev
, int request_id
)
286 dma_addr_t command_que_value
;
288 command_que_value
= tw_dev
->command_packet_phys
[request_id
];
289 command_que_value
+= TW_COMMAND_OFFSET
;
291 /* First write upper 4 bytes */
292 writel((u32
)((u64
)command_que_value
>> 32), TWL_HIBQPH_REG_ADDR(tw_dev
));
293 /* Then the lower 4 bytes */
294 writel((u32
)(command_que_value
| TWL_PULL_MODE
), TWL_HIBQPL_REG_ADDR(tw_dev
));
296 tw_dev
->state
[request_id
] = TW_S_POSTED
;
297 tw_dev
->posted_request_count
++;
298 if (tw_dev
->posted_request_count
> tw_dev
->max_posted_request_count
)
299 tw_dev
->max_posted_request_count
= tw_dev
->posted_request_count
;
302 } /* End twl_post_command_packet() */
304 /* This function will perform a pci-dma mapping for a scatter gather list */
305 static int twl_map_scsi_sg_data(TW_Device_Extension
*tw_dev
, int request_id
)
308 struct scsi_cmnd
*cmd
= tw_dev
->srb
[request_id
];
310 use_sg
= scsi_dma_map(cmd
);
313 else if (use_sg
< 0) {
314 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x1, "Failed to map scatter gather list");
318 cmd
->SCp
.phase
= TW_PHASE_SGLIST
;
319 cmd
->SCp
.have_data_in
= use_sg
;
322 } /* End twl_map_scsi_sg_data() */
324 /* This function hands scsi cdb's to the firmware */
325 static int twl_scsiop_execute_scsi(TW_Device_Extension
*tw_dev
, int request_id
, char *cdb
, int use_sg
, TW_SG_Entry_ISO
*sglistarg
)
327 TW_Command_Full
*full_command_packet
;
328 TW_Command_Apache
*command_packet
;
330 struct scsi_cmnd
*srb
= NULL
;
331 struct scatterlist
*sglist
= NULL
, *sg
;
334 if (tw_dev
->srb
[request_id
]) {
335 srb
= tw_dev
->srb
[request_id
];
336 if (scsi_sglist(srb
))
337 sglist
= scsi_sglist(srb
);
340 /* Initialize command packet */
341 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
342 full_command_packet
->header
.header_desc
.size_header
= 128;
343 full_command_packet
->header
.status_block
.error
= 0;
344 full_command_packet
->header
.status_block
.severity__reserved
= 0;
346 command_packet
= &full_command_packet
->command
.newcommand
;
347 command_packet
->status
= 0;
348 command_packet
->opcode__reserved
= TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI
);
350 /* We forced 16 byte cdb use earlier */
352 memcpy(command_packet
->cdb
, srb
->cmnd
, TW_MAX_CDB_LEN
);
354 memcpy(command_packet
->cdb
, cdb
, TW_MAX_CDB_LEN
);
357 command_packet
->unit
= srb
->device
->id
;
358 command_packet
->request_id__lunl
=
359 cpu_to_le16(TW_REQ_LUN_IN(srb
->device
->lun
, request_id
));
361 command_packet
->request_id__lunl
=
362 cpu_to_le16(TW_REQ_LUN_IN(0, request_id
));
363 command_packet
->unit
= 0;
366 command_packet
->sgl_offset
= 16;
369 /* Map sglist from scsi layer to cmd packet */
370 if (scsi_sg_count(srb
)) {
371 sg_count
= twl_map_scsi_sg_data(tw_dev
, request_id
);
375 scsi_for_each_sg(srb
, sg
, sg_count
, i
) {
376 command_packet
->sg_list
[i
].address
= TW_CPU_TO_SGL(sg_dma_address(sg
));
377 command_packet
->sg_list
[i
].length
= TW_CPU_TO_SGL(sg_dma_len(sg
));
379 command_packet
->sgl_entries__lunh
= cpu_to_le16(TW_REQ_LUN_IN((srb
->device
->lun
>> 4), scsi_sg_count(tw_dev
->srb
[request_id
])));
382 /* Internal cdb post */
383 for (i
= 0; i
< use_sg
; i
++) {
384 command_packet
->sg_list
[i
].address
= TW_CPU_TO_SGL(sglistarg
[i
].address
);
385 command_packet
->sg_list
[i
].length
= TW_CPU_TO_SGL(sglistarg
[i
].length
);
387 command_packet
->sgl_entries__lunh
= cpu_to_le16(TW_REQ_LUN_IN(0, use_sg
));
390 /* Update some stats */
392 tw_dev
->sector_count
= scsi_bufflen(srb
) / 512;
393 if (tw_dev
->sector_count
> tw_dev
->max_sector_count
)
394 tw_dev
->max_sector_count
= tw_dev
->sector_count
;
395 tw_dev
->sgl_entries
= scsi_sg_count(srb
);
396 if (tw_dev
->sgl_entries
> tw_dev
->max_sgl_entries
)
397 tw_dev
->max_sgl_entries
= tw_dev
->sgl_entries
;
400 /* Now post the command to the board */
401 retval
= twl_post_command_packet(tw_dev
, request_id
);
405 } /* End twl_scsiop_execute_scsi() */
407 /* This function will read the aen queue from the isr */
408 static int twl_aen_read_queue(TW_Device_Extension
*tw_dev
, int request_id
)
410 char cdb
[TW_MAX_CDB_LEN
];
411 TW_SG_Entry_ISO sglist
[1];
412 TW_Command_Full
*full_command_packet
;
415 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
416 memset(full_command_packet
, 0, sizeof(TW_Command_Full
));
419 memset(&cdb
, 0, TW_MAX_CDB_LEN
);
420 cdb
[0] = REQUEST_SENSE
; /* opcode */
421 cdb
[4] = TW_ALLOCATION_LENGTH
; /* allocation length */
423 /* Initialize sglist */
424 memset(&sglist
, 0, sizeof(TW_SG_Entry_ISO
));
425 sglist
[0].length
= TW_SECTOR_SIZE
;
426 sglist
[0].address
= tw_dev
->generic_buffer_phys
[request_id
];
428 /* Mark internal command */
429 tw_dev
->srb
[request_id
] = NULL
;
431 /* Now post the command packet */
432 if (twl_scsiop_execute_scsi(tw_dev
, request_id
, cdb
, 1, sglist
)) {
433 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x2, "Post failed while reading AEN queue");
439 } /* End twl_aen_read_queue() */
441 /* This function will sync firmware time with the host time */
442 static void twl_aen_sync_time(TW_Device_Extension
*tw_dev
, int request_id
)
446 TW_Command_Full
*full_command_packet
;
447 TW_Command
*command_packet
;
448 TW_Param_Apache
*param
;
451 /* Fill out the command packet */
452 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
453 memset(full_command_packet
, 0, sizeof(TW_Command_Full
));
454 command_packet
= &full_command_packet
->command
.oldcommand
;
455 command_packet
->opcode__sgloffset
= TW_OPSGL_IN(2, TW_OP_SET_PARAM
);
456 command_packet
->request_id
= request_id
;
457 command_packet
->byte8_offset
.param
.sgl
[0].address
= TW_CPU_TO_SGL(tw_dev
->generic_buffer_phys
[request_id
]);
458 command_packet
->byte8_offset
.param
.sgl
[0].length
= TW_CPU_TO_SGL(TW_SECTOR_SIZE
);
459 command_packet
->size
= TW_COMMAND_SIZE
;
460 command_packet
->byte6_offset
.parameter_count
= cpu_to_le16(1);
462 /* Setup the param */
463 param
= (TW_Param_Apache
*)tw_dev
->generic_buffer_virt
[request_id
];
464 memset(param
, 0, TW_SECTOR_SIZE
);
465 param
->table_id
= cpu_to_le16(TW_TIMEKEEP_TABLE
| 0x8000); /* Controller time keep table */
466 param
->parameter_id
= cpu_to_le16(0x3); /* SchedulerTime */
467 param
->parameter_size_bytes
= cpu_to_le16(4);
469 /* Convert system time in UTC to local time seconds since last
471 do_gettimeofday(&utc
);
472 local_time
= (u32
)(utc
.tv_sec
- (sys_tz
.tz_minuteswest
* 60));
473 schedulertime
= local_time
- (3 * 86400);
474 schedulertime
= cpu_to_le32(schedulertime
% 604800);
476 memcpy(param
->data
, &schedulertime
, sizeof(u32
));
478 /* Mark internal command */
479 tw_dev
->srb
[request_id
] = NULL
;
481 /* Now post the command */
482 twl_post_command_packet(tw_dev
, request_id
);
483 } /* End twl_aen_sync_time() */
485 /* This function will assign an available request id */
486 static void twl_get_request_id(TW_Device_Extension
*tw_dev
, int *request_id
)
488 *request_id
= tw_dev
->free_queue
[tw_dev
->free_head
];
489 tw_dev
->free_head
= (tw_dev
->free_head
+ 1) % TW_Q_LENGTH
;
490 tw_dev
->state
[*request_id
] = TW_S_STARTED
;
491 } /* End twl_get_request_id() */
493 /* This function will free a request id */
494 static void twl_free_request_id(TW_Device_Extension
*tw_dev
, int request_id
)
496 tw_dev
->free_queue
[tw_dev
->free_tail
] = request_id
;
497 tw_dev
->state
[request_id
] = TW_S_FINISHED
;
498 tw_dev
->free_tail
= (tw_dev
->free_tail
+ 1) % TW_Q_LENGTH
;
499 } /* End twl_free_request_id() */
501 /* This function will complete an aen request from the isr */
502 static int twl_aen_complete(TW_Device_Extension
*tw_dev
, int request_id
)
504 TW_Command_Full
*full_command_packet
;
505 TW_Command
*command_packet
;
506 TW_Command_Apache_Header
*header
;
510 header
= (TW_Command_Apache_Header
*)tw_dev
->generic_buffer_virt
[request_id
];
511 tw_dev
->posted_request_count
--;
512 aen
= le16_to_cpu(header
->status_block
.error
);
513 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
514 command_packet
= &full_command_packet
->command
.oldcommand
;
516 /* First check for internal completion of set param for time sync */
517 if (TW_OP_OUT(command_packet
->opcode__sgloffset
) == TW_OP_SET_PARAM
) {
518 /* Keep reading the queue in case there are more aen's */
519 if (twl_aen_read_queue(tw_dev
, request_id
))
528 case TW_AEN_QUEUE_EMPTY
:
529 /* Quit reading the queue if this is the last one */
531 case TW_AEN_SYNC_TIME_WITH_HOST
:
532 twl_aen_sync_time(tw_dev
, request_id
);
536 twl_aen_queue_event(tw_dev
, header
);
538 /* If there are more aen's, keep reading the queue */
539 if (twl_aen_read_queue(tw_dev
, request_id
))
548 tw_dev
->state
[request_id
] = TW_S_COMPLETED
;
549 twl_free_request_id(tw_dev
, request_id
);
550 clear_bit(TW_IN_ATTENTION_LOOP
, &tw_dev
->flags
);
553 } /* End twl_aen_complete() */
555 /* This function will poll for a response */
556 static int twl_poll_response(TW_Device_Extension
*tw_dev
, int request_id
, int seconds
)
558 unsigned long before
;
568 if (sizeof(dma_addr_t
) > 4) {
569 regh
= readl(TWL_HOBQPH_REG_ADDR(tw_dev
));
570 regl
= readl(TWL_HOBQPL_REG_ADDR(tw_dev
));
571 mfa
= ((u64
)regh
<< 32) | regl
;
573 mfa
= readl(TWL_HOBQPL_REG_ADDR(tw_dev
));
577 if (TW_RESID_OUT(response
) == request_id
)
580 if (time_after(jiffies
, before
+ HZ
* seconds
))
588 } /* End twl_poll_response() */
590 /* This function will drain the aen queue */
591 static int twl_aen_drain_queue(TW_Device_Extension
*tw_dev
, int no_check_reset
)
594 char cdb
[TW_MAX_CDB_LEN
];
595 TW_SG_Entry_ISO sglist
[1];
596 int finished
= 0, count
= 0;
597 TW_Command_Full
*full_command_packet
;
598 TW_Command_Apache_Header
*header
;
600 int first_reset
= 0, queue
= 0, retval
= 1;
607 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
608 memset(full_command_packet
, 0, sizeof(TW_Command_Full
));
611 memset(&cdb
, 0, TW_MAX_CDB_LEN
);
612 cdb
[0] = REQUEST_SENSE
; /* opcode */
613 cdb
[4] = TW_ALLOCATION_LENGTH
; /* allocation length */
615 /* Initialize sglist */
616 memset(&sglist
, 0, sizeof(TW_SG_Entry_ISO
));
617 sglist
[0].length
= TW_SECTOR_SIZE
;
618 sglist
[0].address
= tw_dev
->generic_buffer_phys
[request_id
];
620 /* Mark internal command */
621 tw_dev
->srb
[request_id
] = NULL
;
624 /* Send command to the board */
625 if (twl_scsiop_execute_scsi(tw_dev
, request_id
, cdb
, 1, sglist
)) {
626 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x3, "Error posting request sense");
630 /* Now poll for completion */
631 if (twl_poll_response(tw_dev
, request_id
, 30)) {
632 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x4, "No valid response while draining AEN queue");
633 tw_dev
->posted_request_count
--;
637 tw_dev
->posted_request_count
--;
638 header
= (TW_Command_Apache_Header
*)tw_dev
->generic_buffer_virt
[request_id
];
639 aen
= le16_to_cpu(header
->status_block
.error
);
644 case TW_AEN_QUEUE_EMPTY
:
645 if (first_reset
!= 1)
650 case TW_AEN_SOFT_RESET
:
651 if (first_reset
== 0)
656 case TW_AEN_SYNC_TIME_WITH_HOST
:
662 /* Now queue an event info */
664 twl_aen_queue_event(tw_dev
, header
);
665 } while ((finished
== 0) && (count
< TW_MAX_AEN_DRAIN
));
667 if (count
== TW_MAX_AEN_DRAIN
)
672 tw_dev
->state
[request_id
] = TW_S_INITIAL
;
674 } /* End twl_aen_drain_queue() */
676 /* This function will allocate memory and check if it is correctly aligned */
677 static int twl_allocate_memory(TW_Device_Extension
*tw_dev
, int size
, int which
)
680 dma_addr_t dma_handle
;
681 unsigned long *cpu_addr
;
684 cpu_addr
= pci_zalloc_consistent(tw_dev
->tw_pci_dev
, size
* TW_Q_LENGTH
,
687 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x5, "Memory allocation failed");
691 for (i
= 0; i
< TW_Q_LENGTH
; i
++) {
694 tw_dev
->command_packet_phys
[i
] = dma_handle
+(i
*size
);
695 tw_dev
->command_packet_virt
[i
] = (TW_Command_Full
*)((unsigned char *)cpu_addr
+ (i
*size
));
698 tw_dev
->generic_buffer_phys
[i
] = dma_handle
+(i
*size
);
699 tw_dev
->generic_buffer_virt
[i
] = (unsigned long *)((unsigned char *)cpu_addr
+ (i
*size
));
702 tw_dev
->sense_buffer_phys
[i
] = dma_handle
+(i
*size
);
703 tw_dev
->sense_buffer_virt
[i
] = (TW_Command_Apache_Header
*)((unsigned char *)cpu_addr
+ (i
*size
));
710 } /* End twl_allocate_memory() */
712 /* This function will load the request id and various sgls for ioctls */
713 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
)
715 TW_Command
*oldcommand
;
716 TW_Command_Apache
*newcommand
;
717 TW_SG_Entry_ISO
*sgl
;
718 unsigned int pae
= 0;
720 if ((sizeof(long) < 8) && (sizeof(dma_addr_t
) > 4))
723 if (TW_OP_OUT(full_command_packet
->command
.newcommand
.opcode__reserved
) == TW_OP_EXECUTE_SCSI
) {
724 newcommand
= &full_command_packet
->command
.newcommand
;
725 newcommand
->request_id__lunl
=
726 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand
->request_id__lunl
), request_id
));
728 newcommand
->sg_list
[0].address
= TW_CPU_TO_SGL(dma_handle
+ sizeof(TW_Ioctl_Buf_Apache
) - 1);
729 newcommand
->sg_list
[0].length
= TW_CPU_TO_SGL(length
);
731 newcommand
->sgl_entries__lunh
=
732 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand
->sgl_entries__lunh
), length
? 1 : 0));
734 oldcommand
= &full_command_packet
->command
.oldcommand
;
735 oldcommand
->request_id
= request_id
;
737 if (TW_SGL_OUT(oldcommand
->opcode__sgloffset
)) {
738 /* Load the sg list */
739 sgl
= (TW_SG_Entry_ISO
*)((u32
*)oldcommand
+oldcommand
->size
- (sizeof(TW_SG_Entry_ISO
)/4) + pae
+ (sizeof(dma_addr_t
) > 4 ? 1 : 0));
740 sgl
->address
= TW_CPU_TO_SGL(dma_handle
+ sizeof(TW_Ioctl_Buf_Apache
) - 1);
741 sgl
->length
= TW_CPU_TO_SGL(length
);
742 oldcommand
->size
+= pae
;
743 oldcommand
->size
+= sizeof(dma_addr_t
) > 4 ? 1 : 0;
746 } /* End twl_load_sgl() */
748 /* This function handles ioctl for the character device
749 This interface is used by smartmontools open source software */
750 static long twl_chrdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
753 unsigned long *cpu_addr
, data_buffer_length_adjusted
= 0, flags
= 0;
754 dma_addr_t dma_handle
;
756 TW_Ioctl_Driver_Command driver_command
;
757 struct inode
*inode
= file_inode(file
);
758 TW_Ioctl_Buf_Apache
*tw_ioctl
;
759 TW_Command_Full
*full_command_packet
;
760 TW_Device_Extension
*tw_dev
= twl_device_extension_list
[iminor(inode
)];
761 int retval
= -EFAULT
;
762 void __user
*argp
= (void __user
*)arg
;
764 mutex_lock(&twl_chrdev_mutex
);
766 /* Only let one of these through at a time */
767 if (mutex_lock_interruptible(&tw_dev
->ioctl_lock
)) {
772 /* First copy down the driver command */
773 if (copy_from_user(&driver_command
, argp
, sizeof(TW_Ioctl_Driver_Command
)))
776 /* Check data buffer size */
777 if (driver_command
.buffer_length
> TW_MAX_SECTORS
* 2048) {
782 /* Hardware can only do multiple of 512 byte transfers */
783 data_buffer_length_adjusted
= (driver_command
.buffer_length
+ 511) & ~511;
785 /* Now allocate ioctl buf memory */
786 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
);
792 tw_ioctl
= (TW_Ioctl_Buf_Apache
*)cpu_addr
;
794 /* Now copy down the entire ioctl */
795 if (copy_from_user(tw_ioctl
, argp
, driver_command
.buffer_length
+ sizeof(TW_Ioctl_Buf_Apache
) - 1))
798 /* See which ioctl we are doing */
800 case TW_IOCTL_FIRMWARE_PASS_THROUGH
:
801 spin_lock_irqsave(tw_dev
->host
->host_lock
, flags
);
802 twl_get_request_id(tw_dev
, &request_id
);
804 /* Flag internal command */
805 tw_dev
->srb
[request_id
] = NULL
;
807 /* Flag chrdev ioctl */
808 tw_dev
->chrdev_request_id
= request_id
;
810 full_command_packet
= (TW_Command_Full
*)&tw_ioctl
->firmware_command
;
812 /* Load request id and sglist for both command types */
813 twl_load_sgl(tw_dev
, full_command_packet
, request_id
, dma_handle
, data_buffer_length_adjusted
);
815 memcpy(tw_dev
->command_packet_virt
[request_id
], &(tw_ioctl
->firmware_command
), sizeof(TW_Command_Full
));
817 /* Now post the command packet to the controller */
818 twl_post_command_packet(tw_dev
, request_id
);
819 spin_unlock_irqrestore(tw_dev
->host
->host_lock
, flags
);
821 timeout
= TW_IOCTL_CHRDEV_TIMEOUT
*HZ
;
823 /* Now wait for command to complete */
824 timeout
= wait_event_timeout(tw_dev
->ioctl_wqueue
, tw_dev
->chrdev_request_id
== TW_IOCTL_CHRDEV_FREE
, timeout
);
826 /* We timed out, and didn't get an interrupt */
827 if (tw_dev
->chrdev_request_id
!= TW_IOCTL_CHRDEV_FREE
) {
828 /* Now we need to reset the board */
829 printk(KERN_WARNING
"3w-sas: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
830 tw_dev
->host
->host_no
, TW_DRIVER
, 0x6,
833 twl_reset_device_extension(tw_dev
, 1);
837 /* Now copy in the command packet response */
838 memcpy(&(tw_ioctl
->firmware_command
), tw_dev
->command_packet_virt
[request_id
], sizeof(TW_Command_Full
));
840 /* Now complete the io */
841 spin_lock_irqsave(tw_dev
->host
->host_lock
, flags
);
842 tw_dev
->posted_request_count
--;
843 tw_dev
->state
[request_id
] = TW_S_COMPLETED
;
844 twl_free_request_id(tw_dev
, request_id
);
845 spin_unlock_irqrestore(tw_dev
->host
->host_lock
, flags
);
852 /* Now copy the entire response to userspace */
853 if (copy_to_user(argp
, tw_ioctl
, sizeof(TW_Ioctl_Buf_Apache
) + driver_command
.buffer_length
- 1) == 0)
856 /* Now free ioctl buf memory */
857 dma_free_coherent(&tw_dev
->tw_pci_dev
->dev
, data_buffer_length_adjusted
+sizeof(TW_Ioctl_Buf_Apache
) - 1, cpu_addr
, dma_handle
);
859 mutex_unlock(&tw_dev
->ioctl_lock
);
861 mutex_unlock(&twl_chrdev_mutex
);
863 } /* End twl_chrdev_ioctl() */
865 /* This function handles open for the character device */
866 static int twl_chrdev_open(struct inode
*inode
, struct file
*file
)
868 unsigned int minor_number
;
869 int retval
= -ENODEV
;
871 if (!capable(CAP_SYS_ADMIN
)) {
876 minor_number
= iminor(inode
);
877 if (minor_number
>= twl_device_extension_count
)
882 } /* End twl_chrdev_open() */
884 /* File operations struct for character device */
885 static const struct file_operations twl_fops
= {
886 .owner
= THIS_MODULE
,
887 .unlocked_ioctl
= twl_chrdev_ioctl
,
888 .open
= twl_chrdev_open
,
890 .llseek
= noop_llseek
,
893 /* This function passes sense data from firmware to scsi layer */
894 static int twl_fill_sense(TW_Device_Extension
*tw_dev
, int i
, int request_id
, int copy_sense
, int print_host
)
896 TW_Command_Apache_Header
*header
;
897 TW_Command_Full
*full_command_packet
;
898 unsigned short error
;
902 header
= tw_dev
->sense_buffer_virt
[i
];
903 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
905 /* Get embedded firmware error string */
906 error_str
= &(header
->err_specific_desc
[strlen(header
->err_specific_desc
) + 1]);
908 /* Don't print error for Logical unit not supported during rollcall */
909 error
= le16_to_cpu(header
->status_block
.error
);
910 if ((error
!= TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED
) && (error
!= TW_ERROR_UNIT_OFFLINE
) && (error
!= TW_ERROR_INVALID_FIELD_IN_CDB
)) {
912 printk(KERN_WARNING
"3w-sas: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
913 tw_dev
->host
->host_no
,
914 TW_MESSAGE_SOURCE_CONTROLLER_ERROR
,
915 header
->status_block
.error
,
917 header
->err_specific_desc
);
919 printk(KERN_WARNING
"3w-sas: ERROR: (0x%02X:0x%04X): %s:%s.\n",
920 TW_MESSAGE_SOURCE_CONTROLLER_ERROR
,
921 header
->status_block
.error
,
923 header
->err_specific_desc
);
927 memcpy(tw_dev
->srb
[request_id
]->sense_buffer
, header
->sense_data
, TW_SENSE_DATA_LENGTH
);
928 tw_dev
->srb
[request_id
]->result
= (full_command_packet
->command
.newcommand
.status
<< 1);
933 } /* End twl_fill_sense() */
935 /* This function will free up device extension resources */
936 static void twl_free_device_extension(TW_Device_Extension
*tw_dev
)
938 if (tw_dev
->command_packet_virt
[0])
939 pci_free_consistent(tw_dev
->tw_pci_dev
,
940 sizeof(TW_Command_Full
)*TW_Q_LENGTH
,
941 tw_dev
->command_packet_virt
[0],
942 tw_dev
->command_packet_phys
[0]);
944 if (tw_dev
->generic_buffer_virt
[0])
945 pci_free_consistent(tw_dev
->tw_pci_dev
,
946 TW_SECTOR_SIZE
*TW_Q_LENGTH
,
947 tw_dev
->generic_buffer_virt
[0],
948 tw_dev
->generic_buffer_phys
[0]);
950 if (tw_dev
->sense_buffer_virt
[0])
951 pci_free_consistent(tw_dev
->tw_pci_dev
,
952 sizeof(TW_Command_Apache_Header
)*
954 tw_dev
->sense_buffer_virt
[0],
955 tw_dev
->sense_buffer_phys
[0]);
957 kfree(tw_dev
->event_queue
[0]);
958 } /* End twl_free_device_extension() */
960 /* This function will get parameter table entries from the firmware */
961 static void *twl_get_param(TW_Device_Extension
*tw_dev
, int request_id
, int table_id
, int parameter_id
, int parameter_size_bytes
)
963 TW_Command_Full
*full_command_packet
;
964 TW_Command
*command_packet
;
965 TW_Param_Apache
*param
;
968 /* Setup the command packet */
969 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
970 memset(full_command_packet
, 0, sizeof(TW_Command_Full
));
971 command_packet
= &full_command_packet
->command
.oldcommand
;
973 command_packet
->opcode__sgloffset
= TW_OPSGL_IN(2, TW_OP_GET_PARAM
);
974 command_packet
->size
= TW_COMMAND_SIZE
;
975 command_packet
->request_id
= request_id
;
976 command_packet
->byte6_offset
.block_count
= cpu_to_le16(1);
978 /* Now setup the param */
979 param
= (TW_Param_Apache
*)tw_dev
->generic_buffer_virt
[request_id
];
980 memset(param
, 0, TW_SECTOR_SIZE
);
981 param
->table_id
= cpu_to_le16(table_id
| 0x8000);
982 param
->parameter_id
= cpu_to_le16(parameter_id
);
983 param
->parameter_size_bytes
= cpu_to_le16(parameter_size_bytes
);
985 command_packet
->byte8_offset
.param
.sgl
[0].address
= TW_CPU_TO_SGL(tw_dev
->generic_buffer_phys
[request_id
]);
986 command_packet
->byte8_offset
.param
.sgl
[0].length
= TW_CPU_TO_SGL(TW_SECTOR_SIZE
);
988 /* Post the command packet to the board */
989 twl_post_command_packet(tw_dev
, request_id
);
991 /* Poll for completion */
992 if (twl_poll_response(tw_dev
, request_id
, 30))
993 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x7, "No valid response during get param")
995 retval
= (void *)&(param
->data
[0]);
997 tw_dev
->posted_request_count
--;
998 tw_dev
->state
[request_id
] = TW_S_INITIAL
;
1001 } /* End twl_get_param() */
1003 /* This function will send an initconnection command to controller */
1004 static int twl_initconnection(TW_Device_Extension
*tw_dev
, int message_credits
,
1005 u32 set_features
, unsigned short current_fw_srl
,
1006 unsigned short current_fw_arch_id
,
1007 unsigned short current_fw_branch
,
1008 unsigned short current_fw_build
,
1009 unsigned short *fw_on_ctlr_srl
,
1010 unsigned short *fw_on_ctlr_arch_id
,
1011 unsigned short *fw_on_ctlr_branch
,
1012 unsigned short *fw_on_ctlr_build
,
1013 u32
*init_connect_result
)
1015 TW_Command_Full
*full_command_packet
;
1016 TW_Initconnect
*tw_initconnect
;
1017 int request_id
= 0, retval
= 1;
1019 /* Initialize InitConnection command packet */
1020 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
1021 memset(full_command_packet
, 0, sizeof(TW_Command_Full
));
1022 full_command_packet
->header
.header_desc
.size_header
= 128;
1024 tw_initconnect
= (TW_Initconnect
*)&full_command_packet
->command
.oldcommand
;
1025 tw_initconnect
->opcode__reserved
= TW_OPRES_IN(0, TW_OP_INIT_CONNECTION
);
1026 tw_initconnect
->request_id
= request_id
;
1027 tw_initconnect
->message_credits
= cpu_to_le16(message_credits
);
1028 tw_initconnect
->features
= set_features
;
1030 /* Turn on 64-bit sgl support if we need to */
1031 tw_initconnect
->features
|= sizeof(dma_addr_t
) > 4 ? 1 : 0;
1033 tw_initconnect
->features
= cpu_to_le32(tw_initconnect
->features
);
1035 if (set_features
& TW_EXTENDED_INIT_CONNECT
) {
1036 tw_initconnect
->size
= TW_INIT_COMMAND_PACKET_SIZE_EXTENDED
;
1037 tw_initconnect
->fw_srl
= cpu_to_le16(current_fw_srl
);
1038 tw_initconnect
->fw_arch_id
= cpu_to_le16(current_fw_arch_id
);
1039 tw_initconnect
->fw_branch
= cpu_to_le16(current_fw_branch
);
1040 tw_initconnect
->fw_build
= cpu_to_le16(current_fw_build
);
1042 tw_initconnect
->size
= TW_INIT_COMMAND_PACKET_SIZE
;
1044 /* Send command packet to the board */
1045 twl_post_command_packet(tw_dev
, request_id
);
1047 /* Poll for completion */
1048 if (twl_poll_response(tw_dev
, request_id
, 30)) {
1049 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x8, "No valid response during init connection");
1051 if (set_features
& TW_EXTENDED_INIT_CONNECT
) {
1052 *fw_on_ctlr_srl
= le16_to_cpu(tw_initconnect
->fw_srl
);
1053 *fw_on_ctlr_arch_id
= le16_to_cpu(tw_initconnect
->fw_arch_id
);
1054 *fw_on_ctlr_branch
= le16_to_cpu(tw_initconnect
->fw_branch
);
1055 *fw_on_ctlr_build
= le16_to_cpu(tw_initconnect
->fw_build
);
1056 *init_connect_result
= le32_to_cpu(tw_initconnect
->result
);
1061 tw_dev
->posted_request_count
--;
1062 tw_dev
->state
[request_id
] = TW_S_INITIAL
;
1065 } /* End twl_initconnection() */
1067 /* This function will initialize the fields of a device extension */
1068 static int twl_initialize_device_extension(TW_Device_Extension
*tw_dev
)
1072 /* Initialize command packet buffers */
1073 if (twl_allocate_memory(tw_dev
, sizeof(TW_Command_Full
), 0)) {
1074 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x9, "Command packet memory allocation failed");
1078 /* Initialize generic buffer */
1079 if (twl_allocate_memory(tw_dev
, TW_SECTOR_SIZE
, 1)) {
1080 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0xa, "Generic memory allocation failed");
1084 /* Allocate sense buffers */
1085 if (twl_allocate_memory(tw_dev
, sizeof(TW_Command_Apache_Header
), 2)) {
1086 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0xb, "Sense buffer allocation failed");
1090 /* Allocate event info space */
1091 tw_dev
->event_queue
[0] = kcalloc(TW_Q_LENGTH
, sizeof(TW_Event
), GFP_KERNEL
);
1092 if (!tw_dev
->event_queue
[0]) {
1093 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0xc, "Event info memory allocation failed");
1097 for (i
= 0; i
< TW_Q_LENGTH
; i
++) {
1098 tw_dev
->event_queue
[i
] = (TW_Event
*)((unsigned char *)tw_dev
->event_queue
[0] + (i
* sizeof(TW_Event
)));
1099 tw_dev
->free_queue
[i
] = i
;
1100 tw_dev
->state
[i
] = TW_S_INITIAL
;
1103 tw_dev
->free_head
= TW_Q_START
;
1104 tw_dev
->free_tail
= TW_Q_START
;
1105 tw_dev
->error_sequence_id
= 1;
1106 tw_dev
->chrdev_request_id
= TW_IOCTL_CHRDEV_FREE
;
1108 mutex_init(&tw_dev
->ioctl_lock
);
1109 init_waitqueue_head(&tw_dev
->ioctl_wqueue
);
1114 } /* End twl_initialize_device_extension() */
1116 /* This function will perform a pci-dma unmap */
1117 static void twl_unmap_scsi_data(TW_Device_Extension
*tw_dev
, int request_id
)
1119 struct scsi_cmnd
*cmd
= tw_dev
->srb
[request_id
];
1121 if (cmd
->SCp
.phase
== TW_PHASE_SGLIST
)
1122 scsi_dma_unmap(cmd
);
1123 } /* End twl_unmap_scsi_data() */
1125 /* This function will handle attention interrupts */
1126 static int twl_handle_attention_interrupt(TW_Device_Extension
*tw_dev
)
1129 u32 request_id
, doorbell
;
1131 /* Read doorbell status */
1132 doorbell
= readl(TWL_HOBDB_REG_ADDR(tw_dev
));
1134 /* Check for controller errors */
1135 if (doorbell
& TWL_DOORBELL_CONTROLLER_ERROR
) {
1136 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0xd, "Microcontroller Error: clearing");
1140 /* Check if we need to perform an AEN drain */
1141 if (doorbell
& TWL_DOORBELL_ATTENTION_INTERRUPT
) {
1142 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP
, &tw_dev
->flags
))) {
1143 twl_get_request_id(tw_dev
, &request_id
);
1144 if (twl_aen_read_queue(tw_dev
, request_id
)) {
1145 tw_dev
->state
[request_id
] = TW_S_COMPLETED
;
1146 twl_free_request_id(tw_dev
, request_id
);
1147 clear_bit(TW_IN_ATTENTION_LOOP
, &tw_dev
->flags
);
1154 /* Clear doorbell interrupt */
1155 TWL_CLEAR_DB_INTERRUPT(tw_dev
);
1157 /* Make sure the clear was flushed by reading it back */
1158 readl(TWL_HOBDBC_REG_ADDR(tw_dev
));
1161 } /* End twl_handle_attention_interrupt() */
1163 /* Interrupt service routine */
1164 static irqreturn_t
twl_interrupt(int irq
, void *dev_instance
)
1166 TW_Device_Extension
*tw_dev
= (TW_Device_Extension
*)dev_instance
;
1167 int i
, handled
= 0, error
= 0;
1169 u32 reg
, regl
, regh
, response
, request_id
= 0;
1170 struct scsi_cmnd
*cmd
;
1171 TW_Command_Full
*full_command_packet
;
1173 spin_lock(tw_dev
->host
->host_lock
);
1175 /* Read host interrupt status */
1176 reg
= readl(TWL_HISTAT_REG_ADDR(tw_dev
));
1178 /* Check if this is our interrupt, otherwise bail */
1179 if (!(reg
& TWL_HISTATUS_VALID_INTERRUPT
))
1180 goto twl_interrupt_bail
;
1184 /* If we are resetting, bail */
1185 if (test_bit(TW_IN_RESET
, &tw_dev
->flags
))
1186 goto twl_interrupt_bail
;
1188 /* Attention interrupt */
1189 if (reg
& TWL_HISTATUS_ATTENTION_INTERRUPT
) {
1190 if (twl_handle_attention_interrupt(tw_dev
)) {
1191 TWL_MASK_INTERRUPTS(tw_dev
);
1192 goto twl_interrupt_bail
;
1196 /* Response interrupt */
1197 while (reg
& TWL_HISTATUS_RESPONSE_INTERRUPT
) {
1198 if (sizeof(dma_addr_t
) > 4) {
1199 regh
= readl(TWL_HOBQPH_REG_ADDR(tw_dev
));
1200 regl
= readl(TWL_HOBQPL_REG_ADDR(tw_dev
));
1201 mfa
= ((u64
)regh
<< 32) | regl
;
1203 mfa
= readl(TWL_HOBQPL_REG_ADDR(tw_dev
));
1206 response
= (u32
)mfa
;
1208 /* Check for command packet error */
1209 if (!TW_NOTMFA_OUT(response
)) {
1210 for (i
=0;i
<TW_Q_LENGTH
;i
++) {
1211 if (tw_dev
->sense_buffer_phys
[i
] == mfa
) {
1212 request_id
= le16_to_cpu(tw_dev
->sense_buffer_virt
[i
]->header_desc
.request_id
);
1213 if (tw_dev
->srb
[request_id
] != NULL
)
1214 error
= twl_fill_sense(tw_dev
, i
, request_id
, 1, 1);
1216 /* Skip ioctl error prints */
1217 if (request_id
!= tw_dev
->chrdev_request_id
)
1218 error
= twl_fill_sense(tw_dev
, i
, request_id
, 0, 1);
1220 memcpy(tw_dev
->command_packet_virt
[request_id
], tw_dev
->sense_buffer_virt
[i
], sizeof(TW_Command_Apache_Header
));
1223 /* Now re-post the sense buffer */
1224 writel((u32
)((u64
)tw_dev
->sense_buffer_phys
[i
] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev
));
1225 writel((u32
)tw_dev
->sense_buffer_phys
[i
], TWL_HOBQPL_REG_ADDR(tw_dev
));
1230 request_id
= TW_RESID_OUT(response
);
1232 full_command_packet
= tw_dev
->command_packet_virt
[request_id
];
1234 /* Check for correct state */
1235 if (tw_dev
->state
[request_id
] != TW_S_POSTED
) {
1236 if (tw_dev
->srb
[request_id
] != NULL
) {
1237 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0xe, "Received a request id that wasn't posted");
1238 TWL_MASK_INTERRUPTS(tw_dev
);
1239 goto twl_interrupt_bail
;
1243 /* Check for internal command completion */
1244 if (tw_dev
->srb
[request_id
] == NULL
) {
1245 if (request_id
!= tw_dev
->chrdev_request_id
) {
1246 if (twl_aen_complete(tw_dev
, request_id
))
1247 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0xf, "Error completing AEN during attention interrupt");
1249 tw_dev
->chrdev_request_id
= TW_IOCTL_CHRDEV_FREE
;
1250 wake_up(&tw_dev
->ioctl_wqueue
);
1253 cmd
= tw_dev
->srb
[request_id
];
1256 cmd
->result
= (DID_OK
<< 16);
1258 /* Report residual bytes for single sgl */
1259 if ((scsi_sg_count(cmd
) <= 1) && (full_command_packet
->command
.newcommand
.status
== 0)) {
1260 if (full_command_packet
->command
.newcommand
.sg_list
[0].length
< scsi_bufflen(tw_dev
->srb
[request_id
]))
1261 scsi_set_resid(cmd
, scsi_bufflen(cmd
) - full_command_packet
->command
.newcommand
.sg_list
[0].length
);
1264 /* Now complete the io */
1265 tw_dev
->state
[request_id
] = TW_S_COMPLETED
;
1266 twl_free_request_id(tw_dev
, request_id
);
1267 tw_dev
->posted_request_count
--;
1268 tw_dev
->srb
[request_id
]->scsi_done(tw_dev
->srb
[request_id
]);
1269 twl_unmap_scsi_data(tw_dev
, request_id
);
1272 /* Check for another response interrupt */
1273 reg
= readl(TWL_HISTAT_REG_ADDR(tw_dev
));
1277 spin_unlock(tw_dev
->host
->host_lock
);
1278 return IRQ_RETVAL(handled
);
1279 } /* End twl_interrupt() */
1281 /* This function will poll for a register change */
1282 static int twl_poll_register(TW_Device_Extension
*tw_dev
, void *reg
, u32 value
, u32 result
, int seconds
)
1284 unsigned long before
;
1288 reg_value
= readl(reg
);
1291 while ((reg_value
& value
) != result
) {
1292 reg_value
= readl(reg
);
1293 if (time_after(jiffies
, before
+ HZ
* seconds
))
1300 } /* End twl_poll_register() */
1302 /* This function will reset a controller */
1303 static int twl_reset_sequence(TW_Device_Extension
*tw_dev
, int soft_reset
)
1308 unsigned short fw_on_ctlr_srl
= 0, fw_on_ctlr_arch_id
= 0;
1309 unsigned short fw_on_ctlr_branch
= 0, fw_on_ctlr_build
= 0;
1310 u32 init_connect_result
= 0;
1312 int do_soft_reset
= soft_reset
;
1314 while (tries
< TW_MAX_RESET_TRIES
) {
1315 /* Do a soft reset if one is needed */
1316 if (do_soft_reset
) {
1317 TWL_SOFT_RESET(tw_dev
);
1319 /* Make sure controller is in a good state */
1320 if (twl_poll_register(tw_dev
, TWL_SCRPD3_REG_ADDR(tw_dev
), TWL_CONTROLLER_READY
, 0x0, 30)) {
1321 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x10, "Controller never went non-ready during reset sequence");
1325 if (twl_poll_register(tw_dev
, TWL_SCRPD3_REG_ADDR(tw_dev
), TWL_CONTROLLER_READY
, TWL_CONTROLLER_READY
, 60)) {
1326 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x11, "Controller not ready during reset sequence");
1333 if (twl_initconnection(tw_dev
, TW_INIT_MESSAGE_CREDITS
,
1334 TW_EXTENDED_INIT_CONNECT
, TW_CURRENT_DRIVER_SRL
,
1335 TW_9750_ARCH_ID
, TW_CURRENT_DRIVER_BRANCH
,
1336 TW_CURRENT_DRIVER_BUILD
, &fw_on_ctlr_srl
,
1337 &fw_on_ctlr_arch_id
, &fw_on_ctlr_branch
,
1338 &fw_on_ctlr_build
, &init_connect_result
)) {
1339 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x12, "Initconnection failed while checking SRL");
1345 /* Load sense buffers */
1346 while (i
< TW_Q_LENGTH
) {
1347 writel((u32
)((u64
)tw_dev
->sense_buffer_phys
[i
] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev
));
1348 writel((u32
)tw_dev
->sense_buffer_phys
[i
], TWL_HOBQPL_REG_ADDR(tw_dev
));
1350 /* Check status for over-run after each write */
1351 status
= readl(TWL_STATUS_REG_ADDR(tw_dev
));
1352 if (!(status
& TWL_STATUS_OVERRUN_SUBMIT
))
1356 /* Now check status */
1357 status
= readl(TWL_STATUS_REG_ADDR(tw_dev
));
1359 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x13, "Bad controller status after loading sense buffers");
1365 /* Drain the AEN queue */
1366 if (twl_aen_drain_queue(tw_dev
, soft_reset
)) {
1367 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x14, "AEN drain failed during reset sequence");
1373 /* Load rest of compatibility struct */
1374 strncpy(tw_dev
->tw_compat_info
.driver_version
, TW_DRIVER_VERSION
, strlen(TW_DRIVER_VERSION
));
1375 tw_dev
->tw_compat_info
.driver_srl_high
= TW_CURRENT_DRIVER_SRL
;
1376 tw_dev
->tw_compat_info
.driver_branch_high
= TW_CURRENT_DRIVER_BRANCH
;
1377 tw_dev
->tw_compat_info
.driver_build_high
= TW_CURRENT_DRIVER_BUILD
;
1378 tw_dev
->tw_compat_info
.driver_srl_low
= TW_BASE_FW_SRL
;
1379 tw_dev
->tw_compat_info
.driver_branch_low
= TW_BASE_FW_BRANCH
;
1380 tw_dev
->tw_compat_info
.driver_build_low
= TW_BASE_FW_BUILD
;
1381 tw_dev
->tw_compat_info
.fw_on_ctlr_srl
= fw_on_ctlr_srl
;
1382 tw_dev
->tw_compat_info
.fw_on_ctlr_branch
= fw_on_ctlr_branch
;
1383 tw_dev
->tw_compat_info
.fw_on_ctlr_build
= fw_on_ctlr_build
;
1385 /* If we got here, controller is in a good state */
1391 } /* End twl_reset_sequence() */
1393 /* This function will reset a device extension */
1394 static int twl_reset_device_extension(TW_Device_Extension
*tw_dev
, int ioctl_reset
)
1396 int i
= 0, retval
= 1;
1397 unsigned long flags
= 0;
1399 /* Block SCSI requests while we are resetting */
1401 scsi_block_requests(tw_dev
->host
);
1403 set_bit(TW_IN_RESET
, &tw_dev
->flags
);
1404 TWL_MASK_INTERRUPTS(tw_dev
);
1405 TWL_CLEAR_DB_INTERRUPT(tw_dev
);
1407 spin_lock_irqsave(tw_dev
->host
->host_lock
, flags
);
1409 /* Abort all requests that are in progress */
1410 for (i
= 0; i
< TW_Q_LENGTH
; i
++) {
1411 if ((tw_dev
->state
[i
] != TW_S_FINISHED
) &&
1412 (tw_dev
->state
[i
] != TW_S_INITIAL
) &&
1413 (tw_dev
->state
[i
] != TW_S_COMPLETED
)) {
1414 if (tw_dev
->srb
[i
]) {
1415 tw_dev
->srb
[i
]->result
= (DID_RESET
<< 16);
1416 tw_dev
->srb
[i
]->scsi_done(tw_dev
->srb
[i
]);
1417 twl_unmap_scsi_data(tw_dev
, i
);
1422 /* Reset queues and counts */
1423 for (i
= 0; i
< TW_Q_LENGTH
; i
++) {
1424 tw_dev
->free_queue
[i
] = i
;
1425 tw_dev
->state
[i
] = TW_S_INITIAL
;
1427 tw_dev
->free_head
= TW_Q_START
;
1428 tw_dev
->free_tail
= TW_Q_START
;
1429 tw_dev
->posted_request_count
= 0;
1431 spin_unlock_irqrestore(tw_dev
->host
->host_lock
, flags
);
1433 if (twl_reset_sequence(tw_dev
, 1))
1436 TWL_UNMASK_INTERRUPTS(tw_dev
);
1438 clear_bit(TW_IN_RESET
, &tw_dev
->flags
);
1439 tw_dev
->chrdev_request_id
= TW_IOCTL_CHRDEV_FREE
;
1444 scsi_unblock_requests(tw_dev
->host
);
1446 } /* End twl_reset_device_extension() */
1448 /* This funciton returns unit geometry in cylinders/heads/sectors */
1449 static int twl_scsi_biosparam(struct scsi_device
*sdev
, struct block_device
*bdev
, sector_t capacity
, int geom
[])
1452 TW_Device_Extension
*tw_dev
;
1454 tw_dev
= (TW_Device_Extension
*)sdev
->host
->hostdata
;
1456 if (capacity
>= 0x200000) {
1466 geom
[2] = sector_div(capacity
, heads
* sectors
); /* cylinders */
1469 } /* End twl_scsi_biosparam() */
1471 /* This is the new scsi eh reset function */
1472 static int twl_scsi_eh_reset(struct scsi_cmnd
*SCpnt
)
1474 TW_Device_Extension
*tw_dev
= NULL
;
1475 int retval
= FAILED
;
1477 tw_dev
= (TW_Device_Extension
*)SCpnt
->device
->host
->hostdata
;
1479 tw_dev
->num_resets
++;
1481 sdev_printk(KERN_WARNING
, SCpnt
->device
,
1482 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1483 TW_DRIVER
, 0x2c, SCpnt
->cmnd
[0]);
1485 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1486 mutex_lock(&tw_dev
->ioctl_lock
);
1488 /* Now reset the card and some of the device extension data */
1489 if (twl_reset_device_extension(tw_dev
, 0)) {
1490 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x15, "Controller reset failed during scsi host reset");
1496 mutex_unlock(&tw_dev
->ioctl_lock
);
1498 } /* End twl_scsi_eh_reset() */
1500 /* This is the main scsi queue function to handle scsi opcodes */
1501 static int twl_scsi_queue_lck(struct scsi_cmnd
*SCpnt
, void (*done
)(struct scsi_cmnd
*))
1503 int request_id
, retval
;
1504 TW_Device_Extension
*tw_dev
= (TW_Device_Extension
*)SCpnt
->device
->host
->hostdata
;
1506 /* If we are resetting due to timed out ioctl, report as busy */
1507 if (test_bit(TW_IN_RESET
, &tw_dev
->flags
)) {
1508 retval
= SCSI_MLQUEUE_HOST_BUSY
;
1512 /* Save done function into scsi_cmnd struct */
1513 SCpnt
->scsi_done
= done
;
1515 /* Get a free request id */
1516 twl_get_request_id(tw_dev
, &request_id
);
1518 /* Save the scsi command for use by the ISR */
1519 tw_dev
->srb
[request_id
] = SCpnt
;
1521 /* Initialize phase to zero */
1522 SCpnt
->SCp
.phase
= TW_PHASE_INITIAL
;
1524 retval
= twl_scsiop_execute_scsi(tw_dev
, request_id
, NULL
, 0, NULL
);
1526 tw_dev
->state
[request_id
] = TW_S_COMPLETED
;
1527 twl_free_request_id(tw_dev
, request_id
);
1528 SCpnt
->result
= (DID_ERROR
<< 16);
1534 } /* End twl_scsi_queue() */
1536 static DEF_SCSI_QCMD(twl_scsi_queue
)
1538 /* This function tells the controller to shut down */
1539 static void __twl_shutdown(TW_Device_Extension
*tw_dev
)
1541 /* Disable interrupts */
1542 TWL_MASK_INTERRUPTS(tw_dev
);
1544 /* Free up the IRQ */
1545 free_irq(tw_dev
->tw_pci_dev
->irq
, tw_dev
);
1547 printk(KERN_WARNING
"3w-sas: Shutting down host %d.\n", tw_dev
->host
->host_no
);
1549 /* Tell the card we are shutting down */
1550 if (twl_initconnection(tw_dev
, 1, 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
)) {
1551 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x16, "Connection shutdown failed");
1553 printk(KERN_WARNING
"3w-sas: Shutdown complete.\n");
1556 /* Clear doorbell interrupt just before exit */
1557 TWL_CLEAR_DB_INTERRUPT(tw_dev
);
1558 } /* End __twl_shutdown() */
1560 /* Wrapper for __twl_shutdown */
1561 static void twl_shutdown(struct pci_dev
*pdev
)
1563 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
1564 TW_Device_Extension
*tw_dev
;
1569 tw_dev
= (TW_Device_Extension
*)host
->hostdata
;
1572 __twl_shutdown(tw_dev
);
1573 } /* End twl_shutdown() */
1575 /* This function configures unit settings when a unit is coming on-line */
1576 static int twl_slave_configure(struct scsi_device
*sdev
)
1578 /* Force 60 second timeout */
1579 blk_queue_rq_timeout(sdev
->request_queue
, 60 * HZ
);
1582 } /* End twl_slave_configure() */
1584 /* scsi_host_template initializer */
1585 static struct scsi_host_template driver_template
= {
1586 .module
= THIS_MODULE
,
1588 .queuecommand
= twl_scsi_queue
,
1589 .eh_host_reset_handler
= twl_scsi_eh_reset
,
1590 .bios_param
= twl_scsi_biosparam
,
1591 .change_queue_depth
= twl_change_queue_depth
,
1592 .can_queue
= TW_Q_LENGTH
-2,
1593 .slave_configure
= twl_slave_configure
,
1595 .sg_tablesize
= TW_LIBERATOR_MAX_SGL_LENGTH
,
1596 .max_sectors
= TW_MAX_SECTORS
,
1597 .cmd_per_lun
= TW_MAX_CMDS_PER_LUN
,
1598 .use_clustering
= ENABLE_CLUSTERING
,
1599 .shost_attrs
= twl_host_attrs
,
1604 /* This function will probe and initialize a card */
1605 static int twl_probe(struct pci_dev
*pdev
, const struct pci_device_id
*dev_id
)
1607 struct Scsi_Host
*host
= NULL
;
1608 TW_Device_Extension
*tw_dev
;
1609 int retval
= -ENODEV
;
1610 int *ptr_phycount
, phycount
=0;
1612 retval
= pci_enable_device(pdev
);
1614 TW_PRINTK(host
, TW_DRIVER
, 0x17, "Failed to enable pci device");
1615 goto out_disable_device
;
1618 pci_set_master(pdev
);
1619 pci_try_set_mwi(pdev
);
1621 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))
1622 || pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64)))
1623 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))
1624 || pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32))) {
1625 TW_PRINTK(host
, TW_DRIVER
, 0x18, "Failed to set dma mask");
1627 goto out_disable_device
;
1630 host
= scsi_host_alloc(&driver_template
, sizeof(TW_Device_Extension
));
1632 TW_PRINTK(host
, TW_DRIVER
, 0x19, "Failed to allocate memory for device extension");
1634 goto out_disable_device
;
1636 tw_dev
= shost_priv(host
);
1638 /* Save values to device extension */
1639 tw_dev
->host
= host
;
1640 tw_dev
->tw_pci_dev
= pdev
;
1642 if (twl_initialize_device_extension(tw_dev
)) {
1643 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x1a, "Failed to initialize device extension");
1644 goto out_free_device_extension
;
1647 /* Request IO regions */
1648 retval
= pci_request_regions(pdev
, "3w-sas");
1650 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x1b, "Failed to get mem region");
1651 goto out_free_device_extension
;
1654 /* Save base address, use region 1 */
1655 tw_dev
->base_addr
= pci_iomap(pdev
, 1, 0);
1656 if (!tw_dev
->base_addr
) {
1657 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x1c, "Failed to ioremap");
1658 goto out_release_mem_region
;
1661 /* Disable interrupts on the card */
1662 TWL_MASK_INTERRUPTS(tw_dev
);
1664 /* Initialize the card */
1665 if (twl_reset_sequence(tw_dev
, 0)) {
1666 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x1d, "Controller reset failed during probe");
1670 /* Set host specific parameters */
1671 host
->max_id
= TW_MAX_UNITS
;
1672 host
->max_cmd_len
= TW_MAX_CDB_LEN
;
1673 host
->max_lun
= TW_MAX_LUNS
;
1674 host
->max_channel
= 0;
1676 /* Register the card with the kernel SCSI layer */
1677 retval
= scsi_add_host(host
, &pdev
->dev
);
1679 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x1e, "scsi add host failed");
1683 pci_set_drvdata(pdev
, host
);
1685 printk(KERN_WARNING
"3w-sas: scsi%d: Found an LSI 3ware %s Controller at 0x%llx, IRQ: %d.\n",
1687 (char *)twl_get_param(tw_dev
, 1, TW_VERSION_TABLE
,
1688 TW_PARAM_MODEL
, TW_PARAM_MODEL_LENGTH
),
1689 (u64
)pci_resource_start(pdev
, 1), pdev
->irq
);
1691 ptr_phycount
= twl_get_param(tw_dev
, 2, TW_PARAM_PHY_SUMMARY_TABLE
,
1692 TW_PARAM_PHYCOUNT
, TW_PARAM_PHYCOUNT_LENGTH
);
1694 phycount
= le32_to_cpu(*(int *)ptr_phycount
);
1696 printk(KERN_WARNING
"3w-sas: scsi%d: Firmware %s, BIOS %s, Phys: %d.\n",
1698 (char *)twl_get_param(tw_dev
, 1, TW_VERSION_TABLE
,
1699 TW_PARAM_FWVER
, TW_PARAM_FWVER_LENGTH
),
1700 (char *)twl_get_param(tw_dev
, 2, TW_VERSION_TABLE
,
1701 TW_PARAM_BIOSVER
, TW_PARAM_BIOSVER_LENGTH
),
1704 /* Try to enable MSI */
1705 if (use_msi
&& !pci_enable_msi(pdev
))
1706 set_bit(TW_USING_MSI
, &tw_dev
->flags
);
1708 /* Now setup the interrupt handler */
1709 retval
= request_irq(pdev
->irq
, twl_interrupt
, IRQF_SHARED
, "3w-sas", tw_dev
);
1711 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x1f, "Error requesting IRQ");
1712 goto out_remove_host
;
1715 twl_device_extension_list
[twl_device_extension_count
] = tw_dev
;
1716 twl_device_extension_count
++;
1718 /* Re-enable interrupts on the card */
1719 TWL_UNMASK_INTERRUPTS(tw_dev
);
1721 /* Finally, scan the host */
1722 scsi_scan_host(host
);
1724 /* Add sysfs binary files */
1725 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
, &twl_sysfs_aen_read_attr
))
1726 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x20, "Failed to create sysfs binary file: 3ware_aen_read");
1727 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
, &twl_sysfs_compat_info_attr
))
1728 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x21, "Failed to create sysfs binary file: 3ware_compat_info");
1730 if (twl_major
== -1) {
1731 if ((twl_major
= register_chrdev (0, "twl", &twl_fops
)) < 0)
1732 TW_PRINTK(host
, TW_DRIVER
, 0x22, "Failed to register character device");
1738 if (test_bit(TW_USING_MSI
, &tw_dev
->flags
))
1739 pci_disable_msi(pdev
);
1740 scsi_remove_host(host
);
1742 iounmap(tw_dev
->base_addr
);
1743 out_release_mem_region
:
1744 pci_release_regions(pdev
);
1745 out_free_device_extension
:
1746 twl_free_device_extension(tw_dev
);
1747 scsi_host_put(host
);
1749 pci_disable_device(pdev
);
1752 } /* End twl_probe() */
1754 /* This function is called to remove a device */
1755 static void twl_remove(struct pci_dev
*pdev
)
1757 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
1758 TW_Device_Extension
*tw_dev
;
1763 tw_dev
= (TW_Device_Extension
*)host
->hostdata
;
1765 if (!tw_dev
->online
)
1768 /* Remove sysfs binary files */
1769 sysfs_remove_bin_file(&host
->shost_dev
.kobj
, &twl_sysfs_aen_read_attr
);
1770 sysfs_remove_bin_file(&host
->shost_dev
.kobj
, &twl_sysfs_compat_info_attr
);
1772 scsi_remove_host(tw_dev
->host
);
1774 /* Unregister character device */
1775 if (twl_major
>= 0) {
1776 unregister_chrdev(twl_major
, "twl");
1780 /* Shutdown the card */
1781 __twl_shutdown(tw_dev
);
1783 /* Disable MSI if enabled */
1784 if (test_bit(TW_USING_MSI
, &tw_dev
->flags
))
1785 pci_disable_msi(pdev
);
1787 /* Free IO remapping */
1788 iounmap(tw_dev
->base_addr
);
1790 /* Free up the mem region */
1791 pci_release_regions(pdev
);
1793 /* Free up device extension resources */
1794 twl_free_device_extension(tw_dev
);
1796 scsi_host_put(tw_dev
->host
);
1797 pci_disable_device(pdev
);
1798 twl_device_extension_count
--;
1799 } /* End twl_remove() */
1802 /* This function is called on PCI suspend */
1803 static int twl_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1805 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
1806 TW_Device_Extension
*tw_dev
= (TW_Device_Extension
*)host
->hostdata
;
1808 printk(KERN_WARNING
"3w-sas: Suspending host %d.\n", tw_dev
->host
->host_no
);
1809 /* Disable interrupts */
1810 TWL_MASK_INTERRUPTS(tw_dev
);
1812 free_irq(tw_dev
->tw_pci_dev
->irq
, tw_dev
);
1814 /* Tell the card we are shutting down */
1815 if (twl_initconnection(tw_dev
, 1, 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
)) {
1816 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x23, "Connection shutdown failed during suspend");
1818 printk(KERN_WARNING
"3w-sas: Suspend complete.\n");
1821 /* Clear doorbell interrupt */
1822 TWL_CLEAR_DB_INTERRUPT(tw_dev
);
1824 pci_save_state(pdev
);
1825 pci_disable_device(pdev
);
1826 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1829 } /* End twl_suspend() */
1831 /* This function is called on PCI resume */
1832 static int twl_resume(struct pci_dev
*pdev
)
1835 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
1836 TW_Device_Extension
*tw_dev
= (TW_Device_Extension
*)host
->hostdata
;
1838 printk(KERN_WARNING
"3w-sas: Resuming host %d.\n", tw_dev
->host
->host_no
);
1839 pci_set_power_state(pdev
, PCI_D0
);
1840 pci_enable_wake(pdev
, PCI_D0
, 0);
1841 pci_restore_state(pdev
);
1843 retval
= pci_enable_device(pdev
);
1845 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x24, "Enable device failed during resume");
1849 pci_set_master(pdev
);
1850 pci_try_set_mwi(pdev
);
1852 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))
1853 || pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64)))
1854 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))
1855 || pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32))) {
1856 TW_PRINTK(host
, TW_DRIVER
, 0x25, "Failed to set dma mask during resume");
1858 goto out_disable_device
;
1861 /* Initialize the card */
1862 if (twl_reset_sequence(tw_dev
, 0)) {
1864 goto out_disable_device
;
1867 /* Now setup the interrupt handler */
1868 retval
= request_irq(pdev
->irq
, twl_interrupt
, IRQF_SHARED
, "3w-sas", tw_dev
);
1870 TW_PRINTK(tw_dev
->host
, TW_DRIVER
, 0x26, "Error requesting IRQ during resume");
1872 goto out_disable_device
;
1875 /* Now enable MSI if enabled */
1876 if (test_bit(TW_USING_MSI
, &tw_dev
->flags
))
1877 pci_enable_msi(pdev
);
1879 /* Re-enable interrupts on the card */
1880 TWL_UNMASK_INTERRUPTS(tw_dev
);
1882 printk(KERN_WARNING
"3w-sas: Resume complete.\n");
1886 scsi_remove_host(host
);
1887 pci_disable_device(pdev
);
1890 } /* End twl_resume() */
1893 /* PCI Devices supported by this driver */
1894 static struct pci_device_id twl_pci_tbl
[] = {
1895 { PCI_VDEVICE(3WARE
, PCI_DEVICE_ID_3WARE_9750
) },
1898 MODULE_DEVICE_TABLE(pci
, twl_pci_tbl
);
1900 /* pci_driver initializer */
1901 static struct pci_driver twl_driver
= {
1903 .id_table
= twl_pci_tbl
,
1905 .remove
= twl_remove
,
1907 .suspend
= twl_suspend
,
1908 .resume
= twl_resume
,
1910 .shutdown
= twl_shutdown
1913 /* This function is called on driver initialization */
1914 static int __init
twl_init(void)
1916 printk(KERN_INFO
"LSI 3ware SAS/SATA-RAID Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION
);
1918 return pci_register_driver(&twl_driver
);
1919 } /* End twl_init() */
1921 /* This function is called on driver exit */
1922 static void __exit
twl_exit(void)
1924 pci_unregister_driver(&twl_driver
);
1925 } /* End twl_exit() */
1927 module_init(twl_init
);
1928 module_exit(twl_exit
);