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