Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild-2.6
[pandora-kernel.git] / drivers / scsi / aacraid / aachba.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc.
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2010 Adaptec, Inc.
9  *               2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/pci.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
33 #include <linux/completion.h>
34 #include <linux/blkdev.h>
35 #include <asm/uaccess.h>
36 #include <linux/highmem.h> /* For flush_kernel_dcache_page */
37
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_host.h>
42
43 #include "aacraid.h"
44
45 /* values for inqd_pdt: Peripheral device type in plain English */
46 #define INQD_PDT_DA     0x00    /* Direct-access (DISK) device */
47 #define INQD_PDT_PROC   0x03    /* Processor device */
48 #define INQD_PDT_CHNGR  0x08    /* Changer (jukebox, scsi2) */
49 #define INQD_PDT_COMM   0x09    /* Communication device (scsi2) */
50 #define INQD_PDT_NOLUN2 0x1f    /* Unknown Device (scsi2) */
51 #define INQD_PDT_NOLUN  0x7f    /* Logical Unit Not Present */
52
53 #define INQD_PDT_DMASK  0x1F    /* Peripheral Device Type Mask */
54 #define INQD_PDT_QMASK  0xE0    /* Peripheral Device Qualifer Mask */
55
56 /*
57  *      Sense codes
58  */
59
60 #define SENCODE_NO_SENSE                        0x00
61 #define SENCODE_END_OF_DATA                     0x00
62 #define SENCODE_BECOMING_READY                  0x04
63 #define SENCODE_INIT_CMD_REQUIRED               0x04
64 #define SENCODE_PARAM_LIST_LENGTH_ERROR         0x1A
65 #define SENCODE_INVALID_COMMAND                 0x20
66 #define SENCODE_LBA_OUT_OF_RANGE                0x21
67 #define SENCODE_INVALID_CDB_FIELD               0x24
68 #define SENCODE_LUN_NOT_SUPPORTED               0x25
69 #define SENCODE_INVALID_PARAM_FIELD             0x26
70 #define SENCODE_PARAM_NOT_SUPPORTED             0x26
71 #define SENCODE_PARAM_VALUE_INVALID             0x26
72 #define SENCODE_RESET_OCCURRED                  0x29
73 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET     0x3E
74 #define SENCODE_INQUIRY_DATA_CHANGED            0x3F
75 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED     0x39
76 #define SENCODE_DIAGNOSTIC_FAILURE              0x40
77 #define SENCODE_INTERNAL_TARGET_FAILURE         0x44
78 #define SENCODE_INVALID_MESSAGE_ERROR           0x49
79 #define SENCODE_LUN_FAILED_SELF_CONFIG          0x4c
80 #define SENCODE_OVERLAPPED_COMMAND              0x4E
81
82 /*
83  *      Additional sense codes
84  */
85
86 #define ASENCODE_NO_SENSE                       0x00
87 #define ASENCODE_END_OF_DATA                    0x05
88 #define ASENCODE_BECOMING_READY                 0x01
89 #define ASENCODE_INIT_CMD_REQUIRED              0x02
90 #define ASENCODE_PARAM_LIST_LENGTH_ERROR        0x00
91 #define ASENCODE_INVALID_COMMAND                0x00
92 #define ASENCODE_LBA_OUT_OF_RANGE               0x00
93 #define ASENCODE_INVALID_CDB_FIELD              0x00
94 #define ASENCODE_LUN_NOT_SUPPORTED              0x00
95 #define ASENCODE_INVALID_PARAM_FIELD            0x00
96 #define ASENCODE_PARAM_NOT_SUPPORTED            0x01
97 #define ASENCODE_PARAM_VALUE_INVALID            0x02
98 #define ASENCODE_RESET_OCCURRED                 0x00
99 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET    0x00
100 #define ASENCODE_INQUIRY_DATA_CHANGED           0x03
101 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED    0x00
102 #define ASENCODE_DIAGNOSTIC_FAILURE             0x80
103 #define ASENCODE_INTERNAL_TARGET_FAILURE        0x00
104 #define ASENCODE_INVALID_MESSAGE_ERROR          0x00
105 #define ASENCODE_LUN_FAILED_SELF_CONFIG         0x00
106 #define ASENCODE_OVERLAPPED_COMMAND             0x00
107
108 #define BYTE0(x) (unsigned char)(x)
109 #define BYTE1(x) (unsigned char)((x) >> 8)
110 #define BYTE2(x) (unsigned char)((x) >> 16)
111 #define BYTE3(x) (unsigned char)((x) >> 24)
112
113 /*------------------------------------------------------------------------------
114  *              S T R U C T S / T Y P E D E F S
115  *----------------------------------------------------------------------------*/
116 /* SCSI inquiry data */
117 struct inquiry_data {
118         u8 inqd_pdt;    /* Peripheral qualifier | Peripheral Device Type */
119         u8 inqd_dtq;    /* RMB | Device Type Qualifier */
120         u8 inqd_ver;    /* ISO version | ECMA version | ANSI-approved version */
121         u8 inqd_rdf;    /* AENC | TrmIOP | Response data format */
122         u8 inqd_len;    /* Additional length (n-4) */
123         u8 inqd_pad1[2];/* Reserved - must be zero */
124         u8 inqd_pad2;   /* RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
125         u8 inqd_vid[8]; /* Vendor ID */
126         u8 inqd_pid[16];/* Product ID */
127         u8 inqd_prl[4]; /* Product Revision Level */
128 };
129
130 /*
131  *              M O D U L E   G L O B A L S
132  */
133
134 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
135 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
136 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
137 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
138 #ifdef AAC_DETAILED_STATUS_INFO
139 static char *aac_get_status_string(u32 status);
140 #endif
141
142 /*
143  *      Non dasd selection is handled entirely in aachba now
144  */
145
146 static int nondasd = -1;
147 static int aac_cache = 2;       /* WCE=0 to avoid performance problems */
148 static int dacmode = -1;
149 int aac_msi;
150 int aac_commit = -1;
151 int startup_timeout = 180;
152 int aif_timeout = 120;
153
154 module_param(nondasd, int, S_IRUGO|S_IWUSR);
155 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
156         " 0=off, 1=on");
157 module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
158 MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
159         "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
160         "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
161         "\tbit 2 - Disable only if Battery is protecting Cache");
162 module_param(dacmode, int, S_IRUGO|S_IWUSR);
163 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
164         " 0=off, 1=on");
165 module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
166 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
167         " adapter for foreign arrays.\n"
168         "This is typically needed in systems that do not have a BIOS."
169         " 0=off, 1=on");
170 module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
171 MODULE_PARM_DESC(msi, "IRQ handling."
172         " 0=PIC(default), 1=MSI, 2=MSI-X(unsupported, uses MSI)");
173 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
174 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
175         " adapter to have it's kernel up and\n"
176         "running. This is typically adjusted for large systems that do not"
177         " have a BIOS.");
178 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
179 MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
180         " applications to pick up AIFs before\n"
181         "deregistering them. This is typically adjusted for heavily burdened"
182         " systems.");
183
184 int numacb = -1;
185 module_param(numacb, int, S_IRUGO|S_IWUSR);
186 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
187         " blocks (FIB) allocated. Valid values are 512 and down. Default is"
188         " to use suggestion from Firmware.");
189
190 int acbsize = -1;
191 module_param(acbsize, int, S_IRUGO|S_IWUSR);
192 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
193         " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
194         " suggestion from Firmware.");
195
196 int update_interval = 30 * 60;
197 module_param(update_interval, int, S_IRUGO|S_IWUSR);
198 MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
199         " updates issued to adapter.");
200
201 int check_interval = 24 * 60 * 60;
202 module_param(check_interval, int, S_IRUGO|S_IWUSR);
203 MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
204         " checks.");
205
206 int aac_check_reset = 1;
207 module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
208 MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
209         " adapter. a value of -1 forces the reset to adapters programmed to"
210         " ignore it.");
211
212 int expose_physicals = -1;
213 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
214 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
215         " -1=protect 0=off, 1=on");
216
217 int aac_reset_devices;
218 module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
219 MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
220
221 int aac_wwn = 1;
222 module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
223 MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
224         "\t0 - Disable\n"
225         "\t1 - Array Meta Data Signature (default)\n"
226         "\t2 - Adapter Serial Number");
227
228
229 static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
230                 struct fib *fibptr) {
231         struct scsi_device *device;
232
233         if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
234                 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
235                 aac_fib_complete(fibptr);
236                 aac_fib_free(fibptr);
237                 return 0;
238         }
239         scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
240         device = scsicmd->device;
241         if (unlikely(!device || !scsi_device_online(device))) {
242                 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
243                 aac_fib_complete(fibptr);
244                 aac_fib_free(fibptr);
245                 return 0;
246         }
247         return 1;
248 }
249
250 /**
251  *      aac_get_config_status   -       check the adapter configuration
252  *      @common: adapter to query
253  *
254  *      Query config status, and commit the configuration if needed.
255  */
256 int aac_get_config_status(struct aac_dev *dev, int commit_flag)
257 {
258         int status = 0;
259         struct fib * fibptr;
260
261         if (!(fibptr = aac_fib_alloc(dev)))
262                 return -ENOMEM;
263
264         aac_fib_init(fibptr);
265         {
266                 struct aac_get_config_status *dinfo;
267                 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
268
269                 dinfo->command = cpu_to_le32(VM_ContainerConfig);
270                 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
271                 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
272         }
273
274         status = aac_fib_send(ContainerCommand,
275                             fibptr,
276                             sizeof (struct aac_get_config_status),
277                             FsaNormal,
278                             1, 1,
279                             NULL, NULL);
280         if (status < 0) {
281                 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
282         } else {
283                 struct aac_get_config_status_resp *reply
284                   = (struct aac_get_config_status_resp *) fib_data(fibptr);
285                 dprintk((KERN_WARNING
286                   "aac_get_config_status: response=%d status=%d action=%d\n",
287                   le32_to_cpu(reply->response),
288                   le32_to_cpu(reply->status),
289                   le32_to_cpu(reply->data.action)));
290                 if ((le32_to_cpu(reply->response) != ST_OK) ||
291                      (le32_to_cpu(reply->status) != CT_OK) ||
292                      (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
293                         printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
294                         status = -EINVAL;
295                 }
296         }
297         /* Do not set XferState to zero unless receives a response from F/W */
298         if (status >= 0)
299                 aac_fib_complete(fibptr);
300
301         /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
302         if (status >= 0) {
303                 if ((aac_commit == 1) || commit_flag) {
304                         struct aac_commit_config * dinfo;
305                         aac_fib_init(fibptr);
306                         dinfo = (struct aac_commit_config *) fib_data(fibptr);
307
308                         dinfo->command = cpu_to_le32(VM_ContainerConfig);
309                         dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
310
311                         status = aac_fib_send(ContainerCommand,
312                                     fibptr,
313                                     sizeof (struct aac_commit_config),
314                                     FsaNormal,
315                                     1, 1,
316                                     NULL, NULL);
317                         /* Do not set XferState to zero unless
318                          * receives a response from F/W */
319                         if (status >= 0)
320                                 aac_fib_complete(fibptr);
321                 } else if (aac_commit == 0) {
322                         printk(KERN_WARNING
323                           "aac_get_config_status: Foreign device configurations are being ignored\n");
324                 }
325         }
326         /* FIB should be freed only after getting the response from the F/W */
327         if (status != -ERESTARTSYS)
328                 aac_fib_free(fibptr);
329         return status;
330 }
331
332 static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
333 {
334         char inq_data;
335         scsi_sg_copy_to_buffer(scsicmd,  &inq_data, sizeof(inq_data));
336         if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
337                 inq_data &= 0xdf;
338                 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
339         }
340 }
341
342 /**
343  *      aac_get_containers      -       list containers
344  *      @common: adapter to probe
345  *
346  *      Make a list of all containers on this controller
347  */
348 int aac_get_containers(struct aac_dev *dev)
349 {
350         struct fsa_dev_info *fsa_dev_ptr;
351         u32 index;
352         int status = 0;
353         struct fib * fibptr;
354         struct aac_get_container_count *dinfo;
355         struct aac_get_container_count_resp *dresp;
356         int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
357
358         if (!(fibptr = aac_fib_alloc(dev)))
359                 return -ENOMEM;
360
361         aac_fib_init(fibptr);
362         dinfo = (struct aac_get_container_count *) fib_data(fibptr);
363         dinfo->command = cpu_to_le32(VM_ContainerConfig);
364         dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
365
366         status = aac_fib_send(ContainerCommand,
367                     fibptr,
368                     sizeof (struct aac_get_container_count),
369                     FsaNormal,
370                     1, 1,
371                     NULL, NULL);
372         if (status >= 0) {
373                 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
374                 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
375                 aac_fib_complete(fibptr);
376         }
377         /* FIB should be freed only after getting the response from the F/W */
378         if (status != -ERESTARTSYS)
379                 aac_fib_free(fibptr);
380
381         if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
382                 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
383         fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
384                         GFP_KERNEL);
385         if (!fsa_dev_ptr)
386                 return -ENOMEM;
387
388         dev->fsa_dev = fsa_dev_ptr;
389         dev->maximum_num_containers = maximum_num_containers;
390
391         for (index = 0; index < dev->maximum_num_containers; ) {
392                 fsa_dev_ptr[index].devname[0] = '\0';
393
394                 status = aac_probe_container(dev, index);
395
396                 if (status < 0) {
397                         printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
398                         break;
399                 }
400
401                 /*
402                  *      If there are no more containers, then stop asking.
403                  */
404                 if (++index >= status)
405                         break;
406         }
407         return status;
408 }
409
410 static void get_container_name_callback(void *context, struct fib * fibptr)
411 {
412         struct aac_get_name_resp * get_name_reply;
413         struct scsi_cmnd * scsicmd;
414
415         scsicmd = (struct scsi_cmnd *) context;
416
417         if (!aac_valid_context(scsicmd, fibptr))
418                 return;
419
420         dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
421         BUG_ON(fibptr == NULL);
422
423         get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
424         /* Failure is irrelevant, using default value instead */
425         if ((le32_to_cpu(get_name_reply->status) == CT_OK)
426          && (get_name_reply->data[0] != '\0')) {
427                 char *sp = get_name_reply->data;
428                 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
429                 while (*sp == ' ')
430                         ++sp;
431                 if (*sp) {
432                         struct inquiry_data inq;
433                         char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
434                         int count = sizeof(d);
435                         char *dp = d;
436                         do {
437                                 *dp++ = (*sp) ? *sp++ : ' ';
438                         } while (--count > 0);
439
440                         scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
441                         memcpy(inq.inqd_pid, d, sizeof(d));
442                         scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
443                 }
444         }
445
446         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
447
448         aac_fib_complete(fibptr);
449         aac_fib_free(fibptr);
450         scsicmd->scsi_done(scsicmd);
451 }
452
453 /**
454  *      aac_get_container_name  -       get container name, none blocking.
455  */
456 static int aac_get_container_name(struct scsi_cmnd * scsicmd)
457 {
458         int status;
459         struct aac_get_name *dinfo;
460         struct fib * cmd_fibcontext;
461         struct aac_dev * dev;
462
463         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
464
465         if (!(cmd_fibcontext = aac_fib_alloc(dev)))
466                 return -ENOMEM;
467
468         aac_fib_init(cmd_fibcontext);
469         dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
470
471         dinfo->command = cpu_to_le32(VM_ContainerConfig);
472         dinfo->type = cpu_to_le32(CT_READ_NAME);
473         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
474         dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
475
476         status = aac_fib_send(ContainerCommand,
477                   cmd_fibcontext,
478                   sizeof (struct aac_get_name),
479                   FsaNormal,
480                   0, 1,
481                   (fib_callback)get_container_name_callback,
482                   (void *) scsicmd);
483
484         /*
485          *      Check that the command queued to the controller
486          */
487         if (status == -EINPROGRESS) {
488                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
489                 return 0;
490         }
491
492         printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
493         aac_fib_complete(cmd_fibcontext);
494         aac_fib_free(cmd_fibcontext);
495         return -1;
496 }
497
498 static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
499 {
500         struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
501
502         if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
503                 return aac_scsi_cmd(scsicmd);
504
505         scsicmd->result = DID_NO_CONNECT << 16;
506         scsicmd->scsi_done(scsicmd);
507         return 0;
508 }
509
510 static void _aac_probe_container2(void * context, struct fib * fibptr)
511 {
512         struct fsa_dev_info *fsa_dev_ptr;
513         int (*callback)(struct scsi_cmnd *);
514         struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
515
516
517         if (!aac_valid_context(scsicmd, fibptr))
518                 return;
519
520         scsicmd->SCp.Status = 0;
521         fsa_dev_ptr = fibptr->dev->fsa_dev;
522         if (fsa_dev_ptr) {
523                 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
524                 fsa_dev_ptr += scmd_id(scsicmd);
525
526                 if ((le32_to_cpu(dresp->status) == ST_OK) &&
527                     (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
528                     (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
529                         fsa_dev_ptr->valid = 1;
530                         /* sense_key holds the current state of the spin-up */
531                         if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
532                                 fsa_dev_ptr->sense_data.sense_key = NOT_READY;
533                         else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
534                                 fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
535                         fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
536                         fsa_dev_ptr->size
537                           = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
538                             (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
539                         fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
540                 }
541                 if ((fsa_dev_ptr->valid & 1) == 0)
542                         fsa_dev_ptr->valid = 0;
543                 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
544         }
545         aac_fib_complete(fibptr);
546         aac_fib_free(fibptr);
547         callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
548         scsicmd->SCp.ptr = NULL;
549         (*callback)(scsicmd);
550         return;
551 }
552
553 static void _aac_probe_container1(void * context, struct fib * fibptr)
554 {
555         struct scsi_cmnd * scsicmd;
556         struct aac_mount * dresp;
557         struct aac_query_mount *dinfo;
558         int status;
559
560         dresp = (struct aac_mount *) fib_data(fibptr);
561         dresp->mnt[0].capacityhigh = 0;
562         if ((le32_to_cpu(dresp->status) != ST_OK) ||
563             (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
564                 _aac_probe_container2(context, fibptr);
565                 return;
566         }
567         scsicmd = (struct scsi_cmnd *) context;
568
569         if (!aac_valid_context(scsicmd, fibptr))
570                 return;
571
572         aac_fib_init(fibptr);
573
574         dinfo = (struct aac_query_mount *)fib_data(fibptr);
575
576         dinfo->command = cpu_to_le32(VM_NameServe64);
577         dinfo->count = cpu_to_le32(scmd_id(scsicmd));
578         dinfo->type = cpu_to_le32(FT_FILESYS);
579
580         status = aac_fib_send(ContainerCommand,
581                           fibptr,
582                           sizeof(struct aac_query_mount),
583                           FsaNormal,
584                           0, 1,
585                           _aac_probe_container2,
586                           (void *) scsicmd);
587         /*
588          *      Check that the command queued to the controller
589          */
590         if (status == -EINPROGRESS)
591                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
592         else if (status < 0) {
593                 /* Inherit results from VM_NameServe, if any */
594                 dresp->status = cpu_to_le32(ST_OK);
595                 _aac_probe_container2(context, fibptr);
596         }
597 }
598
599 static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
600 {
601         struct fib * fibptr;
602         int status = -ENOMEM;
603
604         if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
605                 struct aac_query_mount *dinfo;
606
607                 aac_fib_init(fibptr);
608
609                 dinfo = (struct aac_query_mount *)fib_data(fibptr);
610
611                 dinfo->command = cpu_to_le32(VM_NameServe);
612                 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
613                 dinfo->type = cpu_to_le32(FT_FILESYS);
614                 scsicmd->SCp.ptr = (char *)callback;
615
616                 status = aac_fib_send(ContainerCommand,
617                           fibptr,
618                           sizeof(struct aac_query_mount),
619                           FsaNormal,
620                           0, 1,
621                           _aac_probe_container1,
622                           (void *) scsicmd);
623                 /*
624                  *      Check that the command queued to the controller
625                  */
626                 if (status == -EINPROGRESS) {
627                         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
628                         return 0;
629                 }
630                 if (status < 0) {
631                         scsicmd->SCp.ptr = NULL;
632                         aac_fib_complete(fibptr);
633                         aac_fib_free(fibptr);
634                 }
635         }
636         if (status < 0) {
637                 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
638                 if (fsa_dev_ptr) {
639                         fsa_dev_ptr += scmd_id(scsicmd);
640                         if ((fsa_dev_ptr->valid & 1) == 0) {
641                                 fsa_dev_ptr->valid = 0;
642                                 return (*callback)(scsicmd);
643                         }
644                 }
645         }
646         return status;
647 }
648
649 /**
650  *      aac_probe_container             -       query a logical volume
651  *      @dev: device to query
652  *      @cid: container identifier
653  *
654  *      Queries the controller about the given volume. The volume information
655  *      is updated in the struct fsa_dev_info structure rather than returned.
656  */
657 static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
658 {
659         scsicmd->device = NULL;
660         return 0;
661 }
662
663 int aac_probe_container(struct aac_dev *dev, int cid)
664 {
665         struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
666         struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
667         int status;
668
669         if (!scsicmd || !scsidev) {
670                 kfree(scsicmd);
671                 kfree(scsidev);
672                 return -ENOMEM;
673         }
674         scsicmd->list.next = NULL;
675         scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
676
677         scsicmd->device = scsidev;
678         scsidev->sdev_state = 0;
679         scsidev->id = cid;
680         scsidev->host = dev->scsi_host_ptr;
681
682         if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
683                 while (scsicmd->device == scsidev)
684                         schedule();
685         kfree(scsidev);
686         status = scsicmd->SCp.Status;
687         kfree(scsicmd);
688         return status;
689 }
690
691 /* Local Structure to set SCSI inquiry data strings */
692 struct scsi_inq {
693         char vid[8];         /* Vendor ID */
694         char pid[16];        /* Product ID */
695         char prl[4];         /* Product Revision Level */
696 };
697
698 /**
699  *      InqStrCopy      -       string merge
700  *      @a:     string to copy from
701  *      @b:     string to copy to
702  *
703  *      Copy a String from one location to another
704  *      without copying \0
705  */
706
707 static void inqstrcpy(char *a, char *b)
708 {
709
710         while (*a != (char)0)
711                 *b++ = *a++;
712 }
713
714 static char *container_types[] = {
715         "None",
716         "Volume",
717         "Mirror",
718         "Stripe",
719         "RAID5",
720         "SSRW",
721         "SSRO",
722         "Morph",
723         "Legacy",
724         "RAID4",
725         "RAID10",
726         "RAID00",
727         "V-MIRRORS",
728         "PSEUDO R4",
729         "RAID50",
730         "RAID5D",
731         "RAID5D0",
732         "RAID1E",
733         "RAID6",
734         "RAID60",
735         "Unknown"
736 };
737
738 char * get_container_type(unsigned tindex)
739 {
740         if (tindex >= ARRAY_SIZE(container_types))
741                 tindex = ARRAY_SIZE(container_types) - 1;
742         return container_types[tindex];
743 }
744
745 /* Function: setinqstr
746  *
747  * Arguments: [1] pointer to void [1] int
748  *
749  * Purpose: Sets SCSI inquiry data strings for vendor, product
750  * and revision level. Allows strings to be set in platform dependent
751  * files instead of in OS dependent driver source.
752  */
753
754 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
755 {
756         struct scsi_inq *str;
757
758         str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
759         memset(str, ' ', sizeof(*str));
760
761         if (dev->supplement_adapter_info.AdapterTypeText[0]) {
762                 char * cp = dev->supplement_adapter_info.AdapterTypeText;
763                 int c;
764                 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
765                         inqstrcpy("SMC", str->vid);
766                 else {
767                         c = sizeof(str->vid);
768                         while (*cp && *cp != ' ' && --c)
769                                 ++cp;
770                         c = *cp;
771                         *cp = '\0';
772                         inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
773                                    str->vid);
774                         *cp = c;
775                         while (*cp && *cp != ' ')
776                                 ++cp;
777                 }
778                 while (*cp == ' ')
779                         ++cp;
780                 /* last six chars reserved for vol type */
781                 c = 0;
782                 if (strlen(cp) > sizeof(str->pid)) {
783                         c = cp[sizeof(str->pid)];
784                         cp[sizeof(str->pid)] = '\0';
785                 }
786                 inqstrcpy (cp, str->pid);
787                 if (c)
788                         cp[sizeof(str->pid)] = c;
789         } else {
790                 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
791
792                 inqstrcpy (mp->vname, str->vid);
793                 /* last six chars reserved for vol type */
794                 inqstrcpy (mp->model, str->pid);
795         }
796
797         if (tindex < ARRAY_SIZE(container_types)){
798                 char *findit = str->pid;
799
800                 for ( ; *findit != ' '; findit++); /* walk till we find a space */
801                 /* RAID is superfluous in the context of a RAID device */
802                 if (memcmp(findit-4, "RAID", 4) == 0)
803                         *(findit -= 4) = ' ';
804                 if (((findit - str->pid) + strlen(container_types[tindex]))
805                  < (sizeof(str->pid) + sizeof(str->prl)))
806                         inqstrcpy (container_types[tindex], findit + 1);
807         }
808         inqstrcpy ("V1.0", str->prl);
809 }
810
811 static void get_container_serial_callback(void *context, struct fib * fibptr)
812 {
813         struct aac_get_serial_resp * get_serial_reply;
814         struct scsi_cmnd * scsicmd;
815
816         BUG_ON(fibptr == NULL);
817
818         scsicmd = (struct scsi_cmnd *) context;
819         if (!aac_valid_context(scsicmd, fibptr))
820                 return;
821
822         get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
823         /* Failure is irrelevant, using default value instead */
824         if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
825                 char sp[13];
826                 /* EVPD bit set */
827                 sp[0] = INQD_PDT_DA;
828                 sp[1] = scsicmd->cmnd[2];
829                 sp[2] = 0;
830                 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
831                   le32_to_cpu(get_serial_reply->uid));
832                 scsi_sg_copy_from_buffer(scsicmd, sp, sizeof(sp));
833         }
834
835         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
836
837         aac_fib_complete(fibptr);
838         aac_fib_free(fibptr);
839         scsicmd->scsi_done(scsicmd);
840 }
841
842 /**
843  *      aac_get_container_serial - get container serial, none blocking.
844  */
845 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
846 {
847         int status;
848         struct aac_get_serial *dinfo;
849         struct fib * cmd_fibcontext;
850         struct aac_dev * dev;
851
852         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
853
854         if (!(cmd_fibcontext = aac_fib_alloc(dev)))
855                 return -ENOMEM;
856
857         aac_fib_init(cmd_fibcontext);
858         dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
859
860         dinfo->command = cpu_to_le32(VM_ContainerConfig);
861         dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
862         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
863
864         status = aac_fib_send(ContainerCommand,
865                   cmd_fibcontext,
866                   sizeof (struct aac_get_serial),
867                   FsaNormal,
868                   0, 1,
869                   (fib_callback) get_container_serial_callback,
870                   (void *) scsicmd);
871
872         /*
873          *      Check that the command queued to the controller
874          */
875         if (status == -EINPROGRESS) {
876                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
877                 return 0;
878         }
879
880         printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
881         aac_fib_complete(cmd_fibcontext);
882         aac_fib_free(cmd_fibcontext);
883         return -1;
884 }
885
886 /* Function: setinqserial
887  *
888  * Arguments: [1] pointer to void [1] int
889  *
890  * Purpose: Sets SCSI Unit Serial number.
891  *          This is a fake. We should read a proper
892  *          serial number from the container. <SuSE>But
893  *          without docs it's quite hard to do it :-)
894  *          So this will have to do in the meantime.</SuSE>
895  */
896
897 static int setinqserial(struct aac_dev *dev, void *data, int cid)
898 {
899         /*
900          *      This breaks array migration.
901          */
902         return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
903                         le32_to_cpu(dev->adapter_info.serial[0]), cid);
904 }
905
906 static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
907         u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
908 {
909         u8 *sense_buf = (u8 *)sense_data;
910         /* Sense data valid, err code 70h */
911         sense_buf[0] = 0x70; /* No info field */
912         sense_buf[1] = 0;       /* Segment number, always zero */
913
914         sense_buf[2] = sense_key;       /* Sense key */
915
916         sense_buf[12] = sense_code;     /* Additional sense code */
917         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
918
919         if (sense_key == ILLEGAL_REQUEST) {
920                 sense_buf[7] = 10;      /* Additional sense length */
921
922                 sense_buf[15] = bit_pointer;
923                 /* Illegal parameter is in the parameter block */
924                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
925                         sense_buf[15] |= 0xc0;/* Std sense key specific field */
926                 /* Illegal parameter is in the CDB block */
927                 sense_buf[16] = field_pointer >> 8;     /* MSB */
928                 sense_buf[17] = field_pointer;          /* LSB */
929         } else
930                 sense_buf[7] = 6;       /* Additional sense length */
931 }
932
933 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
934 {
935         if (lba & 0xffffffff00000000LL) {
936                 int cid = scmd_id(cmd);
937                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
938                 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
939                         SAM_STAT_CHECK_CONDITION;
940                 set_sense(&dev->fsa_dev[cid].sense_data,
941                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
942                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
943                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
944                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
945                              SCSI_SENSE_BUFFERSIZE));
946                 cmd->scsi_done(cmd);
947                 return 1;
948         }
949         return 0;
950 }
951
952 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
953 {
954         return 0;
955 }
956
957 static void io_callback(void *context, struct fib * fibptr);
958
959 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
960 {
961         u16 fibsize;
962         struct aac_raw_io *readcmd;
963         aac_fib_init(fib);
964         readcmd = (struct aac_raw_io *) fib_data(fib);
965         readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
966         readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
967         readcmd->count = cpu_to_le32(count<<9);
968         readcmd->cid = cpu_to_le16(scmd_id(cmd));
969         readcmd->flags = cpu_to_le16(IO_TYPE_READ);
970         readcmd->bpTotal = 0;
971         readcmd->bpComplete = 0;
972
973         aac_build_sgraw(cmd, &readcmd->sg);
974         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
975         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
976         /*
977          *      Now send the Fib to the adapter
978          */
979         return aac_fib_send(ContainerRawIo,
980                           fib,
981                           fibsize,
982                           FsaNormal,
983                           0, 1,
984                           (fib_callback) io_callback,
985                           (void *) cmd);
986 }
987
988 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
989 {
990         u16 fibsize;
991         struct aac_read64 *readcmd;
992         aac_fib_init(fib);
993         readcmd = (struct aac_read64 *) fib_data(fib);
994         readcmd->command = cpu_to_le32(VM_CtHostRead64);
995         readcmd->cid = cpu_to_le16(scmd_id(cmd));
996         readcmd->sector_count = cpu_to_le16(count);
997         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
998         readcmd->pad   = 0;
999         readcmd->flags = 0;
1000
1001         aac_build_sg64(cmd, &readcmd->sg);
1002         fibsize = sizeof(struct aac_read64) +
1003                 ((le32_to_cpu(readcmd->sg.count) - 1) *
1004                  sizeof (struct sgentry64));
1005         BUG_ON (fibsize > (fib->dev->max_fib_size -
1006                                 sizeof(struct aac_fibhdr)));
1007         /*
1008          *      Now send the Fib to the adapter
1009          */
1010         return aac_fib_send(ContainerCommand64,
1011                           fib,
1012                           fibsize,
1013                           FsaNormal,
1014                           0, 1,
1015                           (fib_callback) io_callback,
1016                           (void *) cmd);
1017 }
1018
1019 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1020 {
1021         u16 fibsize;
1022         struct aac_read *readcmd;
1023         aac_fib_init(fib);
1024         readcmd = (struct aac_read *) fib_data(fib);
1025         readcmd->command = cpu_to_le32(VM_CtBlockRead);
1026         readcmd->cid = cpu_to_le32(scmd_id(cmd));
1027         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1028         readcmd->count = cpu_to_le32(count * 512);
1029
1030         aac_build_sg(cmd, &readcmd->sg);
1031         fibsize = sizeof(struct aac_read) +
1032                         ((le32_to_cpu(readcmd->sg.count) - 1) *
1033                          sizeof (struct sgentry));
1034         BUG_ON (fibsize > (fib->dev->max_fib_size -
1035                                 sizeof(struct aac_fibhdr)));
1036         /*
1037          *      Now send the Fib to the adapter
1038          */
1039         return aac_fib_send(ContainerCommand,
1040                           fib,
1041                           fibsize,
1042                           FsaNormal,
1043                           0, 1,
1044                           (fib_callback) io_callback,
1045                           (void *) cmd);
1046 }
1047
1048 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1049 {
1050         u16 fibsize;
1051         struct aac_raw_io *writecmd;
1052         aac_fib_init(fib);
1053         writecmd = (struct aac_raw_io *) fib_data(fib);
1054         writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1055         writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1056         writecmd->count = cpu_to_le32(count<<9);
1057         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1058         writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1059           (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1060                 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
1061                 cpu_to_le16(IO_TYPE_WRITE);
1062         writecmd->bpTotal = 0;
1063         writecmd->bpComplete = 0;
1064
1065         aac_build_sgraw(cmd, &writecmd->sg);
1066         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1067         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1068         /*
1069          *      Now send the Fib to the adapter
1070          */
1071         return aac_fib_send(ContainerRawIo,
1072                           fib,
1073                           fibsize,
1074                           FsaNormal,
1075                           0, 1,
1076                           (fib_callback) io_callback,
1077                           (void *) cmd);
1078 }
1079
1080 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1081 {
1082         u16 fibsize;
1083         struct aac_write64 *writecmd;
1084         aac_fib_init(fib);
1085         writecmd = (struct aac_write64 *) fib_data(fib);
1086         writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1087         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1088         writecmd->sector_count = cpu_to_le16(count);
1089         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1090         writecmd->pad   = 0;
1091         writecmd->flags = 0;
1092
1093         aac_build_sg64(cmd, &writecmd->sg);
1094         fibsize = sizeof(struct aac_write64) +
1095                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1096                  sizeof (struct sgentry64));
1097         BUG_ON (fibsize > (fib->dev->max_fib_size -
1098                                 sizeof(struct aac_fibhdr)));
1099         /*
1100          *      Now send the Fib to the adapter
1101          */
1102         return aac_fib_send(ContainerCommand64,
1103                           fib,
1104                           fibsize,
1105                           FsaNormal,
1106                           0, 1,
1107                           (fib_callback) io_callback,
1108                           (void *) cmd);
1109 }
1110
1111 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1112 {
1113         u16 fibsize;
1114         struct aac_write *writecmd;
1115         aac_fib_init(fib);
1116         writecmd = (struct aac_write *) fib_data(fib);
1117         writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1118         writecmd->cid = cpu_to_le32(scmd_id(cmd));
1119         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1120         writecmd->count = cpu_to_le32(count * 512);
1121         writecmd->sg.count = cpu_to_le32(1);
1122         /* ->stable is not used - it did mean which type of write */
1123
1124         aac_build_sg(cmd, &writecmd->sg);
1125         fibsize = sizeof(struct aac_write) +
1126                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1127                  sizeof (struct sgentry));
1128         BUG_ON (fibsize > (fib->dev->max_fib_size -
1129                                 sizeof(struct aac_fibhdr)));
1130         /*
1131          *      Now send the Fib to the adapter
1132          */
1133         return aac_fib_send(ContainerCommand,
1134                           fib,
1135                           fibsize,
1136                           FsaNormal,
1137                           0, 1,
1138                           (fib_callback) io_callback,
1139                           (void *) cmd);
1140 }
1141
1142 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1143 {
1144         struct aac_srb * srbcmd;
1145         u32 flag;
1146         u32 timeout;
1147
1148         aac_fib_init(fib);
1149         switch(cmd->sc_data_direction){
1150         case DMA_TO_DEVICE:
1151                 flag = SRB_DataOut;
1152                 break;
1153         case DMA_BIDIRECTIONAL:
1154                 flag = SRB_DataIn | SRB_DataOut;
1155                 break;
1156         case DMA_FROM_DEVICE:
1157                 flag = SRB_DataIn;
1158                 break;
1159         case DMA_NONE:
1160         default:        /* shuts up some versions of gcc */
1161                 flag = SRB_NoDataXfer;
1162                 break;
1163         }
1164
1165         srbcmd = (struct aac_srb*) fib_data(fib);
1166         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1167         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1168         srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1169         srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1170         srbcmd->flags    = cpu_to_le32(flag);
1171         timeout = cmd->request->timeout/HZ;
1172         if (timeout == 0)
1173                 timeout = 1;
1174         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1175         srbcmd->retry_limit = 0; /* Obsolete parameter */
1176         srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1177         return srbcmd;
1178 }
1179
1180 static void aac_srb_callback(void *context, struct fib * fibptr);
1181
1182 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1183 {
1184         u16 fibsize;
1185         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1186
1187         aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
1188         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1189
1190         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1191         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1192         /*
1193          *      Build Scatter/Gather list
1194          */
1195         fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1196                 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1197                  sizeof (struct sgentry64));
1198         BUG_ON (fibsize > (fib->dev->max_fib_size -
1199                                 sizeof(struct aac_fibhdr)));
1200
1201         /*
1202          *      Now send the Fib to the adapter
1203          */
1204         return aac_fib_send(ScsiPortCommand64, fib,
1205                                 fibsize, FsaNormal, 0, 1,
1206                                   (fib_callback) aac_srb_callback,
1207                                   (void *) cmd);
1208 }
1209
1210 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1211 {
1212         u16 fibsize;
1213         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1214
1215         aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
1216         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1217
1218         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1219         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1220         /*
1221          *      Build Scatter/Gather list
1222          */
1223         fibsize = sizeof (struct aac_srb) +
1224                 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1225                  sizeof (struct sgentry));
1226         BUG_ON (fibsize > (fib->dev->max_fib_size -
1227                                 sizeof(struct aac_fibhdr)));
1228
1229         /*
1230          *      Now send the Fib to the adapter
1231          */
1232         return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1233                                   (fib_callback) aac_srb_callback, (void *) cmd);
1234 }
1235
1236 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1237 {
1238         if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1239             (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1240                 return FAILED;
1241         return aac_scsi_32(fib, cmd);
1242 }
1243
1244 int aac_get_adapter_info(struct aac_dev* dev)
1245 {
1246         struct fib* fibptr;
1247         int rcode;
1248         u32 tmp;
1249         struct aac_adapter_info *info;
1250         struct aac_bus_info *command;
1251         struct aac_bus_info_response *bus_info;
1252
1253         if (!(fibptr = aac_fib_alloc(dev)))
1254                 return -ENOMEM;
1255
1256         aac_fib_init(fibptr);
1257         info = (struct aac_adapter_info *) fib_data(fibptr);
1258         memset(info,0,sizeof(*info));
1259
1260         rcode = aac_fib_send(RequestAdapterInfo,
1261                          fibptr,
1262                          sizeof(*info),
1263                          FsaNormal,
1264                          -1, 1, /* First `interrupt' command uses special wait */
1265                          NULL,
1266                          NULL);
1267
1268         if (rcode < 0) {
1269                 /* FIB should be freed only after
1270                  * getting the response from the F/W */
1271                 if (rcode != -ERESTARTSYS) {
1272                         aac_fib_complete(fibptr);
1273                         aac_fib_free(fibptr);
1274                 }
1275                 return rcode;
1276         }
1277         memcpy(&dev->adapter_info, info, sizeof(*info));
1278
1279         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1280                 struct aac_supplement_adapter_info * sinfo;
1281
1282                 aac_fib_init(fibptr);
1283
1284                 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1285
1286                 memset(sinfo,0,sizeof(*sinfo));
1287
1288                 rcode = aac_fib_send(RequestSupplementAdapterInfo,
1289                                  fibptr,
1290                                  sizeof(*sinfo),
1291                                  FsaNormal,
1292                                  1, 1,
1293                                  NULL,
1294                                  NULL);
1295
1296                 if (rcode >= 0)
1297                         memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
1298                 if (rcode == -ERESTARTSYS) {
1299                         fibptr = aac_fib_alloc(dev);
1300                         if (!fibptr)
1301                                 return -ENOMEM;
1302                 }
1303
1304         }
1305
1306
1307         /*
1308          * GetBusInfo
1309          */
1310
1311         aac_fib_init(fibptr);
1312
1313         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1314
1315         memset(bus_info, 0, sizeof(*bus_info));
1316
1317         command = (struct aac_bus_info *)bus_info;
1318
1319         command->Command = cpu_to_le32(VM_Ioctl);
1320         command->ObjType = cpu_to_le32(FT_DRIVE);
1321         command->MethodId = cpu_to_le32(1);
1322         command->CtlCmd = cpu_to_le32(GetBusInfo);
1323
1324         rcode = aac_fib_send(ContainerCommand,
1325                          fibptr,
1326                          sizeof (*bus_info),
1327                          FsaNormal,
1328                          1, 1,
1329                          NULL, NULL);
1330
1331         /* reasoned default */
1332         dev->maximum_num_physicals = 16;
1333         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1334                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1335                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1336         }
1337
1338         if (!dev->in_reset) {
1339                 char buffer[16];
1340                 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1341                 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1342                         dev->name,
1343                         dev->id,
1344                         tmp>>24,
1345                         (tmp>>16)&0xff,
1346                         tmp&0xff,
1347                         le32_to_cpu(dev->adapter_info.kernelbuild),
1348                         (int)sizeof(dev->supplement_adapter_info.BuildDate),
1349                         dev->supplement_adapter_info.BuildDate);
1350                 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1351                 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1352                         dev->name, dev->id,
1353                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1354                         le32_to_cpu(dev->adapter_info.monitorbuild));
1355                 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1356                 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
1357                         dev->name, dev->id,
1358                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1359                         le32_to_cpu(dev->adapter_info.biosbuild));
1360                 buffer[0] = '\0';
1361                 if (aac_get_serial_number(
1362                   shost_to_class(dev->scsi_host_ptr), buffer))
1363                         printk(KERN_INFO "%s%d: serial %s",
1364                           dev->name, dev->id, buffer);
1365                 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1366                         printk(KERN_INFO "%s%d: TSID %.*s\n",
1367                           dev->name, dev->id,
1368                           (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1369                           dev->supplement_adapter_info.VpdInfo.Tsid);
1370                 }
1371                 if (!aac_check_reset || ((aac_check_reset == 1) &&
1372                   (dev->supplement_adapter_info.SupportedOptions2 &
1373                   AAC_OPTION_IGNORE_RESET))) {
1374                         printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1375                           dev->name, dev->id);
1376                 }
1377         }
1378
1379         dev->cache_protected = 0;
1380         dev->jbod = ((dev->supplement_adapter_info.FeatureBits &
1381                 AAC_FEATURE_JBOD) != 0);
1382         dev->nondasd_support = 0;
1383         dev->raid_scsi_mode = 0;
1384         if(dev->adapter_info.options & AAC_OPT_NONDASD)
1385                 dev->nondasd_support = 1;
1386
1387         /*
1388          * If the firmware supports ROMB RAID/SCSI mode and we are currently
1389          * in RAID/SCSI mode, set the flag. For now if in this mode we will
1390          * force nondasd support on. If we decide to allow the non-dasd flag
1391          * additional changes changes will have to be made to support
1392          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
1393          * changed to support the new dev->raid_scsi_mode flag instead of
1394          * leaching off of the dev->nondasd_support flag. Also in linit.c the
1395          * function aac_detect will have to be modified where it sets up the
1396          * max number of channels based on the aac->nondasd_support flag only.
1397          */
1398         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1399             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1400                 dev->nondasd_support = 1;
1401                 dev->raid_scsi_mode = 1;
1402         }
1403         if (dev->raid_scsi_mode != 0)
1404                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1405                                 dev->name, dev->id);
1406
1407         if (nondasd != -1)
1408                 dev->nondasd_support = (nondasd!=0);
1409         if (dev->nondasd_support && !dev->in_reset)
1410                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1411
1412         if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
1413                 dev->needs_dac = 1;
1414         dev->dac_support = 0;
1415         if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
1416             (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
1417                 if (!dev->in_reset)
1418                         printk(KERN_INFO "%s%d: 64bit support enabled.\n",
1419                                 dev->name, dev->id);
1420                 dev->dac_support = 1;
1421         }
1422
1423         if(dacmode != -1) {
1424                 dev->dac_support = (dacmode!=0);
1425         }
1426
1427         /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
1428         if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
1429                 & AAC_QUIRK_SCSI_32)) {
1430                 dev->nondasd_support = 0;
1431                 dev->jbod = 0;
1432                 expose_physicals = 0;
1433         }
1434
1435         if(dev->dac_support != 0) {
1436                 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64)) &&
1437                         !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
1438                         if (!dev->in_reset)
1439                                 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1440                                         dev->name, dev->id);
1441                 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32)) &&
1442                         !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
1443                         printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1444                                 dev->name, dev->id);
1445                         dev->dac_support = 0;
1446                 } else {
1447                         printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1448                                 dev->name, dev->id);
1449                         rcode = -ENOMEM;
1450                 }
1451         }
1452         /*
1453          * Deal with configuring for the individualized limits of each packet
1454          * interface.
1455          */
1456         dev->a_ops.adapter_scsi = (dev->dac_support)
1457           ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1458                                 ? aac_scsi_32_64
1459                                 : aac_scsi_64)
1460                                 : aac_scsi_32;
1461         if (dev->raw_io_interface) {
1462                 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1463                                         ? aac_bounds_64
1464                                         : aac_bounds_32;
1465                 dev->a_ops.adapter_read = aac_read_raw_io;
1466                 dev->a_ops.adapter_write = aac_write_raw_io;
1467         } else {
1468                 dev->a_ops.adapter_bounds = aac_bounds_32;
1469                 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
1470                         sizeof(struct aac_fibhdr) -
1471                         sizeof(struct aac_write) + sizeof(struct sgentry)) /
1472                                 sizeof(struct sgentry);
1473                 if (dev->dac_support) {
1474                         dev->a_ops.adapter_read = aac_read_block64;
1475                         dev->a_ops.adapter_write = aac_write_block64;
1476                         /*
1477                          * 38 scatter gather elements
1478                          */
1479                         dev->scsi_host_ptr->sg_tablesize =
1480                                 (dev->max_fib_size -
1481                                 sizeof(struct aac_fibhdr) -
1482                                 sizeof(struct aac_write64) +
1483                                 sizeof(struct sgentry64)) /
1484                                         sizeof(struct sgentry64);
1485                 } else {
1486                         dev->a_ops.adapter_read = aac_read_block;
1487                         dev->a_ops.adapter_write = aac_write_block;
1488                 }
1489                 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1490                 if (dev->adapter_info.options & AAC_OPT_NEW_COMM_TYPE1)
1491                         dev->adapter_info.options |= AAC_OPT_NEW_COMM;
1492                 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1493                         /*
1494                          * Worst case size that could cause sg overflow when
1495                          * we break up SG elements that are larger than 64KB.
1496                          * Would be nice if we could tell the SCSI layer what
1497                          * the maximum SG element size can be. Worst case is
1498                          * (sg_tablesize-1) 4KB elements with one 64KB
1499                          * element.
1500                          *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
1501                          */
1502                         dev->scsi_host_ptr->max_sectors =
1503                           (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1504                 }
1505         }
1506         /* FIB should be freed only after getting the response from the F/W */
1507         if (rcode != -ERESTARTSYS) {
1508                 aac_fib_complete(fibptr);
1509                 aac_fib_free(fibptr);
1510         }
1511
1512         return rcode;
1513 }
1514
1515
1516 static void io_callback(void *context, struct fib * fibptr)
1517 {
1518         struct aac_dev *dev;
1519         struct aac_read_reply *readreply;
1520         struct scsi_cmnd *scsicmd;
1521         u32 cid;
1522
1523         scsicmd = (struct scsi_cmnd *) context;
1524
1525         if (!aac_valid_context(scsicmd, fibptr))
1526                 return;
1527
1528         dev = fibptr->dev;
1529         cid = scmd_id(scsicmd);
1530
1531         if (nblank(dprintk(x))) {
1532                 u64 lba;
1533                 switch (scsicmd->cmnd[0]) {
1534                 case WRITE_6:
1535                 case READ_6:
1536                         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1537                             (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1538                         break;
1539                 case WRITE_16:
1540                 case READ_16:
1541                         lba = ((u64)scsicmd->cmnd[2] << 56) |
1542                               ((u64)scsicmd->cmnd[3] << 48) |
1543                               ((u64)scsicmd->cmnd[4] << 40) |
1544                               ((u64)scsicmd->cmnd[5] << 32) |
1545                               ((u64)scsicmd->cmnd[6] << 24) |
1546                               (scsicmd->cmnd[7] << 16) |
1547                               (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1548                         break;
1549                 case WRITE_12:
1550                 case READ_12:
1551                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1552                               (scsicmd->cmnd[3] << 16) |
1553                               (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1554                         break;
1555                 default:
1556                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1557                                (scsicmd->cmnd[3] << 16) |
1558                                (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1559                         break;
1560                 }
1561                 printk(KERN_DEBUG
1562                   "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1563                   smp_processor_id(), (unsigned long long)lba, jiffies);
1564         }
1565
1566         BUG_ON(fibptr == NULL);
1567
1568         scsi_dma_unmap(scsicmd);
1569
1570         readreply = (struct aac_read_reply *)fib_data(fibptr);
1571         switch (le32_to_cpu(readreply->status)) {
1572         case ST_OK:
1573                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1574                         SAM_STAT_GOOD;
1575                 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
1576                 break;
1577         case ST_NOT_READY:
1578                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1579                         SAM_STAT_CHECK_CONDITION;
1580                 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
1581                   SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
1582                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1583                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1584                              SCSI_SENSE_BUFFERSIZE));
1585                 break;
1586         default:
1587 #ifdef AAC_DETAILED_STATUS_INFO
1588                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
1589                   le32_to_cpu(readreply->status));
1590 #endif
1591                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1592                         SAM_STAT_CHECK_CONDITION;
1593                 set_sense(&dev->fsa_dev[cid].sense_data,
1594                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1595                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1596                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1597                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1598                              SCSI_SENSE_BUFFERSIZE));
1599                 break;
1600         }
1601         aac_fib_complete(fibptr);
1602         aac_fib_free(fibptr);
1603
1604         scsicmd->scsi_done(scsicmd);
1605 }
1606
1607 static int aac_read(struct scsi_cmnd * scsicmd)
1608 {
1609         u64 lba;
1610         u32 count;
1611         int status;
1612         struct aac_dev *dev;
1613         struct fib * cmd_fibcontext;
1614         int cid;
1615
1616         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1617         /*
1618          *      Get block address and transfer length
1619          */
1620         switch (scsicmd->cmnd[0]) {
1621         case READ_6:
1622                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
1623
1624                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1625                         (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1626                 count = scsicmd->cmnd[4];
1627
1628                 if (count == 0)
1629                         count = 256;
1630                 break;
1631         case READ_16:
1632                 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
1633
1634                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1635                         ((u64)scsicmd->cmnd[3] << 48) |
1636                         ((u64)scsicmd->cmnd[4] << 40) |
1637                         ((u64)scsicmd->cmnd[5] << 32) |
1638                         ((u64)scsicmd->cmnd[6] << 24) |
1639                         (scsicmd->cmnd[7] << 16) |
1640                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1641                 count = (scsicmd->cmnd[10] << 24) |
1642                         (scsicmd->cmnd[11] << 16) |
1643                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1644                 break;
1645         case READ_12:
1646                 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
1647
1648                 lba = ((u64)scsicmd->cmnd[2] << 24) |
1649                         (scsicmd->cmnd[3] << 16) |
1650                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1651                 count = (scsicmd->cmnd[6] << 24) |
1652                         (scsicmd->cmnd[7] << 16) |
1653                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1654                 break;
1655         default:
1656                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
1657
1658                 lba = ((u64)scsicmd->cmnd[2] << 24) |
1659                         (scsicmd->cmnd[3] << 16) |
1660                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1661                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1662                 break;
1663         }
1664
1665         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1666                 cid = scmd_id(scsicmd);
1667                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1668                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1669                         SAM_STAT_CHECK_CONDITION;
1670                 set_sense(&dev->fsa_dev[cid].sense_data,
1671                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1672                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1673                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1674                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1675                              SCSI_SENSE_BUFFERSIZE));
1676                 scsicmd->scsi_done(scsicmd);
1677                 return 1;
1678         }
1679
1680         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
1681           smp_processor_id(), (unsigned long long)lba, jiffies));
1682         if (aac_adapter_bounds(dev,scsicmd,lba))
1683                 return 0;
1684         /*
1685          *      Alocate and initialize a Fib
1686          */
1687         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1688                 printk(KERN_WARNING "aac_read: fib allocation failed\n");
1689                 return -1;
1690         }
1691
1692         status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
1693
1694         /*
1695          *      Check that the command queued to the controller
1696          */
1697         if (status == -EINPROGRESS) {
1698                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1699                 return 0;
1700         }
1701
1702         printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
1703         /*
1704          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1705          */
1706         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1707         scsicmd->scsi_done(scsicmd);
1708         aac_fib_complete(cmd_fibcontext);
1709         aac_fib_free(cmd_fibcontext);
1710         return 0;
1711 }
1712
1713 static int aac_write(struct scsi_cmnd * scsicmd)
1714 {
1715         u64 lba;
1716         u32 count;
1717         int fua;
1718         int status;
1719         struct aac_dev *dev;
1720         struct fib * cmd_fibcontext;
1721         int cid;
1722
1723         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1724         /*
1725          *      Get block address and transfer length
1726          */
1727         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
1728         {
1729                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1730                 count = scsicmd->cmnd[4];
1731                 if (count == 0)
1732                         count = 256;
1733                 fua = 0;
1734         } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1735                 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
1736
1737                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1738                         ((u64)scsicmd->cmnd[3] << 48) |
1739                         ((u64)scsicmd->cmnd[4] << 40) |
1740                         ((u64)scsicmd->cmnd[5] << 32) |
1741                         ((u64)scsicmd->cmnd[6] << 24) |
1742                         (scsicmd->cmnd[7] << 16) |
1743                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1744                 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1745                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1746                 fua = scsicmd->cmnd[1] & 0x8;
1747         } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1748                 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
1749
1750                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1751                     | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1752                 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1753                       | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1754                 fua = scsicmd->cmnd[1] & 0x8;
1755         } else {
1756                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
1757                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1758                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1759                 fua = scsicmd->cmnd[1] & 0x8;
1760         }
1761
1762         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1763                 cid = scmd_id(scsicmd);
1764                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1765                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1766                         SAM_STAT_CHECK_CONDITION;
1767                 set_sense(&dev->fsa_dev[cid].sense_data,
1768                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1769                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1770                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1771                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1772                              SCSI_SENSE_BUFFERSIZE));
1773                 scsicmd->scsi_done(scsicmd);
1774                 return 1;
1775         }
1776
1777         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
1778           smp_processor_id(), (unsigned long long)lba, jiffies));
1779         if (aac_adapter_bounds(dev,scsicmd,lba))
1780                 return 0;
1781         /*
1782          *      Allocate and initialize a Fib then setup a BlockWrite command
1783          */
1784         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1785                 /* FIB temporarily unavailable,not catastrophic failure */
1786
1787                 /* scsicmd->result = DID_ERROR << 16;
1788                  * scsicmd->scsi_done(scsicmd);
1789                  * return 0;
1790                  */
1791                 printk(KERN_WARNING "aac_write: fib allocation failed\n");
1792                 return -1;
1793         }
1794
1795         status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
1796
1797         /*
1798          *      Check that the command queued to the controller
1799          */
1800         if (status == -EINPROGRESS) {
1801                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1802                 return 0;
1803         }
1804
1805         printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
1806         /*
1807          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1808          */
1809         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1810         scsicmd->scsi_done(scsicmd);
1811
1812         aac_fib_complete(cmd_fibcontext);
1813         aac_fib_free(cmd_fibcontext);
1814         return 0;
1815 }
1816
1817 static void synchronize_callback(void *context, struct fib *fibptr)
1818 {
1819         struct aac_synchronize_reply *synchronizereply;
1820         struct scsi_cmnd *cmd;
1821
1822         cmd = context;
1823
1824         if (!aac_valid_context(cmd, fibptr))
1825                 return;
1826
1827         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1828                                 smp_processor_id(), jiffies));
1829         BUG_ON(fibptr == NULL);
1830
1831
1832         synchronizereply = fib_data(fibptr);
1833         if (le32_to_cpu(synchronizereply->status) == CT_OK)
1834                 cmd->result = DID_OK << 16 |
1835                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1836         else {
1837                 struct scsi_device *sdev = cmd->device;
1838                 struct aac_dev *dev = fibptr->dev;
1839                 u32 cid = sdev_id(sdev);
1840                 printk(KERN_WARNING
1841                      "synchronize_callback: synchronize failed, status = %d\n",
1842                      le32_to_cpu(synchronizereply->status));
1843                 cmd->result = DID_OK << 16 |
1844                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1845                 set_sense(&dev->fsa_dev[cid].sense_data,
1846                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1847                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1848                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1849                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1850                              SCSI_SENSE_BUFFERSIZE));
1851         }
1852
1853         aac_fib_complete(fibptr);
1854         aac_fib_free(fibptr);
1855         cmd->scsi_done(cmd);
1856 }
1857
1858 static int aac_synchronize(struct scsi_cmnd *scsicmd)
1859 {
1860         int status;
1861         struct fib *cmd_fibcontext;
1862         struct aac_synchronize *synchronizecmd;
1863         struct scsi_cmnd *cmd;
1864         struct scsi_device *sdev = scsicmd->device;
1865         int active = 0;
1866         struct aac_dev *aac;
1867         u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
1868                 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1869         u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1870         unsigned long flags;
1871
1872         /*
1873          * Wait for all outstanding queued commands to complete to this
1874          * specific target (block).
1875          */
1876         spin_lock_irqsave(&sdev->list_lock, flags);
1877         list_for_each_entry(cmd, &sdev->cmd_list, list)
1878                 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1879                         u64 cmnd_lba;
1880                         u32 cmnd_count;
1881
1882                         if (cmd->cmnd[0] == WRITE_6) {
1883                                 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
1884                                         (cmd->cmnd[2] << 8) |
1885                                         cmd->cmnd[3];
1886                                 cmnd_count = cmd->cmnd[4];
1887                                 if (cmnd_count == 0)
1888                                         cmnd_count = 256;
1889                         } else if (cmd->cmnd[0] == WRITE_16) {
1890                                 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
1891                                         ((u64)cmd->cmnd[3] << 48) |
1892                                         ((u64)cmd->cmnd[4] << 40) |
1893                                         ((u64)cmd->cmnd[5] << 32) |
1894                                         ((u64)cmd->cmnd[6] << 24) |
1895                                         (cmd->cmnd[7] << 16) |
1896                                         (cmd->cmnd[8] << 8) |
1897                                         cmd->cmnd[9];
1898                                 cmnd_count = (cmd->cmnd[10] << 24) |
1899                                         (cmd->cmnd[11] << 16) |
1900                                         (cmd->cmnd[12] << 8) |
1901                                         cmd->cmnd[13];
1902                         } else if (cmd->cmnd[0] == WRITE_12) {
1903                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1904                                         (cmd->cmnd[3] << 16) |
1905                                         (cmd->cmnd[4] << 8) |
1906                                         cmd->cmnd[5];
1907                                 cmnd_count = (cmd->cmnd[6] << 24) |
1908                                         (cmd->cmnd[7] << 16) |
1909                                         (cmd->cmnd[8] << 8) |
1910                                         cmd->cmnd[9];
1911                         } else if (cmd->cmnd[0] == WRITE_10) {
1912                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1913                                         (cmd->cmnd[3] << 16) |
1914                                         (cmd->cmnd[4] << 8) |
1915                                         cmd->cmnd[5];
1916                                 cmnd_count = (cmd->cmnd[7] << 8) |
1917                                         cmd->cmnd[8];
1918                         } else
1919                                 continue;
1920                         if (((cmnd_lba + cmnd_count) < lba) ||
1921                           (count && ((lba + count) < cmnd_lba)))
1922                                 continue;
1923                         ++active;
1924                         break;
1925                 }
1926
1927         spin_unlock_irqrestore(&sdev->list_lock, flags);
1928
1929         /*
1930          *      Yield the processor (requeue for later)
1931          */
1932         if (active)
1933                 return SCSI_MLQUEUE_DEVICE_BUSY;
1934
1935         aac = (struct aac_dev *)sdev->host->hostdata;
1936         if (aac->in_reset)
1937                 return SCSI_MLQUEUE_HOST_BUSY;
1938
1939         /*
1940          *      Allocate and initialize a Fib
1941          */
1942         if (!(cmd_fibcontext = aac_fib_alloc(aac)))
1943                 return SCSI_MLQUEUE_HOST_BUSY;
1944
1945         aac_fib_init(cmd_fibcontext);
1946
1947         synchronizecmd = fib_data(cmd_fibcontext);
1948         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1949         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1950         synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
1951         synchronizecmd->count =
1952              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1953
1954         /*
1955          *      Now send the Fib to the adapter
1956          */
1957         status = aac_fib_send(ContainerCommand,
1958                   cmd_fibcontext,
1959                   sizeof(struct aac_synchronize),
1960                   FsaNormal,
1961                   0, 1,
1962                   (fib_callback)synchronize_callback,
1963                   (void *)scsicmd);
1964
1965         /*
1966          *      Check that the command queued to the controller
1967          */
1968         if (status == -EINPROGRESS) {
1969                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1970                 return 0;
1971         }
1972
1973         printk(KERN_WARNING
1974                 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1975         aac_fib_complete(cmd_fibcontext);
1976         aac_fib_free(cmd_fibcontext);
1977         return SCSI_MLQUEUE_HOST_BUSY;
1978 }
1979
1980 static void aac_start_stop_callback(void *context, struct fib *fibptr)
1981 {
1982         struct scsi_cmnd *scsicmd = context;
1983
1984         if (!aac_valid_context(scsicmd, fibptr))
1985                 return;
1986
1987         BUG_ON(fibptr == NULL);
1988
1989         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1990
1991         aac_fib_complete(fibptr);
1992         aac_fib_free(fibptr);
1993         scsicmd->scsi_done(scsicmd);
1994 }
1995
1996 static int aac_start_stop(struct scsi_cmnd *scsicmd)
1997 {
1998         int status;
1999         struct fib *cmd_fibcontext;
2000         struct aac_power_management *pmcmd;
2001         struct scsi_device *sdev = scsicmd->device;
2002         struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2003
2004         if (!(aac->supplement_adapter_info.SupportedOptions2 &
2005               AAC_OPTION_POWER_MANAGEMENT)) {
2006                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2007                                   SAM_STAT_GOOD;
2008                 scsicmd->scsi_done(scsicmd);
2009                 return 0;
2010         }
2011
2012         if (aac->in_reset)
2013                 return SCSI_MLQUEUE_HOST_BUSY;
2014
2015         /*
2016          *      Allocate and initialize a Fib
2017          */
2018         cmd_fibcontext = aac_fib_alloc(aac);
2019         if (!cmd_fibcontext)
2020                 return SCSI_MLQUEUE_HOST_BUSY;
2021
2022         aac_fib_init(cmd_fibcontext);
2023
2024         pmcmd = fib_data(cmd_fibcontext);
2025         pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2026         pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2027         /* Eject bit ignored, not relevant */
2028         pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2029                 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2030         pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2031         pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2032                 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2033
2034         /*
2035          *      Now send the Fib to the adapter
2036          */
2037         status = aac_fib_send(ContainerCommand,
2038                   cmd_fibcontext,
2039                   sizeof(struct aac_power_management),
2040                   FsaNormal,
2041                   0, 1,
2042                   (fib_callback)aac_start_stop_callback,
2043                   (void *)scsicmd);
2044
2045         /*
2046          *      Check that the command queued to the controller
2047          */
2048         if (status == -EINPROGRESS) {
2049                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2050                 return 0;
2051         }
2052
2053         aac_fib_complete(cmd_fibcontext);
2054         aac_fib_free(cmd_fibcontext);
2055         return SCSI_MLQUEUE_HOST_BUSY;
2056 }
2057
2058 /**
2059  *      aac_scsi_cmd()          -       Process SCSI command
2060  *      @scsicmd:               SCSI command block
2061  *
2062  *      Emulate a SCSI command and queue the required request for the
2063  *      aacraid firmware.
2064  */
2065
2066 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2067 {
2068         u32 cid;
2069         struct Scsi_Host *host = scsicmd->device->host;
2070         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2071         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2072
2073         if (fsa_dev_ptr == NULL)
2074                 return -1;
2075         /*
2076          *      If the bus, id or lun is out of range, return fail
2077          *      Test does not apply to ID 16, the pseudo id for the controller
2078          *      itself.
2079          */
2080         cid = scmd_id(scsicmd);
2081         if (cid != host->this_id) {
2082                 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2083                         if((cid >= dev->maximum_num_containers) ||
2084                                         (scsicmd->device->lun != 0)) {
2085                                 scsicmd->result = DID_NO_CONNECT << 16;
2086                                 scsicmd->scsi_done(scsicmd);
2087                                 return 0;
2088                         }
2089
2090                         /*
2091                          *      If the target container doesn't exist, it may have
2092                          *      been newly created
2093                          */
2094                         if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2095                           (fsa_dev_ptr[cid].sense_data.sense_key ==
2096                            NOT_READY)) {
2097                                 switch (scsicmd->cmnd[0]) {
2098                                 case SERVICE_ACTION_IN:
2099                                         if (!(dev->raw_io_interface) ||
2100                                             !(dev->raw_io_64) ||
2101                                             ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2102                                                 break;
2103                                 case INQUIRY:
2104                                 case READ_CAPACITY:
2105                                 case TEST_UNIT_READY:
2106                                         if (dev->in_reset)
2107                                                 return -1;
2108                                         return _aac_probe_container(scsicmd,
2109                                                         aac_probe_container_callback2);
2110                                 default:
2111                                         break;
2112                                 }
2113                         }
2114                 } else {  /* check for physical non-dasd devices */
2115                         if (dev->nondasd_support || expose_physicals ||
2116                                         dev->jbod) {
2117                                 if (dev->in_reset)
2118                                         return -1;
2119                                 return aac_send_srb_fib(scsicmd);
2120                         } else {
2121                                 scsicmd->result = DID_NO_CONNECT << 16;
2122                                 scsicmd->scsi_done(scsicmd);
2123                                 return 0;
2124                         }
2125                 }
2126         }
2127         /*
2128          * else Command for the controller itself
2129          */
2130         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
2131                 (scsicmd->cmnd[0] != TEST_UNIT_READY))
2132         {
2133                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2134                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2135                 set_sense(&dev->fsa_dev[cid].sense_data,
2136                   ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2137                   ASENCODE_INVALID_COMMAND, 0, 0);
2138                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2139                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2140                              SCSI_SENSE_BUFFERSIZE));
2141                 scsicmd->scsi_done(scsicmd);
2142                 return 0;
2143         }
2144
2145
2146         /* Handle commands here that don't really require going out to the adapter */
2147         switch (scsicmd->cmnd[0]) {
2148         case INQUIRY:
2149         {
2150                 struct inquiry_data inq_data;
2151
2152                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2153                 memset(&inq_data, 0, sizeof (struct inquiry_data));
2154
2155                 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2156                         char *arr = (char *)&inq_data;
2157
2158                         /* EVPD bit set */
2159                         arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2160                           INQD_PDT_PROC : INQD_PDT_DA;
2161                         if (scsicmd->cmnd[2] == 0) {
2162                                 /* supported vital product data pages */
2163                                 arr[3] = 2;
2164                                 arr[4] = 0x0;
2165                                 arr[5] = 0x80;
2166                                 arr[1] = scsicmd->cmnd[2];
2167                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2168                                                          sizeof(inq_data));
2169                                 scsicmd->result = DID_OK << 16 |
2170                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2171                         } else if (scsicmd->cmnd[2] == 0x80) {
2172                                 /* unit serial number page */
2173                                 arr[3] = setinqserial(dev, &arr[4],
2174                                   scmd_id(scsicmd));
2175                                 arr[1] = scsicmd->cmnd[2];
2176                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2177                                                          sizeof(inq_data));
2178                                 if (aac_wwn != 2)
2179                                         return aac_get_container_serial(
2180                                                 scsicmd);
2181                                 /* SLES 10 SP1 special */
2182                                 scsicmd->result = DID_OK << 16 |
2183                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2184                         } else {
2185                                 /* vpd page not implemented */
2186                                 scsicmd->result = DID_OK << 16 |
2187                                   COMMAND_COMPLETE << 8 |
2188                                   SAM_STAT_CHECK_CONDITION;
2189                                 set_sense(&dev->fsa_dev[cid].sense_data,
2190                                   ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2191                                   ASENCODE_NO_SENSE, 7, 2);
2192                                 memcpy(scsicmd->sense_buffer,
2193                                   &dev->fsa_dev[cid].sense_data,
2194                                   min_t(size_t,
2195                                         sizeof(dev->fsa_dev[cid].sense_data),
2196                                         SCSI_SENSE_BUFFERSIZE));
2197                         }
2198                         scsicmd->scsi_done(scsicmd);
2199                         return 0;
2200                 }
2201                 inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
2202                 inq_data.inqd_rdf = 2;  /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
2203                 inq_data.inqd_len = 31;
2204                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
2205                 inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
2206                 /*
2207                  *      Set the Vendor, Product, and Revision Level
2208                  *      see: <vendor>.c i.e. aac.c
2209                  */
2210                 if (cid == host->this_id) {
2211                         setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2212                         inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
2213                         scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2214                                                  sizeof(inq_data));
2215                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2216                         scsicmd->scsi_done(scsicmd);
2217                         return 0;
2218                 }
2219                 if (dev->in_reset)
2220                         return -1;
2221                 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2222                 inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
2223                 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
2224                 return aac_get_container_name(scsicmd);
2225         }
2226         case SERVICE_ACTION_IN:
2227                 if (!(dev->raw_io_interface) ||
2228                     !(dev->raw_io_64) ||
2229                     ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2230                         break;
2231         {
2232                 u64 capacity;
2233                 char cp[13];
2234                 unsigned int alloc_len;
2235
2236                 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2237                 capacity = fsa_dev_ptr[cid].size - 1;
2238                 cp[0] = (capacity >> 56) & 0xff;
2239                 cp[1] = (capacity >> 48) & 0xff;
2240                 cp[2] = (capacity >> 40) & 0xff;
2241                 cp[3] = (capacity >> 32) & 0xff;
2242                 cp[4] = (capacity >> 24) & 0xff;
2243                 cp[5] = (capacity >> 16) & 0xff;
2244                 cp[6] = (capacity >> 8) & 0xff;
2245                 cp[7] = (capacity >> 0) & 0xff;
2246                 cp[8] = 0;
2247                 cp[9] = 0;
2248                 cp[10] = 2;
2249                 cp[11] = 0;
2250                 cp[12] = 0;
2251
2252                 alloc_len = ((scsicmd->cmnd[10] << 24)
2253                              + (scsicmd->cmnd[11] << 16)
2254                              + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2255
2256                 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
2257                 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
2258                 if (alloc_len < scsi_bufflen(scsicmd))
2259                         scsi_set_resid(scsicmd,
2260                                        scsi_bufflen(scsicmd) - alloc_len);
2261
2262                 /* Do not cache partition table for arrays */
2263                 scsicmd->device->removable = 1;
2264
2265                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2266                 scsicmd->scsi_done(scsicmd);
2267
2268                 return 0;
2269         }
2270
2271         case READ_CAPACITY:
2272         {
2273                 u32 capacity;
2274                 char cp[8];
2275
2276                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
2277                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2278                         capacity = fsa_dev_ptr[cid].size - 1;
2279                 else
2280                         capacity = (u32)-1;
2281
2282                 cp[0] = (capacity >> 24) & 0xff;
2283                 cp[1] = (capacity >> 16) & 0xff;
2284                 cp[2] = (capacity >> 8) & 0xff;
2285                 cp[3] = (capacity >> 0) & 0xff;
2286                 cp[4] = 0;
2287                 cp[5] = 0;
2288                 cp[6] = 2;
2289                 cp[7] = 0;
2290                 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
2291                 /* Do not cache partition table for arrays */
2292                 scsicmd->device->removable = 1;
2293                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2294                   SAM_STAT_GOOD;
2295                 scsicmd->scsi_done(scsicmd);
2296
2297                 return 0;
2298         }
2299
2300         case MODE_SENSE:
2301         {
2302                 char mode_buf[7];
2303                 int mode_buf_length = 4;
2304
2305                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
2306                 mode_buf[0] = 3;        /* Mode data length */
2307                 mode_buf[1] = 0;        /* Medium type - default */
2308                 mode_buf[2] = 0;        /* Device-specific param,
2309                                            bit 8: 0/1 = write enabled/protected
2310                                            bit 4: 0/1 = FUA enabled */
2311                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2312                         mode_buf[2] = 0x10;
2313                 mode_buf[3] = 0;        /* Block descriptor length */
2314                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2315                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2316                         mode_buf[0] = 6;
2317                         mode_buf[4] = 8;
2318                         mode_buf[5] = 1;
2319                         mode_buf[6] = ((aac_cache & 6) == 2)
2320                                 ? 0 : 0x04; /* WCE */
2321                         mode_buf_length = 7;
2322                         if (mode_buf_length > scsicmd->cmnd[4])
2323                                 mode_buf_length = scsicmd->cmnd[4];
2324                 }
2325                 scsi_sg_copy_from_buffer(scsicmd, mode_buf, mode_buf_length);
2326                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2327                 scsicmd->scsi_done(scsicmd);
2328
2329                 return 0;
2330         }
2331         case MODE_SENSE_10:
2332         {
2333                 char mode_buf[11];
2334                 int mode_buf_length = 8;
2335
2336                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
2337                 mode_buf[0] = 0;        /* Mode data length (MSB) */
2338                 mode_buf[1] = 6;        /* Mode data length (LSB) */
2339                 mode_buf[2] = 0;        /* Medium type - default */
2340                 mode_buf[3] = 0;        /* Device-specific param,
2341                                            bit 8: 0/1 = write enabled/protected
2342                                            bit 4: 0/1 = FUA enabled */
2343                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2344                         mode_buf[3] = 0x10;
2345                 mode_buf[4] = 0;        /* reserved */
2346                 mode_buf[5] = 0;        /* reserved */
2347                 mode_buf[6] = 0;        /* Block descriptor length (MSB) */
2348                 mode_buf[7] = 0;        /* Block descriptor length (LSB) */
2349                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2350                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2351                         mode_buf[1] = 9;
2352                         mode_buf[8] = 8;
2353                         mode_buf[9] = 1;
2354                         mode_buf[10] = ((aac_cache & 6) == 2)
2355                                 ? 0 : 0x04; /* WCE */
2356                         mode_buf_length = 11;
2357                         if (mode_buf_length > scsicmd->cmnd[8])
2358                                 mode_buf_length = scsicmd->cmnd[8];
2359                 }
2360                 scsi_sg_copy_from_buffer(scsicmd, mode_buf, mode_buf_length);
2361
2362                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2363                 scsicmd->scsi_done(scsicmd);
2364
2365                 return 0;
2366         }
2367         case REQUEST_SENSE:
2368                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2369                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2370                 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2371                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2372                 scsicmd->scsi_done(scsicmd);
2373                 return 0;
2374
2375         case ALLOW_MEDIUM_REMOVAL:
2376                 dprintk((KERN_DEBUG "LOCK command.\n"));
2377                 if (scsicmd->cmnd[4])
2378                         fsa_dev_ptr[cid].locked = 1;
2379                 else
2380                         fsa_dev_ptr[cid].locked = 0;
2381
2382                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2383                 scsicmd->scsi_done(scsicmd);
2384                 return 0;
2385         /*
2386          *      These commands are all No-Ops
2387          */
2388         case TEST_UNIT_READY:
2389                 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
2390                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2391                                 SAM_STAT_CHECK_CONDITION;
2392                         set_sense(&dev->fsa_dev[cid].sense_data,
2393                                   NOT_READY, SENCODE_BECOMING_READY,
2394                                   ASENCODE_BECOMING_READY, 0, 0);
2395                         memcpy(scsicmd->sense_buffer,
2396                                &dev->fsa_dev[cid].sense_data,
2397                                min_t(size_t,
2398                                      sizeof(dev->fsa_dev[cid].sense_data),
2399                                      SCSI_SENSE_BUFFERSIZE));
2400                         scsicmd->scsi_done(scsicmd);
2401                         return 0;
2402                 }
2403                 /* FALLTHRU */
2404         case RESERVE:
2405         case RELEASE:
2406         case REZERO_UNIT:
2407         case REASSIGN_BLOCKS:
2408         case SEEK_10:
2409                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2410                 scsicmd->scsi_done(scsicmd);
2411                 return 0;
2412
2413         case START_STOP:
2414                 return aac_start_stop(scsicmd);
2415         }
2416
2417         switch (scsicmd->cmnd[0])
2418         {
2419                 case READ_6:
2420                 case READ_10:
2421                 case READ_12:
2422                 case READ_16:
2423                         if (dev->in_reset)
2424                                 return -1;
2425                         /*
2426                          *      Hack to keep track of ordinal number of the device that
2427                          *      corresponds to a container. Needed to convert
2428                          *      containers to /dev/sd device names
2429                          */
2430
2431                         if (scsicmd->request->rq_disk)
2432                                 strlcpy(fsa_dev_ptr[cid].devname,
2433                                 scsicmd->request->rq_disk->disk_name,
2434                                 min(sizeof(fsa_dev_ptr[cid].devname),
2435                                 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
2436
2437                         return aac_read(scsicmd);
2438
2439                 case WRITE_6:
2440                 case WRITE_10:
2441                 case WRITE_12:
2442                 case WRITE_16:
2443                         if (dev->in_reset)
2444                                 return -1;
2445                         return aac_write(scsicmd);
2446
2447                 case SYNCHRONIZE_CACHE:
2448                         if (((aac_cache & 6) == 6) && dev->cache_protected) {
2449                                 scsicmd->result = DID_OK << 16 |
2450                                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2451                                 scsicmd->scsi_done(scsicmd);
2452                                 return 0;
2453                         }
2454                         /* Issue FIB to tell Firmware to flush it's cache */
2455                         if ((aac_cache & 6) != 2)
2456                                 return aac_synchronize(scsicmd);
2457                         /* FALLTHRU */
2458                 default:
2459                         /*
2460                          *      Unhandled commands
2461                          */
2462                         dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
2463                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2464                         set_sense(&dev->fsa_dev[cid].sense_data,
2465                           ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2466                           ASENCODE_INVALID_COMMAND, 0, 0);
2467                         memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2468                                 min_t(size_t,
2469                                       sizeof(dev->fsa_dev[cid].sense_data),
2470                                       SCSI_SENSE_BUFFERSIZE));
2471                         scsicmd->scsi_done(scsicmd);
2472                         return 0;
2473         }
2474 }
2475
2476 static int query_disk(struct aac_dev *dev, void __user *arg)
2477 {
2478         struct aac_query_disk qd;
2479         struct fsa_dev_info *fsa_dev_ptr;
2480
2481         fsa_dev_ptr = dev->fsa_dev;
2482         if (!fsa_dev_ptr)
2483                 return -EBUSY;
2484         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2485                 return -EFAULT;
2486         if (qd.cnum == -1)
2487                 qd.cnum = qd.id;
2488         else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2489         {
2490                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2491                         return -EINVAL;
2492                 qd.instance = dev->scsi_host_ptr->host_no;
2493                 qd.bus = 0;
2494                 qd.id = CONTAINER_TO_ID(qd.cnum);
2495                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2496         }
2497         else return -EINVAL;
2498
2499         qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
2500         qd.locked = fsa_dev_ptr[qd.cnum].locked;
2501         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2502
2503         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2504                 qd.unmapped = 1;
2505         else
2506                 qd.unmapped = 0;
2507
2508         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2509           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2510
2511         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2512                 return -EFAULT;
2513         return 0;
2514 }
2515
2516 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2517 {
2518         struct aac_delete_disk dd;
2519         struct fsa_dev_info *fsa_dev_ptr;
2520
2521         fsa_dev_ptr = dev->fsa_dev;
2522         if (!fsa_dev_ptr)
2523                 return -EBUSY;
2524
2525         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2526                 return -EFAULT;
2527
2528         if (dd.cnum >= dev->maximum_num_containers)
2529                 return -EINVAL;
2530         /*
2531          *      Mark this container as being deleted.
2532          */
2533         fsa_dev_ptr[dd.cnum].deleted = 1;
2534         /*
2535          *      Mark the container as no longer valid
2536          */
2537         fsa_dev_ptr[dd.cnum].valid = 0;
2538         return 0;
2539 }
2540
2541 static int delete_disk(struct aac_dev *dev, void __user *arg)
2542 {
2543         struct aac_delete_disk dd;
2544         struct fsa_dev_info *fsa_dev_ptr;
2545
2546         fsa_dev_ptr = dev->fsa_dev;
2547         if (!fsa_dev_ptr)
2548                 return -EBUSY;
2549
2550         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2551                 return -EFAULT;
2552
2553         if (dd.cnum >= dev->maximum_num_containers)
2554                 return -EINVAL;
2555         /*
2556          *      If the container is locked, it can not be deleted by the API.
2557          */
2558         if (fsa_dev_ptr[dd.cnum].locked)
2559                 return -EBUSY;
2560         else {
2561                 /*
2562                  *      Mark the container as no longer being valid.
2563                  */
2564                 fsa_dev_ptr[dd.cnum].valid = 0;
2565                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2566                 return 0;
2567         }
2568 }
2569
2570 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2571 {
2572         switch (cmd) {
2573         case FSACTL_QUERY_DISK:
2574                 return query_disk(dev, arg);
2575         case FSACTL_DELETE_DISK:
2576                 return delete_disk(dev, arg);
2577         case FSACTL_FORCE_DELETE_DISK:
2578                 return force_delete_disk(dev, arg);
2579         case FSACTL_GET_CONTAINERS:
2580                 return aac_get_containers(dev);
2581         default:
2582                 return -ENOTTY;
2583         }
2584 }
2585
2586 /**
2587  *
2588  * aac_srb_callback
2589  * @context: the context set in the fib - here it is scsi cmd
2590  * @fibptr: pointer to the fib
2591  *
2592  * Handles the completion of a scsi command to a non dasd device
2593  *
2594  */
2595
2596 static void aac_srb_callback(void *context, struct fib * fibptr)
2597 {
2598         struct aac_dev *dev;
2599         struct aac_srb_reply *srbreply;
2600         struct scsi_cmnd *scsicmd;
2601
2602         scsicmd = (struct scsi_cmnd *) context;
2603
2604         if (!aac_valid_context(scsicmd, fibptr))
2605                 return;
2606
2607         BUG_ON(fibptr == NULL);
2608
2609         dev = fibptr->dev;
2610
2611         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2612
2613         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
2614         /*
2615          *      Calculate resid for sg
2616          */
2617
2618         scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2619                        - le32_to_cpu(srbreply->data_xfer_length));
2620
2621         scsi_dma_unmap(scsicmd);
2622
2623         /* expose physical device if expose_physicald flag is on */
2624         if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
2625           && expose_physicals > 0)
2626                 aac_expose_phy_device(scsicmd);
2627
2628         /*
2629          * First check the fib status
2630          */
2631
2632         if (le32_to_cpu(srbreply->status) != ST_OK){
2633                 int len;
2634                 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2635                 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
2636                             SCSI_SENSE_BUFFERSIZE);
2637                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2638                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2639         }
2640
2641         /*
2642          * Next check the srb status
2643          */
2644         switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2645         case SRB_STATUS_ERROR_RECOVERY:
2646         case SRB_STATUS_PENDING:
2647         case SRB_STATUS_SUCCESS:
2648                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2649                 break;
2650         case SRB_STATUS_DATA_OVERRUN:
2651                 switch(scsicmd->cmnd[0]){
2652                 case  READ_6:
2653                 case  WRITE_6:
2654                 case  READ_10:
2655                 case  WRITE_10:
2656                 case  READ_12:
2657                 case  WRITE_12:
2658                 case  READ_16:
2659                 case  WRITE_16:
2660                         if (le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow) {
2661                                 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2662                         } else {
2663                                 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2664                         }
2665                         scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2666                         break;
2667                 case INQUIRY: {
2668                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2669                         break;
2670                 }
2671                 default:
2672                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2673                         break;
2674                 }
2675                 break;
2676         case SRB_STATUS_ABORTED:
2677                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2678                 break;
2679         case SRB_STATUS_ABORT_FAILED:
2680                 // Not sure about this one - but assuming the hba was trying to abort for some reason
2681                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2682                 break;
2683         case SRB_STATUS_PARITY_ERROR:
2684                 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2685                 break;
2686         case SRB_STATUS_NO_DEVICE:
2687         case SRB_STATUS_INVALID_PATH_ID:
2688         case SRB_STATUS_INVALID_TARGET_ID:
2689         case SRB_STATUS_INVALID_LUN:
2690         case SRB_STATUS_SELECTION_TIMEOUT:
2691                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2692                 break;
2693
2694         case SRB_STATUS_COMMAND_TIMEOUT:
2695         case SRB_STATUS_TIMEOUT:
2696                 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2697                 break;
2698
2699         case SRB_STATUS_BUSY:
2700                 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
2701                 break;
2702
2703         case SRB_STATUS_BUS_RESET:
2704                 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2705                 break;
2706
2707         case SRB_STATUS_MESSAGE_REJECTED:
2708                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2709                 break;
2710         case SRB_STATUS_REQUEST_FLUSHED:
2711         case SRB_STATUS_ERROR:
2712         case SRB_STATUS_INVALID_REQUEST:
2713         case SRB_STATUS_REQUEST_SENSE_FAILED:
2714         case SRB_STATUS_NO_HBA:
2715         case SRB_STATUS_UNEXPECTED_BUS_FREE:
2716         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2717         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2718         case SRB_STATUS_DELAYED_RETRY:
2719         case SRB_STATUS_BAD_FUNCTION:
2720         case SRB_STATUS_NOT_STARTED:
2721         case SRB_STATUS_NOT_IN_USE:
2722         case SRB_STATUS_FORCE_ABORT:
2723         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2724         default:
2725 #ifdef AAC_DETAILED_STATUS_INFO
2726                 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2727                         le32_to_cpu(srbreply->srb_status) & 0x3F,
2728                         aac_get_status_string(
2729                                 le32_to_cpu(srbreply->srb_status) & 0x3F),
2730                         scsicmd->cmnd[0],
2731                         le32_to_cpu(srbreply->scsi_status));
2732 #endif
2733                 if ((scsicmd->cmnd[0] == ATA_12)
2734                   || (scsicmd->cmnd[0] == ATA_16)) {
2735                         if (scsicmd->cmnd[2] & (0x01 << 5)) {
2736                                 scsicmd->result = DID_OK << 16
2737                                                 | COMMAND_COMPLETE << 8;
2738                                 break;
2739                         } else {
2740                                 scsicmd->result = DID_ERROR << 16
2741                                                 | COMMAND_COMPLETE << 8;
2742                                 break;
2743                         }
2744                 } else {
2745                         scsicmd->result = DID_ERROR << 16
2746                                         | COMMAND_COMPLETE << 8;
2747                         break;
2748                 }
2749         }
2750         if (le32_to_cpu(srbreply->scsi_status) == SAM_STAT_CHECK_CONDITION) {
2751                 int len;
2752                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2753                 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
2754                             SCSI_SENSE_BUFFERSIZE);
2755 #ifdef AAC_DETAILED_STATUS_INFO
2756                 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2757                                         le32_to_cpu(srbreply->status), len);
2758 #endif
2759                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2760         }
2761         /*
2762          * OR in the scsi status (already shifted up a bit)
2763          */
2764         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2765
2766         aac_fib_complete(fibptr);
2767         aac_fib_free(fibptr);
2768         scsicmd->scsi_done(scsicmd);
2769 }
2770
2771 /**
2772  *
2773  * aac_send_scb_fib
2774  * @scsicmd: the scsi command block
2775  *
2776  * This routine will form a FIB and fill in the aac_srb from the
2777  * scsicmd passed in.
2778  */
2779
2780 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2781 {
2782         struct fib* cmd_fibcontext;
2783         struct aac_dev* dev;
2784         int status;
2785
2786         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2787         if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
2788                         scsicmd->device->lun > 7) {
2789                 scsicmd->result = DID_NO_CONNECT << 16;
2790                 scsicmd->scsi_done(scsicmd);
2791                 return 0;
2792         }
2793
2794         /*
2795          *      Allocate and initialize a Fib then setup a BlockWrite command
2796          */
2797         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
2798                 return -1;
2799         }
2800         status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
2801
2802         /*
2803          *      Check that the command queued to the controller
2804          */
2805         if (status == -EINPROGRESS) {
2806                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2807                 return 0;
2808         }
2809
2810         printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2811         aac_fib_complete(cmd_fibcontext);
2812         aac_fib_free(cmd_fibcontext);
2813
2814         return -1;
2815 }
2816
2817 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2818 {
2819         struct aac_dev *dev;
2820         unsigned long byte_count = 0;
2821         int nseg;
2822
2823         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2824         // Get rid of old data
2825         psg->count = 0;
2826         psg->sg[0].addr = 0;
2827         psg->sg[0].count = 0;
2828
2829         nseg = scsi_dma_map(scsicmd);
2830         BUG_ON(nseg < 0);
2831         if (nseg) {
2832                 struct scatterlist *sg;
2833                 int i;
2834
2835                 psg->count = cpu_to_le32(nseg);
2836
2837                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2838                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2839                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2840                         byte_count += sg_dma_len(sg);
2841                 }
2842                 /* hba wants the size to be exact */
2843                 if (byte_count > scsi_bufflen(scsicmd)) {
2844                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2845                                 (byte_count - scsi_bufflen(scsicmd));
2846                         psg->sg[i-1].count = cpu_to_le32(temp);
2847                         byte_count = scsi_bufflen(scsicmd);
2848                 }
2849                 /* Check for command underflow */
2850                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2851                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2852                                         byte_count, scsicmd->underflow);
2853                 }
2854         }
2855         return byte_count;
2856 }
2857
2858
2859 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2860 {
2861         struct aac_dev *dev;
2862         unsigned long byte_count = 0;
2863         u64 addr;
2864         int nseg;
2865
2866         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2867         // Get rid of old data
2868         psg->count = 0;
2869         psg->sg[0].addr[0] = 0;
2870         psg->sg[0].addr[1] = 0;
2871         psg->sg[0].count = 0;
2872
2873         nseg = scsi_dma_map(scsicmd);
2874         BUG_ON(nseg < 0);
2875         if (nseg) {
2876                 struct scatterlist *sg;
2877                 int i;
2878
2879                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2880                         int count = sg_dma_len(sg);
2881                         addr = sg_dma_address(sg);
2882                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2883                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
2884                         psg->sg[i].count = cpu_to_le32(count);
2885                         byte_count += count;
2886                 }
2887                 psg->count = cpu_to_le32(nseg);
2888                 /* hba wants the size to be exact */
2889                 if (byte_count > scsi_bufflen(scsicmd)) {
2890                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2891                                 (byte_count - scsi_bufflen(scsicmd));
2892                         psg->sg[i-1].count = cpu_to_le32(temp);
2893                         byte_count = scsi_bufflen(scsicmd);
2894                 }
2895                 /* Check for command underflow */
2896                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2897                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2898                                         byte_count, scsicmd->underflow);
2899                 }
2900         }
2901         return byte_count;
2902 }
2903
2904 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2905 {
2906         unsigned long byte_count = 0;
2907         int nseg;
2908
2909         // Get rid of old data
2910         psg->count = 0;
2911         psg->sg[0].next = 0;
2912         psg->sg[0].prev = 0;
2913         psg->sg[0].addr[0] = 0;
2914         psg->sg[0].addr[1] = 0;
2915         psg->sg[0].count = 0;
2916         psg->sg[0].flags = 0;
2917
2918         nseg = scsi_dma_map(scsicmd);
2919         BUG_ON(nseg < 0);
2920         if (nseg) {
2921                 struct scatterlist *sg;
2922                 int i;
2923
2924                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2925                         int count = sg_dma_len(sg);
2926                         u64 addr = sg_dma_address(sg);
2927                         psg->sg[i].next = 0;
2928                         psg->sg[i].prev = 0;
2929                         psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2930                         psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2931                         psg->sg[i].count = cpu_to_le32(count);
2932                         psg->sg[i].flags = 0;
2933                         byte_count += count;
2934                 }
2935                 psg->count = cpu_to_le32(nseg);
2936                 /* hba wants the size to be exact */
2937                 if (byte_count > scsi_bufflen(scsicmd)) {
2938                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2939                                 (byte_count - scsi_bufflen(scsicmd));
2940                         psg->sg[i-1].count = cpu_to_le32(temp);
2941                         byte_count = scsi_bufflen(scsicmd);
2942                 }
2943                 /* Check for command underflow */
2944                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2945                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2946                                         byte_count, scsicmd->underflow);
2947                 }
2948         }
2949         return byte_count;
2950 }
2951
2952 #ifdef AAC_DETAILED_STATUS_INFO
2953
2954 struct aac_srb_status_info {
2955         u32     status;
2956         char    *str;
2957 };
2958
2959
2960 static struct aac_srb_status_info srb_status_info[] = {
2961         { SRB_STATUS_PENDING,           "Pending Status"},
2962         { SRB_STATUS_SUCCESS,           "Success"},
2963         { SRB_STATUS_ABORTED,           "Aborted Command"},
2964         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
2965         { SRB_STATUS_ERROR,             "Error Event"},
2966         { SRB_STATUS_BUSY,              "Device Busy"},
2967         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
2968         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
2969         { SRB_STATUS_NO_DEVICE,         "No Device"},
2970         { SRB_STATUS_TIMEOUT,           "Timeout"},
2971         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2972         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
2973         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
2974         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
2975         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
2976         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2977         { SRB_STATUS_NO_HBA,            "No HBA"},
2978         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
2979         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2980         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2981         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2982         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
2983         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
2984         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"},
2985         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2986         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
2987         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
2988         { SRB_STATUS_NOT_STARTED,       "Not Started"},
2989         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
2990         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
2991         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2992         { 0xff,                         "Unknown Error"}
2993 };
2994
2995 char *aac_get_status_string(u32 status)
2996 {
2997         int i;
2998
2999         for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
3000                 if (srb_status_info[i].status == status)
3001                         return srb_status_info[i].str;
3002
3003         return "Bad Status Code";
3004 }
3005
3006 #endif