xref: /linux/drivers/net/ethernet/pensando/ionic/ionic_main.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
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 #include <linux/ctype.h>
11 
12 #include "ionic.h"
13 #include "ionic_bus.h"
14 #include "ionic_lif.h"
15 #include "ionic_debugfs.h"
16 
17 MODULE_DESCRIPTION(IONIC_DRV_DESCRIPTION);
18 MODULE_AUTHOR("Pensando Systems, Inc");
19 MODULE_LICENSE("GPL");
20 
21 static const char *ionic_error_to_str(enum ionic_status_code code)
22 {
23 	switch (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";
30 	case IONIC_RC_EIO:
31 		return "IONIC_RC_EIO";
32 	case IONIC_RC_EPERM:
33 		return "IONIC_RC_EPERM";
34 	case IONIC_RC_EQID:
35 		return "IONIC_RC_EQID";
36 	case IONIC_RC_EQTYPE:
37 		return "IONIC_RC_EQTYPE";
38 	case IONIC_RC_ENOENT:
39 		return "IONIC_RC_ENOENT";
40 	case IONIC_RC_EINTR:
41 		return "IONIC_RC_EINTR";
42 	case IONIC_RC_EAGAIN:
43 		return "IONIC_RC_EAGAIN";
44 	case IONIC_RC_ENOMEM:
45 		return "IONIC_RC_ENOMEM";
46 	case IONIC_RC_EFAULT:
47 		return "IONIC_RC_EFAULT";
48 	case IONIC_RC_EBUSY:
49 		return "IONIC_RC_EBUSY";
50 	case IONIC_RC_EEXIST:
51 		return "IONIC_RC_EEXIST";
52 	case IONIC_RC_EINVAL:
53 		return "IONIC_RC_EINVAL";
54 	case IONIC_RC_ENOSPC:
55 		return "IONIC_RC_ENOSPC";
56 	case IONIC_RC_ERANGE:
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";
64 	case IONIC_RC_ERROR:
65 		return "IONIC_RC_ERROR";
66 	case IONIC_RC_ERDMA:
67 		return "IONIC_RC_ERDMA";
68 	case IONIC_RC_EBAD_FW:
69 		return "IONIC_RC_EBAD_FW";
70 	default:
71 		return "IONIC_RC_UNKNOWN";
72 	}
73 }
74 
75 static int ionic_error_to_errno(enum ionic_status_code code)
76 {
77 	switch (code) {
78 	case IONIC_RC_SUCCESS:
79 		return 0;
80 	case IONIC_RC_EVERSION:
81 	case IONIC_RC_EQTYPE:
82 	case IONIC_RC_EQID:
83 	case IONIC_RC_EINVAL:
84 	case IONIC_RC_ENOSUPP:
85 		return -EINVAL;
86 	case IONIC_RC_EPERM:
87 		return -EPERM;
88 	case IONIC_RC_ENOENT:
89 		return -ENOENT;
90 	case IONIC_RC_EAGAIN:
91 		return -EAGAIN;
92 	case IONIC_RC_ENOMEM:
93 		return -ENOMEM;
94 	case IONIC_RC_EFAULT:
95 		return -EFAULT;
96 	case IONIC_RC_EBUSY:
97 		return -EBUSY;
98 	case IONIC_RC_EEXIST:
99 		return -EEXIST;
100 	case IONIC_RC_ENOSPC:
101 		return -ENOSPC;
102 	case IONIC_RC_ERANGE:
103 		return -ERANGE;
104 	case IONIC_RC_BAD_ADDR:
105 		return -EFAULT;
106 	case IONIC_RC_EOPCODE:
107 	case IONIC_RC_EINTR:
108 	case IONIC_RC_DEV_CMD:
109 	case IONIC_RC_ERROR:
110 	case IONIC_RC_ERDMA:
111 	case IONIC_RC_EIO:
112 	default:
113 		return -EIO;
114 	}
115 }
116 
117 static const char *ionic_opcode_to_str(enum ionic_cmd_opcode opcode)
118 {
119 	switch (opcode) {
120 	case IONIC_CMD_NOP:
121 		return "IONIC_CMD_NOP";
122 	case IONIC_CMD_INIT:
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";
186 	default:
187 		return "DEVCMD_UNKNOWN";
188 	}
189 }
190 
191 static void ionic_adminq_flush(struct ionic_lif *lif)
192 {
193 	struct ionic_desc_info *desc_info;
194 	unsigned long irqflags;
195 	struct ionic_queue *q;
196 
197 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
198 	if (!lif->adminqcq) {
199 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
200 		return;
201 	}
202 
203 	q = &lif->adminqcq->q;
204 
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);
211 	}
212 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
213 }
214 
215 void ionic_adminq_netdev_err_print(struct ionic_lif *lif, u8 opcode,
216 				   u8 status, int err)
217 {
218 	const char *stat_str;
219 
220 	stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
221 					 ionic_error_to_str(status);
222 
223 	netdev_err(lif->netdev, "%s (%d) failed: %s (%d)\n",
224 		   ionic_opcode_to_str(opcode), opcode, stat_str, err);
225 }
226 
227 static int ionic_adminq_check_err(struct ionic_lif *lif,
228 				  struct ionic_admin_ctx *ctx,
229 				  const bool timeout,
230 				  const bool do_msg)
231 {
232 	int err = 0;
233 
234 	if (ctx->comp.comp.status || timeout) {
235 		err = timeout ? -ETIMEDOUT :
236 				ionic_error_to_errno(ctx->comp.comp.status);
237 
238 		if (do_msg)
239 			ionic_adminq_netdev_err_print(lif, ctx->cmd.cmd.opcode,
240 						      ctx->comp.comp.status, err);
241 
242 		if (timeout)
243 			ionic_adminq_flush(lif);
244 	}
245 
246 	return err;
247 }
248 
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)
252 {
253 	struct ionic_admin_ctx *ctx = cb_arg;
254 	struct ionic_admin_comp *comp;
255 
256 	if (!ctx)
257 		return;
258 
259 	comp = cq_info->cq_desc;
260 
261 	memcpy(&ctx->comp, comp, sizeof(*comp));
262 
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);
266 
267 	complete_all(&ctx->work);
268 }
269 
270 bool ionic_adminq_poke_doorbell(struct ionic_queue *q)
271 {
272 	struct ionic_lif *lif = q->lif;
273 	unsigned long now, then, dif;
274 	unsigned long irqflags;
275 
276 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
277 
278 	if (q->tail_idx == q->head_idx) {
279 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
280 		return false;
281 	}
282 
283 	now = READ_ONCE(jiffies);
284 	then = q->dbell_jiffies;
285 	dif = now - then;
286 
287 	if (dif > q->dbell_deadline) {
288 		ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
289 				 q->dbval | q->head_idx);
290 
291 		q->dbell_jiffies = now;
292 	}
293 
294 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
295 
296 	return true;
297 }
298 
299 int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
300 {
301 	struct ionic_desc_info *desc_info;
302 	unsigned long irqflags;
303 	struct ionic_queue *q;
304 	int err = 0;
305 
306 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
307 	if (!lif->adminqcq) {
308 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
309 		return -EIO;
310 	}
311 
312 	q = &lif->adminqcq->q;
313 
314 	if (!ionic_q_has_space(q, 1)) {
315 		err = -ENOSPC;
316 		goto err_out;
317 	}
318 
319 	err = ionic_heartbeat_check(lif->ionic);
320 	if (err)
321 		goto err_out;
322 
323 	desc_info = &q->info[q->head_idx];
324 	memcpy(desc_info->desc, &ctx->cmd, sizeof(ctx->cmd));
325 
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);
329 
330 	ionic_q_post(q, true, ionic_adminq_cb, ctx);
331 
332 err_out:
333 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
334 
335 	return err;
336 }
337 
338 int ionic_adminq_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx,
339 		      const int err, const bool do_msg)
340 {
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;
346 	const char *name;
347 
348 	name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
349 
350 	if (err) {
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;
355 		return err;
356 	}
357 
358 	time_start = jiffies;
359 	time_limit = time_start + HZ * (ulong)DEVCMD_TIMEOUT;
360 	do {
361 		remaining = wait_for_completion_timeout(&ctx->work,
362 							IONIC_ADMINQ_TIME_SLICE);
363 
364 		/* check for done */
365 		if (remaining)
366 			break;
367 
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)) {
373 			if (do_msg)
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;
377 			return -ENXIO;
378 		}
379 
380 	} while (time_before(jiffies, time_limit));
381 	time_done = jiffies;
382 
383 	dev_dbg(lif->ionic->dev, "%s: elapsed %d msecs\n",
384 		__func__, jiffies_to_msecs(time_done - time_start));
385 
386 	return ionic_adminq_check_err(lif, ctx,
387 				      time_after_eq(time_done, time_limit),
388 				      do_msg);
389 }
390 
391 static int __ionic_adminq_post_wait(struct ionic_lif *lif,
392 				    struct ionic_admin_ctx *ctx,
393 				    const bool do_msg)
394 {
395 	int err;
396 
397 	if (!ionic_is_fw_running(&lif->ionic->idev))
398 		return 0;
399 
400 	err = ionic_adminq_post(lif, ctx);
401 
402 	return ionic_adminq_wait(lif, ctx, err, do_msg);
403 }
404 
405 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
406 {
407 	return __ionic_adminq_post_wait(lif, ctx, true);
408 }
409 
410 int ionic_adminq_post_wait_nomsg(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
411 {
412 	return __ionic_adminq_post_wait(lif, ctx, false);
413 }
414 
415 static void ionic_dev_cmd_clean(struct ionic *ionic)
416 {
417 	struct ionic_dev *idev = &ionic->idev;
418 
419 	iowrite32(0, &idev->dev_cmd_regs->doorbell);
420 	memset_io(&idev->dev_cmd_regs->cmd, 0, sizeof(idev->dev_cmd_regs->cmd));
421 }
422 
423 void ionic_dev_cmd_dev_err_print(struct ionic *ionic, u8 opcode, u8 status,
424 				 int err)
425 {
426 	const char *stat_str;
427 
428 	stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
429 					 ionic_error_to_str(status);
430 
431 	dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
432 		ionic_opcode_to_str(opcode), opcode, stat_str, err);
433 }
434 
435 static int __ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds,
436 				const bool do_msg)
437 {
438 	struct ionic_dev *idev = &ionic->idev;
439 	unsigned long start_time;
440 	unsigned long max_wait;
441 	unsigned long duration;
442 	int done = 0;
443 	bool fw_up;
444 	int opcode;
445 	int err;
446 
447 	/* Wait for dev cmd to complete, retrying if we get EAGAIN,
448 	 * but don't wait any longer than max_seconds.
449 	 */
450 	max_wait = jiffies + (max_seconds * HZ);
451 try_again:
452 	opcode = idev->opcode;
453 	start_time = jiffies;
454 	for (fw_up = ionic_is_fw_running(idev);
455 	     !done && fw_up && time_before(jiffies, max_wait);
456 	     fw_up = ionic_is_fw_running(idev)) {
457 		done = ionic_dev_cmd_done(idev);
458 		if (done)
459 			break;
460 		usleep_range(100, 200);
461 	}
462 	duration = jiffies - start_time;
463 
464 	dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
465 		ionic_opcode_to_str(opcode), opcode,
466 		done, duration / HZ, duration);
467 
468 	if (!done && !fw_up) {
469 		ionic_dev_cmd_clean(ionic);
470 		dev_warn(ionic->dev, "DEVCMD %s (%d) interrupted - FW is down\n",
471 			 ionic_opcode_to_str(opcode), opcode);
472 		return -ENXIO;
473 	}
474 
475 	if (!done && !time_before(jiffies, max_wait)) {
476 		ionic_dev_cmd_clean(ionic);
477 		dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
478 			 ionic_opcode_to_str(opcode), opcode, max_seconds);
479 		return -ETIMEDOUT;
480 	}
481 
482 	err = ionic_dev_cmd_status(&ionic->idev);
483 	if (err) {
484 		if (err == IONIC_RC_EAGAIN &&
485 		    time_before(jiffies, (max_wait - HZ))) {
486 			dev_dbg(ionic->dev, "DEV_CMD %s (%d), %s (%d) retrying...\n",
487 				ionic_opcode_to_str(opcode), opcode,
488 				ionic_error_to_str(err), err);
489 
490 			iowrite32(0, &idev->dev_cmd_regs->done);
491 			msleep(1000);
492 			iowrite32(1, &idev->dev_cmd_regs->doorbell);
493 			goto try_again;
494 		}
495 
496 		if (!(opcode == IONIC_CMD_FW_CONTROL && err == IONIC_RC_EAGAIN))
497 			if (do_msg)
498 				ionic_dev_cmd_dev_err_print(ionic, opcode, err,
499 							    ionic_error_to_errno(err));
500 
501 		return ionic_error_to_errno(err);
502 	}
503 
504 	ionic_dev_cmd_clean(ionic);
505 
506 	return 0;
507 }
508 
509 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
510 {
511 	return __ionic_dev_cmd_wait(ionic, max_seconds, true);
512 }
513 
514 int ionic_dev_cmd_wait_nomsg(struct ionic *ionic, unsigned long max_seconds)
515 {
516 	return __ionic_dev_cmd_wait(ionic, max_seconds, false);
517 }
518 
519 int ionic_setup(struct ionic *ionic)
520 {
521 	int err;
522 
523 	err = ionic_dev_setup(ionic);
524 	if (err)
525 		return err;
526 	ionic_reset(ionic);
527 
528 	return 0;
529 }
530 
531 int ionic_identify(struct ionic *ionic)
532 {
533 	struct ionic_identity *ident = &ionic->ident;
534 	struct ionic_dev *idev = &ionic->idev;
535 	size_t sz;
536 	int err;
537 
538 	memset(ident, 0, sizeof(*ident));
539 
540 	ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
541 	strscpy(ident->drv.driver_ver_str, UTS_RELEASE,
542 		sizeof(ident->drv.driver_ver_str));
543 
544 	mutex_lock(&ionic->dev_cmd_lock);
545 
546 	sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
547 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
548 
549 	ionic_dev_cmd_identify(idev, IONIC_DEV_IDENTITY_VERSION_2);
550 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
551 	if (!err) {
552 		sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
553 		memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
554 	}
555 	mutex_unlock(&ionic->dev_cmd_lock);
556 
557 	if (err) {
558 		dev_err(ionic->dev, "Cannot identify ionic: %d\n", err);
559 		goto err_out;
560 	}
561 
562 	if (isprint(idev->dev_info.fw_version[0]) &&
563 	    isascii(idev->dev_info.fw_version[0]))
564 		dev_info(ionic->dev, "FW: %.*s\n",
565 			 (int)(sizeof(idev->dev_info.fw_version) - 1),
566 			 idev->dev_info.fw_version);
567 	else
568 		dev_info(ionic->dev, "FW: (invalid string) 0x%02x 0x%02x 0x%02x 0x%02x ...\n",
569 			 (u8)idev->dev_info.fw_version[0],
570 			 (u8)idev->dev_info.fw_version[1],
571 			 (u8)idev->dev_info.fw_version[2],
572 			 (u8)idev->dev_info.fw_version[3]);
573 
574 	err = ionic_lif_identify(ionic, IONIC_LIF_TYPE_CLASSIC,
575 				 &ionic->ident.lif);
576 	if (err) {
577 		dev_err(ionic->dev, "Cannot identify LIFs: %d\n", err);
578 		goto err_out;
579 	}
580 
581 	return 0;
582 
583 err_out:
584 	return err;
585 }
586 
587 int ionic_init(struct ionic *ionic)
588 {
589 	struct ionic_dev *idev = &ionic->idev;
590 	int err;
591 
592 	mutex_lock(&ionic->dev_cmd_lock);
593 	ionic_dev_cmd_init(idev);
594 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
595 	mutex_unlock(&ionic->dev_cmd_lock);
596 
597 	return err;
598 }
599 
600 int ionic_reset(struct ionic *ionic)
601 {
602 	struct ionic_dev *idev = &ionic->idev;
603 	int err;
604 
605 	if (!ionic_is_fw_running(idev))
606 		return 0;
607 
608 	mutex_lock(&ionic->dev_cmd_lock);
609 	ionic_dev_cmd_reset(idev);
610 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
611 	mutex_unlock(&ionic->dev_cmd_lock);
612 
613 	return err;
614 }
615 
616 int ionic_port_identify(struct ionic *ionic)
617 {
618 	struct ionic_identity *ident = &ionic->ident;
619 	struct ionic_dev *idev = &ionic->idev;
620 	size_t sz;
621 	int err;
622 
623 	mutex_lock(&ionic->dev_cmd_lock);
624 
625 	ionic_dev_cmd_port_identify(idev);
626 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
627 	if (!err) {
628 		sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
629 		memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
630 	}
631 
632 	mutex_unlock(&ionic->dev_cmd_lock);
633 
634 	return err;
635 }
636 
637 int ionic_port_init(struct ionic *ionic)
638 {
639 	struct ionic_identity *ident = &ionic->ident;
640 	struct ionic_dev *idev = &ionic->idev;
641 	size_t sz;
642 	int err;
643 
644 	if (!idev->port_info) {
645 		idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
646 		idev->port_info = dma_alloc_coherent(ionic->dev,
647 						     idev->port_info_sz,
648 						     &idev->port_info_pa,
649 						     GFP_KERNEL);
650 		if (!idev->port_info)
651 			return -ENOMEM;
652 	}
653 
654 	sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
655 
656 	mutex_lock(&ionic->dev_cmd_lock);
657 
658 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
659 	ionic_dev_cmd_port_init(idev);
660 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
661 
662 	ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
663 	ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
664 
665 	mutex_unlock(&ionic->dev_cmd_lock);
666 	if (err) {
667 		dev_err(ionic->dev, "Failed to init port\n");
668 		dma_free_coherent(ionic->dev, idev->port_info_sz,
669 				  idev->port_info, idev->port_info_pa);
670 		idev->port_info = NULL;
671 		idev->port_info_pa = 0;
672 	}
673 
674 	return err;
675 }
676 
677 int ionic_port_reset(struct ionic *ionic)
678 {
679 	struct ionic_dev *idev = &ionic->idev;
680 	int err = 0;
681 
682 	if (!idev->port_info)
683 		return 0;
684 
685 	if (ionic_is_fw_running(idev)) {
686 		mutex_lock(&ionic->dev_cmd_lock);
687 		ionic_dev_cmd_port_reset(idev);
688 		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
689 		mutex_unlock(&ionic->dev_cmd_lock);
690 	}
691 
692 	dma_free_coherent(ionic->dev, idev->port_info_sz,
693 			  idev->port_info, idev->port_info_pa);
694 
695 	idev->port_info = NULL;
696 	idev->port_info_pa = 0;
697 
698 	return err;
699 }
700 
701 static int __init ionic_init_module(void)
702 {
703 	int ret;
704 
705 	ionic_debugfs_create();
706 	ret = ionic_bus_register_driver();
707 	if (ret)
708 		ionic_debugfs_destroy();
709 
710 	return ret;
711 }
712 
713 static void __exit ionic_cleanup_module(void)
714 {
715 	ionic_bus_unregister_driver();
716 	ionic_debugfs_destroy();
717 
718 	pr_info("%s removed\n", IONIC_DRV_NAME);
719 }
720 
721 module_init(ionic_init_module);
722 module_exit(ionic_cleanup_module);
723