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