Back to home page

OSCL-LXR

 
 

    


0001 /*
0002    3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
0003 
0004    Written By: Adam Radford <aradford@gmail.com>
0005    Modifications By: Tom Couch
0006 
0007    Copyright (C) 2004-2009 Applied Micro Circuits Corporation.
0008    Copyright (C) 2010 LSI Corporation.
0009 
0010    This program is free software; you can redistribute it and/or modify
0011    it under the terms of the GNU General Public License as published by
0012    the Free Software Foundation; version 2 of the License.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    NO WARRANTY
0020    THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
0021    CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
0022    LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
0023    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
0024    solely responsible for determining the appropriateness of using and
0025    distributing the Program and assumes all risks associated with its
0026    exercise of rights under this Agreement, including but not limited to
0027    the risks and costs of program errors, damage to or loss of data,
0028    programs or equipment, and unavailability or interruption of operations.
0029 
0030    DISCLAIMER OF LIABILITY
0031    NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
0032    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0033    DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
0034    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
0035    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
0036    USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
0037    HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
0038 
0039    You should have received a copy of the GNU General Public License
0040    along with this program; if not, write to the Free Software
0041    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0042 
0043    Bugs/Comments/Suggestions should be mailed to:
0044    aradford@gmail.com
0045 
0046    Note: This version of the driver does not contain a bundled firmware
0047          image.
0048 
0049    History
0050    -------
0051    2.26.02.000 - Driver cleanup for kernel submission.
0052    2.26.02.001 - Replace schedule_timeout() calls with msleep().
0053    2.26.02.002 - Add support for PAE mode.
0054                  Add lun support.
0055                  Fix twa_remove() to free irq handler/unregister_chrdev()
0056                  before shutting down card.
0057                  Change to new 'change_queue_depth' api.
0058                  Fix 'handled=1' ISR usage, remove bogus IRQ check.
0059                  Remove un-needed eh_abort handler.
0060                  Add support for embedded firmware error strings.
0061    2.26.02.003 - Correctly handle single sgl's with use_sg=1.
0062    2.26.02.004 - Add support for 9550SX controllers.
0063    2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
0064    2.26.02.006 - Fix 9550SX pchip reset timeout.
0065                  Add big endian support.
0066    2.26.02.007 - Disable local interrupts during kmap/unmap_atomic().
0067    2.26.02.008 - Free irq handler in __twa_shutdown().
0068                  Serialize reset code.
0069                  Add support for 9650SE controllers.
0070    2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails.
0071    2.26.02.010 - Add support for 9690SA controllers.
0072    2.26.02.011 - Increase max AENs drained to 256.
0073                  Add MSI support and "use_msi" module parameter.
0074                  Fix bug in twa_get_param() on 4GB+.
0075                  Use pci_resource_len() for ioremap().
0076    2.26.02.012 - Add power management support.
0077    2.26.02.013 - Fix bug in twa_load_sgl().
0078    2.26.02.014 - Force 60 second timeout default.
0079 */
0080 
0081 #include <linux/module.h>
0082 #include <linux/reboot.h>
0083 #include <linux/spinlock.h>
0084 #include <linux/interrupt.h>
0085 #include <linux/moduleparam.h>
0086 #include <linux/errno.h>
0087 #include <linux/types.h>
0088 #include <linux/delay.h>
0089 #include <linux/pci.h>
0090 #include <linux/time.h>
0091 #include <linux/mutex.h>
0092 #include <linux/slab.h>
0093 #include <asm/io.h>
0094 #include <asm/irq.h>
0095 #include <linux/uaccess.h>
0096 #include <scsi/scsi.h>
0097 #include <scsi/scsi_host.h>
0098 #include <scsi/scsi_tcq.h>
0099 #include <scsi/scsi_cmnd.h>
0100 #include "3w-9xxx.h"
0101 
0102 /* Globals */
0103 #define TW_DRIVER_VERSION "2.26.02.014"
0104 static DEFINE_MUTEX(twa_chrdev_mutex);
0105 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
0106 static unsigned int twa_device_extension_count;
0107 static int twa_major = -1;
0108 extern struct timezone sys_tz;
0109 
0110 /* Module parameters */
0111 MODULE_AUTHOR ("LSI");
0112 MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
0113 MODULE_LICENSE("GPL");
0114 MODULE_VERSION(TW_DRIVER_VERSION);
0115 
0116 static int use_msi = 0;
0117 module_param(use_msi, int, S_IRUGO);
0118 MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts.  Default: 0");
0119 
0120 /* Function prototypes */
0121 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
0122 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
0123 static char *twa_aen_severity_lookup(unsigned char severity_code);
0124 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
0125 static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
0126 static int twa_chrdev_open(struct inode *inode, struct file *file);
0127 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
0128 static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
0129 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
0130 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
0131                   u32 set_features, unsigned short current_fw_srl,
0132                   unsigned short current_fw_arch_id,
0133                   unsigned short current_fw_branch,
0134                   unsigned short current_fw_build,
0135                   unsigned short *fw_on_ctlr_srl,
0136                   unsigned short *fw_on_ctlr_arch_id,
0137                   unsigned short *fw_on_ctlr_branch,
0138                   unsigned short *fw_on_ctlr_build,
0139                   u32 *init_connect_result);
0140 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);
0141 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
0142 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
0143 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
0144 static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
0145 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
0146 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id,
0147                    unsigned char *cdb, int use_sg,
0148                    TW_SG_Entry *sglistarg);
0149 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
0150 static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
0151 
0152 /* Functions */
0153 
0154 /* Show some statistics about the card */
0155 static ssize_t twa_show_stats(struct device *dev,
0156                   struct device_attribute *attr, char *buf)
0157 {
0158     struct Scsi_Host *host = class_to_shost(dev);
0159     TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
0160     unsigned long flags = 0;
0161     ssize_t len;
0162 
0163     spin_lock_irqsave(tw_dev->host->host_lock, flags);
0164     len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
0165                "Current commands posted:   %4d\n"
0166                "Max commands posted:       %4d\n"
0167                "Current pending commands:  %4d\n"
0168                "Max pending commands:      %4d\n"
0169                "Last sgl length:           %4d\n"
0170                "Max sgl length:            %4d\n"
0171                "Last sector count:         %4d\n"
0172                "Max sector count:          %4d\n"
0173                "SCSI Host Resets:          %4d\n"
0174                "AEN's:                     %4d\n",
0175                TW_DRIVER_VERSION,
0176                tw_dev->posted_request_count,
0177                tw_dev->max_posted_request_count,
0178                tw_dev->pending_request_count,
0179                tw_dev->max_pending_request_count,
0180                tw_dev->sgl_entries,
0181                tw_dev->max_sgl_entries,
0182                tw_dev->sector_count,
0183                tw_dev->max_sector_count,
0184                tw_dev->num_resets,
0185                tw_dev->aen_count);
0186     spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
0187     return len;
0188 } /* End twa_show_stats() */
0189 
0190 /* Create sysfs 'stats' entry */
0191 static struct device_attribute twa_host_stats_attr = {
0192     .attr = {
0193         .name =     "stats",
0194         .mode =     S_IRUGO,
0195     },
0196     .show = twa_show_stats
0197 };
0198 
0199 /* Host attributes initializer */
0200 static struct attribute *twa_host_attrs[] = {
0201     &twa_host_stats_attr.attr,
0202     NULL,
0203 };
0204 
0205 ATTRIBUTE_GROUPS(twa_host);
0206 
0207 /* File operations struct for character device */
0208 static const struct file_operations twa_fops = {
0209     .owner      = THIS_MODULE,
0210     .unlocked_ioctl = twa_chrdev_ioctl,
0211     .open       = twa_chrdev_open,
0212     .release    = NULL,
0213     .llseek     = noop_llseek,
0214 };
0215 
0216 /*
0217  * The controllers use an inline buffer instead of a mapped SGL for small,
0218  * single entry buffers.  Note that we treat a zero-length transfer like
0219  * a mapped SGL.
0220  */
0221 static bool twa_command_mapped(struct scsi_cmnd *cmd)
0222 {
0223     return scsi_sg_count(cmd) != 1 ||
0224         scsi_bufflen(cmd) >= TW_MIN_SGL_LENGTH;
0225 }
0226 
0227 /* This function will complete an aen request from the isr */
0228 static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
0229 {
0230     TW_Command_Full *full_command_packet;
0231     TW_Command *command_packet;
0232     TW_Command_Apache_Header *header;
0233     unsigned short aen;
0234     int retval = 1;
0235 
0236     header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
0237     tw_dev->posted_request_count--;
0238     aen = le16_to_cpu(header->status_block.error);
0239     full_command_packet = tw_dev->command_packet_virt[request_id];
0240     command_packet = &full_command_packet->command.oldcommand;
0241 
0242     /* First check for internal completion of set param for time sync */
0243     if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
0244         /* Keep reading the queue in case there are more aen's */
0245         if (twa_aen_read_queue(tw_dev, request_id))
0246             goto out2;
0247         else {
0248             retval = 0;
0249             goto out;
0250         }
0251     }
0252 
0253     switch (aen) {
0254     case TW_AEN_QUEUE_EMPTY:
0255         /* Quit reading the queue if this is the last one */
0256         break;
0257     case TW_AEN_SYNC_TIME_WITH_HOST:
0258         twa_aen_sync_time(tw_dev, request_id);
0259         retval = 0;
0260         goto out;
0261     default:
0262         twa_aen_queue_event(tw_dev, header);
0263 
0264         /* If there are more aen's, keep reading the queue */
0265         if (twa_aen_read_queue(tw_dev, request_id))
0266             goto out2;
0267         else {
0268             retval = 0;
0269             goto out;
0270         }
0271     }
0272     retval = 0;
0273 out2:
0274     tw_dev->state[request_id] = TW_S_COMPLETED;
0275     twa_free_request_id(tw_dev, request_id);
0276     clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
0277 out:
0278     return retval;
0279 } /* End twa_aen_complete() */
0280 
0281 /* This function will drain aen queue */
0282 static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
0283 {
0284     int request_id = 0;
0285     unsigned char cdb[TW_MAX_CDB_LEN];
0286     TW_SG_Entry sglist[1];
0287     int finished = 0, count = 0;
0288     TW_Command_Full *full_command_packet;
0289     TW_Command_Apache_Header *header;
0290     unsigned short aen;
0291     int first_reset = 0, queue = 0, retval = 1;
0292 
0293     if (no_check_reset)
0294         first_reset = 0;
0295     else
0296         first_reset = 1;
0297 
0298     full_command_packet = tw_dev->command_packet_virt[request_id];
0299     memset(full_command_packet, 0, sizeof(TW_Command_Full));
0300 
0301     /* Initialize cdb */
0302     memset(&cdb, 0, TW_MAX_CDB_LEN);
0303     cdb[0] = REQUEST_SENSE; /* opcode */
0304     cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
0305 
0306     /* Initialize sglist */
0307     memset(&sglist, 0, sizeof(TW_SG_Entry));
0308     sglist[0].length = cpu_to_le32(TW_SECTOR_SIZE);
0309     sglist[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
0310 
0311     if (tw_dev->generic_buffer_phys[request_id] & TW_ALIGNMENT_9000_SGL) {
0312         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
0313         goto out;
0314     }
0315 
0316     /* Mark internal command */
0317     tw_dev->srb[request_id] = NULL;
0318 
0319     do {
0320         /* Send command to the board */
0321         if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
0322             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
0323             goto out;
0324         }
0325 
0326         /* Now poll for completion */
0327         if (twa_poll_response(tw_dev, request_id, 30)) {
0328             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
0329             tw_dev->posted_request_count--;
0330             goto out;
0331         }
0332 
0333         tw_dev->posted_request_count--;
0334         header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
0335         aen = le16_to_cpu(header->status_block.error);
0336         queue = 0;
0337         count++;
0338 
0339         switch (aen) {
0340         case TW_AEN_QUEUE_EMPTY:
0341             if (first_reset != 1)
0342                 goto out;
0343             else
0344                 finished = 1;
0345             break;
0346         case TW_AEN_SOFT_RESET:
0347             if (first_reset == 0)
0348                 first_reset = 1;
0349             else
0350                 queue = 1;
0351             break;
0352         case TW_AEN_SYNC_TIME_WITH_HOST:
0353             break;
0354         default:
0355             queue = 1;
0356         }
0357 
0358         /* Now queue an event info */
0359         if (queue)
0360             twa_aen_queue_event(tw_dev, header);
0361     } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
0362 
0363     if (count == TW_MAX_AEN_DRAIN)
0364         goto out;
0365 
0366     retval = 0;
0367 out:
0368     tw_dev->state[request_id] = TW_S_INITIAL;
0369     return retval;
0370 } /* End twa_aen_drain_queue() */
0371 
0372 /* This function will queue an event */
0373 static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
0374 {
0375     u32 local_time;
0376     TW_Event *event;
0377     unsigned short aen;
0378     char host[16];
0379     char *error_str;
0380 
0381     tw_dev->aen_count++;
0382 
0383     /* Fill out event info */
0384     event = tw_dev->event_queue[tw_dev->error_index];
0385 
0386     /* Check for clobber */
0387     host[0] = '\0';
0388     if (tw_dev->host) {
0389         sprintf(host, " scsi%d:", tw_dev->host->host_no);
0390         if (event->retrieved == TW_AEN_NOT_RETRIEVED)
0391             tw_dev->aen_clobber = 1;
0392     }
0393 
0394     aen = le16_to_cpu(header->status_block.error);
0395     memset(event, 0, sizeof(TW_Event));
0396 
0397     event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
0398     /* event->time_stamp_sec overflows in y2106 */
0399     local_time = (u32)(ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
0400     event->time_stamp_sec = local_time;
0401     event->aen_code = aen;
0402     event->retrieved = TW_AEN_NOT_RETRIEVED;
0403     event->sequence_id = tw_dev->error_sequence_id;
0404     tw_dev->error_sequence_id++;
0405 
0406     /* Check for embedded error string */
0407     error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
0408 
0409     header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
0410     event->parameter_len = strlen(header->err_specific_desc);
0411     memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
0412     if (event->severity != TW_AEN_SEVERITY_DEBUG)
0413         printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
0414                host,
0415                twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
0416                TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
0417                error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
0418                header->err_specific_desc);
0419     else
0420         tw_dev->aen_count--;
0421 
0422     if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
0423         tw_dev->event_queue_wrapped = 1;
0424     tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
0425 } /* End twa_aen_queue_event() */
0426 
0427 /* This function will read the aen queue from the isr */
0428 static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
0429 {
0430     unsigned char cdb[TW_MAX_CDB_LEN];
0431     TW_SG_Entry sglist[1];
0432     TW_Command_Full *full_command_packet;
0433     int retval = 1;
0434 
0435     full_command_packet = tw_dev->command_packet_virt[request_id];
0436     memset(full_command_packet, 0, sizeof(TW_Command_Full));
0437 
0438     /* Initialize cdb */
0439     memset(&cdb, 0, TW_MAX_CDB_LEN);
0440     cdb[0] = REQUEST_SENSE; /* opcode */
0441     cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
0442 
0443     /* Initialize sglist */
0444     memset(&sglist, 0, sizeof(TW_SG_Entry));
0445     sglist[0].length = cpu_to_le32(TW_SECTOR_SIZE);
0446     sglist[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
0447 
0448     /* Mark internal command */
0449     tw_dev->srb[request_id] = NULL;
0450 
0451     /* Now post the command packet */
0452     if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
0453         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
0454         goto out;
0455     }
0456     retval = 0;
0457 out:
0458     return retval;
0459 } /* End twa_aen_read_queue() */
0460 
0461 /* This function will look up an AEN severity string */
0462 static char *twa_aen_severity_lookup(unsigned char severity_code)
0463 {
0464     char *retval = NULL;
0465 
0466     if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
0467         (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
0468         goto out;
0469 
0470     retval = twa_aen_severity_table[severity_code];
0471 out:
0472     return retval;
0473 } /* End twa_aen_severity_lookup() */
0474 
0475 /* This function will sync firmware time with the host time */
0476 static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
0477 {
0478     u32 schedulertime;
0479     TW_Command_Full *full_command_packet;
0480     TW_Command *command_packet;
0481     TW_Param_Apache *param;
0482     time64_t local_time;
0483 
0484     /* Fill out the command packet */
0485     full_command_packet = tw_dev->command_packet_virt[request_id];
0486     memset(full_command_packet, 0, sizeof(TW_Command_Full));
0487     command_packet = &full_command_packet->command.oldcommand;
0488     command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
0489     command_packet->request_id = request_id;
0490     command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
0491     command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
0492     command_packet->size = TW_COMMAND_SIZE;
0493     command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
0494 
0495     /* Setup the param */
0496     param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
0497     memset(param, 0, TW_SECTOR_SIZE);
0498     param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
0499     param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
0500     param->parameter_size_bytes = cpu_to_le16(4);
0501 
0502     /* Convert system time in UTC to local time seconds since last
0503            Sunday 12:00AM */
0504     local_time = (ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
0505     div_u64_rem(local_time - (3 * 86400), 604800, &schedulertime);
0506 
0507     memcpy(param->data, &(__le32){cpu_to_le32(schedulertime)}, sizeof(__le32));
0508 
0509     /* Mark internal command */
0510     tw_dev->srb[request_id] = NULL;
0511 
0512     /* Now post the command */
0513     twa_post_command_packet(tw_dev, request_id, 1);
0514 } /* End twa_aen_sync_time() */
0515 
0516 /* This function will allocate memory and check if it is correctly aligned */
0517 static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
0518 {
0519     int i;
0520     dma_addr_t dma_handle;
0521     unsigned long *cpu_addr;
0522     int retval = 1;
0523 
0524     cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev,
0525             size * TW_Q_LENGTH, &dma_handle, GFP_KERNEL);
0526     if (!cpu_addr) {
0527         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
0528         goto out;
0529     }
0530 
0531     if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
0532         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
0533         dma_free_coherent(&tw_dev->tw_pci_dev->dev, size * TW_Q_LENGTH,
0534                 cpu_addr, dma_handle);
0535         goto out;
0536     }
0537 
0538     memset(cpu_addr, 0, size*TW_Q_LENGTH);
0539 
0540     for (i = 0; i < TW_Q_LENGTH; i++) {
0541         switch(which) {
0542         case 0:
0543             tw_dev->command_packet_phys[i] = dma_handle+(i*size);
0544             tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
0545             break;
0546         case 1:
0547             tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
0548             tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
0549             break;
0550         }
0551     }
0552     retval = 0;
0553 out:
0554     return retval;
0555 } /* End twa_allocate_memory() */
0556 
0557 /* This function will check the status register for unexpected bits */
0558 static int twa_check_bits(u32 status_reg_value)
0559 {
0560     int retval = 1;
0561 
0562     if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
0563         goto out;
0564     if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
0565         goto out;
0566 
0567     retval = 0;
0568 out:
0569     return retval;
0570 } /* End twa_check_bits() */
0571 
0572 /* This function will check the srl and decide if we are compatible  */
0573 static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
0574 {
0575     int retval = 1;
0576     unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
0577     unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
0578     u32 init_connect_result = 0;
0579 
0580     if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
0581                    TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
0582                    TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
0583                    TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
0584                    &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
0585                    &fw_on_ctlr_build, &init_connect_result)) {
0586         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
0587         goto out;
0588     }
0589 
0590     tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
0591     tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
0592     tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
0593 
0594     /* Try base mode compatibility */
0595     if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
0596         if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
0597                        TW_EXTENDED_INIT_CONNECT,
0598                        TW_BASE_FW_SRL, TW_9000_ARCH_ID,
0599                        TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
0600                        &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
0601                        &fw_on_ctlr_branch, &fw_on_ctlr_build,
0602                        &init_connect_result)) {
0603             TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
0604             goto out;
0605         }
0606         if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
0607             if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
0608                 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
0609             } else {
0610                 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
0611             }
0612             goto out;
0613         }
0614         tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
0615         tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
0616         tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
0617     }
0618 
0619     /* Load rest of compatibility struct */
0620     strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION,
0621         sizeof(tw_dev->tw_compat_info.driver_version));
0622     tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
0623     tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
0624     tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
0625     tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
0626     tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
0627     tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
0628     tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
0629     tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
0630     tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
0631 
0632     retval = 0;
0633 out:
0634     return retval;
0635 } /* End twa_check_srl() */
0636 
0637 /* This function handles ioctl for the character device */
0638 static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0639 {
0640     struct inode *inode = file_inode(file);
0641     long timeout;
0642     unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
0643     dma_addr_t dma_handle;
0644     int request_id = 0;
0645     unsigned int sequence_id = 0;
0646     unsigned char event_index, start_index;
0647     TW_Ioctl_Driver_Command driver_command;
0648     TW_Ioctl_Buf_Apache *tw_ioctl;
0649     TW_Lock *tw_lock;
0650     TW_Command_Full *full_command_packet;
0651     TW_Compatibility_Info *tw_compat_info;
0652     TW_Event *event;
0653     ktime_t current_time;
0654     TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
0655     int retval = TW_IOCTL_ERROR_OS_EFAULT;
0656     void __user *argp = (void __user *)arg;
0657 
0658     mutex_lock(&twa_chrdev_mutex);
0659 
0660     /* Only let one of these through at a time */
0661     if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
0662         retval = TW_IOCTL_ERROR_OS_EINTR;
0663         goto out;
0664     }
0665 
0666     /* First copy down the driver command */
0667     if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
0668         goto out2;
0669 
0670     /* Check data buffer size */
0671     if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
0672         retval = TW_IOCTL_ERROR_OS_EINVAL;
0673         goto out2;
0674     }
0675 
0676     /* Hardware can only do multiple of 512 byte transfers */
0677     data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
0678 
0679     /* Now allocate ioctl buf memory */
0680     cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev,
0681                       sizeof(TW_Ioctl_Buf_Apache) + data_buffer_length_adjusted,
0682                       &dma_handle, GFP_KERNEL);
0683     if (!cpu_addr) {
0684         retval = TW_IOCTL_ERROR_OS_ENOMEM;
0685         goto out2;
0686     }
0687 
0688     tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
0689 
0690     /* Now copy down the entire ioctl */
0691     if (copy_from_user(tw_ioctl, argp, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length))
0692         goto out3;
0693 
0694     /* See which ioctl we are doing */
0695     switch (cmd) {
0696     case TW_IOCTL_FIRMWARE_PASS_THROUGH:
0697         spin_lock_irqsave(tw_dev->host->host_lock, flags);
0698         twa_get_request_id(tw_dev, &request_id);
0699 
0700         /* Flag internal command */
0701         tw_dev->srb[request_id] = NULL;
0702 
0703         /* Flag chrdev ioctl */
0704         tw_dev->chrdev_request_id = request_id;
0705 
0706         full_command_packet = &tw_ioctl->firmware_command;
0707 
0708         /* Load request id and sglist for both command types */
0709         twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
0710 
0711         memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
0712 
0713         /* Now post the command packet to the controller */
0714         twa_post_command_packet(tw_dev, request_id, 1);
0715         spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
0716 
0717         timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
0718 
0719         /* Now wait for command to complete */
0720         timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
0721 
0722         /* We timed out, and didn't get an interrupt */
0723         if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
0724             /* Now we need to reset the board */
0725             printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
0726                    tw_dev->host->host_no, TW_DRIVER, 0x37,
0727                    cmd);
0728             retval = TW_IOCTL_ERROR_OS_EIO;
0729             twa_reset_device_extension(tw_dev);
0730             goto out3;
0731         }
0732 
0733         /* Now copy in the command packet response */
0734         memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
0735 
0736         /* Now complete the io */
0737         spin_lock_irqsave(tw_dev->host->host_lock, flags);
0738         tw_dev->posted_request_count--;
0739         tw_dev->state[request_id] = TW_S_COMPLETED;
0740         twa_free_request_id(tw_dev, request_id);
0741         spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
0742         break;
0743     case TW_IOCTL_GET_COMPATIBILITY_INFO:
0744         tw_ioctl->driver_command.status = 0;
0745         /* Copy compatibility struct into ioctl data buffer */
0746         tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
0747         memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
0748         break;
0749     case TW_IOCTL_GET_LAST_EVENT:
0750         if (tw_dev->event_queue_wrapped) {
0751             if (tw_dev->aen_clobber) {
0752                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
0753                 tw_dev->aen_clobber = 0;
0754             } else
0755                 tw_ioctl->driver_command.status = 0;
0756         } else {
0757             if (!tw_dev->error_index) {
0758                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
0759                 break;
0760             }
0761             tw_ioctl->driver_command.status = 0;
0762         }
0763         event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
0764         memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
0765         tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
0766         break;
0767     case TW_IOCTL_GET_FIRST_EVENT:
0768         if (tw_dev->event_queue_wrapped) {
0769             if (tw_dev->aen_clobber) {
0770                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
0771                 tw_dev->aen_clobber = 0;
0772             } else
0773                 tw_ioctl->driver_command.status = 0;
0774             event_index = tw_dev->error_index;
0775         } else {
0776             if (!tw_dev->error_index) {
0777                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
0778                 break;
0779             }
0780             tw_ioctl->driver_command.status = 0;
0781             event_index = 0;
0782         }
0783         memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
0784         tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
0785         break;
0786     case TW_IOCTL_GET_NEXT_EVENT:
0787         event = (TW_Event *)tw_ioctl->data_buffer;
0788         sequence_id = event->sequence_id;
0789         tw_ioctl->driver_command.status = 0;
0790 
0791         if (tw_dev->event_queue_wrapped) {
0792             if (tw_dev->aen_clobber) {
0793                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
0794                 tw_dev->aen_clobber = 0;
0795             }
0796             start_index = tw_dev->error_index;
0797         } else {
0798             if (!tw_dev->error_index) {
0799                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
0800                 break;
0801             }
0802             start_index = 0;
0803         }
0804         event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
0805 
0806         if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
0807             if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
0808                 tw_dev->aen_clobber = 1;
0809             tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
0810             break;
0811         }
0812         memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
0813         tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
0814         break;
0815     case TW_IOCTL_GET_PREVIOUS_EVENT:
0816         event = (TW_Event *)tw_ioctl->data_buffer;
0817         sequence_id = event->sequence_id;
0818         tw_ioctl->driver_command.status = 0;
0819 
0820         if (tw_dev->event_queue_wrapped) {
0821             if (tw_dev->aen_clobber) {
0822                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
0823                 tw_dev->aen_clobber = 0;
0824             }
0825             start_index = tw_dev->error_index;
0826         } else {
0827             if (!tw_dev->error_index) {
0828                 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
0829                 break;
0830             }
0831             start_index = 0;
0832         }
0833         event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
0834 
0835         if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
0836             if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
0837                 tw_dev->aen_clobber = 1;
0838             tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
0839             break;
0840         }
0841         memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
0842         tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
0843         break;
0844     case TW_IOCTL_GET_LOCK:
0845         tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
0846         current_time = ktime_get();
0847 
0848         if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) ||
0849             ktime_after(current_time, tw_dev->ioctl_time)) {
0850             tw_dev->ioctl_sem_lock = 1;
0851             tw_dev->ioctl_time = ktime_add_ms(current_time, tw_lock->timeout_msec);
0852             tw_ioctl->driver_command.status = 0;
0853             tw_lock->time_remaining_msec = tw_lock->timeout_msec;
0854         } else {
0855             tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
0856             tw_lock->time_remaining_msec = ktime_ms_delta(tw_dev->ioctl_time, current_time);
0857         }
0858         break;
0859     case TW_IOCTL_RELEASE_LOCK:
0860         if (tw_dev->ioctl_sem_lock == 1) {
0861             tw_dev->ioctl_sem_lock = 0;
0862             tw_ioctl->driver_command.status = 0;
0863         } else {
0864             tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
0865         }
0866         break;
0867     default:
0868         retval = TW_IOCTL_ERROR_OS_ENOTTY;
0869         goto out3;
0870     }
0871 
0872     /* Now copy the entire response to userspace */
0873     if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length) == 0)
0874         retval = 0;
0875 out3:
0876     /* Now free ioctl buf memory */
0877     dma_free_coherent(&tw_dev->tw_pci_dev->dev,
0878               sizeof(TW_Ioctl_Buf_Apache) + data_buffer_length_adjusted,
0879               cpu_addr, dma_handle);
0880 out2:
0881     mutex_unlock(&tw_dev->ioctl_lock);
0882 out:
0883     mutex_unlock(&twa_chrdev_mutex);
0884     return retval;
0885 } /* End twa_chrdev_ioctl() */
0886 
0887 /* This function handles open for the character device */
0888 /* NOTE that this function will race with remove. */
0889 static int twa_chrdev_open(struct inode *inode, struct file *file)
0890 {
0891     unsigned int minor_number;
0892     int retval = TW_IOCTL_ERROR_OS_ENODEV;
0893 
0894     if (!capable(CAP_SYS_ADMIN)) {
0895         retval = -EACCES;
0896         goto out;
0897     }
0898 
0899     minor_number = iminor(inode);
0900     if (minor_number >= twa_device_extension_count)
0901         goto out;
0902     retval = 0;
0903 out:
0904     return retval;
0905 } /* End twa_chrdev_open() */
0906 
0907 /* This function will print readable messages from status register errors */
0908 static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
0909 {
0910     int retval = 1;
0911 
0912     /* Check for various error conditions and handle them appropriately */
0913     if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
0914         TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
0915         writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
0916     }
0917 
0918     if (status_reg_value & TW_STATUS_PCI_ABORT) {
0919         TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
0920         writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
0921         pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
0922     }
0923 
0924     if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
0925         if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
0926              (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
0927             (!test_bit(TW_IN_RESET, &tw_dev->flags)))
0928             TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
0929         writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
0930     }
0931 
0932     if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
0933         if (tw_dev->reset_print == 0) {
0934             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
0935             tw_dev->reset_print = 1;
0936         }
0937         goto out;
0938     }
0939     retval = 0;
0940 out:
0941     return retval;
0942 } /* End twa_decode_bits() */
0943 
0944 /* This function will empty the response queue */
0945 static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
0946 {
0947     u32 status_reg_value;
0948     int count = 0, retval = 1;
0949 
0950     status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
0951 
0952     while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
0953         readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
0954         status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
0955         count++;
0956     }
0957     if (count == TW_MAX_RESPONSE_DRAIN)
0958         goto out;
0959 
0960     retval = 0;
0961 out:
0962     return retval;
0963 } /* End twa_empty_response_queue() */
0964 
0965 /* This function will clear the pchip/response queue on 9550SX */
0966 static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
0967 {
0968     u32 response_que_value = 0;
0969     unsigned long before;
0970     int retval = 1;
0971 
0972     if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
0973         before = jiffies;
0974         while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
0975             response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
0976             msleep(1);
0977             if (time_after(jiffies, before + HZ * 30))
0978                 goto out;
0979         }
0980         /* P-chip settle time */
0981         msleep(500);
0982         retval = 0;
0983     } else
0984         retval = 0;
0985 out:
0986     return retval;
0987 } /* End twa_empty_response_queue_large() */
0988 
0989 /* This function passes sense keys from firmware to scsi layer */
0990 static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
0991 {
0992     TW_Command_Full *full_command_packet;
0993     unsigned short error;
0994     int retval = 1;
0995     char *error_str;
0996 
0997     full_command_packet = tw_dev->command_packet_virt[request_id];
0998 
0999     /* Check for embedded error string */
1000     error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
1001 
1002     /* Don't print error for Logical unit not supported during rollcall */
1003     error = le16_to_cpu(full_command_packet->header.status_block.error);
1004     if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
1005         if (print_host)
1006             printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1007                    tw_dev->host->host_no,
1008                    TW_MESSAGE_SOURCE_CONTROLLER_ERROR, error,
1009                    error_str[0] ? error_str : twa_string_lookup(twa_error_table, error),
1010                    full_command_packet->header.err_specific_desc);
1011         else
1012             printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1013                    TW_MESSAGE_SOURCE_CONTROLLER_ERROR, error,
1014                    error_str[0] ? error_str : twa_string_lookup(twa_error_table, error),
1015                    full_command_packet->header.err_specific_desc);
1016     }
1017 
1018     if (copy_sense) {
1019         memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1020         tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1021         retval = TW_ISR_DONT_RESULT;
1022         goto out;
1023     }
1024     retval = 0;
1025 out:
1026     return retval;
1027 } /* End twa_fill_sense() */
1028 
1029 /* This function will free up device extension resources */
1030 static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1031 {
1032     if (tw_dev->command_packet_virt[0])
1033         dma_free_coherent(&tw_dev->tw_pci_dev->dev,
1034                 sizeof(TW_Command_Full) * TW_Q_LENGTH,
1035                 tw_dev->command_packet_virt[0],
1036                 tw_dev->command_packet_phys[0]);
1037 
1038     if (tw_dev->generic_buffer_virt[0])
1039         dma_free_coherent(&tw_dev->tw_pci_dev->dev,
1040                 TW_SECTOR_SIZE * TW_Q_LENGTH,
1041                 tw_dev->generic_buffer_virt[0],
1042                 tw_dev->generic_buffer_phys[0]);
1043 
1044     kfree(tw_dev->event_queue[0]);
1045 } /* End twa_free_device_extension() */
1046 
1047 /* This function will free a request id */
1048 static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1049 {
1050     tw_dev->free_queue[tw_dev->free_tail] = request_id;
1051     tw_dev->state[request_id] = TW_S_FINISHED;
1052     tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1053 } /* End twa_free_request_id() */
1054 
1055 /* This function will get parameter table entries from the firmware */
1056 static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1057 {
1058     TW_Command_Full *full_command_packet;
1059     TW_Command *command_packet;
1060     TW_Param_Apache *param;
1061     void *retval = NULL;
1062 
1063     /* Setup the command packet */
1064     full_command_packet = tw_dev->command_packet_virt[request_id];
1065     memset(full_command_packet, 0, sizeof(TW_Command_Full));
1066     command_packet = &full_command_packet->command.oldcommand;
1067 
1068     command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1069     command_packet->size          = TW_COMMAND_SIZE;
1070     command_packet->request_id    = request_id;
1071     command_packet->byte6_offset.block_count = cpu_to_le16(1);
1072 
1073     /* Now setup the param */
1074     param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1075     memset(param, 0, TW_SECTOR_SIZE);
1076     param->table_id = cpu_to_le16(table_id | 0x8000);
1077     param->parameter_id = cpu_to_le16(parameter_id);
1078     param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
1079 
1080     command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1081     command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1082 
1083     /* Post the command packet to the board */
1084     twa_post_command_packet(tw_dev, request_id, 1);
1085 
1086     /* Poll for completion */
1087     if (twa_poll_response(tw_dev, request_id, 30))
1088         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1089     else
1090         retval = (void *)&(param->data[0]);
1091 
1092     tw_dev->posted_request_count--;
1093     tw_dev->state[request_id] = TW_S_INITIAL;
1094 
1095     return retval;
1096 } /* End twa_get_param() */
1097 
1098 /* This function will assign an available request id */
1099 static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1100 {
1101     *request_id = tw_dev->free_queue[tw_dev->free_head];
1102     tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1103     tw_dev->state[*request_id] = TW_S_STARTED;
1104 } /* End twa_get_request_id() */
1105 
1106 /* This function will send an initconnection command to controller */
1107 static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1108                   u32 set_features, unsigned short current_fw_srl,
1109                   unsigned short current_fw_arch_id,
1110                   unsigned short current_fw_branch,
1111                   unsigned short current_fw_build,
1112                   unsigned short *fw_on_ctlr_srl,
1113                   unsigned short *fw_on_ctlr_arch_id,
1114                   unsigned short *fw_on_ctlr_branch,
1115                   unsigned short *fw_on_ctlr_build,
1116                   u32 *init_connect_result)
1117 {
1118     TW_Command_Full *full_command_packet;
1119     TW_Initconnect *tw_initconnect;
1120     int request_id = 0, retval = 1;
1121 
1122     /* Initialize InitConnection command packet */
1123     full_command_packet = tw_dev->command_packet_virt[request_id];
1124     memset(full_command_packet, 0, sizeof(TW_Command_Full));
1125     full_command_packet->header.header_desc.size_header = 128;
1126 
1127     tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1128     tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1129     tw_initconnect->request_id = request_id;
1130     tw_initconnect->message_credits = cpu_to_le16(message_credits);
1131 
1132     /* Turn on 64-bit sgl support if we need to */
1133     set_features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1134 
1135     tw_initconnect->features = cpu_to_le32(set_features);
1136 
1137     if (set_features & TW_EXTENDED_INIT_CONNECT) {
1138         tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1139         tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1140         tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1141         tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1142         tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1143     } else
1144         tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1145 
1146     /* Send command packet to the board */
1147     twa_post_command_packet(tw_dev, request_id, 1);
1148 
1149     /* Poll for completion */
1150     if (twa_poll_response(tw_dev, request_id, 30)) {
1151         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1152     } else {
1153         if (set_features & TW_EXTENDED_INIT_CONNECT) {
1154             *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1155             *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1156             *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1157             *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1158             *init_connect_result = le32_to_cpu(tw_initconnect->result);
1159         }
1160         retval = 0;
1161     }
1162 
1163     tw_dev->posted_request_count--;
1164     tw_dev->state[request_id] = TW_S_INITIAL;
1165 
1166     return retval;
1167 } /* End twa_initconnection() */
1168 
1169 /* This function will initialize the fields of a device extension */
1170 static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1171 {
1172     int i, retval = 1;
1173 
1174     /* Initialize command packet buffers */
1175     if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1176         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1177         goto out;
1178     }
1179 
1180     /* Initialize generic buffer */
1181     if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1182         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1183         goto out;
1184     }
1185 
1186     /* Allocate event info space */
1187     tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1188     if (!tw_dev->event_queue[0]) {
1189         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1190         goto out;
1191     }
1192 
1193 
1194     for (i = 0; i < TW_Q_LENGTH; i++) {
1195         tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1196         tw_dev->free_queue[i] = i;
1197         tw_dev->state[i] = TW_S_INITIAL;
1198     }
1199 
1200     tw_dev->pending_head = TW_Q_START;
1201     tw_dev->pending_tail = TW_Q_START;
1202     tw_dev->free_head = TW_Q_START;
1203     tw_dev->free_tail = TW_Q_START;
1204     tw_dev->error_sequence_id = 1;
1205     tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1206 
1207     mutex_init(&tw_dev->ioctl_lock);
1208     init_waitqueue_head(&tw_dev->ioctl_wqueue);
1209 
1210     retval = 0;
1211 out:
1212     return retval;
1213 } /* End twa_initialize_device_extension() */
1214 
1215 /* This function is the interrupt service routine */
1216 static irqreturn_t twa_interrupt(int irq, void *dev_instance)
1217 {
1218     int request_id, error = 0;
1219     u32 status_reg_value;
1220     TW_Response_Queue response_que;
1221     TW_Command_Full *full_command_packet;
1222     TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1223     int handled = 0;
1224 
1225     /* Get the per adapter lock */
1226     spin_lock(tw_dev->host->host_lock);
1227 
1228     /* Read the registers */
1229     status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1230 
1231     /* Check if this is our interrupt, otherwise bail */
1232     if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1233         goto twa_interrupt_bail;
1234 
1235     handled = 1;
1236 
1237     /* If we are resetting, bail */
1238     if (test_bit(TW_IN_RESET, &tw_dev->flags))
1239         goto twa_interrupt_bail;
1240 
1241     /* Check controller for errors */
1242     if (twa_check_bits(status_reg_value)) {
1243         if (twa_decode_bits(tw_dev, status_reg_value)) {
1244             TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1245             goto twa_interrupt_bail;
1246         }
1247     }
1248 
1249     /* Handle host interrupt */
1250     if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1251         TW_CLEAR_HOST_INTERRUPT(tw_dev);
1252 
1253     /* Handle attention interrupt */
1254     if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1255         TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1256         if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1257             twa_get_request_id(tw_dev, &request_id);
1258 
1259             error = twa_aen_read_queue(tw_dev, request_id);
1260             if (error) {
1261                 tw_dev->state[request_id] = TW_S_COMPLETED;
1262                 twa_free_request_id(tw_dev, request_id);
1263                 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1264             }
1265         }
1266     }
1267 
1268     /* Handle command interrupt */
1269     if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1270         TW_MASK_COMMAND_INTERRUPT(tw_dev);
1271         /* Drain as many pending commands as we can */
1272         while (tw_dev->pending_request_count > 0) {
1273             request_id = tw_dev->pending_queue[tw_dev->pending_head];
1274             if (tw_dev->state[request_id] != TW_S_PENDING) {
1275                 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1276                 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1277                 goto twa_interrupt_bail;
1278             }
1279             if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1280                 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1281                 tw_dev->pending_request_count--;
1282             } else {
1283                 /* If we get here, we will continue re-posting on the next command interrupt */
1284                 break;
1285             }
1286         }
1287     }
1288 
1289     /* Handle response interrupt */
1290     if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1291 
1292         /* Drain the response queue from the board */
1293         while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1294             /* Complete the response */
1295             response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1296             request_id = TW_RESID_OUT(response_que.response_id);
1297             full_command_packet = tw_dev->command_packet_virt[request_id];
1298             error = 0;
1299             /* Check for command packet errors */
1300             if (full_command_packet->command.newcommand.status != 0) {
1301                 if (tw_dev->srb[request_id] != NULL) {
1302                     error = twa_fill_sense(tw_dev, request_id, 1, 1);
1303                 } else {
1304                     /* Skip ioctl error prints */
1305                     if (request_id != tw_dev->chrdev_request_id) {
1306                         error = twa_fill_sense(tw_dev, request_id, 0, 1);
1307                     }
1308                 }
1309             }
1310 
1311             /* Check for correct state */
1312             if (tw_dev->state[request_id] != TW_S_POSTED) {
1313                 if (tw_dev->srb[request_id] != NULL) {
1314                     TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1315                     TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1316                     goto twa_interrupt_bail;
1317                 }
1318             }
1319 
1320             /* Check for internal command completion */
1321             if (tw_dev->srb[request_id] == NULL) {
1322                 if (request_id != tw_dev->chrdev_request_id) {
1323                     if (twa_aen_complete(tw_dev, request_id))
1324                         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1325                 } else {
1326                     tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1327                     wake_up(&tw_dev->ioctl_wqueue);
1328                 }
1329             } else {
1330                 struct scsi_cmnd *cmd;
1331 
1332                 cmd = tw_dev->srb[request_id];
1333 
1334                 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1335                 /* If no error command was a success */
1336                 if (error == 0) {
1337                     cmd->result = (DID_OK << 16);
1338                 }
1339 
1340                 /* If error, command failed */
1341                 if (error == 1) {
1342                     /* Ask for a host reset */
1343                     cmd->result = (DID_OK << 16) | SAM_STAT_CHECK_CONDITION;
1344                 }
1345 
1346                 /* Report residual bytes for single sgl */
1347                 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1348                     u32 length = le32_to_cpu(full_command_packet->command.newcommand.sg_list[0].length);
1349 
1350                     if (length < scsi_bufflen(cmd))
1351                         scsi_set_resid(cmd, scsi_bufflen(cmd) - length);
1352                 }
1353 
1354                 /* Now complete the io */
1355                 if (twa_command_mapped(cmd))
1356                     scsi_dma_unmap(cmd);
1357                 scsi_done(cmd);
1358                 tw_dev->state[request_id] = TW_S_COMPLETED;
1359                 twa_free_request_id(tw_dev, request_id);
1360                 tw_dev->posted_request_count--;
1361             }
1362 
1363             /* Check for valid status after each drain */
1364             status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1365             if (twa_check_bits(status_reg_value)) {
1366                 if (twa_decode_bits(tw_dev, status_reg_value)) {
1367                     TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1368                     goto twa_interrupt_bail;
1369                 }
1370             }
1371         }
1372     }
1373 
1374 twa_interrupt_bail:
1375     spin_unlock(tw_dev->host->host_lock);
1376     return IRQ_RETVAL(handled);
1377 } /* End twa_interrupt() */
1378 
1379 /* This function will load the request id and various sgls for ioctls */
1380 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)
1381 {
1382     TW_Command *oldcommand;
1383     TW_Command_Apache *newcommand;
1384     TW_SG_Entry *sgl;
1385     unsigned int pae = 0;
1386 
1387     if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1388         pae = 1;
1389 
1390     if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1391         newcommand = &full_command_packet->command.newcommand;
1392         newcommand->request_id__lunl =
1393             TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id);
1394         if (length) {
1395             newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache));
1396             newcommand->sg_list[0].length = cpu_to_le32(length);
1397         }
1398         newcommand->sgl_entries__lunh =
1399             TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0);
1400     } else {
1401         oldcommand = &full_command_packet->command.oldcommand;
1402         oldcommand->request_id = request_id;
1403 
1404         if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1405             /* Load the sg list */
1406             if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1407                 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1408             else
1409                 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1410             sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache));
1411             sgl->length = cpu_to_le32(length);
1412 
1413             oldcommand->size += pae;
1414         }
1415     }
1416 } /* End twa_load_sgl() */
1417 
1418 /* This function will poll for a response interrupt of a request */
1419 static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1420 {
1421     int retval = 1, found = 0, response_request_id;
1422     TW_Response_Queue response_queue;
1423     TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1424 
1425     if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1426         response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1427         response_request_id = TW_RESID_OUT(response_queue.response_id);
1428         if (request_id != response_request_id) {
1429             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1430             goto out;
1431         }
1432         if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1433             if (full_command_packet->command.newcommand.status != 0) {
1434                 /* bad response */
1435                 twa_fill_sense(tw_dev, request_id, 0, 0);
1436                 goto out;
1437             }
1438             found = 1;
1439         } else {
1440             if (full_command_packet->command.oldcommand.status != 0) {
1441                 /* bad response */
1442                 twa_fill_sense(tw_dev, request_id, 0, 0);
1443                 goto out;
1444             }
1445             found = 1;
1446         }
1447     }
1448 
1449     if (found)
1450         retval = 0;
1451 out:
1452     return retval;
1453 } /* End twa_poll_response() */
1454 
1455 /* This function will poll the status register for a flag */
1456 static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1457 {
1458     u32 status_reg_value;
1459     unsigned long before;
1460     int retval = 1;
1461 
1462     status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1463     before = jiffies;
1464 
1465     if (twa_check_bits(status_reg_value))
1466         twa_decode_bits(tw_dev, status_reg_value);
1467 
1468     while ((status_reg_value & flag) != flag) {
1469         status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1470 
1471         if (twa_check_bits(status_reg_value))
1472             twa_decode_bits(tw_dev, status_reg_value);
1473 
1474         if (time_after(jiffies, before + HZ * seconds))
1475             goto out;
1476 
1477         msleep(50);
1478     }
1479     retval = 0;
1480 out:
1481     return retval;
1482 } /* End twa_poll_status() */
1483 
1484 /* This function will poll the status register for disappearance of a flag */
1485 static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1486 {
1487     u32 status_reg_value;
1488     unsigned long before;
1489     int retval = 1;
1490 
1491     status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1492     before = jiffies;
1493 
1494     if (twa_check_bits(status_reg_value))
1495         twa_decode_bits(tw_dev, status_reg_value);
1496 
1497     while ((status_reg_value & flag) != 0) {
1498         status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1499         if (twa_check_bits(status_reg_value))
1500             twa_decode_bits(tw_dev, status_reg_value);
1501 
1502         if (time_after(jiffies, before + HZ * seconds))
1503             goto out;
1504 
1505         msleep(50);
1506     }
1507     retval = 0;
1508 out:
1509     return retval;
1510 } /* End twa_poll_status_gone() */
1511 
1512 /* This function will attempt to post a command packet to the board */
1513 static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1514 {
1515     u32 status_reg_value;
1516     dma_addr_t command_que_value;
1517     int retval = 1;
1518 
1519     command_que_value = tw_dev->command_packet_phys[request_id];
1520 
1521     /* For 9650SE write low 4 bytes first */
1522     if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1523         (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1524         command_que_value += TW_COMMAND_OFFSET;
1525         writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1526     }
1527 
1528     status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1529 
1530     if (twa_check_bits(status_reg_value))
1531         twa_decode_bits(tw_dev, status_reg_value);
1532 
1533     if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1534 
1535         /* Only pend internal driver commands */
1536         if (!internal) {
1537             retval = SCSI_MLQUEUE_HOST_BUSY;
1538             goto out;
1539         }
1540 
1541         /* Couldn't post the command packet, so we do it later */
1542         if (tw_dev->state[request_id] != TW_S_PENDING) {
1543             tw_dev->state[request_id] = TW_S_PENDING;
1544             tw_dev->pending_request_count++;
1545             if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1546                 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1547             }
1548             tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1549             tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1550         }
1551         TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1552         goto out;
1553     } else {
1554         if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1555             (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1556             /* Now write upper 4 bytes */
1557             writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1558         } else {
1559             if (sizeof(dma_addr_t) > 4) {
1560                 command_que_value += TW_COMMAND_OFFSET;
1561                 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1562                 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1563             } else {
1564                 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1565             }
1566         }
1567         tw_dev->state[request_id] = TW_S_POSTED;
1568         tw_dev->posted_request_count++;
1569         if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1570             tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1571         }
1572     }
1573     retval = 0;
1574 out:
1575     return retval;
1576 } /* End twa_post_command_packet() */
1577 
1578 /* This function will reset a device extension */
1579 static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1580 {
1581     int i = 0;
1582     int retval = 1;
1583     unsigned long flags = 0;
1584 
1585     set_bit(TW_IN_RESET, &tw_dev->flags);
1586     TW_DISABLE_INTERRUPTS(tw_dev);
1587     TW_MASK_COMMAND_INTERRUPT(tw_dev);
1588     spin_lock_irqsave(tw_dev->host->host_lock, flags);
1589 
1590     /* Abort all requests that are in progress */
1591     for (i = 0; i < TW_Q_LENGTH; i++) {
1592         if ((tw_dev->state[i] != TW_S_FINISHED) &&
1593             (tw_dev->state[i] != TW_S_INITIAL) &&
1594             (tw_dev->state[i] != TW_S_COMPLETED)) {
1595             if (tw_dev->srb[i]) {
1596                 struct scsi_cmnd *cmd = tw_dev->srb[i];
1597 
1598                 cmd->result = (DID_RESET << 16);
1599                 if (twa_command_mapped(cmd))
1600                     scsi_dma_unmap(cmd);
1601                 scsi_done(cmd);
1602             }
1603         }
1604     }
1605 
1606     /* Reset queues and counts */
1607     for (i = 0; i < TW_Q_LENGTH; i++) {
1608         tw_dev->free_queue[i] = i;
1609         tw_dev->state[i] = TW_S_INITIAL;
1610     }
1611     tw_dev->free_head = TW_Q_START;
1612     tw_dev->free_tail = TW_Q_START;
1613     tw_dev->posted_request_count = 0;
1614     tw_dev->pending_request_count = 0;
1615     tw_dev->pending_head = TW_Q_START;
1616     tw_dev->pending_tail = TW_Q_START;
1617     tw_dev->reset_print = 0;
1618 
1619     spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1620 
1621     if (twa_reset_sequence(tw_dev, 1))
1622         goto out;
1623 
1624     TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1625     clear_bit(TW_IN_RESET, &tw_dev->flags);
1626     tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1627 
1628     retval = 0;
1629 out:
1630     return retval;
1631 } /* End twa_reset_device_extension() */
1632 
1633 /* This function will reset a controller */
1634 static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1635 {
1636     int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1637 
1638     while (tries < TW_MAX_RESET_TRIES) {
1639         if (do_soft_reset) {
1640             TW_SOFT_RESET(tw_dev);
1641             /* Clear pchip/response queue on 9550SX */
1642             if (twa_empty_response_queue_large(tw_dev)) {
1643                 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1644                 do_soft_reset = 1;
1645                 tries++;
1646                 continue;
1647             }
1648         }
1649 
1650         /* Make sure controller is in a good state */
1651         if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1652             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1653             do_soft_reset = 1;
1654             tries++;
1655             continue;
1656         }
1657 
1658         /* Empty response queue */
1659         if (twa_empty_response_queue(tw_dev)) {
1660             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1661             do_soft_reset = 1;
1662             tries++;
1663             continue;
1664         }
1665 
1666         flashed = 0;
1667 
1668         /* Check for compatibility/flash */
1669         if (twa_check_srl(tw_dev, &flashed)) {
1670             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1671             do_soft_reset = 1;
1672             tries++;
1673             continue;
1674         } else {
1675             if (flashed) {
1676                 tries++;
1677                 continue;
1678             }
1679         }
1680 
1681         /* Drain the AEN queue */
1682         if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1683             TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1684             do_soft_reset = 1;
1685             tries++;
1686             continue;
1687         }
1688 
1689         /* If we got here, controller is in a good state */
1690         retval = 0;
1691         goto out;
1692     }
1693 out:
1694     return retval;
1695 } /* End twa_reset_sequence() */
1696 
1697 /* This funciton returns unit geometry in cylinders/heads/sectors */
1698 static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1699 {
1700     int heads, sectors, cylinders;
1701 
1702     if (capacity >= 0x200000) {
1703         heads = 255;
1704         sectors = 63;
1705         cylinders = sector_div(capacity, heads * sectors);
1706     } else {
1707         heads = 64;
1708         sectors = 32;
1709         cylinders = sector_div(capacity, heads * sectors);
1710     }
1711 
1712     geom[0] = heads;
1713     geom[1] = sectors;
1714     geom[2] = cylinders;
1715 
1716     return 0;
1717 } /* End twa_scsi_biosparam() */
1718 
1719 /* This is the new scsi eh reset function */
1720 static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1721 {
1722     TW_Device_Extension *tw_dev = NULL;
1723     int retval = FAILED;
1724 
1725     tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1726 
1727     tw_dev->num_resets++;
1728 
1729     sdev_printk(KERN_WARNING, SCpnt->device,
1730         "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1731         TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1732 
1733     /* Make sure we are not issuing an ioctl or resetting from ioctl */
1734     mutex_lock(&tw_dev->ioctl_lock);
1735 
1736     /* Now reset the card and some of the device extension data */
1737     if (twa_reset_device_extension(tw_dev)) {
1738         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1739         goto out;
1740     }
1741 
1742     retval = SUCCESS;
1743 out:
1744     mutex_unlock(&tw_dev->ioctl_lock);
1745     return retval;
1746 } /* End twa_scsi_eh_reset() */
1747 
1748 /* This is the main scsi queue function to handle scsi opcodes */
1749 static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt)
1750 {
1751     void (*done)(struct scsi_cmnd *) = scsi_done;
1752     int request_id, retval;
1753     TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1754 
1755     /* If we are resetting due to timed out ioctl, report as busy */
1756     if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1757         retval = SCSI_MLQUEUE_HOST_BUSY;
1758         goto out;
1759     }
1760 
1761     /* Check if this FW supports luns */
1762     if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1763         SCpnt->result = (DID_BAD_TARGET << 16);
1764         done(SCpnt);
1765         retval = 0;
1766         goto out;
1767     }
1768 
1769     /* Get a free request id */
1770     twa_get_request_id(tw_dev, &request_id);
1771 
1772     /* Save the scsi command for use by the ISR */
1773     tw_dev->srb[request_id] = SCpnt;
1774 
1775     retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1776     switch (retval) {
1777     case SCSI_MLQUEUE_HOST_BUSY:
1778         if (twa_command_mapped(SCpnt))
1779             scsi_dma_unmap(SCpnt);
1780         twa_free_request_id(tw_dev, request_id);
1781         break;
1782     case 1:
1783         SCpnt->result = (DID_ERROR << 16);
1784         if (twa_command_mapped(SCpnt))
1785             scsi_dma_unmap(SCpnt);
1786         done(SCpnt);
1787         tw_dev->state[request_id] = TW_S_COMPLETED;
1788         twa_free_request_id(tw_dev, request_id);
1789         retval = 0;
1790     }
1791 out:
1792     return retval;
1793 } /* End twa_scsi_queue() */
1794 
1795 static DEF_SCSI_QCMD(twa_scsi_queue)
1796 
1797 /* This function hands scsi cdb's to the firmware */
1798 static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id,
1799                    unsigned char *cdb, int use_sg,
1800                    TW_SG_Entry *sglistarg)
1801 {
1802     TW_Command_Full *full_command_packet;
1803     TW_Command_Apache *command_packet;
1804     u32 num_sectors = 0x0;
1805     int i, sg_count;
1806     struct scsi_cmnd *srb = NULL;
1807     struct scatterlist *sg;
1808     int retval = 1;
1809 
1810     if (tw_dev->srb[request_id])
1811         srb = tw_dev->srb[request_id];
1812 
1813     /* Initialize command packet */
1814     full_command_packet = tw_dev->command_packet_virt[request_id];
1815     full_command_packet->header.header_desc.size_header = 128;
1816     full_command_packet->header.status_block.error = 0;
1817     full_command_packet->header.status_block.severity__reserved = 0;
1818 
1819     command_packet = &full_command_packet->command.newcommand;
1820     command_packet->status = 0;
1821     command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1822 
1823     /* We forced 16 byte cdb use earlier */
1824     if (!cdb)
1825         memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1826     else
1827         memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1828 
1829     if (srb) {
1830         command_packet->unit = srb->device->id;
1831         command_packet->request_id__lunl =
1832             TW_REQ_LUN_IN(srb->device->lun, request_id);
1833     } else {
1834         command_packet->request_id__lunl =
1835             TW_REQ_LUN_IN(0, request_id);
1836         command_packet->unit = 0;
1837     }
1838 
1839     command_packet->sgl_offset = 16;
1840 
1841     if (!sglistarg) {
1842         /* Map sglist from scsi layer to cmd packet */
1843 
1844         if (scsi_sg_count(srb)) {
1845             if (!twa_command_mapped(srb)) {
1846                 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1847                     srb->sc_data_direction == DMA_BIDIRECTIONAL)
1848                     scsi_sg_copy_to_buffer(srb,
1849                                    tw_dev->generic_buffer_virt[request_id],
1850                                    TW_SECTOR_SIZE);
1851                 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1852                 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1853             } else {
1854                 sg_count = scsi_dma_map(srb);
1855                 if (sg_count < 0)
1856                     goto out;
1857 
1858                 scsi_for_each_sg(srb, sg, sg_count, i) {
1859                     command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1860                     command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
1861                     if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1862                         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1863                         goto out;
1864                     }
1865                 }
1866             }
1867             command_packet->sgl_entries__lunh = TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id]));
1868         }
1869     } else {
1870         /* Internal cdb post */
1871         for (i = 0; i < use_sg; i++) {
1872             command_packet->sg_list[i].address = sglistarg[i].address;
1873             command_packet->sg_list[i].length = sglistarg[i].length;
1874             if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1875                 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1876                 goto out;
1877             }
1878         }
1879         command_packet->sgl_entries__lunh = TW_REQ_LUN_IN(0, use_sg);
1880     }
1881 
1882     if (srb) {
1883         if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1884             num_sectors = (u32)srb->cmnd[4];
1885 
1886         if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1887             num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1888     }
1889 
1890     /* Update sector statistic */
1891     tw_dev->sector_count = num_sectors;
1892     if (tw_dev->sector_count > tw_dev->max_sector_count)
1893         tw_dev->max_sector_count = tw_dev->sector_count;
1894 
1895     /* Update SG statistics */
1896     if (srb) {
1897         tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1898         if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1899             tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1900     }
1901 
1902     /* Now post the command to the board */
1903     if (srb) {
1904         retval = twa_post_command_packet(tw_dev, request_id, 0);
1905     } else {
1906         twa_post_command_packet(tw_dev, request_id, 1);
1907         retval = 0;
1908     }
1909 out:
1910     return retval;
1911 } /* End twa_scsiop_execute_scsi() */
1912 
1913 /* This function completes an execute scsi operation */
1914 static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1915 {
1916     struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1917 
1918     if (!twa_command_mapped(cmd) &&
1919         (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1920          cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
1921         if (scsi_sg_count(cmd) == 1) {
1922             void *buf = tw_dev->generic_buffer_virt[request_id];
1923 
1924             scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
1925         }
1926     }
1927 } /* End twa_scsiop_execute_scsi_complete() */
1928 
1929 /* This function tells the controller to shut down */
1930 static void __twa_shutdown(TW_Device_Extension *tw_dev)
1931 {
1932     /* Disable interrupts */
1933     TW_DISABLE_INTERRUPTS(tw_dev);
1934 
1935     /* Free up the IRQ */
1936     free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1937 
1938     printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1939 
1940     /* Tell the card we are shutting down */
1941     if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1942         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1943     } else {
1944         printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1945     }
1946 
1947     /* Clear all interrupts just before exit */
1948     TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1949 } /* End __twa_shutdown() */
1950 
1951 /* Wrapper for __twa_shutdown */
1952 static void twa_shutdown(struct pci_dev *pdev)
1953 {
1954     struct Scsi_Host *host = pci_get_drvdata(pdev);
1955     TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1956 
1957     __twa_shutdown(tw_dev);
1958 } /* End twa_shutdown() */
1959 
1960 /* This function will look up a string */
1961 static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1962 {
1963     int index;
1964 
1965     for (index = 0; ((code != table[index].code) &&
1966               (table[index].text != (char *)0)); index++);
1967     return(table[index].text);
1968 } /* End twa_string_lookup() */
1969 
1970 /* This function gets called when a disk is coming on-line */
1971 static int twa_slave_configure(struct scsi_device *sdev)
1972 {
1973     /* Force 60 second timeout */
1974     blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1975 
1976     return 0;
1977 } /* End twa_slave_configure() */
1978 
1979 /* scsi_host_template initializer */
1980 static struct scsi_host_template driver_template = {
1981     .module         = THIS_MODULE,
1982     .name           = "3ware 9000 Storage Controller",
1983     .queuecommand       = twa_scsi_queue,
1984     .eh_host_reset_handler  = twa_scsi_eh_reset,
1985     .bios_param     = twa_scsi_biosparam,
1986     .change_queue_depth = scsi_change_queue_depth,
1987     .can_queue      = TW_Q_LENGTH-2,
1988     .slave_configure    = twa_slave_configure,
1989     .this_id        = -1,
1990     .sg_tablesize       = TW_APACHE_MAX_SGL_LENGTH,
1991     .max_sectors        = TW_MAX_SECTORS,
1992     .cmd_per_lun        = TW_MAX_CMDS_PER_LUN,
1993     .shost_groups       = twa_host_groups,
1994     .emulated       = 1,
1995     .no_write_same      = 1,
1996 };
1997 
1998 /* This function will probe and initialize a card */
1999 static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2000 {
2001     struct Scsi_Host *host = NULL;
2002     TW_Device_Extension *tw_dev;
2003     unsigned long mem_addr, mem_len;
2004     int retval;
2005 
2006     retval = pci_enable_device(pdev);
2007     if (retval) {
2008         TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2009         goto out_disable_device;
2010     }
2011 
2012     pci_set_master(pdev);
2013     pci_try_set_mwi(pdev);
2014 
2015     retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2016     if (retval)
2017         retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2018     if (retval) {
2019         TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2020         retval = -ENODEV;
2021         goto out_disable_device;
2022     }
2023 
2024     host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2025     if (!host) {
2026         TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2027         retval = -ENOMEM;
2028         goto out_disable_device;
2029     }
2030     tw_dev = (TW_Device_Extension *)host->hostdata;
2031 
2032     /* Save values to device extension */
2033     tw_dev->host = host;
2034     tw_dev->tw_pci_dev = pdev;
2035 
2036     if (twa_initialize_device_extension(tw_dev)) {
2037         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2038         retval = -ENOMEM;
2039         goto out_free_device_extension;
2040     }
2041 
2042     /* Request IO regions */
2043     retval = pci_request_regions(pdev, "3w-9xxx");
2044     if (retval) {
2045         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2046         goto out_free_device_extension;
2047     }
2048 
2049     if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
2050         mem_addr = pci_resource_start(pdev, 1);
2051         mem_len = pci_resource_len(pdev, 1);
2052     } else {
2053         mem_addr = pci_resource_start(pdev, 2);
2054         mem_len = pci_resource_len(pdev, 2);
2055     }
2056 
2057     /* Save base address */
2058     tw_dev->base_addr = ioremap(mem_addr, mem_len);
2059     if (!tw_dev->base_addr) {
2060         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2061         retval = -ENOMEM;
2062         goto out_release_mem_region;
2063     }
2064 
2065     /* Disable interrupts on the card */
2066     TW_DISABLE_INTERRUPTS(tw_dev);
2067 
2068     /* Initialize the card */
2069     if (twa_reset_sequence(tw_dev, 0)) {
2070         retval = -ENOMEM;
2071         goto out_iounmap;
2072     }
2073 
2074     /* Set host specific parameters */
2075     if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2076         (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
2077         host->max_id = TW_MAX_UNITS_9650SE;
2078     else
2079         host->max_id = TW_MAX_UNITS;
2080 
2081     host->max_cmd_len = TW_MAX_CDB_LEN;
2082 
2083     /* Channels aren't supported by adapter */
2084     host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
2085     host->max_channel = 0;
2086 
2087     /* Register the card with the kernel SCSI layer */
2088     retval = scsi_add_host(host, &pdev->dev);
2089     if (retval) {
2090         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2091         goto out_iounmap;
2092     }
2093 
2094     pci_set_drvdata(pdev, host);
2095 
2096     printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2097            host->host_no, mem_addr, pdev->irq);
2098     printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2099            host->host_no,
2100            (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2101                      TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2102            (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2103                      TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2104            le32_to_cpu(*(__le32 *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2105                      TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
2106 
2107     /* Try to enable MSI */
2108     if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2109         !pci_enable_msi(pdev))
2110         set_bit(TW_USING_MSI, &tw_dev->flags);
2111 
2112     /* Now setup the interrupt handler */
2113     retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2114     if (retval) {
2115         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2116         goto out_remove_host;
2117     }
2118 
2119     twa_device_extension_list[twa_device_extension_count] = tw_dev;
2120     twa_device_extension_count++;
2121 
2122     /* Re-enable interrupts on the card */
2123     TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2124 
2125     /* Finally, scan the host */
2126     scsi_scan_host(host);
2127 
2128     if (twa_major == -1) {
2129         if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2130             TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2131     }
2132     return 0;
2133 
2134 out_remove_host:
2135     if (test_bit(TW_USING_MSI, &tw_dev->flags))
2136         pci_disable_msi(pdev);
2137     scsi_remove_host(host);
2138 out_iounmap:
2139     iounmap(tw_dev->base_addr);
2140 out_release_mem_region:
2141     pci_release_regions(pdev);
2142 out_free_device_extension:
2143     twa_free_device_extension(tw_dev);
2144     scsi_host_put(host);
2145 out_disable_device:
2146     pci_disable_device(pdev);
2147 
2148     return retval;
2149 } /* End twa_probe() */
2150 
2151 /* This function is called to remove a device */
2152 static void twa_remove(struct pci_dev *pdev)
2153 {
2154     struct Scsi_Host *host = pci_get_drvdata(pdev);
2155     TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2156 
2157     scsi_remove_host(tw_dev->host);
2158 
2159     /* Unregister character device */
2160     if (twa_major >= 0) {
2161         unregister_chrdev(twa_major, "twa");
2162         twa_major = -1;
2163     }
2164 
2165     /* Shutdown the card */
2166     __twa_shutdown(tw_dev);
2167 
2168     /* Disable MSI if enabled */
2169     if (test_bit(TW_USING_MSI, &tw_dev->flags))
2170         pci_disable_msi(pdev);
2171 
2172     /* Free IO remapping */
2173     iounmap(tw_dev->base_addr);
2174 
2175     /* Free up the mem region */
2176     pci_release_regions(pdev);
2177 
2178     /* Free up device extension resources */
2179     twa_free_device_extension(tw_dev);
2180 
2181     scsi_host_put(tw_dev->host);
2182     pci_disable_device(pdev);
2183     twa_device_extension_count--;
2184 } /* End twa_remove() */
2185 
2186 /* This function is called on PCI suspend */
2187 static int __maybe_unused twa_suspend(struct device *dev)
2188 {
2189     struct pci_dev *pdev = to_pci_dev(dev);
2190     struct Scsi_Host *host = pci_get_drvdata(pdev);
2191     TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2192 
2193     printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2194 
2195     TW_DISABLE_INTERRUPTS(tw_dev);
2196     free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2197 
2198     if (test_bit(TW_USING_MSI, &tw_dev->flags))
2199         pci_disable_msi(pdev);
2200 
2201     /* Tell the card we are shutting down */
2202     if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2203         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2204     } else {
2205         printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2206     }
2207     TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2208 
2209     return 0;
2210 } /* End twa_suspend() */
2211 
2212 /* This function is called on PCI resume */
2213 static int __maybe_unused twa_resume(struct device *dev)
2214 {
2215     int retval = 0;
2216     struct pci_dev *pdev = to_pci_dev(dev);
2217     struct Scsi_Host *host = pci_get_drvdata(pdev);
2218     TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2219 
2220     printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2221 
2222     pci_try_set_mwi(pdev);
2223 
2224     retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2225     if (retval)
2226         retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2227     if (retval) {
2228         TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2229         retval = -ENODEV;
2230         goto out_disable_device;
2231     }
2232 
2233     /* Initialize the card */
2234     if (twa_reset_sequence(tw_dev, 0)) {
2235         retval = -ENODEV;
2236         goto out_disable_device;
2237     }
2238 
2239     /* Now setup the interrupt handler */
2240     retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2241     if (retval) {
2242         TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2243         retval = -ENODEV;
2244         goto out_disable_device;
2245     }
2246 
2247     /* Now enable MSI if enabled */
2248     if (test_bit(TW_USING_MSI, &tw_dev->flags))
2249         pci_enable_msi(pdev);
2250 
2251     /* Re-enable interrupts on the card */
2252     TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2253 
2254     printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2255     return 0;
2256 
2257 out_disable_device:
2258     scsi_remove_host(host);
2259 
2260     return retval;
2261 } /* End twa_resume() */
2262 
2263 /* PCI Devices supported by this driver */
2264 static struct pci_device_id twa_pci_tbl[] = {
2265     { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2266       PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2267     { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2268       PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2269     { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2270       PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2271     { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2272       PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2273     { }
2274 };
2275 MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2276 
2277 static SIMPLE_DEV_PM_OPS(twa_pm_ops, twa_suspend, twa_resume);
2278 
2279 /* pci_driver initializer */
2280 static struct pci_driver twa_driver = {
2281     .name       = "3w-9xxx",
2282     .id_table   = twa_pci_tbl,
2283     .probe      = twa_probe,
2284     .remove     = twa_remove,
2285     .driver.pm  = &twa_pm_ops,
2286     .shutdown   = twa_shutdown
2287 };
2288 
2289 /* This function is called on driver initialization */
2290 static int __init twa_init(void)
2291 {
2292     printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2293 
2294     return pci_register_driver(&twa_driver);
2295 } /* End twa_init() */
2296 
2297 /* This function is called on driver exit */
2298 static void __exit twa_exit(void)
2299 {
2300     pci_unregister_driver(&twa_driver);
2301 } /* End twa_exit() */
2302 
2303 module_init(twa_init);
2304 module_exit(twa_exit);
2305