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