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