blob: 94fbc3abe60e6a714c546bc2e8c6dce321742bbe [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
David Woodhouse9c540042008-12-23 04:09:02 +00002/*
3 * Driver for the Solos PCI ADSL2+ card, designed to support Linux by
Alexander A. Klimoved757322020-07-13 12:24:18 +02004 * Traverse Technologies -- https://www.traverse.com.au/
David Woodhouse9c540042008-12-23 04:09:02 +00005 * Xrio Limited -- http://www.xrio.com/
6 *
David Woodhouse9c540042008-12-23 04:09:02 +00007 * 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
15#define DEBUG
16#define VERBOSE_DEBUG
17
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/ioport.h>
23#include <linux/types.h>
24#include <linux/pci.h>
25#include <linux/atm.h>
26#include <linux/atmdev.h>
27#include <linux/skbuff.h>
28#include <linux/sysfs.h>
29#include <linux/device.h>
30#include <linux/kobject.h>
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000031#include <linux/firmware.h>
David Woodhouse01e2ffa2009-01-27 16:20:04 +110032#include <linux/ctype.h>
33#include <linux/swab.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
David Woodhouse9c540042008-12-23 04:09:02 +000035
Nathan Williams13af8162012-12-19 11:01:20 +000036#define VERSION "1.04"
37#define DRIVER_VERSION 0x01
David Woodhouse9c540042008-12-23 04:09:02 +000038#define PTAG "solos-pci"
39
40#define CONFIG_RAM_SIZE 128
41#define FLAGS_ADDR 0x7C
42#define IRQ_EN_ADDR 0x78
43#define FPGA_VER 0x74
44#define IRQ_CLEAR 0x70
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000045#define WRITE_FLASH 0x6C
46#define PORTS 0x68
47#define FLASH_BLOCK 0x64
48#define FLASH_BUSY 0x60
49#define FPGA_MODE 0x5C
50#define FLASH_MODE 0x58
Nathan Williamsf9baad02012-12-19 11:01:18 +000051#define GPIO_STATUS 0x54
Nathan Williams13af8162012-12-19 11:01:20 +000052#define DRIVER_VER 0x50
David Woodhouse90937232009-01-28 16:46:56 +110053#define TX_DMA_ADDR(port) (0x40 + (4 * (port)))
54#define RX_DMA_ADDR(port) (0x30 + (4 * (port)))
David Woodhouse9c540042008-12-23 04:09:02 +000055
56#define DATA_RAM_SIZE 32768
Nathan Williams4dbedf42009-03-25 20:27:37 +110057#define BUF_SIZE 2048
58#define OLD_BUF_SIZE 4096 /* For FPGA versions <= 2*/
Nathan Williams13af8162012-12-19 11:01:20 +000059/* Old boards use ATMEL AD45DB161D flash */
60#define ATMEL_FPGA_PAGE 528 /* FPGA flash page size*/
61#define ATMEL_SOLOS_PAGE 512 /* Solos flash page size*/
62#define ATMEL_FPGA_BLOCK (ATMEL_FPGA_PAGE * 8) /* FPGA block size*/
63#define ATMEL_SOLOS_BLOCK (ATMEL_SOLOS_PAGE * 8) /* Solos block size*/
64/* Current boards use M25P/M25PE SPI flash */
65#define SPI_FLASH_BLOCK (256 * 64)
David Woodhouse9c540042008-12-23 04:09:02 +000066
Nathan Williams4dbedf42009-03-25 20:27:37 +110067#define RX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2)
68#define TX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2 + (card->buffer_size))
69#define FLASH_BUF ((card->buffers) + 4*(card->buffer_size)*2)
David Woodhouse9c540042008-12-23 04:09:02 +000070
David Woodhouseeaf83e32009-01-29 10:51:11 +110071#define RX_DMA_SIZE 2048
72
Nathan Williams4dbedf42009-03-25 20:27:37 +110073#define FPGA_VERSION(a,b) (((a) << 8) + (b))
74#define LEGACY_BUFFERS 2
75#define DMA_SUPPORTED 4
76
David Woodhousecc3657e2009-01-30 14:23:22 +110077static int reset = 0;
David Woodhouse9c540042008-12-23 04:09:02 +000078static int atmdebug = 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000079static int firmware_upgrade = 0;
80static int fpga_upgrade = 0;
Nathan Williams4dbedf42009-03-25 20:27:37 +110081static int db_firmware_upgrade = 0;
82static int db_fpga_upgrade = 0;
David Woodhouse9c540042008-12-23 04:09:02 +000083
84struct pkt_hdr {
85 __le16 size;
86 __le16 vpi;
87 __le16 vci;
88 __le16 type;
89};
90
David Woodhouse90937232009-01-28 16:46:56 +110091struct solos_skb_cb {
92 struct atm_vcc *vcc;
93 uint32_t dma_addr;
94};
95
96
97#define SKB_CB(skb) ((struct solos_skb_cb *)skb->cb)
98
David Woodhouse9c540042008-12-23 04:09:02 +000099#define PKT_DATA 0
100#define PKT_COMMAND 1
101#define PKT_POPEN 3
102#define PKT_PCLOSE 4
David Woodhouse87ebb182009-01-27 20:02:30 +1100103#define PKT_STATUS 5
David Woodhouse9c540042008-12-23 04:09:02 +0000104
105struct solos_card {
106 void __iomem *config_regs;
107 void __iomem *buffers;
108 int nr_ports;
David Woodhousef69e4172009-01-29 11:10:58 +1100109 int tx_mask;
David Woodhouse9c540042008-12-23 04:09:02 +0000110 struct pci_dev *dev;
111 struct atm_dev *atmdev[4];
112 struct tasklet_struct tlet;
113 spinlock_t tx_lock;
114 spinlock_t tx_queue_lock;
115 spinlock_t cli_queue_lock;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100116 spinlock_t param_queue_lock;
117 struct list_head param_queue;
David Woodhouse9c540042008-12-23 04:09:02 +0000118 struct sk_buff_head tx_queue[4];
119 struct sk_buff_head cli_queue[4];
David Woodhouse90937232009-01-28 16:46:56 +1100120 struct sk_buff *tx_skb[4];
121 struct sk_buff *rx_skb[4];
David Woodhouse152a2a82012-12-19 11:01:21 +0000122 unsigned char *dma_bounce;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100123 wait_queue_head_t param_wq;
David Woodhousefa755b92009-01-27 14:16:12 +1100124 wait_queue_head_t fw_wq;
David Woodhouse90937232009-01-28 16:46:56 +1100125 int using_dma;
David Woodhouse152a2a82012-12-19 11:01:21 +0000126 int dma_alignment;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100127 int fpga_version;
128 int buffer_size;
Nathan Williams13af8162012-12-19 11:01:20 +0000129 int atmel_flash;
David Woodhouse9c540042008-12-23 04:09:02 +0000130};
131
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100132
133struct solos_param {
134 struct list_head list;
135 pid_t pid;
136 int port;
137 struct sk_buff *response;
David Woodhouse9c540042008-12-23 04:09:02 +0000138};
139
140#define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
141
142MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
143MODULE_DESCRIPTION("Solos PCI driver");
144MODULE_VERSION(VERSION);
145MODULE_LICENSE("GPL");
Ben Hutchings9fca79d2009-11-07 11:40:32 +0000146MODULE_FIRMWARE("solos-FPGA.bin");
147MODULE_FIRMWARE("solos-Firmware.bin");
148MODULE_FIRMWARE("solos-db-FPGA.bin");
David Woodhousecc3657e2009-01-30 14:23:22 +1100149MODULE_PARM_DESC(reset, "Reset Solos chips on startup");
David Woodhouse9c540042008-12-23 04:09:02 +0000150MODULE_PARM_DESC(atmdebug, "Print ATM data");
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000151MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
152MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100153MODULE_PARM_DESC(db_firmware_upgrade, "Initiate daughter board Solos firmware upgrade");
154MODULE_PARM_DESC(db_fpga_upgrade, "Initiate daughter board FPGA upgrade");
David Woodhousecc3657e2009-01-30 14:23:22 +1100155module_param(reset, int, 0444);
Simon Farnsworth4306cad2009-01-19 21:19:29 +0000156module_param(atmdebug, int, 0644);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000157module_param(firmware_upgrade, int, 0444);
158module_param(fpga_upgrade, int, 0444);
Nathan Williams4dbedf42009-03-25 20:27:37 +1100159module_param(db_firmware_upgrade, int, 0444);
160module_param(db_fpga_upgrade, int, 0444);
David Woodhouse9c540042008-12-23 04:09:02 +0000161
162static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
163 struct atm_vcc *vcc);
David Woodhouse35c22212009-01-29 14:08:27 +1100164static uint32_t fpga_tx(struct solos_card *);
David Woodhouse9c540042008-12-23 04:09:02 +0000165static irqreturn_t solos_irq(int irq, void *dev_id);
166static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
Dan Williamsd9ca6762010-12-08 19:40:47 +0000167static int atm_init(struct solos_card *, struct device *);
David Woodhouse9c540042008-12-23 04:09:02 +0000168static void atm_remove(struct solos_card *);
169static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
170static void solos_bh(unsigned long);
171static int print_buffer(struct sk_buff *buf);
172
173static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
174{
175 if (vcc->pop)
176 vcc->pop(vcc, skb);
177 else
178 dev_kfree_skb_any(skb);
179}
180
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100181static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
182 char *buf)
183{
184 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
185 struct solos_card *card = atmdev->dev_data;
186 struct solos_param prm;
187 struct sk_buff *skb;
188 struct pkt_hdr *header;
189 int buflen;
190
191 buflen = strlen(attr->attr.name) + 10;
192
David Woodhouse3456b222009-01-28 10:39:23 +1100193 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100194 if (!skb) {
195 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
196 return -ENOMEM;
197 }
198
Johannes Berg4df864c2017-06-16 14:29:21 +0200199 header = skb_put(skb, sizeof(*header));
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100200
201 buflen = snprintf((void *)&header[1], buflen - 1,
202 "L%05d\n%s\n", current->pid, attr->attr.name);
203 skb_put(skb, buflen);
204
205 header->size = cpu_to_le16(buflen);
206 header->vpi = cpu_to_le16(0);
207 header->vci = cpu_to_le16(0);
208 header->type = cpu_to_le16(PKT_COMMAND);
209
210 prm.pid = current->pid;
211 prm.response = NULL;
212 prm.port = SOLOS_CHAN(atmdev);
213
214 spin_lock_irq(&card->param_queue_lock);
215 list_add(&prm.list, &card->param_queue);
216 spin_unlock_irq(&card->param_queue_lock);
217
218 fpga_queue(card, prm.port, skb, NULL);
219
220 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
221
222 spin_lock_irq(&card->param_queue_lock);
223 list_del(&prm.list);
224 spin_unlock_irq(&card->param_queue_lock);
225
226 if (!prm.response)
227 return -EIO;
228
229 buflen = prm.response->len;
230 memcpy(buf, prm.response->data, buflen);
231 kfree_skb(prm.response);
232
233 return buflen;
234}
235
236static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
237 const char *buf, size_t count)
238{
239 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
240 struct solos_card *card = atmdev->dev_data;
241 struct solos_param prm;
242 struct sk_buff *skb;
243 struct pkt_hdr *header;
244 int buflen;
245 ssize_t ret;
246
247 buflen = strlen(attr->attr.name) + 11 + count;
248
David Woodhouse3456b222009-01-28 10:39:23 +1100249 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100250 if (!skb) {
251 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
252 return -ENOMEM;
253 }
254
Johannes Berg4df864c2017-06-16 14:29:21 +0200255 header = skb_put(skb, sizeof(*header));
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100256
257 buflen = snprintf((void *)&header[1], buflen - 1,
258 "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
259
260 skb_put(skb, buflen);
261 header->size = cpu_to_le16(buflen);
262 header->vpi = cpu_to_le16(0);
263 header->vci = cpu_to_le16(0);
264 header->type = cpu_to_le16(PKT_COMMAND);
265
266 prm.pid = current->pid;
267 prm.response = NULL;
268 prm.port = SOLOS_CHAN(atmdev);
269
270 spin_lock_irq(&card->param_queue_lock);
271 list_add(&prm.list, &card->param_queue);
272 spin_unlock_irq(&card->param_queue_lock);
273
274 fpga_queue(card, prm.port, skb, NULL);
275
276 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
277
278 spin_lock_irq(&card->param_queue_lock);
279 list_del(&prm.list);
280 spin_unlock_irq(&card->param_queue_lock);
281
282 skb = prm.response;
283
284 if (!skb)
285 return -EIO;
286
287 buflen = skb->len;
288
289 /* Sometimes it has a newline, sometimes it doesn't. */
290 if (skb->data[buflen - 1] == '\n')
291 buflen--;
292
293 if (buflen == 2 && !strncmp(skb->data, "OK", 2))
294 ret = count;
295 else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
296 ret = -EIO;
297 else {
298 /* We know we have enough space allocated for this; we allocated
299 it ourselves */
300 skb->data[buflen] = 0;
301
302 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
303 skb->data);
304 ret = -EIO;
305 }
306 kfree_skb(skb);
307
308 return ret;
309}
310
David Woodhouse87ebb182009-01-27 20:02:30 +1100311static char *next_string(struct sk_buff *skb)
312{
313 int i = 0;
314 char *this = skb->data;
David Woodhousec6428e52009-01-29 12:17:09 +1100315
316 for (i = 0; i < skb->len; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +1100317 if (this[i] == '\n') {
318 this[i] = 0;
David Woodhousec6428e52009-01-29 12:17:09 +1100319 skb_pull(skb, i + 1);
David Woodhouse87ebb182009-01-27 20:02:30 +1100320 return this;
321 }
David Woodhousec6428e52009-01-29 12:17:09 +1100322 if (!isprint(this[i]))
323 return NULL;
David Woodhouse87ebb182009-01-27 20:02:30 +1100324 }
325 return NULL;
326}
327
328/*
329 * Status packet has fields separated by \n, starting with a version number
330 * for the information therein. Fields are....
331 *
332 * packet version
David Woodhouse87ebb182009-01-27 20:02:30 +1100333 * RxBitRate (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100334 * TxBitRate (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100335 * State (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100336 * LocalSNRMargin (version >= 1)
337 * LocalLineAttn (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100338 */
339static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
340{
LABBE Corentine94d91a2015-12-04 08:43:19 +0100341 char *str, *state_str, *snr, *attn;
342 int ver, rate_up, rate_down, err;
David Woodhouse87ebb182009-01-27 20:02:30 +1100343
344 if (!card->atmdev[port])
345 return -ENODEV;
346
347 str = next_string(skb);
348 if (!str)
349 return -EIO;
350
LABBE Corentine94d91a2015-12-04 08:43:19 +0100351 err = kstrtoint(str, 10, &ver);
352 if (err) {
353 dev_warn(&card->dev->dev, "Unexpected status interrupt version\n");
354 return err;
355 }
David Woodhouse87ebb182009-01-27 20:02:30 +1100356 if (ver < 1) {
357 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
358 ver);
359 return -EIO;
360 }
361
362 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100363 if (!str)
364 return -EIO;
David Woodhouse95852f42009-01-30 14:23:52 +1100365 if (!strcmp(str, "ERROR")) {
366 dev_dbg(&card->dev->dev, "Status packet indicated Solos error on port %d (starting up?)\n",
367 port);
368 return 0;
369 }
370
LABBE Corentine94d91a2015-12-04 08:43:19 +0100371 err = kstrtoint(str, 10, &rate_down);
372 if (err)
373 return err;
David Woodhouse87ebb182009-01-27 20:02:30 +1100374
375 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100376 if (!str)
377 return -EIO;
LABBE Corentine94d91a2015-12-04 08:43:19 +0100378 err = kstrtoint(str, 10, &rate_up);
379 if (err)
380 return err;
David Woodhouse87ebb182009-01-27 20:02:30 +1100381
David Woodhouseaf780652009-01-28 10:22:57 +1100382 state_str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100383 if (!state_str)
384 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100385
386 /* Anything but 'Showtime' is down */
387 if (strcmp(state_str, "Showtime")) {
Karl Hiramoto49d49102010-07-08 20:55:34 +0000388 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
David Woodhousef87b2ed2009-01-30 14:31:36 +1100389 dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
390 return 0;
David Woodhouse1e615df2009-01-27 21:47:47 +1100391 }
David Woodhouse87ebb182009-01-27 20:02:30 +1100392
David Woodhousef87b2ed2009-01-30 14:31:36 +1100393 snr = next_string(skb);
Julia Lawall6cf57672009-07-27 11:38:52 -0700394 if (!snr)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100395 return -EIO;
396 attn = next_string(skb);
397 if (!attn)
398 return -EIO;
David Woodhouseaf780652009-01-28 10:22:57 +1100399
David Woodhousef87b2ed2009-01-30 14:31:36 +1100400 dev_info(&card->dev->dev, "Port %d: %s @%d/%d kb/s%s%s%s%s\n",
401 port, state_str, rate_down/1000, rate_up/1000,
402 snr[0]?", SNR ":"", snr, attn[0]?", Attn ":"", attn);
403
David Woodhousec6428e52009-01-29 12:17:09 +1100404 card->atmdev[port]->link_rate = rate_down / 424;
Karl Hiramoto49d49102010-07-08 20:55:34 +0000405 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +1100406
David Woodhouse87ebb182009-01-27 20:02:30 +1100407 return 0;
408}
409
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100410static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
411{
412 struct solos_param *prm;
413 unsigned long flags;
414 int cmdpid;
LABBE Corentine94d91a2015-12-04 08:43:19 +0100415 int found = 0, err;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100416
417 if (skb->len < 7)
418 return 0;
419
420 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
421 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
422 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
423 skb->data[6] != '\n')
424 return 0;
425
LABBE Corentine94d91a2015-12-04 08:43:19 +0100426 err = kstrtoint(&skb->data[1], 10, &cmdpid);
427 if (err)
428 return err;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100429
430 spin_lock_irqsave(&card->param_queue_lock, flags);
431 list_for_each_entry(prm, &card->param_queue, list) {
432 if (prm->port == port && prm->pid == cmdpid) {
433 prm->response = skb;
434 skb_pull(skb, 7);
435 wake_up(&card->param_wq);
436 found = 1;
437 break;
438 }
439 }
440 spin_unlock_irqrestore(&card->param_queue_lock, flags);
441 return found;
442}
443
David Woodhouse9c540042008-12-23 04:09:02 +0000444static ssize_t console_show(struct device *dev, struct device_attribute *attr,
445 char *buf)
446{
447 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
448 struct solos_card *card = atmdev->dev_data;
449 struct sk_buff *skb;
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000450 unsigned int len;
David Woodhouse9c540042008-12-23 04:09:02 +0000451
452 spin_lock(&card->cli_queue_lock);
453 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
454 spin_unlock(&card->cli_queue_lock);
455 if(skb == NULL)
456 return sprintf(buf, "No data.\n");
457
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000458 len = skb->len;
459 memcpy(buf, skb->data, len);
David Woodhouse9c540042008-12-23 04:09:02 +0000460
461 kfree_skb(skb);
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000462 return len;
David Woodhouse9c540042008-12-23 04:09:02 +0000463}
464
465static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
466{
467 struct sk_buff *skb;
468 struct pkt_hdr *header;
469
David Woodhouse9c540042008-12-23 04:09:02 +0000470 if (size > (BUF_SIZE - sizeof(*header))) {
471 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
472 return 0;
473 }
474 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
475 if (!skb) {
476 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
477 return 0;
478 }
479
Johannes Berg4df864c2017-06-16 14:29:21 +0200480 header = skb_put(skb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +0000481
482 header->size = cpu_to_le16(size);
483 header->vpi = cpu_to_le16(0);
484 header->vci = cpu_to_le16(0);
485 header->type = cpu_to_le16(PKT_COMMAND);
486
Johannes Berg59ae1d12017-06-16 14:29:20 +0200487 skb_put_data(skb, buf, size);
David Woodhouse9c540042008-12-23 04:09:02 +0000488
489 fpga_queue(card, dev, skb, NULL);
490
491 return 0;
492}
493
494static ssize_t console_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
496{
497 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
498 struct solos_card *card = atmdev->dev_data;
499 int err;
500
501 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
502
503 return err?:count;
504}
505
Nathan Williamsf9baad02012-12-19 11:01:18 +0000506struct geos_gpio_attr {
507 struct device_attribute attr;
508 int offset;
509};
510
511#define SOLOS_GPIO_ATTR(_name, _mode, _show, _store, _offset) \
512 struct geos_gpio_attr gpio_attr_##_name = { \
513 .attr = __ATTR(_name, _mode, _show, _store), \
514 .offset = _offset }
515
516static ssize_t geos_gpio_store(struct device *dev, struct device_attribute *attr,
517 const char *buf, size_t count)
518{
Nathan Williamsf9baad02012-12-19 11:01:18 +0000519 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
Chuhong Yuanc349c0a2019-07-23 16:13:14 +0800520 struct solos_card *card = dev_get_drvdata(dev);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000521 uint32_t data32;
522
523 if (count != 1 && (count != 2 || buf[1] != '\n'))
524 return -EINVAL;
525
526 spin_lock_irq(&card->param_queue_lock);
527 data32 = ioread32(card->config_regs + GPIO_STATUS);
528 if (buf[0] == '1') {
529 data32 |= 1 << gattr->offset;
530 iowrite32(data32, card->config_regs + GPIO_STATUS);
531 } else if (buf[0] == '0') {
532 data32 &= ~(1 << gattr->offset);
533 iowrite32(data32, card->config_regs + GPIO_STATUS);
534 } else {
535 count = -EINVAL;
536 }
Dan Carpenter29042072012-12-19 21:48:45 +0000537 spin_unlock_irq(&card->param_queue_lock);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000538 return count;
539}
540
541static ssize_t geos_gpio_show(struct device *dev, struct device_attribute *attr,
542 char *buf)
543{
Nathan Williamsf9baad02012-12-19 11:01:18 +0000544 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
Chuhong Yuanc349c0a2019-07-23 16:13:14 +0800545 struct solos_card *card = dev_get_drvdata(dev);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000546 uint32_t data32;
547
548 data32 = ioread32(card->config_regs + GPIO_STATUS);
549 data32 = (data32 >> gattr->offset) & 1;
550
551 return sprintf(buf, "%d\n", data32);
552}
553
554static ssize_t hardware_show(struct device *dev, struct device_attribute *attr,
555 char *buf)
556{
Nathan Williamsf9baad02012-12-19 11:01:18 +0000557 struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
Chuhong Yuanc349c0a2019-07-23 16:13:14 +0800558 struct solos_card *card = dev_get_drvdata(dev);
Nathan Williamsf9baad02012-12-19 11:01:18 +0000559 uint32_t data32;
560
561 data32 = ioread32(card->config_regs + GPIO_STATUS);
562 switch (gattr->offset) {
563 case 0:
564 /* HardwareVersion */
565 data32 = data32 & 0x1F;
566 break;
567 case 1:
568 /* HardwareVariant */
569 data32 = (data32 >> 5) & 0x0F;
570 break;
571 }
572 return sprintf(buf, "%d\n", data32);
573}
574
Julia Lawall89d91232016-10-29 21:37:09 +0200575static DEVICE_ATTR_RW(console);
David Woodhouse9c540042008-12-23 04:09:02 +0000576
David Woodhoused057f0a2009-01-27 16:52:07 +1100577
578#define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
579#define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
580
581#include "solos-attrlist.c"
582
Nathan Williamsf9baad02012-12-19 11:01:18 +0000583static SOLOS_GPIO_ATTR(GPIO1, 0644, geos_gpio_show, geos_gpio_store, 9);
584static SOLOS_GPIO_ATTR(GPIO2, 0644, geos_gpio_show, geos_gpio_store, 10);
585static SOLOS_GPIO_ATTR(GPIO3, 0644, geos_gpio_show, geos_gpio_store, 11);
586static SOLOS_GPIO_ATTR(GPIO4, 0644, geos_gpio_show, geos_gpio_store, 12);
587static SOLOS_GPIO_ATTR(GPIO5, 0644, geos_gpio_show, geos_gpio_store, 13);
588static SOLOS_GPIO_ATTR(PushButton, 0444, geos_gpio_show, NULL, 14);
589static SOLOS_GPIO_ATTR(HardwareVersion, 0444, hardware_show, NULL, 0);
590static SOLOS_GPIO_ATTR(HardwareVariant, 0444, hardware_show, NULL, 1);
David Woodhoused057f0a2009-01-27 16:52:07 +1100591#undef SOLOS_ATTR_RO
592#undef SOLOS_ATTR_RW
593
594#define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
595#define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
596
597static struct attribute *solos_attrs[] = {
598#include "solos-attrlist.c"
599 NULL
600};
601
Amitoj Kaur Chawla638ce0f2017-08-01 19:57:47 -0400602static const struct attribute_group solos_attr_group = {
David Woodhoused057f0a2009-01-27 16:52:07 +1100603 .attrs = solos_attrs,
604 .name = "parameters",
605};
David Woodhouse9c540042008-12-23 04:09:02 +0000606
Nathan Williamsf9baad02012-12-19 11:01:18 +0000607static struct attribute *gpio_attrs[] = {
608 &gpio_attr_GPIO1.attr.attr,
609 &gpio_attr_GPIO2.attr.attr,
610 &gpio_attr_GPIO3.attr.attr,
611 &gpio_attr_GPIO4.attr.attr,
612 &gpio_attr_GPIO5.attr.attr,
613 &gpio_attr_PushButton.attr.attr,
614 &gpio_attr_HardwareVersion.attr.attr,
615 &gpio_attr_HardwareVariant.attr.attr,
616 NULL
617};
618
Amitoj Kaur Chawla638ce0f2017-08-01 19:57:47 -0400619static const struct attribute_group gpio_attr_group = {
Nathan Williamsf9baad02012-12-19 11:01:18 +0000620 .attrs = gpio_attrs,
621 .name = "gpio",
622};
623
David Woodhousefa755b92009-01-27 14:16:12 +1100624static int flash_upgrade(struct solos_card *card, int chip)
625{
626 const struct firmware *fw;
627 const char *fw_name;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000628 int blocksize = 0;
629 int numblocks = 0;
David Woodhousefa755b92009-01-27 14:16:12 +1100630 int offset;
631
Andrew Morton7adcdb42009-11-17 12:46:44 +0000632 switch (chip) {
633 case 0:
David Woodhousefa755b92009-01-27 14:16:12 +1100634 fw_name = "solos-FPGA.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000635 if (card->atmel_flash)
636 blocksize = ATMEL_FPGA_BLOCK;
637 else
638 blocksize = SPI_FLASH_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000639 break;
640 case 1:
David Woodhousefa755b92009-01-27 14:16:12 +1100641 fw_name = "solos-Firmware.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000642 if (card->atmel_flash)
643 blocksize = ATMEL_SOLOS_BLOCK;
644 else
645 blocksize = SPI_FLASH_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000646 break;
647 case 2:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100648 if (card->fpga_version > LEGACY_BUFFERS){
649 fw_name = "solos-db-FPGA.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000650 if (card->atmel_flash)
651 blocksize = ATMEL_FPGA_BLOCK;
652 else
653 blocksize = SPI_FLASH_BLOCK;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100654 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000655 dev_info(&card->dev->dev, "FPGA version doesn't support"
656 " daughter board upgrades\n");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100657 return -EPERM;
658 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000659 break;
660 case 3:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100661 if (card->fpga_version > LEGACY_BUFFERS){
662 fw_name = "solos-Firmware.bin";
Nathan Williams13af8162012-12-19 11:01:20 +0000663 if (card->atmel_flash)
664 blocksize = ATMEL_SOLOS_BLOCK;
665 else
666 blocksize = SPI_FLASH_BLOCK;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100667 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000668 dev_info(&card->dev->dev, "FPGA version doesn't support"
669 " daughter board upgrades\n");
670 return -EPERM;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100671 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000672 break;
673 default:
674 return -ENODEV;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100675 }
David Woodhousefa755b92009-01-27 14:16:12 +1100676
677 if (request_firmware(&fw, fw_name, &card->dev->dev))
678 return -ENOENT;
679
680 dev_info(&card->dev->dev, "Flash upgrade starting\n");
681
Nathan Williams13af8162012-12-19 11:01:20 +0000682 /* New FPGAs require driver version before permitting flash upgrades */
683 iowrite32(DRIVER_VERSION, card->config_regs + DRIVER_VER);
684
David Woodhousefa755b92009-01-27 14:16:12 +1100685 numblocks = fw->size / blocksize;
686 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000687 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
688
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000689 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
690 iowrite32(1, card->config_regs + FPGA_MODE);
David S. Miller06091ed2011-04-17 00:11:25 -0700691 (void) ioread32(card->config_regs + FPGA_MODE);
David Woodhousefa755b92009-01-27 14:16:12 +1100692
693 /* Set mode to Chip Erase */
Nathan Williams4dbedf42009-03-25 20:27:37 +1100694 if(chip == 0 || chip == 2)
695 dev_info(&card->dev->dev, "Set FPGA Flash mode to FPGA Chip Erase\n");
696 if(chip == 1 || chip == 3)
697 dev_info(&card->dev->dev, "Set FPGA Flash mode to Solos Chip Erase\n");
David Woodhousefa755b92009-01-27 14:16:12 +1100698 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
699
700
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000701 iowrite32(1, card->config_regs + WRITE_FLASH);
David Woodhousefa755b92009-01-27 14:16:12 +1100702 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000703
David Woodhousefa755b92009-01-27 14:16:12 +1100704 for (offset = 0; offset < fw->size; offset += blocksize) {
705 int i;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000706
David Woodhousefa755b92009-01-27 14:16:12 +1100707 /* Clear write flag */
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000708 iowrite32(0, card->config_regs + WRITE_FLASH);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000709
David Woodhousefa755b92009-01-27 14:16:12 +1100710 /* Set mode to Block Write */
711 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
712 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
713
Nathan Williams13af8162012-12-19 11:01:20 +0000714 /* Copy block to buffer, swapping each 16 bits for Atmel flash */
David Woodhousefa755b92009-01-27 14:16:12 +1100715 for(i = 0; i < blocksize; i += 4) {
Nathan Williams13af8162012-12-19 11:01:20 +0000716 uint32_t word;
717 if (card->atmel_flash)
718 word = swahb32p((uint32_t *)(fw->data + offset + i));
719 else
720 word = *(uint32_t *)(fw->data + offset + i);
Nathan Williams4dbedf42009-03-25 20:27:37 +1100721 if(card->fpga_version > LEGACY_BUFFERS)
722 iowrite32(word, FLASH_BUF + i);
723 else
724 iowrite32(word, RX_BUF(card, 3) + i);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000725 }
726
David Woodhousefa755b92009-01-27 14:16:12 +1100727 /* Specify block number and then trigger flash write */
728 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
729 iowrite32(1, card->config_regs + WRITE_FLASH);
730 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000731 }
732
David Woodhousefa755b92009-01-27 14:16:12 +1100733 release_firmware(fw);
734 iowrite32(0, card->config_regs + WRITE_FLASH);
735 iowrite32(0, card->config_regs + FPGA_MODE);
736 iowrite32(0, card->config_regs + FLASH_MODE);
737 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
738 return 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000739}
740
David Woodhouse9c540042008-12-23 04:09:02 +0000741static irqreturn_t solos_irq(int irq, void *dev_id)
742{
743 struct solos_card *card = dev_id;
744 int handled = 1;
745
David Woodhouse9c540042008-12-23 04:09:02 +0000746 iowrite32(0, card->config_regs + IRQ_CLEAR);
David Woodhouse9c540042008-12-23 04:09:02 +0000747
David Woodhouse35c22212009-01-29 14:08:27 +1100748 /* If we're up and running, just kick the tasklet to process TX/RX */
David Woodhousefa755b92009-01-27 14:16:12 +1100749 if (card->atmdev[0])
David Woodhouse9c540042008-12-23 04:09:02 +0000750 tasklet_schedule(&card->tlet);
David Woodhousefa755b92009-01-27 14:16:12 +1100751 else
752 wake_up(&card->fw_wq);
David Woodhouse9c540042008-12-23 04:09:02 +0000753
David Woodhouse9c540042008-12-23 04:09:02 +0000754 return IRQ_RETVAL(handled);
755}
756
Daeseok Youn5f4d4e32014-02-19 10:49:12 +0900757static void solos_bh(unsigned long card_arg)
David Woodhouse9c540042008-12-23 04:09:02 +0000758{
759 struct solos_card *card = (void *)card_arg;
David Woodhouse9c540042008-12-23 04:09:02 +0000760 uint32_t card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000761 uint32_t rx_done = 0;
David Woodhouse35c22212009-01-29 14:08:27 +1100762 int port;
David Woodhouse9c540042008-12-23 04:09:02 +0000763
David Woodhouse35c22212009-01-29 14:08:27 +1100764 /*
765 * Since fpga_tx() is going to need to read the flags under its lock,
766 * it can return them to us so that we don't have to hit PCI MMIO
767 * again for the same information
768 */
769 card_flags = fpga_tx(card);
David Woodhouse9c540042008-12-23 04:09:02 +0000770
771 for (port = 0; port < card->nr_ports; port++) {
772 if (card_flags & (0x10 << port)) {
David Woodhouse90937232009-01-28 16:46:56 +1100773 struct pkt_hdr _hdr, *header;
David Woodhouse9c540042008-12-23 04:09:02 +0000774 struct sk_buff *skb;
775 struct atm_vcc *vcc;
776 int size;
777
David Woodhouse90937232009-01-28 16:46:56 +1100778 if (card->using_dma) {
779 skb = card->rx_skb[port];
David Woodhouseeaf83e32009-01-29 10:51:11 +1100780 card->rx_skb[port] = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000781
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -0500782 dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
783 RX_DMA_SIZE, DMA_FROM_DEVICE);
David Woodhouse9c540042008-12-23 04:09:02 +0000784
David Woodhouse90937232009-01-28 16:46:56 +1100785 header = (void *)skb->data;
786 size = le16_to_cpu(header->size);
787 skb_put(skb, size + sizeof(*header));
788 skb_pull(skb, sizeof(*header));
789 } else {
790 header = &_hdr;
David Woodhouse9c540042008-12-23 04:09:02 +0000791
David Woodhouse90937232009-01-28 16:46:56 +1100792 rx_done |= 0x10 << port;
David Woodhouse9c540042008-12-23 04:09:02 +0000793
David Woodhouse90937232009-01-28 16:46:56 +1100794 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
795
796 size = le16_to_cpu(header->size);
Nathan Williams78f857f2009-03-25 20:33:42 +1100797 if (size > (card->buffer_size - sizeof(*header))){
798 dev_warn(&card->dev->dev, "Invalid buffer size\n");
799 continue;
800 }
David Woodhouse90937232009-01-28 16:46:56 +1100801
David Woodhousece816eb2015-09-16 12:35:00 +0100802 /* Use netdev_alloc_skb() because it adds NET_SKB_PAD of
803 * headroom, and ensures we can route packets back out an
804 * Ethernet interface (for example) without having to
805 * reallocate. Adding NET_IP_ALIGN also ensures that both
806 * PPPoATM and PPPoEoBR2684 packets end up aligned. */
807 skb = netdev_alloc_skb_ip_align(NULL, size + 1);
David Woodhouse90937232009-01-28 16:46:56 +1100808 if (!skb) {
809 if (net_ratelimit())
810 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
811 continue;
812 }
813
814 memcpy_fromio(skb_put(skb, size),
815 RX_BUF(card, port) + sizeof(*header),
816 size);
David Woodhouse9c540042008-12-23 04:09:02 +0000817 }
David Woodhouse9c540042008-12-23 04:09:02 +0000818 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +0000819 dev_info(&card->dev->dev, "Received: port %d\n", port);
David Woodhouse9c540042008-12-23 04:09:02 +0000820 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
David Woodhouse90937232009-01-28 16:46:56 +1100821 size, le16_to_cpu(header->vpi),
822 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000823 print_buffer(skb);
824 }
825
David Woodhouse90937232009-01-28 16:46:56 +1100826 switch (le16_to_cpu(header->type)) {
David Woodhouse9c540042008-12-23 04:09:02 +0000827 case PKT_DATA:
David Woodhouse90937232009-01-28 16:46:56 +1100828 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
829 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000830 if (!vcc) {
831 if (net_ratelimit())
Philip A. Prindeville1e19e652011-03-30 13:22:45 +0000832 dev_warn(&card->dev->dev, "Received packet for unknown VPI.VCI %d.%d on port %d\n",
833 le16_to_cpu(header->vpi), le16_to_cpu(header->vci),
David Woodhouse9c540042008-12-23 04:09:02 +0000834 port);
Nathan Williams007ef522012-11-27 17:34:09 +1100835 dev_kfree_skb_any(skb);
836 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000837 }
838 atm_charge(vcc, skb->truesize);
839 vcc->push(vcc, skb);
840 atomic_inc(&vcc->stats->rx);
841 break;
842
David Woodhouse87ebb182009-01-27 20:02:30 +1100843 case PKT_STATUS:
David Woodhouse95852f42009-01-30 14:23:52 +1100844 if (process_status(card, port, skb) &&
845 net_ratelimit()) {
846 dev_warn(&card->dev->dev, "Bad status packet of %d bytes on port %d:\n", skb->len, port);
847 print_buffer(skb);
848 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100849 dev_kfree_skb_any(skb);
David Woodhouse87ebb182009-01-27 20:02:30 +1100850 break;
851
David Woodhouse9c540042008-12-23 04:09:02 +0000852 case PKT_COMMAND:
853 default: /* FIXME: Not really, surely? */
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100854 if (process_command(card, port, skb))
855 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000856 spin_lock(&card->cli_queue_lock);
857 if (skb_queue_len(&card->cli_queue[port]) > 10) {
858 if (net_ratelimit())
859 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
860 port);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100861 dev_kfree_skb_any(skb);
David Woodhouse9c540042008-12-23 04:09:02 +0000862 } else
863 skb_queue_tail(&card->cli_queue[port], skb);
864 spin_unlock(&card->cli_queue_lock);
865 break;
866 }
867 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100868 /* Allocate RX skbs for any ports which need them */
869 if (card->using_dma && card->atmdev[port] &&
870 !card->rx_skb[port]) {
David Woodhousece816eb2015-09-16 12:35:00 +0100871 /* Unlike the MMIO case (qv) we can't add NET_IP_ALIGN
872 * here; the FPGA can only DMA to addresses which are
873 * aligned to 4 bytes. */
874 struct sk_buff *skb = dev_alloc_skb(RX_DMA_SIZE);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100875 if (skb) {
876 SKB_CB(skb)->dma_addr =
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -0500877 dma_map_single(&card->dev->dev, skb->data,
878 RX_DMA_SIZE, DMA_FROM_DEVICE);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100879 iowrite32(SKB_CB(skb)->dma_addr,
880 card->config_regs + RX_DMA_ADDR(port));
881 card->rx_skb[port] = skb;
882 } else {
883 if (net_ratelimit())
884 dev_warn(&card->dev->dev, "Failed to allocate RX skb");
885
886 /* We'll have to try again later */
887 tasklet_schedule(&card->tlet);
888 }
889 }
David Woodhouse9c540042008-12-23 04:09:02 +0000890 }
891 if (rx_done)
892 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
893
894 return;
895}
896
897static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
898{
899 struct hlist_head *head;
900 struct atm_vcc *vcc = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000901 struct sock *s;
902
903 read_lock(&vcc_sklist_lock);
904 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800905 sk_for_each(s, head) {
David Woodhouse9c540042008-12-23 04:09:02 +0000906 vcc = atm_sk(s);
907 if (vcc->dev == dev && vcc->vci == vci &&
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700908 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
909 test_bit(ATM_VF_READY, &vcc->flags))
David Woodhouse9c540042008-12-23 04:09:02 +0000910 goto out;
911 }
912 vcc = NULL;
913 out:
914 read_unlock(&vcc_sklist_lock);
915 return vcc;
916}
917
David Woodhouse9c540042008-12-23 04:09:02 +0000918static int popen(struct atm_vcc *vcc)
919{
920 struct solos_card *card = vcc->dev->dev_data;
921 struct sk_buff *skb;
922 struct pkt_hdr *header;
923
David Woodhouseb28a4b92009-01-27 21:50:36 +1100924 if (vcc->qos.aal != ATM_AAL5) {
925 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
926 vcc->qos.aal);
927 return -EINVAL;
928 }
929
David Woodhousea1db5c52012-11-29 23:27:20 +0000930 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
Jesper Juhlda1ab3e2011-02-13 10:49:32 +0000931 if (!skb) {
932 if (net_ratelimit())
933 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
David Woodhouse9c540042008-12-23 04:09:02 +0000934 return -ENOMEM;
935 }
Johannes Berg4df864c2017-06-16 14:29:21 +0200936 header = skb_put(skb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +0000937
David Woodhouseb76811a2009-01-27 10:18:51 +1100938 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000939 header->vpi = cpu_to_le16(vcc->vpi);
940 header->vci = cpu_to_le16(vcc->vci);
941 header->type = cpu_to_le16(PKT_POPEN);
942
943 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
944
David Woodhousebdc54622009-01-29 14:37:20 +1100945 set_bit(ATM_VF_ADDR, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000946 set_bit(ATM_VF_READY, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000947
948 return 0;
949}
950
951static void pclose(struct atm_vcc *vcc)
952{
953 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000954 unsigned char port = SOLOS_CHAN(vcc->dev);
David Woodhouse213e85d2012-11-29 23:15:30 +0000955 struct sk_buff *skb, *tmpskb;
David Woodhouse9c540042008-12-23 04:09:02 +0000956 struct pkt_hdr *header;
957
David Woodhouse213e85d2012-11-29 23:15:30 +0000958 /* Remove any yet-to-be-transmitted packets from the pending queue */
959 spin_lock(&card->tx_queue_lock);
960 skb_queue_walk_safe(&card->tx_queue[port], skb, tmpskb) {
961 if (SKB_CB(skb)->vcc == vcc) {
962 skb_unlink(skb, &card->tx_queue[port]);
963 solos_pop(vcc, skb);
964 }
965 }
966 spin_unlock(&card->tx_queue_lock);
967
David Woodhousea1db5c52012-11-29 23:27:20 +0000968 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
David Woodhouse9c540042008-12-23 04:09:02 +0000969 if (!skb) {
970 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
971 return;
972 }
Johannes Berg4df864c2017-06-16 14:29:21 +0200973 header = skb_put(skb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +0000974
David Woodhouseb76811a2009-01-27 10:18:51 +1100975 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000976 header->vpi = cpu_to_le16(vcc->vpi);
977 header->vci = cpu_to_le16(vcc->vci);
978 header->type = cpu_to_le16(PKT_PCLOSE);
979
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000980 skb_get(skb);
981 fpga_queue(card, port, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +0000982
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000983 if (!wait_event_timeout(card->param_wq, !skb_shared(skb), 5 * HZ))
984 dev_warn(&card->dev->dev,
985 "Timeout waiting for VCC close on port %d\n", port);
986
987 dev_kfree_skb(skb);
988
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700989 /* Hold up vcc_destroy_socket() (our caller) until solos_bh() in the
990 tasklet has finished processing any incoming packets (and, more to
991 the point, using the vcc pointer). */
992 tasklet_unlock_wait(&card->tlet);
David Woodhouse213e85d2012-11-29 23:15:30 +0000993
994 clear_bit(ATM_VF_ADDR, &vcc->flags);
995
David Woodhouse9c540042008-12-23 04:09:02 +0000996 return;
997}
998
999static int print_buffer(struct sk_buff *buf)
1000{
1001 int len,i;
1002 char msg[500];
1003 char item[10];
1004
1005 len = buf->len;
1006 for (i = 0; i < len; i++){
1007 if(i % 8 == 0)
1008 sprintf(msg, "%02X: ", i);
1009
1010 sprintf(item,"%02X ",*(buf->data + i));
1011 strcat(msg, item);
1012 if(i % 8 == 7) {
1013 sprintf(item, "\n");
1014 strcat(msg, item);
1015 printk(KERN_DEBUG "%s", msg);
1016 }
1017 }
1018 if (i % 8 != 0) {
1019 sprintf(item, "\n");
1020 strcat(msg, item);
1021 printk(KERN_DEBUG "%s", msg);
1022 }
1023 printk(KERN_DEBUG "\n");
1024
1025 return 0;
1026}
1027
1028static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
1029 struct atm_vcc *vcc)
1030{
1031 int old_len;
David Woodhousef69e4172009-01-29 11:10:58 +11001032 unsigned long flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001033
David Woodhouse90937232009-01-28 16:46:56 +11001034 SKB_CB(skb)->vcc = vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001035
David Woodhousef69e4172009-01-29 11:10:58 +11001036 spin_lock_irqsave(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +00001037 old_len = skb_queue_len(&card->tx_queue[port]);
1038 skb_queue_tail(&card->tx_queue[port], skb);
David Woodhouse35c22212009-01-29 14:08:27 +11001039 if (!old_len)
David Woodhousef69e4172009-01-29 11:10:58 +11001040 card->tx_mask |= (1 << port);
David Woodhousef69e4172009-01-29 11:10:58 +11001041 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +00001042
David Woodhousef69e4172009-01-29 11:10:58 +11001043 /* Theoretically we could just schedule the tasklet here, but
1044 that introduces latency we don't want -- it's noticeable */
David Woodhouse9c540042008-12-23 04:09:02 +00001045 if (!old_len)
1046 fpga_tx(card);
1047}
1048
David Woodhouse35c22212009-01-29 14:08:27 +11001049static uint32_t fpga_tx(struct solos_card *card)
David Woodhouse9c540042008-12-23 04:09:02 +00001050{
David Woodhouse35c22212009-01-29 14:08:27 +11001051 uint32_t tx_pending, card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001052 uint32_t tx_started = 0;
1053 struct sk_buff *skb;
1054 struct atm_vcc *vcc;
1055 unsigned char port;
1056 unsigned long flags;
1057
1058 spin_lock_irqsave(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001059
1060 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
1061 /*
1062 * The queue lock is required for _writing_ to tx_mask, but we're
1063 * OK to read it here without locking. The only potential update
1064 * that we could race with is in fpga_queue() where it sets a bit
1065 * for a new port... but it's going to call this function again if
1066 * it's doing that, anyway.
1067 */
1068 tx_pending = card->tx_mask & ~card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001069
David Woodhouse35c22212009-01-29 14:08:27 +11001070 for (port = 0; tx_pending; tx_pending >>= 1, port++) {
1071 if (tx_pending & 1) {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001072 struct sk_buff *oldskb = card->tx_skb[port];
David Woodhousecae49ed2012-12-11 14:57:14 +00001073 if (oldskb) {
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001074 dma_unmap_single(&card->dev->dev, SKB_CB(oldskb)->dma_addr,
1075 oldskb->len, DMA_TO_DEVICE);
David Woodhousecae49ed2012-12-11 14:57:14 +00001076 card->tx_skb[port] = NULL;
1077 }
David Woodhouse9c540042008-12-23 04:09:02 +00001078 spin_lock(&card->tx_queue_lock);
1079 skb = skb_dequeue(&card->tx_queue[port]);
David Woodhousef69e4172009-01-29 11:10:58 +11001080 if (!skb)
1081 card->tx_mask &= ~(1 << port);
David Woodhouse9c540042008-12-23 04:09:02 +00001082 spin_unlock(&card->tx_queue_lock);
1083
David Woodhouseeaf83e32009-01-29 10:51:11 +11001084 if (skb && !card->using_dma) {
1085 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
David Woodhousebdc54622009-01-29 14:37:20 +11001086 tx_started |= 1 << port;
David Woodhouseeaf83e32009-01-29 10:51:11 +11001087 oldskb = skb; /* We're done with this skb already */
1088 } else if (skb && card->using_dma) {
David Woodhouse152a2a82012-12-19 11:01:21 +00001089 unsigned char *data = skb->data;
1090 if ((unsigned long)data & card->dma_alignment) {
1091 data = card->dma_bounce + (BUF_SIZE * port);
1092 memcpy(data, skb->data, skb->len);
1093 }
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001094 SKB_CB(skb)->dma_addr = dma_map_single(&card->dev->dev, data,
1095 skb->len, DMA_TO_DEVICE);
David Woodhouseb4bd8ad2012-05-24 04:58:27 +00001096 card->tx_skb[port] = skb;
David Woodhouseeaf83e32009-01-29 10:51:11 +11001097 iowrite32(SKB_CB(skb)->dma_addr,
1098 card->config_regs + TX_DMA_ADDR(port));
1099 }
1100
1101 if (!oldskb)
David Woodhouse9c540042008-12-23 04:09:02 +00001102 continue;
1103
David Woodhouseeaf83e32009-01-29 10:51:11 +11001104 /* Clean up and free oldskb now it's gone */
David Woodhouse9c540042008-12-23 04:09:02 +00001105 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001106 struct pkt_hdr *header = (void *)oldskb->data;
1107 int size = le16_to_cpu(header->size);
1108
1109 skb_pull(oldskb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +00001110 dev_info(&card->dev->dev, "Transmitted: port %d\n",
1111 port);
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001112 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
1113 size, le16_to_cpu(header->vpi),
1114 le16_to_cpu(header->vci));
David Woodhouseeaf83e32009-01-29 10:51:11 +11001115 print_buffer(oldskb);
David Woodhouse9c540042008-12-23 04:09:02 +00001116 }
David Woodhouse9c540042008-12-23 04:09:02 +00001117
David Woodhouseeaf83e32009-01-29 10:51:11 +11001118 vcc = SKB_CB(oldskb)->vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001119
1120 if (vcc) {
1121 atomic_inc(&vcc->stats->tx);
David Woodhouseeaf83e32009-01-29 10:51:11 +11001122 solos_pop(vcc, oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001123 } else {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001124 dev_kfree_skb_irq(oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001125 wake_up(&card->param_wq);
1126 }
David Woodhouse9c540042008-12-23 04:09:02 +00001127 }
1128 }
David Woodhousebdc54622009-01-29 14:37:20 +11001129 /* For non-DMA TX, write the 'TX start' bit for all four ports simultaneously */
David Woodhouse9c540042008-12-23 04:09:02 +00001130 if (tx_started)
1131 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
1132
1133 spin_unlock_irqrestore(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001134 return card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001135}
1136
1137static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
1138{
1139 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse9c540042008-12-23 04:09:02 +00001140 struct pkt_hdr *header;
David Woodhouseb76811a2009-01-27 10:18:51 +11001141 int pktlen;
David Woodhouse9c540042008-12-23 04:09:02 +00001142
David Woodhouseb76811a2009-01-27 10:18:51 +11001143 pktlen = skb->len;
1144 if (pktlen > (BUF_SIZE - sizeof(*header))) {
David Woodhouse9c540042008-12-23 04:09:02 +00001145 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
1146 solos_pop(vcc, skb);
1147 return 0;
1148 }
1149
1150 if (!skb_clone_writable(skb, sizeof(*header))) {
1151 int expand_by = 0;
1152 int ret;
1153
1154 if (skb_headroom(skb) < sizeof(*header))
1155 expand_by = sizeof(*header) - skb_headroom(skb);
1156
1157 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
1158 if (ret) {
Simon Farnsworth4306cad2009-01-19 21:19:29 +00001159 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
David Woodhouse9c540042008-12-23 04:09:02 +00001160 solos_pop(vcc, skb);
1161 return ret;
1162 }
1163 }
1164
Johannes Bergd58ff352017-06-16 14:29:23 +02001165 header = skb_push(skb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +00001166
David Woodhouseb76811a2009-01-27 10:18:51 +11001167 /* This does _not_ include the size of the header */
1168 header->size = cpu_to_le16(pktlen);
David Woodhouse9c540042008-12-23 04:09:02 +00001169 header->vpi = cpu_to_le16(vcc->vpi);
1170 header->vci = cpu_to_le16(vcc->vci);
1171 header->type = cpu_to_le16(PKT_DATA);
1172
1173 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1174
1175 return 0;
1176}
1177
Bhumika Goyal46c4b7a2017-08-09 14:49:15 +05301178static const struct atmdev_ops fpga_ops = {
David Woodhouse9c540042008-12-23 04:09:02 +00001179 .open = popen,
1180 .close = pclose,
1181 .ioctl = NULL,
David Woodhouse9c540042008-12-23 04:09:02 +00001182 .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
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001213 err = dma_set_mask_and_coherent(&dev->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");
Julia Lawall73112f92014-11-22 15:39:17 +01001228 err = -ENOMEM;
David Woodhouse9c540042008-12-23 04:09:02 +00001229 goto out_release_regions;
1230 }
1231 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1232 if (!card->buffers) {
1233 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
Julia Lawall73112f92014-11-22 15:39:17 +01001234 err = -ENOMEM;
David Woodhouse9c540042008-12-23 04:09:02 +00001235 goto out_unmap_config;
1236 }
1237
David Woodhousecc3657e2009-01-30 14:23:22 +11001238 if (reset) {
1239 iowrite32(1, card->config_regs + FPGA_MODE);
Gustavo A. R. Silva1492a3a2017-06-15 14:56:21 -05001240 ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001241
David Woodhousecc3657e2009-01-30 14:23:22 +11001242 iowrite32(0, card->config_regs + FPGA_MODE);
Gustavo A. R. Silva1492a3a2017-06-15 14:56:21 -05001243 ioread32(card->config_regs + FPGA_MODE);
David Woodhousecc3657e2009-01-30 14:23:22 +11001244 }
David Woodhouse9c540042008-12-23 04:09:02 +00001245
1246 data32 = ioread32(card->config_regs + FPGA_VER);
1247 fpga_ver = (data32 & 0x0000FFFF);
1248 major_ver = ((data32 & 0xFF000000) >> 24);
1249 minor_ver = ((data32 & 0x00FF0000) >> 16);
Nathan Williams4dbedf42009-03-25 20:27:37 +11001250 card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
1251 if (card->fpga_version > LEGACY_BUFFERS)
1252 card->buffer_size = BUF_SIZE;
1253 else
1254 card->buffer_size = OLD_BUF_SIZE;
David Woodhouse9c540042008-12-23 04:09:02 +00001255 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1256 major_ver, minor_ver, fpga_ver);
1257
David Woodhouse3ce12272010-11-01 10:35:28 +00001258 if (fpga_ver < 37 && (fpga_upgrade || firmware_upgrade ||
1259 db_fpga_upgrade || db_firmware_upgrade)) {
1260 dev_warn(&dev->dev,
1261 "FPGA too old; cannot upgrade flash. Use JTAG.\n");
1262 fpga_upgrade = firmware_upgrade = 0;
1263 db_fpga_upgrade = db_firmware_upgrade = 0;
1264 }
1265
Nathan Williams13af8162012-12-19 11:01:20 +00001266 /* Stopped using Atmel flash after 0.03-38 */
1267 if (fpga_ver < 39)
1268 card->atmel_flash = 1;
1269 else
1270 card->atmel_flash = 0;
1271
David Woodhouse152a2a82012-12-19 11:01:21 +00001272 data32 = ioread32(card->config_regs + PORTS);
1273 card->nr_ports = (data32 & 0x000000FF);
1274
David Woodhouseb4bd8ad2012-05-24 04:58:27 +00001275 if (card->fpga_version >= DMA_SUPPORTED) {
1276 pci_set_master(dev);
David Woodhouse90937232009-01-28 16:46:56 +11001277 card->using_dma = 1;
David Woodhouse152a2a82012-12-19 11:01:21 +00001278 if (1) { /* All known FPGA versions so far */
1279 card->dma_alignment = 3;
Kees Cook6da2ec52018-06-12 13:55:00 -07001280 card->dma_bounce = kmalloc_array(card->nr_ports,
1281 BUF_SIZE, GFP_KERNEL);
David Woodhouse152a2a82012-12-19 11:01:21 +00001282 if (!card->dma_bounce) {
1283 dev_warn(&card->dev->dev, "Failed to allocate DMA bounce buffers\n");
Julia Lawallc7bcae42014-08-07 14:49:07 +02001284 err = -ENOMEM;
David Woodhouse152a2a82012-12-19 11:01:21 +00001285 /* Fallback to MMIO doesn't work */
1286 goto out_unmap_both;
1287 }
1288 }
Nathan Williams4dbedf42009-03-25 20:27:37 +11001289 } else {
1290 card->using_dma = 0;
David Woodhouseeab50f72009-01-30 14:27:26 +11001291 /* Set RX empty flag for all ports */
1292 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1293 }
David Woodhouse9c540042008-12-23 04:09:02 +00001294
David Woodhouse9c540042008-12-23 04:09:02 +00001295 pci_set_drvdata(dev, card);
David Woodhousefa755b92009-01-27 14:16:12 +11001296
David Woodhouse9c540042008-12-23 04:09:02 +00001297 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1298 spin_lock_init(&card->tx_lock);
1299 spin_lock_init(&card->tx_queue_lock);
1300 spin_lock_init(&card->cli_queue_lock);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001301 spin_lock_init(&card->param_queue_lock);
1302 INIT_LIST_HEAD(&card->param_queue);
David Woodhouse9c540042008-12-23 04:09:02 +00001303
David Woodhousefcd82662009-01-29 11:29:12 +11001304 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
David Woodhouse9c540042008-12-23 04:09:02 +00001305 "solos-pci", card);
David Woodhousefa755b92009-01-27 14:16:12 +11001306 if (err) {
David Woodhouse9c540042008-12-23 04:09:02 +00001307 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
David Woodhousefa755b92009-01-27 14:16:12 +11001308 goto out_unmap_both;
1309 }
David Woodhouse9c540042008-12-23 04:09:02 +00001310
David Woodhouse9c540042008-12-23 04:09:02 +00001311 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1312
David Woodhousefa755b92009-01-27 14:16:12 +11001313 if (fpga_upgrade)
1314 flash_upgrade(card, 0);
1315
1316 if (firmware_upgrade)
1317 flash_upgrade(card, 1);
1318
Nathan Williams4dbedf42009-03-25 20:27:37 +11001319 if (db_fpga_upgrade)
1320 flash_upgrade(card, 2);
1321
1322 if (db_firmware_upgrade)
1323 flash_upgrade(card, 3);
1324
Dan Williamsd9ca6762010-12-08 19:40:47 +00001325 err = atm_init(card, &dev->dev);
David Woodhousefa755b92009-01-27 14:16:12 +11001326 if (err)
1327 goto out_free_irq;
1328
Nathan Williamsf9baad02012-12-19 11:01:18 +00001329 if (card->fpga_version >= DMA_SUPPORTED &&
1330 sysfs_create_group(&card->dev->dev.kobj, &gpio_attr_group))
1331 dev_err(&card->dev->dev, "Could not register parameter group for GPIOs\n");
1332
David Woodhouse9c540042008-12-23 04:09:02 +00001333 return 0;
1334
David Woodhousefa755b92009-01-27 14:16:12 +11001335 out_free_irq:
1336 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1337 free_irq(dev->irq, card);
1338 tasklet_kill(&card->tlet);
1339
David Woodhouse9c540042008-12-23 04:09:02 +00001340 out_unmap_both:
David Woodhouse152a2a82012-12-19 11:01:21 +00001341 kfree(card->dma_bounce);
David Woodhouse9c540042008-12-23 04:09:02 +00001342 pci_iounmap(dev, card->buffers);
Julia Lawall8ae0cfe2012-02-19 09:43:32 +00001343 out_unmap_config:
1344 pci_iounmap(dev, card->config_regs);
David Woodhouse9c540042008-12-23 04:09:02 +00001345 out_release_regions:
1346 pci_release_regions(dev);
1347 out:
Julia Lawallbc111d52009-02-08 17:00:02 -08001348 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001349 return err;
1350}
1351
Dan Williamsd9ca6762010-12-08 19:40:47 +00001352static int atm_init(struct solos_card *card, struct device *parent)
David Woodhouse9c540042008-12-23 04:09:02 +00001353{
1354 int i;
1355
David Woodhouse9c540042008-12-23 04:09:02 +00001356 for (i = 0; i < card->nr_ports; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +11001357 struct sk_buff *skb;
1358 struct pkt_hdr *header;
1359
David Woodhouse9c540042008-12-23 04:09:02 +00001360 skb_queue_head_init(&card->tx_queue[i]);
1361 skb_queue_head_init(&card->cli_queue[i]);
1362
Dan Williamsd9ca6762010-12-08 19:40:47 +00001363 card->atmdev[i] = atm_dev_register("solos-pci", parent, &fpga_ops, -1, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001364 if (!card->atmdev[i]) {
1365 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1366 atm_remove(card);
1367 return -ENODEV;
1368 }
1369 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1370 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
David Woodhoused057f0a2009-01-27 16:52:07 +11001371 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1372 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
David Woodhouse9c540042008-12-23 04:09:02 +00001373
1374 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1375
1376 card->atmdev[i]->ci_range.vpi_bits = 8;
1377 card->atmdev[i]->ci_range.vci_bits = 16;
1378 card->atmdev[i]->dev_data = card;
1379 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
Philip A. Prindevillec0312352011-03-30 13:17:04 +00001380 atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +11001381
David Woodhousea1db5c52012-11-29 23:27:20 +00001382 skb = alloc_skb(sizeof(*header), GFP_KERNEL);
David Woodhouse87ebb182009-01-27 20:02:30 +11001383 if (!skb) {
1384 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1385 continue;
1386 }
1387
Johannes Berg4df864c2017-06-16 14:29:21 +02001388 header = skb_put(skb, sizeof(*header));
David Woodhouse87ebb182009-01-27 20:02:30 +11001389
1390 header->size = cpu_to_le16(0);
1391 header->vpi = cpu_to_le16(0);
1392 header->vci = cpu_to_le16(0);
1393 header->type = cpu_to_le16(PKT_STATUS);
1394
1395 fpga_queue(card, i, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001396 }
1397 return 0;
1398}
1399
1400static void atm_remove(struct solos_card *card)
1401{
1402 int i;
1403
1404 for (i = 0; i < card->nr_ports; i++) {
1405 if (card->atmdev[i]) {
David Woodhouse97d759d2009-03-17 14:59:34 +00001406 struct sk_buff *skb;
1407
David Woodhouse9c540042008-12-23 04:09:02 +00001408 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
David Woodhousec0fe3022009-01-28 14:34:34 +11001409
1410 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
David Woodhouse9c540042008-12-23 04:09:02 +00001411 atm_dev_deregister(card->atmdev[i]);
David Woodhouse97d759d2009-03-17 14:59:34 +00001412
1413 skb = card->rx_skb[i];
1414 if (skb) {
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001415 dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
1416 RX_DMA_SIZE, DMA_FROM_DEVICE);
David Woodhouse97d759d2009-03-17 14:59:34 +00001417 dev_kfree_skb(skb);
1418 }
1419 skb = card->tx_skb[i];
1420 if (skb) {
chas williams - CONTRACTORede58ef2015-01-16 08:57:21 -05001421 dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
1422 skb->len, DMA_TO_DEVICE);
David Woodhouse97d759d2009-03-17 14:59:34 +00001423 dev_kfree_skb(skb);
1424 }
1425 while ((skb = skb_dequeue(&card->tx_queue[i])))
1426 dev_kfree_skb(skb);
1427
David Woodhouse9c540042008-12-23 04:09:02 +00001428 }
1429 }
1430}
1431
1432static void fpga_remove(struct pci_dev *dev)
1433{
1434 struct solos_card *card = pci_get_drvdata(dev);
David Woodhouse97d759d2009-03-17 14:59:34 +00001435
1436 /* Disable IRQs */
1437 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
David Woodhouse9c540042008-12-23 04:09:02 +00001438
David Woodhouse97d759d2009-03-17 14:59:34 +00001439 /* Reset FPGA */
1440 iowrite32(1, card->config_regs + FPGA_MODE);
1441 (void)ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001442
Nathan Williamsf9baad02012-12-19 11:01:18 +00001443 if (card->fpga_version >= DMA_SUPPORTED)
1444 sysfs_remove_group(&card->dev->dev.kobj, &gpio_attr_group);
1445
David Woodhouse9c540042008-12-23 04:09:02 +00001446 atm_remove(card);
1447
David Woodhouse9c540042008-12-23 04:09:02 +00001448 free_irq(dev->irq, card);
1449 tasklet_kill(&card->tlet);
1450
David Woodhouse152a2a82012-12-19 11:01:21 +00001451 kfree(card->dma_bounce);
1452
David Woodhouse97d759d2009-03-17 14:59:34 +00001453 /* Release device from reset */
1454 iowrite32(0, card->config_regs + FPGA_MODE);
1455 (void)ioread32(card->config_regs + FPGA_MODE);
1456
David Woodhouse9c540042008-12-23 04:09:02 +00001457 pci_iounmap(dev, card->buffers);
1458 pci_iounmap(dev, card->config_regs);
1459
David Woodhouse9c540042008-12-23 04:09:02 +00001460 pci_release_regions(dev);
1461 pci_disable_device(dev);
1462
David Woodhouse9c540042008-12-23 04:09:02 +00001463 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001464}
1465
Arvind Yadav6d6148b32017-07-16 15:02:35 +05301466static const struct pci_device_id fpga_pci_tbl[] = {
David Woodhouse9c540042008-12-23 04:09:02 +00001467 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1468 { 0, }
1469};
1470
1471MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1472
1473static struct pci_driver fpga_driver = {
1474 .name = "solos",
1475 .id_table = fpga_pci_tbl,
1476 .probe = fpga_probe,
1477 .remove = fpga_remove,
1478};
1479
1480
1481static int __init solos_pci_init(void)
1482{
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001483 BUILD_BUG_ON(sizeof(struct solos_skb_cb) > sizeof(((struct sk_buff *)0)->cb));
1484
David Woodhouse9c540042008-12-23 04:09:02 +00001485 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1486 return pci_register_driver(&fpga_driver);
1487}
1488
1489static void __exit solos_pci_exit(void)
1490{
1491 pci_unregister_driver(&fpga_driver);
1492 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1493}
1494
1495module_init(solos_pci_init);
1496module_exit(solos_pci_exit);