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