blob: 6ac2b2b1e8dec204921fa3cd3d3383f45a3a2794 [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{
LABBE Corentine94d91a2015-12-04 08:43:19 +0100350 char *str, *state_str, *snr, *attn;
351 int ver, rate_up, rate_down, err;
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
LABBE Corentine94d91a2015-12-04 08:43:19 +0100360 err = kstrtoint(str, 10, &ver);
361 if (err) {
362 dev_warn(&card->dev->dev, "Unexpected status interrupt version\n");
363 return err;
364 }
David Woodhouse87ebb182009-01-27 20:02:30 +1100365 if (ver < 1) {
366 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
367 ver);
368 return -EIO;
369 }
370
371 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100372 if (!str)
373 return -EIO;
David Woodhouse95852f42009-01-30 14:23:52 +1100374 if (!strcmp(str, "ERROR")) {
375 dev_dbg(&card->dev->dev, "Status packet indicated Solos error on port %d (starting up?)\n",
376 port);
377 return 0;
378 }
379
LABBE Corentine94d91a2015-12-04 08:43:19 +0100380 err = kstrtoint(str, 10, &rate_down);
381 if (err)
382 return err;
David Woodhouse87ebb182009-01-27 20:02:30 +1100383
384 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100385 if (!str)
386 return -EIO;
LABBE Corentine94d91a2015-12-04 08:43:19 +0100387 err = kstrtoint(str, 10, &rate_up);
388 if (err)
389 return err;
David Woodhouse87ebb182009-01-27 20:02:30 +1100390
David Woodhouseaf780652009-01-28 10:22:57 +1100391 state_str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100392 if (!state_str)
393 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100394
395 /* Anything but 'Showtime' is down */
396 if (strcmp(state_str, "Showtime")) {
Karl Hiramoto49d49102010-07-08 20:55:34 +0000397 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
David Woodhousef87b2ed2009-01-30 14:31:36 +1100398 dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
399 return 0;
David Woodhouse1e615df2009-01-27 21:47:47 +1100400 }
David Woodhouse87ebb182009-01-27 20:02:30 +1100401
David Woodhousef87b2ed2009-01-30 14:31:36 +1100402 snr = next_string(skb);
Julia Lawall6cf57672009-07-27 11:38:52 -0700403 if (!snr)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100404 return -EIO;
405 attn = next_string(skb);
406 if (!attn)
407 return -EIO;
David Woodhouseaf780652009-01-28 10:22:57 +1100408
David Woodhousef87b2ed2009-01-30 14:31:36 +1100409 dev_info(&card->dev->dev, "Port %d: %s @%d/%d kb/s%s%s%s%s\n",
410 port, state_str, rate_down/1000, rate_up/1000,
411 snr[0]?", SNR ":"", snr, attn[0]?", Attn ":"", attn);
412
David Woodhousec6428e52009-01-29 12:17:09 +1100413 card->atmdev[port]->link_rate = rate_down / 424;
Karl Hiramoto49d49102010-07-08 20:55:34 +0000414 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +1100415
David Woodhouse87ebb182009-01-27 20:02:30 +1100416 return 0;
417}
418
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100419static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
420{
421 struct solos_param *prm;
422 unsigned long flags;
423 int cmdpid;
LABBE Corentine94d91a2015-12-04 08:43:19 +0100424 int found = 0, err;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100425
426 if (skb->len < 7)
427 return 0;
428
429 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
430 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
431 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
432 skb->data[6] != '\n')
433 return 0;
434
LABBE Corentine94d91a2015-12-04 08:43:19 +0100435 err = kstrtoint(&skb->data[1], 10, &cmdpid);
436 if (err)
437 return err;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100438
439 spin_lock_irqsave(&card->param_queue_lock, flags);
440 list_for_each_entry(prm, &card->param_queue, list) {
441 if (prm->port == port && prm->pid == cmdpid) {
442 prm->response = skb;
443 skb_pull(skb, 7);
444 wake_up(&card->param_wq);
445 found = 1;
446 break;
447 }
448 }
449 spin_unlock_irqrestore(&card->param_queue_lock, flags);
450 return found;
451}
452
David Woodhouse9c540042008-12-23 04:09:02 +0000453static ssize_t console_show(struct device *dev, struct device_attribute *attr,
454 char *buf)
455{
456 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
457 struct solos_card *card = atmdev->dev_data;
458 struct sk_buff *skb;
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000459 unsigned int len;
David Woodhouse9c540042008-12-23 04:09:02 +0000460
461 spin_lock(&card->cli_queue_lock);
462 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
463 spin_unlock(&card->cli_queue_lock);
464 if(skb == NULL)
465 return sprintf(buf, "No data.\n");
466
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000467 len = skb->len;
468 memcpy(buf, skb->data, len);
David Woodhouse9c540042008-12-23 04:09:02 +0000469
470 kfree_skb(skb);
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000471 return len;
David Woodhouse9c540042008-12-23 04:09:02 +0000472}
473
474static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
475{
476 struct sk_buff *skb;
477 struct pkt_hdr *header;
478
David Woodhouse9c540042008-12-23 04:09:02 +0000479 if (size > (BUF_SIZE - sizeof(*header))) {
480 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
481 return 0;
482 }
483 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
484 if (!skb) {
485 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
486 return 0;
487 }
488
489 header = (void *)skb_put(skb, sizeof(*header));
490
491 header->size = cpu_to_le16(size);
492 header->vpi = cpu_to_le16(0);
493 header->vci = cpu_to_le16(0);
494 header->type = cpu_to_le16(PKT_COMMAND);
495
496 memcpy(skb_put(skb, size), buf, size);
497
498 fpga_queue(card, dev, skb, NULL);
499
500 return 0;
501}
502
503static ssize_t console_store(struct device *dev, struct device_attribute *attr,
504 const char *buf, size_t count)
505{
506 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
507 struct solos_card *card = atmdev->dev_data;
508 int err;
509
510 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
511
512 return err?:count;
513}
514
Nathan Williamsf9baad02012-12-19 11:01:18 +0000515struct geos_gpio_attr {
516 struct device_attribute attr;
517 int offset;
518};
519
520#define SOLOS_GPIO_ATTR(_name, _mode, _show, _store, _offset) \
521 struct geos_gpio_attr gpio_attr_##_name = { \
522 .attr = __ATTR(_name, _mode, _show, _store), \
523 .offset = _offset }
524
525static ssize_t geos_gpio_store(struct device *dev, struct device_attribute *attr,
526 const char *buf, size_t count)
527{
Geliang Tangbd0ed562015-12-27 18:45:57 +0800528 struct pci_dev *pdev = to_pci_dev(dev);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000529 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
530 struct solos_card *card = pci_get_drvdata(pdev);
531 uint32_t data32;
532
533 if (count != 1 && (count != 2 || buf[1] != '\n'))
534 return -EINVAL;
535
536 spin_lock_irq(&card->param_queue_lock);
537 data32 = ioread32(card->config_regs + GPIO_STATUS);
538 if (buf[0] == '1') {
539 data32 |= 1 << gattr->offset;
540 iowrite32(data32, card->config_regs + GPIO_STATUS);
541 } else if (buf[0] == '0') {
542 data32 &= ~(1 << gattr->offset);
543 iowrite32(data32, card->config_regs + GPIO_STATUS);
544 } else {
545 count = -EINVAL;
546 }
Dan Carpenter29042072012-12-19 21:48:45 +0000547 spin_unlock_irq(&card->param_queue_lock);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000548 return count;
549}
550
551static ssize_t geos_gpio_show(struct device *dev, struct device_attribute *attr,
552 char *buf)
553{
Geliang Tangbd0ed562015-12-27 18:45:57 +0800554 struct pci_dev *pdev = to_pci_dev(dev);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000555 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
556 struct solos_card *card = pci_get_drvdata(pdev);
557 uint32_t data32;
558
559 data32 = ioread32(card->config_regs + GPIO_STATUS);
560 data32 = (data32 >> gattr->offset) & 1;
561
562 return sprintf(buf, "%d\n", data32);
563}
564
565static ssize_t hardware_show(struct device *dev, struct device_attribute *attr,
566 char *buf)
567{
Geliang Tangbd0ed562015-12-27 18:45:57 +0800568 struct pci_dev *pdev = to_pci_dev(dev);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000569 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
570 struct solos_card *card = pci_get_drvdata(pdev);
571 uint32_t data32;
572
573 data32 = ioread32(card->config_regs + GPIO_STATUS);
574 switch (gattr->offset) {
575 case 0:
576 /* HardwareVersion */
577 data32 = data32 & 0x1F;
578 break;
579 case 1:
580 /* HardwareVariant */
581 data32 = (data32 >> 5) & 0x0F;
582 break;
583 }
584 return sprintf(buf, "%d\n", data32);
585}
586
David Woodhouse9c540042008-12-23 04:09:02 +0000587static DEVICE_ATTR(console, 0644, console_show, console_store);
588
David Woodhoused057f0a2009-01-27 16:52:07 +1100589
590#define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
591#define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
592
593#include "solos-attrlist.c"
594
Nathan Williamsf9baad02012-12-19 11:01:18 +0000595static SOLOS_GPIO_ATTR(GPIO1, 0644, geos_gpio_show, geos_gpio_store, 9);
596static SOLOS_GPIO_ATTR(GPIO2, 0644, geos_gpio_show, geos_gpio_store, 10);
597static SOLOS_GPIO_ATTR(GPIO3, 0644, geos_gpio_show, geos_gpio_store, 11);
598static SOLOS_GPIO_ATTR(GPIO4, 0644, geos_gpio_show, geos_gpio_store, 12);
599static SOLOS_GPIO_ATTR(GPIO5, 0644, geos_gpio_show, geos_gpio_store, 13);
600static SOLOS_GPIO_ATTR(PushButton, 0444, geos_gpio_show, NULL, 14);
601static SOLOS_GPIO_ATTR(HardwareVersion, 0444, hardware_show, NULL, 0);
602static SOLOS_GPIO_ATTR(HardwareVariant, 0444, hardware_show, NULL, 1);
David Woodhoused057f0a2009-01-27 16:52:07 +1100603#undef SOLOS_ATTR_RO
604#undef SOLOS_ATTR_RW
605
606#define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
607#define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
608
609static struct attribute *solos_attrs[] = {
610#include "solos-attrlist.c"
611 NULL
612};
613
614static struct attribute_group solos_attr_group = {
615 .attrs = solos_attrs,
616 .name = "parameters",
617};
David Woodhouse9c540042008-12-23 04:09:02 +0000618
Nathan Williamsf9baad02012-12-19 11:01:18 +0000619static struct attribute *gpio_attrs[] = {
620 &gpio_attr_GPIO1.attr.attr,
621 &gpio_attr_GPIO2.attr.attr,
622 &gpio_attr_GPIO3.attr.attr,
623 &gpio_attr_GPIO4.attr.attr,
624 &gpio_attr_GPIO5.attr.attr,
625 &gpio_attr_PushButton.attr.attr,
626 &gpio_attr_HardwareVersion.attr.attr,
627 &gpio_attr_HardwareVariant.attr.attr,
628 NULL
629};
630
631static struct attribute_group gpio_attr_group = {
632 .attrs = gpio_attrs,
633 .name = "gpio",
634};
635
David Woodhousefa755b92009-01-27 14:16:12 +1100636static int flash_upgrade(struct solos_card *card, int chip)
637{
638 const struct firmware *fw;
639 const char *fw_name;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000640 int blocksize = 0;
641 int numblocks = 0;
David Woodhousefa755b92009-01-27 14:16:12 +1100642 int offset;
643
Andrew Morton7adcdb42009-11-17 12:46:44 +0000644 switch (chip) {
645 case 0:
David Woodhousefa755b92009-01-27 14:16:12 +1100646 fw_name = "solos-FPGA.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000647 if (card->atmel_flash)
648 blocksize = ATMEL_FPGA_BLOCK;
649 else
650 blocksize = SPI_FLASH_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000651 break;
652 case 1:
David Woodhousefa755b92009-01-27 14:16:12 +1100653 fw_name = "solos-Firmware.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000654 if (card->atmel_flash)
655 blocksize = ATMEL_SOLOS_BLOCK;
656 else
657 blocksize = SPI_FLASH_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000658 break;
659 case 2:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100660 if (card->fpga_version > LEGACY_BUFFERS){
661 fw_name = "solos-db-FPGA.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000662 if (card->atmel_flash)
663 blocksize = ATMEL_FPGA_BLOCK;
664 else
665 blocksize = SPI_FLASH_BLOCK;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100666 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000667 dev_info(&card->dev->dev, "FPGA version doesn't support"
668 " daughter board upgrades\n");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100669 return -EPERM;
670 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000671 break;
672 case 3:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100673 if (card->fpga_version > LEGACY_BUFFERS){
674 fw_name = "solos-Firmware.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000675 if (card->atmel_flash)
676 blocksize = ATMEL_SOLOS_BLOCK;
677 else
678 blocksize = SPI_FLASH_BLOCK;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100679 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000680 dev_info(&card->dev->dev, "FPGA version doesn't support"
681 " daughter board upgrades\n");
682 return -EPERM;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100683 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000684 break;
685 default:
686 return -ENODEV;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100687 }
David Woodhousefa755b92009-01-27 14:16:12 +1100688
689 if (request_firmware(&fw, fw_name, &card->dev->dev))
690 return -ENOENT;
691
692 dev_info(&card->dev->dev, "Flash upgrade starting\n");
693
Nathan Williams13af8162012-12-19 11:01:20 +0000694 /* New FPGAs require driver version before permitting flash upgrades */
695 iowrite32(DRIVER_VERSION, card->config_regs + DRIVER_VER);
696
David Woodhousefa755b92009-01-27 14:16:12 +1100697 numblocks = fw->size / blocksize;
698 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000699 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
700
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000701 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
702 iowrite32(1, card->config_regs + FPGA_MODE);
David S. Miller06091ed2011-04-17 00:11:25 -0700703 (void) ioread32(card->config_regs + FPGA_MODE);
David Woodhousefa755b92009-01-27 14:16:12 +1100704
705 /* Set mode to Chip Erase */
Nathan Williams4dbedf42009-03-25 20:27:37 +1100706 if(chip == 0 || chip == 2)
707 dev_info(&card->dev->dev, "Set FPGA Flash mode to FPGA Chip Erase\n");
708 if(chip == 1 || chip == 3)
709 dev_info(&card->dev->dev, "Set FPGA Flash mode to Solos Chip Erase\n");
David Woodhousefa755b92009-01-27 14:16:12 +1100710 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
711
712
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000713 iowrite32(1, card->config_regs + WRITE_FLASH);
David Woodhousefa755b92009-01-27 14:16:12 +1100714 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000715
David Woodhousefa755b92009-01-27 14:16:12 +1100716 for (offset = 0; offset < fw->size; offset += blocksize) {
717 int i;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000718
David Woodhousefa755b92009-01-27 14:16:12 +1100719 /* Clear write flag */
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000720 iowrite32(0, card->config_regs + WRITE_FLASH);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000721
David Woodhousefa755b92009-01-27 14:16:12 +1100722 /* Set mode to Block Write */
723 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
724 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
725
Nathan Williams13af8162012-12-19 11:01:20 +0000726 /* Copy block to buffer, swapping each 16 bits for Atmel flash */
David Woodhousefa755b92009-01-27 14:16:12 +1100727 for(i = 0; i < blocksize; i += 4) {
Nathan Williams13af8162012-12-19 11:01:20 +0000728 uint32_t word;
729 if (card->atmel_flash)
730 word = swahb32p((uint32_t *)(fw->data + offset + i));
731 else
732 word = *(uint32_t *)(fw->data + offset + i);
Nathan Williams4dbedf42009-03-25 20:27:37 +1100733 if(card->fpga_version > LEGACY_BUFFERS)
734 iowrite32(word, FLASH_BUF + i);
735 else
736 iowrite32(word, RX_BUF(card, 3) + i);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000737 }
738
David Woodhousefa755b92009-01-27 14:16:12 +1100739 /* Specify block number and then trigger flash write */
740 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
741 iowrite32(1, card->config_regs + WRITE_FLASH);
742 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000743 }
744
David Woodhousefa755b92009-01-27 14:16:12 +1100745 release_firmware(fw);
746 iowrite32(0, card->config_regs + WRITE_FLASH);
747 iowrite32(0, card->config_regs + FPGA_MODE);
748 iowrite32(0, card->config_regs + FLASH_MODE);
749 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
750 return 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000751}
752
David Woodhouse9c540042008-12-23 04:09:02 +0000753static irqreturn_t solos_irq(int irq, void *dev_id)
754{
755 struct solos_card *card = dev_id;
756 int handled = 1;
757
David Woodhouse9c540042008-12-23 04:09:02 +0000758 iowrite32(0, card->config_regs + IRQ_CLEAR);
David Woodhouse9c540042008-12-23 04:09:02 +0000759
David Woodhouse35c22212009-01-29 14:08:27 +1100760 /* If we're up and running, just kick the tasklet to process TX/RX */
David Woodhousefa755b92009-01-27 14:16:12 +1100761 if (card->atmdev[0])
David Woodhouse9c540042008-12-23 04:09:02 +0000762 tasklet_schedule(&card->tlet);
David Woodhousefa755b92009-01-27 14:16:12 +1100763 else
764 wake_up(&card->fw_wq);
David Woodhouse9c540042008-12-23 04:09:02 +0000765
David Woodhouse9c540042008-12-23 04:09:02 +0000766 return IRQ_RETVAL(handled);
767}
768
Daeseok Youn5f4d4e32014-02-19 10:49:12 +0900769static void solos_bh(unsigned long card_arg)
David Woodhouse9c540042008-12-23 04:09:02 +0000770{
771 struct solos_card *card = (void *)card_arg;
David Woodhouse9c540042008-12-23 04:09:02 +0000772 uint32_t card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000773 uint32_t rx_done = 0;
David Woodhouse35c22212009-01-29 14:08:27 +1100774 int port;
David Woodhouse9c540042008-12-23 04:09:02 +0000775
David Woodhouse35c22212009-01-29 14:08:27 +1100776 /*
777 * Since fpga_tx() is going to need to read the flags under its lock,
778 * it can return them to us so that we don't have to hit PCI MMIO
779 * again for the same information
780 */
781 card_flags = fpga_tx(card);
David Woodhouse9c540042008-12-23 04:09:02 +0000782
783 for (port = 0; port < card->nr_ports; port++) {
784 if (card_flags & (0x10 << port)) {
David Woodhouse90937232009-01-28 16:46:56 +1100785 struct pkt_hdr _hdr, *header;
David Woodhouse9c540042008-12-23 04:09:02 +0000786 struct sk_buff *skb;
787 struct atm_vcc *vcc;
788 int size;
789
David Woodhouse90937232009-01-28 16:46:56 +1100790 if (card->using_dma) {
791 skb = card->rx_skb[port];
David Woodhouseeaf83e32009-01-29 10:51:11 +1100792 card->rx_skb[port] = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000793
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -0500794 dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
795 RX_DMA_SIZE, DMA_FROM_DEVICE);
David Woodhouse9c540042008-12-23 04:09:02 +0000796
David Woodhouse90937232009-01-28 16:46:56 +1100797 header = (void *)skb->data;
798 size = le16_to_cpu(header->size);
799 skb_put(skb, size + sizeof(*header));
800 skb_pull(skb, sizeof(*header));
801 } else {
802 header = &_hdr;
David Woodhouse9c540042008-12-23 04:09:02 +0000803
David Woodhouse90937232009-01-28 16:46:56 +1100804 rx_done |= 0x10 << port;
David Woodhouse9c540042008-12-23 04:09:02 +0000805
David Woodhouse90937232009-01-28 16:46:56 +1100806 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
807
808 size = le16_to_cpu(header->size);
Nathan Williams78f857f2009-03-25 20:33:42 +1100809 if (size > (card->buffer_size - sizeof(*header))){
810 dev_warn(&card->dev->dev, "Invalid buffer size\n");
811 continue;
812 }
David Woodhouse90937232009-01-28 16:46:56 +1100813
David Woodhousece816eb2015-09-16 12:35:00 +0100814 /* Use netdev_alloc_skb() because it adds NET_SKB_PAD of
815 * headroom, and ensures we can route packets back out an
816 * Ethernet interface (for example) without having to
817 * reallocate. Adding NET_IP_ALIGN also ensures that both
818 * PPPoATM and PPPoEoBR2684 packets end up aligned. */
819 skb = netdev_alloc_skb_ip_align(NULL, size + 1);
David Woodhouse90937232009-01-28 16:46:56 +1100820 if (!skb) {
821 if (net_ratelimit())
822 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
823 continue;
824 }
825
826 memcpy_fromio(skb_put(skb, size),
827 RX_BUF(card, port) + sizeof(*header),
828 size);
David Woodhouse9c540042008-12-23 04:09:02 +0000829 }
David Woodhouse9c540042008-12-23 04:09:02 +0000830 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +0000831 dev_info(&card->dev->dev, "Received: port %d\n", port);
David Woodhouse9c540042008-12-23 04:09:02 +0000832 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
David Woodhouse90937232009-01-28 16:46:56 +1100833 size, le16_to_cpu(header->vpi),
834 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000835 print_buffer(skb);
836 }
837
David Woodhouse90937232009-01-28 16:46:56 +1100838 switch (le16_to_cpu(header->type)) {
David Woodhouse9c540042008-12-23 04:09:02 +0000839 case PKT_DATA:
David Woodhouse90937232009-01-28 16:46:56 +1100840 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
841 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000842 if (!vcc) {
843 if (net_ratelimit())
Philip A. Prindeville1e19e652011-03-30 13:22:45 +0000844 dev_warn(&card->dev->dev, "Received packet for unknown VPI.VCI %d.%d on port %d\n",
845 le16_to_cpu(header->vpi), le16_to_cpu(header->vci),
David Woodhouse9c540042008-12-23 04:09:02 +0000846 port);
Nathan Williams007ef522012-11-27 17:34:09 +1100847 dev_kfree_skb_any(skb);
848 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000849 }
850 atm_charge(vcc, skb->truesize);
851 vcc->push(vcc, skb);
852 atomic_inc(&vcc->stats->rx);
853 break;
854
David Woodhouse87ebb182009-01-27 20:02:30 +1100855 case PKT_STATUS:
David Woodhouse95852f42009-01-30 14:23:52 +1100856 if (process_status(card, port, skb) &&
857 net_ratelimit()) {
858 dev_warn(&card->dev->dev, "Bad status packet of %d bytes on port %d:\n", skb->len, port);
859 print_buffer(skb);
860 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100861 dev_kfree_skb_any(skb);
David Woodhouse87ebb182009-01-27 20:02:30 +1100862 break;
863
David Woodhouse9c540042008-12-23 04:09:02 +0000864 case PKT_COMMAND:
865 default: /* FIXME: Not really, surely? */
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100866 if (process_command(card, port, skb))
867 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000868 spin_lock(&card->cli_queue_lock);
869 if (skb_queue_len(&card->cli_queue[port]) > 10) {
870 if (net_ratelimit())
871 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
872 port);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100873 dev_kfree_skb_any(skb);
David Woodhouse9c540042008-12-23 04:09:02 +0000874 } else
875 skb_queue_tail(&card->cli_queue[port], skb);
876 spin_unlock(&card->cli_queue_lock);
877 break;
878 }
879 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100880 /* Allocate RX skbs for any ports which need them */
881 if (card->using_dma && card->atmdev[port] &&
882 !card->rx_skb[port]) {
David Woodhousece816eb2015-09-16 12:35:00 +0100883 /* Unlike the MMIO case (qv) we can't add NET_IP_ALIGN
884 * here; the FPGA can only DMA to addresses which are
885 * aligned to 4 bytes. */
886 struct sk_buff *skb = dev_alloc_skb(RX_DMA_SIZE);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100887 if (skb) {
888 SKB_CB(skb)->dma_addr =
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -0500889 dma_map_single(&card->dev->dev, skb->data,
890 RX_DMA_SIZE, DMA_FROM_DEVICE);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100891 iowrite32(SKB_CB(skb)->dma_addr,
892 card->config_regs + RX_DMA_ADDR(port));
893 card->rx_skb[port] = skb;
894 } else {
895 if (net_ratelimit())
896 dev_warn(&card->dev->dev, "Failed to allocate RX skb");
897
898 /* We'll have to try again later */
899 tasklet_schedule(&card->tlet);
900 }
901 }
David Woodhouse9c540042008-12-23 04:09:02 +0000902 }
903 if (rx_done)
904 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
905
906 return;
907}
908
909static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
910{
911 struct hlist_head *head;
912 struct atm_vcc *vcc = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000913 struct sock *s;
914
915 read_lock(&vcc_sklist_lock);
916 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800917 sk_for_each(s, head) {
David Woodhouse9c540042008-12-23 04:09:02 +0000918 vcc = atm_sk(s);
919 if (vcc->dev == dev && vcc->vci == vci &&
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700920 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
921 test_bit(ATM_VF_READY, &vcc->flags))
David Woodhouse9c540042008-12-23 04:09:02 +0000922 goto out;
923 }
924 vcc = NULL;
925 out:
926 read_unlock(&vcc_sklist_lock);
927 return vcc;
928}
929
David Woodhouse9c540042008-12-23 04:09:02 +0000930static int popen(struct atm_vcc *vcc)
931{
932 struct solos_card *card = vcc->dev->dev_data;
933 struct sk_buff *skb;
934 struct pkt_hdr *header;
935
David Woodhouseb28a4b92009-01-27 21:50:36 +1100936 if (vcc->qos.aal != ATM_AAL5) {
937 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
938 vcc->qos.aal);
939 return -EINVAL;
940 }
941
David Woodhousea1db5c52012-11-29 23:27:20 +0000942 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
Jesper Juhlda1ab3e2011-02-13 10:49:32 +0000943 if (!skb) {
944 if (net_ratelimit())
945 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
David Woodhouse9c540042008-12-23 04:09:02 +0000946 return -ENOMEM;
947 }
948 header = (void *)skb_put(skb, sizeof(*header));
949
David Woodhouseb76811a2009-01-27 10:18:51 +1100950 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000951 header->vpi = cpu_to_le16(vcc->vpi);
952 header->vci = cpu_to_le16(vcc->vci);
953 header->type = cpu_to_le16(PKT_POPEN);
954
955 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
956
David Woodhousebdc54622009-01-29 14:37:20 +1100957 set_bit(ATM_VF_ADDR, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000958 set_bit(ATM_VF_READY, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000959
960 return 0;
961}
962
963static void pclose(struct atm_vcc *vcc)
964{
965 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000966 unsigned char port = SOLOS_CHAN(vcc->dev);
David Woodhouse213e85d2012-11-29 23:15:30 +0000967 struct sk_buff *skb, *tmpskb;
David Woodhouse9c540042008-12-23 04:09:02 +0000968 struct pkt_hdr *header;
969
David Woodhouse213e85d2012-11-29 23:15:30 +0000970 /* Remove any yet-to-be-transmitted packets from the pending queue */
971 spin_lock(&card->tx_queue_lock);
972 skb_queue_walk_safe(&card->tx_queue[port], skb, tmpskb) {
973 if (SKB_CB(skb)->vcc == vcc) {
974 skb_unlink(skb, &card->tx_queue[port]);
975 solos_pop(vcc, skb);
976 }
977 }
978 spin_unlock(&card->tx_queue_lock);
979
David Woodhousea1db5c52012-11-29 23:27:20 +0000980 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
David Woodhouse9c540042008-12-23 04:09:02 +0000981 if (!skb) {
982 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
983 return;
984 }
985 header = (void *)skb_put(skb, sizeof(*header));
986
David Woodhouseb76811a2009-01-27 10:18:51 +1100987 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000988 header->vpi = cpu_to_le16(vcc->vpi);
989 header->vci = cpu_to_le16(vcc->vci);
990 header->type = cpu_to_le16(PKT_PCLOSE);
991
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000992 skb_get(skb);
993 fpga_queue(card, port, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +0000994
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000995 if (!wait_event_timeout(card->param_wq, !skb_shared(skb), 5 * HZ))
996 dev_warn(&card->dev->dev,
997 "Timeout waiting for VCC close on port %d\n", port);
998
999 dev_kfree_skb(skb);
1000
David Woodhouse1f6ea6e2010-08-07 23:02:59 -07001001 /* Hold up vcc_destroy_socket() (our caller) until solos_bh() in the
1002 tasklet has finished processing any incoming packets (and, more to
1003 the point, using the vcc pointer). */
1004 tasklet_unlock_wait(&card->tlet);
David Woodhouse213e85d2012-11-29 23:15:30 +00001005
1006 clear_bit(ATM_VF_ADDR, &vcc->flags);
1007
David Woodhouse9c540042008-12-23 04:09:02 +00001008 return;
1009}
1010
1011static int print_buffer(struct sk_buff *buf)
1012{
1013 int len,i;
1014 char msg[500];
1015 char item[10];
1016
1017 len = buf->len;
1018 for (i = 0; i < len; i++){
1019 if(i % 8 == 0)
1020 sprintf(msg, "%02X: ", i);
1021
1022 sprintf(item,"%02X ",*(buf->data + i));
1023 strcat(msg, item);
1024 if(i % 8 == 7) {
1025 sprintf(item, "\n");
1026 strcat(msg, item);
1027 printk(KERN_DEBUG "%s", msg);
1028 }
1029 }
1030 if (i % 8 != 0) {
1031 sprintf(item, "\n");
1032 strcat(msg, item);
1033 printk(KERN_DEBUG "%s", msg);
1034 }
1035 printk(KERN_DEBUG "\n");
1036
1037 return 0;
1038}
1039
1040static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
1041 struct atm_vcc *vcc)
1042{
1043 int old_len;
David Woodhousef69e4172009-01-29 11:10:58 +11001044 unsigned long flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001045
David Woodhouse90937232009-01-28 16:46:56 +11001046 SKB_CB(skb)->vcc = vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001047
David Woodhousef69e4172009-01-29 11:10:58 +11001048 spin_lock_irqsave(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +00001049 old_len = skb_queue_len(&card->tx_queue[port]);
1050 skb_queue_tail(&card->tx_queue[port], skb);
David Woodhouse35c22212009-01-29 14:08:27 +11001051 if (!old_len)
David Woodhousef69e4172009-01-29 11:10:58 +11001052 card->tx_mask |= (1 << port);
David Woodhousef69e4172009-01-29 11:10:58 +11001053 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +00001054
David Woodhousef69e4172009-01-29 11:10:58 +11001055 /* Theoretically we could just schedule the tasklet here, but
1056 that introduces latency we don't want -- it's noticeable */
David Woodhouse9c540042008-12-23 04:09:02 +00001057 if (!old_len)
1058 fpga_tx(card);
1059}
1060
David Woodhouse35c22212009-01-29 14:08:27 +11001061static uint32_t fpga_tx(struct solos_card *card)
David Woodhouse9c540042008-12-23 04:09:02 +00001062{
David Woodhouse35c22212009-01-29 14:08:27 +11001063 uint32_t tx_pending, card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001064 uint32_t tx_started = 0;
1065 struct sk_buff *skb;
1066 struct atm_vcc *vcc;
1067 unsigned char port;
1068 unsigned long flags;
1069
1070 spin_lock_irqsave(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001071
1072 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
1073 /*
1074 * The queue lock is required for _writing_ to tx_mask, but we're
1075 * OK to read it here without locking. The only potential update
1076 * that we could race with is in fpga_queue() where it sets a bit
1077 * for a new port... but it's going to call this function again if
1078 * it's doing that, anyway.
1079 */
1080 tx_pending = card->tx_mask & ~card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001081
David Woodhouse35c22212009-01-29 14:08:27 +11001082 for (port = 0; tx_pending; tx_pending >>= 1, port++) {
1083 if (tx_pending & 1) {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001084 struct sk_buff *oldskb = card->tx_skb[port];
David Woodhousecae49ed2012-12-11 14:57:14 +00001085 if (oldskb) {
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001086 dma_unmap_single(&card->dev->dev, SKB_CB(oldskb)->dma_addr,
1087 oldskb->len, DMA_TO_DEVICE);
David Woodhousecae49ed2012-12-11 14:57:14 +00001088 card->tx_skb[port] = NULL;
1089 }
David Woodhouse9c540042008-12-23 04:09:02 +00001090 spin_lock(&card->tx_queue_lock);
1091 skb = skb_dequeue(&card->tx_queue[port]);
David Woodhousef69e4172009-01-29 11:10:58 +11001092 if (!skb)
1093 card->tx_mask &= ~(1 << port);
David Woodhouse9c540042008-12-23 04:09:02 +00001094 spin_unlock(&card->tx_queue_lock);
1095
David Woodhouseeaf83e32009-01-29 10:51:11 +11001096 if (skb && !card->using_dma) {
1097 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
David Woodhousebdc54622009-01-29 14:37:20 +11001098 tx_started |= 1 << port;
David Woodhouseeaf83e32009-01-29 10:51:11 +11001099 oldskb = skb; /* We're done with this skb already */
1100 } else if (skb && card->using_dma) {
David Woodhouse152a2a82012-12-19 11:01:21 +00001101 unsigned char *data = skb->data;
1102 if ((unsigned long)data & card->dma_alignment) {
1103 data = card->dma_bounce + (BUF_SIZE * port);
1104 memcpy(data, skb->data, skb->len);
1105 }
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001106 SKB_CB(skb)->dma_addr = dma_map_single(&card->dev->dev, data,
1107 skb->len, DMA_TO_DEVICE);
David Woodhouseb4bd8ad2012-05-24 04:58:27 +00001108 card->tx_skb[port] = skb;
David Woodhouseeaf83e32009-01-29 10:51:11 +11001109 iowrite32(SKB_CB(skb)->dma_addr,
1110 card->config_regs + TX_DMA_ADDR(port));
1111 }
1112
1113 if (!oldskb)
David Woodhouse9c540042008-12-23 04:09:02 +00001114 continue;
1115
David Woodhouseeaf83e32009-01-29 10:51:11 +11001116 /* Clean up and free oldskb now it's gone */
David Woodhouse9c540042008-12-23 04:09:02 +00001117 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001118 struct pkt_hdr *header = (void *)oldskb->data;
1119 int size = le16_to_cpu(header->size);
1120
1121 skb_pull(oldskb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +00001122 dev_info(&card->dev->dev, "Transmitted: port %d\n",
1123 port);
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001124 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
1125 size, le16_to_cpu(header->vpi),
1126 le16_to_cpu(header->vci));
David Woodhouseeaf83e32009-01-29 10:51:11 +11001127 print_buffer(oldskb);
David Woodhouse9c540042008-12-23 04:09:02 +00001128 }
David Woodhouse9c540042008-12-23 04:09:02 +00001129
David Woodhouseeaf83e32009-01-29 10:51:11 +11001130 vcc = SKB_CB(oldskb)->vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001131
1132 if (vcc) {
1133 atomic_inc(&vcc->stats->tx);
David Woodhouseeaf83e32009-01-29 10:51:11 +11001134 solos_pop(vcc, oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001135 } else {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001136 dev_kfree_skb_irq(oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001137 wake_up(&card->param_wq);
1138 }
David Woodhouse9c540042008-12-23 04:09:02 +00001139 }
1140 }
David Woodhousebdc54622009-01-29 14:37:20 +11001141 /* For non-DMA TX, write the 'TX start' bit for all four ports simultaneously */
David Woodhouse9c540042008-12-23 04:09:02 +00001142 if (tx_started)
1143 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
1144
1145 spin_unlock_irqrestore(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001146 return card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001147}
1148
1149static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
1150{
1151 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse9c540042008-12-23 04:09:02 +00001152 struct pkt_hdr *header;
David Woodhouseb76811a2009-01-27 10:18:51 +11001153 int pktlen;
David Woodhouse9c540042008-12-23 04:09:02 +00001154
David Woodhouseb76811a2009-01-27 10:18:51 +11001155 pktlen = skb->len;
1156 if (pktlen > (BUF_SIZE - sizeof(*header))) {
David Woodhouse9c540042008-12-23 04:09:02 +00001157 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
1158 solos_pop(vcc, skb);
1159 return 0;
1160 }
1161
1162 if (!skb_clone_writable(skb, sizeof(*header))) {
1163 int expand_by = 0;
1164 int ret;
1165
1166 if (skb_headroom(skb) < sizeof(*header))
1167 expand_by = sizeof(*header) - skb_headroom(skb);
1168
1169 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
1170 if (ret) {
Simon Farnsworth4306cad2009-01-19 21:19:29 +00001171 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
David Woodhouse9c540042008-12-23 04:09:02 +00001172 solos_pop(vcc, skb);
1173 return ret;
1174 }
1175 }
1176
1177 header = (void *)skb_push(skb, sizeof(*header));
1178
David Woodhouseb76811a2009-01-27 10:18:51 +11001179 /* This does _not_ include the size of the header */
1180 header->size = cpu_to_le16(pktlen);
David Woodhouse9c540042008-12-23 04:09:02 +00001181 header->vpi = cpu_to_le16(vcc->vpi);
1182 header->vci = cpu_to_le16(vcc->vci);
1183 header->type = cpu_to_le16(PKT_DATA);
1184
1185 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1186
1187 return 0;
1188}
1189
1190static struct atmdev_ops fpga_ops = {
1191 .open = popen,
1192 .close = pclose,
1193 .ioctl = NULL,
1194 .getsockopt = NULL,
1195 .setsockopt = NULL,
1196 .send = psend,
1197 .send_oam = NULL,
1198 .phy_put = NULL,
1199 .phy_get = NULL,
1200 .change_qos = NULL,
1201 .proc_read = NULL,
1202 .owner = THIS_MODULE
1203};
1204
1205static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
1206{
David Woodhousecd5549e2009-01-30 14:26:37 +11001207 int err;
David Woodhouse9c540042008-12-23 04:09:02 +00001208 uint16_t fpga_ver;
1209 uint8_t major_ver, minor_ver;
1210 uint32_t data32;
1211 struct solos_card *card;
1212
David Woodhouse9c540042008-12-23 04:09:02 +00001213 card = kzalloc(sizeof(*card), GFP_KERNEL);
1214 if (!card)
1215 return -ENOMEM;
1216
1217 card->dev = dev;
David Woodhousefa755b92009-01-27 14:16:12 +11001218 init_waitqueue_head(&card->fw_wq);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001219 init_waitqueue_head(&card->param_wq);
David Woodhouse9c540042008-12-23 04:09:02 +00001220
1221 err = pci_enable_device(dev);
1222 if (err) {
1223 dev_warn(&dev->dev, "Failed to enable PCI device\n");
1224 goto out;
1225 }
1226
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001227 err = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
David Woodhouse90937232009-01-28 16:46:56 +11001228 if (err) {
1229 dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
1230 goto out;
1231 }
1232
David Woodhouse9c540042008-12-23 04:09:02 +00001233 err = pci_request_regions(dev, "solos");
1234 if (err) {
1235 dev_warn(&dev->dev, "Failed to request regions\n");
1236 goto out;
1237 }
1238
1239 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1240 if (!card->config_regs) {
1241 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
Julia Lawall73112f92014-11-22 15:39:17 +01001242 err = -ENOMEM;
David Woodhouse9c540042008-12-23 04:09:02 +00001243 goto out_release_regions;
1244 }
1245 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1246 if (!card->buffers) {
1247 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
Julia Lawall73112f92014-11-22 15:39:17 +01001248 err = -ENOMEM;
David Woodhouse9c540042008-12-23 04:09:02 +00001249 goto out_unmap_config;
1250 }
1251
David Woodhousecc3657e2009-01-30 14:23:22 +11001252 if (reset) {
1253 iowrite32(1, card->config_regs + FPGA_MODE);
1254 data32 = ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001255
David Woodhousecc3657e2009-01-30 14:23:22 +11001256 iowrite32(0, card->config_regs + FPGA_MODE);
1257 data32 = ioread32(card->config_regs + FPGA_MODE);
1258 }
David Woodhouse9c540042008-12-23 04:09:02 +00001259
1260 data32 = ioread32(card->config_regs + FPGA_VER);
1261 fpga_ver = (data32 & 0x0000FFFF);
1262 major_ver = ((data32 & 0xFF000000) >> 24);
1263 minor_ver = ((data32 & 0x00FF0000) >> 16);
Nathan Williams4dbedf42009-03-25 20:27:37 +11001264 card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
1265 if (card->fpga_version > LEGACY_BUFFERS)
1266 card->buffer_size = BUF_SIZE;
1267 else
1268 card->buffer_size = OLD_BUF_SIZE;
David Woodhouse9c540042008-12-23 04:09:02 +00001269 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1270 major_ver, minor_ver, fpga_ver);
1271
David Woodhouse3ce12272010-11-01 10:35:28 +00001272 if (fpga_ver < 37 && (fpga_upgrade || firmware_upgrade ||
1273 db_fpga_upgrade || db_firmware_upgrade)) {
1274 dev_warn(&dev->dev,
1275 "FPGA too old; cannot upgrade flash. Use JTAG.\n");
1276 fpga_upgrade = firmware_upgrade = 0;
1277 db_fpga_upgrade = db_firmware_upgrade = 0;
1278 }
1279
Nathan Williams13af8162012-12-19 11:01:20 +00001280 /* Stopped using Atmel flash after 0.03-38 */
1281 if (fpga_ver < 39)
1282 card->atmel_flash = 1;
1283 else
1284 card->atmel_flash = 0;
1285
David Woodhouse152a2a82012-12-19 11:01:21 +00001286 data32 = ioread32(card->config_regs + PORTS);
1287 card->nr_ports = (data32 & 0x000000FF);
1288
David Woodhouseb4bd8ad2012-05-24 04:58:27 +00001289 if (card->fpga_version >= DMA_SUPPORTED) {
1290 pci_set_master(dev);
David Woodhouse90937232009-01-28 16:46:56 +11001291 card->using_dma = 1;
David Woodhouse152a2a82012-12-19 11:01:21 +00001292 if (1) { /* All known FPGA versions so far */
1293 card->dma_alignment = 3;
1294 card->dma_bounce = kmalloc(card->nr_ports * BUF_SIZE, GFP_KERNEL);
1295 if (!card->dma_bounce) {
1296 dev_warn(&card->dev->dev, "Failed to allocate DMA bounce buffers\n");
Julia Lawallc7bcae42014-08-07 14:49:07 +02001297 err = -ENOMEM;
David Woodhouse152a2a82012-12-19 11:01:21 +00001298 /* Fallback to MMIO doesn't work */
1299 goto out_unmap_both;
1300 }
1301 }
Nathan Williams4dbedf42009-03-25 20:27:37 +11001302 } else {
1303 card->using_dma = 0;
David Woodhouseeab50f72009-01-30 14:27:26 +11001304 /* Set RX empty flag for all ports */
1305 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1306 }
David Woodhouse9c540042008-12-23 04:09:02 +00001307
David Woodhouse9c540042008-12-23 04:09:02 +00001308 pci_set_drvdata(dev, card);
David Woodhousefa755b92009-01-27 14:16:12 +11001309
David Woodhouse9c540042008-12-23 04:09:02 +00001310 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1311 spin_lock_init(&card->tx_lock);
1312 spin_lock_init(&card->tx_queue_lock);
1313 spin_lock_init(&card->cli_queue_lock);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001314 spin_lock_init(&card->param_queue_lock);
1315 INIT_LIST_HEAD(&card->param_queue);
David Woodhouse9c540042008-12-23 04:09:02 +00001316
David Woodhousefcd82662009-01-29 11:29:12 +11001317 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
David Woodhouse9c540042008-12-23 04:09:02 +00001318 "solos-pci", card);
David Woodhousefa755b92009-01-27 14:16:12 +11001319 if (err) {
David Woodhouse9c540042008-12-23 04:09:02 +00001320 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
David Woodhousefa755b92009-01-27 14:16:12 +11001321 goto out_unmap_both;
1322 }
David Woodhouse9c540042008-12-23 04:09:02 +00001323
David Woodhouse9c540042008-12-23 04:09:02 +00001324 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1325
David Woodhousefa755b92009-01-27 14:16:12 +11001326 if (fpga_upgrade)
1327 flash_upgrade(card, 0);
1328
1329 if (firmware_upgrade)
1330 flash_upgrade(card, 1);
1331
Nathan Williams4dbedf42009-03-25 20:27:37 +11001332 if (db_fpga_upgrade)
1333 flash_upgrade(card, 2);
1334
1335 if (db_firmware_upgrade)
1336 flash_upgrade(card, 3);
1337
Dan Williamsd9ca6762010-12-08 19:40:47 +00001338 err = atm_init(card, &dev->dev);
David Woodhousefa755b92009-01-27 14:16:12 +11001339 if (err)
1340 goto out_free_irq;
1341
Nathan Williamsf9baad02012-12-19 11:01:18 +00001342 if (card->fpga_version >= DMA_SUPPORTED &&
1343 sysfs_create_group(&card->dev->dev.kobj, &gpio_attr_group))
1344 dev_err(&card->dev->dev, "Could not register parameter group for GPIOs\n");
1345
David Woodhouse9c540042008-12-23 04:09:02 +00001346 return 0;
1347
David Woodhousefa755b92009-01-27 14:16:12 +11001348 out_free_irq:
1349 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1350 free_irq(dev->irq, card);
1351 tasklet_kill(&card->tlet);
1352
David Woodhouse9c540042008-12-23 04:09:02 +00001353 out_unmap_both:
David Woodhouse152a2a82012-12-19 11:01:21 +00001354 kfree(card->dma_bounce);
David Woodhouse9c540042008-12-23 04:09:02 +00001355 pci_iounmap(dev, card->buffers);
Julia Lawall8ae0cfe2012-02-19 09:43:32 +00001356 out_unmap_config:
1357 pci_iounmap(dev, card->config_regs);
David Woodhouse9c540042008-12-23 04:09:02 +00001358 out_release_regions:
1359 pci_release_regions(dev);
1360 out:
Julia Lawallbc111d52009-02-08 17:00:02 -08001361 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001362 return err;
1363}
1364
Dan Williamsd9ca6762010-12-08 19:40:47 +00001365static int atm_init(struct solos_card *card, struct device *parent)
David Woodhouse9c540042008-12-23 04:09:02 +00001366{
1367 int i;
1368
David Woodhouse9c540042008-12-23 04:09:02 +00001369 for (i = 0; i < card->nr_ports; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +11001370 struct sk_buff *skb;
1371 struct pkt_hdr *header;
1372
David Woodhouse9c540042008-12-23 04:09:02 +00001373 skb_queue_head_init(&card->tx_queue[i]);
1374 skb_queue_head_init(&card->cli_queue[i]);
1375
Dan Williamsd9ca6762010-12-08 19:40:47 +00001376 card->atmdev[i] = atm_dev_register("solos-pci", parent, &fpga_ops, -1, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001377 if (!card->atmdev[i]) {
1378 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1379 atm_remove(card);
1380 return -ENODEV;
1381 }
1382 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1383 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
David Woodhoused057f0a2009-01-27 16:52:07 +11001384 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1385 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
David Woodhouse9c540042008-12-23 04:09:02 +00001386
1387 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1388
1389 card->atmdev[i]->ci_range.vpi_bits = 8;
1390 card->atmdev[i]->ci_range.vci_bits = 16;
1391 card->atmdev[i]->dev_data = card;
1392 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
Philip A. Prindevillec0312352011-03-30 13:17:04 +00001393 atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +11001394
David Woodhousea1db5c52012-11-29 23:27:20 +00001395 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
David Woodhouse87ebb182009-01-27 20:02:30 +11001396 if (!skb) {
1397 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1398 continue;
1399 }
1400
1401 header = (void *)skb_put(skb, sizeof(*header));
1402
1403 header->size = cpu_to_le16(0);
1404 header->vpi = cpu_to_le16(0);
1405 header->vci = cpu_to_le16(0);
1406 header->type = cpu_to_le16(PKT_STATUS);
1407
1408 fpga_queue(card, i, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001409 }
1410 return 0;
1411}
1412
1413static void atm_remove(struct solos_card *card)
1414{
1415 int i;
1416
1417 for (i = 0; i < card->nr_ports; i++) {
1418 if (card->atmdev[i]) {
David Woodhouse97d759d2009-03-17 14:59:34 +00001419 struct sk_buff *skb;
1420
David Woodhouse9c540042008-12-23 04:09:02 +00001421 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
David Woodhousec0fe3022009-01-28 14:34:34 +11001422
1423 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
David Woodhouse9c540042008-12-23 04:09:02 +00001424 atm_dev_deregister(card->atmdev[i]);
David Woodhouse97d759d2009-03-17 14:59:34 +00001425
1426 skb = card->rx_skb[i];
1427 if (skb) {
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001428 dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
1429 RX_DMA_SIZE, DMA_FROM_DEVICE);
David Woodhouse97d759d2009-03-17 14:59:34 +00001430 dev_kfree_skb(skb);
1431 }
1432 skb = card->tx_skb[i];
1433 if (skb) {
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001434 dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
1435 skb->len, DMA_TO_DEVICE);
David Woodhouse97d759d2009-03-17 14:59:34 +00001436 dev_kfree_skb(skb);
1437 }
1438 while ((skb = skb_dequeue(&card->tx_queue[i])))
1439 dev_kfree_skb(skb);
1440
David Woodhouse9c540042008-12-23 04:09:02 +00001441 }
1442 }
1443}
1444
1445static void fpga_remove(struct pci_dev *dev)
1446{
1447 struct solos_card *card = pci_get_drvdata(dev);
David Woodhouse97d759d2009-03-17 14:59:34 +00001448
1449 /* Disable IRQs */
1450 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
David Woodhouse9c540042008-12-23 04:09:02 +00001451
David Woodhouse97d759d2009-03-17 14:59:34 +00001452 /* Reset FPGA */
1453 iowrite32(1, card->config_regs + FPGA_MODE);
1454 (void)ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001455
Nathan Williamsf9baad02012-12-19 11:01:18 +00001456 if (card->fpga_version >= DMA_SUPPORTED)
1457 sysfs_remove_group(&card->dev->dev.kobj, &gpio_attr_group);
1458
David Woodhouse9c540042008-12-23 04:09:02 +00001459 atm_remove(card);
1460
David Woodhouse9c540042008-12-23 04:09:02 +00001461 free_irq(dev->irq, card);
1462 tasklet_kill(&card->tlet);
1463
David Woodhouse152a2a82012-12-19 11:01:21 +00001464 kfree(card->dma_bounce);
1465
David Woodhouse97d759d2009-03-17 14:59:34 +00001466 /* Release device from reset */
1467 iowrite32(0, card->config_regs + FPGA_MODE);
1468 (void)ioread32(card->config_regs + FPGA_MODE);
1469
David Woodhouse9c540042008-12-23 04:09:02 +00001470 pci_iounmap(dev, card->buffers);
1471 pci_iounmap(dev, card->config_regs);
1472
David Woodhouse9c540042008-12-23 04:09:02 +00001473 pci_release_regions(dev);
1474 pci_disable_device(dev);
1475
David Woodhouse9c540042008-12-23 04:09:02 +00001476 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001477}
1478
Greg Kroah-Hartman6c445122012-12-21 13:25:04 -08001479static struct pci_device_id fpga_pci_tbl[] = {
David Woodhouse9c540042008-12-23 04:09:02 +00001480 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1481 { 0, }
1482};
1483
1484MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1485
1486static struct pci_driver fpga_driver = {
1487 .name = "solos",
1488 .id_table = fpga_pci_tbl,
1489 .probe = fpga_probe,
1490 .remove = fpga_remove,
1491};
1492
1493
1494static int __init solos_pci_init(void)
1495{
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001496 BUILD_BUG_ON(sizeof(struct solos_skb_cb) > sizeof(((struct sk_buff *)0)->cb));
1497
David Woodhouse9c540042008-12-23 04:09:02 +00001498 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1499 return pci_register_driver(&fpga_driver);
1500}
1501
1502static void __exit solos_pci_exit(void)
1503{
1504 pci_unregister_driver(&fpga_driver);
1505 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1506}
1507
1508module_init(solos_pci_init);
1509module_exit(solos_pci_exit);