Merge tag 'gcc-plugins-v4.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / scsi / aacraid / aachba.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc.
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2010 Adaptec, Inc.
9  *               2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/types.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/uaccess.h>
36 #include <linux/highmem.h> /* For flush_kernel_dcache_page */
37 #include <linux/module.h>
38
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_host.h>
43
44 #include "aacraid.h"
45
46 /* values for inqd_pdt: Peripheral device type in plain English */
47 #define INQD_PDT_DA     0x00    /* Direct-access (DISK) device */
48 #define INQD_PDT_PROC   0x03    /* Processor device */
49 #define INQD_PDT_CHNGR  0x08    /* Changer (jukebox, scsi2) */
50 #define INQD_PDT_COMM   0x09    /* Communication device (scsi2) */
51 #define INQD_PDT_NOLUN2 0x1f    /* Unknown Device (scsi2) */
52 #define INQD_PDT_NOLUN  0x7f    /* Logical Unit Not Present */
53
54 #define INQD_PDT_DMASK  0x1F    /* Peripheral Device Type Mask */
55 #define INQD_PDT_QMASK  0xE0    /* Peripheral Device Qualifer Mask */
56
57 /*
58  *      Sense codes
59  */
60
61 #define SENCODE_NO_SENSE                        0x00
62 #define SENCODE_END_OF_DATA                     0x00
63 #define SENCODE_BECOMING_READY                  0x04
64 #define SENCODE_INIT_CMD_REQUIRED               0x04
65 #define SENCODE_PARAM_LIST_LENGTH_ERROR         0x1A
66 #define SENCODE_INVALID_COMMAND                 0x20
67 #define SENCODE_LBA_OUT_OF_RANGE                0x21
68 #define SENCODE_INVALID_CDB_FIELD               0x24
69 #define SENCODE_LUN_NOT_SUPPORTED               0x25
70 #define SENCODE_INVALID_PARAM_FIELD             0x26
71 #define SENCODE_PARAM_NOT_SUPPORTED             0x26
72 #define SENCODE_PARAM_VALUE_INVALID             0x26
73 #define SENCODE_RESET_OCCURRED                  0x29
74 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET     0x3E
75 #define SENCODE_INQUIRY_DATA_CHANGED            0x3F
76 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED     0x39
77 #define SENCODE_DIAGNOSTIC_FAILURE              0x40
78 #define SENCODE_INTERNAL_TARGET_FAILURE         0x44
79 #define SENCODE_INVALID_MESSAGE_ERROR           0x49
80 #define SENCODE_LUN_FAILED_SELF_CONFIG          0x4c
81 #define SENCODE_OVERLAPPED_COMMAND              0x4E
82
83 /*
84  *      Additional sense codes
85  */
86
87 #define ASENCODE_NO_SENSE                       0x00
88 #define ASENCODE_END_OF_DATA                    0x05
89 #define ASENCODE_BECOMING_READY                 0x01
90 #define ASENCODE_INIT_CMD_REQUIRED              0x02
91 #define ASENCODE_PARAM_LIST_LENGTH_ERROR        0x00
92 #define ASENCODE_INVALID_COMMAND                0x00
93 #define ASENCODE_LBA_OUT_OF_RANGE               0x00
94 #define ASENCODE_INVALID_CDB_FIELD              0x00
95 #define ASENCODE_LUN_NOT_SUPPORTED              0x00
96 #define ASENCODE_INVALID_PARAM_FIELD            0x00
97 #define ASENCODE_PARAM_NOT_SUPPORTED            0x01
98 #define ASENCODE_PARAM_VALUE_INVALID            0x02
99 #define ASENCODE_RESET_OCCURRED                 0x00
100 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET    0x00
101 #define ASENCODE_INQUIRY_DATA_CHANGED           0x03
102 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED    0x00
103 #define ASENCODE_DIAGNOSTIC_FAILURE             0x80
104 #define ASENCODE_INTERNAL_TARGET_FAILURE        0x00
105 #define ASENCODE_INVALID_MESSAGE_ERROR          0x00
106 #define ASENCODE_LUN_FAILED_SELF_CONFIG         0x00
107 #define ASENCODE_OVERLAPPED_COMMAND             0x00
108
109 #define BYTE0(x) (unsigned char)(x)
110 #define BYTE1(x) (unsigned char)((x) >> 8)
111 #define BYTE2(x) (unsigned char)((x) >> 16)
112 #define BYTE3(x) (unsigned char)((x) >> 24)
113
114 /* MODE_SENSE data format */
115 typedef struct {
116         struct {
117                 u8      data_length;
118                 u8      med_type;
119                 u8      dev_par;
120                 u8      bd_length;
121         } __attribute__((packed)) hd;
122         struct {
123                 u8      dens_code;
124                 u8      block_count[3];
125                 u8      reserved;
126                 u8      block_length[3];
127         } __attribute__((packed)) bd;
128                 u8      mpc_buf[3];
129 } __attribute__((packed)) aac_modep_data;
130
131 /* MODE_SENSE_10 data format */
132 typedef struct {
133         struct {
134                 u8      data_length[2];
135                 u8      med_type;
136                 u8      dev_par;
137                 u8      rsrvd[2];
138                 u8      bd_length[2];
139         } __attribute__((packed)) hd;
140         struct {
141                 u8      dens_code;
142                 u8      block_count[3];
143                 u8      reserved;
144                 u8      block_length[3];
145         } __attribute__((packed)) bd;
146                 u8      mpc_buf[3];
147 } __attribute__((packed)) aac_modep10_data;
148
149 /*------------------------------------------------------------------------------
150  *              S T R U C T S / T Y P E D E F S
151  *----------------------------------------------------------------------------*/
152 /* SCSI inquiry data */
153 struct inquiry_data {
154         u8 inqd_pdt;    /* Peripheral qualifier | Peripheral Device Type */
155         u8 inqd_dtq;    /* RMB | Device Type Qualifier */
156         u8 inqd_ver;    /* ISO version | ECMA version | ANSI-approved version */
157         u8 inqd_rdf;    /* AENC | TrmIOP | Response data format */
158         u8 inqd_len;    /* Additional length (n-4) */
159         u8 inqd_pad1[2];/* Reserved - must be zero */
160         u8 inqd_pad2;   /* RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
161         u8 inqd_vid[8]; /* Vendor ID */
162         u8 inqd_pid[16];/* Product ID */
163         u8 inqd_prl[4]; /* Product Revision Level */
164 };
165
166 /* Added for VPD 0x83 */
167 typedef struct {
168         u8 CodeSet:4;   /* VPD_CODE_SET */
169         u8 Reserved:4;
170         u8 IdentifierType:4;    /* VPD_IDENTIFIER_TYPE */
171         u8 Reserved2:4;
172         u8 Reserved3;
173         u8 IdentifierLength;
174         u8 VendId[8];
175         u8 ProductId[16];
176         u8 SerialNumber[8];     /* SN in ASCII */
177
178 } TVPD_ID_Descriptor_Type_1;
179
180 typedef struct {
181         u8 CodeSet:4;   /* VPD_CODE_SET */
182         u8 Reserved:4;
183         u8 IdentifierType:4;    /* VPD_IDENTIFIER_TYPE */
184         u8 Reserved2:4;
185         u8 Reserved3;
186         u8 IdentifierLength;
187         struct TEU64Id {
188                 u32 Serial;
189                  /* The serial number supposed to be 40 bits,
190                   * bit we only support 32, so make the last byte zero. */
191                 u8 Reserved;
192                 u8 VendId[3];
193         } EU64Id;
194
195 } TVPD_ID_Descriptor_Type_2;
196
197 typedef struct {
198         u8 DeviceType:5;
199         u8 DeviceTypeQualifier:3;
200         u8 PageCode;
201         u8 Reserved;
202         u8 PageLength;
203         TVPD_ID_Descriptor_Type_1 IdDescriptorType1;
204         TVPD_ID_Descriptor_Type_2 IdDescriptorType2;
205
206 } TVPD_Page83;
207
208 /*
209  *              M O D U L E   G L O B A L S
210  */
211
212 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
213 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
214 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
215 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
216                                 struct aac_raw_io2 *rio2, int sg_max);
217 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
218                                 int pages, int nseg, int nseg_new);
219 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
220 #ifdef AAC_DETAILED_STATUS_INFO
221 static char *aac_get_status_string(u32 status);
222 #endif
223
224 /*
225  *      Non dasd selection is handled entirely in aachba now
226  */
227
228 static int nondasd = -1;
229 static int aac_cache = 2;       /* WCE=0 to avoid performance problems */
230 static int dacmode = -1;
231 int aac_msi;
232 int aac_commit = -1;
233 int startup_timeout = 180;
234 int aif_timeout = 120;
235 int aac_sync_mode;  /* Only Sync. transfer - disabled */
236 int aac_convert_sgl = 1;        /* convert non-conformable s/g list - enabled */
237
238 module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
239 MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
240         " 0=off, 1=on");
241 module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
242 MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
243         " 0=off, 1=on");
244 module_param(nondasd, int, S_IRUGO|S_IWUSR);
245 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
246         " 0=off, 1=on");
247 module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
248 MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
249         "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
250         "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
251         "\tbit 2 - Disable only if Battery is protecting Cache");
252 module_param(dacmode, int, S_IRUGO|S_IWUSR);
253 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
254         " 0=off, 1=on");
255 module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
256 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
257         " adapter for foreign arrays.\n"
258         "This is typically needed in systems that do not have a BIOS."
259         " 0=off, 1=on");
260 module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
261 MODULE_PARM_DESC(msi, "IRQ handling."
262         " 0=PIC(default), 1=MSI, 2=MSI-X)");
263 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
264 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
265         " adapter to have it's kernel up and\n"
266         "running. This is typically adjusted for large systems that do not"
267         " have a BIOS.");
268 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
269 MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
270         " applications to pick up AIFs before\n"
271         "deregistering them. This is typically adjusted for heavily burdened"
272         " systems.");
273
274 int numacb = -1;
275 module_param(numacb, int, S_IRUGO|S_IWUSR);
276 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
277         " blocks (FIB) allocated. Valid values are 512 and down. Default is"
278         " to use suggestion from Firmware.");
279
280 int acbsize = -1;
281 module_param(acbsize, int, S_IRUGO|S_IWUSR);
282 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
283         " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
284         " suggestion from Firmware.");
285
286 int update_interval = 30 * 60;
287 module_param(update_interval, int, S_IRUGO|S_IWUSR);
288 MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
289         " updates issued to adapter.");
290
291 int check_interval = 24 * 60 * 60;
292 module_param(check_interval, int, S_IRUGO|S_IWUSR);
293 MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
294         " checks.");
295
296 int aac_check_reset = 1;
297 module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
298 MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
299         " adapter. a value of -1 forces the reset to adapters programmed to"
300         " ignore it.");
301
302 int expose_physicals = -1;
303 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
304 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
305         " -1=protect 0=off, 1=on");
306
307 int aac_reset_devices;
308 module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
309 MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
310
311 int aac_wwn = 1;
312 module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
313 MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
314         "\t0 - Disable\n"
315         "\t1 - Array Meta Data Signature (default)\n"
316         "\t2 - Adapter Serial Number");
317
318
319 static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
320                 struct fib *fibptr) {
321         struct scsi_device *device;
322
323         if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
324                 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
325                 aac_fib_complete(fibptr);
326                 return 0;
327         }
328         scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
329         device = scsicmd->device;
330         if (unlikely(!device || !scsi_device_online(device))) {
331                 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
332                 aac_fib_complete(fibptr);
333                 return 0;
334         }
335         return 1;
336 }
337
338 /**
339  *      aac_get_config_status   -       check the adapter configuration
340  *      @common: adapter to query
341  *
342  *      Query config status, and commit the configuration if needed.
343  */
344 int aac_get_config_status(struct aac_dev *dev, int commit_flag)
345 {
346         int status = 0;
347         struct fib * fibptr;
348
349         if (!(fibptr = aac_fib_alloc(dev)))
350                 return -ENOMEM;
351
352         aac_fib_init(fibptr);
353         {
354                 struct aac_get_config_status *dinfo;
355                 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
356
357                 dinfo->command = cpu_to_le32(VM_ContainerConfig);
358                 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
359                 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
360         }
361
362         status = aac_fib_send(ContainerCommand,
363                             fibptr,
364                             sizeof (struct aac_get_config_status),
365                             FsaNormal,
366                             1, 1,
367                             NULL, NULL);
368         if (status < 0) {
369                 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
370         } else {
371                 struct aac_get_config_status_resp *reply
372                   = (struct aac_get_config_status_resp *) fib_data(fibptr);
373                 dprintk((KERN_WARNING
374                   "aac_get_config_status: response=%d status=%d action=%d\n",
375                   le32_to_cpu(reply->response),
376                   le32_to_cpu(reply->status),
377                   le32_to_cpu(reply->data.action)));
378                 if ((le32_to_cpu(reply->response) != ST_OK) ||
379                      (le32_to_cpu(reply->status) != CT_OK) ||
380                      (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
381                         printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
382                         status = -EINVAL;
383                 }
384         }
385         /* Do not set XferState to zero unless receives a response from F/W */
386         if (status >= 0)
387                 aac_fib_complete(fibptr);
388
389         /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
390         if (status >= 0) {
391                 if ((aac_commit == 1) || commit_flag) {
392                         struct aac_commit_config * dinfo;
393                         aac_fib_init(fibptr);
394                         dinfo = (struct aac_commit_config *) fib_data(fibptr);
395
396                         dinfo->command = cpu_to_le32(VM_ContainerConfig);
397                         dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
398
399                         status = aac_fib_send(ContainerCommand,
400                                     fibptr,
401                                     sizeof (struct aac_commit_config),
402                                     FsaNormal,
403                                     1, 1,
404                                     NULL, NULL);
405                         /* Do not set XferState to zero unless
406                          * receives a response from F/W */
407                         if (status >= 0)
408                                 aac_fib_complete(fibptr);
409                 } else if (aac_commit == 0) {
410                         printk(KERN_WARNING
411                           "aac_get_config_status: Foreign device configurations are being ignored\n");
412                 }
413         }
414         /* FIB should be freed only after getting the response from the F/W */
415         if (status != -ERESTARTSYS)
416                 aac_fib_free(fibptr);
417         return status;
418 }
419
420 static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
421 {
422         char inq_data;
423         scsi_sg_copy_to_buffer(scsicmd,  &inq_data, sizeof(inq_data));
424         if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
425                 inq_data &= 0xdf;
426                 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
427         }
428 }
429
430 /**
431  *      aac_get_containers      -       list containers
432  *      @common: adapter to probe
433  *
434  *      Make a list of all containers on this controller
435  */
436 int aac_get_containers(struct aac_dev *dev)
437 {
438         struct fsa_dev_info *fsa_dev_ptr;
439         u32 index;
440         int status = 0;
441         struct fib * fibptr;
442         struct aac_get_container_count *dinfo;
443         struct aac_get_container_count_resp *dresp;
444         int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
445
446         if (!(fibptr = aac_fib_alloc(dev)))
447                 return -ENOMEM;
448
449         aac_fib_init(fibptr);
450         dinfo = (struct aac_get_container_count *) fib_data(fibptr);
451         dinfo->command = cpu_to_le32(VM_ContainerConfig);
452         dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
453
454         status = aac_fib_send(ContainerCommand,
455                     fibptr,
456                     sizeof (struct aac_get_container_count),
457                     FsaNormal,
458                     1, 1,
459                     NULL, NULL);
460         if (status >= 0) {
461                 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
462                 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
463                 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
464                     AAC_OPTION_SUPPORTED_240_VOLUMES) {
465                         maximum_num_containers =
466                                 le32_to_cpu(dresp->MaxSimpleVolumes);
467                 }
468                 aac_fib_complete(fibptr);
469         }
470         /* FIB should be freed only after getting the response from the F/W */
471         if (status != -ERESTARTSYS)
472                 aac_fib_free(fibptr);
473
474         if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
475                 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
476         fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
477                         GFP_KERNEL);
478         if (!fsa_dev_ptr)
479                 return -ENOMEM;
480
481         dev->fsa_dev = fsa_dev_ptr;
482         dev->maximum_num_containers = maximum_num_containers;
483
484         for (index = 0; index < dev->maximum_num_containers; ) {
485                 fsa_dev_ptr[index].devname[0] = '\0';
486
487                 status = aac_probe_container(dev, index);
488
489                 if (status < 0) {
490                         printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
491                         break;
492                 }
493
494                 /*
495                  *      If there are no more containers, then stop asking.
496                  */
497                 if (++index >= status)
498                         break;
499         }
500         return status;
501 }
502
503 static void get_container_name_callback(void *context, struct fib * fibptr)
504 {
505         struct aac_get_name_resp * get_name_reply;
506         struct scsi_cmnd * scsicmd;
507
508         scsicmd = (struct scsi_cmnd *) context;
509
510         if (!aac_valid_context(scsicmd, fibptr))
511                 return;
512
513         dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
514         BUG_ON(fibptr == NULL);
515
516         get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
517         /* Failure is irrelevant, using default value instead */
518         if ((le32_to_cpu(get_name_reply->status) == CT_OK)
519          && (get_name_reply->data[0] != '\0')) {
520                 char *sp = get_name_reply->data;
521                 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)] = '\0';
522                 while (*sp == ' ')
523                         ++sp;
524                 if (*sp) {
525                         struct inquiry_data inq;
526                         char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
527                         int count = sizeof(d);
528                         char *dp = d;
529                         do {
530                                 *dp++ = (*sp) ? *sp++ : ' ';
531                         } while (--count > 0);
532
533                         scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
534                         memcpy(inq.inqd_pid, d, sizeof(d));
535                         scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
536                 }
537         }
538
539         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
540
541         aac_fib_complete(fibptr);
542         scsicmd->scsi_done(scsicmd);
543 }
544
545 /**
546  *      aac_get_container_name  -       get container name, none blocking.
547  */
548 static int aac_get_container_name(struct scsi_cmnd * scsicmd)
549 {
550         int status;
551         struct aac_get_name *dinfo;
552         struct fib * cmd_fibcontext;
553         struct aac_dev * dev;
554
555         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
556
557         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
558
559         aac_fib_init(cmd_fibcontext);
560         dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
561
562         dinfo->command = cpu_to_le32(VM_ContainerConfig);
563         dinfo->type = cpu_to_le32(CT_READ_NAME);
564         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
565         dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
566
567         status = aac_fib_send(ContainerCommand,
568                   cmd_fibcontext,
569                   sizeof(struct aac_get_name_resp),
570                   FsaNormal,
571                   0, 1,
572                   (fib_callback)get_container_name_callback,
573                   (void *) scsicmd);
574
575         /*
576          *      Check that the command queued to the controller
577          */
578         if (status == -EINPROGRESS) {
579                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
580                 return 0;
581         }
582
583         printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
584         aac_fib_complete(cmd_fibcontext);
585         return -1;
586 }
587
588 static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
589 {
590         struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
591
592         if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
593                 return aac_scsi_cmd(scsicmd);
594
595         scsicmd->result = DID_NO_CONNECT << 16;
596         scsicmd->scsi_done(scsicmd);
597         return 0;
598 }
599
600 static void _aac_probe_container2(void * context, struct fib * fibptr)
601 {
602         struct fsa_dev_info *fsa_dev_ptr;
603         int (*callback)(struct scsi_cmnd *);
604         struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
605
606
607         if (!aac_valid_context(scsicmd, fibptr))
608                 return;
609
610         scsicmd->SCp.Status = 0;
611         fsa_dev_ptr = fibptr->dev->fsa_dev;
612         if (fsa_dev_ptr) {
613                 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
614                 fsa_dev_ptr += scmd_id(scsicmd);
615
616                 if ((le32_to_cpu(dresp->status) == ST_OK) &&
617                     (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
618                     (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
619                         if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
620                             AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
621                                 dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
622                                 fsa_dev_ptr->block_size = 0x200;
623                         } else {
624                                 fsa_dev_ptr->block_size =
625                                         le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
626                         }
627                         fsa_dev_ptr->valid = 1;
628                         /* sense_key holds the current state of the spin-up */
629                         if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
630                                 fsa_dev_ptr->sense_data.sense_key = NOT_READY;
631                         else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
632                                 fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
633                         fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
634                         fsa_dev_ptr->size
635                           = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
636                             (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
637                         fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
638                 }
639                 if ((fsa_dev_ptr->valid & 1) == 0)
640                         fsa_dev_ptr->valid = 0;
641                 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
642         }
643         aac_fib_complete(fibptr);
644         aac_fib_free(fibptr);
645         callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
646         scsicmd->SCp.ptr = NULL;
647         (*callback)(scsicmd);
648         return;
649 }
650
651 static void _aac_probe_container1(void * context, struct fib * fibptr)
652 {
653         struct scsi_cmnd * scsicmd;
654         struct aac_mount * dresp;
655         struct aac_query_mount *dinfo;
656         int status;
657
658         dresp = (struct aac_mount *) fib_data(fibptr);
659         if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
660             AAC_OPTION_VARIABLE_BLOCK_SIZE))
661                 dresp->mnt[0].capacityhigh = 0;
662         if ((le32_to_cpu(dresp->status) != ST_OK) ||
663             (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
664                 _aac_probe_container2(context, fibptr);
665                 return;
666         }
667         scsicmd = (struct scsi_cmnd *) context;
668
669         if (!aac_valid_context(scsicmd, fibptr))
670                 return;
671
672         aac_fib_init(fibptr);
673
674         dinfo = (struct aac_query_mount *)fib_data(fibptr);
675
676         if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
677             AAC_OPTION_VARIABLE_BLOCK_SIZE)
678                 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
679         else
680                 dinfo->command = cpu_to_le32(VM_NameServe64);
681
682         dinfo->count = cpu_to_le32(scmd_id(scsicmd));
683         dinfo->type = cpu_to_le32(FT_FILESYS);
684
685         status = aac_fib_send(ContainerCommand,
686                           fibptr,
687                           sizeof(struct aac_query_mount),
688                           FsaNormal,
689                           0, 1,
690                           _aac_probe_container2,
691                           (void *) scsicmd);
692         /*
693          *      Check that the command queued to the controller
694          */
695         if (status == -EINPROGRESS)
696                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
697         else if (status < 0) {
698                 /* Inherit results from VM_NameServe, if any */
699                 dresp->status = cpu_to_le32(ST_OK);
700                 _aac_probe_container2(context, fibptr);
701         }
702 }
703
704 static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
705 {
706         struct fib * fibptr;
707         int status = -ENOMEM;
708
709         if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
710                 struct aac_query_mount *dinfo;
711
712                 aac_fib_init(fibptr);
713
714                 dinfo = (struct aac_query_mount *)fib_data(fibptr);
715
716                 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
717                     AAC_OPTION_VARIABLE_BLOCK_SIZE)
718                         dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
719                 else
720                         dinfo->command = cpu_to_le32(VM_NameServe);
721
722                 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
723                 dinfo->type = cpu_to_le32(FT_FILESYS);
724                 scsicmd->SCp.ptr = (char *)callback;
725
726                 status = aac_fib_send(ContainerCommand,
727                           fibptr,
728                           sizeof(struct aac_query_mount),
729                           FsaNormal,
730                           0, 1,
731                           _aac_probe_container1,
732                           (void *) scsicmd);
733                 /*
734                  *      Check that the command queued to the controller
735                  */
736                 if (status == -EINPROGRESS) {
737                         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
738                         return 0;
739                 }
740                 if (status < 0) {
741                         scsicmd->SCp.ptr = NULL;
742                         aac_fib_complete(fibptr);
743                         aac_fib_free(fibptr);
744                 }
745         }
746         if (status < 0) {
747                 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
748                 if (fsa_dev_ptr) {
749                         fsa_dev_ptr += scmd_id(scsicmd);
750                         if ((fsa_dev_ptr->valid & 1) == 0) {
751                                 fsa_dev_ptr->valid = 0;
752                                 return (*callback)(scsicmd);
753                         }
754                 }
755         }
756         return status;
757 }
758
759 /**
760  *      aac_probe_container             -       query a logical volume
761  *      @dev: device to query
762  *      @cid: container identifier
763  *
764  *      Queries the controller about the given volume. The volume information
765  *      is updated in the struct fsa_dev_info structure rather than returned.
766  */
767 static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
768 {
769         scsicmd->device = NULL;
770         return 0;
771 }
772
773 int aac_probe_container(struct aac_dev *dev, int cid)
774 {
775         struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
776         struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
777         int status;
778
779         if (!scsicmd || !scsidev) {
780                 kfree(scsicmd);
781                 kfree(scsidev);
782                 return -ENOMEM;
783         }
784         scsicmd->list.next = NULL;
785         scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
786
787         scsicmd->device = scsidev;
788         scsidev->sdev_state = 0;
789         scsidev->id = cid;
790         scsidev->host = dev->scsi_host_ptr;
791
792         if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
793                 while (scsicmd->device == scsidev)
794                         schedule();
795         kfree(scsidev);
796         status = scsicmd->SCp.Status;
797         kfree(scsicmd);
798         return status;
799 }
800
801 /* Local Structure to set SCSI inquiry data strings */
802 struct scsi_inq {
803         char vid[8];         /* Vendor ID */
804         char pid[16];        /* Product ID */
805         char prl[4];         /* Product Revision Level */
806 };
807
808 /**
809  *      InqStrCopy      -       string merge
810  *      @a:     string to copy from
811  *      @b:     string to copy to
812  *
813  *      Copy a String from one location to another
814  *      without copying \0
815  */
816
817 static void inqstrcpy(char *a, char *b)
818 {
819
820         while (*a != (char)0)
821                 *b++ = *a++;
822 }
823
824 static char *container_types[] = {
825         "None",
826         "Volume",
827         "Mirror",
828         "Stripe",
829         "RAID5",
830         "SSRW",
831         "SSRO",
832         "Morph",
833         "Legacy",
834         "RAID4",
835         "RAID10",
836         "RAID00",
837         "V-MIRRORS",
838         "PSEUDO R4",
839         "RAID50",
840         "RAID5D",
841         "RAID5D0",
842         "RAID1E",
843         "RAID6",
844         "RAID60",
845         "Unknown"
846 };
847
848 char * get_container_type(unsigned tindex)
849 {
850         if (tindex >= ARRAY_SIZE(container_types))
851                 tindex = ARRAY_SIZE(container_types) - 1;
852         return container_types[tindex];
853 }
854
855 /* Function: setinqstr
856  *
857  * Arguments: [1] pointer to void [1] int
858  *
859  * Purpose: Sets SCSI inquiry data strings for vendor, product
860  * and revision level. Allows strings to be set in platform dependent
861  * files instead of in OS dependent driver source.
862  */
863
864 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
865 {
866         struct scsi_inq *str;
867
868         str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
869         memset(str, ' ', sizeof(*str));
870
871         if (dev->supplement_adapter_info.AdapterTypeText[0]) {
872                 char * cp = dev->supplement_adapter_info.AdapterTypeText;
873                 int c;
874                 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
875                         inqstrcpy("SMC", str->vid);
876                 else {
877                         c = sizeof(str->vid);
878                         while (*cp && *cp != ' ' && --c)
879                                 ++cp;
880                         c = *cp;
881                         *cp = '\0';
882                         inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
883                                    str->vid);
884                         *cp = c;
885                         while (*cp && *cp != ' ')
886                                 ++cp;
887                 }
888                 while (*cp == ' ')
889                         ++cp;
890                 /* last six chars reserved for vol type */
891                 c = 0;
892                 if (strlen(cp) > sizeof(str->pid)) {
893                         c = cp[sizeof(str->pid)];
894                         cp[sizeof(str->pid)] = '\0';
895                 }
896                 inqstrcpy (cp, str->pid);
897                 if (c)
898                         cp[sizeof(str->pid)] = c;
899         } else {
900                 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
901
902                 inqstrcpy (mp->vname, str->vid);
903                 /* last six chars reserved for vol type */
904                 inqstrcpy (mp->model, str->pid);
905         }
906
907         if (tindex < ARRAY_SIZE(container_types)){
908                 char *findit = str->pid;
909
910                 for ( ; *findit != ' '; findit++); /* walk till we find a space */
911                 /* RAID is superfluous in the context of a RAID device */
912                 if (memcmp(findit-4, "RAID", 4) == 0)
913                         *(findit -= 4) = ' ';
914                 if (((findit - str->pid) + strlen(container_types[tindex]))
915                  < (sizeof(str->pid) + sizeof(str->prl)))
916                         inqstrcpy (container_types[tindex], findit + 1);
917         }
918         inqstrcpy ("V1.0", str->prl);
919 }
920
921 static void get_container_serial_callback(void *context, struct fib * fibptr)
922 {
923         struct aac_get_serial_resp * get_serial_reply;
924         struct scsi_cmnd * scsicmd;
925
926         BUG_ON(fibptr == NULL);
927
928         scsicmd = (struct scsi_cmnd *) context;
929         if (!aac_valid_context(scsicmd, fibptr))
930                 return;
931
932         get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
933         /* Failure is irrelevant, using default value instead */
934         if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
935                 /*Check to see if it's for VPD 0x83 or 0x80 */
936                 if (scsicmd->cmnd[2] == 0x83) {
937                         /* vpd page 0x83 - Device Identification Page */
938                         int i;
939                         TVPD_Page83 VPDPage83Data;
940
941                         memset(((u8 *)&VPDPage83Data), 0,
942                                sizeof(VPDPage83Data));
943
944                         /* DIRECT_ACCESS_DEVIC */
945                         VPDPage83Data.DeviceType = 0;
946                         /* DEVICE_CONNECTED */
947                         VPDPage83Data.DeviceTypeQualifier = 0;
948                         /* VPD_DEVICE_IDENTIFIERS */
949                         VPDPage83Data.PageCode = 0x83;
950                         VPDPage83Data.Reserved = 0;
951                         VPDPage83Data.PageLength =
952                                 sizeof(VPDPage83Data.IdDescriptorType1) +
953                                 sizeof(VPDPage83Data.IdDescriptorType2);
954
955                         /* T10 Vendor Identifier Field Format */
956                         /* VpdCodeSetAscii */
957                         VPDPage83Data.IdDescriptorType1.CodeSet = 2;
958                         /* VpdIdentifierTypeVendorId */
959                         VPDPage83Data.IdDescriptorType1.IdentifierType = 1;
960                         VPDPage83Data.IdDescriptorType1.IdentifierLength =
961                                 sizeof(VPDPage83Data.IdDescriptorType1) - 4;
962
963                         /* "ADAPTEC " for adaptec */
964                         memcpy(VPDPage83Data.IdDescriptorType1.VendId,
965                                 "ADAPTEC ",
966                                 sizeof(VPDPage83Data.IdDescriptorType1.VendId));
967                         memcpy(VPDPage83Data.IdDescriptorType1.ProductId,
968                                 "ARRAY           ",
969                                 sizeof(
970                                 VPDPage83Data.IdDescriptorType1.ProductId));
971
972                         /* Convert to ascii based serial number.
973                          * The LSB is the the end.
974                          */
975                         for (i = 0; i < 8; i++) {
976                                 u8 temp =
977                                         (u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
978                                 if (temp  > 0x9) {
979                                         VPDPage83Data.IdDescriptorType1.SerialNumber[i] =
980                                                         'A' + (temp - 0xA);
981                                 } else {
982                                         VPDPage83Data.IdDescriptorType1.SerialNumber[i] =
983                                                         '0' + temp;
984                                 }
985                         }
986
987                         /* VpdCodeSetBinary */
988                         VPDPage83Data.IdDescriptorType2.CodeSet = 1;
989                         /* VpdIdentifierTypeEUI64 */
990                         VPDPage83Data.IdDescriptorType2.IdentifierType = 2;
991                         VPDPage83Data.IdDescriptorType2.IdentifierLength =
992                                 sizeof(VPDPage83Data.IdDescriptorType2) - 4;
993
994                         VPDPage83Data.IdDescriptorType2.EU64Id.VendId[0] = 0xD0;
995                         VPDPage83Data.IdDescriptorType2.EU64Id.VendId[1] = 0;
996                         VPDPage83Data.IdDescriptorType2.EU64Id.VendId[2] = 0;
997
998                         VPDPage83Data.IdDescriptorType2.EU64Id.Serial =
999                                                         get_serial_reply->uid;
1000                         VPDPage83Data.IdDescriptorType2.EU64Id.Reserved = 0;
1001
1002                         /* Move the inquiry data to the response buffer. */
1003                         scsi_sg_copy_from_buffer(scsicmd, &VPDPage83Data,
1004                                                  sizeof(VPDPage83Data));
1005                 } else {
1006                         /* It must be for VPD 0x80 */
1007                         char sp[13];
1008                         /* EVPD bit set */
1009                         sp[0] = INQD_PDT_DA;
1010                         sp[1] = scsicmd->cmnd[2];
1011                         sp[2] = 0;
1012                         sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1013                                 le32_to_cpu(get_serial_reply->uid));
1014                         scsi_sg_copy_from_buffer(scsicmd, sp,
1015                                                  sizeof(sp));
1016                 }
1017         }
1018
1019         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1020
1021         aac_fib_complete(fibptr);
1022         scsicmd->scsi_done(scsicmd);
1023 }
1024
1025 /**
1026  *      aac_get_container_serial - get container serial, none blocking.
1027  */
1028 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1029 {
1030         int status;
1031         struct aac_get_serial *dinfo;
1032         struct fib * cmd_fibcontext;
1033         struct aac_dev * dev;
1034
1035         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1036
1037         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1038
1039         aac_fib_init(cmd_fibcontext);
1040         dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1041
1042         dinfo->command = cpu_to_le32(VM_ContainerConfig);
1043         dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1044         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1045
1046         status = aac_fib_send(ContainerCommand,
1047                   cmd_fibcontext,
1048                   sizeof(struct aac_get_serial_resp),
1049                   FsaNormal,
1050                   0, 1,
1051                   (fib_callback) get_container_serial_callback,
1052                   (void *) scsicmd);
1053
1054         /*
1055          *      Check that the command queued to the controller
1056          */
1057         if (status == -EINPROGRESS) {
1058                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1059                 return 0;
1060         }
1061
1062         printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1063         aac_fib_complete(cmd_fibcontext);
1064         return -1;
1065 }
1066
1067 /* Function: setinqserial
1068  *
1069  * Arguments: [1] pointer to void [1] int
1070  *
1071  * Purpose: Sets SCSI Unit Serial number.
1072  *          This is a fake. We should read a proper
1073  *          serial number from the container. <SuSE>But
1074  *          without docs it's quite hard to do it :-)
1075  *          So this will have to do in the meantime.</SuSE>
1076  */
1077
1078 static int setinqserial(struct aac_dev *dev, void *data, int cid)
1079 {
1080         /*
1081          *      This breaks array migration.
1082          */
1083         return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1084                         le32_to_cpu(dev->adapter_info.serial[0]), cid);
1085 }
1086
1087 static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1088         u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
1089 {
1090         u8 *sense_buf = (u8 *)sense_data;
1091         /* Sense data valid, err code 70h */
1092         sense_buf[0] = 0x70; /* No info field */
1093         sense_buf[1] = 0;       /* Segment number, always zero */
1094
1095         sense_buf[2] = sense_key;       /* Sense key */
1096
1097         sense_buf[12] = sense_code;     /* Additional sense code */
1098         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
1099
1100         if (sense_key == ILLEGAL_REQUEST) {
1101                 sense_buf[7] = 10;      /* Additional sense length */
1102
1103                 sense_buf[15] = bit_pointer;
1104                 /* Illegal parameter is in the parameter block */
1105                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
1106                         sense_buf[15] |= 0xc0;/* Std sense key specific field */
1107                 /* Illegal parameter is in the CDB block */
1108                 sense_buf[16] = field_pointer >> 8;     /* MSB */
1109                 sense_buf[17] = field_pointer;          /* LSB */
1110         } else
1111                 sense_buf[7] = 6;       /* Additional sense length */
1112 }
1113
1114 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1115 {
1116         if (lba & 0xffffffff00000000LL) {
1117                 int cid = scmd_id(cmd);
1118                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1119                 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1120                         SAM_STAT_CHECK_CONDITION;
1121                 set_sense(&dev->fsa_dev[cid].sense_data,
1122                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1123                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1124                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1125                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1126                              SCSI_SENSE_BUFFERSIZE));
1127                 cmd->scsi_done(cmd);
1128                 return 1;
1129         }
1130         return 0;
1131 }
1132
1133 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1134 {
1135         return 0;
1136 }
1137
1138 static void io_callback(void *context, struct fib * fibptr);
1139
1140 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1141 {
1142         struct aac_dev *dev = fib->dev;
1143         u16 fibsize, command;
1144         long ret;
1145
1146         aac_fib_init(fib);
1147         if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
1148                 struct aac_raw_io2 *readcmd2;
1149                 readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1150                 memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1151                 readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1152                 readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1153                 readcmd2->byteCount = cpu_to_le32(count *
1154                         dev->fsa_dev[scmd_id(cmd)].block_size);
1155                 readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1156                 readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
1157                 ret = aac_build_sgraw2(cmd, readcmd2,
1158                                 dev->scsi_host_ptr->sg_tablesize);
1159                 if (ret < 0)
1160                         return ret;
1161                 command = ContainerRawIo2;
1162                 fibsize = sizeof(struct aac_raw_io2) +
1163                         ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1164         } else {
1165                 struct aac_raw_io *readcmd;
1166                 readcmd = (struct aac_raw_io *) fib_data(fib);
1167                 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1168                 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1169                 readcmd->count = cpu_to_le32(count *
1170                         dev->fsa_dev[scmd_id(cmd)].block_size);
1171                 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1172                 readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1173                 readcmd->bpTotal = 0;
1174                 readcmd->bpComplete = 0;
1175                 ret = aac_build_sgraw(cmd, &readcmd->sg);
1176                 if (ret < 0)
1177                         return ret;
1178                 command = ContainerRawIo;
1179                 fibsize = sizeof(struct aac_raw_io) +
1180                         ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1181         }
1182
1183         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1184         /*
1185          *      Now send the Fib to the adapter
1186          */
1187         return aac_fib_send(command,
1188                           fib,
1189                           fibsize,
1190                           FsaNormal,
1191                           0, 1,
1192                           (fib_callback) io_callback,
1193                           (void *) cmd);
1194 }
1195
1196 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1197 {
1198         u16 fibsize;
1199         struct aac_read64 *readcmd;
1200         long ret;
1201
1202         aac_fib_init(fib);
1203         readcmd = (struct aac_read64 *) fib_data(fib);
1204         readcmd->command = cpu_to_le32(VM_CtHostRead64);
1205         readcmd->cid = cpu_to_le16(scmd_id(cmd));
1206         readcmd->sector_count = cpu_to_le16(count);
1207         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1208         readcmd->pad   = 0;
1209         readcmd->flags = 0;
1210
1211         ret = aac_build_sg64(cmd, &readcmd->sg);
1212         if (ret < 0)
1213                 return ret;
1214         fibsize = sizeof(struct aac_read64) +
1215                 ((le32_to_cpu(readcmd->sg.count) - 1) *
1216                  sizeof (struct sgentry64));
1217         BUG_ON (fibsize > (fib->dev->max_fib_size -
1218                                 sizeof(struct aac_fibhdr)));
1219         /*
1220          *      Now send the Fib to the adapter
1221          */
1222         return aac_fib_send(ContainerCommand64,
1223                           fib,
1224                           fibsize,
1225                           FsaNormal,
1226                           0, 1,
1227                           (fib_callback) io_callback,
1228                           (void *) cmd);
1229 }
1230
1231 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1232 {
1233         u16 fibsize;
1234         struct aac_read *readcmd;
1235         struct aac_dev *dev = fib->dev;
1236         long ret;
1237
1238         aac_fib_init(fib);
1239         readcmd = (struct aac_read *) fib_data(fib);
1240         readcmd->command = cpu_to_le32(VM_CtBlockRead);
1241         readcmd->cid = cpu_to_le32(scmd_id(cmd));
1242         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1243         readcmd->count = cpu_to_le32(count *
1244                 dev->fsa_dev[scmd_id(cmd)].block_size);
1245
1246         ret = aac_build_sg(cmd, &readcmd->sg);
1247         if (ret < 0)
1248                 return ret;
1249         fibsize = sizeof(struct aac_read) +
1250                         ((le32_to_cpu(readcmd->sg.count) - 1) *
1251                          sizeof (struct sgentry));
1252         BUG_ON (fibsize > (fib->dev->max_fib_size -
1253                                 sizeof(struct aac_fibhdr)));
1254         /*
1255          *      Now send the Fib to the adapter
1256          */
1257         return aac_fib_send(ContainerCommand,
1258                           fib,
1259                           fibsize,
1260                           FsaNormal,
1261                           0, 1,
1262                           (fib_callback) io_callback,
1263                           (void *) cmd);
1264 }
1265
1266 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1267 {
1268         struct aac_dev *dev = fib->dev;
1269         u16 fibsize, command;
1270         long ret;
1271
1272         aac_fib_init(fib);
1273         if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
1274                 struct aac_raw_io2 *writecmd2;
1275                 writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1276                 memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1277                 writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1278                 writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1279                 writecmd2->byteCount = cpu_to_le32(count *
1280                         dev->fsa_dev[scmd_id(cmd)].block_size);
1281                 writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1282                 writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1283                                                    (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1284                         cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1285                         cpu_to_le16(RIO2_IO_TYPE_WRITE);
1286                 ret = aac_build_sgraw2(cmd, writecmd2,
1287                                 dev->scsi_host_ptr->sg_tablesize);
1288                 if (ret < 0)
1289                         return ret;
1290                 command = ContainerRawIo2;
1291                 fibsize = sizeof(struct aac_raw_io2) +
1292                         ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1293         } else {
1294                 struct aac_raw_io *writecmd;
1295                 writecmd = (struct aac_raw_io *) fib_data(fib);
1296                 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1297                 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1298                 writecmd->count = cpu_to_le32(count *
1299                         dev->fsa_dev[scmd_id(cmd)].block_size);
1300                 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1301                 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1302                                                    (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1303                         cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1304                         cpu_to_le16(RIO_TYPE_WRITE);
1305                 writecmd->bpTotal = 0;
1306                 writecmd->bpComplete = 0;
1307                 ret = aac_build_sgraw(cmd, &writecmd->sg);
1308                 if (ret < 0)
1309                         return ret;
1310                 command = ContainerRawIo;
1311                 fibsize = sizeof(struct aac_raw_io) +
1312                         ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1313         }
1314
1315         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1316         /*
1317          *      Now send the Fib to the adapter
1318          */
1319         return aac_fib_send(command,
1320                           fib,
1321                           fibsize,
1322                           FsaNormal,
1323                           0, 1,
1324                           (fib_callback) io_callback,
1325                           (void *) cmd);
1326 }
1327
1328 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1329 {
1330         u16 fibsize;
1331         struct aac_write64 *writecmd;
1332         long ret;
1333
1334         aac_fib_init(fib);
1335         writecmd = (struct aac_write64 *) fib_data(fib);
1336         writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1337         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1338         writecmd->sector_count = cpu_to_le16(count);
1339         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1340         writecmd->pad   = 0;
1341         writecmd->flags = 0;
1342
1343         ret = aac_build_sg64(cmd, &writecmd->sg);
1344         if (ret < 0)
1345                 return ret;
1346         fibsize = sizeof(struct aac_write64) +
1347                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1348                  sizeof (struct sgentry64));
1349         BUG_ON (fibsize > (fib->dev->max_fib_size -
1350                                 sizeof(struct aac_fibhdr)));
1351         /*
1352          *      Now send the Fib to the adapter
1353          */
1354         return aac_fib_send(ContainerCommand64,
1355                           fib,
1356                           fibsize,
1357                           FsaNormal,
1358                           0, 1,
1359                           (fib_callback) io_callback,
1360                           (void *) cmd);
1361 }
1362
1363 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1364 {
1365         u16 fibsize;
1366         struct aac_write *writecmd;
1367         struct aac_dev *dev = fib->dev;
1368         long ret;
1369
1370         aac_fib_init(fib);
1371         writecmd = (struct aac_write *) fib_data(fib);
1372         writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1373         writecmd->cid = cpu_to_le32(scmd_id(cmd));
1374         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1375         writecmd->count = cpu_to_le32(count *
1376                 dev->fsa_dev[scmd_id(cmd)].block_size);
1377         writecmd->sg.count = cpu_to_le32(1);
1378         /* ->stable is not used - it did mean which type of write */
1379
1380         ret = aac_build_sg(cmd, &writecmd->sg);
1381         if (ret < 0)
1382                 return ret;
1383         fibsize = sizeof(struct aac_write) +
1384                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1385                  sizeof (struct sgentry));
1386         BUG_ON (fibsize > (fib->dev->max_fib_size -
1387                                 sizeof(struct aac_fibhdr)));
1388         /*
1389          *      Now send the Fib to the adapter
1390          */
1391         return aac_fib_send(ContainerCommand,
1392                           fib,
1393                           fibsize,
1394                           FsaNormal,
1395                           0, 1,
1396                           (fib_callback) io_callback,
1397                           (void *) cmd);
1398 }
1399
1400 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1401 {
1402         struct aac_srb * srbcmd;
1403         u32 flag;
1404         u32 timeout;
1405
1406         aac_fib_init(fib);
1407         switch(cmd->sc_data_direction){
1408         case DMA_TO_DEVICE:
1409                 flag = SRB_DataOut;
1410                 break;
1411         case DMA_BIDIRECTIONAL:
1412                 flag = SRB_DataIn | SRB_DataOut;
1413                 break;
1414         case DMA_FROM_DEVICE:
1415                 flag = SRB_DataIn;
1416                 break;
1417         case DMA_NONE:
1418         default:        /* shuts up some versions of gcc */
1419                 flag = SRB_NoDataXfer;
1420                 break;
1421         }
1422
1423         srbcmd = (struct aac_srb*) fib_data(fib);
1424         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1425         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1426         srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1427         srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1428         srbcmd->flags    = cpu_to_le32(flag);
1429         timeout = cmd->request->timeout/HZ;
1430         if (timeout == 0)
1431                 timeout = 1;
1432         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1433         srbcmd->retry_limit = 0; /* Obsolete parameter */
1434         srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1435         return srbcmd;
1436 }
1437
1438 static void aac_srb_callback(void *context, struct fib * fibptr);
1439
1440 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1441 {
1442         u16 fibsize;
1443         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1444         long ret;
1445
1446         ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1447         if (ret < 0)
1448                 return ret;
1449         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1450
1451         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1452         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1453         /*
1454          *      Build Scatter/Gather list
1455          */
1456         fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1457                 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1458                  sizeof (struct sgentry64));
1459         BUG_ON (fibsize > (fib->dev->max_fib_size -
1460                                 sizeof(struct aac_fibhdr)));
1461
1462         /*
1463          *      Now send the Fib to the adapter
1464          */
1465         return aac_fib_send(ScsiPortCommand64, fib,
1466                                 fibsize, FsaNormal, 0, 1,
1467                                   (fib_callback) aac_srb_callback,
1468                                   (void *) cmd);
1469 }
1470
1471 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1472 {
1473         u16 fibsize;
1474         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1475         long ret;
1476
1477         ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1478         if (ret < 0)
1479                 return ret;
1480         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1481
1482         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1483         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1484         /*
1485          *      Build Scatter/Gather list
1486          */
1487         fibsize = sizeof (struct aac_srb) +
1488                 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1489                  sizeof (struct sgentry));
1490         BUG_ON (fibsize > (fib->dev->max_fib_size -
1491                                 sizeof(struct aac_fibhdr)));
1492
1493         /*
1494          *      Now send the Fib to the adapter
1495          */
1496         return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1497                                   (fib_callback) aac_srb_callback, (void *) cmd);
1498 }
1499
1500 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1501 {
1502         if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1503             (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1504                 return FAILED;
1505         return aac_scsi_32(fib, cmd);
1506 }
1507
1508 int aac_get_adapter_info(struct aac_dev* dev)
1509 {
1510         struct fib* fibptr;
1511         int rcode;
1512         u32 tmp;
1513         struct aac_adapter_info *info;
1514         struct aac_bus_info *command;
1515         struct aac_bus_info_response *bus_info;
1516
1517         if (!(fibptr = aac_fib_alloc(dev)))
1518                 return -ENOMEM;
1519
1520         aac_fib_init(fibptr);
1521         info = (struct aac_adapter_info *) fib_data(fibptr);
1522         memset(info,0,sizeof(*info));
1523
1524         rcode = aac_fib_send(RequestAdapterInfo,
1525                          fibptr,
1526                          sizeof(*info),
1527                          FsaNormal,
1528                          -1, 1, /* First `interrupt' command uses special wait */
1529                          NULL,
1530                          NULL);
1531
1532         if (rcode < 0) {
1533                 /* FIB should be freed only after
1534                  * getting the response from the F/W */
1535                 if (rcode != -ERESTARTSYS) {
1536                         aac_fib_complete(fibptr);
1537                         aac_fib_free(fibptr);
1538                 }
1539                 return rcode;
1540         }
1541         memcpy(&dev->adapter_info, info, sizeof(*info));
1542
1543         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1544                 struct aac_supplement_adapter_info * sinfo;
1545
1546                 aac_fib_init(fibptr);
1547
1548                 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1549
1550                 memset(sinfo,0,sizeof(*sinfo));
1551
1552                 rcode = aac_fib_send(RequestSupplementAdapterInfo,
1553                                  fibptr,
1554                                  sizeof(*sinfo),
1555                                  FsaNormal,
1556                                  1, 1,
1557                                  NULL,
1558                                  NULL);
1559
1560                 if (rcode >= 0)
1561                         memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
1562                 if (rcode == -ERESTARTSYS) {
1563                         fibptr = aac_fib_alloc(dev);
1564                         if (!fibptr)
1565                                 return -ENOMEM;
1566                 }
1567
1568         }
1569
1570
1571         /*
1572          * GetBusInfo
1573          */
1574
1575         aac_fib_init(fibptr);
1576
1577         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1578
1579         memset(bus_info, 0, sizeof(*bus_info));
1580
1581         command = (struct aac_bus_info *)bus_info;
1582
1583         command->Command = cpu_to_le32(VM_Ioctl);
1584         command->ObjType = cpu_to_le32(FT_DRIVE);
1585         command->MethodId = cpu_to_le32(1);
1586         command->CtlCmd = cpu_to_le32(GetBusInfo);
1587
1588         rcode = aac_fib_send(ContainerCommand,
1589                          fibptr,
1590                          sizeof (*bus_info),
1591                          FsaNormal,
1592                          1, 1,
1593                          NULL, NULL);
1594
1595         /* reasoned default */
1596         dev->maximum_num_physicals = 16;
1597         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1598                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1599                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1600         }
1601
1602         if (!dev->in_reset) {
1603                 char buffer[16];
1604                 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1605                 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1606                         dev->name,
1607                         dev->id,
1608                         tmp>>24,
1609                         (tmp>>16)&0xff,
1610                         tmp&0xff,
1611                         le32_to_cpu(dev->adapter_info.kernelbuild),
1612                         (int)sizeof(dev->supplement_adapter_info.BuildDate),
1613                         dev->supplement_adapter_info.BuildDate);
1614                 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1615                 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1616                         dev->name, dev->id,
1617                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1618                         le32_to_cpu(dev->adapter_info.monitorbuild));
1619                 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1620                 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
1621                         dev->name, dev->id,
1622                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1623                         le32_to_cpu(dev->adapter_info.biosbuild));
1624                 buffer[0] = '\0';
1625                 if (aac_get_serial_number(
1626                   shost_to_class(dev->scsi_host_ptr), buffer))
1627                         printk(KERN_INFO "%s%d: serial %s",
1628                           dev->name, dev->id, buffer);
1629                 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1630                         printk(KERN_INFO "%s%d: TSID %.*s\n",
1631                           dev->name, dev->id,
1632                           (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1633                           dev->supplement_adapter_info.VpdInfo.Tsid);
1634                 }
1635                 if (!aac_check_reset || ((aac_check_reset == 1) &&
1636                   (dev->supplement_adapter_info.SupportedOptions2 &
1637                   AAC_OPTION_IGNORE_RESET))) {
1638                         printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1639                           dev->name, dev->id);
1640                 }
1641         }
1642
1643         dev->cache_protected = 0;
1644         dev->jbod = ((dev->supplement_adapter_info.FeatureBits &
1645                 AAC_FEATURE_JBOD) != 0);
1646         dev->nondasd_support = 0;
1647         dev->raid_scsi_mode = 0;
1648         if(dev->adapter_info.options & AAC_OPT_NONDASD)
1649                 dev->nondasd_support = 1;
1650
1651         /*
1652          * If the firmware supports ROMB RAID/SCSI mode and we are currently
1653          * in RAID/SCSI mode, set the flag. For now if in this mode we will
1654          * force nondasd support on. If we decide to allow the non-dasd flag
1655          * additional changes changes will have to be made to support
1656          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
1657          * changed to support the new dev->raid_scsi_mode flag instead of
1658          * leaching off of the dev->nondasd_support flag. Also in linit.c the
1659          * function aac_detect will have to be modified where it sets up the
1660          * max number of channels based on the aac->nondasd_support flag only.
1661          */
1662         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1663             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1664                 dev->nondasd_support = 1;
1665                 dev->raid_scsi_mode = 1;
1666         }
1667         if (dev->raid_scsi_mode != 0)
1668                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1669                                 dev->name, dev->id);
1670
1671         if (nondasd != -1)
1672                 dev->nondasd_support = (nondasd!=0);
1673         if (dev->nondasd_support && !dev->in_reset)
1674                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1675
1676         if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
1677                 dev->needs_dac = 1;
1678         dev->dac_support = 0;
1679         if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
1680             (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
1681                 if (!dev->in_reset)
1682                         printk(KERN_INFO "%s%d: 64bit support enabled.\n",
1683                                 dev->name, dev->id);
1684                 dev->dac_support = 1;
1685         }
1686
1687         if(dacmode != -1) {
1688                 dev->dac_support = (dacmode!=0);
1689         }
1690
1691         /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
1692         if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
1693                 & AAC_QUIRK_SCSI_32)) {
1694                 dev->nondasd_support = 0;
1695                 dev->jbod = 0;
1696                 expose_physicals = 0;
1697         }
1698
1699         if(dev->dac_support != 0) {
1700                 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64)) &&
1701                         !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
1702                         if (!dev->in_reset)
1703                                 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1704                                         dev->name, dev->id);
1705                 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32)) &&
1706                         !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
1707                         printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1708                                 dev->name, dev->id);
1709                         dev->dac_support = 0;
1710                 } else {
1711                         printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1712                                 dev->name, dev->id);
1713                         rcode = -ENOMEM;
1714                 }
1715         }
1716         /*
1717          * Deal with configuring for the individualized limits of each packet
1718          * interface.
1719          */
1720         dev->a_ops.adapter_scsi = (dev->dac_support)
1721           ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1722                                 ? aac_scsi_32_64
1723                                 : aac_scsi_64)
1724                                 : aac_scsi_32;
1725         if (dev->raw_io_interface) {
1726                 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1727                                         ? aac_bounds_64
1728                                         : aac_bounds_32;
1729                 dev->a_ops.adapter_read = aac_read_raw_io;
1730                 dev->a_ops.adapter_write = aac_write_raw_io;
1731         } else {
1732                 dev->a_ops.adapter_bounds = aac_bounds_32;
1733                 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
1734                         sizeof(struct aac_fibhdr) -
1735                         sizeof(struct aac_write) + sizeof(struct sgentry)) /
1736                                 sizeof(struct sgentry);
1737                 if (dev->dac_support) {
1738                         dev->a_ops.adapter_read = aac_read_block64;
1739                         dev->a_ops.adapter_write = aac_write_block64;
1740                         /*
1741                          * 38 scatter gather elements
1742                          */
1743                         dev->scsi_host_ptr->sg_tablesize =
1744                                 (dev->max_fib_size -
1745                                 sizeof(struct aac_fibhdr) -
1746                                 sizeof(struct aac_write64) +
1747                                 sizeof(struct sgentry64)) /
1748                                         sizeof(struct sgentry64);
1749                 } else {
1750                         dev->a_ops.adapter_read = aac_read_block;
1751                         dev->a_ops.adapter_write = aac_write_block;
1752                 }
1753                 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1754                 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1755                         /*
1756                          * Worst case size that could cause sg overflow when
1757                          * we break up SG elements that are larger than 64KB.
1758                          * Would be nice if we could tell the SCSI layer what
1759                          * the maximum SG element size can be. Worst case is
1760                          * (sg_tablesize-1) 4KB elements with one 64KB
1761                          * element.
1762                          *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
1763                          */
1764                         dev->scsi_host_ptr->max_sectors =
1765                           (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1766                 }
1767         }
1768         /* FIB should be freed only after getting the response from the F/W */
1769         if (rcode != -ERESTARTSYS) {
1770                 aac_fib_complete(fibptr);
1771                 aac_fib_free(fibptr);
1772         }
1773
1774         return rcode;
1775 }
1776
1777
1778 static void io_callback(void *context, struct fib * fibptr)
1779 {
1780         struct aac_dev *dev;
1781         struct aac_read_reply *readreply;
1782         struct scsi_cmnd *scsicmd;
1783         u32 cid;
1784
1785         scsicmd = (struct scsi_cmnd *) context;
1786
1787         if (!aac_valid_context(scsicmd, fibptr))
1788                 return;
1789
1790         dev = fibptr->dev;
1791         cid = scmd_id(scsicmd);
1792
1793         if (nblank(dprintk(x))) {
1794                 u64 lba;
1795                 switch (scsicmd->cmnd[0]) {
1796                 case WRITE_6:
1797                 case READ_6:
1798                         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1799                             (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1800                         break;
1801                 case WRITE_16:
1802                 case READ_16:
1803                         lba = ((u64)scsicmd->cmnd[2] << 56) |
1804                               ((u64)scsicmd->cmnd[3] << 48) |
1805                               ((u64)scsicmd->cmnd[4] << 40) |
1806                               ((u64)scsicmd->cmnd[5] << 32) |
1807                               ((u64)scsicmd->cmnd[6] << 24) |
1808                               (scsicmd->cmnd[7] << 16) |
1809                               (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1810                         break;
1811                 case WRITE_12:
1812                 case READ_12:
1813                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1814                               (scsicmd->cmnd[3] << 16) |
1815                               (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1816                         break;
1817                 default:
1818                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1819                                (scsicmd->cmnd[3] << 16) |
1820                                (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1821                         break;
1822                 }
1823                 printk(KERN_DEBUG
1824                   "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1825                   smp_processor_id(), (unsigned long long)lba, jiffies);
1826         }
1827
1828         BUG_ON(fibptr == NULL);
1829
1830         scsi_dma_unmap(scsicmd);
1831
1832         readreply = (struct aac_read_reply *)fib_data(fibptr);
1833         switch (le32_to_cpu(readreply->status)) {
1834         case ST_OK:
1835                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1836                         SAM_STAT_GOOD;
1837                 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
1838                 break;
1839         case ST_NOT_READY:
1840                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1841                         SAM_STAT_CHECK_CONDITION;
1842                 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
1843                   SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
1844                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1845                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1846                              SCSI_SENSE_BUFFERSIZE));
1847                 break;
1848         default:
1849 #ifdef AAC_DETAILED_STATUS_INFO
1850                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
1851                   le32_to_cpu(readreply->status));
1852 #endif
1853                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1854                         SAM_STAT_CHECK_CONDITION;
1855                 set_sense(&dev->fsa_dev[cid].sense_data,
1856                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1857                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1858                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1859                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1860                              SCSI_SENSE_BUFFERSIZE));
1861                 break;
1862         }
1863         aac_fib_complete(fibptr);
1864
1865         scsicmd->scsi_done(scsicmd);
1866 }
1867
1868 static int aac_read(struct scsi_cmnd * scsicmd)
1869 {
1870         u64 lba;
1871         u32 count;
1872         int status;
1873         struct aac_dev *dev;
1874         struct fib * cmd_fibcontext;
1875         int cid;
1876
1877         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1878         /*
1879          *      Get block address and transfer length
1880          */
1881         switch (scsicmd->cmnd[0]) {
1882         case READ_6:
1883                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
1884
1885                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1886                         (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1887                 count = scsicmd->cmnd[4];
1888
1889                 if (count == 0)
1890                         count = 256;
1891                 break;
1892         case READ_16:
1893                 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
1894
1895                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1896                         ((u64)scsicmd->cmnd[3] << 48) |
1897                         ((u64)scsicmd->cmnd[4] << 40) |
1898                         ((u64)scsicmd->cmnd[5] << 32) |
1899                         ((u64)scsicmd->cmnd[6] << 24) |
1900                         (scsicmd->cmnd[7] << 16) |
1901                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1902                 count = (scsicmd->cmnd[10] << 24) |
1903                         (scsicmd->cmnd[11] << 16) |
1904                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1905                 break;
1906         case READ_12:
1907                 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
1908
1909                 lba = ((u64)scsicmd->cmnd[2] << 24) |
1910                         (scsicmd->cmnd[3] << 16) |
1911                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1912                 count = (scsicmd->cmnd[6] << 24) |
1913                         (scsicmd->cmnd[7] << 16) |
1914                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1915                 break;
1916         default:
1917                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
1918
1919                 lba = ((u64)scsicmd->cmnd[2] << 24) |
1920                         (scsicmd->cmnd[3] << 16) |
1921                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1922                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1923                 break;
1924         }
1925
1926         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1927                 cid = scmd_id(scsicmd);
1928                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1929                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1930                         SAM_STAT_CHECK_CONDITION;
1931                 set_sense(&dev->fsa_dev[cid].sense_data,
1932                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1933                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1934                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1935                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1936                              SCSI_SENSE_BUFFERSIZE));
1937                 scsicmd->scsi_done(scsicmd);
1938                 return 1;
1939         }
1940
1941         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
1942           smp_processor_id(), (unsigned long long)lba, jiffies));
1943         if (aac_adapter_bounds(dev,scsicmd,lba))
1944                 return 0;
1945         /*
1946          *      Alocate and initialize a Fib
1947          */
1948         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1949
1950         status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
1951
1952         /*
1953          *      Check that the command queued to the controller
1954          */
1955         if (status == -EINPROGRESS) {
1956                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1957                 return 0;
1958         }
1959
1960         printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
1961         /*
1962          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1963          */
1964         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1965         scsicmd->scsi_done(scsicmd);
1966         aac_fib_complete(cmd_fibcontext);
1967         aac_fib_free(cmd_fibcontext);
1968         return 0;
1969 }
1970
1971 static int aac_write(struct scsi_cmnd * scsicmd)
1972 {
1973         u64 lba;
1974         u32 count;
1975         int fua;
1976         int status;
1977         struct aac_dev *dev;
1978         struct fib * cmd_fibcontext;
1979         int cid;
1980
1981         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1982         /*
1983          *      Get block address and transfer length
1984          */
1985         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
1986         {
1987                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1988                 count = scsicmd->cmnd[4];
1989                 if (count == 0)
1990                         count = 256;
1991                 fua = 0;
1992         } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1993                 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
1994
1995                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1996                         ((u64)scsicmd->cmnd[3] << 48) |
1997                         ((u64)scsicmd->cmnd[4] << 40) |
1998                         ((u64)scsicmd->cmnd[5] << 32) |
1999                         ((u64)scsicmd->cmnd[6] << 24) |
2000                         (scsicmd->cmnd[7] << 16) |
2001                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2002                 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2003                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2004                 fua = scsicmd->cmnd[1] & 0x8;
2005         } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
2006                 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
2007
2008                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2009                     | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2010                 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2011                       | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2012                 fua = scsicmd->cmnd[1] & 0x8;
2013         } else {
2014                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
2015                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2016                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2017                 fua = scsicmd->cmnd[1] & 0x8;
2018         }
2019
2020         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2021                 cid = scmd_id(scsicmd);
2022                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2023                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2024                         SAM_STAT_CHECK_CONDITION;
2025                 set_sense(&dev->fsa_dev[cid].sense_data,
2026                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2027                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2028                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2029                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2030                              SCSI_SENSE_BUFFERSIZE));
2031                 scsicmd->scsi_done(scsicmd);
2032                 return 1;
2033         }
2034
2035         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
2036           smp_processor_id(), (unsigned long long)lba, jiffies));
2037         if (aac_adapter_bounds(dev,scsicmd,lba))
2038                 return 0;
2039         /*
2040          *      Allocate and initialize a Fib then setup a BlockWrite command
2041          */
2042         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2043
2044         status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
2045
2046         /*
2047          *      Check that the command queued to the controller
2048          */
2049         if (status == -EINPROGRESS) {
2050                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2051                 return 0;
2052         }
2053
2054         printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
2055         /*
2056          *      For some reason, the Fib didn't queue, return QUEUE_FULL
2057          */
2058         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2059         scsicmd->scsi_done(scsicmd);
2060
2061         aac_fib_complete(cmd_fibcontext);
2062         aac_fib_free(cmd_fibcontext);
2063         return 0;
2064 }
2065
2066 static void synchronize_callback(void *context, struct fib *fibptr)
2067 {
2068         struct aac_synchronize_reply *synchronizereply;
2069         struct scsi_cmnd *cmd;
2070
2071         cmd = context;
2072
2073         if (!aac_valid_context(cmd, fibptr))
2074                 return;
2075
2076         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
2077                                 smp_processor_id(), jiffies));
2078         BUG_ON(fibptr == NULL);
2079
2080
2081         synchronizereply = fib_data(fibptr);
2082         if (le32_to_cpu(synchronizereply->status) == CT_OK)
2083                 cmd->result = DID_OK << 16 |
2084                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2085         else {
2086                 struct scsi_device *sdev = cmd->device;
2087                 struct aac_dev *dev = fibptr->dev;
2088                 u32 cid = sdev_id(sdev);
2089                 printk(KERN_WARNING
2090                      "synchronize_callback: synchronize failed, status = %d\n",
2091                      le32_to_cpu(synchronizereply->status));
2092                 cmd->result = DID_OK << 16 |
2093                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2094                 set_sense(&dev->fsa_dev[cid].sense_data,
2095                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2096                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2097                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2098                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2099                              SCSI_SENSE_BUFFERSIZE));
2100         }
2101
2102         aac_fib_complete(fibptr);
2103         aac_fib_free(fibptr);
2104         cmd->scsi_done(cmd);
2105 }
2106
2107 static int aac_synchronize(struct scsi_cmnd *scsicmd)
2108 {
2109         int status;
2110         struct fib *cmd_fibcontext;
2111         struct aac_synchronize *synchronizecmd;
2112         struct scsi_cmnd *cmd;
2113         struct scsi_device *sdev = scsicmd->device;
2114         int active = 0;
2115         struct aac_dev *aac;
2116         u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2117                 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2118         u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2119         unsigned long flags;
2120
2121         /*
2122          * Wait for all outstanding queued commands to complete to this
2123          * specific target (block).
2124          */
2125         spin_lock_irqsave(&sdev->list_lock, flags);
2126         list_for_each_entry(cmd, &sdev->cmd_list, list)
2127                 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2128                         u64 cmnd_lba;
2129                         u32 cmnd_count;
2130
2131                         if (cmd->cmnd[0] == WRITE_6) {
2132                                 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2133                                         (cmd->cmnd[2] << 8) |
2134                                         cmd->cmnd[3];
2135                                 cmnd_count = cmd->cmnd[4];
2136                                 if (cmnd_count == 0)
2137                                         cmnd_count = 256;
2138                         } else if (cmd->cmnd[0] == WRITE_16) {
2139                                 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2140                                         ((u64)cmd->cmnd[3] << 48) |
2141                                         ((u64)cmd->cmnd[4] << 40) |
2142                                         ((u64)cmd->cmnd[5] << 32) |
2143                                         ((u64)cmd->cmnd[6] << 24) |
2144                                         (cmd->cmnd[7] << 16) |
2145                                         (cmd->cmnd[8] << 8) |
2146                                         cmd->cmnd[9];
2147                                 cmnd_count = (cmd->cmnd[10] << 24) |
2148                                         (cmd->cmnd[11] << 16) |
2149                                         (cmd->cmnd[12] << 8) |
2150                                         cmd->cmnd[13];
2151                         } else if (cmd->cmnd[0] == WRITE_12) {
2152                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2153                                         (cmd->cmnd[3] << 16) |
2154                                         (cmd->cmnd[4] << 8) |
2155                                         cmd->cmnd[5];
2156                                 cmnd_count = (cmd->cmnd[6] << 24) |
2157                                         (cmd->cmnd[7] << 16) |
2158                                         (cmd->cmnd[8] << 8) |
2159                                         cmd->cmnd[9];
2160                         } else if (cmd->cmnd[0] == WRITE_10) {
2161                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2162                                         (cmd->cmnd[3] << 16) |
2163                                         (cmd->cmnd[4] << 8) |
2164                                         cmd->cmnd[5];
2165                                 cmnd_count = (cmd->cmnd[7] << 8) |
2166                                         cmd->cmnd[8];
2167                         } else
2168                                 continue;
2169                         if (((cmnd_lba + cmnd_count) < lba) ||
2170                           (count && ((lba + count) < cmnd_lba)))
2171                                 continue;
2172                         ++active;
2173                         break;
2174                 }
2175
2176         spin_unlock_irqrestore(&sdev->list_lock, flags);
2177
2178         /*
2179          *      Yield the processor (requeue for later)
2180          */
2181         if (active)
2182                 return SCSI_MLQUEUE_DEVICE_BUSY;
2183
2184         aac = (struct aac_dev *)sdev->host->hostdata;
2185         if (aac->in_reset)
2186                 return SCSI_MLQUEUE_HOST_BUSY;
2187
2188         /*
2189          *      Allocate and initialize a Fib
2190          */
2191         if (!(cmd_fibcontext = aac_fib_alloc(aac)))
2192                 return SCSI_MLQUEUE_HOST_BUSY;
2193
2194         aac_fib_init(cmd_fibcontext);
2195
2196         synchronizecmd = fib_data(cmd_fibcontext);
2197         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2198         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
2199         synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
2200         synchronizecmd->count =
2201              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2202
2203         /*
2204          *      Now send the Fib to the adapter
2205          */
2206         status = aac_fib_send(ContainerCommand,
2207                   cmd_fibcontext,
2208                   sizeof(struct aac_synchronize),
2209                   FsaNormal,
2210                   0, 1,
2211                   (fib_callback)synchronize_callback,
2212                   (void *)scsicmd);
2213
2214         /*
2215          *      Check that the command queued to the controller
2216          */
2217         if (status == -EINPROGRESS) {
2218                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2219                 return 0;
2220         }
2221
2222         printk(KERN_WARNING
2223                 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2224         aac_fib_complete(cmd_fibcontext);
2225         aac_fib_free(cmd_fibcontext);
2226         return SCSI_MLQUEUE_HOST_BUSY;
2227 }
2228
2229 static void aac_start_stop_callback(void *context, struct fib *fibptr)
2230 {
2231         struct scsi_cmnd *scsicmd = context;
2232
2233         if (!aac_valid_context(scsicmd, fibptr))
2234                 return;
2235
2236         BUG_ON(fibptr == NULL);
2237
2238         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2239
2240         aac_fib_complete(fibptr);
2241         aac_fib_free(fibptr);
2242         scsicmd->scsi_done(scsicmd);
2243 }
2244
2245 static int aac_start_stop(struct scsi_cmnd *scsicmd)
2246 {
2247         int status;
2248         struct fib *cmd_fibcontext;
2249         struct aac_power_management *pmcmd;
2250         struct scsi_device *sdev = scsicmd->device;
2251         struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2252
2253         if (!(aac->supplement_adapter_info.SupportedOptions2 &
2254               AAC_OPTION_POWER_MANAGEMENT)) {
2255                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2256                                   SAM_STAT_GOOD;
2257                 scsicmd->scsi_done(scsicmd);
2258                 return 0;
2259         }
2260
2261         if (aac->in_reset)
2262                 return SCSI_MLQUEUE_HOST_BUSY;
2263
2264         /*
2265          *      Allocate and initialize a Fib
2266          */
2267         cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2268
2269         aac_fib_init(cmd_fibcontext);
2270
2271         pmcmd = fib_data(cmd_fibcontext);
2272         pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2273         pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2274         /* Eject bit ignored, not relevant */
2275         pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2276                 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2277         pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2278         pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2279                 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2280
2281         /*
2282          *      Now send the Fib to the adapter
2283          */
2284         status = aac_fib_send(ContainerCommand,
2285                   cmd_fibcontext,
2286                   sizeof(struct aac_power_management),
2287                   FsaNormal,
2288                   0, 1,
2289                   (fib_callback)aac_start_stop_callback,
2290                   (void *)scsicmd);
2291
2292         /*
2293          *      Check that the command queued to the controller
2294          */
2295         if (status == -EINPROGRESS) {
2296                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2297                 return 0;
2298         }
2299
2300         aac_fib_complete(cmd_fibcontext);
2301         aac_fib_free(cmd_fibcontext);
2302         return SCSI_MLQUEUE_HOST_BUSY;
2303 }
2304
2305 /**
2306  *      aac_scsi_cmd()          -       Process SCSI command
2307  *      @scsicmd:               SCSI command block
2308  *
2309  *      Emulate a SCSI command and queue the required request for the
2310  *      aacraid firmware.
2311  */
2312
2313 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2314 {
2315         u32 cid;
2316         struct Scsi_Host *host = scsicmd->device->host;
2317         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2318         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2319
2320         if (fsa_dev_ptr == NULL)
2321                 return -1;
2322         /*
2323          *      If the bus, id or lun is out of range, return fail
2324          *      Test does not apply to ID 16, the pseudo id for the controller
2325          *      itself.
2326          */
2327         cid = scmd_id(scsicmd);
2328         if (cid != host->this_id) {
2329                 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2330                         if((cid >= dev->maximum_num_containers) ||
2331                                         (scsicmd->device->lun != 0)) {
2332                                 scsicmd->result = DID_NO_CONNECT << 16;
2333                                 scsicmd->scsi_done(scsicmd);
2334                                 return 0;
2335                         }
2336
2337                         /*
2338                          *      If the target container doesn't exist, it may have
2339                          *      been newly created
2340                          */
2341                         if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2342                           (fsa_dev_ptr[cid].sense_data.sense_key ==
2343                            NOT_READY)) {
2344                                 switch (scsicmd->cmnd[0]) {
2345                                 case SERVICE_ACTION_IN_16:
2346                                         if (!(dev->raw_io_interface) ||
2347                                             !(dev->raw_io_64) ||
2348                                             ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2349                                                 break;
2350                                 case INQUIRY:
2351                                 case READ_CAPACITY:
2352                                 case TEST_UNIT_READY:
2353                                         if (dev->in_reset)
2354                                                 return -1;
2355                                         return _aac_probe_container(scsicmd,
2356                                                         aac_probe_container_callback2);
2357                                 default:
2358                                         break;
2359                                 }
2360                         }
2361                 } else {  /* check for physical non-dasd devices */
2362                         if (dev->nondasd_support || expose_physicals ||
2363                                         dev->jbod) {
2364                                 if (dev->in_reset)
2365                                         return -1;
2366                                 return aac_send_srb_fib(scsicmd);
2367                         } else {
2368                                 scsicmd->result = DID_NO_CONNECT << 16;
2369                                 scsicmd->scsi_done(scsicmd);
2370                                 return 0;
2371                         }
2372                 }
2373         }
2374         /*
2375          * else Command for the controller itself
2376          */
2377         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
2378                 (scsicmd->cmnd[0] != TEST_UNIT_READY))
2379         {
2380                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2381                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2382                 set_sense(&dev->fsa_dev[cid].sense_data,
2383                   ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2384                   ASENCODE_INVALID_COMMAND, 0, 0);
2385                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2386                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2387                              SCSI_SENSE_BUFFERSIZE));
2388                 scsicmd->scsi_done(scsicmd);
2389                 return 0;
2390         }
2391
2392
2393         /* Handle commands here that don't really require going out to the adapter */
2394         switch (scsicmd->cmnd[0]) {
2395         case INQUIRY:
2396         {
2397                 struct inquiry_data inq_data;
2398
2399                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2400                 memset(&inq_data, 0, sizeof (struct inquiry_data));
2401
2402                 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2403                         char *arr = (char *)&inq_data;
2404
2405                         /* EVPD bit set */
2406                         arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2407                           INQD_PDT_PROC : INQD_PDT_DA;
2408                         if (scsicmd->cmnd[2] == 0) {
2409                                 /* supported vital product data pages */
2410                                 arr[3] = 3;
2411                                 arr[4] = 0x0;
2412                                 arr[5] = 0x80;
2413                                 arr[6] = 0x83;
2414                                 arr[1] = scsicmd->cmnd[2];
2415                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2416                                                          sizeof(inq_data));
2417                                 scsicmd->result = DID_OK << 16 |
2418                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2419                         } else if (scsicmd->cmnd[2] == 0x80) {
2420                                 /* unit serial number page */
2421                                 arr[3] = setinqserial(dev, &arr[4],
2422                                   scmd_id(scsicmd));
2423                                 arr[1] = scsicmd->cmnd[2];
2424                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2425                                                          sizeof(inq_data));
2426                                 if (aac_wwn != 2)
2427                                         return aac_get_container_serial(
2428                                                 scsicmd);
2429                                 scsicmd->result = DID_OK << 16 |
2430                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2431                         } else if (scsicmd->cmnd[2] == 0x83) {
2432                                 /* vpd page 0x83 - Device Identification Page */
2433                                 char *sno = (char *)&inq_data;
2434                                 sno[3] = setinqserial(dev, &sno[4],
2435                                                       scmd_id(scsicmd));
2436                                 if (aac_wwn != 2)
2437                                         return aac_get_container_serial(
2438                                                 scsicmd);
2439                                 scsicmd->result = DID_OK << 16 |
2440                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2441                         } else {
2442                                 /* vpd page not implemented */
2443                                 scsicmd->result = DID_OK << 16 |
2444                                   COMMAND_COMPLETE << 8 |
2445                                   SAM_STAT_CHECK_CONDITION;
2446                                 set_sense(&dev->fsa_dev[cid].sense_data,
2447                                   ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2448                                   ASENCODE_NO_SENSE, 7, 2);
2449                                 memcpy(scsicmd->sense_buffer,
2450                                   &dev->fsa_dev[cid].sense_data,
2451                                   min_t(size_t,
2452                                         sizeof(dev->fsa_dev[cid].sense_data),
2453                                         SCSI_SENSE_BUFFERSIZE));
2454                         }
2455                         scsicmd->scsi_done(scsicmd);
2456                         return 0;
2457                 }
2458                 inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
2459                 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 */
2460                 inq_data.inqd_len = 31;
2461                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
2462                 inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
2463                 /*
2464                  *      Set the Vendor, Product, and Revision Level
2465                  *      see: <vendor>.c i.e. aac.c
2466                  */
2467                 if (cid == host->this_id) {
2468                         setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2469                         inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
2470                         scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2471                                                  sizeof(inq_data));
2472                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2473                         scsicmd->scsi_done(scsicmd);
2474                         return 0;
2475                 }
2476                 if (dev->in_reset)
2477                         return -1;
2478                 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2479                 inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
2480                 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
2481                 return aac_get_container_name(scsicmd);
2482         }
2483         case SERVICE_ACTION_IN_16:
2484                 if (!(dev->raw_io_interface) ||
2485                     !(dev->raw_io_64) ||
2486                     ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2487                         break;
2488         {
2489                 u64 capacity;
2490                 char cp[13];
2491                 unsigned int alloc_len;
2492
2493                 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2494                 capacity = fsa_dev_ptr[cid].size - 1;
2495                 cp[0] = (capacity >> 56) & 0xff;
2496                 cp[1] = (capacity >> 48) & 0xff;
2497                 cp[2] = (capacity >> 40) & 0xff;
2498                 cp[3] = (capacity >> 32) & 0xff;
2499                 cp[4] = (capacity >> 24) & 0xff;
2500                 cp[5] = (capacity >> 16) & 0xff;
2501                 cp[6] = (capacity >> 8) & 0xff;
2502                 cp[7] = (capacity >> 0) & 0xff;
2503                 cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2504                 cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2505                 cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2506                 cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
2507                 cp[12] = 0;
2508
2509                 alloc_len = ((scsicmd->cmnd[10] << 24)
2510                              + (scsicmd->cmnd[11] << 16)
2511                              + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2512
2513                 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
2514                 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
2515                 if (alloc_len < scsi_bufflen(scsicmd))
2516                         scsi_set_resid(scsicmd,
2517                                        scsi_bufflen(scsicmd) - alloc_len);
2518
2519                 /* Do not cache partition table for arrays */
2520                 scsicmd->device->removable = 1;
2521
2522                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2523                 scsicmd->scsi_done(scsicmd);
2524
2525                 return 0;
2526         }
2527
2528         case READ_CAPACITY:
2529         {
2530                 u32 capacity;
2531                 char cp[8];
2532
2533                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
2534                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2535                         capacity = fsa_dev_ptr[cid].size - 1;
2536                 else
2537                         capacity = (u32)-1;
2538
2539                 cp[0] = (capacity >> 24) & 0xff;
2540                 cp[1] = (capacity >> 16) & 0xff;
2541                 cp[2] = (capacity >> 8) & 0xff;
2542                 cp[3] = (capacity >> 0) & 0xff;
2543                 cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2544                 cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2545                 cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2546                 cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
2547                 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
2548                 /* Do not cache partition table for arrays */
2549                 scsicmd->device->removable = 1;
2550                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2551                   SAM_STAT_GOOD;
2552                 scsicmd->scsi_done(scsicmd);
2553
2554                 return 0;
2555         }
2556
2557         case MODE_SENSE:
2558         {
2559                 int mode_buf_length = 4;
2560                 u32 capacity;
2561                 aac_modep_data mpd;
2562
2563                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2564                         capacity = fsa_dev_ptr[cid].size - 1;
2565                 else
2566                         capacity = (u32)-1;
2567
2568                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
2569                 memset((char *)&mpd, 0, sizeof(aac_modep_data));
2570
2571                 /* Mode data length */
2572                 mpd.hd.data_length = sizeof(mpd.hd) - 1;
2573                 /* Medium type - default */
2574                 mpd.hd.med_type = 0;
2575                 /* Device-specific param,
2576                    bit 8: 0/1 = write enabled/protected
2577                    bit 4: 0/1 = FUA enabled */
2578                 mpd.hd.dev_par = 0;
2579
2580                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2581                         mpd.hd.dev_par = 0x10;
2582                 if (scsicmd->cmnd[1] & 0x8)
2583                         mpd.hd.bd_length = 0;   /* Block descriptor length */
2584                 else {
2585                         mpd.hd.bd_length = sizeof(mpd.bd);
2586                         mpd.hd.data_length += mpd.hd.bd_length;
2587                         mpd.bd.block_length[0] =
2588                                 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2589                         mpd.bd.block_length[1] =
2590                                 (fsa_dev_ptr[cid].block_size >> 8) &  0xff;
2591                         mpd.bd.block_length[2] =
2592                                 fsa_dev_ptr[cid].block_size  & 0xff;
2593
2594                         mpd.mpc_buf[0] = scsicmd->cmnd[2];
2595                         if (scsicmd->cmnd[2] == 0x1C) {
2596                                 /* page length */
2597                                 mpd.mpc_buf[1] = 0xa;
2598                                 /* Mode data length */
2599                                 mpd.hd.data_length = 23;
2600                         } else {
2601                                 /* Mode data length */
2602                                 mpd.hd.data_length = 15;
2603                         }
2604
2605                         if (capacity > 0xffffff) {
2606                                 mpd.bd.block_count[0] = 0xff;
2607                                 mpd.bd.block_count[1] = 0xff;
2608                                 mpd.bd.block_count[2] = 0xff;
2609                         } else {
2610                                 mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
2611                                 mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
2612                                 mpd.bd.block_count[2] = capacity  & 0xff;
2613                         }
2614                 }
2615                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2616                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2617                         mpd.hd.data_length += 3;
2618                         mpd.mpc_buf[0] = 8;
2619                         mpd.mpc_buf[1] = 1;
2620                         mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
2621                                 ? 0 : 0x04; /* WCE */
2622                         mode_buf_length = sizeof(mpd);
2623                 }
2624
2625                 if (mode_buf_length > scsicmd->cmnd[4])
2626                         mode_buf_length = scsicmd->cmnd[4];
2627                 else
2628                         mode_buf_length = sizeof(mpd);
2629                 scsi_sg_copy_from_buffer(scsicmd,
2630                                          (char *)&mpd,
2631                                          mode_buf_length);
2632                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2633                 scsicmd->scsi_done(scsicmd);
2634
2635                 return 0;
2636         }
2637         case MODE_SENSE_10:
2638         {
2639                 u32 capacity;
2640                 int mode_buf_length = 8;
2641                 aac_modep10_data mpd10;
2642
2643                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2644                         capacity = fsa_dev_ptr[cid].size - 1;
2645                 else
2646                         capacity = (u32)-1;
2647
2648                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
2649                 memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
2650                 /* Mode data length (MSB) */
2651                 mpd10.hd.data_length[0] = 0;
2652                 /* Mode data length (LSB) */
2653                 mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
2654                 /* Medium type - default */
2655                 mpd10.hd.med_type = 0;
2656                 /* Device-specific param,
2657                    bit 8: 0/1 = write enabled/protected
2658                    bit 4: 0/1 = FUA enabled */
2659                 mpd10.hd.dev_par = 0;
2660
2661                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2662                         mpd10.hd.dev_par = 0x10;
2663                 mpd10.hd.rsrvd[0] = 0;  /* reserved */
2664                 mpd10.hd.rsrvd[1] = 0;  /* reserved */
2665                 if (scsicmd->cmnd[1] & 0x8) {
2666                         /* Block descriptor length (MSB) */
2667                         mpd10.hd.bd_length[0] = 0;
2668                         /* Block descriptor length (LSB) */
2669                         mpd10.hd.bd_length[1] = 0;
2670                 } else {
2671                         mpd10.hd.bd_length[0] = 0;
2672                         mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
2673
2674                         mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
2675
2676                         mpd10.bd.block_length[0] =
2677                                 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2678                         mpd10.bd.block_length[1] =
2679                                 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2680                         mpd10.bd.block_length[2] =
2681                                 fsa_dev_ptr[cid].block_size  & 0xff;
2682
2683                         if (capacity > 0xffffff) {
2684                                 mpd10.bd.block_count[0] = 0xff;
2685                                 mpd10.bd.block_count[1] = 0xff;
2686                                 mpd10.bd.block_count[2] = 0xff;
2687                         } else {
2688                                 mpd10.bd.block_count[0] =
2689                                         (capacity >> 16) & 0xff;
2690                                 mpd10.bd.block_count[1] =
2691                                         (capacity >> 8) & 0xff;
2692                                 mpd10.bd.block_count[2] =
2693                                         capacity  & 0xff;
2694                         }
2695                 }
2696                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2697                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2698                         mpd10.hd.data_length[1] += 3;
2699                         mpd10.mpc_buf[0] = 8;
2700                         mpd10.mpc_buf[1] = 1;
2701                         mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
2702                                 ? 0 : 0x04; /* WCE */
2703                         mode_buf_length = sizeof(mpd10);
2704                         if (mode_buf_length > scsicmd->cmnd[8])
2705                                 mode_buf_length = scsicmd->cmnd[8];
2706                 }
2707                 scsi_sg_copy_from_buffer(scsicmd,
2708                                          (char *)&mpd10,
2709                                          mode_buf_length);
2710
2711                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2712                 scsicmd->scsi_done(scsicmd);
2713
2714                 return 0;
2715         }
2716         case REQUEST_SENSE:
2717                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2718                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2719                 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2720                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2721                 scsicmd->scsi_done(scsicmd);
2722                 return 0;
2723
2724         case ALLOW_MEDIUM_REMOVAL:
2725                 dprintk((KERN_DEBUG "LOCK command.\n"));
2726                 if (scsicmd->cmnd[4])
2727                         fsa_dev_ptr[cid].locked = 1;
2728                 else
2729                         fsa_dev_ptr[cid].locked = 0;
2730
2731                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2732                 scsicmd->scsi_done(scsicmd);
2733                 return 0;
2734         /*
2735          *      These commands are all No-Ops
2736          */
2737         case TEST_UNIT_READY:
2738                 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
2739                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2740                                 SAM_STAT_CHECK_CONDITION;
2741                         set_sense(&dev->fsa_dev[cid].sense_data,
2742                                   NOT_READY, SENCODE_BECOMING_READY,
2743                                   ASENCODE_BECOMING_READY, 0, 0);
2744                         memcpy(scsicmd->sense_buffer,
2745                                &dev->fsa_dev[cid].sense_data,
2746                                min_t(size_t,
2747                                      sizeof(dev->fsa_dev[cid].sense_data),
2748                                      SCSI_SENSE_BUFFERSIZE));
2749                         scsicmd->scsi_done(scsicmd);
2750                         return 0;
2751                 }
2752                 /* FALLTHRU */
2753         case RESERVE:
2754         case RELEASE:
2755         case REZERO_UNIT:
2756         case REASSIGN_BLOCKS:
2757         case SEEK_10:
2758                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2759                 scsicmd->scsi_done(scsicmd);
2760                 return 0;
2761
2762         case START_STOP:
2763                 return aac_start_stop(scsicmd);
2764         }
2765
2766         switch (scsicmd->cmnd[0])
2767         {
2768                 case READ_6:
2769                 case READ_10:
2770                 case READ_12:
2771                 case READ_16:
2772                         if (dev->in_reset)
2773                                 return -1;
2774                         /*
2775                          *      Hack to keep track of ordinal number of the device that
2776                          *      corresponds to a container. Needed to convert
2777                          *      containers to /dev/sd device names
2778                          */
2779
2780                         if (scsicmd->request->rq_disk)
2781                                 strlcpy(fsa_dev_ptr[cid].devname,
2782                                 scsicmd->request->rq_disk->disk_name,
2783                                 min(sizeof(fsa_dev_ptr[cid].devname),
2784                                 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
2785
2786                         return aac_read(scsicmd);
2787
2788                 case WRITE_6:
2789                 case WRITE_10:
2790                 case WRITE_12:
2791                 case WRITE_16:
2792                         if (dev->in_reset)
2793                                 return -1;
2794                         return aac_write(scsicmd);
2795
2796                 case SYNCHRONIZE_CACHE:
2797                         if (((aac_cache & 6) == 6) && dev->cache_protected) {
2798                                 scsicmd->result = DID_OK << 16 |
2799                                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2800                                 scsicmd->scsi_done(scsicmd);
2801                                 return 0;
2802                         }
2803                         /* Issue FIB to tell Firmware to flush it's cache */
2804                         if ((aac_cache & 6) != 2)
2805                                 return aac_synchronize(scsicmd);
2806                         /* FALLTHRU */
2807                 default:
2808                         /*
2809                          *      Unhandled commands
2810                          */
2811                         dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
2812                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2813                         set_sense(&dev->fsa_dev[cid].sense_data,
2814                           ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2815                           ASENCODE_INVALID_COMMAND, 0, 0);
2816                         memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2817                                 min_t(size_t,
2818                                       sizeof(dev->fsa_dev[cid].sense_data),
2819                                       SCSI_SENSE_BUFFERSIZE));
2820                         scsicmd->scsi_done(scsicmd);
2821                         return 0;
2822         }
2823 }
2824
2825 static int query_disk(struct aac_dev *dev, void __user *arg)
2826 {
2827         struct aac_query_disk qd;
2828         struct fsa_dev_info *fsa_dev_ptr;
2829
2830         fsa_dev_ptr = dev->fsa_dev;
2831         if (!fsa_dev_ptr)
2832                 return -EBUSY;
2833         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2834                 return -EFAULT;
2835         if (qd.cnum == -1)
2836                 qd.cnum = qd.id;
2837         else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2838         {
2839                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2840                         return -EINVAL;
2841                 qd.instance = dev->scsi_host_ptr->host_no;
2842                 qd.bus = 0;
2843                 qd.id = CONTAINER_TO_ID(qd.cnum);
2844                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2845         }
2846         else return -EINVAL;
2847
2848         qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
2849         qd.locked = fsa_dev_ptr[qd.cnum].locked;
2850         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2851
2852         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2853                 qd.unmapped = 1;
2854         else
2855                 qd.unmapped = 0;
2856
2857         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2858           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2859
2860         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2861                 return -EFAULT;
2862         return 0;
2863 }
2864
2865 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2866 {
2867         struct aac_delete_disk dd;
2868         struct fsa_dev_info *fsa_dev_ptr;
2869
2870         fsa_dev_ptr = dev->fsa_dev;
2871         if (!fsa_dev_ptr)
2872                 return -EBUSY;
2873
2874         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2875                 return -EFAULT;
2876
2877         if (dd.cnum >= dev->maximum_num_containers)
2878                 return -EINVAL;
2879         /*
2880          *      Mark this container as being deleted.
2881          */
2882         fsa_dev_ptr[dd.cnum].deleted = 1;
2883         /*
2884          *      Mark the container as no longer valid
2885          */
2886         fsa_dev_ptr[dd.cnum].valid = 0;
2887         return 0;
2888 }
2889
2890 static int delete_disk(struct aac_dev *dev, void __user *arg)
2891 {
2892         struct aac_delete_disk dd;
2893         struct fsa_dev_info *fsa_dev_ptr;
2894
2895         fsa_dev_ptr = dev->fsa_dev;
2896         if (!fsa_dev_ptr)
2897                 return -EBUSY;
2898
2899         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2900                 return -EFAULT;
2901
2902         if (dd.cnum >= dev->maximum_num_containers)
2903                 return -EINVAL;
2904         /*
2905          *      If the container is locked, it can not be deleted by the API.
2906          */
2907         if (fsa_dev_ptr[dd.cnum].locked)
2908                 return -EBUSY;
2909         else {
2910                 /*
2911                  *      Mark the container as no longer being valid.
2912                  */
2913                 fsa_dev_ptr[dd.cnum].valid = 0;
2914                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2915                 return 0;
2916         }
2917 }
2918
2919 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2920 {
2921         switch (cmd) {
2922         case FSACTL_QUERY_DISK:
2923                 return query_disk(dev, arg);
2924         case FSACTL_DELETE_DISK:
2925                 return delete_disk(dev, arg);
2926         case FSACTL_FORCE_DELETE_DISK:
2927                 return force_delete_disk(dev, arg);
2928         case FSACTL_GET_CONTAINERS:
2929                 return aac_get_containers(dev);
2930         default:
2931                 return -ENOTTY;
2932         }
2933 }
2934
2935 /**
2936  *
2937  * aac_srb_callback
2938  * @context: the context set in the fib - here it is scsi cmd
2939  * @fibptr: pointer to the fib
2940  *
2941  * Handles the completion of a scsi command to a non dasd device
2942  *
2943  */
2944
2945 static void aac_srb_callback(void *context, struct fib * fibptr)
2946 {
2947         struct aac_dev *dev;
2948         struct aac_srb_reply *srbreply;
2949         struct scsi_cmnd *scsicmd;
2950
2951         scsicmd = (struct scsi_cmnd *) context;
2952
2953         if (!aac_valid_context(scsicmd, fibptr))
2954                 return;
2955
2956         BUG_ON(fibptr == NULL);
2957         dev = fibptr->dev;
2958
2959         scsi_dma_unmap(scsicmd);
2960
2961         /* expose physical device if expose_physicald flag is on */
2962         if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
2963           && expose_physicals > 0)
2964                 aac_expose_phy_device(scsicmd);
2965
2966         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2967         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
2968
2969         if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
2970                 /* fast response */
2971                 srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
2972                 srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
2973         } else {
2974                 /*
2975                  *      Calculate resid for sg
2976                  */
2977                 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2978                                    - le32_to_cpu(srbreply->data_xfer_length));
2979                 /*
2980                  * First check the fib status
2981                  */
2982
2983                 if (le32_to_cpu(srbreply->status) != ST_OK) {
2984                         int len;
2985
2986                         printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2987                         len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
2988                                     SCSI_SENSE_BUFFERSIZE);
2989                         scsicmd->result = DID_ERROR << 16
2990                                                 | COMMAND_COMPLETE << 8
2991                                                 | SAM_STAT_CHECK_CONDITION;
2992                         memcpy(scsicmd->sense_buffer,
2993                                         srbreply->sense_data, len);
2994                 }
2995
2996                 /*
2997                  * Next check the srb status
2998                  */
2999                 switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3000                 case SRB_STATUS_ERROR_RECOVERY:
3001                 case SRB_STATUS_PENDING:
3002                 case SRB_STATUS_SUCCESS:
3003                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3004                         break;
3005                 case SRB_STATUS_DATA_OVERRUN:
3006                         switch (scsicmd->cmnd[0]) {
3007                         case  READ_6:
3008                         case  WRITE_6:
3009                         case  READ_10:
3010                         case  WRITE_10:
3011                         case  READ_12:
3012                         case  WRITE_12:
3013                         case  READ_16:
3014                         case  WRITE_16:
3015                                 if (le32_to_cpu(srbreply->data_xfer_length)
3016                                                         < scsicmd->underflow)
3017                                         printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
3018                                 else
3019                                         printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
3020                                 scsicmd->result = DID_ERROR << 16
3021                                                         | COMMAND_COMPLETE << 8;
3022                                 break;
3023                         case INQUIRY: {
3024                                 scsicmd->result = DID_OK << 16
3025                                                         | COMMAND_COMPLETE << 8;
3026                                 break;
3027                         }
3028                         default:
3029                                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3030                                 break;
3031                         }
3032                         break;
3033                 case SRB_STATUS_ABORTED:
3034                         scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3035                         break;
3036                 case SRB_STATUS_ABORT_FAILED:
3037                         /*
3038                          * Not sure about this one - but assuming the
3039                          * hba was trying to abort for some reason
3040                          */
3041                         scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3042                         break;
3043                 case SRB_STATUS_PARITY_ERROR:
3044                         scsicmd->result = DID_PARITY << 16
3045                                                 | MSG_PARITY_ERROR << 8;
3046                         break;
3047                 case SRB_STATUS_NO_DEVICE:
3048                 case SRB_STATUS_INVALID_PATH_ID:
3049                 case SRB_STATUS_INVALID_TARGET_ID:
3050                 case SRB_STATUS_INVALID_LUN:
3051                 case SRB_STATUS_SELECTION_TIMEOUT:
3052                         scsicmd->result = DID_NO_CONNECT << 16
3053                                                 | COMMAND_COMPLETE << 8;
3054                         break;
3055
3056                 case SRB_STATUS_COMMAND_TIMEOUT:
3057                 case SRB_STATUS_TIMEOUT:
3058                         scsicmd->result = DID_TIME_OUT << 16
3059                                                 | COMMAND_COMPLETE << 8;
3060                         break;
3061
3062                 case SRB_STATUS_BUSY:
3063                         scsicmd->result = DID_BUS_BUSY << 16
3064                                                 | COMMAND_COMPLETE << 8;
3065                         break;
3066
3067                 case SRB_STATUS_BUS_RESET:
3068                         scsicmd->result = DID_RESET << 16
3069                                                 | COMMAND_COMPLETE << 8;
3070                         break;
3071
3072                 case SRB_STATUS_MESSAGE_REJECTED:
3073                         scsicmd->result = DID_ERROR << 16
3074                                                 | MESSAGE_REJECT << 8;
3075                         break;
3076                 case SRB_STATUS_REQUEST_FLUSHED:
3077                 case SRB_STATUS_ERROR:
3078                 case SRB_STATUS_INVALID_REQUEST:
3079                 case SRB_STATUS_REQUEST_SENSE_FAILED:
3080                 case SRB_STATUS_NO_HBA:
3081                 case SRB_STATUS_UNEXPECTED_BUS_FREE:
3082                 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3083                 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3084                 case SRB_STATUS_DELAYED_RETRY:
3085                 case SRB_STATUS_BAD_FUNCTION:
3086                 case SRB_STATUS_NOT_STARTED:
3087                 case SRB_STATUS_NOT_IN_USE:
3088                 case SRB_STATUS_FORCE_ABORT:
3089                 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3090                 default:
3091 #ifdef AAC_DETAILED_STATUS_INFO
3092                         printk(KERN_INFO "aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
3093                                 le32_to_cpu(srbreply->srb_status) & 0x3F,
3094                                 aac_get_status_string(
3095                                         le32_to_cpu(srbreply->srb_status) & 0x3F),
3096                                 scsicmd->cmnd[0],
3097                                 le32_to_cpu(srbreply->scsi_status));
3098 #endif
3099                         if ((scsicmd->cmnd[0] == ATA_12)
3100                                 || (scsicmd->cmnd[0] == ATA_16)) {
3101                                         if (scsicmd->cmnd[2] & (0x01 << 5)) {
3102                                                 scsicmd->result = DID_OK << 16
3103                                                         | COMMAND_COMPLETE << 8;
3104                                 break;
3105                                 } else {
3106                                         scsicmd->result = DID_ERROR << 16
3107                                                 | COMMAND_COMPLETE << 8;
3108                                         break;
3109                                 }
3110                         } else {
3111                                 scsicmd->result = DID_ERROR << 16
3112                                         | COMMAND_COMPLETE << 8;
3113                                 break;
3114                         }
3115                 }
3116                 if (le32_to_cpu(srbreply->scsi_status)
3117                                 == SAM_STAT_CHECK_CONDITION) {
3118                         int len;
3119
3120                         scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3121                         len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3122                                     SCSI_SENSE_BUFFERSIZE);
3123 #ifdef AAC_DETAILED_STATUS_INFO
3124                         printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
3125                                                 le32_to_cpu(srbreply->status), len);
3126 #endif
3127                         memcpy(scsicmd->sense_buffer,
3128                                         srbreply->sense_data, len);
3129                 }
3130         }
3131         /*
3132          * OR in the scsi status (already shifted up a bit)
3133          */
3134         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3135
3136         aac_fib_complete(fibptr);
3137         scsicmd->scsi_done(scsicmd);
3138 }
3139
3140 /**
3141  *
3142  * aac_send_scb_fib
3143  * @scsicmd: the scsi command block
3144  *
3145  * This routine will form a FIB and fill in the aac_srb from the
3146  * scsicmd passed in.
3147  */
3148
3149 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3150 {
3151         struct fib* cmd_fibcontext;
3152         struct aac_dev* dev;
3153         int status;
3154
3155         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3156         if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3157                         scsicmd->device->lun > 7) {
3158                 scsicmd->result = DID_NO_CONNECT << 16;
3159                 scsicmd->scsi_done(scsicmd);
3160                 return 0;
3161         }
3162
3163         /*
3164          *      Allocate and initialize a Fib then setup a BlockWrite command
3165          */
3166         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3167
3168         status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
3169
3170         /*
3171          *      Check that the command queued to the controller
3172          */
3173         if (status == -EINPROGRESS) {
3174                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3175                 return 0;
3176         }
3177
3178         printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3179         aac_fib_complete(cmd_fibcontext);
3180         aac_fib_free(cmd_fibcontext);
3181
3182         return -1;
3183 }
3184
3185 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
3186 {
3187         struct aac_dev *dev;
3188         unsigned long byte_count = 0;
3189         int nseg;
3190
3191         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3192         // Get rid of old data
3193         psg->count = 0;
3194         psg->sg[0].addr = 0;
3195         psg->sg[0].count = 0;
3196
3197         nseg = scsi_dma_map(scsicmd);
3198         if (nseg < 0)
3199                 return nseg;
3200         if (nseg) {
3201                 struct scatterlist *sg;
3202                 int i;
3203
3204                 psg->count = cpu_to_le32(nseg);
3205
3206                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3207                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3208                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3209                         byte_count += sg_dma_len(sg);
3210                 }
3211                 /* hba wants the size to be exact */
3212                 if (byte_count > scsi_bufflen(scsicmd)) {
3213                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3214                                 (byte_count - scsi_bufflen(scsicmd));
3215                         psg->sg[i-1].count = cpu_to_le32(temp);
3216                         byte_count = scsi_bufflen(scsicmd);
3217                 }
3218                 /* Check for command underflow */
3219                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3220                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3221                                         byte_count, scsicmd->underflow);
3222                 }
3223         }
3224         return byte_count;
3225 }
3226
3227
3228 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
3229 {
3230         struct aac_dev *dev;
3231         unsigned long byte_count = 0;
3232         u64 addr;
3233         int nseg;
3234
3235         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3236         // Get rid of old data
3237         psg->count = 0;
3238         psg->sg[0].addr[0] = 0;
3239         psg->sg[0].addr[1] = 0;
3240         psg->sg[0].count = 0;
3241
3242         nseg = scsi_dma_map(scsicmd);
3243         if (nseg < 0)
3244                 return nseg;
3245         if (nseg) {
3246                 struct scatterlist *sg;
3247                 int i;
3248
3249                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3250                         int count = sg_dma_len(sg);
3251                         addr = sg_dma_address(sg);
3252                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3253                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
3254                         psg->sg[i].count = cpu_to_le32(count);
3255                         byte_count += count;
3256                 }
3257                 psg->count = cpu_to_le32(nseg);
3258                 /* hba wants the size to be exact */
3259                 if (byte_count > scsi_bufflen(scsicmd)) {
3260                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3261                                 (byte_count - scsi_bufflen(scsicmd));
3262                         psg->sg[i-1].count = cpu_to_le32(temp);
3263                         byte_count = scsi_bufflen(scsicmd);
3264                 }
3265                 /* Check for command underflow */
3266                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3267                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3268                                         byte_count, scsicmd->underflow);
3269                 }
3270         }
3271         return byte_count;
3272 }
3273
3274 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
3275 {
3276         unsigned long byte_count = 0;
3277         int nseg;
3278
3279         // Get rid of old data
3280         psg->count = 0;
3281         psg->sg[0].next = 0;
3282         psg->sg[0].prev = 0;
3283         psg->sg[0].addr[0] = 0;
3284         psg->sg[0].addr[1] = 0;
3285         psg->sg[0].count = 0;
3286         psg->sg[0].flags = 0;
3287
3288         nseg = scsi_dma_map(scsicmd);
3289         if (nseg < 0)
3290                 return nseg;
3291         if (nseg) {
3292                 struct scatterlist *sg;
3293                 int i;
3294
3295                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3296                         int count = sg_dma_len(sg);
3297                         u64 addr = sg_dma_address(sg);
3298                         psg->sg[i].next = 0;
3299                         psg->sg[i].prev = 0;
3300                         psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3301                         psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3302                         psg->sg[i].count = cpu_to_le32(count);
3303                         psg->sg[i].flags = 0;
3304                         byte_count += count;
3305                 }
3306                 psg->count = cpu_to_le32(nseg);
3307                 /* hba wants the size to be exact */
3308                 if (byte_count > scsi_bufflen(scsicmd)) {
3309                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3310                                 (byte_count - scsi_bufflen(scsicmd));
3311                         psg->sg[i-1].count = cpu_to_le32(temp);
3312                         byte_count = scsi_bufflen(scsicmd);
3313                 }
3314                 /* Check for command underflow */
3315                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3316                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3317                                         byte_count, scsicmd->underflow);
3318                 }
3319         }
3320         return byte_count;
3321 }
3322
3323 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3324                                 struct aac_raw_io2 *rio2, int sg_max)
3325 {
3326         unsigned long byte_count = 0;
3327         int nseg;
3328
3329         nseg = scsi_dma_map(scsicmd);
3330         if (nseg < 0)
3331                 return nseg;
3332         if (nseg) {
3333                 struct scatterlist *sg;
3334                 int i, conformable = 0;
3335                 u32 min_size = PAGE_SIZE, cur_size;
3336
3337                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3338                         int count = sg_dma_len(sg);
3339                         u64 addr = sg_dma_address(sg);
3340
3341                         BUG_ON(i >= sg_max);
3342                         rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3343                         rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3344                         cur_size = cpu_to_le32(count);
3345                         rio2->sge[i].length = cur_size;
3346                         rio2->sge[i].flags = 0;
3347                         if (i == 0) {
3348                                 conformable = 1;
3349                                 rio2->sgeFirstSize = cur_size;
3350                         } else if (i == 1) {
3351                                 rio2->sgeNominalSize = cur_size;
3352                                 min_size = cur_size;
3353                         } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
3354                                 conformable = 0;
3355                                 if (cur_size < min_size)
3356                                         min_size = cur_size;
3357                         }
3358                         byte_count += count;
3359                 }
3360
3361                 /* hba wants the size to be exact */
3362                 if (byte_count > scsi_bufflen(scsicmd)) {
3363                         u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
3364                                 (byte_count - scsi_bufflen(scsicmd));
3365                         rio2->sge[i-1].length = cpu_to_le32(temp);
3366                         byte_count = scsi_bufflen(scsicmd);
3367                 }
3368
3369                 rio2->sgeCnt = cpu_to_le32(nseg);
3370                 rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
3371                 /* not conformable: evaluate required sg elements */
3372                 if (!conformable) {
3373                         int j, nseg_new = nseg, err_found;
3374                         for (i = min_size / PAGE_SIZE; i >= 1; --i) {
3375                                 err_found = 0;
3376                                 nseg_new = 2;
3377                                 for (j = 1; j < nseg - 1; ++j) {
3378                                         if (rio2->sge[j].length % (i*PAGE_SIZE)) {
3379                                                 err_found = 1;
3380                                                 break;
3381                                         }
3382                                         nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
3383                                 }
3384                                 if (!err_found)
3385                                         break;
3386                         }
3387                         if (i > 0 && nseg_new <= sg_max)
3388                                 aac_convert_sgraw2(rio2, i, nseg, nseg_new);
3389                 } else
3390                         rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3391
3392                 /* Check for command underflow */
3393                 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3394                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3395                                         byte_count, scsicmd->underflow);
3396                 }
3397         }
3398
3399         return byte_count;
3400 }
3401
3402 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
3403 {
3404         struct sge_ieee1212 *sge;
3405         int i, j, pos;
3406         u32 addr_low;
3407
3408         if (aac_convert_sgl == 0)
3409                 return 0;
3410
3411         sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
3412         if (sge == NULL)
3413                 return -1;
3414
3415         for (i = 1, pos = 1; i < nseg-1; ++i) {
3416                 for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
3417                         addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
3418                         sge[pos].addrLow = addr_low;
3419                         sge[pos].addrHigh = rio2->sge[i].addrHigh;
3420                         if (addr_low < rio2->sge[i].addrLow)
3421                                 sge[pos].addrHigh++;
3422                         sge[pos].length = pages * PAGE_SIZE;
3423                         sge[pos].flags = 0;
3424                         pos++;
3425                 }
3426         }
3427         sge[pos] = rio2->sge[nseg-1];
3428         memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
3429
3430         kfree(sge);
3431         rio2->sgeCnt = cpu_to_le32(nseg_new);
3432         rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3433         rio2->sgeNominalSize = pages * PAGE_SIZE;
3434         return 0;
3435 }
3436
3437 #ifdef AAC_DETAILED_STATUS_INFO
3438
3439 struct aac_srb_status_info {
3440         u32     status;
3441         char    *str;
3442 };
3443
3444
3445 static struct aac_srb_status_info srb_status_info[] = {
3446         { SRB_STATUS_PENDING,           "Pending Status"},
3447         { SRB_STATUS_SUCCESS,           "Success"},
3448         { SRB_STATUS_ABORTED,           "Aborted Command"},
3449         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
3450         { SRB_STATUS_ERROR,             "Error Event"},
3451         { SRB_STATUS_BUSY,              "Device Busy"},
3452         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
3453         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
3454         { SRB_STATUS_NO_DEVICE,         "No Device"},
3455         { SRB_STATUS_TIMEOUT,           "Timeout"},
3456         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
3457         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
3458         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
3459         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
3460         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
3461         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
3462         { SRB_STATUS_NO_HBA,            "No HBA"},
3463         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
3464         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
3465         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
3466         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
3467         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
3468         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
3469         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"},
3470         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
3471         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
3472         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
3473         { SRB_STATUS_NOT_STARTED,       "Not Started"},
3474         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
3475         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
3476         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
3477         { 0xff,                         "Unknown Error"}
3478 };
3479
3480 char *aac_get_status_string(u32 status)
3481 {
3482         int i;
3483
3484         for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
3485                 if (srb_status_info[i].status == status)
3486                         return srb_status_info[i].str;
3487
3488         return "Bad Status Code";
3489 }
3490
3491 #endif