blob: 5d2989754c9f3a170bcb67ce295dee586584999b [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;
151
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800152module_param(nondasd, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800154module_param(dacmode, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800156module_param(commit, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157MODULE_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");
158
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700159int numacb = -1;
160module_param(numacb, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800161MODULE_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 -0700162
163int acbsize = -1;
164module_param(acbsize, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800165MODULE_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 -0700166/**
167 * aac_get_config_status - check the adapter configuration
168 * @common: adapter to query
169 *
170 * Query config status, and commit the configuration if needed.
171 */
172int aac_get_config_status(struct aac_dev *dev)
173{
174 int status = 0;
175 struct fib * fibptr;
176
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800177 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return -ENOMEM;
179
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800180 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 {
182 struct aac_get_config_status *dinfo;
183 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
184
185 dinfo->command = cpu_to_le32(VM_ContainerConfig);
186 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
187 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
188 }
189
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800190 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 fibptr,
192 sizeof (struct aac_get_config_status),
193 FsaNormal,
194 1, 1,
195 NULL, NULL);
196 if (status < 0 ) {
197 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
198 } else {
199 struct aac_get_config_status_resp *reply
200 = (struct aac_get_config_status_resp *) fib_data(fibptr);
201 dprintk((KERN_WARNING
202 "aac_get_config_status: response=%d status=%d action=%d\n",
203 le32_to_cpu(reply->response),
204 le32_to_cpu(reply->status),
205 le32_to_cpu(reply->data.action)));
206 if ((le32_to_cpu(reply->response) != ST_OK) ||
207 (le32_to_cpu(reply->status) != CT_OK) ||
208 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
209 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
210 status = -EINVAL;
211 }
212 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800213 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
215 if (status >= 0) {
216 if (commit == 1) {
217 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800218 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dinfo = (struct aac_commit_config *) fib_data(fibptr);
220
221 dinfo->command = cpu_to_le32(VM_ContainerConfig);
222 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
223
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800224 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 fibptr,
226 sizeof (struct aac_commit_config),
227 FsaNormal,
228 1, 1,
229 NULL, NULL);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800230 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 } else if (commit == 0) {
232 printk(KERN_WARNING
233 "aac_get_config_status: Foreign device configurations are being ignored\n");
234 }
235 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800236 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return status;
238}
239
240/**
241 * aac_get_containers - list containers
242 * @common: adapter to probe
243 *
244 * Make a list of all containers on this controller
245 */
246int aac_get_containers(struct aac_dev *dev)
247{
248 struct fsa_dev_info *fsa_dev_ptr;
249 u32 index;
250 int status = 0;
251 struct fib * fibptr;
252 unsigned instance;
253 struct aac_get_container_count *dinfo;
254 struct aac_get_container_count_resp *dresp;
255 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
256
257 instance = dev->scsi_host_ptr->unique_id;
258
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800259 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -ENOMEM;
261
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800262 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
264 dinfo->command = cpu_to_le32(VM_ContainerConfig);
265 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
266
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800267 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 fibptr,
269 sizeof (struct aac_get_container_count),
270 FsaNormal,
271 1, 1,
272 NULL, NULL);
273 if (status >= 0) {
274 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
275 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800276 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
280 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
282 sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
283 if (!fsa_dev_ptr) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800284 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -ENOMEM;
286 }
287 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
288
289 dev->fsa_dev = fsa_dev_ptr;
290 dev->maximum_num_containers = maximum_num_containers;
291
292 for (index = 0; index < dev->maximum_num_containers; index++) {
293 struct aac_query_mount *dinfo;
294 struct aac_mount *dresp;
295
296 fsa_dev_ptr[index].devname[0] = '\0';
297
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800298 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 dinfo = (struct aac_query_mount *) fib_data(fibptr);
300
301 dinfo->command = cpu_to_le32(VM_NameServe);
302 dinfo->count = cpu_to_le32(index);
303 dinfo->type = cpu_to_le32(FT_FILESYS);
304
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800305 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 fibptr,
307 sizeof (struct aac_query_mount),
308 FsaNormal,
309 1, 1,
310 NULL, NULL);
311 if (status < 0 ) {
312 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
313 break;
314 }
315 dresp = (struct aac_mount *)fib_data(fibptr);
316
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700317 if ((le32_to_cpu(dresp->status) == ST_OK) &&
318 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
319 dinfo->command = cpu_to_le32(VM_NameServe64);
320 dinfo->count = cpu_to_le32(index);
321 dinfo->type = cpu_to_le32(FT_FILESYS);
322
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800323 if (aac_fib_send(ContainerCommand,
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700324 fibptr,
325 sizeof(struct aac_query_mount),
326 FsaNormal,
327 1, 1,
328 NULL, NULL) < 0)
329 continue;
330 } else
331 dresp->mnt[0].capacityhigh = 0;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dprintk ((KERN_DEBUG
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700334 "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (int)index, (int)le32_to_cpu(dresp->status),
336 (int)le32_to_cpu(dresp->mnt[0].vol),
337 (int)le32_to_cpu(dresp->mnt[0].state),
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700338 ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
339 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if ((le32_to_cpu(dresp->status) == ST_OK) &&
341 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
342 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
343 fsa_dev_ptr[index].valid = 1;
344 fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700345 fsa_dev_ptr[index].size
346 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
347 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
349 fsa_dev_ptr[index].ro = 1;
350 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800351 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
353 * If there are no more containers, then stop asking.
354 */
355 if ((index + 1) >= le32_to_cpu(dresp->count)){
356 break;
357 }
358 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800359 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return status;
361}
362
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700363static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
364{
365 void *buf;
366 unsigned int transfer_len;
367 struct scatterlist *sg = scsicmd->request_buffer;
368
369 if (scsicmd->use_sg) {
370 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
371 transfer_len = min(sg->length, len + offset);
372 } else {
373 buf = scsicmd->request_buffer;
374 transfer_len = min(scsicmd->request_bufflen, len + offset);
375 }
376
377 memcpy(buf + offset, data, transfer_len - offset);
378
379 if (scsicmd->use_sg)
380 kunmap_atomic(buf - sg->offset, KM_IRQ0);
381
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void get_container_name_callback(void *context, struct fib * fibptr)
385{
386 struct aac_get_name_resp * get_name_reply;
387 struct scsi_cmnd * scsicmd;
388
389 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800390 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
393 if (fibptr == NULL)
394 BUG();
395
396 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
397 /* Failure is irrelevant, using default value instead */
398 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
399 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700400 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
402 while (*sp == ' ')
403 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700404 if (*sp) {
405 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
406 int count = sizeof(d);
407 char *dp = d;
408 do {
409 *dp++ = (*sp) ? *sp++ : ' ';
410 } while (--count > 0);
411 aac_internal_transfer(scsicmd, d,
412 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
417
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800418 aac_fib_complete(fibptr);
419 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700420 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/**
424 * aac_get_container_name - get container name, none blocking.
425 */
426static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
427{
428 int status;
429 struct aac_get_name *dinfo;
430 struct fib * cmd_fibcontext;
431 struct aac_dev * dev;
432
433 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
434
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800435 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -ENOMEM;
437
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800438 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
440
441 dinfo->command = cpu_to_le32(VM_ContainerConfig);
442 dinfo->type = cpu_to_le32(CT_READ_NAME);
443 dinfo->cid = cpu_to_le32(cid);
444 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
445
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800446 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 cmd_fibcontext,
448 sizeof (struct aac_get_name),
449 FsaNormal,
450 0, 1,
451 (fib_callback) get_container_name_callback,
452 (void *) scsicmd);
453
454 /*
455 * Check that the command queued to the controller
456 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800457 if (status == -EINPROGRESS) {
458 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800462 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
463 aac_fib_complete(cmd_fibcontext);
464 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -1;
466}
467
468/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800469 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * @dev: device to query
471 * @cid: container identifier
472 *
473 * Queries the controller about the given volume. The volume information
474 * is updated in the struct fsa_dev_info structure rather than returned.
475 */
476
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800477int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct fsa_dev_info *fsa_dev_ptr;
480 int status;
481 struct aac_query_mount *dinfo;
482 struct aac_mount *dresp;
483 struct fib * fibptr;
484 unsigned instance;
485
486 fsa_dev_ptr = dev->fsa_dev;
487 instance = dev->scsi_host_ptr->unique_id;
488
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800489 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -ENOMEM;
491
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800492 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 dinfo = (struct aac_query_mount *)fib_data(fibptr);
495
496 dinfo->command = cpu_to_le32(VM_NameServe);
497 dinfo->count = cpu_to_le32(cid);
498 dinfo->type = cpu_to_le32(FT_FILESYS);
499
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800500 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 fibptr,
502 sizeof(struct aac_query_mount),
503 FsaNormal,
504 1, 1,
505 NULL, NULL);
506 if (status < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800507 printk(KERN_WARNING "aacraid: aac_probe_container query failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto error;
509 }
510
511 dresp = (struct aac_mount *) fib_data(fibptr);
512
513 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700514 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
515 dinfo->command = cpu_to_le32(VM_NameServe64);
516 dinfo->count = cpu_to_le32(cid);
517 dinfo->type = cpu_to_le32(FT_FILESYS);
518
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800519 if (aac_fib_send(ContainerCommand,
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700520 fibptr,
521 sizeof(struct aac_query_mount),
522 FsaNormal,
523 1, 1,
524 NULL, NULL) < 0)
525 goto error;
526 } else
527 dresp->mnt[0].capacityhigh = 0;
528
529 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
531 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
532 fsa_dev_ptr[cid].valid = 1;
533 fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700534 fsa_dev_ptr[cid].size
535 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
536 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
538 fsa_dev_ptr[cid].ro = 1;
539 }
540
541error:
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800542 aac_fib_complete(fibptr);
543 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return status;
546}
547
548/* Local Structure to set SCSI inquiry data strings */
549struct scsi_inq {
550 char vid[8]; /* Vendor ID */
551 char pid[16]; /* Product ID */
552 char prl[4]; /* Product Revision Level */
553};
554
555/**
556 * InqStrCopy - string merge
557 * @a: string to copy from
558 * @b: string to copy to
559 *
560 * Copy a String from one location to another
561 * without copying \0
562 */
563
564static void inqstrcpy(char *a, char *b)
565{
566
567 while(*a != (char)0)
568 *b++ = *a++;
569}
570
571static char *container_types[] = {
572 "None",
573 "Volume",
574 "Mirror",
575 "Stripe",
576 "RAID5",
577 "SSRW",
578 "SSRO",
579 "Morph",
580 "Legacy",
581 "RAID4",
582 "RAID10",
583 "RAID00",
584 "V-MIRRORS",
585 "PSEUDO R4",
586 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700587 "RAID5D",
588 "RAID5D0",
589 "RAID1E",
590 "RAID6",
591 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 "Unknown"
593};
594
595
596
597/* Function: setinqstr
598 *
599 * Arguments: [1] pointer to void [1] int
600 *
601 * Purpose: Sets SCSI inquiry data strings for vendor, product
602 * and revision level. Allows strings to be set in platform dependant
603 * files instead of in OS dependant driver source.
604 */
605
Mark Haverkamp794d0602005-10-24 10:51:53 -0700606static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700611 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Mark Haverkamp794d0602005-10-24 10:51:53 -0700613 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
614 char * cp = dev->supplement_adapter_info.AdapterTypeText;
615 int c = sizeof(str->vid);
616 while (*cp && *cp != ' ' && --c)
617 ++cp;
618 c = *cp;
619 *cp = '\0';
620 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
621 str->vid);
622 *cp = c;
623 while (*cp && *cp != ' ')
624 ++cp;
625 while (*cp == ' ')
626 ++cp;
627 /* last six chars reserved for vol type */
628 c = 0;
629 if (strlen(cp) > sizeof(str->pid)) {
630 c = cp[sizeof(str->pid)];
631 cp[sizeof(str->pid)] = '\0';
632 }
633 inqstrcpy (cp, str->pid);
634 if (c)
635 cp[sizeof(str->pid)] = c;
636 } else {
637 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
638
639 inqstrcpy (mp->vname, str->vid);
640 /* last six chars reserved for vol type */
641 inqstrcpy (mp->model, str->pid);
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (tindex < (sizeof(container_types)/sizeof(char *))){
645 char *findit = str->pid;
646
647 for ( ; *findit != ' '; findit++); /* walk till we find a space */
648 /* RAID is superfluous in the context of a RAID device */
649 if (memcmp(findit-4, "RAID", 4) == 0)
650 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700651 if (((findit - str->pid) + strlen(container_types[tindex]))
652 < (sizeof(str->pid) + sizeof(str->prl)))
653 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 inqstrcpy ("V1.0", str->prl);
656}
657
Adrian Bunk 48338692005-04-25 19:45:58 -0700658static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
659 u8 a_sense_code, u8 incorrect_length,
660 u8 bit_pointer, u16 field_pointer,
661 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
664 sense_buf[1] = 0; /* Segment number, always zero */
665
666 if (incorrect_length) {
667 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
668 sense_buf[3] = BYTE3(residue);
669 sense_buf[4] = BYTE2(residue);
670 sense_buf[5] = BYTE1(residue);
671 sense_buf[6] = BYTE0(residue);
672 } else
673 sense_buf[2] = sense_key; /* Sense key */
674
675 if (sense_key == ILLEGAL_REQUEST)
676 sense_buf[7] = 10; /* Additional sense length */
677 else
678 sense_buf[7] = 6; /* Additional sense length */
679
680 sense_buf[12] = sense_code; /* Additional sense code */
681 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
682 if (sense_key == ILLEGAL_REQUEST) {
683 sense_buf[15] = 0;
684
685 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
686 sense_buf[15] = 0x80;/* Std sense key specific field */
687 /* Illegal parameter is in the parameter block */
688
689 if (sense_code == SENCODE_INVALID_CDB_FIELD)
690 sense_buf[15] = 0xc0;/* Std sense key specific field */
691 /* Illegal parameter is in the CDB block */
692 sense_buf[15] |= bit_pointer;
693 sense_buf[16] = field_pointer >> 8; /* MSB */
694 sense_buf[17] = field_pointer; /* LSB */
695 }
696}
697
698int aac_get_adapter_info(struct aac_dev* dev)
699{
700 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int rcode;
702 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -0700703 struct aac_adapter_info *info;
704 struct aac_bus_info *command;
705 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700706
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800707 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -ENOMEM;
709
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800710 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700711 info = (struct aac_adapter_info *) fib_data(fibptr);
712 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800714 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700715 fibptr,
716 sizeof(*info),
717 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -0700718 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700719 NULL,
720 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700722 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800723 aac_fib_complete(fibptr);
724 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700725 return rcode;
726 }
727 memcpy(&dev->adapter_info, info, sizeof(*info));
728
729 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
730 struct aac_supplement_adapter_info * info;
731
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800732 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700733
734 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
735
736 memset(info,0,sizeof(*info));
737
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800738 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700739 fibptr,
740 sizeof(*info),
741 FsaNormal,
742 1, 1,
743 NULL,
744 NULL);
745
746 if (rcode >= 0)
747 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Mark Haverkamp84971732005-06-20 11:55:24 -0700750
751 /*
752 * GetBusInfo
753 */
754
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800755 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -0700756
757 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
758
759 memset(bus_info, 0, sizeof(*bus_info));
760
761 command = (struct aac_bus_info *)bus_info;
762
763 command->Command = cpu_to_le32(VM_Ioctl);
764 command->ObjType = cpu_to_le32(FT_DRIVE);
765 command->MethodId = cpu_to_le32(1);
766 command->CtlCmd = cpu_to_le32(GetBusInfo);
767
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800768 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -0700769 fibptr,
770 sizeof (*bus_info),
771 FsaNormal,
772 1, 1,
773 NULL, NULL);
774
775 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
776 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
777 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
778 }
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700781 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 dev->name,
783 dev->id,
784 tmp>>24,
785 (tmp>>16)&0xff,
786 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700787 le32_to_cpu(dev->adapter_info.kernelbuild),
788 (int)sizeof(dev->supplement_adapter_info.BuildDate),
789 dev->supplement_adapter_info.BuildDate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
791 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
792 dev->name, dev->id,
793 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
794 le32_to_cpu(dev->adapter_info.monitorbuild));
795 tmp = le32_to_cpu(dev->adapter_info.biosrev);
796 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
797 dev->name, dev->id,
798 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
799 le32_to_cpu(dev->adapter_info.biosbuild));
800 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
801 printk(KERN_INFO "%s%d: serial %x\n",
802 dev->name, dev->id,
803 le32_to_cpu(dev->adapter_info.serial[0]));
804
805 dev->nondasd_support = 0;
806 dev->raid_scsi_mode = 0;
807 if(dev->adapter_info.options & AAC_OPT_NONDASD){
808 dev->nondasd_support = 1;
809 }
810
811 /*
812 * If the firmware supports ROMB RAID/SCSI mode and we are currently
813 * in RAID/SCSI mode, set the flag. For now if in this mode we will
814 * force nondasd support on. If we decide to allow the non-dasd flag
815 * additional changes changes will have to be made to support
816 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
817 * changed to support the new dev->raid_scsi_mode flag instead of
818 * leaching off of the dev->nondasd_support flag. Also in linit.c the
819 * function aac_detect will have to be modified where it sets up the
820 * max number of channels based on the aac->nondasd_support flag only.
821 */
822 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
823 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
824 dev->nondasd_support = 1;
825 dev->raid_scsi_mode = 1;
826 }
827 if (dev->raid_scsi_mode != 0)
828 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
829 dev->name, dev->id);
830
831 if(nondasd != -1) {
832 dev->nondasd_support = (nondasd!=0);
833 }
834 if(dev->nondasd_support != 0){
835 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
836 }
837
838 dev->dac_support = 0;
839 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
840 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
841 dev->dac_support = 1;
842 }
843
844 if(dacmode != -1) {
845 dev->dac_support = (dacmode!=0);
846 }
847 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -0500848 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
849 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
851 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -0500852 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
853 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
855 dev->name, dev->id);
856 dev->dac_support = 0;
857 } else {
858 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
859 dev->name, dev->id);
860 rcode = -ENOMEM;
861 }
862 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700863 /*
864 * 57 scatter gather elements
865 */
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700866 if (!(dev->raw_io_interface)) {
867 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700868 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700869 sizeof(struct aac_write) + sizeof(struct sgentry)) /
870 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700871 if (dev->dac_support) {
872 /*
873 * 38 scatter gather elements
874 */
875 dev->scsi_host_ptr->sg_tablesize =
876 (dev->max_fib_size -
877 sizeof(struct aac_fibhdr) -
878 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -0700879 sizeof(struct sgentry64)) /
880 sizeof(struct sgentry64);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700881 }
882 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
883 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
884 /*
885 * Worst case size that could cause sg overflow when
886 * we break up SG elements that are larger than 64KB.
887 * Would be nice if we could tell the SCSI layer what
888 * the maximum SG element size can be. Worst case is
889 * (sg_tablesize-1) 4KB elements with one 64KB
890 * element.
891 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
892 */
893 dev->scsi_host_ptr->max_sectors =
894 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
895 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800898 aac_fib_complete(fibptr);
899 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 return rcode;
902}
903
904
Mark Haverkampe53cb352005-08-03 15:39:09 -0700905static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct aac_dev *dev;
908 struct aac_read_reply *readreply;
909 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 u32 cid;
911
912 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800913 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -0800916 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700918 if (nblank(dprintk(x))) {
919 u64 lba;
920 switch (scsicmd->cmnd[0]) {
921 case WRITE_6:
922 case READ_6:
923 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
924 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
925 break;
926 case WRITE_16:
927 case READ_16:
928 lba = ((u64)scsicmd->cmnd[2] << 56) |
929 ((u64)scsicmd->cmnd[3] << 48) |
930 ((u64)scsicmd->cmnd[4] << 40) |
931 ((u64)scsicmd->cmnd[5] << 32) |
932 ((u64)scsicmd->cmnd[6] << 24) |
933 (scsicmd->cmnd[7] << 16) |
934 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
935 break;
936 case WRITE_12:
937 case READ_12:
938 lba = ((u64)scsicmd->cmnd[2] << 24) |
939 (scsicmd->cmnd[3] << 16) |
940 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
941 break;
942 default:
943 lba = ((u64)scsicmd->cmnd[2] << 24) |
944 (scsicmd->cmnd[3] << 16) |
945 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
946 break;
947 }
948 printk(KERN_DEBUG
949 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
950 smp_processor_id(), (unsigned long long)lba, jiffies);
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if (fibptr == NULL)
954 BUG();
955
956 if(scsicmd->use_sg)
957 pci_unmap_sg(dev->pdev,
958 (struct scatterlist *)scsicmd->buffer,
959 scsicmd->use_sg,
960 scsicmd->sc_data_direction);
961 else if(scsicmd->request_bufflen)
962 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
963 scsicmd->request_bufflen,
964 scsicmd->sc_data_direction);
965 readreply = (struct aac_read_reply *)fib_data(fibptr);
966 if (le32_to_cpu(readreply->status) == ST_OK)
967 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
968 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700969#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -0700970 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700971 le32_to_cpu(readreply->status));
972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
974 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
975 HARDWARE_ERROR,
976 SENCODE_INTERNAL_TARGET_FAILURE,
977 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
978 0, 0);
979 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
980 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
981 ? sizeof(scsicmd->sense_buffer)
982 : sizeof(dev->fsa_dev[cid].sense_data));
983 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800984 aac_fib_complete(fibptr);
985 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700987 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Adrian Bunk 48338692005-04-25 19:45:58 -0700990static int aac_read(struct scsi_cmnd * scsicmd, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700992 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 u32 count;
994 int status;
995
996 u16 fibsize;
997 struct aac_dev *dev;
998 struct fib * cmd_fibcontext;
999
1000 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1001 /*
1002 * Get block address and transfer length
1003 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001004 switch (scsicmd->cmnd[0]) {
1005 case READ_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
1007
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001008 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1009 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 count = scsicmd->cmnd[4];
1011
1012 if (count == 0)
1013 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001014 break;
1015 case READ_16:
1016 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
1017
1018 lba = ((u64)scsicmd->cmnd[2] << 56) |
1019 ((u64)scsicmd->cmnd[3] << 48) |
1020 ((u64)scsicmd->cmnd[4] << 40) |
1021 ((u64)scsicmd->cmnd[5] << 32) |
1022 ((u64)scsicmd->cmnd[6] << 24) |
1023 (scsicmd->cmnd[7] << 16) |
1024 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1025 count = (scsicmd->cmnd[10] << 24) |
1026 (scsicmd->cmnd[11] << 16) |
1027 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1028 break;
1029 case READ_12:
1030 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
1031
1032 lba = ((u64)scsicmd->cmnd[2] << 24) |
1033 (scsicmd->cmnd[3] << 16) |
1034 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1035 count = (scsicmd->cmnd[6] << 24) |
1036 (scsicmd->cmnd[7] << 16) |
1037 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1038 break;
1039 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
1041
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001042 lba = ((u64)scsicmd->cmnd[2] << 24) |
1043 (scsicmd->cmnd[3] << 16) |
1044 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001048 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001049 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001050 if ((!(dev->raw_io_interface) || !(dev->raw_io_64)) &&
1051 (lba & 0xffffffff00000000LL)) {
1052 dprintk((KERN_DEBUG "aac_read: Illegal lba\n"));
1053 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1054 SAM_STAT_CHECK_CONDITION;
1055 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1056 HARDWARE_ERROR,
1057 SENCODE_INTERNAL_TARGET_FAILURE,
1058 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1059 0, 0);
1060 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1061 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1062 ? sizeof(scsicmd->sense_buffer)
1063 : sizeof(dev->fsa_dev[cid].sense_data));
1064 scsicmd->scsi_done(scsicmd);
1065 return 0;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /*
1068 * Alocate and initialize a Fib
1069 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001070 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -1;
1072 }
1073
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001074 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001076 if (dev->raw_io_interface) {
1077 struct aac_raw_io *readcmd;
1078 readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001079 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1080 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001081 readcmd->count = cpu_to_le32(count<<9);
1082 readcmd->cid = cpu_to_le16(cid);
1083 readcmd->flags = cpu_to_le16(1);
1084 readcmd->bpTotal = 0;
1085 readcmd->bpComplete = 0;
1086
1087 aac_build_sgraw(scsicmd, &readcmd->sg);
1088 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
1089 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
1090 BUG();
1091 /*
1092 * Now send the Fib to the adapter
1093 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001094 status = aac_fib_send(ContainerRawIo,
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001095 cmd_fibcontext,
1096 fibsize,
1097 FsaNormal,
1098 0, 1,
1099 (fib_callback) io_callback,
1100 (void *) scsicmd);
1101 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct aac_read64 *readcmd;
1103 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
1104 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1105 readcmd->cid = cpu_to_le16(cid);
1106 readcmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001107 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 readcmd->pad = 0;
1109 readcmd->flags = 0;
1110
1111 aac_build_sg64(scsicmd, &readcmd->sg);
1112 fibsize = sizeof(struct aac_read64) +
1113 ((le32_to_cpu(readcmd->sg.count) - 1) *
1114 sizeof (struct sgentry64));
Mark Haverkamp77d71d22005-09-01 08:19:23 -07001115 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 sizeof(struct aac_fibhdr)));
1117 /*
1118 * Now send the Fib to the adapter
1119 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001120 status = aac_fib_send(ContainerCommand64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 cmd_fibcontext,
1122 fibsize,
1123 FsaNormal,
1124 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001125 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 (void *) scsicmd);
1127 } else {
1128 struct aac_read *readcmd;
1129 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
1130 readcmd->command = cpu_to_le32(VM_CtBlockRead);
1131 readcmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001132 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 readcmd->count = cpu_to_le32(count * 512);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 aac_build_sg(scsicmd, &readcmd->sg);
1136 fibsize = sizeof(struct aac_read) +
1137 ((le32_to_cpu(readcmd->sg.count) - 1) *
1138 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001139 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 sizeof(struct aac_fibhdr)));
1141 /*
1142 * Now send the Fib to the adapter
1143 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001144 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 cmd_fibcontext,
1146 fibsize,
1147 FsaNormal,
1148 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001149 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 (void *) scsicmd);
1151 }
1152
1153
1154
1155 /*
1156 * Check that the command queued to the controller
1157 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001158 if (status == -EINPROGRESS) {
1159 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001163 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * For some reason, the Fib didn't queue, return QUEUE_FULL
1166 */
1167 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001168 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001169 aac_fib_complete(cmd_fibcontext);
1170 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172}
1173
1174static int aac_write(struct scsi_cmnd * scsicmd, int cid)
1175{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001176 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 u32 count;
1178 int status;
1179 u16 fibsize;
1180 struct aac_dev *dev;
1181 struct fib * cmd_fibcontext;
1182
1183 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1184 /*
1185 * Get block address and transfer length
1186 */
1187 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1188 {
1189 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1190 count = scsicmd->cmnd[4];
1191 if (count == 0)
1192 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001193 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1194 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
1195
1196 lba = ((u64)scsicmd->cmnd[2] << 56) |
1197 ((u64)scsicmd->cmnd[3] << 48) |
1198 ((u64)scsicmd->cmnd[4] << 40) |
1199 ((u64)scsicmd->cmnd[5] << 32) |
1200 ((u64)scsicmd->cmnd[6] << 24) |
1201 (scsicmd->cmnd[7] << 16) |
1202 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1203 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1204 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1205 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1206 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
1207
1208 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1209 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1210 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1211 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 } else {
1213 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001214 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1216 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001217 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001219 if ((!(dev->raw_io_interface) || !(dev->raw_io_64))
1220 && (lba & 0xffffffff00000000LL)) {
1221 dprintk((KERN_DEBUG "aac_write: Illegal lba\n"));
1222 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1223 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1224 HARDWARE_ERROR,
1225 SENCODE_INTERNAL_TARGET_FAILURE,
1226 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1227 0, 0);
1228 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1229 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1230 ? sizeof(scsicmd->sense_buffer)
1231 : sizeof(dev->fsa_dev[cid].sense_data));
1232 scsicmd->scsi_done(scsicmd);
1233 return 0;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /*
1236 * Allocate and initialize a Fib then setup a BlockWrite command
1237 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001238 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001240 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001243 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001245 if (dev->raw_io_interface) {
1246 struct aac_raw_io *writecmd;
1247 writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001248 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1249 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001250 writecmd->count = cpu_to_le32(count<<9);
1251 writecmd->cid = cpu_to_le16(cid);
1252 writecmd->flags = 0;
1253 writecmd->bpTotal = 0;
1254 writecmd->bpComplete = 0;
1255
1256 aac_build_sgraw(scsicmd, &writecmd->sg);
1257 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1258 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
1259 BUG();
1260 /*
1261 * Now send the Fib to the adapter
1262 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001263 status = aac_fib_send(ContainerRawIo,
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001264 cmd_fibcontext,
1265 fibsize,
1266 FsaNormal,
1267 0, 1,
1268 (fib_callback) io_callback,
1269 (void *) scsicmd);
1270 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct aac_write64 *writecmd;
1272 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
1273 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1274 writecmd->cid = cpu_to_le16(cid);
1275 writecmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001276 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 writecmd->pad = 0;
1278 writecmd->flags = 0;
1279
1280 aac_build_sg64(scsicmd, &writecmd->sg);
1281 fibsize = sizeof(struct aac_write64) +
1282 ((le32_to_cpu(writecmd->sg.count) - 1) *
1283 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001284 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 sizeof(struct aac_fibhdr)));
1286 /*
1287 * Now send the Fib to the adapter
1288 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001289 status = aac_fib_send(ContainerCommand64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 cmd_fibcontext,
1291 fibsize,
1292 FsaNormal,
1293 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001294 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 (void *) scsicmd);
1296 } else {
1297 struct aac_write *writecmd;
1298 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
1299 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1300 writecmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001301 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 writecmd->count = cpu_to_le32(count * 512);
1303 writecmd->sg.count = cpu_to_le32(1);
1304 /* ->stable is not used - it did mean which type of write */
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 aac_build_sg(scsicmd, &writecmd->sg);
1307 fibsize = sizeof(struct aac_write) +
1308 ((le32_to_cpu(writecmd->sg.count) - 1) *
1309 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001310 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 sizeof(struct aac_fibhdr)));
1312 /*
1313 * Now send the Fib to the adapter
1314 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001315 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 cmd_fibcontext,
1317 fibsize,
1318 FsaNormal,
1319 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001320 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 (void *) scsicmd);
1322 }
1323
1324 /*
1325 * Check that the command queued to the controller
1326 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001327 if (status == -EINPROGRESS) {
1328 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
1330 }
1331
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001332 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
1334 * For some reason, the Fib didn't queue, return QUEUE_FULL
1335 */
1336 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001337 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001339 aac_fib_complete(cmd_fibcontext);
1340 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
1342}
1343
1344static void synchronize_callback(void *context, struct fib *fibptr)
1345{
1346 struct aac_synchronize_reply *synchronizereply;
1347 struct scsi_cmnd *cmd;
1348
1349 cmd = context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001350 cmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1353 smp_processor_id(), jiffies));
1354 BUG_ON(fibptr == NULL);
1355
1356
1357 synchronizereply = fib_data(fibptr);
1358 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1359 cmd->result = DID_OK << 16 |
1360 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1361 else {
1362 struct scsi_device *sdev = cmd->device;
1363 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08001364 u32 cid = sdev_id(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 printk(KERN_WARNING
1366 "synchronize_callback: synchronize failed, status = %d\n",
1367 le32_to_cpu(synchronizereply->status));
1368 cmd->result = DID_OK << 16 |
1369 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1370 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1371 HARDWARE_ERROR,
1372 SENCODE_INTERNAL_TARGET_FAILURE,
1373 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1374 0, 0);
1375 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1376 min(sizeof(dev->fsa_dev[cid].sense_data),
1377 sizeof(cmd->sense_buffer)));
1378 }
1379
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001380 aac_fib_complete(fibptr);
1381 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001382 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1386{
1387 int status;
1388 struct fib *cmd_fibcontext;
1389 struct aac_synchronize *synchronizecmd;
1390 struct scsi_cmnd *cmd;
1391 struct scsi_device *sdev = scsicmd->device;
1392 int active = 0;
1393 unsigned long flags;
1394
1395 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001396 * Wait for all outstanding queued commands to complete to this
1397 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
1399 spin_lock_irqsave(&sdev->list_lock, flags);
1400 list_for_each_entry(cmd, &sdev->cmd_list, list)
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001401 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 ++active;
1403 break;
1404 }
1405
1406 spin_unlock_irqrestore(&sdev->list_lock, flags);
1407
1408 /*
1409 * Yield the processor (requeue for later)
1410 */
1411 if (active)
1412 return SCSI_MLQUEUE_DEVICE_BUSY;
1413
1414 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001415 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
1417 if (!(cmd_fibcontext =
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001418 aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return SCSI_MLQUEUE_HOST_BUSY;
1420
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001421 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 synchronizecmd = fib_data(cmd_fibcontext);
1424 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1425 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1426 synchronizecmd->cid = cpu_to_le32(cid);
1427 synchronizecmd->count =
1428 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1429
1430 /*
1431 * Now send the Fib to the adapter
1432 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001433 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 cmd_fibcontext,
1435 sizeof(struct aac_synchronize),
1436 FsaNormal,
1437 0, 1,
1438 (fib_callback)synchronize_callback,
1439 (void *)scsicmd);
1440
1441 /*
1442 * Check that the command queued to the controller
1443 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001444 if (status == -EINPROGRESS) {
1445 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001450 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1451 aac_fib_complete(cmd_fibcontext);
1452 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return SCSI_MLQUEUE_HOST_BUSY;
1454}
1455
1456/**
1457 * aac_scsi_cmd() - Process SCSI command
1458 * @scsicmd: SCSI command block
1459 *
1460 * Emulate a SCSI command and queue the required request for the
1461 * aacraid firmware.
1462 */
1463
1464int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1465{
1466 u32 cid = 0;
1467 struct Scsi_Host *host = scsicmd->device->host;
1468 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1469 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /*
1472 * If the bus, id or lun is out of range, return fail
1473 * Test does not apply to ID 16, the pseudo id for the controller
1474 * itself.
1475 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001476 if (scmd_id(scsicmd) != host->this_id) {
Mark Haverkampe5718772006-03-27 09:43:25 -08001477 if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1478 if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1479 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 scsicmd->result = DID_NO_CONNECT << 16;
1481 scsicmd->scsi_done(scsicmd);
1482 return 0;
1483 }
Mark Haverkampe5718772006-03-27 09:43:25 -08001484 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /*
1487 * If the target container doesn't exist, it may have
1488 * been newly created
1489 */
1490 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1491 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001492 case SERVICE_ACTION_IN:
1493 if (!(dev->raw_io_interface) ||
1494 !(dev->raw_io_64) ||
1495 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1496 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 case INQUIRY:
1498 case READ_CAPACITY:
1499 case TEST_UNIT_READY:
1500 spin_unlock_irq(host->host_lock);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001501 aac_probe_container(dev, cid);
Mark Haverkamp131256c2005-09-26 13:04:56 -07001502 if ((fsa_dev_ptr[cid].valid & 1) == 0)
1503 fsa_dev_ptr[cid].valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 spin_lock_irq(host->host_lock);
1505 if (fsa_dev_ptr[cid].valid == 0) {
1506 scsicmd->result = DID_NO_CONNECT << 16;
1507 scsicmd->scsi_done(scsicmd);
1508 return 0;
1509 }
1510 default:
1511 break;
1512 }
1513 }
1514 /*
1515 * If the target container still doesn't exist,
1516 * return failure
1517 */
1518 if (fsa_dev_ptr[cid].valid == 0) {
1519 scsicmd->result = DID_BAD_TARGET << 16;
1520 scsicmd->scsi_done(scsicmd);
1521 return 0;
1522 }
1523 } else { /* check for physical non-dasd devices */
1524 if(dev->nondasd_support == 1){
1525 return aac_send_srb_fib(scsicmd);
1526 } else {
1527 scsicmd->result = DID_NO_CONNECT << 16;
1528 scsicmd->scsi_done(scsicmd);
1529 return 0;
1530 }
1531 }
1532 }
1533 /*
1534 * else Command for the controller itself
1535 */
1536 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1537 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1538 {
1539 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1540 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1541 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1542 ILLEGAL_REQUEST,
1543 SENCODE_INVALID_COMMAND,
1544 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1545 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1546 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1547 ? sizeof(scsicmd->sense_buffer)
1548 : sizeof(dev->fsa_dev[cid].sense_data));
1549 scsicmd->scsi_done(scsicmd);
1550 return 0;
1551 }
1552
1553
1554 /* Handle commands here that don't really require going out to the adapter */
1555 switch (scsicmd->cmnd[0]) {
1556 case INQUIRY:
1557 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001558 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Mark Haverkampe5718772006-03-27 09:43:25 -08001560 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001561 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001563 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001564 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 */
1565 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001567 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /*
1569 * Set the Vendor, Product, and Revision Level
1570 * see: <vendor>.c i.e. aac.c
1571 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001572 if (scmd_id(scsicmd) == host->this_id) {
Mark Haverkamp794d0602005-10-24 10:51:53 -07001573 setinqstr(dev, (void *) (inq_data.inqd_vid), (sizeof(container_types)/sizeof(char *)));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001574 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1575 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1577 scsicmd->scsi_done(scsicmd);
1578 return 0;
1579 }
Mark Haverkamp794d0602005-10-24 10:51:53 -07001580 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001581 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1582 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return aac_get_container_name(scsicmd, cid);
1584 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001585 case SERVICE_ACTION_IN:
1586 if (!(dev->raw_io_interface) ||
1587 !(dev->raw_io_64) ||
1588 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1589 break;
1590 {
1591 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001592 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001593
1594 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1595 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001596 cp[0] = (capacity >> 56) & 0xff;
1597 cp[1] = (capacity >> 48) & 0xff;
1598 cp[2] = (capacity >> 40) & 0xff;
1599 cp[3] = (capacity >> 32) & 0xff;
1600 cp[4] = (capacity >> 24) & 0xff;
1601 cp[5] = (capacity >> 16) & 0xff;
1602 cp[6] = (capacity >> 8) & 0xff;
1603 cp[7] = (capacity >> 0) & 0xff;
1604 cp[8] = 0;
1605 cp[9] = 0;
1606 cp[10] = 2;
1607 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001608 cp[12] = 0;
1609 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08001610 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001611 if (sizeof(cp) < scsicmd->cmnd[13]) {
1612 unsigned int len, offset = sizeof(cp);
1613
1614 memset(cp, 0, offset);
1615 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08001616 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1617 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001618 aac_internal_transfer(scsicmd, cp, offset, len);
1619 } while ((offset += len) < scsicmd->cmnd[13]);
1620 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001621
1622 /* Do not cache partition table for arrays */
1623 scsicmd->device->removable = 1;
1624
1625 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1626 scsicmd->scsi_done(scsicmd);
1627
1628 return 0;
1629 }
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 case READ_CAPACITY:
1632 {
1633 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001634 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001637 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 capacity = fsa_dev_ptr[cid].size - 1;
1639 else
1640 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 cp[0] = (capacity >> 24) & 0xff;
1643 cp[1] = (capacity >> 16) & 0xff;
1644 cp[2] = (capacity >> 8) & 0xff;
1645 cp[3] = (capacity >> 0) & 0xff;
1646 cp[4] = 0;
1647 cp[5] = 0;
1648 cp[6] = 2;
1649 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001650 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001651 /* Do not cache partition table for arrays */
1652 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1655 scsicmd->scsi_done(scsicmd);
1656
1657 return 0;
1658 }
1659
1660 case MODE_SENSE:
1661 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001662 char mode_buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 mode_buf[0] = 3; /* Mode data length */
1666 mode_buf[1] = 0; /* Medium type - default */
1667 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1668 mode_buf[3] = 0; /* Block descriptor length */
1669
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001670 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1672 scsicmd->scsi_done(scsicmd);
1673
1674 return 0;
1675 }
1676 case MODE_SENSE_10:
1677 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001678 char mode_buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 mode_buf[0] = 0; /* Mode data length (MSB) */
1682 mode_buf[1] = 6; /* Mode data length (LSB) */
1683 mode_buf[2] = 0; /* Medium type - default */
1684 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1685 mode_buf[4] = 0; /* reserved */
1686 mode_buf[5] = 0; /* reserved */
1687 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1688 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001689 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1692 scsicmd->scsi_done(scsicmd);
1693
1694 return 0;
1695 }
1696 case REQUEST_SENSE:
1697 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1698 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1699 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1700 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1701 scsicmd->scsi_done(scsicmd);
1702 return 0;
1703
1704 case ALLOW_MEDIUM_REMOVAL:
1705 dprintk((KERN_DEBUG "LOCK command.\n"));
1706 if (scsicmd->cmnd[4])
1707 fsa_dev_ptr[cid].locked = 1;
1708 else
1709 fsa_dev_ptr[cid].locked = 0;
1710
1711 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1712 scsicmd->scsi_done(scsicmd);
1713 return 0;
1714 /*
1715 * These commands are all No-Ops
1716 */
1717 case TEST_UNIT_READY:
1718 case RESERVE:
1719 case RELEASE:
1720 case REZERO_UNIT:
1721 case REASSIGN_BLOCKS:
1722 case SEEK_10:
1723 case START_STOP:
1724 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1725 scsicmd->scsi_done(scsicmd);
1726 return 0;
1727 }
1728
1729 switch (scsicmd->cmnd[0])
1730 {
1731 case READ_6:
1732 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001733 case READ_12:
1734 case READ_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
1736 * Hack to keep track of ordinal number of the device that
1737 * corresponds to a container. Needed to convert
1738 * containers to /dev/sd device names
1739 */
1740
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001741 if (scsicmd->request->rq_disk)
1742 strlcpy(fsa_dev_ptr[cid].devname,
1743 scsicmd->request->rq_disk->disk_name,
1744 min(sizeof(fsa_dev_ptr[cid].devname),
1745 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001746
1747 return aac_read(scsicmd, cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 case WRITE_6:
1750 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001751 case WRITE_12:
1752 case WRITE_16:
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001753 return aac_write(scsicmd, cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 case SYNCHRONIZE_CACHE:
1756 /* Issue FIB to tell Firmware to flush it's cache */
1757 return aac_synchronize(scsicmd, cid);
1758
1759 default:
1760 /*
1761 * Unhandled commands
1762 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001763 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1765 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1766 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1767 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1768 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1769 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1770 ? sizeof(scsicmd->sense_buffer)
1771 : sizeof(dev->fsa_dev[cid].sense_data));
1772 scsicmd->scsi_done(scsicmd);
1773 return 0;
1774 }
1775}
1776
1777static int query_disk(struct aac_dev *dev, void __user *arg)
1778{
1779 struct aac_query_disk qd;
1780 struct fsa_dev_info *fsa_dev_ptr;
1781
1782 fsa_dev_ptr = dev->fsa_dev;
1783 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1784 return -EFAULT;
1785 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08001786 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
1788 {
1789 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1790 return -EINVAL;
1791 qd.instance = dev->scsi_host_ptr->host_no;
1792 qd.bus = 0;
1793 qd.id = CONTAINER_TO_ID(qd.cnum);
1794 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1795 }
1796 else return -EINVAL;
1797
1798 qd.valid = fsa_dev_ptr[qd.cnum].valid;
1799 qd.locked = fsa_dev_ptr[qd.cnum].locked;
1800 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1801
1802 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1803 qd.unmapped = 1;
1804 else
1805 qd.unmapped = 0;
1806
1807 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1808 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1809
1810 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1811 return -EFAULT;
1812 return 0;
1813}
1814
1815static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1816{
1817 struct aac_delete_disk dd;
1818 struct fsa_dev_info *fsa_dev_ptr;
1819
1820 fsa_dev_ptr = dev->fsa_dev;
1821
1822 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1823 return -EFAULT;
1824
1825 if (dd.cnum >= dev->maximum_num_containers)
1826 return -EINVAL;
1827 /*
1828 * Mark this container as being deleted.
1829 */
1830 fsa_dev_ptr[dd.cnum].deleted = 1;
1831 /*
1832 * Mark the container as no longer valid
1833 */
1834 fsa_dev_ptr[dd.cnum].valid = 0;
1835 return 0;
1836}
1837
1838static int delete_disk(struct aac_dev *dev, void __user *arg)
1839{
1840 struct aac_delete_disk dd;
1841 struct fsa_dev_info *fsa_dev_ptr;
1842
1843 fsa_dev_ptr = dev->fsa_dev;
1844
1845 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1846 return -EFAULT;
1847
1848 if (dd.cnum >= dev->maximum_num_containers)
1849 return -EINVAL;
1850 /*
1851 * If the container is locked, it can not be deleted by the API.
1852 */
1853 if (fsa_dev_ptr[dd.cnum].locked)
1854 return -EBUSY;
1855 else {
1856 /*
1857 * Mark the container as no longer being valid.
1858 */
1859 fsa_dev_ptr[dd.cnum].valid = 0;
1860 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1861 return 0;
1862 }
1863}
1864
1865int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1866{
1867 switch (cmd) {
1868 case FSACTL_QUERY_DISK:
1869 return query_disk(dev, arg);
1870 case FSACTL_DELETE_DISK:
1871 return delete_disk(dev, arg);
1872 case FSACTL_FORCE_DELETE_DISK:
1873 return force_delete_disk(dev, arg);
1874 case FSACTL_GET_CONTAINERS:
1875 return aac_get_containers(dev);
1876 default:
1877 return -ENOTTY;
1878 }
1879}
1880
1881/**
1882 *
1883 * aac_srb_callback
1884 * @context: the context set in the fib - here it is scsi cmd
1885 * @fibptr: pointer to the fib
1886 *
1887 * Handles the completion of a scsi command to a non dasd device
1888 *
1889 */
1890
1891static void aac_srb_callback(void *context, struct fib * fibptr)
1892{
1893 struct aac_dev *dev;
1894 struct aac_srb_reply *srbreply;
1895 struct scsi_cmnd *scsicmd;
1896
1897 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001898 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1900
1901 if (fibptr == NULL)
1902 BUG();
1903
1904 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1905
1906 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
1907 /*
1908 * Calculate resid for sg
1909 */
1910
1911 scsicmd->resid = scsicmd->request_bufflen -
1912 le32_to_cpu(srbreply->data_xfer_length);
1913
1914 if(scsicmd->use_sg)
1915 pci_unmap_sg(dev->pdev,
1916 (struct scatterlist *)scsicmd->buffer,
1917 scsicmd->use_sg,
1918 scsicmd->sc_data_direction);
1919 else if(scsicmd->request_bufflen)
1920 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1921 scsicmd->sc_data_direction);
1922
1923 /*
1924 * First check the fib status
1925 */
1926
1927 if (le32_to_cpu(srbreply->status) != ST_OK){
1928 int len;
1929 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1930 len = (le32_to_cpu(srbreply->sense_data_size) >
1931 sizeof(scsicmd->sense_buffer)) ?
1932 sizeof(scsicmd->sense_buffer) :
1933 le32_to_cpu(srbreply->sense_data_size);
1934 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1935 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1936 }
1937
1938 /*
1939 * Next check the srb status
1940 */
1941 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1942 case SRB_STATUS_ERROR_RECOVERY:
1943 case SRB_STATUS_PENDING:
1944 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08001945 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
1947 case SRB_STATUS_DATA_OVERRUN:
1948 switch(scsicmd->cmnd[0]){
1949 case READ_6:
1950 case WRITE_6:
1951 case READ_10:
1952 case WRITE_10:
1953 case READ_12:
1954 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001955 case READ_16:
1956 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1958 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1959 } else {
1960 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1961 }
1962 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1963 break;
1964 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08001965 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 break;
1967 }
1968 default:
1969 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1970 break;
1971 }
1972 break;
1973 case SRB_STATUS_ABORTED:
1974 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1975 break;
1976 case SRB_STATUS_ABORT_FAILED:
1977 // Not sure about this one - but assuming the hba was trying to abort for some reason
1978 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1979 break;
1980 case SRB_STATUS_PARITY_ERROR:
1981 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1982 break;
1983 case SRB_STATUS_NO_DEVICE:
1984 case SRB_STATUS_INVALID_PATH_ID:
1985 case SRB_STATUS_INVALID_TARGET_ID:
1986 case SRB_STATUS_INVALID_LUN:
1987 case SRB_STATUS_SELECTION_TIMEOUT:
1988 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1989 break;
1990
1991 case SRB_STATUS_COMMAND_TIMEOUT:
1992 case SRB_STATUS_TIMEOUT:
1993 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
1994 break;
1995
1996 case SRB_STATUS_BUSY:
1997 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1998 break;
1999
2000 case SRB_STATUS_BUS_RESET:
2001 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2002 break;
2003
2004 case SRB_STATUS_MESSAGE_REJECTED:
2005 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2006 break;
2007 case SRB_STATUS_REQUEST_FLUSHED:
2008 case SRB_STATUS_ERROR:
2009 case SRB_STATUS_INVALID_REQUEST:
2010 case SRB_STATUS_REQUEST_SENSE_FAILED:
2011 case SRB_STATUS_NO_HBA:
2012 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2013 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2014 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2015 case SRB_STATUS_DELAYED_RETRY:
2016 case SRB_STATUS_BAD_FUNCTION:
2017 case SRB_STATUS_NOT_STARTED:
2018 case SRB_STATUS_NOT_IN_USE:
2019 case SRB_STATUS_FORCE_ABORT:
2020 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2021 default:
2022#ifdef AAC_DETAILED_STATUS_INFO
2023 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2024 le32_to_cpu(srbreply->srb_status) & 0x3F,
2025 aac_get_status_string(
2026 le32_to_cpu(srbreply->srb_status) & 0x3F),
2027 scsicmd->cmnd[0],
2028 le32_to_cpu(srbreply->scsi_status));
2029#endif
2030 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2031 break;
2032 }
2033 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2034 int len;
2035 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2036 len = (le32_to_cpu(srbreply->sense_data_size) >
2037 sizeof(scsicmd->sense_buffer)) ?
2038 sizeof(scsicmd->sense_buffer) :
2039 le32_to_cpu(srbreply->sense_data_size);
2040#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002041 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2042 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043#endif
2044 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2045
2046 }
2047 /*
2048 * OR in the scsi status (already shifted up a bit)
2049 */
2050 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2051
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002052 aac_fib_complete(fibptr);
2053 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002054 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057/**
2058 *
2059 * aac_send_scb_fib
2060 * @scsicmd: the scsi command block
2061 *
2062 * This routine will form a FIB and fill in the aac_srb from the
2063 * scsicmd passed in.
2064 */
2065
2066static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2067{
2068 struct fib* cmd_fibcontext;
2069 struct aac_dev* dev;
2070 int status;
2071 struct aac_srb *srbcmd;
2072 u16 fibsize;
2073 u32 flag;
2074 u32 timeout;
2075
Mark Haverkamp84971732005-06-20 11:55:24 -07002076 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002077 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002078 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 scsicmd->result = DID_NO_CONNECT << 16;
2080 scsicmd->scsi_done(scsicmd);
2081 return 0;
2082 }
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 switch(scsicmd->sc_data_direction){
2085 case DMA_TO_DEVICE:
2086 flag = SRB_DataOut;
2087 break;
2088 case DMA_BIDIRECTIONAL:
2089 flag = SRB_DataIn | SRB_DataOut;
2090 break;
2091 case DMA_FROM_DEVICE:
2092 flag = SRB_DataIn;
2093 break;
2094 case DMA_NONE:
2095 default: /* shuts up some versions of gcc */
2096 flag = SRB_NoDataXfer;
2097 break;
2098 }
2099
2100
2101 /*
2102 * Allocate and initialize a Fib then setup a BlockWrite command
2103 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002104 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return -1;
2106 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002107 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
2110 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
Mark Haverkampe5718772006-03-27 09:43:25 -08002111 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(scsicmd)));
2112 srbcmd->id = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
2114 srbcmd->flags = cpu_to_le32(flag);
James Bottomley5262d082005-08-05 16:31:35 -05002115 timeout = scsicmd->timeout_per_command/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if(timeout == 0){
2117 timeout = 1;
2118 }
2119 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
2120 srbcmd->retry_limit = 0; /* Obsolete parameter */
2121 srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
2122
2123 if( dev->dac_support == 1 ) {
2124 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
2125 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2126
2127 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2128 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2129 /*
2130 * Build Scatter/Gather list
2131 */
2132 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
2133 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
2134 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002135 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 sizeof(struct aac_fibhdr)));
2137
2138 /*
2139 * Now send the Fib to the adapter
2140 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002141 status = aac_fib_send(ScsiPortCommand64, cmd_fibcontext,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 fibsize, FsaNormal, 0, 1,
2143 (fib_callback) aac_srb_callback,
2144 (void *) scsicmd);
2145 } else {
2146 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
2147 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2148
2149 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2150 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2151 /*
2152 * Build Scatter/Gather list
2153 */
2154 fibsize = sizeof (struct aac_srb) +
2155 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
2156 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002157 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 sizeof(struct aac_fibhdr)));
2159
2160 /*
2161 * Now send the Fib to the adapter
2162 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002163 status = aac_fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 (fib_callback) aac_srb_callback, (void *) scsicmd);
2165 }
2166 /*
2167 * Check that the command queued to the controller
2168 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002169 if (status == -EINPROGRESS) {
2170 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172 }
2173
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002174 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2175 aac_fib_complete(cmd_fibcontext);
2176 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 return -1;
2179}
2180
2181static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2182{
2183 struct aac_dev *dev;
2184 unsigned long byte_count = 0;
2185
2186 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2187 // Get rid of old data
2188 psg->count = 0;
2189 psg->sg[0].addr = 0;
2190 psg->sg[0].count = 0;
2191 if (scsicmd->use_sg) {
2192 struct scatterlist *sg;
2193 int i;
2194 int sg_count;
2195 sg = (struct scatterlist *) scsicmd->request_buffer;
2196
2197 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2198 scsicmd->sc_data_direction);
2199 psg->count = cpu_to_le32(sg_count);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 for (i = 0; i < sg_count; i++) {
2202 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2203 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2204 byte_count += sg_dma_len(sg);
2205 sg++;
2206 }
2207 /* hba wants the size to be exact */
2208 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002209 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2210 (byte_count - scsicmd->request_bufflen);
2211 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 byte_count = scsicmd->request_bufflen;
2213 }
2214 /* Check for command underflow */
2215 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2216 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2217 byte_count, scsicmd->underflow);
2218 }
2219 }
2220 else if(scsicmd->request_bufflen) {
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002221 u32 addr;
2222 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 scsicmd->request_buffer,
2224 scsicmd->request_bufflen,
2225 scsicmd->sc_data_direction);
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002226 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 psg->count = cpu_to_le32(1);
2228 psg->sg[0].addr = cpu_to_le32(addr);
2229 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 byte_count = scsicmd->request_bufflen;
2231 }
2232 return byte_count;
2233}
2234
2235
2236static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2237{
2238 struct aac_dev *dev;
2239 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002240 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2243 // Get rid of old data
2244 psg->count = 0;
2245 psg->sg[0].addr[0] = 0;
2246 psg->sg[0].addr[1] = 0;
2247 psg->sg[0].count = 0;
2248 if (scsicmd->use_sg) {
2249 struct scatterlist *sg;
2250 int i;
2251 int sg_count;
2252 sg = (struct scatterlist *) scsicmd->request_buffer;
2253
2254 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2255 scsicmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 for (i = 0; i < sg_count; i++) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002258 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002259 addr = sg_dma_address(sg);
2260 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2261 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002262 psg->sg[i].count = cpu_to_le32(count);
2263 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 sg++;
2265 }
Mark Haverkamp1241f352006-03-27 09:44:23 -08002266 psg->count = cpu_to_le32(sg_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* hba wants the size to be exact */
2268 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002269 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2270 (byte_count - scsicmd->request_bufflen);
2271 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 byte_count = scsicmd->request_bufflen;
2273 }
2274 /* Check for command underflow */
2275 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2276 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2277 byte_count, scsicmd->underflow);
2278 }
2279 }
2280 else if(scsicmd->request_bufflen) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002281 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 scsicmd->request_buffer,
2283 scsicmd->request_bufflen,
2284 scsicmd->sc_data_direction);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002285 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 psg->count = cpu_to_le32(1);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002287 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2288 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 byte_count = scsicmd->request_bufflen;
2291 }
2292 return byte_count;
2293}
2294
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002295static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2296{
2297 struct Scsi_Host *host = scsicmd->device->host;
2298 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2299 unsigned long byte_count = 0;
2300
2301 // Get rid of old data
2302 psg->count = 0;
2303 psg->sg[0].next = 0;
2304 psg->sg[0].prev = 0;
2305 psg->sg[0].addr[0] = 0;
2306 psg->sg[0].addr[1] = 0;
2307 psg->sg[0].count = 0;
2308 psg->sg[0].flags = 0;
2309 if (scsicmd->use_sg) {
2310 struct scatterlist *sg;
2311 int i;
2312 int sg_count;
2313 sg = (struct scatterlist *) scsicmd->request_buffer;
2314
2315 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2316 scsicmd->sc_data_direction);
2317
2318 for (i = 0; i < sg_count; i++) {
2319 int count = sg_dma_len(sg);
2320 u64 addr = sg_dma_address(sg);
2321 psg->sg[i].next = 0;
2322 psg->sg[i].prev = 0;
2323 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2324 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2325 psg->sg[i].count = cpu_to_le32(count);
2326 psg->sg[i].flags = 0;
2327 byte_count += count;
2328 sg++;
2329 }
2330 psg->count = cpu_to_le32(sg_count);
2331 /* hba wants the size to be exact */
2332 if(byte_count > scsicmd->request_bufflen){
2333 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2334 (byte_count - scsicmd->request_bufflen);
2335 psg->sg[i-1].count = cpu_to_le32(temp);
2336 byte_count = scsicmd->request_bufflen;
2337 }
2338 /* Check for command underflow */
2339 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2340 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2341 byte_count, scsicmd->underflow);
2342 }
2343 }
2344 else if(scsicmd->request_bufflen) {
2345 int count;
2346 u64 addr;
2347 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2348 scsicmd->request_buffer,
2349 scsicmd->request_bufflen,
2350 scsicmd->sc_data_direction);
2351 addr = scsicmd->SCp.dma_handle;
2352 count = scsicmd->request_bufflen;
2353 psg->count = cpu_to_le32(1);
2354 psg->sg[0].next = 0;
2355 psg->sg[0].prev = 0;
2356 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2357 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2358 psg->sg[0].count = cpu_to_le32(count);
2359 psg->sg[0].flags = 0;
2360 byte_count = scsicmd->request_bufflen;
2361 }
2362 return byte_count;
2363}
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365#ifdef AAC_DETAILED_STATUS_INFO
2366
2367struct aac_srb_status_info {
2368 u32 status;
2369 char *str;
2370};
2371
2372
2373static struct aac_srb_status_info srb_status_info[] = {
2374 { SRB_STATUS_PENDING, "Pending Status"},
2375 { SRB_STATUS_SUCCESS, "Success"},
2376 { SRB_STATUS_ABORTED, "Aborted Command"},
2377 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
2378 { SRB_STATUS_ERROR, "Error Event"},
2379 { SRB_STATUS_BUSY, "Device Busy"},
2380 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2381 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2382 { SRB_STATUS_NO_DEVICE, "No Device"},
2383 { SRB_STATUS_TIMEOUT, "Timeout"},
2384 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2385 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2386 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2387 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2388 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2389 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2390 { SRB_STATUS_NO_HBA, "No HBA"},
2391 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2392 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2393 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2394 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2395 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2396 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
2397 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
2398 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2399 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2400 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2401 { SRB_STATUS_NOT_STARTED, "Not Started"},
2402 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2403 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2404 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2405 { 0xff, "Unknown Error"}
2406};
2407
2408char *aac_get_status_string(u32 status)
2409{
2410 int i;
2411
2412 for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
2413 if(srb_status_info[i].status == status){
2414 return srb_status_info[i].str;
2415 }
2416 }
2417
2418 return "Bad Status Code";
2419}
2420
2421#endif