octeontx2-af: CGX Rx/Tx enable/disable mbox handlers
[linux-2.6-microblaze.git] / drivers / net / ethernet / marvell / octeontx2 / af / cgx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 CGX driver
3  *
4  * Copyright (C) 2018 Marvell International Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/acpi.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/pci.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/phy.h>
18 #include <linux/of.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_net.h>
21
22 #include "cgx.h"
23
24 #define DRV_NAME        "octeontx2-cgx"
25 #define DRV_STRING      "Marvell OcteonTX2 CGX/MAC Driver"
26
27 /**
28  * struct lmac
29  * @wq_cmd_cmplt:       waitq to keep the process blocked until cmd completion
30  * @cmd_lock:           Lock to serialize the command interface
31  * @resp:               command response
32  * @event_cb:           callback for linkchange events
33  * @cmd_pend:           flag set before new command is started
34  *                      flag cleared after command response is received
35  * @cgx:                parent cgx port
36  * @lmac_id:            lmac port id
37  * @name:               lmac port name
38  */
39 struct lmac {
40         wait_queue_head_t wq_cmd_cmplt;
41         struct mutex cmd_lock;
42         u64 resp;
43         struct cgx_event_cb event_cb;
44         bool cmd_pend;
45         struct cgx *cgx;
46         u8 lmac_id;
47         char *name;
48 };
49
50 struct cgx {
51         void __iomem            *reg_base;
52         struct pci_dev          *pdev;
53         u8                      cgx_id;
54         u8                      lmac_count;
55         struct lmac             *lmac_idmap[MAX_LMAC_PER_CGX];
56         struct list_head        cgx_list;
57 };
58
59 static LIST_HEAD(cgx_list);
60
61 /* Supported devices */
62 static const struct pci_device_id cgx_id_table[] = {
63         { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_CGX) },
64         { 0, }  /* end of table */
65 };
66
67 MODULE_DEVICE_TABLE(pci, cgx_id_table);
68
69 static void cgx_write(struct cgx *cgx, u64 lmac, u64 offset, u64 val)
70 {
71         writeq(val, cgx->reg_base + (lmac << 18) + offset);
72 }
73
74 static u64 cgx_read(struct cgx *cgx, u64 lmac, u64 offset)
75 {
76         return readq(cgx->reg_base + (lmac << 18) + offset);
77 }
78
79 static inline struct lmac *lmac_pdata(u8 lmac_id, struct cgx *cgx)
80 {
81         if (!cgx || lmac_id >= MAX_LMAC_PER_CGX)
82                 return NULL;
83
84         return cgx->lmac_idmap[lmac_id];
85 }
86
87 int cgx_get_cgx_cnt(void)
88 {
89         struct cgx *cgx_dev;
90         int count = 0;
91
92         list_for_each_entry(cgx_dev, &cgx_list, cgx_list)
93                 count++;
94
95         return count;
96 }
97 EXPORT_SYMBOL(cgx_get_cgx_cnt);
98
99 int cgx_get_lmac_cnt(void *cgxd)
100 {
101         struct cgx *cgx = cgxd;
102
103         if (!cgx)
104                 return -ENODEV;
105
106         return cgx->lmac_count;
107 }
108 EXPORT_SYMBOL(cgx_get_lmac_cnt);
109
110 void *cgx_get_pdata(int cgx_id)
111 {
112         struct cgx *cgx_dev;
113
114         list_for_each_entry(cgx_dev, &cgx_list, cgx_list) {
115                 if (cgx_dev->cgx_id == cgx_id)
116                         return cgx_dev;
117         }
118         return NULL;
119 }
120 EXPORT_SYMBOL(cgx_get_pdata);
121
122 int cgx_lmac_rx_tx_enable(void *cgxd, int lmac_id, bool enable)
123 {
124         struct cgx *cgx = cgxd;
125         u64 cfg;
126
127         if (!cgx || lmac_id >= cgx->lmac_count)
128                 return -ENODEV;
129
130         cfg = cgx_read(cgx, lmac_id, CGXX_CMRX_CFG);
131         if (enable)
132                 cfg |= CMR_EN | DATA_PKT_RX_EN | DATA_PKT_TX_EN;
133         else
134                 cfg &= ~(CMR_EN | DATA_PKT_RX_EN | DATA_PKT_TX_EN);
135         cgx_write(cgx, lmac_id, CGXX_CMRX_CFG, cfg);
136         return 0;
137 }
138 EXPORT_SYMBOL(cgx_lmac_rx_tx_enable);
139
140 /* CGX Firmware interface low level support */
141 static int cgx_fwi_cmd_send(u64 req, u64 *resp, struct lmac *lmac)
142 {
143         struct cgx *cgx = lmac->cgx;
144         struct device *dev;
145         int err = 0;
146         u64 cmd;
147
148         /* Ensure no other command is in progress */
149         err = mutex_lock_interruptible(&lmac->cmd_lock);
150         if (err)
151                 return err;
152
153         /* Ensure command register is free */
154         cmd = cgx_read(cgx, lmac->lmac_id,  CGX_COMMAND_REG);
155         if (FIELD_GET(CMDREG_OWN, cmd) != CGX_CMD_OWN_NS) {
156                 err = -EBUSY;
157                 goto unlock;
158         }
159
160         /* Update ownership in command request */
161         req = FIELD_SET(CMDREG_OWN, CGX_CMD_OWN_FIRMWARE, req);
162
163         /* Mark this lmac as pending, before we start */
164         lmac->cmd_pend = true;
165
166         /* Start command in hardware */
167         cgx_write(cgx, lmac->lmac_id, CGX_COMMAND_REG, req);
168
169         /* Ensure command is completed without errors */
170         if (!wait_event_timeout(lmac->wq_cmd_cmplt, !lmac->cmd_pend,
171                                 msecs_to_jiffies(CGX_CMD_TIMEOUT))) {
172                 dev = &cgx->pdev->dev;
173                 dev_err(dev, "cgx port %d:%d cmd timeout\n",
174                         cgx->cgx_id, lmac->lmac_id);
175                 err = -EIO;
176                 goto unlock;
177         }
178
179         /* we have a valid command response */
180         smp_rmb(); /* Ensure the latest updates are visible */
181         *resp = lmac->resp;
182
183 unlock:
184         mutex_unlock(&lmac->cmd_lock);
185
186         return err;
187 }
188
189 static inline int cgx_fwi_cmd_generic(u64 req, u64 *resp,
190                                       struct cgx *cgx, int lmac_id)
191 {
192         struct lmac *lmac;
193         int err;
194
195         lmac = lmac_pdata(lmac_id, cgx);
196         if (!lmac)
197                 return -ENODEV;
198
199         err = cgx_fwi_cmd_send(req, resp, lmac);
200
201         /* Check for valid response */
202         if (!err) {
203                 if (FIELD_GET(EVTREG_STAT, *resp) == CGX_STAT_FAIL)
204                         return -EIO;
205                 else
206                         return 0;
207         }
208
209         return err;
210 }
211
212 /* Hardware event handlers */
213 static inline void cgx_link_change_handler(u64 lstat,
214                                            struct lmac *lmac)
215 {
216         struct cgx *cgx = lmac->cgx;
217         struct cgx_link_event event;
218         struct device *dev;
219
220         dev = &cgx->pdev->dev;
221
222         event.lstat.link_up = FIELD_GET(RESP_LINKSTAT_UP, lstat);
223         event.lstat.full_duplex = FIELD_GET(RESP_LINKSTAT_FDUPLEX, lstat);
224         event.lstat.speed = FIELD_GET(RESP_LINKSTAT_SPEED, lstat);
225         event.lstat.err_type = FIELD_GET(RESP_LINKSTAT_ERRTYPE, lstat);
226
227         event.cgx_id = cgx->cgx_id;
228         event.lmac_id = lmac->lmac_id;
229
230         if (!lmac->event_cb.notify_link_chg) {
231                 dev_dbg(dev, "cgx port %d:%d Link change handler null",
232                         cgx->cgx_id, lmac->lmac_id);
233                 if (event.lstat.err_type != CGX_ERR_NONE) {
234                         dev_err(dev, "cgx port %d:%d Link error %d\n",
235                                 cgx->cgx_id, lmac->lmac_id,
236                                 event.lstat.err_type);
237                 }
238                 dev_info(dev, "cgx port %d:%d Link status %s, speed %x\n",
239                          cgx->cgx_id, lmac->lmac_id,
240                         event.lstat.link_up ? "UP" : "DOWN",
241                         event.lstat.speed);
242                 return;
243         }
244
245         if (lmac->event_cb.notify_link_chg(&event, lmac->event_cb.data))
246                 dev_err(dev, "event notification failure\n");
247 }
248
249 static inline bool cgx_cmdresp_is_linkevent(u64 event)
250 {
251         u8 id;
252
253         id = FIELD_GET(EVTREG_ID, event);
254         if (id == CGX_CMD_LINK_BRING_UP ||
255             id == CGX_CMD_LINK_BRING_DOWN)
256                 return true;
257         else
258                 return false;
259 }
260
261 static inline bool cgx_event_is_linkevent(u64 event)
262 {
263         if (FIELD_GET(EVTREG_ID, event) == CGX_EVT_LINK_CHANGE)
264                 return true;
265         else
266                 return false;
267 }
268
269 static irqreturn_t cgx_fwi_event_handler(int irq, void *data)
270 {
271         struct lmac *lmac = data;
272         struct cgx *cgx;
273         u64 event;
274
275         cgx = lmac->cgx;
276
277         event = cgx_read(cgx, lmac->lmac_id, CGX_EVENT_REG);
278
279         if (!FIELD_GET(EVTREG_ACK, event))
280                 return IRQ_NONE;
281
282         switch (FIELD_GET(EVTREG_EVT_TYPE, event)) {
283         case CGX_EVT_CMD_RESP:
284                 /* Copy the response. Since only one command is active at a
285                  * time, there is no way a response can get overwritten
286                  */
287                 lmac->resp = event;
288                 /* Ensure response is updated before thread context starts */
289                 smp_wmb();
290
291                 /* There wont be separate events for link change initiated from
292                  * software; Hence report the command responses as events
293                  */
294                 if (cgx_cmdresp_is_linkevent(event))
295                         cgx_link_change_handler(event, lmac);
296
297                 /* Release thread waiting for completion  */
298                 lmac->cmd_pend = false;
299                 wake_up_interruptible(&lmac->wq_cmd_cmplt);
300                 break;
301         case CGX_EVT_ASYNC:
302                 if (cgx_event_is_linkevent(event))
303                         cgx_link_change_handler(event, lmac);
304                 break;
305         }
306
307         /* Any new event or command response will be posted by firmware
308          * only after the current status is acked.
309          * Ack the interrupt register as well.
310          */
311         cgx_write(lmac->cgx, lmac->lmac_id, CGX_EVENT_REG, 0);
312         cgx_write(lmac->cgx, lmac->lmac_id, CGXX_CMRX_INT, FW_CGX_INT);
313
314         return IRQ_HANDLED;
315 }
316
317 /* APIs for PHY management using CGX firmware interface */
318
319 /* callback registration for hardware events like link change */
320 int cgx_lmac_evh_register(struct cgx_event_cb *cb, void *cgxd, int lmac_id)
321 {
322         struct cgx *cgx = cgxd;
323         struct lmac *lmac;
324
325         lmac = lmac_pdata(lmac_id, cgx);
326         if (!lmac)
327                 return -ENODEV;
328
329         lmac->event_cb = *cb;
330
331         return 0;
332 }
333 EXPORT_SYMBOL(cgx_lmac_evh_register);
334
335 static inline int cgx_fwi_read_version(u64 *resp, struct cgx *cgx)
336 {
337         u64 req = 0;
338
339         req = FIELD_SET(CMDREG_ID, CGX_CMD_GET_FW_VER, req);
340         return cgx_fwi_cmd_generic(req, resp, cgx, 0);
341 }
342
343 static int cgx_lmac_verify_fwi_version(struct cgx *cgx)
344 {
345         struct device *dev = &cgx->pdev->dev;
346         int major_ver, minor_ver;
347         u64 resp;
348         int err;
349
350         if (!cgx->lmac_count)
351                 return 0;
352
353         err = cgx_fwi_read_version(&resp, cgx);
354         if (err)
355                 return err;
356
357         major_ver = FIELD_GET(RESP_MAJOR_VER, resp);
358         minor_ver = FIELD_GET(RESP_MINOR_VER, resp);
359         dev_dbg(dev, "Firmware command interface version = %d.%d\n",
360                 major_ver, minor_ver);
361         if (major_ver != CGX_FIRMWARE_MAJOR_VER ||
362             minor_ver != CGX_FIRMWARE_MINOR_VER)
363                 return -EIO;
364         else
365                 return 0;
366 }
367
368 static int cgx_lmac_init(struct cgx *cgx)
369 {
370         struct lmac *lmac;
371         int i, err;
372
373         cgx->lmac_count = cgx_read(cgx, 0, CGXX_CMRX_RX_LMACS) & 0x7;
374         if (cgx->lmac_count > MAX_LMAC_PER_CGX)
375                 cgx->lmac_count = MAX_LMAC_PER_CGX;
376
377         for (i = 0; i < cgx->lmac_count; i++) {
378                 lmac = kcalloc(1, sizeof(struct lmac), GFP_KERNEL);
379                 if (!lmac)
380                         return -ENOMEM;
381                 lmac->name = kcalloc(1, sizeof("cgx_fwi_xxx_yyy"), GFP_KERNEL);
382                 if (!lmac->name)
383                         return -ENOMEM;
384                 sprintf(lmac->name, "cgx_fwi_%d_%d", cgx->cgx_id, i);
385                 lmac->lmac_id = i;
386                 lmac->cgx = cgx;
387                 init_waitqueue_head(&lmac->wq_cmd_cmplt);
388                 mutex_init(&lmac->cmd_lock);
389                 err = request_irq(pci_irq_vector(cgx->pdev,
390                                                  CGX_LMAC_FWI + i * 9),
391                                    cgx_fwi_event_handler, 0, lmac->name, lmac);
392                 if (err)
393                         return err;
394
395                 /* Enable interrupt */
396                 cgx_write(cgx, lmac->lmac_id, CGXX_CMRX_INT_ENA_W1S,
397                           FW_CGX_INT);
398
399                 /* Add reference */
400                 cgx->lmac_idmap[i] = lmac;
401         }
402
403         return cgx_lmac_verify_fwi_version(cgx);
404 }
405
406 static int cgx_lmac_exit(struct cgx *cgx)
407 {
408         struct lmac *lmac;
409         int i;
410
411         /* Free all lmac related resources */
412         for (i = 0; i < cgx->lmac_count; i++) {
413                 lmac = cgx->lmac_idmap[i];
414                 if (!lmac)
415                         continue;
416                 free_irq(pci_irq_vector(cgx->pdev, CGX_LMAC_FWI + i * 9), lmac);
417                 kfree(lmac->name);
418                 kfree(lmac);
419         }
420
421         return 0;
422 }
423
424 static int cgx_probe(struct pci_dev *pdev, const struct pci_device_id *id)
425 {
426         struct device *dev = &pdev->dev;
427         struct cgx *cgx;
428         int err, nvec;
429
430         cgx = devm_kzalloc(dev, sizeof(*cgx), GFP_KERNEL);
431         if (!cgx)
432                 return -ENOMEM;
433         cgx->pdev = pdev;
434
435         pci_set_drvdata(pdev, cgx);
436
437         err = pci_enable_device(pdev);
438         if (err) {
439                 dev_err(dev, "Failed to enable PCI device\n");
440                 pci_set_drvdata(pdev, NULL);
441                 return err;
442         }
443
444         err = pci_request_regions(pdev, DRV_NAME);
445         if (err) {
446                 dev_err(dev, "PCI request regions failed 0x%x\n", err);
447                 goto err_disable_device;
448         }
449
450         /* MAP configuration registers */
451         cgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
452         if (!cgx->reg_base) {
453                 dev_err(dev, "CGX: Cannot map CSR memory space, aborting\n");
454                 err = -ENOMEM;
455                 goto err_release_regions;
456         }
457
458         nvec = CGX_NVEC;
459         err = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_MSIX);
460         if (err < 0 || err != nvec) {
461                 dev_err(dev, "Request for %d msix vectors failed, err %d\n",
462                         nvec, err);
463                 goto err_release_regions;
464         }
465
466         list_add(&cgx->cgx_list, &cgx_list);
467         cgx->cgx_id = cgx_get_cgx_cnt() - 1;
468
469         err = cgx_lmac_init(cgx);
470         if (err)
471                 goto err_release_lmac;
472
473         return 0;
474
475 err_release_lmac:
476         cgx_lmac_exit(cgx);
477         list_del(&cgx->cgx_list);
478 err_release_regions:
479         pci_release_regions(pdev);
480 err_disable_device:
481         pci_disable_device(pdev);
482         pci_set_drvdata(pdev, NULL);
483         return err;
484 }
485
486 static void cgx_remove(struct pci_dev *pdev)
487 {
488         struct cgx *cgx = pci_get_drvdata(pdev);
489
490         cgx_lmac_exit(cgx);
491         list_del(&cgx->cgx_list);
492         pci_free_irq_vectors(pdev);
493         pci_release_regions(pdev);
494         pci_disable_device(pdev);
495         pci_set_drvdata(pdev, NULL);
496 }
497
498 struct pci_driver cgx_driver = {
499         .name = DRV_NAME,
500         .id_table = cgx_id_table,
501         .probe = cgx_probe,
502         .remove = cgx_remove,
503 };