xref: /linux/drivers/net/ethernet/pensando/ionic/ionic_main.c (revision faee676944dab731c9b2b91cf86c769d291a2237)
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 <linux/vermagic.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_INIT:
156 		return "IONIC_CMD_Q_INIT";
157 	case IONIC_CMD_Q_CONTROL:
158 		return "IONIC_CMD_Q_CONTROL";
159 	case IONIC_CMD_RDMA_RESET_LIF:
160 		return "IONIC_CMD_RDMA_RESET_LIF";
161 	case IONIC_CMD_RDMA_CREATE_EQ:
162 		return "IONIC_CMD_RDMA_CREATE_EQ";
163 	case IONIC_CMD_RDMA_CREATE_CQ:
164 		return "IONIC_CMD_RDMA_CREATE_CQ";
165 	case IONIC_CMD_RDMA_CREATE_ADMINQ:
166 		return "IONIC_CMD_RDMA_CREATE_ADMINQ";
167 	case IONIC_CMD_FW_DOWNLOAD:
168 		return "IONIC_CMD_FW_DOWNLOAD";
169 	case IONIC_CMD_FW_CONTROL:
170 		return "IONIC_CMD_FW_CONTROL";
171 	case IONIC_CMD_VF_GETATTR:
172 		return "IONIC_CMD_VF_GETATTR";
173 	case IONIC_CMD_VF_SETATTR:
174 		return "IONIC_CMD_VF_SETATTR";
175 	default:
176 		return "DEVCMD_UNKNOWN";
177 	}
178 }
179 
180 static void ionic_adminq_flush(struct ionic_lif *lif)
181 {
182 	struct ionic_queue *adminq = &lif->adminqcq->q;
183 
184 	spin_lock(&lif->adminq_lock);
185 
186 	while (adminq->tail != adminq->head) {
187 		memset(adminq->tail->desc, 0, sizeof(union ionic_adminq_cmd));
188 		adminq->tail->cb = NULL;
189 		adminq->tail->cb_arg = NULL;
190 		adminq->tail = adminq->tail->next;
191 	}
192 	spin_unlock(&lif->adminq_lock);
193 }
194 
195 static int ionic_adminq_check_err(struct ionic_lif *lif,
196 				  struct ionic_admin_ctx *ctx,
197 				  bool timeout)
198 {
199 	struct net_device *netdev = lif->netdev;
200 	const char *opcode_str;
201 	const char *status_str;
202 	int err = 0;
203 
204 	if (ctx->comp.comp.status || timeout) {
205 		opcode_str = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
206 		status_str = ionic_error_to_str(ctx->comp.comp.status);
207 		err = timeout ? -ETIMEDOUT :
208 				ionic_error_to_errno(ctx->comp.comp.status);
209 
210 		netdev_err(netdev, "%s (%d) failed: %s (%d)\n",
211 			   opcode_str, ctx->cmd.cmd.opcode,
212 			   timeout ? "TIMEOUT" : status_str, err);
213 
214 		if (timeout)
215 			ionic_adminq_flush(lif);
216 	}
217 
218 	return err;
219 }
220 
221 static void ionic_adminq_cb(struct ionic_queue *q,
222 			    struct ionic_desc_info *desc_info,
223 			    struct ionic_cq_info *cq_info, void *cb_arg)
224 {
225 	struct ionic_admin_ctx *ctx = cb_arg;
226 	struct ionic_admin_comp *comp;
227 	struct device *dev;
228 
229 	if (!ctx)
230 		return;
231 
232 	comp = cq_info->cq_desc;
233 	dev = &q->lif->netdev->dev;
234 
235 	memcpy(&ctx->comp, comp, sizeof(*comp));
236 
237 	dev_dbg(dev, "comp admin queue command:\n");
238 	dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
239 			 &ctx->comp, sizeof(ctx->comp), true);
240 
241 	complete_all(&ctx->work);
242 }
243 
244 static int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
245 {
246 	struct ionic_queue *adminq;
247 	int err = 0;
248 
249 	WARN_ON(in_interrupt());
250 
251 	if (!lif->adminqcq)
252 		return -EIO;
253 
254 	adminq = &lif->adminqcq->q;
255 
256 	spin_lock(&lif->adminq_lock);
257 	if (!ionic_q_has_space(adminq, 1)) {
258 		err = -ENOSPC;
259 		goto err_out;
260 	}
261 
262 	err = ionic_heartbeat_check(lif->ionic);
263 	if (err)
264 		goto err_out;
265 
266 	memcpy(adminq->head->desc, &ctx->cmd, sizeof(ctx->cmd));
267 
268 	dev_dbg(&lif->netdev->dev, "post admin queue command:\n");
269 	dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
270 			 &ctx->cmd, sizeof(ctx->cmd), true);
271 
272 	ionic_q_post(adminq, true, ionic_adminq_cb, ctx);
273 
274 err_out:
275 	spin_unlock(&lif->adminq_lock);
276 
277 	return err;
278 }
279 
280 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
281 {
282 	struct net_device *netdev = lif->netdev;
283 	unsigned long remaining;
284 	const char *name;
285 	int err;
286 
287 	err = ionic_adminq_post(lif, ctx);
288 	if (err) {
289 		name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
290 		netdev_err(netdev, "Posting of %s (%d) failed: %d\n",
291 			   name, ctx->cmd.cmd.opcode, err);
292 		return err;
293 	}
294 
295 	remaining = wait_for_completion_timeout(&ctx->work,
296 						HZ * (ulong)DEVCMD_TIMEOUT);
297 	return ionic_adminq_check_err(lif, ctx, (remaining == 0));
298 }
299 
300 int ionic_napi(struct napi_struct *napi, int budget, ionic_cq_cb cb,
301 	       ionic_cq_done_cb done_cb, void *done_arg)
302 {
303 	struct ionic_qcq *qcq = napi_to_qcq(napi);
304 	struct ionic_cq *cq = &qcq->cq;
305 	u32 work_done, flags = 0;
306 
307 	work_done = ionic_cq_service(cq, budget, cb, done_cb, done_arg);
308 
309 	if (work_done < budget && napi_complete_done(napi, work_done)) {
310 		flags |= IONIC_INTR_CRED_UNMASK;
311 		DEBUG_STATS_INTR_REARM(cq->bound_intr);
312 	}
313 
314 	if (work_done || flags) {
315 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
316 		ionic_intr_credits(cq->lif->ionic->idev.intr_ctrl,
317 				   cq->bound_intr->index,
318 				   work_done, flags);
319 	}
320 
321 	DEBUG_STATS_NAPI_POLL(qcq, work_done);
322 
323 	return work_done;
324 }
325 
326 static void ionic_dev_cmd_clean(struct ionic *ionic)
327 {
328 	union ionic_dev_cmd_regs *regs = ionic->idev.dev_cmd_regs;
329 
330 	iowrite32(0, &regs->doorbell);
331 	memset_io(&regs->cmd, 0, sizeof(regs->cmd));
332 }
333 
334 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
335 {
336 	struct ionic_dev *idev = &ionic->idev;
337 	unsigned long start_time;
338 	unsigned long max_wait;
339 	unsigned long duration;
340 	int opcode;
341 	int hb = 0;
342 	int done;
343 	int err;
344 
345 	WARN_ON(in_interrupt());
346 
347 	/* Wait for dev cmd to complete, retrying if we get EAGAIN,
348 	 * but don't wait any longer than max_seconds.
349 	 */
350 	max_wait = jiffies + (max_seconds * HZ);
351 try_again:
352 	start_time = jiffies;
353 	do {
354 		done = ionic_dev_cmd_done(idev);
355 		if (done)
356 			break;
357 		msleep(20);
358 		hb = ionic_heartbeat_check(ionic);
359 	} while (!done && !hb && time_before(jiffies, max_wait));
360 	duration = jiffies - start_time;
361 
362 	opcode = idev->dev_cmd_regs->cmd.cmd.opcode;
363 	dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
364 		ionic_opcode_to_str(opcode), opcode,
365 		done, duration / HZ, duration);
366 
367 	if (!done && hb) {
368 		/* It is possible (but unlikely) that FW was busy and missed a
369 		 * heartbeat check but is still alive and will process this
370 		 * request, so don't clean the dev_cmd in this case.
371 		 */
372 		dev_warn(ionic->dev, "DEVCMD %s (%d) failed - FW halted\n",
373 			 ionic_opcode_to_str(opcode), opcode);
374 		return -ENXIO;
375 	}
376 
377 	if (!done && !time_before(jiffies, max_wait)) {
378 		ionic_dev_cmd_clean(ionic);
379 		dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
380 			 ionic_opcode_to_str(opcode), opcode, max_seconds);
381 		return -ETIMEDOUT;
382 	}
383 
384 	err = ionic_dev_cmd_status(&ionic->idev);
385 	if (err) {
386 		if (err == IONIC_RC_EAGAIN && !time_after(jiffies, max_wait)) {
387 			dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) retrying...\n",
388 				ionic_opcode_to_str(opcode), opcode,
389 				ionic_error_to_str(err), err);
390 
391 			msleep(1000);
392 			iowrite32(0, &idev->dev_cmd_regs->done);
393 			iowrite32(1, &idev->dev_cmd_regs->doorbell);
394 			goto try_again;
395 		}
396 
397 		dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
398 			ionic_opcode_to_str(opcode), opcode,
399 			ionic_error_to_str(err), err);
400 
401 		return ionic_error_to_errno(err);
402 	}
403 
404 	return 0;
405 }
406 
407 int ionic_setup(struct ionic *ionic)
408 {
409 	int err;
410 
411 	err = ionic_dev_setup(ionic);
412 	if (err)
413 		return err;
414 
415 	return 0;
416 }
417 
418 int ionic_identify(struct ionic *ionic)
419 {
420 	struct ionic_identity *ident = &ionic->ident;
421 	struct ionic_dev *idev = &ionic->idev;
422 	size_t sz;
423 	int err;
424 
425 	memset(ident, 0, sizeof(*ident));
426 
427 	ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
428 	strncpy(ident->drv.driver_ver_str, UTS_RELEASE,
429 		sizeof(ident->drv.driver_ver_str) - 1);
430 
431 	mutex_lock(&ionic->dev_cmd_lock);
432 
433 	sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
434 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
435 
436 	ionic_dev_cmd_identify(idev, IONIC_IDENTITY_VERSION_1);
437 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
438 	if (!err) {
439 		sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
440 		memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
441 	}
442 
443 	mutex_unlock(&ionic->dev_cmd_lock);
444 
445 	if (err)
446 		goto err_out_unmap;
447 
448 	ionic_debugfs_add_ident(ionic);
449 
450 	return 0;
451 
452 err_out_unmap:
453 	return err;
454 }
455 
456 int ionic_init(struct ionic *ionic)
457 {
458 	struct ionic_dev *idev = &ionic->idev;
459 	int err;
460 
461 	mutex_lock(&ionic->dev_cmd_lock);
462 	ionic_dev_cmd_init(idev);
463 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
464 	mutex_unlock(&ionic->dev_cmd_lock);
465 
466 	return err;
467 }
468 
469 int ionic_reset(struct ionic *ionic)
470 {
471 	struct ionic_dev *idev = &ionic->idev;
472 	int err;
473 
474 	mutex_lock(&ionic->dev_cmd_lock);
475 	ionic_dev_cmd_reset(idev);
476 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
477 	mutex_unlock(&ionic->dev_cmd_lock);
478 
479 	return err;
480 }
481 
482 int ionic_port_identify(struct ionic *ionic)
483 {
484 	struct ionic_identity *ident = &ionic->ident;
485 	struct ionic_dev *idev = &ionic->idev;
486 	size_t sz;
487 	int err;
488 
489 	mutex_lock(&ionic->dev_cmd_lock);
490 
491 	ionic_dev_cmd_port_identify(idev);
492 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
493 	if (!err) {
494 		sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
495 		memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
496 	}
497 
498 	mutex_unlock(&ionic->dev_cmd_lock);
499 
500 	return err;
501 }
502 
503 int ionic_port_init(struct ionic *ionic)
504 {
505 	struct ionic_identity *ident = &ionic->ident;
506 	struct ionic_dev *idev = &ionic->idev;
507 	size_t sz;
508 	int err;
509 
510 	if (idev->port_info)
511 		return 0;
512 
513 	idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
514 	idev->port_info = dma_alloc_coherent(ionic->dev, idev->port_info_sz,
515 					     &idev->port_info_pa,
516 					     GFP_KERNEL);
517 	if (!idev->port_info) {
518 		dev_err(ionic->dev, "Failed to allocate port info, aborting\n");
519 		return -ENOMEM;
520 	}
521 
522 	sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
523 
524 	mutex_lock(&ionic->dev_cmd_lock);
525 
526 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
527 	ionic_dev_cmd_port_init(idev);
528 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
529 
530 	ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
531 	(void)ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
532 
533 	mutex_unlock(&ionic->dev_cmd_lock);
534 	if (err) {
535 		dev_err(ionic->dev, "Failed to init port\n");
536 		dma_free_coherent(ionic->dev, idev->port_info_sz,
537 				  idev->port_info, idev->port_info_pa);
538 		idev->port_info = NULL;
539 		idev->port_info_pa = 0;
540 	}
541 
542 	return err;
543 }
544 
545 int ionic_port_reset(struct ionic *ionic)
546 {
547 	struct ionic_dev *idev = &ionic->idev;
548 	int err;
549 
550 	if (!idev->port_info)
551 		return 0;
552 
553 	mutex_lock(&ionic->dev_cmd_lock);
554 	ionic_dev_cmd_port_reset(idev);
555 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
556 	mutex_unlock(&ionic->dev_cmd_lock);
557 
558 	dma_free_coherent(ionic->dev, idev->port_info_sz,
559 			  idev->port_info, idev->port_info_pa);
560 
561 	idev->port_info = NULL;
562 	idev->port_info_pa = 0;
563 
564 	if (err)
565 		dev_err(ionic->dev, "Failed to reset port\n");
566 
567 	return err;
568 }
569 
570 static int __init ionic_init_module(void)
571 {
572 	ionic_debugfs_create();
573 	return ionic_bus_register_driver();
574 }
575 
576 static void __exit ionic_cleanup_module(void)
577 {
578 	ionic_bus_unregister_driver();
579 	ionic_debugfs_destroy();
580 
581 	pr_info("%s removed\n", IONIC_DRV_NAME);
582 }
583 
584 module_init(ionic_init_module);
585 module_exit(ionic_cleanup_module);
586