13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <linux/compiler.h>
24 #include <linux/bitops.h>
26 #include <linux/time.h>
32 #include <asm/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 ": "
68 static int max_queue = 1;
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)
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))
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...)
89 #define DPRINTK(fmt, args...)
90 #define VPRINTK(fmt, args...)
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__); \
228 static const char *state_name[] = {
233 "HST_DEV_SCAN_START",
236 "HST_PROBE_FINISHED",
409 static void carm_remove_one (
struct pci_dev *pdev);
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 = {
428 .getgeo = carm_bdev_getgeo,
431 static unsigned int carm_host_id;
432 static unsigned long carm_major_alloc;
453 if (msg_size <= msg_sizes[i])
459 static void carm_init_buckets(
void __iomem *mmio)
468 unsigned int msg_idx)
474 unsigned int msg_idx)
498 DPRINTK(
"host msg queue full\n");
516 for (i = 0; i < max_queue; i++)
517 if ((host->
msg_alloc & (1ULL << i)) == 0) {
530 DPRINTK(
"no request available, returning NULL\n");
557 while (tries-- > 0) {
559 crq = carm_get_request(host);
560 spin_unlock_irqrestore(&host->
lock, flags);
573 carm_put_request(host, crq);
574 spin_unlock_irqrestore(&host->
lock, flags);
591 crq = carm_get_special(host);
599 ioc = carm_ref_msg(host, idx);
600 msg_dma = carm_ref_msg_dma(host, idx);
610 memset(ioc, 0,
sizeof(*ioc));
617 spin_lock_irq(&host->
lock);
620 spin_unlock_irq(&host->
lock);
622 DPRINTK(
"blk_execute_rq_nowait, tag == %u\n", idx);
623 crq->
rq->cmd_type = REQ_TYPE_SPECIAL;
624 crq->
rq->special = crq;
630 spin_lock_irq(&host->
lock);
632 spin_unlock_irq(&host->
lock);
646 crq = carm_get_special(host);
652 mem = carm_ref_msg(host, idx);
654 msg_size =
func(host, idx, mem);
659 rc = carm_lookup_bucket(msg_size);
663 DPRINTK(
"blk_execute_rq_nowait, tag == %u\n", idx);
664 crq->
rq->cmd_type = REQ_TYPE_SPECIAL;
665 crq->
rq->special = crq;
671 static unsigned int carm_fill_sync_time(
struct carm_host *host,
672 unsigned int idx,
void *
mem)
679 memset(st, 0,
sizeof(*st));
688 static unsigned int carm_fill_alloc_buf(
struct carm_host *host,
689 unsigned int idx,
void *mem)
693 memset(ab, 0,
sizeof(*ab));
713 static unsigned int carm_fill_scan_channels(
struct carm_host *host,
714 unsigned int idx,
void *mem)
717 u32 msg_data = (
u32) (carm_ref_msg_dma(host, idx) +
720 memset(ioc, 0,
sizeof(*ioc));
733 static unsigned int carm_fill_get_fw_ver(
struct carm_host *host,
734 unsigned int idx,
void *mem)
737 u32 msg_data = (
u32) (carm_ref_msg_dma(host, idx) +
sizeof(*ioc));
739 memset(ioc, 0,
sizeof(*ioc));
749 static inline void carm_end_request_queued(
struct carm_host *host,
758 rc = carm_put_request(host, crq);
767 VPRINTK(
"STOPPED QUEUE %p\n", q);
787 static inline void carm_round_robin(
struct carm_host *host)
792 VPRINTK(
"STARTED QUEUE %p\n", q);
799 carm_end_request_queued(host, crq, error);
801 carm_round_robin(host);
804 carm_round_robin(host);
828 rc = carm_send_msg(host, crq);
831 carm_push_q(host, q);
845 int writing = 0, pci_dir,
i, n_elem,
rc;
855 crq = carm_get_request(host);
857 carm_push_q(host, q);
864 if (rq_data_dir(rq) ==
WRITE) {
875 carm_end_rq(host, crq, -
EIO);
880 n_elem = pci_map_sg(host->
pdev, sg, n_elem, pci_dir);
882 carm_end_rq(host, crq, -
EIO);
909 tmp = (blk_rq_pos(rq) >> 16) >> 16;
914 for (i = 0; i < n_elem; i++) {
921 rc = carm_lookup_bucket(msg_size);
930 rc = carm_send_msg(host, crq);
932 carm_put_request(host, crq);
934 carm_push_q(host, q);
938 goto queue_one_request;
941 static void carm_handle_array_info(
struct carm_host *host,
954 carm_end_rq(host, crq, error);
970 port = &host->
port[cur_port];
983 port->
name[
sizeof(port->
name) - 1] = 0;
985 while (slen && (port->
name[slen - 1] ==
' ')) {
986 port->
name[slen - 1] = 0;
992 (
unsigned long long) port->
capacity);
1001 static void carm_handle_scan_chan(
struct carm_host *host,
1006 unsigned int i, dev_count = 0;
1011 carm_end_rq(host, crq, error);
1019 for (i = 0; i < 8; i++)
1020 if (msg_data[i] == 0) {
1026 pci_name(host->
pdev), dev_count);
1034 static void carm_handle_generic(
struct carm_host *host,
1040 carm_end_rq(host, crq, error);
1050 static inline void carm_handle_rw(
struct carm_host *host,
1057 if (rq_data_dir(crq->
rq) ==
WRITE)
1062 pci_unmap_sg(host->
pdev, &crq->
sg[0], crq->
n_elem, pci_dir);
1064 carm_end_rq(host, crq, error);
1067 static inline void carm_handle_resp(
struct carm_host *host,
1071 unsigned int msg_idx;
1076 VPRINTK(
"ENTER, handle == 0x%x\n", handle);
1080 pci_name(host->
pdev), handle);
1085 VPRINTK(
"tag == %u\n", msg_idx);
1087 crq = &host->
req[msg_idx];
1092 carm_handle_rw(host, crq, error);
1096 mem = carm_ref_msg(host, msg_idx);
1102 carm_handle_scan_chan(host, crq, mem, error);
1114 carm_handle_generic(host, crq, error,
1118 carm_handle_generic(host, crq, error,
1128 carm_handle_generic(host, crq, error,
1142 carm_handle_array_info(host, crq, mem, error);
1161 carm_end_rq(host, crq, -
EIO);
1164 static inline void carm_handle_responses(
struct carm_host *host)
1168 unsigned int work = 0;
1169 unsigned int idx = host->
resp_idx % RMSG_Q_LEN;
1174 if (status == 0xffffffff) {
1175 VPRINTK(
"ending response on index %u\n", idx);
1181 else if ((status & (1 << 31)) == 0) {
1182 VPRINTK(
"handling msg response on index %u\n", idx);
1183 carm_handle_resp(host, resp[idx].
ret_handle, status);
1188 else if ((status & 0xff000000) == (1 << 31)) {
1189 u8 *evt_type_ptr = (
u8 *) &resp[idx];
1190 u8 evt_type = *evt_type_ptr;
1192 pci_name(host->
pdev), (
int) evt_type);
1204 static irqreturn_t carm_interrupt(
int irq,
void *__host)
1210 unsigned long flags;
1224 if (mask == 0 || mask == 0xffffffff) {
1225 VPRINTK(
"no work, mask == 0x%x\n", mask);
1233 VPRINTK(
"not initialized yet, mask = 0x%x\n", mask);
1239 carm_handle_responses(host);
1243 spin_unlock_irqrestore(&host->
lock, flags);
1252 unsigned long flags;
1254 int rc,
i, next_dev;
1259 state = host->
state;
1260 spin_unlock_irqrestore(&host->
lock, flags);
1262 DPRINTK(
"ENTER, state == %s\n", state_name[state]);
1271 rc = carm_send_special(host, carm_fill_alloc_buf);
1279 rc = carm_send_special(host, carm_fill_sync_time);
1287 rc = carm_send_special(host, carm_fill_get_fw_ver);
1295 rc = carm_send_special(host, carm_fill_scan_channels);
1316 if (next_dev >= 0) {
1334 struct gendisk *disk = port->
disk;
1336 set_capacity(disk, port->
capacity);
1342 pci_name(host->
pdev), activated);
1367 spin_unlock_irqrestore(&host->
lock, flags);
1377 for (i = 0; i < 50000; i++) {
1382 if ((tmp & bits) == bits)
1385 if ((tmp & bits) == 0)
1393 bits, test_bit ?
"yes" :
"no");
1397 static void carm_init_responses(
struct carm_host *host)
1409 static int carm_init_host(
struct carm_host *host)
1432 DPRINTK(
"CME bit present, waiting\n");
1433 rc = carm_init_wait(mmio, CARM_CME, 1);
1435 DPRINTK(
"EXIT, carm_init_wait 1 failed\n");
1440 DPRINTK(
"RME bit present, waiting\n");
1441 rc = carm_init_wait(mmio, CARM_RME, 1);
1443 DPRINTK(
"EXIT, carm_init_wait 2 failed\n");
1452 rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 0);
1454 DPRINTK(
"EXIT, carm_init_wait 3 failed\n");
1458 carm_init_buckets(mmio);
1465 tmp |= (CARM_RME | CARM_CME |
CARM_WZBC);
1469 rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 1);
1471 DPRINTK(
"EXIT, carm_init_wait 4 failed\n");
1478 carm_init_responses(host);
1481 spin_lock_irq(&host->
lock);
1484 spin_unlock_irq(&host->
lock);
1491 static int carm_init_disks(
struct carm_host *host)
1497 struct gendisk *disk;
1501 port = &host->
port[
i];
1512 sprintf(disk->disk_name, DRV_NAME
"/%u",
1513 (
unsigned int) (host->
id * CARM_MAX_PORTS) + i);
1514 disk->major = host->
major;
1516 disk->fops = &carm_bd_ops;
1517 disk->private_data =
port;
1528 q->queuedata =
port;
1534 static void carm_free_disks(
struct carm_host *host)
1539 struct gendisk *disk = host->
port[
i].disk;
1543 if (disk->flags & GENHD_FL_UP)
1552 static int carm_init_shm(
struct carm_host *host)
1571 unsigned int pci_dac;
1586 #ifdef IF_64BIT_DMA_IS_POSSIBLE
1589 rc = pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(64));
1593 goto err_out_regions;
1602 goto err_out_regions;
1605 #ifdef IF_64BIT_DMA_IS_POSSIBLE
1614 goto err_out_regions;
1624 host->
req[i].tag = i;
1635 rc = carm_init_shm(host);
1639 goto err_out_iounmap;
1647 goto err_out_pci_free;
1650 q->queuedata = host;
1662 host->
id = carm_host_id;
1667 goto err_out_free_majors;
1671 rc = carm_init_disks(host);
1673 goto err_out_blkdev_disks;
1681 goto err_out_blkdev_disks;
1684 rc = carm_init_host(host);
1686 goto err_out_free_irq;
1688 DPRINTK(
"waiting for probe_comp\n");
1692 host->
name, pci_name(pdev), (
int) CARM_MAX_PORTS,
1697 pci_set_drvdata(pdev, host);
1702 err_out_blkdev_disks:
1703 carm_free_disks(host);
1705 err_out_free_majors:
1706 if (host->
major == 160)
1708 else if (host->
major == 161)
1724 static void carm_remove_one (
struct pci_dev *pdev)
1726 struct carm_host *host = pci_get_drvdata(pdev);
1735 carm_free_disks(host);
1737 if (host->
major == 160)
1739 else if (host->
major == 161)
1747 pci_set_drvdata(pdev,
NULL);
1750 static int __init carm_init(
void)
1752 return pci_register_driver(&carm_driver);
1755 static void __exit carm_exit(
void)