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