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 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 226 static u32 prestera_fw_read(struct prestera_fw *fw, u32 reg) 227 { 228 return readl(PRESTERA_FW_REG_ADDR(fw, reg)); 229 } 230 231 static u32 prestera_fw_evtq_len(struct prestera_fw *fw, u8 qid) 232 { 233 return fw->evt_queue[qid].len; 234 } 235 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 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 252 static u8 __iomem *prestera_fw_evtq_buf(struct prestera_fw *fw, u8 qid) 253 { 254 return fw->evt_queue[qid].addr; 255 } 256 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 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 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 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 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 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 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 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 360 static u32 prestera_fw_cmdq_len(struct prestera_fw *fw, u8 qid) 361 { 362 return fw->cmd_queue[qid].len; 363 } 364 365 static u8 __iomem *prestera_fw_cmdq_buf(struct prestera_fw *fw, u8 qid) 366 { 367 return fw->cmd_queue[qid].addr; 368 } 369 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, prestera_fw_cmdq_buf(fw, qid) + in_size, ret_size); 415 416 cmd_exit: 417 prestera_fw_write(fw, PRESTERA_CMDQ_REQ_CTL_REG(qid), 418 PRESTERA_CMD_F_REPL_RCVD); 419 return err; 420 } 421 422 static int prestera_fw_send_req(struct prestera_device *dev, int qid, 423 void *in_msg, size_t in_size, void *out_msg, 424 size_t out_size, unsigned int waitms) 425 { 426 struct prestera_fw *fw; 427 ssize_t ret; 428 429 fw = container_of(dev, struct prestera_fw, dev); 430 431 prestera_fw_cmdq_lock(fw, qid); 432 ret = prestera_fw_cmd_send(fw, qid, in_msg, in_size, out_msg, out_size, 433 waitms); 434 prestera_fw_cmdq_unlock(fw, qid); 435 436 return ret; 437 } 438 439 static int prestera_fw_init(struct prestera_fw *fw) 440 { 441 u8 __iomem *base; 442 int err; 443 u8 qid; 444 445 fw->dev.send_req = prestera_fw_send_req; 446 fw->ldr_regs = fw->dev.ctl_regs; 447 448 err = prestera_fw_load(fw); 449 if (err) 450 return err; 451 452 err = prestera_fw_wait_reg32(fw, PRESTERA_FW_READY_REG, 453 PRESTERA_FW_READY_MAGIC, 454 PRESTERA_FW_READY_WAIT_MS); 455 if (err) { 456 dev_err(fw->dev.dev, "FW failed to start\n"); 457 return err; 458 } 459 460 base = fw->dev.ctl_regs; 461 462 fw->cmd_mbox = base + prestera_fw_read(fw, PRESTERA_CMD_BUF_OFFS_REG); 463 fw->cmd_mbox_len = prestera_fw_read(fw, PRESTERA_CMD_BUF_LEN_REG); 464 fw->cmd_qnum = prestera_fw_read(fw, PRESTERA_CMD_QNUM_REG); 465 466 for (qid = 0; qid < fw->cmd_qnum; qid++) { 467 u32 offs = prestera_fw_read(fw, PRESTERA_CMDQ_OFFS_REG(qid)); 468 struct prestera_fw_cmdq *cmdq = &fw->cmd_queue[qid]; 469 470 cmdq->len = prestera_fw_read(fw, PRESTERA_CMDQ_LEN_REG(qid)); 471 cmdq->addr = fw->cmd_mbox + offs; 472 mutex_init(&cmdq->cmd_mtx); 473 } 474 475 fw->evt_buf = base + prestera_fw_read(fw, PRESTERA_EVT_BUF_OFFS_REG); 476 fw->evt_qnum = prestera_fw_read(fw, PRESTERA_EVT_QNUM_REG); 477 fw->evt_msg = kmalloc(PRESTERA_MSG_MAX_SIZE, GFP_KERNEL); 478 if (!fw->evt_msg) 479 return -ENOMEM; 480 481 for (qid = 0; qid < fw->evt_qnum; qid++) { 482 u32 offs = prestera_fw_read(fw, PRESTERA_EVTQ_OFFS_REG(qid)); 483 struct prestera_fw_evtq *evtq = &fw->evt_queue[qid]; 484 485 evtq->len = prestera_fw_read(fw, PRESTERA_EVTQ_LEN_REG(qid)); 486 evtq->addr = fw->evt_buf + offs; 487 } 488 489 return 0; 490 } 491 492 static void prestera_fw_uninit(struct prestera_fw *fw) 493 { 494 kfree(fw->evt_msg); 495 } 496 497 static irqreturn_t prestera_pci_irq_handler(int irq, void *dev_id) 498 { 499 struct prestera_fw *fw = dev_id; 500 501 if (prestera_fw_read(fw, PRESTERA_RX_STATUS_REG)) { 502 prestera_fw_write(fw, PRESTERA_RX_STATUS_REG, 0); 503 504 if (fw->dev.recv_pkt) 505 fw->dev.recv_pkt(&fw->dev); 506 } 507 508 queue_work(fw->wq, &fw->evt_work); 509 510 return IRQ_HANDLED; 511 } 512 513 static void prestera_ldr_write(struct prestera_fw *fw, u32 reg, u32 val) 514 { 515 writel(val, PRESTERA_LDR_REG_ADDR(fw, reg)); 516 } 517 518 static u32 prestera_ldr_read(struct prestera_fw *fw, u32 reg) 519 { 520 return readl(PRESTERA_LDR_REG_ADDR(fw, reg)); 521 } 522 523 static int prestera_ldr_wait_reg32(struct prestera_fw *fw, 524 u32 reg, u32 cmp, unsigned int waitms) 525 { 526 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, reg); 527 u32 val; 528 529 return readl_poll_timeout(addr, val, cmp == val, 530 10 * USEC_PER_MSEC, waitms * USEC_PER_MSEC); 531 } 532 533 static u32 prestera_ldr_wait_buf(struct prestera_fw *fw, size_t len) 534 { 535 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_BUF_RD_REG); 536 u32 buf_len = fw->ldr_buf_len; 537 u32 wr_idx = fw->ldr_wr_idx; 538 u32 rd_idx; 539 540 return readl_poll_timeout(addr, rd_idx, 541 CIRC_SPACE(wr_idx, rd_idx, buf_len) >= len, 542 1 * USEC_PER_MSEC, 100 * USEC_PER_MSEC); 543 } 544 545 static int prestera_ldr_wait_dl_finish(struct prestera_fw *fw) 546 { 547 u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_STATUS_REG); 548 unsigned long mask = ~(PRESTERA_LDR_STATUS_IMG_DL); 549 u32 val; 550 int err; 551 552 err = readl_poll_timeout(addr, val, val & mask, 10 * USEC_PER_MSEC, 553 PRESTERA_FW_DL_TIMEOUT_MS * USEC_PER_MSEC); 554 if (err) { 555 dev_err(fw->dev.dev, "Timeout to load FW img [state=%d]", 556 prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG)); 557 return err; 558 } 559 560 return 0; 561 } 562 563 static void prestera_ldr_wr_idx_move(struct prestera_fw *fw, unsigned int n) 564 { 565 fw->ldr_wr_idx = (fw->ldr_wr_idx + (n)) & (fw->ldr_buf_len - 1); 566 } 567 568 static void prestera_ldr_wr_idx_commit(struct prestera_fw *fw) 569 { 570 prestera_ldr_write(fw, PRESTERA_LDR_BUF_WR_REG, fw->ldr_wr_idx); 571 } 572 573 static u8 __iomem *prestera_ldr_wr_ptr(struct prestera_fw *fw) 574 { 575 return fw->ldr_ring_buf + fw->ldr_wr_idx; 576 } 577 578 static int prestera_ldr_send(struct prestera_fw *fw, const u8 *buf, size_t len) 579 { 580 int err; 581 int i; 582 583 err = prestera_ldr_wait_buf(fw, len); 584 if (err) { 585 dev_err(fw->dev.dev, "failed wait for sending firmware\n"); 586 return err; 587 } 588 589 for (i = 0; i < len; i += 4) { 590 writel_relaxed(*(u32 *)(buf + i), prestera_ldr_wr_ptr(fw)); 591 prestera_ldr_wr_idx_move(fw, 4); 592 } 593 594 prestera_ldr_wr_idx_commit(fw); 595 return 0; 596 } 597 598 static int prestera_ldr_fw_send(struct prestera_fw *fw, 599 const char *img, u32 fw_size) 600 { 601 u32 status; 602 u32 pos; 603 int err; 604 605 err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_STATUS_REG, 606 PRESTERA_LDR_STATUS_IMG_DL, 607 5 * MSEC_PER_SEC); 608 if (err) { 609 dev_err(fw->dev.dev, "Loader is not ready to load image\n"); 610 return err; 611 } 612 613 for (pos = 0; pos < fw_size; pos += PRESTERA_FW_BLK_SZ) { 614 if (pos + PRESTERA_FW_BLK_SZ > fw_size) 615 break; 616 617 err = prestera_ldr_send(fw, img + pos, PRESTERA_FW_BLK_SZ); 618 if (err) 619 return err; 620 } 621 622 if (pos < fw_size) { 623 err = prestera_ldr_send(fw, img + pos, fw_size - pos); 624 if (err) 625 return err; 626 } 627 628 err = prestera_ldr_wait_dl_finish(fw); 629 if (err) 630 return err; 631 632 status = prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG); 633 634 switch (status) { 635 case PRESTERA_LDR_STATUS_INVALID_IMG: 636 dev_err(fw->dev.dev, "FW img has bad CRC\n"); 637 return -EINVAL; 638 case PRESTERA_LDR_STATUS_NOMEM: 639 dev_err(fw->dev.dev, "Loader has no enough mem\n"); 640 return -ENOMEM; 641 } 642 643 return 0; 644 } 645 646 static void prestera_fw_rev_parse(const struct prestera_fw_header *hdr, 647 struct prestera_fw_rev *rev) 648 { 649 u32 version = be32_to_cpu(hdr->version_value); 650 651 rev->maj = PRESTERA_FW_VER_MAJ(version); 652 rev->min = PRESTERA_FW_VER_MIN(version); 653 rev->sub = PRESTERA_FW_VER_PATCH(version); 654 } 655 656 static int prestera_fw_rev_check(struct prestera_fw *fw) 657 { 658 struct prestera_fw_rev *rev = &fw->dev.fw_rev; 659 660 if (rev->maj == fw->rev_supp.maj && rev->min >= fw->rev_supp.min) 661 return 0; 662 663 dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'", 664 fw->rev_supp.maj, fw->rev_supp.min); 665 666 return -EINVAL; 667 } 668 669 static int prestera_fw_hdr_parse(struct prestera_fw *fw) 670 { 671 struct prestera_fw_rev *rev = &fw->dev.fw_rev; 672 struct prestera_fw_header *hdr; 673 u32 magic; 674 675 hdr = (struct prestera_fw_header *)fw->bin->data; 676 677 magic = be32_to_cpu(hdr->magic_number); 678 if (magic != PRESTERA_FW_HDR_MAGIC) { 679 dev_err(fw->dev.dev, "FW img hdr magic is invalid"); 680 return -EINVAL; 681 } 682 683 prestera_fw_rev_parse(hdr, rev); 684 685 dev_info(fw->dev.dev, "FW version '%u.%u.%u'\n", 686 rev->maj, rev->min, rev->sub); 687 688 return prestera_fw_rev_check(fw); 689 } 690 691 static int prestera_fw_get(struct prestera_fw *fw) 692 { 693 int ver_maj = PRESTERA_SUPP_FW_MAJ_VER; 694 int ver_min = PRESTERA_SUPP_FW_MIN_VER; 695 char fw_path[128]; 696 int err; 697 698 pick_fw_ver: 699 snprintf(fw_path, sizeof(fw_path), PRESTERA_FW_PATH_FMT, 700 ver_maj, ver_min); 701 702 err = request_firmware_direct(&fw->bin, fw_path, fw->dev.dev); 703 if (err) { 704 if (ver_maj == PRESTERA_SUPP_FW_MAJ_VER) { 705 ver_maj = PRESTERA_PREV_FW_MAJ_VER; 706 ver_min = PRESTERA_PREV_FW_MIN_VER; 707 708 dev_warn(fw->dev.dev, 709 "missing latest %s firmware, fall-back to previous %u.%u version\n", 710 fw_path, ver_maj, ver_min); 711 712 goto pick_fw_ver; 713 } else { 714 dev_err(fw->dev.dev, "failed to request previous firmware: %s\n", 715 fw_path); 716 return err; 717 } 718 } 719 720 dev_info(fw->dev.dev, "Loading %s ...", fw_path); 721 722 fw->rev_supp.maj = ver_maj; 723 fw->rev_supp.min = ver_min; 724 fw->rev_supp.sub = 0; 725 726 return 0; 727 } 728 729 static void prestera_fw_put(struct prestera_fw *fw) 730 { 731 release_firmware(fw->bin); 732 } 733 734 static int prestera_fw_load(struct prestera_fw *fw) 735 { 736 size_t hlen = sizeof(struct prestera_fw_header); 737 int err; 738 739 err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_READY_REG, 740 PRESTERA_LDR_READY_MAGIC, 741 5 * MSEC_PER_SEC); 742 if (err) { 743 dev_err(fw->dev.dev, "waiting for FW loader is timed out"); 744 return err; 745 } 746 747 fw->ldr_ring_buf = fw->ldr_regs + 748 prestera_ldr_read(fw, PRESTERA_LDR_BUF_OFFS_REG); 749 750 fw->ldr_buf_len = 751 prestera_ldr_read(fw, PRESTERA_LDR_BUF_SIZE_REG); 752 753 fw->ldr_wr_idx = 0; 754 755 err = prestera_fw_get(fw); 756 if (err) 757 return err; 758 759 err = prestera_fw_hdr_parse(fw); 760 if (err) { 761 dev_err(fw->dev.dev, "FW image header is invalid\n"); 762 goto out_release; 763 } 764 765 prestera_ldr_write(fw, PRESTERA_LDR_IMG_SIZE_REG, fw->bin->size - hlen); 766 prestera_ldr_write(fw, PRESTERA_LDR_CTL_REG, PRESTERA_LDR_CTL_DL_START); 767 768 err = prestera_ldr_fw_send(fw, fw->bin->data + hlen, 769 fw->bin->size - hlen); 770 771 out_release: 772 prestera_fw_put(fw); 773 return err; 774 } 775 776 static int prestera_pci_probe(struct pci_dev *pdev, 777 const struct pci_device_id *id) 778 { 779 const char *driver_name = pdev->driver->name; 780 struct prestera_fw *fw; 781 int err; 782 783 err = pcim_enable_device(pdev); 784 if (err) 785 return err; 786 787 err = pcim_iomap_regions(pdev, BIT(PRESTERA_PCI_BAR_FW) | 788 BIT(PRESTERA_PCI_BAR_PP), 789 pci_name(pdev)); 790 if (err) 791 return err; 792 793 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(30)); 794 if (err) { 795 dev_err(&pdev->dev, "fail to set DMA mask\n"); 796 goto err_dma_mask; 797 } 798 799 pci_set_master(pdev); 800 801 fw = devm_kzalloc(&pdev->dev, sizeof(*fw), GFP_KERNEL); 802 if (!fw) { 803 err = -ENOMEM; 804 goto err_pci_dev_alloc; 805 } 806 807 fw->dev.ctl_regs = pcim_iomap_table(pdev)[PRESTERA_PCI_BAR_FW]; 808 fw->dev.pp_regs = pcim_iomap_table(pdev)[PRESTERA_PCI_BAR_PP]; 809 fw->dev.dev = &pdev->dev; 810 811 pci_set_drvdata(pdev, fw); 812 813 err = prestera_fw_init(fw); 814 if (err) 815 goto err_prestera_fw_init; 816 817 dev_info(fw->dev.dev, "Prestera FW is ready\n"); 818 819 fw->wq = alloc_workqueue("prestera_fw_wq", WQ_HIGHPRI, 1); 820 if (!fw->wq) { 821 err = -ENOMEM; 822 goto err_wq_alloc; 823 } 824 825 INIT_WORK(&fw->evt_work, prestera_fw_evt_work_fn); 826 827 err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI); 828 if (err < 0) { 829 dev_err(&pdev->dev, "MSI IRQ init failed\n"); 830 goto err_irq_alloc; 831 } 832 833 err = request_irq(pci_irq_vector(pdev, 0), prestera_pci_irq_handler, 834 0, driver_name, fw); 835 if (err) { 836 dev_err(&pdev->dev, "fail to request IRQ\n"); 837 goto err_request_irq; 838 } 839 840 err = prestera_device_register(&fw->dev); 841 if (err) 842 goto err_prestera_dev_register; 843 844 return 0; 845 846 err_prestera_dev_register: 847 free_irq(pci_irq_vector(pdev, 0), fw); 848 err_request_irq: 849 pci_free_irq_vectors(pdev); 850 err_irq_alloc: 851 destroy_workqueue(fw->wq); 852 err_wq_alloc: 853 prestera_fw_uninit(fw); 854 err_prestera_fw_init: 855 err_pci_dev_alloc: 856 err_dma_mask: 857 return err; 858 } 859 860 static void prestera_pci_remove(struct pci_dev *pdev) 861 { 862 struct prestera_fw *fw = pci_get_drvdata(pdev); 863 864 prestera_device_unregister(&fw->dev); 865 free_irq(pci_irq_vector(pdev, 0), fw); 866 pci_free_irq_vectors(pdev); 867 destroy_workqueue(fw->wq); 868 prestera_fw_uninit(fw); 869 } 870 871 static const struct pci_device_id prestera_pci_devices[] = { 872 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC804) }, 873 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC80C) }, 874 { } 875 }; 876 MODULE_DEVICE_TABLE(pci, prestera_pci_devices); 877 878 static struct pci_driver prestera_pci_driver = { 879 .name = "Prestera DX", 880 .id_table = prestera_pci_devices, 881 .probe = prestera_pci_probe, 882 .remove = prestera_pci_remove, 883 }; 884 module_pci_driver(prestera_pci_driver); 885 886 MODULE_LICENSE("Dual BSD/GPL"); 887 MODULE_DESCRIPTION("Marvell Prestera switch PCI interface"); 888