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>
36 #include <scsi/scsi_eh.h>
38 #define DRV_NAME "stex"
39 #define ST_DRIVER_VERSION "3.6.0000.1"
40 #define ST_VER_MAJOR 3
41 #define ST_VER_MINOR 6
43 #define ST_BUILD_VER 1
46 /* MU register offset */
47 IMR0 = 0x10, /* MU_INBOUND_MESSAGE_REG0 */
48 IMR1 = 0x14, /* MU_INBOUND_MESSAGE_REG1 */
49 OMR0 = 0x18, /* MU_OUTBOUND_MESSAGE_REG0 */
50 OMR1 = 0x1c, /* MU_OUTBOUND_MESSAGE_REG1 */
51 IDBL = 0x20, /* MU_INBOUND_DOORBELL */
52 IIS = 0x24, /* MU_INBOUND_INTERRUPT_STATUS */
53 IIM = 0x28, /* MU_INBOUND_INTERRUPT_MASK */
54 ODBL = 0x2c, /* MU_OUTBOUND_DOORBELL */
55 OIS = 0x30, /* MU_OUTBOUND_INTERRUPT_STATUS */
56 OIM = 0x3c, /* MU_OUTBOUND_INTERRUPT_MASK */
58 /* MU register value */
59 MU_INBOUND_DOORBELL_HANDSHAKE = 1,
60 MU_INBOUND_DOORBELL_REQHEADCHANGED = 2,
61 MU_INBOUND_DOORBELL_STATUSTAILCHANGED = 4,
62 MU_INBOUND_DOORBELL_HMUSTOPPED = 8,
63 MU_INBOUND_DOORBELL_RESET = 16,
65 MU_OUTBOUND_DOORBELL_HANDSHAKE = 1,
66 MU_OUTBOUND_DOORBELL_REQUESTTAILCHANGED = 2,
67 MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED = 4,
68 MU_OUTBOUND_DOORBELL_BUSCHANGE = 8,
69 MU_OUTBOUND_DOORBELL_HASEVENT = 16,
72 MU_STATE_STARTING = 1,
73 MU_STATE_FMU_READY_FOR_HANDSHAKE = 2,
74 MU_STATE_SEND_HANDSHAKE_FRAME = 3,
76 MU_STATE_RESETTING = 5,
79 MU_HANDSHAKE_SIGNATURE = 0x55aaaa55,
80 MU_HANDSHAKE_SIGNATURE_HALF = 0x5a5a0000,
81 MU_HARD_RESET_WAIT = 30000,
84 /* firmware returned values */
85 SRB_STATUS_SUCCESS = 0x01,
86 SRB_STATUS_ERROR = 0x04,
87 SRB_STATUS_BUSY = 0x05,
88 SRB_STATUS_INVALID_REQUEST = 0x06,
89 SRB_STATUS_SELECTION_TIMEOUT = 0x0A,
93 TASK_ATTRIBUTE_SIMPLE = 0x0,
94 TASK_ATTRIBUTE_HEADOFQUEUE = 0x1,
95 TASK_ATTRIBUTE_ORDERED = 0x2,
96 TASK_ATTRIBUTE_ACA = 0x4,
98 /* request count, etc. */
101 /* one message wasted, use MU_MAX_REQUEST+1
102 to handle MU_MAX_REQUEST messages */
103 MU_REQ_COUNT = (MU_MAX_REQUEST + 1),
104 MU_STATUS_COUNT = (MU_MAX_REQUEST + 1),
106 STEX_CDB_LENGTH = MAX_COMMAND_SIZE,
107 REQ_VARIABLE_LEN = 1024,
108 STATUS_VAR_LEN = 128,
109 ST_CAN_QUEUE = MU_MAX_REQUEST,
110 ST_CMD_PER_LUN = MU_MAX_REQUEST,
114 SG_CF_EOT = 0x80, /* end of table */
115 SG_CF_64B = 0x40, /* 64 bit item */
116 SG_CF_HOST = 0x20, /* sg in host memory */
124 PASSTHRU_REQ_TYPE = 0x00000001,
125 PASSTHRU_REQ_NO_WAKEUP = 0x00000100,
126 ST_INTERNAL_TIMEOUT = 30,
131 /* vendor specific commands of Promise */
133 SINBAND_MGT_CMD = 0xd9,
135 CONTROLLER_CMD = 0xe1,
136 DEBUGGING_CMD = 0xe2,
139 PASSTHRU_GET_ADAPTER = 0x05,
140 PASSTHRU_GET_DRVVER = 0x10,
142 CTLR_CONFIG_CMD = 0x03,
143 CTLR_SHUTDOWN = 0x0d,
145 CTLR_POWER_STATE_CHANGE = 0x0e,
146 CTLR_POWER_SAVING = 0x01,
148 PASSTHRU_SIGNATURE = 0x4e415041,
149 MGT_CMD_SIGNATURE = 0xba,
153 ST_ADDITIONAL_MEM = 0x200000,
156 /* SCSI inquiry data */
157 typedef struct st_inq {
159 u8 DeviceTypeQualifier :3;
160 u8 DeviceTypeModifier :7;
161 u8 RemovableMedia :1;
163 u8 ResponseDataFormat :4;
173 u8 LinkedCommands :1;
177 u8 RelativeAddressing :1;
180 u8 ProductRevisionLevel[4];
181 u8 VendorSpecific[20];
186 u8 ctrl; /* SG_CF_xxx */
197 struct st_sgitem table[ST_MAX_SG];
200 struct handshake_frame {
201 __le32 rb_phy; /* request payload queue physical address */
203 __le16 req_sz; /* size of each request payload */
204 __le16 req_cnt; /* count of reqs the buffer can hold */
205 __le16 status_sz; /* size of each status payload */
206 __le16 status_cnt; /* count of status the buffer can hold */
207 __le32 hosttime; /* seconds from Jan 1, 1970 (GMT) */
209 u8 partner_type; /* who sends this frame */
211 __le32 partner_ver_major;
212 __le32 partner_ver_minor;
213 __le32 partner_ver_oem;
214 __le32 partner_ver_build;
215 __le32 extra_offset; /* NEW */
216 __le32 extra_size; /* NEW */
227 u8 payload_sz; /* payload size in 4-byte, not used */
228 u8 cdb[STEX_CDB_LENGTH];
229 u8 variable[REQ_VARIABLE_LEN];
239 u8 payload_sz; /* payload size in 4-byte */
240 u8 variable[STATUS_VAR_LEN];
255 struct ver_info drv_ver;
256 struct ver_info bios_ver;
285 #define MU_REQ_BUFFER_SIZE (MU_REQ_COUNT * sizeof(struct req_msg))
286 #define MU_STATUS_BUFFER_SIZE (MU_STATUS_COUNT * sizeof(struct status_msg))
287 #define MU_BUFFER_SIZE (MU_REQ_BUFFER_SIZE + MU_STATUS_BUFFER_SIZE)
288 #define STEX_EXTRA_SIZE max(sizeof(struct st_frame), sizeof(ST_INQ))
289 #define STEX_BUFFER_SIZE (MU_BUFFER_SIZE + STEX_EXTRA_SIZE)
293 struct scsi_cmnd *cmd;
296 unsigned int sense_bufflen;
305 void __iomem *mmio_base; /* iomapped PCI memory space */
307 dma_addr_t dma_handle;
310 struct Scsi_Host *host;
311 struct pci_dev *pdev;
318 struct status_msg *status_buffer;
319 void *copy_buffer; /* temp buffer for driver-handled commands */
320 struct st_ccb ccb[MU_MAX_REQUEST];
321 struct st_ccb *wait_ccb;
322 wait_queue_head_t waitq;
324 unsigned int mu_status;
327 unsigned int cardtype;
330 static const char console_inq_page[] =
332 0x03,0x00,0x03,0x03,0xFA,0x00,0x00,0x30,
333 0x50,0x72,0x6F,0x6D,0x69,0x73,0x65,0x20, /* "Promise " */
334 0x52,0x41,0x49,0x44,0x20,0x43,0x6F,0x6E, /* "RAID Con" */
335 0x73,0x6F,0x6C,0x65,0x20,0x20,0x20,0x20, /* "sole " */
336 0x31,0x2E,0x30,0x30,0x20,0x20,0x20,0x20, /* "1.00 " */
337 0x53,0x58,0x2F,0x52,0x53,0x41,0x46,0x2D, /* "SX/RSAF-" */
338 0x54,0x45,0x31,0x2E,0x30,0x30,0x20,0x20, /* "TE1.00 " */
339 0x0C,0x20,0x20,0x20,0x20,0x20,0x20,0x20
342 MODULE_AUTHOR("Ed Lin");
343 MODULE_DESCRIPTION("Promise Technology SuperTrak EX Controllers");
344 MODULE_LICENSE("GPL");
345 MODULE_VERSION(ST_DRIVER_VERSION);
347 static void stex_gettime(__le32 *time)
350 do_gettimeofday(&tv);
352 *time = cpu_to_le32(tv.tv_sec & 0xffffffff);
353 *(time + 1) = cpu_to_le32((tv.tv_sec >> 16) >> 16);
356 static struct status_msg *stex_get_status(struct st_hba *hba)
358 struct status_msg *status =
359 hba->status_buffer + hba->status_tail;
362 hba->status_tail %= MU_STATUS_COUNT;
367 static void stex_invalid_field(struct scsi_cmnd *cmd,
368 void (*done)(struct scsi_cmnd *))
370 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
372 /* "Invalid field in cbd" */
373 scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x24,
378 static struct req_msg *stex_alloc_req(struct st_hba *hba)
380 struct req_msg *req = ((struct req_msg *)hba->dma_mem) +
384 hba->req_head %= MU_REQ_COUNT;
389 static int stex_map_sg(struct st_hba *hba,
390 struct req_msg *req, struct st_ccb *ccb)
392 struct scsi_cmnd *cmd;
393 struct scatterlist *sg;
394 struct st_sgtable *dst;
398 dst = (struct st_sgtable *)req->variable;
399 dst->max_sg_count = cpu_to_le16(ST_MAX_SG);
400 dst->sz_in_byte = cpu_to_le32(scsi_bufflen(cmd));
402 nseg = scsi_dma_map(cmd);
406 ccb->sg_count = nseg;
407 dst->sg_count = cpu_to_le16((u16)nseg);
409 scsi_for_each_sg(cmd, sg, nseg, i) {
410 dst->table[i].count = cpu_to_le32((u32)sg_dma_len(sg));
412 cpu_to_le32(sg_dma_address(sg) & 0xffffffff);
413 dst->table[i].addr_hi =
414 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
415 dst->table[i].ctrl = SG_CF_64B | SG_CF_HOST;
417 dst->table[--i].ctrl |= SG_CF_EOT;
423 static void stex_controller_info(struct st_hba *hba, struct st_ccb *ccb)
426 size_t count = sizeof(struct st_frame);
428 p = hba->copy_buffer;
429 count = scsi_sg_copy_to_buffer(ccb->cmd, p, count);
430 memset(p->base, 0, sizeof(u32)*6);
431 *(unsigned long *)(p->base) = pci_resource_start(hba->pdev, 0);
434 p->drv_ver.major = ST_VER_MAJOR;
435 p->drv_ver.minor = ST_VER_MINOR;
436 p->drv_ver.oem = ST_OEM;
437 p->drv_ver.build = ST_BUILD_VER;
439 p->bus = hba->pdev->bus->number;
440 p->slot = hba->pdev->devfn;
442 p->irq_vec = hba->pdev->irq;
443 p->id = hba->pdev->vendor << 16 | hba->pdev->device;
445 hba->pdev->subsystem_vendor << 16 | hba->pdev->subsystem_device;
447 count = scsi_sg_copy_from_buffer(ccb->cmd, p, count);
451 stex_send_cmd(struct st_hba *hba, struct req_msg *req, u16 tag)
453 req->tag = cpu_to_le16(tag);
454 req->task_attr = TASK_ATTRIBUTE_SIMPLE;
455 req->task_manage = 0; /* not supported yet */
457 hba->ccb[tag].req = req;
460 writel(hba->req_head, hba->mmio_base + IMR0);
461 writel(MU_INBOUND_DOORBELL_REQHEADCHANGED, hba->mmio_base + IDBL);
462 readl(hba->mmio_base + IDBL); /* flush */
466 stex_slave_alloc(struct scsi_device *sdev)
468 /* Cheat: usually extracted from Inquiry data */
469 sdev->tagged_supported = 1;
471 scsi_activate_tcq(sdev, ST_CMD_PER_LUN);
477 stex_slave_config(struct scsi_device *sdev)
479 sdev->use_10_for_rw = 1;
480 sdev->use_10_for_ms = 1;
481 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
482 sdev->tagged_supported = 1;
488 stex_slave_destroy(struct scsi_device *sdev)
490 scsi_deactivate_tcq(sdev, 1);
494 stex_queuecommand(struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
497 struct Scsi_Host *host;
501 host = cmd->device->host;
502 id = cmd->device->id;
503 lun = cmd->device->lun;
504 hba = (struct st_hba *) &host->hostdata[0];
506 switch (cmd->cmnd[0]) {
509 static char ms10_caching_page[12] =
510 { 0, 0x12, 0, 0, 0, 0, 0, 0, 0x8, 0xa, 0x4, 0 };
512 page = cmd->cmnd[2] & 0x3f;
513 if (page == 0x8 || page == 0x3f) {
514 scsi_sg_copy_from_buffer(cmd, ms10_caching_page,
515 sizeof(ms10_caching_page));
516 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
519 stex_invalid_field(cmd, done);
524 * The shasta firmware does not report actual luns in the
525 * target, so fail the command to force sequential lun scan.
526 * Also, the console device does not support this command.
528 if (hba->cardtype == st_shasta || id == host->max_id - 1) {
529 stex_invalid_field(cmd, done);
533 case TEST_UNIT_READY:
534 if (id == host->max_id - 1) {
535 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
541 if (id != host->max_id - 1)
543 if (lun == 0 && (cmd->cmnd[1] & INQUIRY_EVPD) == 0) {
544 scsi_sg_copy_from_buffer(cmd, (void *)console_inq_page,
545 sizeof(console_inq_page));
546 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
549 stex_invalid_field(cmd, done);
552 if (cmd->cmnd[1] == PASSTHRU_GET_DRVVER) {
553 struct st_drvver ver;
554 size_t cp_len = sizeof(ver);
555 ver.major = ST_VER_MAJOR;
556 ver.minor = ST_VER_MINOR;
558 ver.build = ST_BUILD_VER;
559 ver.signature[0] = PASSTHRU_SIGNATURE;
560 ver.console_id = host->max_id - 1;
561 ver.host_no = hba->host->host_no;
562 cp_len = scsi_sg_copy_from_buffer(cmd, &ver, cp_len);
563 cmd->result = sizeof(ver) == cp_len ?
564 DID_OK << 16 | COMMAND_COMPLETE << 8 :
565 DID_ERROR << 16 | COMMAND_COMPLETE << 8;
573 cmd->scsi_done = done;
575 tag = cmd->request->tag;
577 if (unlikely(tag >= host->can_queue))
578 return SCSI_MLQUEUE_HOST_BUSY;
580 req = stex_alloc_req(hba);
586 memcpy(req->cdb, cmd->cmnd, STEX_CDB_LENGTH);
588 hba->ccb[tag].cmd = cmd;
589 hba->ccb[tag].sense_bufflen = SCSI_SENSE_BUFFERSIZE;
590 hba->ccb[tag].sense_buffer = cmd->sense_buffer;
591 hba->ccb[tag].req_type = 0;
593 if (cmd->sc_data_direction != DMA_NONE)
594 stex_map_sg(hba, req, &hba->ccb[tag]);
596 stex_send_cmd(hba, req, tag);
600 static void stex_scsi_done(struct st_ccb *ccb)
602 struct scsi_cmnd *cmd = ccb->cmd;
605 if (ccb->srb_status == SRB_STATUS_SUCCESS || ccb->srb_status == 0) {
606 result = ccb->scsi_status;
607 switch (ccb->scsi_status) {
609 result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
611 case SAM_STAT_CHECK_CONDITION:
612 result |= DRIVER_SENSE << 24;
615 result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
618 result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
622 else if (ccb->srb_status & SRB_SEE_SENSE)
623 result = DRIVER_SENSE << 24 | SAM_STAT_CHECK_CONDITION;
624 else switch (ccb->srb_status) {
625 case SRB_STATUS_SELECTION_TIMEOUT:
626 result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
628 case SRB_STATUS_BUSY:
629 result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
631 case SRB_STATUS_INVALID_REQUEST:
632 case SRB_STATUS_ERROR:
634 result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
638 cmd->result = result;
642 static void stex_copy_data(struct st_ccb *ccb,
643 struct status_msg *resp, unsigned int variable)
645 size_t count = variable;
646 if (resp->scsi_status != SAM_STAT_GOOD) {
647 if (ccb->sense_buffer != NULL)
648 memcpy(ccb->sense_buffer, resp->variable,
649 min(variable, ccb->sense_bufflen));
653 if (ccb->cmd == NULL)
655 count = scsi_sg_copy_from_buffer(ccb->cmd, resp->variable, count);
658 static void stex_ys_commands(struct st_hba *hba,
659 struct st_ccb *ccb, struct status_msg *resp)
661 if (ccb->cmd->cmnd[0] == MGT_CMD &&
662 resp->scsi_status != SAM_STAT_CHECK_CONDITION) {
663 scsi_set_resid(ccb->cmd, scsi_bufflen(ccb->cmd) -
664 le32_to_cpu(*(__le32 *)&resp->variable[0]));
668 if (resp->srb_status != 0)
671 /* determine inquiry command status by DeviceTypeQualifier */
672 if (ccb->cmd->cmnd[0] == INQUIRY &&
673 resp->scsi_status == SAM_STAT_GOOD) {
676 scsi_sg_copy_to_buffer(ccb->cmd, hba->copy_buffer,
678 inq_data = (ST_INQ *)hba->copy_buffer;
679 if (inq_data->DeviceTypeQualifier != 0)
680 ccb->srb_status = SRB_STATUS_SELECTION_TIMEOUT;
682 ccb->srb_status = SRB_STATUS_SUCCESS;
686 static void stex_mu_intr(struct st_hba *hba, u32 doorbell)
688 void __iomem *base = hba->mmio_base;
689 struct status_msg *resp;
694 if (!(doorbell & MU_OUTBOUND_DOORBELL_STATUSHEADCHANGED))
697 /* status payloads */
698 hba->status_head = readl(base + OMR1);
699 if (unlikely(hba->status_head >= MU_STATUS_COUNT)) {
700 printk(KERN_WARNING DRV_NAME "(%s): invalid status head\n",
701 pci_name(hba->pdev));
706 * it's not a valid status payload if:
707 * 1. there are no pending requests(e.g. during init stage)
708 * 2. there are some pending requests, but the controller is in
709 * reset status, and its type is not st_yosemite
710 * firmware of st_yosemite in reset status will return pending requests
711 * to driver, so we allow it to pass
713 if (unlikely(hba->out_req_cnt <= 0 ||
714 (hba->mu_status == MU_STATE_RESETTING &&
715 hba->cardtype != st_yosemite))) {
716 hba->status_tail = hba->status_head;
720 while (hba->status_tail != hba->status_head) {
721 resp = stex_get_status(hba);
722 tag = le16_to_cpu(resp->tag);
723 if (unlikely(tag >= hba->host->can_queue)) {
724 printk(KERN_WARNING DRV_NAME
725 "(%s): invalid tag\n", pci_name(hba->pdev));
729 ccb = &hba->ccb[tag];
730 if (hba->wait_ccb == ccb)
731 hba->wait_ccb = NULL;
732 if (unlikely(ccb->req == NULL)) {
733 printk(KERN_WARNING DRV_NAME
734 "(%s): lagging req\n", pci_name(hba->pdev));
739 size = resp->payload_sz * sizeof(u32); /* payload size */
740 if (unlikely(size < sizeof(*resp) - STATUS_VAR_LEN ||
741 size > sizeof(*resp))) {
742 printk(KERN_WARNING DRV_NAME "(%s): bad status size\n",
743 pci_name(hba->pdev));
745 size -= sizeof(*resp) - STATUS_VAR_LEN; /* copy size */
747 stex_copy_data(ccb, resp, size);
751 ccb->srb_status = resp->srb_status;
752 ccb->scsi_status = resp->scsi_status;
754 if (likely(ccb->cmd != NULL)) {
755 if (hba->cardtype == st_yosemite)
756 stex_ys_commands(hba, ccb, resp);
758 if (unlikely(ccb->cmd->cmnd[0] == PASSTHRU_CMD &&
759 ccb->cmd->cmnd[1] == PASSTHRU_GET_ADAPTER))
760 stex_controller_info(hba, ccb);
762 scsi_dma_unmap(ccb->cmd);
765 } else if (ccb->req_type & PASSTHRU_REQ_TYPE) {
767 if (ccb->req_type & PASSTHRU_REQ_NO_WAKEUP) {
772 if (waitqueue_active(&hba->waitq))
773 wake_up(&hba->waitq);
778 writel(hba->status_head, base + IMR1);
779 readl(base + IMR1); /* flush */
782 static irqreturn_t stex_intr(int irq, void *__hba)
784 struct st_hba *hba = __hba;
785 void __iomem *base = hba->mmio_base;
790 spin_lock_irqsave(hba->host->host_lock, flags);
792 data = readl(base + ODBL);
794 if (data && data != 0xffffffff) {
795 /* clear the interrupt */
796 writel(data, base + ODBL);
797 readl(base + ODBL); /* flush */
798 stex_mu_intr(hba, data);
802 spin_unlock_irqrestore(hba->host->host_lock, flags);
804 return IRQ_RETVAL(handled);
807 static int stex_handshake(struct st_hba *hba)
809 void __iomem *base = hba->mmio_base;
810 struct handshake_frame *h;
811 dma_addr_t status_phys;
813 unsigned long before;
815 if (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
816 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
819 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
820 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
821 printk(KERN_ERR DRV_NAME
822 "(%s): no handshake signature\n",
823 pci_name(hba->pdev));
833 data = readl(base + OMR1);
834 if ((data & 0xffff0000) == MU_HANDSHAKE_SIGNATURE_HALF) {
836 if (hba->host->can_queue > data)
837 hba->host->can_queue = data;
840 h = (struct handshake_frame *)(hba->dma_mem + MU_REQ_BUFFER_SIZE);
841 h->rb_phy = cpu_to_le32(hba->dma_handle);
842 h->rb_phy_hi = cpu_to_le32((hba->dma_handle >> 16) >> 16);
843 h->req_sz = cpu_to_le16(sizeof(struct req_msg));
844 h->req_cnt = cpu_to_le16(MU_REQ_COUNT);
845 h->status_sz = cpu_to_le16(sizeof(struct status_msg));
846 h->status_cnt = cpu_to_le16(MU_STATUS_COUNT);
847 stex_gettime(&h->hosttime);
848 h->partner_type = HMU_PARTNER_TYPE;
849 if (hba->dma_size > STEX_BUFFER_SIZE) {
850 h->extra_offset = cpu_to_le32(STEX_BUFFER_SIZE);
851 h->extra_size = cpu_to_le32(ST_ADDITIONAL_MEM);
853 h->extra_offset = h->extra_size = 0;
855 status_phys = hba->dma_handle + MU_REQ_BUFFER_SIZE;
856 writel(status_phys, base + IMR0);
858 writel((status_phys >> 16) >> 16, base + IMR1);
861 writel((status_phys >> 16) >> 16, base + OMR0); /* old fw compatible */
863 writel(MU_INBOUND_DOORBELL_HANDSHAKE, base + IDBL);
864 readl(base + IDBL); /* flush */
868 while (readl(base + OMR0) != MU_HANDSHAKE_SIGNATURE) {
869 if (time_after(jiffies, before + MU_MAX_DELAY * HZ)) {
870 printk(KERN_ERR DRV_NAME
871 "(%s): no signature after handshake frame\n",
872 pci_name(hba->pdev));
879 writel(0, base + IMR0);
881 writel(0, base + OMR0);
883 writel(0, base + IMR1);
885 writel(0, base + OMR1);
886 readl(base + OMR1); /* flush */
887 hba->mu_status = MU_STATE_STARTED;
891 static int stex_abort(struct scsi_cmnd *cmd)
893 struct Scsi_Host *host = cmd->device->host;
894 struct st_hba *hba = (struct st_hba *)host->hostdata;
895 u16 tag = cmd->request->tag;
898 int result = SUCCESS;
901 printk(KERN_INFO DRV_NAME
902 "(%s): aborting command\n", pci_name(hba->pdev));
903 scsi_print_command(cmd);
905 base = hba->mmio_base;
906 spin_lock_irqsave(host->host_lock, flags);
907 if (tag < host->can_queue && hba->ccb[tag].cmd == cmd)
908 hba->wait_ccb = &hba->ccb[tag];
910 for (tag = 0; tag < host->can_queue; tag++)
911 if (hba->ccb[tag].cmd == cmd) {
912 hba->wait_ccb = &hba->ccb[tag];
915 if (tag >= host->can_queue)
919 data = readl(base + ODBL);
920 if (data == 0 || data == 0xffffffff)
923 writel(data, base + ODBL);
924 readl(base + ODBL); /* flush */
926 stex_mu_intr(hba, data);
928 if (hba->wait_ccb == NULL) {
929 printk(KERN_WARNING DRV_NAME
930 "(%s): lost interrupt\n", pci_name(hba->pdev));
936 hba->wait_ccb->req = NULL; /* nullify the req's future return */
937 hba->wait_ccb = NULL;
940 spin_unlock_irqrestore(host->host_lock, flags);
944 static void stex_hard_reset(struct st_hba *hba)
951 for (i = 0; i < 16; i++)
952 pci_read_config_dword(hba->pdev, i * 4,
953 &hba->pdev->saved_config_space[i]);
955 /* Reset secondary bus. Our controller(MU/ATU) is the only device on
956 secondary bus. Consult Intel 80331/3 developer's manual for detail */
957 bus = hba->pdev->bus;
958 pci_read_config_byte(bus->self, PCI_BRIDGE_CONTROL, &pci_bctl);
959 pci_bctl |= PCI_BRIDGE_CTL_BUS_RESET;
960 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
963 * 1 ms may be enough for 8-port controllers. But 16-port controllers
964 * require more time to finish bus reset. Use 100 ms here for safety
967 pci_bctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
968 pci_write_config_byte(bus->self, PCI_BRIDGE_CONTROL, pci_bctl);
970 for (i = 0; i < MU_HARD_RESET_WAIT; i++) {
971 pci_read_config_word(hba->pdev, PCI_COMMAND, &pci_cmd);
972 if (pci_cmd != 0xffff && (pci_cmd & PCI_COMMAND_MASTER))
978 for (i = 0; i < 16; i++)
979 pci_write_config_dword(hba->pdev, i * 4,
980 hba->pdev->saved_config_space[i]);
983 static int stex_reset(struct scsi_cmnd *cmd)
987 unsigned long before;
988 hba = (struct st_hba *) &cmd->device->host->hostdata[0];
990 printk(KERN_INFO DRV_NAME
991 "(%s): resetting host\n", pci_name(hba->pdev));
992 scsi_print_command(cmd);
994 hba->mu_status = MU_STATE_RESETTING;
996 if (hba->cardtype == st_shasta)
997 stex_hard_reset(hba);
999 if (hba->cardtype != st_yosemite) {
1000 if (stex_handshake(hba)) {
1001 printk(KERN_WARNING DRV_NAME
1002 "(%s): resetting: handshake failed\n",
1003 pci_name(hba->pdev));
1006 spin_lock_irqsave(hba->host->host_lock, flags);
1009 hba->status_head = 0;
1010 hba->status_tail = 0;
1011 hba->out_req_cnt = 0;
1012 spin_unlock_irqrestore(hba->host->host_lock, flags);
1017 writel(MU_INBOUND_DOORBELL_RESET, hba->mmio_base + IDBL);
1018 readl(hba->mmio_base + IDBL); /* flush */
1020 while (hba->out_req_cnt > 0) {
1021 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ)) {
1022 printk(KERN_WARNING DRV_NAME
1023 "(%s): reset timeout\n", pci_name(hba->pdev));
1029 hba->mu_status = MU_STATE_STARTED;
1033 static int stex_biosparam(struct scsi_device *sdev,
1034 struct block_device *bdev, sector_t capacity, int geom[])
1036 int heads = 255, sectors = 63;
1038 if (capacity < 0x200000) {
1043 sector_div(capacity, heads * sectors);
1052 static struct scsi_host_template driver_template = {
1053 .module = THIS_MODULE,
1055 .proc_name = DRV_NAME,
1056 .bios_param = stex_biosparam,
1057 .queuecommand = stex_queuecommand,
1058 .slave_alloc = stex_slave_alloc,
1059 .slave_configure = stex_slave_config,
1060 .slave_destroy = stex_slave_destroy,
1061 .eh_abort_handler = stex_abort,
1062 .eh_host_reset_handler = stex_reset,
1063 .can_queue = ST_CAN_QUEUE,
1065 .sg_tablesize = ST_MAX_SG,
1066 .cmd_per_lun = ST_CMD_PER_LUN,
1069 static int stex_set_dma_mask(struct pci_dev * pdev)
1072 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)
1073 && !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
1075 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1077 ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1081 static int __devinit
1082 stex_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1085 struct Scsi_Host *host;
1088 err = pci_enable_device(pdev);
1092 pci_set_master(pdev);
1094 host = scsi_host_alloc(&driver_template, sizeof(struct st_hba));
1097 printk(KERN_ERR DRV_NAME "(%s): scsi_host_alloc failed\n",
1103 hba = (struct st_hba *)host->hostdata;
1104 memset(hba, 0, sizeof(struct st_hba));
1106 err = pci_request_regions(pdev, DRV_NAME);
1108 printk(KERN_ERR DRV_NAME "(%s): request regions failed\n",
1110 goto out_scsi_host_put;
1113 hba->mmio_base = pci_ioremap_bar(pdev, 0);
1114 if ( !hba->mmio_base) {
1115 printk(KERN_ERR DRV_NAME "(%s): memory map failed\n",
1118 goto out_release_regions;
1121 err = stex_set_dma_mask(pdev);
1123 printk(KERN_ERR DRV_NAME "(%s): set dma mask failed\n",
1128 hba->cardtype = (unsigned int) id->driver_data;
1129 if (hba->cardtype == st_vsc && (pdev->subsystem_device & 0xf) == 0x1)
1130 hba->cardtype = st_vsc1;
1131 hba->dma_size = (hba->cardtype == st_vsc1 || hba->cardtype == st_seq) ?
1132 (STEX_BUFFER_SIZE + ST_ADDITIONAL_MEM) : (STEX_BUFFER_SIZE);
1133 hba->dma_mem = dma_alloc_coherent(&pdev->dev,
1134 hba->dma_size, &hba->dma_handle, GFP_KERNEL);
1135 if (!hba->dma_mem) {
1137 printk(KERN_ERR DRV_NAME "(%s): dma mem alloc failed\n",
1142 hba->status_buffer =
1143 (struct status_msg *)(hba->dma_mem + MU_REQ_BUFFER_SIZE);
1144 hba->copy_buffer = hba->dma_mem + MU_BUFFER_SIZE;
1145 hba->mu_status = MU_STATE_STARTING;
1147 if (hba->cardtype == st_shasta) {
1149 host->max_id = 16 + 1;
1150 } else if (hba->cardtype == st_yosemite) {
1151 host->max_lun = 128;
1152 host->max_id = 1 + 1;
1154 /* st_vsc , st_vsc1 and st_seq */
1156 host->max_id = 128 + 1;
1158 host->max_channel = 0;
1159 host->unique_id = host->host_no;
1160 host->max_cmd_len = STEX_CDB_LENGTH;
1164 init_waitqueue_head(&hba->waitq);
1166 err = request_irq(pdev->irq, stex_intr, IRQF_SHARED, DRV_NAME, hba);
1168 printk(KERN_ERR DRV_NAME "(%s): request irq failed\n",
1173 err = stex_handshake(hba);
1177 err = scsi_init_shared_tag_map(host, host->can_queue);
1179 printk(KERN_ERR DRV_NAME "(%s): init shared queue failed\n",
1184 pci_set_drvdata(pdev, hba);
1186 err = scsi_add_host(host, &pdev->dev);
1188 printk(KERN_ERR DRV_NAME "(%s): scsi_add_host failed\n",
1193 scsi_scan_host(host);
1198 free_irq(pdev->irq, hba);
1200 dma_free_coherent(&pdev->dev, hba->dma_size,
1201 hba->dma_mem, hba->dma_handle);
1203 iounmap(hba->mmio_base);
1204 out_release_regions:
1205 pci_release_regions(pdev);
1207 scsi_host_put(host);
1209 pci_disable_device(pdev);
1214 static void stex_hba_stop(struct st_hba *hba)
1216 struct req_msg *req;
1217 unsigned long flags;
1218 unsigned long before;
1221 spin_lock_irqsave(hba->host->host_lock, flags);
1222 req = stex_alloc_req(hba);
1223 memset(req->cdb, 0, STEX_CDB_LENGTH);
1225 if (hba->cardtype == st_yosemite) {
1226 req->cdb[0] = MGT_CMD;
1227 req->cdb[1] = MGT_CMD_SIGNATURE;
1228 req->cdb[2] = CTLR_CONFIG_CMD;
1229 req->cdb[3] = CTLR_SHUTDOWN;
1231 req->cdb[0] = CONTROLLER_CMD;
1232 req->cdb[1] = CTLR_POWER_STATE_CHANGE;
1233 req->cdb[2] = CTLR_POWER_SAVING;
1236 hba->ccb[tag].cmd = NULL;
1237 hba->ccb[tag].sg_count = 0;
1238 hba->ccb[tag].sense_bufflen = 0;
1239 hba->ccb[tag].sense_buffer = NULL;
1240 hba->ccb[tag].req_type |= PASSTHRU_REQ_TYPE;
1242 stex_send_cmd(hba, req, tag);
1243 spin_unlock_irqrestore(hba->host->host_lock, flags);
1246 while (hba->ccb[tag].req_type & PASSTHRU_REQ_TYPE) {
1247 if (time_after(jiffies, before + ST_INTERNAL_TIMEOUT * HZ))
1253 static void stex_hba_free(struct st_hba *hba)
1255 free_irq(hba->pdev->irq, hba);
1257 iounmap(hba->mmio_base);
1259 pci_release_regions(hba->pdev);
1261 dma_free_coherent(&hba->pdev->dev, hba->dma_size,
1262 hba->dma_mem, hba->dma_handle);
1265 static void stex_remove(struct pci_dev *pdev)
1267 struct st_hba *hba = pci_get_drvdata(pdev);
1269 scsi_remove_host(hba->host);
1271 pci_set_drvdata(pdev, NULL);
1277 scsi_host_put(hba->host);
1279 pci_disable_device(pdev);
1282 static void stex_shutdown(struct pci_dev *pdev)
1284 struct st_hba *hba = pci_get_drvdata(pdev);
1289 static struct pci_device_id stex_pci_tbl[] = {
1291 { 0x105a, 0x8350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1292 st_shasta }, /* SuperTrak EX8350/8300/16350/16300 */
1293 { 0x105a, 0xc350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1294 st_shasta }, /* SuperTrak EX12350 */
1295 { 0x105a, 0x4302, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1296 st_shasta }, /* SuperTrak EX4350 */
1297 { 0x105a, 0xe350, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1298 st_shasta }, /* SuperTrak EX24350 */
1301 { 0x105a, 0x7250, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_vsc },
1304 { 0x105a, 0x8650, PCI_ANY_ID, 0x4600, 0, 0,
1305 st_yosemite }, /* SuperTrak EX4650 */
1306 { 0x105a, 0x8650, PCI_ANY_ID, 0x4610, 0, 0,
1307 st_yosemite }, /* SuperTrak EX4650o */
1308 { 0x105a, 0x8650, PCI_ANY_ID, 0x8600, 0, 0,
1309 st_yosemite }, /* SuperTrak EX8650EL */
1310 { 0x105a, 0x8650, PCI_ANY_ID, 0x8601, 0, 0,
1311 st_yosemite }, /* SuperTrak EX8650 */
1312 { 0x105a, 0x8650, PCI_ANY_ID, 0x8602, 0, 0,
1313 st_yosemite }, /* SuperTrak EX8654 */
1314 { 0x105a, 0x8650, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
1315 st_yosemite }, /* generic st_yosemite */
1318 { 0x105a, 0x3360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, st_seq },
1319 { } /* terminate list */
1321 MODULE_DEVICE_TABLE(pci, stex_pci_tbl);
1323 static struct pci_driver stex_pci_driver = {
1325 .id_table = stex_pci_tbl,
1326 .probe = stex_probe,
1327 .remove = __devexit_p(stex_remove),
1328 .shutdown = stex_shutdown,
1331 static int __init stex_init(void)
1333 printk(KERN_INFO DRV_NAME
1334 ": Promise SuperTrak EX Driver version: %s\n",
1337 return pci_register_driver(&stex_pci_driver);
1340 static void __exit stex_exit(void)
1342 pci_unregister_driver(&stex_pci_driver);
1345 module_init(stex_init);
1346 module_exit(stex_exit);