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