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 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/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>
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 */
56 #define MAX_FIB_DATA (sizeof(struct hw_fib) - sizeof(FIB_HEADER))
58 #define MAX_DRIVER_SG_SEGMENT_COUNT 17
64 #define SENCODE_NO_SENSE 0x00
65 #define SENCODE_END_OF_DATA 0x00
66 #define SENCODE_BECOMING_READY 0x04
67 #define SENCODE_INIT_CMD_REQUIRED 0x04
68 #define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
69 #define SENCODE_INVALID_COMMAND 0x20
70 #define SENCODE_LBA_OUT_OF_RANGE 0x21
71 #define SENCODE_INVALID_CDB_FIELD 0x24
72 #define SENCODE_LUN_NOT_SUPPORTED 0x25
73 #define SENCODE_INVALID_PARAM_FIELD 0x26
74 #define SENCODE_PARAM_NOT_SUPPORTED 0x26
75 #define SENCODE_PARAM_VALUE_INVALID 0x26
76 #define SENCODE_RESET_OCCURRED 0x29
77 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
78 #define SENCODE_INQUIRY_DATA_CHANGED 0x3F
79 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
80 #define SENCODE_DIAGNOSTIC_FAILURE 0x40
81 #define SENCODE_INTERNAL_TARGET_FAILURE 0x44
82 #define SENCODE_INVALID_MESSAGE_ERROR 0x49
83 #define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
84 #define SENCODE_OVERLAPPED_COMMAND 0x4E
87 * Additional sense codes
90 #define ASENCODE_NO_SENSE 0x00
91 #define ASENCODE_END_OF_DATA 0x05
92 #define ASENCODE_BECOMING_READY 0x01
93 #define ASENCODE_INIT_CMD_REQUIRED 0x02
94 #define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
95 #define ASENCODE_INVALID_COMMAND 0x00
96 #define ASENCODE_LBA_OUT_OF_RANGE 0x00
97 #define ASENCODE_INVALID_CDB_FIELD 0x00
98 #define ASENCODE_LUN_NOT_SUPPORTED 0x00
99 #define ASENCODE_INVALID_PARAM_FIELD 0x00
100 #define ASENCODE_PARAM_NOT_SUPPORTED 0x01
101 #define ASENCODE_PARAM_VALUE_INVALID 0x02
102 #define ASENCODE_RESET_OCCURRED 0x00
103 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
104 #define ASENCODE_INQUIRY_DATA_CHANGED 0x03
105 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
106 #define ASENCODE_DIAGNOSTIC_FAILURE 0x80
107 #define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
108 #define ASENCODE_INVALID_MESSAGE_ERROR 0x00
109 #define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
110 #define ASENCODE_OVERLAPPED_COMMAND 0x00
112 #define BYTE0(x) (unsigned char)(x)
113 #define BYTE1(x) (unsigned char)((x) >> 8)
114 #define BYTE2(x) (unsigned char)((x) >> 16)
115 #define BYTE3(x) (unsigned char)((x) >> 24)
117 /*------------------------------------------------------------------------------
118 * S T R U C T S / T Y P E D E F S
119 *----------------------------------------------------------------------------*/
120 /* SCSI inquiry data */
121 struct inquiry_data {
122 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
123 u8 inqd_dtq; /* RMB | Device Type Qualifier */
124 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
125 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
126 u8 inqd_len; /* Additional length (n-4) */
127 u8 inqd_pad1[2];/* Reserved - must be zero */
128 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
129 u8 inqd_vid[8]; /* Vendor ID */
130 u8 inqd_pid[16];/* Product ID */
131 u8 inqd_prl[4]; /* Product Revision Level */
135 * M O D U L E G L O B A L S
138 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
139 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
140 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
141 #ifdef AAC_DETAILED_STATUS_INFO
142 static char *aac_get_status_string(u32 status);
146 * Non dasd selection is handled entirely in aachba now
149 static int nondasd = -1;
150 static int dacmode = -1;
152 static int commit = -1;
154 module_param(nondasd, int, 0);
155 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
156 module_param(dacmode, int, 0);
157 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
158 module_param(commit, int, 0);
159 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");
162 * aac_get_config_status - check the adapter configuration
163 * @common: adapter to query
165 * Query config status, and commit the configuration if needed.
167 int aac_get_config_status(struct aac_dev *dev)
172 if (!(fibptr = fib_alloc(dev)))
177 struct aac_get_config_status *dinfo;
178 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
180 dinfo->command = cpu_to_le32(VM_ContainerConfig);
181 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
182 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
185 status = fib_send(ContainerCommand,
187 sizeof (struct aac_get_config_status),
192 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
194 struct aac_get_config_status_resp *reply
195 = (struct aac_get_config_status_resp *) fib_data(fibptr);
196 dprintk((KERN_WARNING
197 "aac_get_config_status: response=%d status=%d action=%d\n",
198 reply->response, reply->status, reply->data.action));
199 if ((reply->response != ST_OK)
200 || (reply->status != CT_OK)
201 || (reply->data.action > CFACT_PAUSE)) {
202 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
206 fib_complete(fibptr);
207 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
210 struct aac_commit_config * dinfo;
212 dinfo = (struct aac_commit_config *) fib_data(fibptr);
214 dinfo->command = cpu_to_le32(VM_ContainerConfig);
215 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
217 status = fib_send(ContainerCommand,
219 sizeof (struct aac_commit_config),
223 fib_complete(fibptr);
224 } else if (commit == 0) {
226 "aac_get_config_status: Foreign device configurations are being ignored\n");
234 * aac_get_containers - list containers
235 * @common: adapter to probe
237 * Make a list of all containers on this controller
239 int aac_get_containers(struct aac_dev *dev)
241 struct fsa_dev_info *fsa_dev_ptr;
246 struct aac_get_container_count *dinfo;
247 struct aac_get_container_count_resp *dresp;
248 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
250 instance = dev->scsi_host_ptr->unique_id;
252 if (!(fibptr = fib_alloc(dev)))
256 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
257 dinfo->command = cpu_to_le32(VM_ContainerConfig);
258 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
260 status = fib_send(ContainerCommand,
262 sizeof (struct aac_get_container_count),
267 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
268 maximum_num_containers = dresp->ContainerSwitchEntries;
269 fib_complete(fibptr);
272 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
273 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
275 fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
276 sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
281 memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
283 dev->fsa_dev = fsa_dev_ptr;
284 dev->maximum_num_containers = maximum_num_containers;
286 for (index = 0; index < dev->maximum_num_containers; index++) {
287 struct aac_query_mount *dinfo;
288 struct aac_mount *dresp;
290 fsa_dev_ptr[index].devname[0] = '\0';
293 dinfo = (struct aac_query_mount *) fib_data(fibptr);
295 dinfo->command = cpu_to_le32(VM_NameServe);
296 dinfo->count = cpu_to_le32(index);
297 dinfo->type = cpu_to_le32(FT_FILESYS);
299 status = fib_send(ContainerCommand,
301 sizeof (struct aac_query_mount),
306 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
309 dresp = (struct aac_mount *)fib_data(fibptr);
312 "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%u\n",
313 (int)index, (int)le32_to_cpu(dresp->status),
314 (int)le32_to_cpu(dresp->mnt[0].vol),
315 (int)le32_to_cpu(dresp->mnt[0].state),
316 (unsigned)le32_to_cpu(dresp->mnt[0].capacity)));
317 if ((le32_to_cpu(dresp->status) == ST_OK) &&
318 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
319 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
320 fsa_dev_ptr[index].valid = 1;
321 fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
322 fsa_dev_ptr[index].size = le32_to_cpu(dresp->mnt[0].capacity);
323 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
324 fsa_dev_ptr[index].ro = 1;
326 fib_complete(fibptr);
328 * If there are no more containers, then stop asking.
330 if ((index + 1) >= le32_to_cpu(dresp->count)){
338 static void aac_io_done(struct scsi_cmnd * scsicmd)
340 unsigned long cpu_flags;
341 struct Scsi_Host *host = scsicmd->device->host;
342 spin_lock_irqsave(host->host_lock, cpu_flags);
343 scsicmd->scsi_done(scsicmd);
344 spin_unlock_irqrestore(host->host_lock, cpu_flags);
347 static void get_container_name_callback(void *context, struct fib * fibptr)
349 struct aac_get_name_resp * get_name_reply;
350 struct scsi_cmnd * scsicmd;
352 scsicmd = (struct scsi_cmnd *) context;
354 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
358 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
359 /* Failure is irrelevant, using default value instead */
360 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
361 && (get_name_reply->data[0] != '\0')) {
364 char * sp = get_name_reply->data;
365 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
368 count = sizeof(((struct inquiry_data *)NULL)->inqd_pid);
369 dp = ((struct inquiry_data *)scsicmd->request_buffer)->inqd_pid;
371 *dp++ = (*sp) ? *sp++ : ' ';
372 } while (--count > 0);
374 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
376 fib_complete(fibptr);
378 aac_io_done(scsicmd);
382 * aac_get_container_name - get container name, none blocking.
384 static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
387 struct aac_get_name *dinfo;
388 struct fib * cmd_fibcontext;
389 struct aac_dev * dev;
391 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
393 if (!(cmd_fibcontext = fib_alloc(dev)))
396 fib_init(cmd_fibcontext);
397 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
399 dinfo->command = cpu_to_le32(VM_ContainerConfig);
400 dinfo->type = cpu_to_le32(CT_READ_NAME);
401 dinfo->cid = cpu_to_le32(cid);
402 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
404 status = fib_send(ContainerCommand,
406 sizeof (struct aac_get_name),
409 (fib_callback) get_container_name_callback,
413 * Check that the command queued to the controller
415 if (status == -EINPROGRESS)
418 printk(KERN_WARNING "aac_get_container_name: fib_send failed with status: %d.\n", status);
419 fib_complete(cmd_fibcontext);
420 fib_free(cmd_fibcontext);
425 * probe_container - query a logical volume
426 * @dev: device to query
427 * @cid: container identifier
429 * Queries the controller about the given volume. The volume information
430 * is updated in the struct fsa_dev_info structure rather than returned.
433 static int probe_container(struct aac_dev *dev, int cid)
435 struct fsa_dev_info *fsa_dev_ptr;
437 struct aac_query_mount *dinfo;
438 struct aac_mount *dresp;
442 fsa_dev_ptr = dev->fsa_dev;
443 instance = dev->scsi_host_ptr->unique_id;
445 if (!(fibptr = fib_alloc(dev)))
450 dinfo = (struct aac_query_mount *)fib_data(fibptr);
452 dinfo->command = cpu_to_le32(VM_NameServe);
453 dinfo->count = cpu_to_le32(cid);
454 dinfo->type = cpu_to_le32(FT_FILESYS);
456 status = fib_send(ContainerCommand,
458 sizeof(struct aac_query_mount),
463 printk(KERN_WARNING "aacraid: probe_containers query failed.\n");
467 dresp = (struct aac_mount *) fib_data(fibptr);
469 if ((le32_to_cpu(dresp->status) == ST_OK) &&
470 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
471 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
472 fsa_dev_ptr[cid].valid = 1;
473 fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
474 fsa_dev_ptr[cid].size = le32_to_cpu(dresp->mnt[0].capacity);
475 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
476 fsa_dev_ptr[cid].ro = 1;
480 fib_complete(fibptr);
486 /* Local Structure to set SCSI inquiry data strings */
488 char vid[8]; /* Vendor ID */
489 char pid[16]; /* Product ID */
490 char prl[4]; /* Product Revision Level */
494 * InqStrCopy - string merge
495 * @a: string to copy from
496 * @b: string to copy to
498 * Copy a String from one location to another
502 static void inqstrcpy(char *a, char *b)
509 static char *container_types[] = {
530 /* Function: setinqstr
532 * Arguments: [1] pointer to void [1] int
534 * Purpose: Sets SCSI inquiry data strings for vendor, product
535 * and revision level. Allows strings to be set in platform dependant
536 * files instead of in OS dependant driver source.
539 static void setinqstr(int devtype, void *data, int tindex)
541 struct scsi_inq *str;
542 struct aac_driver_ident *mp;
544 mp = aac_get_driver_ident(devtype);
546 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
548 inqstrcpy (mp->vname, str->vid);
549 inqstrcpy (mp->model, str->pid); /* last six chars reserved for vol type */
551 if (tindex < (sizeof(container_types)/sizeof(char *))){
552 char *findit = str->pid;
554 for ( ; *findit != ' '; findit++); /* walk till we find a space */
555 /* RAID is superfluous in the context of a RAID device */
556 if (memcmp(findit-4, "RAID", 4) == 0)
557 *(findit -= 4) = ' ';
558 inqstrcpy (container_types[tindex], findit + 1);
560 inqstrcpy ("V1.0", str->prl);
563 void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
564 u8 a_sense_code, u8 incorrect_length,
565 u8 bit_pointer, u16 field_pointer,
568 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
569 sense_buf[1] = 0; /* Segment number, always zero */
571 if (incorrect_length) {
572 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
573 sense_buf[3] = BYTE3(residue);
574 sense_buf[4] = BYTE2(residue);
575 sense_buf[5] = BYTE1(residue);
576 sense_buf[6] = BYTE0(residue);
578 sense_buf[2] = sense_key; /* Sense key */
580 if (sense_key == ILLEGAL_REQUEST)
581 sense_buf[7] = 10; /* Additional sense length */
583 sense_buf[7] = 6; /* Additional sense length */
585 sense_buf[12] = sense_code; /* Additional sense code */
586 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
587 if (sense_key == ILLEGAL_REQUEST) {
590 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
591 sense_buf[15] = 0x80;/* Std sense key specific field */
592 /* Illegal parameter is in the parameter block */
594 if (sense_code == SENCODE_INVALID_CDB_FIELD)
595 sense_buf[15] = 0xc0;/* Std sense key specific field */
596 /* Illegal parameter is in the CDB block */
597 sense_buf[15] |= bit_pointer;
598 sense_buf[16] = field_pointer >> 8; /* MSB */
599 sense_buf[17] = field_pointer; /* LSB */
603 int aac_get_adapter_info(struct aac_dev* dev)
606 struct aac_adapter_info* info;
609 if (!(fibptr = fib_alloc(dev)))
613 info = (struct aac_adapter_info*) fib_data(fibptr);
615 memset(info,0,sizeof(struct aac_adapter_info));
617 rcode = fib_send(RequestAdapterInfo,
619 sizeof(struct aac_adapter_info),
625 memcpy(&dev->adapter_info, info, sizeof(struct aac_adapter_info));
627 tmp = dev->adapter_info.kernelrev;
628 printk(KERN_INFO"%s%d: kernel %d.%d.%d build %d\n",
630 tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
631 dev->adapter_info.kernelbuild);
632 tmp = dev->adapter_info.monitorrev;
633 printk(KERN_INFO"%s%d: monitor %d.%d.%d build %d\n",
635 tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
636 dev->adapter_info.monitorbuild);
637 tmp = dev->adapter_info.biosrev;
638 printk(KERN_INFO"%s%d: bios %d.%d.%d build %d\n",
640 tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
641 dev->adapter_info.biosbuild);
642 printk(KERN_INFO"%s%d: serial %x%x\n",
644 dev->adapter_info.serial[0],
645 dev->adapter_info.serial[1]);
647 dev->nondasd_support = 0;
648 dev->raid_scsi_mode = 0;
649 if(dev->adapter_info.options & AAC_OPT_NONDASD){
650 dev->nondasd_support = 1;
654 * If the firmware supports ROMB RAID/SCSI mode and we are currently
655 * in RAID/SCSI mode, set the flag. For now if in this mode we will
656 * force nondasd support on. If we decide to allow the non-dasd flag
657 * additional changes changes will have to be made to support
658 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
659 * changed to support the new dev->raid_scsi_mode flag instead of
660 * leaching off of the dev->nondasd_support flag. Also in linit.c the
661 * function aac_detect will have to be modified where it sets up the
662 * max number of channels based on the aac->nondasd_support flag only.
664 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
665 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
666 dev->nondasd_support = 1;
667 dev->raid_scsi_mode = 1;
669 if (dev->raid_scsi_mode != 0)
670 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
674 dev->nondasd_support = (nondasd!=0);
676 if(dev->nondasd_support != 0){
677 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
680 dev->dac_support = 0;
681 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
682 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
683 dev->dac_support = 1;
687 dev->dac_support = (dacmode!=0);
689 if(dev->dac_support != 0) {
690 if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL) &&
691 !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL)) {
692 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
694 } else if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFULL) &&
695 !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFULL)) {
696 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
698 dev->dac_support = 0;
700 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
706 fib_complete(fibptr);
713 static void read_callback(void *context, struct fib * fibptr)
716 struct aac_read_reply *readreply;
717 struct scsi_cmnd *scsicmd;
721 scsicmd = (struct scsi_cmnd *) context;
723 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
724 cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
726 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
727 dprintk((KERN_DEBUG "read_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
733 pci_unmap_sg(dev->pdev,
734 (struct scatterlist *)scsicmd->buffer,
736 scsicmd->sc_data_direction);
737 else if(scsicmd->request_bufflen)
738 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
739 scsicmd->request_bufflen,
740 scsicmd->sc_data_direction);
741 readreply = (struct aac_read_reply *)fib_data(fibptr);
742 if (le32_to_cpu(readreply->status) == ST_OK)
743 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
745 printk(KERN_WARNING "read_callback: read failed, status = %d\n", readreply->status);
746 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
747 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
749 SENCODE_INTERNAL_TARGET_FAILURE,
750 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
752 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
753 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
754 ? sizeof(scsicmd->sense_buffer)
755 : sizeof(dev->fsa_dev[cid].sense_data));
757 fib_complete(fibptr);
760 aac_io_done(scsicmd);
763 static void write_callback(void *context, struct fib * fibptr)
766 struct aac_write_reply *writereply;
767 struct scsi_cmnd *scsicmd;
771 scsicmd = (struct scsi_cmnd *) context;
772 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
773 cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
775 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
776 dprintk((KERN_DEBUG "write_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
781 pci_unmap_sg(dev->pdev,
782 (struct scatterlist *)scsicmd->buffer,
784 scsicmd->sc_data_direction);
785 else if(scsicmd->request_bufflen)
786 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
787 scsicmd->request_bufflen,
788 scsicmd->sc_data_direction);
790 writereply = (struct aac_write_reply *) fib_data(fibptr);
791 if (le32_to_cpu(writereply->status) == ST_OK)
792 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
794 printk(KERN_WARNING "write_callback: write failed, status = %d\n", writereply->status);
795 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | 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(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
802 sizeof(struct sense_data));
805 fib_complete(fibptr);
807 aac_io_done(scsicmd);
810 int aac_read(struct scsi_cmnd * scsicmd, int cid)
818 struct fib * cmd_fibcontext;
820 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
822 * Get block address and transfer length
824 if (scsicmd->cmnd[0] == READ_6) /* 6 byte command */
826 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
828 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
829 count = scsicmd->cmnd[4];
834 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
836 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
837 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
839 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
841 * Alocate and initialize a Fib
843 if (!(cmd_fibcontext = fib_alloc(dev))) {
847 fib_init(cmd_fibcontext);
849 if(dev->dac_support == 1) {
850 struct aac_read64 *readcmd;
851 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
852 readcmd->command = cpu_to_le32(VM_CtHostRead64);
853 readcmd->cid = cpu_to_le16(cid);
854 readcmd->sector_count = cpu_to_le16(count);
855 readcmd->block = cpu_to_le32(lba);
856 readcmd->pad = cpu_to_le16(0);
857 readcmd->flags = cpu_to_le16(0);
859 aac_build_sg64(scsicmd, &readcmd->sg);
860 if(readcmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
862 fibsize = sizeof(struct aac_read64) + ((readcmd->sg.count - 1) * sizeof (struct sgentry64));
864 * Now send the Fib to the adapter
866 status = fib_send(ContainerCommand64,
871 (fib_callback) read_callback,
874 struct aac_read *readcmd;
875 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
876 readcmd->command = cpu_to_le32(VM_CtBlockRead);
877 readcmd->cid = cpu_to_le32(cid);
878 readcmd->block = cpu_to_le32(lba);
879 readcmd->count = cpu_to_le32(count * 512);
881 if (count * 512 > (64 * 1024))
884 aac_build_sg(scsicmd, &readcmd->sg);
885 if(readcmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
887 fibsize = sizeof(struct aac_read) + ((readcmd->sg.count - 1) * sizeof (struct sgentry));
889 * Now send the Fib to the adapter
891 status = fib_send(ContainerCommand,
896 (fib_callback) read_callback,
903 * Check that the command queued to the controller
905 if (status == -EINPROGRESS)
907 dprintk("read queued.\n");
911 printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
913 * For some reason, the Fib didn't queue, return QUEUE_FULL
915 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
916 aac_io_done(scsicmd);
917 fib_complete(cmd_fibcontext);
918 fib_free(cmd_fibcontext);
922 static int aac_write(struct scsi_cmnd * scsicmd, int cid)
929 struct fib * cmd_fibcontext;
931 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
933 * Get block address and transfer length
935 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
937 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
938 count = scsicmd->cmnd[4];
942 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
943 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
944 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
946 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
948 * Allocate and initialize a Fib then setup a BlockWrite command
950 if (!(cmd_fibcontext = fib_alloc(dev))) {
951 scsicmd->result = DID_ERROR << 16;
952 aac_io_done(scsicmd);
955 fib_init(cmd_fibcontext);
957 if(dev->dac_support == 1) {
958 struct aac_write64 *writecmd;
959 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
960 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
961 writecmd->cid = cpu_to_le16(cid);
962 writecmd->sector_count = cpu_to_le16(count);
963 writecmd->block = cpu_to_le32(lba);
964 writecmd->pad = cpu_to_le16(0);
965 writecmd->flags = cpu_to_le16(0);
967 aac_build_sg64(scsicmd, &writecmd->sg);
968 if(writecmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
970 fibsize = sizeof(struct aac_write64) + ((writecmd->sg.count - 1) * sizeof (struct sgentry64));
972 * Now send the Fib to the adapter
974 status = fib_send(ContainerCommand64,
979 (fib_callback) write_callback,
982 struct aac_write *writecmd;
983 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
984 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
985 writecmd->cid = cpu_to_le32(cid);
986 writecmd->block = cpu_to_le32(lba);
987 writecmd->count = cpu_to_le32(count * 512);
988 writecmd->sg.count = cpu_to_le32(1);
989 /* ->stable is not used - it did mean which type of write */
991 if (count * 512 > (64 * 1024)) {
995 aac_build_sg(scsicmd, &writecmd->sg);
996 if(writecmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
998 fibsize = sizeof(struct aac_write) + ((writecmd->sg.count - 1) * sizeof (struct sgentry));
1000 * Now send the Fib to the adapter
1002 status = fib_send(ContainerCommand,
1007 (fib_callback) write_callback,
1012 * Check that the command queued to the controller
1014 if (status == -EINPROGRESS)
1016 dprintk("write queued.\n");
1020 printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
1022 * For some reason, the Fib didn't queue, return QUEUE_FULL
1024 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1025 aac_io_done(scsicmd);
1027 fib_complete(cmd_fibcontext);
1028 fib_free(cmd_fibcontext);
1032 static void synchronize_callback(void *context, struct fib *fibptr)
1034 struct aac_synchronize_reply *synchronizereply;
1035 struct scsi_cmnd *cmd;
1039 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1040 smp_processor_id(), jiffies));
1041 BUG_ON(fibptr == NULL);
1044 synchronizereply = fib_data(fibptr);
1045 if (le32_to_cpu(synchronizereply->status) == CT_OK)
1046 cmd->result = DID_OK << 16 |
1047 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1049 struct scsi_device *sdev = cmd->device;
1050 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
1051 u32 cid = ID_LUN_TO_CONTAINER(sdev->id, sdev->lun);
1053 "synchronize_callback: synchronize failed, status = %d\n",
1054 synchronizereply->status);
1055 cmd->result = DID_OK << 16 |
1056 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1057 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1059 SENCODE_INTERNAL_TARGET_FAILURE,
1060 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1062 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1063 min(sizeof(dev->fsa_dev[cid].sense_data),
1064 sizeof(cmd->sense_buffer)));
1067 fib_complete(fibptr);
1072 static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1075 struct fib *cmd_fibcontext;
1076 struct aac_synchronize *synchronizecmd;
1077 struct scsi_cmnd *cmd;
1078 struct scsi_device *sdev = scsicmd->device;
1080 unsigned long flags;
1083 * Wait for all commands to complete to this specific
1086 spin_lock_irqsave(&sdev->list_lock, flags);
1087 list_for_each_entry(cmd, &sdev->cmd_list, list)
1088 if (cmd != scsicmd && cmd->serial_number != 0) {
1093 spin_unlock_irqrestore(&sdev->list_lock, flags);
1096 * Yield the processor (requeue for later)
1099 return SCSI_MLQUEUE_DEVICE_BUSY;
1102 * Alocate and initialize a Fib
1104 if (!(cmd_fibcontext =
1105 fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata)))
1106 return SCSI_MLQUEUE_HOST_BUSY;
1108 fib_init(cmd_fibcontext);
1110 synchronizecmd = fib_data(cmd_fibcontext);
1111 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1112 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1113 synchronizecmd->cid = cpu_to_le32(cid);
1114 synchronizecmd->count =
1115 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1118 * Now send the Fib to the adapter
1120 status = fib_send(ContainerCommand,
1122 sizeof(struct aac_synchronize),
1125 (fib_callback)synchronize_callback,
1129 * Check that the command queued to the controller
1131 if (status == -EINPROGRESS)
1135 "aac_synchronize: fib_send failed with status: %d.\n", status);
1136 fib_complete(cmd_fibcontext);
1137 fib_free(cmd_fibcontext);
1138 return SCSI_MLQUEUE_HOST_BUSY;
1142 * aac_scsi_cmd() - Process SCSI command
1143 * @scsicmd: SCSI command block
1145 * Emulate a SCSI command and queue the required request for the
1149 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1152 struct Scsi_Host *host = scsicmd->device->host;
1153 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1154 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1155 int cardtype = dev->cardtype;
1159 * If the bus, id or lun is out of range, return fail
1160 * Test does not apply to ID 16, the pseudo id for the controller
1163 if (scsicmd->device->id != host->this_id) {
1164 if ((scsicmd->device->channel == 0) ){
1165 if( (scsicmd->device->id >= dev->maximum_num_containers) || (scsicmd->device->lun != 0)){
1166 scsicmd->result = DID_NO_CONNECT << 16;
1167 scsicmd->scsi_done(scsicmd);
1170 cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
1173 * If the target container doesn't exist, it may have
1174 * been newly created
1176 if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1177 switch (scsicmd->cmnd[0]) {
1180 case TEST_UNIT_READY:
1181 spin_unlock_irq(host->host_lock);
1182 probe_container(dev, cid);
1183 spin_lock_irq(host->host_lock);
1184 if (fsa_dev_ptr[cid].valid == 0) {
1185 scsicmd->result = DID_NO_CONNECT << 16;
1186 scsicmd->scsi_done(scsicmd);
1194 * If the target container still doesn't exist,
1197 if (fsa_dev_ptr[cid].valid == 0) {
1198 scsicmd->result = DID_BAD_TARGET << 16;
1199 scsicmd->scsi_done(scsicmd);
1202 } else { /* check for physical non-dasd devices */
1203 if(dev->nondasd_support == 1){
1204 return aac_send_srb_fib(scsicmd);
1206 scsicmd->result = DID_NO_CONNECT << 16;
1207 scsicmd->scsi_done(scsicmd);
1213 * else Command for the controller itself
1215 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
1216 (scsicmd->cmnd[0] != TEST_UNIT_READY))
1218 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1219 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1220 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1222 SENCODE_INVALID_COMMAND,
1223 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1224 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1225 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1226 ? sizeof(scsicmd->sense_buffer)
1227 : sizeof(dev->fsa_dev[cid].sense_data));
1228 scsicmd->scsi_done(scsicmd);
1233 /* Handle commands here that don't really require going out to the adapter */
1234 switch (scsicmd->cmnd[0]) {
1237 struct inquiry_data *inq_data_ptr;
1239 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
1240 inq_data_ptr = (struct inquiry_data *)scsicmd->request_buffer;
1241 memset(inq_data_ptr, 0, sizeof (struct inquiry_data));
1243 inq_data_ptr->inqd_ver = 2; /* claim compliance to SCSI-2 */
1244 inq_data_ptr->inqd_dtq = 0x80; /* set RMB bit to one indicating that the medium is removable */
1245 inq_data_ptr->inqd_rdf = 2; /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
1246 inq_data_ptr->inqd_len = 31;
1247 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
1248 inq_data_ptr->inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
1250 * Set the Vendor, Product, and Revision Level
1251 * see: <vendor>.c i.e. aac.c
1253 if (scsicmd->device->id == host->this_id) {
1254 setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), (sizeof(container_types)/sizeof(char *)));
1255 inq_data_ptr->inqd_pdt = INQD_PDT_PROC; /* Processor device */
1256 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1257 scsicmd->scsi_done(scsicmd);
1260 setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), fsa_dev_ptr[cid].type);
1261 inq_data_ptr->inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1262 return aac_get_container_name(scsicmd, cid);
1269 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
1270 if (fsa_dev_ptr[cid].size <= 0x100000000LL)
1271 capacity = fsa_dev_ptr[cid].size - 1;
1274 cp = scsicmd->request_buffer;
1275 cp[0] = (capacity >> 24) & 0xff;
1276 cp[1] = (capacity >> 16) & 0xff;
1277 cp[2] = (capacity >> 8) & 0xff;
1278 cp[3] = (capacity >> 0) & 0xff;
1284 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1285 scsicmd->scsi_done(scsicmd);
1294 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1295 mode_buf = scsicmd->request_buffer;
1296 mode_buf[0] = 3; /* Mode data length */
1297 mode_buf[1] = 0; /* Medium type - default */
1298 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1299 mode_buf[3] = 0; /* Block descriptor length */
1301 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1302 scsicmd->scsi_done(scsicmd);
1310 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1311 mode_buf = scsicmd->request_buffer;
1312 mode_buf[0] = 0; /* Mode data length (MSB) */
1313 mode_buf[1] = 6; /* Mode data length (LSB) */
1314 mode_buf[2] = 0; /* Medium type - default */
1315 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1316 mode_buf[4] = 0; /* reserved */
1317 mode_buf[5] = 0; /* reserved */
1318 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1319 mode_buf[7] = 0; /* Block descriptor length (LSB) */
1321 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1322 scsicmd->scsi_done(scsicmd);
1327 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1328 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1329 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1330 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1331 scsicmd->scsi_done(scsicmd);
1334 case ALLOW_MEDIUM_REMOVAL:
1335 dprintk((KERN_DEBUG "LOCK command.\n"));
1336 if (scsicmd->cmnd[4])
1337 fsa_dev_ptr[cid].locked = 1;
1339 fsa_dev_ptr[cid].locked = 0;
1341 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1342 scsicmd->scsi_done(scsicmd);
1345 * These commands are all No-Ops
1347 case TEST_UNIT_READY:
1351 case REASSIGN_BLOCKS:
1354 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1355 scsicmd->scsi_done(scsicmd);
1359 switch (scsicmd->cmnd[0])
1364 * Hack to keep track of ordinal number of the device that
1365 * corresponds to a container. Needed to convert
1366 * containers to /dev/sd device names
1369 spin_unlock_irq(host->host_lock);
1370 if (scsicmd->request->rq_disk)
1371 memcpy(fsa_dev_ptr[cid].devname,
1372 scsicmd->request->rq_disk->disk_name,
1375 ret = aac_read(scsicmd, cid);
1376 spin_lock_irq(host->host_lock);
1381 spin_unlock_irq(host->host_lock);
1382 ret = aac_write(scsicmd, cid);
1383 spin_lock_irq(host->host_lock);
1386 case SYNCHRONIZE_CACHE:
1387 /* Issue FIB to tell Firmware to flush it's cache */
1388 return aac_synchronize(scsicmd, cid);
1392 * Unhandled commands
1394 printk(KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]);
1395 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1396 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1397 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1398 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1399 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1400 (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1401 ? sizeof(scsicmd->sense_buffer)
1402 : sizeof(dev->fsa_dev[cid].sense_data));
1403 scsicmd->scsi_done(scsicmd);
1408 static int query_disk(struct aac_dev *dev, void __user *arg)
1410 struct aac_query_disk qd;
1411 struct fsa_dev_info *fsa_dev_ptr;
1413 fsa_dev_ptr = dev->fsa_dev;
1414 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1417 qd.cnum = ID_LUN_TO_CONTAINER(qd.id, qd.lun);
1418 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
1420 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1422 qd.instance = dev->scsi_host_ptr->host_no;
1424 qd.id = CONTAINER_TO_ID(qd.cnum);
1425 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1427 else return -EINVAL;
1429 qd.valid = fsa_dev_ptr[qd.cnum].valid;
1430 qd.locked = fsa_dev_ptr[qd.cnum].locked;
1431 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1433 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1438 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1439 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1441 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1446 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1448 struct aac_delete_disk dd;
1449 struct fsa_dev_info *fsa_dev_ptr;
1451 fsa_dev_ptr = dev->fsa_dev;
1453 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1456 if (dd.cnum >= dev->maximum_num_containers)
1459 * Mark this container as being deleted.
1461 fsa_dev_ptr[dd.cnum].deleted = 1;
1463 * Mark the container as no longer valid
1465 fsa_dev_ptr[dd.cnum].valid = 0;
1469 static int delete_disk(struct aac_dev *dev, void __user *arg)
1471 struct aac_delete_disk dd;
1472 struct fsa_dev_info *fsa_dev_ptr;
1474 fsa_dev_ptr = dev->fsa_dev;
1476 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1479 if (dd.cnum >= dev->maximum_num_containers)
1482 * If the container is locked, it can not be deleted by the API.
1484 if (fsa_dev_ptr[dd.cnum].locked)
1488 * Mark the container as no longer being valid.
1490 fsa_dev_ptr[dd.cnum].valid = 0;
1491 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1496 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1499 case FSACTL_QUERY_DISK:
1500 return query_disk(dev, arg);
1501 case FSACTL_DELETE_DISK:
1502 return delete_disk(dev, arg);
1503 case FSACTL_FORCE_DELETE_DISK:
1504 return force_delete_disk(dev, arg);
1505 case FSACTL_GET_CONTAINERS:
1506 return aac_get_containers(dev);
1515 * @context: the context set in the fib - here it is scsi cmd
1516 * @fibptr: pointer to the fib
1518 * Handles the completion of a scsi command to a non dasd device
1522 static void aac_srb_callback(void *context, struct fib * fibptr)
1524 struct aac_dev *dev;
1525 struct aac_srb_reply *srbreply;
1526 struct scsi_cmnd *scsicmd;
1528 scsicmd = (struct scsi_cmnd *) context;
1529 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1534 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1536 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
1538 * Calculate resid for sg
1541 scsicmd->resid = scsicmd->request_bufflen - srbreply->data_xfer_length;
1544 pci_unmap_sg(dev->pdev,
1545 (struct scatterlist *)scsicmd->buffer,
1547 scsicmd->sc_data_direction);
1548 else if(scsicmd->request_bufflen)
1549 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1550 scsicmd->sc_data_direction);
1553 * First check the fib status
1556 if (le32_to_cpu(srbreply->status) != ST_OK){
1558 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1559 len = (srbreply->sense_data_size > sizeof(scsicmd->sense_buffer))?
1560 sizeof(scsicmd->sense_buffer):srbreply->sense_data_size;
1561 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1562 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1566 * Next check the srb status
1568 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1569 case SRB_STATUS_ERROR_RECOVERY:
1570 case SRB_STATUS_PENDING:
1571 case SRB_STATUS_SUCCESS:
1572 if(scsicmd->cmnd[0] == INQUIRY ){
1575 /* We can't expose disk devices because we can't tell whether they
1576 * are the raw container drives or stand alone drives. If they have
1577 * the removable bit set then we should expose them though.
1579 b = (*(u8*)scsicmd->buffer)&0x1f;
1580 b1 = ((u8*)scsicmd->buffer)[1];
1581 if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1582 || (b==TYPE_DISK && (b1&0x80)) ){
1583 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1585 * We will allow disk devices if in RAID/SCSI mode and
1588 } else if ((dev->raid_scsi_mode) &&
1589 (scsicmd->device->channel == 2)) {
1590 scsicmd->result = DID_OK << 16 |
1591 COMMAND_COMPLETE << 8;
1593 scsicmd->result = DID_NO_CONNECT << 16 |
1594 COMMAND_COMPLETE << 8;
1597 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1600 case SRB_STATUS_DATA_OVERRUN:
1601 switch(scsicmd->cmnd[0]){
1608 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1609 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1611 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1613 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1618 /* We can't expose disk devices because we can't tell whether they
1619 * are the raw container drives or stand alone drives
1621 b = (*(u8*)scsicmd->buffer)&0x0f;
1622 b1 = ((u8*)scsicmd->buffer)[1];
1623 if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1624 || (b==TYPE_DISK && (b1&0x80)) ){
1625 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1627 * We will allow disk devices if in RAID/SCSI mode and
1630 } else if ((dev->raid_scsi_mode) &&
1631 (scsicmd->device->channel == 2)) {
1632 scsicmd->result = DID_OK << 16 |
1633 COMMAND_COMPLETE << 8;
1635 scsicmd->result = DID_NO_CONNECT << 16 |
1636 COMMAND_COMPLETE << 8;
1641 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1645 case SRB_STATUS_ABORTED:
1646 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1648 case SRB_STATUS_ABORT_FAILED:
1649 // Not sure about this one - but assuming the hba was trying to abort for some reason
1650 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1652 case SRB_STATUS_PARITY_ERROR:
1653 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1655 case SRB_STATUS_NO_DEVICE:
1656 case SRB_STATUS_INVALID_PATH_ID:
1657 case SRB_STATUS_INVALID_TARGET_ID:
1658 case SRB_STATUS_INVALID_LUN:
1659 case SRB_STATUS_SELECTION_TIMEOUT:
1660 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1663 case SRB_STATUS_COMMAND_TIMEOUT:
1664 case SRB_STATUS_TIMEOUT:
1665 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
1668 case SRB_STATUS_BUSY:
1669 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1672 case SRB_STATUS_BUS_RESET:
1673 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
1676 case SRB_STATUS_MESSAGE_REJECTED:
1677 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
1679 case SRB_STATUS_REQUEST_FLUSHED:
1680 case SRB_STATUS_ERROR:
1681 case SRB_STATUS_INVALID_REQUEST:
1682 case SRB_STATUS_REQUEST_SENSE_FAILED:
1683 case SRB_STATUS_NO_HBA:
1684 case SRB_STATUS_UNEXPECTED_BUS_FREE:
1685 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
1686 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
1687 case SRB_STATUS_DELAYED_RETRY:
1688 case SRB_STATUS_BAD_FUNCTION:
1689 case SRB_STATUS_NOT_STARTED:
1690 case SRB_STATUS_NOT_IN_USE:
1691 case SRB_STATUS_FORCE_ABORT:
1692 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
1694 #ifdef AAC_DETAILED_STATUS_INFO
1695 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
1696 le32_to_cpu(srbreply->srb_status & 0x3F),
1697 aac_get_status_string(
1698 le32_to_cpu(srbreply->srb_status) & 0x3F),
1700 le32_to_cpu(srbreply->scsi_status));
1702 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1705 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
1707 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
1708 len = (srbreply->sense_data_size > sizeof(scsicmd->sense_buffer))?
1709 sizeof(scsicmd->sense_buffer):srbreply->sense_data_size;
1710 #ifdef AAC_DETAILED_STATUS_INFO
1711 dprintk((KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
1712 le32_to_cpu(srbreply->status), len));
1714 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1718 * OR in the scsi status (already shifted up a bit)
1720 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
1722 fib_complete(fibptr);
1724 aac_io_done(scsicmd);
1730 * @scsicmd: the scsi command block
1732 * This routine will form a FIB and fill in the aac_srb from the
1733 * scsicmd passed in.
1736 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
1738 struct fib* cmd_fibcontext;
1739 struct aac_dev* dev;
1741 struct aac_srb *srbcmd;
1746 if( scsicmd->device->id > 15 || scsicmd->device->lun > 7) {
1747 scsicmd->result = DID_NO_CONNECT << 16;
1748 scsicmd->scsi_done(scsicmd);
1752 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1753 switch(scsicmd->sc_data_direction){
1757 case DMA_BIDIRECTIONAL:
1758 flag = SRB_DataIn | SRB_DataOut;
1760 case DMA_FROM_DEVICE:
1764 default: /* shuts up some versions of gcc */
1765 flag = SRB_NoDataXfer;
1771 * Allocate and initialize a Fib then setup a BlockWrite command
1773 if (!(cmd_fibcontext = fib_alloc(dev))) {
1776 fib_init(cmd_fibcontext);
1778 srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
1779 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1780 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
1781 srbcmd->id = cpu_to_le32(scsicmd->device->id);
1782 srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
1783 srbcmd->flags = cpu_to_le32(flag);
1784 timeout = (scsicmd->timeout-jiffies)/HZ;
1788 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1789 srbcmd->retry_limit =cpu_to_le32(0); // Obsolete parameter
1790 srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
1792 if( dev->dac_support == 1 ) {
1793 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
1794 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1796 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1797 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1799 * Build Scatter/Gather list
1801 fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry64));
1804 * Now send the Fib to the adapter
1806 status = fib_send(ScsiPortCommand64, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
1807 (fib_callback) aac_srb_callback, (void *) scsicmd);
1809 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
1810 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1812 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1813 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1815 * Build Scatter/Gather list
1817 fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
1820 * Now send the Fib to the adapter
1822 status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
1823 (fib_callback) aac_srb_callback, (void *) scsicmd);
1826 * Check that the command queued to the controller
1828 if (status == -EINPROGRESS){
1832 printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
1833 fib_complete(cmd_fibcontext);
1834 fib_free(cmd_fibcontext);
1839 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
1841 struct aac_dev *dev;
1842 unsigned long byte_count = 0;
1844 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1845 // Get rid of old data
1846 psg->count = cpu_to_le32(0);
1847 psg->sg[0].addr = cpu_to_le32(0);
1848 psg->sg[0].count = cpu_to_le32(0);
1849 if (scsicmd->use_sg) {
1850 struct scatterlist *sg;
1853 sg = (struct scatterlist *) scsicmd->request_buffer;
1855 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1856 scsicmd->sc_data_direction);
1857 psg->count = cpu_to_le32(sg_count);
1861 for (i = 0; i < sg_count; i++) {
1862 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
1863 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1864 byte_count += sg_dma_len(sg);
1867 /* hba wants the size to be exact */
1868 if(byte_count > scsicmd->request_bufflen){
1869 psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
1870 byte_count = scsicmd->request_bufflen;
1872 /* Check for command underflow */
1873 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
1874 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
1875 byte_count, scsicmd->underflow);
1878 else if(scsicmd->request_bufflen) {
1880 addr = pci_map_single(dev->pdev,
1881 scsicmd->request_buffer,
1882 scsicmd->request_bufflen,
1883 scsicmd->sc_data_direction);
1884 psg->count = cpu_to_le32(1);
1885 psg->sg[0].addr = cpu_to_le32(addr);
1886 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
1887 scsicmd->SCp.dma_handle = addr;
1888 byte_count = scsicmd->request_bufflen;
1894 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
1896 struct aac_dev *dev;
1897 unsigned long byte_count = 0;
1900 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1901 // Get rid of old data
1902 psg->count = cpu_to_le32(0);
1903 psg->sg[0].addr[0] = cpu_to_le32(0);
1904 psg->sg[0].addr[1] = cpu_to_le32(0);
1905 psg->sg[0].count = cpu_to_le32(0);
1906 if (scsicmd->use_sg) {
1907 struct scatterlist *sg;
1910 sg = (struct scatterlist *) scsicmd->request_buffer;
1912 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1913 scsicmd->sc_data_direction);
1914 psg->count = cpu_to_le32(sg_count);
1918 for (i = 0; i < sg_count; i++) {
1919 le_addr = cpu_to_le64(sg_dma_address(sg));
1920 psg->sg[i].addr[1] = (u32)(le_addr>>32);
1921 psg->sg[i].addr[0] = (u32)(le_addr & 0xffffffff);
1922 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1923 byte_count += sg_dma_len(sg);
1926 /* hba wants the size to be exact */
1927 if(byte_count > scsicmd->request_bufflen){
1928 psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
1929 byte_count = scsicmd->request_bufflen;
1931 /* Check for command underflow */
1932 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
1933 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
1934 byte_count, scsicmd->underflow);
1937 else if(scsicmd->request_bufflen) {
1939 addr = pci_map_single(dev->pdev,
1940 scsicmd->request_buffer,
1941 scsicmd->request_bufflen,
1942 scsicmd->sc_data_direction);
1943 psg->count = cpu_to_le32(1);
1944 le_addr = cpu_to_le64(addr);
1945 psg->sg[0].addr[1] = (u32)(le_addr>>32);
1946 psg->sg[0].addr[0] = (u32)(le_addr & 0xffffffff);
1947 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
1948 scsicmd->SCp.dma_handle = addr;
1949 byte_count = scsicmd->request_bufflen;
1954 #ifdef AAC_DETAILED_STATUS_INFO
1956 struct aac_srb_status_info {
1962 static struct aac_srb_status_info srb_status_info[] = {
1963 { SRB_STATUS_PENDING, "Pending Status"},
1964 { SRB_STATUS_SUCCESS, "Success"},
1965 { SRB_STATUS_ABORTED, "Aborted Command"},
1966 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
1967 { SRB_STATUS_ERROR, "Error Event"},
1968 { SRB_STATUS_BUSY, "Device Busy"},
1969 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
1970 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
1971 { SRB_STATUS_NO_DEVICE, "No Device"},
1972 { SRB_STATUS_TIMEOUT, "Timeout"},
1973 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
1974 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
1975 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
1976 { SRB_STATUS_BUS_RESET, "Bus Reset"},
1977 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
1978 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
1979 { SRB_STATUS_NO_HBA, "No HBA"},
1980 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
1981 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
1982 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
1983 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
1984 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
1985 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
1986 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
1987 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
1988 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
1989 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
1990 { SRB_STATUS_NOT_STARTED, "Not Started"},
1991 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
1992 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
1993 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
1994 { 0xff, "Unknown Error"}
1997 char *aac_get_status_string(u32 status)
2001 for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
2002 if(srb_status_info[i].status == status){
2003 return srb_status_info[i].str;
2007 return "Bad Status Code";