[SCSI] aacraid: remove duplicate io callback code
[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 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/sched.h>
30 #include <linux/pci.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
33 #include <linux/completion.h>
34 #include <linux/blkdev.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 int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
137 #ifdef AAC_DETAILED_STATUS_INFO
138 static char *aac_get_status_string(u32 status);
139 #endif
140
141 /*
142  *      Non dasd selection is handled entirely in aachba now
143  */     
144  
145 static int nondasd = -1;
146 static int dacmode = -1;
147
148 static int commit = -1;
149
150 module_param(nondasd, int, 0);
151 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
152 module_param(dacmode, int, 0);
153 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
154 module_param(commit, int, 0);
155 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");
156
157 int numacb = -1;
158 module_param(numacb, int, S_IRUGO|S_IWUSR);
159 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid\nvalues are 512 and down. Default is to use suggestion from Firmware.");
160
161 int acbsize = -1;
162 module_param(acbsize, int, S_IRUGO|S_IWUSR);
163 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512,\n2048, 4096 and 8192. Default is to use suggestion from Firmware.");
164 /**
165  *      aac_get_config_status   -       check the adapter configuration
166  *      @common: adapter to query
167  *
168  *      Query config status, and commit the configuration if needed.
169  */
170 int aac_get_config_status(struct aac_dev *dev)
171 {
172         int status = 0;
173         struct fib * fibptr;
174
175         if (!(fibptr = fib_alloc(dev)))
176                 return -ENOMEM;
177
178         fib_init(fibptr);
179         {
180                 struct aac_get_config_status *dinfo;
181                 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
182
183                 dinfo->command = cpu_to_le32(VM_ContainerConfig);
184                 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
185                 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
186         }
187
188         status = fib_send(ContainerCommand,
189                             fibptr,
190                             sizeof (struct aac_get_config_status),
191                             FsaNormal,
192                             1, 1,
193                             NULL, NULL);
194         if (status < 0 ) {
195                 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
196         } else {
197                 struct aac_get_config_status_resp *reply
198                   = (struct aac_get_config_status_resp *) fib_data(fibptr);
199                 dprintk((KERN_WARNING
200                   "aac_get_config_status: response=%d status=%d action=%d\n",
201                   le32_to_cpu(reply->response),
202                   le32_to_cpu(reply->status),
203                   le32_to_cpu(reply->data.action)));
204                 if ((le32_to_cpu(reply->response) != ST_OK) ||
205                      (le32_to_cpu(reply->status) != CT_OK) ||
206                      (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
207                         printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
208                         status = -EINVAL;
209                 }
210         }
211         fib_complete(fibptr);
212         /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
213         if (status >= 0) {
214                 if (commit == 1) {
215                         struct aac_commit_config * dinfo;
216                         fib_init(fibptr);
217                         dinfo = (struct aac_commit_config *) fib_data(fibptr);
218         
219                         dinfo->command = cpu_to_le32(VM_ContainerConfig);
220                         dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
221         
222                         status = fib_send(ContainerCommand,
223                                     fibptr,
224                                     sizeof (struct aac_commit_config),
225                                     FsaNormal,
226                                     1, 1,
227                                     NULL, NULL);
228                         fib_complete(fibptr);
229                 } else if (commit == 0) {
230                         printk(KERN_WARNING
231                           "aac_get_config_status: Foreign device configurations are being ignored\n");
232                 }
233         }
234         fib_free(fibptr);
235         return status;
236 }
237
238 /**
239  *      aac_get_containers      -       list containers
240  *      @common: adapter to probe
241  *
242  *      Make a list of all containers on this controller
243  */
244 int aac_get_containers(struct aac_dev *dev)
245 {
246         struct fsa_dev_info *fsa_dev_ptr;
247         u32 index; 
248         int status = 0;
249         struct fib * fibptr;
250         unsigned instance;
251         struct aac_get_container_count *dinfo;
252         struct aac_get_container_count_resp *dresp;
253         int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
254
255         instance = dev->scsi_host_ptr->unique_id;
256
257         if (!(fibptr = fib_alloc(dev)))
258                 return -ENOMEM;
259
260         fib_init(fibptr);
261         dinfo = (struct aac_get_container_count *) fib_data(fibptr);
262         dinfo->command = cpu_to_le32(VM_ContainerConfig);
263         dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
264
265         status = fib_send(ContainerCommand,
266                     fibptr,
267                     sizeof (struct aac_get_container_count),
268                     FsaNormal,
269                     1, 1,
270                     NULL, NULL);
271         if (status >= 0) {
272                 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
273                 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
274                 fib_complete(fibptr);
275         }
276
277         if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
278                 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
279         fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
280           sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
281         if (!fsa_dev_ptr) {
282                 fib_free(fibptr);
283                 return -ENOMEM;
284         }
285         memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
286
287         dev->fsa_dev = fsa_dev_ptr;
288         dev->maximum_num_containers = maximum_num_containers;
289
290         for (index = 0; index < dev->maximum_num_containers; index++) {
291                 struct aac_query_mount *dinfo;
292                 struct aac_mount *dresp;
293
294                 fsa_dev_ptr[index].devname[0] = '\0';
295
296                 fib_init(fibptr);
297                 dinfo = (struct aac_query_mount *) fib_data(fibptr);
298
299                 dinfo->command = cpu_to_le32(VM_NameServe);
300                 dinfo->count = cpu_to_le32(index);
301                 dinfo->type = cpu_to_le32(FT_FILESYS);
302
303                 status = fib_send(ContainerCommand,
304                                     fibptr,
305                                     sizeof (struct aac_query_mount),
306                                     FsaNormal,
307                                     1, 1,
308                                     NULL, NULL);
309                 if (status < 0 ) {
310                         printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
311                         break;
312                 }
313                 dresp = (struct aac_mount *)fib_data(fibptr);
314
315                 dprintk ((KERN_DEBUG
316                   "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%u\n",
317                   (int)index, (int)le32_to_cpu(dresp->status),
318                   (int)le32_to_cpu(dresp->mnt[0].vol),
319                   (int)le32_to_cpu(dresp->mnt[0].state),
320                   (unsigned)le32_to_cpu(dresp->mnt[0].capacity)));
321                 if ((le32_to_cpu(dresp->status) == ST_OK) &&
322                     (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
323                     (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
324                         fsa_dev_ptr[index].valid = 1;
325                         fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
326                         fsa_dev_ptr[index].size = le32_to_cpu(dresp->mnt[0].capacity);
327                         if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
328                                     fsa_dev_ptr[index].ro = 1;
329                 }
330                 fib_complete(fibptr);
331                 /*
332                  *      If there are no more containers, then stop asking.
333                  */
334                 if ((index + 1) >= le32_to_cpu(dresp->count)){
335                         break;
336                 }
337         }
338         fib_free(fibptr);
339         return status;
340 }
341
342 static void aac_io_done(struct scsi_cmnd * scsicmd)
343 {
344         unsigned long cpu_flags;
345         struct Scsi_Host *host = scsicmd->device->host;
346         spin_lock_irqsave(host->host_lock, cpu_flags);
347         scsicmd->scsi_done(scsicmd);
348         spin_unlock_irqrestore(host->host_lock, cpu_flags);
349 }
350
351 static void get_container_name_callback(void *context, struct fib * fibptr)
352 {
353         struct aac_get_name_resp * get_name_reply;
354         struct scsi_cmnd * scsicmd;
355
356         scsicmd = (struct scsi_cmnd *) context;
357
358         dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
359         if (fibptr == NULL)
360                 BUG();
361
362         get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
363         /* Failure is irrelevant, using default value instead */
364         if ((le32_to_cpu(get_name_reply->status) == CT_OK)
365          && (get_name_reply->data[0] != '\0')) {
366                 int    count;
367                 char * dp;
368                 char * sp = get_name_reply->data;
369                 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
370                 while (*sp == ' ')
371                         ++sp;
372                 count = sizeof(((struct inquiry_data *)NULL)->inqd_pid);
373                 dp = ((struct inquiry_data *)scsicmd->request_buffer)->inqd_pid;
374                 if (*sp) do {
375                         *dp++ = (*sp) ? *sp++ : ' ';
376                 } while (--count > 0);
377         }
378         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
379
380         fib_complete(fibptr);
381         fib_free(fibptr);
382         aac_io_done(scsicmd);
383 }
384
385 /**
386  *      aac_get_container_name  -       get container name, none blocking.
387  */
388 static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
389 {
390         int status;
391         struct aac_get_name *dinfo;
392         struct fib * cmd_fibcontext;
393         struct aac_dev * dev;
394
395         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
396
397         if (!(cmd_fibcontext = fib_alloc(dev)))
398                 return -ENOMEM;
399
400         fib_init(cmd_fibcontext);
401         dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
402
403         dinfo->command = cpu_to_le32(VM_ContainerConfig);
404         dinfo->type = cpu_to_le32(CT_READ_NAME);
405         dinfo->cid = cpu_to_le32(cid);
406         dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
407
408         status = fib_send(ContainerCommand, 
409                   cmd_fibcontext, 
410                   sizeof (struct aac_get_name),
411                   FsaNormal, 
412                   0, 1, 
413                   (fib_callback) get_container_name_callback, 
414                   (void *) scsicmd);
415         
416         /*
417          *      Check that the command queued to the controller
418          */
419         if (status == -EINPROGRESS) 
420                 return 0;
421                 
422         printk(KERN_WARNING "aac_get_container_name: fib_send failed with status: %d.\n", status);
423         fib_complete(cmd_fibcontext);
424         fib_free(cmd_fibcontext);
425         return -1;
426 }
427
428 /**
429  *      probe_container         -       query a logical volume
430  *      @dev: device to query
431  *      @cid: container identifier
432  *
433  *      Queries the controller about the given volume. The volume information
434  *      is updated in the struct fsa_dev_info structure rather than returned.
435  */
436  
437 static int probe_container(struct aac_dev *dev, int cid)
438 {
439         struct fsa_dev_info *fsa_dev_ptr;
440         int status;
441         struct aac_query_mount *dinfo;
442         struct aac_mount *dresp;
443         struct fib * fibptr;
444         unsigned instance;
445
446         fsa_dev_ptr = dev->fsa_dev;
447         instance = dev->scsi_host_ptr->unique_id;
448
449         if (!(fibptr = fib_alloc(dev)))
450                 return -ENOMEM;
451
452         fib_init(fibptr);
453
454         dinfo = (struct aac_query_mount *)fib_data(fibptr);
455
456         dinfo->command = cpu_to_le32(VM_NameServe);
457         dinfo->count = cpu_to_le32(cid);
458         dinfo->type = cpu_to_le32(FT_FILESYS);
459
460         status = fib_send(ContainerCommand,
461                             fibptr,
462                             sizeof(struct aac_query_mount),
463                             FsaNormal,
464                             1, 1,
465                             NULL, NULL);
466         if (status < 0) {
467                 printk(KERN_WARNING "aacraid: probe_container query failed.\n");
468                 goto error;
469         }
470
471         dresp = (struct aac_mount *) fib_data(fibptr);
472
473         if ((le32_to_cpu(dresp->status) == ST_OK) &&
474             (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
475             (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
476                 fsa_dev_ptr[cid].valid = 1;
477                 fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
478                 fsa_dev_ptr[cid].size = le32_to_cpu(dresp->mnt[0].capacity);
479                 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
480                         fsa_dev_ptr[cid].ro = 1;
481         }
482
483 error:
484         fib_complete(fibptr);
485         fib_free(fibptr);
486
487         return status;
488 }
489
490 /* Local Structure to set SCSI inquiry data strings */
491 struct scsi_inq {
492         char vid[8];         /* Vendor ID */
493         char pid[16];        /* Product ID */
494         char prl[4];         /* Product Revision Level */
495 };
496
497 /**
498  *      InqStrCopy      -       string merge
499  *      @a:     string to copy from
500  *      @b:     string to copy to
501  *
502  *      Copy a String from one location to another
503  *      without copying \0
504  */
505
506 static void inqstrcpy(char *a, char *b)
507 {
508
509         while(*a != (char)0) 
510                 *b++ = *a++;
511 }
512
513 static char *container_types[] = {
514         "None",
515         "Volume",
516         "Mirror",
517         "Stripe",
518         "RAID5",
519         "SSRW",
520         "SSRO",
521         "Morph",
522         "Legacy",
523         "RAID4",
524         "RAID10",             
525         "RAID00",             
526         "V-MIRRORS",          
527         "PSEUDO R4",          
528         "RAID50",
529         "RAID5D",
530         "RAID5D0",
531         "RAID1E",
532         "RAID6",
533         "RAID60",
534         "Unknown"
535 };
536
537
538
539 /* Function: setinqstr
540  *
541  * Arguments: [1] pointer to void [1] int
542  *
543  * Purpose: Sets SCSI inquiry data strings for vendor, product
544  * and revision level. Allows strings to be set in platform dependant
545  * files instead of in OS dependant driver source.
546  */
547
548 static void setinqstr(int devtype, void *data, int tindex)
549 {
550         struct scsi_inq *str;
551         struct aac_driver_ident *mp;
552
553         mp = aac_get_driver_ident(devtype);
554    
555         str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
556
557         inqstrcpy (mp->vname, str->vid); 
558         inqstrcpy (mp->model, str->pid); /* last six chars reserved for vol type */
559
560         if (tindex < (sizeof(container_types)/sizeof(char *))){
561                 char *findit = str->pid;
562
563                 for ( ; *findit != ' '; findit++); /* walk till we find a space */
564                 /* RAID is superfluous in the context of a RAID device */
565                 if (memcmp(findit-4, "RAID", 4) == 0)
566                         *(findit -= 4) = ' ';
567                 inqstrcpy (container_types[tindex], findit + 1);
568         }
569         inqstrcpy ("V1.0", str->prl);
570 }
571
572 static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
573                       u8 a_sense_code, u8 incorrect_length,
574                       u8 bit_pointer, u16 field_pointer,
575                       u32 residue)
576 {
577         sense_buf[0] = 0xF0;    /* Sense data valid, err code 70h (current error) */
578         sense_buf[1] = 0;       /* Segment number, always zero */
579
580         if (incorrect_length) {
581                 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
582                 sense_buf[3] = BYTE3(residue);
583                 sense_buf[4] = BYTE2(residue);
584                 sense_buf[5] = BYTE1(residue);
585                 sense_buf[6] = BYTE0(residue);
586         } else
587                 sense_buf[2] = sense_key;       /* Sense key */
588
589         if (sense_key == ILLEGAL_REQUEST)
590                 sense_buf[7] = 10;      /* Additional sense length */
591         else
592                 sense_buf[7] = 6;       /* Additional sense length */
593
594         sense_buf[12] = sense_code;     /* Additional sense code */
595         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
596         if (sense_key == ILLEGAL_REQUEST) {
597                 sense_buf[15] = 0;
598
599                 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
600                         sense_buf[15] = 0x80;/* Std sense key specific field */
601                 /* Illegal parameter is in the parameter block */
602
603                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
604                         sense_buf[15] = 0xc0;/* Std sense key specific field */
605                 /* Illegal parameter is in the CDB block */
606                 sense_buf[15] |= bit_pointer;
607                 sense_buf[16] = field_pointer >> 8;     /* MSB */
608                 sense_buf[17] = field_pointer;          /* LSB */
609         }
610 }
611
612 int aac_get_adapter_info(struct aac_dev* dev)
613 {
614         struct fib* fibptr;
615         int rcode;
616         u32 tmp;
617         struct aac_adapter_info *info;
618         struct aac_bus_info *command;
619         struct aac_bus_info_response *bus_info;
620
621         if (!(fibptr = fib_alloc(dev)))
622                 return -ENOMEM;
623
624         fib_init(fibptr);
625         info = (struct aac_adapter_info *) fib_data(fibptr);
626         memset(info,0,sizeof(*info));
627
628         rcode = fib_send(RequestAdapterInfo,
629                          fibptr, 
630                          sizeof(*info),
631                          FsaNormal, 
632                          1, 1, 
633                          NULL, 
634                          NULL);
635
636         if (rcode < 0) {
637                 fib_complete(fibptr);
638                 fib_free(fibptr);
639                 return rcode;
640         }
641         memcpy(&dev->adapter_info, info, sizeof(*info));
642
643         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
644                 struct aac_supplement_adapter_info * info;
645
646                 fib_init(fibptr);
647
648                 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
649
650                 memset(info,0,sizeof(*info));
651
652                 rcode = fib_send(RequestSupplementAdapterInfo,
653                                  fibptr,
654                                  sizeof(*info),
655                                  FsaNormal,
656                                  1, 1,
657                                  NULL,
658                                  NULL);
659
660                 if (rcode >= 0)
661                         memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
662         }
663
664
665         /* 
666          * GetBusInfo 
667          */
668
669         fib_init(fibptr);
670
671         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
672
673         memset(bus_info, 0, sizeof(*bus_info));
674
675         command = (struct aac_bus_info *)bus_info;
676
677         command->Command = cpu_to_le32(VM_Ioctl);
678         command->ObjType = cpu_to_le32(FT_DRIVE);
679         command->MethodId = cpu_to_le32(1);
680         command->CtlCmd = cpu_to_le32(GetBusInfo);
681
682         rcode = fib_send(ContainerCommand,
683                          fibptr,
684                          sizeof (*bus_info),
685                          FsaNormal,
686                          1, 1,
687                          NULL, NULL);
688
689         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
690                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
691                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
692         }
693
694         tmp = le32_to_cpu(dev->adapter_info.kernelrev);
695         printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n", 
696                         dev->name, 
697                         dev->id,
698                         tmp>>24,
699                         (tmp>>16)&0xff,
700                         tmp&0xff,
701                         le32_to_cpu(dev->adapter_info.kernelbuild),
702                         (int)sizeof(dev->supplement_adapter_info.BuildDate),
703                         dev->supplement_adapter_info.BuildDate);
704         tmp = le32_to_cpu(dev->adapter_info.monitorrev);
705         printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n", 
706                         dev->name, dev->id,
707                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
708                         le32_to_cpu(dev->adapter_info.monitorbuild));
709         tmp = le32_to_cpu(dev->adapter_info.biosrev);
710         printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n", 
711                         dev->name, dev->id,
712                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
713                         le32_to_cpu(dev->adapter_info.biosbuild));
714         if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
715                 printk(KERN_INFO "%s%d: serial %x\n",
716                         dev->name, dev->id,
717                         le32_to_cpu(dev->adapter_info.serial[0]));
718
719         dev->nondasd_support = 0;
720         dev->raid_scsi_mode = 0;
721         if(dev->adapter_info.options & AAC_OPT_NONDASD){
722                 dev->nondasd_support = 1;
723         }
724
725         /*
726          * If the firmware supports ROMB RAID/SCSI mode and we are currently
727          * in RAID/SCSI mode, set the flag. For now if in this mode we will
728          * force nondasd support on. If we decide to allow the non-dasd flag
729          * additional changes changes will have to be made to support
730          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
731          * changed to support the new dev->raid_scsi_mode flag instead of
732          * leaching off of the dev->nondasd_support flag. Also in linit.c the
733          * function aac_detect will have to be modified where it sets up the
734          * max number of channels based on the aac->nondasd_support flag only.
735          */
736         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
737             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
738                 dev->nondasd_support = 1;
739                 dev->raid_scsi_mode = 1;
740         }
741         if (dev->raid_scsi_mode != 0)
742                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
743                                 dev->name, dev->id);
744                 
745         if(nondasd != -1) {  
746                 dev->nondasd_support = (nondasd!=0);
747         }
748         if(dev->nondasd_support != 0){
749                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
750         }
751
752         dev->dac_support = 0;
753         if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
754                 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
755                 dev->dac_support = 1;
756         }
757
758         if(dacmode != -1) {
759                 dev->dac_support = (dacmode!=0);
760         }
761         if(dev->dac_support != 0) {
762                 if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL) &&
763                         !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL)) {
764                         printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
765                                 dev->name, dev->id);
766                 } else if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFULL) &&
767                         !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFULL)) {
768                         printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
769                                 dev->name, dev->id);
770                         dev->dac_support = 0;
771                 } else {
772                         printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
773                                 dev->name, dev->id);
774                         rcode = -ENOMEM;
775                 }
776         }
777         /* 
778          * 57 scatter gather elements 
779          */
780         dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
781                 sizeof(struct aac_fibhdr) -
782                 sizeof(struct aac_write) + sizeof(struct sgmap)) /
783                         sizeof(struct sgmap);
784         if (dev->dac_support) {
785                 /* 
786                  * 38 scatter gather elements 
787                  */
788                 dev->scsi_host_ptr->sg_tablesize =
789                         (dev->max_fib_size -
790                         sizeof(struct aac_fibhdr) -
791                         sizeof(struct aac_write64) +
792                         sizeof(struct sgmap64)) /
793                                 sizeof(struct sgmap64);
794         }
795         dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
796         if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
797                 /*
798                  * Worst case size that could cause sg overflow when
799                  * we break up SG elements that are larger than 64KB.
800                  * Would be nice if we could tell the SCSI layer what
801                  * the maximum SG element size can be. Worst case is
802                  * (sg_tablesize-1) 4KB elements with one 64KB
803                  * element.
804                  *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
805                  */
806                 dev->scsi_host_ptr->max_sectors =
807                   (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
808         }
809
810         fib_complete(fibptr);
811         fib_free(fibptr);
812
813         return rcode;
814 }
815
816
817 static void io_callback(void *context, struct fib * fibptr)
818 {
819         struct aac_dev *dev;
820         struct aac_read_reply *readreply;
821         struct scsi_cmnd *scsicmd;
822         u32 cid;
823
824         scsicmd = (struct scsi_cmnd *) context;
825
826         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
827         cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
828
829         dprintk((KERN_DEBUG "io_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3], jiffies));
830
831         if (fibptr == NULL)
832                 BUG();
833                 
834         if(scsicmd->use_sg)
835                 pci_unmap_sg(dev->pdev, 
836                         (struct scatterlist *)scsicmd->buffer,
837                         scsicmd->use_sg,
838                         scsicmd->sc_data_direction);
839         else if(scsicmd->request_bufflen)
840                 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
841                                  scsicmd->request_bufflen,
842                                  scsicmd->sc_data_direction);
843         readreply = (struct aac_read_reply *)fib_data(fibptr);
844         if (le32_to_cpu(readreply->status) == ST_OK)
845                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
846         else {
847 #ifdef AAC_DETAILED_STATUS_INFO
848                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
849                   le32_to_cpu(readreply->status));
850 #endif
851                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
852                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
853                                     HARDWARE_ERROR,
854                                     SENCODE_INTERNAL_TARGET_FAILURE,
855                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
856                                     0, 0);
857                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
858                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
859                     ? sizeof(scsicmd->sense_buffer)
860                     : sizeof(dev->fsa_dev[cid].sense_data));
861         }
862         fib_complete(fibptr);
863         fib_free(fibptr);
864
865         aac_io_done(scsicmd);
866 }
867
868 static int aac_read(struct scsi_cmnd * scsicmd, int cid)
869 {
870         u32 lba;
871         u32 count;
872         int status;
873
874         u16 fibsize;
875         struct aac_dev *dev;
876         struct fib * cmd_fibcontext;
877
878         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
879         /*
880          *      Get block address and transfer length
881          */
882         if (scsicmd->cmnd[0] == READ_6) /* 6 byte command */
883         {
884                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
885
886                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
887                 count = scsicmd->cmnd[4];
888
889                 if (count == 0)
890                         count = 256;
891         } else {
892                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
893
894                 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
895                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
896         }
897         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %u, t = %ld.\n",
898           smp_processor_id(), (unsigned long long)lba, jiffies));
899         /*
900          *      Alocate and initialize a Fib
901          */
902         if (!(cmd_fibcontext = fib_alloc(dev))) {
903                 return -1;
904         }
905
906         fib_init(cmd_fibcontext);
907
908         if (dev->dac_support == 1) {
909                 struct aac_read64 *readcmd;
910                 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
911                 readcmd->command = cpu_to_le32(VM_CtHostRead64);
912                 readcmd->cid = cpu_to_le16(cid);
913                 readcmd->sector_count = cpu_to_le16(count);
914                 readcmd->block = cpu_to_le32(lba);
915                 readcmd->pad   = 0;
916                 readcmd->flags = 0; 
917
918                 aac_build_sg64(scsicmd, &readcmd->sg);
919                 fibsize = sizeof(struct aac_read64) + 
920                         ((le32_to_cpu(readcmd->sg.count) - 1) * 
921                          sizeof (struct sgentry64));
922                 BUG_ON (fibsize > (sizeof(struct hw_fib) - 
923                                         sizeof(struct aac_fibhdr)));
924                 /*
925                  *      Now send the Fib to the adapter
926                  */
927                 status = fib_send(ContainerCommand64, 
928                           cmd_fibcontext, 
929                           fibsize, 
930                           FsaNormal, 
931                           0, 1, 
932                           (fib_callback) io_callback, 
933                           (void *) scsicmd);
934         } else {
935                 struct aac_read *readcmd;
936                 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
937                 readcmd->command = cpu_to_le32(VM_CtBlockRead);
938                 readcmd->cid = cpu_to_le32(cid);
939                 readcmd->block = cpu_to_le32(lba);
940                 readcmd->count = cpu_to_le32(count * 512);
941
942                 aac_build_sg(scsicmd, &readcmd->sg);
943                 fibsize = sizeof(struct aac_read) + 
944                         ((le32_to_cpu(readcmd->sg.count) - 1) * 
945                          sizeof (struct sgentry));
946                 BUG_ON (fibsize > (dev->max_fib_size -
947                                         sizeof(struct aac_fibhdr)));
948                 /*
949                  *      Now send the Fib to the adapter
950                  */
951                 status = fib_send(ContainerCommand, 
952                           cmd_fibcontext, 
953                           fibsize, 
954                           FsaNormal, 
955                           0, 1, 
956                           (fib_callback) io_callback, 
957                           (void *) scsicmd);
958         }
959
960         
961
962         /*
963          *      Check that the command queued to the controller
964          */
965         if (status == -EINPROGRESS) 
966                 return 0;
967                 
968         printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
969         /*
970          *      For some reason, the Fib didn't queue, return QUEUE_FULL
971          */
972         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
973         aac_io_done(scsicmd);
974         fib_complete(cmd_fibcontext);
975         fib_free(cmd_fibcontext);
976         return 0;
977 }
978
979 static int aac_write(struct scsi_cmnd * scsicmd, int cid)
980 {
981         u32 lba;
982         u32 count;
983         int status;
984         u16 fibsize;
985         struct aac_dev *dev;
986         struct fib * cmd_fibcontext;
987
988         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
989         /*
990          *      Get block address and transfer length
991          */
992         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
993         {
994                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
995                 count = scsicmd->cmnd[4];
996                 if (count == 0)
997                         count = 256;
998         } else {
999                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
1000                 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1001                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1002         }
1003         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %u, t = %ld.\n",
1004           smp_processor_id(), (unsigned long long)lba, jiffies));
1005         /*
1006          *      Allocate and initialize a Fib then setup a BlockWrite command
1007          */
1008         if (!(cmd_fibcontext = fib_alloc(dev))) {
1009                 scsicmd->result = DID_ERROR << 16;
1010                 aac_io_done(scsicmd);
1011                 return 0;
1012         }
1013         fib_init(cmd_fibcontext);
1014
1015         if(dev->dac_support == 1) {
1016                 struct aac_write64 *writecmd;
1017                 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
1018                 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1019                 writecmd->cid = cpu_to_le16(cid);
1020                 writecmd->sector_count = cpu_to_le16(count); 
1021                 writecmd->block = cpu_to_le32(lba);
1022                 writecmd->pad   = 0;
1023                 writecmd->flags = 0;
1024
1025                 aac_build_sg64(scsicmd, &writecmd->sg);
1026                 fibsize = sizeof(struct aac_write64) + 
1027                         ((le32_to_cpu(writecmd->sg.count) - 1) * 
1028                          sizeof (struct sgentry64));
1029                 BUG_ON (fibsize > (dev->max_fib_size -
1030                                         sizeof(struct aac_fibhdr)));
1031                 /*
1032                  *      Now send the Fib to the adapter
1033                  */
1034                 status = fib_send(ContainerCommand64, 
1035                           cmd_fibcontext, 
1036                           fibsize, 
1037                           FsaNormal, 
1038                           0, 1, 
1039                           (fib_callback) io_callback, 
1040                           (void *) scsicmd);
1041         } else {
1042                 struct aac_write *writecmd;
1043                 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
1044                 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1045                 writecmd->cid = cpu_to_le32(cid);
1046                 writecmd->block = cpu_to_le32(lba);
1047                 writecmd->count = cpu_to_le32(count * 512);
1048                 writecmd->sg.count = cpu_to_le32(1);
1049                 /* ->stable is not used - it did mean which type of write */
1050
1051                 aac_build_sg(scsicmd, &writecmd->sg);
1052                 fibsize = sizeof(struct aac_write) + 
1053                         ((le32_to_cpu(writecmd->sg.count) - 1) * 
1054                          sizeof (struct sgentry));
1055                 BUG_ON (fibsize > (dev->max_fib_size -
1056                                         sizeof(struct aac_fibhdr)));
1057                 /*
1058                  *      Now send the Fib to the adapter
1059                  */
1060                 status = fib_send(ContainerCommand, 
1061                           cmd_fibcontext, 
1062                           fibsize, 
1063                           FsaNormal, 
1064                           0, 1, 
1065                           (fib_callback) io_callback, 
1066                           (void *) scsicmd);
1067         }
1068
1069         /*
1070          *      Check that the command queued to the controller
1071          */
1072         if (status == -EINPROGRESS)
1073         {
1074                 return 0;
1075         }
1076
1077         printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
1078         /*
1079          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1080          */
1081         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1082         aac_io_done(scsicmd);
1083
1084         fib_complete(cmd_fibcontext);
1085         fib_free(cmd_fibcontext);
1086         return 0;
1087 }
1088
1089 static void synchronize_callback(void *context, struct fib *fibptr)
1090 {
1091         struct aac_synchronize_reply *synchronizereply;
1092         struct scsi_cmnd *cmd;
1093
1094         cmd = context;
1095
1096         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n", 
1097                                 smp_processor_id(), jiffies));
1098         BUG_ON(fibptr == NULL);
1099
1100
1101         synchronizereply = fib_data(fibptr);
1102         if (le32_to_cpu(synchronizereply->status) == CT_OK)
1103                 cmd->result = DID_OK << 16 | 
1104                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1105         else {
1106                 struct scsi_device *sdev = cmd->device;
1107                 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
1108                 u32 cid = ID_LUN_TO_CONTAINER(sdev->id, sdev->lun);
1109                 printk(KERN_WARNING 
1110                      "synchronize_callback: synchronize failed, status = %d\n",
1111                      le32_to_cpu(synchronizereply->status));
1112                 cmd->result = DID_OK << 16 | 
1113                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1114                 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1115                                     HARDWARE_ERROR,
1116                                     SENCODE_INTERNAL_TARGET_FAILURE,
1117                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1118                                     0, 0);
1119                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1120                   min(sizeof(dev->fsa_dev[cid].sense_data), 
1121                           sizeof(cmd->sense_buffer)));
1122         }
1123
1124         fib_complete(fibptr);
1125         fib_free(fibptr);
1126         aac_io_done(cmd);
1127 }
1128
1129 static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1130 {
1131         int status;
1132         struct fib *cmd_fibcontext;
1133         struct aac_synchronize *synchronizecmd;
1134         struct scsi_cmnd *cmd;
1135         struct scsi_device *sdev = scsicmd->device;
1136         int active = 0;
1137         unsigned long flags;
1138
1139         /*
1140          * Wait for all commands to complete to this specific
1141          * target (block).
1142          */
1143         spin_lock_irqsave(&sdev->list_lock, flags);
1144         list_for_each_entry(cmd, &sdev->cmd_list, list)
1145                 if (cmd != scsicmd && cmd->serial_number != 0) {
1146                         ++active;
1147                         break;
1148                 }
1149
1150         spin_unlock_irqrestore(&sdev->list_lock, flags);
1151
1152         /*
1153          *      Yield the processor (requeue for later)
1154          */
1155         if (active)
1156                 return SCSI_MLQUEUE_DEVICE_BUSY;
1157
1158         /*
1159          *      Allocate and initialize a Fib
1160          */
1161         if (!(cmd_fibcontext = 
1162             fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) 
1163                 return SCSI_MLQUEUE_HOST_BUSY;
1164
1165         fib_init(cmd_fibcontext);
1166
1167         synchronizecmd = fib_data(cmd_fibcontext);
1168         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1169         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1170         synchronizecmd->cid = cpu_to_le32(cid);
1171         synchronizecmd->count = 
1172              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1173
1174         /*
1175          *      Now send the Fib to the adapter
1176          */
1177         status = fib_send(ContainerCommand,
1178                   cmd_fibcontext,
1179                   sizeof(struct aac_synchronize),
1180                   FsaNormal,
1181                   0, 1,
1182                   (fib_callback)synchronize_callback,
1183                   (void *)scsicmd);
1184
1185         /*
1186          *      Check that the command queued to the controller
1187          */
1188         if (status == -EINPROGRESS)
1189                 return 0;
1190
1191         printk(KERN_WARNING 
1192                 "aac_synchronize: fib_send failed with status: %d.\n", status);
1193         fib_complete(cmd_fibcontext);
1194         fib_free(cmd_fibcontext);
1195         return SCSI_MLQUEUE_HOST_BUSY;
1196 }
1197
1198 /**
1199  *      aac_scsi_cmd()          -       Process SCSI command
1200  *      @scsicmd:               SCSI command block
1201  *
1202  *      Emulate a SCSI command and queue the required request for the
1203  *      aacraid firmware.
1204  */
1205  
1206 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1207 {
1208         u32 cid = 0;
1209         struct Scsi_Host *host = scsicmd->device->host;
1210         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1211         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1212         int cardtype = dev->cardtype;
1213         int ret;
1214         
1215         /*
1216          *      If the bus, id or lun is out of range, return fail
1217          *      Test does not apply to ID 16, the pseudo id for the controller
1218          *      itself.
1219          */
1220         if (scsicmd->device->id != host->this_id) {
1221                 if ((scsicmd->device->channel == 0) ){
1222                         if( (scsicmd->device->id >= dev->maximum_num_containers) || (scsicmd->device->lun != 0)){ 
1223                                 scsicmd->result = DID_NO_CONNECT << 16;
1224                                 scsicmd->scsi_done(scsicmd);
1225                                 return 0;
1226                         }
1227                         cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
1228
1229                         /*
1230                          *      If the target container doesn't exist, it may have
1231                          *      been newly created
1232                          */
1233                         if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1234                                 switch (scsicmd->cmnd[0]) {
1235                                 case INQUIRY:
1236                                 case READ_CAPACITY:
1237                                 case TEST_UNIT_READY:
1238                                         spin_unlock_irq(host->host_lock);
1239                                         probe_container(dev, cid);
1240                                         spin_lock_irq(host->host_lock);
1241                                         if (fsa_dev_ptr[cid].valid == 0) {
1242                                                 scsicmd->result = DID_NO_CONNECT << 16;
1243                                                 scsicmd->scsi_done(scsicmd);
1244                                                 return 0;
1245                                         }
1246                                 default:
1247                                         break;
1248                                 }
1249                         }
1250                         /*
1251                          *      If the target container still doesn't exist, 
1252                          *      return failure
1253                          */
1254                         if (fsa_dev_ptr[cid].valid == 0) {
1255                                 scsicmd->result = DID_BAD_TARGET << 16;
1256                                 scsicmd->scsi_done(scsicmd);
1257                                 return 0;
1258                         }
1259                 } else {  /* check for physical non-dasd devices */
1260                         if(dev->nondasd_support == 1){
1261                                 return aac_send_srb_fib(scsicmd);
1262                         } else {
1263                                 scsicmd->result = DID_NO_CONNECT << 16;
1264                                 scsicmd->scsi_done(scsicmd);
1265                                 return 0;
1266                         }
1267                 }
1268         }
1269         /*
1270          * else Command for the controller itself
1271          */
1272         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
1273                 (scsicmd->cmnd[0] != TEST_UNIT_READY)) 
1274         {
1275                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1276                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1277                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1278                             ILLEGAL_REQUEST,
1279                             SENCODE_INVALID_COMMAND,
1280                             ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1281                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1282                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1283                     ? sizeof(scsicmd->sense_buffer)
1284                     : sizeof(dev->fsa_dev[cid].sense_data));
1285                 scsicmd->scsi_done(scsicmd);
1286                 return 0;
1287         }
1288
1289
1290         /* Handle commands here that don't really require going out to the adapter */
1291         switch (scsicmd->cmnd[0]) {
1292         case INQUIRY:
1293         {
1294                 struct inquiry_data *inq_data_ptr;
1295
1296                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
1297                 inq_data_ptr = (struct inquiry_data *)scsicmd->request_buffer;
1298                 memset(inq_data_ptr, 0, sizeof (struct inquiry_data));
1299
1300                 inq_data_ptr->inqd_ver = 2;     /* claim compliance to SCSI-2 */
1301                 inq_data_ptr->inqd_dtq = 0x80;  /* set RMB bit to one indicating that the medium is removable */
1302                 inq_data_ptr->inqd_rdf = 2;     /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
1303                 inq_data_ptr->inqd_len = 31;
1304                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
1305                 inq_data_ptr->inqd_pad2= 0x32 ;  /*WBus16|Sync|CmdQue */
1306                 /*
1307                  *      Set the Vendor, Product, and Revision Level
1308                  *      see: <vendor>.c i.e. aac.c
1309                  */
1310                 if (scsicmd->device->id == host->this_id) {
1311                         setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), (sizeof(container_types)/sizeof(char *)));
1312                         inq_data_ptr->inqd_pdt = INQD_PDT_PROC; /* Processor device */
1313                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1314                         scsicmd->scsi_done(scsicmd);
1315                         return 0;
1316                 }
1317                 setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), fsa_dev_ptr[cid].type);
1318                 inq_data_ptr->inqd_pdt = INQD_PDT_DA;   /* Direct/random access device */
1319                 return aac_get_container_name(scsicmd, cid);
1320         }
1321         case READ_CAPACITY:
1322         {
1323                 u32 capacity;
1324                 char *cp;
1325
1326                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
1327                 if (fsa_dev_ptr[cid].size <= 0x100000000LL)
1328                         capacity = fsa_dev_ptr[cid].size - 1;
1329                 else
1330                         capacity = (u32)-1;
1331                 cp = scsicmd->request_buffer;
1332                 cp[0] = (capacity >> 24) & 0xff;
1333                 cp[1] = (capacity >> 16) & 0xff;
1334                 cp[2] = (capacity >> 8) & 0xff;
1335                 cp[3] = (capacity >> 0) & 0xff;
1336                 cp[4] = 0;
1337                 cp[5] = 0;
1338                 cp[6] = 2;
1339                 cp[7] = 0;
1340
1341                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1342                 scsicmd->scsi_done(scsicmd);
1343
1344                 return 0;
1345         }
1346
1347         case MODE_SENSE:
1348         {
1349                 char *mode_buf;
1350
1351                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1352                 mode_buf = scsicmd->request_buffer;
1353                 mode_buf[0] = 3;        /* Mode data length */
1354                 mode_buf[1] = 0;        /* Medium type - default */
1355                 mode_buf[2] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1356                 mode_buf[3] = 0;        /* Block descriptor length */
1357
1358                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1359                 scsicmd->scsi_done(scsicmd);
1360
1361                 return 0;
1362         }
1363         case MODE_SENSE_10:
1364         {
1365                 char *mode_buf;
1366
1367                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1368                 mode_buf = scsicmd->request_buffer;
1369                 mode_buf[0] = 0;        /* Mode data length (MSB) */
1370                 mode_buf[1] = 6;        /* Mode data length (LSB) */
1371                 mode_buf[2] = 0;        /* Medium type - default */
1372                 mode_buf[3] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1373                 mode_buf[4] = 0;        /* reserved */
1374                 mode_buf[5] = 0;        /* reserved */
1375                 mode_buf[6] = 0;        /* Block descriptor length (MSB) */
1376                 mode_buf[7] = 0;        /* Block descriptor length (LSB) */
1377
1378                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1379                 scsicmd->scsi_done(scsicmd);
1380
1381                 return 0;
1382         }
1383         case REQUEST_SENSE:
1384                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1385                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1386                 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1387                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1388                 scsicmd->scsi_done(scsicmd);
1389                 return 0;
1390
1391         case ALLOW_MEDIUM_REMOVAL:
1392                 dprintk((KERN_DEBUG "LOCK command.\n"));
1393                 if (scsicmd->cmnd[4])
1394                         fsa_dev_ptr[cid].locked = 1;
1395                 else
1396                         fsa_dev_ptr[cid].locked = 0;
1397
1398                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1399                 scsicmd->scsi_done(scsicmd);
1400                 return 0;
1401         /*
1402          *      These commands are all No-Ops
1403          */
1404         case TEST_UNIT_READY:
1405         case RESERVE:
1406         case RELEASE:
1407         case REZERO_UNIT:
1408         case REASSIGN_BLOCKS:
1409         case SEEK_10:
1410         case START_STOP:
1411                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1412                 scsicmd->scsi_done(scsicmd);
1413                 return 0;
1414         }
1415
1416         switch (scsicmd->cmnd[0]) 
1417         {
1418                 case READ_6:
1419                 case READ_10:
1420                         /*
1421                          *      Hack to keep track of ordinal number of the device that
1422                          *      corresponds to a container. Needed to convert
1423                          *      containers to /dev/sd device names
1424                          */
1425                          
1426                         spin_unlock_irq(host->host_lock);
1427                         if  (scsicmd->request->rq_disk)
1428                                 memcpy(fsa_dev_ptr[cid].devname,
1429                                         scsicmd->request->rq_disk->disk_name,
1430                                         8);
1431
1432                         ret = aac_read(scsicmd, cid);
1433                         spin_lock_irq(host->host_lock);
1434                         return ret;
1435
1436                 case WRITE_6:
1437                 case WRITE_10:
1438                         spin_unlock_irq(host->host_lock);
1439                         ret = aac_write(scsicmd, cid);
1440                         spin_lock_irq(host->host_lock);
1441                         return ret;
1442
1443                 case SYNCHRONIZE_CACHE:
1444                         /* Issue FIB to tell Firmware to flush it's cache */
1445                         return aac_synchronize(scsicmd, cid);
1446                         
1447                 default:
1448                         /*
1449                          *      Unhandled commands
1450                          */
1451                         dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
1452                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1453                         set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1454                                 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1455                                 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1456                         memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1457                           (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1458                             ? sizeof(scsicmd->sense_buffer)
1459                             : sizeof(dev->fsa_dev[cid].sense_data));
1460                         scsicmd->scsi_done(scsicmd);
1461                         return 0;
1462         }
1463 }
1464
1465 static int query_disk(struct aac_dev *dev, void __user *arg)
1466 {
1467         struct aac_query_disk qd;
1468         struct fsa_dev_info *fsa_dev_ptr;
1469
1470         fsa_dev_ptr = dev->fsa_dev;
1471         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1472                 return -EFAULT;
1473         if (qd.cnum == -1)
1474                 qd.cnum = ID_LUN_TO_CONTAINER(qd.id, qd.lun);
1475         else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) 
1476         {
1477                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1478                         return -EINVAL;
1479                 qd.instance = dev->scsi_host_ptr->host_no;
1480                 qd.bus = 0;
1481                 qd.id = CONTAINER_TO_ID(qd.cnum);
1482                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1483         }
1484         else return -EINVAL;
1485
1486         qd.valid = fsa_dev_ptr[qd.cnum].valid;
1487         qd.locked = fsa_dev_ptr[qd.cnum].locked;
1488         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1489
1490         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1491                 qd.unmapped = 1;
1492         else
1493                 qd.unmapped = 0;
1494
1495         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1496           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1497
1498         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1499                 return -EFAULT;
1500         return 0;
1501 }
1502
1503 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1504 {
1505         struct aac_delete_disk dd;
1506         struct fsa_dev_info *fsa_dev_ptr;
1507
1508         fsa_dev_ptr = dev->fsa_dev;
1509
1510         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1511                 return -EFAULT;
1512
1513         if (dd.cnum >= dev->maximum_num_containers)
1514                 return -EINVAL;
1515         /*
1516          *      Mark this container as being deleted.
1517          */
1518         fsa_dev_ptr[dd.cnum].deleted = 1;
1519         /*
1520          *      Mark the container as no longer valid
1521          */
1522         fsa_dev_ptr[dd.cnum].valid = 0;
1523         return 0;
1524 }
1525
1526 static int delete_disk(struct aac_dev *dev, void __user *arg)
1527 {
1528         struct aac_delete_disk dd;
1529         struct fsa_dev_info *fsa_dev_ptr;
1530
1531         fsa_dev_ptr = dev->fsa_dev;
1532
1533         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1534                 return -EFAULT;
1535
1536         if (dd.cnum >= dev->maximum_num_containers)
1537                 return -EINVAL;
1538         /*
1539          *      If the container is locked, it can not be deleted by the API.
1540          */
1541         if (fsa_dev_ptr[dd.cnum].locked)
1542                 return -EBUSY;
1543         else {
1544                 /*
1545                  *      Mark the container as no longer being valid.
1546                  */
1547                 fsa_dev_ptr[dd.cnum].valid = 0;
1548                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1549                 return 0;
1550         }
1551 }
1552
1553 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1554 {
1555         switch (cmd) {
1556         case FSACTL_QUERY_DISK:
1557                 return query_disk(dev, arg);
1558         case FSACTL_DELETE_DISK:
1559                 return delete_disk(dev, arg);
1560         case FSACTL_FORCE_DELETE_DISK:
1561                 return force_delete_disk(dev, arg);
1562         case FSACTL_GET_CONTAINERS:
1563                 return aac_get_containers(dev);
1564         default:
1565                 return -ENOTTY;
1566         }
1567 }
1568
1569 /**
1570  *
1571  * aac_srb_callback
1572  * @context: the context set in the fib - here it is scsi cmd
1573  * @fibptr: pointer to the fib
1574  *
1575  * Handles the completion of a scsi command to a non dasd device
1576  *
1577  */
1578
1579 static void aac_srb_callback(void *context, struct fib * fibptr)
1580 {
1581         struct aac_dev *dev;
1582         struct aac_srb_reply *srbreply;
1583         struct scsi_cmnd *scsicmd;
1584
1585         scsicmd = (struct scsi_cmnd *) context;
1586         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1587
1588         if (fibptr == NULL)
1589                 BUG();
1590
1591         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1592
1593         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
1594         /*
1595          *      Calculate resid for sg 
1596          */
1597          
1598         scsicmd->resid = scsicmd->request_bufflen - 
1599                 le32_to_cpu(srbreply->data_xfer_length);
1600
1601         if(scsicmd->use_sg)
1602                 pci_unmap_sg(dev->pdev, 
1603                         (struct scatterlist *)scsicmd->buffer,
1604                         scsicmd->use_sg,
1605                         scsicmd->sc_data_direction);
1606         else if(scsicmd->request_bufflen)
1607                 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1608                         scsicmd->sc_data_direction);
1609
1610         /*
1611          * First check the fib status
1612          */
1613
1614         if (le32_to_cpu(srbreply->status) != ST_OK){
1615                 int len;
1616                 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1617                 len = (le32_to_cpu(srbreply->sense_data_size) > 
1618                                 sizeof(scsicmd->sense_buffer)) ?
1619                                 sizeof(scsicmd->sense_buffer) : 
1620                                 le32_to_cpu(srbreply->sense_data_size);
1621                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1622                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1623         }
1624
1625         /*
1626          * Next check the srb status
1627          */
1628         switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1629         case SRB_STATUS_ERROR_RECOVERY:
1630         case SRB_STATUS_PENDING:
1631         case SRB_STATUS_SUCCESS:
1632                 if(scsicmd->cmnd[0] == INQUIRY ){
1633                         u8 b;
1634                         u8 b1;
1635                         /* We can't expose disk devices because we can't tell whether they
1636                          * are the raw container drives or stand alone drives.  If they have
1637                          * the removable bit set then we should expose them though.
1638                          */
1639                         b = (*(u8*)scsicmd->buffer)&0x1f;
1640                         b1 = ((u8*)scsicmd->buffer)[1];
1641                         if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER 
1642                                         || (b==TYPE_DISK && (b1&0x80)) ){
1643                                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1644                         /*
1645                          * We will allow disk devices if in RAID/SCSI mode and
1646                          * the channel is 2
1647                          */
1648                         } else if ((dev->raid_scsi_mode) &&
1649                                         (scsicmd->device->channel == 2)) {
1650                                 scsicmd->result = DID_OK << 16 | 
1651                                                 COMMAND_COMPLETE << 8;
1652                         } else {
1653                                 scsicmd->result = DID_NO_CONNECT << 16 | 
1654                                                 COMMAND_COMPLETE << 8;
1655                         }
1656                 } else {
1657                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1658                 }
1659                 break;
1660         case SRB_STATUS_DATA_OVERRUN:
1661                 switch(scsicmd->cmnd[0]){
1662                 case  READ_6:
1663                 case  WRITE_6:
1664                 case  READ_10:
1665                 case  WRITE_10:
1666                 case  READ_12:
1667                 case  WRITE_12:
1668                         if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1669                                 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1670                         } else {
1671                                 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1672                         }
1673                         scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1674                         break;
1675                 case INQUIRY: {
1676                         u8 b;
1677                         u8 b1;
1678                         /* We can't expose disk devices because we can't tell whether they
1679                         * are the raw container drives or stand alone drives
1680                         */
1681                         b = (*(u8*)scsicmd->buffer)&0x0f;
1682                         b1 = ((u8*)scsicmd->buffer)[1];
1683                         if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1684                                         || (b==TYPE_DISK && (b1&0x80)) ){
1685                                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1686                         /*
1687                          * We will allow disk devices if in RAID/SCSI mode and
1688                          * the channel is 2
1689                          */
1690                         } else if ((dev->raid_scsi_mode) &&
1691                                         (scsicmd->device->channel == 2)) {
1692                                 scsicmd->result = DID_OK << 16 | 
1693                                                 COMMAND_COMPLETE << 8;
1694                         } else {
1695                                 scsicmd->result = DID_NO_CONNECT << 16 | 
1696                                                 COMMAND_COMPLETE << 8;
1697                         }
1698                         break;
1699                 }
1700                 default:
1701                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1702                         break;
1703                 }
1704                 break;
1705         case SRB_STATUS_ABORTED:
1706                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1707                 break;
1708         case SRB_STATUS_ABORT_FAILED:
1709                 // Not sure about this one - but assuming the hba was trying to abort for some reason
1710                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1711                 break;
1712         case SRB_STATUS_PARITY_ERROR:
1713                 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1714                 break;
1715         case SRB_STATUS_NO_DEVICE:
1716         case SRB_STATUS_INVALID_PATH_ID:
1717         case SRB_STATUS_INVALID_TARGET_ID:
1718         case SRB_STATUS_INVALID_LUN:
1719         case SRB_STATUS_SELECTION_TIMEOUT:
1720                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1721                 break;
1722
1723         case SRB_STATUS_COMMAND_TIMEOUT:
1724         case SRB_STATUS_TIMEOUT:
1725                 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
1726                 break;
1727
1728         case SRB_STATUS_BUSY:
1729                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1730                 break;
1731
1732         case SRB_STATUS_BUS_RESET:
1733                 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
1734                 break;
1735
1736         case SRB_STATUS_MESSAGE_REJECTED:
1737                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
1738                 break;
1739         case SRB_STATUS_REQUEST_FLUSHED:
1740         case SRB_STATUS_ERROR:
1741         case SRB_STATUS_INVALID_REQUEST:
1742         case SRB_STATUS_REQUEST_SENSE_FAILED:
1743         case SRB_STATUS_NO_HBA:
1744         case SRB_STATUS_UNEXPECTED_BUS_FREE:
1745         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
1746         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
1747         case SRB_STATUS_DELAYED_RETRY:
1748         case SRB_STATUS_BAD_FUNCTION:
1749         case SRB_STATUS_NOT_STARTED:
1750         case SRB_STATUS_NOT_IN_USE:
1751         case SRB_STATUS_FORCE_ABORT:
1752         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
1753         default:
1754 #ifdef AAC_DETAILED_STATUS_INFO
1755                 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
1756                         le32_to_cpu(srbreply->srb_status) & 0x3F,
1757                         aac_get_status_string(
1758                                 le32_to_cpu(srbreply->srb_status) & 0x3F), 
1759                         scsicmd->cmnd[0], 
1760                         le32_to_cpu(srbreply->scsi_status));
1761 #endif
1762                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1763                 break;
1764         }
1765         if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){  // Check Condition
1766                 int len;
1767                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
1768                 len = (le32_to_cpu(srbreply->sense_data_size) > 
1769                                 sizeof(scsicmd->sense_buffer)) ?
1770                                 sizeof(scsicmd->sense_buffer) :
1771                                 le32_to_cpu(srbreply->sense_data_size);
1772 #ifdef AAC_DETAILED_STATUS_INFO
1773                 dprintk((KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n", 
1774                                         le32_to_cpu(srbreply->status), len));
1775 #endif
1776                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1777                 
1778         }
1779         /*
1780          * OR in the scsi status (already shifted up a bit)
1781          */
1782         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
1783
1784         fib_complete(fibptr);
1785         fib_free(fibptr);
1786         aac_io_done(scsicmd);
1787 }
1788
1789 /**
1790  *
1791  * aac_send_scb_fib
1792  * @scsicmd: the scsi command block
1793  *
1794  * This routine will form a FIB and fill in the aac_srb from the 
1795  * scsicmd passed in.
1796  */
1797
1798 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
1799 {
1800         struct fib* cmd_fibcontext;
1801         struct aac_dev* dev;
1802         int status;
1803         struct aac_srb *srbcmd;
1804         u16 fibsize;
1805         u32 flag;
1806         u32 timeout;
1807
1808         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1809         if (scsicmd->device->id >= dev->maximum_num_physicals || 
1810                         scsicmd->device->lun > 7) {
1811                 scsicmd->result = DID_NO_CONNECT << 16;
1812                 scsicmd->scsi_done(scsicmd);
1813                 return 0;
1814         }
1815
1816         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1817         switch(scsicmd->sc_data_direction){
1818         case DMA_TO_DEVICE:
1819                 flag = SRB_DataOut;
1820                 break;
1821         case DMA_BIDIRECTIONAL:
1822                 flag = SRB_DataIn | SRB_DataOut;
1823                 break;
1824         case DMA_FROM_DEVICE:
1825                 flag = SRB_DataIn;
1826                 break;
1827         case DMA_NONE:
1828         default:        /* shuts up some versions of gcc */
1829                 flag = SRB_NoDataXfer;
1830                 break;
1831         }
1832
1833
1834         /*
1835          *      Allocate and initialize a Fib then setup a BlockWrite command
1836          */
1837         if (!(cmd_fibcontext = fib_alloc(dev))) {
1838                 return -1;
1839         }
1840         fib_init(cmd_fibcontext);
1841
1842         srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
1843         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1844         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
1845         srbcmd->id   = cpu_to_le32(scsicmd->device->id);
1846         srbcmd->lun      = cpu_to_le32(scsicmd->device->lun);
1847         srbcmd->flags    = cpu_to_le32(flag);
1848         timeout = (scsicmd->timeout-jiffies)/HZ;
1849         if(timeout == 0){
1850                 timeout = 1;
1851         }
1852         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1853         srbcmd->retry_limit = 0; /* Obsolete parameter */
1854         srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
1855         
1856         if( dev->dac_support == 1 ) {
1857                 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
1858                 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1859
1860                 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1861                 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1862                 /*
1863                  *      Build Scatter/Gather list
1864                  */
1865                 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1866                         ((le32_to_cpu(srbcmd->sg.count) & 0xff) * 
1867                          sizeof (struct sgentry64));
1868                 BUG_ON (fibsize > (dev->max_fib_size -
1869                                         sizeof(struct aac_fibhdr)));
1870
1871                 /*
1872                  *      Now send the Fib to the adapter
1873                  */
1874                 status = fib_send(ScsiPortCommand64, cmd_fibcontext, 
1875                                 fibsize, FsaNormal, 0, 1,
1876                                   (fib_callback) aac_srb_callback, 
1877                                   (void *) scsicmd);
1878         } else {
1879                 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
1880                 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1881
1882                 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1883                 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1884                 /*
1885                  *      Build Scatter/Gather list
1886                  */
1887                 fibsize = sizeof (struct aac_srb) + 
1888                         (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) * 
1889                          sizeof (struct sgentry));
1890                 BUG_ON (fibsize > (dev->max_fib_size -
1891                                         sizeof(struct aac_fibhdr)));
1892
1893                 /*
1894                  *      Now send the Fib to the adapter
1895                  */
1896                 status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
1897                                   (fib_callback) aac_srb_callback, (void *) scsicmd);
1898         }
1899         /*
1900          *      Check that the command queued to the controller
1901          */
1902         if (status == -EINPROGRESS){
1903                 return 0;
1904         }
1905
1906         printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
1907         fib_complete(cmd_fibcontext);
1908         fib_free(cmd_fibcontext);
1909
1910         return -1;
1911 }
1912
1913 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
1914 {
1915         struct aac_dev *dev;
1916         unsigned long byte_count = 0;
1917
1918         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1919         // Get rid of old data
1920         psg->count = 0;
1921         psg->sg[0].addr = 0;
1922         psg->sg[0].count = 0;  
1923         if (scsicmd->use_sg) {
1924                 struct scatterlist *sg;
1925                 int i;
1926                 int sg_count;
1927                 sg = (struct scatterlist *) scsicmd->request_buffer;
1928
1929                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1930                         scsicmd->sc_data_direction);
1931                 psg->count = cpu_to_le32(sg_count);
1932
1933                 byte_count = 0;
1934
1935                 for (i = 0; i < sg_count; i++) {
1936                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
1937                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1938                         byte_count += sg_dma_len(sg);
1939                         sg++;
1940                 }
1941                 /* hba wants the size to be exact */
1942                 if(byte_count > scsicmd->request_bufflen){
1943                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
1944                                 (byte_count - scsicmd->request_bufflen);
1945                         psg->sg[i-1].count = cpu_to_le32(temp);
1946                         byte_count = scsicmd->request_bufflen;
1947                 }
1948                 /* Check for command underflow */
1949                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
1950                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
1951                                         byte_count, scsicmd->underflow);
1952                 }
1953         }
1954         else if(scsicmd->request_bufflen) {
1955                 dma_addr_t addr; 
1956                 addr = pci_map_single(dev->pdev,
1957                                 scsicmd->request_buffer,
1958                                 scsicmd->request_bufflen,
1959                                 scsicmd->sc_data_direction);
1960                 psg->count = cpu_to_le32(1);
1961                 psg->sg[0].addr = cpu_to_le32(addr);
1962                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
1963                 scsicmd->SCp.dma_handle = addr;
1964                 byte_count = scsicmd->request_bufflen;
1965         }
1966         return byte_count;
1967 }
1968
1969
1970 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
1971 {
1972         struct aac_dev *dev;
1973         unsigned long byte_count = 0;
1974         u64 addr;
1975
1976         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1977         // Get rid of old data
1978         psg->count = 0;
1979         psg->sg[0].addr[0] = 0;
1980         psg->sg[0].addr[1] = 0;
1981         psg->sg[0].count = 0;
1982         if (scsicmd->use_sg) {
1983                 struct scatterlist *sg;
1984                 int i;
1985                 int sg_count;
1986                 sg = (struct scatterlist *) scsicmd->request_buffer;
1987
1988                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1989                         scsicmd->sc_data_direction);
1990                 psg->count = cpu_to_le32(sg_count);
1991
1992                 byte_count = 0;
1993
1994                 for (i = 0; i < sg_count; i++) {
1995                         addr = sg_dma_address(sg);
1996                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
1997                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
1998                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1999                         byte_count += sg_dma_len(sg);
2000                         sg++;
2001                 }
2002                 /* hba wants the size to be exact */
2003                 if(byte_count > scsicmd->request_bufflen){
2004                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
2005                                 (byte_count - scsicmd->request_bufflen);
2006                         psg->sg[i-1].count = cpu_to_le32(temp);
2007                         byte_count = scsicmd->request_bufflen;
2008                 }
2009                 /* Check for command underflow */
2010                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2011                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2012                                         byte_count, scsicmd->underflow);
2013                 }
2014         }
2015         else if(scsicmd->request_bufflen) {
2016                 u64 addr; 
2017                 addr = pci_map_single(dev->pdev,
2018                                 scsicmd->request_buffer,
2019                                 scsicmd->request_bufflen,
2020                                 scsicmd->sc_data_direction);
2021                 psg->count = cpu_to_le32(1);
2022                 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2023                 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
2024                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
2025                 scsicmd->SCp.dma_handle = addr;
2026                 byte_count = scsicmd->request_bufflen;
2027         }
2028         return byte_count;
2029 }
2030
2031 #ifdef AAC_DETAILED_STATUS_INFO
2032
2033 struct aac_srb_status_info {
2034         u32     status;
2035         char    *str;
2036 };
2037
2038
2039 static struct aac_srb_status_info srb_status_info[] = {
2040         { SRB_STATUS_PENDING,           "Pending Status"},
2041         { SRB_STATUS_SUCCESS,           "Success"},
2042         { SRB_STATUS_ABORTED,           "Aborted Command"},
2043         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
2044         { SRB_STATUS_ERROR,             "Error Event"}, 
2045         { SRB_STATUS_BUSY,              "Device Busy"},
2046         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
2047         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
2048         { SRB_STATUS_NO_DEVICE,         "No Device"},
2049         { SRB_STATUS_TIMEOUT,           "Timeout"},
2050         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2051         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
2052         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
2053         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
2054         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
2055         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2056         { SRB_STATUS_NO_HBA,            "No HBA"},
2057         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
2058         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2059         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2060         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2061         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
2062         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
2063         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"}, 
2064         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2065         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
2066         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
2067         { SRB_STATUS_NOT_STARTED,       "Not Started"},
2068         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
2069         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
2070         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2071         { 0xff,                         "Unknown Error"}
2072 };
2073
2074 char *aac_get_status_string(u32 status)
2075 {
2076         int i;
2077
2078         for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
2079                 if(srb_status_info[i].status == status){
2080                         return srb_status_info[i].str;
2081                 }
2082         }
2083
2084         return "Bad Status Code";
2085 }
2086
2087 #endif