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>
43 #include <linux/slab.h>
45 #define VERSION "0.07"
46 #define PTAG "solos-pci"
48 #define CONFIG_RAM_SIZE 128
49 #define FLAGS_ADDR 0x7C
50 #define IRQ_EN_ADDR 0x78
52 #define IRQ_CLEAR 0x70
53 #define WRITE_FLASH 0x6C
55 #define FLASH_BLOCK 0x64
56 #define FLASH_BUSY 0x60
57 #define FPGA_MODE 0x5C
58 #define FLASH_MODE 0x58
59 #define GPIO_STATUS 0x54
60 #define TX_DMA_ADDR(port) (0x40 + (4 * (port)))
61 #define RX_DMA_ADDR(port) (0x30 + (4 * (port)))
63 #define DATA_RAM_SIZE 32768
65 #define OLD_BUF_SIZE 4096 /* For FPGA versions <= 2*/
66 #define FPGA_PAGE 528 /* FPGA flash page size*/
67 #define SOLOS_PAGE 512 /* Solos flash page size*/
68 #define FPGA_BLOCK (FPGA_PAGE * 8) /* FPGA flash block size*/
69 #define SOLOS_BLOCK (SOLOS_PAGE * 8) /* Solos flash block size*/
71 #define RX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2)
72 #define TX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2 + (card->buffer_size))
73 #define FLASH_BUF ((card->buffers) + 4*(card->buffer_size)*2)
75 #define RX_DMA_SIZE 2048
77 #define FPGA_VERSION(a,b) (((a) << 8) + (b))
78 #define LEGACY_BUFFERS 2
79 #define DMA_SUPPORTED 4
82 static int atmdebug = 0;
83 static int firmware_upgrade = 0;
84 static int fpga_upgrade = 0;
85 static int db_firmware_upgrade = 0;
86 static int db_fpga_upgrade = 0;
101 #define SKB_CB(skb) ((struct solos_skb_cb *)skb->cb)
104 #define PKT_COMMAND 1
110 void __iomem *config_regs;
111 void __iomem *buffers;
115 struct atm_dev *atmdev[4];
116 struct tasklet_struct tlet;
118 spinlock_t tx_queue_lock;
119 spinlock_t cli_queue_lock;
120 spinlock_t param_queue_lock;
121 struct list_head param_queue;
122 struct sk_buff_head tx_queue[4];
123 struct sk_buff_head cli_queue[4];
124 struct sk_buff *tx_skb[4];
125 struct sk_buff *rx_skb[4];
126 wait_queue_head_t param_wq;
127 wait_queue_head_t fw_wq;
135 struct list_head list;
138 struct sk_buff *response;
141 #define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
143 MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
144 MODULE_DESCRIPTION("Solos PCI driver");
145 MODULE_VERSION(VERSION);
146 MODULE_LICENSE("GPL");
147 MODULE_FIRMWARE("solos-FPGA.bin");
148 MODULE_FIRMWARE("solos-Firmware.bin");
149 MODULE_FIRMWARE("solos-db-FPGA.bin");
150 MODULE_PARM_DESC(reset, "Reset Solos chips on startup");
151 MODULE_PARM_DESC(atmdebug, "Print ATM data");
152 MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
153 MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
154 MODULE_PARM_DESC(db_firmware_upgrade, "Initiate daughter board Solos firmware upgrade");
155 MODULE_PARM_DESC(db_fpga_upgrade, "Initiate daughter board FPGA upgrade");
156 module_param(reset, int, 0444);
157 module_param(atmdebug, int, 0644);
158 module_param(firmware_upgrade, int, 0444);
159 module_param(fpga_upgrade, int, 0444);
160 module_param(db_firmware_upgrade, int, 0444);
161 module_param(db_fpga_upgrade, int, 0444);
163 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
164 struct atm_vcc *vcc);
165 static uint32_t fpga_tx(struct solos_card *);
166 static irqreturn_t solos_irq(int irq, void *dev_id);
167 static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
168 static int atm_init(struct solos_card *, struct device *);
169 static void atm_remove(struct solos_card *);
170 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
171 static void solos_bh(unsigned long);
172 static int print_buffer(struct sk_buff *buf);
174 static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
179 dev_kfree_skb_any(skb);
182 static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
185 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
186 struct solos_card *card = atmdev->dev_data;
187 struct solos_param prm;
189 struct pkt_hdr *header;
192 buflen = strlen(attr->attr.name) + 10;
194 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
196 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
200 header = (void *)skb_put(skb, sizeof(*header));
202 buflen = snprintf((void *)&header[1], buflen - 1,
203 "L%05d\n%s\n", current->pid, attr->attr.name);
204 skb_put(skb, buflen);
206 header->size = cpu_to_le16(buflen);
207 header->vpi = cpu_to_le16(0);
208 header->vci = cpu_to_le16(0);
209 header->type = cpu_to_le16(PKT_COMMAND);
211 prm.pid = current->pid;
213 prm.port = SOLOS_CHAN(atmdev);
215 spin_lock_irq(&card->param_queue_lock);
216 list_add(&prm.list, &card->param_queue);
217 spin_unlock_irq(&card->param_queue_lock);
219 fpga_queue(card, prm.port, skb, NULL);
221 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
223 spin_lock_irq(&card->param_queue_lock);
225 spin_unlock_irq(&card->param_queue_lock);
230 buflen = prm.response->len;
231 memcpy(buf, prm.response->data, buflen);
232 kfree_skb(prm.response);
237 static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t count)
240 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
241 struct solos_card *card = atmdev->dev_data;
242 struct solos_param prm;
244 struct pkt_hdr *header;
248 buflen = strlen(attr->attr.name) + 11 + count;
250 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
252 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
256 header = (void *)skb_put(skb, sizeof(*header));
258 buflen = snprintf((void *)&header[1], buflen - 1,
259 "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
261 skb_put(skb, buflen);
262 header->size = cpu_to_le16(buflen);
263 header->vpi = cpu_to_le16(0);
264 header->vci = cpu_to_le16(0);
265 header->type = cpu_to_le16(PKT_COMMAND);
267 prm.pid = current->pid;
269 prm.port = SOLOS_CHAN(atmdev);
271 spin_lock_irq(&card->param_queue_lock);
272 list_add(&prm.list, &card->param_queue);
273 spin_unlock_irq(&card->param_queue_lock);
275 fpga_queue(card, prm.port, skb, NULL);
277 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
279 spin_lock_irq(&card->param_queue_lock);
281 spin_unlock_irq(&card->param_queue_lock);
290 /* Sometimes it has a newline, sometimes it doesn't. */
291 if (skb->data[buflen - 1] == '\n')
294 if (buflen == 2 && !strncmp(skb->data, "OK", 2))
296 else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
299 /* We know we have enough space allocated for this; we allocated
301 skb->data[buflen] = 0;
303 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
312 static char *next_string(struct sk_buff *skb)
315 char *this = skb->data;
317 for (i = 0; i < skb->len; i++) {
318 if (this[i] == '\n') {
320 skb_pull(skb, i + 1);
323 if (!isprint(this[i]))
330 * Status packet has fields separated by \n, starting with a version number
331 * for the information therein. Fields are....
334 * RxBitRate (version >= 1)
335 * TxBitRate (version >= 1)
336 * State (version >= 1)
337 * LocalSNRMargin (version >= 1)
338 * LocalLineAttn (version >= 1)
340 static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
342 char *str, *end, *state_str, *snr, *attn;
343 int ver, rate_up, rate_down;
345 if (!card->atmdev[port])
348 str = next_string(skb);
352 ver = simple_strtol(str, NULL, 10);
354 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
359 str = next_string(skb);
362 if (!strcmp(str, "ERROR")) {
363 dev_dbg(&card->dev->dev, "Status packet indicated Solos error on port %d (starting up?)\n",
368 rate_down = simple_strtol(str, &end, 10);
372 str = next_string(skb);
375 rate_up = simple_strtol(str, &end, 10);
379 state_str = next_string(skb);
383 /* Anything but 'Showtime' is down */
384 if (strcmp(state_str, "Showtime")) {
385 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
386 dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
390 snr = next_string(skb);
393 attn = next_string(skb);
397 dev_info(&card->dev->dev, "Port %d: %s @%d/%d kb/s%s%s%s%s\n",
398 port, state_str, rate_down/1000, rate_up/1000,
399 snr[0]?", SNR ":"", snr, attn[0]?", Attn ":"", attn);
401 card->atmdev[port]->link_rate = rate_down / 424;
402 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
407 static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
409 struct solos_param *prm;
417 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
418 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
419 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
420 skb->data[6] != '\n')
423 cmdpid = simple_strtol(&skb->data[1], NULL, 10);
425 spin_lock_irqsave(&card->param_queue_lock, flags);
426 list_for_each_entry(prm, &card->param_queue, list) {
427 if (prm->port == port && prm->pid == cmdpid) {
430 wake_up(&card->param_wq);
435 spin_unlock_irqrestore(&card->param_queue_lock, flags);
439 static ssize_t console_show(struct device *dev, struct device_attribute *attr,
442 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
443 struct solos_card *card = atmdev->dev_data;
447 spin_lock(&card->cli_queue_lock);
448 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
449 spin_unlock(&card->cli_queue_lock);
451 return sprintf(buf, "No data.\n");
454 memcpy(buf, skb->data, len);
460 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
463 struct pkt_hdr *header;
465 if (size > (BUF_SIZE - sizeof(*header))) {
466 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
469 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
471 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
475 header = (void *)skb_put(skb, sizeof(*header));
477 header->size = cpu_to_le16(size);
478 header->vpi = cpu_to_le16(0);
479 header->vci = cpu_to_le16(0);
480 header->type = cpu_to_le16(PKT_COMMAND);
482 memcpy(skb_put(skb, size), buf, size);
484 fpga_queue(card, dev, skb, NULL);
489 static ssize_t console_store(struct device *dev, struct device_attribute *attr,
490 const char *buf, size_t count)
492 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
493 struct solos_card *card = atmdev->dev_data;
496 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
501 struct geos_gpio_attr {
502 struct device_attribute attr;
506 #define SOLOS_GPIO_ATTR(_name, _mode, _show, _store, _offset) \
507 struct geos_gpio_attr gpio_attr_##_name = { \
508 .attr = __ATTR(_name, _mode, _show, _store), \
511 static ssize_t geos_gpio_store(struct device *dev, struct device_attribute *attr,
512 const char *buf, size_t count)
514 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
515 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
516 struct solos_card *card = pci_get_drvdata(pdev);
519 if (count != 1 && (count != 2 || buf[1] != '\n'))
522 spin_lock_irq(&card->param_queue_lock);
523 data32 = ioread32(card->config_regs + GPIO_STATUS);
525 data32 |= 1 << gattr->offset;
526 iowrite32(data32, card->config_regs + GPIO_STATUS);
527 } else if (buf[0] == '0') {
528 data32 &= ~(1 << gattr->offset);
529 iowrite32(data32, card->config_regs + GPIO_STATUS);
533 spin_lock_irq(&card->param_queue_lock);
537 static ssize_t geos_gpio_show(struct device *dev, struct device_attribute *attr,
540 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
541 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
542 struct solos_card *card = pci_get_drvdata(pdev);
545 data32 = ioread32(card->config_regs + GPIO_STATUS);
546 data32 = (data32 >> gattr->offset) & 1;
548 return sprintf(buf, "%d\n", data32);
551 static ssize_t hardware_show(struct device *dev, struct device_attribute *attr,
554 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
555 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
556 struct solos_card *card = pci_get_drvdata(pdev);
559 data32 = ioread32(card->config_regs + GPIO_STATUS);
560 switch (gattr->offset) {
562 /* HardwareVersion */
563 data32 = data32 & 0x1F;
566 /* HardwareVariant */
567 data32 = (data32 >> 5) & 0x0F;
570 return sprintf(buf, "%d\n", data32);
573 static DEVICE_ATTR(console, 0644, console_show, console_store);
576 #define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
577 #define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
579 #include "solos-attrlist.c"
581 static SOLOS_GPIO_ATTR(GPIO1, 0644, geos_gpio_show, geos_gpio_store, 9);
582 static SOLOS_GPIO_ATTR(GPIO2, 0644, geos_gpio_show, geos_gpio_store, 10);
583 static SOLOS_GPIO_ATTR(GPIO3, 0644, geos_gpio_show, geos_gpio_store, 11);
584 static SOLOS_GPIO_ATTR(GPIO4, 0644, geos_gpio_show, geos_gpio_store, 12);
585 static SOLOS_GPIO_ATTR(GPIO5, 0644, geos_gpio_show, geos_gpio_store, 13);
586 static SOLOS_GPIO_ATTR(PushButton, 0444, geos_gpio_show, NULL, 14);
587 static SOLOS_GPIO_ATTR(HardwareVersion, 0444, hardware_show, NULL, 0);
588 static SOLOS_GPIO_ATTR(HardwareVariant, 0444, hardware_show, NULL, 1);
592 #define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
593 #define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
595 static struct attribute *solos_attrs[] = {
596 #include "solos-attrlist.c"
600 static struct attribute_group solos_attr_group = {
601 .attrs = solos_attrs,
602 .name = "parameters",
605 static struct attribute *gpio_attrs[] = {
606 &gpio_attr_GPIO1.attr.attr,
607 &gpio_attr_GPIO2.attr.attr,
608 &gpio_attr_GPIO3.attr.attr,
609 &gpio_attr_GPIO4.attr.attr,
610 &gpio_attr_GPIO5.attr.attr,
611 &gpio_attr_PushButton.attr.attr,
612 &gpio_attr_HardwareVersion.attr.attr,
613 &gpio_attr_HardwareVariant.attr.attr,
617 static struct attribute_group gpio_attr_group = {
622 static int flash_upgrade(struct solos_card *card, int chip)
624 const struct firmware *fw;
632 fw_name = "solos-FPGA.bin";
633 blocksize = FPGA_BLOCK;
636 fw_name = "solos-Firmware.bin";
637 blocksize = SOLOS_BLOCK;
640 if (card->fpga_version > LEGACY_BUFFERS){
641 fw_name = "solos-db-FPGA.bin";
642 blocksize = FPGA_BLOCK;
644 dev_info(&card->dev->dev, "FPGA version doesn't support"
645 " daughter board upgrades\n");
650 if (card->fpga_version > LEGACY_BUFFERS){
651 fw_name = "solos-Firmware.bin";
652 blocksize = SOLOS_BLOCK;
654 dev_info(&card->dev->dev, "FPGA version doesn't support"
655 " daughter board upgrades\n");
663 if (request_firmware(&fw, fw_name, &card->dev->dev))
666 dev_info(&card->dev->dev, "Flash upgrade starting\n");
668 numblocks = fw->size / blocksize;
669 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
670 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
672 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
673 iowrite32(1, card->config_regs + FPGA_MODE);
674 (void) ioread32(card->config_regs + FPGA_MODE);
676 /* Set mode to Chip Erase */
677 if(chip == 0 || chip == 2)
678 dev_info(&card->dev->dev, "Set FPGA Flash mode to FPGA Chip Erase\n");
679 if(chip == 1 || chip == 3)
680 dev_info(&card->dev->dev, "Set FPGA Flash mode to Solos Chip Erase\n");
681 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
684 iowrite32(1, card->config_regs + WRITE_FLASH);
685 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
687 for (offset = 0; offset < fw->size; offset += blocksize) {
690 /* Clear write flag */
691 iowrite32(0, card->config_regs + WRITE_FLASH);
693 /* Set mode to Block Write */
694 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
695 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
697 /* Copy block to buffer, swapping each 16 bits */
698 for(i = 0; i < blocksize; i += 4) {
699 uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
700 if(card->fpga_version > LEGACY_BUFFERS)
701 iowrite32(word, FLASH_BUF + i);
703 iowrite32(word, RX_BUF(card, 3) + i);
706 /* Specify block number and then trigger flash write */
707 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
708 iowrite32(1, card->config_regs + WRITE_FLASH);
709 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
712 release_firmware(fw);
713 iowrite32(0, card->config_regs + WRITE_FLASH);
714 iowrite32(0, card->config_regs + FPGA_MODE);
715 iowrite32(0, card->config_regs + FLASH_MODE);
716 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
720 static irqreturn_t solos_irq(int irq, void *dev_id)
722 struct solos_card *card = dev_id;
725 iowrite32(0, card->config_regs + IRQ_CLEAR);
727 /* If we're up and running, just kick the tasklet to process TX/RX */
729 tasklet_schedule(&card->tlet);
731 wake_up(&card->fw_wq);
733 return IRQ_RETVAL(handled);
736 void solos_bh(unsigned long card_arg)
738 struct solos_card *card = (void *)card_arg;
740 uint32_t rx_done = 0;
744 * Since fpga_tx() is going to need to read the flags under its lock,
745 * it can return them to us so that we don't have to hit PCI MMIO
746 * again for the same information
748 card_flags = fpga_tx(card);
750 for (port = 0; port < card->nr_ports; port++) {
751 if (card_flags & (0x10 << port)) {
752 struct pkt_hdr _hdr, *header;
757 if (card->using_dma) {
758 skb = card->rx_skb[port];
759 card->rx_skb[port] = NULL;
761 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
762 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
764 header = (void *)skb->data;
765 size = le16_to_cpu(header->size);
766 skb_put(skb, size + sizeof(*header));
767 skb_pull(skb, sizeof(*header));
771 rx_done |= 0x10 << port;
773 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
775 size = le16_to_cpu(header->size);
776 if (size > (card->buffer_size - sizeof(*header))){
777 dev_warn(&card->dev->dev, "Invalid buffer size\n");
781 skb = alloc_skb(size + 1, GFP_ATOMIC);
784 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
788 memcpy_fromio(skb_put(skb, size),
789 RX_BUF(card, port) + sizeof(*header),
793 dev_info(&card->dev->dev, "Received: port %d\n", port);
794 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
795 size, le16_to_cpu(header->vpi),
796 le16_to_cpu(header->vci));
800 switch (le16_to_cpu(header->type)) {
802 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
803 le16_to_cpu(header->vci));
806 dev_warn(&card->dev->dev, "Received packet for unknown VPI.VCI %d.%d on port %d\n",
807 le16_to_cpu(header->vpi), le16_to_cpu(header->vci),
809 dev_kfree_skb_any(skb);
812 atm_charge(vcc, skb->truesize);
814 atomic_inc(&vcc->stats->rx);
818 if (process_status(card, port, skb) &&
820 dev_warn(&card->dev->dev, "Bad status packet of %d bytes on port %d:\n", skb->len, port);
823 dev_kfree_skb_any(skb);
827 default: /* FIXME: Not really, surely? */
828 if (process_command(card, port, skb))
830 spin_lock(&card->cli_queue_lock);
831 if (skb_queue_len(&card->cli_queue[port]) > 10) {
833 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
835 dev_kfree_skb_any(skb);
837 skb_queue_tail(&card->cli_queue[port], skb);
838 spin_unlock(&card->cli_queue_lock);
842 /* Allocate RX skbs for any ports which need them */
843 if (card->using_dma && card->atmdev[port] &&
844 !card->rx_skb[port]) {
845 struct sk_buff *skb = alloc_skb(RX_DMA_SIZE, GFP_ATOMIC);
847 SKB_CB(skb)->dma_addr =
848 pci_map_single(card->dev, skb->data,
849 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
850 iowrite32(SKB_CB(skb)->dma_addr,
851 card->config_regs + RX_DMA_ADDR(port));
852 card->rx_skb[port] = skb;
855 dev_warn(&card->dev->dev, "Failed to allocate RX skb");
857 /* We'll have to try again later */
858 tasklet_schedule(&card->tlet);
863 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
868 static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
870 struct hlist_head *head;
871 struct atm_vcc *vcc = NULL;
872 struct hlist_node *node;
875 read_lock(&vcc_sklist_lock);
876 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
877 sk_for_each(s, node, head) {
879 if (vcc->dev == dev && vcc->vci == vci &&
880 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
881 test_bit(ATM_VF_READY, &vcc->flags))
886 read_unlock(&vcc_sklist_lock);
890 static int popen(struct atm_vcc *vcc)
892 struct solos_card *card = vcc->dev->dev_data;
894 struct pkt_hdr *header;
896 if (vcc->qos.aal != ATM_AAL5) {
897 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
902 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
905 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
908 header = (void *)skb_put(skb, sizeof(*header));
910 header->size = cpu_to_le16(0);
911 header->vpi = cpu_to_le16(vcc->vpi);
912 header->vci = cpu_to_le16(vcc->vci);
913 header->type = cpu_to_le16(PKT_POPEN);
915 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
917 set_bit(ATM_VF_ADDR, &vcc->flags);
918 set_bit(ATM_VF_READY, &vcc->flags);
923 static void pclose(struct atm_vcc *vcc)
925 struct solos_card *card = vcc->dev->dev_data;
926 unsigned char port = SOLOS_CHAN(vcc->dev);
927 struct sk_buff *skb, *tmpskb;
928 struct pkt_hdr *header;
930 /* Remove any yet-to-be-transmitted packets from the pending queue */
931 spin_lock(&card->tx_queue_lock);
932 skb_queue_walk_safe(&card->tx_queue[port], skb, tmpskb) {
933 if (SKB_CB(skb)->vcc == vcc) {
934 skb_unlink(skb, &card->tx_queue[port]);
938 spin_unlock(&card->tx_queue_lock);
940 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
942 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
945 header = (void *)skb_put(skb, sizeof(*header));
947 header->size = cpu_to_le16(0);
948 header->vpi = cpu_to_le16(vcc->vpi);
949 header->vci = cpu_to_le16(vcc->vci);
950 header->type = cpu_to_le16(PKT_PCLOSE);
953 fpga_queue(card, port, skb, NULL);
955 if (!wait_event_timeout(card->param_wq, !skb_shared(skb), 5 * HZ))
956 dev_warn(&card->dev->dev,
957 "Timeout waiting for VCC close on port %d\n", port);
961 /* Hold up vcc_destroy_socket() (our caller) until solos_bh() in the
962 tasklet has finished processing any incoming packets (and, more to
963 the point, using the vcc pointer). */
964 tasklet_unlock_wait(&card->tlet);
966 clear_bit(ATM_VF_ADDR, &vcc->flags);
971 static int print_buffer(struct sk_buff *buf)
978 for (i = 0; i < len; i++){
980 sprintf(msg, "%02X: ", i);
982 sprintf(item,"%02X ",*(buf->data + i));
987 printk(KERN_DEBUG "%s", msg);
993 printk(KERN_DEBUG "%s", msg);
995 printk(KERN_DEBUG "\n");
1000 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
1001 struct atm_vcc *vcc)
1004 unsigned long flags;
1006 SKB_CB(skb)->vcc = vcc;
1008 spin_lock_irqsave(&card->tx_queue_lock, flags);
1009 old_len = skb_queue_len(&card->tx_queue[port]);
1010 skb_queue_tail(&card->tx_queue[port], skb);
1012 card->tx_mask |= (1 << port);
1013 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
1015 /* Theoretically we could just schedule the tasklet here, but
1016 that introduces latency we don't want -- it's noticeable */
1021 static uint32_t fpga_tx(struct solos_card *card)
1023 uint32_t tx_pending, card_flags;
1024 uint32_t tx_started = 0;
1025 struct sk_buff *skb;
1026 struct atm_vcc *vcc;
1028 unsigned long flags;
1030 spin_lock_irqsave(&card->tx_lock, flags);
1032 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
1034 * The queue lock is required for _writing_ to tx_mask, but we're
1035 * OK to read it here without locking. The only potential update
1036 * that we could race with is in fpga_queue() where it sets a bit
1037 * for a new port... but it's going to call this function again if
1038 * it's doing that, anyway.
1040 tx_pending = card->tx_mask & ~card_flags;
1042 for (port = 0; tx_pending; tx_pending >>= 1, port++) {
1043 if (tx_pending & 1) {
1044 struct sk_buff *oldskb = card->tx_skb[port];
1046 pci_unmap_single(card->dev, SKB_CB(oldskb)->dma_addr,
1047 oldskb->len, PCI_DMA_TODEVICE);
1048 card->tx_skb[port] = NULL;
1050 spin_lock(&card->tx_queue_lock);
1051 skb = skb_dequeue(&card->tx_queue[port]);
1053 card->tx_mask &= ~(1 << port);
1054 spin_unlock(&card->tx_queue_lock);
1056 if (skb && !card->using_dma) {
1057 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
1058 tx_started |= 1 << port;
1059 oldskb = skb; /* We're done with this skb already */
1060 } else if (skb && card->using_dma) {
1061 SKB_CB(skb)->dma_addr = pci_map_single(card->dev, skb->data,
1062 skb->len, PCI_DMA_TODEVICE);
1063 card->tx_skb[port] = skb;
1064 iowrite32(SKB_CB(skb)->dma_addr,
1065 card->config_regs + TX_DMA_ADDR(port));
1071 /* Clean up and free oldskb now it's gone */
1073 struct pkt_hdr *header = (void *)oldskb->data;
1074 int size = le16_to_cpu(header->size);
1076 skb_pull(oldskb, sizeof(*header));
1077 dev_info(&card->dev->dev, "Transmitted: port %d\n",
1079 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
1080 size, le16_to_cpu(header->vpi),
1081 le16_to_cpu(header->vci));
1082 print_buffer(oldskb);
1085 vcc = SKB_CB(oldskb)->vcc;
1088 atomic_inc(&vcc->stats->tx);
1089 solos_pop(vcc, oldskb);
1091 dev_kfree_skb_irq(oldskb);
1092 wake_up(&card->param_wq);
1096 /* For non-DMA TX, write the 'TX start' bit for all four ports simultaneously */
1098 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
1100 spin_unlock_irqrestore(&card->tx_lock, flags);
1104 static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
1106 struct solos_card *card = vcc->dev->dev_data;
1107 struct pkt_hdr *header;
1111 if (pktlen > (BUF_SIZE - sizeof(*header))) {
1112 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
1113 solos_pop(vcc, skb);
1117 if (!skb_clone_writable(skb, sizeof(*header))) {
1121 if (skb_headroom(skb) < sizeof(*header))
1122 expand_by = sizeof(*header) - skb_headroom(skb);
1124 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
1126 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
1127 solos_pop(vcc, skb);
1132 header = (void *)skb_push(skb, sizeof(*header));
1134 /* This does _not_ include the size of the header */
1135 header->size = cpu_to_le16(pktlen);
1136 header->vpi = cpu_to_le16(vcc->vpi);
1137 header->vci = cpu_to_le16(vcc->vci);
1138 header->type = cpu_to_le16(PKT_DATA);
1140 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1145 static struct atmdev_ops fpga_ops = {
1157 .owner = THIS_MODULE
1160 static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
1164 uint8_t major_ver, minor_ver;
1166 struct solos_card *card;
1168 card = kzalloc(sizeof(*card), GFP_KERNEL);
1173 init_waitqueue_head(&card->fw_wq);
1174 init_waitqueue_head(&card->param_wq);
1176 err = pci_enable_device(dev);
1178 dev_warn(&dev->dev, "Failed to enable PCI device\n");
1182 err = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
1184 dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
1188 err = pci_request_regions(dev, "solos");
1190 dev_warn(&dev->dev, "Failed to request regions\n");
1194 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1195 if (!card->config_regs) {
1196 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
1197 goto out_release_regions;
1199 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1200 if (!card->buffers) {
1201 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
1202 goto out_unmap_config;
1206 iowrite32(1, card->config_regs + FPGA_MODE);
1207 data32 = ioread32(card->config_regs + FPGA_MODE);
1209 iowrite32(0, card->config_regs + FPGA_MODE);
1210 data32 = ioread32(card->config_regs + FPGA_MODE);
1213 data32 = ioread32(card->config_regs + FPGA_VER);
1214 fpga_ver = (data32 & 0x0000FFFF);
1215 major_ver = ((data32 & 0xFF000000) >> 24);
1216 minor_ver = ((data32 & 0x00FF0000) >> 16);
1217 card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
1218 if (card->fpga_version > LEGACY_BUFFERS)
1219 card->buffer_size = BUF_SIZE;
1221 card->buffer_size = OLD_BUF_SIZE;
1222 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1223 major_ver, minor_ver, fpga_ver);
1225 if (fpga_ver < 37 && (fpga_upgrade || firmware_upgrade ||
1226 db_fpga_upgrade || db_firmware_upgrade)) {
1228 "FPGA too old; cannot upgrade flash. Use JTAG.\n");
1229 fpga_upgrade = firmware_upgrade = 0;
1230 db_fpga_upgrade = db_firmware_upgrade = 0;
1233 if (card->fpga_version >= DMA_SUPPORTED) {
1234 pci_set_master(dev);
1235 card->using_dma = 1;
1237 card->using_dma = 0;
1238 /* Set RX empty flag for all ports */
1239 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1242 data32 = ioread32(card->config_regs + PORTS);
1243 card->nr_ports = (data32 & 0x000000FF);
1245 pci_set_drvdata(dev, card);
1247 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1248 spin_lock_init(&card->tx_lock);
1249 spin_lock_init(&card->tx_queue_lock);
1250 spin_lock_init(&card->cli_queue_lock);
1251 spin_lock_init(&card->param_queue_lock);
1252 INIT_LIST_HEAD(&card->param_queue);
1254 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
1257 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
1258 goto out_unmap_both;
1261 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1264 flash_upgrade(card, 0);
1266 if (firmware_upgrade)
1267 flash_upgrade(card, 1);
1269 if (db_fpga_upgrade)
1270 flash_upgrade(card, 2);
1272 if (db_firmware_upgrade)
1273 flash_upgrade(card, 3);
1275 err = atm_init(card, &dev->dev);
1279 if (card->fpga_version >= DMA_SUPPORTED &&
1280 sysfs_create_group(&card->dev->dev.kobj, &gpio_attr_group))
1281 dev_err(&card->dev->dev, "Could not register parameter group for GPIOs\n");
1286 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1287 free_irq(dev->irq, card);
1288 tasklet_kill(&card->tlet);
1291 pci_set_drvdata(dev, NULL);
1292 pci_iounmap(dev, card->buffers);
1294 pci_iounmap(dev, card->config_regs);
1295 out_release_regions:
1296 pci_release_regions(dev);
1302 static int atm_init(struct solos_card *card, struct device *parent)
1306 for (i = 0; i < card->nr_ports; i++) {
1307 struct sk_buff *skb;
1308 struct pkt_hdr *header;
1310 skb_queue_head_init(&card->tx_queue[i]);
1311 skb_queue_head_init(&card->cli_queue[i]);
1313 card->atmdev[i] = atm_dev_register("solos-pci", parent, &fpga_ops, -1, NULL);
1314 if (!card->atmdev[i]) {
1315 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1319 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1320 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
1321 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1322 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
1324 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1326 card->atmdev[i]->ci_range.vpi_bits = 8;
1327 card->atmdev[i]->ci_range.vci_bits = 16;
1328 card->atmdev[i]->dev_data = card;
1329 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
1330 atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_FOUND);
1332 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
1334 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1338 header = (void *)skb_put(skb, sizeof(*header));
1340 header->size = cpu_to_le16(0);
1341 header->vpi = cpu_to_le16(0);
1342 header->vci = cpu_to_le16(0);
1343 header->type = cpu_to_le16(PKT_STATUS);
1345 fpga_queue(card, i, skb, NULL);
1350 static void atm_remove(struct solos_card *card)
1354 for (i = 0; i < card->nr_ports; i++) {
1355 if (card->atmdev[i]) {
1356 struct sk_buff *skb;
1358 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
1360 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
1361 atm_dev_deregister(card->atmdev[i]);
1363 skb = card->rx_skb[i];
1365 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1366 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
1369 skb = card->tx_skb[i];
1371 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1372 skb->len, PCI_DMA_TODEVICE);
1375 while ((skb = skb_dequeue(&card->tx_queue[i])))
1382 static void fpga_remove(struct pci_dev *dev)
1384 struct solos_card *card = pci_get_drvdata(dev);
1387 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1390 iowrite32(1, card->config_regs + FPGA_MODE);
1391 (void)ioread32(card->config_regs + FPGA_MODE);
1393 if (card->fpga_version >= DMA_SUPPORTED)
1394 sysfs_remove_group(&card->dev->dev.kobj, &gpio_attr_group);
1398 free_irq(dev->irq, card);
1399 tasklet_kill(&card->tlet);
1401 /* Release device from reset */
1402 iowrite32(0, card->config_regs + FPGA_MODE);
1403 (void)ioread32(card->config_regs + FPGA_MODE);
1405 pci_iounmap(dev, card->buffers);
1406 pci_iounmap(dev, card->config_regs);
1408 pci_release_regions(dev);
1409 pci_disable_device(dev);
1411 pci_set_drvdata(dev, NULL);
1415 static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1416 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1420 MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1422 static struct pci_driver fpga_driver = {
1424 .id_table = fpga_pci_tbl,
1425 .probe = fpga_probe,
1426 .remove = fpga_remove,
1430 static int __init solos_pci_init(void)
1432 BUILD_BUG_ON(sizeof(struct solos_skb_cb) > sizeof(((struct sk_buff *)0)->cb));
1434 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1435 return pci_register_driver(&fpga_driver);
1438 static void __exit solos_pci_exit(void)
1440 pci_unregister_driver(&fpga_driver);
1441 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1444 module_init(solos_pci_init);
1445 module_exit(solos_pci_exit);