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