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