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