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