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