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>
12 * Treker Chen <treker@xrio.com>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * version 2, as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/ioport.h>
32 #include <linux/types.h>
33 #include <linux/pci.h>
34 #include <linux/atm.h>
35 #include <linux/atmdev.h>
36 #include <linux/skbuff.h>
37 #include <linux/sysfs.h>
38 #include <linux/device.h>
39 #include <linux/kobject.h>
40 #include <linux/firmware.h>
41 #include <linux/ctype.h>
42 #include <linux/swab.h>
44 #define VERSION "0.07"
45 #define PTAG "solos-pci"
47 #define CONFIG_RAM_SIZE 128
48 #define FLAGS_ADDR 0x7C
49 #define IRQ_EN_ADDR 0x78
51 #define IRQ_CLEAR 0x70
52 #define WRITE_FLASH 0x6C
54 #define FLASH_BLOCK 0x64
55 #define FLASH_BUSY 0x60
56 #define FPGA_MODE 0x5C
57 #define FLASH_MODE 0x58
58 #define TX_DMA_ADDR(port) (0x40 + (4 * (port)))
59 #define RX_DMA_ADDR(port) (0x30 + (4 * (port)))
61 #define DATA_RAM_SIZE 32768
63 #define FPGA_PAGE 528 /* FPGA flash page size*/
64 #define SOLOS_PAGE 512 /* Solos flash page size*/
65 #define FPGA_BLOCK (FPGA_PAGE * 8) /* FPGA flash block size*/
66 #define SOLOS_BLOCK (SOLOS_PAGE * 8) /* Solos flash block size*/
68 #define RX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2)
69 #define TX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2 + BUF_SIZE)
71 #define RX_DMA_SIZE 2048
73 static int atmdebug = 0;
74 static int firmware_upgrade = 0;
75 static int fpga_upgrade = 0;
90 #define SKB_CB(skb) ((struct solos_skb_cb *)skb->cb)
99 void __iomem *config_regs;
100 void __iomem *buffers;
104 struct atm_dev *atmdev[4];
105 struct tasklet_struct tlet;
107 spinlock_t tx_queue_lock;
108 spinlock_t cli_queue_lock;
109 spinlock_t param_queue_lock;
110 struct list_head param_queue;
111 struct sk_buff_head tx_queue[4];
112 struct sk_buff_head cli_queue[4];
113 struct sk_buff *tx_skb[4];
114 struct sk_buff *rx_skb[4];
115 wait_queue_head_t param_wq;
116 wait_queue_head_t fw_wq;
122 struct list_head list;
125 struct sk_buff *response;
128 #define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
130 MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
131 MODULE_DESCRIPTION("Solos PCI driver");
132 MODULE_VERSION(VERSION);
133 MODULE_LICENSE("GPL");
134 MODULE_PARM_DESC(atmdebug, "Print ATM data");
135 MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
136 MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
137 module_param(atmdebug, int, 0644);
138 module_param(firmware_upgrade, int, 0444);
139 module_param(fpga_upgrade, int, 0444);
141 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
142 struct atm_vcc *vcc);
143 static int fpga_tx(struct solos_card *);
144 static irqreturn_t solos_irq(int irq, void *dev_id);
145 static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
146 static int list_vccs(int vci);
147 static void release_vccs(struct atm_dev *dev);
148 static int atm_init(struct solos_card *);
149 static void atm_remove(struct solos_card *);
150 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
151 static void solos_bh(unsigned long);
152 static int print_buffer(struct sk_buff *buf);
154 static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
159 dev_kfree_skb_any(skb);
162 static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
165 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
166 struct solos_card *card = atmdev->dev_data;
167 struct solos_param prm;
169 struct pkt_hdr *header;
172 buflen = strlen(attr->attr.name) + 10;
174 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
176 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
180 header = (void *)skb_put(skb, sizeof(*header));
182 buflen = snprintf((void *)&header[1], buflen - 1,
183 "L%05d\n%s\n", current->pid, attr->attr.name);
184 skb_put(skb, buflen);
186 header->size = cpu_to_le16(buflen);
187 header->vpi = cpu_to_le16(0);
188 header->vci = cpu_to_le16(0);
189 header->type = cpu_to_le16(PKT_COMMAND);
191 prm.pid = current->pid;
193 prm.port = SOLOS_CHAN(atmdev);
195 spin_lock_irq(&card->param_queue_lock);
196 list_add(&prm.list, &card->param_queue);
197 spin_unlock_irq(&card->param_queue_lock);
199 fpga_queue(card, prm.port, skb, NULL);
201 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
203 spin_lock_irq(&card->param_queue_lock);
205 spin_unlock_irq(&card->param_queue_lock);
210 buflen = prm.response->len;
211 memcpy(buf, prm.response->data, buflen);
212 kfree_skb(prm.response);
217 static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
220 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
221 struct solos_card *card = atmdev->dev_data;
222 struct solos_param prm;
224 struct pkt_hdr *header;
228 buflen = strlen(attr->attr.name) + 11 + count;
230 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
232 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
236 header = (void *)skb_put(skb, sizeof(*header));
238 buflen = snprintf((void *)&header[1], buflen - 1,
239 "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
241 skb_put(skb, buflen);
242 header->size = cpu_to_le16(buflen);
243 header->vpi = cpu_to_le16(0);
244 header->vci = cpu_to_le16(0);
245 header->type = cpu_to_le16(PKT_COMMAND);
247 prm.pid = current->pid;
249 prm.port = SOLOS_CHAN(atmdev);
251 spin_lock_irq(&card->param_queue_lock);
252 list_add(&prm.list, &card->param_queue);
253 spin_unlock_irq(&card->param_queue_lock);
255 fpga_queue(card, prm.port, skb, NULL);
257 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
259 spin_lock_irq(&card->param_queue_lock);
261 spin_unlock_irq(&card->param_queue_lock);
270 /* Sometimes it has a newline, sometimes it doesn't. */
271 if (skb->data[buflen - 1] == '\n')
274 if (buflen == 2 && !strncmp(skb->data, "OK", 2))
276 else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
279 /* We know we have enough space allocated for this; we allocated
281 skb->data[buflen] = 0;
283 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
292 static char *next_string(struct sk_buff *skb)
295 char *this = skb->data;
297 while (i < skb->len) {
298 if (this[i] == '\n') {
308 * Status packet has fields separated by \n, starting with a version number
309 * for the information therein. Fields are....
312 * TxBitRate (version >= 1)
313 * RxBitRate (version >= 1)
314 * State (version >= 1)
316 static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
318 char *str, *end, *state_str;
319 int ver, rate_up, rate_down, state, snr, attn;
321 if (!card->atmdev[port])
324 str = next_string(skb);
328 ver = simple_strtol(str, NULL, 10);
330 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
335 str = next_string(skb);
336 rate_up = simple_strtol(str, &end, 10);
340 str = next_string(skb);
341 rate_down = simple_strtol(str, &end, 10);
345 state_str = next_string(skb);
346 if (!strcmp(state_str, "Showtime"))
347 state = ATM_PHY_SIG_FOUND;
349 state = ATM_PHY_SIG_LOST;
350 release_vccs(card->atmdev[port]);
353 str = next_string(skb);
354 snr = simple_strtol(str, &end, 10);
358 str = next_string(skb);
359 attn = simple_strtol(str, &end, 10);
363 if (state == ATM_PHY_SIG_LOST && !rate_up && !rate_down)
364 dev_info(&card->dev->dev, "Port %d ATM state: %s\n",
367 dev_info(&card->dev->dev, "Port %d ATM state: %s (%d/%d kb/s, SNR %ddB, Attn %ddB)\n",
368 port, state_str, rate_up/1000, rate_down/1000,
371 card->atmdev[port]->link_rate = rate_down;
372 card->atmdev[port]->signal = state;
377 static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
379 struct solos_param *prm;
387 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
388 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
389 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
390 skb->data[6] != '\n')
393 cmdpid = simple_strtol(&skb->data[1], NULL, 10);
395 spin_lock_irqsave(&card->param_queue_lock, flags);
396 list_for_each_entry(prm, &card->param_queue, list) {
397 if (prm->port == port && prm->pid == cmdpid) {
400 wake_up(&card->param_wq);
405 spin_unlock_irqrestore(&card->param_queue_lock, flags);
409 static ssize_t console_show(struct device *dev, struct device_attribute *attr,
412 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
413 struct solos_card *card = atmdev->dev_data;
416 spin_lock(&card->cli_queue_lock);
417 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
418 spin_unlock(&card->cli_queue_lock);
420 return sprintf(buf, "No data.\n");
422 memcpy(buf, skb->data, skb->len);
423 dev_dbg(&card->dev->dev, "len: %d\n", skb->len);
429 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
432 struct pkt_hdr *header;
434 // dev_dbg(&card->dev->dev, "size: %d\n", size);
436 if (size > (BUF_SIZE - sizeof(*header))) {
437 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
440 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
442 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
446 header = (void *)skb_put(skb, sizeof(*header));
448 header->size = cpu_to_le16(size);
449 header->vpi = cpu_to_le16(0);
450 header->vci = cpu_to_le16(0);
451 header->type = cpu_to_le16(PKT_COMMAND);
453 memcpy(skb_put(skb, size), buf, size);
455 fpga_queue(card, dev, skb, NULL);
460 static ssize_t console_store(struct device *dev, struct device_attribute *attr,
461 const char *buf, size_t count)
463 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
464 struct solos_card *card = atmdev->dev_data;
467 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
472 static DEVICE_ATTR(console, 0644, console_show, console_store);
475 #define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
476 #define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
478 #include "solos-attrlist.c"
483 #define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
484 #define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
486 static struct attribute *solos_attrs[] = {
487 #include "solos-attrlist.c"
491 static struct attribute_group solos_attr_group = {
492 .attrs = solos_attrs,
493 .name = "parameters",
496 static int flash_upgrade(struct solos_card *card, int chip)
498 const struct firmware *fw;
506 fw_name = "solos-FPGA.bin";
507 blocksize = FPGA_BLOCK;
509 fw_name = "solos-Firmware.bin";
510 blocksize = SOLOS_BLOCK;
513 if (request_firmware(&fw, fw_name, &card->dev->dev))
516 dev_info(&card->dev->dev, "Flash upgrade starting\n");
518 numblocks = fw->size / blocksize;
519 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
520 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
522 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
523 iowrite32(1, card->config_regs + FPGA_MODE);
524 data32 = ioread32(card->config_regs + FPGA_MODE);
526 /* Set mode to Chip Erase */
527 dev_info(&card->dev->dev, "Set FPGA Flash mode to %s Chip Erase\n",
528 chip?"Solos":"FPGA");
529 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
532 iowrite32(1, card->config_regs + WRITE_FLASH);
533 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
535 for (offset = 0; offset < fw->size; offset += blocksize) {
538 /* Clear write flag */
539 iowrite32(0, card->config_regs + WRITE_FLASH);
541 /* Set mode to Block Write */
542 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
543 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
545 /* Copy block to buffer, swapping each 16 bits */
546 for(i = 0; i < blocksize; i += 4) {
547 uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
548 iowrite32(word, RX_BUF(card, 3) + i);
551 /* Specify block number and then trigger flash write */
552 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
553 iowrite32(1, card->config_regs + WRITE_FLASH);
554 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
557 release_firmware(fw);
558 iowrite32(0, card->config_regs + WRITE_FLASH);
559 iowrite32(0, card->config_regs + FPGA_MODE);
560 iowrite32(0, card->config_regs + FLASH_MODE);
561 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
565 static irqreturn_t solos_irq(int irq, void *dev_id)
567 struct solos_card *card = dev_id;
571 iowrite32(0, card->config_regs + IRQ_CLEAR);
574 tasklet_schedule(&card->tlet);
576 wake_up(&card->fw_wq);
578 return IRQ_RETVAL(handled);
581 void solos_bh(unsigned long card_arg)
583 struct solos_card *card = (void *)card_arg;
586 uint32_t rx_done = 0;
588 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
590 /* The TX bits are set if the channel is busy; clear if not. We want to
591 invoke fpga_tx() unless _all_ the bits for active channels are set */
592 if ((card_flags & card->tx_mask) != card->tx_mask)
595 for (port = 0; port < card->nr_ports; port++) {
596 if (card_flags & (0x10 << port)) {
597 struct pkt_hdr _hdr, *header;
602 if (card->using_dma) {
603 skb = card->rx_skb[port];
604 card->rx_skb[port] = NULL;
606 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
607 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
609 header = (void *)skb->data;
610 size = le16_to_cpu(header->size);
611 skb_put(skb, size + sizeof(*header));
612 skb_pull(skb, sizeof(*header));
616 rx_done |= 0x10 << port;
618 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
620 size = le16_to_cpu(header->size);
622 skb = alloc_skb(size + 1, GFP_ATOMIC);
625 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
629 memcpy_fromio(skb_put(skb, size),
630 RX_BUF(card, port) + sizeof(*header),
634 dev_info(&card->dev->dev, "Received: device %d\n", port);
635 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
636 size, le16_to_cpu(header->vpi),
637 le16_to_cpu(header->vci));
641 switch (le16_to_cpu(header->type)) {
643 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
644 le16_to_cpu(header->vci));
647 dev_warn(&card->dev->dev, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
648 le16_to_cpu(header->vci), le16_to_cpu(header->vpi),
652 atm_charge(vcc, skb->truesize);
654 atomic_inc(&vcc->stats->rx);
658 process_status(card, port, skb);
659 dev_kfree_skb_any(skb);
663 default: /* FIXME: Not really, surely? */
664 if (process_command(card, port, skb))
666 spin_lock(&card->cli_queue_lock);
667 if (skb_queue_len(&card->cli_queue[port]) > 10) {
669 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
671 dev_kfree_skb_any(skb);
673 skb_queue_tail(&card->cli_queue[port], skb);
674 spin_unlock(&card->cli_queue_lock);
678 /* Allocate RX skbs for any ports which need them */
679 if (card->using_dma && card->atmdev[port] &&
680 !card->rx_skb[port]) {
681 struct sk_buff *skb = alloc_skb(RX_DMA_SIZE, GFP_ATOMIC);
683 SKB_CB(skb)->dma_addr =
684 pci_map_single(card->dev, skb->data,
685 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
686 iowrite32(SKB_CB(skb)->dma_addr,
687 card->config_regs + RX_DMA_ADDR(port));
688 card->rx_skb[port] = skb;
691 dev_warn(&card->dev->dev, "Failed to allocate RX skb");
693 /* We'll have to try again later */
694 tasklet_schedule(&card->tlet);
699 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
704 static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
706 struct hlist_head *head;
707 struct atm_vcc *vcc = NULL;
708 struct hlist_node *node;
711 read_lock(&vcc_sklist_lock);
712 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
713 sk_for_each(s, node, head) {
715 if (vcc->dev == dev && vcc->vci == vci &&
716 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE)
721 read_unlock(&vcc_sklist_lock);
725 static int list_vccs(int vci)
727 struct hlist_head *head;
729 struct hlist_node *node;
734 read_lock(&vcc_sklist_lock);
736 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
737 sk_for_each(s, node, head) {
740 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
746 for(i = 0; i < VCC_HTABLE_SIZE; i++){
748 sk_for_each(s, node, head) {
751 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
758 read_unlock(&vcc_sklist_lock);
762 static void release_vccs(struct atm_dev *dev)
766 write_lock_irq(&vcc_sklist_lock);
767 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
768 struct hlist_head *head = &vcc_hash[i];
769 struct hlist_node *node, *tmp;
773 sk_for_each_safe(s, node, tmp, head) {
775 if (vcc->dev == dev) {
776 vcc_release_async(vcc, -EPIPE);
781 write_unlock_irq(&vcc_sklist_lock);
785 static int popen(struct atm_vcc *vcc)
787 struct solos_card *card = vcc->dev->dev_data;
789 struct pkt_hdr *header;
791 if (vcc->qos.aal != ATM_AAL5) {
792 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
797 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
798 if (!skb && net_ratelimit()) {
799 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
802 header = (void *)skb_put(skb, sizeof(*header));
804 header->size = cpu_to_le16(0);
805 header->vpi = cpu_to_le16(vcc->vpi);
806 header->vci = cpu_to_le16(vcc->vci);
807 header->type = cpu_to_le16(PKT_POPEN);
809 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
811 // dev_dbg(&card->dev->dev, "Open for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
812 set_bit(ATM_VF_ADDR, &vcc->flags); // accept the vpi / vci
813 set_bit(ATM_VF_READY, &vcc->flags);
820 static void pclose(struct atm_vcc *vcc)
822 struct solos_card *card = vcc->dev->dev_data;
824 struct pkt_hdr *header;
826 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
828 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
831 header = (void *)skb_put(skb, sizeof(*header));
833 header->size = cpu_to_le16(0);
834 header->vpi = cpu_to_le16(vcc->vpi);
835 header->vci = cpu_to_le16(vcc->vci);
836 header->type = cpu_to_le16(PKT_PCLOSE);
838 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
840 // dev_dbg(&card->dev->dev, "Close for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
842 clear_bit(ATM_VF_ADDR, &vcc->flags);
843 clear_bit(ATM_VF_READY, &vcc->flags);
848 static int print_buffer(struct sk_buff *buf)
855 for (i = 0; i < len; i++){
857 sprintf(msg, "%02X: ", i);
859 sprintf(item,"%02X ",*(buf->data + i));
864 printk(KERN_DEBUG "%s", msg);
870 printk(KERN_DEBUG "%s", msg);
872 printk(KERN_DEBUG "\n");
877 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
883 SKB_CB(skb)->vcc = vcc;
885 spin_lock_irqsave(&card->tx_queue_lock, flags);
886 old_len = skb_queue_len(&card->tx_queue[port]);
887 skb_queue_tail(&card->tx_queue[port], skb);
889 card->tx_mask |= (1 << port);
891 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
893 /* Theoretically we could just schedule the tasklet here, but
894 that introduces latency we don't want -- it's noticeable */
899 static int fpga_tx(struct solos_card *card)
902 uint32_t tx_started = 0;
908 spin_lock_irqsave(&card->tx_lock, flags);
910 tx_pending = ioread32(card->config_regs + FLAGS_ADDR) & card->tx_mask;
912 dev_vdbg(&card->dev->dev, "TX Flags are %X\n", tx_pending);
914 for (port = 0; port < card->nr_ports; port++) {
915 if (card->atmdev[port] && !(tx_pending & (1 << port))) {
916 struct sk_buff *oldskb = card->tx_skb[port];
919 pci_unmap_single(card->dev, SKB_CB(oldskb)->dma_addr,
920 oldskb->len, PCI_DMA_TODEVICE);
922 spin_lock(&card->tx_queue_lock);
923 skb = skb_dequeue(&card->tx_queue[port]);
925 card->tx_mask &= ~(1 << port);
926 spin_unlock(&card->tx_queue_lock);
928 if (skb && !card->using_dma) {
929 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
930 tx_started |= 1 << port; //Set TX full flag
931 oldskb = skb; /* We're done with this skb already */
932 } else if (skb && card->using_dma) {
933 SKB_CB(skb)->dma_addr = pci_map_single(card->dev, skb->data,
934 skb->len, PCI_DMA_TODEVICE);
935 iowrite32(SKB_CB(skb)->dma_addr,
936 card->config_regs + TX_DMA_ADDR(port));
942 /* Clean up and free oldskb now it's gone */
944 dev_info(&card->dev->dev, "Transmitted: port %d\n",
946 print_buffer(oldskb);
949 vcc = SKB_CB(oldskb)->vcc;
952 atomic_inc(&vcc->stats->tx);
953 solos_pop(vcc, oldskb);
955 dev_kfree_skb_irq(oldskb);
960 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
962 spin_unlock_irqrestore(&card->tx_lock, flags);
966 static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
968 struct solos_card *card = vcc->dev->dev_data;
969 struct pkt_hdr *header;
972 //dev_dbg(&card->dev->dev, "psend called.\n");
973 //dev_dbg(&card->dev->dev, "dev,vpi,vci = %d,%d,%d\n",SOLOS_CHAN(vcc->dev),vcc->vpi,vcc->vci);
976 if (pktlen > (BUF_SIZE - sizeof(*header))) {
977 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
982 if (!skb_clone_writable(skb, sizeof(*header))) {
986 if (skb_headroom(skb) < sizeof(*header))
987 expand_by = sizeof(*header) - skb_headroom(skb);
989 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
991 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
997 header = (void *)skb_push(skb, sizeof(*header));
999 /* This does _not_ include the size of the header */
1000 header->size = cpu_to_le16(pktlen);
1001 header->vpi = cpu_to_le16(vcc->vpi);
1002 header->vci = cpu_to_le16(vcc->vci);
1003 header->type = cpu_to_le16(PKT_DATA);
1005 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1010 static struct atmdev_ops fpga_ops = {
1022 .owner = THIS_MODULE
1025 static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
1029 uint8_t major_ver, minor_ver;
1031 struct solos_card *card;
1033 card = kzalloc(sizeof(*card), GFP_KERNEL);
1038 init_waitqueue_head(&card->fw_wq);
1039 init_waitqueue_head(&card->param_wq);
1041 err = pci_enable_device(dev);
1043 dev_warn(&dev->dev, "Failed to enable PCI device\n");
1047 err = pci_set_dma_mask(dev, DMA_32BIT_MASK);
1049 dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
1053 err = pci_request_regions(dev, "solos");
1055 dev_warn(&dev->dev, "Failed to request regions\n");
1059 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1060 if (!card->config_regs) {
1061 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
1062 goto out_release_regions;
1064 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1065 if (!card->buffers) {
1066 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
1067 goto out_unmap_config;
1070 // for(i=0;i<64 ;i+=4){
1071 // data32=ioread32(card->buffers + i);
1072 // dev_dbg(&card->dev->dev, "%08lX\n",(unsigned long)data32);
1075 //Fill Config Mem with zeros
1076 for(i = 0; i < 128; i += 4)
1077 iowrite32(0, card->config_regs + i);
1079 //Set RX empty flags
1080 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1082 data32 = ioread32(card->config_regs + FPGA_VER);
1083 fpga_ver = (data32 & 0x0000FFFF);
1084 major_ver = ((data32 & 0xFF000000) >> 24);
1085 minor_ver = ((data32 & 0x00FF0000) >> 16);
1086 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1087 major_ver, minor_ver, fpga_ver);
1090 card->using_dma = 1;
1092 card->nr_ports = 2; /* FIXME: Detect daughterboard */
1094 pci_set_drvdata(dev, card);
1096 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1097 spin_lock_init(&card->tx_lock);
1098 spin_lock_init(&card->tx_queue_lock);
1099 spin_lock_init(&card->cli_queue_lock);
1100 spin_lock_init(&card->param_queue_lock);
1101 INIT_LIST_HEAD(&card->param_queue);
1104 // Set Loopback mode
1105 data32 = 0x00010000;
1106 iowrite32(data32,card->config_regs + FLAGS_ADDR);
1109 // Fill Buffers with zeros
1110 for (i = 0; i < BUF_SIZE * 8; i += 4)
1111 iowrite32(0, card->buffers + i);
1114 for(i = 0; i < (BUF_SIZE * 1); i += 4)
1115 iowrite32(0x12345678, card->buffers + i + (0*BUF_SIZE));
1116 for(i = 0; i < (BUF_SIZE * 1); i += 4)
1117 iowrite32(0xabcdef98, card->buffers + i + (1*BUF_SIZE));
1119 // Read Config Memory
1120 printk(KERN_DEBUG "Reading Config MEM\n");
1122 for(i = 0; i < 16; i++) {
1123 data32=ioread32(card->buffers + i*(BUF_SIZE/2));
1124 printk(KERN_ALERT "Addr: %lX Data: %08lX\n",
1125 (unsigned long)(addr_start + i*(BUF_SIZE/2)),
1126 (unsigned long)data32);
1129 //dev_dbg(&card->dev->dev, "Requesting IRQ: %d\n",dev->irq);
1130 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
1133 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
1134 goto out_unmap_both;
1138 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1141 flash_upgrade(card, 0);
1143 if (firmware_upgrade)
1144 flash_upgrade(card, 1);
1146 err = atm_init(card);
1153 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1154 free_irq(dev->irq, card);
1155 tasklet_kill(&card->tlet);
1158 pci_set_drvdata(dev, NULL);
1159 pci_iounmap(dev, card->config_regs);
1161 pci_iounmap(dev, card->buffers);
1162 out_release_regions:
1163 pci_release_regions(dev);
1168 static int atm_init(struct solos_card *card)
1172 for (i = 0; i < card->nr_ports; i++) {
1173 struct sk_buff *skb;
1174 struct pkt_hdr *header;
1176 skb_queue_head_init(&card->tx_queue[i]);
1177 skb_queue_head_init(&card->cli_queue[i]);
1179 card->atmdev[i] = atm_dev_register("solos-pci", &fpga_ops, -1, NULL);
1180 if (!card->atmdev[i]) {
1181 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1185 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1186 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
1187 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1188 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
1190 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1192 card->atmdev[i]->ci_range.vpi_bits = 8;
1193 card->atmdev[i]->ci_range.vci_bits = 16;
1194 card->atmdev[i]->dev_data = card;
1195 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
1196 card->atmdev[i]->signal = ATM_PHY_SIG_UNKNOWN;
1198 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
1200 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1204 header = (void *)skb_put(skb, sizeof(*header));
1206 header->size = cpu_to_le16(0);
1207 header->vpi = cpu_to_le16(0);
1208 header->vci = cpu_to_le16(0);
1209 header->type = cpu_to_le16(PKT_STATUS);
1211 fpga_queue(card, i, skb, NULL);
1216 static void atm_remove(struct solos_card *card)
1220 for (i = 0; i < card->nr_ports; i++) {
1221 if (card->atmdev[i]) {
1222 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
1224 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
1225 atm_dev_deregister(card->atmdev[i]);
1230 static void fpga_remove(struct pci_dev *dev)
1232 struct solos_card *card = pci_get_drvdata(dev);
1236 dev_vdbg(&dev->dev, "Freeing IRQ\n");
1237 // Disable IRQs from FPGA
1238 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1239 free_irq(dev->irq, card);
1240 tasklet_kill(&card->tlet);
1242 // iowrite32(0x01,pciregs);
1243 dev_vdbg(&dev->dev, "Unmapping PCI resource\n");
1244 pci_iounmap(dev, card->buffers);
1245 pci_iounmap(dev, card->config_regs);
1247 dev_vdbg(&dev->dev, "Releasing PCI Region\n");
1248 pci_release_regions(dev);
1249 pci_disable_device(dev);
1251 pci_set_drvdata(dev, NULL);
1253 // dev_dbg(&card->dev->dev, "fpga_remove\n");
1257 static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1258 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1262 MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1264 static struct pci_driver fpga_driver = {
1266 .id_table = fpga_pci_tbl,
1267 .probe = fpga_probe,
1268 .remove = fpga_remove,
1272 static int __init solos_pci_init(void)
1274 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1275 return pci_register_driver(&fpga_driver);
1278 static void __exit solos_pci_exit(void)
1280 pci_unregister_driver(&fpga_driver);
1281 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1284 module_init(solos_pci_init);
1285 module_exit(solos_pci_exit);