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