blob: 0a209b2cd695b5c2cc74a16486e33c942f7d6e18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/types.h>
29#include <linux/sched.h>
30#include <linux/pci.h>
31#include <linux/spinlock.h>
32#include <linux/slab.h>
33#include <linux/completion.h>
34#include <linux/blkdev.h>
35#include <asm/semaphore.h>
36#include <asm/uaccess.h>
37
38#include <scsi/scsi.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42
43#include "aacraid.h"
44
45/* values for inqd_pdt: Peripheral device type in plain English */
46#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
47#define INQD_PDT_PROC 0x03 /* Processor device */
48#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
49#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
50#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
51#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
52
53#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
54#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Sense codes
58 */
59
60#define SENCODE_NO_SENSE 0x00
61#define SENCODE_END_OF_DATA 0x00
62#define SENCODE_BECOMING_READY 0x04
63#define SENCODE_INIT_CMD_REQUIRED 0x04
64#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
65#define SENCODE_INVALID_COMMAND 0x20
66#define SENCODE_LBA_OUT_OF_RANGE 0x21
67#define SENCODE_INVALID_CDB_FIELD 0x24
68#define SENCODE_LUN_NOT_SUPPORTED 0x25
69#define SENCODE_INVALID_PARAM_FIELD 0x26
70#define SENCODE_PARAM_NOT_SUPPORTED 0x26
71#define SENCODE_PARAM_VALUE_INVALID 0x26
72#define SENCODE_RESET_OCCURRED 0x29
73#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
74#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
75#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
76#define SENCODE_DIAGNOSTIC_FAILURE 0x40
77#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
78#define SENCODE_INVALID_MESSAGE_ERROR 0x49
79#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
80#define SENCODE_OVERLAPPED_COMMAND 0x4E
81
82/*
83 * Additional sense codes
84 */
85
86#define ASENCODE_NO_SENSE 0x00
87#define ASENCODE_END_OF_DATA 0x05
88#define ASENCODE_BECOMING_READY 0x01
89#define ASENCODE_INIT_CMD_REQUIRED 0x02
90#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
91#define ASENCODE_INVALID_COMMAND 0x00
92#define ASENCODE_LBA_OUT_OF_RANGE 0x00
93#define ASENCODE_INVALID_CDB_FIELD 0x00
94#define ASENCODE_LUN_NOT_SUPPORTED 0x00
95#define ASENCODE_INVALID_PARAM_FIELD 0x00
96#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
97#define ASENCODE_PARAM_VALUE_INVALID 0x02
98#define ASENCODE_RESET_OCCURRED 0x00
99#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
100#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
101#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
102#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
103#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
104#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
105#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
106#define ASENCODE_OVERLAPPED_COMMAND 0x00
107
108#define BYTE0(x) (unsigned char)(x)
109#define BYTE1(x) (unsigned char)((x) >> 8)
110#define BYTE2(x) (unsigned char)((x) >> 16)
111#define BYTE3(x) (unsigned char)((x) >> 24)
112
113/*------------------------------------------------------------------------------
114 * S T R U C T S / T Y P E D E F S
115 *----------------------------------------------------------------------------*/
116/* SCSI inquiry data */
117struct inquiry_data {
118 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
119 u8 inqd_dtq; /* RMB | Device Type Qualifier */
120 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
121 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
122 u8 inqd_len; /* Additional length (n-4) */
123 u8 inqd_pad1[2];/* Reserved - must be zero */
124 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
125 u8 inqd_vid[8]; /* Vendor ID */
126 u8 inqd_pid[16];/* Product ID */
127 u8 inqd_prl[4]; /* Product Revision Level */
128};
129
130/*
131 * M O D U L E G L O B A L S
132 */
133
134static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
135static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700136static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
138#ifdef AAC_DETAILED_STATUS_INFO
139static char *aac_get_status_string(u32 status);
140#endif
141
142/*
143 * Non dasd selection is handled entirely in aachba now
144 */
145
146static int nondasd = -1;
147static int dacmode = -1;
148
149static int commit = -1;
150
151module_param(nondasd, int, 0);
152MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
153module_param(dacmode, int, 0);
154MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
155module_param(commit, int, 0);
156MODULE_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");
157
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700158int numacb = -1;
159module_param(numacb, int, S_IRUGO|S_IWUSR);
160MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid\nvalues are 512 and down. Default is to use suggestion from Firmware.");
161
162int acbsize = -1;
163module_param(acbsize, int, S_IRUGO|S_IWUSR);
164MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512,\n2048, 4096 and 8192. Default is to use suggestion from Firmware.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/**
166 * aac_get_config_status - check the adapter configuration
167 * @common: adapter to query
168 *
169 * Query config status, and commit the configuration if needed.
170 */
171int aac_get_config_status(struct aac_dev *dev)
172{
173 int status = 0;
174 struct fib * fibptr;
175
176 if (!(fibptr = fib_alloc(dev)))
177 return -ENOMEM;
178
179 fib_init(fibptr);
180 {
181 struct aac_get_config_status *dinfo;
182 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
183
184 dinfo->command = cpu_to_le32(VM_ContainerConfig);
185 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
186 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
187 }
188
189 status = fib_send(ContainerCommand,
190 fibptr,
191 sizeof (struct aac_get_config_status),
192 FsaNormal,
193 1, 1,
194 NULL, NULL);
195 if (status < 0 ) {
196 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
197 } else {
198 struct aac_get_config_status_resp *reply
199 = (struct aac_get_config_status_resp *) fib_data(fibptr);
200 dprintk((KERN_WARNING
201 "aac_get_config_status: response=%d status=%d action=%d\n",
202 le32_to_cpu(reply->response),
203 le32_to_cpu(reply->status),
204 le32_to_cpu(reply->data.action)));
205 if ((le32_to_cpu(reply->response) != ST_OK) ||
206 (le32_to_cpu(reply->status) != CT_OK) ||
207 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
208 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
209 status = -EINVAL;
210 }
211 }
212 fib_complete(fibptr);
213 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
214 if (status >= 0) {
215 if (commit == 1) {
216 struct aac_commit_config * dinfo;
217 fib_init(fibptr);
218 dinfo = (struct aac_commit_config *) fib_data(fibptr);
219
220 dinfo->command = cpu_to_le32(VM_ContainerConfig);
221 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
222
223 status = fib_send(ContainerCommand,
224 fibptr,
225 sizeof (struct aac_commit_config),
226 FsaNormal,
227 1, 1,
228 NULL, NULL);
229 fib_complete(fibptr);
230 } else if (commit == 0) {
231 printk(KERN_WARNING
232 "aac_get_config_status: Foreign device configurations are being ignored\n");
233 }
234 }
235 fib_free(fibptr);
236 return status;
237}
238
239/**
240 * aac_get_containers - list containers
241 * @common: adapter to probe
242 *
243 * Make a list of all containers on this controller
244 */
245int aac_get_containers(struct aac_dev *dev)
246{
247 struct fsa_dev_info *fsa_dev_ptr;
248 u32 index;
249 int status = 0;
250 struct fib * fibptr;
251 unsigned instance;
252 struct aac_get_container_count *dinfo;
253 struct aac_get_container_count_resp *dresp;
254 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
255
256 instance = dev->scsi_host_ptr->unique_id;
257
258 if (!(fibptr = fib_alloc(dev)))
259 return -ENOMEM;
260
261 fib_init(fibptr);
262 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
263 dinfo->command = cpu_to_le32(VM_ContainerConfig);
264 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
265
266 status = fib_send(ContainerCommand,
267 fibptr,
268 sizeof (struct aac_get_container_count),
269 FsaNormal,
270 1, 1,
271 NULL, NULL);
272 if (status >= 0) {
273 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
274 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
275 fib_complete(fibptr);
276 }
277
278 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
279 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
281 sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
282 if (!fsa_dev_ptr) {
283 fib_free(fibptr);
284 return -ENOMEM;
285 }
286 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
287
288 dev->fsa_dev = fsa_dev_ptr;
289 dev->maximum_num_containers = maximum_num_containers;
290
291 for (index = 0; index < dev->maximum_num_containers; index++) {
292 struct aac_query_mount *dinfo;
293 struct aac_mount *dresp;
294
295 fsa_dev_ptr[index].devname[0] = '\0';
296
297 fib_init(fibptr);
298 dinfo = (struct aac_query_mount *) fib_data(fibptr);
299
300 dinfo->command = cpu_to_le32(VM_NameServe);
301 dinfo->count = cpu_to_le32(index);
302 dinfo->type = cpu_to_le32(FT_FILESYS);
303
304 status = fib_send(ContainerCommand,
305 fibptr,
306 sizeof (struct aac_query_mount),
307 FsaNormal,
308 1, 1,
309 NULL, NULL);
310 if (status < 0 ) {
311 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
312 break;
313 }
314 dresp = (struct aac_mount *)fib_data(fibptr);
315
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700316 if ((le32_to_cpu(dresp->status) == ST_OK) &&
317 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
318 dinfo->command = cpu_to_le32(VM_NameServe64);
319 dinfo->count = cpu_to_le32(index);
320 dinfo->type = cpu_to_le32(FT_FILESYS);
321
322 if (fib_send(ContainerCommand,
323 fibptr,
324 sizeof(struct aac_query_mount),
325 FsaNormal,
326 1, 1,
327 NULL, NULL) < 0)
328 continue;
329 } else
330 dresp->mnt[0].capacityhigh = 0;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 dprintk ((KERN_DEBUG
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700333 "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (int)index, (int)le32_to_cpu(dresp->status),
335 (int)le32_to_cpu(dresp->mnt[0].vol),
336 (int)le32_to_cpu(dresp->mnt[0].state),
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700337 ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
338 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if ((le32_to_cpu(dresp->status) == ST_OK) &&
340 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
341 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
342 fsa_dev_ptr[index].valid = 1;
343 fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700344 fsa_dev_ptr[index].size
345 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
346 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
348 fsa_dev_ptr[index].ro = 1;
349 }
350 fib_complete(fibptr);
351 /*
352 * If there are no more containers, then stop asking.
353 */
354 if ((index + 1) >= le32_to_cpu(dresp->count)){
355 break;
356 }
357 }
358 fib_free(fibptr);
359 return status;
360}
361
362static void aac_io_done(struct scsi_cmnd * scsicmd)
363{
364 unsigned long cpu_flags;
365 struct Scsi_Host *host = scsicmd->device->host;
366 spin_lock_irqsave(host->host_lock, cpu_flags);
367 scsicmd->scsi_done(scsicmd);
368 spin_unlock_irqrestore(host->host_lock, cpu_flags);
369}
370
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700371static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
372{
373 void *buf;
374 unsigned int transfer_len;
375 struct scatterlist *sg = scsicmd->request_buffer;
376
377 if (scsicmd->use_sg) {
378 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
379 transfer_len = min(sg->length, len + offset);
380 } else {
381 buf = scsicmd->request_buffer;
382 transfer_len = min(scsicmd->request_bufflen, len + offset);
383 }
384
385 memcpy(buf + offset, data, transfer_len - offset);
386
387 if (scsicmd->use_sg)
388 kunmap_atomic(buf - sg->offset, KM_IRQ0);
389
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static void get_container_name_callback(void *context, struct fib * fibptr)
393{
394 struct aac_get_name_resp * get_name_reply;
395 struct scsi_cmnd * scsicmd;
396
397 scsicmd = (struct scsi_cmnd *) context;
398
399 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
400 if (fibptr == NULL)
401 BUG();
402
403 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
404 /* Failure is irrelevant, using default value instead */
405 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
406 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700407 char *sp = get_name_reply->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
409 while (*sp == ' ')
410 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700411 if (*sp) {
412 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
413 int count = sizeof(d);
414 char *dp = d;
415 do {
416 *dp++ = (*sp) ? *sp++ : ' ';
417 } while (--count > 0);
418 aac_internal_transfer(scsicmd, d,
419 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
424
425 fib_complete(fibptr);
426 fib_free(fibptr);
427 aac_io_done(scsicmd);
428}
429
430/**
431 * aac_get_container_name - get container name, none blocking.
432 */
433static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
434{
435 int status;
436 struct aac_get_name *dinfo;
437 struct fib * cmd_fibcontext;
438 struct aac_dev * dev;
439
440 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
441
442 if (!(cmd_fibcontext = fib_alloc(dev)))
443 return -ENOMEM;
444
445 fib_init(cmd_fibcontext);
446 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
447
448 dinfo->command = cpu_to_le32(VM_ContainerConfig);
449 dinfo->type = cpu_to_le32(CT_READ_NAME);
450 dinfo->cid = cpu_to_le32(cid);
451 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
452
453 status = fib_send(ContainerCommand,
454 cmd_fibcontext,
455 sizeof (struct aac_get_name),
456 FsaNormal,
457 0, 1,
458 (fib_callback) get_container_name_callback,
459 (void *) scsicmd);
460
461 /*
462 * Check that the command queued to the controller
463 */
464 if (status == -EINPROGRESS)
465 return 0;
466
467 printk(KERN_WARNING "aac_get_container_name: fib_send failed with status: %d.\n", status);
468 fib_complete(cmd_fibcontext);
469 fib_free(cmd_fibcontext);
470 return -1;
471}
472
473/**
474 * probe_container - query a logical volume
475 * @dev: device to query
476 * @cid: container identifier
477 *
478 * Queries the controller about the given volume. The volume information
479 * is updated in the struct fsa_dev_info structure rather than returned.
480 */
481
482static int probe_container(struct aac_dev *dev, int cid)
483{
484 struct fsa_dev_info *fsa_dev_ptr;
485 int status;
486 struct aac_query_mount *dinfo;
487 struct aac_mount *dresp;
488 struct fib * fibptr;
489 unsigned instance;
490
491 fsa_dev_ptr = dev->fsa_dev;
492 instance = dev->scsi_host_ptr->unique_id;
493
494 if (!(fibptr = fib_alloc(dev)))
495 return -ENOMEM;
496
497 fib_init(fibptr);
498
499 dinfo = (struct aac_query_mount *)fib_data(fibptr);
500
501 dinfo->command = cpu_to_le32(VM_NameServe);
502 dinfo->count = cpu_to_le32(cid);
503 dinfo->type = cpu_to_le32(FT_FILESYS);
504
505 status = fib_send(ContainerCommand,
506 fibptr,
507 sizeof(struct aac_query_mount),
508 FsaNormal,
509 1, 1,
510 NULL, NULL);
511 if (status < 0) {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700512 printk(KERN_WARNING "aacraid: probe_container query failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto error;
514 }
515
516 dresp = (struct aac_mount *) fib_data(fibptr);
517
518 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700519 (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
520 dinfo->command = cpu_to_le32(VM_NameServe64);
521 dinfo->count = cpu_to_le32(cid);
522 dinfo->type = cpu_to_le32(FT_FILESYS);
523
524 if (fib_send(ContainerCommand,
525 fibptr,
526 sizeof(struct aac_query_mount),
527 FsaNormal,
528 1, 1,
529 NULL, NULL) < 0)
530 goto error;
531 } else
532 dresp->mnt[0].capacityhigh = 0;
533
534 if ((le32_to_cpu(dresp->status) == ST_OK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
536 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
537 fsa_dev_ptr[cid].valid = 1;
538 fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700539 fsa_dev_ptr[cid].size
540 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
541 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
543 fsa_dev_ptr[cid].ro = 1;
544 }
545
546error:
547 fib_complete(fibptr);
548 fib_free(fibptr);
549
550 return status;
551}
552
553/* Local Structure to set SCSI inquiry data strings */
554struct scsi_inq {
555 char vid[8]; /* Vendor ID */
556 char pid[16]; /* Product ID */
557 char prl[4]; /* Product Revision Level */
558};
559
560/**
561 * InqStrCopy - string merge
562 * @a: string to copy from
563 * @b: string to copy to
564 *
565 * Copy a String from one location to another
566 * without copying \0
567 */
568
569static void inqstrcpy(char *a, char *b)
570{
571
572 while(*a != (char)0)
573 *b++ = *a++;
574}
575
576static char *container_types[] = {
577 "None",
578 "Volume",
579 "Mirror",
580 "Stripe",
581 "RAID5",
582 "SSRW",
583 "SSRO",
584 "Morph",
585 "Legacy",
586 "RAID4",
587 "RAID10",
588 "RAID00",
589 "V-MIRRORS",
590 "PSEUDO R4",
591 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700592 "RAID5D",
593 "RAID5D0",
594 "RAID1E",
595 "RAID6",
596 "RAID60",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 "Unknown"
598};
599
600
601
602/* Function: setinqstr
603 *
604 * Arguments: [1] pointer to void [1] int
605 *
606 * Purpose: Sets SCSI inquiry data strings for vendor, product
607 * and revision level. Allows strings to be set in platform dependant
608 * files instead of in OS dependant driver source.
609 */
610
611static void setinqstr(int devtype, void *data, int tindex)
612{
613 struct scsi_inq *str;
614 struct aac_driver_ident *mp;
615
616 mp = aac_get_driver_ident(devtype);
617
618 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
619
620 inqstrcpy (mp->vname, str->vid);
621 inqstrcpy (mp->model, str->pid); /* last six chars reserved for vol type */
622
623 if (tindex < (sizeof(container_types)/sizeof(char *))){
624 char *findit = str->pid;
625
626 for ( ; *findit != ' '; findit++); /* walk till we find a space */
627 /* RAID is superfluous in the context of a RAID device */
628 if (memcmp(findit-4, "RAID", 4) == 0)
629 *(findit -= 4) = ' ';
630 inqstrcpy (container_types[tindex], findit + 1);
631 }
632 inqstrcpy ("V1.0", str->prl);
633}
634
Adrian Bunk 48338692005-04-25 19:45:58 -0700635static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
636 u8 a_sense_code, u8 incorrect_length,
637 u8 bit_pointer, u16 field_pointer,
638 u32 residue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
641 sense_buf[1] = 0; /* Segment number, always zero */
642
643 if (incorrect_length) {
644 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
645 sense_buf[3] = BYTE3(residue);
646 sense_buf[4] = BYTE2(residue);
647 sense_buf[5] = BYTE1(residue);
648 sense_buf[6] = BYTE0(residue);
649 } else
650 sense_buf[2] = sense_key; /* Sense key */
651
652 if (sense_key == ILLEGAL_REQUEST)
653 sense_buf[7] = 10; /* Additional sense length */
654 else
655 sense_buf[7] = 6; /* Additional sense length */
656
657 sense_buf[12] = sense_code; /* Additional sense code */
658 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
659 if (sense_key == ILLEGAL_REQUEST) {
660 sense_buf[15] = 0;
661
662 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
663 sense_buf[15] = 0x80;/* Std sense key specific field */
664 /* Illegal parameter is in the parameter block */
665
666 if (sense_code == SENCODE_INVALID_CDB_FIELD)
667 sense_buf[15] = 0xc0;/* Std sense key specific field */
668 /* Illegal parameter is in the CDB block */
669 sense_buf[15] |= bit_pointer;
670 sense_buf[16] = field_pointer >> 8; /* MSB */
671 sense_buf[17] = field_pointer; /* LSB */
672 }
673}
674
675int aac_get_adapter_info(struct aac_dev* dev)
676{
677 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int rcode;
679 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -0700680 struct aac_adapter_info *info;
681 struct aac_bus_info *command;
682 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!(fibptr = fib_alloc(dev)))
685 return -ENOMEM;
686
687 fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700688 info = (struct aac_adapter_info *) fib_data(fibptr);
689 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 rcode = fib_send(RequestAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700692 fibptr,
693 sizeof(*info),
694 FsaNormal,
695 1, 1,
696 NULL,
697 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700699 if (rcode < 0) {
700 fib_complete(fibptr);
701 fib_free(fibptr);
702 return rcode;
703 }
704 memcpy(&dev->adapter_info, info, sizeof(*info));
705
706 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
707 struct aac_supplement_adapter_info * info;
708
709 fib_init(fibptr);
710
711 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
712
713 memset(info,0,sizeof(*info));
714
715 rcode = fib_send(RequestSupplementAdapterInfo,
716 fibptr,
717 sizeof(*info),
718 FsaNormal,
719 1, 1,
720 NULL,
721 NULL);
722
723 if (rcode >= 0)
724 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Mark Haverkamp84971732005-06-20 11:55:24 -0700727
728 /*
729 * GetBusInfo
730 */
731
732 fib_init(fibptr);
733
734 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
735
736 memset(bus_info, 0, sizeof(*bus_info));
737
738 command = (struct aac_bus_info *)bus_info;
739
740 command->Command = cpu_to_le32(VM_Ioctl);
741 command->ObjType = cpu_to_le32(FT_DRIVE);
742 command->MethodId = cpu_to_le32(1);
743 command->CtlCmd = cpu_to_le32(GetBusInfo);
744
745 rcode = fib_send(ContainerCommand,
746 fibptr,
747 sizeof (*bus_info),
748 FsaNormal,
749 1, 1,
750 NULL, NULL);
751
752 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
753 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
754 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700758 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dev->name,
760 dev->id,
761 tmp>>24,
762 (tmp>>16)&0xff,
763 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700764 le32_to_cpu(dev->adapter_info.kernelbuild),
765 (int)sizeof(dev->supplement_adapter_info.BuildDate),
766 dev->supplement_adapter_info.BuildDate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
768 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
769 dev->name, dev->id,
770 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
771 le32_to_cpu(dev->adapter_info.monitorbuild));
772 tmp = le32_to_cpu(dev->adapter_info.biosrev);
773 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
774 dev->name, dev->id,
775 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
776 le32_to_cpu(dev->adapter_info.biosbuild));
777 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
778 printk(KERN_INFO "%s%d: serial %x\n",
779 dev->name, dev->id,
780 le32_to_cpu(dev->adapter_info.serial[0]));
781
782 dev->nondasd_support = 0;
783 dev->raid_scsi_mode = 0;
784 if(dev->adapter_info.options & AAC_OPT_NONDASD){
785 dev->nondasd_support = 1;
786 }
787
788 /*
789 * If the firmware supports ROMB RAID/SCSI mode and we are currently
790 * in RAID/SCSI mode, set the flag. For now if in this mode we will
791 * force nondasd support on. If we decide to allow the non-dasd flag
792 * additional changes changes will have to be made to support
793 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
794 * changed to support the new dev->raid_scsi_mode flag instead of
795 * leaching off of the dev->nondasd_support flag. Also in linit.c the
796 * function aac_detect will have to be modified where it sets up the
797 * max number of channels based on the aac->nondasd_support flag only.
798 */
799 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
800 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
801 dev->nondasd_support = 1;
802 dev->raid_scsi_mode = 1;
803 }
804 if (dev->raid_scsi_mode != 0)
805 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
806 dev->name, dev->id);
807
808 if(nondasd != -1) {
809 dev->nondasd_support = (nondasd!=0);
810 }
811 if(dev->nondasd_support != 0){
812 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
813 }
814
815 dev->dac_support = 0;
816 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
817 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
818 dev->dac_support = 1;
819 }
820
821 if(dacmode != -1) {
822 dev->dac_support = (dacmode!=0);
823 }
824 if(dev->dac_support != 0) {
825 if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL) &&
826 !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL)) {
827 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
828 dev->name, dev->id);
829 } else if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFULL) &&
830 !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFULL)) {
831 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
832 dev->name, dev->id);
833 dev->dac_support = 0;
834 } else {
835 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
836 dev->name, dev->id);
837 rcode = -ENOMEM;
838 }
839 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700840 /*
841 * 57 scatter gather elements
842 */
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700843 if (!(dev->raw_io_interface)) {
844 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700845 sizeof(struct aac_fibhdr) -
Mark Haverkamp0e68c002005-08-03 15:39:49 -0700846 sizeof(struct aac_write) + sizeof(struct sgmap)) /
847 sizeof(struct sgmap);
848 if (dev->dac_support) {
849 /*
850 * 38 scatter gather elements
851 */
852 dev->scsi_host_ptr->sg_tablesize =
853 (dev->max_fib_size -
854 sizeof(struct aac_fibhdr) -
855 sizeof(struct aac_write64) +
856 sizeof(struct sgmap64)) /
857 sizeof(struct sgmap64);
858 }
859 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
860 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
861 /*
862 * Worst case size that could cause sg overflow when
863 * we break up SG elements that are larger than 64KB.
864 * Would be nice if we could tell the SCSI layer what
865 * the maximum SG element size can be. Worst case is
866 * (sg_tablesize-1) 4KB elements with one 64KB
867 * element.
868 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
869 */
870 dev->scsi_host_ptr->max_sectors =
871 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
872 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 fib_complete(fibptr);
876 fib_free(fibptr);
877
878 return rcode;
879}
880
881
Mark Haverkampe53cb352005-08-03 15:39:09 -0700882static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 struct aac_dev *dev;
885 struct aac_read_reply *readreply;
886 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 u32 cid;
888
889 scsicmd = (struct scsi_cmnd *) context;
890
891 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
892 cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
893
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700894 if (nblank(dprintk(x))) {
895 u64 lba;
896 switch (scsicmd->cmnd[0]) {
897 case WRITE_6:
898 case READ_6:
899 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
900 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
901 break;
902 case WRITE_16:
903 case READ_16:
904 lba = ((u64)scsicmd->cmnd[2] << 56) |
905 ((u64)scsicmd->cmnd[3] << 48) |
906 ((u64)scsicmd->cmnd[4] << 40) |
907 ((u64)scsicmd->cmnd[5] << 32) |
908 ((u64)scsicmd->cmnd[6] << 24) |
909 (scsicmd->cmnd[7] << 16) |
910 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
911 break;
912 case WRITE_12:
913 case READ_12:
914 lba = ((u64)scsicmd->cmnd[2] << 24) |
915 (scsicmd->cmnd[3] << 16) |
916 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
917 break;
918 default:
919 lba = ((u64)scsicmd->cmnd[2] << 24) |
920 (scsicmd->cmnd[3] << 16) |
921 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
922 break;
923 }
924 printk(KERN_DEBUG
925 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
926 smp_processor_id(), (unsigned long long)lba, jiffies);
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (fibptr == NULL)
930 BUG();
931
932 if(scsicmd->use_sg)
933 pci_unmap_sg(dev->pdev,
934 (struct scatterlist *)scsicmd->buffer,
935 scsicmd->use_sg,
936 scsicmd->sc_data_direction);
937 else if(scsicmd->request_bufflen)
938 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
939 scsicmd->request_bufflen,
940 scsicmd->sc_data_direction);
941 readreply = (struct aac_read_reply *)fib_data(fibptr);
942 if (le32_to_cpu(readreply->status) == ST_OK)
943 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
944 else {
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700945#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -0700946 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700947 le32_to_cpu(readreply->status));
948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
950 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
951 HARDWARE_ERROR,
952 SENCODE_INTERNAL_TARGET_FAILURE,
953 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
954 0, 0);
955 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
956 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
957 ? sizeof(scsicmd->sense_buffer)
958 : sizeof(dev->fsa_dev[cid].sense_data));
959 }
960 fib_complete(fibptr);
961 fib_free(fibptr);
962
963 aac_io_done(scsicmd);
964}
965
Adrian Bunk 48338692005-04-25 19:45:58 -0700966static int aac_read(struct scsi_cmnd * scsicmd, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700968 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 u32 count;
970 int status;
971
972 u16 fibsize;
973 struct aac_dev *dev;
974 struct fib * cmd_fibcontext;
975
976 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
977 /*
978 * Get block address and transfer length
979 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700980 switch (scsicmd->cmnd[0]) {
981 case READ_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
983
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700984 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
985 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 count = scsicmd->cmnd[4];
987
988 if (count == 0)
989 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -0700990 break;
991 case READ_16:
992 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
993
994 lba = ((u64)scsicmd->cmnd[2] << 56) |
995 ((u64)scsicmd->cmnd[3] << 48) |
996 ((u64)scsicmd->cmnd[4] << 40) |
997 ((u64)scsicmd->cmnd[5] << 32) |
998 ((u64)scsicmd->cmnd[6] << 24) |
999 (scsicmd->cmnd[7] << 16) |
1000 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1001 count = (scsicmd->cmnd[10] << 24) |
1002 (scsicmd->cmnd[11] << 16) |
1003 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1004 break;
1005 case READ_12:
1006 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
1007
1008 lba = ((u64)scsicmd->cmnd[2] << 24) |
1009 (scsicmd->cmnd[3] << 16) |
1010 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1011 count = (scsicmd->cmnd[6] << 24) |
1012 (scsicmd->cmnd[7] << 16) |
1013 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1014 break;
1015 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
1017
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001018 lba = ((u64)scsicmd->cmnd[2] << 24) |
1019 (scsicmd->cmnd[3] << 16) |
1020 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001024 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001025 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001026 if ((!(dev->raw_io_interface) || !(dev->raw_io_64)) &&
1027 (lba & 0xffffffff00000000LL)) {
1028 dprintk((KERN_DEBUG "aac_read: Illegal lba\n"));
1029 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1030 SAM_STAT_CHECK_CONDITION;
1031 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1032 HARDWARE_ERROR,
1033 SENCODE_INTERNAL_TARGET_FAILURE,
1034 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1035 0, 0);
1036 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1037 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1038 ? sizeof(scsicmd->sense_buffer)
1039 : sizeof(dev->fsa_dev[cid].sense_data));
1040 scsicmd->scsi_done(scsicmd);
1041 return 0;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * Alocate and initialize a Fib
1045 */
1046 if (!(cmd_fibcontext = fib_alloc(dev))) {
1047 return -1;
1048 }
1049
1050 fib_init(cmd_fibcontext);
1051
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001052 if (dev->raw_io_interface) {
1053 struct aac_raw_io *readcmd;
1054 readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001055 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1056 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001057 readcmd->count = cpu_to_le32(count<<9);
1058 readcmd->cid = cpu_to_le16(cid);
1059 readcmd->flags = cpu_to_le16(1);
1060 readcmd->bpTotal = 0;
1061 readcmd->bpComplete = 0;
1062
1063 aac_build_sgraw(scsicmd, &readcmd->sg);
1064 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
1065 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
1066 BUG();
1067 /*
1068 * Now send the Fib to the adapter
1069 */
1070 status = fib_send(ContainerRawIo,
1071 cmd_fibcontext,
1072 fibsize,
1073 FsaNormal,
1074 0, 1,
1075 (fib_callback) io_callback,
1076 (void *) scsicmd);
1077 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct aac_read64 *readcmd;
1079 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
1080 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1081 readcmd->cid = cpu_to_le16(cid);
1082 readcmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001083 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 readcmd->pad = 0;
1085 readcmd->flags = 0;
1086
1087 aac_build_sg64(scsicmd, &readcmd->sg);
1088 fibsize = sizeof(struct aac_read64) +
1089 ((le32_to_cpu(readcmd->sg.count) - 1) *
1090 sizeof (struct sgentry64));
Mark Haverkamp77d71d22005-09-01 08:19:23 -07001091 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 sizeof(struct aac_fibhdr)));
1093 /*
1094 * Now send the Fib to the adapter
1095 */
1096 status = fib_send(ContainerCommand64,
1097 cmd_fibcontext,
1098 fibsize,
1099 FsaNormal,
1100 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001101 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 (void *) scsicmd);
1103 } else {
1104 struct aac_read *readcmd;
1105 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
1106 readcmd->command = cpu_to_le32(VM_CtBlockRead);
1107 readcmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001108 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 readcmd->count = cpu_to_le32(count * 512);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 aac_build_sg(scsicmd, &readcmd->sg);
1112 fibsize = sizeof(struct aac_read) +
1113 ((le32_to_cpu(readcmd->sg.count) - 1) *
1114 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001115 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 sizeof(struct aac_fibhdr)));
1117 /*
1118 * Now send the Fib to the adapter
1119 */
1120 status = fib_send(ContainerCommand,
1121 cmd_fibcontext,
1122 fibsize,
1123 FsaNormal,
1124 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001125 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 (void *) scsicmd);
1127 }
1128
1129
1130
1131 /*
1132 * Check that the command queued to the controller
1133 */
1134 if (status == -EINPROGRESS)
1135 return 0;
1136
1137 printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
1138 /*
1139 * For some reason, the Fib didn't queue, return QUEUE_FULL
1140 */
1141 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1142 aac_io_done(scsicmd);
1143 fib_complete(cmd_fibcontext);
1144 fib_free(cmd_fibcontext);
1145 return 0;
1146}
1147
1148static int aac_write(struct scsi_cmnd * scsicmd, int cid)
1149{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001150 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 u32 count;
1152 int status;
1153 u16 fibsize;
1154 struct aac_dev *dev;
1155 struct fib * cmd_fibcontext;
1156
1157 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1158 /*
1159 * Get block address and transfer length
1160 */
1161 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1162 {
1163 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1164 count = scsicmd->cmnd[4];
1165 if (count == 0)
1166 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001167 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1168 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
1169
1170 lba = ((u64)scsicmd->cmnd[2] << 56) |
1171 ((u64)scsicmd->cmnd[3] << 48) |
1172 ((u64)scsicmd->cmnd[4] << 40) |
1173 ((u64)scsicmd->cmnd[5] << 32) |
1174 ((u64)scsicmd->cmnd[6] << 24) |
1175 (scsicmd->cmnd[7] << 16) |
1176 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1177 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1178 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1179 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1180 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
1181
1182 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1183 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1184 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1185 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else {
1187 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001188 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1190 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001191 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001193 if ((!(dev->raw_io_interface) || !(dev->raw_io_64))
1194 && (lba & 0xffffffff00000000LL)) {
1195 dprintk((KERN_DEBUG "aac_write: Illegal lba\n"));
1196 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1197 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1198 HARDWARE_ERROR,
1199 SENCODE_INTERNAL_TARGET_FAILURE,
1200 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1201 0, 0);
1202 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1203 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1204 ? sizeof(scsicmd->sense_buffer)
1205 : sizeof(dev->fsa_dev[cid].sense_data));
1206 scsicmd->scsi_done(scsicmd);
1207 return 0;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * Allocate and initialize a Fib then setup a BlockWrite command
1211 */
1212 if (!(cmd_fibcontext = fib_alloc(dev))) {
1213 scsicmd->result = DID_ERROR << 16;
1214 aac_io_done(scsicmd);
1215 return 0;
1216 }
1217 fib_init(cmd_fibcontext);
1218
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001219 if (dev->raw_io_interface) {
1220 struct aac_raw_io *writecmd;
1221 writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001222 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1223 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001224 writecmd->count = cpu_to_le32(count<<9);
1225 writecmd->cid = cpu_to_le16(cid);
1226 writecmd->flags = 0;
1227 writecmd->bpTotal = 0;
1228 writecmd->bpComplete = 0;
1229
1230 aac_build_sgraw(scsicmd, &writecmd->sg);
1231 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1232 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
1233 BUG();
1234 /*
1235 * Now send the Fib to the adapter
1236 */
1237 status = fib_send(ContainerRawIo,
1238 cmd_fibcontext,
1239 fibsize,
1240 FsaNormal,
1241 0, 1,
1242 (fib_callback) io_callback,
1243 (void *) scsicmd);
1244 } else if (dev->dac_support == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct aac_write64 *writecmd;
1246 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
1247 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1248 writecmd->cid = cpu_to_le16(cid);
1249 writecmd->sector_count = cpu_to_le16(count);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001250 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 writecmd->pad = 0;
1252 writecmd->flags = 0;
1253
1254 aac_build_sg64(scsicmd, &writecmd->sg);
1255 fibsize = sizeof(struct aac_write64) +
1256 ((le32_to_cpu(writecmd->sg.count) - 1) *
1257 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001258 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 sizeof(struct aac_fibhdr)));
1260 /*
1261 * Now send the Fib to the adapter
1262 */
1263 status = fib_send(ContainerCommand64,
1264 cmd_fibcontext,
1265 fibsize,
1266 FsaNormal,
1267 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001268 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 (void *) scsicmd);
1270 } else {
1271 struct aac_write *writecmd;
1272 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
1273 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1274 writecmd->cid = cpu_to_le32(cid);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001275 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 writecmd->count = cpu_to_le32(count * 512);
1277 writecmd->sg.count = cpu_to_le32(1);
1278 /* ->stable is not used - it did mean which type of write */
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 aac_build_sg(scsicmd, &writecmd->sg);
1281 fibsize = sizeof(struct aac_write) +
1282 ((le32_to_cpu(writecmd->sg.count) - 1) *
1283 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001284 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 sizeof(struct aac_fibhdr)));
1286 /*
1287 * Now send the Fib to the adapter
1288 */
1289 status = fib_send(ContainerCommand,
1290 cmd_fibcontext,
1291 fibsize,
1292 FsaNormal,
1293 0, 1,
Mark Haverkampe53cb352005-08-03 15:39:09 -07001294 (fib_callback) io_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 (void *) scsicmd);
1296 }
1297
1298 /*
1299 * Check that the command queued to the controller
1300 */
1301 if (status == -EINPROGRESS)
1302 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return 0;
1304 }
1305
1306 printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
1307 /*
1308 * For some reason, the Fib didn't queue, return QUEUE_FULL
1309 */
1310 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1311 aac_io_done(scsicmd);
1312
1313 fib_complete(cmd_fibcontext);
1314 fib_free(cmd_fibcontext);
1315 return 0;
1316}
1317
1318static void synchronize_callback(void *context, struct fib *fibptr)
1319{
1320 struct aac_synchronize_reply *synchronizereply;
1321 struct scsi_cmnd *cmd;
1322
1323 cmd = context;
1324
1325 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1326 smp_processor_id(), jiffies));
1327 BUG_ON(fibptr == NULL);
1328
1329
1330 synchronizereply = fib_data(fibptr);
1331 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1332 cmd->result = DID_OK << 16 |
1333 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1334 else {
1335 struct scsi_device *sdev = cmd->device;
1336 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
1337 u32 cid = ID_LUN_TO_CONTAINER(sdev->id, sdev->lun);
1338 printk(KERN_WARNING
1339 "synchronize_callback: synchronize failed, status = %d\n",
1340 le32_to_cpu(synchronizereply->status));
1341 cmd->result = DID_OK << 16 |
1342 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1343 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1344 HARDWARE_ERROR,
1345 SENCODE_INTERNAL_TARGET_FAILURE,
1346 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1347 0, 0);
1348 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1349 min(sizeof(dev->fsa_dev[cid].sense_data),
1350 sizeof(cmd->sense_buffer)));
1351 }
1352
1353 fib_complete(fibptr);
1354 fib_free(fibptr);
1355 aac_io_done(cmd);
1356}
1357
1358static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1359{
1360 int status;
1361 struct fib *cmd_fibcontext;
1362 struct aac_synchronize *synchronizecmd;
1363 struct scsi_cmnd *cmd;
1364 struct scsi_device *sdev = scsicmd->device;
1365 int active = 0;
1366 unsigned long flags;
1367
1368 /*
1369 * Wait for all commands to complete to this specific
1370 * target (block).
1371 */
1372 spin_lock_irqsave(&sdev->list_lock, flags);
1373 list_for_each_entry(cmd, &sdev->cmd_list, list)
1374 if (cmd != scsicmd && cmd->serial_number != 0) {
1375 ++active;
1376 break;
1377 }
1378
1379 spin_unlock_irqrestore(&sdev->list_lock, flags);
1380
1381 /*
1382 * Yield the processor (requeue for later)
1383 */
1384 if (active)
1385 return SCSI_MLQUEUE_DEVICE_BUSY;
1386
1387 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001388 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 */
1390 if (!(cmd_fibcontext =
1391 fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata)))
1392 return SCSI_MLQUEUE_HOST_BUSY;
1393
1394 fib_init(cmd_fibcontext);
1395
1396 synchronizecmd = fib_data(cmd_fibcontext);
1397 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1398 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1399 synchronizecmd->cid = cpu_to_le32(cid);
1400 synchronizecmd->count =
1401 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1402
1403 /*
1404 * Now send the Fib to the adapter
1405 */
1406 status = fib_send(ContainerCommand,
1407 cmd_fibcontext,
1408 sizeof(struct aac_synchronize),
1409 FsaNormal,
1410 0, 1,
1411 (fib_callback)synchronize_callback,
1412 (void *)scsicmd);
1413
1414 /*
1415 * Check that the command queued to the controller
1416 */
1417 if (status == -EINPROGRESS)
1418 return 0;
1419
1420 printk(KERN_WARNING
1421 "aac_synchronize: fib_send failed with status: %d.\n", status);
1422 fib_complete(cmd_fibcontext);
1423 fib_free(cmd_fibcontext);
1424 return SCSI_MLQUEUE_HOST_BUSY;
1425}
1426
1427/**
1428 * aac_scsi_cmd() - Process SCSI command
1429 * @scsicmd: SCSI command block
1430 *
1431 * Emulate a SCSI command and queue the required request for the
1432 * aacraid firmware.
1433 */
1434
1435int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1436{
1437 u32 cid = 0;
1438 struct Scsi_Host *host = scsicmd->device->host;
1439 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1440 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1441 int cardtype = dev->cardtype;
1442 int ret;
1443
1444 /*
1445 * If the bus, id or lun is out of range, return fail
1446 * Test does not apply to ID 16, the pseudo id for the controller
1447 * itself.
1448 */
1449 if (scsicmd->device->id != host->this_id) {
1450 if ((scsicmd->device->channel == 0) ){
1451 if( (scsicmd->device->id >= dev->maximum_num_containers) || (scsicmd->device->lun != 0)){
1452 scsicmd->result = DID_NO_CONNECT << 16;
1453 scsicmd->scsi_done(scsicmd);
1454 return 0;
1455 }
1456 cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
1457
1458 /*
1459 * If the target container doesn't exist, it may have
1460 * been newly created
1461 */
1462 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1463 switch (scsicmd->cmnd[0]) {
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001464 case SERVICE_ACTION_IN:
1465 if (!(dev->raw_io_interface) ||
1466 !(dev->raw_io_64) ||
1467 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 case INQUIRY:
1470 case READ_CAPACITY:
1471 case TEST_UNIT_READY:
1472 spin_unlock_irq(host->host_lock);
1473 probe_container(dev, cid);
1474 spin_lock_irq(host->host_lock);
1475 if (fsa_dev_ptr[cid].valid == 0) {
1476 scsicmd->result = DID_NO_CONNECT << 16;
1477 scsicmd->scsi_done(scsicmd);
1478 return 0;
1479 }
1480 default:
1481 break;
1482 }
1483 }
1484 /*
1485 * If the target container still doesn't exist,
1486 * return failure
1487 */
1488 if (fsa_dev_ptr[cid].valid == 0) {
1489 scsicmd->result = DID_BAD_TARGET << 16;
1490 scsicmd->scsi_done(scsicmd);
1491 return 0;
1492 }
1493 } else { /* check for physical non-dasd devices */
1494 if(dev->nondasd_support == 1){
1495 return aac_send_srb_fib(scsicmd);
1496 } else {
1497 scsicmd->result = DID_NO_CONNECT << 16;
1498 scsicmd->scsi_done(scsicmd);
1499 return 0;
1500 }
1501 }
1502 }
1503 /*
1504 * else Command for the controller itself
1505 */
1506 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1507 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1508 {
1509 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1510 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1511 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1512 ILLEGAL_REQUEST,
1513 SENCODE_INVALID_COMMAND,
1514 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1515 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1516 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1517 ? sizeof(scsicmd->sense_buffer)
1518 : sizeof(dev->fsa_dev[cid].sense_data));
1519 scsicmd->scsi_done(scsicmd);
1520 return 0;
1521 }
1522
1523
1524 /* Handle commands here that don't really require going out to the adapter */
1525 switch (scsicmd->cmnd[0]) {
1526 case INQUIRY:
1527 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001528 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001531 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001533 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001534 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 */
1535 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001537 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /*
1539 * Set the Vendor, Product, and Revision Level
1540 * see: <vendor>.c i.e. aac.c
1541 */
1542 if (scsicmd->device->id == host->this_id) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001543 setinqstr(cardtype, (void *) (inq_data.inqd_vid), (sizeof(container_types)/sizeof(char *)));
1544 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1545 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1547 scsicmd->scsi_done(scsicmd);
1548 return 0;
1549 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001550 setinqstr(cardtype, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
1551 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1552 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return aac_get_container_name(scsicmd, cid);
1554 }
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001555 case SERVICE_ACTION_IN:
1556 if (!(dev->raw_io_interface) ||
1557 !(dev->raw_io_64) ||
1558 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1559 break;
1560 {
1561 u64 capacity;
1562 char cp[12];
1563 unsigned int offset = 0;
1564
1565 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1566 capacity = fsa_dev_ptr[cid].size - 1;
1567 if (scsicmd->cmnd[13] > 12) {
1568 offset = scsicmd->cmnd[13] - 12;
1569 if (offset > sizeof(cp))
1570 break;
1571 memset(cp, 0, offset);
1572 aac_internal_transfer(scsicmd, cp, 0, offset);
1573 }
1574 cp[0] = (capacity >> 56) & 0xff;
1575 cp[1] = (capacity >> 48) & 0xff;
1576 cp[2] = (capacity >> 40) & 0xff;
1577 cp[3] = (capacity >> 32) & 0xff;
1578 cp[4] = (capacity >> 24) & 0xff;
1579 cp[5] = (capacity >> 16) & 0xff;
1580 cp[6] = (capacity >> 8) & 0xff;
1581 cp[7] = (capacity >> 0) & 0xff;
1582 cp[8] = 0;
1583 cp[9] = 0;
1584 cp[10] = 2;
1585 cp[11] = 0;
1586 aac_internal_transfer(scsicmd, cp, offset, sizeof(cp));
1587
1588 /* Do not cache partition table for arrays */
1589 scsicmd->device->removable = 1;
1590
1591 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1592 scsicmd->scsi_done(scsicmd);
1593
1594 return 0;
1595 }
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 case READ_CAPACITY:
1598 {
1599 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001600 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001603 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 capacity = fsa_dev_ptr[cid].size - 1;
1605 else
1606 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 cp[0] = (capacity >> 24) & 0xff;
1609 cp[1] = (capacity >> 16) & 0xff;
1610 cp[2] = (capacity >> 8) & 0xff;
1611 cp[3] = (capacity >> 0) & 0xff;
1612 cp[4] = 0;
1613 cp[5] = 0;
1614 cp[6] = 2;
1615 cp[7] = 0;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001616 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001617 /* Do not cache partition table for arrays */
1618 scsicmd->device->removable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1621 scsicmd->scsi_done(scsicmd);
1622
1623 return 0;
1624 }
1625
1626 case MODE_SENSE:
1627 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001628 char mode_buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 mode_buf[0] = 3; /* Mode data length */
1632 mode_buf[1] = 0; /* Medium type - default */
1633 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1634 mode_buf[3] = 0; /* Block descriptor length */
1635
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001636 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1638 scsicmd->scsi_done(scsicmd);
1639
1640 return 0;
1641 }
1642 case MODE_SENSE_10:
1643 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001644 char mode_buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 mode_buf[0] = 0; /* Mode data length (MSB) */
1648 mode_buf[1] = 6; /* Mode data length (LSB) */
1649 mode_buf[2] = 0; /* Medium type - default */
1650 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1651 mode_buf[4] = 0; /* reserved */
1652 mode_buf[5] = 0; /* reserved */
1653 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1654 mode_buf[7] = 0; /* Block descriptor length (LSB) */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07001655 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1658 scsicmd->scsi_done(scsicmd);
1659
1660 return 0;
1661 }
1662 case REQUEST_SENSE:
1663 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1664 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1665 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1666 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1667 scsicmd->scsi_done(scsicmd);
1668 return 0;
1669
1670 case ALLOW_MEDIUM_REMOVAL:
1671 dprintk((KERN_DEBUG "LOCK command.\n"));
1672 if (scsicmd->cmnd[4])
1673 fsa_dev_ptr[cid].locked = 1;
1674 else
1675 fsa_dev_ptr[cid].locked = 0;
1676
1677 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1678 scsicmd->scsi_done(scsicmd);
1679 return 0;
1680 /*
1681 * These commands are all No-Ops
1682 */
1683 case TEST_UNIT_READY:
1684 case RESERVE:
1685 case RELEASE:
1686 case REZERO_UNIT:
1687 case REASSIGN_BLOCKS:
1688 case SEEK_10:
1689 case START_STOP:
1690 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1691 scsicmd->scsi_done(scsicmd);
1692 return 0;
1693 }
1694
1695 switch (scsicmd->cmnd[0])
1696 {
1697 case READ_6:
1698 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001699 case READ_12:
1700 case READ_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /*
1702 * Hack to keep track of ordinal number of the device that
1703 * corresponds to a container. Needed to convert
1704 * containers to /dev/sd device names
1705 */
1706
1707 spin_unlock_irq(host->host_lock);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001708 if (scsicmd->request->rq_disk)
1709 strlcpy(fsa_dev_ptr[cid].devname,
1710 scsicmd->request->rq_disk->disk_name,
1711 min(sizeof(fsa_dev_ptr[cid].devname),
1712 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 ret = aac_read(scsicmd, cid);
1714 spin_lock_irq(host->host_lock);
1715 return ret;
1716
1717 case WRITE_6:
1718 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001719 case WRITE_12:
1720 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 spin_unlock_irq(host->host_lock);
1722 ret = aac_write(scsicmd, cid);
1723 spin_lock_irq(host->host_lock);
1724 return ret;
1725
1726 case SYNCHRONIZE_CACHE:
1727 /* Issue FIB to tell Firmware to flush it's cache */
1728 return aac_synchronize(scsicmd, cid);
1729
1730 default:
1731 /*
1732 * Unhandled commands
1733 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001734 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1736 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1737 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1738 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1739 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1740 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1741 ? sizeof(scsicmd->sense_buffer)
1742 : sizeof(dev->fsa_dev[cid].sense_data));
1743 scsicmd->scsi_done(scsicmd);
1744 return 0;
1745 }
1746}
1747
1748static int query_disk(struct aac_dev *dev, void __user *arg)
1749{
1750 struct aac_query_disk qd;
1751 struct fsa_dev_info *fsa_dev_ptr;
1752
1753 fsa_dev_ptr = dev->fsa_dev;
1754 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1755 return -EFAULT;
1756 if (qd.cnum == -1)
1757 qd.cnum = ID_LUN_TO_CONTAINER(qd.id, qd.lun);
1758 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
1759 {
1760 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1761 return -EINVAL;
1762 qd.instance = dev->scsi_host_ptr->host_no;
1763 qd.bus = 0;
1764 qd.id = CONTAINER_TO_ID(qd.cnum);
1765 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1766 }
1767 else return -EINVAL;
1768
1769 qd.valid = fsa_dev_ptr[qd.cnum].valid;
1770 qd.locked = fsa_dev_ptr[qd.cnum].locked;
1771 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1772
1773 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1774 qd.unmapped = 1;
1775 else
1776 qd.unmapped = 0;
1777
1778 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1779 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1780
1781 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1782 return -EFAULT;
1783 return 0;
1784}
1785
1786static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1787{
1788 struct aac_delete_disk dd;
1789 struct fsa_dev_info *fsa_dev_ptr;
1790
1791 fsa_dev_ptr = dev->fsa_dev;
1792
1793 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1794 return -EFAULT;
1795
1796 if (dd.cnum >= dev->maximum_num_containers)
1797 return -EINVAL;
1798 /*
1799 * Mark this container as being deleted.
1800 */
1801 fsa_dev_ptr[dd.cnum].deleted = 1;
1802 /*
1803 * Mark the container as no longer valid
1804 */
1805 fsa_dev_ptr[dd.cnum].valid = 0;
1806 return 0;
1807}
1808
1809static int delete_disk(struct aac_dev *dev, void __user *arg)
1810{
1811 struct aac_delete_disk dd;
1812 struct fsa_dev_info *fsa_dev_ptr;
1813
1814 fsa_dev_ptr = dev->fsa_dev;
1815
1816 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1817 return -EFAULT;
1818
1819 if (dd.cnum >= dev->maximum_num_containers)
1820 return -EINVAL;
1821 /*
1822 * If the container is locked, it can not be deleted by the API.
1823 */
1824 if (fsa_dev_ptr[dd.cnum].locked)
1825 return -EBUSY;
1826 else {
1827 /*
1828 * Mark the container as no longer being valid.
1829 */
1830 fsa_dev_ptr[dd.cnum].valid = 0;
1831 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1832 return 0;
1833 }
1834}
1835
1836int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1837{
1838 switch (cmd) {
1839 case FSACTL_QUERY_DISK:
1840 return query_disk(dev, arg);
1841 case FSACTL_DELETE_DISK:
1842 return delete_disk(dev, arg);
1843 case FSACTL_FORCE_DELETE_DISK:
1844 return force_delete_disk(dev, arg);
1845 case FSACTL_GET_CONTAINERS:
1846 return aac_get_containers(dev);
1847 default:
1848 return -ENOTTY;
1849 }
1850}
1851
1852/**
1853 *
1854 * aac_srb_callback
1855 * @context: the context set in the fib - here it is scsi cmd
1856 * @fibptr: pointer to the fib
1857 *
1858 * Handles the completion of a scsi command to a non dasd device
1859 *
1860 */
1861
1862static void aac_srb_callback(void *context, struct fib * fibptr)
1863{
1864 struct aac_dev *dev;
1865 struct aac_srb_reply *srbreply;
1866 struct scsi_cmnd *scsicmd;
1867
1868 scsicmd = (struct scsi_cmnd *) context;
1869 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1870
1871 if (fibptr == NULL)
1872 BUG();
1873
1874 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1875
1876 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
1877 /*
1878 * Calculate resid for sg
1879 */
1880
1881 scsicmd->resid = scsicmd->request_bufflen -
1882 le32_to_cpu(srbreply->data_xfer_length);
1883
1884 if(scsicmd->use_sg)
1885 pci_unmap_sg(dev->pdev,
1886 (struct scatterlist *)scsicmd->buffer,
1887 scsicmd->use_sg,
1888 scsicmd->sc_data_direction);
1889 else if(scsicmd->request_bufflen)
1890 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1891 scsicmd->sc_data_direction);
1892
1893 /*
1894 * First check the fib status
1895 */
1896
1897 if (le32_to_cpu(srbreply->status) != ST_OK){
1898 int len;
1899 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1900 len = (le32_to_cpu(srbreply->sense_data_size) >
1901 sizeof(scsicmd->sense_buffer)) ?
1902 sizeof(scsicmd->sense_buffer) :
1903 le32_to_cpu(srbreply->sense_data_size);
1904 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1905 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1906 }
1907
1908 /*
1909 * Next check the srb status
1910 */
1911 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1912 case SRB_STATUS_ERROR_RECOVERY:
1913 case SRB_STATUS_PENDING:
1914 case SRB_STATUS_SUCCESS:
1915 if(scsicmd->cmnd[0] == INQUIRY ){
1916 u8 b;
1917 u8 b1;
1918 /* We can't expose disk devices because we can't tell whether they
1919 * are the raw container drives or stand alone drives. If they have
1920 * the removable bit set then we should expose them though.
1921 */
1922 b = (*(u8*)scsicmd->buffer)&0x1f;
1923 b1 = ((u8*)scsicmd->buffer)[1];
1924 if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1925 || (b==TYPE_DISK && (b1&0x80)) ){
1926 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1927 /*
1928 * We will allow disk devices if in RAID/SCSI mode and
1929 * the channel is 2
1930 */
1931 } else if ((dev->raid_scsi_mode) &&
1932 (scsicmd->device->channel == 2)) {
1933 scsicmd->result = DID_OK << 16 |
1934 COMMAND_COMPLETE << 8;
1935 } else {
1936 scsicmd->result = DID_NO_CONNECT << 16 |
1937 COMMAND_COMPLETE << 8;
1938 }
1939 } else {
1940 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1941 }
1942 break;
1943 case SRB_STATUS_DATA_OVERRUN:
1944 switch(scsicmd->cmnd[0]){
1945 case READ_6:
1946 case WRITE_6:
1947 case READ_10:
1948 case WRITE_10:
1949 case READ_12:
1950 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001951 case READ_16:
1952 case WRITE_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1954 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1955 } else {
1956 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1957 }
1958 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1959 break;
1960 case INQUIRY: {
1961 u8 b;
1962 u8 b1;
1963 /* We can't expose disk devices because we can't tell whether they
1964 * are the raw container drives or stand alone drives
1965 */
1966 b = (*(u8*)scsicmd->buffer)&0x0f;
1967 b1 = ((u8*)scsicmd->buffer)[1];
1968 if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1969 || (b==TYPE_DISK && (b1&0x80)) ){
1970 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1971 /*
1972 * We will allow disk devices if in RAID/SCSI mode and
1973 * the channel is 2
1974 */
1975 } else if ((dev->raid_scsi_mode) &&
1976 (scsicmd->device->channel == 2)) {
1977 scsicmd->result = DID_OK << 16 |
1978 COMMAND_COMPLETE << 8;
1979 } else {
1980 scsicmd->result = DID_NO_CONNECT << 16 |
1981 COMMAND_COMPLETE << 8;
1982 }
1983 break;
1984 }
1985 default:
1986 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1987 break;
1988 }
1989 break;
1990 case SRB_STATUS_ABORTED:
1991 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1992 break;
1993 case SRB_STATUS_ABORT_FAILED:
1994 // Not sure about this one - but assuming the hba was trying to abort for some reason
1995 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1996 break;
1997 case SRB_STATUS_PARITY_ERROR:
1998 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1999 break;
2000 case SRB_STATUS_NO_DEVICE:
2001 case SRB_STATUS_INVALID_PATH_ID:
2002 case SRB_STATUS_INVALID_TARGET_ID:
2003 case SRB_STATUS_INVALID_LUN:
2004 case SRB_STATUS_SELECTION_TIMEOUT:
2005 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2006 break;
2007
2008 case SRB_STATUS_COMMAND_TIMEOUT:
2009 case SRB_STATUS_TIMEOUT:
2010 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2011 break;
2012
2013 case SRB_STATUS_BUSY:
2014 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2015 break;
2016
2017 case SRB_STATUS_BUS_RESET:
2018 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2019 break;
2020
2021 case SRB_STATUS_MESSAGE_REJECTED:
2022 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2023 break;
2024 case SRB_STATUS_REQUEST_FLUSHED:
2025 case SRB_STATUS_ERROR:
2026 case SRB_STATUS_INVALID_REQUEST:
2027 case SRB_STATUS_REQUEST_SENSE_FAILED:
2028 case SRB_STATUS_NO_HBA:
2029 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2030 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2031 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2032 case SRB_STATUS_DELAYED_RETRY:
2033 case SRB_STATUS_BAD_FUNCTION:
2034 case SRB_STATUS_NOT_STARTED:
2035 case SRB_STATUS_NOT_IN_USE:
2036 case SRB_STATUS_FORCE_ABORT:
2037 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2038 default:
2039#ifdef AAC_DETAILED_STATUS_INFO
2040 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2041 le32_to_cpu(srbreply->srb_status) & 0x3F,
2042 aac_get_status_string(
2043 le32_to_cpu(srbreply->srb_status) & 0x3F),
2044 scsicmd->cmnd[0],
2045 le32_to_cpu(srbreply->scsi_status));
2046#endif
2047 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2048 break;
2049 }
2050 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2051 int len;
2052 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2053 len = (le32_to_cpu(srbreply->sense_data_size) >
2054 sizeof(scsicmd->sense_buffer)) ?
2055 sizeof(scsicmd->sense_buffer) :
2056 le32_to_cpu(srbreply->sense_data_size);
2057#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002058 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2059 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060#endif
2061 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2062
2063 }
2064 /*
2065 * OR in the scsi status (already shifted up a bit)
2066 */
2067 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2068
2069 fib_complete(fibptr);
2070 fib_free(fibptr);
2071 aac_io_done(scsicmd);
2072}
2073
2074/**
2075 *
2076 * aac_send_scb_fib
2077 * @scsicmd: the scsi command block
2078 *
2079 * This routine will form a FIB and fill in the aac_srb from the
2080 * scsicmd passed in.
2081 */
2082
2083static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2084{
2085 struct fib* cmd_fibcontext;
2086 struct aac_dev* dev;
2087 int status;
2088 struct aac_srb *srbcmd;
2089 u16 fibsize;
2090 u32 flag;
2091 u32 timeout;
2092
Mark Haverkamp84971732005-06-20 11:55:24 -07002093 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2094 if (scsicmd->device->id >= dev->maximum_num_physicals ||
2095 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 scsicmd->result = DID_NO_CONNECT << 16;
2097 scsicmd->scsi_done(scsicmd);
2098 return 0;
2099 }
2100
2101 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2102 switch(scsicmd->sc_data_direction){
2103 case DMA_TO_DEVICE:
2104 flag = SRB_DataOut;
2105 break;
2106 case DMA_BIDIRECTIONAL:
2107 flag = SRB_DataIn | SRB_DataOut;
2108 break;
2109 case DMA_FROM_DEVICE:
2110 flag = SRB_DataIn;
2111 break;
2112 case DMA_NONE:
2113 default: /* shuts up some versions of gcc */
2114 flag = SRB_NoDataXfer;
2115 break;
2116 }
2117
2118
2119 /*
2120 * Allocate and initialize a Fib then setup a BlockWrite command
2121 */
2122 if (!(cmd_fibcontext = fib_alloc(dev))) {
2123 return -1;
2124 }
2125 fib_init(cmd_fibcontext);
2126
2127 srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
2128 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
2129 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
2130 srbcmd->id = cpu_to_le32(scsicmd->device->id);
2131 srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
2132 srbcmd->flags = cpu_to_le32(flag);
James Bottomley5262d082005-08-05 16:31:35 -05002133 timeout = scsicmd->timeout_per_command/HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if(timeout == 0){
2135 timeout = 1;
2136 }
2137 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
2138 srbcmd->retry_limit = 0; /* Obsolete parameter */
2139 srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
2140
2141 if( dev->dac_support == 1 ) {
2142 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
2143 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2144
2145 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2146 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2147 /*
2148 * Build Scatter/Gather list
2149 */
2150 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
2151 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
2152 sizeof (struct sgentry64));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002153 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 sizeof(struct aac_fibhdr)));
2155
2156 /*
2157 * Now send the Fib to the adapter
2158 */
2159 status = fib_send(ScsiPortCommand64, cmd_fibcontext,
2160 fibsize, FsaNormal, 0, 1,
2161 (fib_callback) aac_srb_callback,
2162 (void *) scsicmd);
2163 } else {
2164 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
2165 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2166
2167 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2168 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2169 /*
2170 * Build Scatter/Gather list
2171 */
2172 fibsize = sizeof (struct aac_srb) +
2173 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
2174 sizeof (struct sgentry));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002175 BUG_ON (fibsize > (dev->max_fib_size -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 sizeof(struct aac_fibhdr)));
2177
2178 /*
2179 * Now send the Fib to the adapter
2180 */
2181 status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
2182 (fib_callback) aac_srb_callback, (void *) scsicmd);
2183 }
2184 /*
2185 * Check that the command queued to the controller
2186 */
2187 if (status == -EINPROGRESS){
2188 return 0;
2189 }
2190
2191 printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
2192 fib_complete(cmd_fibcontext);
2193 fib_free(cmd_fibcontext);
2194
2195 return -1;
2196}
2197
2198static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2199{
2200 struct aac_dev *dev;
2201 unsigned long byte_count = 0;
2202
2203 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2204 // Get rid of old data
2205 psg->count = 0;
2206 psg->sg[0].addr = 0;
2207 psg->sg[0].count = 0;
2208 if (scsicmd->use_sg) {
2209 struct scatterlist *sg;
2210 int i;
2211 int sg_count;
2212 sg = (struct scatterlist *) scsicmd->request_buffer;
2213
2214 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2215 scsicmd->sc_data_direction);
2216 psg->count = cpu_to_le32(sg_count);
2217
2218 byte_count = 0;
2219
2220 for (i = 0; i < sg_count; i++) {
2221 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2222 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2223 byte_count += sg_dma_len(sg);
2224 sg++;
2225 }
2226 /* hba wants the size to be exact */
2227 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002228 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2229 (byte_count - scsicmd->request_bufflen);
2230 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 byte_count = scsicmd->request_bufflen;
2232 }
2233 /* Check for command underflow */
2234 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2235 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2236 byte_count, scsicmd->underflow);
2237 }
2238 }
2239 else if(scsicmd->request_bufflen) {
2240 dma_addr_t addr;
2241 addr = pci_map_single(dev->pdev,
2242 scsicmd->request_buffer,
2243 scsicmd->request_bufflen,
2244 scsicmd->sc_data_direction);
2245 psg->count = cpu_to_le32(1);
2246 psg->sg[0].addr = cpu_to_le32(addr);
2247 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
2248 scsicmd->SCp.dma_handle = addr;
2249 byte_count = scsicmd->request_bufflen;
2250 }
2251 return byte_count;
2252}
2253
2254
2255static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2256{
2257 struct aac_dev *dev;
2258 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002259 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2262 // Get rid of old data
2263 psg->count = 0;
2264 psg->sg[0].addr[0] = 0;
2265 psg->sg[0].addr[1] = 0;
2266 psg->sg[0].count = 0;
2267 if (scsicmd->use_sg) {
2268 struct scatterlist *sg;
2269 int i;
2270 int sg_count;
2271 sg = (struct scatterlist *) scsicmd->request_buffer;
2272
2273 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2274 scsicmd->sc_data_direction);
2275 psg->count = cpu_to_le32(sg_count);
2276
2277 byte_count = 0;
2278
2279 for (i = 0; i < sg_count; i++) {
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002280 addr = sg_dma_address(sg);
2281 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2282 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2284 byte_count += sg_dma_len(sg);
2285 sg++;
2286 }
2287 /* hba wants the size to be exact */
2288 if(byte_count > scsicmd->request_bufflen){
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002289 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2290 (byte_count - scsicmd->request_bufflen);
2291 psg->sg[i-1].count = cpu_to_le32(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 byte_count = scsicmd->request_bufflen;
2293 }
2294 /* Check for command underflow */
2295 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2296 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2297 byte_count, scsicmd->underflow);
2298 }
2299 }
2300 else if(scsicmd->request_bufflen) {
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002301 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 addr = pci_map_single(dev->pdev,
2303 scsicmd->request_buffer,
2304 scsicmd->request_bufflen,
2305 scsicmd->sc_data_direction);
2306 psg->count = cpu_to_le32(1);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07002307 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2308 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
2310 scsicmd->SCp.dma_handle = addr;
2311 byte_count = scsicmd->request_bufflen;
2312 }
2313 return byte_count;
2314}
2315
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002316static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2317{
2318 struct Scsi_Host *host = scsicmd->device->host;
2319 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2320 unsigned long byte_count = 0;
2321
2322 // Get rid of old data
2323 psg->count = 0;
2324 psg->sg[0].next = 0;
2325 psg->sg[0].prev = 0;
2326 psg->sg[0].addr[0] = 0;
2327 psg->sg[0].addr[1] = 0;
2328 psg->sg[0].count = 0;
2329 psg->sg[0].flags = 0;
2330 if (scsicmd->use_sg) {
2331 struct scatterlist *sg;
2332 int i;
2333 int sg_count;
2334 sg = (struct scatterlist *) scsicmd->request_buffer;
2335
2336 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2337 scsicmd->sc_data_direction);
2338
2339 for (i = 0; i < sg_count; i++) {
2340 int count = sg_dma_len(sg);
2341 u64 addr = sg_dma_address(sg);
2342 psg->sg[i].next = 0;
2343 psg->sg[i].prev = 0;
2344 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2345 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2346 psg->sg[i].count = cpu_to_le32(count);
2347 psg->sg[i].flags = 0;
2348 byte_count += count;
2349 sg++;
2350 }
2351 psg->count = cpu_to_le32(sg_count);
2352 /* hba wants the size to be exact */
2353 if(byte_count > scsicmd->request_bufflen){
2354 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2355 (byte_count - scsicmd->request_bufflen);
2356 psg->sg[i-1].count = cpu_to_le32(temp);
2357 byte_count = scsicmd->request_bufflen;
2358 }
2359 /* Check for command underflow */
2360 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2361 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2362 byte_count, scsicmd->underflow);
2363 }
2364 }
2365 else if(scsicmd->request_bufflen) {
2366 int count;
2367 u64 addr;
2368 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2369 scsicmd->request_buffer,
2370 scsicmd->request_bufflen,
2371 scsicmd->sc_data_direction);
2372 addr = scsicmd->SCp.dma_handle;
2373 count = scsicmd->request_bufflen;
2374 psg->count = cpu_to_le32(1);
2375 psg->sg[0].next = 0;
2376 psg->sg[0].prev = 0;
2377 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2378 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2379 psg->sg[0].count = cpu_to_le32(count);
2380 psg->sg[0].flags = 0;
2381 byte_count = scsicmd->request_bufflen;
2382 }
2383 return byte_count;
2384}
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386#ifdef AAC_DETAILED_STATUS_INFO
2387
2388struct aac_srb_status_info {
2389 u32 status;
2390 char *str;
2391};
2392
2393
2394static struct aac_srb_status_info srb_status_info[] = {
2395 { SRB_STATUS_PENDING, "Pending Status"},
2396 { SRB_STATUS_SUCCESS, "Success"},
2397 { SRB_STATUS_ABORTED, "Aborted Command"},
2398 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
2399 { SRB_STATUS_ERROR, "Error Event"},
2400 { SRB_STATUS_BUSY, "Device Busy"},
2401 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2402 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2403 { SRB_STATUS_NO_DEVICE, "No Device"},
2404 { SRB_STATUS_TIMEOUT, "Timeout"},
2405 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2406 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2407 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2408 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2409 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2410 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2411 { SRB_STATUS_NO_HBA, "No HBA"},
2412 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2413 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2414 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2415 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2416 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2417 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
2418 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
2419 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2420 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2421 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2422 { SRB_STATUS_NOT_STARTED, "Not Started"},
2423 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2424 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2425 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2426 { 0xff, "Unknown Error"}
2427};
2428
2429char *aac_get_status_string(u32 status)
2430{
2431 int i;
2432
2433 for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
2434 if(srb_status_info[i].status == status){
2435 return srb_status_info[i].str;
2436 }
2437 }
2438
2439 return "Bad Status Code";
2440}
2441
2442#endif