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