2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
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)
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.
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.
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
31 #include <linux/slab.h>
32 #include <linux/completion.h>
33 #include <linux/blkdev.h>
34 #include <linux/dma-mapping.h>
35 #include <asm/semaphore.h>
36 #include <asm/uaccess.h>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_host.h>
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 */
53 #define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
54 #define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
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
83 * Additional sense codes
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
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)
113 /*------------------------------------------------------------------------------
114 * S T R U C T S / T Y P E D E F S
115 *----------------------------------------------------------------------------*/
116 /* SCSI inquiry data */
117 struct 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 */
131 * M O D U L E G L O B A L S
134 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
135 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
136 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
137 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
138 #ifdef AAC_DETAILED_STATUS_INFO
139 static char *aac_get_status_string(u32 status);
143 * Non dasd selection is handled entirely in aachba now
146 static int nondasd = -1;
147 static int dacmode = -1;
150 int startup_timeout = 180;
151 int aif_timeout = 120;
153 module_param(nondasd, int, S_IRUGO|S_IWUSR);
154 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
155 module_param(dacmode, int, S_IRUGO|S_IWUSR);
156 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
157 module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
158 MODULE_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");
159 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
160 MODULE_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.");
161 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
162 MODULE_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.");
165 module_param(numacb, int, S_IRUGO|S_IWUSR);
166 MODULE_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.");
169 module_param(acbsize, int, S_IRUGO|S_IWUSR);
170 MODULE_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.");
172 int expose_physicals = -1;
173 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
174 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. -1=protect 0=off, 1=on");
176 int aac_reset_devices = 0;
177 module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
178 MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
180 static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
181 struct fib *fibptr) {
182 struct scsi_device *device;
184 if (unlikely(!scsicmd || !scsicmd->scsi_done )) {
185 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"))
187 aac_fib_complete(fibptr);
188 aac_fib_free(fibptr);
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);
203 * aac_get_config_status - check the adapter configuration
204 * @common: adapter to query
206 * Query config status, and commit the configuration if needed.
208 int aac_get_config_status(struct aac_dev *dev, int commit_flag)
213 if (!(fibptr = aac_fib_alloc(dev)))
216 aac_fib_init(fibptr);
218 struct aac_get_config_status *dinfo;
219 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
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));
226 status = aac_fib_send(ContainerCommand,
228 sizeof (struct aac_get_config_status),
233 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
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");
249 aac_fib_complete(fibptr);
250 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
252 if ((aac_commit == 1) || commit_flag) {
253 struct aac_commit_config * dinfo;
254 aac_fib_init(fibptr);
255 dinfo = (struct aac_commit_config *) fib_data(fibptr);
257 dinfo->command = cpu_to_le32(VM_ContainerConfig);
258 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
260 status = aac_fib_send(ContainerCommand,
262 sizeof (struct aac_commit_config),
266 aac_fib_complete(fibptr);
267 } else if (aac_commit == 0) {
269 "aac_get_config_status: Foreign device configurations are being ignored\n");
272 aac_fib_free(fibptr);
277 * aac_get_containers - list containers
278 * @common: adapter to probe
280 * Make a list of all containers on this controller
282 int aac_get_containers(struct aac_dev *dev)
284 struct fsa_dev_info *fsa_dev_ptr;
288 struct aac_get_container_count *dinfo;
289 struct aac_get_container_count_resp *dresp;
290 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
292 if (!(fibptr = aac_fib_alloc(dev)))
295 aac_fib_init(fibptr);
296 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);
300 status = aac_fib_send(ContainerCommand,
302 sizeof (struct aac_get_container_count),
307 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
308 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
309 aac_fib_complete(fibptr);
311 aac_fib_free(fibptr);
313 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
314 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
315 fsa_dev_ptr = kmalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
319 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
321 dev->fsa_dev = fsa_dev_ptr;
322 dev->maximum_num_containers = maximum_num_containers;
324 for (index = 0; index < dev->maximum_num_containers; ) {
325 fsa_dev_ptr[index].devname[0] = '\0';
327 status = aac_probe_container(dev, index);
330 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
335 * If there are no more containers, then stop asking.
337 if (++index >= status)
343 static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
347 struct scatterlist *sg = scsicmd->request_buffer;
349 if (scsicmd->use_sg) {
350 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
351 transfer_len = min(sg->length, len + offset);
353 buf = scsicmd->request_buffer;
354 transfer_len = min(scsicmd->request_bufflen, len + offset);
356 transfer_len -= offset;
357 if (buf && transfer_len > 0)
358 memcpy(buf + offset, data, transfer_len);
361 kunmap_atomic(buf - sg->offset, KM_IRQ0);
365 static void get_container_name_callback(void *context, struct fib * fibptr)
367 struct aac_get_name_resp * get_name_reply;
368 struct scsi_cmnd * scsicmd;
370 scsicmd = (struct scsi_cmnd *) context;
372 if (!aac_valid_context(scsicmd, fibptr))
375 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
376 BUG_ON(fibptr == NULL);
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')) {
382 char *sp = get_name_reply->data;
383 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
387 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
388 int count = sizeof(d);
391 *dp++ = (*sp) ? *sp++ : ' ';
392 } while (--count > 0);
393 aac_internal_transfer(scsicmd, d,
394 offsetof(struct inquiry_data, inqd_pid), sizeof(d));
398 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
400 aac_fib_complete(fibptr);
401 aac_fib_free(fibptr);
402 scsicmd->scsi_done(scsicmd);
406 * aac_get_container_name - get container name, none blocking.
408 static int aac_get_container_name(struct scsi_cmnd * scsicmd)
411 struct aac_get_name *dinfo;
412 struct fib * cmd_fibcontext;
413 struct aac_dev * dev;
415 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
417 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
420 aac_fib_init(cmd_fibcontext);
421 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
423 dinfo->command = cpu_to_le32(VM_ContainerConfig);
424 dinfo->type = cpu_to_le32(CT_READ_NAME);
425 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
426 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
428 status = aac_fib_send(ContainerCommand,
430 sizeof (struct aac_get_name),
433 (fib_callback) get_container_name_callback,
437 * Check that the command queued to the controller
439 if (status == -EINPROGRESS) {
440 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
444 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);
450 static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
452 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
454 if (fsa_dev_ptr[scmd_id(scsicmd)].valid)
455 return aac_scsi_cmd(scsicmd);
457 scsicmd->result = DID_NO_CONNECT << 16;
458 scsicmd->scsi_done(scsicmd);
462 static int _aac_probe_container2(void * context, struct fib * fibptr)
464 struct fsa_dev_info *fsa_dev_ptr;
465 int (*callback)(struct scsi_cmnd *);
466 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
468 if (!aac_valid_context(scsicmd, fibptr))
471 fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
473 scsicmd->SCp.Status = 0;
475 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
476 fsa_dev_ptr += scmd_id(scsicmd);
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);
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);
488 if ((fsa_dev_ptr->valid & 1) == 0)
489 fsa_dev_ptr->valid = 0;
490 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
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);
499 static int _aac_probe_container1(void * context, struct fib * fibptr)
501 struct scsi_cmnd * scsicmd;
502 struct aac_mount * dresp;
503 struct aac_query_mount *dinfo;
506 dresp = (struct aac_mount *) fib_data(fibptr);
507 dresp->mnt[0].capacityhigh = 0;
508 if ((le32_to_cpu(dresp->status) != ST_OK) ||
509 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE))
510 return _aac_probe_container2(context, fibptr);
511 scsicmd = (struct scsi_cmnd *) context;
512 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
514 if (!aac_valid_context(scsicmd, fibptr))
517 aac_fib_init(fibptr);
519 dinfo = (struct aac_query_mount *)fib_data(fibptr);
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);
525 status = aac_fib_send(ContainerCommand,
527 sizeof(struct aac_query_mount),
530 (fib_callback) _aac_probe_container2,
533 * Check that the command queued to the controller
535 if (status == -EINPROGRESS) {
536 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
540 /* Inherit results from VM_NameServe, if any */
541 dresp->status = cpu_to_le32(ST_OK);
542 return _aac_probe_container2(context, fibptr);
547 static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
550 int status = -ENOMEM;
552 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
553 struct aac_query_mount *dinfo;
555 aac_fib_init(fibptr);
557 dinfo = (struct aac_query_mount *)fib_data(fibptr);
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;
564 status = aac_fib_send(ContainerCommand,
566 sizeof(struct aac_query_mount),
569 (fib_callback) _aac_probe_container1,
572 * Check that the command queued to the controller
574 if (status == -EINPROGRESS) {
575 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
579 scsicmd->SCp.ptr = NULL;
580 aac_fib_complete(fibptr);
581 aac_fib_free(fibptr);
585 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
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);
598 * aac_probe_container - query a logical volume
599 * @dev: device to query
600 * @cid: container identifier
602 * Queries the controller about the given volume. The volume information
603 * is updated in the struct fsa_dev_info structure rather than returned.
605 static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
607 scsicmd->device = NULL;
611 int aac_probe_container(struct aac_dev *dev, int cid)
613 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
614 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
617 if (!scsicmd || !scsidev) {
622 scsicmd->list.next = NULL;
623 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))_aac_probe_container1;
625 scsicmd->device = scsidev;
626 scsidev->sdev_state = 0;
628 scsidev->host = dev->scsi_host_ptr;
630 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
631 while (scsicmd->device == scsidev)
634 status = scsicmd->SCp.Status;
639 /* Local Structure to set SCSI inquiry data strings */
641 char vid[8]; /* Vendor ID */
642 char pid[16]; /* Product ID */
643 char prl[4]; /* Product Revision Level */
647 * InqStrCopy - string merge
648 * @a: string to copy from
649 * @b: string to copy to
651 * Copy a String from one location to another
655 static void inqstrcpy(char *a, char *b)
662 static char *container_types[] = {
688 /* Function: setinqstr
690 * Arguments: [1] pointer to void [1] int
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.
697 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
699 struct scsi_inq *str;
701 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
702 memset(str, ' ', sizeof(*str));
704 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)
711 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
714 while (*cp && *cp != ' ')
718 /* last six chars reserved for vol type */
720 if (strlen(cp) > sizeof(str->pid)) {
721 c = cp[sizeof(str->pid)];
722 cp[sizeof(str->pid)] = '\0';
724 inqstrcpy (cp, str->pid);
726 cp[sizeof(str->pid)] = c;
728 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
730 inqstrcpy (mp->vname, str->vid);
731 /* last six chars reserved for vol type */
732 inqstrcpy (mp->model, str->pid);
735 if (tindex < ARRAY_SIZE(container_types)){
736 char *findit = str->pid;
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) = ' ';
742 if (((findit - str->pid) + strlen(container_types[tindex]))
743 < (sizeof(str->pid) + sizeof(str->prl)))
744 inqstrcpy (container_types[tindex], findit + 1);
746 inqstrcpy ("V1.0", str->prl);
749 static 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,
754 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
755 sense_buf[1] = 0; /* Segment number, always zero */
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);
764 sense_buf[2] = sense_key; /* Sense key */
766 if (sense_key == ILLEGAL_REQUEST)
767 sense_buf[7] = 10; /* Additional sense length */
769 sense_buf[7] = 6; /* Additional sense length */
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) {
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 */
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 */
789 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
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,
798 SENCODE_INTERNAL_TARGET_FAILURE,
799 ASENCODE_INTERNAL_TARGET_FAILURE, 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));
811 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
816 static void io_callback(void *context, struct fib * fibptr);
818 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
821 struct aac_raw_io *readcmd;
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(IO_TYPE_READ);
829 readcmd->bpTotal = 0;
830 readcmd->bpComplete = 0;
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)));
836 * Now send the Fib to the adapter
838 return aac_fib_send(ContainerRawIo,
843 (fib_callback) io_callback,
847 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
850 struct aac_read64 *readcmd;
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));
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)));
867 * Now send the Fib to the adapter
869 return aac_fib_send(ContainerCommand64,
874 (fib_callback) io_callback,
878 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
881 struct aac_read *readcmd;
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);
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)));
896 * Now send the Fib to the adapter
898 return aac_fib_send(ContainerCommand,
903 (fib_callback) io_callback,
907 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
910 struct aac_raw_io *writecmd;
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 = fua ?
918 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
919 cpu_to_le16(IO_TYPE_WRITE);
920 writecmd->bpTotal = 0;
921 writecmd->bpComplete = 0;
923 aac_build_sgraw(cmd, &writecmd->sg);
924 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
925 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
927 * Now send the Fib to the adapter
929 return aac_fib_send(ContainerRawIo,
934 (fib_callback) io_callback,
938 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
941 struct aac_write64 *writecmd;
943 writecmd = (struct aac_write64 *) fib_data(fib);
944 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
945 writecmd->cid = cpu_to_le16(scmd_id(cmd));
946 writecmd->sector_count = cpu_to_le16(count);
947 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
951 aac_build_sg64(cmd, &writecmd->sg);
952 fibsize = sizeof(struct aac_write64) +
953 ((le32_to_cpu(writecmd->sg.count) - 1) *
954 sizeof (struct sgentry64));
955 BUG_ON (fibsize > (fib->dev->max_fib_size -
956 sizeof(struct aac_fibhdr)));
958 * Now send the Fib to the adapter
960 return aac_fib_send(ContainerCommand64,
965 (fib_callback) io_callback,
969 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
972 struct aac_write *writecmd;
974 writecmd = (struct aac_write *) fib_data(fib);
975 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
976 writecmd->cid = cpu_to_le16(scmd_id(cmd));
977 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
978 writecmd->count = cpu_to_le32(count * 512);
979 writecmd->sg.count = cpu_to_le32(1);
980 /* ->stable is not used - it did mean which type of write */
982 aac_build_sg(cmd, &writecmd->sg);
983 fibsize = sizeof(struct aac_write) +
984 ((le32_to_cpu(writecmd->sg.count) - 1) *
985 sizeof (struct sgentry));
986 BUG_ON (fibsize > (fib->dev->max_fib_size -
987 sizeof(struct aac_fibhdr)));
989 * Now send the Fib to the adapter
991 return aac_fib_send(ContainerCommand,
996 (fib_callback) io_callback,
1000 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1002 struct aac_srb * srbcmd;
1007 switch(cmd->sc_data_direction){
1011 case DMA_BIDIRECTIONAL:
1012 flag = SRB_DataIn | SRB_DataOut;
1014 case DMA_FROM_DEVICE:
1018 default: /* shuts up some versions of gcc */
1019 flag = SRB_NoDataXfer;
1023 srbcmd = (struct aac_srb*) fib_data(fib);
1024 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1025 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1026 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1027 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1028 srbcmd->flags = cpu_to_le32(flag);
1029 timeout = cmd->timeout_per_command/HZ;
1032 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1033 srbcmd->retry_limit = 0; /* Obsolete parameter */
1034 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1038 static void aac_srb_callback(void *context, struct fib * fibptr);
1040 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1043 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1045 aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
1046 srbcmd->count = cpu_to_le32(cmd->request_bufflen);
1048 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1049 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1051 * Build Scatter/Gather list
1053 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1054 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1055 sizeof (struct sgentry64));
1056 BUG_ON (fibsize > (fib->dev->max_fib_size -
1057 sizeof(struct aac_fibhdr)));
1060 * Now send the Fib to the adapter
1062 return aac_fib_send(ScsiPortCommand64, fib,
1063 fibsize, FsaNormal, 0, 1,
1064 (fib_callback) aac_srb_callback,
1068 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1071 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1073 aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
1074 srbcmd->count = cpu_to_le32(cmd->request_bufflen);
1076 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1077 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1079 * Build Scatter/Gather list
1081 fibsize = sizeof (struct aac_srb) +
1082 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1083 sizeof (struct sgentry));
1084 BUG_ON (fibsize > (fib->dev->max_fib_size -
1085 sizeof(struct aac_fibhdr)));
1088 * Now send the Fib to the adapter
1090 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1091 (fib_callback) aac_srb_callback, (void *) cmd);
1094 int aac_get_adapter_info(struct aac_dev* dev)
1099 struct aac_adapter_info *info;
1100 struct aac_bus_info *command;
1101 struct aac_bus_info_response *bus_info;
1103 if (!(fibptr = aac_fib_alloc(dev)))
1106 aac_fib_init(fibptr);
1107 info = (struct aac_adapter_info *) fib_data(fibptr);
1108 memset(info,0,sizeof(*info));
1110 rcode = aac_fib_send(RequestAdapterInfo,
1114 -1, 1, /* First `interrupt' command uses special wait */
1119 aac_fib_complete(fibptr);
1120 aac_fib_free(fibptr);
1123 memcpy(&dev->adapter_info, info, sizeof(*info));
1125 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1126 struct aac_supplement_adapter_info * info;
1128 aac_fib_init(fibptr);
1130 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1132 memset(info,0,sizeof(*info));
1134 rcode = aac_fib_send(RequestSupplementAdapterInfo,
1143 memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
1151 aac_fib_init(fibptr);
1153 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1155 memset(bus_info, 0, sizeof(*bus_info));
1157 command = (struct aac_bus_info *)bus_info;
1159 command->Command = cpu_to_le32(VM_Ioctl);
1160 command->ObjType = cpu_to_le32(FT_DRIVE);
1161 command->MethodId = cpu_to_le32(1);
1162 command->CtlCmd = cpu_to_le32(GetBusInfo);
1164 rcode = aac_fib_send(ContainerCommand,
1171 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1172 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1173 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1176 if (!dev->in_reset) {
1177 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1178 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1184 le32_to_cpu(dev->adapter_info.kernelbuild),
1185 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1186 dev->supplement_adapter_info.BuildDate);
1187 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1188 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1190 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1191 le32_to_cpu(dev->adapter_info.monitorbuild));
1192 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1193 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
1195 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1196 le32_to_cpu(dev->adapter_info.biosbuild));
1197 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
1198 printk(KERN_INFO "%s%d: serial %x\n",
1200 le32_to_cpu(dev->adapter_info.serial[0]));
1201 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1202 printk(KERN_INFO "%s%d: TSID %.*s\n",
1204 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1205 dev->supplement_adapter_info.VpdInfo.Tsid);
1209 dev->nondasd_support = 0;
1210 dev->raid_scsi_mode = 0;
1211 if(dev->adapter_info.options & AAC_OPT_NONDASD){
1212 dev->nondasd_support = 1;
1216 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1217 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1218 * force nondasd support on. If we decide to allow the non-dasd flag
1219 * additional changes changes will have to be made to support
1220 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1221 * changed to support the new dev->raid_scsi_mode flag instead of
1222 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1223 * function aac_detect will have to be modified where it sets up the
1224 * max number of channels based on the aac->nondasd_support flag only.
1226 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1227 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1228 dev->nondasd_support = 1;
1229 dev->raid_scsi_mode = 1;
1231 if (dev->raid_scsi_mode != 0)
1232 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1233 dev->name, dev->id);
1236 dev->nondasd_support = (nondasd!=0);
1238 if(dev->nondasd_support != 0){
1239 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1242 dev->dac_support = 0;
1243 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1244 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1245 dev->dac_support = 1;
1249 dev->dac_support = (dacmode!=0);
1251 if(dev->dac_support != 0) {
1252 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1253 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
1254 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1255 dev->name, dev->id);
1256 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1257 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
1258 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1259 dev->name, dev->id);
1260 dev->dac_support = 0;
1262 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1263 dev->name, dev->id);
1268 * Deal with configuring for the individualized limits of each packet
1271 dev->a_ops.adapter_scsi = (dev->dac_support)
1274 if (dev->raw_io_interface) {
1275 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1278 dev->a_ops.adapter_read = aac_read_raw_io;
1279 dev->a_ops.adapter_write = aac_write_raw_io;
1281 dev->a_ops.adapter_bounds = aac_bounds_32;
1282 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
1283 sizeof(struct aac_fibhdr) -
1284 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1285 sizeof(struct sgentry);
1286 if (dev->dac_support) {
1287 dev->a_ops.adapter_read = aac_read_block64;
1288 dev->a_ops.adapter_write = aac_write_block64;
1290 * 38 scatter gather elements
1292 dev->scsi_host_ptr->sg_tablesize =
1293 (dev->max_fib_size -
1294 sizeof(struct aac_fibhdr) -
1295 sizeof(struct aac_write64) +
1296 sizeof(struct sgentry64)) /
1297 sizeof(struct sgentry64);
1299 dev->a_ops.adapter_read = aac_read_block;
1300 dev->a_ops.adapter_write = aac_write_block;
1302 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1303 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1305 * Worst case size that could cause sg overflow when
1306 * we break up SG elements that are larger than 64KB.
1307 * Would be nice if we could tell the SCSI layer what
1308 * the maximum SG element size can be. Worst case is
1309 * (sg_tablesize-1) 4KB elements with one 64KB
1311 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1313 dev->scsi_host_ptr->max_sectors =
1314 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1318 aac_fib_complete(fibptr);
1319 aac_fib_free(fibptr);
1325 static void io_callback(void *context, struct fib * fibptr)
1327 struct aac_dev *dev;
1328 struct aac_read_reply *readreply;
1329 struct scsi_cmnd *scsicmd;
1332 scsicmd = (struct scsi_cmnd *) context;
1334 if (!aac_valid_context(scsicmd, fibptr))
1337 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1338 cid = scmd_id(scsicmd);
1340 if (nblank(dprintk(x))) {
1342 switch (scsicmd->cmnd[0]) {
1345 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1346 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1350 lba = ((u64)scsicmd->cmnd[2] << 56) |
1351 ((u64)scsicmd->cmnd[3] << 48) |
1352 ((u64)scsicmd->cmnd[4] << 40) |
1353 ((u64)scsicmd->cmnd[5] << 32) |
1354 ((u64)scsicmd->cmnd[6] << 24) |
1355 (scsicmd->cmnd[7] << 16) |
1356 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1360 lba = ((u64)scsicmd->cmnd[2] << 24) |
1361 (scsicmd->cmnd[3] << 16) |
1362 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1365 lba = ((u64)scsicmd->cmnd[2] << 24) |
1366 (scsicmd->cmnd[3] << 16) |
1367 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1371 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1372 smp_processor_id(), (unsigned long long)lba, jiffies);
1375 BUG_ON(fibptr == NULL);
1378 pci_unmap_sg(dev->pdev,
1379 (struct scatterlist *)scsicmd->request_buffer,
1381 scsicmd->sc_data_direction);
1382 else if(scsicmd->request_bufflen)
1383 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
1384 scsicmd->request_bufflen,
1385 scsicmd->sc_data_direction);
1386 readreply = (struct aac_read_reply *)fib_data(fibptr);
1387 if (le32_to_cpu(readreply->status) == ST_OK)
1388 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1390 #ifdef AAC_DETAILED_STATUS_INFO
1391 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
1392 le32_to_cpu(readreply->status));
1394 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1395 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1397 SENCODE_INTERNAL_TARGET_FAILURE,
1398 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1400 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1401 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1402 ? sizeof(scsicmd->sense_buffer)
1403 : sizeof(dev->fsa_dev[cid].sense_data));
1405 aac_fib_complete(fibptr);
1406 aac_fib_free(fibptr);
1408 scsicmd->scsi_done(scsicmd);
1411 static int aac_read(struct scsi_cmnd * scsicmd)
1416 struct aac_dev *dev;
1417 struct fib * cmd_fibcontext;
1419 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1421 * Get block address and transfer length
1423 switch (scsicmd->cmnd[0]) {
1425 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
1427 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1428 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1429 count = scsicmd->cmnd[4];
1435 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
1437 lba = ((u64)scsicmd->cmnd[2] << 56) |
1438 ((u64)scsicmd->cmnd[3] << 48) |
1439 ((u64)scsicmd->cmnd[4] << 40) |
1440 ((u64)scsicmd->cmnd[5] << 32) |
1441 ((u64)scsicmd->cmnd[6] << 24) |
1442 (scsicmd->cmnd[7] << 16) |
1443 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1444 count = (scsicmd->cmnd[10] << 24) |
1445 (scsicmd->cmnd[11] << 16) |
1446 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1449 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
1451 lba = ((u64)scsicmd->cmnd[2] << 24) |
1452 (scsicmd->cmnd[3] << 16) |
1453 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1454 count = (scsicmd->cmnd[6] << 24) |
1455 (scsicmd->cmnd[7] << 16) |
1456 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1459 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
1461 lba = ((u64)scsicmd->cmnd[2] << 24) |
1462 (scsicmd->cmnd[3] << 16) |
1463 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1464 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1467 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
1468 smp_processor_id(), (unsigned long long)lba, jiffies));
1469 if (aac_adapter_bounds(dev,scsicmd,lba))
1472 * Alocate and initialize a Fib
1474 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1478 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
1481 * Check that the command queued to the controller
1483 if (status == -EINPROGRESS) {
1484 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1488 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
1490 * For some reason, the Fib didn't queue, return QUEUE_FULL
1492 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1493 scsicmd->scsi_done(scsicmd);
1494 aac_fib_complete(cmd_fibcontext);
1495 aac_fib_free(cmd_fibcontext);
1499 static int aac_write(struct scsi_cmnd * scsicmd)
1505 struct aac_dev *dev;
1506 struct fib * cmd_fibcontext;
1508 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1510 * Get block address and transfer length
1512 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1514 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1515 count = scsicmd->cmnd[4];
1519 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1520 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
1522 lba = ((u64)scsicmd->cmnd[2] << 56) |
1523 ((u64)scsicmd->cmnd[3] << 48) |
1524 ((u64)scsicmd->cmnd[4] << 40) |
1525 ((u64)scsicmd->cmnd[5] << 32) |
1526 ((u64)scsicmd->cmnd[6] << 24) |
1527 (scsicmd->cmnd[7] << 16) |
1528 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1529 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1530 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1531 fua = scsicmd->cmnd[1] & 0x8;
1532 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1533 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
1535 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1536 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1537 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1538 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1539 fua = scsicmd->cmnd[1] & 0x8;
1541 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
1542 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1543 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1544 fua = scsicmd->cmnd[1] & 0x8;
1546 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
1547 smp_processor_id(), (unsigned long long)lba, jiffies));
1548 if (aac_adapter_bounds(dev,scsicmd,lba))
1551 * Allocate and initialize a Fib then setup a BlockWrite command
1553 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1554 scsicmd->result = DID_ERROR << 16;
1555 scsicmd->scsi_done(scsicmd);
1559 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
1562 * Check that the command queued to the controller
1564 if (status == -EINPROGRESS) {
1565 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1569 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
1571 * For some reason, the Fib didn't queue, return QUEUE_FULL
1573 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1574 scsicmd->scsi_done(scsicmd);
1576 aac_fib_complete(cmd_fibcontext);
1577 aac_fib_free(cmd_fibcontext);
1581 static void synchronize_callback(void *context, struct fib *fibptr)
1583 struct aac_synchronize_reply *synchronizereply;
1584 struct scsi_cmnd *cmd;
1588 if (!aac_valid_context(cmd, fibptr))
1591 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1592 smp_processor_id(), jiffies));
1593 BUG_ON(fibptr == NULL);
1596 synchronizereply = fib_data(fibptr);
1597 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1598 cmd->result = DID_OK << 16 |
1599 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1601 struct scsi_device *sdev = cmd->device;
1602 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
1603 u32 cid = sdev_id(sdev);
1605 "synchronize_callback: synchronize failed, status = %d\n",
1606 le32_to_cpu(synchronizereply->status));
1607 cmd->result = DID_OK << 16 |
1608 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1609 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1611 SENCODE_INTERNAL_TARGET_FAILURE,
1612 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1614 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1615 min(sizeof(dev->fsa_dev[cid].sense_data),
1616 sizeof(cmd->sense_buffer)));
1619 aac_fib_complete(fibptr);
1620 aac_fib_free(fibptr);
1621 cmd->scsi_done(cmd);
1624 static int aac_synchronize(struct scsi_cmnd *scsicmd)
1627 struct fib *cmd_fibcontext;
1628 struct aac_synchronize *synchronizecmd;
1629 struct scsi_cmnd *cmd;
1630 struct scsi_device *sdev = scsicmd->device;
1632 struct aac_dev *aac;
1633 unsigned long flags;
1636 * Wait for all outstanding queued commands to complete to this
1637 * specific target (block).
1639 spin_lock_irqsave(&sdev->list_lock, flags);
1640 list_for_each_entry(cmd, &sdev->cmd_list, list)
1641 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1646 spin_unlock_irqrestore(&sdev->list_lock, flags);
1649 * Yield the processor (requeue for later)
1652 return SCSI_MLQUEUE_DEVICE_BUSY;
1654 aac = (struct aac_dev *)scsicmd->device->host->hostdata;
1656 return SCSI_MLQUEUE_HOST_BUSY;
1659 * Allocate and initialize a Fib
1661 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
1662 return SCSI_MLQUEUE_HOST_BUSY;
1664 aac_fib_init(cmd_fibcontext);
1666 synchronizecmd = fib_data(cmd_fibcontext);
1667 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1668 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1669 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
1670 synchronizecmd->count =
1671 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1674 * Now send the Fib to the adapter
1676 status = aac_fib_send(ContainerCommand,
1678 sizeof(struct aac_synchronize),
1681 (fib_callback)synchronize_callback,
1685 * Check that the command queued to the controller
1687 if (status == -EINPROGRESS) {
1688 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1693 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1694 aac_fib_complete(cmd_fibcontext);
1695 aac_fib_free(cmd_fibcontext);
1696 return SCSI_MLQUEUE_HOST_BUSY;
1700 * aac_scsi_cmd() - Process SCSI command
1701 * @scsicmd: SCSI command block
1703 * Emulate a SCSI command and queue the required request for the
1707 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1710 struct Scsi_Host *host = scsicmd->device->host;
1711 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1712 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1714 if (fsa_dev_ptr == NULL)
1717 * If the bus, id or lun is out of range, return fail
1718 * Test does not apply to ID 16, the pseudo id for the controller
1721 if (scmd_id(scsicmd) != host->this_id) {
1722 if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1723 if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1724 (scsicmd->device->lun != 0)) {
1725 scsicmd->result = DID_NO_CONNECT << 16;
1726 scsicmd->scsi_done(scsicmd);
1729 cid = scmd_id(scsicmd);
1732 * If the target container doesn't exist, it may have
1733 * been newly created
1735 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1736 switch (scsicmd->cmnd[0]) {
1737 case SERVICE_ACTION_IN:
1738 if (!(dev->raw_io_interface) ||
1739 !(dev->raw_io_64) ||
1740 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1744 case TEST_UNIT_READY:
1747 return _aac_probe_container(scsicmd,
1748 aac_probe_container_callback2);
1753 } else { /* check for physical non-dasd devices */
1754 if ((dev->nondasd_support == 1) || expose_physicals) {
1757 return aac_send_srb_fib(scsicmd);
1759 scsicmd->result = DID_NO_CONNECT << 16;
1760 scsicmd->scsi_done(scsicmd);
1766 * else Command for the controller itself
1768 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1769 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1771 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1772 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1773 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1775 SENCODE_INVALID_COMMAND,
1776 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1777 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1778 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1779 ? sizeof(scsicmd->sense_buffer)
1780 : sizeof(dev->fsa_dev[cid].sense_data));
1781 scsicmd->scsi_done(scsicmd);
1786 /* Handle commands here that don't really require going out to the adapter */
1787 switch (scsicmd->cmnd[0]) {
1790 struct inquiry_data inq_data;
1792 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
1793 memset(&inq_data, 0, sizeof (struct inquiry_data));
1795 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
1796 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 */
1797 inq_data.inqd_len = 31;
1798 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
1799 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
1801 * Set the Vendor, Product, and Revision Level
1802 * see: <vendor>.c i.e. aac.c
1804 if (scmd_id(scsicmd) == host->this_id) {
1805 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
1806 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1807 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1808 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1809 scsicmd->scsi_done(scsicmd);
1814 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
1815 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1816 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1817 return aac_get_container_name(scsicmd);
1819 case SERVICE_ACTION_IN:
1820 if (!(dev->raw_io_interface) ||
1821 !(dev->raw_io_64) ||
1822 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1828 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1829 capacity = fsa_dev_ptr[cid].size - 1;
1830 cp[0] = (capacity >> 56) & 0xff;
1831 cp[1] = (capacity >> 48) & 0xff;
1832 cp[2] = (capacity >> 40) & 0xff;
1833 cp[3] = (capacity >> 32) & 0xff;
1834 cp[4] = (capacity >> 24) & 0xff;
1835 cp[5] = (capacity >> 16) & 0xff;
1836 cp[6] = (capacity >> 8) & 0xff;
1837 cp[7] = (capacity >> 0) & 0xff;
1843 aac_internal_transfer(scsicmd, cp, 0,
1844 min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
1845 if (sizeof(cp) < scsicmd->cmnd[13]) {
1846 unsigned int len, offset = sizeof(cp);
1848 memset(cp, 0, offset);
1850 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1852 aac_internal_transfer(scsicmd, cp, offset, len);
1853 } while ((offset += len) < scsicmd->cmnd[13]);
1856 /* Do not cache partition table for arrays */
1857 scsicmd->device->removable = 1;
1859 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1860 scsicmd->scsi_done(scsicmd);
1870 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
1871 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
1872 capacity = fsa_dev_ptr[cid].size - 1;
1876 cp[0] = (capacity >> 24) & 0xff;
1877 cp[1] = (capacity >> 16) & 0xff;
1878 cp[2] = (capacity >> 8) & 0xff;
1879 cp[3] = (capacity >> 0) & 0xff;
1884 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
1885 /* Do not cache partition table for arrays */
1886 scsicmd->device->removable = 1;
1888 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1889 scsicmd->scsi_done(scsicmd);
1897 int mode_buf_length = 4;
1899 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1900 mode_buf[0] = 3; /* Mode data length */
1901 mode_buf[1] = 0; /* Medium type - default */
1902 mode_buf[2] = 0; /* Device-specific param,
1903 bit 8: 0/1 = write enabled/protected
1904 bit 4: 0/1 = FUA enabled */
1905 if (dev->raw_io_interface)
1907 mode_buf[3] = 0; /* Block descriptor length */
1908 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
1909 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
1913 mode_buf[6] = 0x04; /* WCE */
1914 mode_buf_length = 7;
1915 if (mode_buf_length > scsicmd->cmnd[4])
1916 mode_buf_length = scsicmd->cmnd[4];
1918 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
1919 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1920 scsicmd->scsi_done(scsicmd);
1927 int mode_buf_length = 8;
1929 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1930 mode_buf[0] = 0; /* Mode data length (MSB) */
1931 mode_buf[1] = 6; /* Mode data length (LSB) */
1932 mode_buf[2] = 0; /* Medium type - default */
1933 mode_buf[3] = 0; /* Device-specific param,
1934 bit 8: 0/1 = write enabled/protected
1935 bit 4: 0/1 = FUA enabled */
1936 if (dev->raw_io_interface)
1938 mode_buf[4] = 0; /* reserved */
1939 mode_buf[5] = 0; /* reserved */
1940 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1941 mode_buf[7] = 0; /* Block descriptor length (LSB) */
1942 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
1943 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
1947 mode_buf[10] = 0x04; /* WCE */
1948 mode_buf_length = 11;
1949 if (mode_buf_length > scsicmd->cmnd[8])
1950 mode_buf_length = scsicmd->cmnd[8];
1952 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
1954 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1955 scsicmd->scsi_done(scsicmd);
1960 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1961 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1962 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1963 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1964 scsicmd->scsi_done(scsicmd);
1967 case ALLOW_MEDIUM_REMOVAL:
1968 dprintk((KERN_DEBUG "LOCK command.\n"));
1969 if (scsicmd->cmnd[4])
1970 fsa_dev_ptr[cid].locked = 1;
1972 fsa_dev_ptr[cid].locked = 0;
1974 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1975 scsicmd->scsi_done(scsicmd);
1978 * These commands are all No-Ops
1980 case TEST_UNIT_READY:
1984 case REASSIGN_BLOCKS:
1987 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1988 scsicmd->scsi_done(scsicmd);
1992 switch (scsicmd->cmnd[0])
2001 * Hack to keep track of ordinal number of the device that
2002 * corresponds to a container. Needed to convert
2003 * containers to /dev/sd device names
2006 if (scsicmd->request->rq_disk)
2007 strlcpy(fsa_dev_ptr[cid].devname,
2008 scsicmd->request->rq_disk->disk_name,
2009 min(sizeof(fsa_dev_ptr[cid].devname),
2010 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
2012 return aac_read(scsicmd);
2020 return aac_write(scsicmd);
2022 case SYNCHRONIZE_CACHE:
2023 /* Issue FIB to tell Firmware to flush it's cache */
2024 return aac_synchronize(scsicmd);
2028 * Unhandled commands
2030 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
2031 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2032 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
2033 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2034 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
2035 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2036 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
2037 ? sizeof(scsicmd->sense_buffer)
2038 : sizeof(dev->fsa_dev[cid].sense_data));
2039 scsicmd->scsi_done(scsicmd);
2044 static int query_disk(struct aac_dev *dev, void __user *arg)
2046 struct aac_query_disk qd;
2047 struct fsa_dev_info *fsa_dev_ptr;
2049 fsa_dev_ptr = dev->fsa_dev;
2052 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2056 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
2058 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2060 qd.instance = dev->scsi_host_ptr->host_no;
2062 qd.id = CONTAINER_TO_ID(qd.cnum);
2063 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2065 else return -EINVAL;
2067 qd.valid = fsa_dev_ptr[qd.cnum].valid;
2068 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2069 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2071 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2076 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2077 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2079 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2084 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2086 struct aac_delete_disk dd;
2087 struct fsa_dev_info *fsa_dev_ptr;
2089 fsa_dev_ptr = dev->fsa_dev;
2093 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2096 if (dd.cnum >= dev->maximum_num_containers)
2099 * Mark this container as being deleted.
2101 fsa_dev_ptr[dd.cnum].deleted = 1;
2103 * Mark the container as no longer valid
2105 fsa_dev_ptr[dd.cnum].valid = 0;
2109 static int delete_disk(struct aac_dev *dev, void __user *arg)
2111 struct aac_delete_disk dd;
2112 struct fsa_dev_info *fsa_dev_ptr;
2114 fsa_dev_ptr = dev->fsa_dev;
2118 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2121 if (dd.cnum >= dev->maximum_num_containers)
2124 * If the container is locked, it can not be deleted by the API.
2126 if (fsa_dev_ptr[dd.cnum].locked)
2130 * Mark the container as no longer being valid.
2132 fsa_dev_ptr[dd.cnum].valid = 0;
2133 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2138 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2141 case FSACTL_QUERY_DISK:
2142 return query_disk(dev, arg);
2143 case FSACTL_DELETE_DISK:
2144 return delete_disk(dev, arg);
2145 case FSACTL_FORCE_DELETE_DISK:
2146 return force_delete_disk(dev, arg);
2147 case FSACTL_GET_CONTAINERS:
2148 return aac_get_containers(dev);
2157 * @context: the context set in the fib - here it is scsi cmd
2158 * @fibptr: pointer to the fib
2160 * Handles the completion of a scsi command to a non dasd device
2164 static void aac_srb_callback(void *context, struct fib * fibptr)
2166 struct aac_dev *dev;
2167 struct aac_srb_reply *srbreply;
2168 struct scsi_cmnd *scsicmd;
2170 scsicmd = (struct scsi_cmnd *) context;
2172 if (!aac_valid_context(scsicmd, fibptr))
2175 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2177 BUG_ON(fibptr == NULL);
2179 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2181 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
2183 * Calculate resid for sg
2186 scsicmd->resid = scsicmd->request_bufflen -
2187 le32_to_cpu(srbreply->data_xfer_length);
2190 pci_unmap_sg(dev->pdev,
2191 (struct scatterlist *)scsicmd->request_buffer,
2193 scsicmd->sc_data_direction);
2194 else if(scsicmd->request_bufflen)
2195 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
2196 scsicmd->sc_data_direction);
2199 * First check the fib status
2202 if (le32_to_cpu(srbreply->status) != ST_OK){
2204 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2205 len = (le32_to_cpu(srbreply->sense_data_size) >
2206 sizeof(scsicmd->sense_buffer)) ?
2207 sizeof(scsicmd->sense_buffer) :
2208 le32_to_cpu(srbreply->sense_data_size);
2209 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2210 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2214 * Next check the srb status
2216 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2217 case SRB_STATUS_ERROR_RECOVERY:
2218 case SRB_STATUS_PENDING:
2219 case SRB_STATUS_SUCCESS:
2220 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2222 case SRB_STATUS_DATA_OVERRUN:
2223 switch(scsicmd->cmnd[0]){
2232 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2233 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2235 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2237 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2240 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2244 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2248 case SRB_STATUS_ABORTED:
2249 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2251 case SRB_STATUS_ABORT_FAILED:
2252 // Not sure about this one - but assuming the hba was trying to abort for some reason
2253 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2255 case SRB_STATUS_PARITY_ERROR:
2256 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2258 case SRB_STATUS_NO_DEVICE:
2259 case SRB_STATUS_INVALID_PATH_ID:
2260 case SRB_STATUS_INVALID_TARGET_ID:
2261 case SRB_STATUS_INVALID_LUN:
2262 case SRB_STATUS_SELECTION_TIMEOUT:
2263 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2266 case SRB_STATUS_COMMAND_TIMEOUT:
2267 case SRB_STATUS_TIMEOUT:
2268 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2271 case SRB_STATUS_BUSY:
2272 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2275 case SRB_STATUS_BUS_RESET:
2276 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2279 case SRB_STATUS_MESSAGE_REJECTED:
2280 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2282 case SRB_STATUS_REQUEST_FLUSHED:
2283 case SRB_STATUS_ERROR:
2284 case SRB_STATUS_INVALID_REQUEST:
2285 case SRB_STATUS_REQUEST_SENSE_FAILED:
2286 case SRB_STATUS_NO_HBA:
2287 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2288 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2289 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2290 case SRB_STATUS_DELAYED_RETRY:
2291 case SRB_STATUS_BAD_FUNCTION:
2292 case SRB_STATUS_NOT_STARTED:
2293 case SRB_STATUS_NOT_IN_USE:
2294 case SRB_STATUS_FORCE_ABORT:
2295 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2297 #ifdef AAC_DETAILED_STATUS_INFO
2298 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2299 le32_to_cpu(srbreply->srb_status) & 0x3F,
2300 aac_get_status_string(
2301 le32_to_cpu(srbreply->srb_status) & 0x3F),
2303 le32_to_cpu(srbreply->scsi_status));
2305 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2308 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
2310 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2311 len = (le32_to_cpu(srbreply->sense_data_size) >
2312 sizeof(scsicmd->sense_buffer)) ?
2313 sizeof(scsicmd->sense_buffer) :
2314 le32_to_cpu(srbreply->sense_data_size);
2315 #ifdef AAC_DETAILED_STATUS_INFO
2316 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2317 le32_to_cpu(srbreply->status), len);
2319 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2323 * OR in the scsi status (already shifted up a bit)
2325 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2327 aac_fib_complete(fibptr);
2328 aac_fib_free(fibptr);
2329 scsicmd->scsi_done(scsicmd);
2335 * @scsicmd: the scsi command block
2337 * This routine will form a FIB and fill in the aac_srb from the
2338 * scsicmd passed in.
2341 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2343 struct fib* cmd_fibcontext;
2344 struct aac_dev* dev;
2347 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2348 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
2349 scsicmd->device->lun > 7) {
2350 scsicmd->result = DID_NO_CONNECT << 16;
2351 scsicmd->scsi_done(scsicmd);
2356 * Allocate and initialize a Fib then setup a BlockWrite command
2358 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
2361 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
2364 * Check that the command queued to the controller
2366 if (status == -EINPROGRESS) {
2367 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2371 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2372 aac_fib_complete(cmd_fibcontext);
2373 aac_fib_free(cmd_fibcontext);
2378 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2380 struct aac_dev *dev;
2381 unsigned long byte_count = 0;
2383 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2384 // Get rid of old data
2386 psg->sg[0].addr = 0;
2387 psg->sg[0].count = 0;
2388 if (scsicmd->use_sg) {
2389 struct scatterlist *sg;
2392 sg = (struct scatterlist *) scsicmd->request_buffer;
2394 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2395 scsicmd->sc_data_direction);
2396 psg->count = cpu_to_le32(sg_count);
2398 for (i = 0; i < sg_count; i++) {
2399 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2400 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2401 byte_count += sg_dma_len(sg);
2404 /* hba wants the size to be exact */
2405 if(byte_count > scsicmd->request_bufflen){
2406 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2407 (byte_count - scsicmd->request_bufflen);
2408 psg->sg[i-1].count = cpu_to_le32(temp);
2409 byte_count = scsicmd->request_bufflen;
2411 /* Check for command underflow */
2412 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2413 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2414 byte_count, scsicmd->underflow);
2417 else if(scsicmd->request_bufflen) {
2419 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2420 scsicmd->request_buffer,
2421 scsicmd->request_bufflen,
2422 scsicmd->sc_data_direction);
2423 addr = scsicmd->SCp.dma_handle;
2424 psg->count = cpu_to_le32(1);
2425 psg->sg[0].addr = cpu_to_le32(addr);
2426 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
2427 byte_count = scsicmd->request_bufflen;
2433 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2435 struct aac_dev *dev;
2436 unsigned long byte_count = 0;
2439 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2440 // Get rid of old data
2442 psg->sg[0].addr[0] = 0;
2443 psg->sg[0].addr[1] = 0;
2444 psg->sg[0].count = 0;
2445 if (scsicmd->use_sg) {
2446 struct scatterlist *sg;
2449 sg = (struct scatterlist *) scsicmd->request_buffer;
2451 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2452 scsicmd->sc_data_direction);
2454 for (i = 0; i < sg_count; i++) {
2455 int count = sg_dma_len(sg);
2456 addr = sg_dma_address(sg);
2457 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2458 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
2459 psg->sg[i].count = cpu_to_le32(count);
2460 byte_count += count;
2463 psg->count = cpu_to_le32(sg_count);
2464 /* hba wants the size to be exact */
2465 if(byte_count > scsicmd->request_bufflen){
2466 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2467 (byte_count - scsicmd->request_bufflen);
2468 psg->sg[i-1].count = cpu_to_le32(temp);
2469 byte_count = scsicmd->request_bufflen;
2471 /* Check for command underflow */
2472 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2473 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2474 byte_count, scsicmd->underflow);
2477 else if(scsicmd->request_bufflen) {
2478 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2479 scsicmd->request_buffer,
2480 scsicmd->request_bufflen,
2481 scsicmd->sc_data_direction);
2482 addr = scsicmd->SCp.dma_handle;
2483 psg->count = cpu_to_le32(1);
2484 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2485 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
2486 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
2487 byte_count = scsicmd->request_bufflen;
2492 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2494 struct Scsi_Host *host = scsicmd->device->host;
2495 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2496 unsigned long byte_count = 0;
2498 // Get rid of old data
2500 psg->sg[0].next = 0;
2501 psg->sg[0].prev = 0;
2502 psg->sg[0].addr[0] = 0;
2503 psg->sg[0].addr[1] = 0;
2504 psg->sg[0].count = 0;
2505 psg->sg[0].flags = 0;
2506 if (scsicmd->use_sg) {
2507 struct scatterlist *sg;
2510 sg = (struct scatterlist *) scsicmd->request_buffer;
2512 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2513 scsicmd->sc_data_direction);
2515 for (i = 0; i < sg_count; i++) {
2516 int count = sg_dma_len(sg);
2517 u64 addr = sg_dma_address(sg);
2518 psg->sg[i].next = 0;
2519 psg->sg[i].prev = 0;
2520 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2521 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2522 psg->sg[i].count = cpu_to_le32(count);
2523 psg->sg[i].flags = 0;
2524 byte_count += count;
2527 psg->count = cpu_to_le32(sg_count);
2528 /* hba wants the size to be exact */
2529 if(byte_count > scsicmd->request_bufflen){
2530 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2531 (byte_count - scsicmd->request_bufflen);
2532 psg->sg[i-1].count = cpu_to_le32(temp);
2533 byte_count = scsicmd->request_bufflen;
2535 /* Check for command underflow */
2536 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2537 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2538 byte_count, scsicmd->underflow);
2541 else if(scsicmd->request_bufflen) {
2544 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2545 scsicmd->request_buffer,
2546 scsicmd->request_bufflen,
2547 scsicmd->sc_data_direction);
2548 addr = scsicmd->SCp.dma_handle;
2549 count = scsicmd->request_bufflen;
2550 psg->count = cpu_to_le32(1);
2551 psg->sg[0].next = 0;
2552 psg->sg[0].prev = 0;
2553 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2554 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2555 psg->sg[0].count = cpu_to_le32(count);
2556 psg->sg[0].flags = 0;
2557 byte_count = scsicmd->request_bufflen;
2562 #ifdef AAC_DETAILED_STATUS_INFO
2564 struct aac_srb_status_info {
2570 static struct aac_srb_status_info srb_status_info[] = {
2571 { SRB_STATUS_PENDING, "Pending Status"},
2572 { SRB_STATUS_SUCCESS, "Success"},
2573 { SRB_STATUS_ABORTED, "Aborted Command"},
2574 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
2575 { SRB_STATUS_ERROR, "Error Event"},
2576 { SRB_STATUS_BUSY, "Device Busy"},
2577 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
2578 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
2579 { SRB_STATUS_NO_DEVICE, "No Device"},
2580 { SRB_STATUS_TIMEOUT, "Timeout"},
2581 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2582 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
2583 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
2584 { SRB_STATUS_BUS_RESET, "Bus Reset"},
2585 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
2586 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2587 { SRB_STATUS_NO_HBA, "No HBA"},
2588 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
2589 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2590 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2591 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2592 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
2593 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
2594 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
2595 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2596 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
2597 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
2598 { SRB_STATUS_NOT_STARTED, "Not Started"},
2599 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
2600 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
2601 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2602 { 0xff, "Unknown Error"}
2605 char *aac_get_status_string(u32 status)
2609 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2610 if (srb_status_info[i].status == status)
2611 return srb_status_info[i].str;
2613 return "Bad Status Code";