solos: Fix under-allocation of skb size for get/set parameters
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / atm / solos-pci.c
blob2dca5ffc806314eeb040bcbfdec4917e69445acb
1 /*
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.
24 #define DEBUG
25 #define VERBOSE_DEBUG
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
50 #define FPGA_VER 0x74
51 #define IRQ_CLEAR 0x70
52 #define WRITE_FLASH 0x6C
53 #define PORTS 0x68
54 #define FLASH_BLOCK 0x64
55 #define FLASH_BUSY 0x60
56 #define FPGA_MODE 0x5C
57 #define FLASH_MODE 0x58
59 #define DATA_RAM_SIZE 32768
60 #define BUF_SIZE 4096
61 #define FPGA_PAGE 528 /* FPGA flash page size*/
62 #define SOLOS_PAGE 512 /* Solos flash page size*/
63 #define FPGA_BLOCK (FPGA_PAGE * 8) /* FPGA flash block size*/
64 #define SOLOS_BLOCK (SOLOS_PAGE * 8) /* Solos flash block size*/
66 #define RX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2)
67 #define TX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2 + BUF_SIZE)
69 static int debug = 0;
70 static int atmdebug = 0;
71 static int firmware_upgrade = 0;
72 static int fpga_upgrade = 0;
74 struct pkt_hdr {
75 __le16 size;
76 __le16 vpi;
77 __le16 vci;
78 __le16 type;
81 #define PKT_DATA 0
82 #define PKT_COMMAND 1
83 #define PKT_POPEN 3
84 #define PKT_PCLOSE 4
85 #define PKT_STATUS 5
87 struct solos_card {
88 void __iomem *config_regs;
89 void __iomem *buffers;
90 int nr_ports;
91 struct pci_dev *dev;
92 struct atm_dev *atmdev[4];
93 struct tasklet_struct tlet;
94 spinlock_t tx_lock;
95 spinlock_t tx_queue_lock;
96 spinlock_t cli_queue_lock;
97 spinlock_t param_queue_lock;
98 struct list_head param_queue;
99 struct sk_buff_head tx_queue[4];
100 struct sk_buff_head cli_queue[4];
101 wait_queue_head_t param_wq;
102 wait_queue_head_t fw_wq;
106 struct solos_param {
107 struct list_head list;
108 pid_t pid;
109 int port;
110 struct sk_buff *response;
111 wait_queue_head_t wq;
114 #define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
116 MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
117 MODULE_DESCRIPTION("Solos PCI driver");
118 MODULE_VERSION(VERSION);
119 MODULE_LICENSE("GPL");
120 MODULE_PARM_DESC(debug, "Enable Loopback");
121 MODULE_PARM_DESC(atmdebug, "Print ATM data");
122 MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
123 MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
124 module_param(debug, int, 0444);
125 module_param(atmdebug, int, 0644);
126 module_param(firmware_upgrade, int, 0444);
127 module_param(fpga_upgrade, int, 0444);
129 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
130 struct atm_vcc *vcc);
131 static int fpga_tx(struct solos_card *);
132 static irqreturn_t solos_irq(int irq, void *dev_id);
133 static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
134 static int list_vccs(int vci);
135 static void release_vccs(struct atm_dev *dev);
136 static int atm_init(struct solos_card *);
137 static void atm_remove(struct solos_card *);
138 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
139 static void solos_bh(unsigned long);
140 static int print_buffer(struct sk_buff *buf);
142 static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
144 if (vcc->pop)
145 vcc->pop(vcc, skb);
146 else
147 dev_kfree_skb_any(skb);
150 static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
151 char *buf)
153 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
154 struct solos_card *card = atmdev->dev_data;
155 struct solos_param prm;
156 struct sk_buff *skb;
157 struct pkt_hdr *header;
158 int buflen;
160 buflen = strlen(attr->attr.name) + 10;
162 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
163 if (!skb) {
164 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
165 return -ENOMEM;
168 header = (void *)skb_put(skb, sizeof(*header));
170 buflen = snprintf((void *)&header[1], buflen - 1,
171 "L%05d\n%s\n", current->pid, attr->attr.name);
172 skb_put(skb, buflen);
174 header->size = cpu_to_le16(buflen);
175 header->vpi = cpu_to_le16(0);
176 header->vci = cpu_to_le16(0);
177 header->type = cpu_to_le16(PKT_COMMAND);
179 prm.pid = current->pid;
180 prm.response = NULL;
181 prm.port = SOLOS_CHAN(atmdev);
183 spin_lock_irq(&card->param_queue_lock);
184 list_add(&prm.list, &card->param_queue);
185 spin_unlock_irq(&card->param_queue_lock);
187 fpga_queue(card, prm.port, skb, NULL);
189 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
191 spin_lock_irq(&card->param_queue_lock);
192 list_del(&prm.list);
193 spin_unlock_irq(&card->param_queue_lock);
195 if (!prm.response)
196 return -EIO;
198 buflen = prm.response->len;
199 memcpy(buf, prm.response->data, buflen);
200 kfree_skb(prm.response);
202 return buflen;
205 static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
206 const char *buf, size_t count)
208 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
209 struct solos_card *card = atmdev->dev_data;
210 struct solos_param prm;
211 struct sk_buff *skb;
212 struct pkt_hdr *header;
213 int buflen;
214 ssize_t ret;
216 buflen = strlen(attr->attr.name) + 11 + count;
218 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
219 if (!skb) {
220 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
221 return -ENOMEM;
224 header = (void *)skb_put(skb, sizeof(*header));
226 buflen = snprintf((void *)&header[1], buflen - 1,
227 "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
229 skb_put(skb, buflen);
230 header->size = cpu_to_le16(buflen);
231 header->vpi = cpu_to_le16(0);
232 header->vci = cpu_to_le16(0);
233 header->type = cpu_to_le16(PKT_COMMAND);
235 prm.pid = current->pid;
236 prm.response = NULL;
237 prm.port = SOLOS_CHAN(atmdev);
239 spin_lock_irq(&card->param_queue_lock);
240 list_add(&prm.list, &card->param_queue);
241 spin_unlock_irq(&card->param_queue_lock);
243 fpga_queue(card, prm.port, skb, NULL);
245 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
247 spin_lock_irq(&card->param_queue_lock);
248 list_del(&prm.list);
249 spin_unlock_irq(&card->param_queue_lock);
251 skb = prm.response;
253 if (!skb)
254 return -EIO;
256 buflen = skb->len;
258 /* Sometimes it has a newline, sometimes it doesn't. */
259 if (skb->data[buflen - 1] == '\n')
260 buflen--;
262 if (buflen == 2 && !strncmp(skb->data, "OK", 2))
263 ret = count;
264 else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
265 ret = -EIO;
266 else {
267 /* We know we have enough space allocated for this; we allocated
268 it ourselves */
269 skb->data[buflen] = 0;
271 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
272 skb->data);
273 ret = -EIO;
275 kfree_skb(skb);
277 return ret;
280 static char *next_string(struct sk_buff *skb)
282 int i = 0;
283 char *this = skb->data;
285 while (i < skb->len) {
286 if (this[i] == '\n') {
287 this[i] = 0;
288 skb_pull(skb, i);
289 return this;
292 return NULL;
296 * Status packet has fields separated by \n, starting with a version number
297 * for the information therein. Fields are....
299 * packet version
300 * TxBitRate (version >= 1)
301 * RxBitRate (version >= 1)
302 * State (version >= 1)
304 static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
306 char *str, *end, *state_str;
307 int ver, rate_up, rate_down, state, snr, attn;
309 if (!card->atmdev[port])
310 return -ENODEV;
312 str = next_string(skb);
313 if (!str)
314 return -EIO;
316 ver = simple_strtol(str, NULL, 10);
317 if (ver < 1) {
318 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
319 ver);
320 return -EIO;
323 str = next_string(skb);
324 rate_up = simple_strtol(str, &end, 10);
325 if (*end)
326 return -EIO;
328 str = next_string(skb);
329 rate_down = simple_strtol(str, &end, 10);
330 if (*end)
331 return -EIO;
333 state_str = next_string(skb);
334 if (!strcmp(state_str, "Showtime"))
335 state = ATM_PHY_SIG_FOUND;
336 else {
337 state = ATM_PHY_SIG_LOST;
338 release_vccs(card->atmdev[port]);
341 str = next_string(skb);
342 snr = simple_strtol(str, &end, 10);
343 if (*end)
344 return -EIO;
346 str = next_string(skb);
347 attn = simple_strtol(str, &end, 10);
348 if (*end)
349 return -EIO;
351 if (state == ATM_PHY_SIG_LOST && !rate_up && !rate_down)
352 dev_info(&card->dev->dev, "Port %d ATM state: %s\n",
353 port, state_str);
354 else
355 dev_info(&card->dev->dev, "Port %d ATM state: %s (%d/%d kb/s, SNR %ddB, Attn %ddB)\n",
356 port, state_str, rate_up/1000, rate_down/1000,
357 snr, attn);
359 card->atmdev[port]->link_rate = rate_down;
360 card->atmdev[port]->signal = state;
362 return 0;
365 static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
367 struct solos_param *prm;
368 unsigned long flags;
369 int cmdpid;
370 int found = 0;
372 if (skb->len < 7)
373 return 0;
375 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
376 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
377 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
378 skb->data[6] != '\n')
379 return 0;
381 cmdpid = simple_strtol(&skb->data[1], NULL, 10);
383 spin_lock_irqsave(&card->param_queue_lock, flags);
384 list_for_each_entry(prm, &card->param_queue, list) {
385 if (prm->port == port && prm->pid == cmdpid) {
386 prm->response = skb;
387 skb_pull(skb, 7);
388 wake_up(&card->param_wq);
389 found = 1;
390 break;
393 spin_unlock_irqrestore(&card->param_queue_lock, flags);
394 return found;
397 static ssize_t console_show(struct device *dev, struct device_attribute *attr,
398 char *buf)
400 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
401 struct solos_card *card = atmdev->dev_data;
402 struct sk_buff *skb;
404 spin_lock(&card->cli_queue_lock);
405 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
406 spin_unlock(&card->cli_queue_lock);
407 if(skb == NULL)
408 return sprintf(buf, "No data.\n");
410 memcpy(buf, skb->data, skb->len);
411 dev_dbg(&card->dev->dev, "len: %d\n", skb->len);
413 kfree_skb(skb);
414 return skb->len;
417 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
419 struct sk_buff *skb;
420 struct pkt_hdr *header;
422 // dev_dbg(&card->dev->dev, "size: %d\n", size);
424 if (size > (BUF_SIZE - sizeof(*header))) {
425 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
426 return 0;
428 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
429 if (!skb) {
430 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
431 return 0;
434 header = (void *)skb_put(skb, sizeof(*header));
436 header->size = cpu_to_le16(size);
437 header->vpi = cpu_to_le16(0);
438 header->vci = cpu_to_le16(0);
439 header->type = cpu_to_le16(PKT_COMMAND);
441 memcpy(skb_put(skb, size), buf, size);
443 fpga_queue(card, dev, skb, NULL);
445 return 0;
448 static ssize_t console_store(struct device *dev, struct device_attribute *attr,
449 const char *buf, size_t count)
451 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
452 struct solos_card *card = atmdev->dev_data;
453 int err;
455 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
457 return err?:count;
460 static DEVICE_ATTR(console, 0644, console_show, console_store);
463 #define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
464 #define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
466 #include "solos-attrlist.c"
468 #undef SOLOS_ATTR_RO
469 #undef SOLOS_ATTR_RW
471 #define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
472 #define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
474 static struct attribute *solos_attrs[] = {
475 #include "solos-attrlist.c"
476 NULL
479 static struct attribute_group solos_attr_group = {
480 .attrs = solos_attrs,
481 .name = "parameters",
484 static int flash_upgrade(struct solos_card *card, int chip)
486 const struct firmware *fw;
487 const char *fw_name;
488 uint32_t data32 = 0;
489 int blocksize = 0;
490 int numblocks = 0;
491 int offset;
493 if (chip == 0) {
494 fw_name = "solos-FPGA.bin";
495 blocksize = FPGA_BLOCK;
496 } else {
497 fw_name = "solos-Firmware.bin";
498 blocksize = SOLOS_BLOCK;
501 if (request_firmware(&fw, fw_name, &card->dev->dev))
502 return -ENOENT;
504 dev_info(&card->dev->dev, "Flash upgrade starting\n");
506 numblocks = fw->size / blocksize;
507 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
508 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
510 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
511 iowrite32(1, card->config_regs + FPGA_MODE);
512 data32 = ioread32(card->config_regs + FPGA_MODE);
514 /* Set mode to Chip Erase */
515 dev_info(&card->dev->dev, "Set FPGA Flash mode to %s Chip Erase\n",
516 chip?"Solos":"FPGA");
517 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
520 iowrite32(1, card->config_regs + WRITE_FLASH);
521 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
523 for (offset = 0; offset < fw->size; offset += blocksize) {
524 int i;
526 /* Clear write flag */
527 iowrite32(0, card->config_regs + WRITE_FLASH);
529 /* Set mode to Block Write */
530 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
531 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
533 /* Copy block to buffer, swapping each 16 bits */
534 for(i = 0; i < blocksize; i += 4) {
535 uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
536 iowrite32(word, RX_BUF(card, 3) + i);
539 /* Specify block number and then trigger flash write */
540 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
541 iowrite32(1, card->config_regs + WRITE_FLASH);
542 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
545 release_firmware(fw);
546 iowrite32(0, card->config_regs + WRITE_FLASH);
547 iowrite32(0, card->config_regs + FPGA_MODE);
548 iowrite32(0, card->config_regs + FLASH_MODE);
549 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
550 return 0;
553 static irqreturn_t solos_irq(int irq, void *dev_id)
555 struct solos_card *card = dev_id;
556 int handled = 1;
558 //ACK IRQ
559 iowrite32(0, card->config_regs + IRQ_CLEAR);
560 //Disable IRQs from FPGA
561 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
563 if (card->atmdev[0])
564 tasklet_schedule(&card->tlet);
565 else
566 wake_up(&card->fw_wq);
568 //Enable IRQs from FPGA
569 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
570 return IRQ_RETVAL(handled);
573 void solos_bh(unsigned long card_arg)
575 struct solos_card *card = (void *)card_arg;
576 int port;
577 uint32_t card_flags;
578 uint32_t tx_mask;
579 uint32_t rx_done = 0;
581 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
583 /* The TX bits are set if the channel is busy; clear if not. We want to
584 invoke fpga_tx() unless _all_ the bits for active channels are set */
585 tx_mask = (1 << card->nr_ports) - 1;
586 if ((card_flags & tx_mask) != tx_mask)
587 fpga_tx(card);
589 for (port = 0; port < card->nr_ports; port++) {
590 if (card_flags & (0x10 << port)) {
591 struct pkt_hdr header;
592 struct sk_buff *skb;
593 struct atm_vcc *vcc;
594 int size;
596 rx_done |= 0x10 << port;
598 memcpy_fromio(&header, RX_BUF(card, port), sizeof(header));
600 size = le16_to_cpu(header.size);
602 skb = alloc_skb(size + 1, GFP_ATOMIC);
603 if (!skb) {
604 if (net_ratelimit())
605 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
606 continue;
609 memcpy_fromio(skb_put(skb, size),
610 RX_BUF(card, port) + sizeof(header),
611 size);
613 if (atmdebug) {
614 dev_info(&card->dev->dev, "Received: device %d\n", port);
615 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
616 size, le16_to_cpu(header.vpi),
617 le16_to_cpu(header.vci));
618 print_buffer(skb);
621 switch (le16_to_cpu(header.type)) {
622 case PKT_DATA:
623 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header.vpi),
624 le16_to_cpu(header.vci));
625 if (!vcc) {
626 if (net_ratelimit())
627 dev_warn(&card->dev->dev, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
628 le16_to_cpu(header.vci), le16_to_cpu(header.vpi),
629 port);
630 continue;
632 atm_charge(vcc, skb->truesize);
633 vcc->push(vcc, skb);
634 atomic_inc(&vcc->stats->rx);
635 break;
637 case PKT_STATUS:
638 process_status(card, port, skb);
639 dev_kfree_skb(skb);
640 break;
642 case PKT_COMMAND:
643 default: /* FIXME: Not really, surely? */
644 if (process_command(card, port, skb))
645 break;
646 spin_lock(&card->cli_queue_lock);
647 if (skb_queue_len(&card->cli_queue[port]) > 10) {
648 if (net_ratelimit())
649 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
650 port);
651 } else
652 skb_queue_tail(&card->cli_queue[port], skb);
653 spin_unlock(&card->cli_queue_lock);
654 break;
658 if (rx_done)
659 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
661 return;
664 static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
666 struct hlist_head *head;
667 struct atm_vcc *vcc = NULL;
668 struct hlist_node *node;
669 struct sock *s;
671 read_lock(&vcc_sklist_lock);
672 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
673 sk_for_each(s, node, head) {
674 vcc = atm_sk(s);
675 if (vcc->dev == dev && vcc->vci == vci &&
676 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE)
677 goto out;
679 vcc = NULL;
680 out:
681 read_unlock(&vcc_sklist_lock);
682 return vcc;
685 static int list_vccs(int vci)
687 struct hlist_head *head;
688 struct atm_vcc *vcc;
689 struct hlist_node *node;
690 struct sock *s;
691 int num_found = 0;
692 int i;
694 read_lock(&vcc_sklist_lock);
695 if (vci != 0){
696 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
697 sk_for_each(s, node, head) {
698 num_found ++;
699 vcc = atm_sk(s);
700 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
701 vcc->dev->number,
702 vcc->vpi,
703 vcc->vci);
705 } else {
706 for(i = 0; i < VCC_HTABLE_SIZE; i++){
707 head = &vcc_hash[i];
708 sk_for_each(s, node, head) {
709 num_found ++;
710 vcc = atm_sk(s);
711 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
712 vcc->dev->number,
713 vcc->vpi,
714 vcc->vci);
718 read_unlock(&vcc_sklist_lock);
719 return num_found;
722 static void release_vccs(struct atm_dev *dev)
724 int i;
726 write_lock_irq(&vcc_sklist_lock);
727 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
728 struct hlist_head *head = &vcc_hash[i];
729 struct hlist_node *node, *tmp;
730 struct sock *s;
731 struct atm_vcc *vcc;
733 sk_for_each_safe(s, node, tmp, head) {
734 vcc = atm_sk(s);
735 if (vcc->dev == dev) {
736 vcc_release_async(vcc, -EPIPE);
737 sk_del_node_init(s);
741 write_unlock_irq(&vcc_sklist_lock);
745 static int popen(struct atm_vcc *vcc)
747 struct solos_card *card = vcc->dev->dev_data;
748 struct sk_buff *skb;
749 struct pkt_hdr *header;
751 if (vcc->qos.aal != ATM_AAL5) {
752 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
753 vcc->qos.aal);
754 return -EINVAL;
757 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
758 if (!skb && net_ratelimit()) {
759 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
760 return -ENOMEM;
762 header = (void *)skb_put(skb, sizeof(*header));
764 header->size = cpu_to_le16(0);
765 header->vpi = cpu_to_le16(vcc->vpi);
766 header->vci = cpu_to_le16(vcc->vci);
767 header->type = cpu_to_le16(PKT_POPEN);
769 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
771 // dev_dbg(&card->dev->dev, "Open for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
772 set_bit(ATM_VF_ADDR, &vcc->flags); // accept the vpi / vci
773 set_bit(ATM_VF_READY, &vcc->flags);
774 list_vccs(0);
777 return 0;
780 static void pclose(struct atm_vcc *vcc)
782 struct solos_card *card = vcc->dev->dev_data;
783 struct sk_buff *skb;
784 struct pkt_hdr *header;
786 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
787 if (!skb) {
788 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
789 return;
791 header = (void *)skb_put(skb, sizeof(*header));
793 header->size = cpu_to_le16(0);
794 header->vpi = cpu_to_le16(vcc->vpi);
795 header->vci = cpu_to_le16(vcc->vci);
796 header->type = cpu_to_le16(PKT_PCLOSE);
798 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
800 // dev_dbg(&card->dev->dev, "Close for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
802 clear_bit(ATM_VF_ADDR, &vcc->flags);
803 clear_bit(ATM_VF_READY, &vcc->flags);
805 return;
808 static int print_buffer(struct sk_buff *buf)
810 int len,i;
811 char msg[500];
812 char item[10];
814 len = buf->len;
815 for (i = 0; i < len; i++){
816 if(i % 8 == 0)
817 sprintf(msg, "%02X: ", i);
819 sprintf(item,"%02X ",*(buf->data + i));
820 strcat(msg, item);
821 if(i % 8 == 7) {
822 sprintf(item, "\n");
823 strcat(msg, item);
824 printk(KERN_DEBUG "%s", msg);
827 if (i % 8 != 0) {
828 sprintf(item, "\n");
829 strcat(msg, item);
830 printk(KERN_DEBUG "%s", msg);
832 printk(KERN_DEBUG "\n");
834 return 0;
837 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
838 struct atm_vcc *vcc)
840 int old_len;
842 *(void **)skb->cb = vcc;
844 spin_lock(&card->tx_queue_lock);
845 old_len = skb_queue_len(&card->tx_queue[port]);
846 skb_queue_tail(&card->tx_queue[port], skb);
847 spin_unlock(&card->tx_queue_lock);
849 /* If TX might need to be started, do so */
850 if (!old_len)
851 fpga_tx(card);
854 static int fpga_tx(struct solos_card *card)
856 uint32_t tx_pending;
857 uint32_t tx_started = 0;
858 struct sk_buff *skb;
859 struct atm_vcc *vcc;
860 unsigned char port;
861 unsigned long flags;
863 spin_lock_irqsave(&card->tx_lock, flags);
865 tx_pending = ioread32(card->config_regs + FLAGS_ADDR);
867 dev_vdbg(&card->dev->dev, "TX Flags are %X\n", tx_pending);
869 for (port = 0; port < card->nr_ports; port++) {
870 if (card->atmdev[port] && !(tx_pending & (1 << port))) {
872 spin_lock(&card->tx_queue_lock);
873 skb = skb_dequeue(&card->tx_queue[port]);
874 spin_unlock(&card->tx_queue_lock);
876 if (!skb)
877 continue;
879 if (atmdebug) {
880 dev_info(&card->dev->dev, "Transmitted: port %d\n",
881 port);
882 print_buffer(skb);
884 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
886 vcc = *(void **)skb->cb;
888 if (vcc) {
889 atomic_inc(&vcc->stats->tx);
890 solos_pop(vcc, skb);
891 } else
892 dev_kfree_skb_irq(skb);
894 tx_started |= 1 << port; //Set TX full flag
897 if (tx_started)
898 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
900 spin_unlock_irqrestore(&card->tx_lock, flags);
901 return 0;
904 static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
906 struct solos_card *card = vcc->dev->dev_data;
907 struct sk_buff *skb2 = NULL;
908 struct pkt_hdr *header;
909 int pktlen;
911 //dev_dbg(&card->dev->dev, "psend called.\n");
912 //dev_dbg(&card->dev->dev, "dev,vpi,vci = %d,%d,%d\n",SOLOS_CHAN(vcc->dev),vcc->vpi,vcc->vci);
914 if (debug) {
915 skb2 = atm_alloc_charge(vcc, skb->len, GFP_ATOMIC);
916 if (skb2) {
917 memcpy(skb2->data, skb->data, skb->len);
918 skb_put(skb2, skb->len);
919 vcc->push(vcc, skb2);
920 atomic_inc(&vcc->stats->rx);
922 atomic_inc(&vcc->stats->tx);
923 solos_pop(vcc, skb);
924 return 0;
927 pktlen = skb->len;
928 if (pktlen > (BUF_SIZE - sizeof(*header))) {
929 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
930 solos_pop(vcc, skb);
931 return 0;
934 if (!skb_clone_writable(skb, sizeof(*header))) {
935 int expand_by = 0;
936 int ret;
938 if (skb_headroom(skb) < sizeof(*header))
939 expand_by = sizeof(*header) - skb_headroom(skb);
941 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
942 if (ret) {
943 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
944 solos_pop(vcc, skb);
945 return ret;
949 header = (void *)skb_push(skb, sizeof(*header));
951 /* This does _not_ include the size of the header */
952 header->size = cpu_to_le16(pktlen);
953 header->vpi = cpu_to_le16(vcc->vpi);
954 header->vci = cpu_to_le16(vcc->vci);
955 header->type = cpu_to_le16(PKT_DATA);
957 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
959 return 0;
962 static struct atmdev_ops fpga_ops = {
963 .open = popen,
964 .close = pclose,
965 .ioctl = NULL,
966 .getsockopt = NULL,
967 .setsockopt = NULL,
968 .send = psend,
969 .send_oam = NULL,
970 .phy_put = NULL,
971 .phy_get = NULL,
972 .change_qos = NULL,
973 .proc_read = NULL,
974 .owner = THIS_MODULE
977 static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
979 int err, i;
980 uint16_t fpga_ver;
981 uint8_t major_ver, minor_ver;
982 uint32_t data32;
983 struct solos_card *card;
985 if (debug)
986 return 0;
988 card = kzalloc(sizeof(*card), GFP_KERNEL);
989 if (!card)
990 return -ENOMEM;
992 card->dev = dev;
993 init_waitqueue_head(&card->fw_wq);
994 init_waitqueue_head(&card->param_wq);
996 err = pci_enable_device(dev);
997 if (err) {
998 dev_warn(&dev->dev, "Failed to enable PCI device\n");
999 goto out;
1002 err = pci_request_regions(dev, "solos");
1003 if (err) {
1004 dev_warn(&dev->dev, "Failed to request regions\n");
1005 goto out;
1008 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1009 if (!card->config_regs) {
1010 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
1011 goto out_release_regions;
1013 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1014 if (!card->buffers) {
1015 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
1016 goto out_unmap_config;
1019 // for(i=0;i<64 ;i+=4){
1020 // data32=ioread32(card->buffers + i);
1021 // dev_dbg(&card->dev->dev, "%08lX\n",(unsigned long)data32);
1022 // }
1024 //Fill Config Mem with zeros
1025 for(i = 0; i < 128; i += 4)
1026 iowrite32(0, card->config_regs + i);
1028 //Set RX empty flags
1029 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1031 data32 = ioread32(card->config_regs + FPGA_VER);
1032 fpga_ver = (data32 & 0x0000FFFF);
1033 major_ver = ((data32 & 0xFF000000) >> 24);
1034 minor_ver = ((data32 & 0x00FF0000) >> 16);
1035 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1036 major_ver, minor_ver, fpga_ver);
1038 card->nr_ports = 2; /* FIXME: Detect daughterboard */
1040 pci_set_drvdata(dev, card);
1042 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1043 spin_lock_init(&card->tx_lock);
1044 spin_lock_init(&card->tx_queue_lock);
1045 spin_lock_init(&card->cli_queue_lock);
1046 spin_lock_init(&card->param_queue_lock);
1047 INIT_LIST_HEAD(&card->param_queue);
1050 // Set Loopback mode
1051 data32 = 0x00010000;
1052 iowrite32(data32,card->config_regs + FLAGS_ADDR);
1055 // Fill Buffers with zeros
1056 for (i = 0; i < BUF_SIZE * 8; i += 4)
1057 iowrite32(0, card->buffers + i);
1060 for(i = 0; i < (BUF_SIZE * 1); i += 4)
1061 iowrite32(0x12345678, card->buffers + i + (0*BUF_SIZE));
1062 for(i = 0; i < (BUF_SIZE * 1); i += 4)
1063 iowrite32(0xabcdef98, card->buffers + i + (1*BUF_SIZE));
1065 // Read Config Memory
1066 printk(KERN_DEBUG "Reading Config MEM\n");
1067 i = 0;
1068 for(i = 0; i < 16; i++) {
1069 data32=ioread32(card->buffers + i*(BUF_SIZE/2));
1070 printk(KERN_ALERT "Addr: %lX Data: %08lX\n",
1071 (unsigned long)(addr_start + i*(BUF_SIZE/2)),
1072 (unsigned long)data32);
1075 //dev_dbg(&card->dev->dev, "Requesting IRQ: %d\n",dev->irq);
1076 err = request_irq(dev->irq, solos_irq, IRQF_DISABLED|IRQF_SHARED,
1077 "solos-pci", card);
1078 if (err) {
1079 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
1080 goto out_unmap_both;
1083 // Enable IRQs
1084 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1086 if (fpga_upgrade)
1087 flash_upgrade(card, 0);
1089 if (firmware_upgrade)
1090 flash_upgrade(card, 1);
1092 err = atm_init(card);
1093 if (err)
1094 goto out_free_irq;
1096 return 0;
1098 out_free_irq:
1099 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1100 free_irq(dev->irq, card);
1101 tasklet_kill(&card->tlet);
1103 out_unmap_both:
1104 pci_set_drvdata(dev, NULL);
1105 pci_iounmap(dev, card->config_regs);
1106 out_unmap_config:
1107 pci_iounmap(dev, card->buffers);
1108 out_release_regions:
1109 pci_release_regions(dev);
1110 out:
1111 return err;
1114 static int atm_init(struct solos_card *card)
1116 int i;
1118 for (i = 0; i < card->nr_ports; i++) {
1119 struct sk_buff *skb;
1120 struct pkt_hdr *header;
1122 skb_queue_head_init(&card->tx_queue[i]);
1123 skb_queue_head_init(&card->cli_queue[i]);
1125 card->atmdev[i] = atm_dev_register("solos-pci", &fpga_ops, -1, NULL);
1126 if (!card->atmdev[i]) {
1127 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1128 atm_remove(card);
1129 return -ENODEV;
1131 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1132 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
1133 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1134 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
1136 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1138 card->atmdev[i]->ci_range.vpi_bits = 8;
1139 card->atmdev[i]->ci_range.vci_bits = 16;
1140 card->atmdev[i]->dev_data = card;
1141 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
1142 card->atmdev[i]->signal = ATM_PHY_SIG_UNKNOWN;
1144 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
1145 if (!skb) {
1146 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1147 continue;
1150 header = (void *)skb_put(skb, sizeof(*header));
1152 header->size = cpu_to_le16(0);
1153 header->vpi = cpu_to_le16(0);
1154 header->vci = cpu_to_le16(0);
1155 header->type = cpu_to_le16(PKT_STATUS);
1157 fpga_queue(card, i, skb, NULL);
1159 return 0;
1162 static void atm_remove(struct solos_card *card)
1164 int i;
1166 for (i = 0; i < card->nr_ports; i++) {
1167 if (card->atmdev[i]) {
1168 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
1169 atm_dev_deregister(card->atmdev[i]);
1174 static void fpga_remove(struct pci_dev *dev)
1176 struct solos_card *card = pci_get_drvdata(dev);
1178 if (debug)
1179 return;
1181 atm_remove(card);
1183 dev_vdbg(&dev->dev, "Freeing IRQ\n");
1184 // Disable IRQs from FPGA
1185 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1186 free_irq(dev->irq, card);
1187 tasklet_kill(&card->tlet);
1189 // iowrite32(0x01,pciregs);
1190 dev_vdbg(&dev->dev, "Unmapping PCI resource\n");
1191 pci_iounmap(dev, card->buffers);
1192 pci_iounmap(dev, card->config_regs);
1194 dev_vdbg(&dev->dev, "Releasing PCI Region\n");
1195 pci_release_regions(dev);
1196 pci_disable_device(dev);
1198 pci_set_drvdata(dev, NULL);
1199 kfree(card);
1200 // dev_dbg(&card->dev->dev, "fpga_remove\n");
1201 return;
1204 static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1205 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1206 { 0, }
1209 MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1211 static struct pci_driver fpga_driver = {
1212 .name = "solos",
1213 .id_table = fpga_pci_tbl,
1214 .probe = fpga_probe,
1215 .remove = fpga_remove,
1219 static int __init solos_pci_init(void)
1221 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1222 return pci_register_driver(&fpga_driver);
1225 static void __exit solos_pci_exit(void)
1227 pci_unregister_driver(&fpga_driver);
1228 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1231 module_init(solos_pci_init);
1232 module_exit(solos_pci_exit);