2 * SuperTrak EX Series Storage Controller driver for Linux
4 * Copyright (C) 2005, 2006 Promise Technology Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 * Ed Lin <promise_linux@promise.com>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/delay.h>
20 #include <linux/time.h>
21 #include <linux/pci.h>
22 #include <linux/blkdev.h>
23 #include <linux/interrupt.h>
24 #include <linux/types.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
29 #include <asm/byteorder.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/scsi_dbg.h>
37 #define DRV_NAME "stex"
38 #define ST_DRIVER_VERSION "3.6.0000.1"
39 #define ST_VER_MAJOR 3
40 #define ST_VER_MINOR 6
42 #define ST_BUILD_VER 1
45 /* MU register offset */
46 IMR0
= 0x10, /* MU_INBOUND_MESSAGE_REG0 */
47 IMR1
= 0x14, /* MU_INBOUND_MESSAGE_REG1 */
48 OMR0
= 0x18, /* MU_OUTBOUND_MESSAGE_REG0 */
49 OMR1
= 0x1c, /* MU_OUTBOUND_MESSAGE_REG1 */
50 IDBL
= 0x20, /* MU_INBOUND_DOORBELL */
51 IIS
= 0x24, /* MU_INBOUND_INTERRUPT_STATUS */
52 IIM
= 0x28, /* MU_INBOUND_INTERRUPT_MASK */
53 ODBL
= 0x2c, /* MU_OUTBOUND_DOORBELL */
54 OIS
= 0x30, /* MU_OUTBOUND_INTERRUPT_STATUS */
55 OIM
= 0x3c, /* MU_OUTBOUND_INTERRUPT_MASK */
57 /* MU register value */
58 MU_INBOUND_DOORBELL_HANDSHAKE
= 1,
59 MU_INBOUND_DOORBELL_REQHEADCHANGED
= 2,
60 MU_INBOUND_DOORBELL_STATUSTAILCHANGED
= 4,
61 MU_INBOUND_DOORBELL_HMUSTOPPED
= 8,
62 MU_INBOUND_DOORBELL_RESET
= 16,
64 MU_OUTBOUND_DOORBELL_HANDSHAKE
= 1,
65 MU_OUTBOUND_DOORBELL_REQUESTTAILCHANGED
= 2,
66 MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED
= 4,
67 MU_OUTBOUND_DOORBELL_BUSCHANGE
= 8,
68 MU_OUTBOUND_DOORBELL_HASEVENT
= 16,
71 MU_STATE_STARTING
= 1,
72 MU_STATE_FMU_READY_FOR_HANDSHAKE
= 2,
73 MU_STATE_SEND_HANDSHAKE_FRAME
= 3,
75 MU_STATE_RESETTING
= 5,
78 MU_HANDSHAKE_SIGNATURE
= 0x55aaaa55,
79 MU_HANDSHAKE_SIGNATURE_HALF
= 0x5a5a0000,
80 MU_HARD_RESET_WAIT
= 30000,
83 /* firmware returned values */
84 SRB_STATUS_SUCCESS
= 0x01,
85 SRB_STATUS_ERROR
= 0x04,
86 SRB_STATUS_BUSY
= 0x05,
87 SRB_STATUS_INVALID_REQUEST
= 0x06,
88 SRB_STATUS_SELECTION_TIMEOUT
= 0x0A,
92 TASK_ATTRIBUTE_SIMPLE
= 0x0,
93 TASK_ATTRIBUTE_HEADOFQUEUE
= 0x1,
94 TASK_ATTRIBUTE_ORDERED
= 0x2,
95 TASK_ATTRIBUTE_ACA
= 0x4,
97 /* request count, etc. */
100 /* one message wasted, use MU_MAX_REQUEST+1
101 to handle MU_MAX_REQUEST messages */
102 MU_REQ_COUNT
= (MU_MAX_REQUEST
+ 1),
103 MU_STATUS_COUNT
= (MU_MAX_REQUEST
+ 1),
105 STEX_CDB_LENGTH
= MAX_COMMAND_SIZE
,
106 REQ_VARIABLE_LEN
= 1024,
107 STATUS_VAR_LEN
= 128,
108 ST_CAN_QUEUE
= MU_MAX_REQUEST
,
109 ST_CMD_PER_LUN
= MU_MAX_REQUEST
,
113 SG_CF_EOT
= 0x80, /* end of table */
114 SG_CF_64B
= 0x40, /* 64 bit item */
115 SG_CF_HOST
= 0x20, /* sg in host memory */
122 PASSTHRU_REQ_TYPE
= 0x00000001,
123 PASSTHRU_REQ_NO_WAKEUP
= 0x00000100,
124 ST_INTERNAL_TIMEOUT
= 30,
129 /* vendor specific commands of Promise */
131 SINBAND_MGT_CMD
= 0xd9,
133 CONTROLLER_CMD
= 0xe1,
134 DEBUGGING_CMD
= 0xe2,
137 PASSTHRU_GET_ADAPTER
= 0x05,
138 PASSTHRU_GET_DRVVER
= 0x10,
140 CTLR_CONFIG_CMD
= 0x03,
141 CTLR_SHUTDOWN
= 0x0d,
143 CTLR_POWER_STATE_CHANGE
= 0x0e,
144 CTLR_POWER_SAVING
= 0x01,
146 PASSTHRU_SIGNATURE
= 0x4e415041,
147 MGT_CMD_SIGNATURE
= 0xba,
151 ST_ADDITIONAL_MEM
= 0x200000,
154 /* SCSI inquiry data */
155 typedef struct st_inq
{
157 u8 DeviceTypeQualifier
:3;
158 u8 DeviceTypeModifier
:7;
159 u8 RemovableMedia
:1;
161 u8 ResponseDataFormat
:4;
171 u8 LinkedCommands
:1;
175 u8 RelativeAddressing
:1;
178 u8 ProductRevisionLevel
[4];
179 u8 VendorSpecific
[20];
184 u8 ctrl
; /* SG_CF_xxx */
195 struct st_sgitem table
[ST_MAX_SG
];
198 struct handshake_frame
{
199 __le32 rb_phy
; /* request payload queue physical address */
201 __le16 req_sz
; /* size of each request payload */
202 __le16 req_cnt
; /* count of reqs the buffer can hold */
203 __le16 status_sz
; /* size of each status payload */
204 __le16 status_cnt
; /* count of status the buffer can hold */
205 __le32 hosttime
; /* seconds from Jan 1, 1970 (GMT) */
207 u8 partner_type
; /* who sends this frame */
209 __le32 partner_ver_major
;
210 __le32 partner_ver_minor
;
211 __le32 partner_ver_oem
;
212 __le32 partner_ver_build
;
213 __le32 extra_offset
; /* NEW */
214 __le32 extra_size
; /* NEW */
225 u8 payload_sz
; /* payload size in 4-byte, not used */
226 u8 cdb
[STEX_CDB_LENGTH
];
227 u8 variable
[REQ_VARIABLE_LEN
];
237 u8 payload_sz
; /* payload size in 4-byte */
238 u8 variable
[STATUS_VAR_LEN
];
253 struct ver_info drv_ver
;
254 struct ver_info bios_ver
;
283 #define MU_REQ_BUFFER_SIZE (MU_REQ_COUNT * sizeof(struct req_msg))
284 #define MU_STATUS_BUFFER_SIZE (MU_STATUS_COUNT * sizeof(struct status_msg))
285 #define MU_BUFFER_SIZE (MU_REQ_BUFFER_SIZE + MU_STATUS_BUFFER_SIZE)
286 #define STEX_EXTRA_SIZE max(sizeof(struct st_frame), sizeof(ST_INQ))
287 #define STEX_BUFFER_SIZE (MU_BUFFER_SIZE + STEX_EXTRA_SIZE)
291 struct scsi_cmnd
*cmd
;
294 unsigned int sense_bufflen
;
303 void __iomem
*mmio_base
; /* iomapped PCI memory space */
305 dma_addr_t dma_handle
;
308 struct Scsi_Host
*host
;
309 struct pci_dev
*pdev
;
316 struct status_msg
*status_buffer
;
317 void *copy_buffer
; /* temp buffer for driver-handled commands */
318 struct st_ccb ccb
[MU_MAX_REQUEST
];
319 struct st_ccb
*wait_ccb
;
320 wait_queue_head_t waitq
;
322 unsigned int mu_status
;
325 unsigned int cardtype
;
328 static const char console_inq_page
[] =
330 0x03,0x00,0x03,0x03,0xFA,0x00,0x00,0x30,
331 0x50,0x72,0x6F,0x6D,0x69,0x73,0x65,0x20, /* "Promise " */
332 0x52,0x41,0x49,0x44,0x20,0x43,0x6F,0x6E, /* "RAID Con" */
333 0x73,0x6F,0x6C,0x65,0x20,0x20,0x20,0x20, /* "sole " */
334 0x31,0x2E,0x30,0x30,0x20,0x20,0x20,0x20, /* "1.00 " */
335 0x53,0x58,0x2F,0x52,0x53,0x41,0x46,0x2D, /* "SX/RSAF-" */
336 0x54,0x45,0x31,0x2E,0x30,0x30,0x20,0x20, /* "TE1.00 " */
337 0x0C,0x20,0x20,0x20,0x20,0x20,0x20,0x20
340 MODULE_AUTHOR("Ed Lin");
341 MODULE_DESCRIPTION("Promise Technology SuperTrak EX Controllers");
342 MODULE_LICENSE("GPL");
343 MODULE_VERSION(ST_DRIVER_VERSION
);
345 static void stex_gettime(__le32
*time
)
348 do_gettimeofday(&tv
);
350 *time
= cpu_to_le32(tv
.tv_sec
& 0xffffffff);
351 *(time
+ 1) = cpu_to_le32((tv
.tv_sec
>> 16) >> 16);
354 static struct status_msg
*stex_get_status(struct st_hba
*hba
)
356 struct status_msg
*status
=
357 hba
->status_buffer
+ hba
->status_tail
;
360 hba
->status_tail
%= MU_STATUS_COUNT
;
365 static void stex_set_sense(struct scsi_cmnd
*cmd
, u8 sk
, u8 asc
, u8 ascq
)
367 cmd
->result
= (DRIVER_SENSE
<< 24) | SAM_STAT_CHECK_CONDITION
;
369 cmd
->sense_buffer
[0] = 0x70; /* fixed format, current */
370 cmd
->sense_buffer
[2] = sk
;
371 cmd
->sense_buffer
[7] = 18 - 8; /* additional sense length */
372 cmd
->sense_buffer
[12] = asc
;
373 cmd
->sense_buffer
[13] = ascq
;
376 static void stex_invalid_field(struct scsi_cmnd
*cmd
,
377 void (*done
)(struct scsi_cmnd
*))
379 /* "Invalid field in cbd" */
380 stex_set_sense(cmd
, ILLEGAL_REQUEST
, 0x24, 0x0);
384 static struct req_msg
*stex_alloc_req(struct st_hba
*hba
)
386 struct req_msg
*req
= ((struct req_msg
*)hba
->dma_mem
) +
390 hba
->req_head
%= MU_REQ_COUNT
;
395 static int stex_map_sg(struct st_hba
*hba
,
396 struct req_msg
*req
, struct st_ccb
*ccb
)
398 struct pci_dev
*pdev
= hba
->pdev
;
399 struct scsi_cmnd
*cmd
;
400 dma_addr_t dma_handle
;
401 struct scatterlist
*src
;
402 struct st_sgtable
*dst
;
406 dst
= (struct st_sgtable
*)req
->variable
;
407 dst
->max_sg_count
= cpu_to_le16(ST_MAX_SG
);
408 dst
->sz_in_byte
= cpu_to_le32(cmd
->request_bufflen
);
413 src
= (struct scatterlist
*) cmd
->request_buffer
;
414 n_elem
= pci_map_sg(pdev
, src
,
415 cmd
->use_sg
, cmd
->sc_data_direction
);
419 ccb
->sg_count
= n_elem
;
420 dst
->sg_count
= cpu_to_le16((u16
)n_elem
);
422 for (i
= 0; i
< n_elem
; i
++, src
++) {
423 dst
->table
[i
].count
= cpu_to_le32((u32
)sg_dma_len(src
));
425 cpu_to_le32(sg_dma_address(src
) & 0xffffffff);
426 dst
->table
[i
].addr_hi
=
427 cpu_to_le32((sg_dma_address(src
) >> 16) >> 16);
428 dst
->table
[i
].ctrl
= SG_CF_64B
| SG_CF_HOST
;
430 dst
->table
[--i
].ctrl
|= SG_CF_EOT
;
434 dma_handle
= pci_map_single(pdev
, cmd
->request_buffer
,
435 cmd
->request_bufflen
, cmd
->sc_data_direction
);
436 cmd
->SCp
.dma_handle
= dma_handle
;
439 dst
->sg_count
= cpu_to_le16(1);
440 dst
->table
[0].addr
= cpu_to_le32(dma_handle
& 0xffffffff);
441 dst
->table
[0].addr_hi
= cpu_to_le32((dma_handle
>> 16) >> 16);
442 dst
->table
[0].count
= cpu_to_le32((u32
)cmd
->request_bufflen
);
443 dst
->table
[0].ctrl
= SG_CF_EOT
| SG_CF_64B
| SG_CF_HOST
;
448 static void stex_internal_copy(struct scsi_cmnd
*cmd
,
449 const void *src
, size_t *count
, int sg_count
, int direction
)
453 void *s
, *d
, *base
= NULL
;
454 if (*count
> cmd
->request_bufflen
)
455 *count
= cmd
->request_bufflen
;
461 size_t offset
= *count
- lcount
;
463 base
= scsi_kmap_atomic_sg(cmd
->request_buffer
,
464 sg_count
, &offset
, &len
);
471 d
= cmd
->request_buffer
;
473 if (direction
== ST_TO_CMD
)
480 scsi_kunmap_atomic_sg(base
);
484 static int stex_direct_copy(struct scsi_cmnd
*cmd
,
485 const void *src
, size_t count
)
487 struct st_hba
*hba
= (struct st_hba
*) &cmd
->device
->host
->hostdata
[0];
488 size_t cp_len
= count
;
492 n_elem
= pci_map_sg(hba
->pdev
, cmd
->request_buffer
,
493 cmd
->use_sg
, cmd
->sc_data_direction
);
498 stex_internal_copy(cmd
, src
, &cp_len
, n_elem
, ST_TO_CMD
);
501 pci_unmap_sg(hba
->pdev
, cmd
->request_buffer
,
502 cmd
->use_sg
, cmd
->sc_data_direction
);
503 return cp_len
== count
;
506 static void stex_controller_info(struct st_hba
*hba
, struct st_ccb
*ccb
)
509 size_t count
= sizeof(struct st_frame
);
511 p
= hba
->copy_buffer
;
512 stex_internal_copy(ccb
->cmd
, p
, &count
, ccb
->sg_count
, ST_FROM_CMD
);
513 memset(p
->base
, 0, sizeof(u32
)*6);
514 *(unsigned long *)(p
->base
) = pci_resource_start(hba
->pdev
, 0);
517 p
->drv_ver
.major
= ST_VER_MAJOR
;
518 p
->drv_ver
.minor
= ST_VER_MINOR
;
519 p
->drv_ver
.oem
= ST_OEM
;
520 p
->drv_ver
.build
= ST_BUILD_VER
;
522 p
->bus
= hba
->pdev
->bus
->number
;
523 p
->slot
= hba
->pdev
->devfn
;
525 p
->irq_vec
= hba
->pdev
->irq
;
526 p
->id
= hba
->pdev
->vendor
<< 16 | hba
->pdev
->device
;
528 hba
->pdev
->subsystem_vendor
<< 16 | hba
->pdev
->subsystem_device
;
530 stex_internal_copy(ccb
->cmd
, p
, &count
, ccb
->sg_count
, ST_TO_CMD
);
534 stex_send_cmd(struct st_hba
*hba
, struct req_msg
*req
, u16 tag
)
536 req
->tag
= cpu_to_le16(tag
);
537 req
->task_attr
= TASK_ATTRIBUTE_SIMPLE
;
538 req
->task_manage
= 0; /* not supported yet */
540 hba
->ccb
[tag
].req
= req
;
543 writel(hba
->req_head
, hba
->mmio_base
+ IMR0
);
544 writel(MU_INBOUND_DOORBELL_REQHEADCHANGED
, hba
->mmio_base
+ IDBL
);
545 readl(hba
->mmio_base
+ IDBL
); /* flush */
549 stex_slave_alloc(struct scsi_device
*sdev
)
551 /* Cheat: usually extracted from Inquiry data */
552 sdev
->tagged_supported
= 1;
554 scsi_activate_tcq(sdev
, sdev
->host
->can_queue
);
560 stex_slave_config(struct scsi_device
*sdev
)
562 sdev
->use_10_for_rw
= 1;
563 sdev
->use_10_for_ms
= 1;
564 sdev
->timeout
= 60 * HZ
;
565 sdev
->tagged_supported
= 1;
571 stex_slave_destroy(struct scsi_device
*sdev
)
573 scsi_deactivate_tcq(sdev
, 1);
577 stex_queuecommand(struct scsi_cmnd
*cmd
, void (* done
)(struct scsi_cmnd
*))
580 struct Scsi_Host
*host
;
584 host
= cmd
->device
->host
;
585 id
= cmd
->device
->id
;
586 lun
= cmd
->device
->lun
;
587 hba
= (struct st_hba
*) &host
->hostdata
[0];
589 switch (cmd
->cmnd
[0]) {
592 static char ms10_caching_page
[12] =
593 { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
595 page
= cmd
->cmnd
[2] & 0x3f;
596 if (page
== 0x8 || page
== 0x3f) {
597 stex_direct_copy(cmd
, ms10_caching_page
,
598 sizeof(ms10_caching_page
));
599 cmd
->result
= DID_OK
<< 16 | COMMAND_COMPLETE
<< 8;
602 stex_invalid_field(cmd
, done
);
607 * The shasta firmware does not report actual luns in the
608 * target, so fail the command to force sequential lun scan.
609 * Also, the console device does not support this command.
611 if (hba
->cardtype
== st_shasta
|| id
== host
->max_id
- 1) {
612 stex_invalid_field(cmd
, done
);
616 case TEST_UNIT_READY
:
617 if (id
== host
->max_id
- 1) {
618 cmd
->result
= DID_OK
<< 16 | COMMAND_COMPLETE
<< 8;
624 if (id
!= host
->max_id
- 1)
626 if (lun
== 0 && (cmd
->cmnd
[1] & INQUIRY_EVPD
) == 0) {
627 stex_direct_copy(cmd
, console_inq_page
,
628 sizeof(console_inq_page
));
629 cmd
->result
= DID_OK
<< 16 | COMMAND_COMPLETE
<< 8;
632 stex_invalid_field(cmd
, done
);
635 if (cmd
->cmnd
[1] == PASSTHRU_GET_DRVVER
) {
636 struct st_drvver ver
;
637 ver
.major
= ST_VER_MAJOR
;
638 ver
.minor
= ST_VER_MINOR
;
640 ver
.build
= ST_BUILD_VER
;
641 ver
.signature
[0] = PASSTHRU_SIGNATURE
;
642 ver
.console_id
= host
->max_id
- 1;
643 ver
.host_no
= hba
->host
->host_no
;
644 cmd
->result
= stex_direct_copy(cmd
, &ver
, sizeof(ver
)) ?
645 DID_OK
<< 16 | COMMAND_COMPLETE
<< 8 :
646 DID_ERROR
<< 16 | COMMAND_COMPLETE
<< 8;
654 cmd
->scsi_done
= done
;
656 tag
= cmd
->request
->tag
;
658 if (unlikely(tag
>= host
->can_queue
))
659 return SCSI_MLQUEUE_HOST_BUSY
;
661 req
= stex_alloc_req(hba
);
667 memcpy(req
->cdb
, cmd
->cmnd
, STEX_CDB_LENGTH
);
669 hba
->ccb
[tag
].cmd
= cmd
;
670 hba
->ccb
[tag
].sense_bufflen
= SCSI_SENSE_BUFFERSIZE
;
671 hba
->ccb
[tag
].sense_buffer
= cmd
->sense_buffer
;
672 hba
->ccb
[tag
].req_type
= 0;
674 if (cmd
->sc_data_direction
!= DMA_NONE
)
675 stex_map_sg(hba
, req
, &hba
->ccb
[tag
]);
677 stex_send_cmd(hba
, req
, tag
);
681 static void stex_unmap_sg(struct st_hba
*hba
, struct scsi_cmnd
*cmd
)
683 if (cmd
->sc_data_direction
!= DMA_NONE
) {
685 pci_unmap_sg(hba
->pdev
, cmd
->request_buffer
,
686 cmd
->use_sg
, cmd
->sc_data_direction
);
688 pci_unmap_single(hba
->pdev
, cmd
->SCp
.dma_handle
,
689 cmd
->request_bufflen
, cmd
->sc_data_direction
);
693 static void stex_scsi_done(struct st_ccb
*ccb
)
695 struct scsi_cmnd
*cmd
= ccb
->cmd
;
698 if (ccb
->srb_status
== SRB_STATUS_SUCCESS
|| ccb
->srb_status
== 0) {
699 result
= ccb
->scsi_status
;
700 switch (ccb
->scsi_status
) {
702 result
|= DID_OK
<< 16 | COMMAND_COMPLETE
<< 8;
704 case SAM_STAT_CHECK_CONDITION
:
705 result
|= DRIVER_SENSE
<< 24;
708 result
|= DID_BUS_BUSY
<< 16 | COMMAND_COMPLETE
<< 8;
711 result
|= DID_ERROR
<< 16 | COMMAND_COMPLETE
<< 8;
715 else if (ccb
->srb_status
& SRB_SEE_SENSE
)
716 result
= DRIVER_SENSE
<< 24 | SAM_STAT_CHECK_CONDITION
;
717 else switch (ccb
->srb_status
) {
718 case SRB_STATUS_SELECTION_TIMEOUT
:
719 result
= DID_NO_CONNECT
<< 16 | COMMAND_COMPLETE
<< 8;
721 case SRB_STATUS_BUSY
:
722 result
= DID_BUS_BUSY
<< 16 | COMMAND_COMPLETE
<< 8;
724 case SRB_STATUS_INVALID_REQUEST
:
725 case SRB_STATUS_ERROR
:
727 result
= DID_ERROR
<< 16 | COMMAND_COMPLETE
<< 8;
731 cmd
->result
= result
;
735 static void stex_copy_data(struct st_ccb
*ccb
,
736 struct status_msg
*resp
, unsigned int variable
)
738 size_t count
= variable
;
739 if (resp
->scsi_status
!= SAM_STAT_GOOD
) {
740 if (ccb
->sense_buffer
!= NULL
)
741 memcpy(ccb
->sense_buffer
, resp
->variable
,
742 min(variable
, ccb
->sense_bufflen
));
746 if (ccb
->cmd
== NULL
)
748 stex_internal_copy(ccb
->cmd
,
749 resp
->variable
, &count
, ccb
->sg_count
, ST_TO_CMD
);
752 static void stex_ys_commands(struct st_hba
*hba
,
753 struct st_ccb
*ccb
, struct status_msg
*resp
)
757 if (ccb
->cmd
->cmnd
[0] == MGT_CMD
&&
758 resp
->scsi_status
!= SAM_STAT_CHECK_CONDITION
) {
759 ccb
->cmd
->request_bufflen
=
760 le32_to_cpu(*(__le32
*)&resp
->variable
[0]);
764 if (resp
->srb_status
!= 0)
767 /* determine inquiry command status by DeviceTypeQualifier */
768 if (ccb
->cmd
->cmnd
[0] == INQUIRY
&&
769 resp
->scsi_status
== SAM_STAT_GOOD
) {
772 count
= STEX_EXTRA_SIZE
;
773 stex_internal_copy(ccb
->cmd
, hba
->copy_buffer
,
774 &count
, ccb
->sg_count
, ST_FROM_CMD
);
775 inq_data
= (ST_INQ
*)hba
->copy_buffer
;
776 if (inq_data
->DeviceTypeQualifier
!= 0)
777 ccb
->srb_status
= SRB_STATUS_SELECTION_TIMEOUT
;
779 ccb
->srb_status
= SRB_STATUS_SUCCESS
;
783 static void stex_mu_intr(struct st_hba
*hba
, u32 doorbell
)
785 void __iomem
*base
= hba
->mmio_base
;
786 struct status_msg
*resp
;
791 if (!(doorbell
& MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED
))
794 /* status payloads */
795 hba
->status_head
= readl(base
+ OMR1
);
796 if (unlikely(hba
->status_head
>= MU_STATUS_COUNT
)) {
797 printk(KERN_WARNING DRV_NAME
"(%s): invalid status head\n",
798 pci_name(hba
->pdev
));
803 * it's not a valid status payload if:
804 * 1. there are no pending requests(e.g. during init stage)
805 * 2. there are some pending requests, but the controller is in
806 * reset status, and its type is not st_yosemite
807 * firmware of st_yosemite in reset status will return pending requests
808 * to driver, so we allow it to pass
810 if (unlikely(hba
->out_req_cnt
<= 0 ||
811 (hba
->mu_status
== MU_STATE_RESETTING
&&
812 hba
->cardtype
!= st_yosemite
))) {
813 hba
->status_tail
= hba
->status_head
;
817 while (hba
->status_tail
!= hba
->status_head
) {
818 resp
= stex_get_status(hba
);
819 tag
= le16_to_cpu(resp
->tag
);
820 if (unlikely(tag
>= hba
->host
->can_queue
)) {
821 printk(KERN_WARNING DRV_NAME
822 "(%s): invalid tag\n", pci_name(hba
->pdev
));
826 ccb
= &hba
->ccb
[tag
];
827 if (hba
->wait_ccb
== ccb
)
828 hba
->wait_ccb
= NULL
;
829 if (unlikely(ccb
->req
== NULL
)) {
830 printk(KERN_WARNING DRV_NAME
831 "(%s): lagging req\n", pci_name(hba
->pdev
));
836 size
= resp
->payload_sz
* sizeof(u32
); /* payload size */
837 if (unlikely(size
< sizeof(*resp
) - STATUS_VAR_LEN
||
838 size
> sizeof(*resp
))) {
839 printk(KERN_WARNING DRV_NAME
"(%s): bad status size\n",
840 pci_name(hba
->pdev
));
842 size
-= sizeof(*resp
) - STATUS_VAR_LEN
; /* copy size */
844 stex_copy_data(ccb
, resp
, size
);
847 ccb
->srb_status
= resp
->srb_status
;
848 ccb
->scsi_status
= resp
->scsi_status
;
850 if (likely(ccb
->cmd
!= NULL
)) {
851 if (hba
->cardtype
== st_yosemite
)
852 stex_ys_commands(hba
, ccb
, resp
);
854 if (unlikely(ccb
->cmd
->cmnd
[0] == PASSTHRU_CMD
&&
855 ccb
->cmd
->cmnd
[1] == PASSTHRU_GET_ADAPTER
))
856 stex_controller_info(hba
, ccb
);
858 stex_unmap_sg(hba
, ccb
->cmd
);
861 } else if (ccb
->req_type
& PASSTHRU_REQ_TYPE
) {
863 if (ccb
->req_type
& PASSTHRU_REQ_NO_WAKEUP
) {
868 if (waitqueue_active(&hba
->waitq
))
869 wake_up(&hba
->waitq
);
874 writel(hba
->status_head
, base
+ IMR1
);
875 readl(base
+ IMR1
); /* flush */
878 static irqreturn_t
stex_intr(int irq
, void *__hba
)
880 struct st_hba
*hba
= __hba
;
881 void __iomem
*base
= hba
->mmio_base
;
886 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
888 data
= readl(base
+ ODBL
);
890 if (data
&& data
!= 0xffffffff) {
891 /* clear the interrupt */
892 writel(data
, base
+ ODBL
);
893 readl(base
+ ODBL
); /* flush */
894 stex_mu_intr(hba
, data
);
898 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
900 return IRQ_RETVAL(handled
);
903 static int stex_handshake(struct st_hba
*hba
)
905 void __iomem
*base
= hba
->mmio_base
;
906 struct handshake_frame
*h
;
907 dma_addr_t status_phys
;
909 unsigned long before
;
911 if (readl(base
+ OMR0
) != MU_HANDSHAKE_SIGNATURE
) {
912 writel(MU_INBOUND_DOORBELL_HANDSHAKE
, base
+ IDBL
);
915 while (readl(base
+ OMR0
) != MU_HANDSHAKE_SIGNATURE
) {
916 if (time_after(jiffies
, before
+ MU_MAX_DELAY
* HZ
)) {
917 printk(KERN_ERR DRV_NAME
918 "(%s): no handshake signature\n",
919 pci_name(hba
->pdev
));
929 data
= readl(base
+ OMR1
);
930 if ((data
& 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF
) {
932 if (hba
->host
->can_queue
> data
)
933 hba
->host
->can_queue
= data
;
936 h
= (struct handshake_frame
*)(hba
->dma_mem
+ MU_REQ_BUFFER_SIZE
);
937 h
->rb_phy
= cpu_to_le32(hba
->dma_handle
);
938 h
->rb_phy_hi
= cpu_to_le32((hba
->dma_handle
>> 16) >> 16);
939 h
->req_sz
= cpu_to_le16(sizeof(struct req_msg
));
940 h
->req_cnt
= cpu_to_le16(MU_REQ_COUNT
);
941 h
->status_sz
= cpu_to_le16(sizeof(struct status_msg
));
942 h
->status_cnt
= cpu_to_le16(MU_STATUS_COUNT
);
943 stex_gettime(&h
->hosttime
);
944 h
->partner_type
= HMU_PARTNER_TYPE
;
945 if (hba
->dma_size
> STEX_BUFFER_SIZE
) {
946 h
->extra_offset
= cpu_to_le32(STEX_BUFFER_SIZE
);
947 h
->extra_size
= cpu_to_le32(ST_ADDITIONAL_MEM
);
949 h
->extra_offset
= h
->extra_size
= 0;
951 status_phys
= hba
->dma_handle
+ MU_REQ_BUFFER_SIZE
;
952 writel(status_phys
, base
+ IMR0
);
954 writel((status_phys
>> 16) >> 16, base
+ IMR1
);
957 writel((status_phys
>> 16) >> 16, base
+ OMR0
); /* old fw compatible */
959 writel(MU_INBOUND_DOORBELL_HANDSHAKE
, base
+ IDBL
);
960 readl(base
+ IDBL
); /* flush */
964 while (readl(base
+ OMR0
) != MU_HANDSHAKE_SIGNATURE
) {
965 if (time_after(jiffies
, before
+ MU_MAX_DELAY
* HZ
)) {
966 printk(KERN_ERR DRV_NAME
967 "(%s): no signature after handshake frame\n",
968 pci_name(hba
->pdev
));
975 writel(0, base
+ IMR0
);
977 writel(0, base
+ OMR0
);
979 writel(0, base
+ IMR1
);
981 writel(0, base
+ OMR1
);
982 readl(base
+ OMR1
); /* flush */
983 hba
->mu_status
= MU_STATE_STARTED
;
987 static int stex_abort(struct scsi_cmnd
*cmd
)
989 struct Scsi_Host
*host
= cmd
->device
->host
;
990 struct st_hba
*hba
= (struct st_hba
*)host
->hostdata
;
991 u16 tag
= cmd
->request
->tag
;
994 int result
= SUCCESS
;
997 printk(KERN_INFO DRV_NAME
998 "(%s): aborting command\n", pci_name(hba
->pdev
));
999 scsi_print_command(cmd
);
1001 base
= hba
->mmio_base
;
1002 spin_lock_irqsave(host
->host_lock
, flags
);
1003 if (tag
< host
->can_queue
&& hba
->ccb
[tag
].cmd
== cmd
)
1004 hba
->wait_ccb
= &hba
->ccb
[tag
];
1006 for (tag
= 0; tag
< host
->can_queue
; tag
++)
1007 if (hba
->ccb
[tag
].cmd
== cmd
) {
1008 hba
->wait_ccb
= &hba
->ccb
[tag
];
1011 if (tag
>= host
->can_queue
)
1015 data
= readl(base
+ ODBL
);
1016 if (data
== 0 || data
== 0xffffffff)
1019 writel(data
, base
+ ODBL
);
1020 readl(base
+ ODBL
); /* flush */
1022 stex_mu_intr(hba
, data
);
1024 if (hba
->wait_ccb
== NULL
) {
1025 printk(KERN_WARNING DRV_NAME
1026 "(%s): lost interrupt\n", pci_name(hba
->pdev
));
1031 stex_unmap_sg(hba
, cmd
);
1032 hba
->wait_ccb
->req
= NULL
; /* nullify the req's future return */
1033 hba
->wait_ccb
= NULL
;
1036 spin_unlock_irqrestore(host
->host_lock
, flags
);
1040 static void stex_hard_reset(struct st_hba
*hba
)
1042 struct pci_bus
*bus
;
1047 for (i
= 0; i
< 16; i
++)
1048 pci_read_config_dword(hba
->pdev
, i
* 4,
1049 &hba
->pdev
->saved_config_space
[i
]);
1051 /* Reset secondary bus. Our controller(MU/ATU) is the only device on
1052 secondary bus. Consult Intel 80331/3 developer's manual for detail */
1053 bus
= hba
->pdev
->bus
;
1054 pci_read_config_byte(bus
->self
, PCI_BRIDGE_CONTROL
, &pci_bctl
);
1055 pci_bctl
|= PCI_BRIDGE_CTL_BUS_RESET
;
1056 pci_write_config_byte(bus
->self
, PCI_BRIDGE_CONTROL
, pci_bctl
);
1059 * 1 ms may be enough for 8-port controllers. But 16-port controllers
1060 * require more time to finish bus reset. Use 100 ms here for safety
1063 pci_bctl
&= ~PCI_BRIDGE_CTL_BUS_RESET
;
1064 pci_write_config_byte(bus
->self
, PCI_BRIDGE_CONTROL
, pci_bctl
);
1066 for (i
= 0; i
< MU_HARD_RESET_WAIT
; i
++) {
1067 pci_read_config_word(hba
->pdev
, PCI_COMMAND
, &pci_cmd
);
1068 if (pci_cmd
!= 0xffff && (pci_cmd
& PCI_COMMAND_MASTER
))
1074 for (i
= 0; i
< 16; i
++)
1075 pci_write_config_dword(hba
->pdev
, i
* 4,
1076 hba
->pdev
->saved_config_space
[i
]);
1079 static int stex_reset(struct scsi_cmnd
*cmd
)
1082 unsigned long flags
;
1083 unsigned long before
;
1084 hba
= (struct st_hba
*) &cmd
->device
->host
->hostdata
[0];
1086 printk(KERN_INFO DRV_NAME
1087 "(%s): resetting host\n", pci_name(hba
->pdev
));
1088 scsi_print_command(cmd
);
1090 hba
->mu_status
= MU_STATE_RESETTING
;
1092 if (hba
->cardtype
== st_shasta
)
1093 stex_hard_reset(hba
);
1095 if (hba
->cardtype
!= st_yosemite
) {
1096 if (stex_handshake(hba
)) {
1097 printk(KERN_WARNING DRV_NAME
1098 "(%s): resetting: handshake failed\n",
1099 pci_name(hba
->pdev
));
1102 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1105 hba
->status_head
= 0;
1106 hba
->status_tail
= 0;
1107 hba
->out_req_cnt
= 0;
1108 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1113 writel(MU_INBOUND_DOORBELL_RESET
, hba
->mmio_base
+ IDBL
);
1114 readl(hba
->mmio_base
+ IDBL
); /* flush */
1116 while (hba
->out_req_cnt
> 0) {
1117 if (time_after(jiffies
, before
+ ST_INTERNAL_TIMEOUT
* HZ
)) {
1118 printk(KERN_WARNING DRV_NAME
1119 "(%s): reset timeout\n", pci_name(hba
->pdev
));
1125 hba
->mu_status
= MU_STATE_STARTED
;
1129 static int stex_biosparam(struct scsi_device
*sdev
,
1130 struct block_device
*bdev
, sector_t capacity
, int geom
[])
1132 int heads
= 255, sectors
= 63;
1134 if (capacity
< 0x200000) {
1139 sector_div(capacity
, heads
* sectors
);
1148 static struct scsi_host_template driver_template
= {
1149 .module
= THIS_MODULE
,
1151 .proc_name
= DRV_NAME
,
1152 .bios_param
= stex_biosparam
,
1153 .queuecommand
= stex_queuecommand
,
1154 .slave_alloc
= stex_slave_alloc
,
1155 .slave_configure
= stex_slave_config
,
1156 .slave_destroy
= stex_slave_destroy
,
1157 .eh_abort_handler
= stex_abort
,
1158 .eh_host_reset_handler
= stex_reset
,
1159 .can_queue
= ST_CAN_QUEUE
,
1161 .sg_tablesize
= ST_MAX_SG
,
1162 .cmd_per_lun
= ST_CMD_PER_LUN
,
1165 static int stex_set_dma_mask(struct pci_dev
* pdev
)
1168 if (!pci_set_dma_mask(pdev
, DMA_64BIT_MASK
)
1169 && !pci_set_consistent_dma_mask(pdev
, DMA_64BIT_MASK
))
1171 ret
= pci_set_dma_mask(pdev
, DMA_32BIT_MASK
);
1173 ret
= pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
);
1177 static int __devinit
1178 stex_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1181 struct Scsi_Host
*host
;
1184 err
= pci_enable_device(pdev
);
1188 pci_set_master(pdev
);
1190 host
= scsi_host_alloc(&driver_template
, sizeof(struct st_hba
));
1193 printk(KERN_ERR DRV_NAME
"(%s): scsi_host_alloc failed\n",
1199 hba
= (struct st_hba
*)host
->hostdata
;
1200 memset(hba
, 0, sizeof(struct st_hba
));
1202 err
= pci_request_regions(pdev
, DRV_NAME
);
1204 printk(KERN_ERR DRV_NAME
"(%s): request regions failed\n",
1206 goto out_scsi_host_put
;
1209 hba
->mmio_base
= ioremap_nocache(pci_resource_start(pdev
, 0),
1210 pci_resource_len(pdev
, 0));
1211 if ( !hba
->mmio_base
) {
1212 printk(KERN_ERR DRV_NAME
"(%s): memory map failed\n",
1215 goto out_release_regions
;
1218 err
= stex_set_dma_mask(pdev
);
1220 printk(KERN_ERR DRV_NAME
"(%s): set dma mask failed\n",
1225 hba
->cardtype
= (unsigned int) id
->driver_data
;
1226 if (hba
->cardtype
== st_vsc
&& (pdev
->subsystem_device
& 0xf) == 0x1)
1227 hba
->cardtype
= st_vsc1
;
1228 hba
->dma_size
= (hba
->cardtype
== st_vsc1
) ?
1229 (STEX_BUFFER_SIZE
+ ST_ADDITIONAL_MEM
) : (STEX_BUFFER_SIZE
);
1230 hba
->dma_mem
= dma_alloc_coherent(&pdev
->dev
,
1231 hba
->dma_size
, &hba
->dma_handle
, GFP_KERNEL
);
1232 if (!hba
->dma_mem
) {
1234 printk(KERN_ERR DRV_NAME
"(%s): dma mem alloc failed\n",
1239 hba
->status_buffer
=
1240 (struct status_msg
*)(hba
->dma_mem
+ MU_REQ_BUFFER_SIZE
);
1241 hba
->copy_buffer
= hba
->dma_mem
+ MU_BUFFER_SIZE
;
1242 hba
->mu_status
= MU_STATE_STARTING
;
1244 if (hba
->cardtype
== st_shasta
) {
1246 host
->max_id
= 16 + 1;
1247 } else if (hba
->cardtype
== st_yosemite
) {
1248 host
->max_lun
= 128;
1249 host
->max_id
= 1 + 1;
1251 /* st_vsc and st_vsc1 */
1253 host
->max_id
= 128 + 1;
1255 host
->max_channel
= 0;
1256 host
->unique_id
= host
->host_no
;
1257 host
->max_cmd_len
= STEX_CDB_LENGTH
;
1261 init_waitqueue_head(&hba
->waitq
);
1263 err
= request_irq(pdev
->irq
, stex_intr
, IRQF_SHARED
, DRV_NAME
, hba
);
1265 printk(KERN_ERR DRV_NAME
"(%s): request irq failed\n",
1270 err
= stex_handshake(hba
);
1274 err
= scsi_init_shared_tag_map(host
, host
->can_queue
);
1276 printk(KERN_ERR DRV_NAME
"(%s): init shared queue failed\n",
1281 pci_set_drvdata(pdev
, hba
);
1283 err
= scsi_add_host(host
, &pdev
->dev
);
1285 printk(KERN_ERR DRV_NAME
"(%s): scsi_add_host failed\n",
1290 scsi_scan_host(host
);
1295 free_irq(pdev
->irq
, hba
);
1297 dma_free_coherent(&pdev
->dev
, hba
->dma_size
,
1298 hba
->dma_mem
, hba
->dma_handle
);
1300 iounmap(hba
->mmio_base
);
1301 out_release_regions
:
1302 pci_release_regions(pdev
);
1304 scsi_host_put(host
);
1306 pci_disable_device(pdev
);
1311 static void stex_hba_stop(struct st_hba
*hba
)
1313 struct req_msg
*req
;
1314 unsigned long flags
;
1315 unsigned long before
;
1318 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1319 req
= stex_alloc_req(hba
);
1320 memset(req
->cdb
, 0, STEX_CDB_LENGTH
);
1322 if (hba
->cardtype
== st_yosemite
) {
1323 req
->cdb
[0] = MGT_CMD
;
1324 req
->cdb
[1] = MGT_CMD_SIGNATURE
;
1325 req
->cdb
[2] = CTLR_CONFIG_CMD
;
1326 req
->cdb
[3] = CTLR_SHUTDOWN
;
1328 req
->cdb
[0] = CONTROLLER_CMD
;
1329 req
->cdb
[1] = CTLR_POWER_STATE_CHANGE
;
1330 req
->cdb
[2] = CTLR_POWER_SAVING
;
1333 hba
->ccb
[tag
].cmd
= NULL
;
1334 hba
->ccb
[tag
].sg_count
= 0;
1335 hba
->ccb
[tag
].sense_bufflen
= 0;
1336 hba
->ccb
[tag
].sense_buffer
= NULL
;
1337 hba
->ccb
[tag
].req_type
|= PASSTHRU_REQ_TYPE
;
1339 stex_send_cmd(hba
, req
, tag
);
1340 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1343 while (hba
->ccb
[tag
].req_type
& PASSTHRU_REQ_TYPE
) {
1344 if (time_after(jiffies
, before
+ ST_INTERNAL_TIMEOUT
* HZ
))
1350 static void stex_hba_free(struct st_hba
*hba
)
1352 free_irq(hba
->pdev
->irq
, hba
);
1354 iounmap(hba
->mmio_base
);
1356 pci_release_regions(hba
->pdev
);
1358 dma_free_coherent(&hba
->pdev
->dev
, hba
->dma_size
,
1359 hba
->dma_mem
, hba
->dma_handle
);
1362 static void stex_remove(struct pci_dev
*pdev
)
1364 struct st_hba
*hba
= pci_get_drvdata(pdev
);
1366 scsi_remove_host(hba
->host
);
1368 pci_set_drvdata(pdev
, NULL
);
1374 scsi_host_put(hba
->host
);
1376 pci_disable_device(pdev
);
1379 static void stex_shutdown(struct pci_dev
*pdev
)
1381 struct st_hba
*hba
= pci_get_drvdata(pdev
);
1386 static struct pci_device_id stex_pci_tbl
[] = {
1388 { 0x105a, 0x8350, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0,
1389 st_shasta
}, /* SuperTrak EX8350/8300/16350/16300 */
1390 { 0x105a, 0xc350, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0,
1391 st_shasta
}, /* SuperTrak EX12350 */
1392 { 0x105a, 0x4302, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0,
1393 st_shasta
}, /* SuperTrak EX4350 */
1394 { 0x105a, 0xe350, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0,
1395 st_shasta
}, /* SuperTrak EX24350 */
1398 { 0x105a, 0x7250, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, st_vsc
},
1401 { 0x105a, 0x8650, PCI_ANY_ID
, 0x4600, 0, 0,
1402 st_yosemite
}, /* SuperTrak EX4650 */
1403 { 0x105a, 0x8650, PCI_ANY_ID
, 0x4610, 0, 0,
1404 st_yosemite
}, /* SuperTrak EX4650o */
1405 { 0x105a, 0x8650, PCI_ANY_ID
, 0x8600, 0, 0,
1406 st_yosemite
}, /* SuperTrak EX8650EL */
1407 { 0x105a, 0x8650, PCI_ANY_ID
, 0x8601, 0, 0,
1408 st_yosemite
}, /* SuperTrak EX8650 */
1409 { 0x105a, 0x8650, PCI_ANY_ID
, 0x8602, 0, 0,
1410 st_yosemite
}, /* SuperTrak EX8654 */
1411 { 0x105a, 0x8650, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0,
1412 st_yosemite
}, /* generic st_yosemite */
1413 { } /* terminate list */
1415 MODULE_DEVICE_TABLE(pci
, stex_pci_tbl
);
1417 static struct pci_driver stex_pci_driver
= {
1419 .id_table
= stex_pci_tbl
,
1420 .probe
= stex_probe
,
1421 .remove
= __devexit_p(stex_remove
),
1422 .shutdown
= stex_shutdown
,
1425 static int __init
stex_init(void)
1427 printk(KERN_INFO DRV_NAME
1428 ": Promise SuperTrak EX Driver version: %s\n",
1431 return pci_register_driver(&stex_pci_driver
);
1434 static void __exit
stex_exit(void)
1436 pci_unregister_driver(&stex_pci_driver
);
1439 module_init(stex_init
);
1440 module_exit(stex_exit
);