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