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