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