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