solos: Add initial list of parameters
[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
375
376 #define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
377 #define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
378
379 #include "solos-attrlist.c"
380
381 #undef SOLOS_ATTR_RO
382 #undef SOLOS_ATTR_RW
383
384 #define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
385 #define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
386
387 static struct attribute *solos_attrs[] = {
388 #include "solos-attrlist.c"
389         NULL
390 };
391
392 static struct attribute_group solos_attr_group = {
393         .attrs = solos_attrs,
394         .name = "parameters",
395 };
396
397 static int flash_upgrade(struct solos_card *card, int chip)
398 {
399         const struct firmware *fw;
400         const char *fw_name;
401         uint32_t data32 = 0;
402         int blocksize = 0;
403         int numblocks = 0;
404         int offset;
405
406         if (chip == 0) {
407                 fw_name = "solos-FPGA.bin";
408                 blocksize = FPGA_BLOCK;
409         } else {
410                 fw_name = "solos-Firmware.bin";
411                 blocksize = SOLOS_BLOCK;
412         }
413
414         if (request_firmware(&fw, fw_name, &card->dev->dev))
415                 return -ENOENT;
416
417         dev_info(&card->dev->dev, "Flash upgrade starting\n");
418
419         numblocks = fw->size / blocksize;
420         dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
421         dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
422         
423         dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
424         iowrite32(1, card->config_regs + FPGA_MODE);
425         data32 = ioread32(card->config_regs + FPGA_MODE); 
426
427         /* Set mode to Chip Erase */
428         dev_info(&card->dev->dev, "Set FPGA Flash mode to %s Chip Erase\n",
429                  chip?"Solos":"FPGA");
430         iowrite32((chip * 2), card->config_regs + FLASH_MODE);
431
432
433         iowrite32(1, card->config_regs + WRITE_FLASH);
434         wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
435
436         for (offset = 0; offset < fw->size; offset += blocksize) {
437                 int i;
438
439                 /* Clear write flag */
440                 iowrite32(0, card->config_regs + WRITE_FLASH);
441
442                 /* Set mode to Block Write */
443                 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
444                 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
445
446                 /* Copy block to buffer, swapping each 16 bits */
447                 for(i = 0; i < blocksize; i += 4) {
448                         uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
449                         iowrite32(word, RX_BUF(card, 3) + i);
450                 }
451
452                 /* Specify block number and then trigger flash write */
453                 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
454                 iowrite32(1, card->config_regs + WRITE_FLASH);
455                 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
456         }
457
458         release_firmware(fw);
459         iowrite32(0, card->config_regs + WRITE_FLASH);
460         iowrite32(0, card->config_regs + FPGA_MODE);
461         iowrite32(0, card->config_regs + FLASH_MODE);
462         dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
463         return 0;
464 }
465
466 static irqreturn_t solos_irq(int irq, void *dev_id)
467 {
468         struct solos_card *card = dev_id;
469         int handled = 1;
470
471         //ACK IRQ
472         iowrite32(0, card->config_regs + IRQ_CLEAR);
473         //Disable IRQs from FPGA
474         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
475
476         if (card->atmdev[0])
477                 tasklet_schedule(&card->tlet);
478         else
479                 wake_up(&card->fw_wq);
480
481         //Enable IRQs from FPGA
482         iowrite32(1, card->config_regs + IRQ_EN_ADDR);
483         return IRQ_RETVAL(handled);
484 }
485
486 void solos_bh(unsigned long card_arg)
487 {
488         struct solos_card *card = (void *)card_arg;
489         int port;
490         uint32_t card_flags;
491         uint32_t tx_mask;
492         uint32_t rx_done = 0;
493
494         card_flags = ioread32(card->config_regs + FLAGS_ADDR);
495
496         /* The TX bits are set if the channel is busy; clear if not. We want to
497            invoke fpga_tx() unless _all_ the bits for active channels are set */
498         tx_mask = (1 << card->nr_ports) - 1;
499         if ((card_flags & tx_mask) != tx_mask)
500                 fpga_tx(card);
501
502         for (port = 0; port < card->nr_ports; port++) {
503                 if (card_flags & (0x10 << port)) {
504                         struct pkt_hdr header;
505                         struct sk_buff *skb;
506                         struct atm_vcc *vcc;
507                         int size;
508
509                         rx_done |= 0x10 << port;
510
511                         memcpy_fromio(&header, RX_BUF(card, port), sizeof(header));
512
513                         size = le16_to_cpu(header.size);
514
515                         skb = alloc_skb(size, GFP_ATOMIC);
516                         if (!skb) {
517                                 if (net_ratelimit())
518                                         dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
519                                 continue;
520                         }
521
522                         memcpy_fromio(skb_put(skb, size),
523                                       RX_BUF(card, port) + sizeof(header),
524                                       size);
525
526                         if (atmdebug) {
527                                 dev_info(&card->dev->dev, "Received: device %d\n", port);
528                                 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
529                                          size, le16_to_cpu(header.vpi),
530                                          le16_to_cpu(header.vci));
531                                 print_buffer(skb);
532                         }
533
534                         switch (le16_to_cpu(header.type)) {
535                         case PKT_DATA:
536                                 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header.vpi),
537                                                le16_to_cpu(header.vci));
538                                 if (!vcc) {
539                                         if (net_ratelimit())
540                                                 dev_warn(&card->dev->dev, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
541                                                          le16_to_cpu(header.vci), le16_to_cpu(header.vpi),
542                                                          port);
543                                         continue;
544                                 }
545                                 atm_charge(vcc, skb->truesize);
546                                 vcc->push(vcc, skb);
547                                 atomic_inc(&vcc->stats->rx);
548                                 break;
549
550                         case PKT_COMMAND:
551                         default: /* FIXME: Not really, surely? */
552                                 if (process_command(card, port, skb))
553                                         break;
554                                 spin_lock(&card->cli_queue_lock);
555                                 if (skb_queue_len(&card->cli_queue[port]) > 10) {
556                                         if (net_ratelimit())
557                                                 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
558                                                          port);
559                                 } else
560                                         skb_queue_tail(&card->cli_queue[port], skb);
561                                 spin_unlock(&card->cli_queue_lock);
562                                 break;
563                         }
564                 }
565         }
566         if (rx_done)
567                 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
568
569         return;
570 }
571
572 static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
573 {
574         struct hlist_head *head;
575         struct atm_vcc *vcc = NULL;
576         struct hlist_node *node;
577         struct sock *s;
578
579         read_lock(&vcc_sklist_lock);
580         head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
581         sk_for_each(s, node, head) {
582                 vcc = atm_sk(s);
583                 if (vcc->dev == dev && vcc->vci == vci &&
584                     vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE)
585                         goto out;
586         }
587         vcc = NULL;
588  out:
589         read_unlock(&vcc_sklist_lock);
590         return vcc;
591 }
592
593 static int list_vccs(int vci)
594 {
595         struct hlist_head *head;
596         struct atm_vcc *vcc;
597         struct hlist_node *node;
598         struct sock *s;
599         int num_found = 0;
600         int i;
601
602         read_lock(&vcc_sklist_lock);
603         if (vci != 0){
604                 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
605                 sk_for_each(s, node, head) {
606                         num_found ++;
607                         vcc = atm_sk(s);
608                         printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
609                                vcc->dev->number,
610                                vcc->vpi,
611                                vcc->vci);
612                 }
613         } else {
614                 for(i=0; i<32; i++){
615                         head = &vcc_hash[i];
616                         sk_for_each(s, node, head) {
617                                 num_found ++;
618                                 vcc = atm_sk(s);
619                                 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
620                                        vcc->dev->number,
621                                        vcc->vpi,
622                                        vcc->vci);
623                         }
624                 }
625         }
626         read_unlock(&vcc_sklist_lock);
627         return num_found;
628 }
629
630
631 static int popen(struct atm_vcc *vcc)
632 {
633         struct solos_card *card = vcc->dev->dev_data;
634         struct sk_buff *skb;
635         struct pkt_hdr *header;
636
637         skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
638         if (!skb && net_ratelimit()) {
639                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
640                 return -ENOMEM;
641         }
642         header = (void *)skb_put(skb, sizeof(*header));
643
644         header->size = cpu_to_le16(0);
645         header->vpi = cpu_to_le16(vcc->vpi);
646         header->vci = cpu_to_le16(vcc->vci);
647         header->type = cpu_to_le16(PKT_POPEN);
648
649         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
650
651 //      dev_dbg(&card->dev->dev, "Open for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
652         set_bit(ATM_VF_ADDR, &vcc->flags); // accept the vpi / vci
653         set_bit(ATM_VF_READY, &vcc->flags);
654         list_vccs(0);
655
656
657         return 0;
658 }
659
660 static void pclose(struct atm_vcc *vcc)
661 {
662         struct solos_card *card = vcc->dev->dev_data;
663         struct sk_buff *skb;
664         struct pkt_hdr *header;
665
666         skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
667         if (!skb) {
668                 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
669                 return;
670         }
671         header = (void *)skb_put(skb, sizeof(*header));
672
673         header->size = cpu_to_le16(0);
674         header->vpi = cpu_to_le16(vcc->vpi);
675         header->vci = cpu_to_le16(vcc->vci);
676         header->type = cpu_to_le16(PKT_PCLOSE);
677
678         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
679
680 //      dev_dbg(&card->dev->dev, "Close for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
681
682         clear_bit(ATM_VF_ADDR, &vcc->flags);
683         clear_bit(ATM_VF_READY, &vcc->flags);
684
685         return;
686 }
687
688 static int print_buffer(struct sk_buff *buf)
689 {
690         int len,i;
691         char msg[500];
692         char item[10];
693
694         len = buf->len;
695         for (i = 0; i < len; i++){
696                 if(i % 8 == 0)
697                         sprintf(msg, "%02X: ", i);
698
699                 sprintf(item,"%02X ",*(buf->data + i));
700                 strcat(msg, item);
701                 if(i % 8 == 7) {
702                         sprintf(item, "\n");
703                         strcat(msg, item);
704                         printk(KERN_DEBUG "%s", msg);
705                 }
706         }
707         if (i % 8 != 0) {
708                 sprintf(item, "\n");
709                 strcat(msg, item);
710                 printk(KERN_DEBUG "%s", msg);
711         }
712         printk(KERN_DEBUG "\n");
713
714         return 0;
715 }
716
717 static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
718                        struct atm_vcc *vcc)
719 {
720         int old_len;
721
722         *(void **)skb->cb = vcc;
723
724         spin_lock(&card->tx_queue_lock);
725         old_len = skb_queue_len(&card->tx_queue[port]);
726         skb_queue_tail(&card->tx_queue[port], skb);
727         spin_unlock(&card->tx_queue_lock);
728
729         /* If TX might need to be started, do so */
730         if (!old_len)
731                 fpga_tx(card);
732 }
733
734 static int fpga_tx(struct solos_card *card)
735 {
736         uint32_t tx_pending;
737         uint32_t tx_started = 0;
738         struct sk_buff *skb;
739         struct atm_vcc *vcc;
740         unsigned char port;
741         unsigned long flags;
742
743         spin_lock_irqsave(&card->tx_lock, flags);
744
745         tx_pending = ioread32(card->config_regs + FLAGS_ADDR);
746
747         dev_vdbg(&card->dev->dev, "TX Flags are %X\n", tx_pending);
748
749         for (port = 0; port < card->nr_ports; port++) {
750                 if (!(tx_pending & (1 << port))) {
751
752                         spin_lock(&card->tx_queue_lock);
753                         skb = skb_dequeue(&card->tx_queue[port]);
754                         spin_unlock(&card->tx_queue_lock);
755
756                         if (!skb)
757                                 continue;
758
759                         if (atmdebug) {
760                                 dev_info(&card->dev->dev, "Transmitted: port %d\n",
761                                          port);
762                                 print_buffer(skb);
763                         }
764                         memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
765
766                         vcc = *(void **)skb->cb;
767
768                         if (vcc) {
769                                 atomic_inc(&vcc->stats->tx);
770                                 solos_pop(vcc, skb);
771                         } else
772                                 dev_kfree_skb_irq(skb);
773
774                         tx_started |= 1 << port; //Set TX full flag
775                 }
776         }
777         if (tx_started)
778                 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
779
780         spin_unlock_irqrestore(&card->tx_lock, flags);
781         return 0;
782 }
783
784 static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
785 {
786         struct solos_card *card = vcc->dev->dev_data;
787         struct sk_buff *skb2 = NULL;
788         struct pkt_hdr *header;
789         int pktlen;
790
791         //dev_dbg(&card->dev->dev, "psend called.\n");
792         //dev_dbg(&card->dev->dev, "dev,vpi,vci = %d,%d,%d\n",SOLOS_CHAN(vcc->dev),vcc->vpi,vcc->vci);
793
794         if (debug) {
795                 skb2 = atm_alloc_charge(vcc, skb->len, GFP_ATOMIC);
796                 if (skb2) {
797                         memcpy(skb2->data, skb->data, skb->len);
798                         skb_put(skb2, skb->len);
799                         vcc->push(vcc, skb2);
800                         atomic_inc(&vcc->stats->rx);
801                 }
802                 atomic_inc(&vcc->stats->tx);
803                 solos_pop(vcc, skb);
804                 return 0;
805         }
806
807         pktlen = skb->len;
808         if (pktlen > (BUF_SIZE - sizeof(*header))) {
809                 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
810                 solos_pop(vcc, skb);
811                 return 0;
812         }
813
814         if (!skb_clone_writable(skb, sizeof(*header))) {
815                 int expand_by = 0;
816                 int ret;
817
818                 if (skb_headroom(skb) < sizeof(*header))
819                         expand_by = sizeof(*header) - skb_headroom(skb);
820
821                 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
822                 if (ret) {
823                         dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
824                         solos_pop(vcc, skb);
825                         return ret;
826                 }
827         }
828
829         header = (void *)skb_push(skb, sizeof(*header));
830
831         /* This does _not_ include the size of the header */
832         header->size = cpu_to_le16(pktlen);
833         header->vpi = cpu_to_le16(vcc->vpi);
834         header->vci = cpu_to_le16(vcc->vci);
835         header->type = cpu_to_le16(PKT_DATA);
836
837         fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
838
839         return 0;
840 }
841
842 static struct atmdev_ops fpga_ops = {
843         .open =         popen,
844         .close =        pclose,
845         .ioctl =        NULL,
846         .getsockopt =   NULL,
847         .setsockopt =   NULL,
848         .send =         psend,
849         .send_oam =     NULL,
850         .phy_put =      NULL,
851         .phy_get =      NULL,
852         .change_qos =   NULL,
853         .proc_read =    NULL,
854         .owner =        THIS_MODULE
855 };
856
857 static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
858 {
859         int err, i;
860         uint16_t fpga_ver;
861         uint8_t major_ver, minor_ver;
862         uint32_t data32;
863         struct solos_card *card;
864
865         if (debug)
866                 return 0;
867
868         card = kzalloc(sizeof(*card), GFP_KERNEL);
869         if (!card)
870                 return -ENOMEM;
871
872         card->dev = dev;
873         init_waitqueue_head(&card->fw_wq);
874         init_waitqueue_head(&card->param_wq);
875
876         err = pci_enable_device(dev);
877         if (err) {
878                 dev_warn(&dev->dev,  "Failed to enable PCI device\n");
879                 goto out;
880         }
881
882         err = pci_request_regions(dev, "solos");
883         if (err) {
884                 dev_warn(&dev->dev, "Failed to request regions\n");
885                 goto out;
886         }
887
888         card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
889         if (!card->config_regs) {
890                 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
891                 goto out_release_regions;
892         }
893         card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
894         if (!card->buffers) {
895                 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
896                 goto out_unmap_config;
897         }
898
899 //      for(i=0;i<64 ;i+=4){
900 //              data32=ioread32(card->buffers + i);
901 //              dev_dbg(&card->dev->dev, "%08lX\n",(unsigned long)data32);
902 //      }
903
904         //Fill Config Mem with zeros
905         for(i = 0; i < 128; i += 4)
906                 iowrite32(0, card->config_regs + i);
907
908         //Set RX empty flags
909         iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
910
911         data32 = ioread32(card->config_regs + FPGA_VER);
912         fpga_ver = (data32 & 0x0000FFFF);
913         major_ver = ((data32 & 0xFF000000) >> 24);
914         minor_ver = ((data32 & 0x00FF0000) >> 16);
915         dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
916                  major_ver, minor_ver, fpga_ver);
917
918         card->nr_ports = 2; /* FIXME: Detect daughterboard */
919
920         pci_set_drvdata(dev, card);
921
922         tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
923         spin_lock_init(&card->tx_lock);
924         spin_lock_init(&card->tx_queue_lock);
925         spin_lock_init(&card->cli_queue_lock);
926         spin_lock_init(&card->param_queue_lock);
927         INIT_LIST_HEAD(&card->param_queue);
928
929 /*
930         // Set Loopback mode
931         data32 = 0x00010000;
932         iowrite32(data32,card->config_regs + FLAGS_ADDR);
933 */
934 /*
935         // Fill Buffers with zeros
936         for (i = 0; i < BUF_SIZE * 8; i += 4)
937                 iowrite32(0, card->buffers + i);
938 */
939 /*
940         for(i = 0; i < (BUF_SIZE * 1); i += 4)
941                 iowrite32(0x12345678, card->buffers + i + (0*BUF_SIZE));
942         for(i = 0; i < (BUF_SIZE * 1); i += 4)
943                 iowrite32(0xabcdef98, card->buffers + i + (1*BUF_SIZE));
944
945         // Read Config Memory
946         printk(KERN_DEBUG "Reading Config MEM\n");
947         i = 0;
948         for(i = 0; i < 16; i++) {
949                 data32=ioread32(card->buffers + i*(BUF_SIZE/2));
950                 printk(KERN_ALERT "Addr: %lX Data: %08lX\n",
951                        (unsigned long)(addr_start + i*(BUF_SIZE/2)),
952                        (unsigned long)data32);
953         }
954 */
955         //dev_dbg(&card->dev->dev, "Requesting IRQ: %d\n",dev->irq);
956         err = request_irq(dev->irq, solos_irq, IRQF_DISABLED|IRQF_SHARED,
957                           "solos-pci", card);
958         if (err) {
959                 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
960                 goto out_unmap_both;
961         }
962
963         // Enable IRQs
964         iowrite32(1, card->config_regs + IRQ_EN_ADDR);
965
966         if (fpga_upgrade)
967                 flash_upgrade(card, 0);
968
969         if (firmware_upgrade)
970                 flash_upgrade(card, 1);
971
972         err = atm_init(card);
973         if (err)
974                 goto out_free_irq;
975
976         return 0;
977
978  out_free_irq:
979         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
980         free_irq(dev->irq, card);
981         tasklet_kill(&card->tlet);
982         
983  out_unmap_both:
984         pci_set_drvdata(dev, NULL);
985         pci_iounmap(dev, card->config_regs);
986  out_unmap_config:
987         pci_iounmap(dev, card->buffers);
988  out_release_regions:
989         pci_release_regions(dev);
990  out:
991         return err;
992 }
993
994 static int atm_init(struct solos_card *card)
995 {
996         int i;
997
998         for (i = 0; i < card->nr_ports; i++) {
999                 skb_queue_head_init(&card->tx_queue[i]);
1000                 skb_queue_head_init(&card->cli_queue[i]);
1001
1002                 card->atmdev[i] = atm_dev_register("solos-pci", &fpga_ops, -1, NULL);
1003                 if (!card->atmdev[i]) {
1004                         dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1005                         atm_remove(card);
1006                         return -ENODEV;
1007                 }
1008                 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1009                         dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
1010                 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1011                         dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
1012
1013                 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1014
1015                 card->atmdev[i]->ci_range.vpi_bits = 8;
1016                 card->atmdev[i]->ci_range.vci_bits = 16;
1017                 card->atmdev[i]->dev_data = card;
1018                 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
1019         }
1020         return 0;
1021 }
1022
1023 static void atm_remove(struct solos_card *card)
1024 {
1025         int i;
1026
1027         for (i = 0; i < card->nr_ports; i++) {
1028                 if (card->atmdev[i]) {
1029                         dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
1030                         atm_dev_deregister(card->atmdev[i]);
1031                 }
1032         }
1033 }
1034
1035 static void fpga_remove(struct pci_dev *dev)
1036 {
1037         struct solos_card *card = pci_get_drvdata(dev);
1038
1039         if (debug)
1040                 return;
1041
1042         atm_remove(card);
1043
1044         dev_vdbg(&dev->dev, "Freeing IRQ\n");
1045         // Disable IRQs from FPGA
1046         iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1047         free_irq(dev->irq, card);
1048         tasklet_kill(&card->tlet);
1049
1050         //      iowrite32(0x01,pciregs);
1051         dev_vdbg(&dev->dev, "Unmapping PCI resource\n");
1052         pci_iounmap(dev, card->buffers);
1053         pci_iounmap(dev, card->config_regs);
1054
1055         dev_vdbg(&dev->dev, "Releasing PCI Region\n");
1056         pci_release_regions(dev);
1057         pci_disable_device(dev);
1058
1059         pci_set_drvdata(dev, NULL);
1060         kfree(card);
1061 //      dev_dbg(&card->dev->dev, "fpga_remove\n");
1062         return;
1063 }
1064
1065 static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1066         { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1067         { 0, }
1068 };
1069
1070 MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1071
1072 static struct pci_driver fpga_driver = {
1073         .name =         "solos",
1074         .id_table =     fpga_pci_tbl,
1075         .probe =        fpga_probe,
1076         .remove =       fpga_remove,
1077 };
1078
1079
1080 static int __init solos_pci_init(void)
1081 {
1082         printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1083         return pci_register_driver(&fpga_driver);
1084 }
1085
1086 static void __exit solos_pci_exit(void)
1087 {
1088         pci_unregister_driver(&fpga_driver);
1089         printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1090 }
1091
1092 module_init(solos_pci_init);
1093 module_exit(solos_pci_exit);