scsi: aacraid: Fix udev inquiry race condition
[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                 c = 0;
788                 if (strlen(cp) > sizeof(str->pid)) {
789                         c = cp[sizeof(str->pid)];
790                         cp[sizeof(str->pid)] = '\0';
791                 }
792                 inqstrcpy (cp, str->pid);
793
794                 kfree(cname);
795         } else {
796                 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
797
798                 inqstrcpy (mp->vname, str->vid);
799                 /* last six chars reserved for vol type */
800                 inqstrcpy (mp->model, str->pid);
801         }
802
803         if (tindex < ARRAY_SIZE(container_types)){
804                 char *findit = str->pid;
805
806                 for ( ; *findit != ' '; findit++); /* walk till we find a space */
807                 /* RAID is superfluous in the context of a RAID device */
808                 if (memcmp(findit-4, "RAID", 4) == 0)
809                         *(findit -= 4) = ' ';
810                 if (((findit - str->pid) + strlen(container_types[tindex]))
811                  < (sizeof(str->pid) + sizeof(str->prl)))
812                         inqstrcpy (container_types[tindex], findit + 1);
813         }
814         inqstrcpy ("V1.0", str->prl);
815 }
816
817 static void get_container_serial_callback(void *context, struct fib * fibptr)
818 {
819         struct aac_get_serial_resp * get_serial_reply;
820         struct scsi_cmnd * scsicmd;
821
822         BUG_ON(fibptr == NULL);
823
824         scsicmd = (struct scsi_cmnd *) context;
825         if (!aac_valid_context(scsicmd, fibptr))
826                 return;
827
828         get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
829         /* Failure is irrelevant, using default value instead */
830         if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
831                 char sp[13];
832                 /* EVPD bit set */
833                 sp[0] = INQD_PDT_DA;
834                 sp[1] = scsicmd->cmnd[2];
835                 sp[2] = 0;
836                 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
837                   le32_to_cpu(get_serial_reply->uid));
838                 scsi_sg_copy_from_buffer(scsicmd, sp, sizeof(sp));
839         }
840
841         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
842
843         aac_fib_complete(fibptr);
844         aac_fib_free(fibptr);
845         scsicmd->scsi_done(scsicmd);
846 }
847
848 /**
849  *      aac_get_container_serial - get container serial, none blocking.
850  */
851 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
852 {
853         int status;
854         struct aac_get_serial *dinfo;
855         struct fib * cmd_fibcontext;
856         struct aac_dev * dev;
857
858         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
859
860         if (!(cmd_fibcontext = aac_fib_alloc(dev)))
861                 return -ENOMEM;
862
863         aac_fib_init(cmd_fibcontext);
864         dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
865
866         dinfo->command = cpu_to_le32(VM_ContainerConfig);
867         dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
868         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
869         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
870
871         status = aac_fib_send(ContainerCommand,
872                   cmd_fibcontext,
873                   sizeof (struct aac_get_serial),
874                   FsaNormal,
875                   0, 1,
876                   (fib_callback) get_container_serial_callback,
877                   (void *) scsicmd);
878
879         /*
880          *      Check that the command queued to the controller
881          */
882         if (status == -EINPROGRESS)
883                 return 0;
884
885         printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
886         aac_fib_complete(cmd_fibcontext);
887         aac_fib_free(cmd_fibcontext);
888         return -1;
889 }
890
891 /* Function: setinqserial
892  *
893  * Arguments: [1] pointer to void [1] int
894  *
895  * Purpose: Sets SCSI Unit Serial number.
896  *          This is a fake. We should read a proper
897  *          serial number from the container. <SuSE>But
898  *          without docs it's quite hard to do it :-)
899  *          So this will have to do in the meantime.</SuSE>
900  */
901
902 static int setinqserial(struct aac_dev *dev, void *data, int cid)
903 {
904         /*
905          *      This breaks array migration.
906          */
907         return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
908                         le32_to_cpu(dev->adapter_info.serial[0]), cid);
909 }
910
911 static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
912         u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
913 {
914         u8 *sense_buf = (u8 *)sense_data;
915         /* Sense data valid, err code 70h */
916         sense_buf[0] = 0x70; /* No info field */
917         sense_buf[1] = 0;       /* Segment number, always zero */
918
919         sense_buf[2] = sense_key;       /* Sense key */
920
921         sense_buf[12] = sense_code;     /* Additional sense code */
922         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
923
924         if (sense_key == ILLEGAL_REQUEST) {
925                 sense_buf[7] = 10;      /* Additional sense length */
926
927                 sense_buf[15] = bit_pointer;
928                 /* Illegal parameter is in the parameter block */
929                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
930                         sense_buf[15] |= 0xc0;/* Std sense key specific field */
931                 /* Illegal parameter is in the CDB block */
932                 sense_buf[16] = field_pointer >> 8;     /* MSB */
933                 sense_buf[17] = field_pointer;          /* LSB */
934         } else
935                 sense_buf[7] = 6;       /* Additional sense length */
936 }
937
938 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
939 {
940         if (lba & 0xffffffff00000000LL) {
941                 int cid = scmd_id(cmd);
942                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
943                 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
944                         SAM_STAT_CHECK_CONDITION;
945                 set_sense(&dev->fsa_dev[cid].sense_data,
946                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
947                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
948                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
949                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
950                              SCSI_SENSE_BUFFERSIZE));
951                 cmd->scsi_done(cmd);
952                 return 1;
953         }
954         return 0;
955 }
956
957 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
958 {
959         return 0;
960 }
961
962 static void io_callback(void *context, struct fib * fibptr);
963
964 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
965 {
966         u16 fibsize;
967         struct aac_raw_io *readcmd;
968         aac_fib_init(fib);
969         readcmd = (struct aac_raw_io *) fib_data(fib);
970         readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
971         readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
972         readcmd->count = cpu_to_le32(count<<9);
973         readcmd->cid = cpu_to_le16(scmd_id(cmd));
974         readcmd->flags = cpu_to_le16(IO_TYPE_READ);
975         readcmd->bpTotal = 0;
976         readcmd->bpComplete = 0;
977
978         aac_build_sgraw(cmd, &readcmd->sg);
979         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
980         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
981         /*
982          *      Now send the Fib to the adapter
983          */
984         return aac_fib_send(ContainerRawIo,
985                           fib,
986                           fibsize,
987                           FsaNormal,
988                           0, 1,
989                           (fib_callback) io_callback,
990                           (void *) cmd);
991 }
992
993 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
994 {
995         u16 fibsize;
996         struct aac_read64 *readcmd;
997         aac_fib_init(fib);
998         readcmd = (struct aac_read64 *) fib_data(fib);
999         readcmd->command = cpu_to_le32(VM_CtHostRead64);
1000         readcmd->cid = cpu_to_le16(scmd_id(cmd));
1001         readcmd->sector_count = cpu_to_le16(count);
1002         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1003         readcmd->pad   = 0;
1004         readcmd->flags = 0;
1005
1006         aac_build_sg64(cmd, &readcmd->sg);
1007         fibsize = sizeof(struct aac_read64) +
1008                 ((le32_to_cpu(readcmd->sg.count) - 1) *
1009                  sizeof (struct sgentry64));
1010         BUG_ON (fibsize > (fib->dev->max_fib_size -
1011                                 sizeof(struct aac_fibhdr)));
1012         /*
1013          *      Now send the Fib to the adapter
1014          */
1015         return aac_fib_send(ContainerCommand64,
1016                           fib,
1017                           fibsize,
1018                           FsaNormal,
1019                           0, 1,
1020                           (fib_callback) io_callback,
1021                           (void *) cmd);
1022 }
1023
1024 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1025 {
1026         u16 fibsize;
1027         struct aac_read *readcmd;
1028         aac_fib_init(fib);
1029         readcmd = (struct aac_read *) fib_data(fib);
1030         readcmd->command = cpu_to_le32(VM_CtBlockRead);
1031         readcmd->cid = cpu_to_le32(scmd_id(cmd));
1032         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1033         readcmd->count = cpu_to_le32(count * 512);
1034
1035         aac_build_sg(cmd, &readcmd->sg);
1036         fibsize = sizeof(struct aac_read) +
1037                         ((le32_to_cpu(readcmd->sg.count) - 1) *
1038                          sizeof (struct sgentry));
1039         BUG_ON (fibsize > (fib->dev->max_fib_size -
1040                                 sizeof(struct aac_fibhdr)));
1041         /*
1042          *      Now send the Fib to the adapter
1043          */
1044         return aac_fib_send(ContainerCommand,
1045                           fib,
1046                           fibsize,
1047                           FsaNormal,
1048                           0, 1,
1049                           (fib_callback) io_callback,
1050                           (void *) cmd);
1051 }
1052
1053 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1054 {
1055         u16 fibsize;
1056         struct aac_raw_io *writecmd;
1057         aac_fib_init(fib);
1058         writecmd = (struct aac_raw_io *) fib_data(fib);
1059         writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1060         writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1061         writecmd->count = cpu_to_le32(count<<9);
1062         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1063         writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1064           (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1065                 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
1066                 cpu_to_le16(IO_TYPE_WRITE);
1067         writecmd->bpTotal = 0;
1068         writecmd->bpComplete = 0;
1069
1070         aac_build_sgraw(cmd, &writecmd->sg);
1071         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1072         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1073         /*
1074          *      Now send the Fib to the adapter
1075          */
1076         return aac_fib_send(ContainerRawIo,
1077                           fib,
1078                           fibsize,
1079                           FsaNormal,
1080                           0, 1,
1081                           (fib_callback) io_callback,
1082                           (void *) cmd);
1083 }
1084
1085 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1086 {
1087         u16 fibsize;
1088         struct aac_write64 *writecmd;
1089         aac_fib_init(fib);
1090         writecmd = (struct aac_write64 *) fib_data(fib);
1091         writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1092         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1093         writecmd->sector_count = cpu_to_le16(count);
1094         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1095         writecmd->pad   = 0;
1096         writecmd->flags = 0;
1097
1098         aac_build_sg64(cmd, &writecmd->sg);
1099         fibsize = sizeof(struct aac_write64) +
1100                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1101                  sizeof (struct sgentry64));
1102         BUG_ON (fibsize > (fib->dev->max_fib_size -
1103                                 sizeof(struct aac_fibhdr)));
1104         /*
1105          *      Now send the Fib to the adapter
1106          */
1107         return aac_fib_send(ContainerCommand64,
1108                           fib,
1109                           fibsize,
1110                           FsaNormal,
1111                           0, 1,
1112                           (fib_callback) io_callback,
1113                           (void *) cmd);
1114 }
1115
1116 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1117 {
1118         u16 fibsize;
1119         struct aac_write *writecmd;
1120         aac_fib_init(fib);
1121         writecmd = (struct aac_write *) fib_data(fib);
1122         writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1123         writecmd->cid = cpu_to_le32(scmd_id(cmd));
1124         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1125         writecmd->count = cpu_to_le32(count * 512);
1126         writecmd->sg.count = cpu_to_le32(1);
1127         /* ->stable is not used - it did mean which type of write */
1128
1129         aac_build_sg(cmd, &writecmd->sg);
1130         fibsize = sizeof(struct aac_write) +
1131                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1132                  sizeof (struct sgentry));
1133         BUG_ON (fibsize > (fib->dev->max_fib_size -
1134                                 sizeof(struct aac_fibhdr)));
1135         /*
1136          *      Now send the Fib to the adapter
1137          */
1138         return aac_fib_send(ContainerCommand,
1139                           fib,
1140                           fibsize,
1141                           FsaNormal,
1142                           0, 1,
1143                           (fib_callback) io_callback,
1144                           (void *) cmd);
1145 }
1146
1147 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1148 {
1149         struct aac_srb * srbcmd;
1150         u32 flag;
1151         u32 timeout;
1152
1153         aac_fib_init(fib);
1154         switch(cmd->sc_data_direction){
1155         case DMA_TO_DEVICE:
1156                 flag = SRB_DataOut;
1157                 break;
1158         case DMA_BIDIRECTIONAL:
1159                 flag = SRB_DataIn | SRB_DataOut;
1160                 break;
1161         case DMA_FROM_DEVICE:
1162                 flag = SRB_DataIn;
1163                 break;
1164         case DMA_NONE:
1165         default:        /* shuts up some versions of gcc */
1166                 flag = SRB_NoDataXfer;
1167                 break;
1168         }
1169
1170         srbcmd = (struct aac_srb*) fib_data(fib);
1171         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1172         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1173         srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1174         srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1175         srbcmd->flags    = cpu_to_le32(flag);
1176         timeout = cmd->request->timeout/HZ;
1177         if (timeout == 0)
1178                 timeout = 1;
1179         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1180         srbcmd->retry_limit = 0; /* Obsolete parameter */
1181         srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1182         return srbcmd;
1183 }
1184
1185 static void aac_srb_callback(void *context, struct fib * fibptr);
1186
1187 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1188 {
1189         u16 fibsize;
1190         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1191
1192         aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
1193         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1194
1195         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1196         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1197         /*
1198          *      Build Scatter/Gather list
1199          */
1200         fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1201                 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1202                  sizeof (struct sgentry64));
1203         BUG_ON (fibsize > (fib->dev->max_fib_size -
1204                                 sizeof(struct aac_fibhdr)));
1205
1206         /*
1207          *      Now send the Fib to the adapter
1208          */
1209         return aac_fib_send(ScsiPortCommand64, fib,
1210                                 fibsize, FsaNormal, 0, 1,
1211                                   (fib_callback) aac_srb_callback,
1212                                   (void *) cmd);
1213 }
1214
1215 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1216 {
1217         u16 fibsize;
1218         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1219
1220         aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
1221         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1222
1223         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1224         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1225         /*
1226          *      Build Scatter/Gather list
1227          */
1228         fibsize = sizeof (struct aac_srb) +
1229                 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1230                  sizeof (struct sgentry));
1231         BUG_ON (fibsize > (fib->dev->max_fib_size -
1232                                 sizeof(struct aac_fibhdr)));
1233
1234         /*
1235          *      Now send the Fib to the adapter
1236          */
1237         return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1238                                   (fib_callback) aac_srb_callback, (void *) cmd);
1239 }
1240
1241 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1242 {
1243         if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1244             (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1245                 return FAILED;
1246         return aac_scsi_32(fib, cmd);
1247 }
1248
1249 int aac_get_adapter_info(struct aac_dev* dev)
1250 {
1251         struct fib* fibptr;
1252         int rcode;
1253         u32 tmp;
1254         struct aac_adapter_info *info;
1255         struct aac_bus_info *command;
1256         struct aac_bus_info_response *bus_info;
1257
1258         if (!(fibptr = aac_fib_alloc(dev)))
1259                 return -ENOMEM;
1260
1261         aac_fib_init(fibptr);
1262         info = (struct aac_adapter_info *) fib_data(fibptr);
1263         memset(info,0,sizeof(*info));
1264
1265         rcode = aac_fib_send(RequestAdapterInfo,
1266                          fibptr,
1267                          sizeof(*info),
1268                          FsaNormal,
1269                          -1, 1, /* First `interrupt' command uses special wait */
1270                          NULL,
1271                          NULL);
1272
1273         if (rcode < 0) {
1274                 /* FIB should be freed only after
1275                  * getting the response from the F/W */
1276                 if (rcode != -ERESTARTSYS) {
1277                         aac_fib_complete(fibptr);
1278                         aac_fib_free(fibptr);
1279                 }
1280                 return rcode;
1281         }
1282         memcpy(&dev->adapter_info, info, sizeof(*info));
1283
1284         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1285                 struct aac_supplement_adapter_info * sinfo;
1286
1287                 aac_fib_init(fibptr);
1288
1289                 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1290
1291                 memset(sinfo,0,sizeof(*sinfo));
1292
1293                 rcode = aac_fib_send(RequestSupplementAdapterInfo,
1294                                  fibptr,
1295                                  sizeof(*sinfo),
1296                                  FsaNormal,
1297                                  1, 1,
1298                                  NULL,
1299                                  NULL);
1300
1301                 if (rcode >= 0)
1302                         memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
1303                 if (rcode == -ERESTARTSYS) {
1304                         fibptr = aac_fib_alloc(dev);
1305                         if (!fibptr)
1306                                 return -ENOMEM;
1307                 }
1308
1309         }
1310
1311
1312         /*
1313          * GetBusInfo
1314          */
1315
1316         aac_fib_init(fibptr);
1317
1318         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1319
1320         memset(bus_info, 0, sizeof(*bus_info));
1321
1322         command = (struct aac_bus_info *)bus_info;
1323
1324         command->Command = cpu_to_le32(VM_Ioctl);
1325         command->ObjType = cpu_to_le32(FT_DRIVE);
1326         command->MethodId = cpu_to_le32(1);
1327         command->CtlCmd = cpu_to_le32(GetBusInfo);
1328
1329         rcode = aac_fib_send(ContainerCommand,
1330                          fibptr,
1331                          sizeof (*bus_info),
1332                          FsaNormal,
1333                          1, 1,
1334                          NULL, NULL);
1335
1336         /* reasoned default */
1337         dev->maximum_num_physicals = 16;
1338         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1339                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1340                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1341         }
1342
1343         if (!dev->in_reset) {
1344                 char buffer[16];
1345                 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1346                 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1347                         dev->name,
1348                         dev->id,
1349                         tmp>>24,
1350                         (tmp>>16)&0xff,
1351                         tmp&0xff,
1352                         le32_to_cpu(dev->adapter_info.kernelbuild),
1353                         (int)sizeof(dev->supplement_adapter_info.BuildDate),
1354                         dev->supplement_adapter_info.BuildDate);
1355                 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1356                 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1357                         dev->name, dev->id,
1358                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1359                         le32_to_cpu(dev->adapter_info.monitorbuild));
1360                 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1361                 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
1362                         dev->name, dev->id,
1363                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1364                         le32_to_cpu(dev->adapter_info.biosbuild));
1365                 buffer[0] = '\0';
1366                 if (aac_get_serial_number(
1367                   shost_to_class(dev->scsi_host_ptr), buffer))
1368                         printk(KERN_INFO "%s%d: serial %s",
1369                           dev->name, dev->id, buffer);
1370                 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1371                         printk(KERN_INFO "%s%d: TSID %.*s\n",
1372                           dev->name, dev->id,
1373                           (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1374                           dev->supplement_adapter_info.VpdInfo.Tsid);
1375                 }
1376                 if (!aac_check_reset || ((aac_check_reset == 1) &&
1377                   (dev->supplement_adapter_info.SupportedOptions2 &
1378                   AAC_OPTION_IGNORE_RESET))) {
1379                         printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1380                           dev->name, dev->id);
1381                 }
1382         }
1383
1384         dev->cache_protected = 0;
1385         dev->jbod = ((dev->supplement_adapter_info.FeatureBits &
1386                 AAC_FEATURE_JBOD) != 0);
1387         dev->nondasd_support = 0;
1388         dev->raid_scsi_mode = 0;
1389         if(dev->adapter_info.options & AAC_OPT_NONDASD)
1390                 dev->nondasd_support = 1;
1391
1392         /*
1393          * If the firmware supports ROMB RAID/SCSI mode and we are currently
1394          * in RAID/SCSI mode, set the flag. For now if in this mode we will
1395          * force nondasd support on. If we decide to allow the non-dasd flag
1396          * additional changes changes will have to be made to support
1397          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
1398          * changed to support the new dev->raid_scsi_mode flag instead of
1399          * leaching off of the dev->nondasd_support flag. Also in linit.c the
1400          * function aac_detect will have to be modified where it sets up the
1401          * max number of channels based on the aac->nondasd_support flag only.
1402          */
1403         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1404             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1405                 dev->nondasd_support = 1;
1406                 dev->raid_scsi_mode = 1;
1407         }
1408         if (dev->raid_scsi_mode != 0)
1409                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1410                                 dev->name, dev->id);
1411
1412         if (nondasd != -1)
1413                 dev->nondasd_support = (nondasd!=0);
1414         if (dev->nondasd_support && !dev->in_reset)
1415                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1416
1417         if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
1418                 dev->needs_dac = 1;
1419         dev->dac_support = 0;
1420         if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
1421             (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
1422                 if (!dev->in_reset)
1423                         printk(KERN_INFO "%s%d: 64bit support enabled.\n",
1424                                 dev->name, dev->id);
1425                 dev->dac_support = 1;
1426         }
1427
1428         if(dacmode != -1) {
1429                 dev->dac_support = (dacmode!=0);
1430         }
1431
1432         /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
1433         if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
1434                 & AAC_QUIRK_SCSI_32)) {
1435                 dev->nondasd_support = 0;
1436                 dev->jbod = 0;
1437                 expose_physicals = 0;
1438         }
1439
1440         if(dev->dac_support != 0) {
1441                 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64)) &&
1442                         !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
1443                         if (!dev->in_reset)
1444                                 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1445                                         dev->name, dev->id);
1446                 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32)) &&
1447                         !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
1448                         printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1449                                 dev->name, dev->id);
1450                         dev->dac_support = 0;
1451                 } else {
1452                         printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1453                                 dev->name, dev->id);
1454                         rcode = -ENOMEM;
1455                 }
1456         }
1457         /*
1458          * Deal with configuring for the individualized limits of each packet
1459          * interface.
1460          */
1461         dev->a_ops.adapter_scsi = (dev->dac_support)
1462           ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1463                                 ? aac_scsi_32_64
1464                                 : aac_scsi_64)
1465                                 : aac_scsi_32;
1466         if (dev->raw_io_interface) {
1467                 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1468                                         ? aac_bounds_64
1469                                         : aac_bounds_32;
1470                 dev->a_ops.adapter_read = aac_read_raw_io;
1471                 dev->a_ops.adapter_write = aac_write_raw_io;
1472         } else {
1473                 dev->a_ops.adapter_bounds = aac_bounds_32;
1474                 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
1475                         sizeof(struct aac_fibhdr) -
1476                         sizeof(struct aac_write) + sizeof(struct sgentry)) /
1477                                 sizeof(struct sgentry);
1478                 if (dev->dac_support) {
1479                         dev->a_ops.adapter_read = aac_read_block64;
1480                         dev->a_ops.adapter_write = aac_write_block64;
1481                         /*
1482                          * 38 scatter gather elements
1483                          */
1484                         dev->scsi_host_ptr->sg_tablesize =
1485                                 (dev->max_fib_size -
1486                                 sizeof(struct aac_fibhdr) -
1487                                 sizeof(struct aac_write64) +
1488                                 sizeof(struct sgentry64)) /
1489                                         sizeof(struct sgentry64);
1490                 } else {
1491                         dev->a_ops.adapter_read = aac_read_block;
1492                         dev->a_ops.adapter_write = aac_write_block;
1493                 }
1494                 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1495                 if (dev->adapter_info.options & AAC_OPT_NEW_COMM_TYPE1)
1496                         dev->adapter_info.options |= AAC_OPT_NEW_COMM;
1497                 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1498                         /*
1499                          * Worst case size that could cause sg overflow when
1500                          * we break up SG elements that are larger than 64KB.
1501                          * Would be nice if we could tell the SCSI layer what
1502                          * the maximum SG element size can be. Worst case is
1503                          * (sg_tablesize-1) 4KB elements with one 64KB
1504                          * element.
1505                          *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
1506                          */
1507                         dev->scsi_host_ptr->max_sectors =
1508                           (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1509                 }
1510         }
1511         /* FIB should be freed only after getting the response from the F/W */
1512         if (rcode != -ERESTARTSYS) {
1513                 aac_fib_complete(fibptr);
1514                 aac_fib_free(fibptr);
1515         }
1516
1517         return rcode;
1518 }
1519
1520
1521 static void io_callback(void *context, struct fib * fibptr)
1522 {
1523         struct aac_dev *dev;
1524         struct aac_read_reply *readreply;
1525         struct scsi_cmnd *scsicmd;
1526         u32 cid;
1527
1528         scsicmd = (struct scsi_cmnd *) context;
1529
1530         if (!aac_valid_context(scsicmd, fibptr))
1531                 return;
1532
1533         dev = fibptr->dev;
1534         cid = scmd_id(scsicmd);
1535
1536         if (nblank(dprintk(x))) {
1537                 u64 lba;
1538                 switch (scsicmd->cmnd[0]) {
1539                 case WRITE_6:
1540                 case READ_6:
1541                         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1542                             (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1543                         break;
1544                 case WRITE_16:
1545                 case READ_16:
1546                         lba = ((u64)scsicmd->cmnd[2] << 56) |
1547                               ((u64)scsicmd->cmnd[3] << 48) |
1548                               ((u64)scsicmd->cmnd[4] << 40) |
1549                               ((u64)scsicmd->cmnd[5] << 32) |
1550                               ((u64)scsicmd->cmnd[6] << 24) |
1551                               (scsicmd->cmnd[7] << 16) |
1552                               (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1553                         break;
1554                 case WRITE_12:
1555                 case READ_12:
1556                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1557                               (scsicmd->cmnd[3] << 16) |
1558                               (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1559                         break;
1560                 default:
1561                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1562                                (scsicmd->cmnd[3] << 16) |
1563                                (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1564                         break;
1565                 }
1566                 printk(KERN_DEBUG
1567                   "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1568                   smp_processor_id(), (unsigned long long)lba, jiffies);
1569         }
1570
1571         BUG_ON(fibptr == NULL);
1572
1573         scsi_dma_unmap(scsicmd);
1574
1575         readreply = (struct aac_read_reply *)fib_data(fibptr);
1576         switch (le32_to_cpu(readreply->status)) {
1577         case ST_OK:
1578                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1579                         SAM_STAT_GOOD;
1580                 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
1581                 break;
1582         case ST_NOT_READY:
1583                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1584                         SAM_STAT_CHECK_CONDITION;
1585                 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
1586                   SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
1587                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1588                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1589                              SCSI_SENSE_BUFFERSIZE));
1590                 break;
1591         default:
1592 #ifdef AAC_DETAILED_STATUS_INFO
1593                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
1594                   le32_to_cpu(readreply->status));
1595 #endif
1596                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1597                         SAM_STAT_CHECK_CONDITION;
1598                 set_sense(&dev->fsa_dev[cid].sense_data,
1599                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1600                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1601                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1602                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1603                              SCSI_SENSE_BUFFERSIZE));
1604                 break;
1605         }
1606         aac_fib_complete(fibptr);
1607         aac_fib_free(fibptr);
1608
1609         scsicmd->scsi_done(scsicmd);
1610 }
1611
1612 static int aac_read(struct scsi_cmnd * scsicmd)
1613 {
1614         u64 lba;
1615         u32 count;
1616         int status;
1617         struct aac_dev *dev;
1618         struct fib * cmd_fibcontext;
1619         int cid;
1620
1621         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1622         /*
1623          *      Get block address and transfer length
1624          */
1625         switch (scsicmd->cmnd[0]) {
1626         case READ_6:
1627                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
1628
1629                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1630                         (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1631                 count = scsicmd->cmnd[4];
1632
1633                 if (count == 0)
1634                         count = 256;
1635                 break;
1636         case READ_16:
1637                 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
1638
1639                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1640                         ((u64)scsicmd->cmnd[3] << 48) |
1641                         ((u64)scsicmd->cmnd[4] << 40) |
1642                         ((u64)scsicmd->cmnd[5] << 32) |
1643                         ((u64)scsicmd->cmnd[6] << 24) |
1644                         (scsicmd->cmnd[7] << 16) |
1645                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1646                 count = (scsicmd->cmnd[10] << 24) |
1647                         (scsicmd->cmnd[11] << 16) |
1648                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1649                 break;
1650         case READ_12:
1651                 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
1652
1653                 lba = ((u64)scsicmd->cmnd[2] << 24) |
1654                         (scsicmd->cmnd[3] << 16) |
1655                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1656                 count = (scsicmd->cmnd[6] << 24) |
1657                         (scsicmd->cmnd[7] << 16) |
1658                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1659                 break;
1660         default:
1661                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
1662
1663                 lba = ((u64)scsicmd->cmnd[2] << 24) |
1664                         (scsicmd->cmnd[3] << 16) |
1665                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1666                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1667                 break;
1668         }
1669
1670         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1671                 cid = scmd_id(scsicmd);
1672                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1673                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1674                         SAM_STAT_CHECK_CONDITION;
1675                 set_sense(&dev->fsa_dev[cid].sense_data,
1676                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1677                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1678                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1679                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1680                              SCSI_SENSE_BUFFERSIZE));
1681                 scsicmd->scsi_done(scsicmd);
1682                 return 1;
1683         }
1684
1685         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
1686           smp_processor_id(), (unsigned long long)lba, jiffies));
1687         if (aac_adapter_bounds(dev,scsicmd,lba))
1688                 return 0;
1689         /*
1690          *      Alocate and initialize a Fib
1691          */
1692         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1693                 printk(KERN_WARNING "aac_read: fib allocation failed\n");
1694                 return -1;
1695         }
1696         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1697         status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
1698
1699         /*
1700          *      Check that the command queued to the controller
1701          */
1702         if (status == -EINPROGRESS)
1703                 return 0;
1704
1705         printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
1706         /*
1707          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1708          */
1709         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1710         scsicmd->scsi_done(scsicmd);
1711         aac_fib_complete(cmd_fibcontext);
1712         aac_fib_free(cmd_fibcontext);
1713         return 0;
1714 }
1715
1716 static int aac_write(struct scsi_cmnd * scsicmd)
1717 {
1718         u64 lba;
1719         u32 count;
1720         int fua;
1721         int status;
1722         struct aac_dev *dev;
1723         struct fib * cmd_fibcontext;
1724         int cid;
1725
1726         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1727         /*
1728          *      Get block address and transfer length
1729          */
1730         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
1731         {
1732                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1733                 count = scsicmd->cmnd[4];
1734                 if (count == 0)
1735                         count = 256;
1736                 fua = 0;
1737         } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1738                 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
1739
1740                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1741                         ((u64)scsicmd->cmnd[3] << 48) |
1742                         ((u64)scsicmd->cmnd[4] << 40) |
1743                         ((u64)scsicmd->cmnd[5] << 32) |
1744                         ((u64)scsicmd->cmnd[6] << 24) |
1745                         (scsicmd->cmnd[7] << 16) |
1746                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1747                 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1748                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1749                 fua = scsicmd->cmnd[1] & 0x8;
1750         } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1751                 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
1752
1753                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1754                     | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1755                 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1756                       | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1757                 fua = scsicmd->cmnd[1] & 0x8;
1758         } else {
1759                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
1760                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1761                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1762                 fua = scsicmd->cmnd[1] & 0x8;
1763         }
1764
1765         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1766                 cid = scmd_id(scsicmd);
1767                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1768                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1769                         SAM_STAT_CHECK_CONDITION;
1770                 set_sense(&dev->fsa_dev[cid].sense_data,
1771                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1772                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1773                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1774                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1775                              SCSI_SENSE_BUFFERSIZE));
1776                 scsicmd->scsi_done(scsicmd);
1777                 return 1;
1778         }
1779
1780         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
1781           smp_processor_id(), (unsigned long long)lba, jiffies));
1782         if (aac_adapter_bounds(dev,scsicmd,lba))
1783                 return 0;
1784         /*
1785          *      Allocate and initialize a Fib then setup a BlockWrite command
1786          */
1787         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1788                 /* FIB temporarily unavailable,not catastrophic failure */
1789
1790                 /* scsicmd->result = DID_ERROR << 16;
1791                  * scsicmd->scsi_done(scsicmd);
1792                  * return 0;
1793                  */
1794                 printk(KERN_WARNING "aac_write: fib allocation failed\n");
1795                 return -1;
1796         }
1797         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1798         status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
1799
1800         /*
1801          *      Check that the command queued to the controller
1802          */
1803         if (status == -EINPROGRESS)
1804                 return 0;
1805
1806         printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
1807         /*
1808          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1809          */
1810         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1811         scsicmd->scsi_done(scsicmd);
1812
1813         aac_fib_complete(cmd_fibcontext);
1814         aac_fib_free(cmd_fibcontext);
1815         return 0;
1816 }
1817
1818 static void synchronize_callback(void *context, struct fib *fibptr)
1819 {
1820         struct aac_synchronize_reply *synchronizereply;
1821         struct scsi_cmnd *cmd;
1822
1823         cmd = context;
1824
1825         if (!aac_valid_context(cmd, fibptr))
1826                 return;
1827
1828         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1829                                 smp_processor_id(), jiffies));
1830         BUG_ON(fibptr == NULL);
1831
1832
1833         synchronizereply = fib_data(fibptr);
1834         if (le32_to_cpu(synchronizereply->status) == CT_OK)
1835                 cmd->result = DID_OK << 16 |
1836                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1837         else {
1838                 struct scsi_device *sdev = cmd->device;
1839                 struct aac_dev *dev = fibptr->dev;
1840                 u32 cid = sdev_id(sdev);
1841                 printk(KERN_WARNING
1842                      "synchronize_callback: synchronize failed, status = %d\n",
1843                      le32_to_cpu(synchronizereply->status));
1844                 cmd->result = DID_OK << 16 |
1845                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1846                 set_sense(&dev->fsa_dev[cid].sense_data,
1847                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1848                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1849                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1850                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1851                              SCSI_SENSE_BUFFERSIZE));
1852         }
1853
1854         aac_fib_complete(fibptr);
1855         aac_fib_free(fibptr);
1856         cmd->scsi_done(cmd);
1857 }
1858
1859 static int aac_synchronize(struct scsi_cmnd *scsicmd)
1860 {
1861         int status;
1862         struct fib *cmd_fibcontext;
1863         struct aac_synchronize *synchronizecmd;
1864         struct scsi_cmnd *cmd;
1865         struct scsi_device *sdev = scsicmd->device;
1866         int active = 0;
1867         struct aac_dev *aac;
1868         u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
1869                 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1870         u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1871         unsigned long flags;
1872
1873         /*
1874          * Wait for all outstanding queued commands to complete to this
1875          * specific target (block).
1876          */
1877         spin_lock_irqsave(&sdev->list_lock, flags);
1878         list_for_each_entry(cmd, &sdev->cmd_list, list)
1879                 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1880                         u64 cmnd_lba;
1881                         u32 cmnd_count;
1882
1883                         if (cmd->cmnd[0] == WRITE_6) {
1884                                 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
1885                                         (cmd->cmnd[2] << 8) |
1886                                         cmd->cmnd[3];
1887                                 cmnd_count = cmd->cmnd[4];
1888                                 if (cmnd_count == 0)
1889                                         cmnd_count = 256;
1890                         } else if (cmd->cmnd[0] == WRITE_16) {
1891                                 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
1892                                         ((u64)cmd->cmnd[3] << 48) |
1893                                         ((u64)cmd->cmnd[4] << 40) |
1894                                         ((u64)cmd->cmnd[5] << 32) |
1895                                         ((u64)cmd->cmnd[6] << 24) |
1896                                         (cmd->cmnd[7] << 16) |
1897                                         (cmd->cmnd[8] << 8) |
1898                                         cmd->cmnd[9];
1899                                 cmnd_count = (cmd->cmnd[10] << 24) |
1900                                         (cmd->cmnd[11] << 16) |
1901                                         (cmd->cmnd[12] << 8) |
1902                                         cmd->cmnd[13];
1903                         } else if (cmd->cmnd[0] == WRITE_12) {
1904                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1905                                         (cmd->cmnd[3] << 16) |
1906                                         (cmd->cmnd[4] << 8) |
1907                                         cmd->cmnd[5];
1908                                 cmnd_count = (cmd->cmnd[6] << 24) |
1909                                         (cmd->cmnd[7] << 16) |
1910                                         (cmd->cmnd[8] << 8) |
1911                                         cmd->cmnd[9];
1912                         } else if (cmd->cmnd[0] == WRITE_10) {
1913                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1914                                         (cmd->cmnd[3] << 16) |
1915                                         (cmd->cmnd[4] << 8) |
1916                                         cmd->cmnd[5];
1917                                 cmnd_count = (cmd->cmnd[7] << 8) |
1918                                         cmd->cmnd[8];
1919                         } else
1920                                 continue;
1921                         if (((cmnd_lba + cmnd_count) < lba) ||
1922                           (count && ((lba + count) < cmnd_lba)))
1923                                 continue;
1924                         ++active;
1925                         break;
1926                 }
1927
1928         spin_unlock_irqrestore(&sdev->list_lock, flags);
1929
1930         /*
1931          *      Yield the processor (requeue for later)
1932          */
1933         if (active)
1934                 return SCSI_MLQUEUE_DEVICE_BUSY;
1935
1936         aac = (struct aac_dev *)sdev->host->hostdata;
1937         if (aac->in_reset)
1938                 return SCSI_MLQUEUE_HOST_BUSY;
1939
1940         /*
1941          *      Allocate and initialize a Fib
1942          */
1943         if (!(cmd_fibcontext = aac_fib_alloc(aac)))
1944                 return SCSI_MLQUEUE_HOST_BUSY;
1945
1946         aac_fib_init(cmd_fibcontext);
1947
1948         synchronizecmd = fib_data(cmd_fibcontext);
1949         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1950         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1951         synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
1952         synchronizecmd->count =
1953              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1954         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1955
1956         /*
1957          *      Now send the Fib to the adapter
1958          */
1959         status = aac_fib_send(ContainerCommand,
1960                   cmd_fibcontext,
1961                   sizeof(struct aac_synchronize),
1962                   FsaNormal,
1963                   0, 1,
1964                   (fib_callback)synchronize_callback,
1965                   (void *)scsicmd);
1966
1967         /*
1968          *      Check that the command queued to the controller
1969          */
1970         if (status == -EINPROGRESS)
1971                 return 0;
1972
1973         printk(KERN_WARNING
1974                 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1975         aac_fib_complete(cmd_fibcontext);
1976         aac_fib_free(cmd_fibcontext);
1977         return SCSI_MLQUEUE_HOST_BUSY;
1978 }
1979
1980 static void aac_start_stop_callback(void *context, struct fib *fibptr)
1981 {
1982         struct scsi_cmnd *scsicmd = context;
1983
1984         if (!aac_valid_context(scsicmd, fibptr))
1985                 return;
1986
1987         BUG_ON(fibptr == NULL);
1988
1989         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1990
1991         aac_fib_complete(fibptr);
1992         aac_fib_free(fibptr);
1993         scsicmd->scsi_done(scsicmd);
1994 }
1995
1996 static int aac_start_stop(struct scsi_cmnd *scsicmd)
1997 {
1998         int status;
1999         struct fib *cmd_fibcontext;
2000         struct aac_power_management *pmcmd;
2001         struct scsi_device *sdev = scsicmd->device;
2002         struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2003
2004         if (!(aac->supplement_adapter_info.SupportedOptions2 &
2005               AAC_OPTION_POWER_MANAGEMENT)) {
2006                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2007                                   SAM_STAT_GOOD;
2008                 scsicmd->scsi_done(scsicmd);
2009                 return 0;
2010         }
2011
2012         if (aac->in_reset)
2013                 return SCSI_MLQUEUE_HOST_BUSY;
2014
2015         /*
2016          *      Allocate and initialize a Fib
2017          */
2018         cmd_fibcontext = aac_fib_alloc(aac);
2019         if (!cmd_fibcontext)
2020                 return SCSI_MLQUEUE_HOST_BUSY;
2021
2022         aac_fib_init(cmd_fibcontext);
2023
2024         pmcmd = fib_data(cmd_fibcontext);
2025         pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2026         pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2027         /* Eject bit ignored, not relevant */
2028         pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2029                 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2030         pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2031         pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2032                 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2033         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2034
2035         /*
2036          *      Now send the Fib to the adapter
2037          */
2038         status = aac_fib_send(ContainerCommand,
2039                   cmd_fibcontext,
2040                   sizeof(struct aac_power_management),
2041                   FsaNormal,
2042                   0, 1,
2043                   (fib_callback)aac_start_stop_callback,
2044                   (void *)scsicmd);
2045
2046         /*
2047          *      Check that the command queued to the controller
2048          */
2049         if (status == -EINPROGRESS)
2050                 return 0;
2051
2052         aac_fib_complete(cmd_fibcontext);
2053         aac_fib_free(cmd_fibcontext);
2054         return SCSI_MLQUEUE_HOST_BUSY;
2055 }
2056
2057 /**
2058  *      aac_scsi_cmd()          -       Process SCSI command
2059  *      @scsicmd:               SCSI command block
2060  *
2061  *      Emulate a SCSI command and queue the required request for the
2062  *      aacraid firmware.
2063  */
2064
2065 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2066 {
2067         u32 cid;
2068         struct Scsi_Host *host = scsicmd->device->host;
2069         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2070         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2071
2072         if (fsa_dev_ptr == NULL)
2073                 return -1;
2074         /*
2075          *      If the bus, id or lun is out of range, return fail
2076          *      Test does not apply to ID 16, the pseudo id for the controller
2077          *      itself.
2078          */
2079         cid = scmd_id(scsicmd);
2080         if (cid != host->this_id) {
2081                 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2082                         if((cid >= dev->maximum_num_containers) ||
2083                                         (scsicmd->device->lun != 0)) {
2084                                 scsicmd->result = DID_NO_CONNECT << 16;
2085                                 scsicmd->scsi_done(scsicmd);
2086                                 return 0;
2087                         }
2088
2089                         /*
2090                          *      If the target container doesn't exist, it may have
2091                          *      been newly created
2092                          */
2093                         if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2094                           (fsa_dev_ptr[cid].sense_data.sense_key ==
2095                            NOT_READY)) {
2096                                 switch (scsicmd->cmnd[0]) {
2097                                 case SERVICE_ACTION_IN:
2098                                         if (!(dev->raw_io_interface) ||
2099                                             !(dev->raw_io_64) ||
2100                                             ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2101                                                 break;
2102                                 case INQUIRY:
2103                                 case READ_CAPACITY:
2104                                 case TEST_UNIT_READY:
2105                                         if (dev->in_reset)
2106                                                 return -1;
2107                                         return _aac_probe_container(scsicmd,
2108                                                         aac_probe_container_callback2);
2109                                 default:
2110                                         break;
2111                                 }
2112                         }
2113                 } else {  /* check for physical non-dasd devices */
2114                         if (dev->nondasd_support || expose_physicals ||
2115                                         dev->jbod) {
2116                                 if (dev->in_reset)
2117                                         return -1;
2118                                 return aac_send_srb_fib(scsicmd);
2119                         } else {
2120                                 scsicmd->result = DID_NO_CONNECT << 16;
2121                                 scsicmd->scsi_done(scsicmd);
2122                                 return 0;
2123                         }
2124                 }
2125         }
2126         /*
2127          * else Command for the controller itself
2128          */
2129         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
2130                 (scsicmd->cmnd[0] != TEST_UNIT_READY))
2131         {
2132                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2133                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2134                 set_sense(&dev->fsa_dev[cid].sense_data,
2135                   ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2136                   ASENCODE_INVALID_COMMAND, 0, 0);
2137                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2138                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2139                              SCSI_SENSE_BUFFERSIZE));
2140                 scsicmd->scsi_done(scsicmd);
2141                 return 0;
2142         }
2143
2144
2145         /* Handle commands here that don't really require going out to the adapter */
2146         switch (scsicmd->cmnd[0]) {
2147         case INQUIRY:
2148         {
2149                 struct inquiry_data inq_data;
2150
2151                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2152                 memset(&inq_data, 0, sizeof (struct inquiry_data));
2153
2154                 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2155                         char *arr = (char *)&inq_data;
2156
2157                         /* EVPD bit set */
2158                         arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2159                           INQD_PDT_PROC : INQD_PDT_DA;
2160                         if (scsicmd->cmnd[2] == 0) {
2161                                 /* supported vital product data pages */
2162                                 arr[3] = 2;
2163                                 arr[4] = 0x0;
2164                                 arr[5] = 0x80;
2165                                 arr[1] = scsicmd->cmnd[2];
2166                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2167                                                          sizeof(inq_data));
2168                                 scsicmd->result = DID_OK << 16 |
2169                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2170                         } else if (scsicmd->cmnd[2] == 0x80) {
2171                                 /* unit serial number page */
2172                                 arr[3] = setinqserial(dev, &arr[4],
2173                                   scmd_id(scsicmd));
2174                                 arr[1] = scsicmd->cmnd[2];
2175                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2176                                                          sizeof(inq_data));
2177                                 if (aac_wwn != 2)
2178                                         return aac_get_container_serial(
2179                                                 scsicmd);
2180                                 /* SLES 10 SP1 special */
2181                                 scsicmd->result = DID_OK << 16 |
2182                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2183                         } else {
2184                                 /* vpd page not implemented */
2185                                 scsicmd->result = DID_OK << 16 |
2186                                   COMMAND_COMPLETE << 8 |
2187                                   SAM_STAT_CHECK_CONDITION;
2188                                 set_sense(&dev->fsa_dev[cid].sense_data,
2189                                   ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2190                                   ASENCODE_NO_SENSE, 7, 2);
2191                                 memcpy(scsicmd->sense_buffer,
2192                                   &dev->fsa_dev[cid].sense_data,
2193                                   min_t(size_t,
2194                                         sizeof(dev->fsa_dev[cid].sense_data),
2195                                         SCSI_SENSE_BUFFERSIZE));
2196                         }
2197                         scsicmd->scsi_done(scsicmd);
2198                         return 0;
2199                 }
2200                 inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
2201                 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 */
2202                 inq_data.inqd_len = 31;
2203                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
2204                 inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
2205                 /*
2206                  *      Set the Vendor, Product, and Revision Level
2207                  *      see: <vendor>.c i.e. aac.c
2208                  */
2209                 if (cid == host->this_id) {
2210                         setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2211                         inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
2212                         scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2213                                                  sizeof(inq_data));
2214                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2215                         scsicmd->scsi_done(scsicmd);
2216                         return 0;
2217                 }
2218                 if (dev->in_reset)
2219                         return -1;
2220                 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2221                 inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
2222                 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
2223                 return aac_get_container_name(scsicmd);
2224         }
2225         case SERVICE_ACTION_IN:
2226                 if (!(dev->raw_io_interface) ||
2227                     !(dev->raw_io_64) ||
2228                     ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2229                         break;
2230         {
2231                 u64 capacity;
2232                 char cp[13];
2233                 unsigned int alloc_len;
2234
2235                 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2236                 capacity = fsa_dev_ptr[cid].size - 1;
2237                 cp[0] = (capacity >> 56) & 0xff;
2238                 cp[1] = (capacity >> 48) & 0xff;
2239                 cp[2] = (capacity >> 40) & 0xff;
2240                 cp[3] = (capacity >> 32) & 0xff;
2241                 cp[4] = (capacity >> 24) & 0xff;
2242                 cp[5] = (capacity >> 16) & 0xff;
2243                 cp[6] = (capacity >> 8) & 0xff;
2244                 cp[7] = (capacity >> 0) & 0xff;
2245                 cp[8] = 0;
2246                 cp[9] = 0;
2247                 cp[10] = 2;
2248                 cp[11] = 0;
2249                 cp[12] = 0;
2250
2251                 alloc_len = ((scsicmd->cmnd[10] << 24)
2252                              + (scsicmd->cmnd[11] << 16)
2253                              + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2254
2255                 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
2256                 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
2257                 if (alloc_len < scsi_bufflen(scsicmd))
2258                         scsi_set_resid(scsicmd,
2259                                        scsi_bufflen(scsicmd) - alloc_len);
2260
2261                 /* Do not cache partition table for arrays */
2262                 scsicmd->device->removable = 1;
2263
2264                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2265                 scsicmd->scsi_done(scsicmd);
2266
2267                 return 0;
2268         }
2269
2270         case READ_CAPACITY:
2271         {
2272                 u32 capacity;
2273                 char cp[8];
2274
2275                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
2276                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2277                         capacity = fsa_dev_ptr[cid].size - 1;
2278                 else
2279                         capacity = (u32)-1;
2280
2281                 cp[0] = (capacity >> 24) & 0xff;
2282                 cp[1] = (capacity >> 16) & 0xff;
2283                 cp[2] = (capacity >> 8) & 0xff;
2284                 cp[3] = (capacity >> 0) & 0xff;
2285                 cp[4] = 0;
2286                 cp[5] = 0;
2287                 cp[6] = 2;
2288                 cp[7] = 0;
2289                 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
2290                 /* Do not cache partition table for arrays */
2291                 scsicmd->device->removable = 1;
2292                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2293                   SAM_STAT_GOOD;
2294                 scsicmd->scsi_done(scsicmd);
2295
2296                 return 0;
2297         }
2298
2299         case MODE_SENSE:
2300         {
2301                 char mode_buf[7];
2302                 int mode_buf_length = 4;
2303
2304                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
2305                 mode_buf[0] = 3;        /* Mode data length */
2306                 mode_buf[1] = 0;        /* Medium type - default */
2307                 mode_buf[2] = 0;        /* Device-specific param,
2308                                            bit 8: 0/1 = write enabled/protected
2309                                            bit 4: 0/1 = FUA enabled */
2310                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2311                         mode_buf[2] = 0x10;
2312                 mode_buf[3] = 0;        /* Block descriptor length */
2313                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2314                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2315                         mode_buf[0] = 6;
2316                         mode_buf[4] = 8;
2317                         mode_buf[5] = 1;
2318                         mode_buf[6] = ((aac_cache & 6) == 2)
2319                                 ? 0 : 0x04; /* WCE */
2320                         mode_buf_length = 7;
2321                         if (mode_buf_length > scsicmd->cmnd[4])
2322                                 mode_buf_length = scsicmd->cmnd[4];
2323                 }
2324                 scsi_sg_copy_from_buffer(scsicmd, mode_buf, mode_buf_length);
2325                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2326                 scsicmd->scsi_done(scsicmd);
2327
2328                 return 0;
2329         }
2330         case MODE_SENSE_10:
2331         {
2332                 char mode_buf[11];
2333                 int mode_buf_length = 8;
2334
2335                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
2336                 mode_buf[0] = 0;        /* Mode data length (MSB) */
2337                 mode_buf[1] = 6;        /* Mode data length (LSB) */
2338                 mode_buf[2] = 0;        /* Medium type - default */
2339                 mode_buf[3] = 0;        /* Device-specific param,
2340                                            bit 8: 0/1 = write enabled/protected
2341                                            bit 4: 0/1 = FUA enabled */
2342                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2343                         mode_buf[3] = 0x10;
2344                 mode_buf[4] = 0;        /* reserved */
2345                 mode_buf[5] = 0;        /* reserved */
2346                 mode_buf[6] = 0;        /* Block descriptor length (MSB) */
2347                 mode_buf[7] = 0;        /* Block descriptor length (LSB) */
2348                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2349                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2350                         mode_buf[1] = 9;
2351                         mode_buf[8] = 8;
2352                         mode_buf[9] = 1;
2353                         mode_buf[10] = ((aac_cache & 6) == 2)
2354                                 ? 0 : 0x04; /* WCE */
2355                         mode_buf_length = 11;
2356                         if (mode_buf_length > scsicmd->cmnd[8])
2357                                 mode_buf_length = scsicmd->cmnd[8];
2358                 }
2359                 scsi_sg_copy_from_buffer(scsicmd, mode_buf, mode_buf_length);
2360
2361                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2362                 scsicmd->scsi_done(scsicmd);
2363
2364                 return 0;
2365         }
2366         case REQUEST_SENSE:
2367                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2368                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2369                 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2370                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2371                 scsicmd->scsi_done(scsicmd);
2372                 return 0;
2373
2374         case ALLOW_MEDIUM_REMOVAL:
2375                 dprintk((KERN_DEBUG "LOCK command.\n"));
2376                 if (scsicmd->cmnd[4])
2377                         fsa_dev_ptr[cid].locked = 1;
2378                 else
2379                         fsa_dev_ptr[cid].locked = 0;
2380
2381                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2382                 scsicmd->scsi_done(scsicmd);
2383                 return 0;
2384         /*
2385          *      These commands are all No-Ops
2386          */
2387         case TEST_UNIT_READY:
2388                 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
2389                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2390                                 SAM_STAT_CHECK_CONDITION;
2391                         set_sense(&dev->fsa_dev[cid].sense_data,
2392                                   NOT_READY, SENCODE_BECOMING_READY,
2393                                   ASENCODE_BECOMING_READY, 0, 0);
2394                         memcpy(scsicmd->sense_buffer,
2395                                &dev->fsa_dev[cid].sense_data,
2396                                min_t(size_t,
2397                                      sizeof(dev->fsa_dev[cid].sense_data),
2398                                      SCSI_SENSE_BUFFERSIZE));
2399                         scsicmd->scsi_done(scsicmd);
2400                         return 0;
2401                 }
2402                 /* FALLTHRU */
2403         case RESERVE:
2404         case RELEASE:
2405         case REZERO_UNIT:
2406         case REASSIGN_BLOCKS:
2407         case SEEK_10:
2408                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2409                 scsicmd->scsi_done(scsicmd);
2410                 return 0;
2411
2412         case START_STOP:
2413                 return aac_start_stop(scsicmd);
2414         }
2415
2416         switch (scsicmd->cmnd[0])
2417         {
2418                 case READ_6:
2419                 case READ_10:
2420                 case READ_12:
2421                 case READ_16:
2422                         if (dev->in_reset)
2423                                 return -1;
2424                         /*
2425                          *      Hack to keep track of ordinal number of the device that
2426                          *      corresponds to a container. Needed to convert
2427                          *      containers to /dev/sd device names
2428                          */
2429
2430                         if (scsicmd->request->rq_disk)
2431                                 strlcpy(fsa_dev_ptr[cid].devname,
2432                                 scsicmd->request->rq_disk->disk_name,
2433                                 min(sizeof(fsa_dev_ptr[cid].devname),
2434                                 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
2435
2436                         return aac_read(scsicmd);
2437
2438                 case WRITE_6:
2439                 case WRITE_10:
2440                 case WRITE_12:
2441                 case WRITE_16:
2442                         if (dev->in_reset)
2443                                 return -1;
2444                         return aac_write(scsicmd);
2445
2446                 case SYNCHRONIZE_CACHE:
2447                         if (((aac_cache & 6) == 6) && dev->cache_protected) {
2448                                 scsicmd->result = DID_OK << 16 |
2449                                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2450                                 scsicmd->scsi_done(scsicmd);
2451                                 return 0;
2452                         }
2453                         /* Issue FIB to tell Firmware to flush it's cache */
2454                         if ((aac_cache & 6) != 2)
2455                                 return aac_synchronize(scsicmd);
2456                         /* FALLTHRU */
2457                 default:
2458                         /*
2459                          *      Unhandled commands
2460                          */
2461                         dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
2462                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2463                         set_sense(&dev->fsa_dev[cid].sense_data,
2464                           ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2465                           ASENCODE_INVALID_COMMAND, 0, 0);
2466                         memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2467                                 min_t(size_t,
2468                                       sizeof(dev->fsa_dev[cid].sense_data),
2469                                       SCSI_SENSE_BUFFERSIZE));
2470                         scsicmd->scsi_done(scsicmd);
2471                         return 0;
2472         }
2473 }
2474
2475 static int query_disk(struct aac_dev *dev, void __user *arg)
2476 {
2477         struct aac_query_disk qd;
2478         struct fsa_dev_info *fsa_dev_ptr;
2479
2480         fsa_dev_ptr = dev->fsa_dev;
2481         if (!fsa_dev_ptr)
2482                 return -EBUSY;
2483         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2484                 return -EFAULT;
2485         if (qd.cnum == -1)
2486                 qd.cnum = qd.id;
2487         else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2488         {
2489                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2490                         return -EINVAL;
2491                 qd.instance = dev->scsi_host_ptr->host_no;
2492                 qd.bus = 0;
2493                 qd.id = CONTAINER_TO_ID(qd.cnum);
2494                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2495         }
2496         else return -EINVAL;
2497
2498         qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
2499         qd.locked = fsa_dev_ptr[qd.cnum].locked;
2500         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2501
2502         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2503                 qd.unmapped = 1;
2504         else
2505                 qd.unmapped = 0;
2506
2507         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2508           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2509
2510         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2511                 return -EFAULT;
2512         return 0;
2513 }
2514
2515 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2516 {
2517         struct aac_delete_disk dd;
2518         struct fsa_dev_info *fsa_dev_ptr;
2519
2520         fsa_dev_ptr = dev->fsa_dev;
2521         if (!fsa_dev_ptr)
2522                 return -EBUSY;
2523
2524         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2525                 return -EFAULT;
2526
2527         if (dd.cnum >= dev->maximum_num_containers)
2528                 return -EINVAL;
2529         /*
2530          *      Mark this container as being deleted.
2531          */
2532         fsa_dev_ptr[dd.cnum].deleted = 1;
2533         /*
2534          *      Mark the container as no longer valid
2535          */
2536         fsa_dev_ptr[dd.cnum].valid = 0;
2537         return 0;
2538 }
2539
2540 static int delete_disk(struct aac_dev *dev, void __user *arg)
2541 {
2542         struct aac_delete_disk dd;
2543         struct fsa_dev_info *fsa_dev_ptr;
2544
2545         fsa_dev_ptr = dev->fsa_dev;
2546         if (!fsa_dev_ptr)
2547                 return -EBUSY;
2548
2549         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2550                 return -EFAULT;
2551
2552         if (dd.cnum >= dev->maximum_num_containers)
2553                 return -EINVAL;
2554         /*
2555          *      If the container is locked, it can not be deleted by the API.
2556          */
2557         if (fsa_dev_ptr[dd.cnum].locked)
2558                 return -EBUSY;
2559         else {
2560                 /*
2561                  *      Mark the container as no longer being valid.
2562                  */
2563                 fsa_dev_ptr[dd.cnum].valid = 0;
2564                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2565                 return 0;
2566         }
2567 }
2568
2569 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2570 {
2571         switch (cmd) {
2572         case FSACTL_QUERY_DISK:
2573                 return query_disk(dev, arg);
2574         case FSACTL_DELETE_DISK:
2575                 return delete_disk(dev, arg);
2576         case FSACTL_FORCE_DELETE_DISK:
2577                 return force_delete_disk(dev, arg);
2578         case FSACTL_GET_CONTAINERS:
2579                 return aac_get_containers(dev);
2580         default:
2581                 return -ENOTTY;
2582         }
2583 }
2584
2585 /**
2586  *
2587  * aac_srb_callback
2588  * @context: the context set in the fib - here it is scsi cmd
2589  * @fibptr: pointer to the fib
2590  *
2591  * Handles the completion of a scsi command to a non dasd device
2592  *
2593  */
2594
2595 static void aac_srb_callback(void *context, struct fib * fibptr)
2596 {
2597         struct aac_dev *dev;
2598         struct aac_srb_reply *srbreply;
2599         struct scsi_cmnd *scsicmd;
2600
2601         scsicmd = (struct scsi_cmnd *) context;
2602
2603         if (!aac_valid_context(scsicmd, fibptr))
2604                 return;
2605
2606         BUG_ON(fibptr == NULL);
2607
2608         dev = fibptr->dev;
2609
2610         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2611
2612         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
2613         /*
2614          *      Calculate resid for sg
2615          */
2616
2617         scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2618                        - le32_to_cpu(srbreply->data_xfer_length));
2619
2620         scsi_dma_unmap(scsicmd);
2621
2622         /* expose physical device if expose_physicald flag is on */
2623         if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
2624           && expose_physicals > 0)
2625                 aac_expose_phy_device(scsicmd);
2626
2627         /*
2628          * First check the fib status
2629          */
2630
2631         if (le32_to_cpu(srbreply->status) != ST_OK){
2632                 int len;
2633                 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2634                 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
2635                             SCSI_SENSE_BUFFERSIZE);
2636                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2637                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2638         }
2639
2640         /*
2641          * Next check the srb status
2642          */
2643         switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2644         case SRB_STATUS_ERROR_RECOVERY:
2645         case SRB_STATUS_PENDING:
2646         case SRB_STATUS_SUCCESS:
2647                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2648                 break;
2649         case SRB_STATUS_DATA_OVERRUN:
2650                 switch(scsicmd->cmnd[0]){
2651                 case  READ_6:
2652                 case  WRITE_6:
2653                 case  READ_10:
2654                 case  WRITE_10:
2655                 case  READ_12:
2656                 case  WRITE_12:
2657                 case  READ_16:
2658                 case  WRITE_16:
2659                         if (le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow) {
2660                                 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2661                         } else {
2662                                 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2663                         }
2664                         scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2665                         break;
2666                 case INQUIRY: {
2667                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2668                         break;
2669                 }
2670                 default:
2671                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2672                         break;
2673                 }
2674                 break;
2675         case SRB_STATUS_ABORTED:
2676                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2677                 break;
2678         case SRB_STATUS_ABORT_FAILED:
2679                 // Not sure about this one - but assuming the hba was trying to abort for some reason
2680                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2681                 break;
2682         case SRB_STATUS_PARITY_ERROR:
2683                 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2684                 break;
2685         case SRB_STATUS_NO_DEVICE:
2686         case SRB_STATUS_INVALID_PATH_ID:
2687         case SRB_STATUS_INVALID_TARGET_ID:
2688         case SRB_STATUS_INVALID_LUN:
2689         case SRB_STATUS_SELECTION_TIMEOUT:
2690                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2691                 break;
2692
2693         case SRB_STATUS_COMMAND_TIMEOUT:
2694         case SRB_STATUS_TIMEOUT:
2695                 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2696                 break;
2697
2698         case SRB_STATUS_BUSY:
2699                 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
2700                 break;
2701
2702         case SRB_STATUS_BUS_RESET:
2703                 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2704                 break;
2705
2706         case SRB_STATUS_MESSAGE_REJECTED:
2707                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2708                 break;
2709         case SRB_STATUS_REQUEST_FLUSHED:
2710         case SRB_STATUS_ERROR:
2711         case SRB_STATUS_INVALID_REQUEST:
2712         case SRB_STATUS_REQUEST_SENSE_FAILED:
2713         case SRB_STATUS_NO_HBA:
2714         case SRB_STATUS_UNEXPECTED_BUS_FREE:
2715         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2716         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2717         case SRB_STATUS_DELAYED_RETRY:
2718         case SRB_STATUS_BAD_FUNCTION:
2719         case SRB_STATUS_NOT_STARTED:
2720         case SRB_STATUS_NOT_IN_USE:
2721         case SRB_STATUS_FORCE_ABORT:
2722         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2723         default:
2724 #ifdef AAC_DETAILED_STATUS_INFO
2725                 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2726                         le32_to_cpu(srbreply->srb_status) & 0x3F,
2727                         aac_get_status_string(
2728                                 le32_to_cpu(srbreply->srb_status) & 0x3F),
2729                         scsicmd->cmnd[0],
2730                         le32_to_cpu(srbreply->scsi_status));
2731 #endif
2732                 if ((scsicmd->cmnd[0] == ATA_12)
2733                   || (scsicmd->cmnd[0] == ATA_16)) {
2734                         if (scsicmd->cmnd[2] & (0x01 << 5)) {
2735                                 scsicmd->result = DID_OK << 16
2736                                                 | COMMAND_COMPLETE << 8;
2737                                 break;
2738                         } else {
2739                                 scsicmd->result = DID_ERROR << 16
2740                                                 | COMMAND_COMPLETE << 8;
2741                                 break;
2742                         }
2743                 } else {
2744                         scsicmd->result = DID_ERROR << 16
2745                                         | COMMAND_COMPLETE << 8;
2746                         break;
2747                 }
2748         }
2749         if (le32_to_cpu(srbreply->scsi_status) == SAM_STAT_CHECK_CONDITION) {
2750                 int len;
2751                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2752                 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
2753                             SCSI_SENSE_BUFFERSIZE);
2754 #ifdef AAC_DETAILED_STATUS_INFO
2755                 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2756                                         le32_to_cpu(srbreply->status), len);
2757 #endif
2758                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2759         }
2760         /*
2761          * OR in the scsi status (already shifted up a bit)
2762          */
2763         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2764
2765         aac_fib_complete(fibptr);
2766         aac_fib_free(fibptr);
2767         scsicmd->scsi_done(scsicmd);
2768 }
2769
2770 /**
2771  *
2772  * aac_send_scb_fib
2773  * @scsicmd: the scsi command block
2774  *
2775  * This routine will form a FIB and fill in the aac_srb from the
2776  * scsicmd passed in.
2777  */
2778
2779 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2780 {
2781         struct fib* cmd_fibcontext;
2782         struct aac_dev* dev;
2783         int status;
2784
2785         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2786         if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
2787                         scsicmd->device->lun > 7) {
2788                 scsicmd->result = DID_NO_CONNECT << 16;
2789                 scsicmd->scsi_done(scsicmd);
2790                 return 0;
2791         }
2792
2793         /*
2794          *      Allocate and initialize a Fib then setup a BlockWrite command
2795          */
2796         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
2797                 return -1;
2798         }
2799         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2800         status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
2801
2802         /*
2803          *      Check that the command queued to the controller
2804          */
2805         if (status == -EINPROGRESS)
2806                 return 0;
2807
2808         printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2809         aac_fib_complete(cmd_fibcontext);
2810         aac_fib_free(cmd_fibcontext);
2811
2812         return -1;
2813 }
2814
2815 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2816 {
2817         struct aac_dev *dev;
2818         unsigned long byte_count = 0;
2819         int nseg;
2820
2821         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2822         // Get rid of old data
2823         psg->count = 0;
2824         psg->sg[0].addr = 0;
2825         psg->sg[0].count = 0;
2826
2827         nseg = scsi_dma_map(scsicmd);
2828         BUG_ON(nseg < 0);
2829         if (nseg) {
2830                 struct scatterlist *sg;
2831                 int i;
2832
2833                 psg->count = cpu_to_le32(nseg);
2834
2835                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2836                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2837                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2838                         byte_count += sg_dma_len(sg);
2839                 }
2840                 /* hba wants the size to be exact */
2841                 if (byte_count > scsi_bufflen(scsicmd)) {
2842                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2843                                 (byte_count - scsi_bufflen(scsicmd));
2844                         psg->sg[i-1].count = cpu_to_le32(temp);
2845                         byte_count = scsi_bufflen(scsicmd);
2846                 }
2847                 /* Check for command underflow */
2848                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2849                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2850                                         byte_count, scsicmd->underflow);
2851                 }
2852         }
2853         return byte_count;
2854 }
2855
2856
2857 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2858 {
2859         struct aac_dev *dev;
2860         unsigned long byte_count = 0;
2861         u64 addr;
2862         int nseg;
2863
2864         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2865         // Get rid of old data
2866         psg->count = 0;
2867         psg->sg[0].addr[0] = 0;
2868         psg->sg[0].addr[1] = 0;
2869         psg->sg[0].count = 0;
2870
2871         nseg = scsi_dma_map(scsicmd);
2872         BUG_ON(nseg < 0);
2873         if (nseg) {
2874                 struct scatterlist *sg;
2875                 int i;
2876
2877                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2878                         int count = sg_dma_len(sg);
2879                         addr = sg_dma_address(sg);
2880                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2881                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
2882                         psg->sg[i].count = cpu_to_le32(count);
2883                         byte_count += count;
2884                 }
2885                 psg->count = cpu_to_le32(nseg);
2886                 /* hba wants the size to be exact */
2887                 if (byte_count > scsi_bufflen(scsicmd)) {
2888                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2889                                 (byte_count - scsi_bufflen(scsicmd));
2890                         psg->sg[i-1].count = cpu_to_le32(temp);
2891                         byte_count = scsi_bufflen(scsicmd);
2892                 }
2893                 /* Check for command underflow */
2894                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2895                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2896                                         byte_count, scsicmd->underflow);
2897                 }
2898         }
2899         return byte_count;
2900 }
2901
2902 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2903 {
2904         unsigned long byte_count = 0;
2905         int nseg;
2906
2907         // Get rid of old data
2908         psg->count = 0;
2909         psg->sg[0].next = 0;
2910         psg->sg[0].prev = 0;
2911         psg->sg[0].addr[0] = 0;
2912         psg->sg[0].addr[1] = 0;
2913         psg->sg[0].count = 0;
2914         psg->sg[0].flags = 0;
2915
2916         nseg = scsi_dma_map(scsicmd);
2917         BUG_ON(nseg < 0);
2918         if (nseg) {
2919                 struct scatterlist *sg;
2920                 int i;
2921
2922                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2923                         int count = sg_dma_len(sg);
2924                         u64 addr = sg_dma_address(sg);
2925                         psg->sg[i].next = 0;
2926                         psg->sg[i].prev = 0;
2927                         psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2928                         psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2929                         psg->sg[i].count = cpu_to_le32(count);
2930                         psg->sg[i].flags = 0;
2931                         byte_count += count;
2932                 }
2933                 psg->count = cpu_to_le32(nseg);