blob: aadedbd630835ce824c6606e2ed0159ac34d2c30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
Mark Haverkamp03d44332007-03-15 10:27:45 -07008 * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pci.h>
30#include <linux/spinlock.h>
31#include <linux/slab.h>
32#include <linux/completion.h>
33#include <linux/blkdev.h>
34#include <asm/semaphore.h>
35#include <asm/uaccess.h>
Salyzyn, Mark3a0086a2008-01-11 11:46:44 -080036#include <linux/highmem.h> /* For flush_kernel_dcache_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <scsi/scsi.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42
43#include "aacraid.h"
44
45/* values for inqd_pdt: Peripheral device type in plain English */
46#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
47#define INQD_PDT_PROC 0x03 /* Processor device */
48#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
49#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
50#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
51#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
52
53#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
54#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Sense codes
58 */
59
60#define SENCODE_NO_SENSE 0x00
61#define SENCODE_END_OF_DATA 0x00
62#define SENCODE_BECOMING_READY 0x04
63#define SENCODE_INIT_CMD_REQUIRED 0x04
64#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
65#define SENCODE_INVALID_COMMAND 0x20
66#define SENCODE_LBA_OUT_OF_RANGE 0x21
67#define SENCODE_INVALID_CDB_FIELD 0x24
68#define SENCODE_LUN_NOT_SUPPORTED 0x25
69#define SENCODE_INVALID_PARAM_FIELD 0x26
70#define SENCODE_PARAM_NOT_SUPPORTED 0x26
71#define SENCODE_PARAM_VALUE_INVALID 0x26
72#define SENCODE_RESET_OCCURRED 0x29
73#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
74#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
75#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
76#define SENCODE_DIAGNOSTIC_FAILURE 0x40
77#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
78#define SENCODE_INVALID_MESSAGE_ERROR 0x49
79#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
80#define SENCODE_OVERLAPPED_COMMAND 0x4E
81
82/*
83 * Additional sense codes
84 */
85
86#define ASENCODE_NO_SENSE 0x00
87#define ASENCODE_END_OF_DATA 0x05
88#define ASENCODE_BECOMING_READY 0x01
89#define ASENCODE_INIT_CMD_REQUIRED 0x02
90#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
91#define ASENCODE_INVALID_COMMAND 0x00
92#define ASENCODE_LBA_OUT_OF_RANGE 0x00
93#define ASENCODE_INVALID_CDB_FIELD 0x00
94#define ASENCODE_LUN_NOT_SUPPORTED 0x00
95#define ASENCODE_INVALID_PARAM_FIELD 0x00
96#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
97#define ASENCODE_PARAM_VALUE_INVALID 0x02
98#define ASENCODE_RESET_OCCURRED 0x00
99#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
100#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
101#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
102#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
103#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
104#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
105#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
106#define ASENCODE_OVERLAPPED_COMMAND 0x00
107
108#define BYTE0(x) (unsigned char)(x)
109#define BYTE1(x) (unsigned char)((x) >> 8)
110#define BYTE2(x) (unsigned char)((x) >> 16)
111#define BYTE3(x) (unsigned char)((x) >> 24)
112
113/*------------------------------------------------------------------------------
114 * S T R U C T S / T Y P E D E F S
115 *----------------------------------------------------------------------------*/
116/* SCSI inquiry data */
117struct inquiry_data {
118 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
119 u8 inqd_dtq; /* RMB | Device Type Qualifier */
120 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
121 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
122 u8 inqd_len; /* Additional length (n-4) */
123 u8 inqd_pad1[2];/* Reserved - must be zero */
124 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
125 u8 inqd_vid[8]; /* Vendor ID */
126 u8 inqd_pid[16];/* Product ID */
127 u8 inqd_prl[4]; /* Product Revision Level */
128};
129
130/*
131 * M O D U L E G L O B A L S
132 */
133
134static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
135static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700136static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
138#ifdef AAC_DETAILED_STATUS_INFO
139static char *aac_get_status_string(u32 status);
140#endif
141
142/*
143 * Non dasd selection is handled entirely in aachba now
144 */
145
146static int nondasd = -1;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -0800147static int aac_cache = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int dacmode = -1;
149
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400150int aac_commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700151int startup_timeout = 180;
152int aif_timeout = 120;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800154module_param(nondasd, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
Salyzyn, Mark95e852e2008-01-08 12:01:07 -0800156module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
157MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n\tbit 0 - Disable FUA in WRITE SCSI commands\n\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n\tbit 2 - Disable only if Battery not protecting Cache");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800158module_param(dacmode, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400160module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700162module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
163MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for adapter to have it's kernel up and\nrunning. This is typically adjusted for large systems that do not have a BIOS.");
164module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
165MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for applications to pick up AIFs before\nderegistering them. This is typically adjusted for heavily burdened systems.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700167int numacb = -1;
168module_param(numacb, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800169MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid values are 512 and down. Default is to use suggestion from Firmware.");
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700170
171int acbsize = -1;
172module_param(acbsize, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800173MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512, 2048, 4096 and 8192. Default is to use suggestion from Firmware.");
Mark Haverkamp653ba582006-09-19 08:59:43 -0700174
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400175int update_interval = 30 * 60;
176module_param(update_interval, int, S_IRUGO|S_IWUSR);
177MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync updates issued to adapter.");
178
179int check_interval = 24 * 60 * 60;
180module_param(check_interval, int, S_IRUGO|S_IWUSR);
181MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health checks.");
182
Andrew Morton87f3bda2007-08-10 14:50:30 -0700183int aac_check_reset = 1;
184module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
Salyzyn, Markf8583172007-10-30 15:50:49 -0400185MODULE_PARM_DESC(aac_check_reset, "If adapter fails health check, reset the adapter. a value of -1 forces the reset to adapters programmed to ignore it.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400186
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800187int expose_physicals = -1;
Mark Haverkamp653ba582006-09-19 08:59:43 -0700188module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800189MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. -1=protect 0=off, 1=on");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700190
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400191int aac_reset_devices = 0;
192module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
193MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700194
195static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
196 struct fib *fibptr) {
197 struct scsi_device *device;
198
199 if (unlikely(!scsicmd || !scsicmd->scsi_done )) {
Salyzyn, Markc835e372007-07-26 14:20:02 -0400200 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
Mark Haverkamp03d44332007-03-15 10:27:45 -0700201 aac_fib_complete(fibptr);
202 aac_fib_free(fibptr);
203 return 0;
204 }
205 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
206 device = scsicmd->device;
207 if (unlikely(!device || !scsi_device_online(device))) {
208 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
209 aac_fib_complete(fibptr);
210 aac_fib_free(fibptr);
211 return 0;
212 }
213 return 1;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/**
217 * aac_get_config_status - check the adapter configuration
218 * @common: adapter to query
219 *
220 * Query config status, and commit the configuration if needed.
221 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700222int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 int status = 0;
225 struct fib * fibptr;
226
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800227 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -ENOMEM;
229
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800230 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 {
232 struct aac_get_config_status *dinfo;
233 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
234
235 dinfo->command = cpu_to_le32(VM_ContainerConfig);
236 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
237 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
238 }
239
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800240 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 fibptr,
242 sizeof (struct aac_get_config_status),
243 FsaNormal,
244 1, 1,
245 NULL, NULL);
246 if (status < 0 ) {
247 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
248 } else {
249 struct aac_get_config_status_resp *reply
250 = (struct aac_get_config_status_resp *) fib_data(fibptr);
251 dprintk((KERN_WARNING
252 "aac_get_config_status: response=%d status=%d action=%d\n",
253 le32_to_cpu(reply->response),
254 le32_to_cpu(reply->status),
255 le32_to_cpu(reply->data.action)));
256 if ((le32_to_cpu(reply->response) != ST_OK) ||
257 (le32_to_cpu(reply->status) != CT_OK) ||
258 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
259 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
260 status = -EINVAL;
261 }
262 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800263 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
265 if (status >= 0) {
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400266 if ((aac_commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800268 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dinfo = (struct aac_commit_config *) fib_data(fibptr);
270
271 dinfo->command = cpu_to_le32(VM_ContainerConfig);
272 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
273
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800274 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 fibptr,
276 sizeof (struct aac_commit_config),
277 FsaNormal,
278 1, 1,
279 NULL, NULL);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800280 aac_fib_complete(fibptr);
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400281 } else if (aac_commit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 printk(KERN_WARNING
283 "aac_get_config_status: Foreign device configurations are being ignored\n");
284 }
285 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800286 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return status;
288}
289
290/**
291 * aac_get_containers - list containers
292 * @common: adapter to probe
293 *
294 * Make a list of all containers on this controller
295 */
296int aac_get_containers(struct aac_dev *dev)
297{
298 struct fsa_dev_info *fsa_dev_ptr;
299 u32 index;
300 int status = 0;
301 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct aac_get_container_count *dinfo;
303 struct aac_get_container_count_resp *dresp;
304 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
305
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800306 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -ENOMEM;
308
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800309 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
311 dinfo->command = cpu_to_le32(VM_ContainerConfig);
312 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
313
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800314 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 fibptr,
316 sizeof (struct aac_get_container_count),
317 FsaNormal,
318 1, 1,
319 NULL, NULL);
320 if (status >= 0) {
321 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
322 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800323 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700325 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
328 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400329 fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700330 GFP_KERNEL);
331 if (!fsa_dev_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 dev->fsa_dev = fsa_dev_ptr;
335 dev->maximum_num_containers = maximum_num_containers;
336
Mark Haverkampfe76df42007-03-15 12:55:07 -0700337 for (index = 0; index < dev->maximum_num_containers; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 fsa_dev_ptr[index].devname[0] = '\0';
339
Mark Haverkampfe76df42007-03-15 12:55:07 -0700340 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Mark Haverkampfe76df42007-03-15 12:55:07 -0700342 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
344 break;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /*
348 * If there are no more containers, then stop asking.
349 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700350 if (++index >= status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return status;
354}
355
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700356static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
357{
358 void *buf;
James Bottomleycab537d2007-05-16 10:06:39 -0400359 int transfer_len;
FUJITA Tomonori727eead2007-05-26 02:00:42 +0900360 struct scatterlist *sg = scsi_sglist(scsicmd);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700361
Jens Axboe45711f12007-10-22 21:19:53 +0200362 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
FUJITA Tomonori727eead2007-05-26 02:00:42 +0900363 transfer_len = min(sg->length, len + offset);
364
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400365 transfer_len -= offset;
James Bottomleycab537d2007-05-16 10:06:39 -0400366 if (buf && transfer_len > 0)
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400367 memcpy(buf + offset, data, transfer_len);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700368
Salyzyn, Mark3a0086a2008-01-11 11:46:44 -0800369 flush_kernel_dcache_page(kmap_atomic_to_page(buf - sg->offset));
FUJITA Tomonori727eead2007-05-26 02:00:42 +0900370 kunmap_atomic(buf - sg->offset, KM_IRQ0);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700371
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static void get_container_name_callback(void *context, struct fib * fibptr)
375{
376 struct aac_get_name_resp * get_name_reply;
377 struct scsi_cmnd * scsicmd;
378
379 scsicmd = (struct scsi_cmnd *) context;
380
Mark Haverkamp03d44332007-03-15 10:27:45 -0700381 if (!aac_valid_context(scsicmd, fibptr))
382 return;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700385 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
388 /* Failure is irrelevant, using default value instead */
389 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
390 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700391 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
393 while (*sp == ' ')
394 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700395 if (*sp) {
396 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
397 int count = sizeof(d);
398 char *dp = d;
399 do {
400 *dp++ = (*sp) ? *sp++ : ' ';
401 } while (--count > 0);
402 aac_internal_transfer(scsicmd, d,
403 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
408
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800409 aac_fib_complete(fibptr);
410 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700411 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/**
415 * aac_get_container_name - get container name, none blocking.
416 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700417static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 int status;
420 struct aac_get_name *dinfo;
421 struct fib * cmd_fibcontext;
422 struct aac_dev * dev;
423
424 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
425
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800426 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -ENOMEM;
428
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800429 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
431
432 dinfo->command = cpu_to_le32(VM_ContainerConfig);
433 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700434 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
436
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800437 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 cmd_fibcontext,
439 sizeof (struct aac_get_name),
440 FsaNormal,
441 0, 1,
442 (fib_callback) get_container_name_callback,
443 (void *) scsicmd);
444
445 /*
446 * Check that the command queued to the controller
447 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800448 if (status == -EINPROGRESS) {
449 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800453 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
454 aac_fib_complete(cmd_fibcontext);
455 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return -1;
457}
458
Mark Haverkampfe76df42007-03-15 12:55:07 -0700459static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
460{
461 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
462
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400463 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700464 return aac_scsi_cmd(scsicmd);
465
466 scsicmd->result = DID_NO_CONNECT << 16;
467 scsicmd->scsi_done(scsicmd);
468 return 0;
469}
470
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400471static void _aac_probe_container2(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700472{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700473 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700474 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700475 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
476
Mark Haverkamp03d44332007-03-15 10:27:45 -0700477
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400478 if (!aac_valid_context(scsicmd, fibptr))
479 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700480
481 scsicmd->SCp.Status = 0;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400482 fsa_dev_ptr = fibptr->dev->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700483 if (fsa_dev_ptr) {
484 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
485 fsa_dev_ptr += scmd_id(scsicmd);
486
487 if ((le32_to_cpu(dresp->status) == ST_OK) &&
488 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
489 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
490 fsa_dev_ptr->valid = 1;
491 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
492 fsa_dev_ptr->size
493 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
494 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
495 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
496 }
497 if ((fsa_dev_ptr->valid & 1) == 0)
498 fsa_dev_ptr->valid = 0;
499 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
500 }
501 aac_fib_complete(fibptr);
502 aac_fib_free(fibptr);
503 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
504 scsicmd->SCp.ptr = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400505 (*callback)(scsicmd);
506 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700507}
508
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400509static void _aac_probe_container1(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700510{
511 struct scsi_cmnd * scsicmd;
512 struct aac_mount * dresp;
513 struct aac_query_mount *dinfo;
514 int status;
515
516 dresp = (struct aac_mount *) fib_data(fibptr);
517 dresp->mnt[0].capacityhigh = 0;
518 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400519 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
520 _aac_probe_container2(context, fibptr);
521 return;
522 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700523 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700524
Mark Haverkamp03d44332007-03-15 10:27:45 -0700525 if (!aac_valid_context(scsicmd, fibptr))
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400526 return;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700527
Mark Haverkampfe76df42007-03-15 12:55:07 -0700528 aac_fib_init(fibptr);
529
530 dinfo = (struct aac_query_mount *)fib_data(fibptr);
531
532 dinfo->command = cpu_to_le32(VM_NameServe64);
533 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
534 dinfo->type = cpu_to_le32(FT_FILESYS);
535
536 status = aac_fib_send(ContainerCommand,
537 fibptr,
538 sizeof(struct aac_query_mount),
539 FsaNormal,
540 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400541 _aac_probe_container2,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700542 (void *) scsicmd);
543 /*
544 * Check that the command queued to the controller
545 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400546 if (status == -EINPROGRESS)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700547 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400548 else if (status < 0) {
Mark Haverkampfe76df42007-03-15 12:55:07 -0700549 /* Inherit results from VM_NameServe, if any */
550 dresp->status = cpu_to_le32(ST_OK);
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400551 _aac_probe_container2(context, fibptr);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700552 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700553}
554
555static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
556{
557 struct fib * fibptr;
558 int status = -ENOMEM;
559
560 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
561 struct aac_query_mount *dinfo;
562
563 aac_fib_init(fibptr);
564
565 dinfo = (struct aac_query_mount *)fib_data(fibptr);
566
567 dinfo->command = cpu_to_le32(VM_NameServe);
568 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
569 dinfo->type = cpu_to_le32(FT_FILESYS);
570 scsicmd->SCp.ptr = (char *)callback;
571
572 status = aac_fib_send(ContainerCommand,
573 fibptr,
574 sizeof(struct aac_query_mount),
575 FsaNormal,
576 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400577 _aac_probe_container1,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700578 (void *) scsicmd);
579 /*
580 * Check that the command queued to the controller
581 */
582 if (status == -EINPROGRESS) {
583 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
584 return 0;
585 }
586 if (status < 0) {
587 scsicmd->SCp.ptr = NULL;
588 aac_fib_complete(fibptr);
589 aac_fib_free(fibptr);
590 }
591 }
592 if (status < 0) {
593 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
594 if (fsa_dev_ptr) {
595 fsa_dev_ptr += scmd_id(scsicmd);
596 if ((fsa_dev_ptr->valid & 1) == 0) {
597 fsa_dev_ptr->valid = 0;
598 return (*callback)(scsicmd);
599 }
600 }
601 }
602 return status;
603}
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800606 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * @dev: device to query
608 * @cid: container identifier
609 *
610 * Queries the controller about the given volume. The volume information
611 * is updated in the struct fsa_dev_info structure rather than returned.
612 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700613static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
614{
615 scsicmd->device = NULL;
616 return 0;
617}
618
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800619int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700621 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
622 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Mark Haverkampfe76df42007-03-15 12:55:07 -0700625 if (!scsicmd || !scsidev) {
626 kfree(scsicmd);
627 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700628 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700630 scsicmd->list.next = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400631 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Mark Haverkampfe76df42007-03-15 12:55:07 -0700633 scsicmd->device = scsidev;
634 scsidev->sdev_state = 0;
635 scsidev->id = cid;
636 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Mark Haverkampfe76df42007-03-15 12:55:07 -0700638 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
639 while (scsicmd->device == scsidev)
640 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400641 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700642 status = scsicmd->SCp.Status;
643 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return status;
645}
646
647/* Local Structure to set SCSI inquiry data strings */
648struct scsi_inq {
649 char vid[8]; /* Vendor ID */
650 char pid[16]; /* Product ID */
651 char prl[4]; /* Product Revision Level */
652};
653
654/**
655 * InqStrCopy - string merge
656 * @a: string to copy from
657 * @b: string to copy to
658 *
659 * Copy a String from one location to another
660 * without copying \0
661 */
662
663static void inqstrcpy(char *a, char *b)
664{
665
666 while(*a != (char)0)
667 *b++ = *a++;
668}
669
670static char *container_types[] = {
671 "None",
672 "Volume",
673 "Mirror",
674 "Stripe",
675 "RAID5",
676 "SSRW",
677 "SSRO",
678 "Morph",
679 "Legacy",
680 "RAID4",
681 "RAID10",
682 "RAID00",
683 "V-MIRRORS",
684 "PSEUDO R4",
685 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700686 "RAID5D",
687 "RAID5D0",
688 "RAID1E",
689 "RAID6",
690 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 "Unknown"
692};
693
Salyzyn, Mark17eaace2008-01-08 12:23:49 -0800694char * get_container_type(unsigned tindex)
695{
696 if (tindex >= ARRAY_SIZE(container_types))
697 tindex = ARRAY_SIZE(container_types) - 1;
698 return container_types[tindex];
699}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701/* Function: setinqstr
702 *
703 * Arguments: [1] pointer to void [1] int
704 *
705 * Purpose: Sets SCSI inquiry data strings for vendor, product
706 * and revision level. Allows strings to be set in platform dependant
707 * files instead of in OS dependant driver source.
708 */
709
Mark Haverkamp794d0602005-10-24 10:51:53 -0700710static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700715 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Mark Haverkamp794d0602005-10-24 10:51:53 -0700717 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
718 char * cp = dev->supplement_adapter_info.AdapterTypeText;
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800719 int c;
720 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
721 inqstrcpy("SMC", str->vid);
722 else {
723 c = sizeof(str->vid);
724 while (*cp && *cp != ' ' && --c)
725 ++cp;
726 c = *cp;
727 *cp = '\0';
728 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
729 str->vid);
730 *cp = c;
731 while (*cp && *cp != ' ')
732 ++cp;
733 }
Mark Haverkamp794d0602005-10-24 10:51:53 -0700734 while (*cp == ' ')
735 ++cp;
736 /* last six chars reserved for vol type */
737 c = 0;
738 if (strlen(cp) > sizeof(str->pid)) {
739 c = cp[sizeof(str->pid)];
740 cp[sizeof(str->pid)] = '\0';
741 }
742 inqstrcpy (cp, str->pid);
743 if (c)
744 cp[sizeof(str->pid)] = c;
745 } else {
746 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700747
748 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700749 /* last six chars reserved for vol type */
750 inqstrcpy (mp->model, str->pid);
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Tobias Klauser6391a112006-06-08 22:23:48 -0700753 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 char *findit = str->pid;
755
756 for ( ; *findit != ' '; findit++); /* walk till we find a space */
757 /* RAID is superfluous in the context of a RAID device */
758 if (memcmp(findit-4, "RAID", 4) == 0)
759 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700760 if (((findit - str->pid) + strlen(container_types[tindex]))
761 < (sizeof(str->pid) + sizeof(str->prl)))
762 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 inqstrcpy ("V1.0", str->prl);
765}
766
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400767static void get_container_serial_callback(void *context, struct fib * fibptr)
768{
769 struct aac_get_serial_resp * get_serial_reply;
770 struct scsi_cmnd * scsicmd;
771
772 BUG_ON(fibptr == NULL);
773
774 scsicmd = (struct scsi_cmnd *) context;
775 if (!aac_valid_context(scsicmd, fibptr))
776 return;
777
778 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
779 /* Failure is irrelevant, using default value instead */
780 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
781 char sp[13];
782 /* EVPD bit set */
783 sp[0] = INQD_PDT_DA;
784 sp[1] = scsicmd->cmnd[2];
785 sp[2] = 0;
786 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
787 le32_to_cpu(get_serial_reply->uid));
788 aac_internal_transfer(scsicmd, sp, 0, sizeof(sp));
789 }
790
791 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
792
793 aac_fib_complete(fibptr);
794 aac_fib_free(fibptr);
795 scsicmd->scsi_done(scsicmd);
796}
797
798/**
799 * aac_get_container_serial - get container serial, none blocking.
800 */
801static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
802{
803 int status;
804 struct aac_get_serial *dinfo;
805 struct fib * cmd_fibcontext;
806 struct aac_dev * dev;
807
808 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
809
810 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
811 return -ENOMEM;
812
813 aac_fib_init(cmd_fibcontext);
814 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
815
816 dinfo->command = cpu_to_le32(VM_ContainerConfig);
817 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
818 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
819
820 status = aac_fib_send(ContainerCommand,
821 cmd_fibcontext,
822 sizeof (struct aac_get_serial),
823 FsaNormal,
824 0, 1,
825 (fib_callback) get_container_serial_callback,
826 (void *) scsicmd);
827
828 /*
829 * Check that the command queued to the controller
830 */
831 if (status == -EINPROGRESS) {
832 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
833 return 0;
834 }
835
836 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
837 aac_fib_complete(cmd_fibcontext);
838 aac_fib_free(cmd_fibcontext);
839 return -1;
840}
841
842/* Function: setinqserial
843 *
844 * Arguments: [1] pointer to void [1] int
845 *
846 * Purpose: Sets SCSI Unit Serial number.
847 * This is a fake. We should read a proper
848 * serial number from the container. <SuSE>But
849 * without docs it's quite hard to do it :-)
850 * So this will have to do in the meantime.</SuSE>
851 */
852
853static int setinqserial(struct aac_dev *dev, void *data, int cid)
854{
855 /*
856 * This breaks array migration.
857 */
858 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
859 le32_to_cpu(dev->adapter_info.serial[0]), cid);
860}
861
Adrian Bunk 48338692005-04-25 19:45:58 -0700862static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
863 u8 a_sense_code, u8 incorrect_length,
864 u8 bit_pointer, u16 field_pointer,
865 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
868 sense_buf[1] = 0; /* Segment number, always zero */
869
870 if (incorrect_length) {
871 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
872 sense_buf[3] = BYTE3(residue);
873 sense_buf[4] = BYTE2(residue);
874 sense_buf[5] = BYTE1(residue);
875 sense_buf[6] = BYTE0(residue);
876 } else
877 sense_buf[2] = sense_key; /* Sense key */
878
879 if (sense_key == ILLEGAL_REQUEST)
880 sense_buf[7] = 10; /* Additional sense length */
881 else
882 sense_buf[7] = 6; /* Additional sense length */
883
884 sense_buf[12] = sense_code; /* Additional sense code */
885 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
886 if (sense_key == ILLEGAL_REQUEST) {
887 sense_buf[15] = 0;
888
889 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
890 sense_buf[15] = 0x80;/* Std sense key specific field */
891 /* Illegal parameter is in the parameter block */
892
893 if (sense_code == SENCODE_INVALID_CDB_FIELD)
894 sense_buf[15] = 0xc0;/* Std sense key specific field */
895 /* Illegal parameter is in the CDB block */
896 sense_buf[15] |= bit_pointer;
897 sense_buf[16] = field_pointer >> 8; /* MSB */
898 sense_buf[17] = field_pointer; /* LSB */
899 }
900}
901
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800902static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
903{
904 if (lba & 0xffffffff00000000LL) {
905 int cid = scmd_id(cmd);
906 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
907 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
908 SAM_STAT_CHECK_CONDITION;
909 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
910 HARDWARE_ERROR,
911 SENCODE_INTERNAL_TARGET_FAILURE,
912 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
913 0, 0);
914 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900915 (sizeof(dev->fsa_dev[cid].sense_data) > SCSI_SENSE_BUFFERSIZE)
916 ? SCSI_SENSE_BUFFERSIZE
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800917 : sizeof(dev->fsa_dev[cid].sense_data));
918 cmd->scsi_done(cmd);
919 return 1;
920 }
921 return 0;
922}
923
924static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
925{
926 return 0;
927}
928
929static void io_callback(void *context, struct fib * fibptr);
930
931static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
932{
933 u16 fibsize;
934 struct aac_raw_io *readcmd;
935 aac_fib_init(fib);
936 readcmd = (struct aac_raw_io *) fib_data(fib);
937 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
938 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
939 readcmd->count = cpu_to_le32(count<<9);
940 readcmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -0400941 readcmd->flags = cpu_to_le16(IO_TYPE_READ);
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800942 readcmd->bpTotal = 0;
943 readcmd->bpComplete = 0;
944
945 aac_build_sgraw(cmd, &readcmd->sg);
946 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
947 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
948 /*
949 * Now send the Fib to the adapter
950 */
951 return aac_fib_send(ContainerRawIo,
952 fib,
953 fibsize,
954 FsaNormal,
955 0, 1,
956 (fib_callback) io_callback,
957 (void *) cmd);
958}
959
960static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
961{
962 u16 fibsize;
963 struct aac_read64 *readcmd;
964 aac_fib_init(fib);
965 readcmd = (struct aac_read64 *) fib_data(fib);
966 readcmd->command = cpu_to_le32(VM_CtHostRead64);
967 readcmd->cid = cpu_to_le16(scmd_id(cmd));
968 readcmd->sector_count = cpu_to_le16(count);
969 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
970 readcmd->pad = 0;
971 readcmd->flags = 0;
972
973 aac_build_sg64(cmd, &readcmd->sg);
974 fibsize = sizeof(struct aac_read64) +
975 ((le32_to_cpu(readcmd->sg.count) - 1) *
976 sizeof (struct sgentry64));
977 BUG_ON (fibsize > (fib->dev->max_fib_size -
978 sizeof(struct aac_fibhdr)));
979 /*
980 * Now send the Fib to the adapter
981 */
982 return aac_fib_send(ContainerCommand64,
983 fib,
984 fibsize,
985 FsaNormal,
986 0, 1,
987 (fib_callback) io_callback,
988 (void *) cmd);
989}
990
991static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
992{
993 u16 fibsize;
994 struct aac_read *readcmd;
995 aac_fib_init(fib);
996 readcmd = (struct aac_read *) fib_data(fib);
997 readcmd->command = cpu_to_le32(VM_CtBlockRead);
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000998 readcmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800999 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1000 readcmd->count = cpu_to_le32(count * 512);
1001
1002 aac_build_sg(cmd, &readcmd->sg);
1003 fibsize = sizeof(struct aac_read) +
1004 ((le32_to_cpu(readcmd->sg.count) - 1) *
1005 sizeof (struct sgentry));
1006 BUG_ON (fibsize > (fib->dev->max_fib_size -
1007 sizeof(struct aac_fibhdr)));
1008 /*
1009 * Now send the Fib to the adapter
1010 */
1011 return aac_fib_send(ContainerCommand,
1012 fib,
1013 fibsize,
1014 FsaNormal,
1015 0, 1,
1016 (fib_callback) io_callback,
1017 (void *) cmd);
1018}
1019
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001020static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001021{
1022 u16 fibsize;
1023 struct aac_raw_io *writecmd;
1024 aac_fib_init(fib);
1025 writecmd = (struct aac_raw_io *) fib_data(fib);
1026 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1027 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1028 writecmd->count = cpu_to_le32(count<<9);
1029 writecmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001030 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1031 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001032 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
1033 cpu_to_le16(IO_TYPE_WRITE);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001034 writecmd->bpTotal = 0;
1035 writecmd->bpComplete = 0;
1036
1037 aac_build_sgraw(cmd, &writecmd->sg);
1038 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1039 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1040 /*
1041 * Now send the Fib to the adapter
1042 */
1043 return aac_fib_send(ContainerRawIo,
1044 fib,
1045 fibsize,
1046 FsaNormal,
1047 0, 1,
1048 (fib_callback) io_callback,
1049 (void *) cmd);
1050}
1051
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001052static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001053{
1054 u16 fibsize;
1055 struct aac_write64 *writecmd;
1056 aac_fib_init(fib);
1057 writecmd = (struct aac_write64 *) fib_data(fib);
1058 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1059 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1060 writecmd->sector_count = cpu_to_le16(count);
1061 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1062 writecmd->pad = 0;
1063 writecmd->flags = 0;
1064
1065 aac_build_sg64(cmd, &writecmd->sg);
1066 fibsize = sizeof(struct aac_write64) +
1067 ((le32_to_cpu(writecmd->sg.count) - 1) *
1068 sizeof (struct sgentry64));
1069 BUG_ON (fibsize > (fib->dev->max_fib_size -
1070 sizeof(struct aac_fibhdr)));
1071 /*
1072 * Now send the Fib to the adapter
1073 */
1074 return aac_fib_send(ContainerCommand64,
1075 fib,
1076 fibsize,
1077 FsaNormal,
1078 0, 1,
1079 (fib_callback) io_callback,
1080 (void *) cmd);
1081}
1082
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001083static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001084{
1085 u16 fibsize;
1086 struct aac_write *writecmd;
1087 aac_fib_init(fib);
1088 writecmd = (struct aac_write *) fib_data(fib);
1089 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001090 writecmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001091 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1092 writecmd->count = cpu_to_le32(count * 512);
1093 writecmd->sg.count = cpu_to_le32(1);
1094 /* ->stable is not used - it did mean which type of write */
1095
1096 aac_build_sg(cmd, &writecmd->sg);
1097 fibsize = sizeof(struct aac_write) +
1098 ((le32_to_cpu(writecmd->sg.count) - 1) *
1099 sizeof (struct sgentry));
1100 BUG_ON (fibsize > (fib->dev->max_fib_size -
1101 sizeof(struct aac_fibhdr)));
1102 /*
1103 * Now send the Fib to the adapter
1104 */
1105 return aac_fib_send(ContainerCommand,
1106 fib,
1107 fibsize,
1108 FsaNormal,
1109 0, 1,
1110 (fib_callback) io_callback,
1111 (void *) cmd);
1112}
1113
1114static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1115{
1116 struct aac_srb * srbcmd;
1117 u32 flag;
1118 u32 timeout;
1119
1120 aac_fib_init(fib);
1121 switch(cmd->sc_data_direction){
1122 case DMA_TO_DEVICE:
1123 flag = SRB_DataOut;
1124 break;
1125 case DMA_BIDIRECTIONAL:
1126 flag = SRB_DataIn | SRB_DataOut;
1127 break;
1128 case DMA_FROM_DEVICE:
1129 flag = SRB_DataIn;
1130 break;
1131 case DMA_NONE:
1132 default: /* shuts up some versions of gcc */
1133 flag = SRB_NoDataXfer;
1134 break;
1135 }
1136
1137 srbcmd = (struct aac_srb*) fib_data(fib);
1138 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1139 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1140 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1141 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1142 srbcmd->flags = cpu_to_le32(flag);
1143 timeout = cmd->timeout_per_command/HZ;
1144 if (timeout == 0)
1145 timeout = 1;
1146 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1147 srbcmd->retry_limit = 0; /* Obsolete parameter */
1148 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1149 return srbcmd;
1150}
1151
1152static void aac_srb_callback(void *context, struct fib * fibptr);
1153
1154static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1155{
1156 u16 fibsize;
1157 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1158
1159 aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001160 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001161
1162 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1163 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1164 /*
1165 * Build Scatter/Gather list
1166 */
1167 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1168 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1169 sizeof (struct sgentry64));
1170 BUG_ON (fibsize > (fib->dev->max_fib_size -
1171 sizeof(struct aac_fibhdr)));
1172
1173 /*
1174 * Now send the Fib to the adapter
1175 */
1176 return aac_fib_send(ScsiPortCommand64, fib,
1177 fibsize, FsaNormal, 0, 1,
1178 (fib_callback) aac_srb_callback,
1179 (void *) cmd);
1180}
1181
1182static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1183{
1184 u16 fibsize;
1185 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1186
1187 aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001188 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001189
1190 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1191 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1192 /*
1193 * Build Scatter/Gather list
1194 */
1195 fibsize = sizeof (struct aac_srb) +
1196 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1197 sizeof (struct sgentry));
1198 BUG_ON (fibsize > (fib->dev->max_fib_size -
1199 sizeof(struct aac_fibhdr)));
1200
1201 /*
1202 * Now send the Fib to the adapter
1203 */
1204 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1205 (fib_callback) aac_srb_callback, (void *) cmd);
1206}
1207
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001208static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1209{
1210 if ((sizeof(dma_addr_t) > 4) &&
1211 (num_physpages > (0xFFFFFFFFULL >> PAGE_SHIFT)) &&
1212 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1213 return FAILED;
1214 return aac_scsi_32(fib, cmd);
1215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217int aac_get_adapter_info(struct aac_dev* dev)
1218{
1219 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int rcode;
1221 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001222 struct aac_adapter_info *info;
1223 struct aac_bus_info *command;
1224 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001225
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001226 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -ENOMEM;
1228
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001229 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001230 info = (struct aac_adapter_info *) fib_data(fibptr);
1231 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001233 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001234 fibptr,
1235 sizeof(*info),
1236 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001237 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001238 NULL,
1239 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001241 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001242 aac_fib_complete(fibptr);
1243 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001244 return rcode;
1245 }
1246 memcpy(&dev->adapter_info, info, sizeof(*info));
1247
1248 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001249 struct aac_supplement_adapter_info * sinfo;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001250
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001251 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001252
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001253 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001254
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001255 memset(sinfo,0,sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001256
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001257 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001258 fibptr,
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001259 sizeof(*sinfo),
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001260 FsaNormal,
1261 1, 1,
1262 NULL,
1263 NULL);
1264
1265 if (rcode >= 0)
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001266 memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Mark Haverkamp84971732005-06-20 11:55:24 -07001269
1270 /*
1271 * GetBusInfo
1272 */
1273
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001274 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001275
1276 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1277
1278 memset(bus_info, 0, sizeof(*bus_info));
1279
1280 command = (struct aac_bus_info *)bus_info;
1281
1282 command->Command = cpu_to_le32(VM_Ioctl);
1283 command->ObjType = cpu_to_le32(FT_DRIVE);
1284 command->MethodId = cpu_to_le32(1);
1285 command->CtlCmd = cpu_to_le32(GetBusInfo);
1286
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001287 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001288 fibptr,
1289 sizeof (*bus_info),
1290 FsaNormal,
1291 1, 1,
1292 NULL, NULL);
1293
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001294 /* reasoned default */
1295 dev->maximum_num_physicals = 16;
Mark Haverkamp84971732005-06-20 11:55:24 -07001296 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1297 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1298 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1299 }
1300
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001301 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001302 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001303 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1304 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 dev->name,
1306 dev->id,
1307 tmp>>24,
1308 (tmp>>16)&0xff,
1309 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001310 le32_to_cpu(dev->adapter_info.kernelbuild),
1311 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1312 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001313 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1314 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 dev->name, dev->id,
1316 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1317 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001318 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1319 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 dev->name, dev->id,
1321 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1322 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001323 buffer[0] = '\0';
1324 if (aac_show_serial_number(
1325 shost_to_class(dev->scsi_host_ptr), buffer))
1326 printk(KERN_INFO "%s%d: serial %s",
1327 dev->name, dev->id, buffer);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001328 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1329 printk(KERN_INFO "%s%d: TSID %.*s\n",
1330 dev->name, dev->id,
1331 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1332 dev->supplement_adapter_info.VpdInfo.Tsid);
1333 }
Salyzyn, Marka3940da2008-01-08 12:48:25 -08001334 if (!aac_check_reset || ((aac_check_reset != 1) &&
1335 (dev->supplement_adapter_info.SupportedOptions2 &
1336 AAC_OPTION_IGNORE_RESET))) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001337 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1338 dev->name, dev->id);
1339 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001342 dev->cache_protected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 dev->nondasd_support = 0;
1344 dev->raid_scsi_mode = 0;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001345 if(dev->adapter_info.options & AAC_OPT_NONDASD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 dev->nondasd_support = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /*
1349 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1350 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1351 * force nondasd support on. If we decide to allow the non-dasd flag
1352 * additional changes changes will have to be made to support
1353 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1354 * changed to support the new dev->raid_scsi_mode flag instead of
1355 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1356 * function aac_detect will have to be modified where it sets up the
1357 * max number of channels based on the aac->nondasd_support flag only.
1358 */
1359 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1360 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1361 dev->nondasd_support = 1;
1362 dev->raid_scsi_mode = 1;
1363 }
1364 if (dev->raid_scsi_mode != 0)
1365 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1366 dev->name, dev->id);
1367
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001368 if (nondasd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 dev->nondasd_support = (nondasd!=0);
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001370 if(dev->nondasd_support != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1372 }
1373
1374 dev->dac_support = 0;
1375 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1376 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1377 dev->dac_support = 1;
1378 }
1379
1380 if(dacmode != -1) {
1381 dev->dac_support = (dacmode!=0);
1382 }
1383 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -05001384 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1385 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1387 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -05001388 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1389 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1391 dev->name, dev->id);
1392 dev->dac_support = 0;
1393 } else {
1394 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1395 dev->name, dev->id);
1396 rcode = -ENOMEM;
1397 }
1398 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001399 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001400 * Deal with configuring for the individualized limits of each packet
1401 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001402 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001403 dev->a_ops.adapter_scsi = (dev->dac_support)
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001404 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1405 ? aac_scsi_32_64
1406 : aac_scsi_64)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001407 : aac_scsi_32;
1408 if (dev->raw_io_interface) {
1409 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1410 ? aac_bounds_64
1411 : aac_bounds_32;
1412 dev->a_ops.adapter_read = aac_read_raw_io;
1413 dev->a_ops.adapter_write = aac_write_raw_io;
1414 } else {
1415 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001416 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001417 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001418 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1419 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001420 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001421 dev->a_ops.adapter_read = aac_read_block64;
1422 dev->a_ops.adapter_write = aac_write_block64;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001423 /*
1424 * 38 scatter gather elements
1425 */
1426 dev->scsi_host_ptr->sg_tablesize =
1427 (dev->max_fib_size -
1428 sizeof(struct aac_fibhdr) -
1429 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001430 sizeof(struct sgentry64)) /
1431 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001432 } else {
1433 dev->a_ops.adapter_read = aac_read_block;
1434 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001435 }
1436 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1437 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1438 /*
1439 * Worst case size that could cause sg overflow when
1440 * we break up SG elements that are larger than 64KB.
1441 * Would be nice if we could tell the SCSI layer what
1442 * the maximum SG element size can be. Worst case is
1443 * (sg_tablesize-1) 4KB elements with one 64KB
1444 * element.
1445 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1446 */
1447 dev->scsi_host_ptr->max_sectors =
1448 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1449 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001452 aac_fib_complete(fibptr);
1453 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 return rcode;
1456}
1457
1458
Mark Haverkampe53cb352005-08-03 15:39:09 -07001459static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct aac_dev *dev;
1462 struct aac_read_reply *readreply;
1463 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 u32 cid;
1465
1466 scsicmd = (struct scsi_cmnd *) context;
1467
Mark Haverkamp03d44332007-03-15 10:27:45 -07001468 if (!aac_valid_context(scsicmd, fibptr))
1469 return;
1470
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001471 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001472 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001474 if (nblank(dprintk(x))) {
1475 u64 lba;
1476 switch (scsicmd->cmnd[0]) {
1477 case WRITE_6:
1478 case READ_6:
1479 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1480 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1481 break;
1482 case WRITE_16:
1483 case READ_16:
1484 lba = ((u64)scsicmd->cmnd[2] << 56) |
1485 ((u64)scsicmd->cmnd[3] << 48) |
1486 ((u64)scsicmd->cmnd[4] << 40) |
1487 ((u64)scsicmd->cmnd[5] << 32) |
1488 ((u64)scsicmd->cmnd[6] << 24) |
1489 (scsicmd->cmnd[7] << 16) |
1490 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1491 break;
1492 case WRITE_12:
1493 case READ_12:
1494 lba = ((u64)scsicmd->cmnd[2] << 24) |
1495 (scsicmd->cmnd[3] << 16) |
1496 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1497 break;
1498 default:
1499 lba = ((u64)scsicmd->cmnd[2] << 24) |
1500 (scsicmd->cmnd[3] << 16) |
1501 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1502 break;
1503 }
1504 printk(KERN_DEBUG
1505 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1506 smp_processor_id(), (unsigned long long)lba, jiffies);
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001509 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001510
1511 scsi_dma_unmap(scsicmd);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 readreply = (struct aac_read_reply *)fib_data(fibptr);
1514 if (le32_to_cpu(readreply->status) == ST_OK)
1515 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1516 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001517#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001518 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001519 le32_to_cpu(readreply->status));
1520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1522 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1523 HARDWARE_ERROR,
1524 SENCODE_INTERNAL_TARGET_FAILURE,
1525 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1526 0, 0);
1527 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001528 (sizeof(dev->fsa_dev[cid].sense_data) > SCSI_SENSE_BUFFERSIZE)
1529 ? SCSI_SENSE_BUFFERSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 : sizeof(dev->fsa_dev[cid].sense_data));
1531 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001532 aac_fib_complete(fibptr);
1533 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001535 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001538static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001540 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 u32 count;
1542 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 struct aac_dev *dev;
1544 struct fib * cmd_fibcontext;
1545
1546 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1547 /*
1548 * Get block address and transfer length
1549 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001550 switch (scsicmd->cmnd[0]) {
1551 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001552 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001554 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1555 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 count = scsicmd->cmnd[4];
1557
1558 if (count == 0)
1559 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001560 break;
1561 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001562 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001563
1564 lba = ((u64)scsicmd->cmnd[2] << 56) |
1565 ((u64)scsicmd->cmnd[3] << 48) |
1566 ((u64)scsicmd->cmnd[4] << 40) |
1567 ((u64)scsicmd->cmnd[5] << 32) |
1568 ((u64)scsicmd->cmnd[6] << 24) |
1569 (scsicmd->cmnd[7] << 16) |
1570 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1571 count = (scsicmd->cmnd[10] << 24) |
1572 (scsicmd->cmnd[11] << 16) |
1573 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1574 break;
1575 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001576 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001577
1578 lba = ((u64)scsicmd->cmnd[2] << 24) |
1579 (scsicmd->cmnd[3] << 16) |
1580 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1581 count = (scsicmd->cmnd[6] << 24) |
1582 (scsicmd->cmnd[7] << 16) |
1583 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1584 break;
1585 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001586 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001588 lba = ((u64)scsicmd->cmnd[2] << 24) |
1589 (scsicmd->cmnd[3] << 16) |
1590 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001594 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001595 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001596 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /*
1599 * Alocate and initialize a Fib
1600 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001601 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return -1;
1603 }
1604
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001605 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /*
1608 * Check that the command queued to the controller
1609 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001610 if (status == -EINPROGRESS) {
1611 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001615 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /*
1617 * For some reason, the Fib didn't queue, return QUEUE_FULL
1618 */
1619 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001620 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001621 aac_fib_complete(cmd_fibcontext);
1622 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
1624}
1625
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001626static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001628 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001630 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct aac_dev *dev;
1633 struct fib * cmd_fibcontext;
1634
1635 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1636 /*
1637 * Get block address and transfer length
1638 */
1639 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1640 {
1641 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1642 count = scsicmd->cmnd[4];
1643 if (count == 0)
1644 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001645 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001646 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001647 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001648
1649 lba = ((u64)scsicmd->cmnd[2] << 56) |
1650 ((u64)scsicmd->cmnd[3] << 48) |
1651 ((u64)scsicmd->cmnd[4] << 40) |
1652 ((u64)scsicmd->cmnd[5] << 32) |
1653 ((u64)scsicmd->cmnd[6] << 24) |
1654 (scsicmd->cmnd[7] << 16) |
1655 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1656 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1657 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001658 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001659 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001660 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001661
1662 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1663 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1664 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1665 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001666 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001668 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001669 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001671 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001673 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001675 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
1678 * Allocate and initialize a Fib then setup a BlockWrite command
1679 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001680 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001682 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
1684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001686 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /*
1689 * Check that the command queued to the controller
1690 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001691 if (status == -EINPROGRESS) {
1692 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return 0;
1694 }
1695
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001696 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * For some reason, the Fib didn't queue, return QUEUE_FULL
1699 */
1700 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001701 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001703 aac_fib_complete(cmd_fibcontext);
1704 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return 0;
1706}
1707
1708static void synchronize_callback(void *context, struct fib *fibptr)
1709{
1710 struct aac_synchronize_reply *synchronizereply;
1711 struct scsi_cmnd *cmd;
1712
1713 cmd = context;
1714
Mark Haverkamp03d44332007-03-15 10:27:45 -07001715 if (!aac_valid_context(cmd, fibptr))
1716 return;
1717
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001718 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 smp_processor_id(), jiffies));
1720 BUG_ON(fibptr == NULL);
1721
1722
1723 synchronizereply = fib_data(fibptr);
1724 if (le32_to_cpu(synchronizereply->status) == CT_OK)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001725 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1727 else {
1728 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001729 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001730 u32 cid = sdev_id(sdev);
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001731 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 "synchronize_callback: synchronize failed, status = %d\n",
1733 le32_to_cpu(synchronizereply->status));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001734 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1736 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1737 HARDWARE_ERROR,
1738 SENCODE_INTERNAL_TARGET_FAILURE,
1739 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1740 0, 0);
1741 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001742 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1743 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001746 aac_fib_complete(fibptr);
1747 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001748 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001751static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 int status;
1754 struct fib *cmd_fibcontext;
1755 struct aac_synchronize *synchronizecmd;
1756 struct scsi_cmnd *cmd;
1757 struct scsi_device *sdev = scsicmd->device;
1758 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001759 struct aac_dev *aac;
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001760 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
1761 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1762 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 unsigned long flags;
1764
1765 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001766 * Wait for all outstanding queued commands to complete to this
1767 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
1769 spin_lock_irqsave(&sdev->list_lock, flags);
1770 list_for_each_entry(cmd, &sdev->cmd_list, list)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001771 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1772 u64 cmnd_lba;
1773 u32 cmnd_count;
1774
1775 if (cmd->cmnd[0] == WRITE_6) {
1776 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
1777 (cmd->cmnd[2] << 8) |
1778 cmd->cmnd[3];
1779 cmnd_count = cmd->cmnd[4];
1780 if (cmnd_count == 0)
1781 cmnd_count = 256;
1782 } else if (cmd->cmnd[0] == WRITE_16) {
1783 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
1784 ((u64)cmd->cmnd[3] << 48) |
1785 ((u64)cmd->cmnd[4] << 40) |
1786 ((u64)cmd->cmnd[5] << 32) |
1787 ((u64)cmd->cmnd[6] << 24) |
1788 (cmd->cmnd[7] << 16) |
1789 (cmd->cmnd[8] << 8) |
1790 cmd->cmnd[9];
1791 cmnd_count = (cmd->cmnd[10] << 24) |
1792 (cmd->cmnd[11] << 16) |
1793 (cmd->cmnd[12] << 8) |
1794 cmd->cmnd[13];
1795 } else if (cmd->cmnd[0] == WRITE_12) {
1796 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1797 (cmd->cmnd[3] << 16) |
1798 (cmd->cmnd[4] << 8) |
1799 cmd->cmnd[5];
1800 cmnd_count = (cmd->cmnd[6] << 24) |
1801 (cmd->cmnd[7] << 16) |
1802 (cmd->cmnd[8] << 8) |
1803 cmd->cmnd[9];
1804 } else if (cmd->cmnd[0] == WRITE_10) {
1805 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1806 (cmd->cmnd[3] << 16) |
1807 (cmd->cmnd[4] << 8) |
1808 cmd->cmnd[5];
1809 cmnd_count = (cmd->cmnd[7] << 8) |
1810 cmd->cmnd[8];
1811 } else
1812 continue;
1813 if (((cmnd_lba + cmnd_count) < lba) ||
1814 (count && ((lba + count) < cmnd_lba)))
1815 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 ++active;
1817 break;
1818 }
1819
1820 spin_unlock_irqrestore(&sdev->list_lock, flags);
1821
1822 /*
1823 * Yield the processor (requeue for later)
1824 */
1825 if (active)
1826 return SCSI_MLQUEUE_DEVICE_BUSY;
1827
Salyzyn, Markf8583172007-10-30 15:50:49 -04001828 aac = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001829 if (aac->in_reset)
1830 return SCSI_MLQUEUE_HOST_BUSY;
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001833 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001835 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return SCSI_MLQUEUE_HOST_BUSY;
1837
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001838 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 synchronizecmd = fib_data(cmd_fibcontext);
1841 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1842 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001843 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001844 synchronizecmd->count =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1846
1847 /*
1848 * Now send the Fib to the adapter
1849 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001850 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 cmd_fibcontext,
1852 sizeof(struct aac_synchronize),
1853 FsaNormal,
1854 0, 1,
1855 (fib_callback)synchronize_callback,
1856 (void *)scsicmd);
1857
1858 /*
1859 * Check that the command queued to the controller
1860 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001861 if (status == -EINPROGRESS) {
1862 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001866 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001867 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1868 aac_fib_complete(cmd_fibcontext);
1869 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return SCSI_MLQUEUE_HOST_BUSY;
1871}
1872
1873/**
1874 * aac_scsi_cmd() - Process SCSI command
1875 * @scsicmd: SCSI command block
1876 *
1877 * Emulate a SCSI command and queue the required request for the
1878 * aacraid firmware.
1879 */
1880
1881int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1882{
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001883 u32 cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct Scsi_Host *host = scsicmd->device->host;
1885 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1886 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001888 if (fsa_dev_ptr == NULL)
1889 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /*
1891 * If the bus, id or lun is out of range, return fail
1892 * Test does not apply to ID 16, the pseudo id for the controller
1893 * itself.
1894 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001895 cid = scmd_id(scsicmd);
1896 if (cid != host->this_id) {
1897 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
1898 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08001899 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 scsicmd->result = DID_NO_CONNECT << 16;
1901 scsicmd->scsi_done(scsicmd);
1902 return 0;
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /*
1906 * If the target container doesn't exist, it may have
1907 * been newly created
1908 */
1909 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1910 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001911 case SERVICE_ACTION_IN:
1912 if (!(dev->raw_io_interface) ||
1913 !(dev->raw_io_64) ||
1914 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 case INQUIRY:
1917 case READ_CAPACITY:
1918 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001919 if (dev->in_reset)
1920 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07001921 return _aac_probe_container(scsicmd,
1922 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 default:
1924 break;
1925 }
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 } else { /* check for physical non-dasd devices */
Mark Haverkamp653ba582006-09-19 08:59:43 -07001928 if ((dev->nondasd_support == 1) || expose_physicals) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001929 if (dev->in_reset)
1930 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return aac_send_srb_fib(scsicmd);
1932 } else {
1933 scsicmd->result = DID_NO_CONNECT << 16;
1934 scsicmd->scsi_done(scsicmd);
1935 return 0;
1936 }
1937 }
1938 }
1939 /*
1940 * else Command for the controller itself
1941 */
1942 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1943 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1944 {
1945 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1946 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1947 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1948 ILLEGAL_REQUEST,
1949 SENCODE_INVALID_COMMAND,
1950 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1951 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001952 (sizeof(dev->fsa_dev[cid].sense_data) > SCSI_SENSE_BUFFERSIZE)
1953 ? SCSI_SENSE_BUFFERSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 : sizeof(dev->fsa_dev[cid].sense_data));
1955 scsicmd->scsi_done(scsicmd);
1956 return 0;
1957 }
1958
1959
1960 /* Handle commands here that don't really require going out to the adapter */
1961 switch (scsicmd->cmnd[0]) {
1962 case INQUIRY:
1963 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001964 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001966 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001967 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001969 if (scsicmd->cmnd[1] & 0x1 ) {
1970 char *arr = (char *)&inq_data;
1971
1972 /* EVPD bit set */
1973 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
1974 INQD_PDT_PROC : INQD_PDT_DA;
1975 if (scsicmd->cmnd[2] == 0) {
1976 /* supported vital product data pages */
1977 arr[3] = 2;
1978 arr[4] = 0x0;
1979 arr[5] = 0x80;
1980 arr[1] = scsicmd->cmnd[2];
1981 aac_internal_transfer(scsicmd, &inq_data, 0,
1982 sizeof(inq_data));
1983 scsicmd->result = DID_OK << 16 |
1984 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1985 } else if (scsicmd->cmnd[2] == 0x80) {
1986 /* unit serial number page */
1987 arr[3] = setinqserial(dev, &arr[4],
1988 scmd_id(scsicmd));
1989 arr[1] = scsicmd->cmnd[2];
1990 aac_internal_transfer(scsicmd, &inq_data, 0,
1991 sizeof(inq_data));
1992 return aac_get_container_serial(scsicmd);
1993 } else {
1994 /* vpd page not implemented */
1995 scsicmd->result = DID_OK << 16 |
1996 COMMAND_COMPLETE << 8 |
1997 SAM_STAT_CHECK_CONDITION;
1998 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1999 ILLEGAL_REQUEST,
2000 SENCODE_INVALID_CDB_FIELD,
2001 ASENCODE_NO_SENSE, 0, 7, 2, 0);
2002 memcpy(scsicmd->sense_buffer,
2003 &dev->fsa_dev[cid].sense_data,
2004 (sizeof(dev->fsa_dev[cid].sense_data) >
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002005 SCSI_SENSE_BUFFERSIZE)
2006 ? SCSI_SENSE_BUFFERSIZE
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002007 : sizeof(dev->fsa_dev[cid].sense_data));
2008 }
2009 scsicmd->scsi_done(scsicmd);
2010 return 0;
2011 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002012 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002013 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 */
2014 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002016 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /*
2018 * Set the Vendor, Product, and Revision Level
2019 * see: <vendor>.c i.e. aac.c
2020 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002021 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07002022 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002023 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
2024 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2026 scsicmd->scsi_done(scsicmd);
2027 return 0;
2028 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002029 if (dev->in_reset)
2030 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07002031 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002032 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
2033 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002034 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002036 case SERVICE_ACTION_IN:
2037 if (!(dev->raw_io_interface) ||
2038 !(dev->raw_io_64) ||
2039 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2040 break;
2041 {
2042 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002043 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002044
2045 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2046 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002047 cp[0] = (capacity >> 56) & 0xff;
2048 cp[1] = (capacity >> 48) & 0xff;
2049 cp[2] = (capacity >> 40) & 0xff;
2050 cp[3] = (capacity >> 32) & 0xff;
2051 cp[4] = (capacity >> 24) & 0xff;
2052 cp[5] = (capacity >> 16) & 0xff;
2053 cp[6] = (capacity >> 8) & 0xff;
2054 cp[7] = (capacity >> 0) & 0xff;
2055 cp[8] = 0;
2056 cp[9] = 0;
2057 cp[10] = 2;
2058 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002059 cp[12] = 0;
2060 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08002061 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002062 if (sizeof(cp) < scsicmd->cmnd[13]) {
2063 unsigned int len, offset = sizeof(cp);
2064
2065 memset(cp, 0, offset);
2066 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002067 len = min_t(size_t, scsicmd->cmnd[13] - offset,
2068 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002069 aac_internal_transfer(scsicmd, cp, offset, len);
2070 } while ((offset += len) < scsicmd->cmnd[13]);
2071 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002072
2073 /* Do not cache partition table for arrays */
2074 scsicmd->device->removable = 1;
2075
2076 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2077 scsicmd->scsi_done(scsicmd);
2078
2079 return 0;
2080 }
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 case READ_CAPACITY:
2083 {
2084 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002085 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002088 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 capacity = fsa_dev_ptr[cid].size - 1;
2090 else
2091 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 cp[0] = (capacity >> 24) & 0xff;
2094 cp[1] = (capacity >> 16) & 0xff;
2095 cp[2] = (capacity >> 8) & 0xff;
2096 cp[3] = (capacity >> 0) & 0xff;
2097 cp[4] = 0;
2098 cp[5] = 0;
2099 cp[6] = 2;
2100 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002101 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002102 /* Do not cache partition table for arrays */
2103 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2106 scsicmd->scsi_done(scsicmd);
2107
2108 return 0;
2109 }
2110
2111 case MODE_SENSE:
2112 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002113 char mode_buf[7];
2114 int mode_buf_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 mode_buf[0] = 3; /* Mode data length */
2118 mode_buf[1] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002119 mode_buf[2] = 0; /* Device-specific param,
2120 bit 8: 0/1 = write enabled/protected
2121 bit 4: 0/1 = FUA enabled */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002122 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002123 mode_buf[2] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 mode_buf[3] = 0; /* Block descriptor length */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002125 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2126 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2127 mode_buf[0] = 6;
2128 mode_buf[4] = 8;
2129 mode_buf[5] = 1;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002130 mode_buf[6] = ((aac_cache & 6) == 2)
2131 ? 0 : 0x04; /* WCE */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002132 mode_buf_length = 7;
2133 if (mode_buf_length > scsicmd->cmnd[4])
2134 mode_buf_length = scsicmd->cmnd[4];
2135 }
2136 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2138 scsicmd->scsi_done(scsicmd);
2139
2140 return 0;
2141 }
2142 case MODE_SENSE_10:
2143 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002144 char mode_buf[11];
2145 int mode_buf_length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 mode_buf[0] = 0; /* Mode data length (MSB) */
2149 mode_buf[1] = 6; /* Mode data length (LSB) */
2150 mode_buf[2] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002151 mode_buf[3] = 0; /* Device-specific param,
2152 bit 8: 0/1 = write enabled/protected
2153 bit 4: 0/1 = FUA enabled */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002154 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002155 mode_buf[3] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 mode_buf[4] = 0; /* reserved */
2157 mode_buf[5] = 0; /* reserved */
2158 mode_buf[6] = 0; /* Block descriptor length (MSB) */
2159 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002160 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2161 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2162 mode_buf[1] = 9;
2163 mode_buf[8] = 8;
2164 mode_buf[9] = 1;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002165 mode_buf[10] = ((aac_cache & 6) == 2)
2166 ? 0 : 0x04; /* WCE */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002167 mode_buf_length = 11;
2168 if (mode_buf_length > scsicmd->cmnd[8])
2169 mode_buf_length = scsicmd->cmnd[8];
2170 }
2171 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2174 scsicmd->scsi_done(scsicmd);
2175
2176 return 0;
2177 }
2178 case REQUEST_SENSE:
2179 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2180 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2181 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2182 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2183 scsicmd->scsi_done(scsicmd);
2184 return 0;
2185
2186 case ALLOW_MEDIUM_REMOVAL:
2187 dprintk((KERN_DEBUG "LOCK command.\n"));
2188 if (scsicmd->cmnd[4])
2189 fsa_dev_ptr[cid].locked = 1;
2190 else
2191 fsa_dev_ptr[cid].locked = 0;
2192
2193 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2194 scsicmd->scsi_done(scsicmd);
2195 return 0;
2196 /*
2197 * These commands are all No-Ops
2198 */
2199 case TEST_UNIT_READY:
2200 case RESERVE:
2201 case RELEASE:
2202 case REZERO_UNIT:
2203 case REASSIGN_BLOCKS:
2204 case SEEK_10:
2205 case START_STOP:
2206 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2207 scsicmd->scsi_done(scsicmd);
2208 return 0;
2209 }
2210
2211 switch (scsicmd->cmnd[0])
2212 {
2213 case READ_6:
2214 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002215 case READ_12:
2216 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002217 if (dev->in_reset)
2218 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /*
2220 * Hack to keep track of ordinal number of the device that
2221 * corresponds to a container. Needed to convert
2222 * containers to /dev/sd device names
2223 */
2224
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002225 if (scsicmd->request->rq_disk)
2226 strlcpy(fsa_dev_ptr[cid].devname,
2227 scsicmd->request->rq_disk->disk_name,
2228 min(sizeof(fsa_dev_ptr[cid].devname),
2229 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002230
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002231 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 case WRITE_6:
2234 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002235 case WRITE_12:
2236 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002237 if (dev->in_reset)
2238 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002239 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 case SYNCHRONIZE_CACHE:
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002242 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2243 scsicmd->result = DID_OK << 16 |
2244 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2245 scsicmd->scsi_done(scsicmd);
2246 return 0;
2247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /* Issue FIB to tell Firmware to flush it's cache */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002249 if ((aac_cache & 6) != 2)
2250 return aac_synchronize(scsicmd);
2251 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 default:
2253 /*
2254 * Unhandled commands
2255 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002256 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2258 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
2259 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2260 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
2261 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002262 (sizeof(dev->fsa_dev[cid].sense_data) > SCSI_SENSE_BUFFERSIZE)
2263 ? SCSI_SENSE_BUFFERSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 : sizeof(dev->fsa_dev[cid].sense_data));
2265 scsicmd->scsi_done(scsicmd);
2266 return 0;
2267 }
2268}
2269
2270static int query_disk(struct aac_dev *dev, void __user *arg)
2271{
2272 struct aac_query_disk qd;
2273 struct fsa_dev_info *fsa_dev_ptr;
2274
2275 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002276 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002277 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2279 return -EFAULT;
2280 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002281 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2283 {
2284 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2285 return -EINVAL;
2286 qd.instance = dev->scsi_host_ptr->host_no;
2287 qd.bus = 0;
2288 qd.id = CONTAINER_TO_ID(qd.cnum);
2289 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2290 }
2291 else return -EINVAL;
2292
Salyzyn, Markfd622b12007-07-17 10:59:19 -04002293 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2295 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2296
2297 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2298 qd.unmapped = 1;
2299 else
2300 qd.unmapped = 0;
2301
2302 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2303 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2304
2305 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2306 return -EFAULT;
2307 return 0;
2308}
2309
2310static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2311{
2312 struct aac_delete_disk dd;
2313 struct fsa_dev_info *fsa_dev_ptr;
2314
2315 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp651013552006-09-19 08:59:23 -07002316 if (!fsa_dev_ptr)
2317 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2320 return -EFAULT;
2321
2322 if (dd.cnum >= dev->maximum_num_containers)
2323 return -EINVAL;
2324 /*
2325 * Mark this container as being deleted.
2326 */
2327 fsa_dev_ptr[dd.cnum].deleted = 1;
2328 /*
2329 * Mark the container as no longer valid
2330 */
2331 fsa_dev_ptr[dd.cnum].valid = 0;
2332 return 0;
2333}
2334
2335static int delete_disk(struct aac_dev *dev, void __user *arg)
2336{
2337 struct aac_delete_disk dd;
2338 struct fsa_dev_info *fsa_dev_ptr;
2339
2340 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002341 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002342 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2345 return -EFAULT;
2346
2347 if (dd.cnum >= dev->maximum_num_containers)
2348 return -EINVAL;
2349 /*
2350 * If the container is locked, it can not be deleted by the API.
2351 */
2352 if (fsa_dev_ptr[dd.cnum].locked)
2353 return -EBUSY;
2354 else {
2355 /*
2356 * Mark the container as no longer being valid.
2357 */
2358 fsa_dev_ptr[dd.cnum].valid = 0;
2359 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2360 return 0;
2361 }
2362}
2363
2364int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2365{
2366 switch (cmd) {
2367 case FSACTL_QUERY_DISK:
2368 return query_disk(dev, arg);
2369 case FSACTL_DELETE_DISK:
2370 return delete_disk(dev, arg);
2371 case FSACTL_FORCE_DELETE_DISK:
2372 return force_delete_disk(dev, arg);
2373 case FSACTL_GET_CONTAINERS:
2374 return aac_get_containers(dev);
2375 default:
2376 return -ENOTTY;
2377 }
2378}
2379
2380/**
2381 *
2382 * aac_srb_callback
2383 * @context: the context set in the fib - here it is scsi cmd
2384 * @fibptr: pointer to the fib
2385 *
2386 * Handles the completion of a scsi command to a non dasd device
2387 *
2388 */
2389
2390static void aac_srb_callback(void *context, struct fib * fibptr)
2391{
2392 struct aac_dev *dev;
2393 struct aac_srb_reply *srbreply;
2394 struct scsi_cmnd *scsicmd;
2395
2396 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002397
2398 if (!aac_valid_context(scsicmd, fibptr))
2399 return;
2400
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002401 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002403 dev = fibptr->dev;
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2406
2407 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
2408 /*
2409 * Calculate resid for sg
2410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002412 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2413 - le32_to_cpu(srbreply->data_xfer_length));
2414
2415 scsi_dma_unmap(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 /*
2418 * First check the fib status
2419 */
2420
2421 if (le32_to_cpu(srbreply->status) != ST_OK){
2422 int len;
2423 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2424 len = (le32_to_cpu(srbreply->sense_data_size) >
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002425 SCSI_SENSE_BUFFERSIZE) ?
2426 SCSI_SENSE_BUFFERSIZE :
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 le32_to_cpu(srbreply->sense_data_size);
2428 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2429 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2430 }
2431
2432 /*
2433 * Next check the srb status
2434 */
2435 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2436 case SRB_STATUS_ERROR_RECOVERY:
2437 case SRB_STATUS_PENDING:
2438 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08002439 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 break;
2441 case SRB_STATUS_DATA_OVERRUN:
2442 switch(scsicmd->cmnd[0]){
2443 case READ_6:
2444 case WRITE_6:
2445 case READ_10:
2446 case WRITE_10:
2447 case READ_12:
2448 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002449 case READ_16:
2450 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2452 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2453 } else {
2454 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2455 }
2456 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2457 break;
2458 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08002459 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 break;
2461 }
2462 default:
2463 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2464 break;
2465 }
2466 break;
2467 case SRB_STATUS_ABORTED:
2468 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2469 break;
2470 case SRB_STATUS_ABORT_FAILED:
2471 // Not sure about this one - but assuming the hba was trying to abort for some reason
2472 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2473 break;
2474 case SRB_STATUS_PARITY_ERROR:
2475 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2476 break;
2477 case SRB_STATUS_NO_DEVICE:
2478 case SRB_STATUS_INVALID_PATH_ID:
2479 case SRB_STATUS_INVALID_TARGET_ID:
2480 case SRB_STATUS_INVALID_LUN:
2481 case SRB_STATUS_SELECTION_TIMEOUT:
2482 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2483 break;
2484
2485 case SRB_STATUS_COMMAND_TIMEOUT:
2486 case SRB_STATUS_TIMEOUT:
2487 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2488 break;
2489
2490 case SRB_STATUS_BUSY:
Salyzyn, Mark760af102007-06-19 11:41:21 -04002491 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 break;
2493
2494 case SRB_STATUS_BUS_RESET:
2495 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2496 break;
2497
2498 case SRB_STATUS_MESSAGE_REJECTED:
2499 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2500 break;
2501 case SRB_STATUS_REQUEST_FLUSHED:
2502 case SRB_STATUS_ERROR:
2503 case SRB_STATUS_INVALID_REQUEST:
2504 case SRB_STATUS_REQUEST_SENSE_FAILED:
2505 case SRB_STATUS_NO_HBA:
2506 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2507 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2508 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2509 case SRB_STATUS_DELAYED_RETRY:
2510 case SRB_STATUS_BAD_FUNCTION:
2511 case SRB_STATUS_NOT_STARTED:
2512 case SRB_STATUS_NOT_IN_USE:
2513 case SRB_STATUS_FORCE_ABORT:
2514 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2515 default:
2516#ifdef AAC_DETAILED_STATUS_INFO
2517 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2518 le32_to_cpu(srbreply->srb_status) & 0x3F,
2519 aac_get_status_string(
2520 le32_to_cpu(srbreply->srb_status) & 0x3F),
2521 scsicmd->cmnd[0],
2522 le32_to_cpu(srbreply->scsi_status));
2523#endif
2524 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2525 break;
2526 }
2527 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2528 int len;
2529 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2530 len = (le32_to_cpu(srbreply->sense_data_size) >
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002531 SCSI_SENSE_BUFFERSIZE) ?
2532 SCSI_SENSE_BUFFERSIZE :
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 le32_to_cpu(srbreply->sense_data_size);
2534#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002535 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2536 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537#endif
2538 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2539
2540 }
2541 /*
2542 * OR in the scsi status (already shifted up a bit)
2543 */
2544 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2545
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002546 aac_fib_complete(fibptr);
2547 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002548 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
2551/**
2552 *
2553 * aac_send_scb_fib
2554 * @scsicmd: the scsi command block
2555 *
2556 * This routine will form a FIB and fill in the aac_srb from the
2557 * scsicmd passed in.
2558 */
2559
2560static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2561{
2562 struct fib* cmd_fibcontext;
2563 struct aac_dev* dev;
2564 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Mark Haverkamp84971732005-06-20 11:55:24 -07002566 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002567 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002568 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 scsicmd->result = DID_NO_CONNECT << 16;
2570 scsicmd->scsi_done(scsicmd);
2571 return 0;
2572 }
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 /*
2575 * Allocate and initialize a Fib then setup a BlockWrite command
2576 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002577 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return -1;
2579 }
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002580 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 /*
2583 * Check that the command queued to the controller
2584 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002585 if (status == -EINPROGRESS) {
2586 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return 0;
2588 }
2589
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002590 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2591 aac_fib_complete(cmd_fibcontext);
2592 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 return -1;
2595}
2596
2597static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2598{
2599 struct aac_dev *dev;
2600 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002601 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2604 // Get rid of old data
2605 psg->count = 0;
2606 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002607 psg->sg[0].count = 0;
2608
2609 nseg = scsi_dma_map(scsicmd);
2610 BUG_ON(nseg < 0);
2611 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct scatterlist *sg;
2613 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002615 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002617 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2619 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2620 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
2622 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002623 if (byte_count > scsi_bufflen(scsicmd)) {
2624 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2625 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002626 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002627 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
2629 /* Check for command underflow */
2630 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2631 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2632 byte_count, scsicmd->underflow);
2633 }
2634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return byte_count;
2636}
2637
2638
2639static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2640{
2641 struct aac_dev *dev;
2642 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002643 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002644 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2647 // Get rid of old data
2648 psg->count = 0;
2649 psg->sg[0].addr[0] = 0;
2650 psg->sg[0].addr[1] = 0;
2651 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002652
2653 nseg = scsi_dma_map(scsicmd);
2654 BUG_ON(nseg < 0);
2655 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct scatterlist *sg;
2657 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002659 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002660 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002661 addr = sg_dma_address(sg);
2662 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2663 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002664 psg->sg[i].count = cpu_to_le32(count);
2665 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002667 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002669 if (byte_count > scsi_bufflen(scsicmd)) {
2670 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2671 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002672 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002673 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 /* Check for command underflow */
2676 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2677 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2678 byte_count, scsicmd->underflow);
2679 }
2680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return byte_count;
2682}
2683
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002684static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2685{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002686 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002687 int nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002688
2689 // Get rid of old data
2690 psg->count = 0;
2691 psg->sg[0].next = 0;
2692 psg->sg[0].prev = 0;
2693 psg->sg[0].addr[0] = 0;
2694 psg->sg[0].addr[1] = 0;
2695 psg->sg[0].count = 0;
2696 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002697
2698 nseg = scsi_dma_map(scsicmd);
2699 BUG_ON(nseg < 0);
2700 if (nseg) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002701 struct scatterlist *sg;
2702 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002703
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002704 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002705 int count = sg_dma_len(sg);
2706 u64 addr = sg_dma_address(sg);
2707 psg->sg[i].next = 0;
2708 psg->sg[i].prev = 0;
2709 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2710 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2711 psg->sg[i].count = cpu_to_le32(count);
2712 psg->sg[i].flags = 0;
2713 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002714 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002715 psg->count = cpu_to_le32(nseg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002716 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002717 if (byte_count > scsi_bufflen(scsicmd)) {
2718 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2719 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002720 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002721 byte_count = scsi_bufflen(scsicmd);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002722 }
2723 /* Check for command underflow */
2724 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2725 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2726 byte_count, scsicmd->underflow);
2727 }
2728 }
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002729 return byte_count;
2730}
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732#ifdef AAC_DETAILED_STATUS_INFO
2733
2734struct aac_srb_status_info {
2735 u32 status;
2736 char *str;
2737};
2738
2739
2740static struct aac_srb_status_info srb_status_info[] = {
2741 { SRB_STATUS_PENDING, "Pending Status"},
2742 { SRB_STATUS_SUCCESS, "Success"},
2743 { SRB_STATUS_ABORTED, "Aborted Command"},
2744 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002745 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 { SRB_STATUS_BUSY, "Device Busy"},
2747 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2748 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2749 { SRB_STATUS_NO_DEVICE, "No Device"},
2750 { SRB_STATUS_TIMEOUT, "Timeout"},
2751 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2752 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2753 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2754 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2755 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2756 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2757 { SRB_STATUS_NO_HBA, "No HBA"},
2758 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2759 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2760 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2761 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2762 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2763 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002764 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2766 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2767 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2768 { SRB_STATUS_NOT_STARTED, "Not Started"},
2769 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2770 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2771 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2772 { 0xff, "Unknown Error"}
2773};
2774
2775char *aac_get_status_string(u32 status)
2776{
2777 int i;
2778
Tobias Klauser6391a112006-06-08 22:23:48 -07002779 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2780 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 return "Bad Status Code";
2784}
2785
2786#endif