2 * sx8.c: Driver for Promise SATA SX8 looks-like-I2O hardware
4 * Copyright 2004-2005 Red Hat, Inc.
6 * Author/maintainer: Jeff Garzik <jgarzik@pobox.com>
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/blkdev.h>
20 #include <linux/sched.h>
21 #include <linux/interrupt.h>
22 #include <linux/compiler.h>
23 #include <linux/workqueue.h>
24 #include <linux/bitops.h>
25 #include <linux/delay.h>
26 #include <linux/ktime.h>
27 #include <linux/hdreg.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/completion.h>
30 #include <linux/scatterlist.h>
32 #include <linux/uaccess.h>
36 #define CARM_VERBOSE_DEBUG
39 #undef CARM_VERBOSE_DEBUG
43 #define DRV_NAME "sx8"
44 #define DRV_VERSION "1.0"
45 #define PFX DRV_NAME ": "
47 MODULE_AUTHOR("Jeff Garzik");
48 MODULE_LICENSE("GPL");
49 MODULE_DESCRIPTION("Promise SATA SX8 block driver");
50 MODULE_VERSION(DRV_VERSION);
53 * SX8 hardware has a single message queue for all ATA ports.
54 * When this driver was written, the hardware (firmware?) would
55 * corrupt data eventually, if more than one request was outstanding.
56 * As one can imagine, having 8 ports bottlenecking on a single
57 * command hurts performance.
59 * Based on user reports, later versions of the hardware (firmware?)
60 * seem to be able to survive with more than one command queued.
62 * Therefore, we default to the safe option -- 1 command -- but
63 * allow the user to increase this.
65 * SX8 should be able to support up to ~60 queued commands (CARM_MAX_REQ),
66 * but problems seem to occur when you exceed ~30, even on newer hardware.
68 static int max_queue = 1;
69 module_param(max_queue, int, 0444);
70 MODULE_PARM_DESC(max_queue, "Maximum number of queued commands. (min==1, max==30, safe==1)");
73 #define NEXT_RESP(idx) ((idx + 1) % RMSG_Q_LEN)
75 /* 0xf is just arbitrary, non-zero noise; this is sorta like poisoning */
76 #define TAG_ENCODE(tag) (((tag) << 16) | 0xf)
77 #define TAG_DECODE(tag) (((tag) >> 16) & 0x1f)
78 #define TAG_VALID(tag) ((((tag) & 0xf) == 0xf) && (TAG_DECODE(tag) < 32))
80 /* note: prints function name for you */
82 #define DPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __func__, ## args)
83 #ifdef CARM_VERBOSE_DEBUG
84 #define VPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __func__, ## args)
86 #define VPRINTK(fmt, args...)
87 #endif /* CARM_VERBOSE_DEBUG */
89 #define DPRINTK(fmt, args...)
90 #define VPRINTK(fmt, args...)
91 #endif /* CARM_DEBUG */
96 #define assert(expr) \
97 if(unlikely(!(expr))) { \
98 printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n", \
99 #expr, __FILE__, __func__, __LINE__); \
103 /* defines only for the constants which don't work well as enums */
107 /* adapter-wide limits */
109 CARM_SHM_SIZE = (4096 << 7),
110 CARM_MINORS_PER_MAJOR = 256 / CARM_MAX_PORTS,
111 CARM_MAX_WAIT_Q = CARM_MAX_PORTS + 1,
113 /* command message queue limits */
114 CARM_MAX_REQ = 64, /* max command msgs per host */
115 CARM_MSG_LOW_WATER = (CARM_MAX_REQ / 4), /* refill mark */
117 /* S/G limits, host-wide and per-request */
118 CARM_MAX_REQ_SG = 32, /* max s/g entries per request */
119 CARM_MAX_HOST_SG = 600, /* max s/g entries per host */
120 CARM_SG_LOW_WATER = (CARM_MAX_HOST_SG / 4), /* re-fill mark */
122 /* hardware registers */
124 CARM_INT_STAT = 0x10, /* interrupt status */
125 CARM_INT_MASK = 0x14, /* interrupt mask */
126 CARM_HMUC = 0x18, /* host message unit control */
127 RBUF_ADDR_LO = 0x20, /* response msg DMA buf low 32 bits */
128 RBUF_ADDR_HI = 0x24, /* response msg DMA buf high 32 bits */
130 CARM_RESP_IDX = 0x2c,
131 CARM_CMS0 = 0x30, /* command message size reg 0 */
136 /* bits in CARM_INT_{STAT,MASK} */
137 INT_RESERVED = 0xfffffff0,
138 INT_WATCHDOG = (1 << 3), /* watchdog timer */
139 INT_Q_OVERFLOW = (1 << 2), /* cmd msg q overflow */
140 INT_Q_AVAILABLE = (1 << 1), /* cmd msg q has free space */
141 INT_RESPONSE = (1 << 0), /* response msg available */
142 INT_ACK_MASK = INT_WATCHDOG | INT_Q_OVERFLOW,
143 INT_DEF_MASK = INT_RESERVED | INT_Q_OVERFLOW |
146 /* command messages, and related register bits */
147 CARM_HAVE_RESP = 0x01,
151 CARM_MSG_GET_CAPACITY = 4,
158 CARM_WZBC = (1 << 0),
160 CARM_Q_FULL = (1 << 3),
164 /* CARM_MSG_IOCTL messages */
165 CARM_IOC_SCAN_CHAN = 5, /* scan channels for devices */
166 CARM_IOC_GET_TCQ = 13, /* get tcq/ncq depth */
167 CARM_IOC_SET_TCQ = 14, /* set tcq/ncq depth */
169 IOC_SCAN_CHAN_NODEV = 0x1f,
170 IOC_SCAN_CHAN_OFFSET = 0x40,
172 /* CARM_MSG_ARRAY messages */
175 ARRAY_NO_EXIST = (1 << 31),
177 /* response messages */
178 RMSG_SZ = 8, /* sizeof(struct carm_response) */
179 RMSG_Q_LEN = 48, /* resp. msg list length */
180 RMSG_OK = 1, /* bit indicating msg was successful */
181 /* length of entire resp. msg buffer */
182 RBUF_LEN = RMSG_SZ * RMSG_Q_LEN,
184 PDC_SHM_SIZE = (4096 << 7), /* length of entire h/w buffer */
186 /* CARM_MSG_MISC messages */
191 /* MISC_GET_FW_VER feature bits */
192 FW_VER_4PORT = (1 << 2), /* 1=4 ports, 0=8 ports */
193 FW_VER_NON_RAID = (1 << 1), /* 1=non-RAID firmware, 0=RAID */
194 FW_VER_ZCR = (1 << 0), /* zero channel RAID (whatever that is) */
196 /* carm_host flags */
197 FL_NON_RAID = FW_VER_NON_RAID,
198 FL_4PORT = FW_VER_4PORT,
199 FL_FW_VER_MASK = (FW_VER_NON_RAID | FW_VER_4PORT),
201 FL_DYN_MAJOR = (1 << 17),
205 CARM_SG_BOUNDARY = 0xffffUL, /* s/g segment boundary */
208 enum scatter_gather_types {
214 HST_INVALID, /* invalid state; never used */
215 HST_ALLOC_BUF, /* setting up master SHM area */
216 HST_ERROR, /* we never leave here */
217 HST_PORT_SCAN, /* start dev scan */
218 HST_DEV_SCAN_START, /* start per-device probe */
219 HST_DEV_SCAN, /* continue per-device probe */
220 HST_DEV_ACTIVATE, /* activate devices we found */
221 HST_PROBE_FINISHED, /* probe is complete */
222 HST_PROBE_START, /* initiate probe */
223 HST_SYNC_TIME, /* tell firmware what time it is */
224 HST_GET_FW_VER, /* get firmware version, adapter port cnt */
228 static const char *state_name[] = {
233 "HST_DEV_SCAN_START",
236 "HST_PROBE_FINISHED",
244 unsigned int port_no;
245 struct gendisk *disk;
246 struct carm_host *host;
248 /* attached device characteristics */
256 struct carm_request {
259 unsigned int msg_type;
260 unsigned int msg_subtype;
261 unsigned int msg_bucket;
263 struct carm_port *port;
264 struct scatterlist sg[CARM_MAX_REQ_SG];
278 struct pci_dev *pdev;
282 struct request_queue *oob_q;
285 unsigned int hw_sg_used;
287 unsigned int resp_idx;
289 unsigned int wait_q_prod;
290 unsigned int wait_q_cons;
291 struct request_queue *wait_q[CARM_MAX_WAIT_Q];
295 struct carm_request req[CARM_MAX_REQ];
300 unsigned long dev_active;
301 unsigned long dev_present;
302 struct carm_port port[CARM_MAX_PORTS];
304 struct work_struct fsm_task;
306 struct completion probe_comp;
309 struct carm_response {
312 } __attribute__((packed));
317 } __attribute__((packed));
328 struct carm_msg_sg sg[32];
329 } __attribute__((packed));
331 struct carm_msg_allocbuf {
345 struct carm_msg_sg sg[8];
346 } __attribute__((packed));
348 struct carm_msg_ioctl {
356 } __attribute__((packed));
358 struct carm_msg_sync_time {
365 } __attribute__((packed));
367 struct carm_msg_get_fw_ver {
374 } __attribute__((packed));
381 } __attribute__((packed));
383 struct carm_array_info {
391 __le16 stripe_blk_sz;
405 /* device list continues beyond this point? */
406 } __attribute__((packed));
408 static int carm_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
409 static void carm_remove_one (struct pci_dev *pdev);
410 static int carm_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo);
412 static const struct pci_device_id carm_pci_tbl[] = {
413 { PCI_VENDOR_ID_PROMISE, 0x8000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
414 { PCI_VENDOR_ID_PROMISE, 0x8002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
415 { } /* terminate list */
417 MODULE_DEVICE_TABLE(pci, carm_pci_tbl);
419 static struct pci_driver carm_driver = {
421 .id_table = carm_pci_tbl,
422 .probe = carm_init_one,
423 .remove = carm_remove_one,
426 static const struct block_device_operations carm_bd_ops = {
427 .owner = THIS_MODULE,
428 .getgeo = carm_bdev_getgeo,
431 static unsigned int carm_host_id;
432 static unsigned long carm_major_alloc;
436 static int carm_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
438 struct carm_port *port = bdev->bd_disk->private_data;
440 geo->heads = (u8) port->dev_geom_head;
441 geo->sectors = (u8) port->dev_geom_sect;
442 geo->cylinders = port->dev_geom_cyl;
446 static const u32 msg_sizes[] = { 32, 64, 128, CARM_MSG_SIZE };
448 static inline int carm_lookup_bucket(u32 msg_size)
452 for (i = 0; i < ARRAY_SIZE(msg_sizes); i++)
453 if (msg_size <= msg_sizes[i])
459 static void carm_init_buckets(void __iomem *mmio)
463 for (i = 0; i < ARRAY_SIZE(msg_sizes); i++)
464 writel(msg_sizes[i], mmio + CARM_CMS0 + (4 * i));
467 static inline void *carm_ref_msg(struct carm_host *host,
468 unsigned int msg_idx)
470 return host->msg_base + (msg_idx * CARM_MSG_SIZE);
473 static inline dma_addr_t carm_ref_msg_dma(struct carm_host *host,
474 unsigned int msg_idx)
476 return host->msg_dma + (msg_idx * CARM_MSG_SIZE);
479 static int carm_send_msg(struct carm_host *host,
480 struct carm_request *crq)
482 void __iomem *mmio = host->mmio;
483 u32 msg = (u32) carm_ref_msg_dma(host, crq->tag);
484 u32 cm_bucket = crq->msg_bucket;
490 tmp = readl(mmio + CARM_HMUC);
491 if (tmp & CARM_Q_FULL) {
493 tmp = readl(mmio + CARM_INT_MASK);
494 tmp |= INT_Q_AVAILABLE;
495 writel(tmp, mmio + CARM_INT_MASK);
496 readl(mmio + CARM_INT_MASK); /* flush */
498 DPRINTK("host msg queue full\n");
501 writel(msg | (cm_bucket << 1), mmio + CARM_IHQP);
502 readl(mmio + CARM_IHQP); /* flush */
508 static struct carm_request *carm_get_request(struct carm_host *host)
512 /* obey global hardware limit on S/G entries */
513 if (host->hw_sg_used >= (CARM_MAX_HOST_SG - CARM_MAX_REQ_SG))
516 for (i = 0; i < max_queue; i++)
517 if ((host->msg_alloc & (1ULL << i)) == 0) {
518 struct carm_request *crq = &host->req[i];
522 host->msg_alloc |= (1ULL << i);
525 assert(host->n_msgs <= CARM_MAX_REQ);
526 sg_init_table(crq->sg, CARM_MAX_REQ_SG);
530 DPRINTK("no request available, returning NULL\n");
534 static int carm_put_request(struct carm_host *host, struct carm_request *crq)
536 assert(crq->tag < max_queue);
538 if (unlikely((host->msg_alloc & (1ULL << crq->tag)) == 0))
539 return -EINVAL; /* tried to clear a tag that was not active */
541 assert(host->hw_sg_used >= crq->n_elem);
543 host->msg_alloc &= ~(1ULL << crq->tag);
544 host->hw_sg_used -= crq->n_elem;
550 static struct carm_request *carm_get_special(struct carm_host *host)
553 struct carm_request *crq = NULL;
557 while (tries-- > 0) {
558 spin_lock_irqsave(&host->lock, flags);
559 crq = carm_get_request(host);
560 spin_unlock_irqrestore(&host->lock, flags);
570 rq = blk_get_request(host->oob_q, REQ_OP_DRV_OUT, GFP_KERNEL);
572 spin_lock_irqsave(&host->lock, flags);
573 carm_put_request(host, crq);
574 spin_unlock_irqrestore(&host->lock, flags);
582 static int carm_array_info (struct carm_host *host, unsigned int array_idx)
584 struct carm_msg_ioctl *ioc;
588 struct carm_request *crq;
591 crq = carm_get_special(host);
599 ioc = carm_ref_msg(host, idx);
600 msg_dma = carm_ref_msg_dma(host, idx);
601 msg_data = (u32) (msg_dma + sizeof(struct carm_array_info));
603 crq->msg_type = CARM_MSG_ARRAY;
604 crq->msg_subtype = CARM_ARRAY_INFO;
605 rc = carm_lookup_bucket(sizeof(struct carm_msg_ioctl) +
606 sizeof(struct carm_array_info));
608 crq->msg_bucket = (u32) rc;
610 memset(ioc, 0, sizeof(*ioc));
611 ioc->type = CARM_MSG_ARRAY;
612 ioc->subtype = CARM_ARRAY_INFO;
613 ioc->array_id = (u8) array_idx;
614 ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
615 ioc->data_addr = cpu_to_le32(msg_data);
617 spin_lock_irq(&host->lock);
618 assert(host->state == HST_DEV_SCAN_START ||
619 host->state == HST_DEV_SCAN);
620 spin_unlock_irq(&host->lock);
622 DPRINTK("blk_execute_rq_nowait, tag == %u\n", idx);
623 crq->rq->special = crq;
624 blk_execute_rq_nowait(host->oob_q, NULL, crq->rq, true, NULL);
629 spin_lock_irq(&host->lock);
630 host->state = HST_ERROR;
631 spin_unlock_irq(&host->lock);
635 typedef unsigned int (*carm_sspc_t)(struct carm_host *, unsigned int, void *);
637 static int carm_send_special (struct carm_host *host, carm_sspc_t func)
639 struct carm_request *crq;
640 struct carm_msg_ioctl *ioc;
642 unsigned int idx, msg_size;
645 crq = carm_get_special(host);
651 mem = carm_ref_msg(host, idx);
653 msg_size = func(host, idx, mem);
656 crq->msg_type = ioc->type;
657 crq->msg_subtype = ioc->subtype;
658 rc = carm_lookup_bucket(msg_size);
660 crq->msg_bucket = (u32) rc;
662 DPRINTK("blk_execute_rq_nowait, tag == %u\n", idx);
663 crq->rq->special = crq;
664 blk_execute_rq_nowait(host->oob_q, NULL, crq->rq, true, NULL);
669 static unsigned int carm_fill_sync_time(struct carm_host *host,
670 unsigned int idx, void *mem)
672 struct carm_msg_sync_time *st = mem;
674 time64_t tv = ktime_get_real_seconds();
676 memset(st, 0, sizeof(*st));
677 st->type = CARM_MSG_MISC;
678 st->subtype = MISC_SET_TIME;
679 st->handle = cpu_to_le32(TAG_ENCODE(idx));
680 st->timestamp = cpu_to_le32(tv);
682 return sizeof(struct carm_msg_sync_time);
685 static unsigned int carm_fill_alloc_buf(struct carm_host *host,
686 unsigned int idx, void *mem)
688 struct carm_msg_allocbuf *ab = mem;
690 memset(ab, 0, sizeof(*ab));
691 ab->type = CARM_MSG_MISC;
692 ab->subtype = MISC_ALLOC_MEM;
693 ab->handle = cpu_to_le32(TAG_ENCODE(idx));
695 ab->sg_type = SGT_32BIT;
696 ab->addr = cpu_to_le32(host->shm_dma + (PDC_SHM_SIZE >> 1));
697 ab->len = cpu_to_le32(PDC_SHM_SIZE >> 1);
698 ab->evt_pool = cpu_to_le32(host->shm_dma + (16 * 1024));
699 ab->n_evt = cpu_to_le32(1024);
700 ab->rbuf_pool = cpu_to_le32(host->shm_dma);
701 ab->n_rbuf = cpu_to_le32(RMSG_Q_LEN);
702 ab->msg_pool = cpu_to_le32(host->shm_dma + RBUF_LEN);
703 ab->n_msg = cpu_to_le32(CARM_Q_LEN);
704 ab->sg[0].start = cpu_to_le32(host->shm_dma + (PDC_SHM_SIZE >> 1));
705 ab->sg[0].len = cpu_to_le32(65536);
707 return sizeof(struct carm_msg_allocbuf);
710 static unsigned int carm_fill_scan_channels(struct carm_host *host,
711 unsigned int idx, void *mem)
713 struct carm_msg_ioctl *ioc = mem;
714 u32 msg_data = (u32) (carm_ref_msg_dma(host, idx) +
715 IOC_SCAN_CHAN_OFFSET);
717 memset(ioc, 0, sizeof(*ioc));
718 ioc->type = CARM_MSG_IOCTL;
719 ioc->subtype = CARM_IOC_SCAN_CHAN;
720 ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
721 ioc->data_addr = cpu_to_le32(msg_data);
723 /* fill output data area with "no device" default values */
724 mem += IOC_SCAN_CHAN_OFFSET;
725 memset(mem, IOC_SCAN_CHAN_NODEV, CARM_MAX_PORTS);
727 return IOC_SCAN_CHAN_OFFSET + CARM_MAX_PORTS;
730 static unsigned int carm_fill_get_fw_ver(struct carm_host *host,
731 unsigned int idx, void *mem)
733 struct carm_msg_get_fw_ver *ioc = mem;
734 u32 msg_data = (u32) (carm_ref_msg_dma(host, idx) + sizeof(*ioc));
736 memset(ioc, 0, sizeof(*ioc));
737 ioc->type = CARM_MSG_MISC;
738 ioc->subtype = MISC_GET_FW_VER;
739 ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
740 ioc->data_addr = cpu_to_le32(msg_data);
742 return sizeof(struct carm_msg_get_fw_ver) +
743 sizeof(struct carm_fw_ver);
746 static inline void carm_end_request_queued(struct carm_host *host,
747 struct carm_request *crq,
750 struct request *req = crq->rq;
753 __blk_end_request_all(req, error);
755 rc = carm_put_request(host, crq);
759 static inline void carm_push_q (struct carm_host *host, struct request_queue *q)
761 unsigned int idx = host->wait_q_prod % CARM_MAX_WAIT_Q;
764 VPRINTK("STOPPED QUEUE %p\n", q);
766 host->wait_q[idx] = q;
768 BUG_ON(host->wait_q_prod == host->wait_q_cons); /* overrun */
771 static inline struct request_queue *carm_pop_q(struct carm_host *host)
775 if (host->wait_q_prod == host->wait_q_cons)
778 idx = host->wait_q_cons % CARM_MAX_WAIT_Q;
781 return host->wait_q[idx];
784 static inline void carm_round_robin(struct carm_host *host)
786 struct request_queue *q = carm_pop_q(host);
789 VPRINTK("STARTED QUEUE %p\n", q);
793 static inline void carm_end_rq(struct carm_host *host, struct carm_request *crq,
796 carm_end_request_queued(host, crq, error);
798 carm_round_robin(host);
799 else if ((host->n_msgs <= CARM_MSG_LOW_WATER) &&
800 (host->hw_sg_used <= CARM_SG_LOW_WATER)) {
801 carm_round_robin(host);
805 static void carm_oob_rq_fn(struct request_queue *q)
807 struct carm_host *host = q->queuedata;
808 struct carm_request *crq;
813 DPRINTK("get req\n");
814 rq = blk_fetch_request(q);
820 assert(crq->rq == rq);
824 DPRINTK("send req\n");
825 rc = carm_send_msg(host, crq);
827 blk_requeue_request(q, rq);
828 carm_push_q(host, q);
829 return; /* call us again later, eventually */
834 static void carm_rq_fn(struct request_queue *q)
836 struct carm_port *port = q->queuedata;
837 struct carm_host *host = port->host;
838 struct carm_msg_rw *msg;
839 struct carm_request *crq;
841 struct scatterlist *sg;
842 int writing = 0, pci_dir, i, n_elem, rc;
844 unsigned int msg_size;
847 VPRINTK("get req\n");
848 rq = blk_peek_request(q);
852 crq = carm_get_request(host);
854 carm_push_q(host, q);
855 return; /* call us again later, eventually */
859 blk_start_request(rq);
861 if (rq_data_dir(rq) == WRITE) {
863 pci_dir = PCI_DMA_TODEVICE;
865 pci_dir = PCI_DMA_FROMDEVICE;
868 /* get scatterlist from block layer */
870 n_elem = blk_rq_map_sg(q, rq, sg);
872 carm_end_rq(host, crq, BLK_STS_IOERR);
873 return; /* request with no s/g entries? */
876 /* map scatterlist to PCI bus addresses */
877 n_elem = pci_map_sg(host->pdev, sg, n_elem, pci_dir);
879 carm_end_rq(host, crq, BLK_STS_IOERR);
880 return; /* request with no s/g entries? */
882 crq->n_elem = n_elem;
884 host->hw_sg_used += n_elem;
887 * build read/write message
890 VPRINTK("build msg\n");
891 msg = (struct carm_msg_rw *) carm_ref_msg(host, crq->tag);
894 msg->type = CARM_MSG_WRITE;
895 crq->msg_type = CARM_MSG_WRITE;
897 msg->type = CARM_MSG_READ;
898 crq->msg_type = CARM_MSG_READ;
901 msg->id = port->port_no;
902 msg->sg_count = n_elem;
903 msg->sg_type = SGT_32BIT;
904 msg->handle = cpu_to_le32(TAG_ENCODE(crq->tag));
905 msg->lba = cpu_to_le32(blk_rq_pos(rq) & 0xffffffff);
906 tmp = (blk_rq_pos(rq) >> 16) >> 16;
907 msg->lba_high = cpu_to_le16( (u16) tmp );
908 msg->lba_count = cpu_to_le16(blk_rq_sectors(rq));
910 msg_size = sizeof(struct carm_msg_rw) - sizeof(msg->sg);
911 for (i = 0; i < n_elem; i++) {
912 struct carm_msg_sg *carm_sg = &msg->sg[i];
913 carm_sg->start = cpu_to_le32(sg_dma_address(&crq->sg[i]));
914 carm_sg->len = cpu_to_le32(sg_dma_len(&crq->sg[i]));
915 msg_size += sizeof(struct carm_msg_sg);
918 rc = carm_lookup_bucket(msg_size);
920 crq->msg_bucket = (u32) rc;
923 * queue read/write message to hardware
926 VPRINTK("send msg, tag == %u\n", crq->tag);
927 rc = carm_send_msg(host, crq);
929 carm_put_request(host, crq);
930 blk_requeue_request(q, rq);
931 carm_push_q(host, q);
932 return; /* call us again later, eventually */
935 goto queue_one_request;
938 static void carm_handle_array_info(struct carm_host *host,
939 struct carm_request *crq, u8 *mem,
942 struct carm_port *port;
943 u8 *msg_data = mem + sizeof(struct carm_array_info);
944 struct carm_array_info *desc = (struct carm_array_info *) msg_data;
951 carm_end_rq(host, crq, error);
955 if (le32_to_cpu(desc->array_status) & ARRAY_NO_EXIST)
958 cur_port = host->cur_scan_dev;
960 /* should never occur */
961 if ((cur_port < 0) || (cur_port >= CARM_MAX_PORTS)) {
962 printk(KERN_ERR PFX "BUG: cur_scan_dev==%d, array_id==%d\n",
963 cur_port, (int) desc->array_id);
967 port = &host->port[cur_port];
969 lo = (u64) le32_to_cpu(desc->size);
970 hi = (u64) le16_to_cpu(desc->size_hi);
972 port->capacity = lo | (hi << 32);
973 port->dev_geom_head = le16_to_cpu(desc->head);
974 port->dev_geom_sect = le16_to_cpu(desc->sect);
975 port->dev_geom_cyl = le16_to_cpu(desc->cyl);
977 host->dev_active |= (1 << cur_port);
979 strncpy(port->name, desc->name, sizeof(port->name));
980 port->name[sizeof(port->name) - 1] = 0;
981 slen = strlen(port->name);
982 while (slen && (port->name[slen - 1] == ' ')) {
983 port->name[slen - 1] = 0;
987 printk(KERN_INFO DRV_NAME "(%s): port %u device %Lu sectors\n",
988 pci_name(host->pdev), port->port_no,
989 (unsigned long long) port->capacity);
990 printk(KERN_INFO DRV_NAME "(%s): port %u device \"%s\"\n",
991 pci_name(host->pdev), port->port_no, port->name);
994 assert(host->state == HST_DEV_SCAN);
995 schedule_work(&host->fsm_task);
998 static void carm_handle_scan_chan(struct carm_host *host,
999 struct carm_request *crq, u8 *mem,
1002 u8 *msg_data = mem + IOC_SCAN_CHAN_OFFSET;
1003 unsigned int i, dev_count = 0;
1004 int new_state = HST_DEV_SCAN_START;
1008 carm_end_rq(host, crq, error);
1011 new_state = HST_ERROR;
1015 /* TODO: scan and support non-disk devices */
1016 for (i = 0; i < 8; i++)
1017 if (msg_data[i] == 0) { /* direct-access device (disk) */
1018 host->dev_present |= (1 << i);
1022 printk(KERN_INFO DRV_NAME "(%s): found %u interesting devices\n",
1023 pci_name(host->pdev), dev_count);
1026 assert(host->state == HST_PORT_SCAN);
1027 host->state = new_state;
1028 schedule_work(&host->fsm_task);
1031 static void carm_handle_generic(struct carm_host *host,
1032 struct carm_request *crq, blk_status_t error,
1033 int cur_state, int next_state)
1037 carm_end_rq(host, crq, error);
1039 assert(host->state == cur_state);
1041 host->state = HST_ERROR;
1043 host->state = next_state;
1044 schedule_work(&host->fsm_task);
1047 static inline void carm_handle_rw(struct carm_host *host,
1048 struct carm_request *crq, blk_status_t error)
1054 if (rq_data_dir(crq->rq) == WRITE)
1055 pci_dir = PCI_DMA_TODEVICE;
1057 pci_dir = PCI_DMA_FROMDEVICE;
1059 pci_unmap_sg(host->pdev, &crq->sg[0], crq->n_elem, pci_dir);
1061 carm_end_rq(host, crq, error);
1064 static inline void carm_handle_resp(struct carm_host *host,
1065 __le32 ret_handle_le, u32 status)
1067 u32 handle = le32_to_cpu(ret_handle_le);
1068 unsigned int msg_idx;
1069 struct carm_request *crq;
1070 blk_status_t error = (status == RMSG_OK) ? 0 : BLK_STS_IOERR;
1073 VPRINTK("ENTER, handle == 0x%x\n", handle);
1075 if (unlikely(!TAG_VALID(handle))) {
1076 printk(KERN_ERR DRV_NAME "(%s): BUG: invalid tag 0x%x\n",
1077 pci_name(host->pdev), handle);
1081 msg_idx = TAG_DECODE(handle);
1082 VPRINTK("tag == %u\n", msg_idx);
1084 crq = &host->req[msg_idx];
1087 if (likely(crq->msg_type == CARM_MSG_READ ||
1088 crq->msg_type == CARM_MSG_WRITE)) {
1089 carm_handle_rw(host, crq, error);
1093 mem = carm_ref_msg(host, msg_idx);
1095 switch (crq->msg_type) {
1096 case CARM_MSG_IOCTL: {
1097 switch (crq->msg_subtype) {
1098 case CARM_IOC_SCAN_CHAN:
1099 carm_handle_scan_chan(host, crq, mem, error);
1102 /* unknown / invalid response */
1108 case CARM_MSG_MISC: {
1109 switch (crq->msg_subtype) {
1110 case MISC_ALLOC_MEM:
1111 carm_handle_generic(host, crq, error,
1112 HST_ALLOC_BUF, HST_SYNC_TIME);
1115 carm_handle_generic(host, crq, error,
1116 HST_SYNC_TIME, HST_GET_FW_VER);
1118 case MISC_GET_FW_VER: {
1119 struct carm_fw_ver *ver = (struct carm_fw_ver *)
1120 (mem + sizeof(struct carm_msg_get_fw_ver));
1122 host->fw_ver = le32_to_cpu(ver->version);
1123 host->flags |= (ver->features & FL_FW_VER_MASK);
1125 carm_handle_generic(host, crq, error,
1126 HST_GET_FW_VER, HST_PORT_SCAN);
1130 /* unknown / invalid response */
1136 case CARM_MSG_ARRAY: {
1137 switch (crq->msg_subtype) {
1138 case CARM_ARRAY_INFO:
1139 carm_handle_array_info(host, crq, mem, error);
1142 /* unknown / invalid response */
1149 /* unknown / invalid response */
1156 printk(KERN_WARNING DRV_NAME "(%s): BUG: unhandled message type %d/%d\n",
1157 pci_name(host->pdev), crq->msg_type, crq->msg_subtype);
1158 carm_end_rq(host, crq, BLK_STS_IOERR);
1161 static inline void carm_handle_responses(struct carm_host *host)
1163 void __iomem *mmio = host->mmio;
1164 struct carm_response *resp = (struct carm_response *) host->shm;
1165 unsigned int work = 0;
1166 unsigned int idx = host->resp_idx % RMSG_Q_LEN;
1169 u32 status = le32_to_cpu(resp[idx].status);
1171 if (status == 0xffffffff) {
1172 VPRINTK("ending response on index %u\n", idx);
1173 writel(idx << 3, mmio + CARM_RESP_IDX);
1177 /* response to a message we sent */
1178 else if ((status & (1 << 31)) == 0) {
1179 VPRINTK("handling msg response on index %u\n", idx);
1180 carm_handle_resp(host, resp[idx].ret_handle, status);
1181 resp[idx].status = cpu_to_le32(0xffffffff);
1184 /* asynchronous events the hardware throws our way */
1185 else if ((status & 0xff000000) == (1 << 31)) {
1186 u8 *evt_type_ptr = (u8 *) &resp[idx];
1187 u8 evt_type = *evt_type_ptr;
1188 printk(KERN_WARNING DRV_NAME "(%s): unhandled event type %d\n",
1189 pci_name(host->pdev), (int) evt_type);
1190 resp[idx].status = cpu_to_le32(0xffffffff);
1193 idx = NEXT_RESP(idx);
1197 VPRINTK("EXIT, work==%u\n", work);
1198 host->resp_idx += work;
1201 static irqreturn_t carm_interrupt(int irq, void *__host)
1203 struct carm_host *host = __host;
1207 unsigned long flags;
1210 VPRINTK("no host\n");
1214 spin_lock_irqsave(&host->lock, flags);
1218 /* reading should also clear interrupts */
1219 mask = readl(mmio + CARM_INT_STAT);
1221 if (mask == 0 || mask == 0xffffffff) {
1222 VPRINTK("no work, mask == 0x%x\n", mask);
1226 if (mask & INT_ACK_MASK)
1227 writel(mask, mmio + CARM_INT_STAT);
1229 if (unlikely(host->state == HST_INVALID)) {
1230 VPRINTK("not initialized yet, mask = 0x%x\n", mask);
1234 if (mask & CARM_HAVE_RESP) {
1236 carm_handle_responses(host);
1240 spin_unlock_irqrestore(&host->lock, flags);
1242 return IRQ_RETVAL(handled);
1245 static void carm_fsm_task (struct work_struct *work)
1247 struct carm_host *host =
1248 container_of(work, struct carm_host, fsm_task);
1249 unsigned long flags;
1251 int rc, i, next_dev;
1253 int new_state = HST_INVALID;
1255 spin_lock_irqsave(&host->lock, flags);
1256 state = host->state;
1257 spin_unlock_irqrestore(&host->lock, flags);
1259 DPRINTK("ENTER, state == %s\n", state_name[state]);
1262 case HST_PROBE_START:
1263 new_state = HST_ALLOC_BUF;
1268 rc = carm_send_special(host, carm_fill_alloc_buf);
1270 new_state = HST_ERROR;
1276 rc = carm_send_special(host, carm_fill_sync_time);
1278 new_state = HST_ERROR;
1283 case HST_GET_FW_VER:
1284 rc = carm_send_special(host, carm_fill_get_fw_ver);
1286 new_state = HST_ERROR;
1292 rc = carm_send_special(host, carm_fill_scan_channels);
1294 new_state = HST_ERROR;
1299 case HST_DEV_SCAN_START:
1300 host->cur_scan_dev = -1;
1301 new_state = HST_DEV_SCAN;
1307 for (i = host->cur_scan_dev + 1; i < CARM_MAX_PORTS; i++)
1308 if (host->dev_present & (1 << i)) {
1313 if (next_dev >= 0) {
1314 host->cur_scan_dev = next_dev;
1315 rc = carm_array_info(host, next_dev);
1317 new_state = HST_ERROR;
1321 new_state = HST_DEV_ACTIVATE;
1326 case HST_DEV_ACTIVATE: {
1328 for (i = 0; i < CARM_MAX_PORTS; i++)
1329 if (host->dev_active & (1 << i)) {
1330 struct carm_port *port = &host->port[i];
1331 struct gendisk *disk = port->disk;
1333 set_capacity(disk, port->capacity);
1338 printk(KERN_INFO DRV_NAME "(%s): %d ports activated\n",
1339 pci_name(host->pdev), activated);
1341 new_state = HST_PROBE_FINISHED;
1346 case HST_PROBE_FINISHED:
1347 complete(&host->probe_comp);
1355 /* should never occur */
1356 printk(KERN_ERR PFX "BUG: unknown state %d\n", state);
1361 if (new_state != HST_INVALID) {
1362 spin_lock_irqsave(&host->lock, flags);
1363 host->state = new_state;
1364 spin_unlock_irqrestore(&host->lock, flags);
1367 schedule_work(&host->fsm_task);
1370 static int carm_init_wait(void __iomem *mmio, u32 bits, unsigned int test_bit)
1374 for (i = 0; i < 50000; i++) {
1375 u32 tmp = readl(mmio + CARM_LMUC);
1379 if ((tmp & bits) == bits)
1382 if ((tmp & bits) == 0)
1389 printk(KERN_ERR PFX "carm_init_wait timeout, bits == 0x%x, test_bit == %s\n",
1390 bits, test_bit ? "yes" : "no");
1394 static void carm_init_responses(struct carm_host *host)
1396 void __iomem *mmio = host->mmio;
1398 struct carm_response *resp = (struct carm_response *) host->shm;
1400 for (i = 0; i < RMSG_Q_LEN; i++)
1401 resp[i].status = cpu_to_le32(0xffffffff);
1403 writel(0, mmio + CARM_RESP_IDX);
1406 static int carm_init_host(struct carm_host *host)
1408 void __iomem *mmio = host->mmio;
1415 writel(0, mmio + CARM_INT_MASK);
1417 tmp8 = readb(mmio + CARM_INITC);
1420 writeb(tmp8, mmio + CARM_INITC);
1421 readb(mmio + CARM_INITC); /* flush */
1423 DPRINTK("snooze...\n");
1427 tmp = readl(mmio + CARM_HMUC);
1428 if (tmp & CARM_CME) {
1429 DPRINTK("CME bit present, waiting\n");
1430 rc = carm_init_wait(mmio, CARM_CME, 1);
1432 DPRINTK("EXIT, carm_init_wait 1 failed\n");
1436 if (tmp & CARM_RME) {
1437 DPRINTK("RME bit present, waiting\n");
1438 rc = carm_init_wait(mmio, CARM_RME, 1);
1440 DPRINTK("EXIT, carm_init_wait 2 failed\n");
1445 tmp &= ~(CARM_RME | CARM_CME);
1446 writel(tmp, mmio + CARM_HMUC);
1447 readl(mmio + CARM_HMUC); /* flush */
1449 rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 0);
1451 DPRINTK("EXIT, carm_init_wait 3 failed\n");
1455 carm_init_buckets(mmio);
1457 writel(host->shm_dma & 0xffffffff, mmio + RBUF_ADDR_LO);
1458 writel((host->shm_dma >> 16) >> 16, mmio + RBUF_ADDR_HI);
1459 writel(RBUF_LEN, mmio + RBUF_BYTE_SZ);
1461 tmp = readl(mmio + CARM_HMUC);
1462 tmp |= (CARM_RME | CARM_CME | CARM_WZBC);
1463 writel(tmp, mmio + CARM_HMUC);
1464 readl(mmio + CARM_HMUC); /* flush */
1466 rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 1);
1468 DPRINTK("EXIT, carm_init_wait 4 failed\n");
1472 writel(0, mmio + CARM_HMPHA);
1473 writel(INT_DEF_MASK, mmio + CARM_INT_MASK);
1475 carm_init_responses(host);
1477 /* start initialization, probing state machine */
1478 spin_lock_irq(&host->lock);
1479 assert(host->state == HST_INVALID);
1480 host->state = HST_PROBE_START;
1481 spin_unlock_irq(&host->lock);
1482 schedule_work(&host->fsm_task);
1488 static int carm_init_disks(struct carm_host *host)
1493 for (i = 0; i < CARM_MAX_PORTS; i++) {
1494 struct gendisk *disk;
1495 struct request_queue *q;
1496 struct carm_port *port;
1498 port = &host->port[i];
1502 disk = alloc_disk(CARM_MINORS_PER_MAJOR);
1509 sprintf(disk->disk_name, DRV_NAME "/%u",
1510 (unsigned int) (host->id * CARM_MAX_PORTS) + i);
1511 disk->major = host->major;
1512 disk->first_minor = i * CARM_MINORS_PER_MAJOR;
1513 disk->fops = &carm_bd_ops;
1514 disk->private_data = port;
1516 q = blk_init_queue(carm_rq_fn, &host->lock);
1522 blk_queue_max_segments(q, CARM_MAX_REQ_SG);
1523 blk_queue_segment_boundary(q, CARM_SG_BOUNDARY);
1525 q->queuedata = port;
1531 static void carm_free_disks(struct carm_host *host)
1535 for (i = 0; i < CARM_MAX_PORTS; i++) {
1536 struct gendisk *disk = host->port[i].disk;
1538 struct request_queue *q = disk->queue;
1540 if (disk->flags & GENHD_FL_UP)
1543 blk_cleanup_queue(q);
1549 static int carm_init_shm(struct carm_host *host)
1551 host->shm = pci_alloc_consistent(host->pdev, CARM_SHM_SIZE,
1556 host->msg_base = host->shm + RBUF_LEN;
1557 host->msg_dma = host->shm_dma + RBUF_LEN;
1559 memset(host->shm, 0xff, RBUF_LEN);
1560 memset(host->msg_base, 0, PDC_SHM_SIZE - RBUF_LEN);
1565 static int carm_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1567 struct carm_host *host;
1568 unsigned int pci_dac;
1570 struct request_queue *q;
1573 printk_once(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
1575 rc = pci_enable_device(pdev);
1579 rc = pci_request_regions(pdev, DRV_NAME);
1583 #ifdef IF_64BIT_DMA_IS_POSSIBLE /* grrrr... */
1584 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1586 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1588 printk(KERN_ERR DRV_NAME "(%s): consistent DMA mask failure\n",
1590 goto err_out_regions;
1595 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1597 printk(KERN_ERR DRV_NAME "(%s): DMA mask failure\n",
1599 goto err_out_regions;
1602 #ifdef IF_64BIT_DMA_IS_POSSIBLE /* grrrr... */
1606 host = kzalloc(sizeof(*host), GFP_KERNEL);
1608 printk(KERN_ERR DRV_NAME "(%s): memory alloc failure\n",
1611 goto err_out_regions;
1615 host->flags = pci_dac ? FL_DAC : 0;
1616 spin_lock_init(&host->lock);
1617 INIT_WORK(&host->fsm_task, carm_fsm_task);
1618 init_completion(&host->probe_comp);
1620 for (i = 0; i < ARRAY_SIZE(host->req); i++)
1621 host->req[i].tag = i;
1623 host->mmio = ioremap(pci_resource_start(pdev, 0),
1624 pci_resource_len(pdev, 0));
1626 printk(KERN_ERR DRV_NAME "(%s): MMIO alloc failure\n",
1632 rc = carm_init_shm(host);
1634 printk(KERN_ERR DRV_NAME "(%s): DMA SHM alloc failure\n",
1636 goto err_out_iounmap;
1639 q = blk_init_queue(carm_oob_rq_fn, &host->lock);
1641 printk(KERN_ERR DRV_NAME "(%s): OOB queue alloc failure\n",
1644 goto err_out_pci_free;
1647 q->queuedata = host;
1650 * Figure out which major to use: 160, 161, or dynamic
1652 if (!test_and_set_bit(0, &carm_major_alloc))
1654 else if (!test_and_set_bit(1, &carm_major_alloc))
1657 host->flags |= FL_DYN_MAJOR;
1659 host->id = carm_host_id;
1660 sprintf(host->name, DRV_NAME "%d", carm_host_id);
1662 rc = register_blkdev(host->major, host->name);
1664 goto err_out_free_majors;
1665 if (host->flags & FL_DYN_MAJOR)
1668 rc = carm_init_disks(host);
1670 goto err_out_blkdev_disks;
1672 pci_set_master(pdev);
1674 rc = request_irq(pdev->irq, carm_interrupt, IRQF_SHARED, DRV_NAME, host);
1676 printk(KERN_ERR DRV_NAME "(%s): irq alloc failure\n",
1678 goto err_out_blkdev_disks;
1681 rc = carm_init_host(host);
1683 goto err_out_free_irq;
1685 DPRINTK("waiting for probe_comp\n");
1686 wait_for_completion(&host->probe_comp);
1688 printk(KERN_INFO "%s: pci %s, ports %d, io %llx, irq %u, major %d\n",
1689 host->name, pci_name(pdev), (int) CARM_MAX_PORTS,
1690 (unsigned long long)pci_resource_start(pdev, 0),
1691 pdev->irq, host->major);
1694 pci_set_drvdata(pdev, host);
1698 free_irq(pdev->irq, host);
1699 err_out_blkdev_disks:
1700 carm_free_disks(host);
1701 unregister_blkdev(host->major, host->name);
1702 err_out_free_majors:
1703 if (host->major == 160)
1704 clear_bit(0, &carm_major_alloc);
1705 else if (host->major == 161)
1706 clear_bit(1, &carm_major_alloc);
1707 blk_cleanup_queue(host->oob_q);
1709 pci_free_consistent(pdev, CARM_SHM_SIZE, host->shm, host->shm_dma);
1711 iounmap(host->mmio);
1715 pci_release_regions(pdev);
1717 pci_disable_device(pdev);
1721 static void carm_remove_one (struct pci_dev *pdev)
1723 struct carm_host *host = pci_get_drvdata(pdev);
1726 printk(KERN_ERR PFX "BUG: no host data for PCI(%s)\n",
1731 free_irq(pdev->irq, host);
1732 carm_free_disks(host);
1733 unregister_blkdev(host->major, host->name);
1734 if (host->major == 160)
1735 clear_bit(0, &carm_major_alloc);
1736 else if (host->major == 161)
1737 clear_bit(1, &carm_major_alloc);
1738 blk_cleanup_queue(host->oob_q);
1739 pci_free_consistent(pdev, CARM_SHM_SIZE, host->shm, host->shm_dma);
1740 iounmap(host->mmio);
1742 pci_release_regions(pdev);
1743 pci_disable_device(pdev);
1746 module_pci_driver(carm_driver);