blob: 3cddcc0901b24a57cd778df52a5be1911eb27056 [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
Salyzyn, Mark17eaace2008-01-08 12:23:49 -0800693char * get_container_type(unsigned tindex)
694{
695 if (tindex >= ARRAY_SIZE(container_types))
696 tindex = ARRAY_SIZE(container_types) - 1;
697 return container_types[tindex];
698}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700/* Function: setinqstr
701 *
702 * Arguments: [1] pointer to void [1] int
703 *
704 * Purpose: Sets SCSI inquiry data strings for vendor, product
705 * and revision level. Allows strings to be set in platform dependant
706 * files instead of in OS dependant driver source.
707 */
708
Mark Haverkamp794d0602005-10-24 10:51:53 -0700709static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700714 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Mark Haverkamp794d0602005-10-24 10:51:53 -0700716 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
717 char * cp = dev->supplement_adapter_info.AdapterTypeText;
718 int c = sizeof(str->vid);
719 while (*cp && *cp != ' ' && --c)
720 ++cp;
721 c = *cp;
722 *cp = '\0';
723 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
724 str->vid);
725 *cp = c;
726 while (*cp && *cp != ' ')
727 ++cp;
728 while (*cp == ' ')
729 ++cp;
730 /* last six chars reserved for vol type */
731 c = 0;
732 if (strlen(cp) > sizeof(str->pid)) {
733 c = cp[sizeof(str->pid)];
734 cp[sizeof(str->pid)] = '\0';
735 }
736 inqstrcpy (cp, str->pid);
737 if (c)
738 cp[sizeof(str->pid)] = c;
739 } else {
740 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700741
742 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700743 /* last six chars reserved for vol type */
744 inqstrcpy (mp->model, str->pid);
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Tobias Klauser6391a112006-06-08 22:23:48 -0700747 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 char *findit = str->pid;
749
750 for ( ; *findit != ' '; findit++); /* walk till we find a space */
751 /* RAID is superfluous in the context of a RAID device */
752 if (memcmp(findit-4, "RAID", 4) == 0)
753 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700754 if (((findit - str->pid) + strlen(container_types[tindex]))
755 < (sizeof(str->pid) + sizeof(str->prl)))
756 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 inqstrcpy ("V1.0", str->prl);
759}
760
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400761static void get_container_serial_callback(void *context, struct fib * fibptr)
762{
763 struct aac_get_serial_resp * get_serial_reply;
764 struct scsi_cmnd * scsicmd;
765
766 BUG_ON(fibptr == NULL);
767
768 scsicmd = (struct scsi_cmnd *) context;
769 if (!aac_valid_context(scsicmd, fibptr))
770 return;
771
772 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
773 /* Failure is irrelevant, using default value instead */
774 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
775 char sp[13];
776 /* EVPD bit set */
777 sp[0] = INQD_PDT_DA;
778 sp[1] = scsicmd->cmnd[2];
779 sp[2] = 0;
780 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
781 le32_to_cpu(get_serial_reply->uid));
782 aac_internal_transfer(scsicmd, sp, 0, sizeof(sp));
783 }
784
785 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
786
787 aac_fib_complete(fibptr);
788 aac_fib_free(fibptr);
789 scsicmd->scsi_done(scsicmd);
790}
791
792/**
793 * aac_get_container_serial - get container serial, none blocking.
794 */
795static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
796{
797 int status;
798 struct aac_get_serial *dinfo;
799 struct fib * cmd_fibcontext;
800 struct aac_dev * dev;
801
802 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
803
804 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
805 return -ENOMEM;
806
807 aac_fib_init(cmd_fibcontext);
808 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
809
810 dinfo->command = cpu_to_le32(VM_ContainerConfig);
811 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
812 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
813
814 status = aac_fib_send(ContainerCommand,
815 cmd_fibcontext,
816 sizeof (struct aac_get_serial),
817 FsaNormal,
818 0, 1,
819 (fib_callback) get_container_serial_callback,
820 (void *) scsicmd);
821
822 /*
823 * Check that the command queued to the controller
824 */
825 if (status == -EINPROGRESS) {
826 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
827 return 0;
828 }
829
830 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
831 aac_fib_complete(cmd_fibcontext);
832 aac_fib_free(cmd_fibcontext);
833 return -1;
834}
835
836/* Function: setinqserial
837 *
838 * Arguments: [1] pointer to void [1] int
839 *
840 * Purpose: Sets SCSI Unit Serial number.
841 * This is a fake. We should read a proper
842 * serial number from the container. <SuSE>But
843 * without docs it's quite hard to do it :-)
844 * So this will have to do in the meantime.</SuSE>
845 */
846
847static int setinqserial(struct aac_dev *dev, void *data, int cid)
848{
849 /*
850 * This breaks array migration.
851 */
852 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
853 le32_to_cpu(dev->adapter_info.serial[0]), cid);
854}
855
Adrian Bunk 48338692005-04-25 19:45:58 -0700856static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
857 u8 a_sense_code, u8 incorrect_length,
858 u8 bit_pointer, u16 field_pointer,
859 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
862 sense_buf[1] = 0; /* Segment number, always zero */
863
864 if (incorrect_length) {
865 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
866 sense_buf[3] = BYTE3(residue);
867 sense_buf[4] = BYTE2(residue);
868 sense_buf[5] = BYTE1(residue);
869 sense_buf[6] = BYTE0(residue);
870 } else
871 sense_buf[2] = sense_key; /* Sense key */
872
873 if (sense_key == ILLEGAL_REQUEST)
874 sense_buf[7] = 10; /* Additional sense length */
875 else
876 sense_buf[7] = 6; /* Additional sense length */
877
878 sense_buf[12] = sense_code; /* Additional sense code */
879 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
880 if (sense_key == ILLEGAL_REQUEST) {
881 sense_buf[15] = 0;
882
883 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
884 sense_buf[15] = 0x80;/* Std sense key specific field */
885 /* Illegal parameter is in the parameter block */
886
887 if (sense_code == SENCODE_INVALID_CDB_FIELD)
888 sense_buf[15] = 0xc0;/* Std sense key specific field */
889 /* Illegal parameter is in the CDB block */
890 sense_buf[15] |= bit_pointer;
891 sense_buf[16] = field_pointer >> 8; /* MSB */
892 sense_buf[17] = field_pointer; /* LSB */
893 }
894}
895
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800896static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
897{
898 if (lba & 0xffffffff00000000LL) {
899 int cid = scmd_id(cmd);
900 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
901 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
902 SAM_STAT_CHECK_CONDITION;
903 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
904 HARDWARE_ERROR,
905 SENCODE_INTERNAL_TARGET_FAILURE,
906 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
907 0, 0);
908 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
909 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(cmd->sense_buffer))
910 ? sizeof(cmd->sense_buffer)
911 : sizeof(dev->fsa_dev[cid].sense_data));
912 cmd->scsi_done(cmd);
913 return 1;
914 }
915 return 0;
916}
917
918static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
919{
920 return 0;
921}
922
923static void io_callback(void *context, struct fib * fibptr);
924
925static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
926{
927 u16 fibsize;
928 struct aac_raw_io *readcmd;
929 aac_fib_init(fib);
930 readcmd = (struct aac_raw_io *) fib_data(fib);
931 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
932 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
933 readcmd->count = cpu_to_le32(count<<9);
934 readcmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -0400935 readcmd->flags = cpu_to_le16(IO_TYPE_READ);
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800936 readcmd->bpTotal = 0;
937 readcmd->bpComplete = 0;
938
939 aac_build_sgraw(cmd, &readcmd->sg);
940 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
941 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
942 /*
943 * Now send the Fib to the adapter
944 */
945 return aac_fib_send(ContainerRawIo,
946 fib,
947 fibsize,
948 FsaNormal,
949 0, 1,
950 (fib_callback) io_callback,
951 (void *) cmd);
952}
953
954static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
955{
956 u16 fibsize;
957 struct aac_read64 *readcmd;
958 aac_fib_init(fib);
959 readcmd = (struct aac_read64 *) fib_data(fib);
960 readcmd->command = cpu_to_le32(VM_CtHostRead64);
961 readcmd->cid = cpu_to_le16(scmd_id(cmd));
962 readcmd->sector_count = cpu_to_le16(count);
963 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
964 readcmd->pad = 0;
965 readcmd->flags = 0;
966
967 aac_build_sg64(cmd, &readcmd->sg);
968 fibsize = sizeof(struct aac_read64) +
969 ((le32_to_cpu(readcmd->sg.count) - 1) *
970 sizeof (struct sgentry64));
971 BUG_ON (fibsize > (fib->dev->max_fib_size -
972 sizeof(struct aac_fibhdr)));
973 /*
974 * Now send the Fib to the adapter
975 */
976 return aac_fib_send(ContainerCommand64,
977 fib,
978 fibsize,
979 FsaNormal,
980 0, 1,
981 (fib_callback) io_callback,
982 (void *) cmd);
983}
984
985static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
986{
987 u16 fibsize;
988 struct aac_read *readcmd;
989 aac_fib_init(fib);
990 readcmd = (struct aac_read *) fib_data(fib);
991 readcmd->command = cpu_to_le32(VM_CtBlockRead);
Christoph Hellwigf3307f72007-11-08 17:27:47 +0000992 readcmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800993 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
994 readcmd->count = cpu_to_le32(count * 512);
995
996 aac_build_sg(cmd, &readcmd->sg);
997 fibsize = sizeof(struct aac_read) +
998 ((le32_to_cpu(readcmd->sg.count) - 1) *
999 sizeof (struct sgentry));
1000 BUG_ON (fibsize > (fib->dev->max_fib_size -
1001 sizeof(struct aac_fibhdr)));
1002 /*
1003 * Now send the Fib to the adapter
1004 */
1005 return aac_fib_send(ContainerCommand,
1006 fib,
1007 fibsize,
1008 FsaNormal,
1009 0, 1,
1010 (fib_callback) io_callback,
1011 (void *) cmd);
1012}
1013
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001014static 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 -08001015{
1016 u16 fibsize;
1017 struct aac_raw_io *writecmd;
1018 aac_fib_init(fib);
1019 writecmd = (struct aac_raw_io *) fib_data(fib);
1020 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1021 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1022 writecmd->count = cpu_to_le32(count<<9);
1023 writecmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001024 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1025 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001026 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
1027 cpu_to_le16(IO_TYPE_WRITE);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001028 writecmd->bpTotal = 0;
1029 writecmd->bpComplete = 0;
1030
1031 aac_build_sgraw(cmd, &writecmd->sg);
1032 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1033 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1034 /*
1035 * Now send the Fib to the adapter
1036 */
1037 return aac_fib_send(ContainerRawIo,
1038 fib,
1039 fibsize,
1040 FsaNormal,
1041 0, 1,
1042 (fib_callback) io_callback,
1043 (void *) cmd);
1044}
1045
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001046static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001047{
1048 u16 fibsize;
1049 struct aac_write64 *writecmd;
1050 aac_fib_init(fib);
1051 writecmd = (struct aac_write64 *) fib_data(fib);
1052 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1053 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1054 writecmd->sector_count = cpu_to_le16(count);
1055 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1056 writecmd->pad = 0;
1057 writecmd->flags = 0;
1058
1059 aac_build_sg64(cmd, &writecmd->sg);
1060 fibsize = sizeof(struct aac_write64) +
1061 ((le32_to_cpu(writecmd->sg.count) - 1) *
1062 sizeof (struct sgentry64));
1063 BUG_ON (fibsize > (fib->dev->max_fib_size -
1064 sizeof(struct aac_fibhdr)));
1065 /*
1066 * Now send the Fib to the adapter
1067 */
1068 return aac_fib_send(ContainerCommand64,
1069 fib,
1070 fibsize,
1071 FsaNormal,
1072 0, 1,
1073 (fib_callback) io_callback,
1074 (void *) cmd);
1075}
1076
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001077static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001078{
1079 u16 fibsize;
1080 struct aac_write *writecmd;
1081 aac_fib_init(fib);
1082 writecmd = (struct aac_write *) fib_data(fib);
1083 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001084 writecmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001085 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1086 writecmd->count = cpu_to_le32(count * 512);
1087 writecmd->sg.count = cpu_to_le32(1);
1088 /* ->stable is not used - it did mean which type of write */
1089
1090 aac_build_sg(cmd, &writecmd->sg);
1091 fibsize = sizeof(struct aac_write) +
1092 ((le32_to_cpu(writecmd->sg.count) - 1) *
1093 sizeof (struct sgentry));
1094 BUG_ON (fibsize > (fib->dev->max_fib_size -
1095 sizeof(struct aac_fibhdr)));
1096 /*
1097 * Now send the Fib to the adapter
1098 */
1099 return aac_fib_send(ContainerCommand,
1100 fib,
1101 fibsize,
1102 FsaNormal,
1103 0, 1,
1104 (fib_callback) io_callback,
1105 (void *) cmd);
1106}
1107
1108static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1109{
1110 struct aac_srb * srbcmd;
1111 u32 flag;
1112 u32 timeout;
1113
1114 aac_fib_init(fib);
1115 switch(cmd->sc_data_direction){
1116 case DMA_TO_DEVICE:
1117 flag = SRB_DataOut;
1118 break;
1119 case DMA_BIDIRECTIONAL:
1120 flag = SRB_DataIn | SRB_DataOut;
1121 break;
1122 case DMA_FROM_DEVICE:
1123 flag = SRB_DataIn;
1124 break;
1125 case DMA_NONE:
1126 default: /* shuts up some versions of gcc */
1127 flag = SRB_NoDataXfer;
1128 break;
1129 }
1130
1131 srbcmd = (struct aac_srb*) fib_data(fib);
1132 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1133 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1134 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1135 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1136 srbcmd->flags = cpu_to_le32(flag);
1137 timeout = cmd->timeout_per_command/HZ;
1138 if (timeout == 0)
1139 timeout = 1;
1140 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1141 srbcmd->retry_limit = 0; /* Obsolete parameter */
1142 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1143 return srbcmd;
1144}
1145
1146static void aac_srb_callback(void *context, struct fib * fibptr);
1147
1148static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1149{
1150 u16 fibsize;
1151 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1152
1153 aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001154 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001155
1156 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1157 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1158 /*
1159 * Build Scatter/Gather list
1160 */
1161 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1162 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1163 sizeof (struct sgentry64));
1164 BUG_ON (fibsize > (fib->dev->max_fib_size -
1165 sizeof(struct aac_fibhdr)));
1166
1167 /*
1168 * Now send the Fib to the adapter
1169 */
1170 return aac_fib_send(ScsiPortCommand64, fib,
1171 fibsize, FsaNormal, 0, 1,
1172 (fib_callback) aac_srb_callback,
1173 (void *) cmd);
1174}
1175
1176static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1177{
1178 u16 fibsize;
1179 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1180
1181 aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001182 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001183
1184 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1185 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1186 /*
1187 * Build Scatter/Gather list
1188 */
1189 fibsize = sizeof (struct aac_srb) +
1190 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1191 sizeof (struct sgentry));
1192 BUG_ON (fibsize > (fib->dev->max_fib_size -
1193 sizeof(struct aac_fibhdr)));
1194
1195 /*
1196 * Now send the Fib to the adapter
1197 */
1198 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1199 (fib_callback) aac_srb_callback, (void *) cmd);
1200}
1201
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001202static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1203{
1204 if ((sizeof(dma_addr_t) > 4) &&
1205 (num_physpages > (0xFFFFFFFFULL >> PAGE_SHIFT)) &&
1206 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1207 return FAILED;
1208 return aac_scsi_32(fib, cmd);
1209}
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211int aac_get_adapter_info(struct aac_dev* dev)
1212{
1213 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int rcode;
1215 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001216 struct aac_adapter_info *info;
1217 struct aac_bus_info *command;
1218 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001219
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001220 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return -ENOMEM;
1222
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001223 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001224 info = (struct aac_adapter_info *) fib_data(fibptr);
1225 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001227 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001228 fibptr,
1229 sizeof(*info),
1230 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001231 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001232 NULL,
1233 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001235 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001236 aac_fib_complete(fibptr);
1237 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001238 return rcode;
1239 }
1240 memcpy(&dev->adapter_info, info, sizeof(*info));
1241
1242 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001243 struct aac_supplement_adapter_info * sinfo;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001244
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001245 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001246
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001247 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001248
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001249 memset(sinfo,0,sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001250
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001251 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001252 fibptr,
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001253 sizeof(*sinfo),
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001254 FsaNormal,
1255 1, 1,
1256 NULL,
1257 NULL);
1258
1259 if (rcode >= 0)
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001260 memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Mark Haverkamp84971732005-06-20 11:55:24 -07001263
1264 /*
1265 * GetBusInfo
1266 */
1267
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001268 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001269
1270 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1271
1272 memset(bus_info, 0, sizeof(*bus_info));
1273
1274 command = (struct aac_bus_info *)bus_info;
1275
1276 command->Command = cpu_to_le32(VM_Ioctl);
1277 command->ObjType = cpu_to_le32(FT_DRIVE);
1278 command->MethodId = cpu_to_le32(1);
1279 command->CtlCmd = cpu_to_le32(GetBusInfo);
1280
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001281 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001282 fibptr,
1283 sizeof (*bus_info),
1284 FsaNormal,
1285 1, 1,
1286 NULL, NULL);
1287
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001288 /* reasoned default */
1289 dev->maximum_num_physicals = 16;
Mark Haverkamp84971732005-06-20 11:55:24 -07001290 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1291 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1292 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1293 }
1294
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001295 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001296 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001297 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1298 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 dev->name,
1300 dev->id,
1301 tmp>>24,
1302 (tmp>>16)&0xff,
1303 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001304 le32_to_cpu(dev->adapter_info.kernelbuild),
1305 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1306 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001307 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1308 printk(KERN_INFO "%s%d: monitor %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.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001312 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1313 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 dev->name, dev->id,
1315 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1316 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001317 buffer[0] = '\0';
1318 if (aac_show_serial_number(
1319 shost_to_class(dev->scsi_host_ptr), buffer))
1320 printk(KERN_INFO "%s%d: serial %s",
1321 dev->name, dev->id, buffer);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001322 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1323 printk(KERN_INFO "%s%d: TSID %.*s\n",
1324 dev->name, dev->id,
1325 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1326 dev->supplement_adapter_info.VpdInfo.Tsid);
1327 }
Salyzyn, Marka3940da2008-01-08 12:48:25 -08001328 if (!aac_check_reset || ((aac_check_reset != 1) &&
1329 (dev->supplement_adapter_info.SupportedOptions2 &
1330 AAC_OPTION_IGNORE_RESET))) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001331 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1332 dev->name, dev->id);
1333 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001336 dev->cache_protected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 dev->nondasd_support = 0;
1338 dev->raid_scsi_mode = 0;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001339 if(dev->adapter_info.options & AAC_OPT_NONDASD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 dev->nondasd_support = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /*
1343 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1344 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1345 * force nondasd support on. If we decide to allow the non-dasd flag
1346 * additional changes changes will have to be made to support
1347 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1348 * changed to support the new dev->raid_scsi_mode flag instead of
1349 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1350 * function aac_detect will have to be modified where it sets up the
1351 * max number of channels based on the aac->nondasd_support flag only.
1352 */
1353 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1354 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1355 dev->nondasd_support = 1;
1356 dev->raid_scsi_mode = 1;
1357 }
1358 if (dev->raid_scsi_mode != 0)
1359 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1360 dev->name, dev->id);
1361
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001362 if (nondasd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 dev->nondasd_support = (nondasd!=0);
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001364 if(dev->nondasd_support != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1366 }
1367
1368 dev->dac_support = 0;
1369 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1370 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1371 dev->dac_support = 1;
1372 }
1373
1374 if(dacmode != -1) {
1375 dev->dac_support = (dacmode!=0);
1376 }
1377 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -05001378 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1379 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1381 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -05001382 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1383 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1385 dev->name, dev->id);
1386 dev->dac_support = 0;
1387 } else {
1388 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1389 dev->name, dev->id);
1390 rcode = -ENOMEM;
1391 }
1392 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001393 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001394 * Deal with configuring for the individualized limits of each packet
1395 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001396 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001397 dev->a_ops.adapter_scsi = (dev->dac_support)
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001398 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1399 ? aac_scsi_32_64
1400 : aac_scsi_64)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001401 : aac_scsi_32;
1402 if (dev->raw_io_interface) {
1403 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1404 ? aac_bounds_64
1405 : aac_bounds_32;
1406 dev->a_ops.adapter_read = aac_read_raw_io;
1407 dev->a_ops.adapter_write = aac_write_raw_io;
1408 } else {
1409 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001410 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001411 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001412 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1413 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001414 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001415 dev->a_ops.adapter_read = aac_read_block64;
1416 dev->a_ops.adapter_write = aac_write_block64;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001417 /*
1418 * 38 scatter gather elements
1419 */
1420 dev->scsi_host_ptr->sg_tablesize =
1421 (dev->max_fib_size -
1422 sizeof(struct aac_fibhdr) -
1423 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001424 sizeof(struct sgentry64)) /
1425 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001426 } else {
1427 dev->a_ops.adapter_read = aac_read_block;
1428 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001429 }
1430 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1431 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1432 /*
1433 * Worst case size that could cause sg overflow when
1434 * we break up SG elements that are larger than 64KB.
1435 * Would be nice if we could tell the SCSI layer what
1436 * the maximum SG element size can be. Worst case is
1437 * (sg_tablesize-1) 4KB elements with one 64KB
1438 * element.
1439 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1440 */
1441 dev->scsi_host_ptr->max_sectors =
1442 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1443 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001446 aac_fib_complete(fibptr);
1447 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 return rcode;
1450}
1451
1452
Mark Haverkampe53cb352005-08-03 15:39:09 -07001453static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 struct aac_dev *dev;
1456 struct aac_read_reply *readreply;
1457 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 u32 cid;
1459
1460 scsicmd = (struct scsi_cmnd *) context;
1461
Mark Haverkamp03d44332007-03-15 10:27:45 -07001462 if (!aac_valid_context(scsicmd, fibptr))
1463 return;
1464
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001465 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001466 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001468 if (nblank(dprintk(x))) {
1469 u64 lba;
1470 switch (scsicmd->cmnd[0]) {
1471 case WRITE_6:
1472 case READ_6:
1473 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1474 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1475 break;
1476 case WRITE_16:
1477 case READ_16:
1478 lba = ((u64)scsicmd->cmnd[2] << 56) |
1479 ((u64)scsicmd->cmnd[3] << 48) |
1480 ((u64)scsicmd->cmnd[4] << 40) |
1481 ((u64)scsicmd->cmnd[5] << 32) |
1482 ((u64)scsicmd->cmnd[6] << 24) |
1483 (scsicmd->cmnd[7] << 16) |
1484 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1485 break;
1486 case WRITE_12:
1487 case READ_12:
1488 lba = ((u64)scsicmd->cmnd[2] << 24) |
1489 (scsicmd->cmnd[3] << 16) |
1490 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1491 break;
1492 default:
1493 lba = ((u64)scsicmd->cmnd[2] << 24) |
1494 (scsicmd->cmnd[3] << 16) |
1495 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1496 break;
1497 }
1498 printk(KERN_DEBUG
1499 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1500 smp_processor_id(), (unsigned long long)lba, jiffies);
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001503 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001504
1505 scsi_dma_unmap(scsicmd);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 readreply = (struct aac_read_reply *)fib_data(fibptr);
1508 if (le32_to_cpu(readreply->status) == ST_OK)
1509 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1510 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001511#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001512 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001513 le32_to_cpu(readreply->status));
1514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1516 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1517 HARDWARE_ERROR,
1518 SENCODE_INTERNAL_TARGET_FAILURE,
1519 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1520 0, 0);
1521 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1522 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1523 ? sizeof(scsicmd->sense_buffer)
1524 : sizeof(dev->fsa_dev[cid].sense_data));
1525 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001526 aac_fib_complete(fibptr);
1527 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001529 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001532static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001534 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 u32 count;
1536 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 struct aac_dev *dev;
1538 struct fib * cmd_fibcontext;
1539
1540 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1541 /*
1542 * Get block address and transfer length
1543 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001544 switch (scsicmd->cmnd[0]) {
1545 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001546 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001548 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1549 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 count = scsicmd->cmnd[4];
1551
1552 if (count == 0)
1553 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001554 break;
1555 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001556 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001557
1558 lba = ((u64)scsicmd->cmnd[2] << 56) |
1559 ((u64)scsicmd->cmnd[3] << 48) |
1560 ((u64)scsicmd->cmnd[4] << 40) |
1561 ((u64)scsicmd->cmnd[5] << 32) |
1562 ((u64)scsicmd->cmnd[6] << 24) |
1563 (scsicmd->cmnd[7] << 16) |
1564 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1565 count = (scsicmd->cmnd[10] << 24) |
1566 (scsicmd->cmnd[11] << 16) |
1567 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1568 break;
1569 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001570 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001571
1572 lba = ((u64)scsicmd->cmnd[2] << 24) |
1573 (scsicmd->cmnd[3] << 16) |
1574 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1575 count = (scsicmd->cmnd[6] << 24) |
1576 (scsicmd->cmnd[7] << 16) |
1577 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1578 break;
1579 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001580 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001582 lba = ((u64)scsicmd->cmnd[2] << 24) |
1583 (scsicmd->cmnd[3] << 16) |
1584 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001588 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001589 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001590 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
1593 * Alocate and initialize a Fib
1594 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001595 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return -1;
1597 }
1598
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001599 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /*
1602 * Check that the command queued to the controller
1603 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001604 if (status == -EINPROGRESS) {
1605 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001609 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * For some reason, the Fib didn't queue, return QUEUE_FULL
1612 */
1613 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001614 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001615 aac_fib_complete(cmd_fibcontext);
1616 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
1618}
1619
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001620static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001622 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001624 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 struct aac_dev *dev;
1627 struct fib * cmd_fibcontext;
1628
1629 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1630 /*
1631 * Get block address and transfer length
1632 */
1633 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1634 {
1635 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1636 count = scsicmd->cmnd[4];
1637 if (count == 0)
1638 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001639 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001640 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001641 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001642
1643 lba = ((u64)scsicmd->cmnd[2] << 56) |
1644 ((u64)scsicmd->cmnd[3] << 48) |
1645 ((u64)scsicmd->cmnd[4] << 40) |
1646 ((u64)scsicmd->cmnd[5] << 32) |
1647 ((u64)scsicmd->cmnd[6] << 24) |
1648 (scsicmd->cmnd[7] << 16) |
1649 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1650 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1651 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001652 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001653 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001654 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001655
1656 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1657 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1658 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1659 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001660 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001662 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001663 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001665 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001667 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001669 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /*
1672 * Allocate and initialize a Fib then setup a BlockWrite command
1673 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001674 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001676 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001680 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 /*
1683 * Check that the command queued to the controller
1684 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001685 if (status == -EINPROGRESS) {
1686 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688 }
1689
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001690 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /*
1692 * For some reason, the Fib didn't queue, return QUEUE_FULL
1693 */
1694 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001695 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001697 aac_fib_complete(cmd_fibcontext);
1698 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700}
1701
1702static void synchronize_callback(void *context, struct fib *fibptr)
1703{
1704 struct aac_synchronize_reply *synchronizereply;
1705 struct scsi_cmnd *cmd;
1706
1707 cmd = context;
1708
Mark Haverkamp03d44332007-03-15 10:27:45 -07001709 if (!aac_valid_context(cmd, fibptr))
1710 return;
1711
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001712 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 smp_processor_id(), jiffies));
1714 BUG_ON(fibptr == NULL);
1715
1716
1717 synchronizereply = fib_data(fibptr);
1718 if (le32_to_cpu(synchronizereply->status) == CT_OK)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001719 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1721 else {
1722 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001723 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001724 u32 cid = sdev_id(sdev);
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001725 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 "synchronize_callback: synchronize failed, status = %d\n",
1727 le32_to_cpu(synchronizereply->status));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001728 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1730 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1731 HARDWARE_ERROR,
1732 SENCODE_INTERNAL_TARGET_FAILURE,
1733 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1734 0, 0);
1735 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001736 min(sizeof(dev->fsa_dev[cid].sense_data),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 sizeof(cmd->sense_buffer)));
1738 }
1739
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001740 aac_fib_complete(fibptr);
1741 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001742 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001745static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 int status;
1748 struct fib *cmd_fibcontext;
1749 struct aac_synchronize *synchronizecmd;
1750 struct scsi_cmnd *cmd;
1751 struct scsi_device *sdev = scsicmd->device;
1752 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001753 struct aac_dev *aac;
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001754 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
1755 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1756 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 unsigned long flags;
1758
1759 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001760 * Wait for all outstanding queued commands to complete to this
1761 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
1763 spin_lock_irqsave(&sdev->list_lock, flags);
1764 list_for_each_entry(cmd, &sdev->cmd_list, list)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001765 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1766 u64 cmnd_lba;
1767 u32 cmnd_count;
1768
1769 if (cmd->cmnd[0] == WRITE_6) {
1770 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
1771 (cmd->cmnd[2] << 8) |
1772 cmd->cmnd[3];
1773 cmnd_count = cmd->cmnd[4];
1774 if (cmnd_count == 0)
1775 cmnd_count = 256;
1776 } else if (cmd->cmnd[0] == WRITE_16) {
1777 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
1778 ((u64)cmd->cmnd[3] << 48) |
1779 ((u64)cmd->cmnd[4] << 40) |
1780 ((u64)cmd->cmnd[5] << 32) |
1781 ((u64)cmd->cmnd[6] << 24) |
1782 (cmd->cmnd[7] << 16) |
1783 (cmd->cmnd[8] << 8) |
1784 cmd->cmnd[9];
1785 cmnd_count = (cmd->cmnd[10] << 24) |
1786 (cmd->cmnd[11] << 16) |
1787 (cmd->cmnd[12] << 8) |
1788 cmd->cmnd[13];
1789 } else if (cmd->cmnd[0] == WRITE_12) {
1790 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1791 (cmd->cmnd[3] << 16) |
1792 (cmd->cmnd[4] << 8) |
1793 cmd->cmnd[5];
1794 cmnd_count = (cmd->cmnd[6] << 24) |
1795 (cmd->cmnd[7] << 16) |
1796 (cmd->cmnd[8] << 8) |
1797 cmd->cmnd[9];
1798 } else if (cmd->cmnd[0] == WRITE_10) {
1799 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1800 (cmd->cmnd[3] << 16) |
1801 (cmd->cmnd[4] << 8) |
1802 cmd->cmnd[5];
1803 cmnd_count = (cmd->cmnd[7] << 8) |
1804 cmd->cmnd[8];
1805 } else
1806 continue;
1807 if (((cmnd_lba + cmnd_count) < lba) ||
1808 (count && ((lba + count) < cmnd_lba)))
1809 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 ++active;
1811 break;
1812 }
1813
1814 spin_unlock_irqrestore(&sdev->list_lock, flags);
1815
1816 /*
1817 * Yield the processor (requeue for later)
1818 */
1819 if (active)
1820 return SCSI_MLQUEUE_DEVICE_BUSY;
1821
Salyzyn, Markf8583172007-10-30 15:50:49 -04001822 aac = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001823 if (aac->in_reset)
1824 return SCSI_MLQUEUE_HOST_BUSY;
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001827 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001829 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return SCSI_MLQUEUE_HOST_BUSY;
1831
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001832 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 synchronizecmd = fib_data(cmd_fibcontext);
1835 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1836 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001837 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001838 synchronizecmd->count =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1840
1841 /*
1842 * Now send the Fib to the adapter
1843 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001844 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 cmd_fibcontext,
1846 sizeof(struct aac_synchronize),
1847 FsaNormal,
1848 0, 1,
1849 (fib_callback)synchronize_callback,
1850 (void *)scsicmd);
1851
1852 /*
1853 * Check that the command queued to the controller
1854 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001855 if (status == -EINPROGRESS) {
1856 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001860 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001861 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1862 aac_fib_complete(cmd_fibcontext);
1863 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return SCSI_MLQUEUE_HOST_BUSY;
1865}
1866
1867/**
1868 * aac_scsi_cmd() - Process SCSI command
1869 * @scsicmd: SCSI command block
1870 *
1871 * Emulate a SCSI command and queue the required request for the
1872 * aacraid firmware.
1873 */
1874
1875int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1876{
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001877 u32 cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 struct Scsi_Host *host = scsicmd->device->host;
1879 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1880 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001882 if (fsa_dev_ptr == NULL)
1883 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /*
1885 * If the bus, id or lun is out of range, return fail
1886 * Test does not apply to ID 16, the pseudo id for the controller
1887 * itself.
1888 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001889 cid = scmd_id(scsicmd);
1890 if (cid != host->this_id) {
1891 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
1892 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08001893 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 scsicmd->result = DID_NO_CONNECT << 16;
1895 scsicmd->scsi_done(scsicmd);
1896 return 0;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /*
1900 * If the target container doesn't exist, it may have
1901 * been newly created
1902 */
1903 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1904 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001905 case SERVICE_ACTION_IN:
1906 if (!(dev->raw_io_interface) ||
1907 !(dev->raw_io_64) ||
1908 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 case INQUIRY:
1911 case READ_CAPACITY:
1912 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001913 if (dev->in_reset)
1914 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07001915 return _aac_probe_container(scsicmd,
1916 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 default:
1918 break;
1919 }
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 } else { /* check for physical non-dasd devices */
Mark Haverkamp653ba582006-09-19 08:59:43 -07001922 if ((dev->nondasd_support == 1) || expose_physicals) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001923 if (dev->in_reset)
1924 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return aac_send_srb_fib(scsicmd);
1926 } else {
1927 scsicmd->result = DID_NO_CONNECT << 16;
1928 scsicmd->scsi_done(scsicmd);
1929 return 0;
1930 }
1931 }
1932 }
1933 /*
1934 * else Command for the controller itself
1935 */
1936 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1937 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1938 {
1939 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1940 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1941 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1942 ILLEGAL_REQUEST,
1943 SENCODE_INVALID_COMMAND,
1944 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1945 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1946 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1947 ? sizeof(scsicmd->sense_buffer)
1948 : sizeof(dev->fsa_dev[cid].sense_data));
1949 scsicmd->scsi_done(scsicmd);
1950 return 0;
1951 }
1952
1953
1954 /* Handle commands here that don't really require going out to the adapter */
1955 switch (scsicmd->cmnd[0]) {
1956 case INQUIRY:
1957 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001958 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001960 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001961 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001963 if (scsicmd->cmnd[1] & 0x1 ) {
1964 char *arr = (char *)&inq_data;
1965
1966 /* EVPD bit set */
1967 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
1968 INQD_PDT_PROC : INQD_PDT_DA;
1969 if (scsicmd->cmnd[2] == 0) {
1970 /* supported vital product data pages */
1971 arr[3] = 2;
1972 arr[4] = 0x0;
1973 arr[5] = 0x80;
1974 arr[1] = scsicmd->cmnd[2];
1975 aac_internal_transfer(scsicmd, &inq_data, 0,
1976 sizeof(inq_data));
1977 scsicmd->result = DID_OK << 16 |
1978 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1979 } else if (scsicmd->cmnd[2] == 0x80) {
1980 /* unit serial number page */
1981 arr[3] = setinqserial(dev, &arr[4],
1982 scmd_id(scsicmd));
1983 arr[1] = scsicmd->cmnd[2];
1984 aac_internal_transfer(scsicmd, &inq_data, 0,
1985 sizeof(inq_data));
1986 return aac_get_container_serial(scsicmd);
1987 } else {
1988 /* vpd page not implemented */
1989 scsicmd->result = DID_OK << 16 |
1990 COMMAND_COMPLETE << 8 |
1991 SAM_STAT_CHECK_CONDITION;
1992 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1993 ILLEGAL_REQUEST,
1994 SENCODE_INVALID_CDB_FIELD,
1995 ASENCODE_NO_SENSE, 0, 7, 2, 0);
1996 memcpy(scsicmd->sense_buffer,
1997 &dev->fsa_dev[cid].sense_data,
1998 (sizeof(dev->fsa_dev[cid].sense_data) >
1999 sizeof(scsicmd->sense_buffer))
2000 ? sizeof(scsicmd->sense_buffer)
2001 : sizeof(dev->fsa_dev[cid].sense_data));
2002 }
2003 scsicmd->scsi_done(scsicmd);
2004 return 0;
2005 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002006 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002007 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 */
2008 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002010 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /*
2012 * Set the Vendor, Product, and Revision Level
2013 * see: <vendor>.c i.e. aac.c
2014 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002015 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07002016 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002017 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
2018 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2020 scsicmd->scsi_done(scsicmd);
2021 return 0;
2022 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002023 if (dev->in_reset)
2024 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07002025 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002026 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
2027 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002028 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002030 case SERVICE_ACTION_IN:
2031 if (!(dev->raw_io_interface) ||
2032 !(dev->raw_io_64) ||
2033 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2034 break;
2035 {
2036 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002037 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002038
2039 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2040 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002041 cp[0] = (capacity >> 56) & 0xff;
2042 cp[1] = (capacity >> 48) & 0xff;
2043 cp[2] = (capacity >> 40) & 0xff;
2044 cp[3] = (capacity >> 32) & 0xff;
2045 cp[4] = (capacity >> 24) & 0xff;
2046 cp[5] = (capacity >> 16) & 0xff;
2047 cp[6] = (capacity >> 8) & 0xff;
2048 cp[7] = (capacity >> 0) & 0xff;
2049 cp[8] = 0;
2050 cp[9] = 0;
2051 cp[10] = 2;
2052 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002053 cp[12] = 0;
2054 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08002055 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002056 if (sizeof(cp) < scsicmd->cmnd[13]) {
2057 unsigned int len, offset = sizeof(cp);
2058
2059 memset(cp, 0, offset);
2060 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002061 len = min_t(size_t, scsicmd->cmnd[13] - offset,
2062 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002063 aac_internal_transfer(scsicmd, cp, offset, len);
2064 } while ((offset += len) < scsicmd->cmnd[13]);
2065 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002066
2067 /* Do not cache partition table for arrays */
2068 scsicmd->device->removable = 1;
2069
2070 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2071 scsicmd->scsi_done(scsicmd);
2072
2073 return 0;
2074 }
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 case READ_CAPACITY:
2077 {
2078 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002079 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002082 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 capacity = fsa_dev_ptr[cid].size - 1;
2084 else
2085 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 cp[0] = (capacity >> 24) & 0xff;
2088 cp[1] = (capacity >> 16) & 0xff;
2089 cp[2] = (capacity >> 8) & 0xff;
2090 cp[3] = (capacity >> 0) & 0xff;
2091 cp[4] = 0;
2092 cp[5] = 0;
2093 cp[6] = 2;
2094 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002095 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002096 /* Do not cache partition table for arrays */
2097 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2100 scsicmd->scsi_done(scsicmd);
2101
2102 return 0;
2103 }
2104
2105 case MODE_SENSE:
2106 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002107 char mode_buf[7];
2108 int mode_buf_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 mode_buf[0] = 3; /* Mode data length */
2112 mode_buf[1] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002113 mode_buf[2] = 0; /* Device-specific param,
2114 bit 8: 0/1 = write enabled/protected
2115 bit 4: 0/1 = FUA enabled */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002116 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002117 mode_buf[2] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 mode_buf[3] = 0; /* Block descriptor length */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002119 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2120 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2121 mode_buf[0] = 6;
2122 mode_buf[4] = 8;
2123 mode_buf[5] = 1;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002124 mode_buf[6] = ((aac_cache & 6) == 2)
2125 ? 0 : 0x04; /* WCE */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002126 mode_buf_length = 7;
2127 if (mode_buf_length > scsicmd->cmnd[4])
2128 mode_buf_length = scsicmd->cmnd[4];
2129 }
2130 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2132 scsicmd->scsi_done(scsicmd);
2133
2134 return 0;
2135 }
2136 case MODE_SENSE_10:
2137 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002138 char mode_buf[11];
2139 int mode_buf_length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 mode_buf[0] = 0; /* Mode data length (MSB) */
2143 mode_buf[1] = 6; /* Mode data length (LSB) */
2144 mode_buf[2] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002145 mode_buf[3] = 0; /* Device-specific param,
2146 bit 8: 0/1 = write enabled/protected
2147 bit 4: 0/1 = FUA enabled */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002148 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002149 mode_buf[3] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 mode_buf[4] = 0; /* reserved */
2151 mode_buf[5] = 0; /* reserved */
2152 mode_buf[6] = 0; /* Block descriptor length (MSB) */
2153 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002154 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2155 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2156 mode_buf[1] = 9;
2157 mode_buf[8] = 8;
2158 mode_buf[9] = 1;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002159 mode_buf[10] = ((aac_cache & 6) == 2)
2160 ? 0 : 0x04; /* WCE */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002161 mode_buf_length = 11;
2162 if (mode_buf_length > scsicmd->cmnd[8])
2163 mode_buf_length = scsicmd->cmnd[8];
2164 }
2165 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2168 scsicmd->scsi_done(scsicmd);
2169
2170 return 0;
2171 }
2172 case REQUEST_SENSE:
2173 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2174 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2175 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2176 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2177 scsicmd->scsi_done(scsicmd);
2178 return 0;
2179
2180 case ALLOW_MEDIUM_REMOVAL:
2181 dprintk((KERN_DEBUG "LOCK command.\n"));
2182 if (scsicmd->cmnd[4])
2183 fsa_dev_ptr[cid].locked = 1;
2184 else
2185 fsa_dev_ptr[cid].locked = 0;
2186
2187 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2188 scsicmd->scsi_done(scsicmd);
2189 return 0;
2190 /*
2191 * These commands are all No-Ops
2192 */
2193 case TEST_UNIT_READY:
2194 case RESERVE:
2195 case RELEASE:
2196 case REZERO_UNIT:
2197 case REASSIGN_BLOCKS:
2198 case SEEK_10:
2199 case START_STOP:
2200 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2201 scsicmd->scsi_done(scsicmd);
2202 return 0;
2203 }
2204
2205 switch (scsicmd->cmnd[0])
2206 {
2207 case READ_6:
2208 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002209 case READ_12:
2210 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002211 if (dev->in_reset)
2212 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Hack to keep track of ordinal number of the device that
2215 * corresponds to a container. Needed to convert
2216 * containers to /dev/sd device names
2217 */
2218
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002219 if (scsicmd->request->rq_disk)
2220 strlcpy(fsa_dev_ptr[cid].devname,
2221 scsicmd->request->rq_disk->disk_name,
2222 min(sizeof(fsa_dev_ptr[cid].devname),
2223 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002224
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002225 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 case WRITE_6:
2228 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002229 case WRITE_12:
2230 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002231 if (dev->in_reset)
2232 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002233 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 case SYNCHRONIZE_CACHE:
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002236 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2237 scsicmd->result = DID_OK << 16 |
2238 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2239 scsicmd->scsi_done(scsicmd);
2240 return 0;
2241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /* Issue FIB to tell Firmware to flush it's cache */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002243 if ((aac_cache & 6) != 2)
2244 return aac_synchronize(scsicmd);
2245 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 default:
2247 /*
2248 * Unhandled commands
2249 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002250 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2252 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
2253 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2254 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
2255 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2256 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
2257 ? sizeof(scsicmd->sense_buffer)
2258 : sizeof(dev->fsa_dev[cid].sense_data));
2259 scsicmd->scsi_done(scsicmd);
2260 return 0;
2261 }
2262}
2263
2264static int query_disk(struct aac_dev *dev, void __user *arg)
2265{
2266 struct aac_query_disk qd;
2267 struct fsa_dev_info *fsa_dev_ptr;
2268
2269 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002270 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002271 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2273 return -EFAULT;
2274 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002275 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2277 {
2278 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2279 return -EINVAL;
2280 qd.instance = dev->scsi_host_ptr->host_no;
2281 qd.bus = 0;
2282 qd.id = CONTAINER_TO_ID(qd.cnum);
2283 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2284 }
2285 else return -EINVAL;
2286
Salyzyn, Markfd622b12007-07-17 10:59:19 -04002287 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2289 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2290
2291 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2292 qd.unmapped = 1;
2293 else
2294 qd.unmapped = 0;
2295
2296 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2297 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2298
2299 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2300 return -EFAULT;
2301 return 0;
2302}
2303
2304static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2305{
2306 struct aac_delete_disk dd;
2307 struct fsa_dev_info *fsa_dev_ptr;
2308
2309 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp651013552006-09-19 08:59:23 -07002310 if (!fsa_dev_ptr)
2311 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2314 return -EFAULT;
2315
2316 if (dd.cnum >= dev->maximum_num_containers)
2317 return -EINVAL;
2318 /*
2319 * Mark this container as being deleted.
2320 */
2321 fsa_dev_ptr[dd.cnum].deleted = 1;
2322 /*
2323 * Mark the container as no longer valid
2324 */
2325 fsa_dev_ptr[dd.cnum].valid = 0;
2326 return 0;
2327}
2328
2329static int delete_disk(struct aac_dev *dev, void __user *arg)
2330{
2331 struct aac_delete_disk dd;
2332 struct fsa_dev_info *fsa_dev_ptr;
2333
2334 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002335 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002336 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2339 return -EFAULT;
2340
2341 if (dd.cnum >= dev->maximum_num_containers)
2342 return -EINVAL;
2343 /*
2344 * If the container is locked, it can not be deleted by the API.
2345 */
2346 if (fsa_dev_ptr[dd.cnum].locked)
2347 return -EBUSY;
2348 else {
2349 /*
2350 * Mark the container as no longer being valid.
2351 */
2352 fsa_dev_ptr[dd.cnum].valid = 0;
2353 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2354 return 0;
2355 }
2356}
2357
2358int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2359{
2360 switch (cmd) {
2361 case FSACTL_QUERY_DISK:
2362 return query_disk(dev, arg);
2363 case FSACTL_DELETE_DISK:
2364 return delete_disk(dev, arg);
2365 case FSACTL_FORCE_DELETE_DISK:
2366 return force_delete_disk(dev, arg);
2367 case FSACTL_GET_CONTAINERS:
2368 return aac_get_containers(dev);
2369 default:
2370 return -ENOTTY;
2371 }
2372}
2373
2374/**
2375 *
2376 * aac_srb_callback
2377 * @context: the context set in the fib - here it is scsi cmd
2378 * @fibptr: pointer to the fib
2379 *
2380 * Handles the completion of a scsi command to a non dasd device
2381 *
2382 */
2383
2384static void aac_srb_callback(void *context, struct fib * fibptr)
2385{
2386 struct aac_dev *dev;
2387 struct aac_srb_reply *srbreply;
2388 struct scsi_cmnd *scsicmd;
2389
2390 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002391
2392 if (!aac_valid_context(scsicmd, fibptr))
2393 return;
2394
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002395 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002397 dev = fibptr->dev;
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2400
2401 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
2402 /*
2403 * Calculate resid for sg
2404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002406 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2407 - le32_to_cpu(srbreply->data_xfer_length));
2408
2409 scsi_dma_unmap(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 /*
2412 * First check the fib status
2413 */
2414
2415 if (le32_to_cpu(srbreply->status) != ST_OK){
2416 int len;
2417 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2418 len = (le32_to_cpu(srbreply->sense_data_size) >
2419 sizeof(scsicmd->sense_buffer)) ?
2420 sizeof(scsicmd->sense_buffer) :
2421 le32_to_cpu(srbreply->sense_data_size);
2422 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2423 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2424 }
2425
2426 /*
2427 * Next check the srb status
2428 */
2429 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2430 case SRB_STATUS_ERROR_RECOVERY:
2431 case SRB_STATUS_PENDING:
2432 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08002433 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 break;
2435 case SRB_STATUS_DATA_OVERRUN:
2436 switch(scsicmd->cmnd[0]){
2437 case READ_6:
2438 case WRITE_6:
2439 case READ_10:
2440 case WRITE_10:
2441 case READ_12:
2442 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002443 case READ_16:
2444 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2446 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2447 } else {
2448 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2449 }
2450 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2451 break;
2452 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08002453 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 break;
2455 }
2456 default:
2457 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2458 break;
2459 }
2460 break;
2461 case SRB_STATUS_ABORTED:
2462 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2463 break;
2464 case SRB_STATUS_ABORT_FAILED:
2465 // Not sure about this one - but assuming the hba was trying to abort for some reason
2466 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2467 break;
2468 case SRB_STATUS_PARITY_ERROR:
2469 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2470 break;
2471 case SRB_STATUS_NO_DEVICE:
2472 case SRB_STATUS_INVALID_PATH_ID:
2473 case SRB_STATUS_INVALID_TARGET_ID:
2474 case SRB_STATUS_INVALID_LUN:
2475 case SRB_STATUS_SELECTION_TIMEOUT:
2476 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2477 break;
2478
2479 case SRB_STATUS_COMMAND_TIMEOUT:
2480 case SRB_STATUS_TIMEOUT:
2481 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2482 break;
2483
2484 case SRB_STATUS_BUSY:
Salyzyn, Mark760af102007-06-19 11:41:21 -04002485 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487
2488 case SRB_STATUS_BUS_RESET:
2489 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2490 break;
2491
2492 case SRB_STATUS_MESSAGE_REJECTED:
2493 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2494 break;
2495 case SRB_STATUS_REQUEST_FLUSHED:
2496 case SRB_STATUS_ERROR:
2497 case SRB_STATUS_INVALID_REQUEST:
2498 case SRB_STATUS_REQUEST_SENSE_FAILED:
2499 case SRB_STATUS_NO_HBA:
2500 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2501 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2502 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2503 case SRB_STATUS_DELAYED_RETRY:
2504 case SRB_STATUS_BAD_FUNCTION:
2505 case SRB_STATUS_NOT_STARTED:
2506 case SRB_STATUS_NOT_IN_USE:
2507 case SRB_STATUS_FORCE_ABORT:
2508 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2509 default:
2510#ifdef AAC_DETAILED_STATUS_INFO
2511 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2512 le32_to_cpu(srbreply->srb_status) & 0x3F,
2513 aac_get_status_string(
2514 le32_to_cpu(srbreply->srb_status) & 0x3F),
2515 scsicmd->cmnd[0],
2516 le32_to_cpu(srbreply->scsi_status));
2517#endif
2518 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2519 break;
2520 }
2521 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2522 int len;
2523 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2524 len = (le32_to_cpu(srbreply->sense_data_size) >
2525 sizeof(scsicmd->sense_buffer)) ?
2526 sizeof(scsicmd->sense_buffer) :
2527 le32_to_cpu(srbreply->sense_data_size);
2528#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002529 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2530 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531#endif
2532 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2533
2534 }
2535 /*
2536 * OR in the scsi status (already shifted up a bit)
2537 */
2538 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2539
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002540 aac_fib_complete(fibptr);
2541 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002542 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545/**
2546 *
2547 * aac_send_scb_fib
2548 * @scsicmd: the scsi command block
2549 *
2550 * This routine will form a FIB and fill in the aac_srb from the
2551 * scsicmd passed in.
2552 */
2553
2554static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2555{
2556 struct fib* cmd_fibcontext;
2557 struct aac_dev* dev;
2558 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Mark Haverkamp84971732005-06-20 11:55:24 -07002560 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002561 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002562 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 scsicmd->result = DID_NO_CONNECT << 16;
2564 scsicmd->scsi_done(scsicmd);
2565 return 0;
2566 }
2567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 /*
2569 * Allocate and initialize a Fib then setup a BlockWrite command
2570 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002571 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return -1;
2573 }
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002574 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 /*
2577 * Check that the command queued to the controller
2578 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002579 if (status == -EINPROGRESS) {
2580 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return 0;
2582 }
2583
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002584 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2585 aac_fib_complete(cmd_fibcontext);
2586 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 return -1;
2589}
2590
2591static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2592{
2593 struct aac_dev *dev;
2594 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002595 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2598 // Get rid of old data
2599 psg->count = 0;
2600 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002601 psg->sg[0].count = 0;
2602
2603 nseg = scsi_dma_map(scsicmd);
2604 BUG_ON(nseg < 0);
2605 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct scatterlist *sg;
2607 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002609 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002611 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2613 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2614 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002617 if (byte_count > scsi_bufflen(scsicmd)) {
2618 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2619 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002620 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002621 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623 /* Check for command underflow */
2624 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2625 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2626 byte_count, scsicmd->underflow);
2627 }
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return byte_count;
2630}
2631
2632
2633static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2634{
2635 struct aac_dev *dev;
2636 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002637 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002638 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2641 // Get rid of old data
2642 psg->count = 0;
2643 psg->sg[0].addr[0] = 0;
2644 psg->sg[0].addr[1] = 0;
2645 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002646
2647 nseg = scsi_dma_map(scsicmd);
2648 BUG_ON(nseg < 0);
2649 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct scatterlist *sg;
2651 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002653 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002654 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002655 addr = sg_dma_address(sg);
2656 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2657 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002658 psg->sg[i].count = cpu_to_le32(count);
2659 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002661 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002663 if (byte_count > scsi_bufflen(scsicmd)) {
2664 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2665 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002666 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002667 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
2669 /* Check for command underflow */
2670 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2671 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2672 byte_count, scsicmd->underflow);
2673 }
2674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return byte_count;
2676}
2677
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002678static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2679{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002680 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002681 int nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002682
2683 // Get rid of old data
2684 psg->count = 0;
2685 psg->sg[0].next = 0;
2686 psg->sg[0].prev = 0;
2687 psg->sg[0].addr[0] = 0;
2688 psg->sg[0].addr[1] = 0;
2689 psg->sg[0].count = 0;
2690 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002691
2692 nseg = scsi_dma_map(scsicmd);
2693 BUG_ON(nseg < 0);
2694 if (nseg) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002695 struct scatterlist *sg;
2696 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002697
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002698 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002699 int count = sg_dma_len(sg);
2700 u64 addr = sg_dma_address(sg);
2701 psg->sg[i].next = 0;
2702 psg->sg[i].prev = 0;
2703 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2704 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2705 psg->sg[i].count = cpu_to_le32(count);
2706 psg->sg[i].flags = 0;
2707 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002708 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002709 psg->count = cpu_to_le32(nseg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002710 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002711 if (byte_count > scsi_bufflen(scsicmd)) {
2712 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2713 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002714 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002715 byte_count = scsi_bufflen(scsicmd);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002716 }
2717 /* Check for command underflow */
2718 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2719 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2720 byte_count, scsicmd->underflow);
2721 }
2722 }
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002723 return byte_count;
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726#ifdef AAC_DETAILED_STATUS_INFO
2727
2728struct aac_srb_status_info {
2729 u32 status;
2730 char *str;
2731};
2732
2733
2734static struct aac_srb_status_info srb_status_info[] = {
2735 { SRB_STATUS_PENDING, "Pending Status"},
2736 { SRB_STATUS_SUCCESS, "Success"},
2737 { SRB_STATUS_ABORTED, "Aborted Command"},
2738 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002739 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 { SRB_STATUS_BUSY, "Device Busy"},
2741 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2742 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2743 { SRB_STATUS_NO_DEVICE, "No Device"},
2744 { SRB_STATUS_TIMEOUT, "Timeout"},
2745 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2746 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2747 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2748 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2749 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2750 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2751 { SRB_STATUS_NO_HBA, "No HBA"},
2752 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2753 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2754 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2755 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2756 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2757 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002758 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2760 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2761 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2762 { SRB_STATUS_NOT_STARTED, "Not Started"},
2763 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2764 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2765 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2766 { 0xff, "Unknown Error"}
2767};
2768
2769char *aac_get_status_string(u32 status)
2770{
2771 int i;
2772
Tobias Klauser6391a112006-06-08 22:23:48 -07002773 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2774 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 return "Bad Status Code";
2778}
2779
2780#endif