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