xref: /linux/drivers/net/ethernet/pensando/ionic/ionic_main.c (revision e3b9626f09d429788d929c9b9000a069fcfc056e)
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 #include <generated/utsrelease.h>
10 
11 #include "ionic.h"
12 #include "ionic_bus.h"
13 #include "ionic_lif.h"
14 #include "ionic_debugfs.h"
15 
16 MODULE_DESCRIPTION(IONIC_DRV_DESCRIPTION);
17 MODULE_AUTHOR("Pensando Systems, Inc");
18 MODULE_LICENSE("GPL");
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_ENOSUPP:
62 		return "IONIC_RC_ENOSUPP";
63 	case IONIC_RC_ERROR:
64 		return "IONIC_RC_ERROR";
65 	case IONIC_RC_ERDMA:
66 		return "IONIC_RC_ERDMA";
67 	default:
68 		return "IONIC_RC_UNKNOWN";
69 	}
70 }
71 
72 static int ionic_error_to_errno(enum ionic_status_code code)
73 {
74 	switch (code) {
75 	case IONIC_RC_SUCCESS:
76 		return 0;
77 	case IONIC_RC_EVERSION:
78 	case IONIC_RC_EQTYPE:
79 	case IONIC_RC_EQID:
80 	case IONIC_RC_EINVAL:
81 	case IONIC_RC_ENOSUPP:
82 		return -EINVAL;
83 	case IONIC_RC_EPERM:
84 		return -EPERM;
85 	case IONIC_RC_ENOENT:
86 		return -ENOENT;
87 	case IONIC_RC_EAGAIN:
88 		return -EAGAIN;
89 	case IONIC_RC_ENOMEM:
90 		return -ENOMEM;
91 	case IONIC_RC_EFAULT:
92 		return -EFAULT;
93 	case IONIC_RC_EBUSY:
94 		return -EBUSY;
95 	case IONIC_RC_EEXIST:
96 		return -EEXIST;
97 	case IONIC_RC_ENOSPC:
98 		return -ENOSPC;
99 	case IONIC_RC_ERANGE:
100 		return -ERANGE;
101 	case IONIC_RC_BAD_ADDR:
102 		return -EFAULT;
103 	case IONIC_RC_EOPCODE:
104 	case IONIC_RC_EINTR:
105 	case IONIC_RC_DEV_CMD:
106 	case IONIC_RC_ERROR:
107 	case IONIC_RC_ERDMA:
108 	case IONIC_RC_EIO:
109 	default:
110 		return -EIO;
111 	}
112 }
113 
114 static const char *ionic_opcode_to_str(enum ionic_cmd_opcode opcode)
115 {
116 	switch (opcode) {
117 	case IONIC_CMD_NOP:
118 		return "IONIC_CMD_NOP";
119 	case IONIC_CMD_INIT:
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";
177 	default:
178 		return "DEVCMD_UNKNOWN";
179 	}
180 }
181 
182 static void ionic_adminq_flush(struct ionic_lif *lif)
183 {
184 	struct ionic_queue *q = &lif->adminqcq->q;
185 	struct ionic_desc_info *desc_info;
186 
187 	spin_lock(&lif->adminq_lock);
188 
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);
195 	}
196 	spin_unlock(&lif->adminq_lock);
197 }
198 
199 static int ionic_adminq_check_err(struct ionic_lif *lif,
200 				  struct ionic_admin_ctx *ctx,
201 				  bool timeout)
202 {
203 	struct net_device *netdev = lif->netdev;
204 	const char *opcode_str;
205 	const char *status_str;
206 	int err = 0;
207 
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);
213 
214 		netdev_err(netdev, "%s (%d) failed: %s (%d)\n",
215 			   opcode_str, ctx->cmd.cmd.opcode,
216 			   timeout ? "TIMEOUT" : status_str, err);
217 
218 		if (timeout)
219 			ionic_adminq_flush(lif);
220 	}
221 
222 	return err;
223 }
224 
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)
228 {
229 	struct ionic_admin_ctx *ctx = cb_arg;
230 	struct ionic_admin_comp *comp;
231 	struct device *dev;
232 
233 	if (!ctx)
234 		return;
235 
236 	comp = cq_info->cq_desc;
237 	dev = &q->lif->netdev->dev;
238 
239 	memcpy(&ctx->comp, comp, sizeof(*comp));
240 
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);
244 
245 	complete_all(&ctx->work);
246 }
247 
248 static int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
249 {
250 	struct ionic_desc_info *desc_info;
251 	struct ionic_queue *q;
252 	int err = 0;
253 
254 	WARN_ON(in_interrupt());
255 
256 	if (!lif->adminqcq)
257 		return -EIO;
258 
259 	q = &lif->adminqcq->q;
260 
261 	spin_lock(&lif->adminq_lock);
262 	if (!ionic_q_has_space(q, 1)) {
263 		err = -ENOSPC;
264 		goto err_out;
265 	}
266 
267 	err = ionic_heartbeat_check(lif->ionic);
268 	if (err)
269 		goto err_out;
270 
271 	desc_info = &q->info[q->head_idx];
272 	memcpy(desc_info->desc, &ctx->cmd, sizeof(ctx->cmd));
273 
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);
277 
278 	ionic_q_post(q, true, ionic_adminq_cb, ctx);
279 
280 err_out:
281 	spin_unlock(&lif->adminq_lock);
282 
283 	return err;
284 }
285 
286 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
287 {
288 	struct net_device *netdev = lif->netdev;
289 	unsigned long remaining;
290 	const char *name;
291 	int err;
292 
293 	err = ionic_adminq_post(lif, ctx);
294 	if (err) {
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);
299 		}
300 		return err;
301 	}
302 
303 	remaining = wait_for_completion_timeout(&ctx->work,
304 						HZ * (ulong)DEVCMD_TIMEOUT);
305 	return ionic_adminq_check_err(lif, ctx, (remaining == 0));
306 }
307 
308 int ionic_napi(struct napi_struct *napi, int budget, ionic_cq_cb cb,
309 	       ionic_cq_done_cb done_cb, void *done_arg)
310 {
311 	struct ionic_qcq *qcq = napi_to_qcq(napi);
312 	struct ionic_cq *cq = &qcq->cq;
313 	u32 work_done, flags = 0;
314 
315 	work_done = ionic_cq_service(cq, budget, cb, done_cb, done_arg);
316 
317 	if (work_done < budget && napi_complete_done(napi, work_done)) {
318 		flags |= IONIC_INTR_CRED_UNMASK;
319 		DEBUG_STATS_INTR_REARM(cq->bound_intr);
320 	}
321 
322 	if (work_done || flags) {
323 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
324 		ionic_intr_credits(cq->lif->ionic->idev.intr_ctrl,
325 				   cq->bound_intr->index,
326 				   work_done, flags);
327 	}
328 
329 	DEBUG_STATS_NAPI_POLL(qcq, work_done);
330 
331 	return work_done;
332 }
333 
334 static void ionic_dev_cmd_clean(struct ionic *ionic)
335 {
336 	union ionic_dev_cmd_regs *regs = ionic->idev.dev_cmd_regs;
337 
338 	iowrite32(0, &regs->doorbell);
339 	memset_io(&regs->cmd, 0, sizeof(regs->cmd));
340 }
341 
342 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
343 {
344 	struct ionic_dev *idev = &ionic->idev;
345 	unsigned long start_time;
346 	unsigned long max_wait;
347 	unsigned long duration;
348 	int opcode;
349 	int hb = 0;
350 	int done;
351 	int err;
352 
353 	WARN_ON(in_interrupt());
354 
355 	/* Wait for dev cmd to complete, retrying if we get EAGAIN,
356 	 * but don't wait any longer than max_seconds.
357 	 */
358 	max_wait = jiffies + (max_seconds * HZ);
359 try_again:
360 	start_time = jiffies;
361 	do {
362 		done = ionic_dev_cmd_done(idev);
363 		if (done)
364 			break;
365 		msleep(5);
366 		hb = ionic_heartbeat_check(ionic);
367 	} while (!done && !hb && time_before(jiffies, max_wait));
368 	duration = jiffies - start_time;
369 
370 	opcode = idev->dev_cmd_regs->cmd.cmd.opcode;
371 	dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
372 		ionic_opcode_to_str(opcode), opcode,
373 		done, duration / HZ, duration);
374 
375 	if (!done && hb) {
376 		/* It is possible (but unlikely) that FW was busy and missed a
377 		 * heartbeat check but is still alive and will process this
378 		 * request, so don't clean the dev_cmd in this case.
379 		 */
380 		dev_warn(ionic->dev, "DEVCMD %s (%d) failed - FW halted\n",
381 			 ionic_opcode_to_str(opcode), opcode);
382 		return -ENXIO;
383 	}
384 
385 	if (!done && !time_before(jiffies, max_wait)) {
386 		ionic_dev_cmd_clean(ionic);
387 		dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
388 			 ionic_opcode_to_str(opcode), opcode, max_seconds);
389 		return -ETIMEDOUT;
390 	}
391 
392 	err = ionic_dev_cmd_status(&ionic->idev);
393 	if (err) {
394 		if (err == IONIC_RC_EAGAIN && !time_after(jiffies, max_wait)) {
395 			dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) retrying...\n",
396 				ionic_opcode_to_str(opcode), opcode,
397 				ionic_error_to_str(err), err);
398 
399 			msleep(1000);
400 			iowrite32(0, &idev->dev_cmd_regs->done);
401 			iowrite32(1, &idev->dev_cmd_regs->doorbell);
402 			goto try_again;
403 		}
404 
405 		dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
406 			ionic_opcode_to_str(opcode), opcode,
407 			ionic_error_to_str(err), err);
408 
409 		return ionic_error_to_errno(err);
410 	}
411 
412 	return 0;
413 }
414 
415 int ionic_setup(struct ionic *ionic)
416 {
417 	int err;
418 
419 	err = ionic_dev_setup(ionic);
420 	if (err)
421 		return err;
422 	ionic_reset(ionic);
423 
424 	return 0;
425 }
426 
427 int ionic_identify(struct ionic *ionic)
428 {
429 	struct ionic_identity *ident = &ionic->ident;
430 	struct ionic_dev *idev = &ionic->idev;
431 	size_t sz;
432 	int err;
433 
434 	memset(ident, 0, sizeof(*ident));
435 
436 	ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
437 	strncpy(ident->drv.driver_ver_str, UTS_RELEASE,
438 		sizeof(ident->drv.driver_ver_str) - 1);
439 
440 	mutex_lock(&ionic->dev_cmd_lock);
441 
442 	sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
443 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
444 
445 	ionic_dev_cmd_identify(idev, IONIC_IDENTITY_VERSION_1);
446 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
447 	if (!err) {
448 		sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
449 		memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
450 	}
451 
452 	mutex_unlock(&ionic->dev_cmd_lock);
453 
454 	if (err)
455 		goto err_out_unmap;
456 
457 	ionic_debugfs_add_ident(ionic);
458 
459 	return 0;
460 
461 err_out_unmap:
462 	return err;
463 }
464 
465 int ionic_init(struct ionic *ionic)
466 {
467 	struct ionic_dev *idev = &ionic->idev;
468 	int err;
469 
470 	mutex_lock(&ionic->dev_cmd_lock);
471 	ionic_dev_cmd_init(idev);
472 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
473 	mutex_unlock(&ionic->dev_cmd_lock);
474 
475 	return err;
476 }
477 
478 int ionic_reset(struct ionic *ionic)
479 {
480 	struct ionic_dev *idev = &ionic->idev;
481 	int err;
482 
483 	mutex_lock(&ionic->dev_cmd_lock);
484 	ionic_dev_cmd_reset(idev);
485 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
486 	mutex_unlock(&ionic->dev_cmd_lock);
487 
488 	return err;
489 }
490 
491 int ionic_port_identify(struct ionic *ionic)
492 {
493 	struct ionic_identity *ident = &ionic->ident;
494 	struct ionic_dev *idev = &ionic->idev;
495 	size_t sz;
496 	int err;
497 
498 	mutex_lock(&ionic->dev_cmd_lock);
499 
500 	ionic_dev_cmd_port_identify(idev);
501 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
502 	if (!err) {
503 		sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
504 		memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
505 	}
506 
507 	mutex_unlock(&ionic->dev_cmd_lock);
508 
509 	return err;
510 }
511 
512 int ionic_port_init(struct ionic *ionic)
513 {
514 	struct ionic_identity *ident = &ionic->ident;
515 	struct ionic_dev *idev = &ionic->idev;
516 	size_t sz;
517 	int err;
518 
519 	if (!idev->port_info) {
520 		idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
521 		idev->port_info = dma_alloc_coherent(ionic->dev,
522 						     idev->port_info_sz,
523 						     &idev->port_info_pa,
524 						     GFP_KERNEL);
525 		if (!idev->port_info) {
526 			dev_err(ionic->dev, "Failed to allocate port info\n");
527 			return -ENOMEM;
528 		}
529 	}
530 
531 	sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
532 
533 	mutex_lock(&ionic->dev_cmd_lock);
534 
535 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
536 	ionic_dev_cmd_port_init(idev);
537 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
538 
539 	ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
540 	(void)ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
541 
542 	mutex_unlock(&ionic->dev_cmd_lock);
543 	if (err) {
544 		dev_err(ionic->dev, "Failed to init port\n");
545 		dma_free_coherent(ionic->dev, idev->port_info_sz,
546 				  idev->port_info, idev->port_info_pa);
547 		idev->port_info = NULL;
548 		idev->port_info_pa = 0;
549 	}
550 
551 	return err;
552 }
553 
554 int ionic_port_reset(struct ionic *ionic)
555 {
556 	struct ionic_dev *idev = &ionic->idev;
557 	int err;
558 
559 	if (!idev->port_info)
560 		return 0;
561 
562 	mutex_lock(&ionic->dev_cmd_lock);
563 	ionic_dev_cmd_port_reset(idev);
564 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
565 	mutex_unlock(&ionic->dev_cmd_lock);
566 
567 	dma_free_coherent(ionic->dev, idev->port_info_sz,
568 			  idev->port_info, idev->port_info_pa);
569 
570 	idev->port_info = NULL;
571 	idev->port_info_pa = 0;
572 
573 	if (err)
574 		dev_err(ionic->dev, "Failed to reset port\n");
575 
576 	return err;
577 }
578 
579 static int __init ionic_init_module(void)
580 {
581 	ionic_debugfs_create();
582 	return ionic_bus_register_driver();
583 }
584 
585 static void __exit ionic_cleanup_module(void)
586 {
587 	ionic_bus_unregister_driver();
588 	ionic_debugfs_destroy();
589 
590 	pr_info("%s removed\n", IONIC_DRV_NAME);
591 }
592 
593 module_init(ionic_init_module);
594 module_exit(ionic_cleanup_module);
595