blob: ac108f9e26741c544daa6c8756ff8a512a1b6fd2 [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 *
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9 *
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>
29#include <linux/sched.h>
30#include <linux/pci.h>
31#include <linux/spinlock.h>
32#include <linux/slab.h>
33#include <linux/completion.h>
34#include <linux/blkdev.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/semaphore.h>
37#include <asm/uaccess.h>
38
39#include <scsi/scsi.h>
40#include <scsi/scsi_cmnd.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_host.h>
43
44#include "aacraid.h"
45
46/* values for inqd_pdt: Peripheral device type in plain English */
47#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
48#define INQD_PDT_PROC 0x03 /* Processor device */
49#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
50#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
51#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
52#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
53
54#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
55#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Sense codes
59 */
60
61#define SENCODE_NO_SENSE 0x00
62#define SENCODE_END_OF_DATA 0x00
63#define SENCODE_BECOMING_READY 0x04
64#define SENCODE_INIT_CMD_REQUIRED 0x04
65#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
66#define SENCODE_INVALID_COMMAND 0x20
67#define SENCODE_LBA_OUT_OF_RANGE 0x21
68#define SENCODE_INVALID_CDB_FIELD 0x24
69#define SENCODE_LUN_NOT_SUPPORTED 0x25
70#define SENCODE_INVALID_PARAM_FIELD 0x26
71#define SENCODE_PARAM_NOT_SUPPORTED 0x26
72#define SENCODE_PARAM_VALUE_INVALID 0x26
73#define SENCODE_RESET_OCCURRED 0x29
74#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
75#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
76#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
77#define SENCODE_DIAGNOSTIC_FAILURE 0x40
78#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
79#define SENCODE_INVALID_MESSAGE_ERROR 0x49
80#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
81#define SENCODE_OVERLAPPED_COMMAND 0x4E
82
83/*
84 * Additional sense codes
85 */
86
87#define ASENCODE_NO_SENSE 0x00
88#define ASENCODE_END_OF_DATA 0x05
89#define ASENCODE_BECOMING_READY 0x01
90#define ASENCODE_INIT_CMD_REQUIRED 0x02
91#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
92#define ASENCODE_INVALID_COMMAND 0x00
93#define ASENCODE_LBA_OUT_OF_RANGE 0x00
94#define ASENCODE_INVALID_CDB_FIELD 0x00
95#define ASENCODE_LUN_NOT_SUPPORTED 0x00
96#define ASENCODE_INVALID_PARAM_FIELD 0x00
97#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
98#define ASENCODE_PARAM_VALUE_INVALID 0x02
99#define ASENCODE_RESET_OCCURRED 0x00
100#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
101#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
102#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
103#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
104#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
105#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
106#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
107#define ASENCODE_OVERLAPPED_COMMAND 0x00
108
109#define BYTE0(x) (unsigned char)(x)
110#define BYTE1(x) (unsigned char)((x) >> 8)
111#define BYTE2(x) (unsigned char)((x) >> 16)
112#define BYTE3(x) (unsigned char)((x) >> 24)
113
114/*------------------------------------------------------------------------------
115 * S T R U C T S / T Y P E D E F S
116 *----------------------------------------------------------------------------*/
117/* SCSI inquiry data */
118struct inquiry_data {
119 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
120 u8 inqd_dtq; /* RMB | Device Type Qualifier */
121 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
122 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
123 u8 inqd_len; /* Additional length (n-4) */
124 u8 inqd_pad1[2];/* Reserved - must be zero */
125 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
126 u8 inqd_vid[8]; /* Vendor ID */
127 u8 inqd_pid[16];/* Product ID */
128 u8 inqd_prl[4]; /* Product Revision Level */
129};
130
131/*
132 * M O D U L E G L O B A L S
133 */
134
135static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
136static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700137static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
139#ifdef AAC_DETAILED_STATUS_INFO
140static char *aac_get_status_string(u32 status);
141#endif
142
143/*
144 * Non dasd selection is handled entirely in aachba now
145 */
146
147static int nondasd = -1;
148static int dacmode = -1;
149
150static int commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700151int startup_timeout = 180;
152int aif_timeout = 120;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800154module_param(nondasd, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800156module_param(dacmode, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800158module_param(commit, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159MODULE_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 -0700160module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
161MODULE_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.");
162module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
163MODULE_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 -0700164
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700165int numacb = -1;
166module_param(numacb, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800167MODULE_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 -0700168
169int acbsize = -1;
170module_param(acbsize, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800171MODULE_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 -0700172
173int expose_physicals = 0;
174module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
175MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. 0=off, 1=on");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/**
177 * aac_get_config_status - check the adapter configuration
178 * @common: adapter to query
179 *
180 * Query config status, and commit the configuration if needed.
181 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700182int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 int status = 0;
185 struct fib * fibptr;
186
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800187 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -ENOMEM;
189
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800190 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 {
192 struct aac_get_config_status *dinfo;
193 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
194
195 dinfo->command = cpu_to_le32(VM_ContainerConfig);
196 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
197 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
198 }
199
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800200 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 fibptr,
202 sizeof (struct aac_get_config_status),
203 FsaNormal,
204 1, 1,
205 NULL, NULL);
206 if (status < 0 ) {
207 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
208 } else {
209 struct aac_get_config_status_resp *reply
210 = (struct aac_get_config_status_resp *) fib_data(fibptr);
211 dprintk((KERN_WARNING
212 "aac_get_config_status: response=%d status=%d action=%d\n",
213 le32_to_cpu(reply->response),
214 le32_to_cpu(reply->status),
215 le32_to_cpu(reply->data.action)));
216 if ((le32_to_cpu(reply->response) != ST_OK) ||
217 (le32_to_cpu(reply->status) != CT_OK) ||
218 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
219 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
220 status = -EINVAL;
221 }
222 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800223 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
225 if (status >= 0) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700226 if ((commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800228 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 dinfo = (struct aac_commit_config *) fib_data(fibptr);
230
231 dinfo->command = cpu_to_le32(VM_ContainerConfig);
232 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
233
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800234 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 fibptr,
236 sizeof (struct aac_commit_config),
237 FsaNormal,
238 1, 1,
239 NULL, NULL);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800240 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 } else if (commit == 0) {
242 printk(KERN_WARNING
243 "aac_get_config_status: Foreign device configurations are being ignored\n");
244 }
245 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800246 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return status;
248}
249
250/**
251 * aac_get_containers - list containers
252 * @common: adapter to probe
253 *
254 * Make a list of all containers on this controller
255 */
256int aac_get_containers(struct aac_dev *dev)
257{
258 struct fsa_dev_info *fsa_dev_ptr;
259 u32 index;
260 int status = 0;
261 struct fib * fibptr;
262 unsigned instance;
263 struct aac_get_container_count *dinfo;
264 struct aac_get_container_count_resp *dresp;
265 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
266
267 instance = dev->scsi_host_ptr->unique_id;
268
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800269 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -ENOMEM;
271
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800272 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
274 dinfo->command = cpu_to_le32(VM_ContainerConfig);
275 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
276
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800277 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 fibptr,
279 sizeof (struct aac_get_container_count),
280 FsaNormal,
281 1, 1,
282 NULL, NULL);
283 if (status >= 0) {
284 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
285 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800286 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
289 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
290 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
292 sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
293 if (!fsa_dev_ptr) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800294 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return -ENOMEM;
296 }
297 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
298
299 dev->fsa_dev = fsa_dev_ptr;
300 dev->maximum_num_containers = maximum_num_containers;
301
302 for (index = 0; index < dev->maximum_num_containers; index++) {
303 struct aac_query_mount *dinfo;
304 struct aac_mount *dresp;
305
306 fsa_dev_ptr[index].devname[0] = '\0';
307
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800308 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dinfo = (struct aac_query_mount *) fib_data(fibptr);
310
311 dinfo->command = cpu_to_le32(VM_NameServe);
312 dinfo->count = cpu_to_le32(index);
313 dinfo->type = cpu_to_le32(FT_FILESYS);
314
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800315 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 fibptr,
317 sizeof (struct aac_query_mount),
318 FsaNormal,
319 1, 1,
320 NULL, NULL);
321 if (status < 0 ) {
322 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
323 break;
324 }
325 dresp = (struct aac_mount *)fib_data(fibptr);
326
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700327 if ((le32_to_cpu(dresp->status) == ST_OK) &&
328 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
329 dinfo->command = cpu_to_le32(VM_NameServe64);
330 dinfo->count = cpu_to_le32(index);
331 dinfo->type = cpu_to_le32(FT_FILESYS);
332
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800333 if (aac_fib_send(ContainerCommand,
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700334 fibptr,
335 sizeof(struct aac_query_mount),
336 FsaNormal,
337 1, 1,
338 NULL, NULL) < 0)
339 continue;
340 } else
341 dresp->mnt[0].capacityhigh = 0;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 dprintk ((KERN_DEBUG
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700344 "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 (int)index, (int)le32_to_cpu(dresp->status),
346 (int)le32_to_cpu(dresp->mnt[0].vol),
347 (int)le32_to_cpu(dresp->mnt[0].state),
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700348 ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
349 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if ((le32_to_cpu(dresp->status) == ST_OK) &&
351 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
352 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
353 fsa_dev_ptr[index].valid = 1;
354 fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700355 fsa_dev_ptr[index].size
356 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
357 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
359 fsa_dev_ptr[index].ro = 1;
360 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800361 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /*
363 * If there are no more containers, then stop asking.
364 */
365 if ((index + 1) >= le32_to_cpu(dresp->count)){
366 break;
367 }
368 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800369 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return status;
371}
372
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700373static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
374{
375 void *buf;
376 unsigned int transfer_len;
377 struct scatterlist *sg = scsicmd->request_buffer;
378
379 if (scsicmd->use_sg) {
380 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
381 transfer_len = min(sg->length, len + offset);
382 } else {
383 buf = scsicmd->request_buffer;
384 transfer_len = min(scsicmd->request_bufflen, len + offset);
385 }
386
387 memcpy(buf + offset, data, transfer_len - offset);
388
389 if (scsicmd->use_sg)
390 kunmap_atomic(buf - sg->offset, KM_IRQ0);
391
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static void get_container_name_callback(void *context, struct fib * fibptr)
395{
396 struct aac_get_name_resp * get_name_reply;
397 struct scsi_cmnd * scsicmd;
398
399 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800400 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700403 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
406 /* Failure is irrelevant, using default value instead */
407 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
408 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700409 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
411 while (*sp == ' ')
412 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700413 if (*sp) {
414 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
415 int count = sizeof(d);
416 char *dp = d;
417 do {
418 *dp++ = (*sp) ? *sp++ : ' ';
419 } while (--count > 0);
420 aac_internal_transfer(scsicmd, d,
421 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
426
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800427 aac_fib_complete(fibptr);
428 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700429 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/**
433 * aac_get_container_name - get container name, none blocking.
434 */
435static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
436{
437 int status;
438 struct aac_get_name *dinfo;
439 struct fib * cmd_fibcontext;
440 struct aac_dev * dev;
441
442 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
443
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800444 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -ENOMEM;
446
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800447 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
449
450 dinfo->command = cpu_to_le32(VM_ContainerConfig);
451 dinfo->type = cpu_to_le32(CT_READ_NAME);
452 dinfo->cid = cpu_to_le32(cid);
453 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
454
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800455 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 cmd_fibcontext,
457 sizeof (struct aac_get_name),
458 FsaNormal,
459 0, 1,
460 (fib_callback) get_container_name_callback,
461 (void *) scsicmd);
462
463 /*
464 * Check that the command queued to the controller
465 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800466 if (status == -EINPROGRESS) {
467 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800471 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
472 aac_fib_complete(cmd_fibcontext);
473 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -1;
475}
476
477/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800478 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * @dev: device to query
480 * @cid: container identifier
481 *
482 * Queries the controller about the given volume. The volume information
483 * is updated in the struct fsa_dev_info structure rather than returned.
484 */
485
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800486int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct fsa_dev_info *fsa_dev_ptr;
489 int status;
490 struct aac_query_mount *dinfo;
491 struct aac_mount *dresp;
492 struct fib * fibptr;
493 unsigned instance;
494
495 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700496 if (!fsa_dev_ptr)
497 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 instance = dev->scsi_host_ptr->unique_id;
499
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800500 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -ENOMEM;
502
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800503 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 dinfo = (struct aac_query_mount *)fib_data(fibptr);
506
507 dinfo->command = cpu_to_le32(VM_NameServe);
508 dinfo->count = cpu_to_le32(cid);
509 dinfo->type = cpu_to_le32(FT_FILESYS);
510
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800511 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 fibptr,
513 sizeof(struct aac_query_mount),
514 FsaNormal,
515 1, 1,
516 NULL, NULL);
517 if (status < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800518 printk(KERN_WARNING "aacraid: aac_probe_container query failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto error;
520 }
521
522 dresp = (struct aac_mount *) fib_data(fibptr);
523
524 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700525 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
526 dinfo->command = cpu_to_le32(VM_NameServe64);
527 dinfo->count = cpu_to_le32(cid);
528 dinfo->type = cpu_to_le32(FT_FILESYS);
529
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800530 if (aac_fib_send(ContainerCommand,
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700531 fibptr,
532 sizeof(struct aac_query_mount),
533 FsaNormal,
534 1, 1,
535 NULL, NULL) < 0)
536 goto error;
537 } else
538 dresp->mnt[0].capacityhigh = 0;
539
540 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
542 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
543 fsa_dev_ptr[cid].valid = 1;
544 fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700545 fsa_dev_ptr[cid].size
546 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
547 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
549 fsa_dev_ptr[cid].ro = 1;
550 }
551
552error:
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800553 aac_fib_complete(fibptr);
554 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return status;
557}
558
559/* Local Structure to set SCSI inquiry data strings */
560struct scsi_inq {
561 char vid[8]; /* Vendor ID */
562 char pid[16]; /* Product ID */
563 char prl[4]; /* Product Revision Level */
564};
565
566/**
567 * InqStrCopy - string merge
568 * @a: string to copy from
569 * @b: string to copy to
570 *
571 * Copy a String from one location to another
572 * without copying \0
573 */
574
575static void inqstrcpy(char *a, char *b)
576{
577
578 while(*a != (char)0)
579 *b++ = *a++;
580}
581
582static char *container_types[] = {
583 "None",
584 "Volume",
585 "Mirror",
586 "Stripe",
587 "RAID5",
588 "SSRW",
589 "SSRO",
590 "Morph",
591 "Legacy",
592 "RAID4",
593 "RAID10",
594 "RAID00",
595 "V-MIRRORS",
596 "PSEUDO R4",
597 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700598 "RAID5D",
599 "RAID5D0",
600 "RAID1E",
601 "RAID6",
602 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 "Unknown"
604};
605
606
607
608/* Function: setinqstr
609 *
610 * Arguments: [1] pointer to void [1] int
611 *
612 * Purpose: Sets SCSI inquiry data strings for vendor, product
613 * and revision level. Allows strings to be set in platform dependant
614 * files instead of in OS dependant driver source.
615 */
616
Mark Haverkamp794d0602005-10-24 10:51:53 -0700617static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700622 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Mark Haverkamp794d0602005-10-24 10:51:53 -0700624 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
625 char * cp = dev->supplement_adapter_info.AdapterTypeText;
626 int c = sizeof(str->vid);
627 while (*cp && *cp != ' ' && --c)
628 ++cp;
629 c = *cp;
630 *cp = '\0';
631 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
632 str->vid);
633 *cp = c;
634 while (*cp && *cp != ' ')
635 ++cp;
636 while (*cp == ' ')
637 ++cp;
638 /* last six chars reserved for vol type */
639 c = 0;
640 if (strlen(cp) > sizeof(str->pid)) {
641 c = cp[sizeof(str->pid)];
642 cp[sizeof(str->pid)] = '\0';
643 }
644 inqstrcpy (cp, str->pid);
645 if (c)
646 cp[sizeof(str->pid)] = c;
647 } else {
648 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700649
650 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700651 /* last six chars reserved for vol type */
652 inqstrcpy (mp->model, str->pid);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Tobias Klauser6391a112006-06-08 22:23:48 -0700655 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 char *findit = str->pid;
657
658 for ( ; *findit != ' '; findit++); /* walk till we find a space */
659 /* RAID is superfluous in the context of a RAID device */
660 if (memcmp(findit-4, "RAID", 4) == 0)
661 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700662 if (((findit - str->pid) + strlen(container_types[tindex]))
663 < (sizeof(str->pid) + sizeof(str->prl)))
664 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 inqstrcpy ("V1.0", str->prl);
667}
668
Adrian Bunk 48338692005-04-25 19:45:58 -0700669static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
670 u8 a_sense_code, u8 incorrect_length,
671 u8 bit_pointer, u16 field_pointer,
672 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
675 sense_buf[1] = 0; /* Segment number, always zero */
676
677 if (incorrect_length) {
678 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
679 sense_buf[3] = BYTE3(residue);
680 sense_buf[4] = BYTE2(residue);
681 sense_buf[5] = BYTE1(residue);
682 sense_buf[6] = BYTE0(residue);
683 } else
684 sense_buf[2] = sense_key; /* Sense key */
685
686 if (sense_key == ILLEGAL_REQUEST)
687 sense_buf[7] = 10; /* Additional sense length */
688 else
689 sense_buf[7] = 6; /* Additional sense length */
690
691 sense_buf[12] = sense_code; /* Additional sense code */
692 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
693 if (sense_key == ILLEGAL_REQUEST) {
694 sense_buf[15] = 0;
695
696 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
697 sense_buf[15] = 0x80;/* Std sense key specific field */
698 /* Illegal parameter is in the parameter block */
699
700 if (sense_code == SENCODE_INVALID_CDB_FIELD)
701 sense_buf[15] = 0xc0;/* Std sense key specific field */
702 /* Illegal parameter is in the CDB block */
703 sense_buf[15] |= bit_pointer;
704 sense_buf[16] = field_pointer >> 8; /* MSB */
705 sense_buf[17] = field_pointer; /* LSB */
706 }
707}
708
709int aac_get_adapter_info(struct aac_dev* dev)
710{
711 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int rcode;
713 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -0700714 struct aac_adapter_info *info;
715 struct aac_bus_info *command;
716 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700717
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800718 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -ENOMEM;
720
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800721 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700722 info = (struct aac_adapter_info *) fib_data(fibptr);
723 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800725 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700726 fibptr,
727 sizeof(*info),
728 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -0700729 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700730 NULL,
731 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700733 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800734 aac_fib_complete(fibptr);
735 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700736 return rcode;
737 }
738 memcpy(&dev->adapter_info, info, sizeof(*info));
739
740 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
741 struct aac_supplement_adapter_info * info;
742
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800743 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700744
745 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
746
747 memset(info,0,sizeof(*info));
748
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800749 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700750 fibptr,
751 sizeof(*info),
752 FsaNormal,
753 1, 1,
754 NULL,
755 NULL);
756
757 if (rcode >= 0)
758 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Mark Haverkamp84971732005-06-20 11:55:24 -0700761
762 /*
763 * GetBusInfo
764 */
765
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800766 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -0700767
768 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
769
770 memset(bus_info, 0, sizeof(*bus_info));
771
772 command = (struct aac_bus_info *)bus_info;
773
774 command->Command = cpu_to_le32(VM_Ioctl);
775 command->ObjType = cpu_to_le32(FT_DRIVE);
776 command->MethodId = cpu_to_le32(1);
777 command->CtlCmd = cpu_to_le32(GetBusInfo);
778
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800779 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -0700780 fibptr,
781 sizeof (*bus_info),
782 FsaNormal,
783 1, 1,
784 NULL, NULL);
785
786 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
787 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
788 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
789 }
790
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700791 if (!dev->in_reset) {
792 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
793 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 dev->name,
795 dev->id,
796 tmp>>24,
797 (tmp>>16)&0xff,
798 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700799 le32_to_cpu(dev->adapter_info.kernelbuild),
800 (int)sizeof(dev->supplement_adapter_info.BuildDate),
801 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700802 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
803 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 dev->name, dev->id,
805 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
806 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700807 tmp = le32_to_cpu(dev->adapter_info.biosrev);
808 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 dev->name, dev->id,
810 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
811 le32_to_cpu(dev->adapter_info.biosbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700812 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
813 printk(KERN_INFO "%s%d: serial %x\n",
814 dev->name, dev->id,
815 le32_to_cpu(dev->adapter_info.serial[0]));
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 dev->nondasd_support = 0;
819 dev->raid_scsi_mode = 0;
820 if(dev->adapter_info.options & AAC_OPT_NONDASD){
821 dev->nondasd_support = 1;
822 }
823
824 /*
825 * If the firmware supports ROMB RAID/SCSI mode and we are currently
826 * in RAID/SCSI mode, set the flag. For now if in this mode we will
827 * force nondasd support on. If we decide to allow the non-dasd flag
828 * additional changes changes will have to be made to support
829 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
830 * changed to support the new dev->raid_scsi_mode flag instead of
831 * leaching off of the dev->nondasd_support flag. Also in linit.c the
832 * function aac_detect will have to be modified where it sets up the
833 * max number of channels based on the aac->nondasd_support flag only.
834 */
835 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
836 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
837 dev->nondasd_support = 1;
838 dev->raid_scsi_mode = 1;
839 }
840 if (dev->raid_scsi_mode != 0)
841 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
842 dev->name, dev->id);
843
844 if(nondasd != -1) {
845 dev->nondasd_support = (nondasd!=0);
846 }
847 if(dev->nondasd_support != 0){
848 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
849 }
850
851 dev->dac_support = 0;
852 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
853 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
854 dev->dac_support = 1;
855 }
856
857 if(dacmode != -1) {
858 dev->dac_support = (dacmode!=0);
859 }
860 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -0500861 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
862 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
864 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -0500865 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
866 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
868 dev->name, dev->id);
869 dev->dac_support = 0;
870 } else {
871 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
872 dev->name, dev->id);
873 rcode = -ENOMEM;
874 }
875 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700876 /*
877 * 57 scatter gather elements
878 */
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700879 if (!(dev->raw_io_interface)) {
880 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700881 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700882 sizeof(struct aac_write) + sizeof(struct sgentry)) /
883 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700884 if (dev->dac_support) {
885 /*
886 * 38 scatter gather elements
887 */
888 dev->scsi_host_ptr->sg_tablesize =
889 (dev->max_fib_size -
890 sizeof(struct aac_fibhdr) -
891 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700892 sizeof(struct sgentry64)) /
893 sizeof(struct sgentry64);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700894 }
895 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
896 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
897 /*
898 * Worst case size that could cause sg overflow when
899 * we break up SG elements that are larger than 64KB.
900 * Would be nice if we could tell the SCSI layer what
901 * the maximum SG element size can be. Worst case is
902 * (sg_tablesize-1) 4KB elements with one 64KB
903 * element.
904 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
905 */
906 dev->scsi_host_ptr->max_sectors =
907 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
908 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800911 aac_fib_complete(fibptr);
912 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 return rcode;
915}
916
917
Mark Haverkampe53cb352005-08-03 15:39:09 -0700918static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 struct aac_dev *dev;
921 struct aac_read_reply *readreply;
922 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 u32 cid;
924
925 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800926 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -0800929 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700931 if (nblank(dprintk(x))) {
932 u64 lba;
933 switch (scsicmd->cmnd[0]) {
934 case WRITE_6:
935 case READ_6:
936 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
937 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
938 break;
939 case WRITE_16:
940 case READ_16:
941 lba = ((u64)scsicmd->cmnd[2] << 56) |
942 ((u64)scsicmd->cmnd[3] << 48) |
943 ((u64)scsicmd->cmnd[4] << 40) |
944 ((u64)scsicmd->cmnd[5] << 32) |
945 ((u64)scsicmd->cmnd[6] << 24) |
946 (scsicmd->cmnd[7] << 16) |
947 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
948 break;
949 case WRITE_12:
950 case READ_12:
951 lba = ((u64)scsicmd->cmnd[2] << 24) |
952 (scsicmd->cmnd[3] << 16) |
953 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
954 break;
955 default:
956 lba = ((u64)scsicmd->cmnd[2] << 24) |
957 (scsicmd->cmnd[3] << 16) |
958 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
959 break;
960 }
961 printk(KERN_DEBUG
962 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
963 smp_processor_id(), (unsigned long long)lba, jiffies);
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700966 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if(scsicmd->use_sg)
969 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +0200970 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 scsicmd->use_sg,
972 scsicmd->sc_data_direction);
973 else if(scsicmd->request_bufflen)
974 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
975 scsicmd->request_bufflen,
976 scsicmd->sc_data_direction);
977 readreply = (struct aac_read_reply *)fib_data(fibptr);
978 if (le32_to_cpu(readreply->status) == ST_OK)
979 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
980 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700981#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -0700982 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700983 le32_to_cpu(readreply->status));
984#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
986 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
987 HARDWARE_ERROR,
988 SENCODE_INTERNAL_TARGET_FAILURE,
989 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
990 0, 0);
991 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
992 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
993 ? sizeof(scsicmd->sense_buffer)
994 : sizeof(dev->fsa_dev[cid].sense_data));
995 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800996 aac_fib_complete(fibptr);
997 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700999 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Adrian Bunk 48338692005-04-25 19:45:58 -07001002static int aac_read(struct scsi_cmnd * scsicmd, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001004 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 u32 count;
1006 int status;
1007
1008 u16 fibsize;
1009 struct aac_dev *dev;
1010 struct fib * cmd_fibcontext;
1011
1012 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1013 /*
1014 * Get block address and transfer length
1015 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001016 switch (scsicmd->cmnd[0]) {
1017 case READ_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
1019
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001020 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1021 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 count = scsicmd->cmnd[4];
1023
1024 if (count == 0)
1025 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001026 break;
1027 case READ_16:
1028 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
1029
1030 lba = ((u64)scsicmd->cmnd[2] << 56) |
1031 ((u64)scsicmd->cmnd[3] << 48) |
1032 ((u64)scsicmd->cmnd[4] << 40) |
1033 ((u64)scsicmd->cmnd[5] << 32) |
1034 ((u64)scsicmd->cmnd[6] << 24) |
1035 (scsicmd->cmnd[7] << 16) |
1036 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1037 count = (scsicmd->cmnd[10] << 24) |
1038 (scsicmd->cmnd[11] << 16) |
1039 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1040 break;
1041 case READ_12:
1042 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
1043
1044 lba = ((u64)scsicmd->cmnd[2] << 24) |
1045 (scsicmd->cmnd[3] << 16) |
1046 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1047 count = (scsicmd->cmnd[6] << 24) |
1048 (scsicmd->cmnd[7] << 16) |
1049 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1050 break;
1051 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
1053
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001054 lba = ((u64)scsicmd->cmnd[2] << 24) |
1055 (scsicmd->cmnd[3] << 16) |
1056 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001060 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001061 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001062 if ((!(dev->raw_io_interface) || !(dev->raw_io_64)) &&
1063 (lba & 0xffffffff00000000LL)) {
1064 dprintk((KERN_DEBUG "aac_read: Illegal lba\n"));
1065 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1066 SAM_STAT_CHECK_CONDITION;
1067 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1068 HARDWARE_ERROR,
1069 SENCODE_INTERNAL_TARGET_FAILURE,
1070 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1071 0, 0);
1072 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1073 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1074 ? sizeof(scsicmd->sense_buffer)
1075 : sizeof(dev->fsa_dev[cid].sense_data));
1076 scsicmd->scsi_done(scsicmd);
1077 return 0;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * Alocate and initialize a Fib
1081 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001082 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -1;
1084 }
1085
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001086 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001088 if (dev->raw_io_interface) {
1089 struct aac_raw_io *readcmd;
1090 readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001091 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1092 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001093 readcmd->count = cpu_to_le32(count<<9);
1094 readcmd->cid = cpu_to_le16(cid);
1095 readcmd->flags = cpu_to_le16(1);
1096 readcmd->bpTotal = 0;
1097 readcmd->bpComplete = 0;
1098
1099 aac_build_sgraw(scsicmd, &readcmd->sg);
1100 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001101 BUG_ON(fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001102 /*
1103 * Now send the Fib to the adapter
1104 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001105 status = aac_fib_send(ContainerRawIo,
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001106 cmd_fibcontext,
1107 fibsize,
1108 FsaNormal,
1109 0, 1,
1110 (fib_callback) io_callback,
1111 (void *) scsicmd);
1112 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct aac_read64 *readcmd;
1114 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
1115 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1116 readcmd->cid = cpu_to_le16(cid);
1117 readcmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001118 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 readcmd->pad = 0;
1120 readcmd->flags = 0;
1121
1122 aac_build_sg64(scsicmd, &readcmd->sg);
1123 fibsize = sizeof(struct aac_read64) +
1124 ((le32_to_cpu(readcmd->sg.count) - 1) *
1125 sizeof (struct sgentry64));
Mark Haverkamp77d71d22005-09-01 08:19:23 -07001126 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 sizeof(struct aac_fibhdr)));
1128 /*
1129 * Now send the Fib to the adapter
1130 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001131 status = aac_fib_send(ContainerCommand64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 cmd_fibcontext,
1133 fibsize,
1134 FsaNormal,
1135 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001136 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 (void *) scsicmd);
1138 } else {
1139 struct aac_read *readcmd;
1140 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
1141 readcmd->command = cpu_to_le32(VM_CtBlockRead);
1142 readcmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001143 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 readcmd->count = cpu_to_le32(count * 512);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 aac_build_sg(scsicmd, &readcmd->sg);
1147 fibsize = sizeof(struct aac_read) +
1148 ((le32_to_cpu(readcmd->sg.count) - 1) *
1149 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001150 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 sizeof(struct aac_fibhdr)));
1152 /*
1153 * Now send the Fib to the adapter
1154 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001155 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 cmd_fibcontext,
1157 fibsize,
1158 FsaNormal,
1159 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001160 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 (void *) scsicmd);
1162 }
1163
1164
1165
1166 /*
1167 * Check that the command queued to the controller
1168 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001169 if (status == -EINPROGRESS) {
1170 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001174 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * For some reason, the Fib didn't queue, return QUEUE_FULL
1177 */
1178 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001179 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001180 aac_fib_complete(cmd_fibcontext);
1181 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return 0;
1183}
1184
1185static int aac_write(struct scsi_cmnd * scsicmd, int cid)
1186{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001187 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 u32 count;
1189 int status;
1190 u16 fibsize;
1191 struct aac_dev *dev;
1192 struct fib * cmd_fibcontext;
1193
1194 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1195 /*
1196 * Get block address and transfer length
1197 */
1198 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1199 {
1200 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1201 count = scsicmd->cmnd[4];
1202 if (count == 0)
1203 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001204 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1205 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
1206
1207 lba = ((u64)scsicmd->cmnd[2] << 56) |
1208 ((u64)scsicmd->cmnd[3] << 48) |
1209 ((u64)scsicmd->cmnd[4] << 40) |
1210 ((u64)scsicmd->cmnd[5] << 32) |
1211 ((u64)scsicmd->cmnd[6] << 24) |
1212 (scsicmd->cmnd[7] << 16) |
1213 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1214 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1215 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1216 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1217 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
1218
1219 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1220 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1221 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1222 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 } else {
1224 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001225 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1227 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001228 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001230 if ((!(dev->raw_io_interface) || !(dev->raw_io_64))
1231 && (lba & 0xffffffff00000000LL)) {
1232 dprintk((KERN_DEBUG "aac_write: Illegal lba\n"));
1233 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1234 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1235 HARDWARE_ERROR,
1236 SENCODE_INTERNAL_TARGET_FAILURE,
1237 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1238 0, 0);
1239 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1240 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1241 ? sizeof(scsicmd->sense_buffer)
1242 : sizeof(dev->fsa_dev[cid].sense_data));
1243 scsicmd->scsi_done(scsicmd);
1244 return 0;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Allocate and initialize a Fib then setup a BlockWrite command
1248 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001249 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001251 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001254 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001256 if (dev->raw_io_interface) {
1257 struct aac_raw_io *writecmd;
1258 writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001259 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1260 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001261 writecmd->count = cpu_to_le32(count<<9);
1262 writecmd->cid = cpu_to_le16(cid);
1263 writecmd->flags = 0;
1264 writecmd->bpTotal = 0;
1265 writecmd->bpComplete = 0;
1266
1267 aac_build_sgraw(scsicmd, &writecmd->sg);
1268 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001269 BUG_ON(fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001270 /*
1271 * Now send the Fib to the adapter
1272 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001273 status = aac_fib_send(ContainerRawIo,
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001274 cmd_fibcontext,
1275 fibsize,
1276 FsaNormal,
1277 0, 1,
1278 (fib_callback) io_callback,
1279 (void *) scsicmd);
1280 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct aac_write64 *writecmd;
1282 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
1283 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1284 writecmd->cid = cpu_to_le16(cid);
1285 writecmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001286 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 writecmd->pad = 0;
1288 writecmd->flags = 0;
1289
1290 aac_build_sg64(scsicmd, &writecmd->sg);
1291 fibsize = sizeof(struct aac_write64) +
1292 ((le32_to_cpu(writecmd->sg.count) - 1) *
1293 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001294 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 sizeof(struct aac_fibhdr)));
1296 /*
1297 * Now send the Fib to the adapter
1298 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001299 status = aac_fib_send(ContainerCommand64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 cmd_fibcontext,
1301 fibsize,
1302 FsaNormal,
1303 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001304 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 (void *) scsicmd);
1306 } else {
1307 struct aac_write *writecmd;
1308 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
1309 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1310 writecmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001311 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 writecmd->count = cpu_to_le32(count * 512);
1313 writecmd->sg.count = cpu_to_le32(1);
1314 /* ->stable is not used - it did mean which type of write */
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 aac_build_sg(scsicmd, &writecmd->sg);
1317 fibsize = sizeof(struct aac_write) +
1318 ((le32_to_cpu(writecmd->sg.count) - 1) *
1319 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001320 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 sizeof(struct aac_fibhdr)));
1322 /*
1323 * Now send the Fib to the adapter
1324 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001325 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 cmd_fibcontext,
1327 fibsize,
1328 FsaNormal,
1329 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001330 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 (void *) scsicmd);
1332 }
1333
1334 /*
1335 * Check that the command queued to the controller
1336 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001337 if (status == -EINPROGRESS) {
1338 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0;
1340 }
1341
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001342 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /*
1344 * For some reason, the Fib didn't queue, return QUEUE_FULL
1345 */
1346 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001347 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001349 aac_fib_complete(cmd_fibcontext);
1350 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352}
1353
1354static void synchronize_callback(void *context, struct fib *fibptr)
1355{
1356 struct aac_synchronize_reply *synchronizereply;
1357 struct scsi_cmnd *cmd;
1358
1359 cmd = context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001360 cmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1363 smp_processor_id(), jiffies));
1364 BUG_ON(fibptr == NULL);
1365
1366
1367 synchronizereply = fib_data(fibptr);
1368 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1369 cmd->result = DID_OK << 16 |
1370 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1371 else {
1372 struct scsi_device *sdev = cmd->device;
1373 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08001374 u32 cid = sdev_id(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 printk(KERN_WARNING
1376 "synchronize_callback: synchronize failed, status = %d\n",
1377 le32_to_cpu(synchronizereply->status));
1378 cmd->result = DID_OK << 16 |
1379 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1380 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1381 HARDWARE_ERROR,
1382 SENCODE_INTERNAL_TARGET_FAILURE,
1383 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1384 0, 0);
1385 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1386 min(sizeof(dev->fsa_dev[cid].sense_data),
1387 sizeof(cmd->sense_buffer)));
1388 }
1389
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001390 aac_fib_complete(fibptr);
1391 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001392 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1396{
1397 int status;
1398 struct fib *cmd_fibcontext;
1399 struct aac_synchronize *synchronizecmd;
1400 struct scsi_cmnd *cmd;
1401 struct scsi_device *sdev = scsicmd->device;
1402 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001403 struct aac_dev *aac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 unsigned long flags;
1405
1406 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001407 * Wait for all outstanding queued commands to complete to this
1408 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
1410 spin_lock_irqsave(&sdev->list_lock, flags);
1411 list_for_each_entry(cmd, &sdev->cmd_list, list)
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001412 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 ++active;
1414 break;
1415 }
1416
1417 spin_unlock_irqrestore(&sdev->list_lock, flags);
1418
1419 /*
1420 * Yield the processor (requeue for later)
1421 */
1422 if (active)
1423 return SCSI_MLQUEUE_DEVICE_BUSY;
1424
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001425 aac = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001426 if (aac->in_reset)
1427 return SCSI_MLQUEUE_HOST_BUSY;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001430 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001432 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return SCSI_MLQUEUE_HOST_BUSY;
1434
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001435 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 synchronizecmd = fib_data(cmd_fibcontext);
1438 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1439 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1440 synchronizecmd->cid = cpu_to_le32(cid);
1441 synchronizecmd->count =
1442 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1443
1444 /*
1445 * Now send the Fib to the adapter
1446 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001447 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 cmd_fibcontext,
1449 sizeof(struct aac_synchronize),
1450 FsaNormal,
1451 0, 1,
1452 (fib_callback)synchronize_callback,
1453 (void *)scsicmd);
1454
1455 /*
1456 * Check that the command queued to the controller
1457 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001458 if (status == -EINPROGRESS) {
1459 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001464 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1465 aac_fib_complete(cmd_fibcontext);
1466 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return SCSI_MLQUEUE_HOST_BUSY;
1468}
1469
1470/**
1471 * aac_scsi_cmd() - Process SCSI command
1472 * @scsicmd: SCSI command block
1473 *
1474 * Emulate a SCSI command and queue the required request for the
1475 * aacraid firmware.
1476 */
1477
1478int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1479{
1480 u32 cid = 0;
1481 struct Scsi_Host *host = scsicmd->device->host;
1482 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1483 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001485 if (fsa_dev_ptr == NULL)
1486 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * If the bus, id or lun is out of range, return fail
1489 * Test does not apply to ID 16, the pseudo id for the controller
1490 * itself.
1491 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001492 if (scmd_id(scsicmd) != host->this_id) {
Mark Haverkampe5718772006-03-27 09:43:25 -08001493 if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1494 if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1495 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 scsicmd->result = DID_NO_CONNECT << 16;
1497 scsicmd->scsi_done(scsicmd);
1498 return 0;
1499 }
Mark Haverkampe5718772006-03-27 09:43:25 -08001500 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 /*
1503 * If the target container doesn't exist, it may have
1504 * been newly created
1505 */
1506 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1507 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001508 case SERVICE_ACTION_IN:
1509 if (!(dev->raw_io_interface) ||
1510 !(dev->raw_io_64) ||
1511 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 case INQUIRY:
1514 case READ_CAPACITY:
1515 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001516 if (dev->in_reset)
1517 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 spin_unlock_irq(host->host_lock);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001519 aac_probe_container(dev, cid);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001520 if ((fsa_dev_ptr[cid].valid & 1) == 0)
1521 fsa_dev_ptr[cid].valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 spin_lock_irq(host->host_lock);
1523 if (fsa_dev_ptr[cid].valid == 0) {
1524 scsicmd->result = DID_NO_CONNECT << 16;
1525 scsicmd->scsi_done(scsicmd);
1526 return 0;
1527 }
1528 default:
1529 break;
1530 }
1531 }
1532 /*
1533 * If the target container still doesn't exist,
1534 * return failure
1535 */
1536 if (fsa_dev_ptr[cid].valid == 0) {
1537 scsicmd->result = DID_BAD_TARGET << 16;
1538 scsicmd->scsi_done(scsicmd);
1539 return 0;
1540 }
1541 } else { /* check for physical non-dasd devices */
Mark Haverkamp653ba582006-09-19 08:59:43 -07001542 if ((dev->nondasd_support == 1) || expose_physicals) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001543 if (dev->in_reset)
1544 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return aac_send_srb_fib(scsicmd);
1546 } else {
1547 scsicmd->result = DID_NO_CONNECT << 16;
1548 scsicmd->scsi_done(scsicmd);
1549 return 0;
1550 }
1551 }
1552 }
1553 /*
1554 * else Command for the controller itself
1555 */
1556 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1557 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1558 {
1559 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1560 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1561 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1562 ILLEGAL_REQUEST,
1563 SENCODE_INVALID_COMMAND,
1564 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1565 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1566 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1567 ? sizeof(scsicmd->sense_buffer)
1568 : sizeof(dev->fsa_dev[cid].sense_data));
1569 scsicmd->scsi_done(scsicmd);
1570 return 0;
1571 }
1572
1573
1574 /* Handle commands here that don't really require going out to the adapter */
1575 switch (scsicmd->cmnd[0]) {
1576 case INQUIRY:
1577 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001578 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Mark Haverkampe5718772006-03-27 09:43:25 -08001580 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001581 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001583 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001584 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 */
1585 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001587 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Set the Vendor, Product, and Revision Level
1590 * see: <vendor>.c i.e. aac.c
1591 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001592 if (scmd_id(scsicmd) == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07001593 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001594 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1595 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1597 scsicmd->scsi_done(scsicmd);
1598 return 0;
1599 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001600 if (dev->in_reset)
1601 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07001602 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001603 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1604 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return aac_get_container_name(scsicmd, cid);
1606 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001607 case SERVICE_ACTION_IN:
1608 if (!(dev->raw_io_interface) ||
1609 !(dev->raw_io_64) ||
1610 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1611 break;
1612 {
1613 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001614 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001615
1616 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1617 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001618 cp[0] = (capacity >> 56) & 0xff;
1619 cp[1] = (capacity >> 48) & 0xff;
1620 cp[2] = (capacity >> 40) & 0xff;
1621 cp[3] = (capacity >> 32) & 0xff;
1622 cp[4] = (capacity >> 24) & 0xff;
1623 cp[5] = (capacity >> 16) & 0xff;
1624 cp[6] = (capacity >> 8) & 0xff;
1625 cp[7] = (capacity >> 0) & 0xff;
1626 cp[8] = 0;
1627 cp[9] = 0;
1628 cp[10] = 2;
1629 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001630 cp[12] = 0;
1631 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08001632 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001633 if (sizeof(cp) < scsicmd->cmnd[13]) {
1634 unsigned int len, offset = sizeof(cp);
1635
1636 memset(cp, 0, offset);
1637 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08001638 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1639 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001640 aac_internal_transfer(scsicmd, cp, offset, len);
1641 } while ((offset += len) < scsicmd->cmnd[13]);
1642 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001643
1644 /* Do not cache partition table for arrays */
1645 scsicmd->device->removable = 1;
1646
1647 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1648 scsicmd->scsi_done(scsicmd);
1649
1650 return 0;
1651 }
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 case READ_CAPACITY:
1654 {
1655 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001656 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001659 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 capacity = fsa_dev_ptr[cid].size - 1;
1661 else
1662 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 cp[0] = (capacity >> 24) & 0xff;
1665 cp[1] = (capacity >> 16) & 0xff;
1666 cp[2] = (capacity >> 8) & 0xff;
1667 cp[3] = (capacity >> 0) & 0xff;
1668 cp[4] = 0;
1669 cp[5] = 0;
1670 cp[6] = 2;
1671 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001672 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001673 /* Do not cache partition table for arrays */
1674 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1677 scsicmd->scsi_done(scsicmd);
1678
1679 return 0;
1680 }
1681
1682 case MODE_SENSE:
1683 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001684 char mode_buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 mode_buf[0] = 3; /* Mode data length */
1688 mode_buf[1] = 0; /* Medium type - default */
1689 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1690 mode_buf[3] = 0; /* Block descriptor length */
1691
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001692 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1694 scsicmd->scsi_done(scsicmd);
1695
1696 return 0;
1697 }
1698 case MODE_SENSE_10:
1699 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001700 char mode_buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 mode_buf[0] = 0; /* Mode data length (MSB) */
1704 mode_buf[1] = 6; /* Mode data length (LSB) */
1705 mode_buf[2] = 0; /* Medium type - default */
1706 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1707 mode_buf[4] = 0; /* reserved */
1708 mode_buf[5] = 0; /* reserved */
1709 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1710 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001711 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1714 scsicmd->scsi_done(scsicmd);
1715
1716 return 0;
1717 }
1718 case REQUEST_SENSE:
1719 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1720 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1721 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1722 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1723 scsicmd->scsi_done(scsicmd);
1724 return 0;
1725
1726 case ALLOW_MEDIUM_REMOVAL:
1727 dprintk((KERN_DEBUG "LOCK command.\n"));
1728 if (scsicmd->cmnd[4])
1729 fsa_dev_ptr[cid].locked = 1;
1730 else
1731 fsa_dev_ptr[cid].locked = 0;
1732
1733 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1734 scsicmd->scsi_done(scsicmd);
1735 return 0;
1736 /*
1737 * These commands are all No-Ops
1738 */
1739 case TEST_UNIT_READY:
1740 case RESERVE:
1741 case RELEASE:
1742 case REZERO_UNIT:
1743 case REASSIGN_BLOCKS:
1744 case SEEK_10:
1745 case START_STOP:
1746 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1747 scsicmd->scsi_done(scsicmd);
1748 return 0;
1749 }
1750
1751 switch (scsicmd->cmnd[0])
1752 {
1753 case READ_6:
1754 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001755 case READ_12:
1756 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001757 if (dev->in_reset)
1758 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /*
1760 * Hack to keep track of ordinal number of the device that
1761 * corresponds to a container. Needed to convert
1762 * containers to /dev/sd device names
1763 */
1764
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001765 if (scsicmd->request->rq_disk)
1766 strlcpy(fsa_dev_ptr[cid].devname,
1767 scsicmd->request->rq_disk->disk_name,
1768 min(sizeof(fsa_dev_ptr[cid].devname),
1769 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001770
1771 return aac_read(scsicmd, cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 case WRITE_6:
1774 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001775 case WRITE_12:
1776 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001777 if (dev->in_reset)
1778 return -1;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001779 return aac_write(scsicmd, cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 case SYNCHRONIZE_CACHE:
1782 /* Issue FIB to tell Firmware to flush it's cache */
1783 return aac_synchronize(scsicmd, cid);
1784
1785 default:
1786 /*
1787 * Unhandled commands
1788 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001789 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1791 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1792 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1793 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1794 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1795 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1796 ? sizeof(scsicmd->sense_buffer)
1797 : sizeof(dev->fsa_dev[cid].sense_data));
1798 scsicmd->scsi_done(scsicmd);
1799 return 0;
1800 }
1801}
1802
1803static int query_disk(struct aac_dev *dev, void __user *arg)
1804{
1805 struct aac_query_disk qd;
1806 struct fsa_dev_info *fsa_dev_ptr;
1807
1808 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001809 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07001810 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1812 return -EFAULT;
1813 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08001814 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
1816 {
1817 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1818 return -EINVAL;
1819 qd.instance = dev->scsi_host_ptr->host_no;
1820 qd.bus = 0;
1821 qd.id = CONTAINER_TO_ID(qd.cnum);
1822 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1823 }
1824 else return -EINVAL;
1825
1826 qd.valid = fsa_dev_ptr[qd.cnum].valid;
1827 qd.locked = fsa_dev_ptr[qd.cnum].locked;
1828 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1829
1830 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1831 qd.unmapped = 1;
1832 else
1833 qd.unmapped = 0;
1834
1835 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1836 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1837
1838 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1839 return -EFAULT;
1840 return 0;
1841}
1842
1843static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1844{
1845 struct aac_delete_disk dd;
1846 struct fsa_dev_info *fsa_dev_ptr;
1847
1848 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp651013552006-09-19 08:59:23 -07001849 if (!fsa_dev_ptr)
1850 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1853 return -EFAULT;
1854
1855 if (dd.cnum >= dev->maximum_num_containers)
1856 return -EINVAL;
1857 /*
1858 * Mark this container as being deleted.
1859 */
1860 fsa_dev_ptr[dd.cnum].deleted = 1;
1861 /*
1862 * Mark the container as no longer valid
1863 */
1864 fsa_dev_ptr[dd.cnum].valid = 0;
1865 return 0;
1866}
1867
1868static int delete_disk(struct aac_dev *dev, void __user *arg)
1869{
1870 struct aac_delete_disk dd;
1871 struct fsa_dev_info *fsa_dev_ptr;
1872
1873 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001874 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07001875 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1878 return -EFAULT;
1879
1880 if (dd.cnum >= dev->maximum_num_containers)
1881 return -EINVAL;
1882 /*
1883 * If the container is locked, it can not be deleted by the API.
1884 */
1885 if (fsa_dev_ptr[dd.cnum].locked)
1886 return -EBUSY;
1887 else {
1888 /*
1889 * Mark the container as no longer being valid.
1890 */
1891 fsa_dev_ptr[dd.cnum].valid = 0;
1892 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1893 return 0;
1894 }
1895}
1896
1897int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1898{
1899 switch (cmd) {
1900 case FSACTL_QUERY_DISK:
1901 return query_disk(dev, arg);
1902 case FSACTL_DELETE_DISK:
1903 return delete_disk(dev, arg);
1904 case FSACTL_FORCE_DELETE_DISK:
1905 return force_delete_disk(dev, arg);
1906 case FSACTL_GET_CONTAINERS:
1907 return aac_get_containers(dev);
1908 default:
1909 return -ENOTTY;
1910 }
1911}
1912
1913/**
1914 *
1915 * aac_srb_callback
1916 * @context: the context set in the fib - here it is scsi cmd
1917 * @fibptr: pointer to the fib
1918 *
1919 * Handles the completion of a scsi command to a non dasd device
1920 *
1921 */
1922
1923static void aac_srb_callback(void *context, struct fib * fibptr)
1924{
1925 struct aac_dev *dev;
1926 struct aac_srb_reply *srbreply;
1927 struct scsi_cmnd *scsicmd;
1928
1929 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001930 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1932
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001933 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1936
1937 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
1938 /*
1939 * Calculate resid for sg
1940 */
1941
1942 scsicmd->resid = scsicmd->request_bufflen -
1943 le32_to_cpu(srbreply->data_xfer_length);
1944
1945 if(scsicmd->use_sg)
1946 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02001947 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 scsicmd->use_sg,
1949 scsicmd->sc_data_direction);
1950 else if(scsicmd->request_bufflen)
1951 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1952 scsicmd->sc_data_direction);
1953
1954 /*
1955 * First check the fib status
1956 */
1957
1958 if (le32_to_cpu(srbreply->status) != ST_OK){
1959 int len;
1960 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1961 len = (le32_to_cpu(srbreply->sense_data_size) >
1962 sizeof(scsicmd->sense_buffer)) ?
1963 sizeof(scsicmd->sense_buffer) :
1964 le32_to_cpu(srbreply->sense_data_size);
1965 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1966 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1967 }
1968
1969 /*
1970 * Next check the srb status
1971 */
1972 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1973 case SRB_STATUS_ERROR_RECOVERY:
1974 case SRB_STATUS_PENDING:
1975 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08001976 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978 case SRB_STATUS_DATA_OVERRUN:
1979 switch(scsicmd->cmnd[0]){
1980 case READ_6:
1981 case WRITE_6:
1982 case READ_10:
1983 case WRITE_10:
1984 case READ_12:
1985 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001986 case READ_16:
1987 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1989 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1990 } else {
1991 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1992 }
1993 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1994 break;
1995 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08001996 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998 }
1999 default:
2000 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2001 break;
2002 }
2003 break;
2004 case SRB_STATUS_ABORTED:
2005 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2006 break;
2007 case SRB_STATUS_ABORT_FAILED:
2008 // Not sure about this one - but assuming the hba was trying to abort for some reason
2009 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2010 break;
2011 case SRB_STATUS_PARITY_ERROR:
2012 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2013 break;
2014 case SRB_STATUS_NO_DEVICE:
2015 case SRB_STATUS_INVALID_PATH_ID:
2016 case SRB_STATUS_INVALID_TARGET_ID:
2017 case SRB_STATUS_INVALID_LUN:
2018 case SRB_STATUS_SELECTION_TIMEOUT:
2019 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2020 break;
2021
2022 case SRB_STATUS_COMMAND_TIMEOUT:
2023 case SRB_STATUS_TIMEOUT:
2024 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2025 break;
2026
2027 case SRB_STATUS_BUSY:
2028 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2029 break;
2030
2031 case SRB_STATUS_BUS_RESET:
2032 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2033 break;
2034
2035 case SRB_STATUS_MESSAGE_REJECTED:
2036 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2037 break;
2038 case SRB_STATUS_REQUEST_FLUSHED:
2039 case SRB_STATUS_ERROR:
2040 case SRB_STATUS_INVALID_REQUEST:
2041 case SRB_STATUS_REQUEST_SENSE_FAILED:
2042 case SRB_STATUS_NO_HBA:
2043 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2044 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2045 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2046 case SRB_STATUS_DELAYED_RETRY:
2047 case SRB_STATUS_BAD_FUNCTION:
2048 case SRB_STATUS_NOT_STARTED:
2049 case SRB_STATUS_NOT_IN_USE:
2050 case SRB_STATUS_FORCE_ABORT:
2051 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2052 default:
2053#ifdef AAC_DETAILED_STATUS_INFO
2054 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2055 le32_to_cpu(srbreply->srb_status) & 0x3F,
2056 aac_get_status_string(
2057 le32_to_cpu(srbreply->srb_status) & 0x3F),
2058 scsicmd->cmnd[0],
2059 le32_to_cpu(srbreply->scsi_status));
2060#endif
2061 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2062 break;
2063 }
2064 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2065 int len;
2066 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2067 len = (le32_to_cpu(srbreply->sense_data_size) >
2068 sizeof(scsicmd->sense_buffer)) ?
2069 sizeof(scsicmd->sense_buffer) :
2070 le32_to_cpu(srbreply->sense_data_size);
2071#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002072 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2073 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074#endif
2075 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2076
2077 }
2078 /*
2079 * OR in the scsi status (already shifted up a bit)
2080 */
2081 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2082
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002083 aac_fib_complete(fibptr);
2084 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002085 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
2088/**
2089 *
2090 * aac_send_scb_fib
2091 * @scsicmd: the scsi command block
2092 *
2093 * This routine will form a FIB and fill in the aac_srb from the
2094 * scsicmd passed in.
2095 */
2096
2097static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2098{
2099 struct fib* cmd_fibcontext;
2100 struct aac_dev* dev;
2101 int status;
2102 struct aac_srb *srbcmd;
2103 u16 fibsize;
2104 u32 flag;
2105 u32 timeout;
2106
Mark Haverkamp84971732005-06-20 11:55:24 -07002107 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002108 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002109 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 scsicmd->result = DID_NO_CONNECT << 16;
2111 scsicmd->scsi_done(scsicmd);
2112 return 0;
2113 }
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 switch(scsicmd->sc_data_direction){
2116 case DMA_TO_DEVICE:
2117 flag = SRB_DataOut;
2118 break;
2119 case DMA_BIDIRECTIONAL:
2120 flag = SRB_DataIn | SRB_DataOut;
2121 break;
2122 case DMA_FROM_DEVICE:
2123 flag = SRB_DataIn;
2124 break;
2125 case DMA_NONE:
2126 default: /* shuts up some versions of gcc */
2127 flag = SRB_NoDataXfer;
2128 break;
2129 }
2130
2131
2132 /*
2133 * Allocate and initialize a Fib then setup a BlockWrite command
2134 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002135 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return -1;
2137 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002138 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
2141 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
Mark Haverkampe5718772006-03-27 09:43:25 -08002142 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(scsicmd)));
2143 srbcmd->id = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
2145 srbcmd->flags = cpu_to_le32(flag);
James Bottomley5262d082005-08-05 16:31:35 -05002146 timeout = scsicmd->timeout_per_command/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if(timeout == 0){
2148 timeout = 1;
2149 }
2150 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
2151 srbcmd->retry_limit = 0; /* Obsolete parameter */
2152 srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
2153
2154 if( dev->dac_support == 1 ) {
2155 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
2156 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2157
2158 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2159 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2160 /*
2161 * Build Scatter/Gather list
2162 */
2163 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
2164 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
2165 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002166 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 sizeof(struct aac_fibhdr)));
2168
2169 /*
2170 * Now send the Fib to the adapter
2171 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002172 status = aac_fib_send(ScsiPortCommand64, cmd_fibcontext,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 fibsize, FsaNormal, 0, 1,
2174 (fib_callback) aac_srb_callback,
2175 (void *) scsicmd);
2176 } else {
2177 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
2178 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2179
2180 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2181 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2182 /*
2183 * Build Scatter/Gather list
2184 */
2185 fibsize = sizeof (struct aac_srb) +
2186 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
2187 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002188 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 sizeof(struct aac_fibhdr)));
2190
2191 /*
2192 * Now send the Fib to the adapter
2193 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002194 status = aac_fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 (fib_callback) aac_srb_callback, (void *) scsicmd);
2196 }
2197 /*
2198 * Check that the command queued to the controller
2199 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002200 if (status == -EINPROGRESS) {
2201 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return 0;
2203 }
2204
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002205 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2206 aac_fib_complete(cmd_fibcontext);
2207 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 return -1;
2210}
2211
2212static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2213{
2214 struct aac_dev *dev;
2215 unsigned long byte_count = 0;
2216
2217 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2218 // Get rid of old data
2219 psg->count = 0;
2220 psg->sg[0].addr = 0;
2221 psg->sg[0].count = 0;
2222 if (scsicmd->use_sg) {
2223 struct scatterlist *sg;
2224 int i;
2225 int sg_count;
2226 sg = (struct scatterlist *) scsicmd->request_buffer;
2227
2228 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2229 scsicmd->sc_data_direction);
2230 psg->count = cpu_to_le32(sg_count);
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 for (i = 0; i < sg_count; i++) {
2233 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2234 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2235 byte_count += sg_dma_len(sg);
2236 sg++;
2237 }
2238 /* hba wants the size to be exact */
2239 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002240 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2241 (byte_count - scsicmd->request_bufflen);
2242 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 byte_count = scsicmd->request_bufflen;
2244 }
2245 /* Check for command underflow */
2246 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2247 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2248 byte_count, scsicmd->underflow);
2249 }
2250 }
2251 else if(scsicmd->request_bufflen) {
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002252 u32 addr;
2253 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 scsicmd->request_buffer,
2255 scsicmd->request_bufflen,
2256 scsicmd->sc_data_direction);
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002257 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 psg->count = cpu_to_le32(1);
2259 psg->sg[0].addr = cpu_to_le32(addr);
2260 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 byte_count = scsicmd->request_bufflen;
2262 }
2263 return byte_count;
2264}
2265
2266
2267static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2268{
2269 struct aac_dev *dev;
2270 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002271 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2274 // Get rid of old data
2275 psg->count = 0;
2276 psg->sg[0].addr[0] = 0;
2277 psg->sg[0].addr[1] = 0;
2278 psg->sg[0].count = 0;
2279 if (scsicmd->use_sg) {
2280 struct scatterlist *sg;
2281 int i;
2282 int sg_count;
2283 sg = (struct scatterlist *) scsicmd->request_buffer;
2284
2285 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2286 scsicmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 for (i = 0; i < sg_count; i++) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002289 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002290 addr = sg_dma_address(sg);
2291 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2292 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002293 psg->sg[i].count = cpu_to_le32(count);
2294 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 sg++;
2296 }
Mark Haverkamp1241f352006-03-27 09:44:23 -08002297 psg->count = cpu_to_le32(sg_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /* hba wants the size to be exact */
2299 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002300 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2301 (byte_count - scsicmd->request_bufflen);
2302 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 byte_count = scsicmd->request_bufflen;
2304 }
2305 /* Check for command underflow */
2306 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2307 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2308 byte_count, scsicmd->underflow);
2309 }
2310 }
2311 else if(scsicmd->request_bufflen) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002312 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 scsicmd->request_buffer,
2314 scsicmd->request_bufflen,
2315 scsicmd->sc_data_direction);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002316 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 psg->count = cpu_to_le32(1);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002318 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2319 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 byte_count = scsicmd->request_bufflen;
2322 }
2323 return byte_count;
2324}
2325
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002326static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2327{
2328 struct Scsi_Host *host = scsicmd->device->host;
2329 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2330 unsigned long byte_count = 0;
2331
2332 // Get rid of old data
2333 psg->count = 0;
2334 psg->sg[0].next = 0;
2335 psg->sg[0].prev = 0;
2336 psg->sg[0].addr[0] = 0;
2337 psg->sg[0].addr[1] = 0;
2338 psg->sg[0].count = 0;
2339 psg->sg[0].flags = 0;
2340 if (scsicmd->use_sg) {
2341 struct scatterlist *sg;
2342 int i;
2343 int sg_count;
2344 sg = (struct scatterlist *) scsicmd->request_buffer;
2345
2346 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2347 scsicmd->sc_data_direction);
2348
2349 for (i = 0; i < sg_count; i++) {
2350 int count = sg_dma_len(sg);
2351 u64 addr = sg_dma_address(sg);
2352 psg->sg[i].next = 0;
2353 psg->sg[i].prev = 0;
2354 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2355 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2356 psg->sg[i].count = cpu_to_le32(count);
2357 psg->sg[i].flags = 0;
2358 byte_count += count;
2359 sg++;
2360 }
2361 psg->count = cpu_to_le32(sg_count);
2362 /* hba wants the size to be exact */
2363 if(byte_count > scsicmd->request_bufflen){
2364 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2365 (byte_count - scsicmd->request_bufflen);
2366 psg->sg[i-1].count = cpu_to_le32(temp);
2367 byte_count = scsicmd->request_bufflen;
2368 }
2369 /* Check for command underflow */
2370 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2371 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2372 byte_count, scsicmd->underflow);
2373 }
2374 }
2375 else if(scsicmd->request_bufflen) {
2376 int count;
2377 u64 addr;
2378 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2379 scsicmd->request_buffer,
2380 scsicmd->request_bufflen,
2381 scsicmd->sc_data_direction);
2382 addr = scsicmd->SCp.dma_handle;
2383 count = scsicmd->request_bufflen;
2384 psg->count = cpu_to_le32(1);
2385 psg->sg[0].next = 0;
2386 psg->sg[0].prev = 0;
2387 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2388 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2389 psg->sg[0].count = cpu_to_le32(count);
2390 psg->sg[0].flags = 0;
2391 byte_count = scsicmd->request_bufflen;
2392 }
2393 return byte_count;
2394}
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396#ifdef AAC_DETAILED_STATUS_INFO
2397
2398struct aac_srb_status_info {
2399 u32 status;
2400 char *str;
2401};
2402
2403
2404static struct aac_srb_status_info srb_status_info[] = {
2405 { SRB_STATUS_PENDING, "Pending Status"},
2406 { SRB_STATUS_SUCCESS, "Success"},
2407 { SRB_STATUS_ABORTED, "Aborted Command"},
2408 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002409 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 { SRB_STATUS_BUSY, "Device Busy"},
2411 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2412 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2413 { SRB_STATUS_NO_DEVICE, "No Device"},
2414 { SRB_STATUS_TIMEOUT, "Timeout"},
2415 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2416 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2417 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2418 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2419 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2420 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2421 { SRB_STATUS_NO_HBA, "No HBA"},
2422 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2423 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2424 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2425 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2426 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2427 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002428 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2430 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2431 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2432 { SRB_STATUS_NOT_STARTED, "Not Started"},
2433 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2434 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2435 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2436 { 0xff, "Unknown Error"}
2437};
2438
2439char *aac_get_status_string(u32 status)
2440{
2441 int i;
2442
Tobias Klauser6391a112006-06-08 22:23:48 -07002443 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2444 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 return "Bad Status Code";
2448}
2449
2450#endif