2 * Driver for the Solos PCI ADSL2+ card, designed to support Linux by
3 * Traverse Technologies -- http://www.traverse.com.au/
4 * Xrio Limited -- http://www.xrio.com/
7 * Copyright © 2008 Traverse Technologies
8 * Copyright © 2008 Intel Corporation
10 * Authors: Nathan Williams <nathan@traverse.com.au>
11 * David Woodhouse <dwmw2@infradead.org>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * version 2, as published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/types.h>
32 #include <linux/pci.h>
33 #include <linux/atm.h>
34 #include <linux/atmdev.h>
35 #include <linux/skbuff.h>
36 #include <linux/sysfs.h>
37 #include <linux/device.h>
38 #include <linux/kobject.h>
40 #define VERSION "0.04"
41 #define PTAG "solos-pci"
43 #define CONFIG_RAM_SIZE 128
44 #define FLAGS_ADDR 0x7C
45 #define IRQ_EN_ADDR 0x78
47 #define IRQ_CLEAR 0x70
50 #define DATA_RAM_SIZE 32768
53 #define RX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2)
54 #define TX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2 + BUF_SIZE)
57 static int atmdebug
= 0;
72 void __iomem
*config_regs
;
73 void __iomem
*buffers
;
76 struct atm_dev
*atmdev
[4];
77 struct tasklet_struct tlet
;
79 spinlock_t tx_queue_lock
;
80 spinlock_t cli_queue_lock
;
81 struct sk_buff_head tx_queue
[4];
82 struct sk_buff_head cli_queue
[4];
85 #define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
87 MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
88 MODULE_DESCRIPTION("Solos PCI driver");
89 MODULE_VERSION(VERSION
);
90 MODULE_LICENSE("GPL");
91 MODULE_PARM_DESC(debug
, "Enable Loopback");
92 MODULE_PARM_DESC(atmdebug
, "Print ATM data");
93 module_param(debug
, int, 0444);
94 module_param(atmdebug
, int, 0444);
98 static void fpga_queue(struct solos_card
*card
, int port
, struct sk_buff
*skb
,
100 static int fpga_tx(struct solos_card
*);
101 static irqreturn_t
solos_irq(int irq
, void *dev_id
);
102 static struct atm_vcc
* find_vcc(struct atm_dev
*dev
, short vpi
, int vci
);
103 static int list_vccs(int vci
);
104 static int atm_init(struct solos_card
*);
105 static void atm_remove(struct solos_card
*);
106 static int send_command(struct solos_card
*card
, int dev
, const char *buf
, size_t size
);
107 static void solos_bh(unsigned long);
108 static int print_buffer(struct sk_buff
*buf
);
110 static inline void solos_pop(struct atm_vcc
*vcc
, struct sk_buff
*skb
)
115 dev_kfree_skb_any(skb
);
118 static ssize_t
console_show(struct device
*dev
, struct device_attribute
*attr
,
121 struct atm_dev
*atmdev
= container_of(dev
, struct atm_dev
, class_dev
);
122 struct solos_card
*card
= atmdev
->dev_data
;
125 spin_lock(&card
->cli_queue_lock
);
126 skb
= skb_dequeue(&card
->cli_queue
[SOLOS_CHAN(atmdev
)]);
127 spin_unlock(&card
->cli_queue_lock
);
129 return sprintf(buf
, "No data.\n");
131 memcpy(buf
, skb
->data
, skb
->len
);
132 dev_dbg(&card
->dev
->dev
, "len: %d\n", skb
->len
);
138 static int send_command(struct solos_card
*card
, int dev
, const char *buf
, size_t size
)
141 struct pkt_hdr
*header
;
143 // dev_dbg(&card->dev->dev, "size: %d\n", size);
145 if (size
> (BUF_SIZE
- sizeof(*header
))) {
146 dev_dbg(&card
->dev
->dev
, "Command is too big. Dropping request\n");
149 skb
= alloc_skb(size
+ sizeof(*header
), GFP_ATOMIC
);
151 dev_warn(&card
->dev
->dev
, "Failed to allocate sk_buff in send_command()\n");
155 header
= (void *)skb_put(skb
, sizeof(*header
));
157 header
->size
= cpu_to_le16(size
);
158 header
->vpi
= cpu_to_le16(0);
159 header
->vci
= cpu_to_le16(0);
160 header
->type
= cpu_to_le16(PKT_COMMAND
);
162 memcpy(skb_put(skb
, size
), buf
, size
);
164 fpga_queue(card
, dev
, skb
, NULL
);
169 static ssize_t
console_store(struct device
*dev
, struct device_attribute
*attr
,
170 const char *buf
, size_t count
)
172 struct atm_dev
*atmdev
= container_of(dev
, struct atm_dev
, class_dev
);
173 struct solos_card
*card
= atmdev
->dev_data
;
176 err
= send_command(card
, SOLOS_CHAN(atmdev
), buf
, count
);
181 static DEVICE_ATTR(console
, 0644, console_show
, console_store
);
183 static irqreturn_t
solos_irq(int irq
, void *dev_id
)
185 struct solos_card
*card
= dev_id
;
189 iowrite32(0, card
->config_regs
+ IRQ_CLEAR
);
190 //Disable IRQs from FPGA
191 iowrite32(0, card
->config_regs
+ IRQ_EN_ADDR
);
193 /* If we only do it when the device is open, we lose console
196 tasklet_schedule(&card
->tlet
);
198 //Enable IRQs from FPGA
199 iowrite32(1, card
->config_regs
+ IRQ_EN_ADDR
);
200 return IRQ_RETVAL(handled
);
203 void solos_bh(unsigned long card_arg
)
205 struct solos_card
*card
= (void *)card_arg
;
209 uint32_t rx_done
= 0;
211 card_flags
= ioread32(card
->config_regs
+ FLAGS_ADDR
);
213 /* The TX bits are set if the channel is busy; clear if not. We want to
214 invoke fpga_tx() unless _all_ the bits for active channels are set */
215 tx_mask
= (1 << card
->nr_ports
) - 1;
216 if ((card_flags
& tx_mask
) != tx_mask
)
219 for (port
= 0; port
< card
->nr_ports
; port
++) {
220 if (card_flags
& (0x10 << port
)) {
221 struct pkt_hdr header
;
226 rx_done
|= 0x10 << port
;
228 memcpy_fromio(&header
, RX_BUF(card
, port
), sizeof(header
));
230 size
= le16_to_cpu(header
.size
);
232 skb
= alloc_skb(size
, GFP_ATOMIC
);
235 dev_warn(&card
->dev
->dev
, "Failed to allocate sk_buff for RX\n");
239 memcpy_fromio(skb_put(skb
, size
),
240 RX_BUF(card
, port
) + sizeof(header
),
244 dev_info(&card
->dev
->dev
, "Received: device %d\n", port
);
245 dev_info(&card
->dev
->dev
, "size: %d VPI: %d VCI: %d\n",
246 size
, le16_to_cpu(header
.vpi
),
247 le16_to_cpu(header
.vci
));
251 switch (le16_to_cpu(header
.type
)) {
253 vcc
= find_vcc(card
->atmdev
[port
], le16_to_cpu(header
.vpi
),
254 le16_to_cpu(header
.vci
));
257 dev_warn(&card
->dev
->dev
, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
258 le16_to_cpu(header
.vci
), le16_to_cpu(header
.vpi
),
262 atm_charge(vcc
, skb
->truesize
);
264 atomic_inc(&vcc
->stats
->rx
);
268 default: /* FIXME: Not really, surely? */
269 spin_lock(&card
->cli_queue_lock
);
270 if (skb_queue_len(&card
->cli_queue
[port
]) > 10) {
272 dev_warn(&card
->dev
->dev
, "Dropping console response on port %d\n",
275 skb_queue_tail(&card
->cli_queue
[port
], skb
);
276 spin_unlock(&card
->cli_queue_lock
);
282 iowrite32(rx_done
, card
->config_regs
+ FLAGS_ADDR
);
287 static struct atm_vcc
*find_vcc(struct atm_dev
*dev
, short vpi
, int vci
)
289 struct hlist_head
*head
;
290 struct atm_vcc
*vcc
= NULL
;
291 struct hlist_node
*node
;
294 read_lock(&vcc_sklist_lock
);
295 head
= &vcc_hash
[vci
& (VCC_HTABLE_SIZE
-1)];
296 sk_for_each(s
, node
, head
) {
298 if (vcc
->dev
== dev
&& vcc
->vci
== vci
&&
299 vcc
->vpi
== vpi
&& vcc
->qos
.rxtp
.traffic_class
!= ATM_NONE
)
304 read_unlock(&vcc_sklist_lock
);
308 static int list_vccs(int vci
)
310 struct hlist_head
*head
;
312 struct hlist_node
*node
;
317 read_lock(&vcc_sklist_lock
);
319 head
= &vcc_hash
[vci
& (VCC_HTABLE_SIZE
-1)];
320 sk_for_each(s
, node
, head
) {
323 printk(KERN_DEBUG
"Device: %d Vpi: %d Vci: %d\n",
331 sk_for_each(s
, node
, head
) {
334 printk(KERN_DEBUG
"Device: %d Vpi: %d Vci: %d\n",
341 read_unlock(&vcc_sklist_lock
);
346 static int popen(struct atm_vcc
*vcc
)
348 struct solos_card
*card
= vcc
->dev
->dev_data
;
350 struct pkt_hdr
*header
;
352 skb
= alloc_skb(sizeof(*header
), GFP_ATOMIC
);
353 if (!skb
&& net_ratelimit()) {
354 dev_warn(&card
->dev
->dev
, "Failed to allocate sk_buff in popen()\n");
357 header
= (void *)skb_put(skb
, sizeof(*header
));
359 header
->size
= cpu_to_le16(sizeof(*header
));
360 header
->vpi
= cpu_to_le16(vcc
->vpi
);
361 header
->vci
= cpu_to_le16(vcc
->vci
);
362 header
->type
= cpu_to_le16(PKT_POPEN
);
364 fpga_queue(card
, SOLOS_CHAN(vcc
->dev
), skb
, NULL
);
366 // dev_dbg(&card->dev->dev, "Open for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
367 set_bit(ATM_VF_ADDR
, &vcc
->flags
); // accept the vpi / vci
368 set_bit(ATM_VF_READY
, &vcc
->flags
);
372 iowrite32(1, card
->config_regs
+ IRQ_EN_ADDR
);
374 opens
++; //count open PVCs
379 static void pclose(struct atm_vcc
*vcc
)
381 struct solos_card
*card
= vcc
->dev
->dev_data
;
383 struct pkt_hdr
*header
;
385 skb
= alloc_skb(sizeof(*header
), GFP_ATOMIC
);
387 dev_warn(&card
->dev
->dev
, "Failed to allocate sk_buff in pclose()\n");
390 header
= (void *)skb_put(skb
, sizeof(*header
));
392 header
->size
= cpu_to_le16(sizeof(*header
));
393 header
->vpi
= cpu_to_le16(vcc
->vpi
);
394 header
->vci
= cpu_to_le16(vcc
->vci
);
395 header
->type
= cpu_to_le16(PKT_PCLOSE
);
397 fpga_queue(card
, SOLOS_CHAN(vcc
->dev
), skb
, NULL
);
399 // dev_dbg(&card->dev->dev, "Close for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
401 iowrite32(0, card
->config_regs
+ IRQ_EN_ADDR
);
403 clear_bit(ATM_VF_ADDR
, &vcc
->flags
);
404 clear_bit(ATM_VF_READY
, &vcc
->flags
);
409 static int print_buffer(struct sk_buff
*buf
)
416 for (i
= 0; i
< len
; i
++){
418 sprintf(msg
, "%02X: ", i
);
420 sprintf(item
,"%02X ",*(buf
->data
+ i
));
425 printk(KERN_DEBUG
"%s", msg
);
431 printk(KERN_DEBUG
"%s", msg
);
433 printk(KERN_DEBUG
"\n");
438 static void fpga_queue(struct solos_card
*card
, int port
, struct sk_buff
*skb
,
443 *(void **)skb
->cb
= vcc
;
445 spin_lock(&card
->tx_queue_lock
);
446 old_len
= skb_queue_len(&card
->tx_queue
[port
]);
447 skb_queue_tail(&card
->tx_queue
[port
], skb
);
448 spin_unlock(&card
->tx_queue_lock
);
450 /* If TX might need to be started, do so */
455 static int fpga_tx(struct solos_card
*card
)
458 uint32_t tx_started
= 0;
464 spin_lock_irqsave(&card
->tx_lock
, flags
);
466 tx_pending
= ioread32(card
->config_regs
+ FLAGS_ADDR
);
468 dev_vdbg(&card
->dev
->dev
, "TX Flags are %X\n", tx_pending
);
470 for (port
= 0; port
< card
->nr_ports
; port
++) {
471 if (!(tx_pending
& (1 << port
))) {
473 spin_lock(&card
->tx_queue_lock
);
474 skb
= skb_dequeue(&card
->tx_queue
[port
]);
475 spin_unlock(&card
->tx_queue_lock
);
481 dev_info(&card
->dev
->dev
, "Transmitted: port %d\n",
485 memcpy_toio(TX_BUF(card
, port
), skb
->data
, skb
->len
);
487 vcc
= *(void **)skb
->cb
;
490 atomic_inc(&vcc
->stats
->tx
);
493 dev_kfree_skb_irq(skb
);
495 tx_started
|= 1 << port
; //Set TX full flag
499 iowrite32(tx_started
, card
->config_regs
+ FLAGS_ADDR
);
501 spin_unlock_irqrestore(&card
->tx_lock
, flags
);
505 static int psend(struct atm_vcc
*vcc
, struct sk_buff
*skb
)
507 struct solos_card
*card
= vcc
->dev
->dev_data
;
508 struct sk_buff
*skb2
= NULL
;
509 struct pkt_hdr
*header
;
511 //dev_dbg(&card->dev->dev, "psend called.\n");
512 //dev_dbg(&card->dev->dev, "dev,vpi,vci = %d,%d,%d\n",SOLOS_CHAN(vcc->dev),vcc->vpi,vcc->vci);
515 skb2
= atm_alloc_charge(vcc
, skb
->len
, GFP_ATOMIC
);
517 memcpy(skb2
->data
, skb
->data
, skb
->len
);
518 skb_put(skb2
, skb
->len
);
519 vcc
->push(vcc
, skb2
);
520 atomic_inc(&vcc
->stats
->rx
);
522 atomic_inc(&vcc
->stats
->tx
);
527 if (skb
->len
> (BUF_SIZE
- sizeof(*header
))) {
528 dev_warn(&card
->dev
->dev
, "Length of PDU is too large. Dropping PDU.\n");
533 if (!skb_clone_writable(skb
, sizeof(*header
))) {
537 if (skb_headroom(skb
) < sizeof(*header
))
538 expand_by
= sizeof(*header
) - skb_headroom(skb
);
540 ret
= pskb_expand_head(skb
, expand_by
, 0, GFP_ATOMIC
);
547 header
= (void *)skb_push(skb
, sizeof(*header
));
549 header
->size
= cpu_to_le16(skb
->len
);
550 header
->vpi
= cpu_to_le16(vcc
->vpi
);
551 header
->vci
= cpu_to_le16(vcc
->vci
);
552 header
->type
= cpu_to_le16(PKT_DATA
);
554 fpga_queue(card
, SOLOS_CHAN(vcc
->dev
), skb
, vcc
);
559 static struct atmdev_ops fpga_ops
= {
574 static int fpga_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
578 uint8_t major_ver
, minor_ver
;
580 struct solos_card
*card
;
585 card
= kzalloc(sizeof(*card
), GFP_KERNEL
);
591 err
= pci_enable_device(dev
);
593 dev_warn(&dev
->dev
, "Failed to enable PCI device\n");
597 err
= pci_request_regions(dev
, "solos");
599 dev_warn(&dev
->dev
, "Failed to request regions\n");
603 card
->config_regs
= pci_iomap(dev
, 0, CONFIG_RAM_SIZE
);
604 if (!card
->config_regs
) {
605 dev_warn(&dev
->dev
, "Failed to ioremap config registers\n");
606 goto out_release_regions
;
608 card
->buffers
= pci_iomap(dev
, 1, DATA_RAM_SIZE
);
609 if (!card
->buffers
) {
610 dev_warn(&dev
->dev
, "Failed to ioremap data buffers\n");
611 goto out_unmap_config
;
614 // for(i=0;i<64 ;i+=4){
615 // data32=ioread32(card->buffers + i);
616 // dev_dbg(&card->dev->dev, "%08lX\n",(unsigned long)data32);
619 //Fill Config Mem with zeros
620 for(i
= 0; i
< 128; i
+= 4)
621 iowrite32(0, card
->config_regs
+ i
);
624 iowrite32(0xF0, card
->config_regs
+ FLAGS_ADDR
);
626 data32
= ioread32(card
->config_regs
+ FPGA_VER
);
627 fpga_ver
= (data32
& 0x0000FFFF);
628 major_ver
= ((data32
& 0xFF000000) >> 24);
629 minor_ver
= ((data32
& 0x00FF0000) >> 16);
630 dev_info(&dev
->dev
, "Solos FPGA Version %d.%02d svn-%d\n",
631 major_ver
, minor_ver
, fpga_ver
);
633 card
->nr_ports
= 2; /* FIXME: Detect daughterboard */
635 err
= atm_init(card
);
639 pci_set_drvdata(dev
, card
);
640 tasklet_init(&card
->tlet
, solos_bh
, (unsigned long)card
);
641 spin_lock_init(&card
->tx_lock
);
642 spin_lock_init(&card
->tx_queue_lock
);
643 spin_lock_init(&card
->cli_queue_lock
);
647 iowrite32(data32,card->config_regs + FLAGS_ADDR);
650 // Fill Buffers with zeros
651 for (i = 0; i < BUF_SIZE * 8; i += 4)
652 iowrite32(0, card->buffers + i);
655 for(i = 0; i < (BUF_SIZE * 1); i += 4)
656 iowrite32(0x12345678, card->buffers + i + (0*BUF_SIZE));
657 for(i = 0; i < (BUF_SIZE * 1); i += 4)
658 iowrite32(0xabcdef98, card->buffers + i + (1*BUF_SIZE));
660 // Read Config Memory
661 printk(KERN_DEBUG "Reading Config MEM\n");
663 for(i = 0; i < 16; i++) {
664 data32=ioread32(card->buffers + i*(BUF_SIZE/2));
665 printk(KERN_ALERT "Addr: %lX Data: %08lX\n",
666 (unsigned long)(addr_start + i*(BUF_SIZE/2)),
667 (unsigned long)data32);
670 //dev_dbg(&card->dev->dev, "Requesting IRQ: %d\n",dev->irq);
671 err
= request_irq(dev
->irq
, solos_irq
, IRQF_DISABLED
|IRQF_SHARED
,
674 dev_dbg(&card
->dev
->dev
, "Failed to request interrupt IRQ: %d\n", dev
->irq
);
677 iowrite32(1, card
->config_regs
+ IRQ_EN_ADDR
);
682 pci_iounmap(dev
, card
->config_regs
);
684 pci_iounmap(dev
, card
->buffers
);
686 pci_release_regions(dev
);
691 static int atm_init(struct solos_card
*card
)
697 for (i
= 0; i
< card
->nr_ports
; i
++) {
698 skb_queue_head_init(&card
->tx_queue
[i
]);
699 skb_queue_head_init(&card
->cli_queue
[i
]);
701 card
->atmdev
[i
] = atm_dev_register("solos-pci", &fpga_ops
, -1, NULL
);
702 if (!card
->atmdev
[i
]) {
703 dev_err(&card
->dev
->dev
, "Could not register ATM device %d\n", i
);
707 if (device_create_file(&card
->atmdev
[i
]->class_dev
, &dev_attr_console
))
708 dev_err(&card
->dev
->dev
, "Could not register console for ATM device %d\n", i
);
710 dev_info(&card
->dev
->dev
, "Registered ATM device %d\n", card
->atmdev
[i
]->number
);
712 card
->atmdev
[i
]->ci_range
.vpi_bits
= 8;
713 card
->atmdev
[i
]->ci_range
.vci_bits
= 16;
714 card
->atmdev
[i
]->dev_data
= card
;
715 card
->atmdev
[i
]->phy_data
= (void *)(unsigned long)i
;
720 static void atm_remove(struct solos_card
*card
)
724 for (i
= 0; i
< card
->nr_ports
; i
++) {
725 if (card
->atmdev
[i
]) {
726 dev_info(&card
->dev
->dev
, "Unregistering ATM device %d\n", card
->atmdev
[i
]->number
);
727 atm_dev_deregister(card
->atmdev
[i
]);
732 static void fpga_remove(struct pci_dev
*dev
)
734 struct solos_card
*card
= pci_get_drvdata(dev
);
741 dev_vdbg(&dev
->dev
, "Freeing IRQ\n");
742 // Disable IRQs from FPGA
743 iowrite32(0, card
->config_regs
+ IRQ_EN_ADDR
);
744 free_irq(dev
->irq
, card
);
745 tasklet_kill(&card
->tlet
);
747 // iowrite32(0x01,pciregs);
748 dev_vdbg(&dev
->dev
, "Unmapping PCI resource\n");
749 pci_iounmap(dev
, card
->buffers
);
750 pci_iounmap(dev
, card
->config_regs
);
752 dev_vdbg(&dev
->dev
, "Releasing PCI Region\n");
753 pci_release_regions(dev
);
754 pci_disable_device(dev
);
756 pci_set_drvdata(dev
, NULL
);
758 // dev_dbg(&card->dev->dev, "fpga_remove\n");
762 static struct pci_device_id fpga_pci_tbl
[] __devinitdata
= {
763 { 0x10ee, 0x0300, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0 },
767 MODULE_DEVICE_TABLE(pci
,fpga_pci_tbl
);
769 static struct pci_driver fpga_driver
= {
771 .id_table
= fpga_pci_tbl
,
773 .remove
= fpga_remove
,
777 static int __init
solos_pci_init(void)
779 printk(KERN_INFO
"Solos PCI Driver Version %s\n", VERSION
);
780 return pci_register_driver(&fpga_driver
);
783 static void __exit
solos_pci_exit(void)
785 pci_unregister_driver(&fpga_driver
);
786 printk(KERN_INFO
"Solos PCI Driver %s Unloaded\n", VERSION
);
789 module_init(solos_pci_init
);
790 module_exit(solos_pci_exit
);