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