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