blob: 43d6aff460d2ca612b24c0bdbbfc9f9e7de8a155 [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>
Matthias Gehre910638a2006-03-28 01:56:48 -080034#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/semaphore.h>
36#include <asm/uaccess.h>
37
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
FUJITA Tomonori727eead2007-05-26 02:00:42 +0900369 kunmap_atomic(buf - sg->offset, KM_IRQ0);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700370
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static void get_container_name_callback(void *context, struct fib * fibptr)
374{
375 struct aac_get_name_resp * get_name_reply;
376 struct scsi_cmnd * scsicmd;
377
378 scsicmd = (struct scsi_cmnd *) context;
379
Mark Haverkamp03d44332007-03-15 10:27:45 -0700380 if (!aac_valid_context(scsicmd, fibptr))
381 return;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700384 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
387 /* Failure is irrelevant, using default value instead */
388 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
389 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700390 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
392 while (*sp == ' ')
393 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700394 if (*sp) {
395 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
396 int count = sizeof(d);
397 char *dp = d;
398 do {
399 *dp++ = (*sp) ? *sp++ : ' ';
400 } while (--count > 0);
401 aac_internal_transfer(scsicmd, d,
402 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
407
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800408 aac_fib_complete(fibptr);
409 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700410 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413/**
414 * aac_get_container_name - get container name, none blocking.
415 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700416static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 int status;
419 struct aac_get_name *dinfo;
420 struct fib * cmd_fibcontext;
421 struct aac_dev * dev;
422
423 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
424
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800425 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -ENOMEM;
427
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800428 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
430
431 dinfo->command = cpu_to_le32(VM_ContainerConfig);
432 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700433 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
435
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800436 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 cmd_fibcontext,
438 sizeof (struct aac_get_name),
439 FsaNormal,
440 0, 1,
441 (fib_callback) get_container_name_callback,
442 (void *) scsicmd);
443
444 /*
445 * Check that the command queued to the controller
446 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800447 if (status == -EINPROGRESS) {
448 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800452 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
453 aac_fib_complete(cmd_fibcontext);
454 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -1;
456}
457
Mark Haverkampfe76df42007-03-15 12:55:07 -0700458static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
459{
460 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
461
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400462 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700463 return aac_scsi_cmd(scsicmd);
464
465 scsicmd->result = DID_NO_CONNECT << 16;
466 scsicmd->scsi_done(scsicmd);
467 return 0;
468}
469
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400470static void _aac_probe_container2(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700471{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700472 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700473 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700474 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
475
Mark Haverkamp03d44332007-03-15 10:27:45 -0700476
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400477 if (!aac_valid_context(scsicmd, fibptr))
478 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700479
480 scsicmd->SCp.Status = 0;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400481 fsa_dev_ptr = fibptr->dev->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700482 if (fsa_dev_ptr) {
483 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
484 fsa_dev_ptr += scmd_id(scsicmd);
485
486 if ((le32_to_cpu(dresp->status) == ST_OK) &&
487 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
488 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
489 fsa_dev_ptr->valid = 1;
490 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
491 fsa_dev_ptr->size
492 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
493 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
494 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
495 }
496 if ((fsa_dev_ptr->valid & 1) == 0)
497 fsa_dev_ptr->valid = 0;
498 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
499 }
500 aac_fib_complete(fibptr);
501 aac_fib_free(fibptr);
502 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
503 scsicmd->SCp.ptr = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400504 (*callback)(scsicmd);
505 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700506}
507
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400508static void _aac_probe_container1(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700509{
510 struct scsi_cmnd * scsicmd;
511 struct aac_mount * dresp;
512 struct aac_query_mount *dinfo;
513 int status;
514
515 dresp = (struct aac_mount *) fib_data(fibptr);
516 dresp->mnt[0].capacityhigh = 0;
517 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400518 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
519 _aac_probe_container2(context, fibptr);
520 return;
521 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700522 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700523
Mark Haverkamp03d44332007-03-15 10:27:45 -0700524 if (!aac_valid_context(scsicmd, fibptr))
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400525 return;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700526
Mark Haverkampfe76df42007-03-15 12:55:07 -0700527 aac_fib_init(fibptr);
528
529 dinfo = (struct aac_query_mount *)fib_data(fibptr);
530
531 dinfo->command = cpu_to_le32(VM_NameServe64);
532 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
533 dinfo->type = cpu_to_le32(FT_FILESYS);
534
535 status = aac_fib_send(ContainerCommand,
536 fibptr,
537 sizeof(struct aac_query_mount),
538 FsaNormal,
539 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400540 _aac_probe_container2,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700541 (void *) scsicmd);
542 /*
543 * Check that the command queued to the controller
544 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400545 if (status == -EINPROGRESS)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700546 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400547 else if (status < 0) {
Mark Haverkampfe76df42007-03-15 12:55:07 -0700548 /* Inherit results from VM_NameServe, if any */
549 dresp->status = cpu_to_le32(ST_OK);
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400550 _aac_probe_container2(context, fibptr);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700551 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700552}
553
554static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
555{
556 struct fib * fibptr;
557 int status = -ENOMEM;
558
559 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
560 struct aac_query_mount *dinfo;
561
562 aac_fib_init(fibptr);
563
564 dinfo = (struct aac_query_mount *)fib_data(fibptr);
565
566 dinfo->command = cpu_to_le32(VM_NameServe);
567 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
568 dinfo->type = cpu_to_le32(FT_FILESYS);
569 scsicmd->SCp.ptr = (char *)callback;
570
571 status = aac_fib_send(ContainerCommand,
572 fibptr,
573 sizeof(struct aac_query_mount),
574 FsaNormal,
575 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400576 _aac_probe_container1,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700577 (void *) scsicmd);
578 /*
579 * Check that the command queued to the controller
580 */
581 if (status == -EINPROGRESS) {
582 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
583 return 0;
584 }
585 if (status < 0) {
586 scsicmd->SCp.ptr = NULL;
587 aac_fib_complete(fibptr);
588 aac_fib_free(fibptr);
589 }
590 }
591 if (status < 0) {
592 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
593 if (fsa_dev_ptr) {
594 fsa_dev_ptr += scmd_id(scsicmd);
595 if ((fsa_dev_ptr->valid & 1) == 0) {
596 fsa_dev_ptr->valid = 0;
597 return (*callback)(scsicmd);
598 }
599 }
600 }
601 return status;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800605 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * @dev: device to query
607 * @cid: container identifier
608 *
609 * Queries the controller about the given volume. The volume information
610 * is updated in the struct fsa_dev_info structure rather than returned.
611 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700612static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
613{
614 scsicmd->device = NULL;
615 return 0;
616}
617
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800618int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700620 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
621 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Mark Haverkampfe76df42007-03-15 12:55:07 -0700624 if (!scsicmd || !scsidev) {
625 kfree(scsicmd);
626 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700627 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700629 scsicmd->list.next = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400630 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Mark Haverkampfe76df42007-03-15 12:55:07 -0700632 scsicmd->device = scsidev;
633 scsidev->sdev_state = 0;
634 scsidev->id = cid;
635 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Mark Haverkampfe76df42007-03-15 12:55:07 -0700637 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
638 while (scsicmd->device == scsidev)
639 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400640 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700641 status = scsicmd->SCp.Status;
642 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return status;
644}
645
646/* Local Structure to set SCSI inquiry data strings */
647struct scsi_inq {
648 char vid[8]; /* Vendor ID */
649 char pid[16]; /* Product ID */
650 char prl[4]; /* Product Revision Level */
651};
652
653/**
654 * InqStrCopy - string merge
655 * @a: string to copy from
656 * @b: string to copy to
657 *
658 * Copy a String from one location to another
659 * without copying \0
660 */
661
662static void inqstrcpy(char *a, char *b)
663{
664
665 while(*a != (char)0)
666 *b++ = *a++;
667}
668
669static char *container_types[] = {
670 "None",
671 "Volume",
672 "Mirror",
673 "Stripe",
674 "RAID5",
675 "SSRW",
676 "SSRO",
677 "Morph",
678 "Legacy",
679 "RAID4",
680 "RAID10",
681 "RAID00",
682 "V-MIRRORS",
683 "PSEUDO R4",
684 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700685 "RAID5D",
686 "RAID5D0",
687 "RAID1E",
688 "RAID6",
689 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 "Unknown"
691};
692
693
694
695/* Function: setinqstr
696 *
697 * Arguments: [1] pointer to void [1] int
698 *
699 * Purpose: Sets SCSI inquiry data strings for vendor, product
700 * and revision level. Allows strings to be set in platform dependant
701 * files instead of in OS dependant driver source.
702 */
703
Mark Haverkamp794d0602005-10-24 10:51:53 -0700704static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700709 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Mark Haverkamp794d0602005-10-24 10:51:53 -0700711 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
712 char * cp = dev->supplement_adapter_info.AdapterTypeText;
713 int c = sizeof(str->vid);
714 while (*cp && *cp != ' ' && --c)
715 ++cp;
716 c = *cp;
717 *cp = '\0';
718 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
719 str->vid);
720 *cp = c;
721 while (*cp && *cp != ' ')
722 ++cp;
723 while (*cp == ' ')
724 ++cp;
725 /* last six chars reserved for vol type */
726 c = 0;
727 if (strlen(cp) > sizeof(str->pid)) {
728 c = cp[sizeof(str->pid)];
729 cp[sizeof(str->pid)] = '\0';
730 }
731 inqstrcpy (cp, str->pid);
732 if (c)
733 cp[sizeof(str->pid)] = c;
734 } else {
735 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700736
737 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700738 /* last six chars reserved for vol type */
739 inqstrcpy (mp->model, str->pid);
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Tobias Klauser6391a112006-06-08 22:23:48 -0700742 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 char *findit = str->pid;
744
745 for ( ; *findit != ' '; findit++); /* walk till we find a space */
746 /* RAID is superfluous in the context of a RAID device */
747 if (memcmp(findit-4, "RAID", 4) == 0)
748 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700749 if (((findit - str->pid) + strlen(container_types[tindex]))
750 < (sizeof(str->pid) + sizeof(str->prl)))
751 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 inqstrcpy ("V1.0", str->prl);
754}
755
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400756static void get_container_serial_callback(void *context, struct fib * fibptr)
757{
758 struct aac_get_serial_resp * get_serial_reply;
759 struct scsi_cmnd * scsicmd;
760
761 BUG_ON(fibptr == NULL);
762
763 scsicmd = (struct scsi_cmnd *) context;
764 if (!aac_valid_context(scsicmd, fibptr))
765 return;
766
767 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
768 /* Failure is irrelevant, using default value instead */
769 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
770 char sp[13];
771 /* EVPD bit set */
772 sp[0] = INQD_PDT_DA;
773 sp[1] = scsicmd->cmnd[2];
774 sp[2] = 0;
775 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
776 le32_to_cpu(get_serial_reply->uid));
777 aac_internal_transfer(scsicmd, sp, 0, sizeof(sp));
778 }
779
780 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
781
782 aac_fib_complete(fibptr);
783 aac_fib_free(fibptr);
784 scsicmd->scsi_done(scsicmd);
785}
786
787/**
788 * aac_get_container_serial - get container serial, none blocking.
789 */
790static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
791{
792 int status;
793 struct aac_get_serial *dinfo;
794 struct fib * cmd_fibcontext;
795 struct aac_dev * dev;
796
797 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
798
799 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
800 return -ENOMEM;
801
802 aac_fib_init(cmd_fibcontext);
803 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
804
805 dinfo->command = cpu_to_le32(VM_ContainerConfig);
806 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
807 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
808
809 status = aac_fib_send(ContainerCommand,
810 cmd_fibcontext,
811 sizeof (struct aac_get_serial),
812 FsaNormal,
813 0, 1,
814 (fib_callback) get_container_serial_callback,
815 (void *) scsicmd);
816
817 /*
818 * Check that the command queued to the controller
819 */
820 if (status == -EINPROGRESS) {
821 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
822 return 0;
823 }
824
825 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
826 aac_fib_complete(cmd_fibcontext);
827 aac_fib_free(cmd_fibcontext);
828 return -1;
829}
830
831/* Function: setinqserial
832 *
833 * Arguments: [1] pointer to void [1] int
834 *
835 * Purpose: Sets SCSI Unit Serial number.
836 * This is a fake. We should read a proper
837 * serial number from the container. <SuSE>But
838 * without docs it's quite hard to do it :-)
839 * So this will have to do in the meantime.</SuSE>
840 */
841
842static int setinqserial(struct aac_dev *dev, void *data, int cid)
843{
844 /*
845 * This breaks array migration.
846 */
847 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
848 le32_to_cpu(dev->adapter_info.serial[0]), cid);
849}
850
Adrian Bunk 48338692005-04-25 19:45:58 -0700851static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
852 u8 a_sense_code, u8 incorrect_length,
853 u8 bit_pointer, u16 field_pointer,
854 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
857 sense_buf[1] = 0; /* Segment number, always zero */
858
859 if (incorrect_length) {
860 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
861 sense_buf[3] = BYTE3(residue);
862 sense_buf[4] = BYTE2(residue);
863 sense_buf[5] = BYTE1(residue);
864 sense_buf[6] = BYTE0(residue);
865 } else
866 sense_buf[2] = sense_key; /* Sense key */
867
868 if (sense_key == ILLEGAL_REQUEST)
869 sense_buf[7] = 10; /* Additional sense length */
870 else
871 sense_buf[7] = 6; /* Additional sense length */
872
873 sense_buf[12] = sense_code; /* Additional sense code */
874 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
875 if (sense_key == ILLEGAL_REQUEST) {
876 sense_buf[15] = 0;
877
878 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
879 sense_buf[15] = 0x80;/* Std sense key specific field */
880 /* Illegal parameter is in the parameter block */
881
882 if (sense_code == SENCODE_INVALID_CDB_FIELD)
883 sense_buf[15] = 0xc0;/* Std sense key specific field */
884 /* Illegal parameter is in the CDB block */
885 sense_buf[15] |= bit_pointer;
886 sense_buf[16] = field_pointer >> 8; /* MSB */
887 sense_buf[17] = field_pointer; /* LSB */
888 }
889}
890
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800891static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
892{
893 if (lba & 0xffffffff00000000LL) {
894 int cid = scmd_id(cmd);
895 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
896 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
897 SAM_STAT_CHECK_CONDITION;
898 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
899 HARDWARE_ERROR,
900 SENCODE_INTERNAL_TARGET_FAILURE,
901 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
902 0, 0);
903 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
904 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(cmd->sense_buffer))
905 ? sizeof(cmd->sense_buffer)
906 : sizeof(dev->fsa_dev[cid].sense_data));
907 cmd->scsi_done(cmd);
908 return 1;
909 }
910 return 0;
911}
912
913static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
914{
915 return 0;
916}
917
918static void io_callback(void *context, struct fib * fibptr);
919
920static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
921{
922 u16 fibsize;
923 struct aac_raw_io *readcmd;
924 aac_fib_init(fib);
925 readcmd = (struct aac_raw_io *) fib_data(fib);
926 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
927 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
928 readcmd->count = cpu_to_le32(count<<9);
929 readcmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -0400930 readcmd->flags = cpu_to_le16(IO_TYPE_READ);
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800931 readcmd->bpTotal = 0;
932 readcmd->bpComplete = 0;
933
934 aac_build_sgraw(cmd, &readcmd->sg);
935 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
936 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
937 /*
938 * Now send the Fib to the adapter
939 */
940 return aac_fib_send(ContainerRawIo,
941 fib,
942 fibsize,
943 FsaNormal,
944 0, 1,
945 (fib_callback) io_callback,
946 (void *) cmd);
947}
948
949static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
950{
951 u16 fibsize;
952 struct aac_read64 *readcmd;
953 aac_fib_init(fib);
954 readcmd = (struct aac_read64 *) fib_data(fib);
955 readcmd->command = cpu_to_le32(VM_CtHostRead64);
956 readcmd->cid = cpu_to_le16(scmd_id(cmd));
957 readcmd->sector_count = cpu_to_le16(count);
958 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
959 readcmd->pad = 0;
960 readcmd->flags = 0;
961
962 aac_build_sg64(cmd, &readcmd->sg);
963 fibsize = sizeof(struct aac_read64) +
964 ((le32_to_cpu(readcmd->sg.count) - 1) *
965 sizeof (struct sgentry64));
966 BUG_ON (fibsize > (fib->dev->max_fib_size -
967 sizeof(struct aac_fibhdr)));
968 /*
969 * Now send the Fib to the adapter
970 */
971 return aac_fib_send(ContainerCommand64,
972 fib,
973 fibsize,
974 FsaNormal,
975 0, 1,
976 (fib_callback) io_callback,
977 (void *) cmd);
978}
979
980static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
981{
982 u16 fibsize;
983 struct aac_read *readcmd;
984 aac_fib_init(fib);
985 readcmd = (struct aac_read *) fib_data(fib);
986 readcmd->command = cpu_to_le32(VM_CtBlockRead);
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000987 readcmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800988 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
989 readcmd->count = cpu_to_le32(count * 512);
990
991 aac_build_sg(cmd, &readcmd->sg);
992 fibsize = sizeof(struct aac_read) +
993 ((le32_to_cpu(readcmd->sg.count) - 1) *
994 sizeof (struct sgentry));
995 BUG_ON (fibsize > (fib->dev->max_fib_size -
996 sizeof(struct aac_fibhdr)));
997 /*
998 * Now send the Fib to the adapter
999 */
1000 return aac_fib_send(ContainerCommand,
1001 fib,
1002 fibsize,
1003 FsaNormal,
1004 0, 1,
1005 (fib_callback) io_callback,
1006 (void *) cmd);
1007}
1008
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001009static 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 -08001010{
1011 u16 fibsize;
1012 struct aac_raw_io *writecmd;
1013 aac_fib_init(fib);
1014 writecmd = (struct aac_raw_io *) fib_data(fib);
1015 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1016 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1017 writecmd->count = cpu_to_le32(count<<9);
1018 writecmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001019 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1020 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001021 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
1022 cpu_to_le16(IO_TYPE_WRITE);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001023 writecmd->bpTotal = 0;
1024 writecmd->bpComplete = 0;
1025
1026 aac_build_sgraw(cmd, &writecmd->sg);
1027 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1028 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1029 /*
1030 * Now send the Fib to the adapter
1031 */
1032 return aac_fib_send(ContainerRawIo,
1033 fib,
1034 fibsize,
1035 FsaNormal,
1036 0, 1,
1037 (fib_callback) io_callback,
1038 (void *) cmd);
1039}
1040
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001041static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001042{
1043 u16 fibsize;
1044 struct aac_write64 *writecmd;
1045 aac_fib_init(fib);
1046 writecmd = (struct aac_write64 *) fib_data(fib);
1047 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1048 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1049 writecmd->sector_count = cpu_to_le16(count);
1050 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1051 writecmd->pad = 0;
1052 writecmd->flags = 0;
1053
1054 aac_build_sg64(cmd, &writecmd->sg);
1055 fibsize = sizeof(struct aac_write64) +
1056 ((le32_to_cpu(writecmd->sg.count) - 1) *
1057 sizeof (struct sgentry64));
1058 BUG_ON (fibsize > (fib->dev->max_fib_size -
1059 sizeof(struct aac_fibhdr)));
1060 /*
1061 * Now send the Fib to the adapter
1062 */
1063 return aac_fib_send(ContainerCommand64,
1064 fib,
1065 fibsize,
1066 FsaNormal,
1067 0, 1,
1068 (fib_callback) io_callback,
1069 (void *) cmd);
1070}
1071
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001072static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001073{
1074 u16 fibsize;
1075 struct aac_write *writecmd;
1076 aac_fib_init(fib);
1077 writecmd = (struct aac_write *) fib_data(fib);
1078 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001079 writecmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001080 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1081 writecmd->count = cpu_to_le32(count * 512);
1082 writecmd->sg.count = cpu_to_le32(1);
1083 /* ->stable is not used - it did mean which type of write */
1084
1085 aac_build_sg(cmd, &writecmd->sg);
1086 fibsize = sizeof(struct aac_write) +
1087 ((le32_to_cpu(writecmd->sg.count) - 1) *
1088 sizeof (struct sgentry));
1089 BUG_ON (fibsize > (fib->dev->max_fib_size -
1090 sizeof(struct aac_fibhdr)));
1091 /*
1092 * Now send the Fib to the adapter
1093 */
1094 return aac_fib_send(ContainerCommand,
1095 fib,
1096 fibsize,
1097 FsaNormal,
1098 0, 1,
1099 (fib_callback) io_callback,
1100 (void *) cmd);
1101}
1102
1103static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1104{
1105 struct aac_srb * srbcmd;
1106 u32 flag;
1107 u32 timeout;
1108
1109 aac_fib_init(fib);
1110 switch(cmd->sc_data_direction){
1111 case DMA_TO_DEVICE:
1112 flag = SRB_DataOut;
1113 break;
1114 case DMA_BIDIRECTIONAL:
1115 flag = SRB_DataIn | SRB_DataOut;
1116 break;
1117 case DMA_FROM_DEVICE:
1118 flag = SRB_DataIn;
1119 break;
1120 case DMA_NONE:
1121 default: /* shuts up some versions of gcc */
1122 flag = SRB_NoDataXfer;
1123 break;
1124 }
1125
1126 srbcmd = (struct aac_srb*) fib_data(fib);
1127 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1128 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1129 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1130 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1131 srbcmd->flags = cpu_to_le32(flag);
1132 timeout = cmd->timeout_per_command/HZ;
1133 if (timeout == 0)
1134 timeout = 1;
1135 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1136 srbcmd->retry_limit = 0; /* Obsolete parameter */
1137 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1138 return srbcmd;
1139}
1140
1141static void aac_srb_callback(void *context, struct fib * fibptr);
1142
1143static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1144{
1145 u16 fibsize;
1146 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1147
1148 aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001149 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001150
1151 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1152 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1153 /*
1154 * Build Scatter/Gather list
1155 */
1156 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1157 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1158 sizeof (struct sgentry64));
1159 BUG_ON (fibsize > (fib->dev->max_fib_size -
1160 sizeof(struct aac_fibhdr)));
1161
1162 /*
1163 * Now send the Fib to the adapter
1164 */
1165 return aac_fib_send(ScsiPortCommand64, fib,
1166 fibsize, FsaNormal, 0, 1,
1167 (fib_callback) aac_srb_callback,
1168 (void *) cmd);
1169}
1170
1171static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1172{
1173 u16 fibsize;
1174 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1175
1176 aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001177 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001178
1179 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1180 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1181 /*
1182 * Build Scatter/Gather list
1183 */
1184 fibsize = sizeof (struct aac_srb) +
1185 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1186 sizeof (struct sgentry));
1187 BUG_ON (fibsize > (fib->dev->max_fib_size -
1188 sizeof(struct aac_fibhdr)));
1189
1190 /*
1191 * Now send the Fib to the adapter
1192 */
1193 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1194 (fib_callback) aac_srb_callback, (void *) cmd);
1195}
1196
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001197static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1198{
1199 if ((sizeof(dma_addr_t) > 4) &&
1200 (num_physpages > (0xFFFFFFFFULL >> PAGE_SHIFT)) &&
1201 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1202 return FAILED;
1203 return aac_scsi_32(fib, cmd);
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206int aac_get_adapter_info(struct aac_dev* dev)
1207{
1208 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int rcode;
1210 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001211 struct aac_adapter_info *info;
1212 struct aac_bus_info *command;
1213 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001214
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001215 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return -ENOMEM;
1217
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001218 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001219 info = (struct aac_adapter_info *) fib_data(fibptr);
1220 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001222 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001223 fibptr,
1224 sizeof(*info),
1225 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001226 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001227 NULL,
1228 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001230 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001231 aac_fib_complete(fibptr);
1232 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001233 return rcode;
1234 }
1235 memcpy(&dev->adapter_info, info, sizeof(*info));
1236
1237 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1238 struct aac_supplement_adapter_info * info;
1239
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001240 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001241
1242 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1243
1244 memset(info,0,sizeof(*info));
1245
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001246 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001247 fibptr,
1248 sizeof(*info),
1249 FsaNormal,
1250 1, 1,
1251 NULL,
1252 NULL);
1253
1254 if (rcode >= 0)
1255 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Mark Haverkamp84971732005-06-20 11:55:24 -07001258
1259 /*
1260 * GetBusInfo
1261 */
1262
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001263 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001264
1265 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1266
1267 memset(bus_info, 0, sizeof(*bus_info));
1268
1269 command = (struct aac_bus_info *)bus_info;
1270
1271 command->Command = cpu_to_le32(VM_Ioctl);
1272 command->ObjType = cpu_to_le32(FT_DRIVE);
1273 command->MethodId = cpu_to_le32(1);
1274 command->CtlCmd = cpu_to_le32(GetBusInfo);
1275
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001276 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001277 fibptr,
1278 sizeof (*bus_info),
1279 FsaNormal,
1280 1, 1,
1281 NULL, NULL);
1282
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001283 /* reasoned default */
1284 dev->maximum_num_physicals = 16;
Mark Haverkamp84971732005-06-20 11:55:24 -07001285 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1286 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1287 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1288 }
1289
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001290 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001291 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001292 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1293 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 dev->name,
1295 dev->id,
1296 tmp>>24,
1297 (tmp>>16)&0xff,
1298 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001299 le32_to_cpu(dev->adapter_info.kernelbuild),
1300 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1301 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001302 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1303 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 dev->name, dev->id,
1305 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1306 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001307 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1308 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 dev->name, dev->id,
1310 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1311 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001312 buffer[0] = '\0';
1313 if (aac_show_serial_number(
1314 shost_to_class(dev->scsi_host_ptr), buffer))
1315 printk(KERN_INFO "%s%d: serial %s",
1316 dev->name, dev->id, buffer);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001317 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1318 printk(KERN_INFO "%s%d: TSID %.*s\n",
1319 dev->name, dev->id,
1320 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1321 dev->supplement_adapter_info.VpdInfo.Tsid);
1322 }
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001323 if (!aac_check_reset ||
1324 ((aac_check_reset != 1) &&
1325 (dev->supplement_adapter_info.SupportedOptions2 &
1326 cpu_to_le32(AAC_OPTION_IGNORE_RESET)))) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001327 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1328 dev->name, dev->id);
1329 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001332 dev->cache_protected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 dev->nondasd_support = 0;
1334 dev->raid_scsi_mode = 0;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001335 if(dev->adapter_info.options & AAC_OPT_NONDASD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 dev->nondasd_support = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /*
1339 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1340 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1341 * force nondasd support on. If we decide to allow the non-dasd flag
1342 * additional changes changes will have to be made to support
1343 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1344 * changed to support the new dev->raid_scsi_mode flag instead of
1345 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1346 * function aac_detect will have to be modified where it sets up the
1347 * max number of channels based on the aac->nondasd_support flag only.
1348 */
1349 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1350 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1351 dev->nondasd_support = 1;
1352 dev->raid_scsi_mode = 1;
1353 }
1354 if (dev->raid_scsi_mode != 0)
1355 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1356 dev->name, dev->id);
1357
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001358 if (nondasd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 dev->nondasd_support = (nondasd!=0);
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001360 if(dev->nondasd_support != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1362 }
1363
1364 dev->dac_support = 0;
1365 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1366 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1367 dev->dac_support = 1;
1368 }
1369
1370 if(dacmode != -1) {
1371 dev->dac_support = (dacmode!=0);
1372 }
1373 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -05001374 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1375 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1377 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -05001378 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1379 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1381 dev->name, dev->id);
1382 dev->dac_support = 0;
1383 } else {
1384 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1385 dev->name, dev->id);
1386 rcode = -ENOMEM;
1387 }
1388 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001389 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001390 * Deal with configuring for the individualized limits of each packet
1391 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001392 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001393 dev->a_ops.adapter_scsi = (dev->dac_support)
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001394 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1395 ? aac_scsi_32_64
1396 : aac_scsi_64)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001397 : aac_scsi_32;
1398 if (dev->raw_io_interface) {
1399 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1400 ? aac_bounds_64
1401 : aac_bounds_32;
1402 dev->a_ops.adapter_read = aac_read_raw_io;
1403 dev->a_ops.adapter_write = aac_write_raw_io;
1404 } else {
1405 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001406 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001407 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001408 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1409 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001410 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001411 dev->a_ops.adapter_read = aac_read_block64;
1412 dev->a_ops.adapter_write = aac_write_block64;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001413 /*
1414 * 38 scatter gather elements
1415 */
1416 dev->scsi_host_ptr->sg_tablesize =
1417 (dev->max_fib_size -
1418 sizeof(struct aac_fibhdr) -
1419 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001420 sizeof(struct sgentry64)) /
1421 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001422 } else {
1423 dev->a_ops.adapter_read = aac_read_block;
1424 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001425 }
1426 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1427 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1428 /*
1429 * Worst case size that could cause sg overflow when
1430 * we break up SG elements that are larger than 64KB.
1431 * Would be nice if we could tell the SCSI layer what
1432 * the maximum SG element size can be. Worst case is
1433 * (sg_tablesize-1) 4KB elements with one 64KB
1434 * element.
1435 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1436 */
1437 dev->scsi_host_ptr->max_sectors =
1438 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1439 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001442 aac_fib_complete(fibptr);
1443 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 return rcode;
1446}
1447
1448
Mark Haverkampe53cb352005-08-03 15:39:09 -07001449static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct aac_dev *dev;
1452 struct aac_read_reply *readreply;
1453 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 u32 cid;
1455
1456 scsicmd = (struct scsi_cmnd *) context;
1457
Mark Haverkamp03d44332007-03-15 10:27:45 -07001458 if (!aac_valid_context(scsicmd, fibptr))
1459 return;
1460
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001461 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001462 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001464 if (nblank(dprintk(x))) {
1465 u64 lba;
1466 switch (scsicmd->cmnd[0]) {
1467 case WRITE_6:
1468 case READ_6:
1469 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1470 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1471 break;
1472 case WRITE_16:
1473 case READ_16:
1474 lba = ((u64)scsicmd->cmnd[2] << 56) |
1475 ((u64)scsicmd->cmnd[3] << 48) |
1476 ((u64)scsicmd->cmnd[4] << 40) |
1477 ((u64)scsicmd->cmnd[5] << 32) |
1478 ((u64)scsicmd->cmnd[6] << 24) |
1479 (scsicmd->cmnd[7] << 16) |
1480 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1481 break;
1482 case WRITE_12:
1483 case READ_12:
1484 lba = ((u64)scsicmd->cmnd[2] << 24) |
1485 (scsicmd->cmnd[3] << 16) |
1486 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1487 break;
1488 default:
1489 lba = ((u64)scsicmd->cmnd[2] << 24) |
1490 (scsicmd->cmnd[3] << 16) |
1491 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1492 break;
1493 }
1494 printk(KERN_DEBUG
1495 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1496 smp_processor_id(), (unsigned long long)lba, jiffies);
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001499 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001500
1501 scsi_dma_unmap(scsicmd);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 readreply = (struct aac_read_reply *)fib_data(fibptr);
1504 if (le32_to_cpu(readreply->status) == ST_OK)
1505 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1506 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001507#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001508 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001509 le32_to_cpu(readreply->status));
1510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1512 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1513 HARDWARE_ERROR,
1514 SENCODE_INTERNAL_TARGET_FAILURE,
1515 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1516 0, 0);
1517 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1518 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1519 ? sizeof(scsicmd->sense_buffer)
1520 : sizeof(dev->fsa_dev[cid].sense_data));
1521 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001522 aac_fib_complete(fibptr);
1523 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001525 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001528static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001530 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 u32 count;
1532 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct aac_dev *dev;
1534 struct fib * cmd_fibcontext;
1535
1536 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1537 /*
1538 * Get block address and transfer length
1539 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001540 switch (scsicmd->cmnd[0]) {
1541 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001542 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001544 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1545 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 count = scsicmd->cmnd[4];
1547
1548 if (count == 0)
1549 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001550 break;
1551 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001552 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001553
1554 lba = ((u64)scsicmd->cmnd[2] << 56) |
1555 ((u64)scsicmd->cmnd[3] << 48) |
1556 ((u64)scsicmd->cmnd[4] << 40) |
1557 ((u64)scsicmd->cmnd[5] << 32) |
1558 ((u64)scsicmd->cmnd[6] << 24) |
1559 (scsicmd->cmnd[7] << 16) |
1560 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1561 count = (scsicmd->cmnd[10] << 24) |
1562 (scsicmd->cmnd[11] << 16) |
1563 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1564 break;
1565 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001566 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001567
1568 lba = ((u64)scsicmd->cmnd[2] << 24) |
1569 (scsicmd->cmnd[3] << 16) |
1570 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1571 count = (scsicmd->cmnd[6] << 24) |
1572 (scsicmd->cmnd[7] << 16) |
1573 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1574 break;
1575 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001576 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001578 lba = ((u64)scsicmd->cmnd[2] << 24) |
1579 (scsicmd->cmnd[3] << 16) |
1580 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001582 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001584 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001585 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001586 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Alocate and initialize a Fib
1590 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001591 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return -1;
1593 }
1594
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001595 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
1598 * Check that the command queued to the controller
1599 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001600 if (status == -EINPROGRESS) {
1601 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001605 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /*
1607 * For some reason, the Fib didn't queue, return QUEUE_FULL
1608 */
1609 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001610 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001611 aac_fib_complete(cmd_fibcontext);
1612 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
1614}
1615
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001616static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001618 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001620 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct aac_dev *dev;
1623 struct fib * cmd_fibcontext;
1624
1625 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1626 /*
1627 * Get block address and transfer length
1628 */
1629 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1630 {
1631 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1632 count = scsicmd->cmnd[4];
1633 if (count == 0)
1634 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001635 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001636 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001637 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001638
1639 lba = ((u64)scsicmd->cmnd[2] << 56) |
1640 ((u64)scsicmd->cmnd[3] << 48) |
1641 ((u64)scsicmd->cmnd[4] << 40) |
1642 ((u64)scsicmd->cmnd[5] << 32) |
1643 ((u64)scsicmd->cmnd[6] << 24) |
1644 (scsicmd->cmnd[7] << 16) |
1645 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1646 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1647 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001648 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001649 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001650 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001651
1652 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1653 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1654 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1655 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001656 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001658 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001659 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001661 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001663 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001665 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /*
1668 * Allocate and initialize a Fib then setup a BlockWrite command
1669 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001670 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001672 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return 0;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001676 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 /*
1679 * Check that the command queued to the controller
1680 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001681 if (status == -EINPROGRESS) {
1682 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
1684 }
1685
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001686 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
1688 * For some reason, the Fib didn't queue, return QUEUE_FULL
1689 */
1690 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001691 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001693 aac_fib_complete(cmd_fibcontext);
1694 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696}
1697
1698static void synchronize_callback(void *context, struct fib *fibptr)
1699{
1700 struct aac_synchronize_reply *synchronizereply;
1701 struct scsi_cmnd *cmd;
1702
1703 cmd = context;
1704
Mark Haverkamp03d44332007-03-15 10:27:45 -07001705 if (!aac_valid_context(cmd, fibptr))
1706 return;
1707
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001708 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 smp_processor_id(), jiffies));
1710 BUG_ON(fibptr == NULL);
1711
1712
1713 synchronizereply = fib_data(fibptr);
1714 if (le32_to_cpu(synchronizereply->status) == CT_OK)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001715 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1717 else {
1718 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001719 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001720 u32 cid = sdev_id(sdev);
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001721 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 "synchronize_callback: synchronize failed, status = %d\n",
1723 le32_to_cpu(synchronizereply->status));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001724 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1726 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1727 HARDWARE_ERROR,
1728 SENCODE_INTERNAL_TARGET_FAILURE,
1729 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1730 0, 0);
1731 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001732 min(sizeof(dev->fsa_dev[cid].sense_data),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 sizeof(cmd->sense_buffer)));
1734 }
1735
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001736 aac_fib_complete(fibptr);
1737 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001738 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001741static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 int status;
1744 struct fib *cmd_fibcontext;
1745 struct aac_synchronize *synchronizecmd;
1746 struct scsi_cmnd *cmd;
1747 struct scsi_device *sdev = scsicmd->device;
1748 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001749 struct aac_dev *aac;
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001750 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
1751 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1752 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 unsigned long flags;
1754
1755 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001756 * Wait for all outstanding queued commands to complete to this
1757 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 */
1759 spin_lock_irqsave(&sdev->list_lock, flags);
1760 list_for_each_entry(cmd, &sdev->cmd_list, list)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001761 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1762 u64 cmnd_lba;
1763 u32 cmnd_count;
1764
1765 if (cmd->cmnd[0] == WRITE_6) {
1766 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
1767 (cmd->cmnd[2] << 8) |
1768 cmd->cmnd[3];
1769 cmnd_count = cmd->cmnd[4];
1770 if (cmnd_count == 0)
1771 cmnd_count = 256;
1772 } else if (cmd->cmnd[0] == WRITE_16) {
1773 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
1774 ((u64)cmd->cmnd[3] << 48) |
1775 ((u64)cmd->cmnd[4] << 40) |
1776 ((u64)cmd->cmnd[5] << 32) |
1777 ((u64)cmd->cmnd[6] << 24) |
1778 (cmd->cmnd[7] << 16) |
1779 (cmd->cmnd[8] << 8) |
1780 cmd->cmnd[9];
1781 cmnd_count = (cmd->cmnd[10] << 24) |
1782 (cmd->cmnd[11] << 16) |
1783 (cmd->cmnd[12] << 8) |
1784 cmd->cmnd[13];
1785 } else if (cmd->cmnd[0] == WRITE_12) {
1786 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1787 (cmd->cmnd[3] << 16) |
1788 (cmd->cmnd[4] << 8) |
1789 cmd->cmnd[5];
1790 cmnd_count = (cmd->cmnd[6] << 24) |
1791 (cmd->cmnd[7] << 16) |
1792 (cmd->cmnd[8] << 8) |
1793 cmd->cmnd[9];
1794 } else if (cmd->cmnd[0] == WRITE_10) {
1795 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1796 (cmd->cmnd[3] << 16) |
1797 (cmd->cmnd[4] << 8) |
1798 cmd->cmnd[5];
1799 cmnd_count = (cmd->cmnd[7] << 8) |
1800 cmd->cmnd[8];
1801 } else
1802 continue;
1803 if (((cmnd_lba + cmnd_count) < lba) ||
1804 (count && ((lba + count) < cmnd_lba)))
1805 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 ++active;
1807 break;
1808 }
1809
1810 spin_unlock_irqrestore(&sdev->list_lock, flags);
1811
1812 /*
1813 * Yield the processor (requeue for later)
1814 */
1815 if (active)
1816 return SCSI_MLQUEUE_DEVICE_BUSY;
1817
Salyzyn, Markf8583172007-10-30 15:50:49 -04001818 aac = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001819 if (aac->in_reset)
1820 return SCSI_MLQUEUE_HOST_BUSY;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001823 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001825 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return SCSI_MLQUEUE_HOST_BUSY;
1827
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001828 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 synchronizecmd = fib_data(cmd_fibcontext);
1831 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1832 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001833 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001834 synchronizecmd->count =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1836
1837 /*
1838 * Now send the Fib to the adapter
1839 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001840 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 cmd_fibcontext,
1842 sizeof(struct aac_synchronize),
1843 FsaNormal,
1844 0, 1,
1845 (fib_callback)synchronize_callback,
1846 (void *)scsicmd);
1847
1848 /*
1849 * Check that the command queued to the controller
1850 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001851 if (status == -EINPROGRESS) {
1852 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001856 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001857 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1858 aac_fib_complete(cmd_fibcontext);
1859 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return SCSI_MLQUEUE_HOST_BUSY;
1861}
1862
1863/**
1864 * aac_scsi_cmd() - Process SCSI command
1865 * @scsicmd: SCSI command block
1866 *
1867 * Emulate a SCSI command and queue the required request for the
1868 * aacraid firmware.
1869 */
1870
1871int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1872{
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001873 u32 cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 struct Scsi_Host *host = scsicmd->device->host;
1875 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1876 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001878 if (fsa_dev_ptr == NULL)
1879 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /*
1881 * If the bus, id or lun is out of range, return fail
1882 * Test does not apply to ID 16, the pseudo id for the controller
1883 * itself.
1884 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001885 cid = scmd_id(scsicmd);
1886 if (cid != host->this_id) {
1887 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
1888 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08001889 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 scsicmd->result = DID_NO_CONNECT << 16;
1891 scsicmd->scsi_done(scsicmd);
1892 return 0;
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /*
1896 * If the target container doesn't exist, it may have
1897 * been newly created
1898 */
1899 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1900 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001901 case SERVICE_ACTION_IN:
1902 if (!(dev->raw_io_interface) ||
1903 !(dev->raw_io_64) ||
1904 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 case INQUIRY:
1907 case READ_CAPACITY:
1908 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001909 if (dev->in_reset)
1910 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07001911 return _aac_probe_container(scsicmd,
1912 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 default:
1914 break;
1915 }
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 } else { /* check for physical non-dasd devices */
Mark Haverkamp653ba582006-09-19 08:59:43 -07001918 if ((dev->nondasd_support == 1) || expose_physicals) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001919 if (dev->in_reset)
1920 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return aac_send_srb_fib(scsicmd);
1922 } else {
1923 scsicmd->result = DID_NO_CONNECT << 16;
1924 scsicmd->scsi_done(scsicmd);
1925 return 0;
1926 }
1927 }
1928 }
1929 /*
1930 * else Command for the controller itself
1931 */
1932 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1933 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1934 {
1935 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1936 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1937 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1938 ILLEGAL_REQUEST,
1939 SENCODE_INVALID_COMMAND,
1940 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1941 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1942 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1943 ? sizeof(scsicmd->sense_buffer)
1944 : sizeof(dev->fsa_dev[cid].sense_data));
1945 scsicmd->scsi_done(scsicmd);
1946 return 0;
1947 }
1948
1949
1950 /* Handle commands here that don't really require going out to the adapter */
1951 switch (scsicmd->cmnd[0]) {
1952 case INQUIRY:
1953 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001954 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001956 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001957 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001959 if (scsicmd->cmnd[1] & 0x1 ) {
1960 char *arr = (char *)&inq_data;
1961
1962 /* EVPD bit set */
1963 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
1964 INQD_PDT_PROC : INQD_PDT_DA;
1965 if (scsicmd->cmnd[2] == 0) {
1966 /* supported vital product data pages */
1967 arr[3] = 2;
1968 arr[4] = 0x0;
1969 arr[5] = 0x80;
1970 arr[1] = scsicmd->cmnd[2];
1971 aac_internal_transfer(scsicmd, &inq_data, 0,
1972 sizeof(inq_data));
1973 scsicmd->result = DID_OK << 16 |
1974 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1975 } else if (scsicmd->cmnd[2] == 0x80) {
1976 /* unit serial number page */
1977 arr[3] = setinqserial(dev, &arr[4],
1978 scmd_id(scsicmd));
1979 arr[1] = scsicmd->cmnd[2];
1980 aac_internal_transfer(scsicmd, &inq_data, 0,
1981 sizeof(inq_data));
1982 return aac_get_container_serial(scsicmd);
1983 } else {
1984 /* vpd page not implemented */
1985 scsicmd->result = DID_OK << 16 |
1986 COMMAND_COMPLETE << 8 |
1987 SAM_STAT_CHECK_CONDITION;
1988 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1989 ILLEGAL_REQUEST,
1990 SENCODE_INVALID_CDB_FIELD,
1991 ASENCODE_NO_SENSE, 0, 7, 2, 0);
1992 memcpy(scsicmd->sense_buffer,
1993 &dev->fsa_dev[cid].sense_data,
1994 (sizeof(dev->fsa_dev[cid].sense_data) >
1995 sizeof(scsicmd->sense_buffer))
1996 ? sizeof(scsicmd->sense_buffer)
1997 : sizeof(dev->fsa_dev[cid].sense_data));
1998 }
1999 scsicmd->scsi_done(scsicmd);
2000 return 0;
2001 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002002 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002003 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 */
2004 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002006 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
2008 * Set the Vendor, Product, and Revision Level
2009 * see: <vendor>.c i.e. aac.c
2010 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002011 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07002012 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002013 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
2014 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2016 scsicmd->scsi_done(scsicmd);
2017 return 0;
2018 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002019 if (dev->in_reset)
2020 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07002021 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002022 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
2023 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002024 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002026 case SERVICE_ACTION_IN:
2027 if (!(dev->raw_io_interface) ||
2028 !(dev->raw_io_64) ||
2029 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2030 break;
2031 {
2032 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002033 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002034
2035 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2036 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002037 cp[0] = (capacity >> 56) & 0xff;
2038 cp[1] = (capacity >> 48) & 0xff;
2039 cp[2] = (capacity >> 40) & 0xff;
2040 cp[3] = (capacity >> 32) & 0xff;
2041 cp[4] = (capacity >> 24) & 0xff;
2042 cp[5] = (capacity >> 16) & 0xff;
2043 cp[6] = (capacity >> 8) & 0xff;
2044 cp[7] = (capacity >> 0) & 0xff;
2045 cp[8] = 0;
2046 cp[9] = 0;
2047 cp[10] = 2;
2048 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002049 cp[12] = 0;
2050 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08002051 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002052 if (sizeof(cp) < scsicmd->cmnd[13]) {
2053 unsigned int len, offset = sizeof(cp);
2054
2055 memset(cp, 0, offset);
2056 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002057 len = min_t(size_t, scsicmd->cmnd[13] - offset,
2058 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002059 aac_internal_transfer(scsicmd, cp, offset, len);
2060 } while ((offset += len) < scsicmd->cmnd[13]);
2061 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002062
2063 /* Do not cache partition table for arrays */
2064 scsicmd->device->removable = 1;
2065
2066 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2067 scsicmd->scsi_done(scsicmd);
2068
2069 return 0;
2070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 case READ_CAPACITY:
2073 {
2074 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002075 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002078 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 capacity = fsa_dev_ptr[cid].size - 1;
2080 else
2081 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 cp[0] = (capacity >> 24) & 0xff;
2084 cp[1] = (capacity >> 16) & 0xff;
2085 cp[2] = (capacity >> 8) & 0xff;
2086 cp[3] = (capacity >> 0) & 0xff;
2087 cp[4] = 0;
2088 cp[5] = 0;
2089 cp[6] = 2;
2090 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002091 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002092 /* Do not cache partition table for arrays */
2093 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2096 scsicmd->scsi_done(scsicmd);
2097
2098 return 0;
2099 }
2100
2101 case MODE_SENSE:
2102 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002103 char mode_buf[7];
2104 int mode_buf_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 mode_buf[0] = 3; /* Mode data length */
2108 mode_buf[1] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002109 mode_buf[2] = 0; /* Device-specific param,
2110 bit 8: 0/1 = write enabled/protected
2111 bit 4: 0/1 = FUA enabled */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002112 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002113 mode_buf[2] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 mode_buf[3] = 0; /* Block descriptor length */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002115 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2116 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2117 mode_buf[0] = 6;
2118 mode_buf[4] = 8;
2119 mode_buf[5] = 1;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002120 mode_buf[6] = ((aac_cache & 6) == 2)
2121 ? 0 : 0x04; /* WCE */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002122 mode_buf_length = 7;
2123 if (mode_buf_length > scsicmd->cmnd[4])
2124 mode_buf_length = scsicmd->cmnd[4];
2125 }
2126 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2128 scsicmd->scsi_done(scsicmd);
2129
2130 return 0;
2131 }
2132 case MODE_SENSE_10:
2133 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002134 char mode_buf[11];
2135 int mode_buf_length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 mode_buf[0] = 0; /* Mode data length (MSB) */
2139 mode_buf[1] = 6; /* Mode data length (LSB) */
2140 mode_buf[2] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002141 mode_buf[3] = 0; /* Device-specific param,
2142 bit 8: 0/1 = write enabled/protected
2143 bit 4: 0/1 = FUA enabled */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002144 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002145 mode_buf[3] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 mode_buf[4] = 0; /* reserved */
2147 mode_buf[5] = 0; /* reserved */
2148 mode_buf[6] = 0; /* Block descriptor length (MSB) */
2149 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002150 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2151 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2152 mode_buf[1] = 9;
2153 mode_buf[8] = 8;
2154 mode_buf[9] = 1;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002155 mode_buf[10] = ((aac_cache & 6) == 2)
2156 ? 0 : 0x04; /* WCE */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002157 mode_buf_length = 11;
2158 if (mode_buf_length > scsicmd->cmnd[8])
2159 mode_buf_length = scsicmd->cmnd[8];
2160 }
2161 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2164 scsicmd->scsi_done(scsicmd);
2165
2166 return 0;
2167 }
2168 case REQUEST_SENSE:
2169 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2170 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2171 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2172 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2173 scsicmd->scsi_done(scsicmd);
2174 return 0;
2175
2176 case ALLOW_MEDIUM_REMOVAL:
2177 dprintk((KERN_DEBUG "LOCK command.\n"));
2178 if (scsicmd->cmnd[4])
2179 fsa_dev_ptr[cid].locked = 1;
2180 else
2181 fsa_dev_ptr[cid].locked = 0;
2182
2183 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2184 scsicmd->scsi_done(scsicmd);
2185 return 0;
2186 /*
2187 * These commands are all No-Ops
2188 */
2189 case TEST_UNIT_READY:
2190 case RESERVE:
2191 case RELEASE:
2192 case REZERO_UNIT:
2193 case REASSIGN_BLOCKS:
2194 case SEEK_10:
2195 case START_STOP:
2196 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2197 scsicmd->scsi_done(scsicmd);
2198 return 0;
2199 }
2200
2201 switch (scsicmd->cmnd[0])
2202 {
2203 case READ_6:
2204 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002205 case READ_12:
2206 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002207 if (dev->in_reset)
2208 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /*
2210 * Hack to keep track of ordinal number of the device that
2211 * corresponds to a container. Needed to convert
2212 * containers to /dev/sd device names
2213 */
2214
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002215 if (scsicmd->request->rq_disk)
2216 strlcpy(fsa_dev_ptr[cid].devname,
2217 scsicmd->request->rq_disk->disk_name,
2218 min(sizeof(fsa_dev_ptr[cid].devname),
2219 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002220
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002221 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 case WRITE_6:
2224 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002225 case WRITE_12:
2226 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002227 if (dev->in_reset)
2228 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002229 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 case SYNCHRONIZE_CACHE:
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002232 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2233 scsicmd->result = DID_OK << 16 |
2234 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2235 scsicmd->scsi_done(scsicmd);
2236 return 0;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* Issue FIB to tell Firmware to flush it's cache */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002239 if ((aac_cache & 6) != 2)
2240 return aac_synchronize(scsicmd);
2241 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 default:
2243 /*
2244 * Unhandled commands
2245 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002246 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2248 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
2249 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2250 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
2251 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2252 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
2253 ? sizeof(scsicmd->sense_buffer)
2254 : sizeof(dev->fsa_dev[cid].sense_data));
2255 scsicmd->scsi_done(scsicmd);
2256 return 0;
2257 }
2258}
2259
2260static int query_disk(struct aac_dev *dev, void __user *arg)
2261{
2262 struct aac_query_disk qd;
2263 struct fsa_dev_info *fsa_dev_ptr;
2264
2265 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002266 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002267 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2269 return -EFAULT;
2270 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002271 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2273 {
2274 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2275 return -EINVAL;
2276 qd.instance = dev->scsi_host_ptr->host_no;
2277 qd.bus = 0;
2278 qd.id = CONTAINER_TO_ID(qd.cnum);
2279 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2280 }
2281 else return -EINVAL;
2282
Salyzyn, Markfd622b12007-07-17 10:59:19 -04002283 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2285 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2286
2287 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2288 qd.unmapped = 1;
2289 else
2290 qd.unmapped = 0;
2291
2292 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2293 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2294
2295 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2296 return -EFAULT;
2297 return 0;
2298}
2299
2300static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2301{
2302 struct aac_delete_disk dd;
2303 struct fsa_dev_info *fsa_dev_ptr;
2304
2305 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp651013552006-09-19 08:59:23 -07002306 if (!fsa_dev_ptr)
2307 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2310 return -EFAULT;
2311
2312 if (dd.cnum >= dev->maximum_num_containers)
2313 return -EINVAL;
2314 /*
2315 * Mark this container as being deleted.
2316 */
2317 fsa_dev_ptr[dd.cnum].deleted = 1;
2318 /*
2319 * Mark the container as no longer valid
2320 */
2321 fsa_dev_ptr[dd.cnum].valid = 0;
2322 return 0;
2323}
2324
2325static int delete_disk(struct aac_dev *dev, void __user *arg)
2326{
2327 struct aac_delete_disk dd;
2328 struct fsa_dev_info *fsa_dev_ptr;
2329
2330 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002331 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002332 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2335 return -EFAULT;
2336
2337 if (dd.cnum >= dev->maximum_num_containers)
2338 return -EINVAL;
2339 /*
2340 * If the container is locked, it can not be deleted by the API.
2341 */
2342 if (fsa_dev_ptr[dd.cnum].locked)
2343 return -EBUSY;
2344 else {
2345 /*
2346 * Mark the container as no longer being valid.
2347 */
2348 fsa_dev_ptr[dd.cnum].valid = 0;
2349 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2350 return 0;
2351 }
2352}
2353
2354int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2355{
2356 switch (cmd) {
2357 case FSACTL_QUERY_DISK:
2358 return query_disk(dev, arg);
2359 case FSACTL_DELETE_DISK:
2360 return delete_disk(dev, arg);
2361 case FSACTL_FORCE_DELETE_DISK:
2362 return force_delete_disk(dev, arg);
2363 case FSACTL_GET_CONTAINERS:
2364 return aac_get_containers(dev);
2365 default:
2366 return -ENOTTY;
2367 }
2368}
2369
2370/**
2371 *
2372 * aac_srb_callback
2373 * @context: the context set in the fib - here it is scsi cmd
2374 * @fibptr: pointer to the fib
2375 *
2376 * Handles the completion of a scsi command to a non dasd device
2377 *
2378 */
2379
2380static void aac_srb_callback(void *context, struct fib * fibptr)
2381{
2382 struct aac_dev *dev;
2383 struct aac_srb_reply *srbreply;
2384 struct scsi_cmnd *scsicmd;
2385
2386 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002387
2388 if (!aac_valid_context(scsicmd, fibptr))
2389 return;
2390
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002391 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002393 dev = fibptr->dev;
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2396
2397 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
2398 /*
2399 * Calculate resid for sg
2400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002402 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2403 - le32_to_cpu(srbreply->data_xfer_length));
2404
2405 scsi_dma_unmap(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 /*
2408 * First check the fib status
2409 */
2410
2411 if (le32_to_cpu(srbreply->status) != ST_OK){
2412 int len;
2413 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2414 len = (le32_to_cpu(srbreply->sense_data_size) >
2415 sizeof(scsicmd->sense_buffer)) ?
2416 sizeof(scsicmd->sense_buffer) :
2417 le32_to_cpu(srbreply->sense_data_size);
2418 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2419 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2420 }
2421
2422 /*
2423 * Next check the srb status
2424 */
2425 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2426 case SRB_STATUS_ERROR_RECOVERY:
2427 case SRB_STATUS_PENDING:
2428 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08002429 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 break;
2431 case SRB_STATUS_DATA_OVERRUN:
2432 switch(scsicmd->cmnd[0]){
2433 case READ_6:
2434 case WRITE_6:
2435 case READ_10:
2436 case WRITE_10:
2437 case READ_12:
2438 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002439 case READ_16:
2440 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2442 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2443 } else {
2444 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2445 }
2446 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2447 break;
2448 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08002449 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 break;
2451 }
2452 default:
2453 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2454 break;
2455 }
2456 break;
2457 case SRB_STATUS_ABORTED:
2458 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2459 break;
2460 case SRB_STATUS_ABORT_FAILED:
2461 // Not sure about this one - but assuming the hba was trying to abort for some reason
2462 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2463 break;
2464 case SRB_STATUS_PARITY_ERROR:
2465 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2466 break;
2467 case SRB_STATUS_NO_DEVICE:
2468 case SRB_STATUS_INVALID_PATH_ID:
2469 case SRB_STATUS_INVALID_TARGET_ID:
2470 case SRB_STATUS_INVALID_LUN:
2471 case SRB_STATUS_SELECTION_TIMEOUT:
2472 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2473 break;
2474
2475 case SRB_STATUS_COMMAND_TIMEOUT:
2476 case SRB_STATUS_TIMEOUT:
2477 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2478 break;
2479
2480 case SRB_STATUS_BUSY:
Salyzyn, Mark760af102007-06-19 11:41:21 -04002481 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 break;
2483
2484 case SRB_STATUS_BUS_RESET:
2485 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2486 break;
2487
2488 case SRB_STATUS_MESSAGE_REJECTED:
2489 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2490 break;
2491 case SRB_STATUS_REQUEST_FLUSHED:
2492 case SRB_STATUS_ERROR:
2493 case SRB_STATUS_INVALID_REQUEST:
2494 case SRB_STATUS_REQUEST_SENSE_FAILED:
2495 case SRB_STATUS_NO_HBA:
2496 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2497 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2498 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2499 case SRB_STATUS_DELAYED_RETRY:
2500 case SRB_STATUS_BAD_FUNCTION:
2501 case SRB_STATUS_NOT_STARTED:
2502 case SRB_STATUS_NOT_IN_USE:
2503 case SRB_STATUS_FORCE_ABORT:
2504 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2505 default:
2506#ifdef AAC_DETAILED_STATUS_INFO
2507 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2508 le32_to_cpu(srbreply->srb_status) & 0x3F,
2509 aac_get_status_string(
2510 le32_to_cpu(srbreply->srb_status) & 0x3F),
2511 scsicmd->cmnd[0],
2512 le32_to_cpu(srbreply->scsi_status));
2513#endif
2514 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2515 break;
2516 }
2517 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2518 int len;
2519 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2520 len = (le32_to_cpu(srbreply->sense_data_size) >
2521 sizeof(scsicmd->sense_buffer)) ?
2522 sizeof(scsicmd->sense_buffer) :
2523 le32_to_cpu(srbreply->sense_data_size);
2524#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002525 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2526 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527#endif
2528 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2529
2530 }
2531 /*
2532 * OR in the scsi status (already shifted up a bit)
2533 */
2534 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2535
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002536 aac_fib_complete(fibptr);
2537 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002538 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
2541/**
2542 *
2543 * aac_send_scb_fib
2544 * @scsicmd: the scsi command block
2545 *
2546 * This routine will form a FIB and fill in the aac_srb from the
2547 * scsicmd passed in.
2548 */
2549
2550static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2551{
2552 struct fib* cmd_fibcontext;
2553 struct aac_dev* dev;
2554 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Mark Haverkamp84971732005-06-20 11:55:24 -07002556 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002557 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002558 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 scsicmd->result = DID_NO_CONNECT << 16;
2560 scsicmd->scsi_done(scsicmd);
2561 return 0;
2562 }
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /*
2565 * Allocate and initialize a Fib then setup a BlockWrite command
2566 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002567 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return -1;
2569 }
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002570 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * Check that the command queued to the controller
2574 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002575 if (status == -EINPROGRESS) {
2576 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return 0;
2578 }
2579
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002580 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2581 aac_fib_complete(cmd_fibcontext);
2582 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 return -1;
2585}
2586
2587static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2588{
2589 struct aac_dev *dev;
2590 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002591 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2594 // Get rid of old data
2595 psg->count = 0;
2596 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002597 psg->sg[0].count = 0;
2598
2599 nseg = scsi_dma_map(scsicmd);
2600 BUG_ON(nseg < 0);
2601 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct scatterlist *sg;
2603 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002605 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002607 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2609 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2610 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002613 if (byte_count > scsi_bufflen(scsicmd)) {
2614 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2615 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002616 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002617 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
2619 /* Check for command underflow */
2620 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2621 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2622 byte_count, scsicmd->underflow);
2623 }
2624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return byte_count;
2626}
2627
2628
2629static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2630{
2631 struct aac_dev *dev;
2632 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002633 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002634 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2637 // Get rid of old data
2638 psg->count = 0;
2639 psg->sg[0].addr[0] = 0;
2640 psg->sg[0].addr[1] = 0;
2641 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002642
2643 nseg = scsi_dma_map(scsicmd);
2644 BUG_ON(nseg < 0);
2645 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct scatterlist *sg;
2647 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002649 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002650 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002651 addr = sg_dma_address(sg);
2652 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2653 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002654 psg->sg[i].count = cpu_to_le32(count);
2655 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002657 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002659 if (byte_count > scsi_bufflen(scsicmd)) {
2660 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2661 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002662 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002663 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 }
2665 /* Check for command underflow */
2666 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2667 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2668 byte_count, scsicmd->underflow);
2669 }
2670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return byte_count;
2672}
2673
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002674static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2675{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002676 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002677 int nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002678
2679 // Get rid of old data
2680 psg->count = 0;
2681 psg->sg[0].next = 0;
2682 psg->sg[0].prev = 0;
2683 psg->sg[0].addr[0] = 0;
2684 psg->sg[0].addr[1] = 0;
2685 psg->sg[0].count = 0;
2686 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002687
2688 nseg = scsi_dma_map(scsicmd);
2689 BUG_ON(nseg < 0);
2690 if (nseg) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002691 struct scatterlist *sg;
2692 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002693
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002694 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002695 int count = sg_dma_len(sg);
2696 u64 addr = sg_dma_address(sg);
2697 psg->sg[i].next = 0;
2698 psg->sg[i].prev = 0;
2699 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2700 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2701 psg->sg[i].count = cpu_to_le32(count);
2702 psg->sg[i].flags = 0;
2703 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002704 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002705 psg->count = cpu_to_le32(nseg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002706 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002707 if (byte_count > scsi_bufflen(scsicmd)) {
2708 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2709 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002710 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002711 byte_count = scsi_bufflen(scsicmd);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002712 }
2713 /* Check for command underflow */
2714 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2715 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2716 byte_count, scsicmd->underflow);
2717 }
2718 }
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002719 return byte_count;
2720}
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722#ifdef AAC_DETAILED_STATUS_INFO
2723
2724struct aac_srb_status_info {
2725 u32 status;
2726 char *str;
2727};
2728
2729
2730static struct aac_srb_status_info srb_status_info[] = {
2731 { SRB_STATUS_PENDING, "Pending Status"},
2732 { SRB_STATUS_SUCCESS, "Success"},
2733 { SRB_STATUS_ABORTED, "Aborted Command"},
2734 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002735 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 { SRB_STATUS_BUSY, "Device Busy"},
2737 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2738 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2739 { SRB_STATUS_NO_DEVICE, "No Device"},
2740 { SRB_STATUS_TIMEOUT, "Timeout"},
2741 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2742 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2743 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2744 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2745 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2746 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2747 { SRB_STATUS_NO_HBA, "No HBA"},
2748 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2749 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2750 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2751 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2752 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2753 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002754 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2756 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2757 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2758 { SRB_STATUS_NOT_STARTED, "Not Started"},
2759 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2760 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2761 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2762 { 0xff, "Unknown Error"}
2763};
2764
2765char *aac_get_status_string(u32 status)
2766{
2767 int i;
2768
Tobias Klauser6391a112006-06-08 22:23:48 -07002769 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2770 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 return "Bad Status Code";
2774}
2775
2776#endif