1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
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>
15 #define PRESTERA_MSG_MAX_SIZE 1500
17 #define PRESTERA_SUPP_FW_MAJ_VER 4
18 #define PRESTERA_SUPP_FW_MIN_VER 0
20 #define PRESTERA_PREV_FW_MAJ_VER 4
21 #define PRESTERA_PREV_FW_MIN_VER 0
23 #define PRESTERA_FW_PATH_FMT "/*(DEBLOBBED)*/"
25 #define PRESTERA_FW_HDR_MAGIC 0x351D9D06
26 #define PRESTERA_FW_DL_TIMEOUT_MS 50000
27 #define PRESTERA_FW_BLK_SZ 1024
29 #define PRESTERA_FW_VER_MAJ_MUL 1000000
30 #define PRESTERA_FW_VER_MIN_MUL 1000
32 #define PRESTERA_FW_VER_MAJ(v) ((v) / PRESTERA_FW_VER_MAJ_MUL)
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)
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))
42 enum prestera_pci_bar_t {
43 PRESTERA_PCI_BAR_FW = 2,
44 PRESTERA_PCI_BAR_PP = 4,
47 struct prestera_fw_header {
53 struct prestera_ldr_regs {
70 #define PRESTERA_LDR_REG_OFFSET(f) offsetof(struct prestera_ldr_regs, f)
72 #define PRESTERA_LDR_READY_MAGIC 0xf00dfeed
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)
79 #define PRESTERA_LDR_REG_BASE(fw) ((fw)->ldr_regs)
80 #define PRESTERA_LDR_REG_ADDR(fw, reg) (PRESTERA_LDR_REG_BASE(fw) + (reg))
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)
92 #define PRESTERA_LDR_CTL_DL_START BIT(0)
94 #define PRESTERA_EVT_QNUM_MAX 4
96 struct prestera_fw_evtq_regs {
105 #define PRESTERA_CMD_QNUM_MAX 4
107 struct prestera_fw_cmdq_regs {
116 struct prestera_fw_regs {
127 struct prestera_fw_cmdq_regs cmdq_list[PRESTERA_EVT_QNUM_MAX];
128 struct prestera_fw_evtq_regs evtq_list[PRESTERA_CMD_QNUM_MAX];
131 #define PRESTERA_FW_REG_OFFSET(f) offsetof(struct prestera_fw_regs, f)
133 #define PRESTERA_FW_READY_MAGIC 0xcafebabe
136 #define PRESTERA_FW_READY_REG PRESTERA_FW_REG_OFFSET(fw_ready)
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)
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))
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)
156 #define PRESTERA_FW_STATUS_REG PRESTERA_FW_REG_OFFSET(fw_status)
157 #define PRESTERA_RX_STATUS_REG PRESTERA_FW_REG_OFFSET(rx_status)
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)
163 /* PRESTERA_CMD_RCV_CTL_REG flags */
164 #define PRESTERA_CMD_F_REPL_SENT BIT(0)
166 #define PRESTERA_FW_EVT_CTL_STATUS_MASK GENMASK(1, 0)
168 #define PRESTERA_FW_EVT_CTL_STATUS_ON 0
169 #define PRESTERA_FW_EVT_CTL_STATUS_OFF 1
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))
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)
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)
184 #define PRESTERA_FW_CMD_DEFAULT_WAIT_MS 30000
185 #define PRESTERA_FW_READY_WAIT_MS 20000
187 struct prestera_fw_evtq {
192 struct prestera_fw_cmdq {
193 /* serialize access to dev->send_req */
194 struct mutex cmd_mtx;
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;
209 u8 __iomem *cmd_mbox;
210 struct prestera_fw_cmdq cmd_queue[PRESTERA_CMD_QNUM_MAX];
212 struct prestera_fw_evtq evt_queue[PRESTERA_EVT_QNUM_MAX];
214 struct work_struct evt_work;
219 static int prestera_fw_load(struct prestera_fw *fw);
221 static void prestera_fw_write(struct prestera_fw *fw, u32 reg, u32 val)
223 writel(val, PRESTERA_FW_REG_ADDR(fw, reg));
226 static u32 prestera_fw_read(struct prestera_fw *fw, u32 reg)
228 return readl(PRESTERA_FW_REG_ADDR(fw, reg));
231 static u32 prestera_fw_evtq_len(struct prestera_fw *fw, u8 qid)
233 return fw->evt_queue[qid].len;
236 static u32 prestera_fw_evtq_avail(struct prestera_fw *fw, u8 qid)
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));
241 return CIRC_CNT(wr_idx, rd_idx, prestera_fw_evtq_len(fw, qid));
244 static void prestera_fw_evtq_rd_set(struct prestera_fw *fw,
247 u32 rd_idx = idx & (prestera_fw_evtq_len(fw, qid) - 1);
249 prestera_fw_write(fw, PRESTERA_EVTQ_RD_IDX_REG(qid), rd_idx);
252 static u8 __iomem *prestera_fw_evtq_buf(struct prestera_fw *fw, u8 qid)
254 return fw->evt_queue[qid].addr;
257 static u32 prestera_fw_evtq_read32(struct prestera_fw *fw, u8 qid)
259 u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
262 val = readl(prestera_fw_evtq_buf(fw, qid) + rd_idx);
263 prestera_fw_evtq_rd_set(fw, qid, rd_idx + 4);
267 static ssize_t prestera_fw_evtq_read_buf(struct prestera_fw *fw,
268 u8 qid, void *buf, size_t len)
270 u32 idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
271 u8 __iomem *evtq_addr = prestera_fw_evtq_buf(fw, qid);
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);
280 prestera_fw_evtq_rd_set(fw, qid, idx);
285 static u8 prestera_fw_evtq_pick(struct prestera_fw *fw)
289 for (qid = 0; qid < fw->evt_qnum; qid++) {
290 if (prestera_fw_evtq_avail(fw, qid) >= 4)
294 return PRESTERA_EVT_QNUM_MAX;
297 static void prestera_fw_evt_ctl_status_set(struct prestera_fw *fw, u32 val)
299 u32 status = prestera_fw_read(fw, PRESTERA_FW_STATUS_REG);
301 u32p_replace_bits(&status, val, PRESTERA_FW_EVT_CTL_STATUS_MASK);
303 prestera_fw_write(fw, PRESTERA_FW_STATUS_REG, status);
306 static void prestera_fw_evt_work_fn(struct work_struct *work)
308 struct prestera_fw *fw;
312 fw = container_of(work, struct prestera_fw, evt_work);
315 prestera_fw_evt_ctl_status_set(fw, PRESTERA_FW_EVT_CTL_STATUS_OFF);
317 while ((qid = prestera_fw_evtq_pick(fw)) < PRESTERA_EVT_QNUM_MAX) {
321 len = prestera_fw_evtq_read32(fw, qid);
322 idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
324 WARN_ON(prestera_fw_evtq_avail(fw, qid) < len);
326 if (WARN_ON(len > PRESTERA_MSG_MAX_SIZE)) {
327 prestera_fw_evtq_rd_set(fw, qid, idx + len);
331 prestera_fw_evtq_read_buf(fw, qid, msg, len);
333 if (fw->dev.recv_msg)
334 fw->dev.recv_msg(&fw->dev, msg, len);
337 prestera_fw_evt_ctl_status_set(fw, PRESTERA_FW_EVT_CTL_STATUS_ON);
340 static int prestera_fw_wait_reg32(struct prestera_fw *fw, u32 reg, u32 cmp,
343 u8 __iomem *addr = PRESTERA_FW_REG_ADDR(fw, reg);
346 return readl_poll_timeout(addr, val, cmp == val,
347 1 * USEC_PER_MSEC, waitms * USEC_PER_MSEC);
350 static void prestera_fw_cmdq_lock(struct prestera_fw *fw, u8 qid)
352 mutex_lock(&fw->cmd_queue[qid].cmd_mtx);
355 static void prestera_fw_cmdq_unlock(struct prestera_fw *fw, u8 qid)
357 mutex_unlock(&fw->cmd_queue[qid].cmd_mtx);
360 static u32 prestera_fw_cmdq_len(struct prestera_fw *fw, u8 qid)
362 return fw->cmd_queue[qid].len;
365 static u8 __iomem *prestera_fw_cmdq_buf(struct prestera_fw *fw, u8 qid)
367 return fw->cmd_queue[qid].addr;
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,
379 waitms = PRESTERA_FW_CMD_DEFAULT_WAIT_MS;
381 if (ALIGN(in_size, 4) > prestera_fw_cmdq_len(fw, qid))
384 /* wait for finish previous reply from FW */
385 err = prestera_fw_wait_reg32(fw, PRESTERA_CMDQ_RCV_CTL_REG(qid), 0, 30);
387 dev_err(fw->dev.dev, "finish reply from FW is timed out\n");
391 prestera_fw_write(fw, PRESTERA_CMDQ_REQ_LEN_REG(qid), in_size);
393 memcpy_toio(prestera_fw_cmdq_buf(fw, qid), in_msg, in_size);
395 prestera_fw_write(fw, PRESTERA_CMDQ_REQ_CTL_REG(qid),
396 PRESTERA_CMD_F_REQ_SENT);
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);
402 dev_err(fw->dev.dev, "reply from FW is timed out\n");
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",
414 memcpy_fromio(out_msg,
415 prestera_fw_cmdq_buf(fw, qid) + in_size, ret_size);
418 prestera_fw_write(fw, PRESTERA_CMDQ_REQ_CTL_REG(qid),
419 PRESTERA_CMD_F_REPL_RCVD);
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)
427 struct prestera_fw *fw;
430 fw = container_of(dev, struct prestera_fw, dev);
432 prestera_fw_cmdq_lock(fw, qid);
433 ret = prestera_fw_cmd_send(fw, qid, in_msg, in_size, out_msg, out_size,
435 prestera_fw_cmdq_unlock(fw, qid);
440 static int prestera_fw_init(struct prestera_fw *fw)
446 fw->dev.send_req = prestera_fw_send_req;
447 fw->ldr_regs = fw->dev.ctl_regs;
449 err = prestera_fw_load(fw);
453 err = prestera_fw_wait_reg32(fw, PRESTERA_FW_READY_REG,
454 PRESTERA_FW_READY_MAGIC,
455 PRESTERA_FW_READY_WAIT_MS);
457 dev_err(fw->dev.dev, "FW failed to start\n");
461 base = fw->dev.ctl_regs;
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);
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];
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);
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);
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];
486 evtq->len = prestera_fw_read(fw, PRESTERA_EVTQ_LEN_REG(qid));
487 evtq->addr = fw->evt_buf + offs;
493 static void prestera_fw_uninit(struct prestera_fw *fw)
498 static irqreturn_t prestera_pci_irq_handler(int irq, void *dev_id)
500 struct prestera_fw *fw = dev_id;
502 if (prestera_fw_read(fw, PRESTERA_RX_STATUS_REG)) {
503 prestera_fw_write(fw, PRESTERA_RX_STATUS_REG, 0);
505 if (fw->dev.recv_pkt)
506 fw->dev.recv_pkt(&fw->dev);
509 queue_work(fw->wq, &fw->evt_work);
514 static void prestera_ldr_write(struct prestera_fw *fw, u32 reg, u32 val)
516 writel(val, PRESTERA_LDR_REG_ADDR(fw, reg));
519 static u32 prestera_ldr_read(struct prestera_fw *fw, u32 reg)
521 return readl(PRESTERA_LDR_REG_ADDR(fw, reg));
524 static int prestera_ldr_wait_reg32(struct prestera_fw *fw,
525 u32 reg, u32 cmp, unsigned int waitms)
527 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, reg);
530 return readl_poll_timeout(addr, val, cmp == val,
531 10 * USEC_PER_MSEC, waitms * USEC_PER_MSEC);
534 static u32 prestera_ldr_wait_buf(struct prestera_fw *fw, size_t len)
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;
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);
546 static int prestera_ldr_wait_dl_finish(struct prestera_fw *fw)
548 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_STATUS_REG);
549 unsigned long mask = ~(PRESTERA_LDR_STATUS_IMG_DL);
553 err = readl_poll_timeout(addr, val, val & mask, 10 * USEC_PER_MSEC,
554 PRESTERA_FW_DL_TIMEOUT_MS * USEC_PER_MSEC);
556 dev_err(fw->dev.dev, "Timeout to load FW img [state=%d]",
557 prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG));
564 static void prestera_ldr_wr_idx_move(struct prestera_fw *fw, unsigned int n)
566 fw->ldr_wr_idx = (fw->ldr_wr_idx + (n)) & (fw->ldr_buf_len - 1);
569 static void prestera_ldr_wr_idx_commit(struct prestera_fw *fw)
571 prestera_ldr_write(fw, PRESTERA_LDR_BUF_WR_REG, fw->ldr_wr_idx);
574 static u8 __iomem *prestera_ldr_wr_ptr(struct prestera_fw *fw)
576 return fw->ldr_ring_buf + fw->ldr_wr_idx;
579 static int prestera_ldr_send(struct prestera_fw *fw, const u8 *buf, size_t len)
584 err = prestera_ldr_wait_buf(fw, len);
586 dev_err(fw->dev.dev, "failed wait for sending firmware\n");
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);
595 prestera_ldr_wr_idx_commit(fw);
599 static int prestera_ldr_fw_send(struct prestera_fw *fw,
600 const char *img, u32 fw_size)
606 err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_STATUS_REG,
607 PRESTERA_LDR_STATUS_IMG_DL,
610 dev_err(fw->dev.dev, "Loader is not ready to load image\n");
614 for (pos = 0; pos < fw_size; pos += PRESTERA_FW_BLK_SZ) {
615 if (pos + PRESTERA_FW_BLK_SZ > fw_size)
618 err = prestera_ldr_send(fw, img + pos, PRESTERA_FW_BLK_SZ);
624 err = prestera_ldr_send(fw, img + pos, fw_size - pos);
629 err = prestera_ldr_wait_dl_finish(fw);
633 status = prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG);
636 case PRESTERA_LDR_STATUS_INVALID_IMG:
637 dev_err(fw->dev.dev, "FW img has bad CRC\n");
639 case PRESTERA_LDR_STATUS_NOMEM:
640 dev_err(fw->dev.dev, "Loader has no enough mem\n");
647 static void prestera_fw_rev_parse(const struct prestera_fw_header *hdr,
648 struct prestera_fw_rev *rev)
650 u32 version = be32_to_cpu(hdr->version_value);
652 rev->maj = PRESTERA_FW_VER_MAJ(version);
653 rev->min = PRESTERA_FW_VER_MIN(version);
654 rev->sub = PRESTERA_FW_VER_PATCH(version);
657 static int prestera_fw_rev_check(struct prestera_fw *fw)
659 struct prestera_fw_rev *rev = &fw->dev.fw_rev;
661 if (rev->maj == fw->rev_supp.maj && rev->min >= fw->rev_supp.min)
664 dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'",
665 fw->rev_supp.maj, fw->rev_supp.min);
670 static int prestera_fw_hdr_parse(struct prestera_fw *fw)
672 struct prestera_fw_rev *rev = &fw->dev.fw_rev;
673 struct prestera_fw_header *hdr;
676 hdr = (struct prestera_fw_header *)fw->bin->data;
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");
684 prestera_fw_rev_parse(hdr, rev);
686 dev_info(fw->dev.dev, "FW version '%u.%u.%u'\n",
687 rev->maj, rev->min, rev->sub);
689 return prestera_fw_rev_check(fw);
692 static int prestera_fw_get(struct prestera_fw *fw)
694 int ver_maj = PRESTERA_SUPP_FW_MAJ_VER;
695 int ver_min = PRESTERA_SUPP_FW_MIN_VER;
700 snprintf(fw_path, sizeof(fw_path), PRESTERA_FW_PATH_FMT,
703 err = reject_firmware_direct(&fw->bin, fw_path, fw->dev.dev);
705 if (ver_maj == PRESTERA_SUPP_FW_MAJ_VER) {
706 ver_maj = PRESTERA_PREV_FW_MAJ_VER;
707 ver_min = PRESTERA_PREV_FW_MIN_VER;
709 dev_warn(fw->dev.dev,
710 "missing latest %s firmware, fall-back to previous %u.%u version\n",
711 fw_path, ver_maj, ver_min);
715 dev_err(fw->dev.dev, "failed to request previous firmware: %s\n",
721 dev_info(fw->dev.dev, "Loading %s ...", fw_path);
723 fw->rev_supp.maj = ver_maj;
724 fw->rev_supp.min = ver_min;
725 fw->rev_supp.sub = 0;
730 static void prestera_fw_put(struct prestera_fw *fw)
732 release_firmware(fw->bin);
735 static int prestera_fw_load(struct prestera_fw *fw)
737 size_t hlen = sizeof(struct prestera_fw_header);
740 err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_READY_REG,
741 PRESTERA_LDR_READY_MAGIC,
744 dev_err(fw->dev.dev, "waiting for FW loader is timed out");
748 fw->ldr_ring_buf = fw->ldr_regs +
749 prestera_ldr_read(fw, PRESTERA_LDR_BUF_OFFS_REG);
752 prestera_ldr_read(fw, PRESTERA_LDR_BUF_SIZE_REG);
756 err = prestera_fw_get(fw);
760 err = prestera_fw_hdr_parse(fw);
762 dev_err(fw->dev.dev, "FW image header is invalid\n");
766 prestera_ldr_write(fw, PRESTERA_LDR_IMG_SIZE_REG, fw->bin->size - hlen);
767 prestera_ldr_write(fw, PRESTERA_LDR_CTL_REG, PRESTERA_LDR_CTL_DL_START);
769 err = prestera_ldr_fw_send(fw, fw->bin->data + hlen,
770 fw->bin->size - hlen);
777 static int prestera_pci_probe(struct pci_dev *pdev,
778 const struct pci_device_id *id)
780 const char *driver_name = dev_driver_string(&pdev->dev);
781 struct prestera_fw *fw;
784 err = pcim_enable_device(pdev);
788 err = pcim_iomap_regions(pdev, BIT(PRESTERA_PCI_BAR_FW) |
789 BIT(PRESTERA_PCI_BAR_PP),
794 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(30));
796 dev_err(&pdev->dev, "fail to set DMA mask\n");
800 pci_set_master(pdev);
802 fw = devm_kzalloc(&pdev->dev, sizeof(*fw), GFP_KERNEL);
805 goto err_pci_dev_alloc;
808 fw->dev.ctl_regs = pcim_iomap_table(pdev)[PRESTERA_PCI_BAR_FW];
809 fw->dev.pp_regs = pcim_iomap_table(pdev)[PRESTERA_PCI_BAR_PP];
810 fw->dev.dev = &pdev->dev;
812 pci_set_drvdata(pdev, fw);
814 err = prestera_fw_init(fw);
816 goto err_prestera_fw_init;
818 dev_info(fw->dev.dev, "Prestera FW is ready\n");
820 fw->wq = alloc_workqueue("prestera_fw_wq", WQ_HIGHPRI, 1);
826 INIT_WORK(&fw->evt_work, prestera_fw_evt_work_fn);
828 err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
830 dev_err(&pdev->dev, "MSI IRQ init failed\n");
834 err = request_irq(pci_irq_vector(pdev, 0), prestera_pci_irq_handler,
837 dev_err(&pdev->dev, "fail to request IRQ\n");
838 goto err_request_irq;
841 err = prestera_device_register(&fw->dev);
843 goto err_prestera_dev_register;
847 err_prestera_dev_register:
848 free_irq(pci_irq_vector(pdev, 0), fw);
850 pci_free_irq_vectors(pdev);
852 destroy_workqueue(fw->wq);
854 prestera_fw_uninit(fw);
855 err_prestera_fw_init:
861 static void prestera_pci_remove(struct pci_dev *pdev)
863 struct prestera_fw *fw = pci_get_drvdata(pdev);
865 prestera_device_unregister(&fw->dev);
866 free_irq(pci_irq_vector(pdev, 0), fw);
867 pci_free_irq_vectors(pdev);
868 destroy_workqueue(fw->wq);
869 prestera_fw_uninit(fw);
872 static const struct pci_device_id prestera_pci_devices[] = {
873 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC804) },
874 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC80C) },
877 MODULE_DEVICE_TABLE(pci, prestera_pci_devices);
879 static struct pci_driver prestera_pci_driver = {
880 .name = "Prestera DX",
881 .id_table = prestera_pci_devices,
882 .probe = prestera_pci_probe,
883 .remove = prestera_pci_remove,
885 module_pci_driver(prestera_pci_driver);
887 MODULE_LICENSE("Dual BSD/GPL");
888 MODULE_DESCRIPTION("Marvell Prestera switch PCI interface");