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