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