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