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