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