blob: 8dcfe4ec35c224d2b296df65455817d608d3b9e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
Mark Haverkamp03d44332007-03-15 10:27:45 -07008 * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pci.h>
30#include <linux/spinlock.h>
31#include <linux/slab.h>
32#include <linux/completion.h>
33#include <linux/blkdev.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080034#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/semaphore.h>
36#include <asm/uaccess.h>
37
38#include <scsi/scsi.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42
43#include "aacraid.h"
44
45/* values for inqd_pdt: Peripheral device type in plain English */
46#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
47#define INQD_PDT_PROC 0x03 /* Processor device */
48#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
49#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
50#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
51#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
52
53#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
54#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Sense codes
58 */
59
60#define SENCODE_NO_SENSE 0x00
61#define SENCODE_END_OF_DATA 0x00
62#define SENCODE_BECOMING_READY 0x04
63#define SENCODE_INIT_CMD_REQUIRED 0x04
64#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
65#define SENCODE_INVALID_COMMAND 0x20
66#define SENCODE_LBA_OUT_OF_RANGE 0x21
67#define SENCODE_INVALID_CDB_FIELD 0x24
68#define SENCODE_LUN_NOT_SUPPORTED 0x25
69#define SENCODE_INVALID_PARAM_FIELD 0x26
70#define SENCODE_PARAM_NOT_SUPPORTED 0x26
71#define SENCODE_PARAM_VALUE_INVALID 0x26
72#define SENCODE_RESET_OCCURRED 0x29
73#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
74#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
75#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
76#define SENCODE_DIAGNOSTIC_FAILURE 0x40
77#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
78#define SENCODE_INVALID_MESSAGE_ERROR 0x49
79#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
80#define SENCODE_OVERLAPPED_COMMAND 0x4E
81
82/*
83 * Additional sense codes
84 */
85
86#define ASENCODE_NO_SENSE 0x00
87#define ASENCODE_END_OF_DATA 0x05
88#define ASENCODE_BECOMING_READY 0x01
89#define ASENCODE_INIT_CMD_REQUIRED 0x02
90#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
91#define ASENCODE_INVALID_COMMAND 0x00
92#define ASENCODE_LBA_OUT_OF_RANGE 0x00
93#define ASENCODE_INVALID_CDB_FIELD 0x00
94#define ASENCODE_LUN_NOT_SUPPORTED 0x00
95#define ASENCODE_INVALID_PARAM_FIELD 0x00
96#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
97#define ASENCODE_PARAM_VALUE_INVALID 0x02
98#define ASENCODE_RESET_OCCURRED 0x00
99#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
100#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
101#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
102#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
103#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
104#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
105#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
106#define ASENCODE_OVERLAPPED_COMMAND 0x00
107
108#define BYTE0(x) (unsigned char)(x)
109#define BYTE1(x) (unsigned char)((x) >> 8)
110#define BYTE2(x) (unsigned char)((x) >> 16)
111#define BYTE3(x) (unsigned char)((x) >> 24)
112
113/*------------------------------------------------------------------------------
114 * S T R U C T S / T Y P E D E F S
115 *----------------------------------------------------------------------------*/
116/* SCSI inquiry data */
117struct inquiry_data {
118 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
119 u8 inqd_dtq; /* RMB | Device Type Qualifier */
120 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
121 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
122 u8 inqd_len; /* Additional length (n-4) */
123 u8 inqd_pad1[2];/* Reserved - must be zero */
124 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
125 u8 inqd_vid[8]; /* Vendor ID */
126 u8 inqd_pid[16];/* Product ID */
127 u8 inqd_prl[4]; /* Product Revision Level */
128};
129
130/*
131 * M O D U L E G L O B A L S
132 */
133
134static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
135static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700136static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
138#ifdef AAC_DETAILED_STATUS_INFO
139static char *aac_get_status_string(u32 status);
140#endif
141
142/*
143 * Non dasd selection is handled entirely in aachba now
144 */
145
146static int nondasd = -1;
147static int dacmode = -1;
148
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400149int aac_commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700150int startup_timeout = 180;
151int aif_timeout = 120;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800153module_param(nondasd, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800155module_param(dacmode, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400157module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700159module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
160MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for adapter to have it's kernel up and\nrunning. This is typically adjusted for large systems that do not have a BIOS.");
161module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
162MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for applications to pick up AIFs before\nderegistering them. This is typically adjusted for heavily burdened systems.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700164int numacb = -1;
165module_param(numacb, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800166MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid values are 512 and down. Default is to use suggestion from Firmware.");
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700167
168int acbsize = -1;
169module_param(acbsize, int, S_IRUGO|S_IWUSR);
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800170MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512, 2048, 4096 and 8192. Default is to use suggestion from Firmware.");
Mark Haverkamp653ba582006-09-19 08:59:43 -0700171
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
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400176int aac_reset_devices = 0;
177module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
178MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700179
180static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
181 struct fib *fibptr) {
182 struct scsi_device *device;
183
184 if (unlikely(!scsicmd || !scsicmd->scsi_done )) {
185 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"))
186;
187 aac_fib_complete(fibptr);
188 aac_fib_free(fibptr);
189 return 0;
190 }
191 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
192 device = scsicmd->device;
193 if (unlikely(!device || !scsi_device_online(device))) {
194 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
195 aac_fib_complete(fibptr);
196 aac_fib_free(fibptr);
197 return 0;
198 }
199 return 1;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/**
203 * aac_get_config_status - check the adapter configuration
204 * @common: adapter to query
205 *
206 * Query config status, and commit the configuration if needed.
207 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700208int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 int status = 0;
211 struct fib * fibptr;
212
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800213 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -ENOMEM;
215
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800216 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 {
218 struct aac_get_config_status *dinfo;
219 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
220
221 dinfo->command = cpu_to_le32(VM_ContainerConfig);
222 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
223 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
224 }
225
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800226 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 fibptr,
228 sizeof (struct aac_get_config_status),
229 FsaNormal,
230 1, 1,
231 NULL, NULL);
232 if (status < 0 ) {
233 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
234 } else {
235 struct aac_get_config_status_resp *reply
236 = (struct aac_get_config_status_resp *) fib_data(fibptr);
237 dprintk((KERN_WARNING
238 "aac_get_config_status: response=%d status=%d action=%d\n",
239 le32_to_cpu(reply->response),
240 le32_to_cpu(reply->status),
241 le32_to_cpu(reply->data.action)));
242 if ((le32_to_cpu(reply->response) != ST_OK) ||
243 (le32_to_cpu(reply->status) != CT_OK) ||
244 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
245 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
246 status = -EINVAL;
247 }
248 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800249 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
251 if (status >= 0) {
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400252 if ((aac_commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800254 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dinfo = (struct aac_commit_config *) fib_data(fibptr);
256
257 dinfo->command = cpu_to_le32(VM_ContainerConfig);
258 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
259
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800260 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 fibptr,
262 sizeof (struct aac_commit_config),
263 FsaNormal,
264 1, 1,
265 NULL, NULL);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800266 aac_fib_complete(fibptr);
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400267 } else if (aac_commit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 printk(KERN_WARNING
269 "aac_get_config_status: Foreign device configurations are being ignored\n");
270 }
271 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800272 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return status;
274}
275
276/**
277 * aac_get_containers - list containers
278 * @common: adapter to probe
279 *
280 * Make a list of all containers on this controller
281 */
282int aac_get_containers(struct aac_dev *dev)
283{
284 struct fsa_dev_info *fsa_dev_ptr;
285 u32 index;
286 int status = 0;
287 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct aac_get_container_count *dinfo;
289 struct aac_get_container_count_resp *dresp;
290 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
291
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800292 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ENOMEM;
294
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800295 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
297 dinfo->command = cpu_to_le32(VM_ContainerConfig);
298 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
299
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800300 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 fibptr,
302 sizeof (struct aac_get_container_count),
303 FsaNormal,
304 1, 1,
305 NULL, NULL);
306 if (status >= 0) {
307 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
308 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800309 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700311 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
314 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700315 fsa_dev_ptr = kmalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
316 GFP_KERNEL);
317 if (!fsa_dev_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
320
321 dev->fsa_dev = fsa_dev_ptr;
322 dev->maximum_num_containers = maximum_num_containers;
323
Mark Haverkampfe76df42007-03-15 12:55:07 -0700324 for (index = 0; index < dev->maximum_num_containers; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 fsa_dev_ptr[index].devname[0] = '\0';
326
Mark Haverkampfe76df42007-03-15 12:55:07 -0700327 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Mark Haverkampfe76df42007-03-15 12:55:07 -0700329 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
331 break;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * If there are no more containers, then stop asking.
336 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700337 if (++index >= status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return status;
341}
342
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700343static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
344{
345 void *buf;
James Bottomleycab537d2007-05-16 10:06:39 -0400346 int transfer_len;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700347 struct scatterlist *sg = scsicmd->request_buffer;
348
349 if (scsicmd->use_sg) {
350 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
351 transfer_len = min(sg->length, len + offset);
352 } else {
353 buf = scsicmd->request_buffer;
354 transfer_len = min(scsicmd->request_bufflen, len + offset);
355 }
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400356 transfer_len -= offset;
James Bottomleycab537d2007-05-16 10:06:39 -0400357 if (buf && transfer_len > 0)
Salyzyn, Mark20235f32007-03-21 13:22:56 -0400358 memcpy(buf + offset, data, transfer_len);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700359
360 if (scsicmd->use_sg)
361 kunmap_atomic(buf - sg->offset, KM_IRQ0);
362
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static void get_container_name_callback(void *context, struct fib * fibptr)
366{
367 struct aac_get_name_resp * get_name_reply;
368 struct scsi_cmnd * scsicmd;
369
370 scsicmd = (struct scsi_cmnd *) context;
371
Mark Haverkamp03d44332007-03-15 10:27:45 -0700372 if (!aac_valid_context(scsicmd, fibptr))
373 return;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700376 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
379 /* Failure is irrelevant, using default value instead */
380 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
381 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700382 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
384 while (*sp == ' ')
385 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700386 if (*sp) {
387 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
388 int count = sizeof(d);
389 char *dp = d;
390 do {
391 *dp++ = (*sp) ? *sp++ : ' ';
392 } while (--count > 0);
393 aac_internal_transfer(scsicmd, d,
394 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
399
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800400 aac_fib_complete(fibptr);
401 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700402 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/**
406 * aac_get_container_name - get container name, none blocking.
407 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700408static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 int status;
411 struct aac_get_name *dinfo;
412 struct fib * cmd_fibcontext;
413 struct aac_dev * dev;
414
415 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
416
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800417 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -ENOMEM;
419
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800420 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
422
423 dinfo->command = cpu_to_le32(VM_ContainerConfig);
424 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700425 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
427
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800428 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 cmd_fibcontext,
430 sizeof (struct aac_get_name),
431 FsaNormal,
432 0, 1,
433 (fib_callback) get_container_name_callback,
434 (void *) scsicmd);
435
436 /*
437 * Check that the command queued to the controller
438 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800439 if (status == -EINPROGRESS) {
440 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800444 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
445 aac_fib_complete(cmd_fibcontext);
446 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -1;
448}
449
Mark Haverkampfe76df42007-03-15 12:55:07 -0700450static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
451{
452 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
453
454 if (fsa_dev_ptr[scmd_id(scsicmd)].valid)
455 return aac_scsi_cmd(scsicmd);
456
457 scsicmd->result = DID_NO_CONNECT << 16;
458 scsicmd->scsi_done(scsicmd);
459 return 0;
460}
461
462static int _aac_probe_container2(void * context, struct fib * fibptr)
463{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700464 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700465 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700466 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
467
468 if (!aac_valid_context(scsicmd, fibptr))
469 return 0;
470
471 fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700472
473 scsicmd->SCp.Status = 0;
474 if (fsa_dev_ptr) {
475 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
476 fsa_dev_ptr += scmd_id(scsicmd);
477
478 if ((le32_to_cpu(dresp->status) == ST_OK) &&
479 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
480 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
481 fsa_dev_ptr->valid = 1;
482 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
483 fsa_dev_ptr->size
484 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
485 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
486 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
487 }
488 if ((fsa_dev_ptr->valid & 1) == 0)
489 fsa_dev_ptr->valid = 0;
490 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
491 }
492 aac_fib_complete(fibptr);
493 aac_fib_free(fibptr);
494 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
495 scsicmd->SCp.ptr = NULL;
496 return (*callback)(scsicmd);
497}
498
499static int _aac_probe_container1(void * context, struct fib * fibptr)
500{
501 struct scsi_cmnd * scsicmd;
502 struct aac_mount * dresp;
503 struct aac_query_mount *dinfo;
504 int status;
505
506 dresp = (struct aac_mount *) fib_data(fibptr);
507 dresp->mnt[0].capacityhigh = 0;
508 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark4def7fa2007-04-13 08:35:25 -0400509 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700510 return _aac_probe_container2(context, fibptr);
511 scsicmd = (struct scsi_cmnd *) context;
512 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
513
Mark Haverkamp03d44332007-03-15 10:27:45 -0700514 if (!aac_valid_context(scsicmd, fibptr))
515 return 0;
516
Mark Haverkampfe76df42007-03-15 12:55:07 -0700517 aac_fib_init(fibptr);
518
519 dinfo = (struct aac_query_mount *)fib_data(fibptr);
520
521 dinfo->command = cpu_to_le32(VM_NameServe64);
522 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
523 dinfo->type = cpu_to_le32(FT_FILESYS);
524
525 status = aac_fib_send(ContainerCommand,
526 fibptr,
527 sizeof(struct aac_query_mount),
528 FsaNormal,
529 0, 1,
530 (fib_callback) _aac_probe_container2,
531 (void *) scsicmd);
532 /*
533 * Check that the command queued to the controller
534 */
535 if (status == -EINPROGRESS) {
536 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
537 return 0;
538 }
539 if (status < 0) {
540 /* Inherit results from VM_NameServe, if any */
541 dresp->status = cpu_to_le32(ST_OK);
542 return _aac_probe_container2(context, fibptr);
543 }
544 return 0;
545}
546
547static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
548{
549 struct fib * fibptr;
550 int status = -ENOMEM;
551
552 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
553 struct aac_query_mount *dinfo;
554
555 aac_fib_init(fibptr);
556
557 dinfo = (struct aac_query_mount *)fib_data(fibptr);
558
559 dinfo->command = cpu_to_le32(VM_NameServe);
560 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
561 dinfo->type = cpu_to_le32(FT_FILESYS);
562 scsicmd->SCp.ptr = (char *)callback;
563
564 status = aac_fib_send(ContainerCommand,
565 fibptr,
566 sizeof(struct aac_query_mount),
567 FsaNormal,
568 0, 1,
569 (fib_callback) _aac_probe_container1,
570 (void *) scsicmd);
571 /*
572 * Check that the command queued to the controller
573 */
574 if (status == -EINPROGRESS) {
575 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
576 return 0;
577 }
578 if (status < 0) {
579 scsicmd->SCp.ptr = NULL;
580 aac_fib_complete(fibptr);
581 aac_fib_free(fibptr);
582 }
583 }
584 if (status < 0) {
585 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
586 if (fsa_dev_ptr) {
587 fsa_dev_ptr += scmd_id(scsicmd);
588 if ((fsa_dev_ptr->valid & 1) == 0) {
589 fsa_dev_ptr->valid = 0;
590 return (*callback)(scsicmd);
591 }
592 }
593 }
594 return status;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800598 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * @dev: device to query
600 * @cid: container identifier
601 *
602 * Queries the controller about the given volume. The volume information
603 * is updated in the struct fsa_dev_info structure rather than returned.
604 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700605static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
606{
607 scsicmd->device = NULL;
608 return 0;
609}
610
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800611int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700613 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
614 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Mark Haverkampfe76df42007-03-15 12:55:07 -0700617 if (!scsicmd || !scsidev) {
618 kfree(scsicmd);
619 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700620 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700622 scsicmd->list.next = NULL;
623 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))_aac_probe_container1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Mark Haverkampfe76df42007-03-15 12:55:07 -0700625 scsicmd->device = scsidev;
626 scsidev->sdev_state = 0;
627 scsidev->id = cid;
628 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Mark Haverkampfe76df42007-03-15 12:55:07 -0700630 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
631 while (scsicmd->device == scsidev)
632 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400633 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700634 status = scsicmd->SCp.Status;
635 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return status;
637}
638
639/* Local Structure to set SCSI inquiry data strings */
640struct scsi_inq {
641 char vid[8]; /* Vendor ID */
642 char pid[16]; /* Product ID */
643 char prl[4]; /* Product Revision Level */
644};
645
646/**
647 * InqStrCopy - string merge
648 * @a: string to copy from
649 * @b: string to copy to
650 *
651 * Copy a String from one location to another
652 * without copying \0
653 */
654
655static void inqstrcpy(char *a, char *b)
656{
657
658 while(*a != (char)0)
659 *b++ = *a++;
660}
661
662static char *container_types[] = {
663 "None",
664 "Volume",
665 "Mirror",
666 "Stripe",
667 "RAID5",
668 "SSRW",
669 "SSRO",
670 "Morph",
671 "Legacy",
672 "RAID4",
673 "RAID10",
674 "RAID00",
675 "V-MIRRORS",
676 "PSEUDO R4",
677 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700678 "RAID5D",
679 "RAID5D0",
680 "RAID1E",
681 "RAID6",
682 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 "Unknown"
684};
685
686
687
688/* Function: setinqstr
689 *
690 * Arguments: [1] pointer to void [1] int
691 *
692 * Purpose: Sets SCSI inquiry data strings for vendor, product
693 * and revision level. Allows strings to be set in platform dependant
694 * files instead of in OS dependant driver source.
695 */
696
Mark Haverkamp794d0602005-10-24 10:51:53 -0700697static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700702 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Mark Haverkamp794d0602005-10-24 10:51:53 -0700704 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
705 char * cp = dev->supplement_adapter_info.AdapterTypeText;
706 int c = sizeof(str->vid);
707 while (*cp && *cp != ' ' && --c)
708 ++cp;
709 c = *cp;
710 *cp = '\0';
711 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
712 str->vid);
713 *cp = c;
714 while (*cp && *cp != ' ')
715 ++cp;
716 while (*cp == ' ')
717 ++cp;
718 /* last six chars reserved for vol type */
719 c = 0;
720 if (strlen(cp) > sizeof(str->pid)) {
721 c = cp[sizeof(str->pid)];
722 cp[sizeof(str->pid)] = '\0';
723 }
724 inqstrcpy (cp, str->pid);
725 if (c)
726 cp[sizeof(str->pid)] = c;
727 } else {
728 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700729
730 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700731 /* last six chars reserved for vol type */
732 inqstrcpy (mp->model, str->pid);
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Tobias Klauser6391a112006-06-08 22:23:48 -0700735 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 char *findit = str->pid;
737
738 for ( ; *findit != ' '; findit++); /* walk till we find a space */
739 /* RAID is superfluous in the context of a RAID device */
740 if (memcmp(findit-4, "RAID", 4) == 0)
741 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700742 if (((findit - str->pid) + strlen(container_types[tindex]))
743 < (sizeof(str->pid) + sizeof(str->prl)))
744 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 inqstrcpy ("V1.0", str->prl);
747}
748
Adrian Bunk 48338692005-04-25 19:45:58 -0700749static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
750 u8 a_sense_code, u8 incorrect_length,
751 u8 bit_pointer, u16 field_pointer,
752 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
755 sense_buf[1] = 0; /* Segment number, always zero */
756
757 if (incorrect_length) {
758 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
759 sense_buf[3] = BYTE3(residue);
760 sense_buf[4] = BYTE2(residue);
761 sense_buf[5] = BYTE1(residue);
762 sense_buf[6] = BYTE0(residue);
763 } else
764 sense_buf[2] = sense_key; /* Sense key */
765
766 if (sense_key == ILLEGAL_REQUEST)
767 sense_buf[7] = 10; /* Additional sense length */
768 else
769 sense_buf[7] = 6; /* Additional sense length */
770
771 sense_buf[12] = sense_code; /* Additional sense code */
772 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
773 if (sense_key == ILLEGAL_REQUEST) {
774 sense_buf[15] = 0;
775
776 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
777 sense_buf[15] = 0x80;/* Std sense key specific field */
778 /* Illegal parameter is in the parameter block */
779
780 if (sense_code == SENCODE_INVALID_CDB_FIELD)
781 sense_buf[15] = 0xc0;/* Std sense key specific field */
782 /* Illegal parameter is in the CDB block */
783 sense_buf[15] |= bit_pointer;
784 sense_buf[16] = field_pointer >> 8; /* MSB */
785 sense_buf[17] = field_pointer; /* LSB */
786 }
787}
788
Mark Haverkampe8f32de2007-01-23 15:00:30 -0800789static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
790{
791 if (lba & 0xffffffff00000000LL) {
792 int cid = scmd_id(cmd);
793 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
794 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
795 SAM_STAT_CHECK_CONDITION;
796 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
797 HARDWARE_ERROR,
798 SENCODE_INTERNAL_TARGET_FAILURE,
799 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
800 0, 0);
801 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
802 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(cmd->sense_buffer))
803 ? sizeof(cmd->sense_buffer)
804 : sizeof(dev->fsa_dev[cid].sense_data));
805 cmd->scsi_done(cmd);
806 return 1;
807 }
808 return 0;
809}
810
811static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
812{
813 return 0;
814}
815
816static void io_callback(void *context, struct fib * fibptr);
817
818static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
819{
820 u16 fibsize;
821 struct aac_raw_io *readcmd;
822 aac_fib_init(fib);
823 readcmd = (struct aac_raw_io *) fib_data(fib);
824 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
825 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
826 readcmd->count = cpu_to_le32(count<<9);
827 readcmd->cid = cpu_to_le16(scmd_id(cmd));
828 readcmd->flags = cpu_to_le16(1);
829 readcmd->bpTotal = 0;
830 readcmd->bpComplete = 0;
831
832 aac_build_sgraw(cmd, &readcmd->sg);
833 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
834 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
835 /*
836 * Now send the Fib to the adapter
837 */
838 return aac_fib_send(ContainerRawIo,
839 fib,
840 fibsize,
841 FsaNormal,
842 0, 1,
843 (fib_callback) io_callback,
844 (void *) cmd);
845}
846
847static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
848{
849 u16 fibsize;
850 struct aac_read64 *readcmd;
851 aac_fib_init(fib);
852 readcmd = (struct aac_read64 *) fib_data(fib);
853 readcmd->command = cpu_to_le32(VM_CtHostRead64);
854 readcmd->cid = cpu_to_le16(scmd_id(cmd));
855 readcmd->sector_count = cpu_to_le16(count);
856 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
857 readcmd->pad = 0;
858 readcmd->flags = 0;
859
860 aac_build_sg64(cmd, &readcmd->sg);
861 fibsize = sizeof(struct aac_read64) +
862 ((le32_to_cpu(readcmd->sg.count) - 1) *
863 sizeof (struct sgentry64));
864 BUG_ON (fibsize > (fib->dev->max_fib_size -
865 sizeof(struct aac_fibhdr)));
866 /*
867 * Now send the Fib to the adapter
868 */
869 return aac_fib_send(ContainerCommand64,
870 fib,
871 fibsize,
872 FsaNormal,
873 0, 1,
874 (fib_callback) io_callback,
875 (void *) cmd);
876}
877
878static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
879{
880 u16 fibsize;
881 struct aac_read *readcmd;
882 aac_fib_init(fib);
883 readcmd = (struct aac_read *) fib_data(fib);
884 readcmd->command = cpu_to_le32(VM_CtBlockRead);
885 readcmd->cid = cpu_to_le16(scmd_id(cmd));
886 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
887 readcmd->count = cpu_to_le32(count * 512);
888
889 aac_build_sg(cmd, &readcmd->sg);
890 fibsize = sizeof(struct aac_read) +
891 ((le32_to_cpu(readcmd->sg.count) - 1) *
892 sizeof (struct sgentry));
893 BUG_ON (fibsize > (fib->dev->max_fib_size -
894 sizeof(struct aac_fibhdr)));
895 /*
896 * Now send the Fib to the adapter
897 */
898 return aac_fib_send(ContainerCommand,
899 fib,
900 fibsize,
901 FsaNormal,
902 0, 1,
903 (fib_callback) io_callback,
904 (void *) cmd);
905}
906
907static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
908{
909 u16 fibsize;
910 struct aac_raw_io *writecmd;
911 aac_fib_init(fib);
912 writecmd = (struct aac_raw_io *) fib_data(fib);
913 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
914 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
915 writecmd->count = cpu_to_le32(count<<9);
916 writecmd->cid = cpu_to_le16(scmd_id(cmd));
917 writecmd->flags = 0;
918 writecmd->bpTotal = 0;
919 writecmd->bpComplete = 0;
920
921 aac_build_sgraw(cmd, &writecmd->sg);
922 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
923 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
924 /*
925 * Now send the Fib to the adapter
926 */
927 return aac_fib_send(ContainerRawIo,
928 fib,
929 fibsize,
930 FsaNormal,
931 0, 1,
932 (fib_callback) io_callback,
933 (void *) cmd);
934}
935
936static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
937{
938 u16 fibsize;
939 struct aac_write64 *writecmd;
940 aac_fib_init(fib);
941 writecmd = (struct aac_write64 *) fib_data(fib);
942 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
943 writecmd->cid = cpu_to_le16(scmd_id(cmd));
944 writecmd->sector_count = cpu_to_le16(count);
945 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
946 writecmd->pad = 0;
947 writecmd->flags = 0;
948
949 aac_build_sg64(cmd, &writecmd->sg);
950 fibsize = sizeof(struct aac_write64) +
951 ((le32_to_cpu(writecmd->sg.count) - 1) *
952 sizeof (struct sgentry64));
953 BUG_ON (fibsize > (fib->dev->max_fib_size -
954 sizeof(struct aac_fibhdr)));
955 /*
956 * Now send the Fib to the adapter
957 */
958 return aac_fib_send(ContainerCommand64,
959 fib,
960 fibsize,
961 FsaNormal,
962 0, 1,
963 (fib_callback) io_callback,
964 (void *) cmd);
965}
966
967static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
968{
969 u16 fibsize;
970 struct aac_write *writecmd;
971 aac_fib_init(fib);
972 writecmd = (struct aac_write *) fib_data(fib);
973 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
974 writecmd->cid = cpu_to_le16(scmd_id(cmd));
975 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
976 writecmd->count = cpu_to_le32(count * 512);
977 writecmd->sg.count = cpu_to_le32(1);
978 /* ->stable is not used - it did mean which type of write */
979
980 aac_build_sg(cmd, &writecmd->sg);
981 fibsize = sizeof(struct aac_write) +
982 ((le32_to_cpu(writecmd->sg.count) - 1) *
983 sizeof (struct sgentry));
984 BUG_ON (fibsize > (fib->dev->max_fib_size -
985 sizeof(struct aac_fibhdr)));
986 /*
987 * Now send the Fib to the adapter
988 */
989 return aac_fib_send(ContainerCommand,
990 fib,
991 fibsize,
992 FsaNormal,
993 0, 1,
994 (fib_callback) io_callback,
995 (void *) cmd);
996}
997
998static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
999{
1000 struct aac_srb * srbcmd;
1001 u32 flag;
1002 u32 timeout;
1003
1004 aac_fib_init(fib);
1005 switch(cmd->sc_data_direction){
1006 case DMA_TO_DEVICE:
1007 flag = SRB_DataOut;
1008 break;
1009 case DMA_BIDIRECTIONAL:
1010 flag = SRB_DataIn | SRB_DataOut;
1011 break;
1012 case DMA_FROM_DEVICE:
1013 flag = SRB_DataIn;
1014 break;
1015 case DMA_NONE:
1016 default: /* shuts up some versions of gcc */
1017 flag = SRB_NoDataXfer;
1018 break;
1019 }
1020
1021 srbcmd = (struct aac_srb*) fib_data(fib);
1022 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1023 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1024 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1025 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1026 srbcmd->flags = cpu_to_le32(flag);
1027 timeout = cmd->timeout_per_command/HZ;
1028 if (timeout == 0)
1029 timeout = 1;
1030 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1031 srbcmd->retry_limit = 0; /* Obsolete parameter */
1032 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1033 return srbcmd;
1034}
1035
1036static void aac_srb_callback(void *context, struct fib * fibptr);
1037
1038static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1039{
1040 u16 fibsize;
1041 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1042
1043 aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
1044 srbcmd->count = cpu_to_le32(cmd->request_bufflen);
1045
1046 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1047 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1048 /*
1049 * Build Scatter/Gather list
1050 */
1051 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1052 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1053 sizeof (struct sgentry64));
1054 BUG_ON (fibsize > (fib->dev->max_fib_size -
1055 sizeof(struct aac_fibhdr)));
1056
1057 /*
1058 * Now send the Fib to the adapter
1059 */
1060 return aac_fib_send(ScsiPortCommand64, fib,
1061 fibsize, FsaNormal, 0, 1,
1062 (fib_callback) aac_srb_callback,
1063 (void *) cmd);
1064}
1065
1066static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1067{
1068 u16 fibsize;
1069 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1070
1071 aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
1072 srbcmd->count = cpu_to_le32(cmd->request_bufflen);
1073
1074 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1075 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1076 /*
1077 * Build Scatter/Gather list
1078 */
1079 fibsize = sizeof (struct aac_srb) +
1080 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1081 sizeof (struct sgentry));
1082 BUG_ON (fibsize > (fib->dev->max_fib_size -
1083 sizeof(struct aac_fibhdr)));
1084
1085 /*
1086 * Now send the Fib to the adapter
1087 */
1088 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1089 (fib_callback) aac_srb_callback, (void *) cmd);
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092int aac_get_adapter_info(struct aac_dev* dev)
1093{
1094 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int rcode;
1096 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001097 struct aac_adapter_info *info;
1098 struct aac_bus_info *command;
1099 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001100
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001101 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -ENOMEM;
1103
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001104 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001105 info = (struct aac_adapter_info *) fib_data(fibptr);
1106 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001108 rcode = aac_fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001109 fibptr,
1110 sizeof(*info),
1111 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001112 -1, 1, /* First `interrupt' command uses special wait */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001113 NULL,
1114 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001116 if (rcode < 0) {
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001117 aac_fib_complete(fibptr);
1118 aac_fib_free(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001119 return rcode;
1120 }
1121 memcpy(&dev->adapter_info, info, sizeof(*info));
1122
1123 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1124 struct aac_supplement_adapter_info * info;
1125
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001126 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001127
1128 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1129
1130 memset(info,0,sizeof(*info));
1131
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001132 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001133 fibptr,
1134 sizeof(*info),
1135 FsaNormal,
1136 1, 1,
1137 NULL,
1138 NULL);
1139
1140 if (rcode >= 0)
1141 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Mark Haverkamp84971732005-06-20 11:55:24 -07001144
1145 /*
1146 * GetBusInfo
1147 */
1148
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001149 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001150
1151 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1152
1153 memset(bus_info, 0, sizeof(*bus_info));
1154
1155 command = (struct aac_bus_info *)bus_info;
1156
1157 command->Command = cpu_to_le32(VM_Ioctl);
1158 command->ObjType = cpu_to_le32(FT_DRIVE);
1159 command->MethodId = cpu_to_le32(1);
1160 command->CtlCmd = cpu_to_le32(GetBusInfo);
1161
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001162 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001163 fibptr,
1164 sizeof (*bus_info),
1165 FsaNormal,
1166 1, 1,
1167 NULL, NULL);
1168
1169 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1170 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1171 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1172 }
1173
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001174 if (!dev->in_reset) {
1175 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1176 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 dev->name,
1178 dev->id,
1179 tmp>>24,
1180 (tmp>>16)&0xff,
1181 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001182 le32_to_cpu(dev->adapter_info.kernelbuild),
1183 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1184 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001185 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1186 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dev->name, dev->id,
1188 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1189 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001190 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1191 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 dev->name, dev->id,
1193 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1194 le32_to_cpu(dev->adapter_info.biosbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001195 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
1196 printk(KERN_INFO "%s%d: serial %x\n",
1197 dev->name, dev->id,
1198 le32_to_cpu(dev->adapter_info.serial[0]));
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001199 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1200 printk(KERN_INFO "%s%d: TSID %.*s\n",
1201 dev->name, dev->id,
1202 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1203 dev->supplement_adapter_info.VpdInfo.Tsid);
1204 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 dev->nondasd_support = 0;
1208 dev->raid_scsi_mode = 0;
1209 if(dev->adapter_info.options & AAC_OPT_NONDASD){
1210 dev->nondasd_support = 1;
1211 }
1212
1213 /*
1214 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1215 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1216 * force nondasd support on. If we decide to allow the non-dasd flag
1217 * additional changes changes will have to be made to support
1218 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1219 * changed to support the new dev->raid_scsi_mode flag instead of
1220 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1221 * function aac_detect will have to be modified where it sets up the
1222 * max number of channels based on the aac->nondasd_support flag only.
1223 */
1224 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1225 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1226 dev->nondasd_support = 1;
1227 dev->raid_scsi_mode = 1;
1228 }
1229 if (dev->raid_scsi_mode != 0)
1230 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1231 dev->name, dev->id);
1232
1233 if(nondasd != -1) {
1234 dev->nondasd_support = (nondasd!=0);
1235 }
1236 if(dev->nondasd_support != 0){
1237 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1238 }
1239
1240 dev->dac_support = 0;
1241 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1242 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1243 dev->dac_support = 1;
1244 }
1245
1246 if(dacmode != -1) {
1247 dev->dac_support = (dacmode!=0);
1248 }
1249 if(dev->dac_support != 0) {
James Bottomley71e0f322005-10-28 11:21:10 -05001250 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1251 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1253 dev->name, dev->id);
James Bottomley71e0f322005-10-28 11:21:10 -05001254 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1255 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1257 dev->name, dev->id);
1258 dev->dac_support = 0;
1259 } else {
1260 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1261 dev->name, dev->id);
1262 rcode = -ENOMEM;
1263 }
1264 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001265 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001266 * Deal with configuring for the individualized limits of each packet
1267 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001268 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001269 dev->a_ops.adapter_scsi = (dev->dac_support)
1270 ? aac_scsi_64
1271 : aac_scsi_32;
1272 if (dev->raw_io_interface) {
1273 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1274 ? aac_bounds_64
1275 : aac_bounds_32;
1276 dev->a_ops.adapter_read = aac_read_raw_io;
1277 dev->a_ops.adapter_write = aac_write_raw_io;
1278 } else {
1279 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001280 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001281 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001282 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1283 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001284 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001285 dev->a_ops.adapter_read = aac_read_block64;
1286 dev->a_ops.adapter_write = aac_write_block64;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001287 /*
1288 * 38 scatter gather elements
1289 */
1290 dev->scsi_host_ptr->sg_tablesize =
1291 (dev->max_fib_size -
1292 sizeof(struct aac_fibhdr) -
1293 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001294 sizeof(struct sgentry64)) /
1295 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001296 } else {
1297 dev->a_ops.adapter_read = aac_read_block;
1298 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001299 }
1300 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1301 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1302 /*
1303 * Worst case size that could cause sg overflow when
1304 * we break up SG elements that are larger than 64KB.
1305 * Would be nice if we could tell the SCSI layer what
1306 * the maximum SG element size can be. Worst case is
1307 * (sg_tablesize-1) 4KB elements with one 64KB
1308 * element.
1309 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1310 */
1311 dev->scsi_host_ptr->max_sectors =
1312 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1313 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001316 aac_fib_complete(fibptr);
1317 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 return rcode;
1320}
1321
1322
Mark Haverkampe53cb352005-08-03 15:39:09 -07001323static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct aac_dev *dev;
1326 struct aac_read_reply *readreply;
1327 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 u32 cid;
1329
1330 scsicmd = (struct scsi_cmnd *) context;
1331
Mark Haverkamp03d44332007-03-15 10:27:45 -07001332 if (!aac_valid_context(scsicmd, fibptr))
1333 return;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08001336 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001338 if (nblank(dprintk(x))) {
1339 u64 lba;
1340 switch (scsicmd->cmnd[0]) {
1341 case WRITE_6:
1342 case READ_6:
1343 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1344 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1345 break;
1346 case WRITE_16:
1347 case READ_16:
1348 lba = ((u64)scsicmd->cmnd[2] << 56) |
1349 ((u64)scsicmd->cmnd[3] << 48) |
1350 ((u64)scsicmd->cmnd[4] << 40) |
1351 ((u64)scsicmd->cmnd[5] << 32) |
1352 ((u64)scsicmd->cmnd[6] << 24) |
1353 (scsicmd->cmnd[7] << 16) |
1354 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1355 break;
1356 case WRITE_12:
1357 case READ_12:
1358 lba = ((u64)scsicmd->cmnd[2] << 24) |
1359 (scsicmd->cmnd[3] << 16) |
1360 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1361 break;
1362 default:
1363 lba = ((u64)scsicmd->cmnd[2] << 24) |
1364 (scsicmd->cmnd[3] << 16) |
1365 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1366 break;
1367 }
1368 printk(KERN_DEBUG
1369 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1370 smp_processor_id(), (unsigned long long)lba, jiffies);
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001373 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 if(scsicmd->use_sg)
1376 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02001377 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 scsicmd->use_sg,
1379 scsicmd->sc_data_direction);
1380 else if(scsicmd->request_bufflen)
1381 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
1382 scsicmd->request_bufflen,
1383 scsicmd->sc_data_direction);
1384 readreply = (struct aac_read_reply *)fib_data(fibptr);
1385 if (le32_to_cpu(readreply->status) == ST_OK)
1386 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1387 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001388#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001389 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001390 le32_to_cpu(readreply->status));
1391#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1393 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1394 HARDWARE_ERROR,
1395 SENCODE_INTERNAL_TARGET_FAILURE,
1396 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1397 0, 0);
1398 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1399 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1400 ? sizeof(scsicmd->sense_buffer)
1401 : sizeof(dev->fsa_dev[cid].sense_data));
1402 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001403 aac_fib_complete(fibptr);
1404 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001406 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001409static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001411 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 u32 count;
1413 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 struct aac_dev *dev;
1415 struct fib * cmd_fibcontext;
1416
1417 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1418 /*
1419 * Get block address and transfer length
1420 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001421 switch (scsicmd->cmnd[0]) {
1422 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001423 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001425 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1426 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 count = scsicmd->cmnd[4];
1428
1429 if (count == 0)
1430 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001431 break;
1432 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001433 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001434
1435 lba = ((u64)scsicmd->cmnd[2] << 56) |
1436 ((u64)scsicmd->cmnd[3] << 48) |
1437 ((u64)scsicmd->cmnd[4] << 40) |
1438 ((u64)scsicmd->cmnd[5] << 32) |
1439 ((u64)scsicmd->cmnd[6] << 24) |
1440 (scsicmd->cmnd[7] << 16) |
1441 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1442 count = (scsicmd->cmnd[10] << 24) |
1443 (scsicmd->cmnd[11] << 16) |
1444 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1445 break;
1446 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001447 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001448
1449 lba = ((u64)scsicmd->cmnd[2] << 24) |
1450 (scsicmd->cmnd[3] << 16) |
1451 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1452 count = (scsicmd->cmnd[6] << 24) |
1453 (scsicmd->cmnd[7] << 16) |
1454 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1455 break;
1456 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001457 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001459 lba = ((u64)scsicmd->cmnd[2] << 24) |
1460 (scsicmd->cmnd[3] << 16) |
1461 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001465 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001466 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001467 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /*
1470 * Alocate and initialize a Fib
1471 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001472 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -1;
1474 }
1475
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001476 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /*
1479 * Check that the command queued to the controller
1480 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001481 if (status == -EINPROGRESS) {
1482 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001486 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * For some reason, the Fib didn't queue, return QUEUE_FULL
1489 */
1490 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001491 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001492 aac_fib_complete(cmd_fibcontext);
1493 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return 0;
1495}
1496
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001497static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001499 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 u32 count;
1501 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct aac_dev *dev;
1503 struct fib * cmd_fibcontext;
1504
1505 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1506 /*
1507 * Get block address and transfer length
1508 */
1509 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1510 {
1511 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1512 count = scsicmd->cmnd[4];
1513 if (count == 0)
1514 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001515 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001516 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001517
1518 lba = ((u64)scsicmd->cmnd[2] << 56) |
1519 ((u64)scsicmd->cmnd[3] << 48) |
1520 ((u64)scsicmd->cmnd[4] << 40) |
1521 ((u64)scsicmd->cmnd[5] << 32) |
1522 ((u64)scsicmd->cmnd[6] << 24) |
1523 (scsicmd->cmnd[7] << 16) |
1524 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1525 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1526 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1527 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001528 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001529
1530 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1531 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1532 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1533 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001535 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001536 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1538 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001539 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001541 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
1544 * Allocate and initialize a Fib then setup a BlockWrite command
1545 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001546 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 scsicmd->result = DID_ERROR << 16;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001548 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001552 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /*
1555 * Check that the command queued to the controller
1556 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001557 if (status == -EINPROGRESS) {
1558 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return 0;
1560 }
1561
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001562 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * For some reason, the Fib didn't queue, return QUEUE_FULL
1565 */
1566 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001567 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001569 aac_fib_complete(cmd_fibcontext);
1570 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572}
1573
1574static void synchronize_callback(void *context, struct fib *fibptr)
1575{
1576 struct aac_synchronize_reply *synchronizereply;
1577 struct scsi_cmnd *cmd;
1578
1579 cmd = context;
1580
Mark Haverkamp03d44332007-03-15 10:27:45 -07001581 if (!aac_valid_context(cmd, fibptr))
1582 return;
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1585 smp_processor_id(), jiffies));
1586 BUG_ON(fibptr == NULL);
1587
1588
1589 synchronizereply = fib_data(fibptr);
1590 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1591 cmd->result = DID_OK << 16 |
1592 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1593 else {
1594 struct scsi_device *sdev = cmd->device;
1595 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08001596 u32 cid = sdev_id(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 printk(KERN_WARNING
1598 "synchronize_callback: synchronize failed, status = %d\n",
1599 le32_to_cpu(synchronizereply->status));
1600 cmd->result = DID_OK << 16 |
1601 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1602 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1603 HARDWARE_ERROR,
1604 SENCODE_INTERNAL_TARGET_FAILURE,
1605 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1606 0, 0);
1607 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1608 min(sizeof(dev->fsa_dev[cid].sense_data),
1609 sizeof(cmd->sense_buffer)));
1610 }
1611
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001612 aac_fib_complete(fibptr);
1613 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001614 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001617static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 int status;
1620 struct fib *cmd_fibcontext;
1621 struct aac_synchronize *synchronizecmd;
1622 struct scsi_cmnd *cmd;
1623 struct scsi_device *sdev = scsicmd->device;
1624 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001625 struct aac_dev *aac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 unsigned long flags;
1627
1628 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001629 * Wait for all outstanding queued commands to complete to this
1630 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
1632 spin_lock_irqsave(&sdev->list_lock, flags);
1633 list_for_each_entry(cmd, &sdev->cmd_list, list)
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001634 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 ++active;
1636 break;
1637 }
1638
1639 spin_unlock_irqrestore(&sdev->list_lock, flags);
1640
1641 /*
1642 * Yield the processor (requeue for later)
1643 */
1644 if (active)
1645 return SCSI_MLQUEUE_DEVICE_BUSY;
1646
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001647 aac = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001648 if (aac->in_reset)
1649 return SCSI_MLQUEUE_HOST_BUSY;
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001652 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001654 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return SCSI_MLQUEUE_HOST_BUSY;
1656
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001657 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 synchronizecmd = fib_data(cmd_fibcontext);
1660 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1661 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001662 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 synchronizecmd->count =
1664 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1665
1666 /*
1667 * Now send the Fib to the adapter
1668 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001669 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 cmd_fibcontext,
1671 sizeof(struct aac_synchronize),
1672 FsaNormal,
1673 0, 1,
1674 (fib_callback)synchronize_callback,
1675 (void *)scsicmd);
1676
1677 /*
1678 * Check that the command queued to the controller
1679 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001680 if (status == -EINPROGRESS) {
1681 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001686 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1687 aac_fib_complete(cmd_fibcontext);
1688 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return SCSI_MLQUEUE_HOST_BUSY;
1690}
1691
1692/**
1693 * aac_scsi_cmd() - Process SCSI command
1694 * @scsicmd: SCSI command block
1695 *
1696 * Emulate a SCSI command and queue the required request for the
1697 * aacraid firmware.
1698 */
1699
1700int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1701{
1702 u32 cid = 0;
1703 struct Scsi_Host *host = scsicmd->device->host;
1704 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1705 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Mark Haverkamp90ee3462006-08-03 08:03:07 -07001707 if (fsa_dev_ptr == NULL)
1708 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /*
1710 * If the bus, id or lun is out of range, return fail
1711 * Test does not apply to ID 16, the pseudo id for the controller
1712 * itself.
1713 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001714 if (scmd_id(scsicmd) != host->this_id) {
Mark Haverkampe5718772006-03-27 09:43:25 -08001715 if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1716 if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1717 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 scsicmd->result = DID_NO_CONNECT << 16;
1719 scsicmd->scsi_done(scsicmd);
1720 return 0;
1721 }
Mark Haverkampe5718772006-03-27 09:43:25 -08001722 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /*
1725 * If the target container doesn't exist, it may have
1726 * been newly created
1727 */
1728 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1729 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001730 case SERVICE_ACTION_IN:
1731 if (!(dev->raw_io_interface) ||
1732 !(dev->raw_io_64) ||
1733 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1734 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 case INQUIRY:
1736 case READ_CAPACITY:
1737 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001738 if (dev->in_reset)
1739 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07001740 return _aac_probe_container(scsicmd,
1741 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 default:
1743 break;
1744 }
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 } else { /* check for physical non-dasd devices */
Mark Haverkamp653ba582006-09-19 08:59:43 -07001747 if ((dev->nondasd_support == 1) || expose_physicals) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001748 if (dev->in_reset)
1749 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return aac_send_srb_fib(scsicmd);
1751 } else {
1752 scsicmd->result = DID_NO_CONNECT << 16;
1753 scsicmd->scsi_done(scsicmd);
1754 return 0;
1755 }
1756 }
1757 }
1758 /*
1759 * else Command for the controller itself
1760 */
1761 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1762 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1763 {
1764 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1765 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1766 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1767 ILLEGAL_REQUEST,
1768 SENCODE_INVALID_COMMAND,
1769 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1770 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1771 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1772 ? sizeof(scsicmd->sense_buffer)
1773 : sizeof(dev->fsa_dev[cid].sense_data));
1774 scsicmd->scsi_done(scsicmd);
1775 return 0;
1776 }
1777
1778
1779 /* Handle commands here that don't really require going out to the adapter */
1780 switch (scsicmd->cmnd[0]) {
1781 case INQUIRY:
1782 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001783 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Mark Haverkampe5718772006-03-27 09:43:25 -08001785 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001786 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001788 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001789 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 */
1790 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001792 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 /*
1794 * Set the Vendor, Product, and Revision Level
1795 * see: <vendor>.c i.e. aac.c
1796 */
Jeff Garzik422c0d62005-10-24 18:05:09 -04001797 if (scmd_id(scsicmd) == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07001798 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001799 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1800 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1802 scsicmd->scsi_done(scsicmd);
1803 return 0;
1804 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001805 if (dev->in_reset)
1806 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07001807 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001808 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1809 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001810 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001812 case SERVICE_ACTION_IN:
1813 if (!(dev->raw_io_interface) ||
1814 !(dev->raw_io_64) ||
1815 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1816 break;
1817 {
1818 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001819 char cp[13];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001820
1821 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1822 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001823 cp[0] = (capacity >> 56) & 0xff;
1824 cp[1] = (capacity >> 48) & 0xff;
1825 cp[2] = (capacity >> 40) & 0xff;
1826 cp[3] = (capacity >> 32) & 0xff;
1827 cp[4] = (capacity >> 24) & 0xff;
1828 cp[5] = (capacity >> 16) & 0xff;
1829 cp[6] = (capacity >> 8) & 0xff;
1830 cp[7] = (capacity >> 0) & 0xff;
1831 cp[8] = 0;
1832 cp[9] = 0;
1833 cp[10] = 2;
1834 cp[11] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001835 cp[12] = 0;
1836 aac_internal_transfer(scsicmd, cp, 0,
Mark Haverkamp1241f352006-03-27 09:44:23 -08001837 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001838 if (sizeof(cp) < scsicmd->cmnd[13]) {
1839 unsigned int len, offset = sizeof(cp);
1840
1841 memset(cp, 0, offset);
1842 do {
Mark Haverkamp1241f352006-03-27 09:44:23 -08001843 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1844 sizeof(cp));
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08001845 aac_internal_transfer(scsicmd, cp, offset, len);
1846 } while ((offset += len) < scsicmd->cmnd[13]);
1847 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001848
1849 /* Do not cache partition table for arrays */
1850 scsicmd->device->removable = 1;
1851
1852 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1853 scsicmd->scsi_done(scsicmd);
1854
1855 return 0;
1856 }
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 case READ_CAPACITY:
1859 {
1860 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001861 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001864 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 capacity = fsa_dev_ptr[cid].size - 1;
1866 else
1867 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 cp[0] = (capacity >> 24) & 0xff;
1870 cp[1] = (capacity >> 16) & 0xff;
1871 cp[2] = (capacity >> 8) & 0xff;
1872 cp[3] = (capacity >> 0) & 0xff;
1873 cp[4] = 0;
1874 cp[5] = 0;
1875 cp[6] = 2;
1876 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001877 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001878 /* Do not cache partition table for arrays */
1879 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1882 scsicmd->scsi_done(scsicmd);
1883
1884 return 0;
1885 }
1886
1887 case MODE_SENSE:
1888 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001889 char mode_buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 mode_buf[0] = 3; /* Mode data length */
1893 mode_buf[1] = 0; /* Medium type - default */
1894 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1895 mode_buf[3] = 0; /* Block descriptor length */
1896
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001897 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1899 scsicmd->scsi_done(scsicmd);
1900
1901 return 0;
1902 }
1903 case MODE_SENSE_10:
1904 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001905 char mode_buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 mode_buf[0] = 0; /* Mode data length (MSB) */
1909 mode_buf[1] = 6; /* Mode data length (LSB) */
1910 mode_buf[2] = 0; /* Medium type - default */
1911 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1912 mode_buf[4] = 0; /* reserved */
1913 mode_buf[5] = 0; /* reserved */
1914 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1915 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001916 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1919 scsicmd->scsi_done(scsicmd);
1920
1921 return 0;
1922 }
1923 case REQUEST_SENSE:
1924 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1925 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1926 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1927 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1928 scsicmd->scsi_done(scsicmd);
1929 return 0;
1930
1931 case ALLOW_MEDIUM_REMOVAL:
1932 dprintk((KERN_DEBUG "LOCK command.\n"));
1933 if (scsicmd->cmnd[4])
1934 fsa_dev_ptr[cid].locked = 1;
1935 else
1936 fsa_dev_ptr[cid].locked = 0;
1937
1938 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1939 scsicmd->scsi_done(scsicmd);
1940 return 0;
1941 /*
1942 * These commands are all No-Ops
1943 */
1944 case TEST_UNIT_READY:
1945 case RESERVE:
1946 case RELEASE:
1947 case REZERO_UNIT:
1948 case REASSIGN_BLOCKS:
1949 case SEEK_10:
1950 case START_STOP:
1951 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1952 scsicmd->scsi_done(scsicmd);
1953 return 0;
1954 }
1955
1956 switch (scsicmd->cmnd[0])
1957 {
1958 case READ_6:
1959 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001960 case READ_12:
1961 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001962 if (dev->in_reset)
1963 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /*
1965 * Hack to keep track of ordinal number of the device that
1966 * corresponds to a container. Needed to convert
1967 * containers to /dev/sd device names
1968 */
1969
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001970 if (scsicmd->request->rq_disk)
1971 strlcpy(fsa_dev_ptr[cid].devname,
1972 scsicmd->request->rq_disk->disk_name,
1973 min(sizeof(fsa_dev_ptr[cid].devname),
1974 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001975
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001976 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 case WRITE_6:
1979 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001980 case WRITE_12:
1981 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001982 if (dev->in_reset)
1983 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001984 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 case SYNCHRONIZE_CACHE:
1987 /* Issue FIB to tell Firmware to flush it's cache */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001988 return aac_synchronize(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 default:
1991 /*
1992 * Unhandled commands
1993 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001994 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1996 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1997 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1998 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1999 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2000 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
2001 ? sizeof(scsicmd->sense_buffer)
2002 : sizeof(dev->fsa_dev[cid].sense_data));
2003 scsicmd->scsi_done(scsicmd);
2004 return 0;
2005 }
2006}
2007
2008static int query_disk(struct aac_dev *dev, void __user *arg)
2009{
2010 struct aac_query_disk qd;
2011 struct fsa_dev_info *fsa_dev_ptr;
2012
2013 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002014 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002015 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2017 return -EFAULT;
2018 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002019 qd.cnum = qd.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2021 {
2022 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2023 return -EINVAL;
2024 qd.instance = dev->scsi_host_ptr->host_no;
2025 qd.bus = 0;
2026 qd.id = CONTAINER_TO_ID(qd.cnum);
2027 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2028 }
2029 else return -EINVAL;
2030
2031 qd.valid = fsa_dev_ptr[qd.cnum].valid;
2032 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2033 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2034
2035 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2036 qd.unmapped = 1;
2037 else
2038 qd.unmapped = 0;
2039
2040 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2041 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2042
2043 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2044 return -EFAULT;
2045 return 0;
2046}
2047
2048static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2049{
2050 struct aac_delete_disk dd;
2051 struct fsa_dev_info *fsa_dev_ptr;
2052
2053 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp651013552006-09-19 08:59:23 -07002054 if (!fsa_dev_ptr)
2055 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2058 return -EFAULT;
2059
2060 if (dd.cnum >= dev->maximum_num_containers)
2061 return -EINVAL;
2062 /*
2063 * Mark this container as being deleted.
2064 */
2065 fsa_dev_ptr[dd.cnum].deleted = 1;
2066 /*
2067 * Mark the container as no longer valid
2068 */
2069 fsa_dev_ptr[dd.cnum].valid = 0;
2070 return 0;
2071}
2072
2073static int delete_disk(struct aac_dev *dev, void __user *arg)
2074{
2075 struct aac_delete_disk dd;
2076 struct fsa_dev_info *fsa_dev_ptr;
2077
2078 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002079 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002080 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2083 return -EFAULT;
2084
2085 if (dd.cnum >= dev->maximum_num_containers)
2086 return -EINVAL;
2087 /*
2088 * If the container is locked, it can not be deleted by the API.
2089 */
2090 if (fsa_dev_ptr[dd.cnum].locked)
2091 return -EBUSY;
2092 else {
2093 /*
2094 * Mark the container as no longer being valid.
2095 */
2096 fsa_dev_ptr[dd.cnum].valid = 0;
2097 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2098 return 0;
2099 }
2100}
2101
2102int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2103{
2104 switch (cmd) {
2105 case FSACTL_QUERY_DISK:
2106 return query_disk(dev, arg);
2107 case FSACTL_DELETE_DISK:
2108 return delete_disk(dev, arg);
2109 case FSACTL_FORCE_DELETE_DISK:
2110 return force_delete_disk(dev, arg);
2111 case FSACTL_GET_CONTAINERS:
2112 return aac_get_containers(dev);
2113 default:
2114 return -ENOTTY;
2115 }
2116}
2117
2118/**
2119 *
2120 * aac_srb_callback
2121 * @context: the context set in the fib - here it is scsi cmd
2122 * @fibptr: pointer to the fib
2123 *
2124 * Handles the completion of a scsi command to a non dasd device
2125 *
2126 */
2127
2128static void aac_srb_callback(void *context, struct fib * fibptr)
2129{
2130 struct aac_dev *dev;
2131 struct aac_srb_reply *srbreply;
2132 struct scsi_cmnd *scsicmd;
2133
2134 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002135
2136 if (!aac_valid_context(scsicmd, fibptr))
2137 return;
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2140
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002141 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2144
2145 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
2146 /*
2147 * Calculate resid for sg
2148 */
2149
2150 scsicmd->resid = scsicmd->request_bufflen -
2151 le32_to_cpu(srbreply->data_xfer_length);
2152
2153 if(scsicmd->use_sg)
2154 pci_unmap_sg(dev->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02002155 (struct scatterlist *)scsicmd->request_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 scsicmd->use_sg,
2157 scsicmd->sc_data_direction);
2158 else if(scsicmd->request_bufflen)
2159 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
2160 scsicmd->sc_data_direction);
2161
2162 /*
2163 * First check the fib status
2164 */
2165
2166 if (le32_to_cpu(srbreply->status) != ST_OK){
2167 int len;
2168 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2169 len = (le32_to_cpu(srbreply->sense_data_size) >
2170 sizeof(scsicmd->sense_buffer)) ?
2171 sizeof(scsicmd->sense_buffer) :
2172 le32_to_cpu(srbreply->sense_data_size);
2173 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2174 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2175 }
2176
2177 /*
2178 * Next check the srb status
2179 */
2180 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2181 case SRB_STATUS_ERROR_RECOVERY:
2182 case SRB_STATUS_PENDING:
2183 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08002184 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 break;
2186 case SRB_STATUS_DATA_OVERRUN:
2187 switch(scsicmd->cmnd[0]){
2188 case READ_6:
2189 case WRITE_6:
2190 case READ_10:
2191 case WRITE_10:
2192 case READ_12:
2193 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002194 case READ_16:
2195 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2197 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2198 } else {
2199 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2200 }
2201 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2202 break;
2203 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08002204 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 break;
2206 }
2207 default:
2208 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2209 break;
2210 }
2211 break;
2212 case SRB_STATUS_ABORTED:
2213 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2214 break;
2215 case SRB_STATUS_ABORT_FAILED:
2216 // Not sure about this one - but assuming the hba was trying to abort for some reason
2217 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2218 break;
2219 case SRB_STATUS_PARITY_ERROR:
2220 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2221 break;
2222 case SRB_STATUS_NO_DEVICE:
2223 case SRB_STATUS_INVALID_PATH_ID:
2224 case SRB_STATUS_INVALID_TARGET_ID:
2225 case SRB_STATUS_INVALID_LUN:
2226 case SRB_STATUS_SELECTION_TIMEOUT:
2227 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2228 break;
2229
2230 case SRB_STATUS_COMMAND_TIMEOUT:
2231 case SRB_STATUS_TIMEOUT:
2232 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2233 break;
2234
2235 case SRB_STATUS_BUSY:
2236 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2237 break;
2238
2239 case SRB_STATUS_BUS_RESET:
2240 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2241 break;
2242
2243 case SRB_STATUS_MESSAGE_REJECTED:
2244 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2245 break;
2246 case SRB_STATUS_REQUEST_FLUSHED:
2247 case SRB_STATUS_ERROR:
2248 case SRB_STATUS_INVALID_REQUEST:
2249 case SRB_STATUS_REQUEST_SENSE_FAILED:
2250 case SRB_STATUS_NO_HBA:
2251 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2252 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2253 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2254 case SRB_STATUS_DELAYED_RETRY:
2255 case SRB_STATUS_BAD_FUNCTION:
2256 case SRB_STATUS_NOT_STARTED:
2257 case SRB_STATUS_NOT_IN_USE:
2258 case SRB_STATUS_FORCE_ABORT:
2259 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2260 default:
2261#ifdef AAC_DETAILED_STATUS_INFO
2262 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2263 le32_to_cpu(srbreply->srb_status) & 0x3F,
2264 aac_get_status_string(
2265 le32_to_cpu(srbreply->srb_status) & 0x3F),
2266 scsicmd->cmnd[0],
2267 le32_to_cpu(srbreply->scsi_status));
2268#endif
2269 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2270 break;
2271 }
2272 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2273 int len;
2274 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2275 len = (le32_to_cpu(srbreply->sense_data_size) >
2276 sizeof(scsicmd->sense_buffer)) ?
2277 sizeof(scsicmd->sense_buffer) :
2278 le32_to_cpu(srbreply->sense_data_size);
2279#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002280 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2281 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282#endif
2283 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2284
2285 }
2286 /*
2287 * OR in the scsi status (already shifted up a bit)
2288 */
2289 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2290
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002291 aac_fib_complete(fibptr);
2292 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002293 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296/**
2297 *
2298 * aac_send_scb_fib
2299 * @scsicmd: the scsi command block
2300 *
2301 * This routine will form a FIB and fill in the aac_srb from the
2302 * scsicmd passed in.
2303 */
2304
2305static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2306{
2307 struct fib* cmd_fibcontext;
2308 struct aac_dev* dev;
2309 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Mark Haverkamp84971732005-06-20 11:55:24 -07002311 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08002312 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07002313 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 scsicmd->result = DID_NO_CONNECT << 16;
2315 scsicmd->scsi_done(scsicmd);
2316 return 0;
2317 }
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 /*
2320 * Allocate and initialize a Fib then setup a BlockWrite command
2321 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002322 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return -1;
2324 }
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002325 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /*
2328 * Check that the command queued to the controller
2329 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002330 if (status == -EINPROGRESS) {
2331 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333 }
2334
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002335 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2336 aac_fib_complete(cmd_fibcontext);
2337 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 return -1;
2340}
2341
2342static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2343{
2344 struct aac_dev *dev;
2345 unsigned long byte_count = 0;
2346
2347 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2348 // Get rid of old data
2349 psg->count = 0;
2350 psg->sg[0].addr = 0;
2351 psg->sg[0].count = 0;
2352 if (scsicmd->use_sg) {
2353 struct scatterlist *sg;
2354 int i;
2355 int sg_count;
2356 sg = (struct scatterlist *) scsicmd->request_buffer;
2357
2358 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2359 scsicmd->sc_data_direction);
2360 psg->count = cpu_to_le32(sg_count);
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 for (i = 0; i < sg_count; i++) {
2363 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2364 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2365 byte_count += sg_dma_len(sg);
2366 sg++;
2367 }
2368 /* hba wants the size to be exact */
2369 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002370 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2371 (byte_count - scsicmd->request_bufflen);
2372 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 byte_count = scsicmd->request_bufflen;
2374 }
2375 /* Check for command underflow */
2376 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2377 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2378 byte_count, scsicmd->underflow);
2379 }
2380 }
2381 else if(scsicmd->request_bufflen) {
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002382 u32 addr;
2383 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 scsicmd->request_buffer,
2385 scsicmd->request_bufflen,
2386 scsicmd->sc_data_direction);
Mark Haverkamp3c1e0cc2006-05-10 09:12:05 -07002387 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 psg->count = cpu_to_le32(1);
2389 psg->sg[0].addr = cpu_to_le32(addr);
2390 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 byte_count = scsicmd->request_bufflen;
2392 }
2393 return byte_count;
2394}
2395
2396
2397static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2398{
2399 struct aac_dev *dev;
2400 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002401 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2404 // Get rid of old data
2405 psg->count = 0;
2406 psg->sg[0].addr[0] = 0;
2407 psg->sg[0].addr[1] = 0;
2408 psg->sg[0].count = 0;
2409 if (scsicmd->use_sg) {
2410 struct scatterlist *sg;
2411 int i;
2412 int sg_count;
2413 sg = (struct scatterlist *) scsicmd->request_buffer;
2414
2415 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2416 scsicmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 for (i = 0; i < sg_count; i++) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002419 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002420 addr = sg_dma_address(sg);
2421 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2422 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002423 psg->sg[i].count = cpu_to_le32(count);
2424 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 sg++;
2426 }
Mark Haverkamp1241f352006-03-27 09:44:23 -08002427 psg->count = cpu_to_le32(sg_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /* hba wants the size to be exact */
2429 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002430 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2431 (byte_count - scsicmd->request_bufflen);
2432 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 byte_count = scsicmd->request_bufflen;
2434 }
2435 /* Check for command underflow */
2436 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2437 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2438 byte_count, scsicmd->underflow);
2439 }
2440 }
2441 else if(scsicmd->request_bufflen) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08002442 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 scsicmd->request_buffer,
2444 scsicmd->request_bufflen,
2445 scsicmd->sc_data_direction);
Mark Haverkamp1241f352006-03-27 09:44:23 -08002446 addr = scsicmd->SCp.dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 psg->count = cpu_to_le32(1);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002448 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2449 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 byte_count = scsicmd->request_bufflen;
2452 }
2453 return byte_count;
2454}
2455
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002456static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2457{
2458 struct Scsi_Host *host = scsicmd->device->host;
2459 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2460 unsigned long byte_count = 0;
2461
2462 // Get rid of old data
2463 psg->count = 0;
2464 psg->sg[0].next = 0;
2465 psg->sg[0].prev = 0;
2466 psg->sg[0].addr[0] = 0;
2467 psg->sg[0].addr[1] = 0;
2468 psg->sg[0].count = 0;
2469 psg->sg[0].flags = 0;
2470 if (scsicmd->use_sg) {
2471 struct scatterlist *sg;
2472 int i;
2473 int sg_count;
2474 sg = (struct scatterlist *) scsicmd->request_buffer;
2475
2476 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2477 scsicmd->sc_data_direction);
2478
2479 for (i = 0; i < sg_count; i++) {
2480 int count = sg_dma_len(sg);
2481 u64 addr = sg_dma_address(sg);
2482 psg->sg[i].next = 0;
2483 psg->sg[i].prev = 0;
2484 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2485 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2486 psg->sg[i].count = cpu_to_le32(count);
2487 psg->sg[i].flags = 0;
2488 byte_count += count;
2489 sg++;
2490 }
2491 psg->count = cpu_to_le32(sg_count);
2492 /* hba wants the size to be exact */
2493 if(byte_count > scsicmd->request_bufflen){
2494 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2495 (byte_count - scsicmd->request_bufflen);
2496 psg->sg[i-1].count = cpu_to_le32(temp);
2497 byte_count = scsicmd->request_bufflen;
2498 }
2499 /* Check for command underflow */
2500 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2501 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2502 byte_count, scsicmd->underflow);
2503 }
2504 }
2505 else if(scsicmd->request_bufflen) {
2506 int count;
2507 u64 addr;
2508 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2509 scsicmd->request_buffer,
2510 scsicmd->request_bufflen,
2511 scsicmd->sc_data_direction);
2512 addr = scsicmd->SCp.dma_handle;
2513 count = scsicmd->request_bufflen;
2514 psg->count = cpu_to_le32(1);
2515 psg->sg[0].next = 0;
2516 psg->sg[0].prev = 0;
2517 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2518 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2519 psg->sg[0].count = cpu_to_le32(count);
2520 psg->sg[0].flags = 0;
2521 byte_count = scsicmd->request_bufflen;
2522 }
2523 return byte_count;
2524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526#ifdef AAC_DETAILED_STATUS_INFO
2527
2528struct aac_srb_status_info {
2529 u32 status;
2530 char *str;
2531};
2532
2533
2534static struct aac_srb_status_info srb_status_info[] = {
2535 { SRB_STATUS_PENDING, "Pending Status"},
2536 { SRB_STATUS_SUCCESS, "Success"},
2537 { SRB_STATUS_ABORTED, "Aborted Command"},
2538 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002539 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 { SRB_STATUS_BUSY, "Device Busy"},
2541 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2542 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2543 { SRB_STATUS_NO_DEVICE, "No Device"},
2544 { SRB_STATUS_TIMEOUT, "Timeout"},
2545 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2546 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2547 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2548 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2549 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2550 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2551 { SRB_STATUS_NO_HBA, "No HBA"},
2552 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2553 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2554 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2555 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2556 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2557 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07002558 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2560 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2561 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2562 { SRB_STATUS_NOT_STARTED, "Not Started"},
2563 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2564 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2565 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2566 { 0xff, "Unknown Error"}
2567};
2568
2569char *aac_get_status_string(u32 status)
2570{
2571 int i;
2572
Tobias Klauser6391a112006-06-08 22:23:48 -07002573 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2574 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 return "Bad Status Code";
2578}
2579
2580#endif