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