xref: /linux/drivers/scsi/3w-sas.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2    3w-sas.c -- LSI 3ware SAS/SATA-RAID Controller device driver for Linux.
3 
4    Written By: Adam Radford <aradford@gmail.com>
5 
6    Copyright (C) 2009 LSI Corporation.
7 
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.
11 
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.
16 
17    NO WARRANTY
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.
27 
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
36 
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
40 
41    Controllers supported by this driver:
42 
43    LSI 3ware 9750 6Gb/s SAS/SATA-RAID
44 
45    Bugs/Comments/Suggestions should be mailed to:
46    aradford@gmail.com
47 
48    History
49    -------
50    3.26.02.000 - Initial driver release.
51 */
52 
53 #include <linux/module.h>
54 #include <linux/reboot.h>
55 #include <linux/spinlock.h>
56 #include <linux/interrupt.h>
57 #include <linux/moduleparam.h>
58 #include <linux/errno.h>
59 #include <linux/types.h>
60 #include <linux/delay.h>
61 #include <linux/pci.h>
62 #include <linux/time.h>
63 #include <linux/mutex.h>
64 #include <linux/slab.h>
65 #include <asm/io.h>
66 #include <asm/irq.h>
67 #include <linux/uaccess.h>
68 #include <scsi/scsi.h>
69 #include <scsi/scsi_host.h>
70 #include <scsi/scsi_tcq.h>
71 #include <scsi/scsi_cmnd.h>
72 #include "3w-sas.h"
73 
74 /* Globals */
75 #define TW_DRIVER_VERSION "3.26.02.000"
76 static DEFINE_MUTEX(twl_chrdev_mutex);
77 static TW_Device_Extension *twl_device_extension_list[TW_MAX_SLOT];
78 static unsigned int twl_device_extension_count;
79 static int twl_major = -1;
80 extern struct timezone sys_tz;
81 
82 /* Module parameters */
83 MODULE_AUTHOR ("LSI");
84 MODULE_DESCRIPTION ("LSI 3ware SAS/SATA-RAID Linux Driver");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(TW_DRIVER_VERSION);
87 
88 static int use_msi;
89 module_param(use_msi, int, S_IRUGO);
90 MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
91 
92 /* Function prototypes */
93 static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset);
94 
95 /* Functions */
96 
97 /* This function returns AENs through sysfs */
98 static ssize_t twl_sysfs_aen_read(struct file *filp, struct kobject *kobj,
99 				  struct bin_attribute *bin_attr,
100 				  char *outbuf, loff_t offset, size_t count)
101 {
102 	struct device *dev = container_of(kobj, struct device, kobj);
103 	struct Scsi_Host *shost = class_to_shost(dev);
104 	TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
105 	unsigned long flags = 0;
106 	ssize_t ret;
107 
108 	if (!capable(CAP_SYS_ADMIN))
109 		return -EACCES;
110 
111 	spin_lock_irqsave(tw_dev->host->host_lock, flags);
112 	ret = memory_read_from_buffer(outbuf, count, &offset, tw_dev->event_queue[0], sizeof(TW_Event) * TW_Q_LENGTH);
113 	spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
114 
115 	return ret;
116 } /* End twl_sysfs_aen_read() */
117 
118 /* aen_read sysfs attribute initializer */
119 static struct bin_attribute twl_sysfs_aen_read_attr = {
120 	.attr = {
121 		.name = "3ware_aen_read",
122 		.mode = S_IRUSR,
123 	},
124 	.size = 0,
125 	.read = twl_sysfs_aen_read
126 };
127 
128 /* This function returns driver compatibility info through sysfs */
129 static ssize_t twl_sysfs_compat_info(struct file *filp, struct kobject *kobj,
130 				     struct bin_attribute *bin_attr,
131 				     char *outbuf, loff_t offset, size_t count)
132 {
133 	struct device *dev = container_of(kobj, struct device, kobj);
134 	struct Scsi_Host *shost = class_to_shost(dev);
135 	TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
136 	unsigned long flags = 0;
137 	ssize_t ret;
138 
139 	if (!capable(CAP_SYS_ADMIN))
140 		return -EACCES;
141 
142 	spin_lock_irqsave(tw_dev->host->host_lock, flags);
143 	ret = memory_read_from_buffer(outbuf, count, &offset, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
144 	spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
145 
146 	return ret;
147 } /* End twl_sysfs_compat_info() */
148 
149 /* compat_info sysfs attribute initializer */
150 static struct bin_attribute twl_sysfs_compat_info_attr = {
151 	.attr = {
152 		.name = "3ware_compat_info",
153 		.mode = S_IRUSR,
154 	},
155 	.size = 0,
156 	.read = twl_sysfs_compat_info
157 };
158 
159 /* Show some statistics about the card */
160 static ssize_t twl_show_stats(struct device *dev,
161 			      struct device_attribute *attr, char *buf)
162 {
163 	struct Scsi_Host *host = class_to_shost(dev);
164 	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
165 	unsigned long flags = 0;
166 	ssize_t len;
167 
168 	spin_lock_irqsave(tw_dev->host->host_lock, flags);
169 	len = snprintf(buf, PAGE_SIZE, "3w-sas Driver version: %s\n"
170 		       "Current commands posted:   %4d\n"
171 		       "Max commands posted:       %4d\n"
172 		       "Last sgl length:           %4d\n"
173 		       "Max sgl length:            %4d\n"
174 		       "Last sector count:         %4d\n"
175 		       "Max sector count:          %4d\n"
176 		       "SCSI Host Resets:          %4d\n"
177 		       "AEN's:                     %4d\n",
178 		       TW_DRIVER_VERSION,
179 		       tw_dev->posted_request_count,
180 		       tw_dev->max_posted_request_count,
181 		       tw_dev->sgl_entries,
182 		       tw_dev->max_sgl_entries,
183 		       tw_dev->sector_count,
184 		       tw_dev->max_sector_count,
185 		       tw_dev->num_resets,
186 		       tw_dev->aen_count);
187 	spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
188 	return len;
189 } /* End twl_show_stats() */
190 
191 /* stats sysfs attribute initializer */
192 static struct device_attribute twl_host_stats_attr = {
193 	.attr = {
194 		.name = 	"3ware_stats",
195 		.mode =		S_IRUGO,
196 	},
197 	.show = twl_show_stats
198 };
199 
200 /* Host attributes initializer */
201 static struct device_attribute *twl_host_attrs[] = {
202 	&twl_host_stats_attr,
203 	NULL,
204 };
205 
206 /* This function will look up an AEN severity string */
207 static char *twl_aen_severity_lookup(unsigned char severity_code)
208 {
209 	char *retval = NULL;
210 
211 	if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
212 	    (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
213 		goto out;
214 
215 	retval = twl_aen_severity_table[severity_code];
216 out:
217 	return retval;
218 } /* End twl_aen_severity_lookup() */
219 
220 /* This function will queue an event */
221 static void twl_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
222 {
223 	u32 local_time;
224 	TW_Event *event;
225 	unsigned short aen;
226 	char host[16];
227 	char *error_str;
228 
229 	tw_dev->aen_count++;
230 
231 	/* Fill out event info */
232 	event = tw_dev->event_queue[tw_dev->error_index];
233 
234 	host[0] = '\0';
235 	if (tw_dev->host)
236 		sprintf(host, " scsi%d:", tw_dev->host->host_no);
237 
238 	aen = le16_to_cpu(header->status_block.error);
239 	memset(event, 0, sizeof(TW_Event));
240 
241 	event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
242 	/* event->time_stamp_sec overflows in y2106 */
243 	local_time = (u32)(ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
244 	event->time_stamp_sec = local_time;
245 	event->aen_code = aen;
246 	event->retrieved = TW_AEN_NOT_RETRIEVED;
247 	event->sequence_id = tw_dev->error_sequence_id;
248 	tw_dev->error_sequence_id++;
249 
250 	/* Check for embedded error string */
251 	error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
252 
253 	header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
254 	event->parameter_len = strlen(header->err_specific_desc);
255 	memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + 1 + strlen(error_str));
256 	if (event->severity != TW_AEN_SEVERITY_DEBUG)
257 		printk(KERN_WARNING "3w-sas:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
258 		       host,
259 		       twl_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
260 		       TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen, error_str,
261 		       header->err_specific_desc);
262 	else
263 		tw_dev->aen_count--;
264 
265 	tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
266 } /* End twl_aen_queue_event() */
267 
268 /* This function will attempt to post a command packet to the board */
269 static int twl_post_command_packet(TW_Device_Extension *tw_dev, int request_id)
270 {
271 	dma_addr_t command_que_value;
272 
273 	command_que_value = tw_dev->command_packet_phys[request_id];
274 	command_que_value += TW_COMMAND_OFFSET;
275 
276 	/* First write upper 4 bytes */
277 	writel((u32)((u64)command_que_value >> 32), TWL_HIBQPH_REG_ADDR(tw_dev));
278 	/* Then the lower 4 bytes */
279 	writel((u32)(command_que_value | TWL_PULL_MODE), TWL_HIBQPL_REG_ADDR(tw_dev));
280 
281 	tw_dev->state[request_id] = TW_S_POSTED;
282 	tw_dev->posted_request_count++;
283 	if (tw_dev->posted_request_count > tw_dev->max_posted_request_count)
284 		tw_dev->max_posted_request_count = tw_dev->posted_request_count;
285 
286 	return 0;
287 } /* End twl_post_command_packet() */
288 
289 /* This function hands scsi cdb's to the firmware */
290 static int twl_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id,
291 				   unsigned char *cdb, int use_sg,
292 				   TW_SG_Entry_ISO *sglistarg)
293 {
294 	TW_Command_Full *full_command_packet;
295 	TW_Command_Apache *command_packet;
296 	int i, sg_count;
297 	struct scsi_cmnd *srb = NULL;
298 	struct scatterlist *sglist = NULL, *sg;
299 	int retval = 1;
300 
301 	if (tw_dev->srb[request_id]) {
302 		srb = tw_dev->srb[request_id];
303 		if (scsi_sglist(srb))
304 			sglist = scsi_sglist(srb);
305 	}
306 
307 	/* Initialize command packet */
308 	full_command_packet = tw_dev->command_packet_virt[request_id];
309 	full_command_packet->header.header_desc.size_header = 128;
310 	full_command_packet->header.status_block.error = 0;
311 	full_command_packet->header.status_block.severity__reserved = 0;
312 
313 	command_packet = &full_command_packet->command.newcommand;
314 	command_packet->status = 0;
315 	command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
316 
317 	/* We forced 16 byte cdb use earlier */
318 	if (!cdb)
319 		memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
320 	else
321 		memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
322 
323 	if (srb) {
324 		command_packet->unit = srb->device->id;
325 		command_packet->request_id__lunl =
326 			cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
327 	} else {
328 		command_packet->request_id__lunl =
329 			cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
330 		command_packet->unit = 0;
331 	}
332 
333 	command_packet->sgl_offset = 16;
334 
335 	if (!sglistarg) {
336 		/* Map sglist from scsi layer to cmd packet */
337 		if (scsi_sg_count(srb)) {
338 			sg_count = scsi_dma_map(srb);
339 			if (sg_count <= 0)
340 				goto out;
341 
342 			scsi_for_each_sg(srb, sg, sg_count, i) {
343 				command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
344 				command_packet->sg_list[i].length = TW_CPU_TO_SGL(sg_dma_len(sg));
345 			}
346 			command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
347 		}
348 	} else {
349 		/* Internal cdb post */
350 		for (i = 0; i < use_sg; i++) {
351 			command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
352 			command_packet->sg_list[i].length = TW_CPU_TO_SGL(sglistarg[i].length);
353 		}
354 		command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
355 	}
356 
357 	/* Update some stats */
358 	if (srb) {
359 		tw_dev->sector_count = scsi_bufflen(srb) / 512;
360 		if (tw_dev->sector_count > tw_dev->max_sector_count)
361 			tw_dev->max_sector_count = tw_dev->sector_count;
362 		tw_dev->sgl_entries = scsi_sg_count(srb);
363 		if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
364 			tw_dev->max_sgl_entries = tw_dev->sgl_entries;
365 	}
366 
367 	/* Now post the command to the board */
368 	retval = twl_post_command_packet(tw_dev, request_id);
369 
370 out:
371 	return retval;
372 } /* End twl_scsiop_execute_scsi() */
373 
374 /* This function will read the aen queue from the isr */
375 static int twl_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
376 {
377 	unsigned char cdb[TW_MAX_CDB_LEN];
378 	TW_SG_Entry_ISO sglist[1];
379 	TW_Command_Full *full_command_packet;
380 	int retval = 1;
381 
382 	full_command_packet = tw_dev->command_packet_virt[request_id];
383 	memset(full_command_packet, 0, sizeof(TW_Command_Full));
384 
385 	/* Initialize cdb */
386 	memset(&cdb, 0, TW_MAX_CDB_LEN);
387 	cdb[0] = REQUEST_SENSE; /* opcode */
388 	cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
389 
390 	/* Initialize sglist */
391 	memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
392 	sglist[0].length = TW_SECTOR_SIZE;
393 	sglist[0].address = tw_dev->generic_buffer_phys[request_id];
394 
395 	/* Mark internal command */
396 	tw_dev->srb[request_id] = NULL;
397 
398 	/* Now post the command packet */
399 	if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
400 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Post failed while reading AEN queue");
401 		goto out;
402 	}
403 	retval = 0;
404 out:
405 	return retval;
406 } /* End twl_aen_read_queue() */
407 
408 /* This function will sync firmware time with the host time */
409 static void twl_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
410 {
411 	u32 schedulertime;
412 	TW_Command_Full *full_command_packet;
413 	TW_Command *command_packet;
414 	TW_Param_Apache *param;
415 	time64_t local_time;
416 
417 	/* Fill out the command packet */
418 	full_command_packet = tw_dev->command_packet_virt[request_id];
419 	memset(full_command_packet, 0, sizeof(TW_Command_Full));
420 	command_packet = &full_command_packet->command.oldcommand;
421 	command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
422 	command_packet->request_id = request_id;
423 	command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
424 	command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
425 	command_packet->size = TW_COMMAND_SIZE;
426 	command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
427 
428 	/* Setup the param */
429 	param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
430 	memset(param, 0, TW_SECTOR_SIZE);
431 	param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
432 	param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
433 	param->parameter_size_bytes = cpu_to_le16(4);
434 
435 	/* Convert system time in UTC to local time seconds since last
436            Sunday 12:00AM */
437 	local_time = (ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
438 	div_u64_rem(local_time - (3 * 86400), 604800, &schedulertime);
439 	schedulertime = cpu_to_le32(schedulertime);
440 
441 	memcpy(param->data, &schedulertime, sizeof(u32));
442 
443 	/* Mark internal command */
444 	tw_dev->srb[request_id] = NULL;
445 
446 	/* Now post the command */
447 	twl_post_command_packet(tw_dev, request_id);
448 } /* End twl_aen_sync_time() */
449 
450 /* This function will assign an available request id */
451 static void twl_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
452 {
453 	*request_id = tw_dev->free_queue[tw_dev->free_head];
454 	tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
455 	tw_dev->state[*request_id] = TW_S_STARTED;
456 } /* End twl_get_request_id() */
457 
458 /* This function will free a request id */
459 static void twl_free_request_id(TW_Device_Extension *tw_dev, int request_id)
460 {
461 	tw_dev->free_queue[tw_dev->free_tail] = request_id;
462 	tw_dev->state[request_id] = TW_S_FINISHED;
463 	tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
464 } /* End twl_free_request_id() */
465 
466 /* This function will complete an aen request from the isr */
467 static int twl_aen_complete(TW_Device_Extension *tw_dev, int request_id)
468 {
469 	TW_Command_Full *full_command_packet;
470 	TW_Command *command_packet;
471 	TW_Command_Apache_Header *header;
472 	unsigned short aen;
473 	int retval = 1;
474 
475 	header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
476 	tw_dev->posted_request_count--;
477 	aen = le16_to_cpu(header->status_block.error);
478 	full_command_packet = tw_dev->command_packet_virt[request_id];
479 	command_packet = &full_command_packet->command.oldcommand;
480 
481 	/* First check for internal completion of set param for time sync */
482 	if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
483 		/* Keep reading the queue in case there are more aen's */
484 		if (twl_aen_read_queue(tw_dev, request_id))
485 			goto out2;
486 	        else {
487 			retval = 0;
488 			goto out;
489 		}
490 	}
491 
492 	switch (aen) {
493 	case TW_AEN_QUEUE_EMPTY:
494 		/* Quit reading the queue if this is the last one */
495 		break;
496 	case TW_AEN_SYNC_TIME_WITH_HOST:
497 		twl_aen_sync_time(tw_dev, request_id);
498 		retval = 0;
499 		goto out;
500 	default:
501 		twl_aen_queue_event(tw_dev, header);
502 
503 		/* If there are more aen's, keep reading the queue */
504 		if (twl_aen_read_queue(tw_dev, request_id))
505 			goto out2;
506 		else {
507 			retval = 0;
508 			goto out;
509 		}
510 	}
511 	retval = 0;
512 out2:
513 	tw_dev->state[request_id] = TW_S_COMPLETED;
514 	twl_free_request_id(tw_dev, request_id);
515 	clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
516 out:
517 	return retval;
518 } /* End twl_aen_complete() */
519 
520 /* This function will poll for a response */
521 static int twl_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
522 {
523 	unsigned long before;
524 	dma_addr_t mfa;
525 	u32 regh, regl;
526 	u32 response;
527 	int retval = 1;
528 	int found = 0;
529 
530 	before = jiffies;
531 
532 	while (!found) {
533 		if (sizeof(dma_addr_t) > 4) {
534 			regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
535 			regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
536 			mfa = ((u64)regh << 32) | regl;
537 		} else
538 			mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
539 
540 		response = (u32)mfa;
541 
542 		if (TW_RESID_OUT(response) == request_id)
543 			found = 1;
544 
545 		if (time_after(jiffies, before + HZ * seconds))
546 			goto out;
547 
548 		msleep(50);
549 	}
550 	retval = 0;
551 out:
552 	return retval;
553 } /* End twl_poll_response() */
554 
555 /* This function will drain the aen queue */
556 static int twl_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
557 {
558 	int request_id = 0;
559 	unsigned char cdb[TW_MAX_CDB_LEN];
560 	TW_SG_Entry_ISO sglist[1];
561 	int finished = 0, count = 0;
562 	TW_Command_Full *full_command_packet;
563 	TW_Command_Apache_Header *header;
564 	unsigned short aen;
565 	int first_reset = 0, queue = 0, retval = 1;
566 
567 	if (no_check_reset)
568 		first_reset = 0;
569 	else
570 		first_reset = 1;
571 
572 	full_command_packet = tw_dev->command_packet_virt[request_id];
573 	memset(full_command_packet, 0, sizeof(TW_Command_Full));
574 
575 	/* Initialize cdb */
576 	memset(&cdb, 0, TW_MAX_CDB_LEN);
577 	cdb[0] = REQUEST_SENSE; /* opcode */
578 	cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
579 
580 	/* Initialize sglist */
581 	memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
582 	sglist[0].length = TW_SECTOR_SIZE;
583 	sglist[0].address = tw_dev->generic_buffer_phys[request_id];
584 
585 	/* Mark internal command */
586 	tw_dev->srb[request_id] = NULL;
587 
588 	do {
589 		/* Send command to the board */
590 		if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
591 			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "Error posting request sense");
592 			goto out;
593 		}
594 
595 		/* Now poll for completion */
596 		if (twl_poll_response(tw_dev, request_id, 30)) {
597 			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "No valid response while draining AEN queue");
598 			tw_dev->posted_request_count--;
599 			goto out;
600 		}
601 
602 		tw_dev->posted_request_count--;
603 		header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
604 		aen = le16_to_cpu(header->status_block.error);
605 		queue = 0;
606 		count++;
607 
608 		switch (aen) {
609 		case TW_AEN_QUEUE_EMPTY:
610 			if (first_reset != 1)
611 				goto out;
612 			else
613 				finished = 1;
614 			break;
615 		case TW_AEN_SOFT_RESET:
616 			if (first_reset == 0)
617 				first_reset = 1;
618 			else
619 				queue = 1;
620 			break;
621 		case TW_AEN_SYNC_TIME_WITH_HOST:
622 			break;
623 		default:
624 			queue = 1;
625 		}
626 
627 		/* Now queue an event info */
628 		if (queue)
629 			twl_aen_queue_event(tw_dev, header);
630 	} while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
631 
632 	if (count == TW_MAX_AEN_DRAIN)
633 		goto out;
634 
635 	retval = 0;
636 out:
637 	tw_dev->state[request_id] = TW_S_INITIAL;
638 	return retval;
639 } /* End twl_aen_drain_queue() */
640 
641 /* This function will allocate memory and check if it is correctly aligned */
642 static int twl_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
643 {
644 	int i;
645 	dma_addr_t dma_handle;
646 	unsigned long *cpu_addr;
647 	int retval = 1;
648 
649 	cpu_addr = dma_zalloc_coherent(&tw_dev->tw_pci_dev->dev,
650 			size * TW_Q_LENGTH, &dma_handle, GFP_KERNEL);
651 	if (!cpu_addr) {
652 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
653 		goto out;
654 	}
655 
656 	for (i = 0; i < TW_Q_LENGTH; i++) {
657 		switch(which) {
658 		case 0:
659 			tw_dev->command_packet_phys[i] = dma_handle+(i*size);
660 			tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
661 			break;
662 		case 1:
663 			tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
664 			tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
665 			break;
666 		case 2:
667 			tw_dev->sense_buffer_phys[i] = dma_handle+(i*size);
668 			tw_dev->sense_buffer_virt[i] = (TW_Command_Apache_Header *)((unsigned char *)cpu_addr + (i*size));
669 			break;
670 		}
671 	}
672 	retval = 0;
673 out:
674 	return retval;
675 } /* End twl_allocate_memory() */
676 
677 /* This function will load the request id and various sgls for ioctls */
678 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)
679 {
680 	TW_Command *oldcommand;
681 	TW_Command_Apache *newcommand;
682 	TW_SG_Entry_ISO *sgl;
683 	unsigned int pae = 0;
684 
685 	if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
686 		pae = 1;
687 
688 	if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
689 		newcommand = &full_command_packet->command.newcommand;
690 		newcommand->request_id__lunl =
691 			cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
692 		if (length) {
693 			newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
694 			newcommand->sg_list[0].length = TW_CPU_TO_SGL(length);
695 		}
696 		newcommand->sgl_entries__lunh =
697 			cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
698 	} else {
699 		oldcommand = &full_command_packet->command.oldcommand;
700 		oldcommand->request_id = request_id;
701 
702 		if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
703 			/* Load the sg list */
704 			sgl = (TW_SG_Entry_ISO *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry_ISO)/4) + pae + (sizeof(dma_addr_t) > 4 ? 1 : 0));
705 			sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
706 			sgl->length = TW_CPU_TO_SGL(length);
707 			oldcommand->size += pae;
708 			oldcommand->size += sizeof(dma_addr_t) > 4 ? 1 : 0;
709 		}
710 	}
711 } /* End twl_load_sgl() */
712 
713 /* This function handles ioctl for the character device
714    This interface is used by smartmontools open source software */
715 static long twl_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
716 {
717 	long timeout;
718 	unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
719 	dma_addr_t dma_handle;
720 	int request_id = 0;
721 	TW_Ioctl_Driver_Command driver_command;
722 	struct inode *inode = file_inode(file);
723 	TW_Ioctl_Buf_Apache *tw_ioctl;
724 	TW_Command_Full *full_command_packet;
725 	TW_Device_Extension *tw_dev = twl_device_extension_list[iminor(inode)];
726 	int retval = -EFAULT;
727 	void __user *argp = (void __user *)arg;
728 
729 	mutex_lock(&twl_chrdev_mutex);
730 
731 	/* Only let one of these through at a time */
732 	if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
733 		retval = -EINTR;
734 		goto out;
735 	}
736 
737 	/* First copy down the driver command */
738 	if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
739 		goto out2;
740 
741 	/* Check data buffer size */
742 	if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
743 		retval = -EINVAL;
744 		goto out2;
745 	}
746 
747 	/* Hardware can only do multiple of 512 byte transfers */
748 	data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
749 
750 	/* Now allocate ioctl buf memory */
751 	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);
752 	if (!cpu_addr) {
753 		retval = -ENOMEM;
754 		goto out2;
755 	}
756 
757 	tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
758 
759 	/* Now copy down the entire ioctl */
760 	if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
761 		goto out3;
762 
763 	/* See which ioctl we are doing */
764 	switch (cmd) {
765 	case TW_IOCTL_FIRMWARE_PASS_THROUGH:
766 		spin_lock_irqsave(tw_dev->host->host_lock, flags);
767 		twl_get_request_id(tw_dev, &request_id);
768 
769 		/* Flag internal command */
770 		tw_dev->srb[request_id] = NULL;
771 
772 		/* Flag chrdev ioctl */
773 		tw_dev->chrdev_request_id = request_id;
774 
775 		full_command_packet = (TW_Command_Full *)&tw_ioctl->firmware_command;
776 
777 		/* Load request id and sglist for both command types */
778 		twl_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
779 
780 		memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
781 
782 		/* Now post the command packet to the controller */
783 		twl_post_command_packet(tw_dev, request_id);
784 		spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
785 
786 		timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
787 
788 		/* Now wait for command to complete */
789 		timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
790 
791 		/* We timed out, and didn't get an interrupt */
792 		if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
793 			/* Now we need to reset the board */
794 			printk(KERN_WARNING "3w-sas: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
795 			       tw_dev->host->host_no, TW_DRIVER, 0x6,
796 			       cmd);
797 			retval = -EIO;
798 			twl_reset_device_extension(tw_dev, 1);
799 			goto out3;
800 		}
801 
802 		/* Now copy in the command packet response */
803 		memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
804 
805 		/* Now complete the io */
806 		spin_lock_irqsave(tw_dev->host->host_lock, flags);
807 		tw_dev->posted_request_count--;
808 		tw_dev->state[request_id] = TW_S_COMPLETED;
809 		twl_free_request_id(tw_dev, request_id);
810 		spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
811 		break;
812 	default:
813 		retval = -ENOTTY;
814 		goto out3;
815 	}
816 
817 	/* Now copy the entire response to userspace */
818 	if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
819 		retval = 0;
820 out3:
821 	/* Now free ioctl buf memory */
822 	dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
823 out2:
824 	mutex_unlock(&tw_dev->ioctl_lock);
825 out:
826 	mutex_unlock(&twl_chrdev_mutex);
827 	return retval;
828 } /* End twl_chrdev_ioctl() */
829 
830 /* This function handles open for the character device */
831 static int twl_chrdev_open(struct inode *inode, struct file *file)
832 {
833 	unsigned int minor_number;
834 	int retval = -ENODEV;
835 
836 	if (!capable(CAP_SYS_ADMIN)) {
837 		retval = -EACCES;
838 		goto out;
839 	}
840 
841 	minor_number = iminor(inode);
842 	if (minor_number >= twl_device_extension_count)
843 		goto out;
844 	retval = 0;
845 out:
846 	return retval;
847 } /* End twl_chrdev_open() */
848 
849 /* File operations struct for character device */
850 static const struct file_operations twl_fops = {
851 	.owner		= THIS_MODULE,
852 	.unlocked_ioctl	= twl_chrdev_ioctl,
853 	.open		= twl_chrdev_open,
854 	.release	= NULL,
855 	.llseek		= noop_llseek,
856 };
857 
858 /* This function passes sense data from firmware to scsi layer */
859 static int twl_fill_sense(TW_Device_Extension *tw_dev, int i, int request_id, int copy_sense, int print_host)
860 {
861 	TW_Command_Apache_Header *header;
862 	TW_Command_Full *full_command_packet;
863 	unsigned short error;
864 	char *error_str;
865 	int retval = 1;
866 
867 	header = tw_dev->sense_buffer_virt[i];
868 	full_command_packet = tw_dev->command_packet_virt[request_id];
869 
870 	/* Get embedded firmware error string */
871 	error_str = &(header->err_specific_desc[strlen(header->err_specific_desc) + 1]);
872 
873 	/* Don't print error for Logical unit not supported during rollcall */
874 	error = le16_to_cpu(header->status_block.error);
875 	if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE) && (error != TW_ERROR_INVALID_FIELD_IN_CDB)) {
876 		if (print_host)
877 			printk(KERN_WARNING "3w-sas: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
878 			       tw_dev->host->host_no,
879 			       TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
880 			       header->status_block.error,
881 			       error_str,
882 			       header->err_specific_desc);
883 		else
884 			printk(KERN_WARNING "3w-sas: ERROR: (0x%02X:0x%04X): %s:%s.\n",
885 			       TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
886 			       header->status_block.error,
887 			       error_str,
888 			       header->err_specific_desc);
889 	}
890 
891 	if (copy_sense) {
892 		memcpy(tw_dev->srb[request_id]->sense_buffer, header->sense_data, TW_SENSE_DATA_LENGTH);
893 		tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
894 		goto out;
895 	}
896 out:
897 	return retval;
898 } /* End twl_fill_sense() */
899 
900 /* This function will free up device extension resources */
901 static void twl_free_device_extension(TW_Device_Extension *tw_dev)
902 {
903 	if (tw_dev->command_packet_virt[0])
904 		dma_free_coherent(&tw_dev->tw_pci_dev->dev,
905 				    sizeof(TW_Command_Full)*TW_Q_LENGTH,
906 				    tw_dev->command_packet_virt[0],
907 				    tw_dev->command_packet_phys[0]);
908 
909 	if (tw_dev->generic_buffer_virt[0])
910 		dma_free_coherent(&tw_dev->tw_pci_dev->dev,
911 				    TW_SECTOR_SIZE*TW_Q_LENGTH,
912 				    tw_dev->generic_buffer_virt[0],
913 				    tw_dev->generic_buffer_phys[0]);
914 
915 	if (tw_dev->sense_buffer_virt[0])
916 		dma_free_coherent(&tw_dev->tw_pci_dev->dev,
917 				    sizeof(TW_Command_Apache_Header)*
918 				    TW_Q_LENGTH,
919 				    tw_dev->sense_buffer_virt[0],
920 				    tw_dev->sense_buffer_phys[0]);
921 
922 	kfree(tw_dev->event_queue[0]);
923 } /* End twl_free_device_extension() */
924 
925 /* This function will get parameter table entries from the firmware */
926 static void *twl_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
927 {
928 	TW_Command_Full *full_command_packet;
929 	TW_Command *command_packet;
930 	TW_Param_Apache *param;
931 	void *retval = NULL;
932 
933 	/* Setup the command packet */
934 	full_command_packet = tw_dev->command_packet_virt[request_id];
935 	memset(full_command_packet, 0, sizeof(TW_Command_Full));
936 	command_packet = &full_command_packet->command.oldcommand;
937 
938 	command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
939 	command_packet->size              = TW_COMMAND_SIZE;
940 	command_packet->request_id        = request_id;
941 	command_packet->byte6_offset.block_count = cpu_to_le16(1);
942 
943 	/* Now setup the param */
944 	param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
945 	memset(param, 0, TW_SECTOR_SIZE);
946 	param->table_id = cpu_to_le16(table_id | 0x8000);
947 	param->parameter_id = cpu_to_le16(parameter_id);
948 	param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
949 
950 	command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
951 	command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
952 
953 	/* Post the command packet to the board */
954 	twl_post_command_packet(tw_dev, request_id);
955 
956 	/* Poll for completion */
957 	if (twl_poll_response(tw_dev, request_id, 30))
958 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "No valid response during get param")
959 	else
960 		retval = (void *)&(param->data[0]);
961 
962 	tw_dev->posted_request_count--;
963 	tw_dev->state[request_id] = TW_S_INITIAL;
964 
965 	return retval;
966 } /* End twl_get_param() */
967 
968 /* This function will send an initconnection command to controller */
969 static int twl_initconnection(TW_Device_Extension *tw_dev, int message_credits,
970  			      u32 set_features, unsigned short current_fw_srl,
971 			      unsigned short current_fw_arch_id,
972 			      unsigned short current_fw_branch,
973 			      unsigned short current_fw_build,
974 			      unsigned short *fw_on_ctlr_srl,
975 			      unsigned short *fw_on_ctlr_arch_id,
976 			      unsigned short *fw_on_ctlr_branch,
977 			      unsigned short *fw_on_ctlr_build,
978 			      u32 *init_connect_result)
979 {
980 	TW_Command_Full *full_command_packet;
981 	TW_Initconnect *tw_initconnect;
982 	int request_id = 0, retval = 1;
983 
984 	/* Initialize InitConnection command packet */
985 	full_command_packet = tw_dev->command_packet_virt[request_id];
986 	memset(full_command_packet, 0, sizeof(TW_Command_Full));
987 	full_command_packet->header.header_desc.size_header = 128;
988 
989 	tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
990 	tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
991 	tw_initconnect->request_id = request_id;
992 	tw_initconnect->message_credits = cpu_to_le16(message_credits);
993 	tw_initconnect->features = set_features;
994 
995 	/* Turn on 64-bit sgl support if we need to */
996 	tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
997 
998 	tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
999 
1000 	if (set_features & TW_EXTENDED_INIT_CONNECT) {
1001 		tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1002 		tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1003 		tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1004 		tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1005 		tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1006 	} else
1007 		tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1008 
1009 	/* Send command packet to the board */
1010 	twl_post_command_packet(tw_dev, request_id);
1011 
1012 	/* Poll for completion */
1013 	if (twl_poll_response(tw_dev, request_id, 30)) {
1014 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x8, "No valid response during init connection");
1015 	} else {
1016 		if (set_features & TW_EXTENDED_INIT_CONNECT) {
1017 			*fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1018 			*fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1019 			*fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1020 			*fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1021 			*init_connect_result = le32_to_cpu(tw_initconnect->result);
1022 		}
1023 		retval = 0;
1024 	}
1025 
1026 	tw_dev->posted_request_count--;
1027 	tw_dev->state[request_id] = TW_S_INITIAL;
1028 
1029 	return retval;
1030 } /* End twl_initconnection() */
1031 
1032 /* This function will initialize the fields of a device extension */
1033 static int twl_initialize_device_extension(TW_Device_Extension *tw_dev)
1034 {
1035 	int i, retval = 1;
1036 
1037 	/* Initialize command packet buffers */
1038 	if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1039 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x9, "Command packet memory allocation failed");
1040 		goto out;
1041 	}
1042 
1043 	/* Initialize generic buffer */
1044 	if (twl_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1045 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Generic memory allocation failed");
1046 		goto out;
1047 	}
1048 
1049 	/* Allocate sense buffers */
1050 	if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Apache_Header), 2)) {
1051 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0xb, "Sense buffer allocation failed");
1052 		goto out;
1053 	}
1054 
1055 	/* Allocate event info space */
1056 	tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1057 	if (!tw_dev->event_queue[0]) {
1058 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "Event info memory allocation failed");
1059 		goto out;
1060 	}
1061 
1062 	for (i = 0; i < TW_Q_LENGTH; i++) {
1063 		tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1064 		tw_dev->free_queue[i] = i;
1065 		tw_dev->state[i] = TW_S_INITIAL;
1066 	}
1067 
1068 	tw_dev->free_head = TW_Q_START;
1069 	tw_dev->free_tail = TW_Q_START;
1070 	tw_dev->error_sequence_id = 1;
1071 	tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1072 
1073 	mutex_init(&tw_dev->ioctl_lock);
1074 	init_waitqueue_head(&tw_dev->ioctl_wqueue);
1075 
1076 	retval = 0;
1077 out:
1078 	return retval;
1079 } /* End twl_initialize_device_extension() */
1080 
1081 /* This function will handle attention interrupts */
1082 static int twl_handle_attention_interrupt(TW_Device_Extension *tw_dev)
1083 {
1084 	int retval = 1;
1085 	u32 request_id, doorbell;
1086 
1087 	/* Read doorbell status */
1088 	doorbell = readl(TWL_HOBDB_REG_ADDR(tw_dev));
1089 
1090 	/* Check for controller errors */
1091 	if (doorbell & TWL_DOORBELL_CONTROLLER_ERROR) {
1092 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "Microcontroller Error: clearing");
1093 		goto out;
1094 	}
1095 
1096 	/* Check if we need to perform an AEN drain */
1097 	if (doorbell & TWL_DOORBELL_ATTENTION_INTERRUPT) {
1098 		if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1099 			twl_get_request_id(tw_dev, &request_id);
1100 			if (twl_aen_read_queue(tw_dev, request_id)) {
1101 				tw_dev->state[request_id] = TW_S_COMPLETED;
1102 				twl_free_request_id(tw_dev, request_id);
1103 				clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1104 			}
1105 		}
1106 	}
1107 
1108 	retval = 0;
1109 out:
1110 	/* Clear doorbell interrupt */
1111 	TWL_CLEAR_DB_INTERRUPT(tw_dev);
1112 
1113 	/* Make sure the clear was flushed by reading it back */
1114 	readl(TWL_HOBDBC_REG_ADDR(tw_dev));
1115 
1116 	return retval;
1117 } /* End twl_handle_attention_interrupt() */
1118 
1119 /* Interrupt service routine */
1120 static irqreturn_t twl_interrupt(int irq, void *dev_instance)
1121 {
1122 	TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1123 	int i, handled = 0, error = 0;
1124 	dma_addr_t mfa = 0;
1125 	u32 reg, regl, regh, response, request_id = 0;
1126 	struct scsi_cmnd *cmd;
1127 	TW_Command_Full *full_command_packet;
1128 
1129 	spin_lock(tw_dev->host->host_lock);
1130 
1131 	/* Read host interrupt status */
1132 	reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1133 
1134 	/* Check if this is our interrupt, otherwise bail */
1135 	if (!(reg & TWL_HISTATUS_VALID_INTERRUPT))
1136 		goto twl_interrupt_bail;
1137 
1138 	handled = 1;
1139 
1140 	/* If we are resetting, bail */
1141 	if (test_bit(TW_IN_RESET, &tw_dev->flags))
1142 		goto twl_interrupt_bail;
1143 
1144 	/* Attention interrupt */
1145 	if (reg & TWL_HISTATUS_ATTENTION_INTERRUPT) {
1146 		if (twl_handle_attention_interrupt(tw_dev)) {
1147 			TWL_MASK_INTERRUPTS(tw_dev);
1148 			goto twl_interrupt_bail;
1149 		}
1150 	}
1151 
1152 	/* Response interrupt */
1153 	while (reg & TWL_HISTATUS_RESPONSE_INTERRUPT) {
1154 		if (sizeof(dma_addr_t) > 4) {
1155 			regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
1156 			regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1157 			mfa = ((u64)regh << 32) | regl;
1158 		} else
1159 			mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1160 
1161 		error = 0;
1162 		response = (u32)mfa;
1163 
1164 		/* Check for command packet error */
1165 		if (!TW_NOTMFA_OUT(response)) {
1166 			for (i=0;i<TW_Q_LENGTH;i++) {
1167 				if (tw_dev->sense_buffer_phys[i] == mfa) {
1168 					request_id = le16_to_cpu(tw_dev->sense_buffer_virt[i]->header_desc.request_id);
1169 					if (tw_dev->srb[request_id] != NULL)
1170 						error = twl_fill_sense(tw_dev, i, request_id, 1, 1);
1171 					else {
1172 						/* Skip ioctl error prints */
1173 						if (request_id != tw_dev->chrdev_request_id)
1174 							error = twl_fill_sense(tw_dev, i, request_id, 0, 1);
1175 						else
1176 							memcpy(tw_dev->command_packet_virt[request_id], tw_dev->sense_buffer_virt[i], sizeof(TW_Command_Apache_Header));
1177 					}
1178 
1179 					/* Now re-post the sense buffer */
1180 					writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1181 					writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1182 					break;
1183 				}
1184 			}
1185 		} else
1186 			request_id = TW_RESID_OUT(response);
1187 
1188 		full_command_packet = tw_dev->command_packet_virt[request_id];
1189 
1190 		/* Check for correct state */
1191 		if (tw_dev->state[request_id] != TW_S_POSTED) {
1192 			if (tw_dev->srb[request_id] != NULL) {
1193 				TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Received a request id that wasn't posted");
1194 				TWL_MASK_INTERRUPTS(tw_dev);
1195 				goto twl_interrupt_bail;
1196 			}
1197 		}
1198 
1199 		/* Check for internal command completion */
1200 		if (tw_dev->srb[request_id] == NULL) {
1201 			if (request_id != tw_dev->chrdev_request_id) {
1202 				if (twl_aen_complete(tw_dev, request_id))
1203 					TW_PRINTK(tw_dev->host, TW_DRIVER, 0xf, "Error completing AEN during attention interrupt");
1204 			} else {
1205 				tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1206 				wake_up(&tw_dev->ioctl_wqueue);
1207 			}
1208 		} else {
1209 			cmd = tw_dev->srb[request_id];
1210 
1211 			if (!error)
1212 				cmd->result = (DID_OK << 16);
1213 
1214 			/* Report residual bytes for single sgl */
1215 			if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1216 				if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1217 					scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1218 			}
1219 
1220 			/* Now complete the io */
1221 			scsi_dma_unmap(cmd);
1222 			cmd->scsi_done(cmd);
1223 			tw_dev->state[request_id] = TW_S_COMPLETED;
1224 			twl_free_request_id(tw_dev, request_id);
1225 			tw_dev->posted_request_count--;
1226 		}
1227 
1228 		/* Check for another response interrupt */
1229 		reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1230 	}
1231 
1232 twl_interrupt_bail:
1233 	spin_unlock(tw_dev->host->host_lock);
1234 	return IRQ_RETVAL(handled);
1235 } /* End twl_interrupt() */
1236 
1237 /* This function will poll for a register change */
1238 static int twl_poll_register(TW_Device_Extension *tw_dev, void *reg, u32 value, u32 result, int seconds)
1239 {
1240 	unsigned long before;
1241 	int retval = 1;
1242 	u32 reg_value;
1243 
1244 	reg_value = readl(reg);
1245 	before = jiffies;
1246 
1247         while ((reg_value & value) != result) {
1248 		reg_value = readl(reg);
1249 		if (time_after(jiffies, before + HZ * seconds))
1250 			goto out;
1251 		msleep(50);
1252 	}
1253 	retval = 0;
1254 out:
1255 	return retval;
1256 } /* End twl_poll_register() */
1257 
1258 /* This function will reset a controller */
1259 static int twl_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1260 {
1261 	int retval = 1;
1262 	int i = 0;
1263 	u32 status = 0;
1264 	unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
1265 	unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
1266 	u32 init_connect_result = 0;
1267 	int tries = 0;
1268 	int do_soft_reset = soft_reset;
1269 
1270 	while (tries < TW_MAX_RESET_TRIES) {
1271 		/* Do a soft reset if one is needed */
1272 		if (do_soft_reset) {
1273 			TWL_SOFT_RESET(tw_dev);
1274 
1275 			/* Make sure controller is in a good state */
1276 			if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, 0x0, 30)) {
1277 				TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Controller never went non-ready during reset sequence");
1278 				tries++;
1279 				continue;
1280 			}
1281 			if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, TWL_CONTROLLER_READY, 60)) {
1282 				TW_PRINTK(tw_dev->host, TW_DRIVER, 0x11, "Controller not ready during reset sequence");
1283 				tries++;
1284 				continue;
1285 			}
1286 		}
1287 
1288 		/* Initconnect */
1289 		if (twl_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
1290 				       TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
1291 				       TW_9750_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
1292 				       TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
1293 				       &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1294 				       &fw_on_ctlr_build, &init_connect_result)) {
1295 			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x12, "Initconnection failed while checking SRL");
1296 			do_soft_reset = 1;
1297 			tries++;
1298 			continue;
1299 		}
1300 
1301 		/* Load sense buffers */
1302 		while (i < TW_Q_LENGTH) {
1303 			writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1304 			writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1305 
1306 			/* Check status for over-run after each write */
1307 			status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1308 			if (!(status & TWL_STATUS_OVERRUN_SUBMIT))
1309 			    i++;
1310 		}
1311 
1312 		/* Now check status */
1313 		status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1314 		if (status) {
1315 			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "Bad controller status after loading sense buffers");
1316 			do_soft_reset = 1;
1317 			tries++;
1318 			continue;
1319 		}
1320 
1321 		/* Drain the AEN queue */
1322 		if (twl_aen_drain_queue(tw_dev, soft_reset)) {
1323 			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x14, "AEN drain failed during reset sequence");
1324 			do_soft_reset = 1;
1325 			tries++;
1326 			continue;
1327 		}
1328 
1329 		/* Load rest of compatibility struct */
1330 		strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
1331 		tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
1332 		tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
1333 		tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
1334 		tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
1335 		tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
1336 		tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
1337 		tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
1338 		tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
1339 		tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
1340 
1341 		/* If we got here, controller is in a good state */
1342 		retval = 0;
1343 		goto out;
1344 	}
1345 out:
1346 	return retval;
1347 } /* End twl_reset_sequence() */
1348 
1349 /* This function will reset a device extension */
1350 static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset)
1351 {
1352 	int i = 0, retval = 1;
1353 	unsigned long flags = 0;
1354 
1355 	/* Block SCSI requests while we are resetting */
1356 	if (ioctl_reset)
1357 		scsi_block_requests(tw_dev->host);
1358 
1359 	set_bit(TW_IN_RESET, &tw_dev->flags);
1360 	TWL_MASK_INTERRUPTS(tw_dev);
1361 	TWL_CLEAR_DB_INTERRUPT(tw_dev);
1362 
1363 	spin_lock_irqsave(tw_dev->host->host_lock, flags);
1364 
1365 	/* Abort all requests that are in progress */
1366 	for (i = 0; i < TW_Q_LENGTH; i++) {
1367 		if ((tw_dev->state[i] != TW_S_FINISHED) &&
1368 		    (tw_dev->state[i] != TW_S_INITIAL) &&
1369 		    (tw_dev->state[i] != TW_S_COMPLETED)) {
1370 			struct scsi_cmnd *cmd = tw_dev->srb[i];
1371 
1372 			if (cmd) {
1373 				cmd->result = (DID_RESET << 16);
1374 				scsi_dma_unmap(cmd);
1375 				cmd->scsi_done(cmd);
1376 			}
1377 		}
1378 	}
1379 
1380 	/* Reset queues and counts */
1381 	for (i = 0; i < TW_Q_LENGTH; i++) {
1382 		tw_dev->free_queue[i] = i;
1383 		tw_dev->state[i] = TW_S_INITIAL;
1384 	}
1385 	tw_dev->free_head = TW_Q_START;
1386 	tw_dev->free_tail = TW_Q_START;
1387 	tw_dev->posted_request_count = 0;
1388 
1389 	spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1390 
1391 	if (twl_reset_sequence(tw_dev, 1))
1392 		goto out;
1393 
1394 	TWL_UNMASK_INTERRUPTS(tw_dev);
1395 
1396 	clear_bit(TW_IN_RESET, &tw_dev->flags);
1397 	tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1398 
1399 	retval = 0;
1400 out:
1401 	if (ioctl_reset)
1402 		scsi_unblock_requests(tw_dev->host);
1403 	return retval;
1404 } /* End twl_reset_device_extension() */
1405 
1406 /* This funciton returns unit geometry in cylinders/heads/sectors */
1407 static int twl_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1408 {
1409 	int heads, sectors;
1410 	TW_Device_Extension *tw_dev;
1411 
1412 	tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1413 
1414 	if (capacity >= 0x200000) {
1415 		heads = 255;
1416 		sectors = 63;
1417 	} else {
1418 		heads = 64;
1419 		sectors = 32;
1420 	}
1421 
1422 	geom[0] = heads;
1423 	geom[1] = sectors;
1424 	geom[2] = sector_div(capacity, heads * sectors); /* cylinders */
1425 
1426 	return 0;
1427 } /* End twl_scsi_biosparam() */
1428 
1429 /* This is the new scsi eh reset function */
1430 static int twl_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1431 {
1432 	TW_Device_Extension *tw_dev = NULL;
1433 	int retval = FAILED;
1434 
1435 	tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1436 
1437 	tw_dev->num_resets++;
1438 
1439 	sdev_printk(KERN_WARNING, SCpnt->device,
1440 		"WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1441 		TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1442 
1443 	/* Make sure we are not issuing an ioctl or resetting from ioctl */
1444 	mutex_lock(&tw_dev->ioctl_lock);
1445 
1446 	/* Now reset the card and some of the device extension data */
1447 	if (twl_reset_device_extension(tw_dev, 0)) {
1448 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "Controller reset failed during scsi host reset");
1449 		goto out;
1450 	}
1451 
1452 	retval = SUCCESS;
1453 out:
1454 	mutex_unlock(&tw_dev->ioctl_lock);
1455 	return retval;
1456 } /* End twl_scsi_eh_reset() */
1457 
1458 /* This is the main scsi queue function to handle scsi opcodes */
1459 static int twl_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1460 {
1461 	int request_id, retval;
1462 	TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1463 
1464 	/* If we are resetting due to timed out ioctl, report as busy */
1465 	if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1466 		retval = SCSI_MLQUEUE_HOST_BUSY;
1467 		goto out;
1468 	}
1469 
1470 	/* Save done function into scsi_cmnd struct */
1471 	SCpnt->scsi_done = done;
1472 
1473 	/* Get a free request id */
1474 	twl_get_request_id(tw_dev, &request_id);
1475 
1476 	/* Save the scsi command for use by the ISR */
1477 	tw_dev->srb[request_id] = SCpnt;
1478 
1479 	retval = twl_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1480 	if (retval) {
1481 		tw_dev->state[request_id] = TW_S_COMPLETED;
1482 		twl_free_request_id(tw_dev, request_id);
1483 		SCpnt->result = (DID_ERROR << 16);
1484 		done(SCpnt);
1485 		retval = 0;
1486 	}
1487 out:
1488 	return retval;
1489 } /* End twl_scsi_queue() */
1490 
1491 static DEF_SCSI_QCMD(twl_scsi_queue)
1492 
1493 /* This function tells the controller to shut down */
1494 static void __twl_shutdown(TW_Device_Extension *tw_dev)
1495 {
1496 	/* Disable interrupts */
1497 	TWL_MASK_INTERRUPTS(tw_dev);
1498 
1499 	/* Free up the IRQ */
1500 	free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1501 
1502 	printk(KERN_WARNING "3w-sas: Shutting down host %d.\n", tw_dev->host->host_no);
1503 
1504 	/* Tell the card we are shutting down */
1505 	if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1506 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Connection shutdown failed");
1507 	} else {
1508 		printk(KERN_WARNING "3w-sas: Shutdown complete.\n");
1509 	}
1510 
1511 	/* Clear doorbell interrupt just before exit */
1512 	TWL_CLEAR_DB_INTERRUPT(tw_dev);
1513 } /* End __twl_shutdown() */
1514 
1515 /* Wrapper for __twl_shutdown */
1516 static void twl_shutdown(struct pci_dev *pdev)
1517 {
1518 	struct Scsi_Host *host = pci_get_drvdata(pdev);
1519 	TW_Device_Extension *tw_dev;
1520 
1521 	if (!host)
1522 		return;
1523 
1524 	tw_dev = (TW_Device_Extension *)host->hostdata;
1525 
1526 	if (tw_dev->online)
1527 		__twl_shutdown(tw_dev);
1528 } /* End twl_shutdown() */
1529 
1530 /* This function configures unit settings when a unit is coming on-line */
1531 static int twl_slave_configure(struct scsi_device *sdev)
1532 {
1533 	/* Force 60 second timeout */
1534 	blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1535 
1536 	return 0;
1537 } /* End twl_slave_configure() */
1538 
1539 /* scsi_host_template initializer */
1540 static struct scsi_host_template driver_template = {
1541 	.module			= THIS_MODULE,
1542 	.name			= "3w-sas",
1543 	.queuecommand		= twl_scsi_queue,
1544 	.eh_host_reset_handler	= twl_scsi_eh_reset,
1545 	.bios_param		= twl_scsi_biosparam,
1546 	.change_queue_depth	= scsi_change_queue_depth,
1547 	.can_queue		= TW_Q_LENGTH-2,
1548 	.slave_configure	= twl_slave_configure,
1549 	.this_id		= -1,
1550 	.sg_tablesize		= TW_LIBERATOR_MAX_SGL_LENGTH,
1551 	.max_sectors		= TW_MAX_SECTORS,
1552 	.cmd_per_lun		= TW_MAX_CMDS_PER_LUN,
1553 	.shost_attrs		= twl_host_attrs,
1554 	.emulated		= 1,
1555 	.no_write_same		= 1,
1556 };
1557 
1558 /* This function will probe and initialize a card */
1559 static int twl_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1560 {
1561 	struct Scsi_Host *host = NULL;
1562 	TW_Device_Extension *tw_dev;
1563 	int retval = -ENODEV;
1564 	int *ptr_phycount, phycount=0;
1565 
1566 	retval = pci_enable_device(pdev);
1567 	if (retval) {
1568 		TW_PRINTK(host, TW_DRIVER, 0x17, "Failed to enable pci device");
1569 		goto out_disable_device;
1570 	}
1571 
1572 	pci_set_master(pdev);
1573 	pci_try_set_mwi(pdev);
1574 
1575 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) ||
1576 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
1577 		TW_PRINTK(host, TW_DRIVER, 0x18, "Failed to set dma mask");
1578 		retval = -ENODEV;
1579 		goto out_disable_device;
1580 	}
1581 
1582 	host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
1583 	if (!host) {
1584 		TW_PRINTK(host, TW_DRIVER, 0x19, "Failed to allocate memory for device extension");
1585 		retval = -ENOMEM;
1586 		goto out_disable_device;
1587 	}
1588 	tw_dev = shost_priv(host);
1589 
1590 	/* Save values to device extension */
1591 	tw_dev->host = host;
1592 	tw_dev->tw_pci_dev = pdev;
1593 
1594 	if (twl_initialize_device_extension(tw_dev)) {
1595 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Failed to initialize device extension");
1596 		retval = -ENOMEM;
1597 		goto out_free_device_extension;
1598 	}
1599 
1600 	/* Request IO regions */
1601 	retval = pci_request_regions(pdev, "3w-sas");
1602 	if (retval) {
1603 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Failed to get mem region");
1604 		goto out_free_device_extension;
1605 	}
1606 
1607 	/* Save base address, use region 1 */
1608 	tw_dev->base_addr = pci_iomap(pdev, 1, 0);
1609 	if (!tw_dev->base_addr) {
1610 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to ioremap");
1611 		retval = -ENOMEM;
1612 		goto out_release_mem_region;
1613 	}
1614 
1615 	/* Disable interrupts on the card */
1616 	TWL_MASK_INTERRUPTS(tw_dev);
1617 
1618 	/* Initialize the card */
1619 	if (twl_reset_sequence(tw_dev, 0)) {
1620 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1d, "Controller reset failed during probe");
1621 		retval = -ENOMEM;
1622 		goto out_iounmap;
1623 	}
1624 
1625 	/* Set host specific parameters */
1626 	host->max_id = TW_MAX_UNITS;
1627 	host->max_cmd_len = TW_MAX_CDB_LEN;
1628 	host->max_lun = TW_MAX_LUNS;
1629 	host->max_channel = 0;
1630 
1631 	/* Register the card with the kernel SCSI layer */
1632 	retval = scsi_add_host(host, &pdev->dev);
1633 	if (retval) {
1634 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "scsi add host failed");
1635 		goto out_iounmap;
1636 	}
1637 
1638 	pci_set_drvdata(pdev, host);
1639 
1640 	printk(KERN_WARNING "3w-sas: scsi%d: Found an LSI 3ware %s Controller at 0x%llx, IRQ: %d.\n",
1641 	       host->host_no,
1642 	       (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1643 				     TW_PARAM_MODEL, TW_PARAM_MODEL_LENGTH),
1644 	       (u64)pci_resource_start(pdev, 1), pdev->irq);
1645 
1646 	ptr_phycount = twl_get_param(tw_dev, 2, TW_PARAM_PHY_SUMMARY_TABLE,
1647 				     TW_PARAM_PHYCOUNT, TW_PARAM_PHYCOUNT_LENGTH);
1648 	if (ptr_phycount)
1649 		phycount = le32_to_cpu(*(int *)ptr_phycount);
1650 
1651 	printk(KERN_WARNING "3w-sas: scsi%d: Firmware %s, BIOS %s, Phys: %d.\n",
1652 	       host->host_no,
1653 	       (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1654 				     TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
1655 	       (char *)twl_get_param(tw_dev, 2, TW_VERSION_TABLE,
1656 				     TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
1657 	       phycount);
1658 
1659 	/* Try to enable MSI */
1660 	if (use_msi && !pci_enable_msi(pdev))
1661 		set_bit(TW_USING_MSI, &tw_dev->flags);
1662 
1663 	/* Now setup the interrupt handler */
1664 	retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1665 	if (retval) {
1666 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Error requesting IRQ");
1667 		goto out_remove_host;
1668 	}
1669 
1670 	twl_device_extension_list[twl_device_extension_count] = tw_dev;
1671 	twl_device_extension_count++;
1672 
1673 	/* Re-enable interrupts on the card */
1674 	TWL_UNMASK_INTERRUPTS(tw_dev);
1675 
1676 	/* Finally, scan the host */
1677 	scsi_scan_host(host);
1678 
1679 	/* Add sysfs binary files */
1680 	if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr))
1681 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Failed to create sysfs binary file: 3ware_aen_read");
1682 	if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr))
1683 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Failed to create sysfs binary file: 3ware_compat_info");
1684 
1685 	if (twl_major == -1) {
1686 		if ((twl_major = register_chrdev (0, "twl", &twl_fops)) < 0)
1687 			TW_PRINTK(host, TW_DRIVER, 0x22, "Failed to register character device");
1688 	}
1689 	tw_dev->online = 1;
1690 	return 0;
1691 
1692 out_remove_host:
1693 	if (test_bit(TW_USING_MSI, &tw_dev->flags))
1694 		pci_disable_msi(pdev);
1695 	scsi_remove_host(host);
1696 out_iounmap:
1697 	iounmap(tw_dev->base_addr);
1698 out_release_mem_region:
1699 	pci_release_regions(pdev);
1700 out_free_device_extension:
1701 	twl_free_device_extension(tw_dev);
1702 	scsi_host_put(host);
1703 out_disable_device:
1704 	pci_disable_device(pdev);
1705 
1706 	return retval;
1707 } /* End twl_probe() */
1708 
1709 /* This function is called to remove a device */
1710 static void twl_remove(struct pci_dev *pdev)
1711 {
1712 	struct Scsi_Host *host = pci_get_drvdata(pdev);
1713 	TW_Device_Extension *tw_dev;
1714 
1715 	if (!host)
1716 		return;
1717 
1718 	tw_dev = (TW_Device_Extension *)host->hostdata;
1719 
1720 	if (!tw_dev->online)
1721 		return;
1722 
1723 	/* Remove sysfs binary files */
1724 	sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr);
1725 	sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr);
1726 
1727 	scsi_remove_host(tw_dev->host);
1728 
1729 	/* Unregister character device */
1730 	if (twl_major >= 0) {
1731 		unregister_chrdev(twl_major, "twl");
1732 		twl_major = -1;
1733 	}
1734 
1735 	/* Shutdown the card */
1736 	__twl_shutdown(tw_dev);
1737 
1738 	/* Disable MSI if enabled */
1739 	if (test_bit(TW_USING_MSI, &tw_dev->flags))
1740 		pci_disable_msi(pdev);
1741 
1742 	/* Free IO remapping */
1743 	iounmap(tw_dev->base_addr);
1744 
1745 	/* Free up the mem region */
1746 	pci_release_regions(pdev);
1747 
1748 	/* Free up device extension resources */
1749 	twl_free_device_extension(tw_dev);
1750 
1751 	scsi_host_put(tw_dev->host);
1752 	pci_disable_device(pdev);
1753 	twl_device_extension_count--;
1754 } /* End twl_remove() */
1755 
1756 #ifdef CONFIG_PM
1757 /* This function is called on PCI suspend */
1758 static int twl_suspend(struct pci_dev *pdev, pm_message_t state)
1759 {
1760 	struct Scsi_Host *host = pci_get_drvdata(pdev);
1761 	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1762 
1763 	printk(KERN_WARNING "3w-sas: Suspending host %d.\n", tw_dev->host->host_no);
1764 	/* Disable interrupts */
1765 	TWL_MASK_INTERRUPTS(tw_dev);
1766 
1767 	free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1768 
1769 	/* Tell the card we are shutting down */
1770 	if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1771 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x23, "Connection shutdown failed during suspend");
1772 	} else {
1773 		printk(KERN_WARNING "3w-sas: Suspend complete.\n");
1774 	}
1775 
1776 	/* Clear doorbell interrupt */
1777 	TWL_CLEAR_DB_INTERRUPT(tw_dev);
1778 
1779 	pci_save_state(pdev);
1780 	pci_disable_device(pdev);
1781 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
1782 
1783 	return 0;
1784 } /* End twl_suspend() */
1785 
1786 /* This function is called on PCI resume */
1787 static int twl_resume(struct pci_dev *pdev)
1788 {
1789 	int retval = 0;
1790 	struct Scsi_Host *host = pci_get_drvdata(pdev);
1791 	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1792 
1793 	printk(KERN_WARNING "3w-sas: Resuming host %d.\n", tw_dev->host->host_no);
1794 	pci_set_power_state(pdev, PCI_D0);
1795 	pci_enable_wake(pdev, PCI_D0, 0);
1796 	pci_restore_state(pdev);
1797 
1798 	retval = pci_enable_device(pdev);
1799 	if (retval) {
1800 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x24, "Enable device failed during resume");
1801 		return retval;
1802 	}
1803 
1804 	pci_set_master(pdev);
1805 	pci_try_set_mwi(pdev);
1806 
1807 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) ||
1808 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
1809 		TW_PRINTK(host, TW_DRIVER, 0x25, "Failed to set dma mask during resume");
1810 		retval = -ENODEV;
1811 		goto out_disable_device;
1812 	}
1813 
1814 	/* Initialize the card */
1815 	if (twl_reset_sequence(tw_dev, 0)) {
1816 		retval = -ENODEV;
1817 		goto out_disable_device;
1818 	}
1819 
1820 	/* Now setup the interrupt handler */
1821 	retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1822 	if (retval) {
1823 		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Error requesting IRQ during resume");
1824 		retval = -ENODEV;
1825 		goto out_disable_device;
1826 	}
1827 
1828 	/* Now enable MSI if enabled */
1829 	if (test_bit(TW_USING_MSI, &tw_dev->flags))
1830 		pci_enable_msi(pdev);
1831 
1832 	/* Re-enable interrupts on the card */
1833 	TWL_UNMASK_INTERRUPTS(tw_dev);
1834 
1835 	printk(KERN_WARNING "3w-sas: Resume complete.\n");
1836 	return 0;
1837 
1838 out_disable_device:
1839 	scsi_remove_host(host);
1840 	pci_disable_device(pdev);
1841 
1842 	return retval;
1843 } /* End twl_resume() */
1844 #endif
1845 
1846 /* PCI Devices supported by this driver */
1847 static struct pci_device_id twl_pci_tbl[] = {
1848 	{ PCI_VDEVICE(3WARE, PCI_DEVICE_ID_3WARE_9750) },
1849 	{ }
1850 };
1851 MODULE_DEVICE_TABLE(pci, twl_pci_tbl);
1852 
1853 /* pci_driver initializer */
1854 static struct pci_driver twl_driver = {
1855 	.name		= "3w-sas",
1856 	.id_table	= twl_pci_tbl,
1857 	.probe		= twl_probe,
1858 	.remove		= twl_remove,
1859 #ifdef CONFIG_PM
1860 	.suspend	= twl_suspend,
1861 	.resume		= twl_resume,
1862 #endif
1863 	.shutdown	= twl_shutdown
1864 };
1865 
1866 /* This function is called on driver initialization */
1867 static int __init twl_init(void)
1868 {
1869 	printk(KERN_INFO "LSI 3ware SAS/SATA-RAID Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
1870 
1871 	return pci_register_driver(&twl_driver);
1872 } /* End twl_init() */
1873 
1874 /* This function is called on driver exit */
1875 static void __exit twl_exit(void)
1876 {
1877 	pci_unregister_driver(&twl_driver);
1878 } /* End twl_exit() */
1879 
1880 module_init(twl_init);
1881 module_exit(twl_exit);
1882 
1883