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