solos: Kill existing connections on link down event
[linux-2.6] / drivers / atm / solos-pci.c
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/
5  *
6  *
7  * Copyright © 2008 Traverse Technologies
8  * Copyright © 2008 Intel Corporation
9  *
10  * Authors: Nathan Williams <nathan@traverse.com.au>
11  *          David Woodhouse <dwmw2@infradead.org>
12  *          Treker Chen <treker@xrio.com>
13  *
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.
17  *
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.
22  */
23
24 #define DEBUG
25 #define VERBOSE_DEBUG
26
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
44 #define VERSION "0.07"
45 #define PTAG "solos-pci"
46
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
58
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*/
65
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)
68
69 static int debug = 0;
70 static int atmdebug = 0;
71 static int firmware_upgrade = 0;
72 static int fpga_upgrade = 0;
73
74 struct pkt_hdr {
75         __le16 size;
76         __le16 vpi;
77         __le16 vci;
78         __le16 type;
79 };
80
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
86
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;
103 };
104
105
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;
112 };
113
114 #define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
115
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);
128
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);
141
142 static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
143 {
144         if (vcc->pop)
145                 vcc->pop(vcc, skb);
146         else
147                 dev_kfree_skb_any(skb);
148 }
149
150 static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
151                                 char *buf)
152 {
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;
159
160         buflen = strlen(attr->attr.name) + 10;
161
162         skb = alloc_skb(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;
166         }
167
168         header = (void *)skb_put(skb, sizeof(*header));
169
170         buflen = snprintf((void *)&header[1], buflen - 1,
171                           "L%05d\n%s\n", current->pid, attr->attr.name);
172         skb_put(skb, buflen);
173
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);
178
179         prm.pid = current->pid;
180         prm.response = NULL;
181         prm.port = SOLOS_CHAN(atmdev);
182
183         spin_lock_irq(&card->param_queue_lock);
184         list_add(&prm.list, &card->param_queue);
185         spin_unlock_irq(&card->param_queue_lock);
186
187         fpga_queue(card, prm.port, skb, NULL);
188
189         wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
190
191         spin_lock_irq(&card->param_queue_lock);
192         list_del(&prm.list);
193         spin_unlock_irq(&card->param_queue_lock);
194
195         if (!prm.response)
196                 return -EIO;
197
198         buflen = prm.response->len;
199         memcpy(buf, prm.response->data, buflen);
200         kfree_skb(prm.response);
201
202         return buflen;
203 }
204
205 static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
206                                  const char *buf, size_t count)
207 {
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;
215
216         buflen = strlen(attr->attr.name) + 11 + count;
217
218         skb = alloc_skb(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;
222         }
223
224         header = (void *)skb_put(skb, sizeof(*header));
225
226         buflen = snprintf((void *)&header[1], buflen - 1,
227                           "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
228
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);
234
235         prm.pid = current->pid;
236         prm.response = NULL;
237         prm.port = SOLOS_CHAN(atmdev);
238
239         spin_lock_irq(&card->param_queue_lock);
240         list_add(&prm.list, &card->param_queue);
241         spin_unlock_irq(&card->param_queue_lock);
242
243         fpga_queue(card, prm.port, skb, NULL);
244
245         wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
246
247         spin_lock_irq(&card->param_queue_lock);
248         list_del(&prm.list);
249         spin_unlock_irq(&card->param_queue_lock);
250
251         skb = prm.response;
252
253         if (!skb)
254                 return -EIO;
255
256         buflen = skb->len;
257
258         /* Sometimes it has a newline, sometimes it doesn't. */
259         if (skb->data[buflen - 1] == '\n')
260                 buflen--;
261
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;
270         
271                 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
272                          skb->data);
273                 ret = -EIO;
274         }
275         kfree_skb(skb);
276
277         return ret;
278 }
279
280 static char *next_string(struct sk_buff *skb)
281 {
282         int i = 0;
283         char *this = skb->data;
284
285         while (i < skb->len) {
286                 if (this[i] == '\n') {
287                         this[i] = 0;
288                         skb_pull(skb, i);
289                         return this;
290                 }
291         }
292         return NULL;
293 }
294
295 /*
296  * Status packet has fields separated by \n, starting with a version number
297  * for the information therein. Fields are....
298  *
299  *     packet version
300  *     TxBitRate        (version >= 1)
301  *     RxBitRate        (version >= 1)
302  *     State            (version >= 1)
303  */       
304 static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
305 {
306         char *str, *end;
307         int ver, rate_up, rate_down, state;
308
309         if (!card->atmdev[port])
310                 return -ENODEV;
311
312         str = next_string(skb);
313         if (!str)
314                 return -EIO;
315
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;
321         }
322
323         str = next_string(skb);
324         rate_up = simple_strtol(str, &end, 10);
325         if (*end)
326                 return -EIO;
327
328         str = next_string(skb);
329         rate_down = simple_strtol(str, &end, 10);
330         if (*end)
331                 return -EIO;
332
333         str = next_string(skb);
334         if (!strcmp(str, "Showtime"))
335                 state = ATM_PHY_SIG_FOUND;
336         else {
337                 state = ATM_PHY_SIG_LOST;
338                 release_vccs(card->atmdev[port]);
339         }
340
341         card->atmdev[port]->link_rate = rate_down;
342         card->atmdev[port]->signal = state;
343
344         dev_info(&card->dev->dev, "ATM state: '%s', %d/%d kb/s up/down.\n",
345                  str, rate_up/1000, rate_down/1000);
346         return 0;
347 }
348
349 static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
350 {
351         struct solos_param *prm;
352         unsigned long flags;
353         int cmdpid;
354         int found = 0;
355
356         if (skb->len < 7)
357                 return 0;
358
359         if (skb->data[0] != 'L'    || !isdigit(skb->data[1]) ||
360             !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
361             !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
362             skb->data[6] != '\n')
363                 return 0;
364
365         cmdpid = simple_strtol(&skb->data[1], NULL, 10);
366
367         spin_lock_irqsave(&card->param_queue_lock, flags);
368         list_for_each_entry(prm, &card->param_queue, list) {
369                 if (prm->port == port && prm->pid == cmdpid) {
370                         prm->response = skb;
371                         skb_pull(skb, 7);
372                         wake_up(&card->param_wq);
373                         found = 1;
374                         break;
375                 }
376         }
377         spin_unlock_irqrestore(&card->param_queue_lock, flags);
378         return found;
379 }
380
381 static ssize_t console_show(struct device *dev, struct device_attribute *attr,
382                             char *buf)
383 {
384         struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
385         struct solos_card *card = atmdev->dev_data;
386         struct sk_buff *skb;
387
388         spin_lock(&card->cli_queue_lock);
389         skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
390         spin_unlock(&card->cli_queue_lock);
391         if(skb == NULL)
392                 return sprintf(buf, "No data.\n");
393
394         memcpy(buf, skb->data, skb->len);
395         dev_dbg(&card->dev->dev, "len: %d\n", skb->len);
396
397         kfree_skb(skb);
398         return skb->len;
399 }
400
401 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
402 {
403         struct sk_buff *skb;
404         struct pkt_hdr *header;
405
406 //      dev_dbg(&card->dev->dev, "size: %d\n", size);
407
408         if (size > (BUF_SIZE - sizeof(*header))) {
409                 dev_dbg(&card->dev->dev, "Command is too big.  Dropping request\n");
410                 return 0;
411         }
412         skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
413         if (!skb) {
414                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
415                 return 0;
416         }
417
418         header = (void *)skb_put(skb, sizeof(*header));
419
420         header->size = cpu_to_le16(size);
421         header->vpi = cpu_to_le16(0);
422         header->vci = cpu_to_le16(0);
423         header->type = cpu_to_le16(PKT_COMMAND);
424
425         memcpy(skb_put(skb, size), buf, size);
426
427         fpga_queue(card, dev, skb, NULL);
428
429         return 0;
430 }
431
432 static ssize_t console_store(struct device *dev, struct device_attribute *attr,
433                              const char *buf, size_t count)
434 {
435         struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
436         struct solos_card *card = atmdev->dev_data;
437         int err;
438
439         err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
440
441         return err?:count;
442 }
443
444 static DEVICE_ATTR(console, 0644, console_show, console_store);
445
446
447 #define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
448 #define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
449
450 #include "solos-attrlist.c"
451
452 #undef SOLOS_ATTR_RO
453 #undef SOLOS_ATTR_RW
454
455 #define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
456 #define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
457
458 static struct attribute *solos_attrs[] = {
459 #include "solos-attrlist.c"
460         NULL
461 };
462
463 static struct attribute_group solos_attr_group = {
464         .attrs = solos_attrs,
465         .name = "parameters",
466 };
467
468 static int flash_upgrade(struct solos_card *card, int chip)
469 {
470         const struct firmware *fw;
471         const char *fw_name;
472         uint32_t data32 = 0;
473         int blocksize = 0;
474         int numblocks = 0;
475         int offset;
476
477         if (chip == 0) {
478                 fw_name = "solos-FPGA.bin";
479                 blocksize = FPGA_BLOCK;
480         } else {
481                 fw_name = "solos-Firmware.bin";
482                 blocksize = SOLOS_BLOCK;
483         }
484
485         if (request_firmware(&fw, fw_name, &card->dev->dev))
486                 return -ENOENT;
487
488         dev_info(&card->dev->dev, "Flash upgrade starting\n");
489
490         numblocks = fw->size / blocksize;
491         dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
492         dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
493         
494         dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
495         iowrite32(1, card->config_regs + FPGA_MODE);
496         data32 = ioread32(card->config_regs + FPGA_MODE); 
497
498         /* Set mode to Chip Erase */
499         dev_info(&card->dev->dev, "Set FPGA Flash mode to %s Chip Erase\n",
500                  chip?"Solos":"FPGA");
501         iowrite32((chip * 2), card->config_regs + FLASH_MODE);
502
503
504         iowrite32(1, card->config_regs + WRITE_FLASH);
505         wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
506
507         for (offset = 0; offset < fw->size; offset += blocksize) {
508                 int i;
509
510                 /* Clear write flag */
511                 iowrite32(0, card->config_regs + WRITE_FLASH);
512
513                 /* Set mode to Block Write */
514                 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
515                 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
516
517                 /* Copy block to buffer, swapping each 16 bits */
518                 for(i = 0; i < blocksize; i += 4) {
519                         uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
520                         iowrite32(word, RX_BUF(card, 3) + i);
521                 }
522
523                 /* Specify block number and then trigger flash write */
524                 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
525                 iowrite32(1, card->config_regs + WRITE_FLASH);
526                 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
527         }
528
529         release_firmware(fw);
530         iowrite32(0, card->config_regs + WRITE_FLASH);
531         iowrite32(0, card->config_regs + FPGA_MODE);
532         iowrite32(0, card->config_regs + FLASH_MODE);
533         dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
534         return 0;
535 }
536
537 static irqreturn_t solos_irq(int irq, void *dev_id)
538 {
539         struct solos_card *card = dev_id;
540         int handled = 1;
541
542         //ACK IRQ
543         iowrite32(0, card->config_regs + IRQ_CLEAR);
544         //Disable IRQs from FPGA
545         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
546
547         if (card->atmdev[0])
548                 tasklet_schedule(&card->tlet);
549         else
550                 wake_up(&card->fw_wq);
551
552         //Enable IRQs from FPGA
553         iowrite32(1, card->config_regs + IRQ_EN_ADDR);
554         return IRQ_RETVAL(handled);
555 }
556
557 void solos_bh(unsigned long card_arg)
558 {
559         struct solos_card *card = (void *)card_arg;
560         int port;
561         uint32_t card_flags;
562         uint32_t tx_mask;
563         uint32_t rx_done = 0;
564
565         card_flags = ioread32(card->config_regs + FLAGS_ADDR);
566
567         /* The TX bits are set if the channel is busy; clear if not. We want to
568            invoke fpga_tx() unless _all_ the bits for active channels are set */
569         tx_mask = (1 << card->nr_ports) - 1;
570         if ((card_flags & tx_mask) != tx_mask)
571                 fpga_tx(card);
572
573         for (port = 0; port < card->nr_ports; port++) {
574                 if (card_flags & (0x10 << port)) {
575                         struct pkt_hdr header;
576                         struct sk_buff *skb;
577                         struct atm_vcc *vcc;
578                         int size;
579
580                         rx_done |= 0x10 << port;
581
582                         memcpy_fromio(&header, RX_BUF(card, port), sizeof(header));
583
584                         size = le16_to_cpu(header.size);
585
586                         skb = alloc_skb(size + 1, GFP_ATOMIC);
587                         if (!skb) {
588                                 if (net_ratelimit())
589                                         dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
590                                 continue;
591                         }
592
593                         memcpy_fromio(skb_put(skb, size),
594                                       RX_BUF(card, port) + sizeof(header),
595                                       size);
596
597                         if (atmdebug) {
598                                 dev_info(&card->dev->dev, "Received: device %d\n", port);
599                                 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
600                                          size, le16_to_cpu(header.vpi),
601                                          le16_to_cpu(header.vci));
602                                 print_buffer(skb);
603                         }
604
605                         switch (le16_to_cpu(header.type)) {
606                         case PKT_DATA:
607                                 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header.vpi),
608                                                le16_to_cpu(header.vci));
609                                 if (!vcc) {
610                                         if (net_ratelimit())
611                                                 dev_warn(&card->dev->dev, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
612                                                          le16_to_cpu(header.vci), le16_to_cpu(header.vpi),
613                                                          port);
614                                         continue;
615                                 }
616                                 atm_charge(vcc, skb->truesize);
617                                 vcc->push(vcc, skb);
618                                 atomic_inc(&vcc->stats->rx);
619                                 break;
620
621                         case PKT_STATUS:
622                                 process_status(card, port, skb);
623                                 dev_kfree_skb(skb);
624                                 break;
625
626                         case PKT_COMMAND:
627                         default: /* FIXME: Not really, surely? */
628                                 if (process_command(card, port, skb))
629                                         break;
630                                 spin_lock(&card->cli_queue_lock);
631                                 if (skb_queue_len(&card->cli_queue[port]) > 10) {
632                                         if (net_ratelimit())
633                                                 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
634                                                          port);
635                                 } else
636                                         skb_queue_tail(&card->cli_queue[port], skb);
637                                 spin_unlock(&card->cli_queue_lock);
638                                 break;
639                         }
640                 }
641         }
642         if (rx_done)
643                 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
644
645         return;
646 }
647
648 static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
649 {
650         struct hlist_head *head;
651         struct atm_vcc *vcc = NULL;
652         struct hlist_node *node;
653         struct sock *s;
654
655         read_lock(&vcc_sklist_lock);
656         head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
657         sk_for_each(s, node, head) {
658                 vcc = atm_sk(s);
659                 if (vcc->dev == dev && vcc->vci == vci &&
660                     vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE)
661                         goto out;
662         }
663         vcc = NULL;
664  out:
665         read_unlock(&vcc_sklist_lock);
666         return vcc;
667 }
668
669 static int list_vccs(int vci)
670 {
671         struct hlist_head *head;
672         struct atm_vcc *vcc;
673         struct hlist_node *node;
674         struct sock *s;
675         int num_found = 0;
676         int i;
677
678         read_lock(&vcc_sklist_lock);
679         if (vci != 0){
680                 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
681                 sk_for_each(s, node, head) {
682                         num_found ++;
683                         vcc = atm_sk(s);
684                         printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
685                                vcc->dev->number,
686                                vcc->vpi,
687                                vcc->vci);
688                 }
689         } else {
690                 for(i = 0; i < VCC_HTABLE_SIZE; i++){
691                         head = &vcc_hash[i];
692                         sk_for_each(s, node, head) {
693                                 num_found ++;
694                                 vcc = atm_sk(s);
695                                 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
696                                        vcc->dev->number,
697                                        vcc->vpi,
698                                        vcc->vci);
699                         }
700                 }
701         }
702         read_unlock(&vcc_sklist_lock);
703         return num_found;
704 }
705
706 static void release_vccs(struct atm_dev *dev)
707 {
708         int i;
709
710         write_lock_irq(&vcc_sklist_lock);
711         for (i = 0; i < VCC_HTABLE_SIZE; i++) {
712                 struct hlist_head *head = &vcc_hash[i];
713                 struct hlist_node *node, *tmp;
714                 struct sock *s;
715                 struct atm_vcc *vcc;
716
717                 sk_for_each_safe(s, node, tmp, head) {
718                         vcc = atm_sk(s);
719                         if (vcc->dev == dev) {
720                                 vcc_release_async(vcc, -EPIPE);
721                                 sk_del_node_init(s);
722                         }
723                 }
724         }
725         write_unlock_irq(&vcc_sklist_lock);
726 }
727
728
729 static int popen(struct atm_vcc *vcc)
730 {
731         struct solos_card *card = vcc->dev->dev_data;
732         struct sk_buff *skb;
733         struct pkt_hdr *header;
734
735         skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
736         if (!skb && net_ratelimit()) {
737                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
738                 return -ENOMEM;
739         }
740         header = (void *)skb_put(skb, sizeof(*header));
741
742         header->size = cpu_to_le16(0);
743         header->vpi = cpu_to_le16(vcc->vpi);
744         header->vci = cpu_to_le16(vcc->vci);
745         header->type = cpu_to_le16(PKT_POPEN);
746
747         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
748
749 //      dev_dbg(&card->dev->dev, "Open for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
750         set_bit(ATM_VF_ADDR, &vcc->flags); // accept the vpi / vci
751         set_bit(ATM_VF_READY, &vcc->flags);
752         list_vccs(0);
753
754
755         return 0;
756 }
757
758 static void pclose(struct atm_vcc *vcc)
759 {
760         struct solos_card *card = vcc->dev->dev_data;
761         struct sk_buff *skb;
762         struct pkt_hdr *header;
763
764         skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
765         if (!skb) {
766                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
767                 return;
768         }
769         header = (void *)skb_put(skb, sizeof(*header));
770
771         header->size = cpu_to_le16(0);
772         header->vpi = cpu_to_le16(vcc->vpi);
773         header->vci = cpu_to_le16(vcc->vci);
774         header->type = cpu_to_le16(PKT_PCLOSE);
775
776         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
777
778 //      dev_dbg(&card->dev->dev, "Close for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
779
780         clear_bit(ATM_VF_ADDR, &vcc->flags);
781         clear_bit(ATM_VF_READY, &vcc->flags);
782
783         return;
784 }
785
786 static int print_buffer(struct sk_buff *buf)
787 {
788         int len,i;
789         char msg[500];
790         char item[10];
791
792         len = buf->len;
793         for (i = 0; i < len; i++){
794                 if(i % 8 == 0)
795                         sprintf(msg, "%02X: ", i);
796
797                 sprintf(item,"%02X ",*(buf->data + i));
798                 strcat(msg, item);
799                 if(i % 8 == 7) {
800                         sprintf(item, "\n");
801                         strcat(msg, item);
802                         printk(KERN_DEBUG "%s", msg);
803                 }
804         }
805         if (i % 8 != 0) {
806                 sprintf(item, "\n");
807                 strcat(msg, item);
808                 printk(KERN_DEBUG "%s", msg);
809         }
810         printk(KERN_DEBUG "\n");
811
812         return 0;
813 }
814
815 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
816                        struct atm_vcc *vcc)
817 {
818         int old_len;
819
820         *(void **)skb->cb = vcc;
821
822         spin_lock(&card->tx_queue_lock);
823         old_len = skb_queue_len(&card->tx_queue[port]);
824         skb_queue_tail(&card->tx_queue[port], skb);
825         spin_unlock(&card->tx_queue_lock);
826
827         /* If TX might need to be started, do so */
828         if (!old_len)
829                 fpga_tx(card);
830 }
831
832 static int fpga_tx(struct solos_card *card)
833 {
834         uint32_t tx_pending;
835         uint32_t tx_started = 0;
836         struct sk_buff *skb;
837         struct atm_vcc *vcc;
838         unsigned char port;
839         unsigned long flags;
840
841         spin_lock_irqsave(&card->tx_lock, flags);
842
843         tx_pending = ioread32(card->config_regs + FLAGS_ADDR);
844
845         dev_vdbg(&card->dev->dev, "TX Flags are %X\n", tx_pending);
846
847         for (port = 0; port < card->nr_ports; port++) {
848                 if (!(tx_pending & (1 << port))) {
849
850                         spin_lock(&card->tx_queue_lock);
851                         skb = skb_dequeue(&card->tx_queue[port]);
852                         spin_unlock(&card->tx_queue_lock);
853
854                         if (!skb)
855                                 continue;
856
857                         if (atmdebug) {
858                                 dev_info(&card->dev->dev, "Transmitted: port %d\n",
859                                          port);
860                                 print_buffer(skb);
861                         }
862                         memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
863
864                         vcc = *(void **)skb->cb;
865
866                         if (vcc) {
867                                 atomic_inc(&vcc->stats->tx);
868                                 solos_pop(vcc, skb);
869                         } else
870                                 dev_kfree_skb_irq(skb);
871
872                         tx_started |= 1 << port; //Set TX full flag
873                 }
874         }
875         if (tx_started)
876                 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
877
878         spin_unlock_irqrestore(&card->tx_lock, flags);
879         return 0;
880 }
881
882 static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
883 {
884         struct solos_card *card = vcc->dev->dev_data;
885         struct sk_buff *skb2 = NULL;
886         struct pkt_hdr *header;
887         int pktlen;
888
889         //dev_dbg(&card->dev->dev, "psend called.\n");
890         //dev_dbg(&card->dev->dev, "dev,vpi,vci = %d,%d,%d\n",SOLOS_CHAN(vcc->dev),vcc->vpi,vcc->vci);
891
892         if (debug) {
893                 skb2 = atm_alloc_charge(vcc, skb->len, GFP_ATOMIC);
894                 if (skb2) {
895                         memcpy(skb2->data, skb->data, skb->len);
896                         skb_put(skb2, skb->len);
897                         vcc->push(vcc, skb2);
898                         atomic_inc(&vcc->stats->rx);
899                 }
900                 atomic_inc(&vcc->stats->tx);
901                 solos_pop(vcc, skb);
902                 return 0;
903         }
904
905         pktlen = skb->len;
906         if (pktlen > (BUF_SIZE - sizeof(*header))) {
907                 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
908                 solos_pop(vcc, skb);
909                 return 0;
910         }
911
912         if (!skb_clone_writable(skb, sizeof(*header))) {
913                 int expand_by = 0;
914                 int ret;
915
916                 if (skb_headroom(skb) < sizeof(*header))
917                         expand_by = sizeof(*header) - skb_headroom(skb);
918
919                 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
920                 if (ret) {
921                         dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
922                         solos_pop(vcc, skb);
923                         return ret;
924                 }
925         }
926
927         header = (void *)skb_push(skb, sizeof(*header));
928
929         /* This does _not_ include the size of the header */
930         header->size = cpu_to_le16(pktlen);
931         header->vpi = cpu_to_le16(vcc->vpi);
932         header->vci = cpu_to_le16(vcc->vci);
933         header->type = cpu_to_le16(PKT_DATA);
934
935         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
936
937         return 0;
938 }
939
940 static struct atmdev_ops fpga_ops = {
941         .open =         popen,
942         .close =        pclose,
943         .ioctl =        NULL,
944         .getsockopt =   NULL,
945         .setsockopt =   NULL,
946         .send =         psend,
947         .send_oam =     NULL,
948         .phy_put =      NULL,
949         .phy_get =      NULL,
950         .change_qos =   NULL,
951         .proc_read =    NULL,
952         .owner =        THIS_MODULE
953 };
954
955 static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
956 {
957         int err, i;
958         uint16_t fpga_ver;
959         uint8_t major_ver, minor_ver;
960         uint32_t data32;
961         struct solos_card *card;
962
963         if (debug)
964                 return 0;
965
966         card = kzalloc(sizeof(*card), GFP_KERNEL);
967         if (!card)
968                 return -ENOMEM;
969
970         card->dev = dev;
971         init_waitqueue_head(&card->fw_wq);
972         init_waitqueue_head(&card->param_wq);
973
974         err = pci_enable_device(dev);
975         if (err) {
976                 dev_warn(&dev->dev,  "Failed to enable PCI device\n");
977                 goto out;
978         }
979
980         err = pci_request_regions(dev, "solos");
981         if (err) {
982                 dev_warn(&dev->dev, "Failed to request regions\n");
983                 goto out;
984         }
985
986         card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
987         if (!card->config_regs) {
988                 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
989                 goto out_release_regions;
990         }
991         card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
992         if (!card->buffers) {
993                 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
994                 goto out_unmap_config;
995         }
996
997 //      for(i=0;i<64 ;i+=4){
998 //              data32=ioread32(card->buffers + i);
999 //              dev_dbg(&card->dev->dev, "%08lX\n",(unsigned long)data32);
1000 //      }
1001
1002         //Fill Config Mem with zeros
1003         for(i = 0; i < 128; i += 4)
1004                 iowrite32(0, card->config_regs + i);
1005
1006         //Set RX empty flags
1007         iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1008
1009         data32 = ioread32(card->config_regs + FPGA_VER);
1010         fpga_ver = (data32 & 0x0000FFFF);
1011         major_ver = ((data32 & 0xFF000000) >> 24);
1012         minor_ver = ((data32 & 0x00FF0000) >> 16);
1013         dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1014                  major_ver, minor_ver, fpga_ver);
1015
1016         card->nr_ports = 2; /* FIXME: Detect daughterboard */
1017
1018         pci_set_drvdata(dev, card);
1019
1020         tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1021         spin_lock_init(&card->tx_lock);
1022         spin_lock_init(&card->tx_queue_lock);
1023         spin_lock_init(&card->cli_queue_lock);
1024         spin_lock_init(&card->param_queue_lock);
1025         INIT_LIST_HEAD(&card->param_queue);
1026
1027 /*
1028         // Set Loopback mode
1029         data32 = 0x00010000;
1030         iowrite32(data32,card->config_regs + FLAGS_ADDR);
1031 */
1032 /*
1033         // Fill Buffers with zeros
1034         for (i = 0; i < BUF_SIZE * 8; i += 4)
1035                 iowrite32(0, card->buffers + i);
1036 */
1037 /*
1038         for(i = 0; i < (BUF_SIZE * 1); i += 4)
1039                 iowrite32(0x12345678, card->buffers + i + (0*BUF_SIZE));
1040         for(i = 0; i < (BUF_SIZE * 1); i += 4)
1041                 iowrite32(0xabcdef98, card->buffers + i + (1*BUF_SIZE));
1042
1043         // Read Config Memory
1044         printk(KERN_DEBUG "Reading Config MEM\n");
1045         i = 0;
1046         for(i = 0; i < 16; i++) {
1047                 data32=ioread32(card->buffers + i*(BUF_SIZE/2));
1048                 printk(KERN_ALERT "Addr: %lX Data: %08lX\n",
1049                        (unsigned long)(addr_start + i*(BUF_SIZE/2)),
1050                        (unsigned long)data32);
1051         }
1052 */
1053         //dev_dbg(&card->dev->dev, "Requesting IRQ: %d\n",dev->irq);
1054         err = request_irq(dev->irq, solos_irq, IRQF_DISABLED|IRQF_SHARED,
1055                           "solos-pci", card);
1056         if (err) {
1057                 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
1058                 goto out_unmap_both;
1059         }
1060
1061         // Enable IRQs
1062         iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1063
1064         if (fpga_upgrade)
1065                 flash_upgrade(card, 0);
1066
1067         if (firmware_upgrade)
1068                 flash_upgrade(card, 1);
1069
1070         err = atm_init(card);
1071         if (err)
1072                 goto out_free_irq;
1073
1074         return 0;
1075
1076  out_free_irq:
1077         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1078         free_irq(dev->irq, card);
1079         tasklet_kill(&card->tlet);
1080         
1081  out_unmap_both:
1082         pci_set_drvdata(dev, NULL);
1083         pci_iounmap(dev, card->config_regs);
1084  out_unmap_config:
1085         pci_iounmap(dev, card->buffers);
1086  out_release_regions:
1087         pci_release_regions(dev);
1088  out:
1089         return err;
1090 }
1091
1092 static int atm_init(struct solos_card *card)
1093 {
1094         int i;
1095
1096         for (i = 0; i < card->nr_ports; i++) {
1097                 struct sk_buff *skb;
1098                 struct pkt_hdr *header;
1099
1100                 skb_queue_head_init(&card->tx_queue[i]);
1101                 skb_queue_head_init(&card->cli_queue[i]);
1102
1103                 card->atmdev[i] = atm_dev_register("solos-pci", &fpga_ops, -1, NULL);
1104                 if (!card->atmdev[i]) {
1105                         dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1106                         atm_remove(card);
1107                         return -ENODEV;
1108                 }
1109                 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1110                         dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
1111                 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1112                         dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
1113
1114                 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1115
1116                 card->atmdev[i]->ci_range.vpi_bits = 8;
1117                 card->atmdev[i]->ci_range.vci_bits = 16;
1118                 card->atmdev[i]->dev_data = card;
1119                 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
1120                 card->atmdev[i]->signal = ATM_PHY_SIG_UNKNOWN;
1121
1122                 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
1123                 if (!skb) {
1124                         dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1125                         continue;
1126                 }
1127
1128                 header = (void *)skb_put(skb, sizeof(*header));
1129
1130                 header->size = cpu_to_le16(0);
1131                 header->vpi = cpu_to_le16(0);
1132                 header->vci = cpu_to_le16(0);
1133                 header->type = cpu_to_le16(PKT_STATUS);
1134
1135                 fpga_queue(card, i, skb, NULL);
1136         }
1137         return 0;
1138 }
1139
1140 static void atm_remove(struct solos_card *card)
1141 {
1142         int i;
1143
1144         for (i = 0; i < card->nr_ports; i++) {
1145                 if (card->atmdev[i]) {
1146                         dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
1147                         atm_dev_deregister(card->atmdev[i]);
1148                 }
1149         }
1150 }
1151
1152 static void fpga_remove(struct pci_dev *dev)
1153 {
1154         struct solos_card *card = pci_get_drvdata(dev);
1155
1156         if (debug)
1157                 return;
1158
1159         atm_remove(card);
1160
1161         dev_vdbg(&dev->dev, "Freeing IRQ\n");
1162         // Disable IRQs from FPGA
1163         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1164         free_irq(dev->irq, card);
1165         tasklet_kill(&card->tlet);
1166
1167         //      iowrite32(0x01,pciregs);
1168         dev_vdbg(&dev->dev, "Unmapping PCI resource\n");
1169         pci_iounmap(dev, card->buffers);
1170         pci_iounmap(dev, card->config_regs);
1171
1172         dev_vdbg(&dev->dev, "Releasing PCI Region\n");
1173         pci_release_regions(dev);
1174         pci_disable_device(dev);
1175
1176         pci_set_drvdata(dev, NULL);
1177         kfree(card);
1178 //      dev_dbg(&card->dev->dev, "fpga_remove\n");
1179         return;
1180 }
1181
1182 static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1183         { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1184         { 0, }
1185 };
1186
1187 MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1188
1189 static struct pci_driver fpga_driver = {
1190         .name =         "solos",
1191         .id_table =     fpga_pci_tbl,
1192         .probe =        fpga_probe,
1193         .remove =       fpga_remove,
1194 };
1195
1196
1197 static int __init solos_pci_init(void)
1198 {
1199         printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1200         return pci_register_driver(&fpga_driver);
1201 }
1202
1203 static void __exit solos_pci_exit(void)
1204 {
1205         pci_unregister_driver(&fpga_driver);
1206         printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1207 }
1208
1209 module_init(solos_pci_init);
1210 module_exit(solos_pci_exit);