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