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>
12 #include "ionic_bus.h"
13 #include "ionic_lif.h"
14 #include "ionic_debugfs.h"
16 MODULE_DESCRIPTION(IONIC_DRV_DESCRIPTION
);
17 MODULE_AUTHOR("Pensando Systems, Inc");
18 MODULE_LICENSE("GPL");
20 static const char *ionic_error_to_str(enum ionic_status_code 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";
30 return "IONIC_RC_EIO";
32 return "IONIC_RC_EPERM";
34 return "IONIC_RC_EQID";
36 return "IONIC_RC_EQTYPE";
38 return "IONIC_RC_ENOENT";
40 return "IONIC_RC_EINTR";
42 return "IONIC_RC_EAGAIN";
44 return "IONIC_RC_ENOMEM";
46 return "IONIC_RC_EFAULT";
48 return "IONIC_RC_EBUSY";
50 return "IONIC_RC_EEXIST";
52 return "IONIC_RC_EINVAL";
54 return "IONIC_RC_ENOSPC";
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_ENOSUPP
:
62 return "IONIC_RC_ENOSUPP";
64 return "IONIC_RC_ERROR";
66 return "IONIC_RC_ERDMA";
68 return "IONIC_RC_UNKNOWN";
72 static int ionic_error_to_errno(enum ionic_status_code code
)
75 case IONIC_RC_SUCCESS
:
77 case IONIC_RC_EVERSION
:
81 case IONIC_RC_ENOSUPP
:
101 case IONIC_RC_BAD_ADDR
:
103 case IONIC_RC_EOPCODE
:
105 case IONIC_RC_DEV_CMD
:
114 static const char *ionic_opcode_to_str(enum ionic_cmd_opcode opcode
)
118 return "IONIC_CMD_NOP";
120 return "IONIC_CMD_INIT";
121 case IONIC_CMD_RESET
:
122 return "IONIC_CMD_RESET";
123 case IONIC_CMD_IDENTIFY
:
124 return "IONIC_CMD_IDENTIFY";
125 case IONIC_CMD_GETATTR
:
126 return "IONIC_CMD_GETATTR";
127 case IONIC_CMD_SETATTR
:
128 return "IONIC_CMD_SETATTR";
129 case IONIC_CMD_PORT_IDENTIFY
:
130 return "IONIC_CMD_PORT_IDENTIFY";
131 case IONIC_CMD_PORT_INIT
:
132 return "IONIC_CMD_PORT_INIT";
133 case IONIC_CMD_PORT_RESET
:
134 return "IONIC_CMD_PORT_RESET";
135 case IONIC_CMD_PORT_GETATTR
:
136 return "IONIC_CMD_PORT_GETATTR";
137 case IONIC_CMD_PORT_SETATTR
:
138 return "IONIC_CMD_PORT_SETATTR";
139 case IONIC_CMD_LIF_INIT
:
140 return "IONIC_CMD_LIF_INIT";
141 case IONIC_CMD_LIF_RESET
:
142 return "IONIC_CMD_LIF_RESET";
143 case IONIC_CMD_LIF_IDENTIFY
:
144 return "IONIC_CMD_LIF_IDENTIFY";
145 case IONIC_CMD_LIF_SETATTR
:
146 return "IONIC_CMD_LIF_SETATTR";
147 case IONIC_CMD_LIF_GETATTR
:
148 return "IONIC_CMD_LIF_GETATTR";
149 case IONIC_CMD_RX_MODE_SET
:
150 return "IONIC_CMD_RX_MODE_SET";
151 case IONIC_CMD_RX_FILTER_ADD
:
152 return "IONIC_CMD_RX_FILTER_ADD";
153 case IONIC_CMD_RX_FILTER_DEL
:
154 return "IONIC_CMD_RX_FILTER_DEL";
155 case IONIC_CMD_Q_IDENTIFY
:
156 return "IONIC_CMD_Q_IDENTIFY";
157 case IONIC_CMD_Q_INIT
:
158 return "IONIC_CMD_Q_INIT";
159 case IONIC_CMD_Q_CONTROL
:
160 return "IONIC_CMD_Q_CONTROL";
161 case IONIC_CMD_RDMA_RESET_LIF
:
162 return "IONIC_CMD_RDMA_RESET_LIF";
163 case IONIC_CMD_RDMA_CREATE_EQ
:
164 return "IONIC_CMD_RDMA_CREATE_EQ";
165 case IONIC_CMD_RDMA_CREATE_CQ
:
166 return "IONIC_CMD_RDMA_CREATE_CQ";
167 case IONIC_CMD_RDMA_CREATE_ADMINQ
:
168 return "IONIC_CMD_RDMA_CREATE_ADMINQ";
169 case IONIC_CMD_FW_DOWNLOAD
:
170 return "IONIC_CMD_FW_DOWNLOAD";
171 case IONIC_CMD_FW_CONTROL
:
172 return "IONIC_CMD_FW_CONTROL";
173 case IONIC_CMD_VF_GETATTR
:
174 return "IONIC_CMD_VF_GETATTR";
175 case IONIC_CMD_VF_SETATTR
:
176 return "IONIC_CMD_VF_SETATTR";
178 return "DEVCMD_UNKNOWN";
182 static void ionic_adminq_flush(struct ionic_lif
*lif
)
184 struct ionic_queue
*q
= &lif
->adminqcq
->q
;
185 struct ionic_desc_info
*desc_info
;
187 spin_lock(&lif
->adminq_lock
);
189 while (q
->tail_idx
!= q
->head_idx
) {
190 desc_info
= &q
->info
[q
->tail_idx
];
191 memset(desc_info
->desc
, 0, sizeof(union ionic_adminq_cmd
));
192 desc_info
->cb
= NULL
;
193 desc_info
->cb_arg
= NULL
;
194 q
->tail_idx
= (q
->tail_idx
+ 1) & (q
->num_descs
- 1);
196 spin_unlock(&lif
->adminq_lock
);
199 static int ionic_adminq_check_err(struct ionic_lif
*lif
,
200 struct ionic_admin_ctx
*ctx
,
203 struct net_device
*netdev
= lif
->netdev
;
204 const char *opcode_str
;
205 const char *status_str
;
208 if (ctx
->comp
.comp
.status
|| timeout
) {
209 opcode_str
= ionic_opcode_to_str(ctx
->cmd
.cmd
.opcode
);
210 status_str
= ionic_error_to_str(ctx
->comp
.comp
.status
);
211 err
= timeout
? -ETIMEDOUT
:
212 ionic_error_to_errno(ctx
->comp
.comp
.status
);
214 netdev_err(netdev
, "%s (%d) failed: %s (%d)\n",
215 opcode_str
, ctx
->cmd
.cmd
.opcode
,
216 timeout
? "TIMEOUT" : status_str
, err
);
219 ionic_adminq_flush(lif
);
225 static void ionic_adminq_cb(struct ionic_queue
*q
,
226 struct ionic_desc_info
*desc_info
,
227 struct ionic_cq_info
*cq_info
, void *cb_arg
)
229 struct ionic_admin_ctx
*ctx
= cb_arg
;
230 struct ionic_admin_comp
*comp
;
236 comp
= cq_info
->cq_desc
;
237 dev
= &q
->lif
->netdev
->dev
;
239 memcpy(&ctx
->comp
, comp
, sizeof(*comp
));
241 dev_dbg(dev
, "comp admin queue command:\n");
242 dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET
, 16, 1,
243 &ctx
->comp
, sizeof(ctx
->comp
), true);
245 complete_all(&ctx
->work
);
248 static int ionic_adminq_post(struct ionic_lif
*lif
, struct ionic_admin_ctx
*ctx
)
250 struct ionic_desc_info
*desc_info
;
251 struct ionic_queue
*q
;
254 WARN_ON(in_interrupt());
259 q
= &lif
->adminqcq
->q
;
261 spin_lock(&lif
->adminq_lock
);
262 if (!ionic_q_has_space(q
, 1)) {
267 err
= ionic_heartbeat_check(lif
->ionic
);
271 desc_info
= &q
->info
[q
->head_idx
];
272 memcpy(desc_info
->desc
, &ctx
->cmd
, sizeof(ctx
->cmd
));
274 dev_dbg(&lif
->netdev
->dev
, "post admin queue command:\n");
275 dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET
, 16, 1,
276 &ctx
->cmd
, sizeof(ctx
->cmd
), true);
278 ionic_q_post(q
, true, ionic_adminq_cb
, ctx
);
281 spin_unlock(&lif
->adminq_lock
);
286 int ionic_adminq_post_wait(struct ionic_lif
*lif
, struct ionic_admin_ctx
*ctx
)
288 struct net_device
*netdev
= lif
->netdev
;
289 unsigned long remaining
;
293 err
= ionic_adminq_post(lif
, ctx
);
295 if (!test_bit(IONIC_LIF_F_FW_RESET
, lif
->state
)) {
296 name
= ionic_opcode_to_str(ctx
->cmd
.cmd
.opcode
);
297 netdev_err(netdev
, "Posting of %s (%d) failed: %d\n",
298 name
, ctx
->cmd
.cmd
.opcode
, err
);
303 remaining
= wait_for_completion_timeout(&ctx
->work
,
304 HZ
* (ulong
)DEVCMD_TIMEOUT
);
305 return ionic_adminq_check_err(lif
, ctx
, (remaining
== 0));
308 static void ionic_dev_cmd_clean(struct ionic
*ionic
)
310 union ionic_dev_cmd_regs
*regs
= ionic
->idev
.dev_cmd_regs
;
312 iowrite32(0, ®s
->doorbell
);
313 memset_io(®s
->cmd
, 0, sizeof(regs
->cmd
));
316 int ionic_dev_cmd_wait(struct ionic
*ionic
, unsigned long max_seconds
)
318 struct ionic_dev
*idev
= &ionic
->idev
;
319 unsigned long start_time
;
320 unsigned long max_wait
;
321 unsigned long duration
;
327 WARN_ON(in_interrupt());
329 /* Wait for dev cmd to complete, retrying if we get EAGAIN,
330 * but don't wait any longer than max_seconds.
332 max_wait
= jiffies
+ (max_seconds
* HZ
);
334 start_time
= jiffies
;
336 done
= ionic_dev_cmd_done(idev
);
340 hb
= ionic_heartbeat_check(ionic
);
341 } while (!done
&& !hb
&& time_before(jiffies
, max_wait
));
342 duration
= jiffies
- start_time
;
344 opcode
= idev
->dev_cmd_regs
->cmd
.cmd
.opcode
;
345 dev_dbg(ionic
->dev
, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
346 ionic_opcode_to_str(opcode
), opcode
,
347 done
, duration
/ HZ
, duration
);
350 /* It is possible (but unlikely) that FW was busy and missed a
351 * heartbeat check but is still alive and will process this
352 * request, so don't clean the dev_cmd in this case.
354 dev_warn(ionic
->dev
, "DEVCMD %s (%d) failed - FW halted\n",
355 ionic_opcode_to_str(opcode
), opcode
);
359 if (!done
&& !time_before(jiffies
, max_wait
)) {
360 ionic_dev_cmd_clean(ionic
);
361 dev_warn(ionic
->dev
, "DEVCMD %s (%d) timeout after %ld secs\n",
362 ionic_opcode_to_str(opcode
), opcode
, max_seconds
);
366 err
= ionic_dev_cmd_status(&ionic
->idev
);
368 if (err
== IONIC_RC_EAGAIN
&& !time_after(jiffies
, max_wait
)) {
369 dev_err(ionic
->dev
, "DEV_CMD %s (%d) error, %s (%d) retrying...\n",
370 ionic_opcode_to_str(opcode
), opcode
,
371 ionic_error_to_str(err
), err
);
374 iowrite32(0, &idev
->dev_cmd_regs
->done
);
375 iowrite32(1, &idev
->dev_cmd_regs
->doorbell
);
379 dev_err(ionic
->dev
, "DEV_CMD %s (%d) error, %s (%d) failed\n",
380 ionic_opcode_to_str(opcode
), opcode
,
381 ionic_error_to_str(err
), err
);
383 return ionic_error_to_errno(err
);
389 int ionic_setup(struct ionic
*ionic
)
393 err
= ionic_dev_setup(ionic
);
401 int ionic_identify(struct ionic
*ionic
)
403 struct ionic_identity
*ident
= &ionic
->ident
;
404 struct ionic_dev
*idev
= &ionic
->idev
;
408 memset(ident
, 0, sizeof(*ident
));
410 ident
->drv
.os_type
= cpu_to_le32(IONIC_OS_TYPE_LINUX
);
411 strncpy(ident
->drv
.driver_ver_str
, UTS_RELEASE
,
412 sizeof(ident
->drv
.driver_ver_str
) - 1);
414 mutex_lock(&ionic
->dev_cmd_lock
);
416 sz
= min(sizeof(ident
->drv
), sizeof(idev
->dev_cmd_regs
->data
));
417 memcpy_toio(&idev
->dev_cmd_regs
->data
, &ident
->drv
, sz
);
419 ionic_dev_cmd_identify(idev
, IONIC_IDENTITY_VERSION_1
);
420 err
= ionic_dev_cmd_wait(ionic
, DEVCMD_TIMEOUT
);
422 sz
= min(sizeof(ident
->dev
), sizeof(idev
->dev_cmd_regs
->data
));
423 memcpy_fromio(&ident
->dev
, &idev
->dev_cmd_regs
->data
, sz
);
426 mutex_unlock(&ionic
->dev_cmd_lock
);
431 ionic_debugfs_add_ident(ionic
);
439 int ionic_init(struct ionic
*ionic
)
441 struct ionic_dev
*idev
= &ionic
->idev
;
444 mutex_lock(&ionic
->dev_cmd_lock
);
445 ionic_dev_cmd_init(idev
);
446 err
= ionic_dev_cmd_wait(ionic
, DEVCMD_TIMEOUT
);
447 mutex_unlock(&ionic
->dev_cmd_lock
);
452 int ionic_reset(struct ionic
*ionic
)
454 struct ionic_dev
*idev
= &ionic
->idev
;
457 mutex_lock(&ionic
->dev_cmd_lock
);
458 ionic_dev_cmd_reset(idev
);
459 err
= ionic_dev_cmd_wait(ionic
, DEVCMD_TIMEOUT
);
460 mutex_unlock(&ionic
->dev_cmd_lock
);
465 int ionic_port_identify(struct ionic
*ionic
)
467 struct ionic_identity
*ident
= &ionic
->ident
;
468 struct ionic_dev
*idev
= &ionic
->idev
;
472 mutex_lock(&ionic
->dev_cmd_lock
);
474 ionic_dev_cmd_port_identify(idev
);
475 err
= ionic_dev_cmd_wait(ionic
, DEVCMD_TIMEOUT
);
477 sz
= min(sizeof(ident
->port
), sizeof(idev
->dev_cmd_regs
->data
));
478 memcpy_fromio(&ident
->port
, &idev
->dev_cmd_regs
->data
, sz
);
481 mutex_unlock(&ionic
->dev_cmd_lock
);
486 int ionic_port_init(struct ionic
*ionic
)
488 struct ionic_identity
*ident
= &ionic
->ident
;
489 struct ionic_dev
*idev
= &ionic
->idev
;
493 if (!idev
->port_info
) {
494 idev
->port_info_sz
= ALIGN(sizeof(*idev
->port_info
), PAGE_SIZE
);
495 idev
->port_info
= dma_alloc_coherent(ionic
->dev
,
499 if (!idev
->port_info
) {
500 dev_err(ionic
->dev
, "Failed to allocate port info\n");
505 sz
= min(sizeof(ident
->port
.config
), sizeof(idev
->dev_cmd_regs
->data
));
507 mutex_lock(&ionic
->dev_cmd_lock
);
509 memcpy_toio(&idev
->dev_cmd_regs
->data
, &ident
->port
.config
, sz
);
510 ionic_dev_cmd_port_init(idev
);
511 err
= ionic_dev_cmd_wait(ionic
, DEVCMD_TIMEOUT
);
513 ionic_dev_cmd_port_state(&ionic
->idev
, IONIC_PORT_ADMIN_STATE_UP
);
514 (void)ionic_dev_cmd_wait(ionic
, DEVCMD_TIMEOUT
);
516 mutex_unlock(&ionic
->dev_cmd_lock
);
518 dev_err(ionic
->dev
, "Failed to init port\n");
519 dma_free_coherent(ionic
->dev
, idev
->port_info_sz
,
520 idev
->port_info
, idev
->port_info_pa
);
521 idev
->port_info
= NULL
;
522 idev
->port_info_pa
= 0;
528 int ionic_port_reset(struct ionic
*ionic
)
530 struct ionic_dev
*idev
= &ionic
->idev
;
533 if (!idev
->port_info
)
536 mutex_lock(&ionic
->dev_cmd_lock
);
537 ionic_dev_cmd_port_reset(idev
);
538 err
= ionic_dev_cmd_wait(ionic
, DEVCMD_TIMEOUT
);
539 mutex_unlock(&ionic
->dev_cmd_lock
);
541 dma_free_coherent(ionic
->dev
, idev
->port_info_sz
,
542 idev
->port_info
, idev
->port_info_pa
);
544 idev
->port_info
= NULL
;
545 idev
->port_info_pa
= 0;
548 dev_err(ionic
->dev
, "Failed to reset port\n");
553 static int __init
ionic_init_module(void)
555 ionic_debugfs_create();
556 return ionic_bus_register_driver();
559 static void __exit
ionic_cleanup_module(void)
561 ionic_bus_unregister_driver();
562 ionic_debugfs_destroy();
564 pr_info("%s removed\n", IONIC_DRV_NAME
);
567 module_init(ionic_init_module
);
568 module_exit(ionic_cleanup_module
);