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