1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */ 3 4 #include <linux/printk.h> 5 #include <linux/dynamic_debug.h> 6 #include <linux/module.h> 7 #include <linux/netdevice.h> 8 #include <linux/utsname.h> 9 10 #include "ionic.h" 11 #include "ionic_bus.h" 12 #include "ionic_lif.h" 13 #include "ionic_debugfs.h" 14 15 MODULE_DESCRIPTION(IONIC_DRV_DESCRIPTION); 16 MODULE_AUTHOR("Pensando Systems, Inc"); 17 MODULE_LICENSE("GPL"); 18 MODULE_VERSION(IONIC_DRV_VERSION); 19 20 static const char *ionic_error_to_str(enum ionic_status_code code) 21 { 22 switch (code) { 23 case IONIC_RC_SUCCESS: 24 return "IONIC_RC_SUCCESS"; 25 case IONIC_RC_EVERSION: 26 return "IONIC_RC_EVERSION"; 27 case IONIC_RC_EOPCODE: 28 return "IONIC_RC_EOPCODE"; 29 case IONIC_RC_EIO: 30 return "IONIC_RC_EIO"; 31 case IONIC_RC_EPERM: 32 return "IONIC_RC_EPERM"; 33 case IONIC_RC_EQID: 34 return "IONIC_RC_EQID"; 35 case IONIC_RC_EQTYPE: 36 return "IONIC_RC_EQTYPE"; 37 case IONIC_RC_ENOENT: 38 return "IONIC_RC_ENOENT"; 39 case IONIC_RC_EINTR: 40 return "IONIC_RC_EINTR"; 41 case IONIC_RC_EAGAIN: 42 return "IONIC_RC_EAGAIN"; 43 case IONIC_RC_ENOMEM: 44 return "IONIC_RC_ENOMEM"; 45 case IONIC_RC_EFAULT: 46 return "IONIC_RC_EFAULT"; 47 case IONIC_RC_EBUSY: 48 return "IONIC_RC_EBUSY"; 49 case IONIC_RC_EEXIST: 50 return "IONIC_RC_EEXIST"; 51 case IONIC_RC_EINVAL: 52 return "IONIC_RC_EINVAL"; 53 case IONIC_RC_ENOSPC: 54 return "IONIC_RC_ENOSPC"; 55 case IONIC_RC_ERANGE: 56 return "IONIC_RC_ERANGE"; 57 case IONIC_RC_BAD_ADDR: 58 return "IONIC_RC_BAD_ADDR"; 59 case IONIC_RC_DEV_CMD: 60 return "IONIC_RC_DEV_CMD"; 61 case IONIC_RC_ERROR: 62 return "IONIC_RC_ERROR"; 63 case IONIC_RC_ERDMA: 64 return "IONIC_RC_ERDMA"; 65 default: 66 return "IONIC_RC_UNKNOWN"; 67 } 68 } 69 70 static int ionic_error_to_errno(enum ionic_status_code code) 71 { 72 switch (code) { 73 case IONIC_RC_SUCCESS: 74 return 0; 75 case IONIC_RC_EVERSION: 76 case IONIC_RC_EQTYPE: 77 case IONIC_RC_EQID: 78 case IONIC_RC_EINVAL: 79 return -EINVAL; 80 case IONIC_RC_EPERM: 81 return -EPERM; 82 case IONIC_RC_ENOENT: 83 return -ENOENT; 84 case IONIC_RC_EAGAIN: 85 return -EAGAIN; 86 case IONIC_RC_ENOMEM: 87 return -ENOMEM; 88 case IONIC_RC_EFAULT: 89 return -EFAULT; 90 case IONIC_RC_EBUSY: 91 return -EBUSY; 92 case IONIC_RC_EEXIST: 93 return -EEXIST; 94 case IONIC_RC_ENOSPC: 95 return -ENOSPC; 96 case IONIC_RC_ERANGE: 97 return -ERANGE; 98 case IONIC_RC_BAD_ADDR: 99 return -EFAULT; 100 case IONIC_RC_EOPCODE: 101 case IONIC_RC_EINTR: 102 case IONIC_RC_DEV_CMD: 103 case IONIC_RC_ERROR: 104 case IONIC_RC_ERDMA: 105 case IONIC_RC_EIO: 106 default: 107 return -EIO; 108 } 109 } 110 111 static const char *ionic_opcode_to_str(enum ionic_cmd_opcode opcode) 112 { 113 switch (opcode) { 114 case IONIC_CMD_NOP: 115 return "IONIC_CMD_NOP"; 116 case IONIC_CMD_INIT: 117 return "IONIC_CMD_INIT"; 118 case IONIC_CMD_RESET: 119 return "IONIC_CMD_RESET"; 120 case IONIC_CMD_IDENTIFY: 121 return "IONIC_CMD_IDENTIFY"; 122 case IONIC_CMD_GETATTR: 123 return "IONIC_CMD_GETATTR"; 124 case IONIC_CMD_SETATTR: 125 return "IONIC_CMD_SETATTR"; 126 case IONIC_CMD_PORT_IDENTIFY: 127 return "IONIC_CMD_PORT_IDENTIFY"; 128 case IONIC_CMD_PORT_INIT: 129 return "IONIC_CMD_PORT_INIT"; 130 case IONIC_CMD_PORT_RESET: 131 return "IONIC_CMD_PORT_RESET"; 132 case IONIC_CMD_PORT_GETATTR: 133 return "IONIC_CMD_PORT_GETATTR"; 134 case IONIC_CMD_PORT_SETATTR: 135 return "IONIC_CMD_PORT_SETATTR"; 136 case IONIC_CMD_LIF_INIT: 137 return "IONIC_CMD_LIF_INIT"; 138 case IONIC_CMD_LIF_RESET: 139 return "IONIC_CMD_LIF_RESET"; 140 case IONIC_CMD_LIF_IDENTIFY: 141 return "IONIC_CMD_LIF_IDENTIFY"; 142 case IONIC_CMD_LIF_SETATTR: 143 return "IONIC_CMD_LIF_SETATTR"; 144 case IONIC_CMD_LIF_GETATTR: 145 return "IONIC_CMD_LIF_GETATTR"; 146 case IONIC_CMD_RX_MODE_SET: 147 return "IONIC_CMD_RX_MODE_SET"; 148 case IONIC_CMD_RX_FILTER_ADD: 149 return "IONIC_CMD_RX_FILTER_ADD"; 150 case IONIC_CMD_RX_FILTER_DEL: 151 return "IONIC_CMD_RX_FILTER_DEL"; 152 case IONIC_CMD_Q_INIT: 153 return "IONIC_CMD_Q_INIT"; 154 case IONIC_CMD_Q_CONTROL: 155 return "IONIC_CMD_Q_CONTROL"; 156 case IONIC_CMD_RDMA_RESET_LIF: 157 return "IONIC_CMD_RDMA_RESET_LIF"; 158 case IONIC_CMD_RDMA_CREATE_EQ: 159 return "IONIC_CMD_RDMA_CREATE_EQ"; 160 case IONIC_CMD_RDMA_CREATE_CQ: 161 return "IONIC_CMD_RDMA_CREATE_CQ"; 162 case IONIC_CMD_RDMA_CREATE_ADMINQ: 163 return "IONIC_CMD_RDMA_CREATE_ADMINQ"; 164 case IONIC_CMD_FW_DOWNLOAD: 165 return "IONIC_CMD_FW_DOWNLOAD"; 166 case IONIC_CMD_FW_CONTROL: 167 return "IONIC_CMD_FW_CONTROL"; 168 default: 169 return "DEVCMD_UNKNOWN"; 170 } 171 } 172 173 static void ionic_adminq_flush(struct ionic_lif *lif) 174 { 175 struct ionic_queue *adminq = &lif->adminqcq->q; 176 177 spin_lock(&lif->adminq_lock); 178 179 while (adminq->tail != adminq->head) { 180 memset(adminq->tail->desc, 0, sizeof(union ionic_adminq_cmd)); 181 adminq->tail->cb = NULL; 182 adminq->tail->cb_arg = NULL; 183 adminq->tail = adminq->tail->next; 184 } 185 spin_unlock(&lif->adminq_lock); 186 } 187 188 static int ionic_adminq_check_err(struct ionic_lif *lif, 189 struct ionic_admin_ctx *ctx, 190 bool timeout) 191 { 192 struct net_device *netdev = lif->netdev; 193 const char *opcode_str; 194 const char *status_str; 195 int err = 0; 196 197 if (ctx->comp.comp.status || timeout) { 198 opcode_str = ionic_opcode_to_str(ctx->cmd.cmd.opcode); 199 status_str = ionic_error_to_str(ctx->comp.comp.status); 200 err = timeout ? -ETIMEDOUT : 201 ionic_error_to_errno(ctx->comp.comp.status); 202 203 netdev_err(netdev, "%s (%d) failed: %s (%d)\n", 204 opcode_str, ctx->cmd.cmd.opcode, 205 timeout ? "TIMEOUT" : status_str, err); 206 207 if (timeout) 208 ionic_adminq_flush(lif); 209 } 210 211 return err; 212 } 213 214 static void ionic_adminq_cb(struct ionic_queue *q, 215 struct ionic_desc_info *desc_info, 216 struct ionic_cq_info *cq_info, void *cb_arg) 217 { 218 struct ionic_admin_ctx *ctx = cb_arg; 219 struct ionic_admin_comp *comp; 220 struct device *dev; 221 222 if (!ctx) 223 return; 224 225 comp = cq_info->cq_desc; 226 dev = &q->lif->netdev->dev; 227 228 memcpy(&ctx->comp, comp, sizeof(*comp)); 229 230 dev_dbg(dev, "comp admin queue command:\n"); 231 dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1, 232 &ctx->comp, sizeof(ctx->comp), true); 233 234 complete_all(&ctx->work); 235 } 236 237 static int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx) 238 { 239 struct ionic_queue *adminq = &lif->adminqcq->q; 240 int err = 0; 241 242 WARN_ON(in_interrupt()); 243 244 spin_lock(&lif->adminq_lock); 245 if (!ionic_q_has_space(adminq, 1)) { 246 err = -ENOSPC; 247 goto err_out; 248 } 249 250 err = ionic_heartbeat_check(lif->ionic); 251 if (err) 252 goto err_out; 253 254 memcpy(adminq->head->desc, &ctx->cmd, sizeof(ctx->cmd)); 255 256 dev_dbg(&lif->netdev->dev, "post admin queue command:\n"); 257 dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1, 258 &ctx->cmd, sizeof(ctx->cmd), true); 259 260 ionic_q_post(adminq, true, ionic_adminq_cb, ctx); 261 262 err_out: 263 spin_unlock(&lif->adminq_lock); 264 265 return err; 266 } 267 268 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx) 269 { 270 struct net_device *netdev = lif->netdev; 271 unsigned long remaining; 272 const char *name; 273 int err; 274 275 err = ionic_adminq_post(lif, ctx); 276 if (err) { 277 name = ionic_opcode_to_str(ctx->cmd.cmd.opcode); 278 netdev_err(netdev, "Posting of %s (%d) failed: %d\n", 279 name, ctx->cmd.cmd.opcode, err); 280 return err; 281 } 282 283 remaining = wait_for_completion_timeout(&ctx->work, 284 HZ * (ulong)DEVCMD_TIMEOUT); 285 return ionic_adminq_check_err(lif, ctx, (remaining == 0)); 286 } 287 288 int ionic_napi(struct napi_struct *napi, int budget, ionic_cq_cb cb, 289 ionic_cq_done_cb done_cb, void *done_arg) 290 { 291 struct ionic_qcq *qcq = napi_to_qcq(napi); 292 struct ionic_cq *cq = &qcq->cq; 293 u32 work_done, flags = 0; 294 295 work_done = ionic_cq_service(cq, budget, cb, done_cb, done_arg); 296 297 if (work_done < budget && napi_complete_done(napi, work_done)) { 298 flags |= IONIC_INTR_CRED_UNMASK; 299 DEBUG_STATS_INTR_REARM(cq->bound_intr); 300 } 301 302 if (work_done || flags) { 303 flags |= IONIC_INTR_CRED_RESET_COALESCE; 304 ionic_intr_credits(cq->lif->ionic->idev.intr_ctrl, 305 cq->bound_intr->index, 306 work_done, flags); 307 } 308 309 DEBUG_STATS_NAPI_POLL(qcq, work_done); 310 311 return work_done; 312 } 313 314 static void ionic_dev_cmd_clean(struct ionic *ionic) 315 { 316 union ionic_dev_cmd_regs *regs = ionic->idev.dev_cmd_regs; 317 318 iowrite32(0, ®s->doorbell); 319 memset_io(®s->cmd, 0, sizeof(regs->cmd)); 320 } 321 322 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds) 323 { 324 struct ionic_dev *idev = &ionic->idev; 325 unsigned long start_time; 326 unsigned long max_wait; 327 unsigned long duration; 328 int opcode; 329 int done; 330 int err; 331 int hb; 332 333 WARN_ON(in_interrupt()); 334 335 /* Wait for dev cmd to complete, retrying if we get EAGAIN, 336 * but don't wait any longer than max_seconds. 337 */ 338 max_wait = jiffies + (max_seconds * HZ); 339 try_again: 340 start_time = jiffies; 341 do { 342 done = ionic_dev_cmd_done(idev); 343 if (done) 344 break; 345 msleep(20); 346 hb = ionic_heartbeat_check(ionic); 347 } while (!done && !hb && time_before(jiffies, max_wait)); 348 duration = jiffies - start_time; 349 350 opcode = idev->dev_cmd_regs->cmd.cmd.opcode; 351 dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n", 352 ionic_opcode_to_str(opcode), opcode, 353 done, duration / HZ, duration); 354 355 if (!done && hb) { 356 ionic_dev_cmd_clean(ionic); 357 dev_warn(ionic->dev, "DEVCMD %s (%d) failed - FW halted\n", 358 ionic_opcode_to_str(opcode), opcode); 359 return -ENXIO; 360 } 361 362 if (!done && !time_before(jiffies, max_wait)) { 363 ionic_dev_cmd_clean(ionic); 364 dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n", 365 ionic_opcode_to_str(opcode), opcode, max_seconds); 366 return -ETIMEDOUT; 367 } 368 369 err = ionic_dev_cmd_status(&ionic->idev); 370 if (err) { 371 if (err == IONIC_RC_EAGAIN && !time_after(jiffies, max_wait)) { 372 dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) retrying...\n", 373 ionic_opcode_to_str(opcode), opcode, 374 ionic_error_to_str(err), err); 375 376 msleep(1000); 377 iowrite32(0, &idev->dev_cmd_regs->done); 378 iowrite32(1, &idev->dev_cmd_regs->doorbell); 379 goto try_again; 380 } 381 382 dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n", 383 ionic_opcode_to_str(opcode), opcode, 384 ionic_error_to_str(err), err); 385 386 return ionic_error_to_errno(err); 387 } 388 389 return 0; 390 } 391 392 int ionic_setup(struct ionic *ionic) 393 { 394 int err; 395 396 err = ionic_dev_setup(ionic); 397 if (err) 398 return err; 399 400 return 0; 401 } 402 403 int ionic_identify(struct ionic *ionic) 404 { 405 struct ionic_identity *ident = &ionic->ident; 406 struct ionic_dev *idev = &ionic->idev; 407 size_t sz; 408 int err; 409 410 memset(ident, 0, sizeof(*ident)); 411 412 ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX); 413 strncpy(ident->drv.driver_ver_str, IONIC_DRV_VERSION, 414 sizeof(ident->drv.driver_ver_str) - 1); 415 416 mutex_lock(&ionic->dev_cmd_lock); 417 418 sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data)); 419 memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz); 420 421 ionic_dev_cmd_identify(idev, IONIC_IDENTITY_VERSION_1); 422 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); 423 if (!err) { 424 sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data)); 425 memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz); 426 } 427 428 mutex_unlock(&ionic->dev_cmd_lock); 429 430 if (err) 431 goto err_out_unmap; 432 433 ionic_debugfs_add_ident(ionic); 434 435 return 0; 436 437 err_out_unmap: 438 return err; 439 } 440 441 int ionic_init(struct ionic *ionic) 442 { 443 struct ionic_dev *idev = &ionic->idev; 444 int err; 445 446 mutex_lock(&ionic->dev_cmd_lock); 447 ionic_dev_cmd_init(idev); 448 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); 449 mutex_unlock(&ionic->dev_cmd_lock); 450 451 return err; 452 } 453 454 int ionic_reset(struct ionic *ionic) 455 { 456 struct ionic_dev *idev = &ionic->idev; 457 int err; 458 459 mutex_lock(&ionic->dev_cmd_lock); 460 ionic_dev_cmd_reset(idev); 461 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); 462 mutex_unlock(&ionic->dev_cmd_lock); 463 464 return err; 465 } 466 467 int ionic_port_identify(struct ionic *ionic) 468 { 469 struct ionic_identity *ident = &ionic->ident; 470 struct ionic_dev *idev = &ionic->idev; 471 size_t sz; 472 int err; 473 474 mutex_lock(&ionic->dev_cmd_lock); 475 476 ionic_dev_cmd_port_identify(idev); 477 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); 478 if (!err) { 479 sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data)); 480 memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz); 481 } 482 483 mutex_unlock(&ionic->dev_cmd_lock); 484 485 return err; 486 } 487 488 int ionic_port_init(struct ionic *ionic) 489 { 490 struct ionic_identity *ident = &ionic->ident; 491 struct ionic_dev *idev = &ionic->idev; 492 size_t sz; 493 int err; 494 495 if (idev->port_info) 496 return 0; 497 498 idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE); 499 idev->port_info = dma_alloc_coherent(ionic->dev, idev->port_info_sz, 500 &idev->port_info_pa, 501 GFP_KERNEL); 502 if (!idev->port_info) { 503 dev_err(ionic->dev, "Failed to allocate port info, aborting\n"); 504 return -ENOMEM; 505 } 506 507 sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data)); 508 509 mutex_lock(&ionic->dev_cmd_lock); 510 511 memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz); 512 ionic_dev_cmd_port_init(idev); 513 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); 514 515 ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP); 516 (void)ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); 517 518 mutex_unlock(&ionic->dev_cmd_lock); 519 if (err) { 520 dev_err(ionic->dev, "Failed to init port\n"); 521 dma_free_coherent(ionic->dev, idev->port_info_sz, 522 idev->port_info, idev->port_info_pa); 523 idev->port_info = NULL; 524 idev->port_info_pa = 0; 525 } 526 527 return err; 528 } 529 530 int ionic_port_reset(struct ionic *ionic) 531 { 532 struct ionic_dev *idev = &ionic->idev; 533 int err; 534 535 if (!idev->port_info) 536 return 0; 537 538 mutex_lock(&ionic->dev_cmd_lock); 539 ionic_dev_cmd_port_reset(idev); 540 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); 541 mutex_unlock(&ionic->dev_cmd_lock); 542 543 dma_free_coherent(ionic->dev, idev->port_info_sz, 544 idev->port_info, idev->port_info_pa); 545 546 idev->port_info = NULL; 547 idev->port_info_pa = 0; 548 549 if (err) 550 dev_err(ionic->dev, "Failed to reset port\n"); 551 552 return err; 553 } 554 555 static int __init ionic_init_module(void) 556 { 557 pr_info("%s %s, ver %s\n", 558 IONIC_DRV_NAME, IONIC_DRV_DESCRIPTION, IONIC_DRV_VERSION); 559 ionic_debugfs_create(); 560 return ionic_bus_register_driver(); 561 } 562 563 static void __exit ionic_cleanup_module(void) 564 { 565 ionic_bus_unregister_driver(); 566 ionic_debugfs_destroy(); 567 568 pr_info("%s removed\n", IONIC_DRV_NAME); 569 } 570 571 module_init(ionic_init_module); 572 module_exit(ionic_cleanup_module); 573