solos: Kill global 'opens' count.
[linux-2.6] / drivers / atm / solos-pci.c
1 /*
2  * Driver for the Solos PCI ADSL2+ card, designed to support Linux by
3  *  Traverse Technologies -- http://www.traverse.com.au/
4  *  Xrio Limited          -- http://www.xrio.com/
5  *
6  *
7  * Copyright © 2008 Traverse Technologies
8  * Copyright © 2008 Intel Corporation
9  *
10  * Authors: Nathan Williams <nathan@traverse.com.au>
11  *          David Woodhouse <dwmw2@infradead.org>
12  *          Treker Chen <treker@xrio.com>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * version 2, as published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  */
23
24 #define DEBUG
25 #define VERBOSE_DEBUG
26
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/ioport.h>
32 #include <linux/types.h>
33 #include <linux/pci.h>
34 #include <linux/atm.h>
35 #include <linux/atmdev.h>
36 #include <linux/skbuff.h>
37 #include <linux/sysfs.h>
38 #include <linux/device.h>
39 #include <linux/kobject.h>
40 #include <linux/firmware.h>
41
42 #define VERSION "0.07"
43 #define PTAG "solos-pci"
44
45 #define CONFIG_RAM_SIZE 128
46 #define FLAGS_ADDR      0x7C
47 #define IRQ_EN_ADDR     0x78
48 #define FPGA_VER        0x74
49 #define IRQ_CLEAR       0x70
50 #define WRITE_FLASH     0x6C
51 #define PORTS           0x68
52 #define FLASH_BLOCK     0x64
53 #define FLASH_BUSY      0x60
54 #define FPGA_MODE       0x5C
55 #define FLASH_MODE      0x58
56
57 #define DATA_RAM_SIZE   32768
58 #define BUF_SIZE        4096
59 #define FPGA_PAGE       528 /* FPGA flash page size*/
60 #define SOLOS_PAGE      512 /* Solos flash page size*/
61 #define FPGA_BLOCK      (FPGA_PAGE * 8) /* FPGA flash block size*/
62 #define SOLOS_BLOCK     (SOLOS_PAGE * 8) /* Solos flash block size*/
63
64 #define RX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2)
65 #define TX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2 + BUF_SIZE)
66
67 static int debug = 0;
68 static int atmdebug = 0;
69 static int firmware_upgrade = 0;
70 static int fpga_upgrade = 0;
71
72 struct pkt_hdr {
73         __le16 size;
74         __le16 vpi;
75         __le16 vci;
76         __le16 type;
77 };
78
79 #define PKT_DATA        0
80 #define PKT_COMMAND     1
81 #define PKT_POPEN       3
82 #define PKT_PCLOSE      4
83
84 struct solos_card {
85         void __iomem *config_regs;
86         void __iomem *buffers;
87         int nr_ports;
88         struct pci_dev *dev;
89         struct atm_dev *atmdev[4];
90         struct tasklet_struct tlet;
91         spinlock_t tx_lock;
92         spinlock_t tx_queue_lock;
93         spinlock_t cli_queue_lock;
94         struct sk_buff_head tx_queue[4];
95         struct sk_buff_head cli_queue[4];
96         wait_queue_head_t fw_wq;
97 };
98
99 #define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
100
101 MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
102 MODULE_DESCRIPTION("Solos PCI driver");
103 MODULE_VERSION(VERSION);
104 MODULE_LICENSE("GPL");
105 MODULE_PARM_DESC(debug, "Enable Loopback");
106 MODULE_PARM_DESC(atmdebug, "Print ATM data");
107 MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
108 MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
109 module_param(debug, int, 0444);
110 module_param(atmdebug, int, 0644);
111 module_param(firmware_upgrade, int, 0444);
112 module_param(fpga_upgrade, int, 0444);
113
114 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
115                        struct atm_vcc *vcc);
116 static int fpga_tx(struct solos_card *);
117 static irqreturn_t solos_irq(int irq, void *dev_id);
118 static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
119 static int list_vccs(int vci);
120 static int atm_init(struct solos_card *);
121 static void atm_remove(struct solos_card *);
122 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
123 static void solos_bh(unsigned long);
124 static int print_buffer(struct sk_buff *buf);
125
126 static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
127 {
128         if (vcc->pop)
129                 vcc->pop(vcc, skb);
130         else
131                 dev_kfree_skb_any(skb);
132 }
133
134 static ssize_t console_show(struct device *dev, struct device_attribute *attr,
135                             char *buf)
136 {
137         struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
138         struct solos_card *card = atmdev->dev_data;
139         struct sk_buff *skb;
140
141         spin_lock(&card->cli_queue_lock);
142         skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
143         spin_unlock(&card->cli_queue_lock);
144         if(skb == NULL)
145                 return sprintf(buf, "No data.\n");
146
147         memcpy(buf, skb->data, skb->len);
148         dev_dbg(&card->dev->dev, "len: %d\n", skb->len);
149
150         kfree_skb(skb);
151         return skb->len;
152 }
153
154 static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
155 {
156         struct sk_buff *skb;
157         struct pkt_hdr *header;
158
159 //      dev_dbg(&card->dev->dev, "size: %d\n", size);
160
161         if (size > (BUF_SIZE - sizeof(*header))) {
162                 dev_dbg(&card->dev->dev, "Command is too big.  Dropping request\n");
163                 return 0;
164         }
165         skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
166         if (!skb) {
167                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
168                 return 0;
169         }
170
171         header = (void *)skb_put(skb, sizeof(*header));
172
173         header->size = cpu_to_le16(size);
174         header->vpi = cpu_to_le16(0);
175         header->vci = cpu_to_le16(0);
176         header->type = cpu_to_le16(PKT_COMMAND);
177
178         memcpy(skb_put(skb, size), buf, size);
179
180         fpga_queue(card, dev, skb, NULL);
181
182         return 0;
183 }
184
185 static ssize_t console_store(struct device *dev, struct device_attribute *attr,
186                              const char *buf, size_t count)
187 {
188         struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
189         struct solos_card *card = atmdev->dev_data;
190         int err;
191
192         err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
193
194         return err?:count;
195 }
196
197 static DEVICE_ATTR(console, 0644, console_show, console_store);
198
199 static int flash_upgrade(struct solos_card *card, int chip)
200 {
201         const struct firmware *fw;
202         const char *fw_name;
203         uint32_t data32 = 0;
204         int blocksize = 0;
205         int numblocks = 0;
206         int offset;
207
208         if (chip == 0) {
209                 fw_name = "solos-FPGA.bin";
210                 blocksize = FPGA_BLOCK;
211         } else {
212                 fw_name = "solos-Firmware.bin";
213                 blocksize = SOLOS_BLOCK;
214         }
215
216         if (request_firmware(&fw, fw_name, &card->dev->dev))
217                 return -ENOENT;
218
219         dev_info(&card->dev->dev, "Flash upgrade starting\n");
220
221         numblocks = fw->size / blocksize;
222         dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
223         dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
224         
225         dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
226         iowrite32(1, card->config_regs + FPGA_MODE);
227         data32 = ioread32(card->config_regs + FPGA_MODE); 
228
229         /* Set mode to Chip Erase */
230         dev_info(&card->dev->dev, "Set FPGA Flash mode to %s Chip Erase\n",
231                  chip?"Solos":"FPGA");
232         iowrite32((chip * 2), card->config_regs + FLASH_MODE);
233
234
235         iowrite32(1, card->config_regs + WRITE_FLASH);
236         wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
237
238         for (offset = 0; offset < fw->size; offset += blocksize) {
239                 int i;
240
241                 /* Clear write flag */
242                 iowrite32(0, card->config_regs + WRITE_FLASH);
243
244                 /* Set mode to Block Write */
245                 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
246                 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
247
248                 /* Copy block to buffer, swapping each 16 bits */
249                 for(i = 0; i < blocksize; i += 4) {
250                         uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
251                         iowrite32(word, RX_BUF(card, 3) + i);
252                 }
253
254                 /* Specify block number and then trigger flash write */
255                 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
256                 iowrite32(1, card->config_regs + WRITE_FLASH);
257                 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
258         }
259
260         release_firmware(fw);
261         iowrite32(0, card->config_regs + WRITE_FLASH);
262         iowrite32(0, card->config_regs + FPGA_MODE);
263         iowrite32(0, card->config_regs + FLASH_MODE);
264         dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
265         return 0;
266 }
267
268 static irqreturn_t solos_irq(int irq, void *dev_id)
269 {
270         struct solos_card *card = dev_id;
271         int handled = 1;
272
273         //ACK IRQ
274         iowrite32(0, card->config_regs + IRQ_CLEAR);
275         //Disable IRQs from FPGA
276         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
277
278         if (card->atmdev[0])
279                 tasklet_schedule(&card->tlet);
280         else
281                 wake_up(&card->fw_wq);
282
283         //Enable IRQs from FPGA
284         iowrite32(1, card->config_regs + IRQ_EN_ADDR);
285         return IRQ_RETVAL(handled);
286 }
287
288 void solos_bh(unsigned long card_arg)
289 {
290         struct solos_card *card = (void *)card_arg;
291         int port;
292         uint32_t card_flags;
293         uint32_t tx_mask;
294         uint32_t rx_done = 0;
295
296         card_flags = ioread32(card->config_regs + FLAGS_ADDR);
297
298         /* The TX bits are set if the channel is busy; clear if not. We want to
299            invoke fpga_tx() unless _all_ the bits for active channels are set */
300         tx_mask = (1 << card->nr_ports) - 1;
301         if ((card_flags & tx_mask) != tx_mask)
302                 fpga_tx(card);
303
304         for (port = 0; port < card->nr_ports; port++) {
305                 if (card_flags & (0x10 << port)) {
306                         struct pkt_hdr header;
307                         struct sk_buff *skb;
308                         struct atm_vcc *vcc;
309                         int size;
310
311                         rx_done |= 0x10 << port;
312
313                         memcpy_fromio(&header, RX_BUF(card, port), sizeof(header));
314
315                         size = le16_to_cpu(header.size);
316
317                         skb = alloc_skb(size, GFP_ATOMIC);
318                         if (!skb) {
319                                 if (net_ratelimit())
320                                         dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
321                                 continue;
322                         }
323
324                         memcpy_fromio(skb_put(skb, size),
325                                       RX_BUF(card, port) + sizeof(header),
326                                       size);
327
328                         if (atmdebug) {
329                                 dev_info(&card->dev->dev, "Received: device %d\n", port);
330                                 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
331                                          size, le16_to_cpu(header.vpi),
332                                          le16_to_cpu(header.vci));
333                                 print_buffer(skb);
334                         }
335
336                         switch (le16_to_cpu(header.type)) {
337                         case PKT_DATA:
338                                 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header.vpi),
339                                                le16_to_cpu(header.vci));
340                                 if (!vcc) {
341                                         if (net_ratelimit())
342                                                 dev_warn(&card->dev->dev, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
343                                                          le16_to_cpu(header.vci), le16_to_cpu(header.vpi),
344                                                          port);
345                                         continue;
346                                 }
347                                 atm_charge(vcc, skb->truesize);
348                                 vcc->push(vcc, skb);
349                                 atomic_inc(&vcc->stats->rx);
350                                 break;
351
352                         case PKT_COMMAND:
353                         default: /* FIXME: Not really, surely? */
354                                 spin_lock(&card->cli_queue_lock);
355                                 if (skb_queue_len(&card->cli_queue[port]) > 10) {
356                                         if (net_ratelimit())
357                                                 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
358                                                          port);
359                                 } else
360                                         skb_queue_tail(&card->cli_queue[port], skb);
361                                 spin_unlock(&card->cli_queue_lock);
362                                 break;
363                         }
364                 }
365         }
366         if (rx_done)
367                 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
368
369         return;
370 }
371
372 static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
373 {
374         struct hlist_head *head;
375         struct atm_vcc *vcc = NULL;
376         struct hlist_node *node;
377         struct sock *s;
378
379         read_lock(&vcc_sklist_lock);
380         head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
381         sk_for_each(s, node, head) {
382                 vcc = atm_sk(s);
383                 if (vcc->dev == dev && vcc->vci == vci &&
384                     vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE)
385                         goto out;
386         }
387         vcc = NULL;
388  out:
389         read_unlock(&vcc_sklist_lock);
390         return vcc;
391 }
392
393 static int list_vccs(int vci)
394 {
395         struct hlist_head *head;
396         struct atm_vcc *vcc;
397         struct hlist_node *node;
398         struct sock *s;
399         int num_found = 0;
400         int i;
401
402         read_lock(&vcc_sklist_lock);
403         if (vci != 0){
404                 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
405                 sk_for_each(s, node, head) {
406                         num_found ++;
407                         vcc = atm_sk(s);
408                         printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
409                                vcc->dev->number,
410                                vcc->vpi,
411                                vcc->vci);
412                 }
413         } else {
414                 for(i=0; i<32; i++){
415                         head = &vcc_hash[i];
416                         sk_for_each(s, node, head) {
417                                 num_found ++;
418                                 vcc = atm_sk(s);
419                                 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
420                                        vcc->dev->number,
421                                        vcc->vpi,
422                                        vcc->vci);
423                         }
424                 }
425         }
426         read_unlock(&vcc_sklist_lock);
427         return num_found;
428 }
429
430
431 static int popen(struct atm_vcc *vcc)
432 {
433         struct solos_card *card = vcc->dev->dev_data;
434         struct sk_buff *skb;
435         struct pkt_hdr *header;
436
437         skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
438         if (!skb && net_ratelimit()) {
439                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
440                 return -ENOMEM;
441         }
442         header = (void *)skb_put(skb, sizeof(*header));
443
444         header->size = cpu_to_le16(0);
445         header->vpi = cpu_to_le16(vcc->vpi);
446         header->vci = cpu_to_le16(vcc->vci);
447         header->type = cpu_to_le16(PKT_POPEN);
448
449         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
450
451 //      dev_dbg(&card->dev->dev, "Open for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
452         set_bit(ATM_VF_ADDR, &vcc->flags); // accept the vpi / vci
453         set_bit(ATM_VF_READY, &vcc->flags);
454         list_vccs(0);
455
456
457         return 0;
458 }
459
460 static void pclose(struct atm_vcc *vcc)
461 {
462         struct solos_card *card = vcc->dev->dev_data;
463         struct sk_buff *skb;
464         struct pkt_hdr *header;
465
466         skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
467         if (!skb) {
468                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
469                 return;
470         }
471         header = (void *)skb_put(skb, sizeof(*header));
472
473         header->size = cpu_to_le16(0);
474         header->vpi = cpu_to_le16(vcc->vpi);
475         header->vci = cpu_to_le16(vcc->vci);
476         header->type = cpu_to_le16(PKT_PCLOSE);
477
478         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
479
480 //      dev_dbg(&card->dev->dev, "Close for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
481
482         clear_bit(ATM_VF_ADDR, &vcc->flags);
483         clear_bit(ATM_VF_READY, &vcc->flags);
484
485         return;
486 }
487
488 static int print_buffer(struct sk_buff *buf)
489 {
490         int len,i;
491         char msg[500];
492         char item[10];
493
494         len = buf->len;
495         for (i = 0; i < len; i++){
496                 if(i % 8 == 0)
497                         sprintf(msg, "%02X: ", i);
498
499                 sprintf(item,"%02X ",*(buf->data + i));
500                 strcat(msg, item);
501                 if(i % 8 == 7) {
502                         sprintf(item, "\n");
503                         strcat(msg, item);
504                         printk(KERN_DEBUG "%s", msg);
505                 }
506         }
507         if (i % 8 != 0) {
508                 sprintf(item, "\n");
509                 strcat(msg, item);
510                 printk(KERN_DEBUG "%s", msg);
511         }
512         printk(KERN_DEBUG "\n");
513
514         return 0;
515 }
516
517 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
518                        struct atm_vcc *vcc)
519 {
520         int old_len;
521
522         *(void **)skb->cb = vcc;
523
524         spin_lock(&card->tx_queue_lock);
525         old_len = skb_queue_len(&card->tx_queue[port]);
526         skb_queue_tail(&card->tx_queue[port], skb);
527         spin_unlock(&card->tx_queue_lock);
528
529         /* If TX might need to be started, do so */
530         if (!old_len)
531                 fpga_tx(card);
532 }
533
534 static int fpga_tx(struct solos_card *card)
535 {
536         uint32_t tx_pending;
537         uint32_t tx_started = 0;
538         struct sk_buff *skb;
539         struct atm_vcc *vcc;
540         unsigned char port;
541         unsigned long flags;
542
543         spin_lock_irqsave(&card->tx_lock, flags);
544
545         tx_pending = ioread32(card->config_regs + FLAGS_ADDR);
546
547         dev_vdbg(&card->dev->dev, "TX Flags are %X\n", tx_pending);
548
549         for (port = 0; port < card->nr_ports; port++) {
550                 if (!(tx_pending & (1 << port))) {
551
552                         spin_lock(&card->tx_queue_lock);
553                         skb = skb_dequeue(&card->tx_queue[port]);
554                         spin_unlock(&card->tx_queue_lock);
555
556                         if (!skb)
557                                 continue;
558
559                         if (atmdebug) {
560                                 dev_info(&card->dev->dev, "Transmitted: port %d\n",
561                                          port);
562                                 print_buffer(skb);
563                         }
564                         memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
565
566                         vcc = *(void **)skb->cb;
567
568                         if (vcc) {
569                                 atomic_inc(&vcc->stats->tx);
570                                 solos_pop(vcc, skb);
571                         } else
572                                 dev_kfree_skb_irq(skb);
573
574                         tx_started |= 1 << port; //Set TX full flag
575                 }
576         }
577         if (tx_started)
578                 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
579
580         spin_unlock_irqrestore(&card->tx_lock, flags);
581         return 0;
582 }
583
584 static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
585 {
586         struct solos_card *card = vcc->dev->dev_data;
587         struct sk_buff *skb2 = NULL;
588         struct pkt_hdr *header;
589         int pktlen;
590
591         //dev_dbg(&card->dev->dev, "psend called.\n");
592         //dev_dbg(&card->dev->dev, "dev,vpi,vci = %d,%d,%d\n",SOLOS_CHAN(vcc->dev),vcc->vpi,vcc->vci);
593
594         if (debug) {
595                 skb2 = atm_alloc_charge(vcc, skb->len, GFP_ATOMIC);
596                 if (skb2) {
597                         memcpy(skb2->data, skb->data, skb->len);
598                         skb_put(skb2, skb->len);
599                         vcc->push(vcc, skb2);
600                         atomic_inc(&vcc->stats->rx);
601                 }
602                 atomic_inc(&vcc->stats->tx);
603                 solos_pop(vcc, skb);
604                 return 0;
605         }
606
607         pktlen = skb->len;
608         if (pktlen > (BUF_SIZE - sizeof(*header))) {
609                 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
610                 solos_pop(vcc, skb);
611                 return 0;
612         }
613
614         if (!skb_clone_writable(skb, sizeof(*header))) {
615                 int expand_by = 0;
616                 int ret;
617
618                 if (skb_headroom(skb) < sizeof(*header))
619                         expand_by = sizeof(*header) - skb_headroom(skb);
620
621                 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
622                 if (ret) {
623                         dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
624                         solos_pop(vcc, skb);
625                         return ret;
626                 }
627         }
628
629         header = (void *)skb_push(skb, sizeof(*header));
630
631         /* This does _not_ include the size of the header */
632         header->size = cpu_to_le16(pktlen);
633         header->vpi = cpu_to_le16(vcc->vpi);
634         header->vci = cpu_to_le16(vcc->vci);
635         header->type = cpu_to_le16(PKT_DATA);
636
637         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
638
639         return 0;
640 }
641
642 static struct atmdev_ops fpga_ops = {
643         .open =         popen,
644         .close =        pclose,
645         .ioctl =        NULL,
646         .getsockopt =   NULL,
647         .setsockopt =   NULL,
648         .send =         psend,
649         .send_oam =     NULL,
650         .phy_put =      NULL,
651         .phy_get =      NULL,
652         .change_qos =   NULL,
653         .proc_read =    NULL,
654         .owner =        THIS_MODULE
655 };
656
657 static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
658 {
659         int err, i;
660         uint16_t fpga_ver;
661         uint8_t major_ver, minor_ver;
662         uint32_t data32;
663         struct solos_card *card;
664
665         if (debug)
666                 return 0;
667
668         card = kzalloc(sizeof(*card), GFP_KERNEL);
669         if (!card)
670                 return -ENOMEM;
671
672         card->dev = dev;
673         init_waitqueue_head(&card->fw_wq);
674
675         err = pci_enable_device(dev);
676         if (err) {
677                 dev_warn(&dev->dev,  "Failed to enable PCI device\n");
678                 goto out;
679         }
680
681         err = pci_request_regions(dev, "solos");
682         if (err) {
683                 dev_warn(&dev->dev, "Failed to request regions\n");
684                 goto out;
685         }
686
687         card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
688         if (!card->config_regs) {
689                 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
690                 goto out_release_regions;
691         }
692         card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
693         if (!card->buffers) {
694                 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
695                 goto out_unmap_config;
696         }
697
698 //      for(i=0;i<64 ;i+=4){
699 //              data32=ioread32(card->buffers + i);
700 //              dev_dbg(&card->dev->dev, "%08lX\n",(unsigned long)data32);
701 //      }
702
703         //Fill Config Mem with zeros
704         for(i = 0; i < 128; i += 4)
705                 iowrite32(0, card->config_regs + i);
706
707         //Set RX empty flags
708         iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
709
710         data32 = ioread32(card->config_regs + FPGA_VER);
711         fpga_ver = (data32 & 0x0000FFFF);
712         major_ver = ((data32 & 0xFF000000) >> 24);
713         minor_ver = ((data32 & 0x00FF0000) >> 16);
714         dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
715                  major_ver, minor_ver, fpga_ver);
716
717         card->nr_ports = 2; /* FIXME: Detect daughterboard */
718
719         pci_set_drvdata(dev, card);
720
721         tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
722         spin_lock_init(&card->tx_lock);
723         spin_lock_init(&card->tx_queue_lock);
724         spin_lock_init(&card->cli_queue_lock);
725
726 /*
727         // Set Loopback mode
728         data32 = 0x00010000;
729         iowrite32(data32,card->config_regs + FLAGS_ADDR);
730 */
731 /*
732         // Fill Buffers with zeros
733         for (i = 0; i < BUF_SIZE * 8; i += 4)
734                 iowrite32(0, card->buffers + i);
735 */
736 /*
737         for(i = 0; i < (BUF_SIZE * 1); i += 4)
738                 iowrite32(0x12345678, card->buffers + i + (0*BUF_SIZE));
739         for(i = 0; i < (BUF_SIZE * 1); i += 4)
740                 iowrite32(0xabcdef98, card->buffers + i + (1*BUF_SIZE));
741
742         // Read Config Memory
743         printk(KERN_DEBUG "Reading Config MEM\n");
744         i = 0;
745         for(i = 0; i < 16; i++) {
746                 data32=ioread32(card->buffers + i*(BUF_SIZE/2));
747                 printk(KERN_ALERT "Addr: %lX Data: %08lX\n",
748                        (unsigned long)(addr_start + i*(BUF_SIZE/2)),
749                        (unsigned long)data32);
750         }
751 */
752         //dev_dbg(&card->dev->dev, "Requesting IRQ: %d\n",dev->irq);
753         err = request_irq(dev->irq, solos_irq, IRQF_DISABLED|IRQF_SHARED,
754                           "solos-pci", card);
755         if (err) {
756                 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
757                 goto out_unmap_both;
758         }
759
760         // Enable IRQs
761         iowrite32(1, card->config_regs + IRQ_EN_ADDR);
762
763         if (fpga_upgrade)
764                 flash_upgrade(card, 0);
765
766         if (firmware_upgrade)
767                 flash_upgrade(card, 1);
768
769         err = atm_init(card);
770         if (err)
771                 goto out_free_irq;
772
773         return 0;
774
775  out_free_irq:
776         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
777         free_irq(dev->irq, card);
778         tasklet_kill(&card->tlet);
779         
780  out_unmap_both:
781         pci_set_drvdata(dev, NULL);
782         pci_iounmap(dev, card->config_regs);
783  out_unmap_config:
784         pci_iounmap(dev, card->buffers);
785  out_release_regions:
786         pci_release_regions(dev);
787  out:
788         return err;
789 }
790
791 static int atm_init(struct solos_card *card)
792 {
793         int i;
794
795         for (i = 0; i < card->nr_ports; i++) {
796                 skb_queue_head_init(&card->tx_queue[i]);
797                 skb_queue_head_init(&card->cli_queue[i]);
798
799                 card->atmdev[i] = atm_dev_register("solos-pci", &fpga_ops, -1, NULL);
800                 if (!card->atmdev[i]) {
801                         dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
802                         atm_remove(card);
803                         return -ENODEV;
804                 }
805                 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
806                         dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
807
808                 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
809
810                 card->atmdev[i]->ci_range.vpi_bits = 8;
811                 card->atmdev[i]->ci_range.vci_bits = 16;
812                 card->atmdev[i]->dev_data = card;
813                 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
814         }
815         return 0;
816 }
817
818 static void atm_remove(struct solos_card *card)
819 {
820         int i;
821
822         for (i = 0; i < card->nr_ports; i++) {
823                 if (card->atmdev[i]) {
824                         dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
825                         atm_dev_deregister(card->atmdev[i]);
826                 }
827         }
828 }
829
830 static void fpga_remove(struct pci_dev *dev)
831 {
832         struct solos_card *card = pci_get_drvdata(dev);
833
834         if (debug)
835                 return;
836
837         atm_remove(card);
838
839         dev_vdbg(&dev->dev, "Freeing IRQ\n");
840         // Disable IRQs from FPGA
841         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
842         free_irq(dev->irq, card);
843         tasklet_kill(&card->tlet);
844
845         //      iowrite32(0x01,pciregs);
846         dev_vdbg(&dev->dev, "Unmapping PCI resource\n");
847         pci_iounmap(dev, card->buffers);
848         pci_iounmap(dev, card->config_regs);
849
850         dev_vdbg(&dev->dev, "Releasing PCI Region\n");
851         pci_release_regions(dev);
852         pci_disable_device(dev);
853
854         pci_set_drvdata(dev, NULL);
855         kfree(card);
856 //      dev_dbg(&card->dev->dev, "fpga_remove\n");
857         return;
858 }
859
860 static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
861         { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
862         { 0, }
863 };
864
865 MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
866
867 static struct pci_driver fpga_driver = {
868         .name =         "solos",
869         .id_table =     fpga_pci_tbl,
870         .probe =        fpga_probe,
871         .remove =       fpga_remove,
872 };
873
874
875 static int __init solos_pci_init(void)
876 {
877         printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
878         return pci_register_driver(&fpga_driver);
879 }
880
881 static void __exit solos_pci_exit(void)
882 {
883         pci_unregister_driver(&fpga_driver);
884         printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
885 }
886
887 module_init(solos_pci_init);
888 module_exit(solos_pci_exit);