1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
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 #include <generated/utsrelease.h>
10 #include <linux/ctype.h>
13 #include "ionic_bus.h"
14 #include "ionic_lif.h"
15 #include "ionic_debugfs.h"
17 MODULE_DESCRIPTION(IONIC_DRV_DESCRIPTION);
18 MODULE_AUTHOR("Pensando Systems, Inc");
19 MODULE_LICENSE("GPL");
21 static const char *ionic_error_to_str(enum ionic_status_code code)
24 case IONIC_RC_SUCCESS:
25 return "IONIC_RC_SUCCESS";
26 case IONIC_RC_EVERSION:
27 return "IONIC_RC_EVERSION";
28 case IONIC_RC_EOPCODE:
29 return "IONIC_RC_EOPCODE";
31 return "IONIC_RC_EIO";
33 return "IONIC_RC_EPERM";
35 return "IONIC_RC_EQID";
37 return "IONIC_RC_EQTYPE";
39 return "IONIC_RC_ENOENT";
41 return "IONIC_RC_EINTR";
43 return "IONIC_RC_EAGAIN";
45 return "IONIC_RC_ENOMEM";
47 return "IONIC_RC_EFAULT";
49 return "IONIC_RC_EBUSY";
51 return "IONIC_RC_EEXIST";
53 return "IONIC_RC_EINVAL";
55 return "IONIC_RC_ENOSPC";
57 return "IONIC_RC_ERANGE";
58 case IONIC_RC_BAD_ADDR:
59 return "IONIC_RC_BAD_ADDR";
60 case IONIC_RC_DEV_CMD:
61 return "IONIC_RC_DEV_CMD";
62 case IONIC_RC_ENOSUPP:
63 return "IONIC_RC_ENOSUPP";
65 return "IONIC_RC_ERROR";
67 return "IONIC_RC_ERDMA";
68 case IONIC_RC_EBAD_FW:
69 return "IONIC_RC_EBAD_FW";
71 return "IONIC_RC_UNKNOWN";
75 static int ionic_error_to_errno(enum ionic_status_code code)
78 case IONIC_RC_SUCCESS:
80 case IONIC_RC_EVERSION:
84 case IONIC_RC_ENOSUPP:
100 case IONIC_RC_ENOSPC:
102 case IONIC_RC_ERANGE:
104 case IONIC_RC_BAD_ADDR:
106 case IONIC_RC_EOPCODE:
108 case IONIC_RC_DEV_CMD:
117 static const char *ionic_opcode_to_str(enum ionic_cmd_opcode opcode)
121 return "IONIC_CMD_NOP";
123 return "IONIC_CMD_INIT";
124 case IONIC_CMD_RESET:
125 return "IONIC_CMD_RESET";
126 case IONIC_CMD_IDENTIFY:
127 return "IONIC_CMD_IDENTIFY";
128 case IONIC_CMD_GETATTR:
129 return "IONIC_CMD_GETATTR";
130 case IONIC_CMD_SETATTR:
131 return "IONIC_CMD_SETATTR";
132 case IONIC_CMD_PORT_IDENTIFY:
133 return "IONIC_CMD_PORT_IDENTIFY";
134 case IONIC_CMD_PORT_INIT:
135 return "IONIC_CMD_PORT_INIT";
136 case IONIC_CMD_PORT_RESET:
137 return "IONIC_CMD_PORT_RESET";
138 case IONIC_CMD_PORT_GETATTR:
139 return "IONIC_CMD_PORT_GETATTR";
140 case IONIC_CMD_PORT_SETATTR:
141 return "IONIC_CMD_PORT_SETATTR";
142 case IONIC_CMD_LIF_INIT:
143 return "IONIC_CMD_LIF_INIT";
144 case IONIC_CMD_LIF_RESET:
145 return "IONIC_CMD_LIF_RESET";
146 case IONIC_CMD_LIF_IDENTIFY:
147 return "IONIC_CMD_LIF_IDENTIFY";
148 case IONIC_CMD_LIF_SETATTR:
149 return "IONIC_CMD_LIF_SETATTR";
150 case IONIC_CMD_LIF_GETATTR:
151 return "IONIC_CMD_LIF_GETATTR";
152 case IONIC_CMD_LIF_SETPHC:
153 return "IONIC_CMD_LIF_SETPHC";
154 case IONIC_CMD_RX_MODE_SET:
155 return "IONIC_CMD_RX_MODE_SET";
156 case IONIC_CMD_RX_FILTER_ADD:
157 return "IONIC_CMD_RX_FILTER_ADD";
158 case IONIC_CMD_RX_FILTER_DEL:
159 return "IONIC_CMD_RX_FILTER_DEL";
160 case IONIC_CMD_Q_IDENTIFY:
161 return "IONIC_CMD_Q_IDENTIFY";
162 case IONIC_CMD_Q_INIT:
163 return "IONIC_CMD_Q_INIT";
164 case IONIC_CMD_Q_CONTROL:
165 return "IONIC_CMD_Q_CONTROL";
166 case IONIC_CMD_RDMA_RESET_LIF:
167 return "IONIC_CMD_RDMA_RESET_LIF";
168 case IONIC_CMD_RDMA_CREATE_EQ:
169 return "IONIC_CMD_RDMA_CREATE_EQ";
170 case IONIC_CMD_RDMA_CREATE_CQ:
171 return "IONIC_CMD_RDMA_CREATE_CQ";
172 case IONIC_CMD_RDMA_CREATE_ADMINQ:
173 return "IONIC_CMD_RDMA_CREATE_ADMINQ";
174 case IONIC_CMD_FW_DOWNLOAD:
175 return "IONIC_CMD_FW_DOWNLOAD";
176 case IONIC_CMD_FW_CONTROL:
177 return "IONIC_CMD_FW_CONTROL";
178 case IONIC_CMD_FW_DOWNLOAD_V1:
179 return "IONIC_CMD_FW_DOWNLOAD_V1";
180 case IONIC_CMD_FW_CONTROL_V1:
181 return "IONIC_CMD_FW_CONTROL_V1";
182 case IONIC_CMD_VF_GETATTR:
183 return "IONIC_CMD_VF_GETATTR";
184 case IONIC_CMD_VF_SETATTR:
185 return "IONIC_CMD_VF_SETATTR";
187 return "DEVCMD_UNKNOWN";
191 static void ionic_adminq_flush(struct ionic_lif *lif)
193 struct ionic_desc_info *desc_info;
194 unsigned long irqflags;
195 struct ionic_queue *q;
197 spin_lock_irqsave(&lif->adminq_lock, irqflags);
198 if (!lif->adminqcq) {
199 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
203 q = &lif->adminqcq->q;
205 while (q->tail_idx != q->head_idx) {
206 desc_info = &q->info[q->tail_idx];
207 memset(desc_info->desc, 0, sizeof(union ionic_adminq_cmd));
208 desc_info->cb = NULL;
209 desc_info->cb_arg = NULL;
210 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
212 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
215 void ionic_adminq_netdev_err_print(struct ionic_lif *lif, u8 opcode,
218 const char *stat_str;
220 stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
221 ionic_error_to_str(status);
223 netdev_err(lif->netdev, "%s (%d) failed: %s (%d)\n",
224 ionic_opcode_to_str(opcode), opcode, stat_str, err);
227 static int ionic_adminq_check_err(struct ionic_lif *lif,
228 struct ionic_admin_ctx *ctx,
234 if (ctx->comp.comp.status || timeout) {
235 err = timeout ? -ETIMEDOUT :
236 ionic_error_to_errno(ctx->comp.comp.status);
239 ionic_adminq_netdev_err_print(lif, ctx->cmd.cmd.opcode,
240 ctx->comp.comp.status, err);
243 ionic_adminq_flush(lif);
249 static void ionic_adminq_cb(struct ionic_queue *q,
250 struct ionic_desc_info *desc_info,
251 struct ionic_cq_info *cq_info, void *cb_arg)
253 struct ionic_admin_ctx *ctx = cb_arg;
254 struct ionic_admin_comp *comp;
259 comp = cq_info->cq_desc;
261 memcpy(&ctx->comp, comp, sizeof(*comp));
263 dev_dbg(q->dev, "comp admin queue command:\n");
264 dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
265 &ctx->comp, sizeof(ctx->comp), true);
267 complete_all(&ctx->work);
270 bool ionic_adminq_poke_doorbell(struct ionic_queue *q)
272 struct ionic_lif *lif = q->lif;
273 unsigned long now, then, dif;
274 unsigned long irqflags;
276 spin_lock_irqsave(&lif->adminq_lock, irqflags);
278 if (q->tail_idx == q->head_idx) {
279 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
283 now = READ_ONCE(jiffies);
284 then = q->dbell_jiffies;
287 if (dif > q->dbell_deadline) {
288 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
289 q->dbval | q->head_idx);
291 q->dbell_jiffies = now;
294 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
299 int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
301 struct ionic_desc_info *desc_info;
302 unsigned long irqflags;
303 struct ionic_queue *q;
306 spin_lock_irqsave(&lif->adminq_lock, irqflags);
307 if (!lif->adminqcq) {
308 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
312 q = &lif->adminqcq->q;
314 if (!ionic_q_has_space(q, 1)) {
319 err = ionic_heartbeat_check(lif->ionic);
323 desc_info = &q->info[q->head_idx];
324 memcpy(desc_info->desc, &ctx->cmd, sizeof(ctx->cmd));
326 dev_dbg(&lif->netdev->dev, "post admin queue command:\n");
327 dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
328 &ctx->cmd, sizeof(ctx->cmd), true);
330 ionic_q_post(q, true, ionic_adminq_cb, ctx);
333 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
338 int ionic_adminq_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx,
339 const int err, const bool do_msg)
341 struct net_device *netdev = lif->netdev;
342 unsigned long time_limit;
343 unsigned long time_start;
344 unsigned long time_done;
345 unsigned long remaining;
348 name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
351 if (do_msg && !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
352 netdev_err(netdev, "Posting of %s (%d) failed: %d\n",
353 name, ctx->cmd.cmd.opcode, err);
354 ctx->comp.comp.status = IONIC_RC_ERROR;
358 time_start = jiffies;
359 time_limit = time_start + HZ * (ulong)DEVCMD_TIMEOUT;
361 remaining = wait_for_completion_timeout(&ctx->work,
362 IONIC_ADMINQ_TIME_SLICE);
368 /* force a check of FW status and break out if FW reset */
369 ionic_heartbeat_check(lif->ionic);
370 if ((test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
371 !lif->ionic->idev.fw_status_ready) ||
372 test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
374 netdev_warn(netdev, "%s (%d) interrupted, FW in reset\n",
375 name, ctx->cmd.cmd.opcode);
376 ctx->comp.comp.status = IONIC_RC_ERROR;
380 } while (time_before(jiffies, time_limit));
383 dev_dbg(lif->ionic->dev, "%s: elapsed %d msecs\n",
384 __func__, jiffies_to_msecs(time_done - time_start));
386 return ionic_adminq_check_err(lif, ctx,
387 time_after_eq(time_done, time_limit),
391 static int __ionic_adminq_post_wait(struct ionic_lif *lif,
392 struct ionic_admin_ctx *ctx,
397 if (!ionic_is_fw_running(&lif->ionic->idev))
400 err = ionic_adminq_post(lif, ctx);
402 return ionic_adminq_wait(lif, ctx, err, do_msg);
405 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
407 return __ionic_adminq_post_wait(lif, ctx, true);
410 int ionic_adminq_post_wait_nomsg(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
412 return __ionic_adminq_post_wait(lif, ctx, false);
415 static void ionic_dev_cmd_clean(struct ionic *ionic)
417 struct ionic_dev *idev = &ionic->idev;
419 if (!idev->dev_cmd_regs)
422 iowrite32(0, &idev->dev_cmd_regs->doorbell);
423 memset_io(&idev->dev_cmd_regs->cmd, 0, sizeof(idev->dev_cmd_regs->cmd));
426 void ionic_dev_cmd_dev_err_print(struct ionic *ionic, u8 opcode, u8 status,
429 const char *stat_str;
431 stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
432 ionic_error_to_str(status);
434 dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
435 ionic_opcode_to_str(opcode), opcode, stat_str, err);
438 static int __ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds,
441 struct ionic_dev *idev = &ionic->idev;
442 unsigned long start_time;
443 unsigned long max_wait;
444 unsigned long duration;
450 /* Wait for dev cmd to complete, retrying if we get EAGAIN,
451 * but don't wait any longer than max_seconds.
453 max_wait = jiffies + (max_seconds * HZ);
455 opcode = idev->opcode;
456 start_time = jiffies;
457 for (fw_up = ionic_is_fw_running(idev);
458 !done && fw_up && time_before(jiffies, max_wait);
459 fw_up = ionic_is_fw_running(idev)) {
460 done = ionic_dev_cmd_done(idev);
463 usleep_range(100, 200);
465 duration = jiffies - start_time;
467 dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
468 ionic_opcode_to_str(opcode), opcode,
469 done, duration / HZ, duration);
471 if (!done && !fw_up) {
472 ionic_dev_cmd_clean(ionic);
473 dev_warn(ionic->dev, "DEVCMD %s (%d) interrupted - FW is down\n",
474 ionic_opcode_to_str(opcode), opcode);
478 if (!done && !time_before(jiffies, max_wait)) {
479 ionic_dev_cmd_clean(ionic);
480 dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
481 ionic_opcode_to_str(opcode), opcode, max_seconds);
485 err = ionic_dev_cmd_status(&ionic->idev);
487 if (err == IONIC_RC_EAGAIN &&
488 time_before(jiffies, (max_wait - HZ))) {
489 dev_dbg(ionic->dev, "DEV_CMD %s (%d), %s (%d) retrying...\n",
490 ionic_opcode_to_str(opcode), opcode,
491 ionic_error_to_str(err), err);
493 iowrite32(0, &idev->dev_cmd_regs->done);
495 iowrite32(1, &idev->dev_cmd_regs->doorbell);
499 if (!(opcode == IONIC_CMD_FW_CONTROL && err == IONIC_RC_EAGAIN))
501 ionic_dev_cmd_dev_err_print(ionic, opcode, err,
502 ionic_error_to_errno(err));
504 return ionic_error_to_errno(err);
507 ionic_dev_cmd_clean(ionic);
512 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
514 return __ionic_dev_cmd_wait(ionic, max_seconds, true);
517 int ionic_dev_cmd_wait_nomsg(struct ionic *ionic, unsigned long max_seconds)
519 return __ionic_dev_cmd_wait(ionic, max_seconds, false);
522 int ionic_setup(struct ionic *ionic)
526 err = ionic_dev_setup(ionic);
534 int ionic_identify(struct ionic *ionic)
536 struct ionic_identity *ident = &ionic->ident;
537 struct ionic_dev *idev = &ionic->idev;
541 memset(ident, 0, sizeof(*ident));
543 ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
544 strscpy(ident->drv.driver_ver_str, UTS_RELEASE,
545 sizeof(ident->drv.driver_ver_str));
547 mutex_lock(&ionic->dev_cmd_lock);
549 sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
550 memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
552 ionic_dev_cmd_identify(idev, IONIC_DEV_IDENTITY_VERSION_2);
553 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
555 sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
556 memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
558 mutex_unlock(&ionic->dev_cmd_lock);
561 dev_err(ionic->dev, "Cannot identify ionic: %d\n", err);
565 if (isprint(idev->dev_info.fw_version[0]) &&
566 isascii(idev->dev_info.fw_version[0]))
567 dev_info(ionic->dev, "FW: %.*s\n",
568 (int)(sizeof(idev->dev_info.fw_version) - 1),
569 idev->dev_info.fw_version);
571 dev_info(ionic->dev, "FW: (invalid string) 0x%02x 0x%02x 0x%02x 0x%02x ...\n",
572 (u8)idev->dev_info.fw_version[0],
573 (u8)idev->dev_info.fw_version[1],
574 (u8)idev->dev_info.fw_version[2],
575 (u8)idev->dev_info.fw_version[3]);
577 err = ionic_lif_identify(ionic, IONIC_LIF_TYPE_CLASSIC,
580 dev_err(ionic->dev, "Cannot identify LIFs: %d\n", err);
590 int ionic_init(struct ionic *ionic)
592 struct ionic_dev *idev = &ionic->idev;
595 mutex_lock(&ionic->dev_cmd_lock);
596 ionic_dev_cmd_init(idev);
597 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
598 mutex_unlock(&ionic->dev_cmd_lock);
603 int ionic_reset(struct ionic *ionic)
605 struct ionic_dev *idev = &ionic->idev;
608 if (!ionic_is_fw_running(idev))
611 mutex_lock(&ionic->dev_cmd_lock);
612 ionic_dev_cmd_reset(idev);
613 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
614 mutex_unlock(&ionic->dev_cmd_lock);
619 int ionic_port_identify(struct ionic *ionic)
621 struct ionic_identity *ident = &ionic->ident;
622 struct ionic_dev *idev = &ionic->idev;
626 mutex_lock(&ionic->dev_cmd_lock);
628 ionic_dev_cmd_port_identify(idev);
629 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
631 sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
632 memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
635 mutex_unlock(&ionic->dev_cmd_lock);
640 int ionic_port_init(struct ionic *ionic)
642 struct ionic_identity *ident = &ionic->ident;
643 struct ionic_dev *idev = &ionic->idev;
647 if (!idev->port_info) {
648 idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
649 idev->port_info = dma_alloc_coherent(ionic->dev,
653 if (!idev->port_info)
657 sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
659 mutex_lock(&ionic->dev_cmd_lock);
661 memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
662 ionic_dev_cmd_port_init(idev);
663 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
665 ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
666 ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
668 mutex_unlock(&ionic->dev_cmd_lock);
670 dev_err(ionic->dev, "Failed to init port\n");
671 dma_free_coherent(ionic->dev, idev->port_info_sz,
672 idev->port_info, idev->port_info_pa);
673 idev->port_info = NULL;
674 idev->port_info_pa = 0;
680 int ionic_port_reset(struct ionic *ionic)
682 struct ionic_dev *idev = &ionic->idev;
685 if (!idev->port_info)
688 if (ionic_is_fw_running(idev)) {
689 mutex_lock(&ionic->dev_cmd_lock);
690 ionic_dev_cmd_port_reset(idev);
691 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
692 mutex_unlock(&ionic->dev_cmd_lock);
695 dma_free_coherent(ionic->dev, idev->port_info_sz,
696 idev->port_info, idev->port_info_pa);
698 idev->port_info = NULL;
699 idev->port_info_pa = 0;
704 static int __init ionic_init_module(void)
708 ionic_debugfs_create();
709 ret = ionic_bus_register_driver();
711 ionic_debugfs_destroy();
716 static void __exit ionic_cleanup_module(void)
718 ionic_bus_unregister_driver();
719 ionic_debugfs_destroy();
721 pr_info("%s removed\n", IONIC_DRV_NAME);
724 module_init(ionic_init_module);
725 module_exit(ionic_cleanup_module);