1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
3
4 #include <linux/bitfield.h>
5 #include <linux/circ_buf.h>
6 #include <linux/device.h>
7 #include <linux/firmware.h>
8 #include <linux/iopoll.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12
13 #include "prestera.h"
14
15 #define PRESTERA_MSG_MAX_SIZE 1500
16
17 #define PRESTERA_SUPP_FW_MAJ_VER 4
18 #define PRESTERA_SUPP_FW_MIN_VER 0
19
20 #define PRESTERA_PREV_FW_MAJ_VER 4
21 #define PRESTERA_PREV_FW_MIN_VER 0
22
23 #define PRESTERA_FW_PATH_FMT "mrvl/prestera/mvsw_prestera_fw-v%u.%u.img"
24
25 #define PRESTERA_FW_HDR_MAGIC 0x351D9D06
26 #define PRESTERA_FW_DL_TIMEOUT_MS 50000
27 #define PRESTERA_FW_BLK_SZ 1024
28
29 #define PRESTERA_FW_VER_MAJ_MUL 1000000
30 #define PRESTERA_FW_VER_MIN_MUL 1000
31
32 #define PRESTERA_FW_VER_MAJ(v) ((v) / PRESTERA_FW_VER_MAJ_MUL)
33
34 #define PRESTERA_FW_VER_MIN(v) \
35 (((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL)) / \
36 PRESTERA_FW_VER_MIN_MUL)
37
38 #define PRESTERA_FW_VER_PATCH(v) \
39 ((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL) - \
40 (PRESTERA_FW_VER_MIN(v) * PRESTERA_FW_VER_MIN_MUL))
41
42 enum prestera_pci_bar_t {
43 PRESTERA_PCI_BAR_FW = 2,
44 PRESTERA_PCI_BAR_PP = 4,
45 };
46
47 struct prestera_fw_header {
48 __be32 magic_number;
49 __be32 version_value;
50 u8 reserved[8];
51 };
52
53 struct prestera_ldr_regs {
54 u32 ldr_ready;
55 u32 pad1;
56
57 u32 ldr_img_size;
58 u32 ldr_ctl_flags;
59
60 u32 ldr_buf_offs;
61 u32 ldr_buf_size;
62
63 u32 ldr_buf_rd;
64 u32 pad2;
65 u32 ldr_buf_wr;
66
67 u32 ldr_status;
68 };
69
70 #define PRESTERA_LDR_REG_OFFSET(f) offsetof(struct prestera_ldr_regs, f)
71
72 #define PRESTERA_LDR_READY_MAGIC 0xf00dfeed
73
74 #define PRESTERA_LDR_STATUS_IMG_DL BIT(0)
75 #define PRESTERA_LDR_STATUS_START_FW BIT(1)
76 #define PRESTERA_LDR_STATUS_INVALID_IMG BIT(2)
77 #define PRESTERA_LDR_STATUS_NOMEM BIT(3)
78
79 #define PRESTERA_LDR_REG_BASE(fw) ((fw)->ldr_regs)
80 #define PRESTERA_LDR_REG_ADDR(fw, reg) (PRESTERA_LDR_REG_BASE(fw) + (reg))
81
82 /* fw loader registers */
83 #define PRESTERA_LDR_READY_REG PRESTERA_LDR_REG_OFFSET(ldr_ready)
84 #define PRESTERA_LDR_IMG_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_img_size)
85 #define PRESTERA_LDR_CTL_REG PRESTERA_LDR_REG_OFFSET(ldr_ctl_flags)
86 #define PRESTERA_LDR_BUF_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_size)
87 #define PRESTERA_LDR_BUF_OFFS_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_offs)
88 #define PRESTERA_LDR_BUF_RD_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_rd)
89 #define PRESTERA_LDR_BUF_WR_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_wr)
90 #define PRESTERA_LDR_STATUS_REG PRESTERA_LDR_REG_OFFSET(ldr_status)
91
92 #define PRESTERA_LDR_CTL_DL_START BIT(0)
93
94 #define PRESTERA_EVT_QNUM_MAX 4
95
96 struct prestera_fw_evtq_regs {
97 u32 rd_idx;
98 u32 pad1;
99 u32 wr_idx;
100 u32 pad2;
101 u32 offs;
102 u32 len;
103 };
104
105 #define PRESTERA_CMD_QNUM_MAX 4
106
107 struct prestera_fw_cmdq_regs {
108 u32 req_ctl;
109 u32 req_len;
110 u32 rcv_ctl;
111 u32 rcv_len;
112 u32 offs;
113 u32 len;
114 };
115
116 struct prestera_fw_regs {
117 u32 fw_ready;
118 u32 cmd_offs;
119 u32 cmd_len;
120 u32 cmd_qnum;
121 u32 evt_offs;
122 u32 evt_qnum;
123
124 u32 fw_status;
125 u32 rx_status;
126
127 struct prestera_fw_cmdq_regs cmdq_list[PRESTERA_EVT_QNUM_MAX];
128 struct prestera_fw_evtq_regs evtq_list[PRESTERA_CMD_QNUM_MAX];
129 };
130
131 #define PRESTERA_FW_REG_OFFSET(f) offsetof(struct prestera_fw_regs, f)
132
133 #define PRESTERA_FW_READY_MAGIC 0xcafebabe
134
135 /* fw registers */
136 #define PRESTERA_FW_READY_REG PRESTERA_FW_REG_OFFSET(fw_ready)
137
138 #define PRESTERA_CMD_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(cmd_offs)
139 #define PRESTERA_CMD_BUF_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_len)
140 #define PRESTERA_CMD_QNUM_REG PRESTERA_FW_REG_OFFSET(cmd_qnum)
141 #define PRESTERA_EVT_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(evt_offs)
142 #define PRESTERA_EVT_QNUM_REG PRESTERA_FW_REG_OFFSET(evt_qnum)
143
144 #define PRESTERA_CMDQ_REG_OFFSET(q, f) \
145 (PRESTERA_FW_REG_OFFSET(cmdq_list) + \
146 (q) * sizeof(struct prestera_fw_cmdq_regs) + \
147 offsetof(struct prestera_fw_cmdq_regs, f))
148
149 #define PRESTERA_CMDQ_REQ_CTL_REG(q) PRESTERA_CMDQ_REG_OFFSET(q, req_ctl)
150 #define PRESTERA_CMDQ_REQ_LEN_REG(q) PRESTERA_CMDQ_REG_OFFSET(q, req_len)
151 #define PRESTERA_CMDQ_RCV_CTL_REG(q) PRESTERA_CMDQ_REG_OFFSET(q, rcv_ctl)
152 #define PRESTERA_CMDQ_RCV_LEN_REG(q) PRESTERA_CMDQ_REG_OFFSET(q, rcv_len)
153 #define PRESTERA_CMDQ_OFFS_REG(q) PRESTERA_CMDQ_REG_OFFSET(q, offs)
154 #define PRESTERA_CMDQ_LEN_REG(q) PRESTERA_CMDQ_REG_OFFSET(q, len)
155
156 #define PRESTERA_FW_STATUS_REG PRESTERA_FW_REG_OFFSET(fw_status)
157 #define PRESTERA_RX_STATUS_REG PRESTERA_FW_REG_OFFSET(rx_status)
158
159 /* PRESTERA_CMD_REQ_CTL_REG flags */
160 #define PRESTERA_CMD_F_REQ_SENT BIT(0)
161 #define PRESTERA_CMD_F_REPL_RCVD BIT(1)
162
163 /* PRESTERA_CMD_RCV_CTL_REG flags */
164 #define PRESTERA_CMD_F_REPL_SENT BIT(0)
165
166 #define PRESTERA_FW_EVT_CTL_STATUS_MASK GENMASK(1, 0)
167
168 #define PRESTERA_FW_EVT_CTL_STATUS_ON 0
169 #define PRESTERA_FW_EVT_CTL_STATUS_OFF 1
170
171 #define PRESTERA_EVTQ_REG_OFFSET(q, f) \
172 (PRESTERA_FW_REG_OFFSET(evtq_list) + \
173 (q) * sizeof(struct prestera_fw_evtq_regs) + \
174 offsetof(struct prestera_fw_evtq_regs, f))
175
176 #define PRESTERA_EVTQ_RD_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, rd_idx)
177 #define PRESTERA_EVTQ_WR_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, wr_idx)
178 #define PRESTERA_EVTQ_OFFS_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, offs)
179 #define PRESTERA_EVTQ_LEN_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, len)
180
181 #define PRESTERA_FW_REG_BASE(fw) ((fw)->dev.ctl_regs)
182 #define PRESTERA_FW_REG_ADDR(fw, reg) PRESTERA_FW_REG_BASE((fw)) + (reg)
183
184 #define PRESTERA_FW_CMD_DEFAULT_WAIT_MS 30000
185 #define PRESTERA_FW_READY_WAIT_MS 20000
186
187 struct prestera_fw_evtq {
188 u8 __iomem *addr;
189 size_t len;
190 };
191
192 struct prestera_fw_cmdq {
193 /* serialize access to dev->send_req */
194 struct mutex cmd_mtx;
195 u8 __iomem *addr;
196 size_t len;
197 };
198
199 struct prestera_fw {
200 struct prestera_fw_rev rev_supp;
201 const struct firmware *bin;
202 struct workqueue_struct *wq;
203 struct prestera_device dev;
204 u8 __iomem *ldr_regs;
205 u8 __iomem *ldr_ring_buf;
206 u32 ldr_buf_len;
207 u32 ldr_wr_idx;
208 size_t cmd_mbox_len;
209 u8 __iomem *cmd_mbox;
210 struct prestera_fw_cmdq cmd_queue[PRESTERA_CMD_QNUM_MAX];
211 u8 cmd_qnum;
212 struct prestera_fw_evtq evt_queue[PRESTERA_EVT_QNUM_MAX];
213 u8 evt_qnum;
214 struct work_struct evt_work;
215 u8 __iomem *evt_buf;
216 u8 *evt_msg;
217 };
218
219 static int prestera_fw_load(struct prestera_fw *fw);
220
prestera_fw_write(struct prestera_fw * fw,u32 reg,u32 val)221 static void prestera_fw_write(struct prestera_fw *fw, u32 reg, u32 val)
222 {
223 writel(val, PRESTERA_FW_REG_ADDR(fw, reg));
224 }
225
prestera_fw_read(struct prestera_fw * fw,u32 reg)226 static u32 prestera_fw_read(struct prestera_fw *fw, u32 reg)
227 {
228 return readl(PRESTERA_FW_REG_ADDR(fw, reg));
229 }
230
prestera_fw_evtq_len(struct prestera_fw * fw,u8 qid)231 static u32 prestera_fw_evtq_len(struct prestera_fw *fw, u8 qid)
232 {
233 return fw->evt_queue[qid].len;
234 }
235
prestera_fw_evtq_avail(struct prestera_fw * fw,u8 qid)236 static u32 prestera_fw_evtq_avail(struct prestera_fw *fw, u8 qid)
237 {
238 u32 wr_idx = prestera_fw_read(fw, PRESTERA_EVTQ_WR_IDX_REG(qid));
239 u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
240
241 return CIRC_CNT(wr_idx, rd_idx, prestera_fw_evtq_len(fw, qid));
242 }
243
prestera_fw_evtq_rd_set(struct prestera_fw * fw,u8 qid,u32 idx)244 static void prestera_fw_evtq_rd_set(struct prestera_fw *fw,
245 u8 qid, u32 idx)
246 {
247 u32 rd_idx = idx & (prestera_fw_evtq_len(fw, qid) - 1);
248
249 prestera_fw_write(fw, PRESTERA_EVTQ_RD_IDX_REG(qid), rd_idx);
250 }
251
prestera_fw_evtq_buf(struct prestera_fw * fw,u8 qid)252 static u8 __iomem *prestera_fw_evtq_buf(struct prestera_fw *fw, u8 qid)
253 {
254 return fw->evt_queue[qid].addr;
255 }
256
prestera_fw_evtq_read32(struct prestera_fw * fw,u8 qid)257 static u32 prestera_fw_evtq_read32(struct prestera_fw *fw, u8 qid)
258 {
259 u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
260 u32 val;
261
262 val = readl(prestera_fw_evtq_buf(fw, qid) + rd_idx);
263 prestera_fw_evtq_rd_set(fw, qid, rd_idx + 4);
264 return val;
265 }
266
prestera_fw_evtq_read_buf(struct prestera_fw * fw,u8 qid,void * buf,size_t len)267 static ssize_t prestera_fw_evtq_read_buf(struct prestera_fw *fw,
268 u8 qid, void *buf, size_t len)
269 {
270 u32 idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
271 u8 __iomem *evtq_addr = prestera_fw_evtq_buf(fw, qid);
272 u32 *buf32 = buf;
273 int i;
274
275 for (i = 0; i < len / 4; buf32++, i++) {
276 *buf32 = readl_relaxed(evtq_addr + idx);
277 idx = (idx + 4) & (prestera_fw_evtq_len(fw, qid) - 1);
278 }
279
280 prestera_fw_evtq_rd_set(fw, qid, idx);
281
282 return i;
283 }
284
prestera_fw_evtq_pick(struct prestera_fw * fw)285 static u8 prestera_fw_evtq_pick(struct prestera_fw *fw)
286 {
287 int qid;
288
289 for (qid = 0; qid < fw->evt_qnum; qid++) {
290 if (prestera_fw_evtq_avail(fw, qid) >= 4)
291 return qid;
292 }
293
294 return PRESTERA_EVT_QNUM_MAX;
295 }
296
prestera_fw_evt_ctl_status_set(struct prestera_fw * fw,u32 val)297 static void prestera_fw_evt_ctl_status_set(struct prestera_fw *fw, u32 val)
298 {
299 u32 status = prestera_fw_read(fw, PRESTERA_FW_STATUS_REG);
300
301 u32p_replace_bits(&status, val, PRESTERA_FW_EVT_CTL_STATUS_MASK);
302
303 prestera_fw_write(fw, PRESTERA_FW_STATUS_REG, status);
304 }
305
prestera_fw_evt_work_fn(struct work_struct * work)306 static void prestera_fw_evt_work_fn(struct work_struct *work)
307 {
308 struct prestera_fw *fw;
309 void *msg;
310 u8 qid;
311
312 fw = container_of(work, struct prestera_fw, evt_work);
313 msg = fw->evt_msg;
314
315 prestera_fw_evt_ctl_status_set(fw, PRESTERA_FW_EVT_CTL_STATUS_OFF);
316
317 while ((qid = prestera_fw_evtq_pick(fw)) < PRESTERA_EVT_QNUM_MAX) {
318 u32 idx;
319 u32 len;
320
321 len = prestera_fw_evtq_read32(fw, qid);
322 idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
323
324 WARN_ON(prestera_fw_evtq_avail(fw, qid) < len);
325
326 if (WARN_ON(len > PRESTERA_MSG_MAX_SIZE)) {
327 prestera_fw_evtq_rd_set(fw, qid, idx + len);
328 continue;
329 }
330
331 prestera_fw_evtq_read_buf(fw, qid, msg, len);
332
333 if (fw->dev.recv_msg)
334 fw->dev.recv_msg(&fw->dev, msg, len);
335 }
336
337 prestera_fw_evt_ctl_status_set(fw, PRESTERA_FW_EVT_CTL_STATUS_ON);
338 }
339
prestera_fw_wait_reg32(struct prestera_fw * fw,u32 reg,u32 cmp,unsigned int waitms)340 static int prestera_fw_wait_reg32(struct prestera_fw *fw, u32 reg, u32 cmp,
341 unsigned int waitms)
342 {
343 u8 __iomem *addr = PRESTERA_FW_REG_ADDR(fw, reg);
344 u32 val;
345
346 return readl_poll_timeout(addr, val, cmp == val,
347 1 * USEC_PER_MSEC, waitms * USEC_PER_MSEC);
348 }
349
prestera_fw_cmdq_lock(struct prestera_fw * fw,u8 qid)350 static void prestera_fw_cmdq_lock(struct prestera_fw *fw, u8 qid)
351 {
352 mutex_lock(&fw->cmd_queue[qid].cmd_mtx);
353 }
354
prestera_fw_cmdq_unlock(struct prestera_fw * fw,u8 qid)355 static void prestera_fw_cmdq_unlock(struct prestera_fw *fw, u8 qid)
356 {
357 mutex_unlock(&fw->cmd_queue[qid].cmd_mtx);
358 }
359
prestera_fw_cmdq_len(struct prestera_fw * fw,u8 qid)360 static u32 prestera_fw_cmdq_len(struct prestera_fw *fw, u8 qid)
361 {
362 return fw->cmd_queue[qid].len;
363 }
364
prestera_fw_cmdq_buf(struct prestera_fw * fw,u8 qid)365 static u8 __iomem *prestera_fw_cmdq_buf(struct prestera_fw *fw, u8 qid)
366 {
367 return fw->cmd_queue[qid].addr;
368 }
369
prestera_fw_cmd_send(struct prestera_fw * fw,int qid,void * in_msg,size_t in_size,void * out_msg,size_t out_size,unsigned int waitms)370 static int prestera_fw_cmd_send(struct prestera_fw *fw, int qid,
371 void *in_msg, size_t in_size,
372 void *out_msg, size_t out_size,
373 unsigned int waitms)
374 {
375 u32 ret_size;
376 int err;
377
378 if (!waitms)
379 waitms = PRESTERA_FW_CMD_DEFAULT_WAIT_MS;
380
381 if (ALIGN(in_size, 4) > prestera_fw_cmdq_len(fw, qid))
382 return -EMSGSIZE;
383
384 /* wait for finish previous reply from FW */
385 err = prestera_fw_wait_reg32(fw, PRESTERA_CMDQ_RCV_CTL_REG(qid), 0, 30);
386 if (err) {
387 dev_err(fw->dev.dev, "finish reply from FW is timed out\n");
388 return err;
389 }
390
391 prestera_fw_write(fw, PRESTERA_CMDQ_REQ_LEN_REG(qid), in_size);
392
393 memcpy_toio(prestera_fw_cmdq_buf(fw, qid), in_msg, in_size);
394
395 prestera_fw_write(fw, PRESTERA_CMDQ_REQ_CTL_REG(qid),
396 PRESTERA_CMD_F_REQ_SENT);
397
398 /* wait for reply from FW */
399 err = prestera_fw_wait_reg32(fw, PRESTERA_CMDQ_RCV_CTL_REG(qid),
400 PRESTERA_CMD_F_REPL_SENT, waitms);
401 if (err) {
402 dev_err(fw->dev.dev, "reply from FW is timed out\n");
403 goto cmd_exit;
404 }
405
406 ret_size = prestera_fw_read(fw, PRESTERA_CMDQ_RCV_LEN_REG(qid));
407 if (ret_size > out_size) {
408 dev_err(fw->dev.dev, "ret_size (%u) > out_len(%zu)\n",
409 ret_size, out_size);
410 err = -EMSGSIZE;
411 goto cmd_exit;
412 }
413
414 memcpy_fromio(out_msg,
415 prestera_fw_cmdq_buf(fw, qid) + in_size, ret_size);
416
417 cmd_exit:
418 prestera_fw_write(fw, PRESTERA_CMDQ_REQ_CTL_REG(qid),
419 PRESTERA_CMD_F_REPL_RCVD);
420 return err;
421 }
422
prestera_fw_send_req(struct prestera_device * dev,int qid,void * in_msg,size_t in_size,void * out_msg,size_t out_size,unsigned int waitms)423 static int prestera_fw_send_req(struct prestera_device *dev, int qid,
424 void *in_msg, size_t in_size, void *out_msg,
425 size_t out_size, unsigned int waitms)
426 {
427 struct prestera_fw *fw;
428 ssize_t ret;
429
430 fw = container_of(dev, struct prestera_fw, dev);
431
432 prestera_fw_cmdq_lock(fw, qid);
433 ret = prestera_fw_cmd_send(fw, qid, in_msg, in_size, out_msg, out_size,
434 waitms);
435 prestera_fw_cmdq_unlock(fw, qid);
436
437 return ret;
438 }
439
prestera_fw_init(struct prestera_fw * fw)440 static int prestera_fw_init(struct prestera_fw *fw)
441 {
442 u8 __iomem *base;
443 int err;
444 u8 qid;
445
446 fw->dev.send_req = prestera_fw_send_req;
447 fw->ldr_regs = fw->dev.ctl_regs;
448
449 err = prestera_fw_load(fw);
450 if (err)
451 return err;
452
453 err = prestera_fw_wait_reg32(fw, PRESTERA_FW_READY_REG,
454 PRESTERA_FW_READY_MAGIC,
455 PRESTERA_FW_READY_WAIT_MS);
456 if (err) {
457 dev_err(fw->dev.dev, "FW failed to start\n");
458 return err;
459 }
460
461 base = fw->dev.ctl_regs;
462
463 fw->cmd_mbox = base + prestera_fw_read(fw, PRESTERA_CMD_BUF_OFFS_REG);
464 fw->cmd_mbox_len = prestera_fw_read(fw, PRESTERA_CMD_BUF_LEN_REG);
465 fw->cmd_qnum = prestera_fw_read(fw, PRESTERA_CMD_QNUM_REG);
466
467 for (qid = 0; qid < fw->cmd_qnum; qid++) {
468 u32 offs = prestera_fw_read(fw, PRESTERA_CMDQ_OFFS_REG(qid));
469 struct prestera_fw_cmdq *cmdq = &fw->cmd_queue[qid];
470
471 cmdq->len = prestera_fw_read(fw, PRESTERA_CMDQ_LEN_REG(qid));
472 cmdq->addr = fw->cmd_mbox + offs;
473 mutex_init(&cmdq->cmd_mtx);
474 }
475
476 fw->evt_buf = base + prestera_fw_read(fw, PRESTERA_EVT_BUF_OFFS_REG);
477 fw->evt_qnum = prestera_fw_read(fw, PRESTERA_EVT_QNUM_REG);
478 fw->evt_msg = kmalloc(PRESTERA_MSG_MAX_SIZE, GFP_KERNEL);
479 if (!fw->evt_msg)
480 return -ENOMEM;
481
482 for (qid = 0; qid < fw->evt_qnum; qid++) {
483 u32 offs = prestera_fw_read(fw, PRESTERA_EVTQ_OFFS_REG(qid));
484 struct prestera_fw_evtq *evtq = &fw->evt_queue[qid];
485
486 evtq->len = prestera_fw_read(fw, PRESTERA_EVTQ_LEN_REG(qid));
487 evtq->addr = fw->evt_buf + offs;
488 }
489
490 return 0;
491 }
492
prestera_fw_uninit(struct prestera_fw * fw)493 static void prestera_fw_uninit(struct prestera_fw *fw)
494 {
495 kfree(fw->evt_msg);
496 }
497
prestera_pci_irq_handler(int irq,void * dev_id)498 static irqreturn_t prestera_pci_irq_handler(int irq, void *dev_id)
499 {
500 struct prestera_fw *fw = dev_id;
501
502 if (prestera_fw_read(fw, PRESTERA_RX_STATUS_REG)) {
503 prestera_fw_write(fw, PRESTERA_RX_STATUS_REG, 0);
504
505 if (fw->dev.recv_pkt)
506 fw->dev.recv_pkt(&fw->dev);
507 }
508
509 queue_work(fw->wq, &fw->evt_work);
510
511 return IRQ_HANDLED;
512 }
513
prestera_ldr_write(struct prestera_fw * fw,u32 reg,u32 val)514 static void prestera_ldr_write(struct prestera_fw *fw, u32 reg, u32 val)
515 {
516 writel(val, PRESTERA_LDR_REG_ADDR(fw, reg));
517 }
518
prestera_ldr_read(struct prestera_fw * fw,u32 reg)519 static u32 prestera_ldr_read(struct prestera_fw *fw, u32 reg)
520 {
521 return readl(PRESTERA_LDR_REG_ADDR(fw, reg));
522 }
523
prestera_ldr_wait_reg32(struct prestera_fw * fw,u32 reg,u32 cmp,unsigned int waitms)524 static int prestera_ldr_wait_reg32(struct prestera_fw *fw,
525 u32 reg, u32 cmp, unsigned int waitms)
526 {
527 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, reg);
528 u32 val;
529
530 return readl_poll_timeout(addr, val, cmp == val,
531 10 * USEC_PER_MSEC, waitms * USEC_PER_MSEC);
532 }
533
prestera_ldr_wait_buf(struct prestera_fw * fw,size_t len)534 static u32 prestera_ldr_wait_buf(struct prestera_fw *fw, size_t len)
535 {
536 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_BUF_RD_REG);
537 u32 buf_len = fw->ldr_buf_len;
538 u32 wr_idx = fw->ldr_wr_idx;
539 u32 rd_idx;
540
541 return readl_poll_timeout(addr, rd_idx,
542 CIRC_SPACE(wr_idx, rd_idx, buf_len) >= len,
543 1 * USEC_PER_MSEC, 100 * USEC_PER_MSEC);
544 }
545
prestera_ldr_wait_dl_finish(struct prestera_fw * fw)546 static int prestera_ldr_wait_dl_finish(struct prestera_fw *fw)
547 {
548 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_STATUS_REG);
549 unsigned long mask = ~(PRESTERA_LDR_STATUS_IMG_DL);
550 u32 val;
551 int err;
552
553 err = readl_poll_timeout(addr, val, val & mask, 10 * USEC_PER_MSEC,
554 PRESTERA_FW_DL_TIMEOUT_MS * USEC_PER_MSEC);
555 if (err) {
556 dev_err(fw->dev.dev, "Timeout to load FW img [state=%d]",
557 prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG));
558 return err;
559 }
560
561 return 0;
562 }
563
prestera_ldr_wr_idx_move(struct prestera_fw * fw,unsigned int n)564 static void prestera_ldr_wr_idx_move(struct prestera_fw *fw, unsigned int n)
565 {
566 fw->ldr_wr_idx = (fw->ldr_wr_idx + (n)) & (fw->ldr_buf_len - 1);
567 }
568
prestera_ldr_wr_idx_commit(struct prestera_fw * fw)569 static void prestera_ldr_wr_idx_commit(struct prestera_fw *fw)
570 {
571 prestera_ldr_write(fw, PRESTERA_LDR_BUF_WR_REG, fw->ldr_wr_idx);
572 }
573
prestera_ldr_wr_ptr(struct prestera_fw * fw)574 static u8 __iomem *prestera_ldr_wr_ptr(struct prestera_fw *fw)
575 {
576 return fw->ldr_ring_buf + fw->ldr_wr_idx;
577 }
578
prestera_ldr_send(struct prestera_fw * fw,const u8 * buf,size_t len)579 static int prestera_ldr_send(struct prestera_fw *fw, const u8 *buf, size_t len)
580 {
581 int err;
582 int i;
583
584 err = prestera_ldr_wait_buf(fw, len);
585 if (err) {
586 dev_err(fw->dev.dev, "failed wait for sending firmware\n");
587 return err;
588 }
589
590 for (i = 0; i < len; i += 4) {
591 writel_relaxed(*(u32 *)(buf + i), prestera_ldr_wr_ptr(fw));
592 prestera_ldr_wr_idx_move(fw, 4);
593 }
594
595 prestera_ldr_wr_idx_commit(fw);
596 return 0;
597 }
598
prestera_ldr_fw_send(struct prestera_fw * fw,const char * img,u32 fw_size)599 static int prestera_ldr_fw_send(struct prestera_fw *fw,
600 const char *img, u32 fw_size)
601 {
602 u32 status;
603 u32 pos;
604 int err;
605
606 err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_STATUS_REG,
607 PRESTERA_LDR_STATUS_IMG_DL,
608 5 * MSEC_PER_SEC);
609 if (err) {
610 dev_err(fw->dev.dev, "Loader is not ready to load image\n");
611 return err;
612 }
613
614 for (pos = 0; pos < fw_size; pos += PRESTERA_FW_BLK_SZ) {
615 if (pos + PRESTERA_FW_BLK_SZ > fw_size)
616 break;
617
618 err = prestera_ldr_send(fw, img + pos, PRESTERA_FW_BLK_SZ);
619 if (err)
620 return err;
621 }
622
623 if (pos < fw_size) {
624 err = prestera_ldr_send(fw, img + pos, fw_size - pos);
625 if (err)
626 return err;
627 }
628
629 err = prestera_ldr_wait_dl_finish(fw);
630 if (err)
631 return err;
632
633 status = prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG);
634
635 switch (status) {
636 case PRESTERA_LDR_STATUS_INVALID_IMG:
637 dev_err(fw->dev.dev, "FW img has bad CRC\n");
638 return -EINVAL;
639 case PRESTERA_LDR_STATUS_NOMEM:
640 dev_err(fw->dev.dev, "Loader has no enough mem\n");
641 return -ENOMEM;
642 }
643
644 return 0;
645 }
646
prestera_fw_rev_parse(const struct prestera_fw_header * hdr,struct prestera_fw_rev * rev)647 static void prestera_fw_rev_parse(const struct prestera_fw_header *hdr,
648 struct prestera_fw_rev *rev)
649 {
650 u32 version = be32_to_cpu(hdr->version_value);
651
652 rev->maj = PRESTERA_FW_VER_MAJ(version);
653 rev->min = PRESTERA_FW_VER_MIN(version);
654 rev->sub = PRESTERA_FW_VER_PATCH(version);
655 }
656
prestera_fw_rev_check(struct prestera_fw * fw)657 static int prestera_fw_rev_check(struct prestera_fw *fw)
658 {
659 struct prestera_fw_rev *rev = &fw->dev.fw_rev;
660
661 if (rev->maj == fw->rev_supp.maj && rev->min >= fw->rev_supp.min)
662 return 0;
663
664 dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'",
665 fw->rev_supp.maj, fw->rev_supp.min);
666
667 return -EINVAL;
668 }
669
prestera_fw_hdr_parse(struct prestera_fw * fw)670 static int prestera_fw_hdr_parse(struct prestera_fw *fw)
671 {
672 struct prestera_fw_rev *rev = &fw->dev.fw_rev;
673 struct prestera_fw_header *hdr;
674 u32 magic;
675
676 hdr = (struct prestera_fw_header *)fw->bin->data;
677
678 magic = be32_to_cpu(hdr->magic_number);
679 if (magic != PRESTERA_FW_HDR_MAGIC) {
680 dev_err(fw->dev.dev, "FW img hdr magic is invalid");
681 return -EINVAL;
682 }
683
684 prestera_fw_rev_parse(hdr, rev);
685
686 dev_info(fw->dev.dev, "FW version '%u.%u.%u'\n",
687 rev->maj, rev->min, rev->sub);
688
689 return prestera_fw_rev_check(fw);
690 }
691
prestera_fw_get(struct prestera_fw * fw)692 static int prestera_fw_get(struct prestera_fw *fw)
693 {
694 int ver_maj = PRESTERA_SUPP_FW_MAJ_VER;
695 int ver_min = PRESTERA_SUPP_FW_MIN_VER;
696 char fw_path[128];
697 int err;
698
699 pick_fw_ver:
700 snprintf(fw_path, sizeof(fw_path), PRESTERA_FW_PATH_FMT,
701 ver_maj, ver_min);
702
703 err = request_firmware_direct(&fw->bin, fw_path, fw->dev.dev);
704 if (err) {
705 if (ver_maj != PRESTERA_PREV_FW_MAJ_VER ||
706 ver_min != PRESTERA_PREV_FW_MIN_VER) {
707 ver_maj = PRESTERA_PREV_FW_MAJ_VER;
708 ver_min = PRESTERA_PREV_FW_MIN_VER;
709
710 dev_warn(fw->dev.dev,
711 "missing latest %s firmware, fall-back to previous %u.%u version\n",
712 fw_path, ver_maj, ver_min);
713
714 goto pick_fw_ver;
715 } else {
716 dev_err(fw->dev.dev, "failed to request previous firmware: %s\n",
717 fw_path);
718 return err;
719 }
720 }
721
722 dev_info(fw->dev.dev, "Loading %s ...", fw_path);
723
724 fw->rev_supp.maj = ver_maj;
725 fw->rev_supp.min = ver_min;
726 fw->rev_supp.sub = 0;
727
728 return 0;
729 }
730
prestera_fw_put(struct prestera_fw * fw)731 static void prestera_fw_put(struct prestera_fw *fw)
732 {
733 release_firmware(fw->bin);
734 }
735
prestera_fw_load(struct prestera_fw * fw)736 static int prestera_fw_load(struct prestera_fw *fw)
737 {
738 size_t hlen = sizeof(struct prestera_fw_header);
739 int err;
740
741 err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_READY_REG,
742 PRESTERA_LDR_READY_MAGIC,
743 5 * MSEC_PER_SEC);
744 if (err) {
745 dev_err(fw->dev.dev, "waiting for FW loader is timed out");
746 return err;
747 }
748
749 fw->ldr_ring_buf = fw->ldr_regs +
750 prestera_ldr_read(fw, PRESTERA_LDR_BUF_OFFS_REG);
751
752 fw->ldr_buf_len =
753 prestera_ldr_read(fw, PRESTERA_LDR_BUF_SIZE_REG);
754
755 fw->ldr_wr_idx = 0;
756
757 err = prestera_fw_get(fw);
758 if (err)
759 return err;
760
761 err = prestera_fw_hdr_parse(fw);
762 if (err) {
763 dev_err(fw->dev.dev, "FW image header is invalid\n");
764 goto out_release;
765 }
766
767 prestera_ldr_write(fw, PRESTERA_LDR_IMG_SIZE_REG, fw->bin->size - hlen);
768 prestera_ldr_write(fw, PRESTERA_LDR_CTL_REG, PRESTERA_LDR_CTL_DL_START);
769
770 err = prestera_ldr_fw_send(fw, fw->bin->data + hlen,
771 fw->bin->size - hlen);
772
773 out_release:
774 prestera_fw_put(fw);
775 return err;
776 }
777
prestera_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)778 static int prestera_pci_probe(struct pci_dev *pdev,
779 const struct pci_device_id *id)
780 {
781 const char *driver_name = dev_driver_string(&pdev->dev);
782 struct prestera_fw *fw;
783 int err;
784
785 err = pcim_enable_device(pdev);
786 if (err)
787 return err;
788
789 err = pcim_iomap_regions(pdev, BIT(PRESTERA_PCI_BAR_FW) |
790 BIT(PRESTERA_PCI_BAR_PP),
791 pci_name(pdev));
792 if (err)
793 return err;
794
795 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(30));
796 if (err) {
797 dev_err(&pdev->dev, "fail to set DMA mask\n");
798 goto err_dma_mask;
799 }
800
801 pci_set_master(pdev);
802
803 fw = devm_kzalloc(&pdev->dev, sizeof(*fw), GFP_KERNEL);
804 if (!fw) {
805 err = -ENOMEM;
806 goto err_pci_dev_alloc;
807 }
808
809 fw->dev.ctl_regs = pcim_iomap_table(pdev)[PRESTERA_PCI_BAR_FW];
810 fw->dev.pp_regs = pcim_iomap_table(pdev)[PRESTERA_PCI_BAR_PP];
811 fw->dev.dev = &pdev->dev;
812
813 pci_set_drvdata(pdev, fw);
814
815 err = prestera_fw_init(fw);
816 if (err)
817 goto err_prestera_fw_init;
818
819 dev_info(fw->dev.dev, "Prestera FW is ready\n");
820
821 fw->wq = alloc_workqueue("prestera_fw_wq", WQ_HIGHPRI, 1);
822 if (!fw->wq) {
823 err = -ENOMEM;
824 goto err_wq_alloc;
825 }
826
827 INIT_WORK(&fw->evt_work, prestera_fw_evt_work_fn);
828
829 err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
830 if (err < 0) {
831 dev_err(&pdev->dev, "MSI IRQ init failed\n");
832 goto err_irq_alloc;
833 }
834
835 err = request_irq(pci_irq_vector(pdev, 0), prestera_pci_irq_handler,
836 0, driver_name, fw);
837 if (err) {
838 dev_err(&pdev->dev, "fail to request IRQ\n");
839 goto err_request_irq;
840 }
841
842 err = prestera_device_register(&fw->dev);
843 if (err)
844 goto err_prestera_dev_register;
845
846 return 0;
847
848 err_prestera_dev_register:
849 free_irq(pci_irq_vector(pdev, 0), fw);
850 err_request_irq:
851 pci_free_irq_vectors(pdev);
852 err_irq_alloc:
853 destroy_workqueue(fw->wq);
854 err_wq_alloc:
855 prestera_fw_uninit(fw);
856 err_prestera_fw_init:
857 err_pci_dev_alloc:
858 err_dma_mask:
859 return err;
860 }
861
prestera_pci_remove(struct pci_dev * pdev)862 static void prestera_pci_remove(struct pci_dev *pdev)
863 {
864 struct prestera_fw *fw = pci_get_drvdata(pdev);
865
866 prestera_device_unregister(&fw->dev);
867 free_irq(pci_irq_vector(pdev, 0), fw);
868 pci_free_irq_vectors(pdev);
869 destroy_workqueue(fw->wq);
870 prestera_fw_uninit(fw);
871 }
872
873 static const struct pci_device_id prestera_pci_devices[] = {
874 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC804) },
875 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC80C) },
876 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xCC1E) },
877 { }
878 };
879 MODULE_DEVICE_TABLE(pci, prestera_pci_devices);
880
881 static struct pci_driver prestera_pci_driver = {
882 .name = "Prestera DX",
883 .id_table = prestera_pci_devices,
884 .probe = prestera_pci_probe,
885 .remove = prestera_pci_remove,
886 };
887 module_pci_driver(prestera_pci_driver);
888
889 MODULE_LICENSE("Dual BSD/GPL");
890 MODULE_DESCRIPTION("Marvell Prestera switch PCI interface");
891