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