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