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 TARGET_LUN_TO_CONTAINER(target, lun) (target)
57 #define CONTAINER_TO_TARGET(cont) ((cont))
58 #define CONTAINER_TO_LUN(cont) (0)
60 #define MAX_FIB_DATA (sizeof(struct hw_fib) - sizeof(FIB_HEADER))
62 #define MAX_DRIVER_SG_SEGMENT_COUNT 17
67 #define SENKEY_NO_SENSE 0x00
68 #define SENKEY_UNDEFINED 0x01
69 #define SENKEY_NOT_READY 0x02
70 #define SENKEY_MEDIUM_ERR 0x03
71 #define SENKEY_HW_ERR 0x04
72 #define SENKEY_ILLEGAL 0x05
73 #define SENKEY_ATTENTION 0x06
74 #define SENKEY_PROTECTED 0x07
75 #define SENKEY_BLANK 0x08
76 #define SENKEY_V_UNIQUE 0x09
77 #define SENKEY_CPY_ABORT 0x0A
78 #define SENKEY_ABORT 0x0B
79 #define SENKEY_EQUAL 0x0C
80 #define SENKEY_VOL_OVERFLOW 0x0D
81 #define SENKEY_MISCOMP 0x0E
82 #define SENKEY_RESERVED 0x0F
88 #define SENCODE_NO_SENSE 0x00
89 #define SENCODE_END_OF_DATA 0x00
90 #define SENCODE_BECOMING_READY 0x04
91 #define SENCODE_INIT_CMD_REQUIRED 0x04
92 #define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
93 #define SENCODE_INVALID_COMMAND 0x20
94 #define SENCODE_LBA_OUT_OF_RANGE 0x21
95 #define SENCODE_INVALID_CDB_FIELD 0x24
96 #define SENCODE_LUN_NOT_SUPPORTED 0x25
97 #define SENCODE_INVALID_PARAM_FIELD 0x26
98 #define SENCODE_PARAM_NOT_SUPPORTED 0x26
99 #define SENCODE_PARAM_VALUE_INVALID 0x26
100 #define SENCODE_RESET_OCCURRED 0x29
101 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
102 #define SENCODE_INQUIRY_DATA_CHANGED 0x3F
103 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
104 #define SENCODE_DIAGNOSTIC_FAILURE 0x40
105 #define SENCODE_INTERNAL_TARGET_FAILURE 0x44
106 #define SENCODE_INVALID_MESSAGE_ERROR 0x49
107 #define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
108 #define SENCODE_OVERLAPPED_COMMAND 0x4E
111 * Additional sense codes
114 #define ASENCODE_NO_SENSE 0x00
115 #define ASENCODE_END_OF_DATA 0x05
116 #define ASENCODE_BECOMING_READY 0x01
117 #define ASENCODE_INIT_CMD_REQUIRED 0x02
118 #define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
119 #define ASENCODE_INVALID_COMMAND 0x00
120 #define ASENCODE_LBA_OUT_OF_RANGE 0x00
121 #define ASENCODE_INVALID_CDB_FIELD 0x00
122 #define ASENCODE_LUN_NOT_SUPPORTED 0x00
123 #define ASENCODE_INVALID_PARAM_FIELD 0x00
124 #define ASENCODE_PARAM_NOT_SUPPORTED 0x01
125 #define ASENCODE_PARAM_VALUE_INVALID 0x02
126 #define ASENCODE_RESET_OCCURRED 0x00
127 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
128 #define ASENCODE_INQUIRY_DATA_CHANGED 0x03
129 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
130 #define ASENCODE_DIAGNOSTIC_FAILURE 0x80
131 #define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
132 #define ASENCODE_INVALID_MESSAGE_ERROR 0x00
133 #define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
134 #define ASENCODE_OVERLAPPED_COMMAND 0x00
136 #define BYTE0(x) (unsigned char)(x)
137 #define BYTE1(x) (unsigned char)((x) >> 8)
138 #define BYTE2(x) (unsigned char)((x) >> 16)
139 #define BYTE3(x) (unsigned char)((x) >> 24)
141 /*------------------------------------------------------------------------------
142 * S T R U C T S / T Y P E D E F S
143 *----------------------------------------------------------------------------*/
144 /* SCSI inquiry data */
145 struct inquiry_data {
146 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
147 u8 inqd_dtq; /* RMB | Device Type Qualifier */
148 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
149 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
150 u8 inqd_len; /* Additional length (n-4) */
151 u8 inqd_pad1[2];/* Reserved - must be zero */
152 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
153 u8 inqd_vid[8]; /* Vendor ID */
154 u8 inqd_pid[16];/* Product ID */
155 u8 inqd_prl[4]; /* Product Revision Level */
159 u8 error_code; /* 70h (current errors), 71h(deferred errors) */
160 u8 valid:1; /* A valid bit of one indicates that the information */
161 /* field contains valid information as defined in the
164 u8 segment_number; /* Only used for COPY, COMPARE, or COPY AND VERIFY Commands */
165 u8 sense_key:4; /* Sense Key */
167 u8 ILI:1; /* Incorrect Length Indicator */
168 u8 EOM:1; /* End Of Medium - reserved for random access devices */
169 u8 filemark:1; /* Filemark - reserved for random access devices */
171 u8 information[4]; /* for direct-access devices, contains the unsigned
172 * logical block address or residue associated with
175 u8 add_sense_len; /* number of additional sense bytes to follow this field */
176 u8 cmnd_info[4]; /* not used */
177 u8 ASC; /* Additional Sense Code */
178 u8 ASCQ; /* Additional Sense Code Qualifier */
179 u8 FRUC; /* Field Replaceable Unit Code - not used */
180 u8 bit_ptr:3; /* indicates which byte of the CDB or parameter data
183 u8 BPV:1; /* bit pointer valid (BPV): 1- indicates that
184 * the bit_ptr field has valid value
187 u8 CD:1; /* command data bit: 1- illegal parameter in CDB.
188 * 0- illegal parameter in data.
191 u8 field_ptr[2]; /* byte of the CDB or parameter data in error */
195 * M O D U L E G L O B A L S
198 static struct fsa_scsi_hba *fsa_dev[MAXIMUM_NUM_ADAPTERS]; /* SCSI Device Instance Pointers */
199 static struct sense_data sense_data[MAXIMUM_NUM_CONTAINERS];
200 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
201 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
202 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
203 #ifdef AAC_DETAILED_STATUS_INFO
204 static char *aac_get_status_string(u32 status);
208 * aac_get_containers - list containers
209 * @common: adapter to probe
211 * Make a list of all containers on this controller
213 int aac_get_containers(struct aac_dev *dev)
215 struct fsa_scsi_hba *fsa_dev_ptr;
218 struct aac_query_mount *dinfo;
219 struct aac_mount *dresp;
223 fsa_dev_ptr = &(dev->fsa_dev);
224 instance = dev->scsi_host_ptr->unique_id;
226 if (!(fibptr = fib_alloc(dev)))
229 for (index = 0; index < MAXIMUM_NUM_CONTAINERS; index++) {
231 dinfo = (struct aac_query_mount *) fib_data(fibptr);
233 dinfo->command = cpu_to_le32(VM_NameServe);
234 dinfo->count = cpu_to_le32(index);
235 dinfo->type = cpu_to_le32(FT_FILESYS);
237 status = fib_send(ContainerCommand,
239 sizeof (struct aac_query_mount),
244 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
247 dresp = (struct aac_mount *)fib_data(fibptr);
249 if ((le32_to_cpu(dresp->status) == ST_OK) &&
250 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
251 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
252 fsa_dev_ptr->valid[index] = 1;
253 fsa_dev_ptr->type[index] = le32_to_cpu(dresp->mnt[0].vol);
254 fsa_dev_ptr->size[index] = le32_to_cpu(dresp->mnt[0].capacity);
255 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
256 fsa_dev_ptr->ro[index] = 1;
258 fib_complete(fibptr);
260 * If there are no more containers, then stop asking.
262 if ((index + 1) >= le32_to_cpu(dresp->count)){
267 fsa_dev[instance] = fsa_dev_ptr;
272 * probe_container - query a logical volume
273 * @dev: device to query
274 * @cid: container identifier
276 * Queries the controller about the given volume. The volume information
277 * is updated in the struct fsa_scsi_hba structure rather than returned.
280 static int probe_container(struct aac_dev *dev, int cid)
282 struct fsa_scsi_hba *fsa_dev_ptr;
284 struct aac_query_mount *dinfo;
285 struct aac_mount *dresp;
289 fsa_dev_ptr = &(dev->fsa_dev);
290 instance = dev->scsi_host_ptr->unique_id;
292 if (!(fibptr = fib_alloc(dev)))
297 dinfo = (struct aac_query_mount *)fib_data(fibptr);
299 dinfo->command = cpu_to_le32(VM_NameServe);
300 dinfo->count = cpu_to_le32(cid);
301 dinfo->type = cpu_to_le32(FT_FILESYS);
303 status = fib_send(ContainerCommand,
305 sizeof(struct aac_query_mount),
310 printk(KERN_WARNING "aacraid: probe_containers query failed.\n");
314 dresp = (struct aac_mount *) fib_data(fibptr);
316 if ((le32_to_cpu(dresp->status) == ST_OK) &&
317 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
318 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
319 fsa_dev_ptr->valid[cid] = 1;
320 fsa_dev_ptr->type[cid] = le32_to_cpu(dresp->mnt[0].vol);
321 fsa_dev_ptr->size[cid] = le32_to_cpu(dresp->mnt[0].capacity);
322 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
323 fsa_dev_ptr->ro[cid] = 1;
327 fib_complete(fibptr);
333 /* Local Structure to set SCSI inquiry data strings */
335 char vid[8]; /* Vendor ID */
336 char pid[16]; /* Product ID */
337 char prl[4]; /* Product Revision Level */
341 * InqStrCopy - string merge
342 * @a: string to copy from
343 * @b: string to copy to
345 * Copy a String from one location to another
349 static void inqstrcpy(char *a, char *b)
356 static char *container_types[] = {
377 /* Function: setinqstr
379 * Arguments: [1] pointer to void [1] int
381 * Purpose: Sets SCSI inquiry data strings for vendor, product
382 * and revision level. Allows strings to be set in platform dependent
383 * files instead of in OS dependent driver source.
386 static void setinqstr(int devtype, void *data, int tindex)
388 struct scsi_inq *str;
390 struct aac_driver_ident *mp;
392 mp = aac_get_driver_ident(devtype);
394 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
396 inqstrcpy (mp->vname, str->vid);
397 inqstrcpy (mp->model, str->pid); /* last six chars reserved for vol type */
401 for ( ; *findit != ' '; findit++); /* walk till we find a space then incr by 1 */
404 if (tindex < (sizeof(container_types)/sizeof(char *))){
405 inqstrcpy (container_types[tindex], findit);
407 inqstrcpy ("V1.0", str->prl);
410 void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
411 u8 a_sense_code, u8 incorrect_length,
412 u8 bit_pointer, u16 field_pointer,
415 sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
416 sense_buf[1] = 0; /* Segment number, always zero */
418 if (incorrect_length) {
419 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
420 sense_buf[3] = BYTE3(residue);
421 sense_buf[4] = BYTE2(residue);
422 sense_buf[5] = BYTE1(residue);
423 sense_buf[6] = BYTE0(residue);
425 sense_buf[2] = sense_key; /* Sense key */
427 if (sense_key == SENKEY_ILLEGAL)
428 sense_buf[7] = 10; /* Additional sense length */
430 sense_buf[7] = 6; /* Additional sense length */
432 sense_buf[12] = sense_code; /* Additional sense code */
433 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
434 if (sense_key == SENKEY_ILLEGAL) {
437 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
438 sense_buf[15] = 0x80;/* Std sense key specific field */
439 /* Illegal parameter is in the parameter block */
441 if (sense_code == SENCODE_INVALID_CDB_FIELD)
442 sense_buf[15] = 0xc0;/* Std sense key specific field */
443 /* Illegal parameter is in the CDB block */
444 sense_buf[15] |= bit_pointer;
445 sense_buf[16] = field_pointer >> 8; /* MSB */
446 sense_buf[17] = field_pointer; /* LSB */
450 static void aac_io_done(struct scsi_cmnd * scsicmd)
452 unsigned long cpu_flags;
453 struct Scsi_Host *host = scsicmd->device->host;
454 spin_lock_irqsave(host->host_lock, cpu_flags);
455 scsicmd->scsi_done(scsicmd);
456 spin_unlock_irqrestore(host->host_lock, cpu_flags);
459 static void __aac_io_done(struct scsi_cmnd * scsicmd)
461 scsicmd->scsi_done(scsicmd);
464 int aac_get_adapter_info(struct aac_dev* dev)
467 struct aac_adapter_info* info;
470 if (!(fibptr = fib_alloc(dev)))
474 info = (struct aac_adapter_info*) fib_data(fibptr);
476 memset(info,0,sizeof(struct aac_adapter_info));
478 rcode = fib_send(RequestAdapterInfo,
480 sizeof(struct aac_adapter_info),
486 memcpy(&dev->adapter_info, info, sizeof(struct aac_adapter_info));
488 tmp = dev->adapter_info.kernelrev;
489 printk(KERN_INFO"%s%d: kernel %d.%d.%d build %d\n",
491 tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
492 dev->adapter_info.kernelbuild);
493 tmp = dev->adapter_info.monitorrev;
494 printk(KERN_INFO"%s%d: monitor %d.%d.%d build %d\n",
496 tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
497 dev->adapter_info.monitorbuild);
498 tmp = dev->adapter_info.biosrev;
499 printk(KERN_INFO"%s%d: bios %d.%d.%d build %d\n",
501 tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
502 dev->adapter_info.biosbuild);
503 printk(KERN_INFO"%s%d: serial %x%x\n",
505 dev->adapter_info.serial[0],
506 dev->adapter_info.serial[1]);
508 dev->nondasd_support = 0;
509 if(dev->adapter_info.options & AAC_OPT_NONDASD){
510 // dev->nondasd_support = 1;
511 // dmb - temporarily disable nondasd
514 dev->nondasd_support = (nondasd!=0);
516 if(dev->nondasd_support != 0){
517 printk(KERN_INFO"%s%d: Non-DASD support enabled\n",dev->name, dev->id);
520 dev->pae_support = 0;
521 if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
522 dev->pae_support = 1;
526 dev->pae_support = (paemode!=0);
528 if(dev->pae_support != 0) {
529 printk(KERN_INFO"%s%d: 64 Bit PAE enabled\n", dev->name, dev->id);
530 pci_set_dma_mask(dev->pdev, (dma_addr_t)0xFFFFFFFFFFFFFFFFULL);
533 fib_complete(fibptr);
540 static void read_callback(void *context, struct fib * fibptr)
543 struct aac_read_reply *readreply;
544 struct scsi_cmnd *scsicmd;
548 scsicmd = (struct scsi_cmnd *) context;
550 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
551 cid =TARGET_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
553 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
554 dprintk((KERN_DEBUG "read_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
560 pci_unmap_sg(dev->pdev,
561 (struct scatterlist *)scsicmd->buffer,
563 scsicmd->sc_data_direction);
564 else if(scsicmd->request_bufflen)
565 pci_unmap_single(dev->pdev, (dma_addr_t)(ulong)scsicmd->SCp.ptr,
566 scsicmd->request_bufflen,
567 scsicmd->sc_data_direction);
568 readreply = (struct aac_read_reply *)fib_data(fibptr);
569 if (le32_to_cpu(readreply->status) == ST_OK)
570 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
572 printk(KERN_WARNING "read_callback: read failed, status = %d\n", readreply->status);
573 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
574 set_sense((u8 *) &sense_data[cid],
576 SENCODE_INTERNAL_TARGET_FAILURE,
577 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
580 fib_complete(fibptr);
583 aac_io_done(scsicmd);
586 static void write_callback(void *context, struct fib * fibptr)
589 struct aac_write_reply *writereply;
590 struct scsi_cmnd *scsicmd;
594 scsicmd = (struct scsi_cmnd *) context;
595 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
596 cid = TARGET_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
598 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
599 dprintk((KERN_DEBUG "write_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
604 pci_unmap_sg(dev->pdev,
605 (struct scatterlist *)scsicmd->buffer,
607 scsicmd->sc_data_direction);
608 else if(scsicmd->request_bufflen)
609 pci_unmap_single(dev->pdev, (dma_addr_t)(ulong)scsicmd->SCp.ptr,
610 scsicmd->request_bufflen,
611 scsicmd->sc_data_direction);
613 writereply = (struct aac_write_reply *) fib_data(fibptr);
614 if (le32_to_cpu(writereply->status) == ST_OK)
615 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
617 printk(KERN_WARNING "write_callback: write failed, status = %d\n", writereply->status);
618 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
619 set_sense((u8 *) &sense_data[cid],
621 SENCODE_INTERNAL_TARGET_FAILURE,
622 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
626 fib_complete(fibptr);
628 aac_io_done(scsicmd);
631 int aac_read(struct scsi_cmnd * scsicmd, int cid)
639 struct fib * cmd_fibcontext;
641 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
643 * Get block address and transfer length
645 if (scsicmd->cmnd[0] == READ_6) /* 6 byte command */
647 dprintk((KERN_DEBUG "aachba: received a read(6) command on target %d.\n", cid));
649 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
650 count = scsicmd->cmnd[4];
655 dprintk((KERN_DEBUG "aachba: received a read(10) command on target %d.\n", cid));
657 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
658 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
660 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
662 * Alocate and initialize a Fib
664 if (!(cmd_fibcontext = fib_alloc(dev))) {
665 scsicmd->result = DID_ERROR << 16;
666 aac_io_done(scsicmd);
670 fib_init(cmd_fibcontext);
672 if(dev->pae_support == 1){
673 struct aac_read64 *readcmd;
674 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
675 readcmd->command = cpu_to_le32(VM_CtHostRead64);
676 readcmd->cid = cpu_to_le16(cid);
677 readcmd->sector_count = cpu_to_le16(count);
678 readcmd->block = cpu_to_le32(lba);
679 readcmd->pad = cpu_to_le16(0);
680 readcmd->flags = cpu_to_le16(0);
682 aac_build_sg64(scsicmd, &readcmd->sg);
683 if(readcmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
685 fibsize = sizeof(struct aac_read64) + ((readcmd->sg.count - 1) * sizeof (struct sgentry64));
687 * Now send the Fib to the adapter
689 status = fib_send(ContainerCommand64,
694 (fib_callback) read_callback,
697 struct aac_read *readcmd;
698 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
699 readcmd->command = cpu_to_le32(VM_CtBlockRead);
700 readcmd->cid = cpu_to_le32(cid);
701 readcmd->block = cpu_to_le32(lba);
702 readcmd->count = cpu_to_le32(count * 512);
704 if (count * 512 > (64 * 1024))
707 aac_build_sg(scsicmd, &readcmd->sg);
708 if(readcmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
710 fibsize = sizeof(struct aac_read) + ((readcmd->sg.count - 1) * sizeof (struct sgentry));
712 * Now send the Fib to the adapter
714 status = fib_send(ContainerCommand,
719 (fib_callback) read_callback,
726 * Check that the command queued to the controller
728 if (status == -EINPROGRESS)
731 printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
733 * For some reason, the Fib didn't queue, return QUEUE_FULL
735 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
736 aac_io_done(scsicmd);
737 fib_complete(cmd_fibcontext);
738 fib_free(cmd_fibcontext);
742 static int aac_write(struct scsi_cmnd * scsicmd, int cid)
749 struct fib * cmd_fibcontext;
751 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
753 * Get block address and transfer length
755 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
757 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
758 count = scsicmd->cmnd[4];
762 dprintk((KERN_DEBUG "aachba: received a write(10) command on target %d.\n", cid));
763 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
764 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
766 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
768 * Allocate and initialize a Fib then setup a BlockWrite command
770 if (!(cmd_fibcontext = fib_alloc(dev))) {
771 scsicmd->result = DID_ERROR << 16;
772 aac_io_done(scsicmd);
775 fib_init(cmd_fibcontext);
777 if(dev->pae_support == 1){
778 struct aac_write64 *writecmd;
779 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
780 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
781 writecmd->cid = cpu_to_le16(cid);
782 writecmd->sector_count = cpu_to_le16(count);
783 writecmd->block = cpu_to_le32(lba);
784 writecmd->pad = cpu_to_le16(0);
785 writecmd->flags = cpu_to_le16(0);
787 aac_build_sg64(scsicmd, &writecmd->sg);
788 if(writecmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
790 fibsize = sizeof(struct aac_write64) + ((writecmd->sg.count - 1) * sizeof (struct sgentry64));
792 * Now send the Fib to the adapter
794 status = fib_send(ContainerCommand64,
799 (fib_callback) write_callback,
802 struct aac_write *writecmd;
803 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
804 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
805 writecmd->cid = cpu_to_le32(cid);
806 writecmd->block = cpu_to_le32(lba);
807 writecmd->count = cpu_to_le32(count * 512);
808 writecmd->sg.count = cpu_to_le32(1);
809 /* ->stable is not used - it did mean which type of write */
811 if (count * 512 > (64 * 1024)) {
815 aac_build_sg(scsicmd, &writecmd->sg);
816 if(writecmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
818 fibsize = sizeof(struct aac_write) + ((writecmd->sg.count - 1) * sizeof (struct sgentry));
820 * Now send the Fib to the adapter
822 status = fib_send(ContainerCommand,
827 (fib_callback) write_callback,
832 * Check that the command queued to the controller
834 if (status == -EINPROGRESS)
837 printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
839 * For some reason, the Fib didn't queue, return QUEUE_FULL
841 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
842 aac_io_done(scsicmd);
844 fib_complete(cmd_fibcontext);
845 fib_free(cmd_fibcontext);
851 * aac_scsi_cmd() - Process SCSI command
852 * @scsicmd: SCSI command block
853 * @wait: 1 if the user wants to await completion
855 * Emulate a SCSI command and queue the required request for the
859 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
862 struct fsa_scsi_hba *fsa_dev_ptr;
865 struct Scsi_Host *host = scsicmd->device->host;
866 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
868 cardtype = dev->cardtype;
870 fsa_dev_ptr = fsa_dev[host->unique_id];
873 * If the bus, target or lun is out of range, return fail
874 * Test does not apply to ID 16, the pseudo id for the controller
877 if (scsicmd->device->id != host->this_id) {
878 if ((scsicmd->device->channel == 0) ){
879 if( (scsicmd->device->id >= AAC_MAX_TARGET) || (scsicmd->device->lun != 0)){
880 scsicmd->result = DID_NO_CONNECT << 16;
881 __aac_io_done(scsicmd);
884 cid = TARGET_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
887 * If the target container doesn't exist, it may have
890 if (fsa_dev_ptr->valid[cid] == 0) {
891 switch (scsicmd->cmnd[0]) {
894 case TEST_UNIT_READY:
895 spin_unlock_irq(host->host_lock);
896 probe_container(dev, cid);
897 spin_lock_irq(host->host_lock);
898 if (fsa_dev_ptr->valid[cid] == 0) {
899 scsicmd->result = DID_NO_CONNECT << 16;
900 __aac_io_done(scsicmd);
908 * If the target container still doesn't exist,
911 if (fsa_dev_ptr->valid[cid] == 0) {
912 scsicmd->result = DID_BAD_TARGET << 16;
913 __aac_io_done(scsicmd);
916 } else { /* check for physical non-dasd devices */
917 if(dev->nondasd_support == 1){
918 return aac_send_srb_fib(scsicmd);
920 scsicmd->result = DID_NO_CONNECT << 16;
921 __aac_io_done(scsicmd);
927 * else Command for the controller itself
929 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
930 (scsicmd->cmnd[0] != TEST_UNIT_READY))
932 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
933 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
934 set_sense((u8 *) &sense_data[cid],
936 SENCODE_INVALID_COMMAND,
937 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
938 __aac_io_done(scsicmd);
943 /* Handle commands here that don't really require going out to the adapter */
944 switch (scsicmd->cmnd[0]) {
947 struct inquiry_data *inq_data_ptr;
949 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
950 inq_data_ptr = (struct inquiry_data *)scsicmd->request_buffer;
951 memset(inq_data_ptr, 0, sizeof (struct inquiry_data));
953 inq_data_ptr->inqd_ver = 2; /* claim compliance to SCSI-2 */
954 inq_data_ptr->inqd_dtq = 0x80; /* set RMB bit to one indicating that the medium is removable */
955 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 */
956 inq_data_ptr->inqd_len = 31;
957 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
958 inq_data_ptr->inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
960 * Set the Vendor, Product, and Revision Level
961 * see: <vendor>.c i.e. aac.c
963 setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), fsa_dev_ptr->type[cid]);
964 if (scsicmd->device->id == host->this_id)
965 inq_data_ptr->inqd_pdt = INQD_PDT_PROC; /* Processor device */
967 inq_data_ptr->inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
968 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
969 __aac_io_done(scsicmd);
977 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
978 capacity = fsa_dev_ptr->size[cid] - 1;
979 cp = scsicmd->request_buffer;
980 cp[0] = (capacity >> 24) & 0xff;
981 cp[1] = (capacity >> 16) & 0xff;
982 cp[2] = (capacity >> 8) & 0xff;
983 cp[3] = (capacity >> 0) & 0xff;
989 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
990 __aac_io_done(scsicmd);
999 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1000 mode_buf = scsicmd->request_buffer;
1001 mode_buf[0] = 3; /* Mode data length */
1002 mode_buf[1] = 0; /* Medium type - default */
1003 mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1004 mode_buf[3] = 0; /* Block descriptor length */
1006 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1007 __aac_io_done(scsicmd);
1015 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1016 mode_buf = scsicmd->request_buffer;
1017 mode_buf[0] = 0; /* Mode data length (MSB) */
1018 mode_buf[1] = 6; /* Mode data length (LSB) */
1019 mode_buf[2] = 0; /* Medium type - default */
1020 mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1021 mode_buf[4] = 0; /* reserved */
1022 mode_buf[5] = 0; /* reserved */
1023 mode_buf[6] = 0; /* Block descriptor length (MSB) */
1024 mode_buf[7] = 0; /* Block descriptor length (LSB) */
1026 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1027 __aac_io_done(scsicmd);
1032 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1033 memcpy(scsicmd->sense_buffer, &sense_data[cid], sizeof (struct sense_data));
1034 memset(&sense_data[cid], 0, sizeof (struct sense_data));
1035 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1036 __aac_io_done(scsicmd);
1039 case ALLOW_MEDIUM_REMOVAL:
1040 dprintk((KERN_DEBUG "LOCK command.\n"));
1041 if (scsicmd->cmnd[4])
1042 fsa_dev_ptr->locked[cid] = 1;
1044 fsa_dev_ptr->locked[cid] = 0;
1046 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1047 __aac_io_done(scsicmd);
1050 * These commands are all No-Ops
1052 case TEST_UNIT_READY:
1056 case REASSIGN_BLOCKS:
1059 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1060 __aac_io_done(scsicmd);
1064 switch (scsicmd->cmnd[0])
1069 * Hack to keep track of ordinal number of the device that
1070 * corresponds to a container. Needed to convert
1071 * containers to /dev/sd device names
1074 spin_unlock_irq(host->host_lock);
1075 if (scsicmd->request->rq_disk)
1076 memcpy(fsa_dev_ptr->devname[cid],
1077 scsicmd->request->rq_disk->disk_name,
1080 ret = aac_read(scsicmd, cid);
1081 spin_lock_irq(host->host_lock);
1086 spin_unlock_irq(host->host_lock);
1087 ret = aac_write(scsicmd, cid);
1088 spin_lock_irq(host->host_lock);
1092 * Unhandled commands
1094 printk(KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]);
1095 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1096 set_sense((u8 *) &sense_data[cid],
1097 SENKEY_ILLEGAL, SENCODE_INVALID_COMMAND,
1098 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1099 __aac_io_done(scsicmd);
1104 static int query_disk(struct aac_dev *dev, void *arg)
1106 struct aac_query_disk qd;
1107 struct fsa_scsi_hba *fsa_dev_ptr;
1109 fsa_dev_ptr = &(dev->fsa_dev);
1110 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1113 qd.cnum = TARGET_LUN_TO_CONTAINER(qd.target, qd.lun);
1114 else if ((qd.bus == -1) && (qd.target == -1) && (qd.lun == -1))
1116 if (qd.cnum < 0 || qd.cnum >= MAXIMUM_NUM_CONTAINERS)
1118 qd.instance = dev->scsi_host_ptr->host_no;
1120 qd.target = CONTAINER_TO_TARGET(qd.cnum);
1121 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1123 else return -EINVAL;
1125 qd.valid = fsa_dev_ptr->valid[qd.cnum];
1126 qd.locked = fsa_dev_ptr->locked[qd.cnum];
1127 qd.deleted = fsa_dev_ptr->deleted[qd.cnum];
1129 if (fsa_dev_ptr->devname[qd.cnum][0] == '\0')
1134 strlcpy(qd.name, fsa_dev_ptr->devname[qd.cnum], sizeof(qd.name));
1136 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1141 static int force_delete_disk(struct aac_dev *dev, void *arg)
1143 struct aac_delete_disk dd;
1144 struct fsa_scsi_hba *fsa_dev_ptr;
1146 fsa_dev_ptr = &(dev->fsa_dev);
1148 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1151 if (dd.cnum >= MAXIMUM_NUM_CONTAINERS)
1154 * Mark this container as being deleted.
1156 fsa_dev_ptr->deleted[dd.cnum] = 1;
1158 * Mark the container as no longer valid
1160 fsa_dev_ptr->valid[dd.cnum] = 0;
1164 static int delete_disk(struct aac_dev *dev, void *arg)
1166 struct aac_delete_disk dd;
1167 struct fsa_scsi_hba *fsa_dev_ptr;
1169 fsa_dev_ptr = &(dev->fsa_dev);
1171 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1174 if (dd.cnum >= MAXIMUM_NUM_CONTAINERS)
1177 * If the container is locked, it can not be deleted by the API.
1179 if (fsa_dev_ptr->locked[dd.cnum])
1183 * Mark the container as no longer being valid.
1185 fsa_dev_ptr->valid[dd.cnum] = 0;
1186 fsa_dev_ptr->devname[dd.cnum][0] = '\0';
1191 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void *arg)
1194 case FSACTL_QUERY_DISK:
1195 return query_disk(dev, arg);
1196 case FSACTL_DELETE_DISK:
1197 return delete_disk(dev, arg);
1198 case FSACTL_FORCE_DELETE_DISK:
1199 return force_delete_disk(dev, arg);
1201 return aac_get_containers(dev);
1210 * @context: the context set in the fib - here it is scsi cmd
1211 * @fibptr: pointer to the fib
1213 * Handles the completion of a scsi command to a non dasd device
1217 static void aac_srb_callback(void *context, struct fib * fibptr)
1219 struct aac_dev *dev;
1220 struct aac_srb_reply *srbreply;
1221 struct scsi_cmnd *scsicmd;
1223 scsicmd = (struct scsi_cmnd *) context;
1224 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1229 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1231 scsicmd->sense_buffer[0] = '\0'; // initialize sense valid flag to false
1232 // calculate resid for sg
1233 scsicmd->resid = scsicmd->request_bufflen - srbreply->data_xfer_length;
1236 pci_unmap_sg(dev->pdev,
1237 (struct scatterlist *)scsicmd->buffer,
1239 scsicmd->sc_data_direction);
1240 else if(scsicmd->request_bufflen)
1241 pci_unmap_single(dev->pdev, (ulong)scsicmd->SCp.ptr, scsicmd->request_bufflen,
1242 scsicmd->sc_data_direction);
1245 * First check the fib status
1248 if (le32_to_cpu(srbreply->status) != ST_OK){
1250 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1251 len = (srbreply->sense_data_size > sizeof(scsicmd->sense_buffer))?
1252 sizeof(scsicmd->sense_buffer):srbreply->sense_data_size;
1253 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1254 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1258 * Next check the srb status
1260 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1261 case SRB_STATUS_ERROR_RECOVERY:
1262 case SRB_STATUS_PENDING:
1263 case SRB_STATUS_SUCCESS:
1264 if(scsicmd->cmnd[0] == INQUIRY ){
1267 /* We can't expose disk devices because we can't tell whether they
1268 * are the raw container drives or stand alone drives. If they have
1269 * the removable bit set then we should expose them though.
1271 b = (*(u8*)scsicmd->buffer)&0x1f;
1272 b1 = ((u8*)scsicmd->buffer)[1];
1273 if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1274 || (b==TYPE_DISK && (b1&0x80)) ){
1275 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1277 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1280 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1283 case SRB_STATUS_DATA_OVERRUN:
1284 switch(scsicmd->cmnd[0]){
1291 if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1292 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1294 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1296 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1301 /* We can't expose disk devices because we can't tell whether they
1302 * are the raw container drives or stand alone drives
1304 b = (*(u8*)scsicmd->buffer)&0x0f;
1305 b1 = ((u8*)scsicmd->buffer)[1];
1306 if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1307 || (b==TYPE_DISK && (b1&0x80)) ){
1308 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1310 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1315 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1319 case SRB_STATUS_ABORTED:
1320 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1322 case SRB_STATUS_ABORT_FAILED:
1323 // Not sure about this one - but assuming the hba was trying to abort for some reason
1324 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1326 case SRB_STATUS_PARITY_ERROR:
1327 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1329 case SRB_STATUS_NO_DEVICE:
1330 case SRB_STATUS_INVALID_PATH_ID:
1331 case SRB_STATUS_INVALID_TARGET_ID:
1332 case SRB_STATUS_INVALID_LUN:
1333 case SRB_STATUS_SELECTION_TIMEOUT:
1334 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1337 case SRB_STATUS_COMMAND_TIMEOUT:
1338 case SRB_STATUS_TIMEOUT:
1339 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
1342 case SRB_STATUS_BUSY:
1343 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1346 case SRB_STATUS_BUS_RESET:
1347 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
1350 case SRB_STATUS_MESSAGE_REJECTED:
1351 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
1353 case SRB_STATUS_REQUEST_FLUSHED:
1354 case SRB_STATUS_ERROR:
1355 case SRB_STATUS_INVALID_REQUEST:
1356 case SRB_STATUS_REQUEST_SENSE_FAILED:
1357 case SRB_STATUS_NO_HBA:
1358 case SRB_STATUS_UNEXPECTED_BUS_FREE:
1359 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
1360 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
1361 case SRB_STATUS_DELAYED_RETRY:
1362 case SRB_STATUS_BAD_FUNCTION:
1363 case SRB_STATUS_NOT_STARTED:
1364 case SRB_STATUS_NOT_IN_USE:
1365 case SRB_STATUS_FORCE_ABORT:
1366 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
1368 #ifdef AAC_DETAILED_STATUS_INFO
1369 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",le32_to_cpu(srbreply->srb_status&0x3f),aac_get_status_string(le32_to_cpu(srbreply->srb_status)), scsicmd->cmnd[0], le32_to_cpu(srbreply->scsi_status) );
1371 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1374 if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
1376 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
1377 len = (srbreply->sense_data_size > sizeof(scsicmd->sense_buffer))?
1378 sizeof(scsicmd->sense_buffer):srbreply->sense_data_size;
1379 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n", le32_to_cpu(srbreply->status), len);
1380 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1384 * OR in the scsi status (already shifted up a bit)
1386 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
1388 fib_complete(fibptr);
1390 aac_io_done(scsicmd);
1396 * @scsicmd: the scsi command block
1398 * This routine will form a FIB and fill in the aac_srb from the
1399 * scsicmd passed in.
1402 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
1404 struct fib* cmd_fibcontext;
1405 struct aac_dev* dev;
1407 struct aac_srb *srbcmd;
1412 if( scsicmd->device->id > 15 || scsicmd->device->lun > 7) {
1413 scsicmd->result = DID_NO_CONNECT << 16;
1414 __aac_io_done(scsicmd);
1418 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1419 switch(scsicmd->sc_data_direction){
1423 case DMA_BIDIRECTIONAL:
1424 flag = SRB_DataIn | SRB_DataOut;
1426 case DMA_FROM_DEVICE:
1430 default: /* shuts up some versions of gcc */
1431 flag = SRB_NoDataXfer;
1437 * Allocate and initialize a Fib then setup a BlockWrite command
1439 if (!(cmd_fibcontext = fib_alloc(dev))) {
1440 scsicmd->result = DID_ERROR << 16;
1441 __aac_io_done(scsicmd);
1444 fib_init(cmd_fibcontext);
1446 srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
1447 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1448 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
1449 srbcmd->target = cpu_to_le32(scsicmd->device->id);
1450 srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
1451 srbcmd->flags = cpu_to_le32(flag);
1452 timeout = (scsicmd->timeout-jiffies)/HZ;
1456 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1457 srbcmd->retry_limit =cpu_to_le32(0); // Obsolete parameter
1458 srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
1460 if( dev->pae_support ==1 ) {
1461 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
1462 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1464 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1465 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1467 * Build Scatter/Gather list
1469 fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry64));
1472 * Now send the Fib to the adapter
1474 status = fib_send(ScsiPortCommand64, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
1475 (fib_callback) aac_srb_callback, (void *) scsicmd);
1477 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
1478 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1480 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1481 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1483 * Build Scatter/Gather list
1485 fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
1488 * Now send the Fib to the adapter
1490 status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
1491 (fib_callback) aac_srb_callback, (void *) scsicmd);
1494 * Check that the command queued to the controller
1496 if (status == -EINPROGRESS){
1500 printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
1502 * For some reason, the Fib didn't queue, return QUEUE_FULL
1504 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1505 __aac_io_done(scsicmd);
1507 fib_complete(cmd_fibcontext);
1508 fib_free(cmd_fibcontext);
1513 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
1515 struct aac_dev *dev;
1516 unsigned long byte_count = 0;
1518 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1519 // Get rid of old data
1520 psg->count = cpu_to_le32(0);
1521 psg->sg[0].addr = cpu_to_le32(0);
1522 psg->sg[0].count = cpu_to_le32(0);
1523 if (scsicmd->use_sg) {
1524 struct scatterlist *sg;
1527 sg = (struct scatterlist *) scsicmd->request_buffer;
1529 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1530 scsicmd->sc_data_direction);
1531 psg->count = cpu_to_le32(sg_count);
1535 for (i = 0; i < sg_count; i++) {
1536 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
1537 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1538 byte_count += sg_dma_len(sg);
1541 /* hba wants the size to be exact */
1542 if(byte_count > scsicmd->request_bufflen){
1543 psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
1544 byte_count = scsicmd->request_bufflen;
1546 /* Check for command underflow */
1547 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
1548 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
1549 byte_count, scsicmd->underflow);
1552 else if(scsicmd->request_bufflen) {
1554 addr = pci_map_single(dev->pdev,
1555 scsicmd->request_buffer,
1556 scsicmd->request_bufflen,
1557 scsicmd->sc_data_direction);
1558 psg->count = cpu_to_le32(1);
1559 psg->sg[0].addr = cpu_to_le32(addr);
1560 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
1561 scsicmd->SCp.ptr = (char *)(ulong)addr;
1562 byte_count = scsicmd->request_bufflen;
1568 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
1570 struct aac_dev *dev;
1571 unsigned long byte_count = 0;
1574 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1575 // Get rid of old data
1576 psg->count = cpu_to_le32(0);
1577 psg->sg[0].addr[0] = cpu_to_le32(0);
1578 psg->sg[0].addr[1] = cpu_to_le32(0);
1579 psg->sg[0].count = cpu_to_le32(0);
1580 if (scsicmd->use_sg) {
1581 struct scatterlist *sg;
1584 sg = (struct scatterlist *) scsicmd->request_buffer;
1586 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1587 scsicmd->sc_data_direction);
1588 psg->count = cpu_to_le32(sg_count);
1592 for (i = 0; i < sg_count; i++) {
1593 le_addr = cpu_to_le64(sg_dma_address(sg));
1594 psg->sg[i].addr[1] = (u32)(le_addr>>32);
1595 psg->sg[i].addr[0] = (u32)(le_addr & 0xffffffff);
1596 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1597 byte_count += sg_dma_len(sg);
1600 /* hba wants the size to be exact */
1601 if(byte_count > scsicmd->request_bufflen){
1602 psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
1603 byte_count = scsicmd->request_bufflen;
1605 /* Check for command underflow */
1606 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
1607 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
1608 byte_count, scsicmd->underflow);
1611 else if(scsicmd->request_bufflen) {
1613 addr = pci_map_single(dev->pdev,
1614 scsicmd->request_buffer,
1615 scsicmd->request_bufflen,
1616 scsicmd->sc_data_direction);
1617 psg->count = cpu_to_le32(1);
1618 le_addr = cpu_to_le64(addr);
1619 psg->sg[0].addr[1] = (u32)(le_addr>>32);
1620 psg->sg[0].addr[0] = (u32)(le_addr & 0xffffffff);
1621 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
1622 scsicmd->SCp.ptr = (char *)(ulong)addr;
1623 byte_count = scsicmd->request_bufflen;
1628 #ifdef AAC_DETAILED_STATUS_INFO
1630 struct aac_srb_status_info {
1636 static struct aac_srb_status_info srb_status_info[] = {
1637 { SRB_STATUS_PENDING, "Pending Status"},
1638 { SRB_STATUS_SUCCESS, "Success"},
1639 { SRB_STATUS_ABORTED, "Aborted Command"},
1640 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
1641 { SRB_STATUS_ERROR, "Error Event"},
1642 { SRB_STATUS_BUSY, "Device Busy"},
1643 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
1644 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
1645 { SRB_STATUS_NO_DEVICE, "No Device"},
1646 { SRB_STATUS_TIMEOUT, "Timeout"},
1647 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
1648 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
1649 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
1650 { SRB_STATUS_BUS_RESET, "Bus Reset"},
1651 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
1652 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
1653 { SRB_STATUS_NO_HBA, "No HBA"},
1654 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
1655 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
1656 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
1657 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
1658 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
1659 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
1660 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
1661 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
1662 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
1663 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
1664 { SRB_STATUS_NOT_STARTED, "Not Started"},
1665 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
1666 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
1667 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
1668 { 0xff, "Unknown Error"}
1671 char *aac_get_status_string(u32 status)
1675 for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
1676 if(srb_status_info[i].status == status){
1677 return srb_status_info[i].str;
1681 return "Bad Status Code";