]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/aacraid/aachba.c
scsi: aacraid: Add helper function to set queue depth
[linux.git] / drivers / scsi / aacraid / aachba.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc.
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2010 Adaptec, Inc.
9  *               2010-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                 c = 0;
944                 if (strlen(cp) > sizeof(str->pid)) {
945                         c = cp[sizeof(str->pid)];
946                         cp[sizeof(str->pid)] = '\0';
947                 }
948                 inqstrcpy (cp, str->pid);
949
950                 kfree(cname);
951         } else {
952                 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
953
954                 inqstrcpy (mp->vname, str->vid);
955                 /* last six chars reserved for vol type */
956                 inqstrcpy (mp->model, str->pid);
957         }
958
959         if (tindex < ARRAY_SIZE(container_types)){
960                 char *findit = str->pid;
961
962                 for ( ; *findit != ' '; findit++); /* walk till we find a space */
963                 /* RAID is superfluous in the context of a RAID device */
964                 if (memcmp(findit-4, "RAID", 4) == 0)
965                         *(findit -= 4) = ' ';
966                 if (((findit - str->pid) + strlen(container_types[tindex]))
967                  < (sizeof(str->pid) + sizeof(str->prl)))
968                         inqstrcpy (container_types[tindex], findit + 1);
969         }
970         inqstrcpy ("V1.0", str->prl);
971 }
972
973 static void build_vpd83_type3(struct tvpd_page83 *vpdpage83data,
974                 struct aac_dev *dev, struct scsi_cmnd *scsicmd)
975 {
976         int container;
977
978         vpdpage83data->type3.codeset = 1;
979         vpdpage83data->type3.identifiertype = 3;
980         vpdpage83data->type3.identifierlength = sizeof(vpdpage83data->type3)
981                         - 4;
982
983         for (container = 0; container < dev->maximum_num_containers;
984                         container++) {
985
986                 if (scmd_id(scsicmd) == container) {
987                         memcpy(vpdpage83data->type3.Identifier,
988                                         dev->fsa_dev[container].identifier,
989                                         16);
990                         break;
991                 }
992         }
993 }
994
995 static void get_container_serial_callback(void *context, struct fib * fibptr)
996 {
997         struct aac_get_serial_resp * get_serial_reply;
998         struct scsi_cmnd * scsicmd;
999
1000         BUG_ON(fibptr == NULL);
1001
1002         scsicmd = (struct scsi_cmnd *) context;
1003         if (!aac_valid_context(scsicmd, fibptr))
1004                 return;
1005
1006         get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
1007         /* Failure is irrelevant, using default value instead */
1008         if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
1009                 /*Check to see if it's for VPD 0x83 or 0x80 */
1010                 if (scsicmd->cmnd[2] == 0x83) {
1011                         /* vpd page 0x83 - Device Identification Page */
1012                         struct aac_dev *dev;
1013                         int i;
1014                         struct tvpd_page83 vpdpage83data;
1015
1016                         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1017
1018                         memset(((u8 *)&vpdpage83data), 0,
1019                                sizeof(vpdpage83data));
1020
1021                         /* DIRECT_ACCESS_DEVIC */
1022                         vpdpage83data.DeviceType = 0;
1023                         /* DEVICE_CONNECTED */
1024                         vpdpage83data.DeviceTypeQualifier = 0;
1025                         /* VPD_DEVICE_IDENTIFIERS */
1026                         vpdpage83data.PageCode = 0x83;
1027                         vpdpage83data.reserved = 0;
1028                         vpdpage83data.PageLength =
1029                                 sizeof(vpdpage83data.type1) +
1030                                 sizeof(vpdpage83data.type2);
1031
1032                         /* VPD 83 Type 3 is not supported for ARC */
1033                         if (dev->sa_firmware)
1034                                 vpdpage83data.PageLength +=
1035                                 sizeof(vpdpage83data.type3);
1036
1037                         /* T10 Vendor Identifier Field Format */
1038                         /* VpdcodesetAscii */
1039                         vpdpage83data.type1.codeset = 2;
1040                         /* VpdIdentifierTypeVendorId */
1041                         vpdpage83data.type1.identifiertype = 1;
1042                         vpdpage83data.type1.identifierlength =
1043                                 sizeof(vpdpage83data.type1) - 4;
1044
1045                         /* "ADAPTEC " for adaptec */
1046                         memcpy(vpdpage83data.type1.venid,
1047                                 "ADAPTEC ",
1048                                 sizeof(vpdpage83data.type1.venid));
1049                         memcpy(vpdpage83data.type1.productid,
1050                                 "ARRAY           ",
1051                                 sizeof(
1052                                 vpdpage83data.type1.productid));
1053
1054                         /* Convert to ascii based serial number.
1055                          * The LSB is the the end.
1056                          */
1057                         for (i = 0; i < 8; i++) {
1058                                 u8 temp =
1059                                         (u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
1060                                 if (temp  > 0x9) {
1061                                         vpdpage83data.type1.serialnumber[i] =
1062                                                         'A' + (temp - 0xA);
1063                                 } else {
1064                                         vpdpage83data.type1.serialnumber[i] =
1065                                                         '0' + temp;
1066                                 }
1067                         }
1068
1069                         /* VpdCodeSetBinary */
1070                         vpdpage83data.type2.codeset = 1;
1071                         /* VpdidentifiertypeEUI64 */
1072                         vpdpage83data.type2.identifiertype = 2;
1073                         vpdpage83data.type2.identifierlength =
1074                                 sizeof(vpdpage83data.type2) - 4;
1075
1076                         vpdpage83data.type2.eu64id.venid[0] = 0xD0;
1077                         vpdpage83data.type2.eu64id.venid[1] = 0;
1078                         vpdpage83data.type2.eu64id.venid[2] = 0;
1079
1080                         vpdpage83data.type2.eu64id.Serial =
1081                                                         get_serial_reply->uid;
1082                         vpdpage83data.type2.eu64id.reserved = 0;
1083
1084                         /*
1085                          * VpdIdentifierTypeFCPHName
1086                          * VPD 0x83 Type 3 not supported for ARC
1087                          */
1088                         if (dev->sa_firmware) {
1089                                 build_vpd83_type3(&vpdpage83data,
1090                                                 dev, scsicmd);
1091                         }
1092
1093                         /* Move the inquiry data to the response buffer. */
1094                         scsi_sg_copy_from_buffer(scsicmd, &vpdpage83data,
1095                                                  sizeof(vpdpage83data));
1096                 } else {
1097                         /* It must be for VPD 0x80 */
1098                         char sp[13];
1099                         /* EVPD bit set */
1100                         sp[0] = INQD_PDT_DA;
1101                         sp[1] = scsicmd->cmnd[2];
1102                         sp[2] = 0;
1103                         sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1104                                 le32_to_cpu(get_serial_reply->uid));
1105                         scsi_sg_copy_from_buffer(scsicmd, sp,
1106                                                  sizeof(sp));
1107                 }
1108         }
1109
1110         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1111
1112         aac_fib_complete(fibptr);
1113         scsicmd->scsi_done(scsicmd);
1114 }
1115
1116 /**
1117  *      aac_get_container_serial - get container serial, none blocking.
1118  */
1119 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1120 {
1121         int status;
1122         struct aac_get_serial *dinfo;
1123         struct fib * cmd_fibcontext;
1124         struct aac_dev * dev;
1125
1126         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1127
1128         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1129
1130         aac_fib_init(cmd_fibcontext);
1131         dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1132
1133         dinfo->command = cpu_to_le32(VM_ContainerConfig);
1134         dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1135         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1136         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1137
1138         status = aac_fib_send(ContainerCommand,
1139                   cmd_fibcontext,
1140                   sizeof(struct aac_get_serial_resp),
1141                   FsaNormal,
1142                   0, 1,
1143                   (fib_callback) get_container_serial_callback,
1144                   (void *) scsicmd);
1145
1146         /*
1147          *      Check that the command queued to the controller
1148          */
1149         if (status == -EINPROGRESS)
1150                 return 0;
1151
1152         printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1153         aac_fib_complete(cmd_fibcontext);
1154         return -1;
1155 }
1156
1157 /* Function: setinqserial
1158  *
1159  * Arguments: [1] pointer to void [1] int
1160  *
1161  * Purpose: Sets SCSI Unit Serial number.
1162  *          This is a fake. We should read a proper
1163  *          serial number from the container. <SuSE>But
1164  *          without docs it's quite hard to do it :-)
1165  *          So this will have to do in the meantime.</SuSE>
1166  */
1167
1168 static int setinqserial(struct aac_dev *dev, void *data, int cid)
1169 {
1170         /*
1171          *      This breaks array migration.
1172          */
1173         return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1174                         le32_to_cpu(dev->adapter_info.serial[0]), cid);
1175 }
1176
1177 static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1178         u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
1179 {
1180         u8 *sense_buf = (u8 *)sense_data;
1181         /* Sense data valid, err code 70h */
1182         sense_buf[0] = 0x70; /* No info field */
1183         sense_buf[1] = 0;       /* Segment number, always zero */
1184
1185         sense_buf[2] = sense_key;       /* Sense key */
1186
1187         sense_buf[12] = sense_code;     /* Additional sense code */
1188         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
1189
1190         if (sense_key == ILLEGAL_REQUEST) {
1191                 sense_buf[7] = 10;      /* Additional sense length */
1192
1193                 sense_buf[15] = bit_pointer;
1194                 /* Illegal parameter is in the parameter block */
1195                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
1196                         sense_buf[15] |= 0xc0;/* Std sense key specific field */
1197                 /* Illegal parameter is in the CDB block */
1198                 sense_buf[16] = field_pointer >> 8;     /* MSB */
1199                 sense_buf[17] = field_pointer;          /* LSB */
1200         } else
1201                 sense_buf[7] = 6;       /* Additional sense length */
1202 }
1203
1204 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1205 {
1206         if (lba & 0xffffffff00000000LL) {
1207                 int cid = scmd_id(cmd);
1208                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1209                 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1210                         SAM_STAT_CHECK_CONDITION;
1211                 set_sense(&dev->fsa_dev[cid].sense_data,
1212                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1213                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1214                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1215                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1216                              SCSI_SENSE_BUFFERSIZE));
1217                 cmd->scsi_done(cmd);
1218                 return 1;
1219         }
1220         return 0;
1221 }
1222
1223 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1224 {
1225         return 0;
1226 }
1227
1228 static void io_callback(void *context, struct fib * fibptr);
1229
1230 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1231 {
1232         struct aac_dev *dev = fib->dev;
1233         u16 fibsize, command;
1234         long ret;
1235
1236         aac_fib_init(fib);
1237         if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1238                 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1239                 !dev->sync_mode) {
1240                 struct aac_raw_io2 *readcmd2;
1241                 readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1242                 memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1243                 readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1244                 readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1245                 readcmd2->byteCount = cpu_to_le32(count *
1246                         dev->fsa_dev[scmd_id(cmd)].block_size);
1247                 readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1248                 readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
1249                 ret = aac_build_sgraw2(cmd, readcmd2,
1250                                 dev->scsi_host_ptr->sg_tablesize);
1251                 if (ret < 0)
1252                         return ret;
1253                 command = ContainerRawIo2;
1254                 fibsize = sizeof(struct aac_raw_io2) +
1255                         ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1256         } else {
1257                 struct aac_raw_io *readcmd;
1258                 readcmd = (struct aac_raw_io *) fib_data(fib);
1259                 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1260                 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1261                 readcmd->count = cpu_to_le32(count *
1262                         dev->fsa_dev[scmd_id(cmd)].block_size);
1263                 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1264                 readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1265                 readcmd->bpTotal = 0;
1266                 readcmd->bpComplete = 0;
1267                 ret = aac_build_sgraw(cmd, &readcmd->sg);
1268                 if (ret < 0)
1269                         return ret;
1270                 command = ContainerRawIo;
1271                 fibsize = sizeof(struct aac_raw_io) +
1272                         ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1273         }
1274
1275         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1276         /*
1277          *      Now send the Fib to the adapter
1278          */
1279         return aac_fib_send(command,
1280                           fib,
1281                           fibsize,
1282                           FsaNormal,
1283                           0, 1,
1284                           (fib_callback) io_callback,
1285                           (void *) cmd);
1286 }
1287
1288 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1289 {
1290         u16 fibsize;
1291         struct aac_read64 *readcmd;
1292         long ret;
1293
1294         aac_fib_init(fib);
1295         readcmd = (struct aac_read64 *) fib_data(fib);
1296         readcmd->command = cpu_to_le32(VM_CtHostRead64);
1297         readcmd->cid = cpu_to_le16(scmd_id(cmd));
1298         readcmd->sector_count = cpu_to_le16(count);
1299         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1300         readcmd->pad   = 0;
1301         readcmd->flags = 0;
1302
1303         ret = aac_build_sg64(cmd, &readcmd->sg);
1304         if (ret < 0)
1305                 return ret;
1306         fibsize = sizeof(struct aac_read64) +
1307                 ((le32_to_cpu(readcmd->sg.count) - 1) *
1308                  sizeof (struct sgentry64));
1309         BUG_ON (fibsize > (fib->dev->max_fib_size -
1310                                 sizeof(struct aac_fibhdr)));
1311         /*
1312          *      Now send the Fib to the adapter
1313          */
1314         return aac_fib_send(ContainerCommand64,
1315                           fib,
1316                           fibsize,
1317                           FsaNormal,
1318                           0, 1,
1319                           (fib_callback) io_callback,
1320                           (void *) cmd);
1321 }
1322
1323 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1324 {
1325         u16 fibsize;
1326         struct aac_read *readcmd;
1327         struct aac_dev *dev = fib->dev;
1328         long ret;
1329
1330         aac_fib_init(fib);
1331         readcmd = (struct aac_read *) fib_data(fib);
1332         readcmd->command = cpu_to_le32(VM_CtBlockRead);
1333         readcmd->cid = cpu_to_le32(scmd_id(cmd));
1334         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1335         readcmd->count = cpu_to_le32(count *
1336                 dev->fsa_dev[scmd_id(cmd)].block_size);
1337
1338         ret = aac_build_sg(cmd, &readcmd->sg);
1339         if (ret < 0)
1340                 return ret;
1341         fibsize = sizeof(struct aac_read) +
1342                         ((le32_to_cpu(readcmd->sg.count) - 1) *
1343                          sizeof (struct sgentry));
1344         BUG_ON (fibsize > (fib->dev->max_fib_size -
1345                                 sizeof(struct aac_fibhdr)));
1346         /*
1347          *      Now send the Fib to the adapter
1348          */
1349         return aac_fib_send(ContainerCommand,
1350                           fib,
1351                           fibsize,
1352                           FsaNormal,
1353                           0, 1,
1354                           (fib_callback) io_callback,
1355                           (void *) cmd);
1356 }
1357
1358 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1359 {
1360         struct aac_dev *dev = fib->dev;
1361         u16 fibsize, command;
1362         long ret;
1363
1364         aac_fib_init(fib);
1365         if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1366                 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1367                 !dev->sync_mode) {
1368                 struct aac_raw_io2 *writecmd2;
1369                 writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1370                 memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1371                 writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1372                 writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1373                 writecmd2->byteCount = cpu_to_le32(count *
1374                         dev->fsa_dev[scmd_id(cmd)].block_size);
1375                 writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1376                 writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1377                                                    (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1378                         cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1379                         cpu_to_le16(RIO2_IO_TYPE_WRITE);
1380                 ret = aac_build_sgraw2(cmd, writecmd2,
1381                                 dev->scsi_host_ptr->sg_tablesize);
1382                 if (ret < 0)
1383                         return ret;
1384                 command = ContainerRawIo2;
1385                 fibsize = sizeof(struct aac_raw_io2) +
1386                         ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1387         } else {
1388                 struct aac_raw_io *writecmd;
1389                 writecmd = (struct aac_raw_io *) fib_data(fib);
1390                 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1391                 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1392                 writecmd->count = cpu_to_le32(count *
1393                         dev->fsa_dev[scmd_id(cmd)].block_size);
1394                 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1395                 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1396                                                    (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1397                         cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1398                         cpu_to_le16(RIO_TYPE_WRITE);
1399                 writecmd->bpTotal = 0;
1400                 writecmd->bpComplete = 0;
1401                 ret = aac_build_sgraw(cmd, &writecmd->sg);
1402                 if (ret < 0)
1403                         return ret;
1404                 command = ContainerRawIo;
1405                 fibsize = sizeof(struct aac_raw_io) +
1406                         ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1407         }
1408
1409         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1410         /*
1411          *      Now send the Fib to the adapter
1412          */
1413         return aac_fib_send(command,
1414                           fib,
1415                           fibsize,
1416                           FsaNormal,
1417                           0, 1,
1418                           (fib_callback) io_callback,
1419                           (void *) cmd);
1420 }
1421
1422 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1423 {
1424         u16 fibsize;
1425         struct aac_write64 *writecmd;
1426         long ret;
1427
1428         aac_fib_init(fib);
1429         writecmd = (struct aac_write64 *) fib_data(fib);
1430         writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1431         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1432         writecmd->sector_count = cpu_to_le16(count);
1433         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1434         writecmd->pad   = 0;
1435         writecmd->flags = 0;
1436
1437         ret = aac_build_sg64(cmd, &writecmd->sg);
1438         if (ret < 0)
1439                 return ret;
1440         fibsize = sizeof(struct aac_write64) +
1441                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1442                  sizeof (struct sgentry64));
1443         BUG_ON (fibsize > (fib->dev->max_fib_size -
1444                                 sizeof(struct aac_fibhdr)));
1445         /*
1446          *      Now send the Fib to the adapter
1447          */
1448         return aac_fib_send(ContainerCommand64,
1449                           fib,
1450                           fibsize,
1451                           FsaNormal,
1452                           0, 1,
1453                           (fib_callback) io_callback,
1454                           (void *) cmd);
1455 }
1456
1457 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1458 {
1459         u16 fibsize;
1460         struct aac_write *writecmd;
1461         struct aac_dev *dev = fib->dev;
1462         long ret;
1463
1464         aac_fib_init(fib);
1465         writecmd = (struct aac_write *) fib_data(fib);
1466         writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1467         writecmd->cid = cpu_to_le32(scmd_id(cmd));
1468         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1469         writecmd->count = cpu_to_le32(count *
1470                 dev->fsa_dev[scmd_id(cmd)].block_size);
1471         writecmd->sg.count = cpu_to_le32(1);
1472         /* ->stable is not used - it did mean which type of write */
1473
1474         ret = aac_build_sg(cmd, &writecmd->sg);
1475         if (ret < 0)
1476                 return ret;
1477         fibsize = sizeof(struct aac_write) +
1478                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1479                  sizeof (struct sgentry));
1480         BUG_ON (fibsize > (fib->dev->max_fib_size -
1481                                 sizeof(struct aac_fibhdr)));
1482         /*
1483          *      Now send the Fib to the adapter
1484          */
1485         return aac_fib_send(ContainerCommand,
1486                           fib,
1487                           fibsize,
1488                           FsaNormal,
1489                           0, 1,
1490                           (fib_callback) io_callback,
1491                           (void *) cmd);
1492 }
1493
1494 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1495 {
1496         struct aac_srb * srbcmd;
1497         u32 flag;
1498         u32 timeout;
1499
1500         aac_fib_init(fib);
1501         switch(cmd->sc_data_direction){
1502         case DMA_TO_DEVICE:
1503                 flag = SRB_DataOut;
1504                 break;
1505         case DMA_BIDIRECTIONAL:
1506                 flag = SRB_DataIn | SRB_DataOut;
1507                 break;
1508         case DMA_FROM_DEVICE:
1509                 flag = SRB_DataIn;
1510                 break;
1511         case DMA_NONE:
1512         default:        /* shuts up some versions of gcc */
1513                 flag = SRB_NoDataXfer;
1514                 break;
1515         }
1516
1517         srbcmd = (struct aac_srb*) fib_data(fib);
1518         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1519         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1520         srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1521         srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1522         srbcmd->flags    = cpu_to_le32(flag);
1523         timeout = cmd->request->timeout/HZ;
1524         if (timeout == 0)
1525                 timeout = 1;
1526         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1527         srbcmd->retry_limit = 0; /* Obsolete parameter */
1528         srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1529         return srbcmd;
1530 }
1531
1532 static struct aac_hba_cmd_req *aac_construct_hbacmd(struct fib *fib,
1533                                                         struct scsi_cmnd *cmd)
1534 {
1535         struct aac_hba_cmd_req *hbacmd;
1536         struct aac_dev *dev;
1537         int bus, target;
1538         u64 address;
1539
1540         dev = (struct aac_dev *)cmd->device->host->hostdata;
1541
1542         hbacmd = (struct aac_hba_cmd_req *)fib->hw_fib_va;
1543         memset(hbacmd, 0, 96);  /* sizeof(*hbacmd) is not necessary */
1544         /* iu_type is a parameter of aac_hba_send */
1545         switch (cmd->sc_data_direction) {
1546         case DMA_TO_DEVICE:
1547                 hbacmd->byte1 = 2;
1548                 break;
1549         case DMA_FROM_DEVICE:
1550         case DMA_BIDIRECTIONAL:
1551                 hbacmd->byte1 = 1;
1552                 break;
1553         case DMA_NONE:
1554         default:
1555                 break;
1556         }
1557         hbacmd->lun[1] = cpu_to_le32(cmd->device->lun);
1558
1559         bus = aac_logical_to_phys(scmd_channel(cmd));
1560         target = scmd_id(cmd);
1561         hbacmd->it_nexus = dev->hba_map[bus][target].rmw_nexus;
1562
1563         /* we fill in reply_qid later in aac_src_deliver_message */
1564         /* we fill in iu_type, request_id later in aac_hba_send */
1565         /* we fill in emb_data_desc_count later in aac_build_sghba */
1566
1567         memcpy(hbacmd->cdb, cmd->cmnd, cmd->cmd_len);
1568         hbacmd->data_length = cpu_to_le32(scsi_bufflen(cmd));
1569
1570         address = (u64)fib->hw_error_pa;
1571         hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
1572         hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
1573         hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
1574
1575         return hbacmd;
1576 }
1577
1578 static void aac_srb_callback(void *context, struct fib * fibptr);
1579
1580 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1581 {
1582         u16 fibsize;
1583         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1584         long ret;
1585
1586         ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1587         if (ret < 0)
1588                 return ret;
1589         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1590
1591         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1592         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1593         /*
1594          *      Build Scatter/Gather list
1595          */
1596         fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1597                 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1598                  sizeof (struct sgentry64));
1599         BUG_ON (fibsize > (fib->dev->max_fib_size -
1600                                 sizeof(struct aac_fibhdr)));
1601
1602         /*
1603          *      Now send the Fib to the adapter
1604          */
1605         return aac_fib_send(ScsiPortCommand64, fib,
1606                                 fibsize, FsaNormal, 0, 1,
1607                                   (fib_callback) aac_srb_callback,
1608                                   (void *) cmd);
1609 }
1610
1611 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1612 {
1613         u16 fibsize;
1614         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1615         long ret;
1616
1617         ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1618         if (ret < 0)
1619                 return ret;
1620         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1621
1622         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1623         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1624         /*
1625          *      Build Scatter/Gather list
1626          */
1627         fibsize = sizeof (struct aac_srb) +
1628                 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1629                  sizeof (struct sgentry));
1630         BUG_ON (fibsize > (fib->dev->max_fib_size -
1631                                 sizeof(struct aac_fibhdr)));
1632
1633         /*
1634          *      Now send the Fib to the adapter
1635          */
1636         return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1637                                   (fib_callback) aac_srb_callback, (void *) cmd);
1638 }
1639
1640 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1641 {
1642         if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1643             (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1644                 return FAILED;
1645         return aac_scsi_32(fib, cmd);
1646 }
1647
1648 static int aac_adapter_hba(struct fib *fib, struct scsi_cmnd *cmd)
1649 {
1650         struct aac_hba_cmd_req *hbacmd = aac_construct_hbacmd(fib, cmd);
1651         struct aac_dev *dev;
1652         long ret;
1653
1654         dev = (struct aac_dev *)cmd->device->host->hostdata;
1655
1656         ret = aac_build_sghba(cmd, hbacmd,
1657                 dev->scsi_host_ptr->sg_tablesize, (u64)fib->hw_sgl_pa);
1658         if (ret < 0)
1659                 return ret;
1660
1661         /*
1662          *      Now send the HBA command to the adapter
1663          */
1664         fib->hbacmd_size = 64 + le32_to_cpu(hbacmd->emb_data_desc_count) *
1665                 sizeof(struct aac_hba_sgl);
1666
1667         return aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, fib,
1668                                   (fib_callback) aac_hba_callback,
1669                                   (void *) cmd);
1670 }
1671
1672 static int aac_send_safw_bmic_cmd(struct aac_dev *dev,
1673         struct aac_srb_unit *srbu, void *xfer_buf, int xfer_len)
1674 {
1675         struct fib      *fibptr;
1676         dma_addr_t      addr;
1677         int             rcode;
1678         int             fibsize;
1679         struct aac_srb  *srb;
1680         struct aac_srb_reply *srb_reply;
1681         struct sgmap64  *sg64;
1682         u32 vbus;
1683         u32 vid;
1684
1685         if (!dev->sa_firmware)
1686                 return 0;
1687
1688         /* allocate FIB */
1689         fibptr = aac_fib_alloc(dev);
1690         if (!fibptr)
1691                 return -ENOMEM;
1692
1693         aac_fib_init(fibptr);
1694         fibptr->hw_fib_va->header.XferState &=
1695                 ~cpu_to_le32(FastResponseCapable);
1696
1697         fibsize  = sizeof(struct aac_srb) - sizeof(struct sgentry) +
1698                                                 sizeof(struct sgentry64);
1699
1700         /* allocate DMA buffer for response */
1701         addr = dma_map_single(&dev->pdev->dev, xfer_buf, xfer_len,
1702                                                         DMA_BIDIRECTIONAL);
1703         if (dma_mapping_error(&dev->pdev->dev, addr)) {
1704                 rcode = -ENOMEM;
1705                 goto fib_error;
1706         }
1707
1708         srb = fib_data(fibptr);
1709         memcpy(srb, &srbu->srb, sizeof(struct aac_srb));
1710
1711         vbus = (u32)le16_to_cpu(
1712                         dev->supplement_adapter_info.virt_device_bus);
1713         vid  = (u32)le16_to_cpu(
1714                         dev->supplement_adapter_info.virt_device_target);
1715
1716         /* set the common request fields */
1717         srb->channel            = cpu_to_le32(vbus);
1718         srb->id                 = cpu_to_le32(vid);
1719         srb->lun                = 0;
1720         srb->function           = cpu_to_le32(SRBF_ExecuteScsi);
1721         srb->timeout            = 0;
1722         srb->retry_limit        = 0;
1723         srb->cdb_size           = cpu_to_le32(16);
1724         srb->count              = cpu_to_le32(xfer_len);
1725
1726         sg64 = (struct sgmap64 *)&srb->sg;
1727         sg64->count             = cpu_to_le32(1);
1728         sg64->sg[0].addr[1]     = cpu_to_le32(upper_32_bits(addr));
1729         sg64->sg[0].addr[0]     = cpu_to_le32(lower_32_bits(addr));
1730         sg64->sg[0].count       = cpu_to_le32(xfer_len);
1731
1732         /*
1733          * Copy the updated data for other dumping or other usage if needed
1734          */
1735         memcpy(&srbu->srb, srb, sizeof(struct aac_srb));
1736
1737         /* issue request to the controller */
1738         rcode = aac_fib_send(ScsiPortCommand64, fibptr, fibsize, FsaNormal,
1739                                         1, 1, NULL, NULL);
1740
1741         if (rcode == -ERESTARTSYS)
1742                 rcode = -ERESTART;
1743
1744         if (unlikely(rcode < 0))
1745                 goto bmic_error;
1746
1747         srb_reply = (struct aac_srb_reply *)fib_data(fibptr);
1748         memcpy(&srbu->srb_reply, srb_reply, sizeof(struct aac_srb_reply));
1749
1750 bmic_error:
1751         dma_unmap_single(&dev->pdev->dev, addr, xfer_len, DMA_BIDIRECTIONAL);
1752 fib_error:
1753         aac_fib_complete(fibptr);
1754         aac_fib_free(fibptr);
1755         return rcode;
1756 }
1757
1758 static void aac_set_safw_target_qd(struct aac_dev *dev, int bus, int target)
1759 {
1760
1761         struct aac_ciss_identify_pd *identify_resp;
1762
1763         if (dev->hba_map[bus][target].devtype != AAC_DEVTYPE_NATIVE_RAW)
1764                 return;
1765
1766         identify_resp = dev->hba_map[bus][target].safw_identify_resp;
1767         if (identify_resp == NULL) {
1768                 dev->hba_map[bus][target].qd_limit = 32;
1769                 return;
1770         }
1771
1772         if (identify_resp->current_queue_depth_limit <= 0 ||
1773                 identify_resp->current_queue_depth_limit > 255)
1774                 dev->hba_map[bus][target].qd_limit = 32;
1775         else
1776                 dev->hba_map[bus][target].qd_limit =
1777                         identify_resp->current_queue_depth_limit;
1778 }
1779
1780 static int aac_issue_safw_bmic_identify(struct aac_dev *dev,
1781         struct aac_ciss_identify_pd **identify_resp, u32 bus, u32 target)
1782 {
1783         int rcode = -ENOMEM;
1784         int datasize;
1785         struct aac_srb_unit srbu;
1786         struct aac_srb *srbcmd;
1787         struct aac_ciss_identify_pd *identify_reply;
1788
1789         datasize = sizeof(struct aac_ciss_identify_pd);
1790         identify_reply = kmalloc(datasize, GFP_KERNEL);
1791         if (!identify_reply)
1792                 goto out;
1793
1794         memset(&srbu, 0, sizeof(struct aac_srb_unit));
1795
1796         srbcmd = &srbu.srb;
1797         srbcmd->flags   = cpu_to_le32(SRB_DataIn);
1798         srbcmd->cdb[0]  = 0x26;
1799         srbcmd->cdb[2]  = (u8)((AAC_MAX_LUN + target) & 0x00FF);
1800         srbcmd->cdb[6]  = CISS_IDENTIFY_PHYSICAL_DEVICE;
1801
1802         rcode = aac_send_safw_bmic_cmd(dev, &srbu, identify_reply, datasize);
1803         if (unlikely(rcode < 0))
1804                 goto mem_free_all;
1805
1806         *identify_resp = identify_reply;
1807
1808 out:
1809         return rcode;
1810 mem_free_all:
1811         kfree(identify_reply);
1812         goto out;
1813 }
1814
1815 static inline void aac_free_safw_ciss_luns(struct aac_dev *dev)
1816 {
1817         kfree(dev->safw_phys_luns);
1818         dev->safw_phys_luns = NULL;
1819 }
1820
1821 /**
1822  *      aac_get_safw_ciss_luns()        Process topology change
1823  *      @dev:           aac_dev structure
1824  *      @rescan:        Indicates rescan
1825  *
1826  *      Execute a CISS REPORT PHYS LUNS and process the results into
1827  *      the current hba_map.
1828  */
1829 static int aac_get_safw_ciss_luns(struct aac_dev *dev, int rescan)
1830 {
1831         int rcode = -ENOMEM;
1832         int datasize;
1833         struct aac_srb *srbcmd;
1834         struct aac_srb_unit srbu;
1835         struct aac_ciss_phys_luns_resp *phys_luns;
1836
1837         datasize = sizeof(struct aac_ciss_phys_luns_resp) +
1838                 (AAC_MAX_TARGETS - 1) * sizeof(struct _ciss_lun);
1839         phys_luns = kmalloc(datasize, GFP_KERNEL);
1840         if (phys_luns == NULL)
1841                 goto out;
1842
1843         memset(&srbu, 0, sizeof(struct aac_srb_unit));
1844
1845         srbcmd = &srbu.srb;
1846         srbcmd->flags   = cpu_to_le32(SRB_DataIn);
1847         srbcmd->cdb[0]  = CISS_REPORT_PHYSICAL_LUNS;
1848         srbcmd->cdb[1]  = 2; /* extended reporting */
1849         srbcmd->cdb[8]  = (u8)(datasize >> 8);
1850         srbcmd->cdb[9]  = (u8)(datasize);
1851
1852         rcode = aac_send_safw_bmic_cmd(dev, &srbu, phys_luns, datasize);
1853         if (unlikely(rcode < 0))
1854                 goto mem_free_all;
1855
1856         if (phys_luns->resp_flag != 2) {
1857                 rcode = -ENOMSG;
1858                 goto mem_free_all;
1859         }
1860
1861         dev->safw_phys_luns = phys_luns;
1862
1863 out:
1864         return rcode;
1865 mem_free_all:
1866         kfree(phys_luns);
1867         goto out;
1868 }
1869
1870 static inline u32 aac_get_safw_phys_lun_count(struct aac_dev *dev)
1871 {
1872         return get_unaligned_be32(&dev->safw_phys_luns->list_length[0]);
1873 }
1874
1875 static inline u32 aac_get_safw_phys_bus(struct aac_dev *dev, int lun)
1876 {
1877         return dev->safw_phys_luns->lun[lun].level2[1] & 0x3f;
1878 }
1879
1880 static inline u32 aac_get_safw_phys_target(struct aac_dev *dev, int lun)
1881 {
1882         return dev->safw_phys_luns->lun[lun].level2[0];
1883 }
1884
1885 static inline u32 aac_get_safw_phys_expose_flag(struct aac_dev *dev, int lun)
1886 {
1887         return dev->safw_phys_luns->lun[lun].bus >> 6;
1888 }
1889
1890 static inline u32 aac_get_safw_phys_attribs(struct aac_dev *dev, int lun)
1891 {
1892         return dev->safw_phys_luns->lun[lun].node_ident[9];
1893 }
1894
1895 static inline u32 aac_get_safw_phys_nexus(struct aac_dev *dev, int lun)
1896 {
1897         return *((u32 *)&dev->safw_phys_luns->lun[lun].node_ident[12]);
1898 }
1899
1900 static inline u32 aac_get_safw_phys_device_type(struct aac_dev *dev, int lun)
1901 {
1902         return dev->safw_phys_luns->lun[lun].node_ident[8];
1903 }
1904
1905 static inline void aac_free_safw_identify_resp(struct aac_dev *dev,
1906                                                 int bus, int target)
1907 {
1908         kfree(dev->hba_map[bus][target].safw_identify_resp);
1909         dev->hba_map[bus][target].safw_identify_resp = NULL;
1910 }
1911
1912 static inline void aac_free_safw_all_identify_resp(struct aac_dev *dev,
1913         int lun_count)
1914 {
1915         int luns;
1916         int i;
1917         u32 bus;
1918         u32 target;
1919
1920         luns = aac_get_safw_phys_lun_count(dev);
1921
1922         if (luns < lun_count)
1923                 lun_count = luns;
1924         else if (lun_count < 0)
1925                 lun_count = luns;
1926
1927         for (i = 0; i < lun_count; i++) {
1928                 bus = aac_get_safw_phys_bus(dev, i);
1929                 target = aac_get_safw_phys_target(dev, i);
1930
1931                 aac_free_safw_identify_resp(dev, bus, target);
1932         }
1933 }
1934
1935 static int aac_get_safw_attr_all_targets(struct aac_dev *dev, int rescan)
1936 {
1937         int i;
1938         int rcode = 0;
1939         u32 lun_count;
1940         u32 bus;
1941         u32 target;
1942         struct aac_ciss_identify_pd *identify_resp = NULL;
1943
1944         lun_count = aac_get_safw_phys_lun_count(dev);
1945
1946         for (i = 0; i < lun_count; ++i) {
1947
1948                 bus = aac_get_safw_phys_bus(dev, i);
1949                 target = aac_get_safw_phys_target(dev, i);
1950
1951                 rcode = aac_issue_safw_bmic_identify(dev,
1952                                                 &identify_resp, bus, target);
1953
1954                 if (unlikely(rcode < 0))
1955                         goto free_identify_resp;
1956
1957                 dev->hba_map[bus][target].safw_identify_resp = identify_resp;
1958         }
1959
1960 out:
1961         return rcode;
1962 free_identify_resp:
1963         aac_free_safw_all_identify_resp(dev, i);
1964         goto out;
1965 }
1966
1967 /**
1968  *      aac_set_safw_attr_all_targets-  update current hba map with data from FW
1969  *      @dev:   aac_dev structure
1970  *      @phys_luns: FW information from report phys luns
1971  *      @rescan: Indicates scan type
1972  *
1973  *      Update our hba map with the information gathered from the FW
1974  */
1975 static void aac_set_safw_attr_all_targets(struct aac_dev *dev, int rescan)
1976 {
1977         /* ok and extended reporting */
1978         u32 lun_count, nexus;
1979         u32 i, bus, target;
1980         u8 expose_flag, attribs;
1981         u8 devtype;
1982
1983         lun_count = aac_get_safw_phys_lun_count(dev);
1984
1985         for (i = 0; i < lun_count; ++i) {
1986
1987                 bus = aac_get_safw_phys_bus(dev, i);
1988                 target = aac_get_safw_phys_target(dev, i);
1989                 expose_flag = aac_get_safw_phys_expose_flag(dev, i);
1990                 attribs = aac_get_safw_phys_attribs(dev, i);
1991                 nexus = aac_get_safw_phys_nexus(dev, i);
1992
1993                 if (bus >= AAC_MAX_BUSES || target >= AAC_MAX_TARGETS)
1994                         continue;
1995
1996                 dev->hba_map[bus][target].expose = expose_flag;
1997
1998                 if (expose_flag != 0) {
1999                         devtype = AAC_DEVTYPE_RAID_MEMBER;
2000                         goto update_devtype;
2001                 }
2002
2003                 if (nexus != 0 && (attribs & 8)) {
2004                         devtype = AAC_DEVTYPE_NATIVE_RAW;
2005                         dev->hba_map[bus][target].rmw_nexus =
2006                                         nexus;
2007                 } else
2008                         devtype = AAC_DEVTYPE_ARC_RAW;
2009
2010                 aac_set_safw_target_qd(dev, bus, target);
2011
2012 update_devtype:
2013                 if (rescan == AAC_INIT)
2014                         dev->hba_map[bus][target].devtype = devtype;
2015                 else
2016                         dev->hba_map[bus][target].new_devtype = devtype;
2017         }
2018 }
2019
2020 static int aac_setup_safw_targets(struct aac_dev *dev, int rescan)
2021 {
2022         int rcode = 0;
2023
2024         rcode = aac_get_safw_ciss_luns(dev, rescan);
2025         if (unlikely(rcode < 0))
2026                 goto out;
2027
2028         rcode = aac_get_safw_attr_all_targets(dev, rescan);
2029         if (unlikely(rcode < 0))
2030                 goto free_ciss_luns;
2031
2032         aac_set_safw_attr_all_targets(dev, rescan);
2033
2034         aac_free_safw_all_identify_resp(dev, -1);
2035 free_ciss_luns:
2036         aac_free_safw_ciss_luns(dev);
2037 out:
2038         return rcode;
2039 }
2040
2041 int aac_setup_safw_adapter(struct aac_dev *dev, int rescan)
2042 {
2043         return aac_setup_safw_targets(dev, rescan);
2044 }
2045
2046 int aac_get_adapter_info(struct aac_dev* dev)
2047 {
2048         struct fib* fibptr;
2049         int rcode;
2050         u32 tmp, bus, target;
2051         struct aac_adapter_info *info;
2052         struct aac_bus_info *command;
2053         struct aac_bus_info_response *bus_info;
2054
2055         if (!(fibptr = aac_fib_alloc(dev)))
2056                 return -ENOMEM;
2057
2058         aac_fib_init(fibptr);
2059         info = (struct aac_adapter_info *) fib_data(fibptr);
2060         memset(info,0,sizeof(*info));
2061
2062         rcode = aac_fib_send(RequestAdapterInfo,
2063                          fibptr,
2064                          sizeof(*info),
2065                          FsaNormal,
2066                          -1, 1, /* First `interrupt' command uses special wait */
2067                          NULL,
2068                          NULL);
2069
2070         if (rcode < 0) {
2071                 /* FIB should be freed only after
2072                  * getting the response from the F/W */
2073                 if (rcode != -ERESTARTSYS) {
2074                         aac_fib_complete(fibptr);
2075                         aac_fib_free(fibptr);
2076                 }
2077                 return rcode;
2078         }
2079         memcpy(&dev->adapter_info, info, sizeof(*info));
2080
2081         dev->supplement_adapter_info.virt_device_bus = 0xffff;
2082         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
2083                 struct aac_supplement_adapter_info * sinfo;
2084
2085                 aac_fib_init(fibptr);
2086
2087                 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
2088
2089                 memset(sinfo,0,sizeof(*sinfo));
2090
2091                 rcode = aac_fib_send(RequestSupplementAdapterInfo,
2092                                  fibptr,
2093                                  sizeof(*sinfo),
2094                                  FsaNormal,
2095                                  1, 1,
2096                                  NULL,
2097                                  NULL);
2098
2099                 if (rcode >= 0)
2100                         memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
2101                 if (rcode == -ERESTARTSYS) {
2102                         fibptr = aac_fib_alloc(dev);
2103                         if (!fibptr)
2104                                 return -ENOMEM;
2105                 }
2106
2107         }
2108
2109         /* reset all previous mapped devices (i.e. for init. after IOP_RESET) */
2110         for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
2111                 for (target = 0; target < AAC_MAX_TARGETS; target++) {
2112                         dev->hba_map[bus][target].devtype = 0;
2113                         dev->hba_map[bus][target].qd_limit = 0;
2114                 }
2115         }
2116
2117         /*
2118          * GetBusInfo
2119          */
2120
2121         aac_fib_init(fibptr);
2122
2123         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
2124
2125         memset(bus_info, 0, sizeof(*bus_info));
2126
2127         command = (struct aac_bus_info *)bus_info;
2128
2129         command->Command = cpu_to_le32(VM_Ioctl);
2130         command->ObjType = cpu_to_le32(FT_DRIVE);
2131         command->MethodId = cpu_to_le32(1);
2132         command->CtlCmd = cpu_to_le32(GetBusInfo);
2133
2134         rcode = aac_fib_send(ContainerCommand,
2135                          fibptr,
2136                          sizeof (*bus_info),
2137                          FsaNormal,
2138                          1, 1,
2139                          NULL, NULL);
2140
2141         /* reasoned default */
2142         dev->maximum_num_physicals = 16;
2143         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
2144                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
2145                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
2146         }
2147
2148         if (!dev->sync_mode && dev->sa_firmware &&
2149                 dev->supplement_adapter_info.virt_device_bus != 0xffff)
2150                 rcode = aac_setup_safw_adapter(dev, AAC_INIT);
2151
2152         if (!dev->in_reset) {
2153                 char buffer[16];
2154                 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
2155                 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
2156                         dev->name,
2157                         dev->id,
2158                         tmp>>24,
2159                         (tmp>>16)&0xff,
2160                         tmp&0xff,
2161                         le32_to_cpu(dev->adapter_info.kernelbuild),
2162                         (int)sizeof(dev->supplement_adapter_info.build_date),
2163                         dev->supplement_adapter_info.build_date);
2164                 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
2165                 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
2166                         dev->name, dev->id,
2167                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2168                         le32_to_cpu(dev->adapter_info.monitorbuild));
2169                 tmp = le32_to_cpu(dev->adapter_info.biosrev);
2170                 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
2171                         dev->name, dev->id,
2172                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2173                         le32_to_cpu(dev->adapter_info.biosbuild));
2174                 buffer[0] = '\0';
2175                 if (aac_get_serial_number(
2176                   shost_to_class(dev->scsi_host_ptr), buffer))
2177                         printk(KERN_INFO "%s%d: serial %s",
2178                           dev->name, dev->id, buffer);
2179                 if (dev->supplement_adapter_info.vpd_info.tsid[0]) {
2180                         printk(KERN_INFO "%s%d: TSID %.*s\n",
2181                           dev->name, dev->id,
2182                           (int)sizeof(dev->supplement_adapter_info
2183                                                         .vpd_info.tsid),
2184                                 dev->supplement_adapter_info.vpd_info.tsid);
2185                 }
2186                 if (!aac_check_reset || ((aac_check_reset == 1) &&
2187                   (dev->supplement_adapter_info.supported_options2 &
2188                   AAC_OPTION_IGNORE_RESET))) {
2189                         printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
2190                           dev->name, dev->id);
2191                 }
2192         }
2193
2194         dev->cache_protected = 0;
2195         dev->jbod = ((dev->supplement_adapter_info.feature_bits &
2196                 AAC_FEATURE_JBOD) != 0);
2197         dev->nondasd_support = 0;
2198         dev->raid_scsi_mode = 0;
2199         if(dev->adapter_info.options & AAC_OPT_NONDASD)
2200                 dev->nondasd_support = 1;
2201
2202         /*
2203          * If the firmware supports ROMB RAID/SCSI mode and we are currently
2204          * in RAID/SCSI mode, set the flag. For now if in this mode we will
2205          * force nondasd support on. If we decide to allow the non-dasd flag
2206          * additional changes changes will have to be made to support
2207          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
2208          * changed to support the new dev->raid_scsi_mode flag instead of
2209          * leaching off of the dev->nondasd_support flag. Also in linit.c the
2210          * function aac_detect will have to be modified where it sets up the
2211          * max number of channels based on the aac->nondasd_support flag only.
2212          */
2213         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
2214             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
2215                 dev->nondasd_support = 1;
2216                 dev->raid_scsi_mode = 1;
2217         }
2218         if (dev->raid_scsi_mode != 0)
2219                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
2220                                 dev->name, dev->id);
2221
2222         if (nondasd != -1)
2223                 dev->nondasd_support = (nondasd!=0);
2224         if (dev->nondasd_support && !dev->in_reset)
2225                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
2226
2227         if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
2228                 dev->needs_dac = 1;
2229         dev->dac_support = 0;
2230         if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
2231             (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
2232                 if (!dev->in_reset)
2233                         printk(KERN_INFO "%s%d: 64bit support enabled.\n",
2234                                 dev->name, dev->id);
2235                 dev->dac_support = 1;
2236         }
2237
2238         if(dacmode != -1) {
2239                 dev->dac_support = (dacmode!=0);
2240         }
2241
2242         /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
2243         if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
2244                 & AAC_QUIRK_SCSI_32)) {
2245                 dev->nondasd_support = 0;
2246                 dev->jbod = 0;
2247                 expose_physicals = 0;
2248         }
2249
2250         if (dev->dac_support) {
2251                 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
2252                         if (!dev->in_reset)
2253                                 dev_info(&dev->pdev->dev, "64 Bit DAC enabled\n");
2254                 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
2255                         dev_info(&dev->pdev->dev, "DMA mask set failed, 64 Bit DAC disabled\n");
2256                         dev->dac_support = 0;
2257                 } else {
2258                         dev_info(&dev->pdev->dev, "No suitable DMA available\n");
2259                         rcode = -ENOMEM;
2260                 }
2261         }
2262         /*
2263          * Deal with configuring for the individualized limits of each packet
2264          * interface.
2265          */
2266         dev->a_ops.adapter_scsi = (dev->dac_support)
2267           ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
2268                                 ? aac_scsi_32_64
2269                                 : aac_scsi_64)
2270                                 : aac_scsi_32;
2271         if (dev->raw_io_interface) {
2272                 dev->a_ops.adapter_bounds = (dev->raw_io_64)
2273                                         ? aac_bounds_64
2274                                         : aac_bounds_32;
2275                 dev->a_ops.adapter_read = aac_read_raw_io;
2276                 dev->a_ops.adapter_write = aac_write_raw_io;
2277         } else {
2278                 dev->a_ops.adapter_bounds = aac_bounds_32;
2279                 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
2280                         sizeof(struct aac_fibhdr) -
2281                         sizeof(struct aac_write) + sizeof(struct sgentry)) /
2282                                 sizeof(struct sgentry);
2283                 if (dev->dac_support) {
2284                         dev->a_ops.adapter_read = aac_read_block64;
2285                         dev->a_ops.adapter_write = aac_write_block64;
2286                         /*
2287                          * 38 scatter gather elements
2288                          */
2289                         dev->scsi_host_ptr->sg_tablesize =
2290                                 (dev->max_fib_size -
2291                                 sizeof(struct aac_fibhdr) -
2292                                 sizeof(struct aac_write64) +
2293                                 sizeof(struct sgentry64)) /
2294                                         sizeof(struct sgentry64);
2295                 } else {
2296                         dev->a_ops.adapter_read = aac_read_block;
2297                         dev->a_ops.adapter_write = aac_write_block;
2298                 }
2299                 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
2300                 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
2301                         /*
2302                          * Worst case size that could cause sg overflow when
2303                          * we break up SG elements that are larger than 64KB.
2304                          * Would be nice if we could tell the SCSI layer what
2305                          * the maximum SG element size can be. Worst case is
2306                          * (sg_tablesize-1) 4KB elements with one 64KB
2307                          * element.
2308                          *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
2309                          */
2310                         dev->scsi_host_ptr->max_sectors =
2311                           (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
2312                 }
2313         }
2314         if (!dev->sync_mode && dev->sa_firmware &&
2315                 dev->scsi_host_ptr->sg_tablesize > HBA_MAX_SG_SEPARATE)
2316                 dev->scsi_host_ptr->sg_tablesize = dev->sg_tablesize =
2317                         HBA_MAX_SG_SEPARATE;
2318
2319         /* FIB should be freed only after getting the response from the F/W */
2320         if (rcode != -ERESTARTSYS) {
2321                 aac_fib_complete(fibptr);
2322                 aac_fib_free(fibptr);
2323         }
2324
2325         return rcode;
2326 }
2327
2328
2329 static void io_callback(void *context, struct fib * fibptr)
2330 {
2331         struct aac_dev *dev;
2332         struct aac_read_reply *readreply;
2333         struct scsi_cmnd *scsicmd;
2334         u32 cid;
2335
2336         scsicmd = (struct scsi_cmnd *) context;
2337
2338         if (!aac_valid_context(scsicmd, fibptr))
2339                 return;
2340
2341         dev = fibptr->dev;
2342         cid = scmd_id(scsicmd);
2343
2344         if (nblank(dprintk(x))) {
2345                 u64 lba;
2346                 switch (scsicmd->cmnd[0]) {
2347                 case WRITE_6:
2348                 case READ_6:
2349                         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2350                             (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2351                         break;
2352                 case WRITE_16:
2353                 case READ_16:
2354                         lba = ((u64)scsicmd->cmnd[2] << 56) |
2355                               ((u64)scsicmd->cmnd[3] << 48) |
2356                               ((u64)scsicmd->cmnd[4] << 40) |
2357                               ((u64)scsicmd->cmnd[5] << 32) |
2358                               ((u64)scsicmd->cmnd[6] << 24) |
2359                               (scsicmd->cmnd[7] << 16) |
2360                               (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2361                         break;
2362                 case WRITE_12:
2363                 case READ_12:
2364                         lba = ((u64)scsicmd->cmnd[2] << 24) |
2365                               (scsicmd->cmnd[3] << 16) |
2366                               (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2367                         break;
2368                 default:
2369                         lba = ((u64)scsicmd->cmnd[2] << 24) |
2370                                (scsicmd->cmnd[3] << 16) |
2371                                (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2372                         break;
2373                 }
2374                 printk(KERN_DEBUG
2375                   "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
2376                   smp_processor_id(), (unsigned long long)lba, jiffies);
2377         }
2378
2379         BUG_ON(fibptr == NULL);
2380
2381         scsi_dma_unmap(scsicmd);
2382
2383         readreply = (struct aac_read_reply *)fib_data(fibptr);
2384         switch (le32_to_cpu(readreply->status)) {
2385         case ST_OK:
2386                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2387                         SAM_STAT_GOOD;
2388                 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
2389                 break;
2390         case ST_NOT_READY:
2391                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2392                         SAM_STAT_CHECK_CONDITION;
2393                 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
2394                   SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
2395                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2396                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2397                              SCSI_SENSE_BUFFERSIZE));
2398                 break;
2399         case ST_MEDERR:
2400                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2401                         SAM_STAT_CHECK_CONDITION;
2402                 set_sense(&dev->fsa_dev[cid].sense_data, MEDIUM_ERROR,
2403                   SENCODE_UNRECOVERED_READ_ERROR, ASENCODE_NO_SENSE, 0, 0);
2404                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2405                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2406                              SCSI_SENSE_BUFFERSIZE));
2407                 break;
2408         default:
2409 #ifdef AAC_DETAILED_STATUS_INFO
2410                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
2411                   le32_to_cpu(readreply->status));
2412 #endif
2413                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2414                         SAM_STAT_CHECK_CONDITION;
2415                 set_sense(&dev->fsa_dev[cid].sense_data,
2416                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2417                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2418                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2419                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2420                              SCSI_SENSE_BUFFERSIZE));
2421                 break;
2422         }
2423         aac_fib_complete(fibptr);
2424
2425         scsicmd->scsi_done(scsicmd);
2426 }
2427
2428 static int aac_read(struct scsi_cmnd * scsicmd)
2429 {
2430         u64 lba;
2431         u32 count;
2432         int status;
2433         struct aac_dev *dev;
2434         struct fib * cmd_fibcontext;
2435         int cid;
2436
2437         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2438         /*
2439          *      Get block address and transfer length
2440          */
2441         switch (scsicmd->cmnd[0]) {
2442         case READ_6:
2443                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
2444
2445                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2446                         (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2447                 count = scsicmd->cmnd[4];
2448
2449                 if (count == 0)
2450                         count = 256;
2451                 break;
2452         case READ_16:
2453                 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
2454
2455                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
2456                         ((u64)scsicmd->cmnd[3] << 48) |
2457                         ((u64)scsicmd->cmnd[4] << 40) |
2458                         ((u64)scsicmd->cmnd[5] << 32) |
2459                         ((u64)scsicmd->cmnd[6] << 24) |
2460                         (scsicmd->cmnd[7] << 16) |
2461                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2462                 count = (scsicmd->cmnd[10] << 24) |
2463                         (scsicmd->cmnd[11] << 16) |
2464                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2465                 break;
2466         case READ_12:
2467                 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
2468
2469                 lba = ((u64)scsicmd->cmnd[2] << 24) |
2470                         (scsicmd->cmnd[3] << 16) |
2471                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2472                 count = (scsicmd->cmnd[6] << 24) |
2473                         (scsicmd->cmnd[7] << 16) |
2474                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2475                 break;
2476         default:
2477                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
2478
2479                 lba = ((u64)scsicmd->cmnd[2] << 24) |
2480                         (scsicmd->cmnd[3] << 16) |
2481                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2482                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2483                 break;
2484         }
2485
2486         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2487                 cid = scmd_id(scsicmd);
2488                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2489                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2490                         SAM_STAT_CHECK_CONDITION;
2491                 set_sense(&dev->fsa_dev[cid].sense_data,
2492                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2493                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2494                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2495                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2496                              SCSI_SENSE_BUFFERSIZE));
2497                 scsicmd->scsi_done(scsicmd);
2498                 return 1;
2499         }
2500
2501         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
2502           smp_processor_id(), (unsigned long long)lba, jiffies));
2503         if (aac_adapter_bounds(dev,scsicmd,lba))
2504                 return 0;
2505         /*
2506          *      Alocate and initialize a Fib
2507          */
2508         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2509         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2510         status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
2511
2512         /*
2513          *      Check that the command queued to the controller
2514          */
2515         if (status == -EINPROGRESS)
2516                 return 0;
2517
2518         printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
2519         /*
2520          *      For some reason, the Fib didn't queue, return QUEUE_FULL
2521          */
2522         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2523         scsicmd->scsi_done(scsicmd);
2524         aac_fib_complete(cmd_fibcontext);
2525         aac_fib_free(cmd_fibcontext);
2526         return 0;
2527 }
2528
2529 static int aac_write(struct scsi_cmnd * scsicmd)
2530 {
2531         u64 lba;
2532         u32 count;
2533         int fua;
2534         int status;
2535         struct aac_dev *dev;
2536         struct fib * cmd_fibcontext;
2537         int cid;
2538
2539         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2540         /*
2541          *      Get block address and transfer length
2542          */
2543         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
2544         {
2545                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2546                 count = scsicmd->cmnd[4];
2547                 if (count == 0)
2548                         count = 256;
2549                 fua = 0;
2550         } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
2551                 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
2552
2553                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
2554                         ((u64)scsicmd->cmnd[3] << 48) |
2555                         ((u64)scsicmd->cmnd[4] << 40) |
2556                         ((u64)scsicmd->cmnd[5] << 32) |
2557                         ((u64)scsicmd->cmnd[6] << 24) |
2558                         (scsicmd->cmnd[7] << 16) |
2559                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2560                 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2561                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2562                 fua = scsicmd->cmnd[1] & 0x8;
2563         } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
2564                 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
2565
2566                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2567                     | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2568                 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2569                       | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2570                 fua = scsicmd->cmnd[1] & 0x8;
2571         } else {
2572                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
2573                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2574                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2575                 fua = scsicmd->cmnd[1] & 0x8;
2576         }
2577
2578         if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2579                 cid = scmd_id(scsicmd);
2580                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2581                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2582                         SAM_STAT_CHECK_CONDITION;
2583                 set_sense(&dev->fsa_dev[cid].sense_data,
2584                           HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2585                           ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2586                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2587                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2588                              SCSI_SENSE_BUFFERSIZE));
2589                 scsicmd->scsi_done(scsicmd);
2590                 return 1;
2591         }
2592
2593         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
2594           smp_processor_id(), (unsigned long long)lba, jiffies));
2595         if (aac_adapter_bounds(dev,scsicmd,lba))
2596                 return 0;
2597         /*
2598          *      Allocate and initialize a Fib then setup a BlockWrite command
2599          */
2600         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2601         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2602         status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
2603
2604         /*
2605          *      Check that the command queued to the controller
2606          */
2607         if (status == -EINPROGRESS)
2608                 return 0;
2609
2610         printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
2611         /*
2612          *      For some reason, the Fib didn't queue, return QUEUE_FULL
2613          */
2614         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2615         scsicmd->scsi_done(scsicmd);
2616
2617         aac_fib_complete(cmd_fibcontext);
2618         aac_fib_free(cmd_fibcontext);
2619         return 0;
2620 }
2621
2622 static void synchronize_callback(void *context, struct fib *fibptr)
2623 {
2624         struct aac_synchronize_reply *synchronizereply;
2625         struct scsi_cmnd *cmd;
2626
2627         cmd = context;
2628
2629         if (!aac_valid_context(cmd, fibptr))
2630                 return;
2631
2632         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
2633                                 smp_processor_id(), jiffies));
2634         BUG_ON(fibptr == NULL);
2635
2636
2637         synchronizereply = fib_data(fibptr);
2638         if (le32_to_cpu(synchronizereply->status) == CT_OK)
2639                 cmd->result = DID_OK << 16 |
2640                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2641         else {
2642                 struct scsi_device *sdev = cmd->device;
2643                 struct aac_dev *dev = fibptr->dev;
2644                 u32 cid = sdev_id(sdev);
2645                 printk(KERN_WARNING
2646                      "synchronize_callback: synchronize failed, status = %d\n",
2647                      le32_to_cpu(synchronizereply->status));
2648                 cmd->result = DID_OK << 16 |
2649                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2650                 set_sense(&dev->fsa_dev[cid].sense_data,
2651                   HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2652                   ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2653                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2654                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2655                              SCSI_SENSE_BUFFERSIZE));
2656         }
2657
2658         aac_fib_complete(fibptr);
2659         aac_fib_free(fibptr);
2660         cmd->scsi_done(cmd);
2661 }
2662
2663 static int aac_synchronize(struct scsi_cmnd *scsicmd)
2664 {
2665         int status;
2666         struct fib *cmd_fibcontext;
2667         struct aac_synchronize *synchronizecmd;
2668         struct scsi_cmnd *cmd;
2669         struct scsi_device *sdev = scsicmd->device;
2670         int active = 0;
2671         struct aac_dev *aac;
2672         u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2673                 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2674         u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2675         unsigned long flags;
2676
2677         /*
2678          * Wait for all outstanding queued commands to complete to this
2679          * specific target (block).
2680          */
2681         spin_lock_irqsave(&sdev->list_lock, flags);
2682         list_for_each_entry(cmd, &sdev->cmd_list, list)
2683                 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2684                         u64 cmnd_lba;
2685                         u32 cmnd_count;
2686
2687                         if (cmd->cmnd[0] == WRITE_6) {
2688                                 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2689                                         (cmd->cmnd[2] << 8) |
2690                                         cmd->cmnd[3];
2691                                 cmnd_count = cmd->cmnd[4];
2692                                 if (cmnd_count == 0)
2693                                         cmnd_count = 256;
2694                         } else if (cmd->cmnd[0] == WRITE_16) {
2695                                 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2696                                         ((u64)cmd->cmnd[3] << 48) |
2697                                         ((u64)cmd->cmnd[4] << 40) |
2698                                         ((u64)cmd->cmnd[5] << 32) |
2699                                         ((u64)cmd->cmnd[6] << 24) |
2700                                         (cmd->cmnd[7] << 16) |
2701                                         (cmd->cmnd[8] << 8) |
2702                                         cmd->cmnd[9];
2703                                 cmnd_count = (cmd->cmnd[10] << 24) |
2704                                         (cmd->cmnd[11] << 16) |
2705                                         (cmd->cmnd[12] << 8) |
2706                                         cmd->cmnd[13];
2707                         } else if (cmd->cmnd[0] == WRITE_12) {
2708                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2709                                         (cmd->cmnd[3] << 16) |
2710                                         (cmd->cmnd[4] << 8) |
2711                                         cmd->cmnd[5];
2712                                 cmnd_count = (cmd->cmnd[6] << 24) |
2713                                         (cmd->cmnd[7] << 16) |
2714                                         (cmd->cmnd[8] << 8) |
2715                                         cmd->cmnd[9];
2716                         } else if (cmd->cmnd[0] == WRITE_10) {
2717                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2718                                         (cmd->cmnd[3] << 16) |
2719                                         (cmd->cmnd[4] << 8) |
2720                                         cmd->cmnd[5];
2721                                 cmnd_count = (cmd->cmnd[7] << 8) |
2722                                         cmd->cmnd[8];
2723                         } else
2724                                 continue;
2725                         if (((cmnd_lba + cmnd_count) < lba) ||
2726                           (count && ((lba + count) < cmnd_lba)))
2727                                 continue;
2728                         ++active;
2729                         break;
2730                 }
2731
2732         spin_unlock_irqrestore(&sdev->list_lock, flags);
2733
2734         /*
2735          *      Yield the processor (requeue for later)
2736          */
2737         if (active)
2738                 return SCSI_MLQUEUE_DEVICE_BUSY;
2739
2740         aac = (struct aac_dev *)sdev->host->hostdata;
2741         if (aac->in_reset)
2742                 return SCSI_MLQUEUE_HOST_BUSY;
2743
2744         /*
2745          *      Allocate and initialize a Fib
2746          */
2747         if (!(cmd_fibcontext = aac_fib_alloc(aac)))
2748                 return SCSI_MLQUEUE_HOST_BUSY;
2749
2750         aac_fib_init(cmd_fibcontext);
2751
2752         synchronizecmd = fib_data(cmd_fibcontext);
2753         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2754         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
2755         synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
2756         synchronizecmd->count =
2757              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2758         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2759
2760         /*
2761          *      Now send the Fib to the adapter
2762          */
2763         status = aac_fib_send(ContainerCommand,
2764                   cmd_fibcontext,
2765                   sizeof(struct aac_synchronize),
2766                   FsaNormal,
2767                   0, 1,
2768                   (fib_callback)synchronize_callback,
2769                   (void *)scsicmd);
2770
2771         /*
2772          *      Check that the command queued to the controller
2773          */
2774         if (status == -EINPROGRESS)
2775                 return 0;
2776
2777         printk(KERN_WARNING
2778                 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2779         aac_fib_complete(cmd_fibcontext);
2780         aac_fib_free(cmd_fibcontext);
2781         return SCSI_MLQUEUE_HOST_BUSY;
2782 }
2783
2784 static void aac_start_stop_callback(void *context, struct fib *fibptr)
2785 {
2786         struct scsi_cmnd *scsicmd = context;
2787
2788         if (!aac_valid_context(scsicmd, fibptr))
2789                 return;
2790
2791         BUG_ON(fibptr == NULL);
2792
2793         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2794
2795         aac_fib_complete(fibptr);
2796         aac_fib_free(fibptr);
2797         scsicmd->scsi_done(scsicmd);
2798 }
2799
2800 static int aac_start_stop(struct scsi_cmnd *scsicmd)
2801 {
2802         int status;
2803         struct fib *cmd_fibcontext;
2804         struct aac_power_management *pmcmd;
2805         struct scsi_device *sdev = scsicmd->device;
2806         struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2807
2808         if (!(aac->supplement_adapter_info.supported_options2 &
2809               AAC_OPTION_POWER_MANAGEMENT)) {
2810                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2811                                   SAM_STAT_GOOD;
2812                 scsicmd->scsi_done(scsicmd);
2813                 return 0;
2814         }
2815
2816         if (aac->in_reset)
2817                 return SCSI_MLQUEUE_HOST_BUSY;
2818
2819         /*
2820          *      Allocate and initialize a Fib
2821          */
2822         cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2823
2824         aac_fib_init(cmd_fibcontext);
2825
2826         pmcmd = fib_data(cmd_fibcontext);
2827         pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2828         pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2829         /* Eject bit ignored, not relevant */
2830         pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2831                 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2832         pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2833         pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2834                 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2835         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2836
2837         /*
2838          *      Now send the Fib to the adapter
2839          */
2840         status = aac_fib_send(ContainerCommand,
2841                   cmd_fibcontext,
2842                   sizeof(struct aac_power_management),
2843                   FsaNormal,
2844                   0, 1,
2845                   (fib_callback)aac_start_stop_callback,
2846                   (void *)scsicmd);
2847
2848         /*
2849          *      Check that the command queued to the controller
2850          */
2851         if (status == -EINPROGRESS)
2852                 return 0;
2853
2854         aac_fib_complete(cmd_fibcontext);
2855         aac_fib_free(cmd_fibcontext);
2856         return SCSI_MLQUEUE_HOST_BUSY;
2857 }
2858
2859 /**
2860  *      aac_scsi_cmd()          -       Process SCSI command
2861  *      @scsicmd:               SCSI command block
2862  *
2863  *      Emulate a SCSI command and queue the required request for the
2864  *      aacraid firmware.
2865  */
2866
2867 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2868 {
2869         u32 cid, bus;
2870         struct Scsi_Host *host = scsicmd->device->host;
2871         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2872         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2873
2874         if (fsa_dev_ptr == NULL)
2875                 return -1;
2876         /*
2877          *      If the bus, id or lun is out of range, return fail
2878          *      Test does not apply to ID 16, the pseudo id for the controller
2879          *      itself.
2880          */
2881         cid = scmd_id(scsicmd);
2882         if (cid != host->this_id) {
2883                 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2884                         if((cid >= dev->maximum_num_containers) ||
2885                                         (scsicmd->device->lun != 0)) {
2886                                 scsicmd->result = DID_NO_CONNECT << 16;
2887                                 goto scsi_done_ret;
2888                         }
2889
2890                         /*
2891                          *      If the target container doesn't exist, it may have
2892                          *      been newly created
2893                          */
2894                         if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2895                           (fsa_dev_ptr[cid].sense_data.sense_key ==
2896                            NOT_READY)) {
2897                                 switch (scsicmd->cmnd[0]) {
2898                                 case SERVICE_ACTION_IN_16:
2899                                         if (!(dev->raw_io_interface) ||
2900                                             !(dev->raw_io_64) ||
2901                                             ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2902                                                 break;
2903                                 case INQUIRY:
2904                                 case READ_CAPACITY:
2905                                 case TEST_UNIT_READY:
2906                                         if (dev->in_reset)
2907                                                 return -1;
2908                                         return _aac_probe_container(scsicmd,
2909                                                         aac_probe_container_callback2);
2910                                 default:
2911                                         break;
2912                                 }
2913                         }
2914                 } else {  /* check for physical non-dasd devices */
2915                         bus = aac_logical_to_phys(scmd_channel(scsicmd));
2916                         if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2917                                 (dev->hba_map[bus][cid].expose
2918                                                 == AAC_HIDE_DISK)){
2919                                 if (scsicmd->cmnd[0] == INQUIRY) {
2920                                         scsicmd->result = DID_NO_CONNECT << 16;
2921                                         goto scsi_done_ret;
2922                                 }
2923                         }
2924
2925                         if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2926                                 dev->hba_map[bus][cid].devtype
2927                                         == AAC_DEVTYPE_NATIVE_RAW) {
2928                                 if (dev->in_reset)
2929                                         return -1;
2930                                 return aac_send_hba_fib(scsicmd);
2931                         } else if (dev->nondasd_support || expose_physicals ||
2932                                 dev->jbod) {
2933                                 if (dev->in_reset)
2934                                         return -1;
2935                                 return aac_send_srb_fib(scsicmd);
2936                         } else {
2937                                 scsicmd->result = DID_NO_CONNECT << 16;
2938                                 goto scsi_done_ret;
2939                         }
2940                 }
2941         }
2942         /*
2943          * else Command for the controller itself
2944          */
2945         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
2946                 (scsicmd->cmnd[0] != TEST_UNIT_READY))
2947         {
2948                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2949                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2950                 set_sense(&dev->fsa_dev[cid].sense_data,
2951                   ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2952                   ASENCODE_INVALID_COMMAND, 0, 0);
2953                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2954                        min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2955                              SCSI_SENSE_BUFFERSIZE));
2956                 goto scsi_done_ret;
2957         }
2958
2959         switch (scsicmd->cmnd[0]) {
2960         case READ_6:
2961         case READ_10:
2962         case READ_12:
2963         case READ_16:
2964                 if (dev->in_reset)
2965                         return -1;
2966                 return aac_read(scsicmd);
2967
2968         case WRITE_6:
2969         case WRITE_10:
2970         case WRITE_12:
2971         case WRITE_16:
2972                 if (dev->in_reset)
2973                         return -1;
2974                 return aac_write(scsicmd);
2975
2976         case SYNCHRONIZE_CACHE:
2977                 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2978                         scsicmd->result = AAC_STAT_GOOD;
2979                         break;
2980                 }
2981                 /* Issue FIB to tell Firmware to flush it's cache */
2982                 if ((aac_cache & 6) != 2)
2983                         return aac_synchronize(scsicmd);
2984         case INQUIRY:
2985         {
2986                 struct inquiry_data inq_data;
2987
2988                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2989                 memset(&inq_data, 0, sizeof (struct inquiry_data));
2990
2991                 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2992                         char *arr = (char *)&inq_data;
2993
2994                         /* EVPD bit set */
2995                         arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2996                           INQD_PDT_PROC : INQD_PDT_DA;
2997                         if (scsicmd->cmnd[2] == 0) {
2998                                 /* supported vital product data pages */
2999                                 arr[3] = 3;
3000                                 arr[4] = 0x0;
3001                                 arr[5] = 0x80;
3002                                 arr[6] = 0x83;
3003                                 arr[1] = scsicmd->cmnd[2];
3004                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
3005                                                          sizeof(inq_data));
3006                                 scsicmd->result = AAC_STAT_GOOD;
3007                         } else if (scsicmd->cmnd[2] == 0x80) {
3008                                 /* unit serial number page */
3009                                 arr[3] = setinqserial(dev, &arr[4],
3010                                   scmd_id(scsicmd));
3011                                 arr[1] = scsicmd->cmnd[2];
3012                                 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
3013                                                          sizeof(inq_data));
3014                                 if (aac_wwn != 2)
3015                                         return aac_get_container_serial(
3016                                                 scsicmd);
3017                                 scsicmd->result = AAC_STAT_GOOD;
3018                         } else if (scsicmd->cmnd[2] == 0x83) {
3019                                 /* vpd page 0x83 - Device Identification Page */
3020                                 char *sno = (char *)&inq_data;
3021                                 sno[3] = setinqserial(dev, &sno[4],
3022                                                       scmd_id(scsicmd));
3023                                 if (aac_wwn != 2)
3024                                         return aac_get_container_serial(
3025                                                 scsicmd);
3026                                 scsicmd->result = AAC_STAT_GOOD;
3027                         } else {
3028                                 /* vpd page not implemented */
3029                                 scsicmd->result = DID_OK << 16 |
3030                                   COMMAND_COMPLETE << 8 |
3031                                   SAM_STAT_CHECK_CONDITION;
3032                                 set_sense(&dev->fsa_dev[cid].sense_data,
3033                                   ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
3034                                   ASENCODE_NO_SENSE, 7, 2);
3035                                 memcpy(scsicmd->sense_buffer,
3036                                   &dev->fsa_dev[cid].sense_data,
3037                                   min_t(size_t,
3038                                         sizeof(dev->fsa_dev[cid].sense_data),
3039                                         SCSI_SENSE_BUFFERSIZE));
3040                         }
3041                         break;
3042                 }
3043                 inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
3044                 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 */
3045                 inq_data.inqd_len = 31;
3046                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
3047                 inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
3048                 /*
3049                  *      Set the Vendor, Product, and Revision Level
3050                  *      see: <vendor>.c i.e. aac.c
3051                  */
3052                 if (cid == host->this_id) {
3053                         setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
3054                         inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
3055                         scsi_sg_copy_from_buffer(scsicmd, &inq_data,
3056                                                  sizeof(inq_data));
3057                         scsicmd->result = AAC_STAT_GOOD;
3058                         break;
3059                 }
3060                 if (dev->in_reset)
3061                         return -1;
3062                 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
3063                 inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
3064                 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
3065                 return aac_get_container_name(scsicmd);
3066         }
3067         case SERVICE_ACTION_IN_16:
3068                 if (!(dev->raw_io_interface) ||
3069                     !(dev->raw_io_64) ||
3070                     ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
3071                         break;
3072         {
3073                 u64 capacity;
3074                 char cp[13];
3075                 unsigned int alloc_len;
3076
3077                 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
3078                 capacity = fsa_dev_ptr[cid].size - 1;
3079                 cp[0] = (capacity >> 56) & 0xff;
3080                 cp[1] = (capacity >> 48) & 0xff;
3081                 cp[2] = (capacity >> 40) & 0xff;
3082                 cp[3] = (capacity >> 32) & 0xff;
3083                 cp[4] = (capacity >> 24) & 0xff;
3084                 cp[5] = (capacity >> 16) & 0xff;
3085                 cp[6] = (capacity >> 8) & 0xff;
3086                 cp[7] = (capacity >> 0) & 0xff;
3087                 cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
3088                 cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3089                 cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3090                 cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
3091                 cp[12] = 0;
3092
3093                 alloc_len = ((scsicmd->cmnd[10] << 24)
3094                              + (scsicmd->cmnd[11] << 16)
3095                              + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
3096
3097                 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
3098                 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
3099                 if (alloc_len < scsi_bufflen(scsicmd))
3100                         scsi_set_resid(scsicmd,
3101                                        scsi_bufflen(scsicmd) - alloc_len);
3102
3103                 /* Do not cache partition table for arrays */
3104                 scsicmd->device->removable = 1;
3105
3106                 scsicmd->result = AAC_STAT_GOOD;
3107                 break;
3108         }
3109
3110         case READ_CAPACITY:
3111         {
3112                 u32 capacity;
3113                 char cp[8];
3114
3115                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
3116                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3117                         capacity = fsa_dev_ptr[cid].size - 1;
3118                 else
3119                         capacity = (u32)-1;
3120
3121                 cp[0] = (capacity >> 24) & 0xff;
3122                 cp[1] = (capacity >> 16) & 0xff;
3123                 cp[2] = (capacity >> 8) & 0xff;
3124                 cp[3] = (capacity >> 0) & 0xff;
3125                 cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
3126                 cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3127                 cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3128                 cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
3129                 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
3130                 /* Do not cache partition table for arrays */
3131                 scsicmd->device->removable = 1;
3132                 scsicmd->result = AAC_STAT_GOOD;
3133                 break;
3134         }
3135
3136         case MODE_SENSE:
3137         {
3138                 int mode_buf_length = 4;
3139                 u32 capacity;
3140                 aac_modep_data mpd;
3141
3142                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3143                         capacity = fsa_dev_ptr[cid].size - 1;
3144                 else
3145                         capacity = (u32)-1;
3146
3147                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
3148                 memset((char *)&mpd, 0, sizeof(aac_modep_data));
3149
3150                 /* Mode data length */
3151                 mpd.hd.data_length = sizeof(mpd.hd) - 1;
3152                 /* Medium type - default */
3153                 mpd.hd.med_type = 0;
3154                 /* Device-specific param,
3155                    bit 8: 0/1 = write enabled/protected
3156                    bit 4: 0/1 = FUA enabled */
3157                 mpd.hd.dev_par = 0;
3158
3159                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
3160                         mpd.hd.dev_par = 0x10;
3161                 if (scsicmd->cmnd[1] & 0x8)
3162                         mpd.hd.bd_length = 0;   /* Block descriptor length */
3163                 else {
3164                         mpd.hd.bd_length = sizeof(mpd.bd);
3165                         mpd.hd.data_length += mpd.hd.bd_length;
3166                         mpd.bd.block_length[0] =
3167                                 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3168                         mpd.bd.block_length[1] =
3169                                 (fsa_dev_ptr[cid].block_size >> 8) &  0xff;
3170                         mpd.bd.block_length[2] =
3171                                 fsa_dev_ptr[cid].block_size  & 0xff;
3172
3173                         mpd.mpc_buf[0] = scsicmd->cmnd[2];
3174                         if (scsicmd->cmnd[2] == 0x1C) {
3175                                 /* page length */
3176                                 mpd.mpc_buf[1] = 0xa;
3177                                 /* Mode data length */
3178                                 mpd.hd.data_length = 23;
3179                         } else {
3180                                 /* Mode data length */
3181                                 mpd.hd.data_length = 15;
3182                         }
3183
3184                         if (capacity > 0xffffff) {
3185                                 mpd.bd.block_count[0] = 0xff;
3186                                 mpd.bd.block_count[1] = 0xff;
3187                                 mpd.bd.block_count[2] = 0xff;
3188                         } else {
3189                                 mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
3190                                 mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
3191                                 mpd.bd.block_count[2] = capacity  & 0xff;
3192                         }
3193                 }
3194                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3195                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3196                         mpd.hd.data_length += 3;
3197                         mpd.mpc_buf[0] = 8;
3198                         mpd.mpc_buf[1] = 1;
3199                         mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
3200                                 ? 0 : 0x04; /* WCE */
3201                         mode_buf_length = sizeof(mpd);
3202                 }
3203
3204                 if (mode_buf_length > scsicmd->cmnd[4])
3205                         mode_buf_length = scsicmd->cmnd[4];
3206                 else
3207                         mode_buf_length = sizeof(mpd);
3208                 scsi_sg_copy_from_buffer(scsicmd,
3209                                          (char *)&mpd,
3210                                          mode_buf_length);
3211                 scsicmd->result = AAC_STAT_GOOD;
3212                 break;
3213         }
3214         case MODE_SENSE_10:
3215         {
3216                 u32 capacity;
3217                 int mode_buf_length = 8;
3218                 aac_modep10_data mpd10;
3219
3220                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3221                         capacity = fsa_dev_ptr[cid].size - 1;
3222                 else
3223                         capacity = (u32)-1;
3224
3225                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
3226                 memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
3227                 /* Mode data length (MSB) */
3228                 mpd10.hd.data_length[0] = 0;
3229                 /* Mode data length (LSB) */
3230                 mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
3231                 /* Medium type - default */
3232                 mpd10.hd.med_type = 0;
3233                 /* Device-specific param,
3234                    bit 8: 0/1 = write enabled/protected
3235                    bit 4: 0/1 = FUA enabled */
3236                 mpd10.hd.dev_par = 0;
3237
3238                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
3239                         mpd10.hd.dev_par = 0x10;
3240                 mpd10.hd.rsrvd[0] = 0;  /* reserved */
3241                 mpd10.hd.rsrvd[1] = 0;  /* reserved */
3242                 if (scsicmd->cmnd[1] & 0x8) {
3243                         /* Block descriptor length (MSB) */
3244                         mpd10.hd.bd_length[0] = 0;
3245                         /* Block descriptor length (LSB) */
3246                         mpd10.hd.bd_length[1] = 0;
3247                 } else {
3248                         mpd10.hd.bd_length[0] = 0;
3249                         mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
3250
3251                         mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
3252
3253                         mpd10.bd.block_length[0] =
3254                                 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3255                         mpd10.bd.block_length[1] =
3256                                 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3257                         mpd10.bd.block_length[2] =
3258                                 fsa_dev_ptr[cid].block_size  & 0xff;
3259
3260                         if (capacity > 0xffffff) {
3261                                 mpd10.bd.block_count[0] = 0xff;
3262                                 mpd10.bd.block_count[1] = 0xff;
3263                                 mpd10.bd.block_count[2] = 0xff;
3264                         } else {
3265                                 mpd10.bd.block_count[0] =
3266                                         (capacity >> 16) & 0xff;
3267                                 mpd10.bd.block_count[1] =
3268                                         (capacity >> 8) & 0xff;
3269                                 mpd10.bd.block_count[2] =
3270                                         capacity  & 0xff;
3271                         }
3272                 }
3273                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3274                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3275                         mpd10.hd.data_length[1] += 3;
3276                         mpd10.mpc_buf[0] = 8;
3277                         mpd10.mpc_buf[1] = 1;
3278                         mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
3279                                 ? 0 : 0x04; /* WCE */
3280                         mode_buf_length = sizeof(mpd10);
3281                         if (mode_buf_length > scsicmd->cmnd[8])
3282                                 mode_buf_length = scsicmd->cmnd[8];
3283                 }
3284                 scsi_sg_copy_from_buffer(scsicmd,
3285                                          (char *)&mpd10,
3286                                          mode_buf_length);
3287
3288                 scsicmd->result = AAC_STAT_GOOD;
3289                 break;
3290         }
3291         case REQUEST_SENSE:
3292                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
3293                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3294                                 sizeof(struct sense_data));
3295                 memset(&dev->fsa_dev[cid].sense_data, 0,
3296                                 sizeof(struct sense_data));
3297                 scsicmd->result = AAC_STAT_GOOD;
3298                 break;
3299
3300         case ALLOW_MEDIUM_REMOVAL:
3301                 dprintk((KERN_DEBUG "LOCK command.\n"));
3302                 if (scsicmd->cmnd[4])
3303                         fsa_dev_ptr[cid].locked = 1;
3304                 else
3305                         fsa_dev_ptr[cid].locked = 0;
3306
3307                 scsicmd->result = AAC_STAT_GOOD;
3308                 break;
3309         /*
3310          *      These commands are all No-Ops
3311          */
3312         case TEST_UNIT_READY:
3313                 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
3314                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3315                                 SAM_STAT_CHECK_CONDITION;
3316                         set_sense(&dev->fsa_dev[cid].sense_data,
3317                                   NOT_READY, SENCODE_BECOMING_READY,
3318                                   ASENCODE_BECOMING_READY, 0, 0);
3319                         memcpy(scsicmd->sense_buffer,
3320                                &dev->fsa_dev[cid].sense_data,
3321                                min_t(size_t,
3322                                      sizeof(dev->fsa_dev[cid].sense_data),
3323                                      SCSI_SENSE_BUFFERSIZE));
3324                 break;
3325                 }
3326         case RESERVE:
3327         case RELEASE:
3328         case REZERO_UNIT:
3329         case REASSIGN_BLOCKS:
3330         case SEEK_10:
3331                 scsicmd->result = AAC_STAT_GOOD;
3332                 break;
3333
3334         case START_STOP:
3335                 return aac_start_stop(scsicmd);
3336
3337         /* FALLTHRU */
3338         default:
3339         /*
3340          *      Unhandled commands
3341          */
3342                 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n",
3343                                 scsicmd->cmnd[0]));
3344                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3345                                 SAM_STAT_CHECK_CONDITION;
3346                 set_sense(&dev->fsa_dev[cid].sense_data,
3347                           ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
3348                           ASENCODE_INVALID_COMMAND, 0, 0);
3349                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3350                                 min_t(size_t,
3351                                       sizeof(dev->fsa_dev[cid].sense_data),
3352                                       SCSI_SENSE_BUFFERSIZE));
3353         }
3354
3355 scsi_done_ret:
3356
3357         scsicmd->scsi_done(scsicmd);
3358         return 0;
3359 }
3360
3361 static int query_disk(struct aac_dev *dev, void __user *arg)
3362 {
3363         struct aac_query_disk qd;
3364         struct fsa_dev_info *fsa_dev_ptr;
3365
3366         fsa_dev_ptr = dev->fsa_dev;
3367         if (!fsa_dev_ptr)
3368                 return -EBUSY;
3369         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
3370                 return -EFAULT;
3371         if (qd.cnum == -1) {
3372                 if (qd.id < 0 || qd.id >= dev->maximum_num_containers)
3373                         return -EINVAL;
3374                 qd.cnum = qd.id;
3375         } else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) {
3376                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
3377                         return -EINVAL;
3378                 qd.instance = dev->scsi_host_ptr->host_no;
3379                 qd.bus = 0;
3380                 qd.id = CONTAINER_TO_ID(qd.cnum);
3381                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
3382         }
3383         else return -EINVAL;
3384
3385         qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
3386         qd.locked = fsa_dev_ptr[qd.cnum].locked;
3387         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
3388
3389         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
3390                 qd.unmapped = 1;
3391         else
3392                 qd.unmapped = 0;
3393
3394         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
3395           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
3396
3397         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
3398                 return -EFAULT;
3399         return 0;
3400 }
3401
3402 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
3403 {
3404         struct aac_delete_disk dd;
3405         struct fsa_dev_info *fsa_dev_ptr;
3406
3407         fsa_dev_ptr = dev->fsa_dev;
3408         if (!fsa_dev_ptr)
3409                 return -EBUSY;
3410
3411         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3412                 return -EFAULT;
3413
3414         if (dd.cnum >= dev->maximum_num_containers)
3415                 return -EINVAL;
3416         /*
3417          *      Mark this container as being deleted.
3418          */
3419         fsa_dev_ptr[dd.cnum].deleted = 1;
3420         /*
3421          *      Mark the container as no longer valid
3422          */
3423         fsa_dev_ptr[dd.cnum].valid = 0;
3424         return 0;
3425 }
3426
3427 static int delete_disk(struct aac_dev *dev, void __user *arg)
3428 {
3429         struct aac_delete_disk dd;
3430         struct fsa_dev_info *fsa_dev_ptr;
3431
3432         fsa_dev_ptr = dev->fsa_dev;
3433         if (!fsa_dev_ptr)
3434                 return -EBUSY;
3435
3436         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3437                 return -EFAULT;
3438
3439         if (dd.cnum >= dev->maximum_num_containers)
3440                 return -EINVAL;
3441         /*
3442          *      If the container is locked, it can not be deleted by the API.
3443          */
3444         if (fsa_dev_ptr[dd.cnum].locked)
3445                 return -EBUSY;
3446         else {
3447                 /*
3448                  *      Mark the container as no longer being valid.
3449                  */
3450                 fsa_dev_ptr[dd.cnum].valid = 0;
3451                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
3452                 return 0;
3453         }
3454 }
3455
3456 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
3457 {
3458         switch (cmd) {
3459         case FSACTL_QUERY_DISK:
3460                 return query_disk(dev, arg);
3461         case FSACTL_DELETE_DISK:
3462                 return delete_disk(dev, arg);
3463         case FSACTL_FORCE_DELETE_DISK:
3464                 return force_delete_disk(dev, arg);
3465         case FSACTL_GET_CONTAINERS:
3466                 return aac_get_containers(dev);
3467         default:
3468                 return -ENOTTY;
3469         }
3470 }
3471
3472 /**
3473  *
3474  * aac_srb_callback
3475  * @context: the context set in the fib - here it is scsi cmd
3476  * @fibptr: pointer to the fib
3477  *
3478  * Handles the completion of a scsi command to a non dasd device
3479  *
3480  */
3481
3482 static void aac_srb_callback(void *context, struct fib * fibptr)
3483 {
3484         struct aac_dev *dev;
3485         struct aac_srb_reply *srbreply;
3486         struct scsi_cmnd *scsicmd;
3487
3488         scsicmd = (struct scsi_cmnd *) context;
3489
3490         if (!aac_valid_context(scsicmd, fibptr))
3491                 return;
3492
3493         BUG_ON(fibptr == NULL);
3494
3495         dev = fibptr->dev;
3496
3497         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
3498
3499         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
3500
3501         if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3502                 /* fast response */
3503                 srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
3504                 srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
3505         } else {
3506                 /*
3507                  *      Calculate resid for sg
3508                  */
3509                 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
3510                                    - le32_to_cpu(srbreply->data_xfer_length));
3511         }
3512
3513
3514         scsi_dma_unmap(scsicmd);
3515
3516         /* expose physical device if expose_physicald flag is on */
3517         if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
3518           && expose_physicals > 0)
3519                 aac_expose_phy_device(scsicmd);
3520
3521         /*
3522          * First check the fib status
3523          */
3524
3525         if (le32_to_cpu(srbreply->status) != ST_OK) {
3526                 int len;
3527
3528                 pr_warn("aac_srb_callback: srb failed, status = %d\n",
3529                                 le32_to_cpu(srbreply->status));
3530                 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3531                             SCSI_SENSE_BUFFERSIZE);
3532                 scsicmd->result = DID_ERROR << 16
3533                                 | COMMAND_COMPLETE << 8
3534                                 | SAM_STAT_CHECK_CONDITION;
3535                 memcpy(scsicmd->sense_buffer,
3536                                 srbreply->sense_data, len);
3537         }
3538
3539         /*
3540          * Next check the srb status
3541          */
3542         switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3543         case SRB_STATUS_ERROR_RECOVERY:
3544         case SRB_STATUS_PENDING:
3545         case SRB_STATUS_SUCCESS:
3546                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3547                 break;
3548         case SRB_STATUS_DATA_OVERRUN:
3549                 switch (scsicmd->cmnd[0]) {
3550                 case  READ_6:
3551                 case  WRITE_6:
3552                 case  READ_10:
3553                 case  WRITE_10:
3554                 case  READ_12:
3555                 case  WRITE_12:
3556                 case  READ_16:
3557                 case  WRITE_16:
3558                         if (le32_to_cpu(srbreply->data_xfer_length)
3559                                                 < scsicmd->underflow)
3560                                 pr_warn("aacraid: SCSI CMD underflow\n");
3561                         else
3562                                 pr_warn("aacraid: SCSI CMD Data Overrun\n");
3563                         scsicmd->result = DID_ERROR << 16
3564                                         | COMMAND_COMPLETE << 8;
3565                         break;
3566                 case INQUIRY:
3567                         scsicmd->result = DID_OK << 16
3568                                         | COMMAND_COMPLETE << 8;
3569                         break;
3570                 default:
3571                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3572                         break;
3573                 }
3574                 break;
3575         case SRB_STATUS_ABORTED:
3576                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3577                 break;
3578         case SRB_STATUS_ABORT_FAILED:
3579                 /*
3580                  * Not sure about this one - but assuming the
3581                  * hba was trying to abort for some reason
3582                  */
3583                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3584                 break;
3585         case SRB_STATUS_PARITY_ERROR:
3586                 scsicmd->result = DID_PARITY << 16
3587                                 | MSG_PARITY_ERROR << 8;
3588                 break;
3589         case SRB_STATUS_NO_DEVICE:
3590         case SRB_STATUS_INVALID_PATH_ID:
3591         case SRB_STATUS_INVALID_TARGET_ID:
3592         case SRB_STATUS_INVALID_LUN:
3593         case SRB_STATUS_SELECTION_TIMEOUT:
3594                 scsicmd->result = DID_NO_CONNECT << 16
3595                                 | COMMAND_COMPLETE << 8;
3596                 break;
3597
3598         case SRB_STATUS_COMMAND_TIMEOUT:
3599         case SRB_STATUS_TIMEOUT:
3600                 scsicmd->result = DID_TIME_OUT << 16
3601                                 | COMMAND_COMPLETE << 8;
3602                 break;
3603
3604         case SRB_STATUS_BUSY:
3605                 scsicmd->result = DID_BUS_BUSY << 16
3606                                 | COMMAND_COMPLETE << 8;
3607                 break;
3608
3609         case SRB_STATUS_BUS_RESET:
3610                 scsicmd->result = DID_RESET << 16
3611                                 | COMMAND_COMPLETE << 8;
3612                 break;
3613
3614         case SRB_STATUS_MESSAGE_REJECTED:
3615                 scsicmd->result = DID_ERROR << 16
3616                                 | MESSAGE_REJECT << 8;
3617                 break;
3618         case SRB_STATUS_REQUEST_FLUSHED:
3619         case SRB_STATUS_ERROR:
3620         case SRB_STATUS_INVALID_REQUEST:
3621         case SRB_STATUS_REQUEST_SENSE_FAILED:
3622         case SRB_STATUS_NO_HBA:
3623         case SRB_STATUS_UNEXPECTED_BUS_FREE:
3624         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3625         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3626         case SRB_STATUS_DELAYED_RETRY:
3627         case SRB_STATUS_BAD_FUNCTION:
3628         case SRB_STATUS_NOT_STARTED:
3629         case SRB_STATUS_NOT_IN_USE:
3630         case SRB_STATUS_FORCE_ABORT:
3631         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3632         default:
3633 #ifdef AAC_DETAILED_STATUS_INFO
3634                 pr_info("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x -scsi status 0x%x\n",
3635                         le32_to_cpu(srbreply->srb_status) & 0x3F,
3636                         aac_get_status_string(
3637                                 le32_to_cpu(srbreply->srb_status) & 0x3F),
3638                         scsicmd->cmnd[0],
3639                         le32_to_cpu(srbreply->scsi_status));
3640 #endif
3641                 /*
3642                  * When the CC bit is SET by the host in ATA pass thru CDB,
3643                  *  driver is supposed to return DID_OK
3644                  *
3645                  * When the CC bit is RESET by the host, driver should
3646                  *  return DID_ERROR
3647                  */
3648                 if ((scsicmd->cmnd[0] == ATA_12)
3649                         || (scsicmd->cmnd[0] == ATA_16)) {
3650
3651                         if (scsicmd->cmnd[2] & (0x01 << 5)) {
3652                                 scsicmd->result = DID_OK << 16
3653                                         | COMMAND_COMPLETE << 8;
3654                         break;
3655                         } else {
3656                                 scsicmd->result = DID_ERROR << 16
3657                                         | COMMAND_COMPLETE << 8;
3658                         break;
3659                         }
3660                 } else {
3661                         scsicmd->result = DID_ERROR << 16
3662                                 | COMMAND_COMPLETE << 8;
3663                         break;
3664                 }
3665         }
3666         if (le32_to_cpu(srbreply->scsi_status)
3667                         == SAM_STAT_CHECK_CONDITION) {
3668                 int len;
3669
3670                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3671                 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3672                             SCSI_SENSE_BUFFERSIZE);
3673 #ifdef AAC_DETAILED_STATUS_INFO
3674                 pr_warn("aac_srb_callback: check condition, status = %d len=%d\n",
3675                                         le32_to_cpu(srbreply->status), len);
3676 #endif
3677                 memcpy(scsicmd->sense_buffer,
3678                                 srbreply->sense_data, len);
3679         }
3680
3681         /*
3682          * OR in the scsi status (already shifted up a bit)
3683          */
3684         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3685
3686         aac_fib_complete(fibptr);
3687         scsicmd->scsi_done(scsicmd);
3688 }
3689
3690 static void hba_resp_task_complete(struct aac_dev *dev,
3691                                         struct scsi_cmnd *scsicmd,
3692                                         struct aac_hba_resp *err) {
3693
3694         scsicmd->result = err->status;
3695         /* set residual count */
3696         scsi_set_resid(scsicmd, le32_to_cpu(err->residual_count));
3697
3698         switch (err->status) {
3699         case SAM_STAT_GOOD:
3700                 scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3701                 break;
3702         case SAM_STAT_CHECK_CONDITION:
3703         {
3704                 int len;
3705
3706                 len = min_t(u8, err->sense_response_data_len,
3707                         SCSI_SENSE_BUFFERSIZE);
3708                 if (len)
3709                         memcpy(scsicmd->sense_buffer,
3710                                 err->sense_response_buf, len);
3711                 scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3712                 break;
3713         }
3714         case SAM_STAT_BUSY:
3715                 scsicmd->result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
3716                 break;
3717         case SAM_STAT_TASK_ABORTED:
3718                 scsicmd->result |= DID_ABORT << 16 | ABORT << 8;
3719                 break;
3720         case SAM_STAT_RESERVATION_CONFLICT:
3721         case SAM_STAT_TASK_SET_FULL:
3722         default:
3723                 scsicmd->result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3724                 break;
3725         }
3726 }
3727
3728 static void hba_resp_task_failure(struct aac_dev *dev,
3729                                         struct scsi_cmnd *scsicmd,
3730                                         struct aac_hba_resp *err)
3731 {
3732         switch (err->status) {
3733         case HBA_RESP_STAT_HBAMODE_DISABLED:
3734         {
3735                 u32 bus, cid;
3736
3737                 bus = aac_logical_to_phys(scmd_channel(scsicmd));
3738                 cid = scmd_id(scsicmd);
3739                 if (dev->hba_map[bus][cid].devtype == AAC_DEVTYPE_NATIVE_RAW) {
3740                         dev->hba_map[bus][cid].devtype = AAC_DEVTYPE_ARC_RAW;
3741                         dev->hba_map[bus][cid].rmw_nexus = 0xffffffff;
3742                 }
3743                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3744                 break;
3745         }
3746         case HBA_RESP_STAT_IO_ERROR:
3747         case HBA_RESP_STAT_NO_PATH_TO_DEVICE:
3748                 scsicmd->result = DID_OK << 16 |
3749                         COMMAND_COMPLETE << 8 | SAM_STAT_BUSY;
3750                 break;
3751         case HBA_RESP_STAT_IO_ABORTED:
3752                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3753                 break;
3754         case HBA_RESP_STAT_INVALID_DEVICE:
3755                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3756                 break;
3757         case HBA_RESP_STAT_UNDERRUN:
3758                 /* UNDERRUN is OK */
3759                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3760                 break;
3761         case HBA_RESP_STAT_OVERRUN:
3762         default:
3763                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3764                 break;
3765         }
3766 }
3767
3768 /**
3769  *
3770  * aac_hba_callback
3771  * @context: the context set in the fib - here it is scsi cmd
3772  * @fibptr: pointer to the fib
3773  *
3774  * Handles the completion of a native HBA scsi command
3775  *
3776  */
3777 void aac_hba_callback(void *context, struct fib *fibptr)
3778 {
3779         struct aac_dev *dev;
3780         struct scsi_cmnd *scsicmd;
3781
3782         struct aac_hba_resp *err =
3783                         &((struct aac_native_hba *)fibptr->hw_fib_va)->resp.err;
3784
3785         scsicmd = (struct scsi_cmnd *) context;
3786
3787         if (!aac_valid_context(scsicmd, fibptr))
3788                 return;
3789
3790         WARN_ON(fibptr == NULL);
3791         dev = fibptr->dev;
3792
3793         if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF))
3794                 scsi_dma_unmap(scsicmd);
3795
3796         if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3797                 /* fast response */
3798                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3799                 goto out;
3800         }
3801
3802         switch (err->service_response) {
3803         case HBA_RESP_SVCRES_TASK_COMPLETE:
3804                 hba_resp_task_complete(dev, scsicmd, err);
3805                 break;
3806         case HBA_RESP_SVCRES_FAILURE:
3807                 hba_resp_task_failure(dev, scsicmd, err);
3808                 break;
3809         case HBA_RESP_SVCRES_TMF_REJECTED:
3810                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
3811                 break;
3812         case HBA_RESP_SVCRES_TMF_LUN_INVALID:
3813                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3814                 break;
3815         case HBA_RESP_SVCRES_TMF_COMPLETE:
3816         case HBA_RESP_SVCRES_TMF_SUCCEEDED:
3817                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3818                 break;
3819         default:
3820                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3821                 break;
3822         }
3823
3824 out:
3825         aac_fib_complete(fibptr);
3826
3827         if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF)
3828                 scsicmd->SCp.sent_command = 1;
3829         else
3830                 scsicmd->scsi_done(scsicmd);
3831 }
3832
3833 /**
3834  *
3835  * aac_send_srb_fib
3836  * @scsicmd: the scsi command block
3837  *
3838  * This routine will form a FIB and fill in the aac_srb from the
3839  * scsicmd passed in.
3840  */
3841
3842 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3843 {
3844         struct fib* cmd_fibcontext;
3845         struct aac_dev* dev;
3846         int status;
3847
3848         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3849         if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3850                         scsicmd->device->lun > 7) {
3851                 scsicmd->result = DID_NO_CONNECT << 16;
3852                 scsicmd->scsi_done(scsicmd);
3853                 return 0;
3854         }
3855
3856         /*
3857          *      Allocate and initialize a Fib then setup a BlockWrite command
3858          */
3859         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3860         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3861         status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
3862
3863         /*
3864          *      Check that the command queued to the controller
3865          */
3866         if (status == -EINPROGRESS)
3867                 return 0;
3868
3869         printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3870         aac_fib_complete(cmd_fibcontext);
3871         aac_fib_free(cmd_fibcontext);
3872
3873         return -1;
3874 }
3875
3876 /**
3877  *
3878  * aac_send_hba_fib
3879  * @scsicmd: the scsi command block
3880  *
3881  * This routine will form a FIB and fill in the aac_hba_cmd_req from the
3882  * scsicmd passed in.
3883  */
3884 static int aac_send_hba_fib(struct scsi_cmnd *scsicmd)
3885 {
3886         struct fib *cmd_fibcontext;
3887         struct aac_dev *dev;
3888         int status;
3889
3890         dev = shost_priv(scsicmd->device->host);
3891         if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3892                         scsicmd->device->lun > AAC_MAX_LUN - 1) {
3893                 scsicmd->result = DID_NO_CONNECT << 16;
3894                 scsicmd->scsi_done(scsicmd);
3895                 return 0;
3896         }
3897
3898         /*
3899          *      Allocate and initialize a Fib then setup a BlockWrite command
3900          */
3901         cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3902         if (!cmd_fibcontext)
3903                 return -1;
3904
3905         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3906         status = aac_adapter_hba(cmd_fibcontext, scsicmd);
3907
3908         /*
3909          *      Check that the command queued to the controller
3910          */
3911         if (status == -EINPROGRESS)
3912                 return 0;
3913
3914         pr_warn("aac_hba_cmd_req: aac_fib_send failed with status: %d\n",
3915                 status);
3916         aac_fib_complete(cmd_fibcontext);
3917         aac_fib_free(cmd_fibcontext);
3918
3919         return -1;
3920 }
3921
3922
3923 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
3924 {
3925         struct aac_dev *dev;
3926         unsigned long byte_count = 0;
3927         int nseg;
3928         struct scatterlist *sg;
3929         int i;
3930
3931         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3932         // Get rid of old data
3933         psg->count = 0;
3934         psg->sg[0].addr = 0;
3935         psg->sg[0].count = 0;
3936
3937         nseg = scsi_dma_map(scsicmd);
3938         if (nseg <= 0)
3939                 return nseg;
3940
3941         psg->count = cpu_to_le32(nseg);
3942
3943         scsi_for_each_sg(scsicmd, sg, nseg, i) {
3944                 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3945                 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3946                 byte_count += sg_dma_len(sg);
3947         }
3948         /* hba wants the size to be exact */
3949         if (byte_count > scsi_bufflen(scsicmd)) {
3950                 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3951                         (byte_count - scsi_bufflen(scsicmd));
3952                 psg->sg[i-1].count = cpu_to_le32(temp);
3953                 byte_count = scsi_bufflen(scsicmd);
3954         }
3955         /* Check for command underflow */
3956         if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3957                 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3958                        byte_count, scsicmd->underflow);
3959         }
3960
3961         return byte_count;
3962 }
3963
3964
3965 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
3966 {
3967         struct aac_dev *dev;
3968         unsigned long byte_count = 0;
3969         u64 addr;
3970         int nseg;
3971         struct scatterlist *sg;
3972         int i;
3973
3974         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3975         // Get rid of old data
3976         psg->count = 0;
3977         psg->sg[0].addr[0] = 0;
3978         psg->sg[0].addr[1] = 0;
3979         psg->sg[0].count = 0;
3980
3981         nseg = scsi_dma_map(scsicmd);
3982         if (nseg <= 0)
3983                 return nseg;
3984
3985         scsi_for_each_sg(scsicmd, sg, nseg, i) {
3986                 int count = sg_dma_len(sg);
3987                 addr = sg_dma_address(sg);
3988                 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3989                 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
3990                 psg->sg[i].count = cpu_to_le32(count);
3991                 byte_count += count;
3992         }
3993         psg->count = cpu_to_le32(nseg);
3994         /* hba wants the size to be exact */
3995         if (byte_count > scsi_bufflen(scsicmd)) {
3996                 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3997                         (byte_count - scsi_bufflen(scsicmd));
3998                 psg->sg[i-1].count = cpu_to_le32(temp);
3999                 byte_count = scsi_bufflen(scsicmd);
4000         }
4001         /* Check for command underflow */
4002         if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4003                 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
4004                        byte_count, scsicmd->underflow);
4005         }
4006
4007         return byte_count;
4008 }
4009
4010 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
4011 {
4012         unsigned long byte_count = 0;
4013         int nseg;
4014         struct scatterlist *sg;
4015         int i;
4016
4017         // Get rid of old data
4018         psg->count = 0;
4019         psg->sg[0].next = 0;
4020         psg->sg[0].prev = 0;
4021         psg->sg[0].addr[0] = 0;
4022         psg->sg[0].addr[1] = 0;
4023         psg->sg[0].count = 0;
4024         psg->sg[0].flags = 0;
4025
4026         nseg = scsi_dma_map(scsicmd);
4027         if (nseg <= 0)
4028                 return nseg;
4029
4030         scsi_for_each_sg(scsicmd, sg, nseg, i) {
4031                 int count = sg_dma_len(sg);
4032                 u64 addr = sg_dma_address(sg);
4033                 psg->sg[i].next = 0;
4034                 psg->sg[i].prev = 0;
4035                 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
4036                 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
4037                 psg->sg[i].count = cpu_to_le32(count);
4038                 psg->sg[i].flags = 0;
4039                 byte_count += count;
4040         }
4041         psg->count = cpu_to_le32(nseg);
4042         /* hba wants the size to be exact */
4043         if (byte_count > scsi_bufflen(scsicmd)) {
4044                 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
4045                         (byte_count - scsi_bufflen(scsicmd));
4046                 psg->sg[i-1].count = cpu_to_le32(temp);
4047                 byte_count = scsi_bufflen(scsicmd);
4048         }
4049         /* Check for command underflow */
4050         if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4051                 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
4052                        byte_count, scsicmd->underflow);
4053         }
4054
4055         return byte_count;
4056 }
4057
4058 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
4059                                 struct aac_raw_io2 *rio2, int sg_max)
4060 {
4061         unsigned long byte_count = 0;
4062         int nseg;
4063         struct scatterlist *sg;
4064         int i, conformable = 0;
4065         u32 min_size = PAGE_SIZE, cur_size;
4066
4067         nseg = scsi_dma_map(scsicmd);
4068         if (nseg <= 0)
4069                 return nseg;
4070
4071         scsi_for_each_sg(scsicmd, sg, nseg, i) {
4072                 int count = sg_dma_len(sg);
4073                 u64 addr = sg_dma_address(sg);
4074
4075                 BUG_ON(i >= sg_max);
4076                 rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
4077                 rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
4078                 cur_size = cpu_to_le32(count);
4079                 rio2->sge[i].length = cur_size;
4080                 rio2->sge[i].flags = 0;
4081                 if (i == 0) {
4082                         conformable = 1;
4083                         rio2->sgeFirstSize = cur_size;
4084                 } else if (i == 1) {
4085                         rio2->sgeNominalSize = cur_size;
4086                         min_size = cur_size;
4087                 } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
4088                         conformable = 0;
4089                         if (cur_size < min_size)
4090                                 min_size = cur_size;
4091                 }
4092                 byte_count += count;
4093         }
4094
4095         /* hba wants the size to be exact */
4096         if (byte_count > scsi_bufflen(scsicmd)) {
4097                 u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
4098                         (byte_count - scsi_bufflen(scsicmd));
4099                 rio2->sge[i-1].length = cpu_to_le32(temp);
4100                 byte_count = scsi_bufflen(scsicmd);
4101         }
4102
4103         rio2->sgeCnt = cpu_to_le32(nseg);
4104         rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
4105         /* not conformable: evaluate required sg elements */
4106         if (!conformable) {
4107                 int j, nseg_new = nseg, err_found;
4108                 for (i = min_size / PAGE_SIZE; i >= 1; --i) {
4109                         err_found = 0;
4110                         nseg_new = 2;
4111                         for (j = 1; j < nseg - 1; ++j) {
4112                                 if (rio2->sge[j].length % (i*PAGE_SIZE)) {
4113                                         err_found = 1;
4114                                         break;
4115                                 }
4116                                 nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
4117                         }
4118                         if (!err_found)
4119                                 break;
4120                 }
4121                 if (i > 0 && nseg_new <= sg_max) {
4122                         int ret = aac_convert_sgraw2(rio2, i, nseg, nseg_new);
4123
4124                         if (ret < 0)
4125                                 return ret;
4126                 }
4127         } else
4128                 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4129
4130         /* Check for command underflow */
4131         if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4132                 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
4133                        byte_count, scsicmd->underflow);
4134         }
4135
4136         return byte_count;
4137 }
4138
4139 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
4140 {
4141         struct sge_ieee1212 *sge;
4142         int i, j, pos;
4143         u32 addr_low;
4144
4145         if (aac_convert_sgl == 0)
4146                 return 0;
4147
4148         sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
4149         if (sge == NULL)
4150                 return -ENOMEM;
4151
4152         for (i = 1, pos = 1; i < nseg-1; ++i) {
4153                 for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
4154                         addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
4155                         sge[pos].addrLow = addr_low;
4156                         sge[pos].addrHigh = rio2->sge[i].addrHigh;
4157                         if (addr_low < rio2->sge[i].addrLow)
4158                                 sge[pos].addrHigh++;
4159                         sge[pos].length = pages * PAGE_SIZE;
4160                         sge[pos].flags = 0;
4161                         pos++;
4162                 }
4163         }
4164         sge[pos] = rio2->sge[nseg-1];
4165         memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
4166
4167         kfree(sge);
4168         rio2->sgeCnt = cpu_to_le32(nseg_new);
4169         rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4170         rio2->sgeNominalSize = pages * PAGE_SIZE;
4171         return 0;
4172 }
4173
4174 static long aac_build_sghba(struct scsi_cmnd *scsicmd,
4175                         struct aac_hba_cmd_req *hbacmd,
4176                         int sg_max,
4177                         u64 sg_address)
4178 {
4179         unsigned long byte_count = 0;
4180         int nseg;
4181         struct scatterlist *sg;
4182         int i;
4183         u32 cur_size;
4184         struct aac_hba_sgl *sge;
4185
4186         nseg = scsi_dma_map(scsicmd);
4187         if (nseg <= 0) {
4188                 byte_count = nseg;
4189                 goto out;
4190         }
4191
4192         if (nseg > HBA_MAX_SG_EMBEDDED)
4193                 sge = &hbacmd->sge[2];
4194         else
4195                 sge = &hbacmd->sge[0];
4196
4197         scsi_for_each_sg(scsicmd, sg, nseg, i) {
4198                 int count = sg_dma_len(sg);
4199                 u64 addr = sg_dma_address(sg);
4200
4201                 WARN_ON(i >= sg_max);
4202                 sge->addr_hi = cpu_to_le32((u32)(addr>>32));
4203                 sge->addr_lo = cpu_to_le32((u32)(addr & 0xffffffff));
4204                 cur_size = cpu_to_le32(count);
4205                 sge->len = cur_size;
4206                 sge->flags = 0;
4207                 byte_count += count;
4208                 sge++;
4209         }
4210
4211         sge--;
4212         /* hba wants the size to be exact */
4213         if (byte_count > scsi_bufflen(scsicmd)) {
4214                 u32 temp;
4215
4216                 temp = le32_to_cpu(sge->len) - byte_count
4217                                                 - scsi_bufflen(scsicmd);
4218                 sge->len = cpu_to_le32(temp);
4219                 byte_count = scsi_bufflen(scsicmd);
4220         }
4221
4222         if (nseg <= HBA_MAX_SG_EMBEDDED) {
4223                 hbacmd->emb_data_desc_count = cpu_to_le32(nseg);
4224                 sge->flags = cpu_to_le32(0x40000000);
4225         } else {
4226                 /* not embedded */
4227                 hbacmd->sge[0].flags = cpu_to_le32(0x80000000);
4228                 hbacmd->emb_data_desc_count = (u8)cpu_to_le32(1);
4229                 hbacmd->sge[0].addr_hi = (u32)cpu_to_le32(sg_address >> 32);
4230                 hbacmd->sge[0].addr_lo =
4231                         cpu_to_le32((u32)(sg_address & 0xffffffff));
4232         }
4233
4234         /* Check for command underflow */
4235         if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4236                 pr_warn("aacraid: cmd len %08lX cmd underflow %08X\n",
4237                                 byte_count, scsicmd->underflow);
4238         }
4239 out:
4240         return byte_count;
4241 }
4242
4243 #ifdef AAC_DETAILED_STATUS_INFO
4244
4245 struct aac_srb_status_info {
4246         u32     status;
4247         char    *str;
4248 };
4249
4250
4251 static struct aac_srb_status_info srb_status_info[] = {
4252         { SRB_STATUS_PENDING,           "Pending Status"},
4253         { SRB_STATUS_SUCCESS,           "Success"},
4254         { SRB_STATUS_ABORTED,           "Aborted Command"},
4255         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
4256         { SRB_STATUS_ERROR,             "Error Event"},
4257         { SRB_STATUS_BUSY,              "Device Busy"},
4258         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
4259         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
4260         { SRB_STATUS_NO_DEVICE,         "No Device"},
4261         { SRB_STATUS_TIMEOUT,           "Timeout"},
4262         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
4263         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
4264         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
4265         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
4266         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
4267         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
4268         { SRB_STATUS_NO_HBA,            "No HBA"},
4269         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
4270         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
4271         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
4272         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
4273         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
4274         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
4275         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"},
4276         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
4277         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
4278         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
4279         { SRB_STATUS_NOT_STARTED,       "Not Started"},
4280         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
4281         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
4282         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
4283         { 0xff,                         "Unknown Error"}
4284 };
4285
4286 char *aac_get_status_string(u32 status)
4287 {
4288         int i;
4289
4290         for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
4291                 if (srb_status_info[i].status == status)
4292                         return srb_status_info[i].str;
4293
4294         return "Bad Status Code";
4295 }
4296
4297 #endif