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