blob: 54cdfcc929ae58e1d1b85eec9fb7a666d9836e06 [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;
147static int dacmode = -1;
148
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400149int aac_commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700150int startup_timeout = 180;
151int aif_timeout = 120;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800153module_param(nondasd, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800155module_param(dacmode, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400157module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158MODULE_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 -0700159module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
160MODULE_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.");
161module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
162MODULE_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 -0700163
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700164int numacb = -1;
165module_param(numacb, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800166MODULE_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 -0700167
168int acbsize = -1;
169module_param(acbsize, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800170MODULE_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 -0700171
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400172int update_interval = 30 * 60;
173module_param(update_interval, int, S_IRUGO|S_IWUSR);
174MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync updates issued to adapter.");
175
176int check_interval = 24 * 60 * 60;
177module_param(check_interval, int, S_IRUGO|S_IWUSR);
178MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health checks.");
179
180int check_reset = 1;
181module_param(check_reset, int, S_IRUGO|S_IWUSR);
182MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the adapter.");
183
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800184int expose_physicals = -1;
Mark Haverkamp653ba582006-09-19 08:59:43 -0700185module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800186MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. -1=protect 0=off, 1=on");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700187
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400188int aac_reset_devices = 0;
189module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
190MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700191
192static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
193 struct fib *fibptr) {
194 struct scsi_device *device;
195
196 if (unlikely(!scsicmd || !scsicmd->scsi_done )) {
Salyzyn, Markc835e372007-07-26 14:20:02 -0400197 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
Mark Haverkamp03d44332007-03-15 10:27:45 -0700198 aac_fib_complete(fibptr);
199 aac_fib_free(fibptr);
200 return 0;
201 }
202 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
203 device = scsicmd->device;
204 if (unlikely(!device || !scsi_device_online(device))) {
205 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
206 aac_fib_complete(fibptr);
207 aac_fib_free(fibptr);
208 return 0;
209 }
210 return 1;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
214 * aac_get_config_status - check the adapter configuration
215 * @common: adapter to query
216 *
217 * Query config status, and commit the configuration if needed.
218 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700219int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 int status = 0;
222 struct fib * fibptr;
223
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800224 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -ENOMEM;
226
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800227 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 {
229 struct aac_get_config_status *dinfo;
230 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
231
232 dinfo->command = cpu_to_le32(VM_ContainerConfig);
233 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
234 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
235 }
236
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800237 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 fibptr,
239 sizeof (struct aac_get_config_status),
240 FsaNormal,
241 1, 1,
242 NULL, NULL);
243 if (status < 0 ) {
244 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
245 } else {
246 struct aac_get_config_status_resp *reply
247 = (struct aac_get_config_status_resp *) fib_data(fibptr);
248 dprintk((KERN_WARNING
249 "aac_get_config_status: response=%d status=%d action=%d\n",
250 le32_to_cpu(reply->response),
251 le32_to_cpu(reply->status),
252 le32_to_cpu(reply->data.action)));
253 if ((le32_to_cpu(reply->response) != ST_OK) ||
254 (le32_to_cpu(reply->status) != CT_OK) ||
255 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
256 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
257 status = -EINVAL;
258 }
259 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800260 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
262 if (status >= 0) {
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400263 if ((aac_commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800265 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dinfo = (struct aac_commit_config *) fib_data(fibptr);
267
268 dinfo->command = cpu_to_le32(VM_ContainerConfig);
269 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
270
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800271 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 fibptr,
273 sizeof (struct aac_commit_config),
274 FsaNormal,
275 1, 1,
276 NULL, NULL);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800277 aac_fib_complete(fibptr);
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400278 } else if (aac_commit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 printk(KERN_WARNING
280 "aac_get_config_status: Foreign device configurations are being ignored\n");
281 }
282 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800283 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return status;
285}
286
287/**
288 * aac_get_containers - list containers
289 * @common: adapter to probe
290 *
291 * Make a list of all containers on this controller
292 */
293int aac_get_containers(struct aac_dev *dev)
294{
295 struct fsa_dev_info *fsa_dev_ptr;
296 u32 index;
297 int status = 0;
298 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct aac_get_container_count *dinfo;
300 struct aac_get_container_count_resp *dresp;
301 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
302
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800303 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -ENOMEM;
305
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800306 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
308 dinfo->command = cpu_to_le32(VM_ContainerConfig);
309 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
310
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800311 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 fibptr,
313 sizeof (struct aac_get_container_count),
314 FsaNormal,
315 1, 1,
316 NULL, NULL);
317 if (status >= 0) {
318 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
319 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800320 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700322 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
325 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400326 fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700327 GFP_KERNEL);
328 if (!fsa_dev_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 dev->fsa_dev = fsa_dev_ptr;
332 dev->maximum_num_containers = maximum_num_containers;
333
Mark Haverkampfe76df42007-03-15 12:55:07 -0700334 for (index = 0; index < dev->maximum_num_containers; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 fsa_dev_ptr[index].devname[0] = '\0';
336
Mark Haverkampfe76df42007-03-15 12:55:07 -0700337 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Mark Haverkampfe76df42007-03-15 12:55:07 -0700339 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
341 break;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
345 * If there are no more containers, then stop asking.
346 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700347 if (++index >= status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return status;
351}
352
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700353static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
354{
355 void *buf;
James Bottomleycab537d2007-05-16 10:06:39 -0400356 int transfer_len;
FUJITA Tomonori727eead2007-05-26 02:00:42 +0900357 struct scatterlist *sg = scsi_sglist(scsicmd);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700358
FUJITA Tomonori727eead2007-05-26 02:00:42 +0900359 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
360 transfer_len = min(sg->length, len + offset);
361
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400362 transfer_len -= offset;
James Bottomleycab537d2007-05-16 10:06:39 -0400363 if (buf && transfer_len > 0)
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400364 memcpy(buf + offset, data, transfer_len);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700365
FUJITA Tomonori727eead2007-05-26 02:00:42 +0900366 kunmap_atomic(buf - sg->offset, KM_IRQ0);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700367
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static void get_container_name_callback(void *context, struct fib * fibptr)
371{
372 struct aac_get_name_resp * get_name_reply;
373 struct scsi_cmnd * scsicmd;
374
375 scsicmd = (struct scsi_cmnd *) context;
376
Mark Haverkamp03d44332007-03-15 10:27:45 -0700377 if (!aac_valid_context(scsicmd, fibptr))
378 return;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700381 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
384 /* Failure is irrelevant, using default value instead */
385 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
386 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700387 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
389 while (*sp == ' ')
390 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700391 if (*sp) {
392 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
393 int count = sizeof(d);
394 char *dp = d;
395 do {
396 *dp++ = (*sp) ? *sp++ : ' ';
397 } while (--count > 0);
398 aac_internal_transfer(scsicmd, d,
399 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
404
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800405 aac_fib_complete(fibptr);
406 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700407 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/**
411 * aac_get_container_name - get container name, none blocking.
412 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700413static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 int status;
416 struct aac_get_name *dinfo;
417 struct fib * cmd_fibcontext;
418 struct aac_dev * dev;
419
420 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
421
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800422 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -ENOMEM;
424
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800425 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
427
428 dinfo->command = cpu_to_le32(VM_ContainerConfig);
429 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700430 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
432
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800433 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 cmd_fibcontext,
435 sizeof (struct aac_get_name),
436 FsaNormal,
437 0, 1,
438 (fib_callback) get_container_name_callback,
439 (void *) scsicmd);
440
441 /*
442 * Check that the command queued to the controller
443 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800444 if (status == -EINPROGRESS) {
445 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800449 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
450 aac_fib_complete(cmd_fibcontext);
451 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -1;
453}
454
Mark Haverkampfe76df42007-03-15 12:55:07 -0700455static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
456{
457 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
458
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400459 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700460 return aac_scsi_cmd(scsicmd);
461
462 scsicmd->result = DID_NO_CONNECT << 16;
463 scsicmd->scsi_done(scsicmd);
464 return 0;
465}
466
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400467static void _aac_probe_container2(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700468{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700469 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700470 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700471 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
472
Mark Haverkamp03d44332007-03-15 10:27:45 -0700473
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400474 if (!aac_valid_context(scsicmd, fibptr))
475 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700476
477 scsicmd->SCp.Status = 0;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400478 fsa_dev_ptr = fibptr->dev->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700479 if (fsa_dev_ptr) {
480 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
481 fsa_dev_ptr += scmd_id(scsicmd);
482
483 if ((le32_to_cpu(dresp->status) == ST_OK) &&
484 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
485 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
486 fsa_dev_ptr->valid = 1;
487 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
488 fsa_dev_ptr->size
489 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
490 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
491 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
492 }
493 if ((fsa_dev_ptr->valid & 1) == 0)
494 fsa_dev_ptr->valid = 0;
495 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
496 }
497 aac_fib_complete(fibptr);
498 aac_fib_free(fibptr);
499 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
500 scsicmd->SCp.ptr = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400501 (*callback)(scsicmd);
502 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700503}
504
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400505static void _aac_probe_container1(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700506{
507 struct scsi_cmnd * scsicmd;
508 struct aac_mount * dresp;
509 struct aac_query_mount *dinfo;
510 int status;
511
512 dresp = (struct aac_mount *) fib_data(fibptr);
513 dresp->mnt[0].capacityhigh = 0;
514 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400515 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
516 _aac_probe_container2(context, fibptr);
517 return;
518 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700519 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700520
Mark Haverkamp03d44332007-03-15 10:27:45 -0700521 if (!aac_valid_context(scsicmd, fibptr))
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400522 return;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700523
Mark Haverkampfe76df42007-03-15 12:55:07 -0700524 aac_fib_init(fibptr);
525
526 dinfo = (struct aac_query_mount *)fib_data(fibptr);
527
528 dinfo->command = cpu_to_le32(VM_NameServe64);
529 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
530 dinfo->type = cpu_to_le32(FT_FILESYS);
531
532 status = aac_fib_send(ContainerCommand,
533 fibptr,
534 sizeof(struct aac_query_mount),
535 FsaNormal,
536 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400537 _aac_probe_container2,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700538 (void *) scsicmd);
539 /*
540 * Check that the command queued to the controller
541 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400542 if (status == -EINPROGRESS)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700543 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400544 else if (status < 0) {
Mark Haverkampfe76df42007-03-15 12:55:07 -0700545 /* Inherit results from VM_NameServe, if any */
546 dresp->status = cpu_to_le32(ST_OK);
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400547 _aac_probe_container2(context, fibptr);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700548 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700549}
550
551static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
552{
553 struct fib * fibptr;
554 int status = -ENOMEM;
555
556 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
557 struct aac_query_mount *dinfo;
558
559 aac_fib_init(fibptr);
560
561 dinfo = (struct aac_query_mount *)fib_data(fibptr);
562
563 dinfo->command = cpu_to_le32(VM_NameServe);
564 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
565 dinfo->type = cpu_to_le32(FT_FILESYS);
566 scsicmd->SCp.ptr = (char *)callback;
567
568 status = aac_fib_send(ContainerCommand,
569 fibptr,
570 sizeof(struct aac_query_mount),
571 FsaNormal,
572 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400573 _aac_probe_container1,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700574 (void *) scsicmd);
575 /*
576 * Check that the command queued to the controller
577 */
578 if (status == -EINPROGRESS) {
579 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
580 return 0;
581 }
582 if (status < 0) {
583 scsicmd->SCp.ptr = NULL;
584 aac_fib_complete(fibptr);
585 aac_fib_free(fibptr);
586 }
587 }
588 if (status < 0) {
589 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
590 if (fsa_dev_ptr) {
591 fsa_dev_ptr += scmd_id(scsicmd);
592 if ((fsa_dev_ptr->valid & 1) == 0) {
593 fsa_dev_ptr->valid = 0;
594 return (*callback)(scsicmd);
595 }
596 }
597 }
598 return status;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800602 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * @dev: device to query
604 * @cid: container identifier
605 *
606 * Queries the controller about the given volume. The volume information
607 * is updated in the struct fsa_dev_info structure rather than returned.
608 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700609static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
610{
611 scsicmd->device = NULL;
612 return 0;
613}
614
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800615int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700617 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
618 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Mark Haverkampfe76df42007-03-15 12:55:07 -0700621 if (!scsicmd || !scsidev) {
622 kfree(scsicmd);
623 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700624 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700626 scsicmd->list.next = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400627 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Mark Haverkampfe76df42007-03-15 12:55:07 -0700629 scsicmd->device = scsidev;
630 scsidev->sdev_state = 0;
631 scsidev->id = cid;
632 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Mark Haverkampfe76df42007-03-15 12:55:07 -0700634 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
635 while (scsicmd->device == scsidev)
636 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400637 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700638 status = scsicmd->SCp.Status;
639 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return status;
641}
642
643/* Local Structure to set SCSI inquiry data strings */
644struct scsi_inq {
645 char vid[8]; /* Vendor ID */
646 char pid[16]; /* Product ID */
647 char prl[4]; /* Product Revision Level */
648};
649
650/**
651 * InqStrCopy - string merge
652 * @a: string to copy from
653 * @b: string to copy to
654 *
655 * Copy a String from one location to another
656 * without copying \0
657 */
658
659static void inqstrcpy(char *a, char *b)
660{
661
662 while(*a != (char)0)
663 *b++ = *a++;
664}
665
666static char *container_types[] = {
667 "None",
668 "Volume",
669 "Mirror",
670 "Stripe",
671 "RAID5",
672 "SSRW",
673 "SSRO",
674 "Morph",
675 "Legacy",
676 "RAID4",
677 "RAID10",
678 "RAID00",
679 "V-MIRRORS",
680 "PSEUDO R4",
681 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700682 "RAID5D",
683 "RAID5D0",
684 "RAID1E",
685 "RAID6",
686 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 "Unknown"
688};
689
690
691
692/* Function: setinqstr
693 *
694 * Arguments: [1] pointer to void [1] int
695 *
696 * Purpose: Sets SCSI inquiry data strings for vendor, product
697 * and revision level. Allows strings to be set in platform dependant
698 * files instead of in OS dependant driver source.
699 */
700
Mark Haverkamp794d0602005-10-24 10:51:53 -0700701static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700706 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Mark Haverkamp794d0602005-10-24 10:51:53 -0700708 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
709 char * cp = dev->supplement_adapter_info.AdapterTypeText;
710 int c = sizeof(str->vid);
711 while (*cp && *cp != ' ' && --c)
712 ++cp;
713 c = *cp;
714 *cp = '\0';
715 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
716 str->vid);
717 *cp = c;
718 while (*cp && *cp != ' ')
719 ++cp;
720 while (*cp == ' ')
721 ++cp;
722 /* last six chars reserved for vol type */
723 c = 0;
724 if (strlen(cp) > sizeof(str->pid)) {
725 c = cp[sizeof(str->pid)];
726 cp[sizeof(str->pid)] = '\0';
727 }
728 inqstrcpy (cp, str->pid);
729 if (c)
730 cp[sizeof(str->pid)] = c;
731 } else {
732 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700733
734 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700735 /* last six chars reserved for vol type */
736 inqstrcpy (mp->model, str->pid);
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Tobias Klauser6391a112006-06-08 22:23:48 -0700739 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 char *findit = str->pid;
741
742 for ( ; *findit != ' '; findit++); /* walk till we find a space */
743 /* RAID is superfluous in the context of a RAID device */
744 if (memcmp(findit-4, "RAID", 4) == 0)
745 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700746 if (((findit - str->pid) + strlen(container_types[tindex]))
747 < (sizeof(str->pid) + sizeof(str->prl)))
748 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 inqstrcpy ("V1.0", str->prl);
751}
752
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400753static void get_container_serial_callback(void *context, struct fib * fibptr)
754{
755 struct aac_get_serial_resp * get_serial_reply;
756 struct scsi_cmnd * scsicmd;
757
758 BUG_ON(fibptr == NULL);
759
760 scsicmd = (struct scsi_cmnd *) context;
761 if (!aac_valid_context(scsicmd, fibptr))
762 return;
763
764 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
765 /* Failure is irrelevant, using default value instead */
766 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
767 char sp[13];
768 /* EVPD bit set */
769 sp[0] = INQD_PDT_DA;
770 sp[1] = scsicmd->cmnd[2];
771 sp[2] = 0;
772 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
773 le32_to_cpu(get_serial_reply->uid));
774 aac_internal_transfer(scsicmd, sp, 0, sizeof(sp));
775 }
776
777 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
778
779 aac_fib_complete(fibptr);
780 aac_fib_free(fibptr);
781 scsicmd->scsi_done(scsicmd);
782}
783
784/**
785 * aac_get_container_serial - get container serial, none blocking.
786 */
787static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
788{
789 int status;
790 struct aac_get_serial *dinfo;
791 struct fib * cmd_fibcontext;
792 struct aac_dev * dev;
793
794 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
795
796 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
797 return -ENOMEM;
798
799 aac_fib_init(cmd_fibcontext);
800 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
801
802 dinfo->command = cpu_to_le32(VM_ContainerConfig);
803 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
804 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
805
806 status = aac_fib_send(ContainerCommand,
807 cmd_fibcontext,
808 sizeof (struct aac_get_serial),
809 FsaNormal,
810 0, 1,
811 (fib_callback) get_container_serial_callback,
812 (void *) scsicmd);
813
814 /*
815 * Check that the command queued to the controller
816 */
817 if (status == -EINPROGRESS) {
818 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
819 return 0;
820 }
821
822 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
823 aac_fib_complete(cmd_fibcontext);
824 aac_fib_free(cmd_fibcontext);
825 return -1;
826}
827
828/* Function: setinqserial
829 *
830 * Arguments: [1] pointer to void [1] int
831 *
832 * Purpose: Sets SCSI Unit Serial number.
833 * This is a fake. We should read a proper
834 * serial number from the container. <SuSE>But
835 * without docs it's quite hard to do it :-)
836 * So this will have to do in the meantime.</SuSE>
837 */
838
839static int setinqserial(struct aac_dev *dev, void *data, int cid)
840{
841 /*
842 * This breaks array migration.
843 */
844 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
845 le32_to_cpu(dev->adapter_info.serial[0]), cid);
846}
847
Adrian Bunk 48338692005-04-25 19:45:58 -0700848static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
849 u8 a_sense_code, u8 incorrect_length,
850 u8 bit_pointer, u16 field_pointer,
851 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
854 sense_buf[1] = 0; /* Segment number, always zero */
855
856 if (incorrect_length) {
857 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
858 sense_buf[3] = BYTE3(residue);
859 sense_buf[4] = BYTE2(residue);
860 sense_buf[5] = BYTE1(residue);
861 sense_buf[6] = BYTE0(residue);
862 } else
863 sense_buf[2] = sense_key; /* Sense key */
864
865 if (sense_key == ILLEGAL_REQUEST)
866 sense_buf[7] = 10; /* Additional sense length */
867 else
868 sense_buf[7] = 6; /* Additional sense length */
869
870 sense_buf[12] = sense_code; /* Additional sense code */
871 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
872 if (sense_key == ILLEGAL_REQUEST) {
873 sense_buf[15] = 0;
874
875 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
876 sense_buf[15] = 0x80;/* Std sense key specific field */
877 /* Illegal parameter is in the parameter block */
878
879 if (sense_code == SENCODE_INVALID_CDB_FIELD)
880 sense_buf[15] = 0xc0;/* Std sense key specific field */
881 /* Illegal parameter is in the CDB block */
882 sense_buf[15] |= bit_pointer;
883 sense_buf[16] = field_pointer >> 8; /* MSB */
884 sense_buf[17] = field_pointer; /* LSB */
885 }
886}
887
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800888static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
889{
890 if (lba & 0xffffffff00000000LL) {
891 int cid = scmd_id(cmd);
892 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
893 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
894 SAM_STAT_CHECK_CONDITION;
895 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
896 HARDWARE_ERROR,
897 SENCODE_INTERNAL_TARGET_FAILURE,
898 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
899 0, 0);
900 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
901 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(cmd->sense_buffer))
902 ? sizeof(cmd->sense_buffer)
903 : sizeof(dev->fsa_dev[cid].sense_data));
904 cmd->scsi_done(cmd);
905 return 1;
906 }
907 return 0;
908}
909
910static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
911{
912 return 0;
913}
914
915static void io_callback(void *context, struct fib * fibptr);
916
917static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
918{
919 u16 fibsize;
920 struct aac_raw_io *readcmd;
921 aac_fib_init(fib);
922 readcmd = (struct aac_raw_io *) fib_data(fib);
923 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
924 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
925 readcmd->count = cpu_to_le32(count<<9);
926 readcmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -0400927 readcmd->flags = cpu_to_le16(IO_TYPE_READ);
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800928 readcmd->bpTotal = 0;
929 readcmd->bpComplete = 0;
930
931 aac_build_sgraw(cmd, &readcmd->sg);
932 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
933 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
934 /*
935 * Now send the Fib to the adapter
936 */
937 return aac_fib_send(ContainerRawIo,
938 fib,
939 fibsize,
940 FsaNormal,
941 0, 1,
942 (fib_callback) io_callback,
943 (void *) cmd);
944}
945
946static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
947{
948 u16 fibsize;
949 struct aac_read64 *readcmd;
950 aac_fib_init(fib);
951 readcmd = (struct aac_read64 *) fib_data(fib);
952 readcmd->command = cpu_to_le32(VM_CtHostRead64);
953 readcmd->cid = cpu_to_le16(scmd_id(cmd));
954 readcmd->sector_count = cpu_to_le16(count);
955 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
956 readcmd->pad = 0;
957 readcmd->flags = 0;
958
959 aac_build_sg64(cmd, &readcmd->sg);
960 fibsize = sizeof(struct aac_read64) +
961 ((le32_to_cpu(readcmd->sg.count) - 1) *
962 sizeof (struct sgentry64));
963 BUG_ON (fibsize > (fib->dev->max_fib_size -
964 sizeof(struct aac_fibhdr)));
965 /*
966 * Now send the Fib to the adapter
967 */
968 return aac_fib_send(ContainerCommand64,
969 fib,
970 fibsize,
971 FsaNormal,
972 0, 1,
973 (fib_callback) io_callback,
974 (void *) cmd);
975}
976
977static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
978{
979 u16 fibsize;
980 struct aac_read *readcmd;
981 aac_fib_init(fib);
982 readcmd = (struct aac_read *) fib_data(fib);
983 readcmd->command = cpu_to_le32(VM_CtBlockRead);
984 readcmd->cid = cpu_to_le16(scmd_id(cmd));
985 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
986 readcmd->count = cpu_to_le32(count * 512);
987
988 aac_build_sg(cmd, &readcmd->sg);
989 fibsize = sizeof(struct aac_read) +
990 ((le32_to_cpu(readcmd->sg.count) - 1) *
991 sizeof (struct sgentry));
992 BUG_ON (fibsize > (fib->dev->max_fib_size -
993 sizeof(struct aac_fibhdr)));
994 /*
995 * Now send the Fib to the adapter
996 */
997 return aac_fib_send(ContainerCommand,
998 fib,
999 fibsize,
1000 FsaNormal,
1001 0, 1,
1002 (fib_callback) io_callback,
1003 (void *) cmd);
1004}
1005
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001006static 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 -08001007{
1008 u16 fibsize;
1009 struct aac_raw_io *writecmd;
1010 aac_fib_init(fib);
1011 writecmd = (struct aac_raw_io *) fib_data(fib);
1012 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1013 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1014 writecmd->count = cpu_to_le32(count<<9);
1015 writecmd->cid = cpu_to_le16(scmd_id(cmd));
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001016 writecmd->flags = fua ?
1017 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
1018 cpu_to_le16(IO_TYPE_WRITE);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001019 writecmd->bpTotal = 0;
1020 writecmd->bpComplete = 0;
1021
1022 aac_build_sgraw(cmd, &writecmd->sg);
1023 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1024 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1025 /*
1026 * Now send the Fib to the adapter
1027 */
1028 return aac_fib_send(ContainerRawIo,
1029 fib,
1030 fibsize,
1031 FsaNormal,
1032 0, 1,
1033 (fib_callback) io_callback,
1034 (void *) cmd);
1035}
1036
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001037static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001038{
1039 u16 fibsize;
1040 struct aac_write64 *writecmd;
1041 aac_fib_init(fib);
1042 writecmd = (struct aac_write64 *) fib_data(fib);
1043 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1044 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1045 writecmd->sector_count = cpu_to_le16(count);
1046 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1047 writecmd->pad = 0;
1048 writecmd->flags = 0;
1049
1050 aac_build_sg64(cmd, &writecmd->sg);
1051 fibsize = sizeof(struct aac_write64) +
1052 ((le32_to_cpu(writecmd->sg.count) - 1) *
1053 sizeof (struct sgentry64));
1054 BUG_ON (fibsize > (fib->dev->max_fib_size -
1055 sizeof(struct aac_fibhdr)));
1056 /*
1057 * Now send the Fib to the adapter
1058 */
1059 return aac_fib_send(ContainerCommand64,
1060 fib,
1061 fibsize,
1062 FsaNormal,
1063 0, 1,
1064 (fib_callback) io_callback,
1065 (void *) cmd);
1066}
1067
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001068static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001069{
1070 u16 fibsize;
1071 struct aac_write *writecmd;
1072 aac_fib_init(fib);
1073 writecmd = (struct aac_write *) fib_data(fib);
1074 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1075 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1076 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1077 writecmd->count = cpu_to_le32(count * 512);
1078 writecmd->sg.count = cpu_to_le32(1);
1079 /* ->stable is not used - it did mean which type of write */
1080
1081 aac_build_sg(cmd, &writecmd->sg);
1082 fibsize = sizeof(struct aac_write) +
1083 ((le32_to_cpu(writecmd->sg.count) - 1) *
1084 sizeof (struct sgentry));
1085 BUG_ON (fibsize > (fib->dev->max_fib_size -
1086 sizeof(struct aac_fibhdr)));
1087 /*
1088 * Now send the Fib to the adapter
1089 */
1090 return aac_fib_send(ContainerCommand,
1091 fib,
1092 fibsize,
1093 FsaNormal,
1094 0, 1,
1095 (fib_callback) io_callback,
1096 (void *) cmd);
1097}
1098
1099static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1100{
1101 struct aac_srb * srbcmd;
1102 u32 flag;
1103 u32 timeout;
1104
1105 aac_fib_init(fib);
1106 switch(cmd->sc_data_direction){
1107 case DMA_TO_DEVICE:
1108 flag = SRB_DataOut;
1109 break;
1110 case DMA_BIDIRECTIONAL:
1111 flag = SRB_DataIn | SRB_DataOut;
1112 break;
1113 case DMA_FROM_DEVICE:
1114 flag = SRB_DataIn;
1115 break;
1116 case DMA_NONE:
1117 default: /* shuts up some versions of gcc */
1118 flag = SRB_NoDataXfer;
1119 break;
1120 }
1121
1122 srbcmd = (struct aac_srb*) fib_data(fib);
1123 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1124 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1125 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1126 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1127 srbcmd->flags = cpu_to_le32(flag);
1128 timeout = cmd->timeout_per_command/HZ;
1129 if (timeout == 0)
1130 timeout = 1;
1131 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1132 srbcmd->retry_limit = 0; /* Obsolete parameter */
1133 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1134 return srbcmd;
1135}
1136
1137static void aac_srb_callback(void *context, struct fib * fibptr);
1138
1139static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1140{
1141 u16 fibsize;
1142 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1143
1144 aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001145 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001146
1147 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1148 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1149 /*
1150 * Build Scatter/Gather list
1151 */
1152 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1153 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1154 sizeof (struct sgentry64));
1155 BUG_ON (fibsize > (fib->dev->max_fib_size -
1156 sizeof(struct aac_fibhdr)));
1157
1158 /*
1159 * Now send the Fib to the adapter
1160 */
1161 return aac_fib_send(ScsiPortCommand64, fib,
1162 fibsize, FsaNormal, 0, 1,
1163 (fib_callback) aac_srb_callback,
1164 (void *) cmd);
1165}
1166
1167static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1168{
1169 u16 fibsize;
1170 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1171
1172 aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001173 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001174
1175 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1176 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1177 /*
1178 * Build Scatter/Gather list
1179 */
1180 fibsize = sizeof (struct aac_srb) +
1181 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1182 sizeof (struct sgentry));
1183 BUG_ON (fibsize > (fib->dev->max_fib_size -
1184 sizeof(struct aac_fibhdr)));
1185
1186 /*
1187 * Now send the Fib to the adapter
1188 */
1189 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1190 (fib_callback) aac_srb_callback, (void *) cmd);
1191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193int aac_get_adapter_info(struct aac_dev* dev)
1194{
1195 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int rcode;
1197 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001198 struct aac_adapter_info *info;
1199 struct aac_bus_info *command;
1200 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001201
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001202 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -ENOMEM;
1204
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001205 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001206 info = (struct aac_adapter_info *) fib_data(fibptr);
1207 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001209 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001210 fibptr,
1211 sizeof(*info),
1212 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001213 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001214 NULL,
1215 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001217 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001218 aac_fib_complete(fibptr);
1219 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001220 return rcode;
1221 }
1222 memcpy(&dev->adapter_info, info, sizeof(*info));
1223
1224 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1225 struct aac_supplement_adapter_info * info;
1226
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001227 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001228
1229 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1230
1231 memset(info,0,sizeof(*info));
1232
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001233 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001234 fibptr,
1235 sizeof(*info),
1236 FsaNormal,
1237 1, 1,
1238 NULL,
1239 NULL);
1240
1241 if (rcode >= 0)
1242 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Mark Haverkamp84971732005-06-20 11:55:24 -07001245
1246 /*
1247 * GetBusInfo
1248 */
1249
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001250 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001251
1252 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1253
1254 memset(bus_info, 0, sizeof(*bus_info));
1255
1256 command = (struct aac_bus_info *)bus_info;
1257
1258 command->Command = cpu_to_le32(VM_Ioctl);
1259 command->ObjType = cpu_to_le32(FT_DRIVE);
1260 command->MethodId = cpu_to_le32(1);
1261 command->CtlCmd = cpu_to_le32(GetBusInfo);
1262
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001263 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001264 fibptr,
1265 sizeof (*bus_info),
1266 FsaNormal,
1267 1, 1,
1268 NULL, NULL);
1269
1270 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1271 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1272 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1273 }
1274
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001275 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001276 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001277 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1278 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 dev->name,
1280 dev->id,
1281 tmp>>24,
1282 (tmp>>16)&0xff,
1283 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001284 le32_to_cpu(dev->adapter_info.kernelbuild),
1285 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1286 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001287 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1288 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 dev->name, dev->id,
1290 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1291 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001292 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1293 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 dev->name, dev->id,
1295 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1296 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001297 buffer[0] = '\0';
1298 if (aac_show_serial_number(
1299 shost_to_class(dev->scsi_host_ptr), buffer))
1300 printk(KERN_INFO "%s%d: serial %s",
1301 dev->name, dev->id, buffer);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001302 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1303 printk(KERN_INFO "%s%d: TSID %.*s\n",
1304 dev->name, dev->id,
1305 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1306 dev->supplement_adapter_info.VpdInfo.Tsid);
1307 }
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001308 if (!check_reset ||
1309 (dev->supplement_adapter_info.SupportedOptions2 &
1310 le32_to_cpu(AAC_OPTION_IGNORE_RESET))) {
1311 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1312 dev->name, dev->id);
1313 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 dev->nondasd_support = 0;
1317 dev->raid_scsi_mode = 0;
1318 if(dev->adapter_info.options & AAC_OPT_NONDASD){
1319 dev->nondasd_support = 1;
1320 }
1321
1322 /*
1323 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1324 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1325 * force nondasd support on. If we decide to allow the non-dasd flag
1326 * additional changes changes will have to be made to support
1327 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1328 * changed to support the new dev->raid_scsi_mode flag instead of
1329 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1330 * function aac_detect will have to be modified where it sets up the
1331 * max number of channels based on the aac->nondasd_support flag only.
1332 */
1333 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1334 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1335 dev->nondasd_support = 1;
1336 dev->raid_scsi_mode = 1;
1337 }
1338 if (dev->raid_scsi_mode != 0)
1339 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1340 dev->name, dev->id);
1341
1342 if(nondasd != -1) {
1343 dev->nondasd_support = (nondasd!=0);
1344 }
1345 if(dev->nondasd_support != 0){
1346 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1347 }
1348
1349 dev->dac_support = 0;
1350 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1351 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1352 dev->dac_support = 1;
1353 }
1354
1355 if(dacmode != -1) {
1356 dev->dac_support = (dacmode!=0);
1357 }
1358 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -05001359 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1360 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1362 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -05001363 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1364 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1366 dev->name, dev->id);
1367 dev->dac_support = 0;
1368 } else {
1369 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1370 dev->name, dev->id);
1371 rcode = -ENOMEM;
1372 }
1373 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001374 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001375 * Deal with configuring for the individualized limits of each packet
1376 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001377 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001378 dev->a_ops.adapter_scsi = (dev->dac_support)
1379 ? aac_scsi_64
1380 : aac_scsi_32;
1381 if (dev->raw_io_interface) {
1382 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1383 ? aac_bounds_64
1384 : aac_bounds_32;
1385 dev->a_ops.adapter_read = aac_read_raw_io;
1386 dev->a_ops.adapter_write = aac_write_raw_io;
1387 } else {
1388 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001389 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001390 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001391 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1392 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001393 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001394 dev->a_ops.adapter_read = aac_read_block64;
1395 dev->a_ops.adapter_write = aac_write_block64;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001396 /*
1397 * 38 scatter gather elements
1398 */
1399 dev->scsi_host_ptr->sg_tablesize =
1400 (dev->max_fib_size -
1401 sizeof(struct aac_fibhdr) -
1402 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001403 sizeof(struct sgentry64)) /
1404 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001405 } else {
1406 dev->a_ops.adapter_read = aac_read_block;
1407 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001408 }
1409 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1410 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1411 /*
1412 * Worst case size that could cause sg overflow when
1413 * we break up SG elements that are larger than 64KB.
1414 * Would be nice if we could tell the SCSI layer what
1415 * the maximum SG element size can be. Worst case is
1416 * (sg_tablesize-1) 4KB elements with one 64KB
1417 * element.
1418 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1419 */
1420 dev->scsi_host_ptr->max_sectors =
1421 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1422 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001425 aac_fib_complete(fibptr);
1426 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 return rcode;
1429}
1430
1431
Mark Haverkampe53cb352005-08-03 15:39:09 -07001432static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct aac_dev *dev;
1435 struct aac_read_reply *readreply;
1436 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 u32 cid;
1438
1439 scsicmd = (struct scsi_cmnd *) context;
1440
Mark Haverkamp03d44332007-03-15 10:27:45 -07001441 if (!aac_valid_context(scsicmd, fibptr))
1442 return;
1443
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001444 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001445 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001447 if (nblank(dprintk(x))) {
1448 u64 lba;
1449 switch (scsicmd->cmnd[0]) {
1450 case WRITE_6:
1451 case READ_6:
1452 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1453 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1454 break;
1455 case WRITE_16:
1456 case READ_16:
1457 lba = ((u64)scsicmd->cmnd[2] << 56) |
1458 ((u64)scsicmd->cmnd[3] << 48) |
1459 ((u64)scsicmd->cmnd[4] << 40) |
1460 ((u64)scsicmd->cmnd[5] << 32) |
1461 ((u64)scsicmd->cmnd[6] << 24) |
1462 (scsicmd->cmnd[7] << 16) |
1463 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1464 break;
1465 case WRITE_12:
1466 case READ_12:
1467 lba = ((u64)scsicmd->cmnd[2] << 24) |
1468 (scsicmd->cmnd[3] << 16) |
1469 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1470 break;
1471 default:
1472 lba = ((u64)scsicmd->cmnd[2] << 24) |
1473 (scsicmd->cmnd[3] << 16) |
1474 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1475 break;
1476 }
1477 printk(KERN_DEBUG
1478 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1479 smp_processor_id(), (unsigned long long)lba, jiffies);
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001482 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001483
1484 scsi_dma_unmap(scsicmd);
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 readreply = (struct aac_read_reply *)fib_data(fibptr);
1487 if (le32_to_cpu(readreply->status) == ST_OK)
1488 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1489 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001490#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001491 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001492 le32_to_cpu(readreply->status));
1493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1495 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1496 HARDWARE_ERROR,
1497 SENCODE_INTERNAL_TARGET_FAILURE,
1498 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1499 0, 0);
1500 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1501 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1502 ? sizeof(scsicmd->sense_buffer)
1503 : sizeof(dev->fsa_dev[cid].sense_data));
1504 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001505 aac_fib_complete(fibptr);
1506 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001508 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001511static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001513 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 u32 count;
1515 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 struct aac_dev *dev;
1517 struct fib * cmd_fibcontext;
1518
1519 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1520 /*
1521 * Get block address and transfer length
1522 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001523 switch (scsicmd->cmnd[0]) {
1524 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001525 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001527 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1528 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 count = scsicmd->cmnd[4];
1530
1531 if (count == 0)
1532 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001533 break;
1534 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001535 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001536
1537 lba = ((u64)scsicmd->cmnd[2] << 56) |
1538 ((u64)scsicmd->cmnd[3] << 48) |
1539 ((u64)scsicmd->cmnd[4] << 40) |
1540 ((u64)scsicmd->cmnd[5] << 32) |
1541 ((u64)scsicmd->cmnd[6] << 24) |
1542 (scsicmd->cmnd[7] << 16) |
1543 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1544 count = (scsicmd->cmnd[10] << 24) |
1545 (scsicmd->cmnd[11] << 16) |
1546 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1547 break;
1548 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001549 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001550
1551 lba = ((u64)scsicmd->cmnd[2] << 24) |
1552 (scsicmd->cmnd[3] << 16) |
1553 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1554 count = (scsicmd->cmnd[6] << 24) |
1555 (scsicmd->cmnd[7] << 16) |
1556 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1557 break;
1558 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001559 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001561 lba = ((u64)scsicmd->cmnd[2] << 24) |
1562 (scsicmd->cmnd[3] << 16) |
1563 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001567 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001568 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001569 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /*
1572 * Alocate and initialize a Fib
1573 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001574 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return -1;
1576 }
1577
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001578 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /*
1581 * Check that the command queued to the controller
1582 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001583 if (status == -EINPROGRESS) {
1584 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001588 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * For some reason, the Fib didn't queue, return QUEUE_FULL
1591 */
1592 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001593 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001594 aac_fib_complete(cmd_fibcontext);
1595 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return 0;
1597}
1598
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001599static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001601 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001603 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 struct aac_dev *dev;
1606 struct fib * cmd_fibcontext;
1607
1608 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1609 /*
1610 * Get block address and transfer length
1611 */
1612 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1613 {
1614 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1615 count = scsicmd->cmnd[4];
1616 if (count == 0)
1617 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001618 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001619 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001620 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001621
1622 lba = ((u64)scsicmd->cmnd[2] << 56) |
1623 ((u64)scsicmd->cmnd[3] << 48) |
1624 ((u64)scsicmd->cmnd[4] << 40) |
1625 ((u64)scsicmd->cmnd[5] << 32) |
1626 ((u64)scsicmd->cmnd[6] << 24) |
1627 (scsicmd->cmnd[7] << 16) |
1628 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1629 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1630 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001631 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001632 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001633 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001634
1635 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1636 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1637 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1638 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001639 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001641 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001642 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001644 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001646 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001648 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 /*
1651 * Allocate and initialize a Fib then setup a BlockWrite command
1652 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001653 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001655 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return 0;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001659 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /*
1662 * Check that the command queued to the controller
1663 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001664 if (status == -EINPROGRESS) {
1665 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return 0;
1667 }
1668
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001669 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /*
1671 * For some reason, the Fib didn't queue, return QUEUE_FULL
1672 */
1673 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001674 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001676 aac_fib_complete(cmd_fibcontext);
1677 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return 0;
1679}
1680
1681static void synchronize_callback(void *context, struct fib *fibptr)
1682{
1683 struct aac_synchronize_reply *synchronizereply;
1684 struct scsi_cmnd *cmd;
1685
1686 cmd = context;
1687
Mark Haverkamp03d44332007-03-15 10:27:45 -07001688 if (!aac_valid_context(cmd, fibptr))
1689 return;
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1692 smp_processor_id(), jiffies));
1693 BUG_ON(fibptr == NULL);
1694
1695
1696 synchronizereply = fib_data(fibptr);
1697 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1698 cmd->result = DID_OK << 16 |
1699 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1700 else {
1701 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001702 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001703 u32 cid = sdev_id(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 printk(KERN_WARNING
1705 "synchronize_callback: synchronize failed, status = %d\n",
1706 le32_to_cpu(synchronizereply->status));
1707 cmd->result = DID_OK << 16 |
1708 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1709 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1710 HARDWARE_ERROR,
1711 SENCODE_INTERNAL_TARGET_FAILURE,
1712 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1713 0, 0);
1714 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1715 min(sizeof(dev->fsa_dev[cid].sense_data),
1716 sizeof(cmd->sense_buffer)));
1717 }
1718
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001719 aac_fib_complete(fibptr);
1720 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001721 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001724static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 int status;
1727 struct fib *cmd_fibcontext;
1728 struct aac_synchronize *synchronizecmd;
1729 struct scsi_cmnd *cmd;
1730 struct scsi_device *sdev = scsicmd->device;
1731 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001732 struct aac_dev *aac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 unsigned long flags;
1734
1735 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001736 * Wait for all outstanding queued commands to complete to this
1737 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
1739 spin_lock_irqsave(&sdev->list_lock, flags);
1740 list_for_each_entry(cmd, &sdev->cmd_list, list)
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001741 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 ++active;
1743 break;
1744 }
1745
1746 spin_unlock_irqrestore(&sdev->list_lock, flags);
1747
1748 /*
1749 * Yield the processor (requeue for later)
1750 */
1751 if (active)
1752 return SCSI_MLQUEUE_DEVICE_BUSY;
1753
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001754 aac = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001755 if (aac->in_reset)
1756 return SCSI_MLQUEUE_HOST_BUSY;
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001759 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001761 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return SCSI_MLQUEUE_HOST_BUSY;
1763
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001764 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 synchronizecmd = fib_data(cmd_fibcontext);
1767 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1768 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001769 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 synchronizecmd->count =
1771 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1772
1773 /*
1774 * Now send the Fib to the adapter
1775 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001776 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 cmd_fibcontext,
1778 sizeof(struct aac_synchronize),
1779 FsaNormal,
1780 0, 1,
1781 (fib_callback)synchronize_callback,
1782 (void *)scsicmd);
1783
1784 /*
1785 * Check that the command queued to the controller
1786 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001787 if (status == -EINPROGRESS) {
1788 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001793 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1794 aac_fib_complete(cmd_fibcontext);
1795 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return SCSI_MLQUEUE_HOST_BUSY;
1797}
1798
1799/**
1800 * aac_scsi_cmd() - Process SCSI command
1801 * @scsicmd: SCSI command block
1802 *
1803 * Emulate a SCSI command and queue the required request for the
1804 * aacraid firmware.
1805 */
1806
1807int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1808{
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001809 u32 cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct Scsi_Host *host = scsicmd->device->host;
1811 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1812 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001814 if (fsa_dev_ptr == NULL)
1815 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
1817 * If the bus, id or lun is out of range, return fail
1818 * Test does not apply to ID 16, the pseudo id for the controller
1819 * itself.
1820 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001821 cid = scmd_id(scsicmd);
1822 if (cid != host->this_id) {
1823 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
1824 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08001825 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 scsicmd->result = DID_NO_CONNECT << 16;
1827 scsicmd->scsi_done(scsicmd);
1828 return 0;
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /*
1832 * If the target container doesn't exist, it may have
1833 * been newly created
1834 */
1835 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1836 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001837 case SERVICE_ACTION_IN:
1838 if (!(dev->raw_io_interface) ||
1839 !(dev->raw_io_64) ||
1840 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 case INQUIRY:
1843 case READ_CAPACITY:
1844 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001845 if (dev->in_reset)
1846 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07001847 return _aac_probe_container(scsicmd,
1848 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 default:
1850 break;
1851 }
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 } else { /* check for physical non-dasd devices */
Mark Haverkamp653ba582006-09-19 08:59:43 -07001854 if ((dev->nondasd_support == 1) || expose_physicals) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001855 if (dev->in_reset)
1856 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return aac_send_srb_fib(scsicmd);
1858 } else {
1859 scsicmd->result = DID_NO_CONNECT << 16;
1860 scsicmd->scsi_done(scsicmd);
1861 return 0;
1862 }
1863 }
1864 }
1865 /*
1866 * else Command for the controller itself
1867 */
1868 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1869 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1870 {
1871 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1872 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1873 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1874 ILLEGAL_REQUEST,
1875 SENCODE_INVALID_COMMAND,
1876 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1877 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1878 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1879 ? sizeof(scsicmd->sense_buffer)
1880 : sizeof(dev->fsa_dev[cid].sense_data));
1881 scsicmd->scsi_done(scsicmd);
1882 return 0;
1883 }
1884
1885
1886 /* Handle commands here that don't really require going out to the adapter */
1887 switch (scsicmd->cmnd[0]) {
1888 case INQUIRY:
1889 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001890 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001892 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001893 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001895 if (scsicmd->cmnd[1] & 0x1 ) {
1896 char *arr = (char *)&inq_data;
1897
1898 /* EVPD bit set */
1899 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
1900 INQD_PDT_PROC : INQD_PDT_DA;
1901 if (scsicmd->cmnd[2] == 0) {
1902 /* supported vital product data pages */
1903 arr[3] = 2;
1904 arr[4] = 0x0;
1905 arr[5] = 0x80;
1906 arr[1] = scsicmd->cmnd[2];
1907 aac_internal_transfer(scsicmd, &inq_data, 0,
1908 sizeof(inq_data));
1909 scsicmd->result = DID_OK << 16 |
1910 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1911 } else if (scsicmd->cmnd[2] == 0x80) {
1912 /* unit serial number page */
1913 arr[3] = setinqserial(dev, &arr[4],
1914 scmd_id(scsicmd));
1915 arr[1] = scsicmd->cmnd[2];
1916 aac_internal_transfer(scsicmd, &inq_data, 0,
1917 sizeof(inq_data));
1918 return aac_get_container_serial(scsicmd);
1919 } else {
1920 /* vpd page not implemented */
1921 scsicmd->result = DID_OK << 16 |
1922 COMMAND_COMPLETE << 8 |
1923 SAM_STAT_CHECK_CONDITION;
1924 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1925 ILLEGAL_REQUEST,
1926 SENCODE_INVALID_CDB_FIELD,
1927 ASENCODE_NO_SENSE, 0, 7, 2, 0);
1928 memcpy(scsicmd->sense_buffer,
1929 &dev->fsa_dev[cid].sense_data,
1930 (sizeof(dev->fsa_dev[cid].sense_data) >
1931 sizeof(scsicmd->sense_buffer))
1932 ? sizeof(scsicmd->sense_buffer)
1933 : sizeof(dev->fsa_dev[cid].sense_data));
1934 }
1935 scsicmd->scsi_done(scsicmd);
1936 return 0;
1937 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001938 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001939 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 */
1940 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001942 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /*
1944 * Set the Vendor, Product, and Revision Level
1945 * see: <vendor>.c i.e. aac.c
1946 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001947 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07001948 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001949 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1950 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1952 scsicmd->scsi_done(scsicmd);
1953 return 0;
1954 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001955 if (dev->in_reset)
1956 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07001957 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001958 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1959 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001960 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001962 case SERVICE_ACTION_IN:
1963 if (!(dev->raw_io_interface) ||
1964 !(dev->raw_io_64) ||
1965 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1966 break;
1967 {
1968 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001969 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001970
1971 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1972 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001973 cp[0] = (capacity >> 56) & 0xff;
1974 cp[1] = (capacity >> 48) & 0xff;
1975 cp[2] = (capacity >> 40) & 0xff;
1976 cp[3] = (capacity >> 32) & 0xff;
1977 cp[4] = (capacity >> 24) & 0xff;
1978 cp[5] = (capacity >> 16) & 0xff;
1979 cp[6] = (capacity >> 8) & 0xff;
1980 cp[7] = (capacity >> 0) & 0xff;
1981 cp[8] = 0;
1982 cp[9] = 0;
1983 cp[10] = 2;
1984 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001985 cp[12] = 0;
1986 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08001987 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001988 if (sizeof(cp) < scsicmd->cmnd[13]) {
1989 unsigned int len, offset = sizeof(cp);
1990
1991 memset(cp, 0, offset);
1992 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08001993 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1994 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001995 aac_internal_transfer(scsicmd, cp, offset, len);
1996 } while ((offset += len) < scsicmd->cmnd[13]);
1997 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001998
1999 /* Do not cache partition table for arrays */
2000 scsicmd->device->removable = 1;
2001
2002 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2003 scsicmd->scsi_done(scsicmd);
2004
2005 return 0;
2006 }
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 case READ_CAPACITY:
2009 {
2010 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002011 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002014 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 capacity = fsa_dev_ptr[cid].size - 1;
2016 else
2017 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 cp[0] = (capacity >> 24) & 0xff;
2020 cp[1] = (capacity >> 16) & 0xff;
2021 cp[2] = (capacity >> 8) & 0xff;
2022 cp[3] = (capacity >> 0) & 0xff;
2023 cp[4] = 0;
2024 cp[5] = 0;
2025 cp[6] = 2;
2026 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002027 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002028 /* Do not cache partition table for arrays */
2029 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2032 scsicmd->scsi_done(scsicmd);
2033
2034 return 0;
2035 }
2036
2037 case MODE_SENSE:
2038 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002039 char mode_buf[7];
2040 int mode_buf_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 mode_buf[0] = 3; /* Mode data length */
2044 mode_buf[1] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002045 mode_buf[2] = 0; /* Device-specific param,
2046 bit 8: 0/1 = write enabled/protected
2047 bit 4: 0/1 = FUA enabled */
2048 if (dev->raw_io_interface)
2049 mode_buf[2] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 mode_buf[3] = 0; /* Block descriptor length */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002051 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2052 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2053 mode_buf[0] = 6;
2054 mode_buf[4] = 8;
2055 mode_buf[5] = 1;
2056 mode_buf[6] = 0x04; /* WCE */
2057 mode_buf_length = 7;
2058 if (mode_buf_length > scsicmd->cmnd[4])
2059 mode_buf_length = scsicmd->cmnd[4];
2060 }
2061 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2063 scsicmd->scsi_done(scsicmd);
2064
2065 return 0;
2066 }
2067 case MODE_SENSE_10:
2068 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002069 char mode_buf[11];
2070 int mode_buf_length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 mode_buf[0] = 0; /* Mode data length (MSB) */
2074 mode_buf[1] = 6; /* Mode data length (LSB) */
2075 mode_buf[2] = 0; /* Medium type - default */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002076 mode_buf[3] = 0; /* Device-specific param,
2077 bit 8: 0/1 = write enabled/protected
2078 bit 4: 0/1 = FUA enabled */
2079 if (dev->raw_io_interface)
2080 mode_buf[3] = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 mode_buf[4] = 0; /* reserved */
2082 mode_buf[5] = 0; /* reserved */
2083 mode_buf[6] = 0; /* Block descriptor length (MSB) */
2084 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002085 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2086 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2087 mode_buf[1] = 9;
2088 mode_buf[8] = 8;
2089 mode_buf[9] = 1;
2090 mode_buf[10] = 0x04; /* WCE */
2091 mode_buf_length = 11;
2092 if (mode_buf_length > scsicmd->cmnd[8])
2093 mode_buf_length = scsicmd->cmnd[8];
2094 }
2095 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2098 scsicmd->scsi_done(scsicmd);
2099
2100 return 0;
2101 }
2102 case REQUEST_SENSE:
2103 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2104 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2105 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2106 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2107 scsicmd->scsi_done(scsicmd);
2108 return 0;
2109
2110 case ALLOW_MEDIUM_REMOVAL:
2111 dprintk((KERN_DEBUG "LOCK command.\n"));
2112 if (scsicmd->cmnd[4])
2113 fsa_dev_ptr[cid].locked = 1;
2114 else
2115 fsa_dev_ptr[cid].locked = 0;
2116
2117 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2118 scsicmd->scsi_done(scsicmd);
2119 return 0;
2120 /*
2121 * These commands are all No-Ops
2122 */
2123 case TEST_UNIT_READY:
2124 case RESERVE:
2125 case RELEASE:
2126 case REZERO_UNIT:
2127 case REASSIGN_BLOCKS:
2128 case SEEK_10:
2129 case START_STOP:
2130 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2131 scsicmd->scsi_done(scsicmd);
2132 return 0;
2133 }
2134
2135 switch (scsicmd->cmnd[0])
2136 {
2137 case READ_6:
2138 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002139 case READ_12:
2140 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002141 if (dev->in_reset)
2142 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /*
2144 * Hack to keep track of ordinal number of the device that
2145 * corresponds to a container. Needed to convert
2146 * containers to /dev/sd device names
2147 */
2148
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002149 if (scsicmd->request->rq_disk)
2150 strlcpy(fsa_dev_ptr[cid].devname,
2151 scsicmd->request->rq_disk->disk_name,
2152 min(sizeof(fsa_dev_ptr[cid].devname),
2153 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002154
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002155 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 case WRITE_6:
2158 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002159 case WRITE_12:
2160 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002161 if (dev->in_reset)
2162 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002163 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 case SYNCHRONIZE_CACHE:
2166 /* Issue FIB to tell Firmware to flush it's cache */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002167 return aac_synchronize(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 default:
2170 /*
2171 * Unhandled commands
2172 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002173 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2175 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
2176 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2177 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
2178 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2179 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
2180 ? sizeof(scsicmd->sense_buffer)
2181 : sizeof(dev->fsa_dev[cid].sense_data));
2182 scsicmd->scsi_done(scsicmd);
2183 return 0;
2184 }
2185}
2186
2187static int query_disk(struct aac_dev *dev, void __user *arg)
2188{
2189 struct aac_query_disk qd;
2190 struct fsa_dev_info *fsa_dev_ptr;
2191
2192 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002193 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07002194 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2196 return -EFAULT;
2197 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002198 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2200 {
2201 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2202 return -EINVAL;
2203 qd.instance = dev->scsi_host_ptr->host_no;
2204 qd.bus = 0;
2205 qd.id = CONTAINER_TO_ID(qd.cnum);
2206 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2207 }
2208 else return -EINVAL;
2209
Salyzyn, Markfd622b12007-07-17 10:59:19 -04002210 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2212 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2213
2214 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2215 qd.unmapped = 1;
2216 else
2217 qd.unmapped = 0;
2218
2219 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2220 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2221
2222 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2223 return -EFAULT;
2224 return 0;
2225}
2226
2227static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2228{
2229 struct aac_delete_disk dd;
2230 struct fsa_dev_info *fsa_dev_ptr;
2231
2232 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp65101352006-09-19 08:59:23 -07002233 if (!fsa_dev_ptr)
2234 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2237 return -EFAULT;
2238
2239 if (dd.cnum >= dev->maximum_num_containers)
2240 return -EINVAL;
2241 /*
2242 * Mark this container as being deleted.
2243 */
2244 fsa_dev_ptr[dd.cnum].deleted = 1;
2245 /*
2246 * Mark the container as no longer valid
2247 */
2248 fsa_dev_ptr[dd.cnum].valid = 0;
2249 return 0;
2250}
2251
2252static int delete_disk(struct aac_dev *dev, void __user *arg)
2253{
2254 struct aac_delete_disk dd;
2255 struct fsa_dev_info *fsa_dev_ptr;
2256
2257 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002258 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07002259 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2262 return -EFAULT;
2263
2264 if (dd.cnum >= dev->maximum_num_containers)
2265 return -EINVAL;
2266 /*
2267 * If the container is locked, it can not be deleted by the API.
2268 */
2269 if (fsa_dev_ptr[dd.cnum].locked)
2270 return -EBUSY;
2271 else {
2272 /*
2273 * Mark the container as no longer being valid.
2274 */
2275 fsa_dev_ptr[dd.cnum].valid = 0;
2276 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2277 return 0;
2278 }
2279}
2280
2281int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2282{
2283 switch (cmd) {
2284 case FSACTL_QUERY_DISK:
2285 return query_disk(dev, arg);
2286 case FSACTL_DELETE_DISK:
2287 return delete_disk(dev, arg);
2288 case FSACTL_FORCE_DELETE_DISK:
2289 return force_delete_disk(dev, arg);
2290 case FSACTL_GET_CONTAINERS:
2291 return aac_get_containers(dev);
2292 default:
2293 return -ENOTTY;
2294 }
2295}
2296
2297/**
2298 *
2299 * aac_srb_callback
2300 * @context: the context set in the fib - here it is scsi cmd
2301 * @fibptr: pointer to the fib
2302 *
2303 * Handles the completion of a scsi command to a non dasd device
2304 *
2305 */
2306
2307static void aac_srb_callback(void *context, struct fib * fibptr)
2308{
2309 struct aac_dev *dev;
2310 struct aac_srb_reply *srbreply;
2311 struct scsi_cmnd *scsicmd;
2312
2313 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002314
2315 if (!aac_valid_context(scsicmd, fibptr))
2316 return;
2317
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002318 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002320 dev = fibptr->dev;
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2323
2324 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
2325 /*
2326 * Calculate resid for sg
2327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002329 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2330 - le32_to_cpu(srbreply->data_xfer_length));
2331
2332 scsi_dma_unmap(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 /*
2335 * First check the fib status
2336 */
2337
2338 if (le32_to_cpu(srbreply->status) != ST_OK){
2339 int len;
2340 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2341 len = (le32_to_cpu(srbreply->sense_data_size) >
2342 sizeof(scsicmd->sense_buffer)) ?
2343 sizeof(scsicmd->sense_buffer) :
2344 le32_to_cpu(srbreply->sense_data_size);
2345 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2346 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2347 }
2348
2349 /*
2350 * Next check the srb status
2351 */
2352 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2353 case SRB_STATUS_ERROR_RECOVERY:
2354 case SRB_STATUS_PENDING:
2355 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08002356 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
2358 case SRB_STATUS_DATA_OVERRUN:
2359 switch(scsicmd->cmnd[0]){
2360 case READ_6:
2361 case WRITE_6:
2362 case READ_10:
2363 case WRITE_10:
2364 case READ_12:
2365 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002366 case READ_16:
2367 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2369 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2370 } else {
2371 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2372 }
2373 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2374 break;
2375 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08002376 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 break;
2378 }
2379 default:
2380 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2381 break;
2382 }
2383 break;
2384 case SRB_STATUS_ABORTED:
2385 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2386 break;
2387 case SRB_STATUS_ABORT_FAILED:
2388 // Not sure about this one - but assuming the hba was trying to abort for some reason
2389 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2390 break;
2391 case SRB_STATUS_PARITY_ERROR:
2392 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2393 break;
2394 case SRB_STATUS_NO_DEVICE:
2395 case SRB_STATUS_INVALID_PATH_ID:
2396 case SRB_STATUS_INVALID_TARGET_ID:
2397 case SRB_STATUS_INVALID_LUN:
2398 case SRB_STATUS_SELECTION_TIMEOUT:
2399 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2400 break;
2401
2402 case SRB_STATUS_COMMAND_TIMEOUT:
2403 case SRB_STATUS_TIMEOUT:
2404 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2405 break;
2406
2407 case SRB_STATUS_BUSY:
Salyzyn, Mark760af102007-06-19 11:41:21 -04002408 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
2410
2411 case SRB_STATUS_BUS_RESET:
2412 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2413 break;
2414
2415 case SRB_STATUS_MESSAGE_REJECTED:
2416 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2417 break;
2418 case SRB_STATUS_REQUEST_FLUSHED:
2419 case SRB_STATUS_ERROR:
2420 case SRB_STATUS_INVALID_REQUEST:
2421 case SRB_STATUS_REQUEST_SENSE_FAILED:
2422 case SRB_STATUS_NO_HBA:
2423 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2424 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2425 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2426 case SRB_STATUS_DELAYED_RETRY:
2427 case SRB_STATUS_BAD_FUNCTION:
2428 case SRB_STATUS_NOT_STARTED:
2429 case SRB_STATUS_NOT_IN_USE:
2430 case SRB_STATUS_FORCE_ABORT:
2431 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2432 default:
2433#ifdef AAC_DETAILED_STATUS_INFO
2434 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2435 le32_to_cpu(srbreply->srb_status) & 0x3F,
2436 aac_get_status_string(
2437 le32_to_cpu(srbreply->srb_status) & 0x3F),
2438 scsicmd->cmnd[0],
2439 le32_to_cpu(srbreply->scsi_status));
2440#endif
2441 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2442 break;
2443 }
2444 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2445 int len;
2446 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2447 len = (le32_to_cpu(srbreply->sense_data_size) >
2448 sizeof(scsicmd->sense_buffer)) ?
2449 sizeof(scsicmd->sense_buffer) :
2450 le32_to_cpu(srbreply->sense_data_size);
2451#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002452 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2453 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454#endif
2455 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2456
2457 }
2458 /*
2459 * OR in the scsi status (already shifted up a bit)
2460 */
2461 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2462
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002463 aac_fib_complete(fibptr);
2464 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002465 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
2468/**
2469 *
2470 * aac_send_scb_fib
2471 * @scsicmd: the scsi command block
2472 *
2473 * This routine will form a FIB and fill in the aac_srb from the
2474 * scsicmd passed in.
2475 */
2476
2477static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2478{
2479 struct fib* cmd_fibcontext;
2480 struct aac_dev* dev;
2481 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Mark Haverkamp84971732005-06-20 11:55:24 -07002483 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002484 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002485 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 scsicmd->result = DID_NO_CONNECT << 16;
2487 scsicmd->scsi_done(scsicmd);
2488 return 0;
2489 }
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 /*
2492 * Allocate and initialize a Fib then setup a BlockWrite command
2493 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002494 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return -1;
2496 }
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002497 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 /*
2500 * Check that the command queued to the controller
2501 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002502 if (status == -EINPROGRESS) {
2503 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return 0;
2505 }
2506
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002507 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2508 aac_fib_complete(cmd_fibcontext);
2509 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 return -1;
2512}
2513
2514static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2515{
2516 struct aac_dev *dev;
2517 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002518 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2521 // Get rid of old data
2522 psg->count = 0;
2523 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002524 psg->sg[0].count = 0;
2525
2526 nseg = scsi_dma_map(scsicmd);
2527 BUG_ON(nseg < 0);
2528 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct scatterlist *sg;
2530 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002532 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002534 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2536 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2537 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002540 if (byte_count > scsi_bufflen(scsicmd)) {
2541 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2542 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002543 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002544 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
2546 /* Check for command underflow */
2547 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2548 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2549 byte_count, scsicmd->underflow);
2550 }
2551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return byte_count;
2553}
2554
2555
2556static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2557{
2558 struct aac_dev *dev;
2559 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002560 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002561 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2564 // Get rid of old data
2565 psg->count = 0;
2566 psg->sg[0].addr[0] = 0;
2567 psg->sg[0].addr[1] = 0;
2568 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002569
2570 nseg = scsi_dma_map(scsicmd);
2571 BUG_ON(nseg < 0);
2572 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 struct scatterlist *sg;
2574 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002576 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002577 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002578 addr = sg_dma_address(sg);
2579 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2580 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002581 psg->sg[i].count = cpu_to_le32(count);
2582 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002584 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002586 if (byte_count > scsi_bufflen(scsicmd)) {
2587 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2588 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002589 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002590 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
2592 /* Check for command underflow */
2593 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2594 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2595 byte_count, scsicmd->underflow);
2596 }
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return byte_count;
2599}
2600
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002601static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2602{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002603 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002604 int nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002605
2606 // Get rid of old data
2607 psg->count = 0;
2608 psg->sg[0].next = 0;
2609 psg->sg[0].prev = 0;
2610 psg->sg[0].addr[0] = 0;
2611 psg->sg[0].addr[1] = 0;
2612 psg->sg[0].count = 0;
2613 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002614
2615 nseg = scsi_dma_map(scsicmd);
2616 BUG_ON(nseg < 0);
2617 if (nseg) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002618 struct scatterlist *sg;
2619 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002620
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002621 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002622 int count = sg_dma_len(sg);
2623 u64 addr = sg_dma_address(sg);
2624 psg->sg[i].next = 0;
2625 psg->sg[i].prev = 0;
2626 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2627 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2628 psg->sg[i].count = cpu_to_le32(count);
2629 psg->sg[i].flags = 0;
2630 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002631 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002632 psg->count = cpu_to_le32(nseg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002633 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002634 if (byte_count > scsi_bufflen(scsicmd)) {
2635 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2636 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002637 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002638 byte_count = scsi_bufflen(scsicmd);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002639 }
2640 /* Check for command underflow */
2641 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2642 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2643 byte_count, scsicmd->underflow);
2644 }
2645 }
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002646 return byte_count;
2647}
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649#ifdef AAC_DETAILED_STATUS_INFO
2650
2651struct aac_srb_status_info {
2652 u32 status;
2653 char *str;
2654};
2655
2656
2657static struct aac_srb_status_info srb_status_info[] = {
2658 { SRB_STATUS_PENDING, "Pending Status"},
2659 { SRB_STATUS_SUCCESS, "Success"},
2660 { SRB_STATUS_ABORTED, "Aborted Command"},
2661 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002662 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 { SRB_STATUS_BUSY, "Device Busy"},
2664 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2665 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2666 { SRB_STATUS_NO_DEVICE, "No Device"},
2667 { SRB_STATUS_TIMEOUT, "Timeout"},
2668 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2669 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2670 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2671 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2672 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2673 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2674 { SRB_STATUS_NO_HBA, "No HBA"},
2675 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2676 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2677 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2678 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2679 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2680 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002681 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2683 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2684 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2685 { SRB_STATUS_NOT_STARTED, "Not Started"},
2686 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2687 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2688 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2689 { 0xff, "Unknown Error"}
2690};
2691
2692char *aac_get_status_string(u32 status)
2693{
2694 int i;
2695
Tobias Klauser6391a112006-06-08 22:23:48 -07002696 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2697 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 return "Bad Status Code";
2701}
2702
2703#endif