blob: 6c4319c98feec6f262c72bba2233450bcbb4311a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
Mark Haverkamp03d44332007-03-15 10:27:45 -07008 * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pci.h>
30#include <linux/spinlock.h>
31#include <linux/slab.h>
32#include <linux/completion.h>
33#include <linux/blkdev.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080034#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/semaphore.h>
36#include <asm/uaccess.h>
37
38#include <scsi/scsi.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42
43#include "aacraid.h"
44
45/* values for inqd_pdt: Peripheral device type in plain English */
46#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
47#define INQD_PDT_PROC 0x03 /* Processor device */
48#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
49#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
50#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
51#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
52
53#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
54#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Sense codes
58 */
59
60#define SENCODE_NO_SENSE 0x00
61#define SENCODE_END_OF_DATA 0x00
62#define SENCODE_BECOMING_READY 0x04
63#define SENCODE_INIT_CMD_REQUIRED 0x04
64#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
65#define SENCODE_INVALID_COMMAND 0x20
66#define SENCODE_LBA_OUT_OF_RANGE 0x21
67#define SENCODE_INVALID_CDB_FIELD 0x24
68#define SENCODE_LUN_NOT_SUPPORTED 0x25
69#define SENCODE_INVALID_PARAM_FIELD 0x26
70#define SENCODE_PARAM_NOT_SUPPORTED 0x26
71#define SENCODE_PARAM_VALUE_INVALID 0x26
72#define SENCODE_RESET_OCCURRED 0x29
73#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
74#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
75#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
76#define SENCODE_DIAGNOSTIC_FAILURE 0x40
77#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
78#define SENCODE_INVALID_MESSAGE_ERROR 0x49
79#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
80#define SENCODE_OVERLAPPED_COMMAND 0x4E
81
82/*
83 * Additional sense codes
84 */
85
86#define ASENCODE_NO_SENSE 0x00
87#define ASENCODE_END_OF_DATA 0x05
88#define ASENCODE_BECOMING_READY 0x01
89#define ASENCODE_INIT_CMD_REQUIRED 0x02
90#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
91#define ASENCODE_INVALID_COMMAND 0x00
92#define ASENCODE_LBA_OUT_OF_RANGE 0x00
93#define ASENCODE_INVALID_CDB_FIELD 0x00
94#define ASENCODE_LUN_NOT_SUPPORTED 0x00
95#define ASENCODE_INVALID_PARAM_FIELD 0x00
96#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
97#define ASENCODE_PARAM_VALUE_INVALID 0x02
98#define ASENCODE_RESET_OCCURRED 0x00
99#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
100#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
101#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
102#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
103#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
104#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
105#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
106#define ASENCODE_OVERLAPPED_COMMAND 0x00
107
108#define BYTE0(x) (unsigned char)(x)
109#define BYTE1(x) (unsigned char)((x) >> 8)
110#define BYTE2(x) (unsigned char)((x) >> 16)
111#define BYTE3(x) (unsigned char)((x) >> 24)
112
113/*------------------------------------------------------------------------------
114 * S T R U C T S / T Y P E D E F S
115 *----------------------------------------------------------------------------*/
116/* SCSI inquiry data */
117struct inquiry_data {
118 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
119 u8 inqd_dtq; /* RMB | Device Type Qualifier */
120 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
121 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
122 u8 inqd_len; /* Additional length (n-4) */
123 u8 inqd_pad1[2];/* Reserved - must be zero */
124 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
125 u8 inqd_vid[8]; /* Vendor ID */
126 u8 inqd_pid[16];/* Product ID */
127 u8 inqd_prl[4]; /* Product Revision Level */
128};
129
130/*
131 * M O D U L E G L O B A L S
132 */
133
134static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
135static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700136static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
138#ifdef AAC_DETAILED_STATUS_INFO
139static char *aac_get_status_string(u32 status);
140#endif
141
142/*
143 * Non dasd selection is handled entirely in aachba now
144 */
145
146static int nondasd = -1;
147static int dacmode = -1;
148
149static int commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700150int startup_timeout = 180;
151int aif_timeout = 120;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800153module_param(nondasd, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800155module_param(dacmode, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800157module_param(commit, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700159module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
160MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for adapter to have it's kernel up and\nrunning. This is typically adjusted for large systems that do not have a BIOS.");
161module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
162MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for applications to pick up AIFs before\nderegistering them. This is typically adjusted for heavily burdened systems.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700164int numacb = -1;
165module_param(numacb, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800166MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid values are 512 and down. Default is to use suggestion from Firmware.");
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700167
168int acbsize = -1;
169module_param(acbsize, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800170MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512, 2048, 4096 and 8192. Default is to use suggestion from Firmware.");
Mark Haverkamp653ba582006-09-19 08:59:43 -0700171
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800172int expose_physicals = -1;
Mark Haverkamp653ba582006-09-19 08:59:43 -0700173module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800174MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. -1=protect 0=off, 1=on");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700175
176
177static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
178 struct fib *fibptr) {
179 struct scsi_device *device;
180
181 if (unlikely(!scsicmd || !scsicmd->scsi_done )) {
182 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"))
183;
184 aac_fib_complete(fibptr);
185 aac_fib_free(fibptr);
186 return 0;
187 }
188 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
189 device = scsicmd->device;
190 if (unlikely(!device || !scsi_device_online(device))) {
191 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
192 aac_fib_complete(fibptr);
193 aac_fib_free(fibptr);
194 return 0;
195 }
196 return 1;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/**
200 * aac_get_config_status - check the adapter configuration
201 * @common: adapter to query
202 *
203 * Query config status, and commit the configuration if needed.
204 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700205int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int status = 0;
208 struct fib * fibptr;
209
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800210 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENOMEM;
212
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800213 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 {
215 struct aac_get_config_status *dinfo;
216 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
217
218 dinfo->command = cpu_to_le32(VM_ContainerConfig);
219 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
220 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
221 }
222
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800223 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 fibptr,
225 sizeof (struct aac_get_config_status),
226 FsaNormal,
227 1, 1,
228 NULL, NULL);
229 if (status < 0 ) {
230 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
231 } else {
232 struct aac_get_config_status_resp *reply
233 = (struct aac_get_config_status_resp *) fib_data(fibptr);
234 dprintk((KERN_WARNING
235 "aac_get_config_status: response=%d status=%d action=%d\n",
236 le32_to_cpu(reply->response),
237 le32_to_cpu(reply->status),
238 le32_to_cpu(reply->data.action)));
239 if ((le32_to_cpu(reply->response) != ST_OK) ||
240 (le32_to_cpu(reply->status) != CT_OK) ||
241 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
242 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
243 status = -EINVAL;
244 }
245 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800246 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
248 if (status >= 0) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700249 if ((commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800251 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 dinfo = (struct aac_commit_config *) fib_data(fibptr);
253
254 dinfo->command = cpu_to_le32(VM_ContainerConfig);
255 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
256
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800257 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 fibptr,
259 sizeof (struct aac_commit_config),
260 FsaNormal,
261 1, 1,
262 NULL, NULL);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800263 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 } else if (commit == 0) {
265 printk(KERN_WARNING
266 "aac_get_config_status: Foreign device configurations are being ignored\n");
267 }
268 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800269 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return status;
271}
272
273/**
274 * aac_get_containers - list containers
275 * @common: adapter to probe
276 *
277 * Make a list of all containers on this controller
278 */
279int aac_get_containers(struct aac_dev *dev)
280{
281 struct fsa_dev_info *fsa_dev_ptr;
282 u32 index;
283 int status = 0;
284 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct aac_get_container_count *dinfo;
286 struct aac_get_container_count_resp *dresp;
287 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
288
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800289 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -ENOMEM;
291
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800292 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
294 dinfo->command = cpu_to_le32(VM_ContainerConfig);
295 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
296
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800297 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 fibptr,
299 sizeof (struct aac_get_container_count),
300 FsaNormal,
301 1, 1,
302 NULL, NULL);
303 if (status >= 0) {
304 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
305 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800306 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700308 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
311 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700312 fsa_dev_ptr = kmalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
313 GFP_KERNEL);
314 if (!fsa_dev_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
317
318 dev->fsa_dev = fsa_dev_ptr;
319 dev->maximum_num_containers = maximum_num_containers;
320
Mark Haverkampfe76df42007-03-15 12:55:07 -0700321 for (index = 0; index < dev->maximum_num_containers; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 fsa_dev_ptr[index].devname[0] = '\0';
323
Mark Haverkampfe76df42007-03-15 12:55:07 -0700324 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Mark Haverkampfe76df42007-03-15 12:55:07 -0700326 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
328 break;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * If there are no more containers, then stop asking.
333 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700334 if (++index >= status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return status;
338}
339
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700340static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
341{
342 void *buf;
James Bottomleycab537d2007-05-16 10:06:39 -0400343 int transfer_len;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700344 struct scatterlist *sg = scsicmd->request_buffer;
345
346 if (scsicmd->use_sg) {
347 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
348 transfer_len = min(sg->length, len + offset);
349 } else {
350 buf = scsicmd->request_buffer;
351 transfer_len = min(scsicmd->request_bufflen, len + offset);
352 }
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400353 transfer_len -= offset;
James Bottomleycab537d2007-05-16 10:06:39 -0400354 if (buf && transfer_len > 0)
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400355 memcpy(buf + offset, data, transfer_len);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700356
357 if (scsicmd->use_sg)
358 kunmap_atomic(buf - sg->offset, KM_IRQ0);
359
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static void get_container_name_callback(void *context, struct fib * fibptr)
363{
364 struct aac_get_name_resp * get_name_reply;
365 struct scsi_cmnd * scsicmd;
366
367 scsicmd = (struct scsi_cmnd *) context;
368
Mark Haverkamp03d44332007-03-15 10:27:45 -0700369 if (!aac_valid_context(scsicmd, fibptr))
370 return;
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700373 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
376 /* Failure is irrelevant, using default value instead */
377 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
378 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700379 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
381 while (*sp == ' ')
382 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700383 if (*sp) {
384 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
385 int count = sizeof(d);
386 char *dp = d;
387 do {
388 *dp++ = (*sp) ? *sp++ : ' ';
389 } while (--count > 0);
390 aac_internal_transfer(scsicmd, d,
391 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
396
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800397 aac_fib_complete(fibptr);
398 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700399 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402/**
403 * aac_get_container_name - get container name, none blocking.
404 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700405static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 int status;
408 struct aac_get_name *dinfo;
409 struct fib * cmd_fibcontext;
410 struct aac_dev * dev;
411
412 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
413
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800414 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return -ENOMEM;
416
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800417 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
419
420 dinfo->command = cpu_to_le32(VM_ContainerConfig);
421 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700422 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
424
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800425 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 cmd_fibcontext,
427 sizeof (struct aac_get_name),
428 FsaNormal,
429 0, 1,
430 (fib_callback) get_container_name_callback,
431 (void *) scsicmd);
432
433 /*
434 * Check that the command queued to the controller
435 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800436 if (status == -EINPROGRESS) {
437 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800441 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
442 aac_fib_complete(cmd_fibcontext);
443 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -1;
445}
446
Mark Haverkampfe76df42007-03-15 12:55:07 -0700447static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
448{
449 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
450
451 if (fsa_dev_ptr[scmd_id(scsicmd)].valid)
452 return aac_scsi_cmd(scsicmd);
453
454 scsicmd->result = DID_NO_CONNECT << 16;
455 scsicmd->scsi_done(scsicmd);
456 return 0;
457}
458
459static int _aac_probe_container2(void * context, struct fib * fibptr)
460{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700461 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700462 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700463 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
464
465 if (!aac_valid_context(scsicmd, fibptr))
466 return 0;
467
468 fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700469
470 scsicmd->SCp.Status = 0;
471 if (fsa_dev_ptr) {
472 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
473 fsa_dev_ptr += scmd_id(scsicmd);
474
475 if ((le32_to_cpu(dresp->status) == ST_OK) &&
476 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
477 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
478 fsa_dev_ptr->valid = 1;
479 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
480 fsa_dev_ptr->size
481 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
482 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
483 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
484 }
485 if ((fsa_dev_ptr->valid & 1) == 0)
486 fsa_dev_ptr->valid = 0;
487 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
488 }
489 aac_fib_complete(fibptr);
490 aac_fib_free(fibptr);
491 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
492 scsicmd->SCp.ptr = NULL;
493 return (*callback)(scsicmd);
494}
495
496static int _aac_probe_container1(void * context, struct fib * fibptr)
497{
498 struct scsi_cmnd * scsicmd;
499 struct aac_mount * dresp;
500 struct aac_query_mount *dinfo;
501 int status;
502
503 dresp = (struct aac_mount *) fib_data(fibptr);
504 dresp->mnt[0].capacityhigh = 0;
505 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark4def7fa2007-04-13 08:35:25 -0400506 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700507 return _aac_probe_container2(context, fibptr);
508 scsicmd = (struct scsi_cmnd *) context;
509 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
510
Mark Haverkamp03d44332007-03-15 10:27:45 -0700511 if (!aac_valid_context(scsicmd, fibptr))
512 return 0;
513
Mark Haverkampfe76df42007-03-15 12:55:07 -0700514 aac_fib_init(fibptr);
515
516 dinfo = (struct aac_query_mount *)fib_data(fibptr);
517
518 dinfo->command = cpu_to_le32(VM_NameServe64);
519 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
520 dinfo->type = cpu_to_le32(FT_FILESYS);
521
522 status = aac_fib_send(ContainerCommand,
523 fibptr,
524 sizeof(struct aac_query_mount),
525 FsaNormal,
526 0, 1,
527 (fib_callback) _aac_probe_container2,
528 (void *) scsicmd);
529 /*
530 * Check that the command queued to the controller
531 */
532 if (status == -EINPROGRESS) {
533 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
534 return 0;
535 }
536 if (status < 0) {
537 /* Inherit results from VM_NameServe, if any */
538 dresp->status = cpu_to_le32(ST_OK);
539 return _aac_probe_container2(context, fibptr);
540 }
541 return 0;
542}
543
544static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
545{
546 struct fib * fibptr;
547 int status = -ENOMEM;
548
549 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
550 struct aac_query_mount *dinfo;
551
552 aac_fib_init(fibptr);
553
554 dinfo = (struct aac_query_mount *)fib_data(fibptr);
555
556 dinfo->command = cpu_to_le32(VM_NameServe);
557 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
558 dinfo->type = cpu_to_le32(FT_FILESYS);
559 scsicmd->SCp.ptr = (char *)callback;
560
561 status = aac_fib_send(ContainerCommand,
562 fibptr,
563 sizeof(struct aac_query_mount),
564 FsaNormal,
565 0, 1,
566 (fib_callback) _aac_probe_container1,
567 (void *) scsicmd);
568 /*
569 * Check that the command queued to the controller
570 */
571 if (status == -EINPROGRESS) {
572 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
573 return 0;
574 }
575 if (status < 0) {
576 scsicmd->SCp.ptr = NULL;
577 aac_fib_complete(fibptr);
578 aac_fib_free(fibptr);
579 }
580 }
581 if (status < 0) {
582 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
583 if (fsa_dev_ptr) {
584 fsa_dev_ptr += scmd_id(scsicmd);
585 if ((fsa_dev_ptr->valid & 1) == 0) {
586 fsa_dev_ptr->valid = 0;
587 return (*callback)(scsicmd);
588 }
589 }
590 }
591 return status;
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800595 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * @dev: device to query
597 * @cid: container identifier
598 *
599 * Queries the controller about the given volume. The volume information
600 * is updated in the struct fsa_dev_info structure rather than returned.
601 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700602static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
603{
604 scsicmd->device = NULL;
605 return 0;
606}
607
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800608int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700610 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
611 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Mark Haverkampfe76df42007-03-15 12:55:07 -0700614 if (!scsicmd || !scsidev) {
615 kfree(scsicmd);
616 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700617 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700619 scsicmd->list.next = NULL;
620 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))_aac_probe_container1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Mark Haverkampfe76df42007-03-15 12:55:07 -0700622 scsicmd->device = scsidev;
623 scsidev->sdev_state = 0;
624 scsidev->id = cid;
625 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Mark Haverkampfe76df42007-03-15 12:55:07 -0700627 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
628 while (scsicmd->device == scsidev)
629 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400630 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700631 status = scsicmd->SCp.Status;
632 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return status;
634}
635
636/* Local Structure to set SCSI inquiry data strings */
637struct scsi_inq {
638 char vid[8]; /* Vendor ID */
639 char pid[16]; /* Product ID */
640 char prl[4]; /* Product Revision Level */
641};
642
643/**
644 * InqStrCopy - string merge
645 * @a: string to copy from
646 * @b: string to copy to
647 *
648 * Copy a String from one location to another
649 * without copying \0
650 */
651
652static void inqstrcpy(char *a, char *b)
653{
654
655 while(*a != (char)0)
656 *b++ = *a++;
657}
658
659static char *container_types[] = {
660 "None",
661 "Volume",
662 "Mirror",
663 "Stripe",
664 "RAID5",
665 "SSRW",
666 "SSRO",
667 "Morph",
668 "Legacy",
669 "RAID4",
670 "RAID10",
671 "RAID00",
672 "V-MIRRORS",
673 "PSEUDO R4",
674 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700675 "RAID5D",
676 "RAID5D0",
677 "RAID1E",
678 "RAID6",
679 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 "Unknown"
681};
682
683
684
685/* Function: setinqstr
686 *
687 * Arguments: [1] pointer to void [1] int
688 *
689 * Purpose: Sets SCSI inquiry data strings for vendor, product
690 * and revision level. Allows strings to be set in platform dependant
691 * files instead of in OS dependant driver source.
692 */
693
Mark Haverkamp794d0602005-10-24 10:51:53 -0700694static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700699 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Mark Haverkamp794d0602005-10-24 10:51:53 -0700701 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
702 char * cp = dev->supplement_adapter_info.AdapterTypeText;
703 int c = sizeof(str->vid);
704 while (*cp && *cp != ' ' && --c)
705 ++cp;
706 c = *cp;
707 *cp = '\0';
708 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
709 str->vid);
710 *cp = c;
711 while (*cp && *cp != ' ')
712 ++cp;
713 while (*cp == ' ')
714 ++cp;
715 /* last six chars reserved for vol type */
716 c = 0;
717 if (strlen(cp) > sizeof(str->pid)) {
718 c = cp[sizeof(str->pid)];
719 cp[sizeof(str->pid)] = '\0';
720 }
721 inqstrcpy (cp, str->pid);
722 if (c)
723 cp[sizeof(str->pid)] = c;
724 } else {
725 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700726
727 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700728 /* last six chars reserved for vol type */
729 inqstrcpy (mp->model, str->pid);
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Tobias Klauser6391a112006-06-08 22:23:48 -0700732 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 char *findit = str->pid;
734
735 for ( ; *findit != ' '; findit++); /* walk till we find a space */
736 /* RAID is superfluous in the context of a RAID device */
737 if (memcmp(findit-4, "RAID", 4) == 0)
738 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700739 if (((findit - str->pid) + strlen(container_types[tindex]))
740 < (sizeof(str->pid) + sizeof(str->prl)))
741 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 inqstrcpy ("V1.0", str->prl);
744}
745
Adrian Bunk 48338692005-04-25 19:45:58 -0700746static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
747 u8 a_sense_code, u8 incorrect_length,
748 u8 bit_pointer, u16 field_pointer,
749 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
752 sense_buf[1] = 0; /* Segment number, always zero */
753
754 if (incorrect_length) {
755 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
756 sense_buf[3] = BYTE3(residue);
757 sense_buf[4] = BYTE2(residue);
758 sense_buf[5] = BYTE1(residue);
759 sense_buf[6] = BYTE0(residue);
760 } else
761 sense_buf[2] = sense_key; /* Sense key */
762
763 if (sense_key == ILLEGAL_REQUEST)
764 sense_buf[7] = 10; /* Additional sense length */
765 else
766 sense_buf[7] = 6; /* Additional sense length */
767
768 sense_buf[12] = sense_code; /* Additional sense code */
769 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
770 if (sense_key == ILLEGAL_REQUEST) {
771 sense_buf[15] = 0;
772
773 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
774 sense_buf[15] = 0x80;/* Std sense key specific field */
775 /* Illegal parameter is in the parameter block */
776
777 if (sense_code == SENCODE_INVALID_CDB_FIELD)
778 sense_buf[15] = 0xc0;/* Std sense key specific field */
779 /* Illegal parameter is in the CDB block */
780 sense_buf[15] |= bit_pointer;
781 sense_buf[16] = field_pointer >> 8; /* MSB */
782 sense_buf[17] = field_pointer; /* LSB */
783 }
784}
785
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800786static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
787{
788 if (lba & 0xffffffff00000000LL) {
789 int cid = scmd_id(cmd);
790 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
791 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
792 SAM_STAT_CHECK_CONDITION;
793 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
794 HARDWARE_ERROR,
795 SENCODE_INTERNAL_TARGET_FAILURE,
796 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
797 0, 0);
798 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
799 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(cmd->sense_buffer))
800 ? sizeof(cmd->sense_buffer)
801 : sizeof(dev->fsa_dev[cid].sense_data));
802 cmd->scsi_done(cmd);
803 return 1;
804 }
805 return 0;
806}
807
808static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
809{
810 return 0;
811}
812
813static void io_callback(void *context, struct fib * fibptr);
814
815static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
816{
817 u16 fibsize;
818 struct aac_raw_io *readcmd;
819 aac_fib_init(fib);
820 readcmd = (struct aac_raw_io *) fib_data(fib);
821 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
822 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
823 readcmd->count = cpu_to_le32(count<<9);
824 readcmd->cid = cpu_to_le16(scmd_id(cmd));
825 readcmd->flags = cpu_to_le16(1);
826 readcmd->bpTotal = 0;
827 readcmd->bpComplete = 0;
828
829 aac_build_sgraw(cmd, &readcmd->sg);
830 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
831 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
832 /*
833 * Now send the Fib to the adapter
834 */
835 return aac_fib_send(ContainerRawIo,
836 fib,
837 fibsize,
838 FsaNormal,
839 0, 1,
840 (fib_callback) io_callback,
841 (void *) cmd);
842}
843
844static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
845{
846 u16 fibsize;
847 struct aac_read64 *readcmd;
848 aac_fib_init(fib);
849 readcmd = (struct aac_read64 *) fib_data(fib);
850 readcmd->command = cpu_to_le32(VM_CtHostRead64);
851 readcmd->cid = cpu_to_le16(scmd_id(cmd));
852 readcmd->sector_count = cpu_to_le16(count);
853 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
854 readcmd->pad = 0;
855 readcmd->flags = 0;
856
857 aac_build_sg64(cmd, &readcmd->sg);
858 fibsize = sizeof(struct aac_read64) +
859 ((le32_to_cpu(readcmd->sg.count) - 1) *
860 sizeof (struct sgentry64));
861 BUG_ON (fibsize > (fib->dev->max_fib_size -
862 sizeof(struct aac_fibhdr)));
863 /*
864 * Now send the Fib to the adapter
865 */
866 return aac_fib_send(ContainerCommand64,
867 fib,
868 fibsize,
869 FsaNormal,
870 0, 1,
871 (fib_callback) io_callback,
872 (void *) cmd);
873}
874
875static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
876{
877 u16 fibsize;
878 struct aac_read *readcmd;
879 aac_fib_init(fib);
880 readcmd = (struct aac_read *) fib_data(fib);
881 readcmd->command = cpu_to_le32(VM_CtBlockRead);
882 readcmd->cid = cpu_to_le16(scmd_id(cmd));
883 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
884 readcmd->count = cpu_to_le32(count * 512);
885
886 aac_build_sg(cmd, &readcmd->sg);
887 fibsize = sizeof(struct aac_read) +
888 ((le32_to_cpu(readcmd->sg.count) - 1) *
889 sizeof (struct sgentry));
890 BUG_ON (fibsize > (fib->dev->max_fib_size -
891 sizeof(struct aac_fibhdr)));
892 /*
893 * Now send the Fib to the adapter
894 */
895 return aac_fib_send(ContainerCommand,
896 fib,
897 fibsize,
898 FsaNormal,
899 0, 1,
900 (fib_callback) io_callback,
901 (void *) cmd);
902}
903
904static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
905{
906 u16 fibsize;
907 struct aac_raw_io *writecmd;
908 aac_fib_init(fib);
909 writecmd = (struct aac_raw_io *) fib_data(fib);
910 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
911 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
912 writecmd->count = cpu_to_le32(count<<9);
913 writecmd->cid = cpu_to_le16(scmd_id(cmd));
914 writecmd->flags = 0;
915 writecmd->bpTotal = 0;
916 writecmd->bpComplete = 0;
917
918 aac_build_sgraw(cmd, &writecmd->sg);
919 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
920 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
921 /*
922 * Now send the Fib to the adapter
923 */
924 return aac_fib_send(ContainerRawIo,
925 fib,
926 fibsize,
927 FsaNormal,
928 0, 1,
929 (fib_callback) io_callback,
930 (void *) cmd);
931}
932
933static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
934{
935 u16 fibsize;
936 struct aac_write64 *writecmd;
937 aac_fib_init(fib);
938 writecmd = (struct aac_write64 *) fib_data(fib);
939 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
940 writecmd->cid = cpu_to_le16(scmd_id(cmd));
941 writecmd->sector_count = cpu_to_le16(count);
942 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
943 writecmd->pad = 0;
944 writecmd->flags = 0;
945
946 aac_build_sg64(cmd, &writecmd->sg);
947 fibsize = sizeof(struct aac_write64) +
948 ((le32_to_cpu(writecmd->sg.count) - 1) *
949 sizeof (struct sgentry64));
950 BUG_ON (fibsize > (fib->dev->max_fib_size -
951 sizeof(struct aac_fibhdr)));
952 /*
953 * Now send the Fib to the adapter
954 */
955 return aac_fib_send(ContainerCommand64,
956 fib,
957 fibsize,
958 FsaNormal,
959 0, 1,
960 (fib_callback) io_callback,
961 (void *) cmd);
962}
963
964static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
965{
966 u16 fibsize;
967 struct aac_write *writecmd;
968 aac_fib_init(fib);
969 writecmd = (struct aac_write *) fib_data(fib);
970 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
971 writecmd->cid = cpu_to_le16(scmd_id(cmd));
972 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
973 writecmd->count = cpu_to_le32(count * 512);
974 writecmd->sg.count = cpu_to_le32(1);
975 /* ->stable is not used - it did mean which type of write */
976
977 aac_build_sg(cmd, &writecmd->sg);
978 fibsize = sizeof(struct aac_write) +
979 ((le32_to_cpu(writecmd->sg.count) - 1) *
980 sizeof (struct sgentry));
981 BUG_ON (fibsize > (fib->dev->max_fib_size -
982 sizeof(struct aac_fibhdr)));
983 /*
984 * Now send the Fib to the adapter
985 */
986 return aac_fib_send(ContainerCommand,
987 fib,
988 fibsize,
989 FsaNormal,
990 0, 1,
991 (fib_callback) io_callback,
992 (void *) cmd);
993}
994
995static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
996{
997 struct aac_srb * srbcmd;
998 u32 flag;
999 u32 timeout;
1000
1001 aac_fib_init(fib);
1002 switch(cmd->sc_data_direction){
1003 case DMA_TO_DEVICE:
1004 flag = SRB_DataOut;
1005 break;
1006 case DMA_BIDIRECTIONAL:
1007 flag = SRB_DataIn | SRB_DataOut;
1008 break;
1009 case DMA_FROM_DEVICE:
1010 flag = SRB_DataIn;
1011 break;
1012 case DMA_NONE:
1013 default: /* shuts up some versions of gcc */
1014 flag = SRB_NoDataXfer;
1015 break;
1016 }
1017
1018 srbcmd = (struct aac_srb*) fib_data(fib);
1019 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1020 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1021 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1022 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1023 srbcmd->flags = cpu_to_le32(flag);
1024 timeout = cmd->timeout_per_command/HZ;
1025 if (timeout == 0)
1026 timeout = 1;
1027 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1028 srbcmd->retry_limit = 0; /* Obsolete parameter */
1029 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1030 return srbcmd;
1031}
1032
1033static void aac_srb_callback(void *context, struct fib * fibptr);
1034
1035static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1036{
1037 u16 fibsize;
1038 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1039
1040 aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
1041 srbcmd->count = cpu_to_le32(cmd->request_bufflen);
1042
1043 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1044 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1045 /*
1046 * Build Scatter/Gather list
1047 */
1048 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1049 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1050 sizeof (struct sgentry64));
1051 BUG_ON (fibsize > (fib->dev->max_fib_size -
1052 sizeof(struct aac_fibhdr)));
1053
1054 /*
1055 * Now send the Fib to the adapter
1056 */
1057 return aac_fib_send(ScsiPortCommand64, fib,
1058 fibsize, FsaNormal, 0, 1,
1059 (fib_callback) aac_srb_callback,
1060 (void *) cmd);
1061}
1062
1063static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1064{
1065 u16 fibsize;
1066 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1067
1068 aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
1069 srbcmd->count = cpu_to_le32(cmd->request_bufflen);
1070
1071 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1072 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1073 /*
1074 * Build Scatter/Gather list
1075 */
1076 fibsize = sizeof (struct aac_srb) +
1077 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1078 sizeof (struct sgentry));
1079 BUG_ON (fibsize > (fib->dev->max_fib_size -
1080 sizeof(struct aac_fibhdr)));
1081
1082 /*
1083 * Now send the Fib to the adapter
1084 */
1085 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1086 (fib_callback) aac_srb_callback, (void *) cmd);
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089int aac_get_adapter_info(struct aac_dev* dev)
1090{
1091 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int rcode;
1093 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001094 struct aac_adapter_info *info;
1095 struct aac_bus_info *command;
1096 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001097
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001098 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return -ENOMEM;
1100
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001101 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001102 info = (struct aac_adapter_info *) fib_data(fibptr);
1103 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001105 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001106 fibptr,
1107 sizeof(*info),
1108 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001109 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001110 NULL,
1111 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001113 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001114 aac_fib_complete(fibptr);
1115 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001116 return rcode;
1117 }
1118 memcpy(&dev->adapter_info, info, sizeof(*info));
1119
1120 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1121 struct aac_supplement_adapter_info * info;
1122
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001123 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001124
1125 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1126
1127 memset(info,0,sizeof(*info));
1128
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001129 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001130 fibptr,
1131 sizeof(*info),
1132 FsaNormal,
1133 1, 1,
1134 NULL,
1135 NULL);
1136
1137 if (rcode >= 0)
1138 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Mark Haverkamp84971732005-06-20 11:55:24 -07001141
1142 /*
1143 * GetBusInfo
1144 */
1145
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001146 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001147
1148 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1149
1150 memset(bus_info, 0, sizeof(*bus_info));
1151
1152 command = (struct aac_bus_info *)bus_info;
1153
1154 command->Command = cpu_to_le32(VM_Ioctl);
1155 command->ObjType = cpu_to_le32(FT_DRIVE);
1156 command->MethodId = cpu_to_le32(1);
1157 command->CtlCmd = cpu_to_le32(GetBusInfo);
1158
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001159 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001160 fibptr,
1161 sizeof (*bus_info),
1162 FsaNormal,
1163 1, 1,
1164 NULL, NULL);
1165
1166 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1167 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1168 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1169 }
1170
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001171 if (!dev->in_reset) {
1172 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1173 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 dev->name,
1175 dev->id,
1176 tmp>>24,
1177 (tmp>>16)&0xff,
1178 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001179 le32_to_cpu(dev->adapter_info.kernelbuild),
1180 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1181 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001182 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1183 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 dev->name, dev->id,
1185 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1186 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001187 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1188 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 dev->name, dev->id,
1190 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1191 le32_to_cpu(dev->adapter_info.biosbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001192 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
1193 printk(KERN_INFO "%s%d: serial %x\n",
1194 dev->name, dev->id,
1195 le32_to_cpu(dev->adapter_info.serial[0]));
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001196 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1197 printk(KERN_INFO "%s%d: TSID %.*s\n",
1198 dev->name, dev->id,
1199 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1200 dev->supplement_adapter_info.VpdInfo.Tsid);
1201 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 dev->nondasd_support = 0;
1205 dev->raid_scsi_mode = 0;
1206 if(dev->adapter_info.options & AAC_OPT_NONDASD){
1207 dev->nondasd_support = 1;
1208 }
1209
1210 /*
1211 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1212 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1213 * force nondasd support on. If we decide to allow the non-dasd flag
1214 * additional changes changes will have to be made to support
1215 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1216 * changed to support the new dev->raid_scsi_mode flag instead of
1217 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1218 * function aac_detect will have to be modified where it sets up the
1219 * max number of channels based on the aac->nondasd_support flag only.
1220 */
1221 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1222 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1223 dev->nondasd_support = 1;
1224 dev->raid_scsi_mode = 1;
1225 }
1226 if (dev->raid_scsi_mode != 0)
1227 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1228 dev->name, dev->id);
1229
1230 if(nondasd != -1) {
1231 dev->nondasd_support = (nondasd!=0);
1232 }
1233 if(dev->nondasd_support != 0){
1234 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1235 }
1236
1237 dev->dac_support = 0;
1238 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1239 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1240 dev->dac_support = 1;
1241 }
1242
1243 if(dacmode != -1) {
1244 dev->dac_support = (dacmode!=0);
1245 }
1246 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -05001247 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1248 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1250 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -05001251 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1252 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1254 dev->name, dev->id);
1255 dev->dac_support = 0;
1256 } else {
1257 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1258 dev->name, dev->id);
1259 rcode = -ENOMEM;
1260 }
1261 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001262 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001263 * Deal with configuring for the individualized limits of each packet
1264 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001265 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001266 dev->a_ops.adapter_scsi = (dev->dac_support)
1267 ? aac_scsi_64
1268 : aac_scsi_32;
1269 if (dev->raw_io_interface) {
1270 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1271 ? aac_bounds_64
1272 : aac_bounds_32;
1273 dev->a_ops.adapter_read = aac_read_raw_io;
1274 dev->a_ops.adapter_write = aac_write_raw_io;
1275 } else {
1276 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001277 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001278 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001279 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1280 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001281 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001282 dev->a_ops.adapter_read = aac_read_block64;
1283 dev->a_ops.adapter_write = aac_write_block64;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001284 /*
1285 * 38 scatter gather elements
1286 */
1287 dev->scsi_host_ptr->sg_tablesize =
1288 (dev->max_fib_size -
1289 sizeof(struct aac_fibhdr) -
1290 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001291 sizeof(struct sgentry64)) /
1292 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001293 } else {
1294 dev->a_ops.adapter_read = aac_read_block;
1295 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001296 }
1297 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1298 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1299 /*
1300 * Worst case size that could cause sg overflow when
1301 * we break up SG elements that are larger than 64KB.
1302 * Would be nice if we could tell the SCSI layer what
1303 * the maximum SG element size can be. Worst case is
1304 * (sg_tablesize-1) 4KB elements with one 64KB
1305 * element.
1306 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1307 */
1308 dev->scsi_host_ptr->max_sectors =
1309 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1310 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001313 aac_fib_complete(fibptr);
1314 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 return rcode;
1317}
1318
1319
Mark Haverkampe53cb352005-08-03 15:39:09 -07001320static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct aac_dev *dev;
1323 struct aac_read_reply *readreply;
1324 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 u32 cid;
1326
1327 scsicmd = (struct scsi_cmnd *) context;
1328
Mark Haverkamp03d44332007-03-15 10:27:45 -07001329 if (!aac_valid_context(scsicmd, fibptr))
1330 return;
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08001333 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001335 if (nblank(dprintk(x))) {
1336 u64 lba;
1337 switch (scsicmd->cmnd[0]) {
1338 case WRITE_6:
1339 case READ_6:
1340 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1341 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1342 break;
1343 case WRITE_16:
1344 case READ_16:
1345 lba = ((u64)scsicmd->cmnd[2] << 56) |
1346 ((u64)scsicmd->cmnd[3] << 48) |
1347 ((u64)scsicmd->cmnd[4] << 40) |
1348 ((u64)scsicmd->cmnd[5] << 32) |
1349 ((u64)scsicmd->cmnd[6] << 24) |
1350 (scsicmd->cmnd[7] << 16) |
1351 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1352 break;
1353 case WRITE_12:
1354 case READ_12:
1355 lba = ((u64)scsicmd->cmnd[2] << 24) |
1356 (scsicmd->cmnd[3] << 16) |
1357 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1358 break;
1359 default:
1360 lba = ((u64)scsicmd->cmnd[2] << 24) |
1361 (scsicmd->cmnd[3] << 16) |
1362 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1363 break;
1364 }
1365 printk(KERN_DEBUG
1366 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1367 smp_processor_id(), (unsigned long long)lba, jiffies);
1368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001370 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if(scsicmd->use_sg)
1373 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02001374 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 scsicmd->use_sg,
1376 scsicmd->sc_data_direction);
1377 else if(scsicmd->request_bufflen)
1378 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
1379 scsicmd->request_bufflen,
1380 scsicmd->sc_data_direction);
1381 readreply = (struct aac_read_reply *)fib_data(fibptr);
1382 if (le32_to_cpu(readreply->status) == ST_OK)
1383 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1384 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001385#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001386 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001387 le32_to_cpu(readreply->status));
1388#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1390 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1391 HARDWARE_ERROR,
1392 SENCODE_INTERNAL_TARGET_FAILURE,
1393 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1394 0, 0);
1395 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1396 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1397 ? sizeof(scsicmd->sense_buffer)
1398 : sizeof(dev->fsa_dev[cid].sense_data));
1399 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001400 aac_fib_complete(fibptr);
1401 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001403 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001406static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001408 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 u32 count;
1410 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct aac_dev *dev;
1412 struct fib * cmd_fibcontext;
1413
1414 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1415 /*
1416 * Get block address and transfer length
1417 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001418 switch (scsicmd->cmnd[0]) {
1419 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001420 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001422 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1423 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 count = scsicmd->cmnd[4];
1425
1426 if (count == 0)
1427 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001428 break;
1429 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001430 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001431
1432 lba = ((u64)scsicmd->cmnd[2] << 56) |
1433 ((u64)scsicmd->cmnd[3] << 48) |
1434 ((u64)scsicmd->cmnd[4] << 40) |
1435 ((u64)scsicmd->cmnd[5] << 32) |
1436 ((u64)scsicmd->cmnd[6] << 24) |
1437 (scsicmd->cmnd[7] << 16) |
1438 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1439 count = (scsicmd->cmnd[10] << 24) |
1440 (scsicmd->cmnd[11] << 16) |
1441 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1442 break;
1443 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001444 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001445
1446 lba = ((u64)scsicmd->cmnd[2] << 24) |
1447 (scsicmd->cmnd[3] << 16) |
1448 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1449 count = (scsicmd->cmnd[6] << 24) |
1450 (scsicmd->cmnd[7] << 16) |
1451 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1452 break;
1453 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001454 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001456 lba = ((u64)scsicmd->cmnd[2] << 24) |
1457 (scsicmd->cmnd[3] << 16) |
1458 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001462 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001463 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001464 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /*
1467 * Alocate and initialize a Fib
1468 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001469 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -1;
1471 }
1472
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001473 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /*
1476 * Check that the command queued to the controller
1477 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001478 if (status == -EINPROGRESS) {
1479 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001483 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /*
1485 * For some reason, the Fib didn't queue, return QUEUE_FULL
1486 */
1487 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001488 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001489 aac_fib_complete(cmd_fibcontext);
1490 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return 0;
1492}
1493
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001494static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001496 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 u32 count;
1498 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct aac_dev *dev;
1500 struct fib * cmd_fibcontext;
1501
1502 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1503 /*
1504 * Get block address and transfer length
1505 */
1506 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1507 {
1508 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1509 count = scsicmd->cmnd[4];
1510 if (count == 0)
1511 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001512 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001513 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001514
1515 lba = ((u64)scsicmd->cmnd[2] << 56) |
1516 ((u64)scsicmd->cmnd[3] << 48) |
1517 ((u64)scsicmd->cmnd[4] << 40) |
1518 ((u64)scsicmd->cmnd[5] << 32) |
1519 ((u64)scsicmd->cmnd[6] << 24) |
1520 (scsicmd->cmnd[7] << 16) |
1521 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1522 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1523 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1524 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001525 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001526
1527 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1528 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1529 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1530 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001532 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001533 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1535 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001536 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001538 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /*
1541 * Allocate and initialize a Fib then setup a BlockWrite command
1542 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001543 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001545 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001549 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /*
1552 * Check that the command queued to the controller
1553 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001554 if (status == -EINPROGRESS) {
1555 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557 }
1558
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001559 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /*
1561 * For some reason, the Fib didn't queue, return QUEUE_FULL
1562 */
1563 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001564 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001566 aac_fib_complete(cmd_fibcontext);
1567 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return 0;
1569}
1570
1571static void synchronize_callback(void *context, struct fib *fibptr)
1572{
1573 struct aac_synchronize_reply *synchronizereply;
1574 struct scsi_cmnd *cmd;
1575
1576 cmd = context;
1577
Mark Haverkamp03d44332007-03-15 10:27:45 -07001578 if (!aac_valid_context(cmd, fibptr))
1579 return;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1582 smp_processor_id(), jiffies));
1583 BUG_ON(fibptr == NULL);
1584
1585
1586 synchronizereply = fib_data(fibptr);
1587 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1588 cmd->result = DID_OK << 16 |
1589 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1590 else {
1591 struct scsi_device *sdev = cmd->device;
1592 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08001593 u32 cid = sdev_id(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 printk(KERN_WARNING
1595 "synchronize_callback: synchronize failed, status = %d\n",
1596 le32_to_cpu(synchronizereply->status));
1597 cmd->result = DID_OK << 16 |
1598 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1599 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1600 HARDWARE_ERROR,
1601 SENCODE_INTERNAL_TARGET_FAILURE,
1602 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1603 0, 0);
1604 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1605 min(sizeof(dev->fsa_dev[cid].sense_data),
1606 sizeof(cmd->sense_buffer)));
1607 }
1608
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001609 aac_fib_complete(fibptr);
1610 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001611 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001614static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 int status;
1617 struct fib *cmd_fibcontext;
1618 struct aac_synchronize *synchronizecmd;
1619 struct scsi_cmnd *cmd;
1620 struct scsi_device *sdev = scsicmd->device;
1621 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001622 struct aac_dev *aac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 unsigned long flags;
1624
1625 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001626 * Wait for all outstanding queued commands to complete to this
1627 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 */
1629 spin_lock_irqsave(&sdev->list_lock, flags);
1630 list_for_each_entry(cmd, &sdev->cmd_list, list)
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001631 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 ++active;
1633 break;
1634 }
1635
1636 spin_unlock_irqrestore(&sdev->list_lock, flags);
1637
1638 /*
1639 * Yield the processor (requeue for later)
1640 */
1641 if (active)
1642 return SCSI_MLQUEUE_DEVICE_BUSY;
1643
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001644 aac = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001645 if (aac->in_reset)
1646 return SCSI_MLQUEUE_HOST_BUSY;
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001649 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001651 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return SCSI_MLQUEUE_HOST_BUSY;
1653
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001654 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 synchronizecmd = fib_data(cmd_fibcontext);
1657 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1658 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001659 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 synchronizecmd->count =
1661 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1662
1663 /*
1664 * Now send the Fib to the adapter
1665 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001666 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 cmd_fibcontext,
1668 sizeof(struct aac_synchronize),
1669 FsaNormal,
1670 0, 1,
1671 (fib_callback)synchronize_callback,
1672 (void *)scsicmd);
1673
1674 /*
1675 * Check that the command queued to the controller
1676 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001677 if (status == -EINPROGRESS) {
1678 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001683 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1684 aac_fib_complete(cmd_fibcontext);
1685 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return SCSI_MLQUEUE_HOST_BUSY;
1687}
1688
1689/**
1690 * aac_scsi_cmd() - Process SCSI command
1691 * @scsicmd: SCSI command block
1692 *
1693 * Emulate a SCSI command and queue the required request for the
1694 * aacraid firmware.
1695 */
1696
1697int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1698{
1699 u32 cid = 0;
1700 struct Scsi_Host *host = scsicmd->device->host;
1701 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1702 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001704 if (fsa_dev_ptr == NULL)
1705 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /*
1707 * If the bus, id or lun is out of range, return fail
1708 * Test does not apply to ID 16, the pseudo id for the controller
1709 * itself.
1710 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001711 if (scmd_id(scsicmd) != host->this_id) {
Mark Haverkampe5718772006-03-27 09:43:25 -08001712 if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1713 if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1714 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 scsicmd->result = DID_NO_CONNECT << 16;
1716 scsicmd->scsi_done(scsicmd);
1717 return 0;
1718 }
Mark Haverkampe5718772006-03-27 09:43:25 -08001719 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 /*
1722 * If the target container doesn't exist, it may have
1723 * been newly created
1724 */
1725 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1726 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001727 case SERVICE_ACTION_IN:
1728 if (!(dev->raw_io_interface) ||
1729 !(dev->raw_io_64) ||
1730 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 case INQUIRY:
1733 case READ_CAPACITY:
1734 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001735 if (dev->in_reset)
1736 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07001737 return _aac_probe_container(scsicmd,
1738 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 default:
1740 break;
1741 }
1742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 } else { /* check for physical non-dasd devices */
Mark Haverkamp653ba582006-09-19 08:59:43 -07001744 if ((dev->nondasd_support == 1) || expose_physicals) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001745 if (dev->in_reset)
1746 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return aac_send_srb_fib(scsicmd);
1748 } else {
1749 scsicmd->result = DID_NO_CONNECT << 16;
1750 scsicmd->scsi_done(scsicmd);
1751 return 0;
1752 }
1753 }
1754 }
1755 /*
1756 * else Command for the controller itself
1757 */
1758 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1759 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1760 {
1761 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1762 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1763 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1764 ILLEGAL_REQUEST,
1765 SENCODE_INVALID_COMMAND,
1766 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1767 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1768 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1769 ? sizeof(scsicmd->sense_buffer)
1770 : sizeof(dev->fsa_dev[cid].sense_data));
1771 scsicmd->scsi_done(scsicmd);
1772 return 0;
1773 }
1774
1775
1776 /* Handle commands here that don't really require going out to the adapter */
1777 switch (scsicmd->cmnd[0]) {
1778 case INQUIRY:
1779 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001780 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Mark Haverkampe5718772006-03-27 09:43:25 -08001782 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001783 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001785 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001786 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 */
1787 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001789 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Set the Vendor, Product, and Revision Level
1792 * see: <vendor>.c i.e. aac.c
1793 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001794 if (scmd_id(scsicmd) == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07001795 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001796 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1797 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1799 scsicmd->scsi_done(scsicmd);
1800 return 0;
1801 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001802 if (dev->in_reset)
1803 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07001804 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001805 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1806 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001807 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001809 case SERVICE_ACTION_IN:
1810 if (!(dev->raw_io_interface) ||
1811 !(dev->raw_io_64) ||
1812 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1813 break;
1814 {
1815 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001816 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001817
1818 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1819 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001820 cp[0] = (capacity >> 56) & 0xff;
1821 cp[1] = (capacity >> 48) & 0xff;
1822 cp[2] = (capacity >> 40) & 0xff;
1823 cp[3] = (capacity >> 32) & 0xff;
1824 cp[4] = (capacity >> 24) & 0xff;
1825 cp[5] = (capacity >> 16) & 0xff;
1826 cp[6] = (capacity >> 8) & 0xff;
1827 cp[7] = (capacity >> 0) & 0xff;
1828 cp[8] = 0;
1829 cp[9] = 0;
1830 cp[10] = 2;
1831 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001832 cp[12] = 0;
1833 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08001834 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001835 if (sizeof(cp) < scsicmd->cmnd[13]) {
1836 unsigned int len, offset = sizeof(cp);
1837
1838 memset(cp, 0, offset);
1839 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08001840 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1841 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001842 aac_internal_transfer(scsicmd, cp, offset, len);
1843 } while ((offset += len) < scsicmd->cmnd[13]);
1844 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001845
1846 /* Do not cache partition table for arrays */
1847 scsicmd->device->removable = 1;
1848
1849 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1850 scsicmd->scsi_done(scsicmd);
1851
1852 return 0;
1853 }
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 case READ_CAPACITY:
1856 {
1857 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001858 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001861 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 capacity = fsa_dev_ptr[cid].size - 1;
1863 else
1864 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 cp[0] = (capacity >> 24) & 0xff;
1867 cp[1] = (capacity >> 16) & 0xff;
1868 cp[2] = (capacity >> 8) & 0xff;
1869 cp[3] = (capacity >> 0) & 0xff;
1870 cp[4] = 0;
1871 cp[5] = 0;
1872 cp[6] = 2;
1873 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001874 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001875 /* Do not cache partition table for arrays */
1876 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1879 scsicmd->scsi_done(scsicmd);
1880
1881 return 0;
1882 }
1883
1884 case MODE_SENSE:
1885 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001886 char mode_buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 mode_buf[0] = 3; /* Mode data length */
1890 mode_buf[1] = 0; /* Medium type - default */
1891 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1892 mode_buf[3] = 0; /* Block descriptor length */
1893
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001894 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1896 scsicmd->scsi_done(scsicmd);
1897
1898 return 0;
1899 }
1900 case MODE_SENSE_10:
1901 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001902 char mode_buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 mode_buf[0] = 0; /* Mode data length (MSB) */
1906 mode_buf[1] = 6; /* Mode data length (LSB) */
1907 mode_buf[2] = 0; /* Medium type - default */
1908 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1909 mode_buf[4] = 0; /* reserved */
1910 mode_buf[5] = 0; /* reserved */
1911 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1912 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001913 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1916 scsicmd->scsi_done(scsicmd);
1917
1918 return 0;
1919 }
1920 case REQUEST_SENSE:
1921 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1922 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1923 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1924 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1925 scsicmd->scsi_done(scsicmd);
1926 return 0;
1927
1928 case ALLOW_MEDIUM_REMOVAL:
1929 dprintk((KERN_DEBUG "LOCK command.\n"));
1930 if (scsicmd->cmnd[4])
1931 fsa_dev_ptr[cid].locked = 1;
1932 else
1933 fsa_dev_ptr[cid].locked = 0;
1934
1935 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1936 scsicmd->scsi_done(scsicmd);
1937 return 0;
1938 /*
1939 * These commands are all No-Ops
1940 */
1941 case TEST_UNIT_READY:
1942 case RESERVE:
1943 case RELEASE:
1944 case REZERO_UNIT:
1945 case REASSIGN_BLOCKS:
1946 case SEEK_10:
1947 case START_STOP:
1948 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1949 scsicmd->scsi_done(scsicmd);
1950 return 0;
1951 }
1952
1953 switch (scsicmd->cmnd[0])
1954 {
1955 case READ_6:
1956 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001957 case READ_12:
1958 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001959 if (dev->in_reset)
1960 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /*
1962 * Hack to keep track of ordinal number of the device that
1963 * corresponds to a container. Needed to convert
1964 * containers to /dev/sd device names
1965 */
1966
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001967 if (scsicmd->request->rq_disk)
1968 strlcpy(fsa_dev_ptr[cid].devname,
1969 scsicmd->request->rq_disk->disk_name,
1970 min(sizeof(fsa_dev_ptr[cid].devname),
1971 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001972
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001973 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 case WRITE_6:
1976 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001977 case WRITE_12:
1978 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001979 if (dev->in_reset)
1980 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001981 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 case SYNCHRONIZE_CACHE:
1984 /* Issue FIB to tell Firmware to flush it's cache */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001985 return aac_synchronize(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 default:
1988 /*
1989 * Unhandled commands
1990 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001991 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1993 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1994 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1995 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1996 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1997 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1998 ? sizeof(scsicmd->sense_buffer)
1999 : sizeof(dev->fsa_dev[cid].sense_data));
2000 scsicmd->scsi_done(scsicmd);
2001 return 0;
2002 }
2003}
2004
2005static int query_disk(struct aac_dev *dev, void __user *arg)
2006{
2007 struct aac_query_disk qd;
2008 struct fsa_dev_info *fsa_dev_ptr;
2009
2010 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002011 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002012 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2014 return -EFAULT;
2015 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002016 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2018 {
2019 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2020 return -EINVAL;
2021 qd.instance = dev->scsi_host_ptr->host_no;
2022 qd.bus = 0;
2023 qd.id = CONTAINER_TO_ID(qd.cnum);
2024 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2025 }
2026 else return -EINVAL;
2027
2028 qd.valid = fsa_dev_ptr[qd.cnum].valid;
2029 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2030 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2031
2032 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2033 qd.unmapped = 1;
2034 else
2035 qd.unmapped = 0;
2036
2037 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2038 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2039
2040 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2041 return -EFAULT;
2042 return 0;
2043}
2044
2045static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2046{
2047 struct aac_delete_disk dd;
2048 struct fsa_dev_info *fsa_dev_ptr;
2049
2050 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp651013552006-09-19 08:59:23 -07002051 if (!fsa_dev_ptr)
2052 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2055 return -EFAULT;
2056
2057 if (dd.cnum >= dev->maximum_num_containers)
2058 return -EINVAL;
2059 /*
2060 * Mark this container as being deleted.
2061 */
2062 fsa_dev_ptr[dd.cnum].deleted = 1;
2063 /*
2064 * Mark the container as no longer valid
2065 */
2066 fsa_dev_ptr[dd.cnum].valid = 0;
2067 return 0;
2068}
2069
2070static int delete_disk(struct aac_dev *dev, void __user *arg)
2071{
2072 struct aac_delete_disk dd;
2073 struct fsa_dev_info *fsa_dev_ptr;
2074
2075 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002076 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002077 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2080 return -EFAULT;
2081
2082 if (dd.cnum >= dev->maximum_num_containers)
2083 return -EINVAL;
2084 /*
2085 * If the container is locked, it can not be deleted by the API.
2086 */
2087 if (fsa_dev_ptr[dd.cnum].locked)
2088 return -EBUSY;
2089 else {
2090 /*
2091 * Mark the container as no longer being valid.
2092 */
2093 fsa_dev_ptr[dd.cnum].valid = 0;
2094 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2095 return 0;
2096 }
2097}
2098
2099int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2100{
2101 switch (cmd) {
2102 case FSACTL_QUERY_DISK:
2103 return query_disk(dev, arg);
2104 case FSACTL_DELETE_DISK:
2105 return delete_disk(dev, arg);
2106 case FSACTL_FORCE_DELETE_DISK:
2107 return force_delete_disk(dev, arg);
2108 case FSACTL_GET_CONTAINERS:
2109 return aac_get_containers(dev);
2110 default:
2111 return -ENOTTY;
2112 }
2113}
2114
2115/**
2116 *
2117 * aac_srb_callback
2118 * @context: the context set in the fib - here it is scsi cmd
2119 * @fibptr: pointer to the fib
2120 *
2121 * Handles the completion of a scsi command to a non dasd device
2122 *
2123 */
2124
2125static void aac_srb_callback(void *context, struct fib * fibptr)
2126{
2127 struct aac_dev *dev;
2128 struct aac_srb_reply *srbreply;
2129 struct scsi_cmnd *scsicmd;
2130
2131 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002132
2133 if (!aac_valid_context(scsicmd, fibptr))
2134 return;
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2137
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002138 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2141
2142 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
2143 /*
2144 * Calculate resid for sg
2145 */
2146
2147 scsicmd->resid = scsicmd->request_bufflen -
2148 le32_to_cpu(srbreply->data_xfer_length);
2149
2150 if(scsicmd->use_sg)
2151 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02002152 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 scsicmd->use_sg,
2154 scsicmd->sc_data_direction);
2155 else if(scsicmd->request_bufflen)
2156 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
2157 scsicmd->sc_data_direction);
2158
2159 /*
2160 * First check the fib status
2161 */
2162
2163 if (le32_to_cpu(srbreply->status) != ST_OK){
2164 int len;
2165 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2166 len = (le32_to_cpu(srbreply->sense_data_size) >
2167 sizeof(scsicmd->sense_buffer)) ?
2168 sizeof(scsicmd->sense_buffer) :
2169 le32_to_cpu(srbreply->sense_data_size);
2170 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2171 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2172 }
2173
2174 /*
2175 * Next check the srb status
2176 */
2177 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2178 case SRB_STATUS_ERROR_RECOVERY:
2179 case SRB_STATUS_PENDING:
2180 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08002181 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 break;
2183 case SRB_STATUS_DATA_OVERRUN:
2184 switch(scsicmd->cmnd[0]){
2185 case READ_6:
2186 case WRITE_6:
2187 case READ_10:
2188 case WRITE_10:
2189 case READ_12:
2190 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002191 case READ_16:
2192 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2194 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2195 } else {
2196 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2197 }
2198 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2199 break;
2200 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08002201 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 break;
2203 }
2204 default:
2205 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2206 break;
2207 }
2208 break;
2209 case SRB_STATUS_ABORTED:
2210 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2211 break;
2212 case SRB_STATUS_ABORT_FAILED:
2213 // Not sure about this one - but assuming the hba was trying to abort for some reason
2214 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2215 break;
2216 case SRB_STATUS_PARITY_ERROR:
2217 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2218 break;
2219 case SRB_STATUS_NO_DEVICE:
2220 case SRB_STATUS_INVALID_PATH_ID:
2221 case SRB_STATUS_INVALID_TARGET_ID:
2222 case SRB_STATUS_INVALID_LUN:
2223 case SRB_STATUS_SELECTION_TIMEOUT:
2224 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2225 break;
2226
2227 case SRB_STATUS_COMMAND_TIMEOUT:
2228 case SRB_STATUS_TIMEOUT:
2229 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2230 break;
2231
2232 case SRB_STATUS_BUSY:
2233 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2234 break;
2235
2236 case SRB_STATUS_BUS_RESET:
2237 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2238 break;
2239
2240 case SRB_STATUS_MESSAGE_REJECTED:
2241 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2242 break;
2243 case SRB_STATUS_REQUEST_FLUSHED:
2244 case SRB_STATUS_ERROR:
2245 case SRB_STATUS_INVALID_REQUEST:
2246 case SRB_STATUS_REQUEST_SENSE_FAILED:
2247 case SRB_STATUS_NO_HBA:
2248 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2249 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2250 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2251 case SRB_STATUS_DELAYED_RETRY:
2252 case SRB_STATUS_BAD_FUNCTION:
2253 case SRB_STATUS_NOT_STARTED:
2254 case SRB_STATUS_NOT_IN_USE:
2255 case SRB_STATUS_FORCE_ABORT:
2256 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2257 default:
2258#ifdef AAC_DETAILED_STATUS_INFO
2259 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2260 le32_to_cpu(srbreply->srb_status) & 0x3F,
2261 aac_get_status_string(
2262 le32_to_cpu(srbreply->srb_status) & 0x3F),
2263 scsicmd->cmnd[0],
2264 le32_to_cpu(srbreply->scsi_status));
2265#endif
2266 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2267 break;
2268 }
2269 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2270 int len;
2271 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2272 len = (le32_to_cpu(srbreply->sense_data_size) >
2273 sizeof(scsicmd->sense_buffer)) ?
2274 sizeof(scsicmd->sense_buffer) :
2275 le32_to_cpu(srbreply->sense_data_size);
2276#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002277 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2278 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279#endif
2280 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2281
2282 }
2283 /*
2284 * OR in the scsi status (already shifted up a bit)
2285 */
2286 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2287
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002288 aac_fib_complete(fibptr);
2289 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002290 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
2293/**
2294 *
2295 * aac_send_scb_fib
2296 * @scsicmd: the scsi command block
2297 *
2298 * This routine will form a FIB and fill in the aac_srb from the
2299 * scsicmd passed in.
2300 */
2301
2302static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2303{
2304 struct fib* cmd_fibcontext;
2305 struct aac_dev* dev;
2306 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Mark Haverkamp84971732005-06-20 11:55:24 -07002308 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002309 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002310 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 scsicmd->result = DID_NO_CONNECT << 16;
2312 scsicmd->scsi_done(scsicmd);
2313 return 0;
2314 }
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /*
2317 * Allocate and initialize a Fib then setup a BlockWrite command
2318 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002319 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return -1;
2321 }
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002322 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /*
2325 * Check that the command queued to the controller
2326 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002327 if (status == -EINPROGRESS) {
2328 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330 }
2331
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002332 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2333 aac_fib_complete(cmd_fibcontext);
2334 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 return -1;
2337}
2338
2339static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2340{
2341 struct aac_dev *dev;
2342 unsigned long byte_count = 0;
2343
2344 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2345 // Get rid of old data
2346 psg->count = 0;
2347 psg->sg[0].addr = 0;
2348 psg->sg[0].count = 0;
2349 if (scsicmd->use_sg) {
2350 struct scatterlist *sg;
2351 int i;
2352 int sg_count;
2353 sg = (struct scatterlist *) scsicmd->request_buffer;
2354
2355 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2356 scsicmd->sc_data_direction);
2357 psg->count = cpu_to_le32(sg_count);
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 for (i = 0; i < sg_count; i++) {
2360 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2361 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2362 byte_count += sg_dma_len(sg);
2363 sg++;
2364 }
2365 /* hba wants the size to be exact */
2366 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002367 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2368 (byte_count - scsicmd->request_bufflen);
2369 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 byte_count = scsicmd->request_bufflen;
2371 }
2372 /* Check for command underflow */
2373 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2374 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2375 byte_count, scsicmd->underflow);
2376 }
2377 }
2378 else if(scsicmd->request_bufflen) {
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002379 u32 addr;
2380 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 scsicmd->request_buffer,
2382 scsicmd->request_bufflen,
2383 scsicmd->sc_data_direction);
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002384 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 psg->count = cpu_to_le32(1);
2386 psg->sg[0].addr = cpu_to_le32(addr);
2387 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 byte_count = scsicmd->request_bufflen;
2389 }
2390 return byte_count;
2391}
2392
2393
2394static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2395{
2396 struct aac_dev *dev;
2397 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002398 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2401 // Get rid of old data
2402 psg->count = 0;
2403 psg->sg[0].addr[0] = 0;
2404 psg->sg[0].addr[1] = 0;
2405 psg->sg[0].count = 0;
2406 if (scsicmd->use_sg) {
2407 struct scatterlist *sg;
2408 int i;
2409 int sg_count;
2410 sg = (struct scatterlist *) scsicmd->request_buffer;
2411
2412 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2413 scsicmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 for (i = 0; i < sg_count; i++) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002416 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002417 addr = sg_dma_address(sg);
2418 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2419 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002420 psg->sg[i].count = cpu_to_le32(count);
2421 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 sg++;
2423 }
Mark Haverkamp1241f352006-03-27 09:44:23 -08002424 psg->count = cpu_to_le32(sg_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /* hba wants the size to be exact */
2426 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002427 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2428 (byte_count - scsicmd->request_bufflen);
2429 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 byte_count = scsicmd->request_bufflen;
2431 }
2432 /* Check for command underflow */
2433 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2434 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2435 byte_count, scsicmd->underflow);
2436 }
2437 }
2438 else if(scsicmd->request_bufflen) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002439 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 scsicmd->request_buffer,
2441 scsicmd->request_bufflen,
2442 scsicmd->sc_data_direction);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002443 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 psg->count = cpu_to_le32(1);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002445 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2446 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 byte_count = scsicmd->request_bufflen;
2449 }
2450 return byte_count;
2451}
2452
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002453static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2454{
2455 struct Scsi_Host *host = scsicmd->device->host;
2456 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2457 unsigned long byte_count = 0;
2458
2459 // Get rid of old data
2460 psg->count = 0;
2461 psg->sg[0].next = 0;
2462 psg->sg[0].prev = 0;
2463 psg->sg[0].addr[0] = 0;
2464 psg->sg[0].addr[1] = 0;
2465 psg->sg[0].count = 0;
2466 psg->sg[0].flags = 0;
2467 if (scsicmd->use_sg) {
2468 struct scatterlist *sg;
2469 int i;
2470 int sg_count;
2471 sg = (struct scatterlist *) scsicmd->request_buffer;
2472
2473 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2474 scsicmd->sc_data_direction);
2475
2476 for (i = 0; i < sg_count; i++) {
2477 int count = sg_dma_len(sg);
2478 u64 addr = sg_dma_address(sg);
2479 psg->sg[i].next = 0;
2480 psg->sg[i].prev = 0;
2481 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2482 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2483 psg->sg[i].count = cpu_to_le32(count);
2484 psg->sg[i].flags = 0;
2485 byte_count += count;
2486 sg++;
2487 }
2488 psg->count = cpu_to_le32(sg_count);
2489 /* hba wants the size to be exact */
2490 if(byte_count > scsicmd->request_bufflen){
2491 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2492 (byte_count - scsicmd->request_bufflen);
2493 psg->sg[i-1].count = cpu_to_le32(temp);
2494 byte_count = scsicmd->request_bufflen;
2495 }
2496 /* Check for command underflow */
2497 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2498 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2499 byte_count, scsicmd->underflow);
2500 }
2501 }
2502 else if(scsicmd->request_bufflen) {
2503 int count;
2504 u64 addr;
2505 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2506 scsicmd->request_buffer,
2507 scsicmd->request_bufflen,
2508 scsicmd->sc_data_direction);
2509 addr = scsicmd->SCp.dma_handle;
2510 count = scsicmd->request_bufflen;
2511 psg->count = cpu_to_le32(1);
2512 psg->sg[0].next = 0;
2513 psg->sg[0].prev = 0;
2514 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2515 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2516 psg->sg[0].count = cpu_to_le32(count);
2517 psg->sg[0].flags = 0;
2518 byte_count = scsicmd->request_bufflen;
2519 }
2520 return byte_count;
2521}
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523#ifdef AAC_DETAILED_STATUS_INFO
2524
2525struct aac_srb_status_info {
2526 u32 status;
2527 char *str;
2528};
2529
2530
2531static struct aac_srb_status_info srb_status_info[] = {
2532 { SRB_STATUS_PENDING, "Pending Status"},
2533 { SRB_STATUS_SUCCESS, "Success"},
2534 { SRB_STATUS_ABORTED, "Aborted Command"},
2535 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002536 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 { SRB_STATUS_BUSY, "Device Busy"},
2538 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2539 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2540 { SRB_STATUS_NO_DEVICE, "No Device"},
2541 { SRB_STATUS_TIMEOUT, "Timeout"},
2542 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2543 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2544 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2545 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2546 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2547 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2548 { SRB_STATUS_NO_HBA, "No HBA"},
2549 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2550 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2551 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2552 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2553 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2554 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002555 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2557 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2558 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2559 { SRB_STATUS_NOT_STARTED, "Not Started"},
2560 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2561 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2562 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2563 { 0xff, "Unknown Error"}
2564};
2565
2566char *aac_get_status_string(u32 status)
2567{
2568 int i;
2569
Tobias Klauser6391a112006-06-08 22:23:48 -07002570 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2571 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 return "Bad Status Code";
2575}
2576
2577#endif