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