blob: 699351c15cc9d91e4e35838f659f510e904c2656 [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.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/**
173 * aac_get_config_status - check the adapter configuration
174 * @common: adapter to query
175 *
176 * Query config status, and commit the configuration if needed.
177 */
178int aac_get_config_status(struct aac_dev *dev)
179{
180 int status = 0;
181 struct fib * fibptr;
182
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800183 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -ENOMEM;
185
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800186 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 {
188 struct aac_get_config_status *dinfo;
189 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
190
191 dinfo->command = cpu_to_le32(VM_ContainerConfig);
192 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
193 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
194 }
195
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800196 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 fibptr,
198 sizeof (struct aac_get_config_status),
199 FsaNormal,
200 1, 1,
201 NULL, NULL);
202 if (status < 0 ) {
203 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
204 } else {
205 struct aac_get_config_status_resp *reply
206 = (struct aac_get_config_status_resp *) fib_data(fibptr);
207 dprintk((KERN_WARNING
208 "aac_get_config_status: response=%d status=%d action=%d\n",
209 le32_to_cpu(reply->response),
210 le32_to_cpu(reply->status),
211 le32_to_cpu(reply->data.action)));
212 if ((le32_to_cpu(reply->response) != ST_OK) ||
213 (le32_to_cpu(reply->status) != CT_OK) ||
214 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
215 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
216 status = -EINVAL;
217 }
218 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800219 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
221 if (status >= 0) {
222 if (commit == 1) {
223 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800224 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dinfo = (struct aac_commit_config *) fib_data(fibptr);
226
227 dinfo->command = cpu_to_le32(VM_ContainerConfig);
228 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
229
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800230 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 fibptr,
232 sizeof (struct aac_commit_config),
233 FsaNormal,
234 1, 1,
235 NULL, NULL);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800236 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 } else if (commit == 0) {
238 printk(KERN_WARNING
239 "aac_get_config_status: Foreign device configurations are being ignored\n");
240 }
241 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800242 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return status;
244}
245
246/**
247 * aac_get_containers - list containers
248 * @common: adapter to probe
249 *
250 * Make a list of all containers on this controller
251 */
252int aac_get_containers(struct aac_dev *dev)
253{
254 struct fsa_dev_info *fsa_dev_ptr;
255 u32 index;
256 int status = 0;
257 struct fib * fibptr;
258 unsigned instance;
259 struct aac_get_container_count *dinfo;
260 struct aac_get_container_count_resp *dresp;
261 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
262
263 instance = dev->scsi_host_ptr->unique_id;
264
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800265 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -ENOMEM;
267
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800268 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
270 dinfo->command = cpu_to_le32(VM_ContainerConfig);
271 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
272
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800273 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 fibptr,
275 sizeof (struct aac_get_container_count),
276 FsaNormal,
277 1, 1,
278 NULL, NULL);
279 if (status >= 0) {
280 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
281 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800282 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
285 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
286 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
288 sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
289 if (!fsa_dev_ptr) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800290 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -ENOMEM;
292 }
293 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
294
295 dev->fsa_dev = fsa_dev_ptr;
296 dev->maximum_num_containers = maximum_num_containers;
297
298 for (index = 0; index < dev->maximum_num_containers; index++) {
299 struct aac_query_mount *dinfo;
300 struct aac_mount *dresp;
301
302 fsa_dev_ptr[index].devname[0] = '\0';
303
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800304 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 dinfo = (struct aac_query_mount *) fib_data(fibptr);
306
307 dinfo->command = cpu_to_le32(VM_NameServe);
308 dinfo->count = cpu_to_le32(index);
309 dinfo->type = cpu_to_le32(FT_FILESYS);
310
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800311 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 fibptr,
313 sizeof (struct aac_query_mount),
314 FsaNormal,
315 1, 1,
316 NULL, NULL);
317 if (status < 0 ) {
318 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
319 break;
320 }
321 dresp = (struct aac_mount *)fib_data(fibptr);
322
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700323 if ((le32_to_cpu(dresp->status) == ST_OK) &&
324 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
325 dinfo->command = cpu_to_le32(VM_NameServe64);
326 dinfo->count = cpu_to_le32(index);
327 dinfo->type = cpu_to_le32(FT_FILESYS);
328
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800329 if (aac_fib_send(ContainerCommand,
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700330 fibptr,
331 sizeof(struct aac_query_mount),
332 FsaNormal,
333 1, 1,
334 NULL, NULL) < 0)
335 continue;
336 } else
337 dresp->mnt[0].capacityhigh = 0;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dprintk ((KERN_DEBUG
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700340 "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 (int)index, (int)le32_to_cpu(dresp->status),
342 (int)le32_to_cpu(dresp->mnt[0].vol),
343 (int)le32_to_cpu(dresp->mnt[0].state),
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700344 ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
345 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if ((le32_to_cpu(dresp->status) == ST_OK) &&
347 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
348 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
349 fsa_dev_ptr[index].valid = 1;
350 fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700351 fsa_dev_ptr[index].size
352 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
353 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
355 fsa_dev_ptr[index].ro = 1;
356 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800357 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * If there are no more containers, then stop asking.
360 */
361 if ((index + 1) >= le32_to_cpu(dresp->count)){
362 break;
363 }
364 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800365 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return status;
367}
368
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700369static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
370{
371 void *buf;
372 unsigned int transfer_len;
373 struct scatterlist *sg = scsicmd->request_buffer;
374
375 if (scsicmd->use_sg) {
376 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
377 transfer_len = min(sg->length, len + offset);
378 } else {
379 buf = scsicmd->request_buffer;
380 transfer_len = min(scsicmd->request_bufflen, len + offset);
381 }
382
383 memcpy(buf + offset, data, transfer_len - offset);
384
385 if (scsicmd->use_sg)
386 kunmap_atomic(buf - sg->offset, KM_IRQ0);
387
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static void get_container_name_callback(void *context, struct fib * fibptr)
391{
392 struct aac_get_name_resp * get_name_reply;
393 struct scsi_cmnd * scsicmd;
394
395 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800396 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700399 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
402 /* Failure is irrelevant, using default value instead */
403 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
404 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700405 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
407 while (*sp == ' ')
408 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700409 if (*sp) {
410 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
411 int count = sizeof(d);
412 char *dp = d;
413 do {
414 *dp++ = (*sp) ? *sp++ : ' ';
415 } while (--count > 0);
416 aac_internal_transfer(scsicmd, d,
417 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
422
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800423 aac_fib_complete(fibptr);
424 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700425 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/**
429 * aac_get_container_name - get container name, none blocking.
430 */
431static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
432{
433 int status;
434 struct aac_get_name *dinfo;
435 struct fib * cmd_fibcontext;
436 struct aac_dev * dev;
437
438 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
439
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800440 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -ENOMEM;
442
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800443 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
445
446 dinfo->command = cpu_to_le32(VM_ContainerConfig);
447 dinfo->type = cpu_to_le32(CT_READ_NAME);
448 dinfo->cid = cpu_to_le32(cid);
449 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
450
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800451 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 cmd_fibcontext,
453 sizeof (struct aac_get_name),
454 FsaNormal,
455 0, 1,
456 (fib_callback) get_container_name_callback,
457 (void *) scsicmd);
458
459 /*
460 * Check that the command queued to the controller
461 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800462 if (status == -EINPROGRESS) {
463 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800467 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
468 aac_fib_complete(cmd_fibcontext);
469 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -1;
471}
472
473/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800474 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * @dev: device to query
476 * @cid: container identifier
477 *
478 * Queries the controller about the given volume. The volume information
479 * is updated in the struct fsa_dev_info structure rather than returned.
480 */
481
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800482int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct fsa_dev_info *fsa_dev_ptr;
485 int status;
486 struct aac_query_mount *dinfo;
487 struct aac_mount *dresp;
488 struct fib * fibptr;
489 unsigned instance;
490
491 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700492 if (!fsa_dev_ptr)
493 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 instance = dev->scsi_host_ptr->unique_id;
495
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800496 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return -ENOMEM;
498
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800499 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 dinfo = (struct aac_query_mount *)fib_data(fibptr);
502
503 dinfo->command = cpu_to_le32(VM_NameServe);
504 dinfo->count = cpu_to_le32(cid);
505 dinfo->type = cpu_to_le32(FT_FILESYS);
506
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800507 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 fibptr,
509 sizeof(struct aac_query_mount),
510 FsaNormal,
511 1, 1,
512 NULL, NULL);
513 if (status < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800514 printk(KERN_WARNING "aacraid: aac_probe_container query failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto error;
516 }
517
518 dresp = (struct aac_mount *) fib_data(fibptr);
519
520 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700521 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
522 dinfo->command = cpu_to_le32(VM_NameServe64);
523 dinfo->count = cpu_to_le32(cid);
524 dinfo->type = cpu_to_le32(FT_FILESYS);
525
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800526 if (aac_fib_send(ContainerCommand,
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700527 fibptr,
528 sizeof(struct aac_query_mount),
529 FsaNormal,
530 1, 1,
531 NULL, NULL) < 0)
532 goto error;
533 } else
534 dresp->mnt[0].capacityhigh = 0;
535
536 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
538 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
539 fsa_dev_ptr[cid].valid = 1;
540 fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700541 fsa_dev_ptr[cid].size
542 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
543 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
545 fsa_dev_ptr[cid].ro = 1;
546 }
547
548error:
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800549 aac_fib_complete(fibptr);
550 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 return status;
553}
554
555/* Local Structure to set SCSI inquiry data strings */
556struct scsi_inq {
557 char vid[8]; /* Vendor ID */
558 char pid[16]; /* Product ID */
559 char prl[4]; /* Product Revision Level */
560};
561
562/**
563 * InqStrCopy - string merge
564 * @a: string to copy from
565 * @b: string to copy to
566 *
567 * Copy a String from one location to another
568 * without copying \0
569 */
570
571static void inqstrcpy(char *a, char *b)
572{
573
574 while(*a != (char)0)
575 *b++ = *a++;
576}
577
578static char *container_types[] = {
579 "None",
580 "Volume",
581 "Mirror",
582 "Stripe",
583 "RAID5",
584 "SSRW",
585 "SSRO",
586 "Morph",
587 "Legacy",
588 "RAID4",
589 "RAID10",
590 "RAID00",
591 "V-MIRRORS",
592 "PSEUDO R4",
593 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700594 "RAID5D",
595 "RAID5D0",
596 "RAID1E",
597 "RAID6",
598 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 "Unknown"
600};
601
602
603
604/* Function: setinqstr
605 *
606 * Arguments: [1] pointer to void [1] int
607 *
608 * Purpose: Sets SCSI inquiry data strings for vendor, product
609 * and revision level. Allows strings to be set in platform dependant
610 * files instead of in OS dependant driver source.
611 */
612
Mark Haverkamp794d0602005-10-24 10:51:53 -0700613static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700618 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Mark Haverkamp794d0602005-10-24 10:51:53 -0700620 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
621 char * cp = dev->supplement_adapter_info.AdapterTypeText;
622 int c = sizeof(str->vid);
623 while (*cp && *cp != ' ' && --c)
624 ++cp;
625 c = *cp;
626 *cp = '\0';
627 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
628 str->vid);
629 *cp = c;
630 while (*cp && *cp != ' ')
631 ++cp;
632 while (*cp == ' ')
633 ++cp;
634 /* last six chars reserved for vol type */
635 c = 0;
636 if (strlen(cp) > sizeof(str->pid)) {
637 c = cp[sizeof(str->pid)];
638 cp[sizeof(str->pid)] = '\0';
639 }
640 inqstrcpy (cp, str->pid);
641 if (c)
642 cp[sizeof(str->pid)] = c;
643 } else {
644 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700645
646 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700647 /* last six chars reserved for vol type */
648 inqstrcpy (mp->model, str->pid);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Tobias Klauser6391a112006-06-08 22:23:48 -0700651 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 char *findit = str->pid;
653
654 for ( ; *findit != ' '; findit++); /* walk till we find a space */
655 /* RAID is superfluous in the context of a RAID device */
656 if (memcmp(findit-4, "RAID", 4) == 0)
657 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700658 if (((findit - str->pid) + strlen(container_types[tindex]))
659 < (sizeof(str->pid) + sizeof(str->prl)))
660 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 inqstrcpy ("V1.0", str->prl);
663}
664
Adrian Bunk 48338692005-04-25 19:45:58 -0700665static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
666 u8 a_sense_code, u8 incorrect_length,
667 u8 bit_pointer, u16 field_pointer,
668 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
671 sense_buf[1] = 0; /* Segment number, always zero */
672
673 if (incorrect_length) {
674 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
675 sense_buf[3] = BYTE3(residue);
676 sense_buf[4] = BYTE2(residue);
677 sense_buf[5] = BYTE1(residue);
678 sense_buf[6] = BYTE0(residue);
679 } else
680 sense_buf[2] = sense_key; /* Sense key */
681
682 if (sense_key == ILLEGAL_REQUEST)
683 sense_buf[7] = 10; /* Additional sense length */
684 else
685 sense_buf[7] = 6; /* Additional sense length */
686
687 sense_buf[12] = sense_code; /* Additional sense code */
688 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
689 if (sense_key == ILLEGAL_REQUEST) {
690 sense_buf[15] = 0;
691
692 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
693 sense_buf[15] = 0x80;/* Std sense key specific field */
694 /* Illegal parameter is in the parameter block */
695
696 if (sense_code == SENCODE_INVALID_CDB_FIELD)
697 sense_buf[15] = 0xc0;/* Std sense key specific field */
698 /* Illegal parameter is in the CDB block */
699 sense_buf[15] |= bit_pointer;
700 sense_buf[16] = field_pointer >> 8; /* MSB */
701 sense_buf[17] = field_pointer; /* LSB */
702 }
703}
704
705int aac_get_adapter_info(struct aac_dev* dev)
706{
707 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int rcode;
709 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -0700710 struct aac_adapter_info *info;
711 struct aac_bus_info *command;
712 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700713
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800714 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -ENOMEM;
716
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800717 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700718 info = (struct aac_adapter_info *) fib_data(fibptr);
719 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800721 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700722 fibptr,
723 sizeof(*info),
724 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -0700725 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700726 NULL,
727 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700729 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800730 aac_fib_complete(fibptr);
731 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700732 return rcode;
733 }
734 memcpy(&dev->adapter_info, info, sizeof(*info));
735
736 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
737 struct aac_supplement_adapter_info * info;
738
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800739 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700740
741 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
742
743 memset(info,0,sizeof(*info));
744
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800745 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700746 fibptr,
747 sizeof(*info),
748 FsaNormal,
749 1, 1,
750 NULL,
751 NULL);
752
753 if (rcode >= 0)
754 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Mark Haverkamp84971732005-06-20 11:55:24 -0700757
758 /*
759 * GetBusInfo
760 */
761
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800762 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -0700763
764 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
765
766 memset(bus_info, 0, sizeof(*bus_info));
767
768 command = (struct aac_bus_info *)bus_info;
769
770 command->Command = cpu_to_le32(VM_Ioctl);
771 command->ObjType = cpu_to_le32(FT_DRIVE);
772 command->MethodId = cpu_to_le32(1);
773 command->CtlCmd = cpu_to_le32(GetBusInfo);
774
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800775 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -0700776 fibptr,
777 sizeof (*bus_info),
778 FsaNormal,
779 1, 1,
780 NULL, NULL);
781
782 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
783 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
784 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700788 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 dev->name,
790 dev->id,
791 tmp>>24,
792 (tmp>>16)&0xff,
793 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700794 le32_to_cpu(dev->adapter_info.kernelbuild),
795 (int)sizeof(dev->supplement_adapter_info.BuildDate),
796 dev->supplement_adapter_info.BuildDate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
798 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
799 dev->name, dev->id,
800 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
801 le32_to_cpu(dev->adapter_info.monitorbuild));
802 tmp = le32_to_cpu(dev->adapter_info.biosrev);
803 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
804 dev->name, dev->id,
805 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
806 le32_to_cpu(dev->adapter_info.biosbuild));
807 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
808 printk(KERN_INFO "%s%d: serial %x\n",
809 dev->name, dev->id,
810 le32_to_cpu(dev->adapter_info.serial[0]));
811
812 dev->nondasd_support = 0;
813 dev->raid_scsi_mode = 0;
814 if(dev->adapter_info.options & AAC_OPT_NONDASD){
815 dev->nondasd_support = 1;
816 }
817
818 /*
819 * If the firmware supports ROMB RAID/SCSI mode and we are currently
820 * in RAID/SCSI mode, set the flag. For now if in this mode we will
821 * force nondasd support on. If we decide to allow the non-dasd flag
822 * additional changes changes will have to be made to support
823 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
824 * changed to support the new dev->raid_scsi_mode flag instead of
825 * leaching off of the dev->nondasd_support flag. Also in linit.c the
826 * function aac_detect will have to be modified where it sets up the
827 * max number of channels based on the aac->nondasd_support flag only.
828 */
829 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
830 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
831 dev->nondasd_support = 1;
832 dev->raid_scsi_mode = 1;
833 }
834 if (dev->raid_scsi_mode != 0)
835 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
836 dev->name, dev->id);
837
838 if(nondasd != -1) {
839 dev->nondasd_support = (nondasd!=0);
840 }
841 if(dev->nondasd_support != 0){
842 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
843 }
844
845 dev->dac_support = 0;
846 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
847 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
848 dev->dac_support = 1;
849 }
850
851 if(dacmode != -1) {
852 dev->dac_support = (dacmode!=0);
853 }
854 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -0500855 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
856 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
858 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -0500859 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
860 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
862 dev->name, dev->id);
863 dev->dac_support = 0;
864 } else {
865 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
866 dev->name, dev->id);
867 rcode = -ENOMEM;
868 }
869 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700870 /*
871 * 57 scatter gather elements
872 */
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700873 if (!(dev->raw_io_interface)) {
874 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700875 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700876 sizeof(struct aac_write) + sizeof(struct sgentry)) /
877 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700878 if (dev->dac_support) {
879 /*
880 * 38 scatter gather elements
881 */
882 dev->scsi_host_ptr->sg_tablesize =
883 (dev->max_fib_size -
884 sizeof(struct aac_fibhdr) -
885 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700886 sizeof(struct sgentry64)) /
887 sizeof(struct sgentry64);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700888 }
889 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
890 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
891 /*
892 * Worst case size that could cause sg overflow when
893 * we break up SG elements that are larger than 64KB.
894 * Would be nice if we could tell the SCSI layer what
895 * the maximum SG element size can be. Worst case is
896 * (sg_tablesize-1) 4KB elements with one 64KB
897 * element.
898 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
899 */
900 dev->scsi_host_ptr->max_sectors =
901 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
902 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800905 aac_fib_complete(fibptr);
906 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 return rcode;
909}
910
911
Mark Haverkampe53cb352005-08-03 15:39:09 -0700912static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct aac_dev *dev;
915 struct aac_read_reply *readreply;
916 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 u32 cid;
918
919 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800920 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -0800923 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700925 if (nblank(dprintk(x))) {
926 u64 lba;
927 switch (scsicmd->cmnd[0]) {
928 case WRITE_6:
929 case READ_6:
930 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
931 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
932 break;
933 case WRITE_16:
934 case READ_16:
935 lba = ((u64)scsicmd->cmnd[2] << 56) |
936 ((u64)scsicmd->cmnd[3] << 48) |
937 ((u64)scsicmd->cmnd[4] << 40) |
938 ((u64)scsicmd->cmnd[5] << 32) |
939 ((u64)scsicmd->cmnd[6] << 24) |
940 (scsicmd->cmnd[7] << 16) |
941 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
942 break;
943 case WRITE_12:
944 case READ_12:
945 lba = ((u64)scsicmd->cmnd[2] << 24) |
946 (scsicmd->cmnd[3] << 16) |
947 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
948 break;
949 default:
950 lba = ((u64)scsicmd->cmnd[2] << 24) |
951 (scsicmd->cmnd[3] << 16) |
952 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
953 break;
954 }
955 printk(KERN_DEBUG
956 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
957 smp_processor_id(), (unsigned long long)lba, jiffies);
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700960 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if(scsicmd->use_sg)
963 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +0200964 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 scsicmd->use_sg,
966 scsicmd->sc_data_direction);
967 else if(scsicmd->request_bufflen)
968 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
969 scsicmd->request_bufflen,
970 scsicmd->sc_data_direction);
971 readreply = (struct aac_read_reply *)fib_data(fibptr);
972 if (le32_to_cpu(readreply->status) == ST_OK)
973 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
974 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700975#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -0700976 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700977 le32_to_cpu(readreply->status));
978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
980 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
981 HARDWARE_ERROR,
982 SENCODE_INTERNAL_TARGET_FAILURE,
983 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
984 0, 0);
985 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
986 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
987 ? sizeof(scsicmd->sense_buffer)
988 : sizeof(dev->fsa_dev[cid].sense_data));
989 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800990 aac_fib_complete(fibptr);
991 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700993 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Adrian Bunk 48338692005-04-25 19:45:58 -0700996static int aac_read(struct scsi_cmnd * scsicmd, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700998 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 u32 count;
1000 int status;
1001
1002 u16 fibsize;
1003 struct aac_dev *dev;
1004 struct fib * cmd_fibcontext;
1005
1006 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1007 /*
1008 * Get block address and transfer length
1009 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001010 switch (scsicmd->cmnd[0]) {
1011 case READ_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
1013
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001014 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1015 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 count = scsicmd->cmnd[4];
1017
1018 if (count == 0)
1019 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001020 break;
1021 case READ_16:
1022 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
1023
1024 lba = ((u64)scsicmd->cmnd[2] << 56) |
1025 ((u64)scsicmd->cmnd[3] << 48) |
1026 ((u64)scsicmd->cmnd[4] << 40) |
1027 ((u64)scsicmd->cmnd[5] << 32) |
1028 ((u64)scsicmd->cmnd[6] << 24) |
1029 (scsicmd->cmnd[7] << 16) |
1030 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1031 count = (scsicmd->cmnd[10] << 24) |
1032 (scsicmd->cmnd[11] << 16) |
1033 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1034 break;
1035 case READ_12:
1036 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
1037
1038 lba = ((u64)scsicmd->cmnd[2] << 24) |
1039 (scsicmd->cmnd[3] << 16) |
1040 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1041 count = (scsicmd->cmnd[6] << 24) |
1042 (scsicmd->cmnd[7] << 16) |
1043 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1044 break;
1045 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
1047
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001048 lba = ((u64)scsicmd->cmnd[2] << 24) |
1049 (scsicmd->cmnd[3] << 16) |
1050 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001054 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001055 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001056 if ((!(dev->raw_io_interface) || !(dev->raw_io_64)) &&
1057 (lba & 0xffffffff00000000LL)) {
1058 dprintk((KERN_DEBUG "aac_read: Illegal lba\n"));
1059 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1060 SAM_STAT_CHECK_CONDITION;
1061 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1062 HARDWARE_ERROR,
1063 SENCODE_INTERNAL_TARGET_FAILURE,
1064 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1065 0, 0);
1066 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1067 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1068 ? sizeof(scsicmd->sense_buffer)
1069 : sizeof(dev->fsa_dev[cid].sense_data));
1070 scsicmd->scsi_done(scsicmd);
1071 return 0;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * Alocate and initialize a Fib
1075 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001076 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -1;
1078 }
1079
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001080 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001082 if (dev->raw_io_interface) {
1083 struct aac_raw_io *readcmd;
1084 readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001085 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1086 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001087 readcmd->count = cpu_to_le32(count<<9);
1088 readcmd->cid = cpu_to_le16(cid);
1089 readcmd->flags = cpu_to_le16(1);
1090 readcmd->bpTotal = 0;
1091 readcmd->bpComplete = 0;
1092
1093 aac_build_sgraw(scsicmd, &readcmd->sg);
1094 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001095 BUG_ON(fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001096 /*
1097 * Now send the Fib to the adapter
1098 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001099 status = aac_fib_send(ContainerRawIo,
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001100 cmd_fibcontext,
1101 fibsize,
1102 FsaNormal,
1103 0, 1,
1104 (fib_callback) io_callback,
1105 (void *) scsicmd);
1106 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct aac_read64 *readcmd;
1108 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
1109 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1110 readcmd->cid = cpu_to_le16(cid);
1111 readcmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001112 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 readcmd->pad = 0;
1114 readcmd->flags = 0;
1115
1116 aac_build_sg64(scsicmd, &readcmd->sg);
1117 fibsize = sizeof(struct aac_read64) +
1118 ((le32_to_cpu(readcmd->sg.count) - 1) *
1119 sizeof (struct sgentry64));
Mark Haverkamp77d71d22005-09-01 08:19:23 -07001120 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 sizeof(struct aac_fibhdr)));
1122 /*
1123 * Now send the Fib to the adapter
1124 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001125 status = aac_fib_send(ContainerCommand64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 cmd_fibcontext,
1127 fibsize,
1128 FsaNormal,
1129 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001130 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 (void *) scsicmd);
1132 } else {
1133 struct aac_read *readcmd;
1134 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
1135 readcmd->command = cpu_to_le32(VM_CtBlockRead);
1136 readcmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001137 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 readcmd->count = cpu_to_le32(count * 512);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 aac_build_sg(scsicmd, &readcmd->sg);
1141 fibsize = sizeof(struct aac_read) +
1142 ((le32_to_cpu(readcmd->sg.count) - 1) *
1143 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001144 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sizeof(struct aac_fibhdr)));
1146 /*
1147 * Now send the Fib to the adapter
1148 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001149 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 cmd_fibcontext,
1151 fibsize,
1152 FsaNormal,
1153 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001154 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 (void *) scsicmd);
1156 }
1157
1158
1159
1160 /*
1161 * Check that the command queued to the controller
1162 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001163 if (status == -EINPROGRESS) {
1164 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001168 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /*
1170 * For some reason, the Fib didn't queue, return QUEUE_FULL
1171 */
1172 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001173 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001174 aac_fib_complete(cmd_fibcontext);
1175 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
1177}
1178
1179static int aac_write(struct scsi_cmnd * scsicmd, int cid)
1180{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001181 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 u32 count;
1183 int status;
1184 u16 fibsize;
1185 struct aac_dev *dev;
1186 struct fib * cmd_fibcontext;
1187
1188 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1189 /*
1190 * Get block address and transfer length
1191 */
1192 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1193 {
1194 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1195 count = scsicmd->cmnd[4];
1196 if (count == 0)
1197 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001198 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1199 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
1200
1201 lba = ((u64)scsicmd->cmnd[2] << 56) |
1202 ((u64)scsicmd->cmnd[3] << 48) |
1203 ((u64)scsicmd->cmnd[4] << 40) |
1204 ((u64)scsicmd->cmnd[5] << 32) |
1205 ((u64)scsicmd->cmnd[6] << 24) |
1206 (scsicmd->cmnd[7] << 16) |
1207 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1208 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1209 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1210 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1211 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
1212
1213 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1214 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1215 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1216 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 } else {
1218 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001219 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1221 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001222 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001224 if ((!(dev->raw_io_interface) || !(dev->raw_io_64))
1225 && (lba & 0xffffffff00000000LL)) {
1226 dprintk((KERN_DEBUG "aac_write: Illegal lba\n"));
1227 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1228 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1229 HARDWARE_ERROR,
1230 SENCODE_INTERNAL_TARGET_FAILURE,
1231 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1232 0, 0);
1233 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1234 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1235 ? sizeof(scsicmd->sense_buffer)
1236 : sizeof(dev->fsa_dev[cid].sense_data));
1237 scsicmd->scsi_done(scsicmd);
1238 return 0;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /*
1241 * Allocate and initialize a Fib then setup a BlockWrite command
1242 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001243 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001245 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001248 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001250 if (dev->raw_io_interface) {
1251 struct aac_raw_io *writecmd;
1252 writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001253 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1254 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001255 writecmd->count = cpu_to_le32(count<<9);
1256 writecmd->cid = cpu_to_le16(cid);
1257 writecmd->flags = 0;
1258 writecmd->bpTotal = 0;
1259 writecmd->bpComplete = 0;
1260
1261 aac_build_sgraw(scsicmd, &writecmd->sg);
1262 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001263 BUG_ON(fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001264 /*
1265 * Now send the Fib to the adapter
1266 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001267 status = aac_fib_send(ContainerRawIo,
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001268 cmd_fibcontext,
1269 fibsize,
1270 FsaNormal,
1271 0, 1,
1272 (fib_callback) io_callback,
1273 (void *) scsicmd);
1274 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct aac_write64 *writecmd;
1276 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
1277 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1278 writecmd->cid = cpu_to_le16(cid);
1279 writecmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001280 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 writecmd->pad = 0;
1282 writecmd->flags = 0;
1283
1284 aac_build_sg64(scsicmd, &writecmd->sg);
1285 fibsize = sizeof(struct aac_write64) +
1286 ((le32_to_cpu(writecmd->sg.count) - 1) *
1287 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001288 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 sizeof(struct aac_fibhdr)));
1290 /*
1291 * Now send the Fib to the adapter
1292 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001293 status = aac_fib_send(ContainerCommand64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 cmd_fibcontext,
1295 fibsize,
1296 FsaNormal,
1297 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001298 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 (void *) scsicmd);
1300 } else {
1301 struct aac_write *writecmd;
1302 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
1303 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1304 writecmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001305 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 writecmd->count = cpu_to_le32(count * 512);
1307 writecmd->sg.count = cpu_to_le32(1);
1308 /* ->stable is not used - it did mean which type of write */
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 aac_build_sg(scsicmd, &writecmd->sg);
1311 fibsize = sizeof(struct aac_write) +
1312 ((le32_to_cpu(writecmd->sg.count) - 1) *
1313 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001314 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sizeof(struct aac_fibhdr)));
1316 /*
1317 * Now send the Fib to the adapter
1318 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001319 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 cmd_fibcontext,
1321 fibsize,
1322 FsaNormal,
1323 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001324 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 (void *) scsicmd);
1326 }
1327
1328 /*
1329 * Check that the command queued to the controller
1330 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001331 if (status == -EINPROGRESS) {
1332 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return 0;
1334 }
1335
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001336 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /*
1338 * For some reason, the Fib didn't queue, return QUEUE_FULL
1339 */
1340 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001341 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001343 aac_fib_complete(cmd_fibcontext);
1344 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return 0;
1346}
1347
1348static void synchronize_callback(void *context, struct fib *fibptr)
1349{
1350 struct aac_synchronize_reply *synchronizereply;
1351 struct scsi_cmnd *cmd;
1352
1353 cmd = context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001354 cmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1357 smp_processor_id(), jiffies));
1358 BUG_ON(fibptr == NULL);
1359
1360
1361 synchronizereply = fib_data(fibptr);
1362 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1363 cmd->result = DID_OK << 16 |
1364 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1365 else {
1366 struct scsi_device *sdev = cmd->device;
1367 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08001368 u32 cid = sdev_id(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 printk(KERN_WARNING
1370 "synchronize_callback: synchronize failed, status = %d\n",
1371 le32_to_cpu(synchronizereply->status));
1372 cmd->result = DID_OK << 16 |
1373 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1374 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1375 HARDWARE_ERROR,
1376 SENCODE_INTERNAL_TARGET_FAILURE,
1377 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1378 0, 0);
1379 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1380 min(sizeof(dev->fsa_dev[cid].sense_data),
1381 sizeof(cmd->sense_buffer)));
1382 }
1383
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001384 aac_fib_complete(fibptr);
1385 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001386 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1390{
1391 int status;
1392 struct fib *cmd_fibcontext;
1393 struct aac_synchronize *synchronizecmd;
1394 struct scsi_cmnd *cmd;
1395 struct scsi_device *sdev = scsicmd->device;
1396 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001397 struct aac_dev *aac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 unsigned long flags;
1399
1400 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001401 * Wait for all outstanding queued commands to complete to this
1402 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 */
1404 spin_lock_irqsave(&sdev->list_lock, flags);
1405 list_for_each_entry(cmd, &sdev->cmd_list, list)
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001406 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 ++active;
1408 break;
1409 }
1410
1411 spin_unlock_irqrestore(&sdev->list_lock, flags);
1412
1413 /*
1414 * Yield the processor (requeue for later)
1415 */
1416 if (active)
1417 return SCSI_MLQUEUE_DEVICE_BUSY;
1418
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001419 aac = (struct aac_dev *)scsicmd->device->host->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001421 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001423 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return SCSI_MLQUEUE_HOST_BUSY;
1425
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001426 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 synchronizecmd = fib_data(cmd_fibcontext);
1429 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1430 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1431 synchronizecmd->cid = cpu_to_le32(cid);
1432 synchronizecmd->count =
1433 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1434
1435 /*
1436 * Now send the Fib to the adapter
1437 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001438 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 cmd_fibcontext,
1440 sizeof(struct aac_synchronize),
1441 FsaNormal,
1442 0, 1,
1443 (fib_callback)synchronize_callback,
1444 (void *)scsicmd);
1445
1446 /*
1447 * Check that the command queued to the controller
1448 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001449 if (status == -EINPROGRESS) {
1450 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001455 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1456 aac_fib_complete(cmd_fibcontext);
1457 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return SCSI_MLQUEUE_HOST_BUSY;
1459}
1460
1461/**
1462 * aac_scsi_cmd() - Process SCSI command
1463 * @scsicmd: SCSI command block
1464 *
1465 * Emulate a SCSI command and queue the required request for the
1466 * aacraid firmware.
1467 */
1468
1469int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1470{
1471 u32 cid = 0;
1472 struct Scsi_Host *host = scsicmd->device->host;
1473 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1474 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001476 if (fsa_dev_ptr == NULL)
1477 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * If the bus, id or lun is out of range, return fail
1480 * Test does not apply to ID 16, the pseudo id for the controller
1481 * itself.
1482 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001483 if (scmd_id(scsicmd) != host->this_id) {
Mark Haverkampe5718772006-03-27 09:43:25 -08001484 if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1485 if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1486 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 scsicmd->result = DID_NO_CONNECT << 16;
1488 scsicmd->scsi_done(scsicmd);
1489 return 0;
1490 }
Mark Haverkampe5718772006-03-27 09:43:25 -08001491 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /*
1494 * If the target container doesn't exist, it may have
1495 * been newly created
1496 */
1497 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1498 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001499 case SERVICE_ACTION_IN:
1500 if (!(dev->raw_io_interface) ||
1501 !(dev->raw_io_64) ||
1502 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 case INQUIRY:
1505 case READ_CAPACITY:
1506 case TEST_UNIT_READY:
1507 spin_unlock_irq(host->host_lock);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001508 aac_probe_container(dev, cid);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001509 if ((fsa_dev_ptr[cid].valid & 1) == 0)
1510 fsa_dev_ptr[cid].valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_lock_irq(host->host_lock);
1512 if (fsa_dev_ptr[cid].valid == 0) {
1513 scsicmd->result = DID_NO_CONNECT << 16;
1514 scsicmd->scsi_done(scsicmd);
1515 return 0;
1516 }
1517 default:
1518 break;
1519 }
1520 }
1521 /*
1522 * If the target container still doesn't exist,
1523 * return failure
1524 */
1525 if (fsa_dev_ptr[cid].valid == 0) {
1526 scsicmd->result = DID_BAD_TARGET << 16;
1527 scsicmd->scsi_done(scsicmd);
1528 return 0;
1529 }
1530 } else { /* check for physical non-dasd devices */
1531 if(dev->nondasd_support == 1){
1532 return aac_send_srb_fib(scsicmd);
1533 } else {
1534 scsicmd->result = DID_NO_CONNECT << 16;
1535 scsicmd->scsi_done(scsicmd);
1536 return 0;
1537 }
1538 }
1539 }
1540 /*
1541 * else Command for the controller itself
1542 */
1543 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1544 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1545 {
1546 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1547 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1548 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1549 ILLEGAL_REQUEST,
1550 SENCODE_INVALID_COMMAND,
1551 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1552 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1553 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1554 ? sizeof(scsicmd->sense_buffer)
1555 : sizeof(dev->fsa_dev[cid].sense_data));
1556 scsicmd->scsi_done(scsicmd);
1557 return 0;
1558 }
1559
1560
1561 /* Handle commands here that don't really require going out to the adapter */
1562 switch (scsicmd->cmnd[0]) {
1563 case INQUIRY:
1564 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001565 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Mark Haverkampe5718772006-03-27 09:43:25 -08001567 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001568 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001570 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001571 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 */
1572 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001574 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /*
1576 * Set the Vendor, Product, and Revision Level
1577 * see: <vendor>.c i.e. aac.c
1578 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001579 if (scmd_id(scsicmd) == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07001580 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001581 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1582 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1584 scsicmd->scsi_done(scsicmd);
1585 return 0;
1586 }
Mark Haverkamp794d0602005-10-24 10:51:53 -07001587 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001588 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1589 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return aac_get_container_name(scsicmd, cid);
1591 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001592 case SERVICE_ACTION_IN:
1593 if (!(dev->raw_io_interface) ||
1594 !(dev->raw_io_64) ||
1595 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1596 break;
1597 {
1598 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001599 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001600
1601 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1602 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001603 cp[0] = (capacity >> 56) & 0xff;
1604 cp[1] = (capacity >> 48) & 0xff;
1605 cp[2] = (capacity >> 40) & 0xff;
1606 cp[3] = (capacity >> 32) & 0xff;
1607 cp[4] = (capacity >> 24) & 0xff;
1608 cp[5] = (capacity >> 16) & 0xff;
1609 cp[6] = (capacity >> 8) & 0xff;
1610 cp[7] = (capacity >> 0) & 0xff;
1611 cp[8] = 0;
1612 cp[9] = 0;
1613 cp[10] = 2;
1614 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001615 cp[12] = 0;
1616 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08001617 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001618 if (sizeof(cp) < scsicmd->cmnd[13]) {
1619 unsigned int len, offset = sizeof(cp);
1620
1621 memset(cp, 0, offset);
1622 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08001623 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1624 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001625 aac_internal_transfer(scsicmd, cp, offset, len);
1626 } while ((offset += len) < scsicmd->cmnd[13]);
1627 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001628
1629 /* Do not cache partition table for arrays */
1630 scsicmd->device->removable = 1;
1631
1632 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1633 scsicmd->scsi_done(scsicmd);
1634
1635 return 0;
1636 }
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 case READ_CAPACITY:
1639 {
1640 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001641 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001644 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 capacity = fsa_dev_ptr[cid].size - 1;
1646 else
1647 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 cp[0] = (capacity >> 24) & 0xff;
1650 cp[1] = (capacity >> 16) & 0xff;
1651 cp[2] = (capacity >> 8) & 0xff;
1652 cp[3] = (capacity >> 0) & 0xff;
1653 cp[4] = 0;
1654 cp[5] = 0;
1655 cp[6] = 2;
1656 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001657 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001658 /* Do not cache partition table for arrays */
1659 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1662 scsicmd->scsi_done(scsicmd);
1663
1664 return 0;
1665 }
1666
1667 case MODE_SENSE:
1668 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001669 char mode_buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 mode_buf[0] = 3; /* Mode data length */
1673 mode_buf[1] = 0; /* Medium type - default */
1674 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1675 mode_buf[3] = 0; /* Block descriptor length */
1676
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001677 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1679 scsicmd->scsi_done(scsicmd);
1680
1681 return 0;
1682 }
1683 case MODE_SENSE_10:
1684 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001685 char mode_buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 mode_buf[0] = 0; /* Mode data length (MSB) */
1689 mode_buf[1] = 6; /* Mode data length (LSB) */
1690 mode_buf[2] = 0; /* Medium type - default */
1691 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1692 mode_buf[4] = 0; /* reserved */
1693 mode_buf[5] = 0; /* reserved */
1694 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1695 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001696 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1699 scsicmd->scsi_done(scsicmd);
1700
1701 return 0;
1702 }
1703 case REQUEST_SENSE:
1704 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1705 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1706 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1707 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1708 scsicmd->scsi_done(scsicmd);
1709 return 0;
1710
1711 case ALLOW_MEDIUM_REMOVAL:
1712 dprintk((KERN_DEBUG "LOCK command.\n"));
1713 if (scsicmd->cmnd[4])
1714 fsa_dev_ptr[cid].locked = 1;
1715 else
1716 fsa_dev_ptr[cid].locked = 0;
1717
1718 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1719 scsicmd->scsi_done(scsicmd);
1720 return 0;
1721 /*
1722 * These commands are all No-Ops
1723 */
1724 case TEST_UNIT_READY:
1725 case RESERVE:
1726 case RELEASE:
1727 case REZERO_UNIT:
1728 case REASSIGN_BLOCKS:
1729 case SEEK_10:
1730 case START_STOP:
1731 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1732 scsicmd->scsi_done(scsicmd);
1733 return 0;
1734 }
1735
1736 switch (scsicmd->cmnd[0])
1737 {
1738 case READ_6:
1739 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001740 case READ_12:
1741 case READ_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
1743 * Hack to keep track of ordinal number of the device that
1744 * corresponds to a container. Needed to convert
1745 * containers to /dev/sd device names
1746 */
1747
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001748 if (scsicmd->request->rq_disk)
1749 strlcpy(fsa_dev_ptr[cid].devname,
1750 scsicmd->request->rq_disk->disk_name,
1751 min(sizeof(fsa_dev_ptr[cid].devname),
1752 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001753
1754 return aac_read(scsicmd, cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 case WRITE_6:
1757 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001758 case WRITE_12:
1759 case WRITE_16:
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001760 return aac_write(scsicmd, cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 case SYNCHRONIZE_CACHE:
1763 /* Issue FIB to tell Firmware to flush it's cache */
1764 return aac_synchronize(scsicmd, cid);
1765
1766 default:
1767 /*
1768 * Unhandled commands
1769 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001770 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1772 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1773 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1774 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1775 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1776 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1777 ? sizeof(scsicmd->sense_buffer)
1778 : sizeof(dev->fsa_dev[cid].sense_data));
1779 scsicmd->scsi_done(scsicmd);
1780 return 0;
1781 }
1782}
1783
1784static int query_disk(struct aac_dev *dev, void __user *arg)
1785{
1786 struct aac_query_disk qd;
1787 struct fsa_dev_info *fsa_dev_ptr;
1788
1789 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001790 if (!fsa_dev_ptr)
1791 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1793 return -EFAULT;
1794 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08001795 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
1797 {
1798 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1799 return -EINVAL;
1800 qd.instance = dev->scsi_host_ptr->host_no;
1801 qd.bus = 0;
1802 qd.id = CONTAINER_TO_ID(qd.cnum);
1803 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1804 }
1805 else return -EINVAL;
1806
1807 qd.valid = fsa_dev_ptr[qd.cnum].valid;
1808 qd.locked = fsa_dev_ptr[qd.cnum].locked;
1809 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1810
1811 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1812 qd.unmapped = 1;
1813 else
1814 qd.unmapped = 0;
1815
1816 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1817 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1818
1819 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1820 return -EFAULT;
1821 return 0;
1822}
1823
1824static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1825{
1826 struct aac_delete_disk dd;
1827 struct fsa_dev_info *fsa_dev_ptr;
1828
1829 fsa_dev_ptr = dev->fsa_dev;
1830
1831 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1832 return -EFAULT;
1833
1834 if (dd.cnum >= dev->maximum_num_containers)
1835 return -EINVAL;
1836 /*
1837 * Mark this container as being deleted.
1838 */
1839 fsa_dev_ptr[dd.cnum].deleted = 1;
1840 /*
1841 * Mark the container as no longer valid
1842 */
1843 fsa_dev_ptr[dd.cnum].valid = 0;
1844 return 0;
1845}
1846
1847static int delete_disk(struct aac_dev *dev, void __user *arg)
1848{
1849 struct aac_delete_disk dd;
1850 struct fsa_dev_info *fsa_dev_ptr;
1851
1852 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001853 if (!fsa_dev_ptr)
1854 return -ENODEV;
1855 if (!fsa_dev_ptr)
1856 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1859 return -EFAULT;
1860
1861 if (dd.cnum >= dev->maximum_num_containers)
1862 return -EINVAL;
1863 /*
1864 * If the container is locked, it can not be deleted by the API.
1865 */
1866 if (fsa_dev_ptr[dd.cnum].locked)
1867 return -EBUSY;
1868 else {
1869 /*
1870 * Mark the container as no longer being valid.
1871 */
1872 fsa_dev_ptr[dd.cnum].valid = 0;
1873 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1874 return 0;
1875 }
1876}
1877
1878int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1879{
1880 switch (cmd) {
1881 case FSACTL_QUERY_DISK:
1882 return query_disk(dev, arg);
1883 case FSACTL_DELETE_DISK:
1884 return delete_disk(dev, arg);
1885 case FSACTL_FORCE_DELETE_DISK:
1886 return force_delete_disk(dev, arg);
1887 case FSACTL_GET_CONTAINERS:
1888 return aac_get_containers(dev);
1889 default:
1890 return -ENOTTY;
1891 }
1892}
1893
1894/**
1895 *
1896 * aac_srb_callback
1897 * @context: the context set in the fib - here it is scsi cmd
1898 * @fibptr: pointer to the fib
1899 *
1900 * Handles the completion of a scsi command to a non dasd device
1901 *
1902 */
1903
1904static void aac_srb_callback(void *context, struct fib * fibptr)
1905{
1906 struct aac_dev *dev;
1907 struct aac_srb_reply *srbreply;
1908 struct scsi_cmnd *scsicmd;
1909
1910 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001911 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1913
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001914 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1917
1918 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
1919 /*
1920 * Calculate resid for sg
1921 */
1922
1923 scsicmd->resid = scsicmd->request_bufflen -
1924 le32_to_cpu(srbreply->data_xfer_length);
1925
1926 if(scsicmd->use_sg)
1927 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02001928 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 scsicmd->use_sg,
1930 scsicmd->sc_data_direction);
1931 else if(scsicmd->request_bufflen)
1932 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1933 scsicmd->sc_data_direction);
1934
1935 /*
1936 * First check the fib status
1937 */
1938
1939 if (le32_to_cpu(srbreply->status) != ST_OK){
1940 int len;
1941 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1942 len = (le32_to_cpu(srbreply->sense_data_size) >
1943 sizeof(scsicmd->sense_buffer)) ?
1944 sizeof(scsicmd->sense_buffer) :
1945 le32_to_cpu(srbreply->sense_data_size);
1946 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1947 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1948 }
1949
1950 /*
1951 * Next check the srb status
1952 */
1953 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1954 case SRB_STATUS_ERROR_RECOVERY:
1955 case SRB_STATUS_PENDING:
1956 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08001957 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 break;
1959 case SRB_STATUS_DATA_OVERRUN:
1960 switch(scsicmd->cmnd[0]){
1961 case READ_6:
1962 case WRITE_6:
1963 case READ_10:
1964 case WRITE_10:
1965 case READ_12:
1966 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001967 case READ_16:
1968 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1970 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1971 } else {
1972 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1973 }
1974 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1975 break;
1976 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08001977 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 break;
1979 }
1980 default:
1981 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1982 break;
1983 }
1984 break;
1985 case SRB_STATUS_ABORTED:
1986 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1987 break;
1988 case SRB_STATUS_ABORT_FAILED:
1989 // Not sure about this one - but assuming the hba was trying to abort for some reason
1990 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1991 break;
1992 case SRB_STATUS_PARITY_ERROR:
1993 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1994 break;
1995 case SRB_STATUS_NO_DEVICE:
1996 case SRB_STATUS_INVALID_PATH_ID:
1997 case SRB_STATUS_INVALID_TARGET_ID:
1998 case SRB_STATUS_INVALID_LUN:
1999 case SRB_STATUS_SELECTION_TIMEOUT:
2000 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2001 break;
2002
2003 case SRB_STATUS_COMMAND_TIMEOUT:
2004 case SRB_STATUS_TIMEOUT:
2005 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2006 break;
2007
2008 case SRB_STATUS_BUSY:
2009 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2010 break;
2011
2012 case SRB_STATUS_BUS_RESET:
2013 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2014 break;
2015
2016 case SRB_STATUS_MESSAGE_REJECTED:
2017 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2018 break;
2019 case SRB_STATUS_REQUEST_FLUSHED:
2020 case SRB_STATUS_ERROR:
2021 case SRB_STATUS_INVALID_REQUEST:
2022 case SRB_STATUS_REQUEST_SENSE_FAILED:
2023 case SRB_STATUS_NO_HBA:
2024 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2025 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2026 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2027 case SRB_STATUS_DELAYED_RETRY:
2028 case SRB_STATUS_BAD_FUNCTION:
2029 case SRB_STATUS_NOT_STARTED:
2030 case SRB_STATUS_NOT_IN_USE:
2031 case SRB_STATUS_FORCE_ABORT:
2032 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2033 default:
2034#ifdef AAC_DETAILED_STATUS_INFO
2035 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2036 le32_to_cpu(srbreply->srb_status) & 0x3F,
2037 aac_get_status_string(
2038 le32_to_cpu(srbreply->srb_status) & 0x3F),
2039 scsicmd->cmnd[0],
2040 le32_to_cpu(srbreply->scsi_status));
2041#endif
2042 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2043 break;
2044 }
2045 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2046 int len;
2047 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2048 len = (le32_to_cpu(srbreply->sense_data_size) >
2049 sizeof(scsicmd->sense_buffer)) ?
2050 sizeof(scsicmd->sense_buffer) :
2051 le32_to_cpu(srbreply->sense_data_size);
2052#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002053 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2054 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055#endif
2056 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2057
2058 }
2059 /*
2060 * OR in the scsi status (already shifted up a bit)
2061 */
2062 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2063
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002064 aac_fib_complete(fibptr);
2065 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002066 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069/**
2070 *
2071 * aac_send_scb_fib
2072 * @scsicmd: the scsi command block
2073 *
2074 * This routine will form a FIB and fill in the aac_srb from the
2075 * scsicmd passed in.
2076 */
2077
2078static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2079{
2080 struct fib* cmd_fibcontext;
2081 struct aac_dev* dev;
2082 int status;
2083 struct aac_srb *srbcmd;
2084 u16 fibsize;
2085 u32 flag;
2086 u32 timeout;
2087
Mark Haverkamp84971732005-06-20 11:55:24 -07002088 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002089 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002090 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 scsicmd->result = DID_NO_CONNECT << 16;
2092 scsicmd->scsi_done(scsicmd);
2093 return 0;
2094 }
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 switch(scsicmd->sc_data_direction){
2097 case DMA_TO_DEVICE:
2098 flag = SRB_DataOut;
2099 break;
2100 case DMA_BIDIRECTIONAL:
2101 flag = SRB_DataIn | SRB_DataOut;
2102 break;
2103 case DMA_FROM_DEVICE:
2104 flag = SRB_DataIn;
2105 break;
2106 case DMA_NONE:
2107 default: /* shuts up some versions of gcc */
2108 flag = SRB_NoDataXfer;
2109 break;
2110 }
2111
2112
2113 /*
2114 * Allocate and initialize a Fib then setup a BlockWrite command
2115 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002116 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return -1;
2118 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002119 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
2122 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
Mark Haverkampe5718772006-03-27 09:43:25 -08002123 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(scsicmd)));
2124 srbcmd->id = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
2126 srbcmd->flags = cpu_to_le32(flag);
James Bottomley5262d082005-08-05 16:31:35 -05002127 timeout = scsicmd->timeout_per_command/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if(timeout == 0){
2129 timeout = 1;
2130 }
2131 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
2132 srbcmd->retry_limit = 0; /* Obsolete parameter */
2133 srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
2134
2135 if( dev->dac_support == 1 ) {
2136 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
2137 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2138
2139 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2140 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2141 /*
2142 * Build Scatter/Gather list
2143 */
2144 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
2145 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
2146 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002147 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 sizeof(struct aac_fibhdr)));
2149
2150 /*
2151 * Now send the Fib to the adapter
2152 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002153 status = aac_fib_send(ScsiPortCommand64, cmd_fibcontext,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 fibsize, FsaNormal, 0, 1,
2155 (fib_callback) aac_srb_callback,
2156 (void *) scsicmd);
2157 } else {
2158 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
2159 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2160
2161 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2162 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2163 /*
2164 * Build Scatter/Gather list
2165 */
2166 fibsize = sizeof (struct aac_srb) +
2167 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
2168 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002169 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 sizeof(struct aac_fibhdr)));
2171
2172 /*
2173 * Now send the Fib to the adapter
2174 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002175 status = aac_fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 (fib_callback) aac_srb_callback, (void *) scsicmd);
2177 }
2178 /*
2179 * Check that the command queued to the controller
2180 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002181 if (status == -EINPROGRESS) {
2182 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184 }
2185
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002186 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2187 aac_fib_complete(cmd_fibcontext);
2188 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 return -1;
2191}
2192
2193static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2194{
2195 struct aac_dev *dev;
2196 unsigned long byte_count = 0;
2197
2198 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2199 // Get rid of old data
2200 psg->count = 0;
2201 psg->sg[0].addr = 0;
2202 psg->sg[0].count = 0;
2203 if (scsicmd->use_sg) {
2204 struct scatterlist *sg;
2205 int i;
2206 int sg_count;
2207 sg = (struct scatterlist *) scsicmd->request_buffer;
2208
2209 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2210 scsicmd->sc_data_direction);
2211 psg->count = cpu_to_le32(sg_count);
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 for (i = 0; i < sg_count; i++) {
2214 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2215 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2216 byte_count += sg_dma_len(sg);
2217 sg++;
2218 }
2219 /* hba wants the size to be exact */
2220 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002221 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2222 (byte_count - scsicmd->request_bufflen);
2223 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 byte_count = scsicmd->request_bufflen;
2225 }
2226 /* Check for command underflow */
2227 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2228 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2229 byte_count, scsicmd->underflow);
2230 }
2231 }
2232 else if(scsicmd->request_bufflen) {
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002233 u32 addr;
2234 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 scsicmd->request_buffer,
2236 scsicmd->request_bufflen,
2237 scsicmd->sc_data_direction);
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002238 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 psg->count = cpu_to_le32(1);
2240 psg->sg[0].addr = cpu_to_le32(addr);
2241 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 byte_count = scsicmd->request_bufflen;
2243 }
2244 return byte_count;
2245}
2246
2247
2248static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2249{
2250 struct aac_dev *dev;
2251 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002252 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2255 // Get rid of old data
2256 psg->count = 0;
2257 psg->sg[0].addr[0] = 0;
2258 psg->sg[0].addr[1] = 0;
2259 psg->sg[0].count = 0;
2260 if (scsicmd->use_sg) {
2261 struct scatterlist *sg;
2262 int i;
2263 int sg_count;
2264 sg = (struct scatterlist *) scsicmd->request_buffer;
2265
2266 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2267 scsicmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 for (i = 0; i < sg_count; i++) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002270 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002271 addr = sg_dma_address(sg);
2272 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2273 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002274 psg->sg[i].count = cpu_to_le32(count);
2275 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 sg++;
2277 }
Mark Haverkamp1241f352006-03-27 09:44:23 -08002278 psg->count = cpu_to_le32(sg_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* hba wants the size to be exact */
2280 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002281 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2282 (byte_count - scsicmd->request_bufflen);
2283 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 byte_count = scsicmd->request_bufflen;
2285 }
2286 /* Check for command underflow */
2287 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2288 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2289 byte_count, scsicmd->underflow);
2290 }
2291 }
2292 else if(scsicmd->request_bufflen) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002293 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 scsicmd->request_buffer,
2295 scsicmd->request_bufflen,
2296 scsicmd->sc_data_direction);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002297 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 psg->count = cpu_to_le32(1);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002299 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2300 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 byte_count = scsicmd->request_bufflen;
2303 }
2304 return byte_count;
2305}
2306
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002307static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2308{
2309 struct Scsi_Host *host = scsicmd->device->host;
2310 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2311 unsigned long byte_count = 0;
2312
2313 // Get rid of old data
2314 psg->count = 0;
2315 psg->sg[0].next = 0;
2316 psg->sg[0].prev = 0;
2317 psg->sg[0].addr[0] = 0;
2318 psg->sg[0].addr[1] = 0;
2319 psg->sg[0].count = 0;
2320 psg->sg[0].flags = 0;
2321 if (scsicmd->use_sg) {
2322 struct scatterlist *sg;
2323 int i;
2324 int sg_count;
2325 sg = (struct scatterlist *) scsicmd->request_buffer;
2326
2327 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2328 scsicmd->sc_data_direction);
2329
2330 for (i = 0; i < sg_count; i++) {
2331 int count = sg_dma_len(sg);
2332 u64 addr = sg_dma_address(sg);
2333 psg->sg[i].next = 0;
2334 psg->sg[i].prev = 0;
2335 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2336 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2337 psg->sg[i].count = cpu_to_le32(count);
2338 psg->sg[i].flags = 0;
2339 byte_count += count;
2340 sg++;
2341 }
2342 psg->count = cpu_to_le32(sg_count);
2343 /* hba wants the size to be exact */
2344 if(byte_count > scsicmd->request_bufflen){
2345 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2346 (byte_count - scsicmd->request_bufflen);
2347 psg->sg[i-1].count = cpu_to_le32(temp);
2348 byte_count = scsicmd->request_bufflen;
2349 }
2350 /* Check for command underflow */
2351 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2352 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2353 byte_count, scsicmd->underflow);
2354 }
2355 }
2356 else if(scsicmd->request_bufflen) {
2357 int count;
2358 u64 addr;
2359 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2360 scsicmd->request_buffer,
2361 scsicmd->request_bufflen,
2362 scsicmd->sc_data_direction);
2363 addr = scsicmd->SCp.dma_handle;
2364 count = scsicmd->request_bufflen;
2365 psg->count = cpu_to_le32(1);
2366 psg->sg[0].next = 0;
2367 psg->sg[0].prev = 0;
2368 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2369 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2370 psg->sg[0].count = cpu_to_le32(count);
2371 psg->sg[0].flags = 0;
2372 byte_count = scsicmd->request_bufflen;
2373 }
2374 return byte_count;
2375}
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377#ifdef AAC_DETAILED_STATUS_INFO
2378
2379struct aac_srb_status_info {
2380 u32 status;
2381 char *str;
2382};
2383
2384
2385static struct aac_srb_status_info srb_status_info[] = {
2386 { SRB_STATUS_PENDING, "Pending Status"},
2387 { SRB_STATUS_SUCCESS, "Success"},
2388 { SRB_STATUS_ABORTED, "Aborted Command"},
2389 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002390 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 { SRB_STATUS_BUSY, "Device Busy"},
2392 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2393 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2394 { SRB_STATUS_NO_DEVICE, "No Device"},
2395 { SRB_STATUS_TIMEOUT, "Timeout"},
2396 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2397 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2398 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2399 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2400 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2401 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2402 { SRB_STATUS_NO_HBA, "No HBA"},
2403 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2404 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2405 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2406 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2407 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2408 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002409 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2411 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2412 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2413 { SRB_STATUS_NOT_STARTED, "Not Started"},
2414 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2415 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2416 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2417 { 0xff, "Unknown Error"}
2418};
2419
2420char *aac_get_status_string(u32 status)
2421{
2422 int i;
2423
Tobias Klauser6391a112006-06-08 22:23:48 -07002424 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2425 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 return "Bad Status Code";
2429}
2430
2431#endif