blob: 32784d18d1f7c321fd88421aba83551fd4463b4d [file] [log] [blame]
David Woodhouse9c540042008-12-23 04:09:02 +00001/*
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>
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000012 * Treker Chen <treker@xrio.com>
David Woodhouse9c540042008-12-23 04:09:02 +000013 *
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>
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000040#include <linux/firmware.h>
David Woodhouse01e2ffa2009-01-27 16:20:04 +110041#include <linux/ctype.h>
42#include <linux/swab.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
David Woodhouse9c540042008-12-23 04:09:02 +000044
Nathan Williams13af8162012-12-19 11:01:20 +000045#define VERSION "1.04"
46#define DRIVER_VERSION 0x01
David Woodhouse9c540042008-12-23 04:09:02 +000047#define PTAG "solos-pci"
48
49#define CONFIG_RAM_SIZE 128
50#define FLAGS_ADDR 0x7C
51#define IRQ_EN_ADDR 0x78
52#define FPGA_VER 0x74
53#define IRQ_CLEAR 0x70
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000054#define WRITE_FLASH 0x6C
55#define PORTS 0x68
56#define FLASH_BLOCK 0x64
57#define FLASH_BUSY 0x60
58#define FPGA_MODE 0x5C
59#define FLASH_MODE 0x58
Nathan Williamsf9baad02012-12-19 11:01:18 +000060#define GPIO_STATUS 0x54
Nathan Williams13af8162012-12-19 11:01:20 +000061#define DRIVER_VER 0x50
David Woodhouse90937232009-01-28 16:46:56 +110062#define TX_DMA_ADDR(port) (0x40 + (4 * (port)))
63#define RX_DMA_ADDR(port) (0x30 + (4 * (port)))
David Woodhouse9c540042008-12-23 04:09:02 +000064
65#define DATA_RAM_SIZE 32768
Nathan Williams4dbedf42009-03-25 20:27:37 +110066#define BUF_SIZE 2048
67#define OLD_BUF_SIZE 4096 /* For FPGA versions <= 2*/
Nathan Williams13af8162012-12-19 11:01:20 +000068/* Old boards use ATMEL AD45DB161D flash */
69#define ATMEL_FPGA_PAGE 528 /* FPGA flash page size*/
70#define ATMEL_SOLOS_PAGE 512 /* Solos flash page size*/
71#define ATMEL_FPGA_BLOCK (ATMEL_FPGA_PAGE * 8) /* FPGA block size*/
72#define ATMEL_SOLOS_BLOCK (ATMEL_SOLOS_PAGE * 8) /* Solos block size*/
73/* Current boards use M25P/M25PE SPI flash */
74#define SPI_FLASH_BLOCK (256 * 64)
David Woodhouse9c540042008-12-23 04:09:02 +000075
Nathan Williams4dbedf42009-03-25 20:27:37 +110076#define RX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2)
77#define TX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2 + (card->buffer_size))
78#define FLASH_BUF ((card->buffers) + 4*(card->buffer_size)*2)
David Woodhouse9c540042008-12-23 04:09:02 +000079
David Woodhouseeaf83e32009-01-29 10:51:11 +110080#define RX_DMA_SIZE 2048
81
Nathan Williams4dbedf42009-03-25 20:27:37 +110082#define FPGA_VERSION(a,b) (((a) << 8) + (b))
83#define LEGACY_BUFFERS 2
84#define DMA_SUPPORTED 4
85
David Woodhousecc3657e2009-01-30 14:23:22 +110086static int reset = 0;
David Woodhouse9c540042008-12-23 04:09:02 +000087static int atmdebug = 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000088static int firmware_upgrade = 0;
89static int fpga_upgrade = 0;
Nathan Williams4dbedf42009-03-25 20:27:37 +110090static int db_firmware_upgrade = 0;
91static int db_fpga_upgrade = 0;
David Woodhouse9c540042008-12-23 04:09:02 +000092
93struct pkt_hdr {
94 __le16 size;
95 __le16 vpi;
96 __le16 vci;
97 __le16 type;
98};
99
David Woodhouse90937232009-01-28 16:46:56 +1100100struct solos_skb_cb {
101 struct atm_vcc *vcc;
102 uint32_t dma_addr;
103};
104
105
106#define SKB_CB(skb) ((struct solos_skb_cb *)skb->cb)
107
David Woodhouse9c540042008-12-23 04:09:02 +0000108#define PKT_DATA 0
109#define PKT_COMMAND 1
110#define PKT_POPEN 3
111#define PKT_PCLOSE 4
David Woodhouse87ebb182009-01-27 20:02:30 +1100112#define PKT_STATUS 5
David Woodhouse9c540042008-12-23 04:09:02 +0000113
114struct solos_card {
115 void __iomem *config_regs;
116 void __iomem *buffers;
117 int nr_ports;
David Woodhousef69e4172009-01-29 11:10:58 +1100118 int tx_mask;
David Woodhouse9c540042008-12-23 04:09:02 +0000119 struct pci_dev *dev;
120 struct atm_dev *atmdev[4];
121 struct tasklet_struct tlet;
122 spinlock_t tx_lock;
123 spinlock_t tx_queue_lock;
124 spinlock_t cli_queue_lock;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100125 spinlock_t param_queue_lock;
126 struct list_head param_queue;
David Woodhouse9c540042008-12-23 04:09:02 +0000127 struct sk_buff_head tx_queue[4];
128 struct sk_buff_head cli_queue[4];
David Woodhouse90937232009-01-28 16:46:56 +1100129 struct sk_buff *tx_skb[4];
130 struct sk_buff *rx_skb[4];
David Woodhouse152a2a82012-12-19 11:01:21 +0000131 unsigned char *dma_bounce;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100132 wait_queue_head_t param_wq;
David Woodhousefa755b92009-01-27 14:16:12 +1100133 wait_queue_head_t fw_wq;
David Woodhouse90937232009-01-28 16:46:56 +1100134 int using_dma;
David Woodhouse152a2a82012-12-19 11:01:21 +0000135 int dma_alignment;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100136 int fpga_version;
137 int buffer_size;
Nathan Williams13af8162012-12-19 11:01:20 +0000138 int atmel_flash;
David Woodhouse9c540042008-12-23 04:09:02 +0000139};
140
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100141
142struct solos_param {
143 struct list_head list;
144 pid_t pid;
145 int port;
146 struct sk_buff *response;
David Woodhouse9c540042008-12-23 04:09:02 +0000147};
148
149#define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
150
151MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
152MODULE_DESCRIPTION("Solos PCI driver");
153MODULE_VERSION(VERSION);
154MODULE_LICENSE("GPL");
Ben Hutchings9fca79d2009-11-07 11:40:32 +0000155MODULE_FIRMWARE("solos-FPGA.bin");
156MODULE_FIRMWARE("solos-Firmware.bin");
157MODULE_FIRMWARE("solos-db-FPGA.bin");
David Woodhousecc3657e2009-01-30 14:23:22 +1100158MODULE_PARM_DESC(reset, "Reset Solos chips on startup");
David Woodhouse9c540042008-12-23 04:09:02 +0000159MODULE_PARM_DESC(atmdebug, "Print ATM data");
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000160MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
161MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100162MODULE_PARM_DESC(db_firmware_upgrade, "Initiate daughter board Solos firmware upgrade");
163MODULE_PARM_DESC(db_fpga_upgrade, "Initiate daughter board FPGA upgrade");
David Woodhousecc3657e2009-01-30 14:23:22 +1100164module_param(reset, int, 0444);
Simon Farnsworth4306cad2009-01-19 21:19:29 +0000165module_param(atmdebug, int, 0644);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000166module_param(firmware_upgrade, int, 0444);
167module_param(fpga_upgrade, int, 0444);
Nathan Williams4dbedf42009-03-25 20:27:37 +1100168module_param(db_firmware_upgrade, int, 0444);
169module_param(db_fpga_upgrade, int, 0444);
David Woodhouse9c540042008-12-23 04:09:02 +0000170
171static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
172 struct atm_vcc *vcc);
David Woodhouse35c22212009-01-29 14:08:27 +1100173static uint32_t fpga_tx(struct solos_card *);
David Woodhouse9c540042008-12-23 04:09:02 +0000174static irqreturn_t solos_irq(int irq, void *dev_id);
175static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
Dan Williamsd9ca6762010-12-08 19:40:47 +0000176static int atm_init(struct solos_card *, struct device *);
David Woodhouse9c540042008-12-23 04:09:02 +0000177static void atm_remove(struct solos_card *);
178static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
179static void solos_bh(unsigned long);
180static int print_buffer(struct sk_buff *buf);
181
182static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
183{
184 if (vcc->pop)
185 vcc->pop(vcc, skb);
186 else
187 dev_kfree_skb_any(skb);
188}
189
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100190static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
191 char *buf)
192{
193 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
194 struct solos_card *card = atmdev->dev_data;
195 struct solos_param prm;
196 struct sk_buff *skb;
197 struct pkt_hdr *header;
198 int buflen;
199
200 buflen = strlen(attr->attr.name) + 10;
201
David Woodhouse3456b222009-01-28 10:39:23 +1100202 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100203 if (!skb) {
204 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
205 return -ENOMEM;
206 }
207
208 header = (void *)skb_put(skb, sizeof(*header));
209
210 buflen = snprintf((void *)&header[1], buflen - 1,
211 "L%05d\n%s\n", current->pid, attr->attr.name);
212 skb_put(skb, buflen);
213
214 header->size = cpu_to_le16(buflen);
215 header->vpi = cpu_to_le16(0);
216 header->vci = cpu_to_le16(0);
217 header->type = cpu_to_le16(PKT_COMMAND);
218
219 prm.pid = current->pid;
220 prm.response = NULL;
221 prm.port = SOLOS_CHAN(atmdev);
222
223 spin_lock_irq(&card->param_queue_lock);
224 list_add(&prm.list, &card->param_queue);
225 spin_unlock_irq(&card->param_queue_lock);
226
227 fpga_queue(card, prm.port, skb, NULL);
228
229 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
230
231 spin_lock_irq(&card->param_queue_lock);
232 list_del(&prm.list);
233 spin_unlock_irq(&card->param_queue_lock);
234
235 if (!prm.response)
236 return -EIO;
237
238 buflen = prm.response->len;
239 memcpy(buf, prm.response->data, buflen);
240 kfree_skb(prm.response);
241
242 return buflen;
243}
244
245static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
246 const char *buf, size_t count)
247{
248 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
249 struct solos_card *card = atmdev->dev_data;
250 struct solos_param prm;
251 struct sk_buff *skb;
252 struct pkt_hdr *header;
253 int buflen;
254 ssize_t ret;
255
256 buflen = strlen(attr->attr.name) + 11 + count;
257
David Woodhouse3456b222009-01-28 10:39:23 +1100258 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100259 if (!skb) {
260 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
261 return -ENOMEM;
262 }
263
264 header = (void *)skb_put(skb, sizeof(*header));
265
266 buflen = snprintf((void *)&header[1], buflen - 1,
267 "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
268
269 skb_put(skb, buflen);
270 header->size = cpu_to_le16(buflen);
271 header->vpi = cpu_to_le16(0);
272 header->vci = cpu_to_le16(0);
273 header->type = cpu_to_le16(PKT_COMMAND);
274
275 prm.pid = current->pid;
276 prm.response = NULL;
277 prm.port = SOLOS_CHAN(atmdev);
278
279 spin_lock_irq(&card->param_queue_lock);
280 list_add(&prm.list, &card->param_queue);
281 spin_unlock_irq(&card->param_queue_lock);
282
283 fpga_queue(card, prm.port, skb, NULL);
284
285 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
286
287 spin_lock_irq(&card->param_queue_lock);
288 list_del(&prm.list);
289 spin_unlock_irq(&card->param_queue_lock);
290
291 skb = prm.response;
292
293 if (!skb)
294 return -EIO;
295
296 buflen = skb->len;
297
298 /* Sometimes it has a newline, sometimes it doesn't. */
299 if (skb->data[buflen - 1] == '\n')
300 buflen--;
301
302 if (buflen == 2 && !strncmp(skb->data, "OK", 2))
303 ret = count;
304 else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
305 ret = -EIO;
306 else {
307 /* We know we have enough space allocated for this; we allocated
308 it ourselves */
309 skb->data[buflen] = 0;
310
311 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
312 skb->data);
313 ret = -EIO;
314 }
315 kfree_skb(skb);
316
317 return ret;
318}
319
David Woodhouse87ebb182009-01-27 20:02:30 +1100320static char *next_string(struct sk_buff *skb)
321{
322 int i = 0;
323 char *this = skb->data;
David Woodhousec6428e52009-01-29 12:17:09 +1100324
325 for (i = 0; i < skb->len; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +1100326 if (this[i] == '\n') {
327 this[i] = 0;
David Woodhousec6428e52009-01-29 12:17:09 +1100328 skb_pull(skb, i + 1);
David Woodhouse87ebb182009-01-27 20:02:30 +1100329 return this;
330 }
David Woodhousec6428e52009-01-29 12:17:09 +1100331 if (!isprint(this[i]))
332 return NULL;
David Woodhouse87ebb182009-01-27 20:02:30 +1100333 }
334 return NULL;
335}
336
337/*
338 * Status packet has fields separated by \n, starting with a version number
339 * for the information therein. Fields are....
340 *
341 * packet version
David Woodhouse87ebb182009-01-27 20:02:30 +1100342 * RxBitRate (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100343 * TxBitRate (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100344 * State (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100345 * LocalSNRMargin (version >= 1)
346 * LocalLineAttn (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100347 */
348static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
349{
David Woodhousef87b2ed2009-01-30 14:31:36 +1100350 char *str, *end, *state_str, *snr, *attn;
351 int ver, rate_up, rate_down;
David Woodhouse87ebb182009-01-27 20:02:30 +1100352
353 if (!card->atmdev[port])
354 return -ENODEV;
355
356 str = next_string(skb);
357 if (!str)
358 return -EIO;
359
360 ver = simple_strtol(str, NULL, 10);
361 if (ver < 1) {
362 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
363 ver);
364 return -EIO;
365 }
366
367 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100368 if (!str)
369 return -EIO;
David Woodhouse95852f42009-01-30 14:23:52 +1100370 if (!strcmp(str, "ERROR")) {
371 dev_dbg(&card->dev->dev, "Status packet indicated Solos error on port %d (starting up?)\n",
372 port);
373 return 0;
374 }
375
David Woodhousef87b2ed2009-01-30 14:31:36 +1100376 rate_down = simple_strtol(str, &end, 10);
David Woodhouse87ebb182009-01-27 20:02:30 +1100377 if (*end)
378 return -EIO;
379
380 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100381 if (!str)
382 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100383 rate_up = simple_strtol(str, &end, 10);
David Woodhouse87ebb182009-01-27 20:02:30 +1100384 if (*end)
385 return -EIO;
386
David Woodhouseaf780652009-01-28 10:22:57 +1100387 state_str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100388 if (!state_str)
389 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100390
391 /* Anything but 'Showtime' is down */
392 if (strcmp(state_str, "Showtime")) {
Karl Hiramoto49d49102010-07-08 20:55:34 +0000393 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
David Woodhousef87b2ed2009-01-30 14:31:36 +1100394 dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
395 return 0;
David Woodhouse1e615df2009-01-27 21:47:47 +1100396 }
David Woodhouse87ebb182009-01-27 20:02:30 +1100397
David Woodhousef87b2ed2009-01-30 14:31:36 +1100398 snr = next_string(skb);
Julia Lawall6cf57672009-07-27 11:38:52 -0700399 if (!snr)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100400 return -EIO;
401 attn = next_string(skb);
402 if (!attn)
403 return -EIO;
David Woodhouseaf780652009-01-28 10:22:57 +1100404
David Woodhousef87b2ed2009-01-30 14:31:36 +1100405 dev_info(&card->dev->dev, "Port %d: %s @%d/%d kb/s%s%s%s%s\n",
406 port, state_str, rate_down/1000, rate_up/1000,
407 snr[0]?", SNR ":"", snr, attn[0]?", Attn ":"", attn);
408
David Woodhousec6428e52009-01-29 12:17:09 +1100409 card->atmdev[port]->link_rate = rate_down / 424;
Karl Hiramoto49d49102010-07-08 20:55:34 +0000410 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +1100411
David Woodhouse87ebb182009-01-27 20:02:30 +1100412 return 0;
413}
414
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100415static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
416{
417 struct solos_param *prm;
418 unsigned long flags;
419 int cmdpid;
420 int found = 0;
421
422 if (skb->len < 7)
423 return 0;
424
425 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
426 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
427 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
428 skb->data[6] != '\n')
429 return 0;
430
431 cmdpid = simple_strtol(&skb->data[1], NULL, 10);
432
433 spin_lock_irqsave(&card->param_queue_lock, flags);
434 list_for_each_entry(prm, &card->param_queue, list) {
435 if (prm->port == port && prm->pid == cmdpid) {
436 prm->response = skb;
437 skb_pull(skb, 7);
438 wake_up(&card->param_wq);
439 found = 1;
440 break;
441 }
442 }
443 spin_unlock_irqrestore(&card->param_queue_lock, flags);
444 return found;
445}
446
David Woodhouse9c540042008-12-23 04:09:02 +0000447static ssize_t console_show(struct device *dev, struct device_attribute *attr,
448 char *buf)
449{
450 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
451 struct solos_card *card = atmdev->dev_data;
452 struct sk_buff *skb;
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000453 unsigned int len;
David Woodhouse9c540042008-12-23 04:09:02 +0000454
455 spin_lock(&card->cli_queue_lock);
456 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
457 spin_unlock(&card->cli_queue_lock);
458 if(skb == NULL)
459 return sprintf(buf, "No data.\n");
460
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000461 len = skb->len;
462 memcpy(buf, skb->data, len);
David Woodhouse9c540042008-12-23 04:09:02 +0000463
464 kfree_skb(skb);
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000465 return len;
David Woodhouse9c540042008-12-23 04:09:02 +0000466}
467
468static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
469{
470 struct sk_buff *skb;
471 struct pkt_hdr *header;
472
David Woodhouse9c540042008-12-23 04:09:02 +0000473 if (size > (BUF_SIZE - sizeof(*header))) {
474 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
475 return 0;
476 }
477 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
478 if (!skb) {
479 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
480 return 0;
481 }
482
483 header = (void *)skb_put(skb, sizeof(*header));
484
485 header->size = cpu_to_le16(size);
486 header->vpi = cpu_to_le16(0);
487 header->vci = cpu_to_le16(0);
488 header->type = cpu_to_le16(PKT_COMMAND);
489
490 memcpy(skb_put(skb, size), buf, size);
491
492 fpga_queue(card, dev, skb, NULL);
493
494 return 0;
495}
496
497static ssize_t console_store(struct device *dev, struct device_attribute *attr,
498 const char *buf, size_t count)
499{
500 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
501 struct solos_card *card = atmdev->dev_data;
502 int err;
503
504 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
505
506 return err?:count;
507}
508
Nathan Williamsf9baad02012-12-19 11:01:18 +0000509struct geos_gpio_attr {
510 struct device_attribute attr;
511 int offset;
512};
513
514#define SOLOS_GPIO_ATTR(_name, _mode, _show, _store, _offset) \
515 struct geos_gpio_attr gpio_attr_##_name = { \
516 .attr = __ATTR(_name, _mode, _show, _store), \
517 .offset = _offset }
518
519static ssize_t geos_gpio_store(struct device *dev, struct device_attribute *attr,
520 const char *buf, size_t count)
521{
522 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
523 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
524 struct solos_card *card = pci_get_drvdata(pdev);
525 uint32_t data32;
526
527 if (count != 1 && (count != 2 || buf[1] != '\n'))
528 return -EINVAL;
529
530 spin_lock_irq(&card->param_queue_lock);
531 data32 = ioread32(card->config_regs + GPIO_STATUS);
532 if (buf[0] == '1') {
533 data32 |= 1 << gattr->offset;
534 iowrite32(data32, card->config_regs + GPIO_STATUS);
535 } else if (buf[0] == '0') {
536 data32 &= ~(1 << gattr->offset);
537 iowrite32(data32, card->config_regs + GPIO_STATUS);
538 } else {
539 count = -EINVAL;
540 }
Dan Carpenter29042072012-12-19 21:48:45 +0000541 spin_unlock_irq(&card->param_queue_lock);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000542 return count;
543}
544
545static ssize_t geos_gpio_show(struct device *dev, struct device_attribute *attr,
546 char *buf)
547{
548 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
549 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
550 struct solos_card *card = pci_get_drvdata(pdev);
551 uint32_t data32;
552
553 data32 = ioread32(card->config_regs + GPIO_STATUS);
554 data32 = (data32 >> gattr->offset) & 1;
555
556 return sprintf(buf, "%d\n", data32);
557}
558
559static ssize_t hardware_show(struct device *dev, struct device_attribute *attr,
560 char *buf)
561{
562 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
563 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
564 struct solos_card *card = pci_get_drvdata(pdev);
565 uint32_t data32;
566
567 data32 = ioread32(card->config_regs + GPIO_STATUS);
568 switch (gattr->offset) {
569 case 0:
570 /* HardwareVersion */
571 data32 = data32 & 0x1F;
572 break;
573 case 1:
574 /* HardwareVariant */
575 data32 = (data32 >> 5) & 0x0F;
576 break;
577 }
578 return sprintf(buf, "%d\n", data32);
579}
580
David Woodhouse9c540042008-12-23 04:09:02 +0000581static DEVICE_ATTR(console, 0644, console_show, console_store);
582
David Woodhoused057f0a2009-01-27 16:52:07 +1100583
584#define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
585#define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
586
587#include "solos-attrlist.c"
588
Nathan Williamsf9baad02012-12-19 11:01:18 +0000589static SOLOS_GPIO_ATTR(GPIO1, 0644, geos_gpio_show, geos_gpio_store, 9);
590static SOLOS_GPIO_ATTR(GPIO2, 0644, geos_gpio_show, geos_gpio_store, 10);
591static SOLOS_GPIO_ATTR(GPIO3, 0644, geos_gpio_show, geos_gpio_store, 11);
592static SOLOS_GPIO_ATTR(GPIO4, 0644, geos_gpio_show, geos_gpio_store, 12);
593static SOLOS_GPIO_ATTR(GPIO5, 0644, geos_gpio_show, geos_gpio_store, 13);
594static SOLOS_GPIO_ATTR(PushButton, 0444, geos_gpio_show, NULL, 14);
595static SOLOS_GPIO_ATTR(HardwareVersion, 0444, hardware_show, NULL, 0);
596static SOLOS_GPIO_ATTR(HardwareVariant, 0444, hardware_show, NULL, 1);
David Woodhoused057f0a2009-01-27 16:52:07 +1100597#undef SOLOS_ATTR_RO
598#undef SOLOS_ATTR_RW
599
600#define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
601#define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
602
603static struct attribute *solos_attrs[] = {
604#include "solos-attrlist.c"
605 NULL
606};
607
608static struct attribute_group solos_attr_group = {
609 .attrs = solos_attrs,
610 .name = "parameters",
611};
David Woodhouse9c540042008-12-23 04:09:02 +0000612
Nathan Williamsf9baad02012-12-19 11:01:18 +0000613static struct attribute *gpio_attrs[] = {
614 &gpio_attr_GPIO1.attr.attr,
615 &gpio_attr_GPIO2.attr.attr,
616 &gpio_attr_GPIO3.attr.attr,
617 &gpio_attr_GPIO4.attr.attr,
618 &gpio_attr_GPIO5.attr.attr,
619 &gpio_attr_PushButton.attr.attr,
620 &gpio_attr_HardwareVersion.attr.attr,
621 &gpio_attr_HardwareVariant.attr.attr,
622 NULL
623};
624
625static struct attribute_group gpio_attr_group = {
626 .attrs = gpio_attrs,
627 .name = "gpio",
628};
629
David Woodhousefa755b92009-01-27 14:16:12 +1100630static int flash_upgrade(struct solos_card *card, int chip)
631{
632 const struct firmware *fw;
633 const char *fw_name;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000634 int blocksize = 0;
635 int numblocks = 0;
David Woodhousefa755b92009-01-27 14:16:12 +1100636 int offset;
637
Andrew Morton7adcdb42009-11-17 12:46:44 +0000638 switch (chip) {
639 case 0:
David Woodhousefa755b92009-01-27 14:16:12 +1100640 fw_name = "solos-FPGA.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000641 if (card->atmel_flash)
642 blocksize = ATMEL_FPGA_BLOCK;
643 else
644 blocksize = SPI_FLASH_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000645 break;
646 case 1:
David Woodhousefa755b92009-01-27 14:16:12 +1100647 fw_name = "solos-Firmware.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000648 if (card->atmel_flash)
649 blocksize = ATMEL_SOLOS_BLOCK;
650 else
651 blocksize = SPI_FLASH_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000652 break;
653 case 2:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100654 if (card->fpga_version > LEGACY_BUFFERS){
655 fw_name = "solos-db-FPGA.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000656 if (card->atmel_flash)
657 blocksize = ATMEL_FPGA_BLOCK;
658 else
659 blocksize = SPI_FLASH_BLOCK;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100660 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000661 dev_info(&card->dev->dev, "FPGA version doesn't support"
662 " daughter board upgrades\n");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100663 return -EPERM;
664 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000665 break;
666 case 3:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100667 if (card->fpga_version > LEGACY_BUFFERS){
668 fw_name = "solos-Firmware.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000669 if (card->atmel_flash)
670 blocksize = ATMEL_SOLOS_BLOCK;
671 else
672 blocksize = SPI_FLASH_BLOCK;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100673 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000674 dev_info(&card->dev->dev, "FPGA version doesn't support"
675 " daughter board upgrades\n");
676 return -EPERM;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100677 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000678 break;
679 default:
680 return -ENODEV;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100681 }
David Woodhousefa755b92009-01-27 14:16:12 +1100682
683 if (request_firmware(&fw, fw_name, &card->dev->dev))
684 return -ENOENT;
685
686 dev_info(&card->dev->dev, "Flash upgrade starting\n");
687
Nathan Williams13af8162012-12-19 11:01:20 +0000688 /* New FPGAs require driver version before permitting flash upgrades */
689 iowrite32(DRIVER_VERSION, card->config_regs + DRIVER_VER);
690
David Woodhousefa755b92009-01-27 14:16:12 +1100691 numblocks = fw->size / blocksize;
692 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000693 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
694
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000695 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
696 iowrite32(1, card->config_regs + FPGA_MODE);
David S. Miller06091ed2011-04-17 00:11:25 -0700697 (void) ioread32(card->config_regs + FPGA_MODE);
David Woodhousefa755b92009-01-27 14:16:12 +1100698
699 /* Set mode to Chip Erase */
Nathan Williams4dbedf42009-03-25 20:27:37 +1100700 if(chip == 0 || chip == 2)
701 dev_info(&card->dev->dev, "Set FPGA Flash mode to FPGA Chip Erase\n");
702 if(chip == 1 || chip == 3)
703 dev_info(&card->dev->dev, "Set FPGA Flash mode to Solos Chip Erase\n");
David Woodhousefa755b92009-01-27 14:16:12 +1100704 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
705
706
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000707 iowrite32(1, card->config_regs + WRITE_FLASH);
David Woodhousefa755b92009-01-27 14:16:12 +1100708 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000709
David Woodhousefa755b92009-01-27 14:16:12 +1100710 for (offset = 0; offset < fw->size; offset += blocksize) {
711 int i;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000712
David Woodhousefa755b92009-01-27 14:16:12 +1100713 /* Clear write flag */
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000714 iowrite32(0, card->config_regs + WRITE_FLASH);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000715
David Woodhousefa755b92009-01-27 14:16:12 +1100716 /* Set mode to Block Write */
717 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
718 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
719
Nathan Williams13af8162012-12-19 11:01:20 +0000720 /* Copy block to buffer, swapping each 16 bits for Atmel flash */
David Woodhousefa755b92009-01-27 14:16:12 +1100721 for(i = 0; i < blocksize; i += 4) {
Nathan Williams13af8162012-12-19 11:01:20 +0000722 uint32_t word;
723 if (card->atmel_flash)
724 word = swahb32p((uint32_t *)(fw->data + offset + i));
725 else
726 word = *(uint32_t *)(fw->data + offset + i);
Nathan Williams4dbedf42009-03-25 20:27:37 +1100727 if(card->fpga_version > LEGACY_BUFFERS)
728 iowrite32(word, FLASH_BUF + i);
729 else
730 iowrite32(word, RX_BUF(card, 3) + i);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000731 }
732
David Woodhousefa755b92009-01-27 14:16:12 +1100733 /* Specify block number and then trigger flash write */
734 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
735 iowrite32(1, card->config_regs + WRITE_FLASH);
736 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000737 }
738
David Woodhousefa755b92009-01-27 14:16:12 +1100739 release_firmware(fw);
740 iowrite32(0, card->config_regs + WRITE_FLASH);
741 iowrite32(0, card->config_regs + FPGA_MODE);
742 iowrite32(0, card->config_regs + FLASH_MODE);
743 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
744 return 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000745}
746
David Woodhouse9c540042008-12-23 04:09:02 +0000747static irqreturn_t solos_irq(int irq, void *dev_id)
748{
749 struct solos_card *card = dev_id;
750 int handled = 1;
751
David Woodhouse9c540042008-12-23 04:09:02 +0000752 iowrite32(0, card->config_regs + IRQ_CLEAR);
David Woodhouse9c540042008-12-23 04:09:02 +0000753
David Woodhouse35c22212009-01-29 14:08:27 +1100754 /* If we're up and running, just kick the tasklet to process TX/RX */
David Woodhousefa755b92009-01-27 14:16:12 +1100755 if (card->atmdev[0])
David Woodhouse9c540042008-12-23 04:09:02 +0000756 tasklet_schedule(&card->tlet);
David Woodhousefa755b92009-01-27 14:16:12 +1100757 else
758 wake_up(&card->fw_wq);
David Woodhouse9c540042008-12-23 04:09:02 +0000759
David Woodhouse9c540042008-12-23 04:09:02 +0000760 return IRQ_RETVAL(handled);
761}
762
763void solos_bh(unsigned long card_arg)
764{
765 struct solos_card *card = (void *)card_arg;
David Woodhouse9c540042008-12-23 04:09:02 +0000766 uint32_t card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000767 uint32_t rx_done = 0;
David Woodhouse35c22212009-01-29 14:08:27 +1100768 int port;
David Woodhouse9c540042008-12-23 04:09:02 +0000769
David Woodhouse35c22212009-01-29 14:08:27 +1100770 /*
771 * Since fpga_tx() is going to need to read the flags under its lock,
772 * it can return them to us so that we don't have to hit PCI MMIO
773 * again for the same information
774 */
775 card_flags = fpga_tx(card);
David Woodhouse9c540042008-12-23 04:09:02 +0000776
777 for (port = 0; port < card->nr_ports; port++) {
778 if (card_flags & (0x10 << port)) {
David Woodhouse90937232009-01-28 16:46:56 +1100779 struct pkt_hdr _hdr, *header;
David Woodhouse9c540042008-12-23 04:09:02 +0000780 struct sk_buff *skb;
781 struct atm_vcc *vcc;
782 int size;
783
David Woodhouse90937232009-01-28 16:46:56 +1100784 if (card->using_dma) {
785 skb = card->rx_skb[port];
David Woodhouseeaf83e32009-01-29 10:51:11 +1100786 card->rx_skb[port] = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000787
David Woodhouseeaf83e32009-01-29 10:51:11 +1100788 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
789 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
David Woodhouse9c540042008-12-23 04:09:02 +0000790
David Woodhouse90937232009-01-28 16:46:56 +1100791 header = (void *)skb->data;
792 size = le16_to_cpu(header->size);
793 skb_put(skb, size + sizeof(*header));
794 skb_pull(skb, sizeof(*header));
795 } else {
796 header = &_hdr;
David Woodhouse9c540042008-12-23 04:09:02 +0000797
David Woodhouse90937232009-01-28 16:46:56 +1100798 rx_done |= 0x10 << port;
David Woodhouse9c540042008-12-23 04:09:02 +0000799
David Woodhouse90937232009-01-28 16:46:56 +1100800 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
801
802 size = le16_to_cpu(header->size);
Nathan Williams78f857f2009-03-25 20:33:42 +1100803 if (size > (card->buffer_size - sizeof(*header))){
804 dev_warn(&card->dev->dev, "Invalid buffer size\n");
805 continue;
806 }
David Woodhouse90937232009-01-28 16:46:56 +1100807
808 skb = alloc_skb(size + 1, GFP_ATOMIC);
809 if (!skb) {
810 if (net_ratelimit())
811 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
812 continue;
813 }
814
815 memcpy_fromio(skb_put(skb, size),
816 RX_BUF(card, port) + sizeof(*header),
817 size);
David Woodhouse9c540042008-12-23 04:09:02 +0000818 }
David Woodhouse9c540042008-12-23 04:09:02 +0000819 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +0000820 dev_info(&card->dev->dev, "Received: port %d\n", port);
David Woodhouse9c540042008-12-23 04:09:02 +0000821 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
David Woodhouse90937232009-01-28 16:46:56 +1100822 size, le16_to_cpu(header->vpi),
823 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000824 print_buffer(skb);
825 }
826
David Woodhouse90937232009-01-28 16:46:56 +1100827 switch (le16_to_cpu(header->type)) {
David Woodhouse9c540042008-12-23 04:09:02 +0000828 case PKT_DATA:
David Woodhouse90937232009-01-28 16:46:56 +1100829 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
830 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000831 if (!vcc) {
832 if (net_ratelimit())
Philip A. Prindeville1e19e652011-03-30 13:22:45 +0000833 dev_warn(&card->dev->dev, "Received packet for unknown VPI.VCI %d.%d on port %d\n",
834 le16_to_cpu(header->vpi), le16_to_cpu(header->vci),
David Woodhouse9c540042008-12-23 04:09:02 +0000835 port);
Nathan Williams007ef522012-11-27 17:34:09 +1100836 dev_kfree_skb_any(skb);
837 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000838 }
839 atm_charge(vcc, skb->truesize);
840 vcc->push(vcc, skb);
841 atomic_inc(&vcc->stats->rx);
842 break;
843
David Woodhouse87ebb182009-01-27 20:02:30 +1100844 case PKT_STATUS:
David Woodhouse95852f42009-01-30 14:23:52 +1100845 if (process_status(card, port, skb) &&
846 net_ratelimit()) {
847 dev_warn(&card->dev->dev, "Bad status packet of %d bytes on port %d:\n", skb->len, port);
848 print_buffer(skb);
849 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100850 dev_kfree_skb_any(skb);
David Woodhouse87ebb182009-01-27 20:02:30 +1100851 break;
852
David Woodhouse9c540042008-12-23 04:09:02 +0000853 case PKT_COMMAND:
854 default: /* FIXME: Not really, surely? */
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100855 if (process_command(card, port, skb))
856 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000857 spin_lock(&card->cli_queue_lock);
858 if (skb_queue_len(&card->cli_queue[port]) > 10) {
859 if (net_ratelimit())
860 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
861 port);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100862 dev_kfree_skb_any(skb);
David Woodhouse9c540042008-12-23 04:09:02 +0000863 } else
864 skb_queue_tail(&card->cli_queue[port], skb);
865 spin_unlock(&card->cli_queue_lock);
866 break;
867 }
868 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100869 /* Allocate RX skbs for any ports which need them */
870 if (card->using_dma && card->atmdev[port] &&
871 !card->rx_skb[port]) {
872 struct sk_buff *skb = alloc_skb(RX_DMA_SIZE, GFP_ATOMIC);
873 if (skb) {
874 SKB_CB(skb)->dma_addr =
875 pci_map_single(card->dev, skb->data,
876 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
877 iowrite32(SKB_CB(skb)->dma_addr,
878 card->config_regs + RX_DMA_ADDR(port));
879 card->rx_skb[port] = skb;
880 } else {
881 if (net_ratelimit())
882 dev_warn(&card->dev->dev, "Failed to allocate RX skb");
883
884 /* We'll have to try again later */
885 tasklet_schedule(&card->tlet);
886 }
887 }
David Woodhouse9c540042008-12-23 04:09:02 +0000888 }
889 if (rx_done)
890 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
891
892 return;
893}
894
895static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
896{
897 struct hlist_head *head;
898 struct atm_vcc *vcc = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000899 struct sock *s;
900
901 read_lock(&vcc_sklist_lock);
902 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800903 sk_for_each(s, head) {
David Woodhouse9c540042008-12-23 04:09:02 +0000904 vcc = atm_sk(s);
905 if (vcc->dev == dev && vcc->vci == vci &&
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700906 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
907 test_bit(ATM_VF_READY, &vcc->flags))
David Woodhouse9c540042008-12-23 04:09:02 +0000908 goto out;
909 }
910 vcc = NULL;
911 out:
912 read_unlock(&vcc_sklist_lock);
913 return vcc;
914}
915
David Woodhouse9c540042008-12-23 04:09:02 +0000916static int popen(struct atm_vcc *vcc)
917{
918 struct solos_card *card = vcc->dev->dev_data;
919 struct sk_buff *skb;
920 struct pkt_hdr *header;
921
David Woodhouseb28a4b92009-01-27 21:50:36 +1100922 if (vcc->qos.aal != ATM_AAL5) {
923 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
924 vcc->qos.aal);
925 return -EINVAL;
926 }
927
David Woodhousea1db5c52012-11-29 23:27:20 +0000928 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
Jesper Juhlda1ab3e2011-02-13 10:49:32 +0000929 if (!skb) {
930 if (net_ratelimit())
931 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
David Woodhouse9c540042008-12-23 04:09:02 +0000932 return -ENOMEM;
933 }
934 header = (void *)skb_put(skb, sizeof(*header));
935
David Woodhouseb76811a2009-01-27 10:18:51 +1100936 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000937 header->vpi = cpu_to_le16(vcc->vpi);
938 header->vci = cpu_to_le16(vcc->vci);
939 header->type = cpu_to_le16(PKT_POPEN);
940
941 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
942
David Woodhousebdc54622009-01-29 14:37:20 +1100943 set_bit(ATM_VF_ADDR, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000944 set_bit(ATM_VF_READY, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000945
946 return 0;
947}
948
949static void pclose(struct atm_vcc *vcc)
950{
951 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000952 unsigned char port = SOLOS_CHAN(vcc->dev);
David Woodhouse213e85d2012-11-29 23:15:30 +0000953 struct sk_buff *skb, *tmpskb;
David Woodhouse9c540042008-12-23 04:09:02 +0000954 struct pkt_hdr *header;
955
David Woodhouse213e85d2012-11-29 23:15:30 +0000956 /* Remove any yet-to-be-transmitted packets from the pending queue */
957 spin_lock(&card->tx_queue_lock);
958 skb_queue_walk_safe(&card->tx_queue[port], skb, tmpskb) {
959 if (SKB_CB(skb)->vcc == vcc) {
960 skb_unlink(skb, &card->tx_queue[port]);
961 solos_pop(vcc, skb);
962 }
963 }
964 spin_unlock(&card->tx_queue_lock);
965
David Woodhousea1db5c52012-11-29 23:27:20 +0000966 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
David Woodhouse9c540042008-12-23 04:09:02 +0000967 if (!skb) {
968 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
969 return;
970 }
971 header = (void *)skb_put(skb, sizeof(*header));
972
David Woodhouseb76811a2009-01-27 10:18:51 +1100973 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000974 header->vpi = cpu_to_le16(vcc->vpi);
975 header->vci = cpu_to_le16(vcc->vci);
976 header->type = cpu_to_le16(PKT_PCLOSE);
977
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000978 skb_get(skb);
979 fpga_queue(card, port, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +0000980
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000981 if (!wait_event_timeout(card->param_wq, !skb_shared(skb), 5 * HZ))
982 dev_warn(&card->dev->dev,
983 "Timeout waiting for VCC close on port %d\n", port);
984
985 dev_kfree_skb(skb);
986
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700987 /* Hold up vcc_destroy_socket() (our caller) until solos_bh() in the
988 tasklet has finished processing any incoming packets (and, more to
989 the point, using the vcc pointer). */
990 tasklet_unlock_wait(&card->tlet);
David Woodhouse213e85d2012-11-29 23:15:30 +0000991
992 clear_bit(ATM_VF_ADDR, &vcc->flags);
993
David Woodhouse9c540042008-12-23 04:09:02 +0000994 return;
995}
996
997static int print_buffer(struct sk_buff *buf)
998{
999 int len,i;
1000 char msg[500];
1001 char item[10];
1002
1003 len = buf->len;
1004 for (i = 0; i < len; i++){
1005 if(i % 8 == 0)
1006 sprintf(msg, "%02X: ", i);
1007
1008 sprintf(item,"%02X ",*(buf->data + i));
1009 strcat(msg, item);
1010 if(i % 8 == 7) {
1011 sprintf(item, "\n");
1012 strcat(msg, item);
1013 printk(KERN_DEBUG "%s", msg);
1014 }
1015 }
1016 if (i % 8 != 0) {
1017 sprintf(item, "\n");
1018 strcat(msg, item);
1019 printk(KERN_DEBUG "%s", msg);
1020 }
1021 printk(KERN_DEBUG "\n");
1022
1023 return 0;
1024}
1025
1026static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
1027 struct atm_vcc *vcc)
1028{
1029 int old_len;
David Woodhousef69e4172009-01-29 11:10:58 +11001030 unsigned long flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001031
David Woodhouse90937232009-01-28 16:46:56 +11001032 SKB_CB(skb)->vcc = vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001033
David Woodhousef69e4172009-01-29 11:10:58 +11001034 spin_lock_irqsave(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +00001035 old_len = skb_queue_len(&card->tx_queue[port]);
1036 skb_queue_tail(&card->tx_queue[port], skb);
David Woodhouse35c22212009-01-29 14:08:27 +11001037 if (!old_len)
David Woodhousef69e4172009-01-29 11:10:58 +11001038 card->tx_mask |= (1 << port);
David Woodhousef69e4172009-01-29 11:10:58 +11001039 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +00001040
David Woodhousef69e4172009-01-29 11:10:58 +11001041 /* Theoretically we could just schedule the tasklet here, but
1042 that introduces latency we don't want -- it's noticeable */
David Woodhouse9c540042008-12-23 04:09:02 +00001043 if (!old_len)
1044 fpga_tx(card);
1045}
1046
David Woodhouse35c22212009-01-29 14:08:27 +11001047static uint32_t fpga_tx(struct solos_card *card)
David Woodhouse9c540042008-12-23 04:09:02 +00001048{
David Woodhouse35c22212009-01-29 14:08:27 +11001049 uint32_t tx_pending, card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001050 uint32_t tx_started = 0;
1051 struct sk_buff *skb;
1052 struct atm_vcc *vcc;
1053 unsigned char port;
1054 unsigned long flags;
1055
1056 spin_lock_irqsave(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001057
1058 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
1059 /*
1060 * The queue lock is required for _writing_ to tx_mask, but we're
1061 * OK to read it here without locking. The only potential update
1062 * that we could race with is in fpga_queue() where it sets a bit
1063 * for a new port... but it's going to call this function again if
1064 * it's doing that, anyway.
1065 */
1066 tx_pending = card->tx_mask & ~card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001067
David Woodhouse35c22212009-01-29 14:08:27 +11001068 for (port = 0; tx_pending; tx_pending >>= 1, port++) {
1069 if (tx_pending & 1) {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001070 struct sk_buff *oldskb = card->tx_skb[port];
David Woodhousecae49ed2012-12-11 14:57:14 +00001071 if (oldskb) {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001072 pci_unmap_single(card->dev, SKB_CB(oldskb)->dma_addr,
1073 oldskb->len, PCI_DMA_TODEVICE);
David Woodhousecae49ed2012-12-11 14:57:14 +00001074 card->tx_skb[port] = NULL;
1075 }
David Woodhouse9c540042008-12-23 04:09:02 +00001076 spin_lock(&card->tx_queue_lock);
1077 skb = skb_dequeue(&card->tx_queue[port]);
David Woodhousef69e4172009-01-29 11:10:58 +11001078 if (!skb)
1079 card->tx_mask &= ~(1 << port);
David Woodhouse9c540042008-12-23 04:09:02 +00001080 spin_unlock(&card->tx_queue_lock);
1081
David Woodhouseeaf83e32009-01-29 10:51:11 +11001082 if (skb && !card->using_dma) {
1083 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
David Woodhousebdc54622009-01-29 14:37:20 +11001084 tx_started |= 1 << port;
David Woodhouseeaf83e32009-01-29 10:51:11 +11001085 oldskb = skb; /* We're done with this skb already */
1086 } else if (skb && card->using_dma) {
David Woodhouse152a2a82012-12-19 11:01:21 +00001087 unsigned char *data = skb->data;
1088 if ((unsigned long)data & card->dma_alignment) {
1089 data = card->dma_bounce + (BUF_SIZE * port);
1090 memcpy(data, skb->data, skb->len);
1091 }
1092 SKB_CB(skb)->dma_addr = pci_map_single(card->dev, data,
David Woodhouseeaf83e32009-01-29 10:51:11 +11001093 skb->len, PCI_DMA_TODEVICE);
David Woodhouseb4bd8ad2012-05-24 04:58:27 +00001094 card->tx_skb[port] = skb;
David Woodhouseeaf83e32009-01-29 10:51:11 +11001095 iowrite32(SKB_CB(skb)->dma_addr,
1096 card->config_regs + TX_DMA_ADDR(port));
1097 }
1098
1099 if (!oldskb)
David Woodhouse9c540042008-12-23 04:09:02 +00001100 continue;
1101
David Woodhouseeaf83e32009-01-29 10:51:11 +11001102 /* Clean up and free oldskb now it's gone */
David Woodhouse9c540042008-12-23 04:09:02 +00001103 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001104 struct pkt_hdr *header = (void *)oldskb->data;
1105 int size = le16_to_cpu(header->size);
1106
1107 skb_pull(oldskb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +00001108 dev_info(&card->dev->dev, "Transmitted: port %d\n",
1109 port);
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001110 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
1111 size, le16_to_cpu(header->vpi),
1112 le16_to_cpu(header->vci));
David Woodhouseeaf83e32009-01-29 10:51:11 +11001113 print_buffer(oldskb);
David Woodhouse9c540042008-12-23 04:09:02 +00001114 }
David Woodhouse9c540042008-12-23 04:09:02 +00001115
David Woodhouseeaf83e32009-01-29 10:51:11 +11001116 vcc = SKB_CB(oldskb)->vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001117
1118 if (vcc) {
1119 atomic_inc(&vcc->stats->tx);
David Woodhouseeaf83e32009-01-29 10:51:11 +11001120 solos_pop(vcc, oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001121 } else {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001122 dev_kfree_skb_irq(oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001123 wake_up(&card->param_wq);
1124 }
David Woodhouse9c540042008-12-23 04:09:02 +00001125 }
1126 }
David Woodhousebdc54622009-01-29 14:37:20 +11001127 /* For non-DMA TX, write the 'TX start' bit for all four ports simultaneously */
David Woodhouse9c540042008-12-23 04:09:02 +00001128 if (tx_started)
1129 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
1130
1131 spin_unlock_irqrestore(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001132 return card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001133}
1134
1135static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
1136{
1137 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse9c540042008-12-23 04:09:02 +00001138 struct pkt_hdr *header;
David Woodhouseb76811a2009-01-27 10:18:51 +11001139 int pktlen;
David Woodhouse9c540042008-12-23 04:09:02 +00001140
David Woodhouseb76811a2009-01-27 10:18:51 +11001141 pktlen = skb->len;
1142 if (pktlen > (BUF_SIZE - sizeof(*header))) {
David Woodhouse9c540042008-12-23 04:09:02 +00001143 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
1144 solos_pop(vcc, skb);
1145 return 0;
1146 }
1147
1148 if (!skb_clone_writable(skb, sizeof(*header))) {
1149 int expand_by = 0;
1150 int ret;
1151
1152 if (skb_headroom(skb) < sizeof(*header))
1153 expand_by = sizeof(*header) - skb_headroom(skb);
1154
1155 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
1156 if (ret) {
Simon Farnsworth4306cad2009-01-19 21:19:29 +00001157 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
David Woodhouse9c540042008-12-23 04:09:02 +00001158 solos_pop(vcc, skb);
1159 return ret;
1160 }
1161 }
1162
1163 header = (void *)skb_push(skb, sizeof(*header));
1164
David Woodhouseb76811a2009-01-27 10:18:51 +11001165 /* This does _not_ include the size of the header */
1166 header->size = cpu_to_le16(pktlen);
David Woodhouse9c540042008-12-23 04:09:02 +00001167 header->vpi = cpu_to_le16(vcc->vpi);
1168 header->vci = cpu_to_le16(vcc->vci);
1169 header->type = cpu_to_le16(PKT_DATA);
1170
1171 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1172
1173 return 0;
1174}
1175
1176static struct atmdev_ops fpga_ops = {
1177 .open = popen,
1178 .close = pclose,
1179 .ioctl = NULL,
1180 .getsockopt = NULL,
1181 .setsockopt = NULL,
1182 .send = psend,
1183 .send_oam = NULL,
1184 .phy_put = NULL,
1185 .phy_get = NULL,
1186 .change_qos = NULL,
1187 .proc_read = NULL,
1188 .owner = THIS_MODULE
1189};
1190
1191static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
1192{
David Woodhousecd5549e2009-01-30 14:26:37 +11001193 int err;
David Woodhouse9c540042008-12-23 04:09:02 +00001194 uint16_t fpga_ver;
1195 uint8_t major_ver, minor_ver;
1196 uint32_t data32;
1197 struct solos_card *card;
1198
David Woodhouse9c540042008-12-23 04:09:02 +00001199 card = kzalloc(sizeof(*card), GFP_KERNEL);
1200 if (!card)
1201 return -ENOMEM;
1202
1203 card->dev = dev;
David Woodhousefa755b92009-01-27 14:16:12 +11001204 init_waitqueue_head(&card->fw_wq);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001205 init_waitqueue_head(&card->param_wq);
David Woodhouse9c540042008-12-23 04:09:02 +00001206
1207 err = pci_enable_device(dev);
1208 if (err) {
1209 dev_warn(&dev->dev, "Failed to enable PCI device\n");
1210 goto out;
1211 }
1212
Yang Hongyange9304382009-04-13 14:40:14 -07001213 err = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
David Woodhouse90937232009-01-28 16:46:56 +11001214 if (err) {
1215 dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
1216 goto out;
1217 }
1218
David Woodhouse9c540042008-12-23 04:09:02 +00001219 err = pci_request_regions(dev, "solos");
1220 if (err) {
1221 dev_warn(&dev->dev, "Failed to request regions\n");
1222 goto out;
1223 }
1224
1225 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1226 if (!card->config_regs) {
1227 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
1228 goto out_release_regions;
1229 }
1230 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1231 if (!card->buffers) {
1232 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
1233 goto out_unmap_config;
1234 }
1235
David Woodhousecc3657e2009-01-30 14:23:22 +11001236 if (reset) {
1237 iowrite32(1, card->config_regs + FPGA_MODE);
1238 data32 = ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001239
David Woodhousecc3657e2009-01-30 14:23:22 +11001240 iowrite32(0, card->config_regs + FPGA_MODE);
1241 data32 = ioread32(card->config_regs + FPGA_MODE);
1242 }
David Woodhouse9c540042008-12-23 04:09:02 +00001243
1244 data32 = ioread32(card->config_regs + FPGA_VER);
1245 fpga_ver = (data32 & 0x0000FFFF);
1246 major_ver = ((data32 & 0xFF000000) >> 24);
1247 minor_ver = ((data32 & 0x00FF0000) >> 16);
Nathan Williams4dbedf42009-03-25 20:27:37 +11001248 card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
1249 if (card->fpga_version > LEGACY_BUFFERS)
1250 card->buffer_size = BUF_SIZE;
1251 else
1252 card->buffer_size = OLD_BUF_SIZE;
David Woodhouse9c540042008-12-23 04:09:02 +00001253 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1254 major_ver, minor_ver, fpga_ver);
1255
David Woodhouse3ce12272010-11-01 10:35:28 +00001256 if (fpga_ver < 37 && (fpga_upgrade || firmware_upgrade ||
1257 db_fpga_upgrade || db_firmware_upgrade)) {
1258 dev_warn(&dev->dev,
1259 "FPGA too old; cannot upgrade flash. Use JTAG.\n");
1260 fpga_upgrade = firmware_upgrade = 0;
1261 db_fpga_upgrade = db_firmware_upgrade = 0;
1262 }
1263
Nathan Williams13af8162012-12-19 11:01:20 +00001264 /* Stopped using Atmel flash after 0.03-38 */
1265 if (fpga_ver < 39)
1266 card->atmel_flash = 1;
1267 else
1268 card->atmel_flash = 0;
1269
David Woodhouse152a2a82012-12-19 11:01:21 +00001270 data32 = ioread32(card->config_regs + PORTS);
1271 card->nr_ports = (data32 & 0x000000FF);
1272
David Woodhouseb4bd8ad2012-05-24 04:58:27 +00001273 if (card->fpga_version >= DMA_SUPPORTED) {
1274 pci_set_master(dev);
David Woodhouse90937232009-01-28 16:46:56 +11001275 card->using_dma = 1;
David Woodhouse152a2a82012-12-19 11:01:21 +00001276 if (1) { /* All known FPGA versions so far */
1277 card->dma_alignment = 3;
1278 card->dma_bounce = kmalloc(card->nr_ports * BUF_SIZE, GFP_KERNEL);
1279 if (!card->dma_bounce) {
1280 dev_warn(&card->dev->dev, "Failed to allocate DMA bounce buffers\n");
1281 /* Fallback to MMIO doesn't work */
1282 goto out_unmap_both;
1283 }
1284 }
Nathan Williams4dbedf42009-03-25 20:27:37 +11001285 } else {
1286 card->using_dma = 0;
David Woodhouseeab50f72009-01-30 14:27:26 +11001287 /* Set RX empty flag for all ports */
1288 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1289 }
David Woodhouse9c540042008-12-23 04:09:02 +00001290
David Woodhouse9c540042008-12-23 04:09:02 +00001291 pci_set_drvdata(dev, card);
David Woodhousefa755b92009-01-27 14:16:12 +11001292
David Woodhouse9c540042008-12-23 04:09:02 +00001293 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1294 spin_lock_init(&card->tx_lock);
1295 spin_lock_init(&card->tx_queue_lock);
1296 spin_lock_init(&card->cli_queue_lock);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001297 spin_lock_init(&card->param_queue_lock);
1298 INIT_LIST_HEAD(&card->param_queue);
David Woodhouse9c540042008-12-23 04:09:02 +00001299
David Woodhousefcd82662009-01-29 11:29:12 +11001300 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
David Woodhouse9c540042008-12-23 04:09:02 +00001301 "solos-pci", card);
David Woodhousefa755b92009-01-27 14:16:12 +11001302 if (err) {
David Woodhouse9c540042008-12-23 04:09:02 +00001303 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
David Woodhousefa755b92009-01-27 14:16:12 +11001304 goto out_unmap_both;
1305 }
David Woodhouse9c540042008-12-23 04:09:02 +00001306
David Woodhouse9c540042008-12-23 04:09:02 +00001307 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1308
David Woodhousefa755b92009-01-27 14:16:12 +11001309 if (fpga_upgrade)
1310 flash_upgrade(card, 0);
1311
1312 if (firmware_upgrade)
1313 flash_upgrade(card, 1);
1314
Nathan Williams4dbedf42009-03-25 20:27:37 +11001315 if (db_fpga_upgrade)
1316 flash_upgrade(card, 2);
1317
1318 if (db_firmware_upgrade)
1319 flash_upgrade(card, 3);
1320
Dan Williamsd9ca6762010-12-08 19:40:47 +00001321 err = atm_init(card, &dev->dev);
David Woodhousefa755b92009-01-27 14:16:12 +11001322 if (err)
1323 goto out_free_irq;
1324
Nathan Williamsf9baad02012-12-19 11:01:18 +00001325 if (card->fpga_version >= DMA_SUPPORTED &&
1326 sysfs_create_group(&card->dev->dev.kobj, &gpio_attr_group))
1327 dev_err(&card->dev->dev, "Could not register parameter group for GPIOs\n");
1328
David Woodhouse9c540042008-12-23 04:09:02 +00001329 return 0;
1330
David Woodhousefa755b92009-01-27 14:16:12 +11001331 out_free_irq:
1332 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1333 free_irq(dev->irq, card);
1334 tasklet_kill(&card->tlet);
1335
David Woodhouse9c540042008-12-23 04:09:02 +00001336 out_unmap_both:
David Woodhouse152a2a82012-12-19 11:01:21 +00001337 kfree(card->dma_bounce);
David Woodhousefa755b92009-01-27 14:16:12 +11001338 pci_set_drvdata(dev, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001339 pci_iounmap(dev, card->buffers);
Julia Lawall8ae0cfe2012-02-19 09:43:32 +00001340 out_unmap_config:
1341 pci_iounmap(dev, card->config_regs);
David Woodhouse9c540042008-12-23 04:09:02 +00001342 out_release_regions:
1343 pci_release_regions(dev);
1344 out:
Julia Lawallbc111d52009-02-08 17:00:02 -08001345 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001346 return err;
1347}
1348
Dan Williamsd9ca6762010-12-08 19:40:47 +00001349static int atm_init(struct solos_card *card, struct device *parent)
David Woodhouse9c540042008-12-23 04:09:02 +00001350{
1351 int i;
1352
David Woodhouse9c540042008-12-23 04:09:02 +00001353 for (i = 0; i < card->nr_ports; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +11001354 struct sk_buff *skb;
1355 struct pkt_hdr *header;
1356
David Woodhouse9c540042008-12-23 04:09:02 +00001357 skb_queue_head_init(&card->tx_queue[i]);
1358 skb_queue_head_init(&card->cli_queue[i]);
1359
Dan Williamsd9ca6762010-12-08 19:40:47 +00001360 card->atmdev[i] = atm_dev_register("solos-pci", parent, &fpga_ops, -1, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001361 if (!card->atmdev[i]) {
1362 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1363 atm_remove(card);
1364 return -ENODEV;
1365 }
1366 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1367 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
David Woodhoused057f0a2009-01-27 16:52:07 +11001368 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1369 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
David Woodhouse9c540042008-12-23 04:09:02 +00001370
1371 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1372
1373 card->atmdev[i]->ci_range.vpi_bits = 8;
1374 card->atmdev[i]->ci_range.vci_bits = 16;
1375 card->atmdev[i]->dev_data = card;
1376 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
Philip A. Prindevillec0312352011-03-30 13:17:04 +00001377 atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +11001378
David Woodhousea1db5c52012-11-29 23:27:20 +00001379 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
David Woodhouse87ebb182009-01-27 20:02:30 +11001380 if (!skb) {
1381 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1382 continue;
1383 }
1384
1385 header = (void *)skb_put(skb, sizeof(*header));
1386
1387 header->size = cpu_to_le16(0);
1388 header->vpi = cpu_to_le16(0);
1389 header->vci = cpu_to_le16(0);
1390 header->type = cpu_to_le16(PKT_STATUS);
1391
1392 fpga_queue(card, i, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001393 }
1394 return 0;
1395}
1396
1397static void atm_remove(struct solos_card *card)
1398{
1399 int i;
1400
1401 for (i = 0; i < card->nr_ports; i++) {
1402 if (card->atmdev[i]) {
David Woodhouse97d759d2009-03-17 14:59:34 +00001403 struct sk_buff *skb;
1404
David Woodhouse9c540042008-12-23 04:09:02 +00001405 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
David Woodhousec0fe3022009-01-28 14:34:34 +11001406
1407 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
David Woodhouse9c540042008-12-23 04:09:02 +00001408 atm_dev_deregister(card->atmdev[i]);
David Woodhouse97d759d2009-03-17 14:59:34 +00001409
1410 skb = card->rx_skb[i];
1411 if (skb) {
1412 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1413 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
1414 dev_kfree_skb(skb);
1415 }
1416 skb = card->tx_skb[i];
1417 if (skb) {
1418 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1419 skb->len, PCI_DMA_TODEVICE);
1420 dev_kfree_skb(skb);
1421 }
1422 while ((skb = skb_dequeue(&card->tx_queue[i])))
1423 dev_kfree_skb(skb);
1424
David Woodhouse9c540042008-12-23 04:09:02 +00001425 }
1426 }
1427}
1428
1429static void fpga_remove(struct pci_dev *dev)
1430{
1431 struct solos_card *card = pci_get_drvdata(dev);
David Woodhouse97d759d2009-03-17 14:59:34 +00001432
1433 /* Disable IRQs */
1434 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
David Woodhouse9c540042008-12-23 04:09:02 +00001435
David Woodhouse97d759d2009-03-17 14:59:34 +00001436 /* Reset FPGA */
1437 iowrite32(1, card->config_regs + FPGA_MODE);
1438 (void)ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001439
Nathan Williamsf9baad02012-12-19 11:01:18 +00001440 if (card->fpga_version >= DMA_SUPPORTED)
1441 sysfs_remove_group(&card->dev->dev.kobj, &gpio_attr_group);
1442
David Woodhouse9c540042008-12-23 04:09:02 +00001443 atm_remove(card);
1444
David Woodhouse9c540042008-12-23 04:09:02 +00001445 free_irq(dev->irq, card);
1446 tasklet_kill(&card->tlet);
1447
David Woodhouse152a2a82012-12-19 11:01:21 +00001448 kfree(card->dma_bounce);
1449
David Woodhouse97d759d2009-03-17 14:59:34 +00001450 /* Release device from reset */
1451 iowrite32(0, card->config_regs + FPGA_MODE);
1452 (void)ioread32(card->config_regs + FPGA_MODE);
1453
David Woodhouse9c540042008-12-23 04:09:02 +00001454 pci_iounmap(dev, card->buffers);
1455 pci_iounmap(dev, card->config_regs);
1456
David Woodhouse9c540042008-12-23 04:09:02 +00001457 pci_release_regions(dev);
1458 pci_disable_device(dev);
1459
1460 pci_set_drvdata(dev, NULL);
1461 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001462}
1463
Greg Kroah-Hartman6c445122012-12-21 13:25:04 -08001464static struct pci_device_id fpga_pci_tbl[] = {
David Woodhouse9c540042008-12-23 04:09:02 +00001465 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1466 { 0, }
1467};
1468
1469MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1470
1471static struct pci_driver fpga_driver = {
1472 .name = "solos",
1473 .id_table = fpga_pci_tbl,
1474 .probe = fpga_probe,
1475 .remove = fpga_remove,
1476};
1477
1478
1479static int __init solos_pci_init(void)
1480{
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001481 BUILD_BUG_ON(sizeof(struct solos_skb_cb) > sizeof(((struct sk_buff *)0)->cb));
1482
David Woodhouse9c540042008-12-23 04:09:02 +00001483 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1484 return pci_register_driver(&fpga_driver);
1485}
1486
1487static void __exit solos_pci_exit(void)
1488{
1489 pci_unregister_driver(&fpga_driver);
1490 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1491}
1492
1493module_init(solos_pci_init);
1494module_exit(solos_pci_exit);