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