xref: /linux/drivers/net/ethernet/pensando/ionic/ionic_main.c (revision 6093a688a07da07808f0122f9aa2a3eed250d853)
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("Shannon Nelson <shannon.nelson@amd.com>");
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 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 		return -EINVAL;
85 	case IONIC_RC_ENOSUPP:
86 		return -EOPNOTSUPP;
87 	case IONIC_RC_EPERM:
88 		return -EPERM;
89 	case IONIC_RC_ENOENT:
90 		return -ENOENT;
91 	case IONIC_RC_EAGAIN:
92 		return -EAGAIN;
93 	case IONIC_RC_ENOMEM:
94 		return -ENOMEM;
95 	case IONIC_RC_EFAULT:
96 		return -EFAULT;
97 	case IONIC_RC_EBUSY:
98 		return -EBUSY;
99 	case IONIC_RC_EEXIST:
100 		return -EEXIST;
101 	case IONIC_RC_ENOSPC:
102 		return -ENOSPC;
103 	case IONIC_RC_ERANGE:
104 		return -ERANGE;
105 	case IONIC_RC_BAD_ADDR:
106 		return -EFAULT;
107 	case IONIC_RC_EOPCODE:
108 	case IONIC_RC_EINTR:
109 	case IONIC_RC_DEV_CMD:
110 	case IONIC_RC_ERROR:
111 	case IONIC_RC_ERDMA:
112 	case IONIC_RC_EIO:
113 	default:
114 		return -EIO;
115 	}
116 }
117 EXPORT_SYMBOL_NS(ionic_error_to_errno, "NET_IONIC");
118 
119 static const char *ionic_opcode_to_str(enum ionic_cmd_opcode opcode)
120 {
121 	switch (opcode) {
122 	case IONIC_CMD_NOP:
123 		return "IONIC_CMD_NOP";
124 	case IONIC_CMD_INIT:
125 		return "IONIC_CMD_INIT";
126 	case IONIC_CMD_RESET:
127 		return "IONIC_CMD_RESET";
128 	case IONIC_CMD_IDENTIFY:
129 		return "IONIC_CMD_IDENTIFY";
130 	case IONIC_CMD_GETATTR:
131 		return "IONIC_CMD_GETATTR";
132 	case IONIC_CMD_SETATTR:
133 		return "IONIC_CMD_SETATTR";
134 	case IONIC_CMD_PORT_IDENTIFY:
135 		return "IONIC_CMD_PORT_IDENTIFY";
136 	case IONIC_CMD_PORT_INIT:
137 		return "IONIC_CMD_PORT_INIT";
138 	case IONIC_CMD_PORT_RESET:
139 		return "IONIC_CMD_PORT_RESET";
140 	case IONIC_CMD_PORT_GETATTR:
141 		return "IONIC_CMD_PORT_GETATTR";
142 	case IONIC_CMD_PORT_SETATTR:
143 		return "IONIC_CMD_PORT_SETATTR";
144 	case IONIC_CMD_LIF_INIT:
145 		return "IONIC_CMD_LIF_INIT";
146 	case IONIC_CMD_LIF_RESET:
147 		return "IONIC_CMD_LIF_RESET";
148 	case IONIC_CMD_LIF_IDENTIFY:
149 		return "IONIC_CMD_LIF_IDENTIFY";
150 	case IONIC_CMD_LIF_SETATTR:
151 		return "IONIC_CMD_LIF_SETATTR";
152 	case IONIC_CMD_LIF_GETATTR:
153 		return "IONIC_CMD_LIF_GETATTR";
154 	case IONIC_CMD_LIF_SETPHC:
155 		return "IONIC_CMD_LIF_SETPHC";
156 	case IONIC_CMD_RX_MODE_SET:
157 		return "IONIC_CMD_RX_MODE_SET";
158 	case IONIC_CMD_RX_FILTER_ADD:
159 		return "IONIC_CMD_RX_FILTER_ADD";
160 	case IONIC_CMD_RX_FILTER_DEL:
161 		return "IONIC_CMD_RX_FILTER_DEL";
162 	case IONIC_CMD_Q_IDENTIFY:
163 		return "IONIC_CMD_Q_IDENTIFY";
164 	case IONIC_CMD_Q_INIT:
165 		return "IONIC_CMD_Q_INIT";
166 	case IONIC_CMD_Q_CONTROL:
167 		return "IONIC_CMD_Q_CONTROL";
168 	case IONIC_CMD_RDMA_RESET_LIF:
169 		return "IONIC_CMD_RDMA_RESET_LIF";
170 	case IONIC_CMD_RDMA_CREATE_EQ:
171 		return "IONIC_CMD_RDMA_CREATE_EQ";
172 	case IONIC_CMD_RDMA_CREATE_CQ:
173 		return "IONIC_CMD_RDMA_CREATE_CQ";
174 	case IONIC_CMD_RDMA_CREATE_ADMINQ:
175 		return "IONIC_CMD_RDMA_CREATE_ADMINQ";
176 	case IONIC_CMD_FW_DOWNLOAD:
177 		return "IONIC_CMD_FW_DOWNLOAD";
178 	case IONIC_CMD_FW_CONTROL:
179 		return "IONIC_CMD_FW_CONTROL";
180 	case IONIC_CMD_FW_DOWNLOAD_V1:
181 		return "IONIC_CMD_FW_DOWNLOAD_V1";
182 	case IONIC_CMD_FW_CONTROL_V1:
183 		return "IONIC_CMD_FW_CONTROL_V1";
184 	case IONIC_CMD_VF_GETATTR:
185 		return "IONIC_CMD_VF_GETATTR";
186 	case IONIC_CMD_VF_SETATTR:
187 		return "IONIC_CMD_VF_SETATTR";
188 	default:
189 		return "DEVCMD_UNKNOWN";
190 	}
191 }
192 
193 static void ionic_adminq_flush(struct ionic_lif *lif)
194 {
195 	struct ionic_admin_desc_info *desc_info;
196 	struct ionic_admin_cmd *desc;
197 	unsigned long irqflags;
198 	struct ionic_queue *q;
199 
200 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
201 	if (!lif->adminqcq) {
202 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
203 		return;
204 	}
205 
206 	q = &lif->adminqcq->q;
207 
208 	while (q->tail_idx != q->head_idx) {
209 		desc = &q->adminq[q->tail_idx];
210 		desc_info = &q->admin_info[q->tail_idx];
211 		memset(desc, 0, sizeof(union ionic_adminq_cmd));
212 		desc_info->ctx = NULL;
213 		q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
214 	}
215 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
216 }
217 
218 void ionic_adminq_netdev_err_print(struct ionic_lif *lif, u8 opcode,
219 				   u8 status, int err)
220 {
221 	const char *stat_str;
222 
223 	stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
224 					 ionic_error_to_str(status);
225 
226 	netdev_err(lif->netdev, "%s (%d) failed: %s (%d)\n",
227 		   ionic_opcode_to_str(opcode), opcode, stat_str, err);
228 }
229 
230 static int ionic_adminq_check_err(struct ionic_lif *lif,
231 				  struct ionic_admin_ctx *ctx,
232 				  const bool timeout,
233 				  const bool do_msg)
234 {
235 	int err = 0;
236 
237 	if (ctx->comp.comp.status || timeout) {
238 		err = timeout ? -ETIMEDOUT :
239 				ionic_error_to_errno(ctx->comp.comp.status);
240 
241 		if (do_msg)
242 			ionic_adminq_netdev_err_print(lif, ctx->cmd.cmd.opcode,
243 						      ctx->comp.comp.status, err);
244 
245 		if (timeout)
246 			ionic_adminq_flush(lif);
247 	}
248 
249 	return err;
250 }
251 
252 bool ionic_notifyq_service(struct ionic_cq *cq)
253 {
254 	struct ionic_deferred_work *work;
255 	union ionic_notifyq_comp *comp;
256 	struct net_device *netdev;
257 	struct ionic_queue *q;
258 	struct ionic_lif *lif;
259 	u64 eid;
260 
261 	comp = &((union ionic_notifyq_comp *)cq->base)[cq->tail_idx];
262 
263 	q = cq->bound_q;
264 	lif = q->admin_info[0].ctx;
265 	netdev = lif->netdev;
266 	eid = le64_to_cpu(comp->event.eid);
267 
268 	/* Have we run out of new completions to process? */
269 	if ((s64)(eid - lif->last_eid) <= 0)
270 		return false;
271 
272 	lif->last_eid = eid;
273 
274 	dev_dbg(lif->ionic->dev, "notifyq event:\n");
275 	dynamic_hex_dump("event ", DUMP_PREFIX_OFFSET, 16, 1,
276 			 comp, sizeof(*comp), true);
277 
278 	switch (le16_to_cpu(comp->event.ecode)) {
279 	case IONIC_EVENT_LINK_CHANGE:
280 		ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
281 		break;
282 	case IONIC_EVENT_RESET:
283 		if (lif->ionic->idev.fw_status_ready &&
284 		    !test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
285 		    !test_and_set_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
286 			work = kzalloc(sizeof(*work), GFP_ATOMIC);
287 			if (!work) {
288 				netdev_err(lif->netdev, "Reset event dropped\n");
289 				clear_bit(IONIC_LIF_F_FW_STOPPING, lif->state);
290 			} else {
291 				work->type = IONIC_DW_TYPE_LIF_RESET;
292 				ionic_lif_deferred_enqueue(lif, work);
293 			}
294 		}
295 		break;
296 	default:
297 		netdev_warn(netdev, "Notifyq event ecode=%d eid=%lld\n",
298 			    comp->event.ecode, eid);
299 		break;
300 	}
301 
302 	return true;
303 }
304 
305 bool ionic_adminq_service(struct ionic_cq *cq)
306 {
307 	struct ionic_admin_desc_info *desc_info;
308 	struct ionic_queue *q = cq->bound_q;
309 	struct ionic_admin_comp *comp;
310 	u16 index;
311 
312 	comp = &((struct ionic_admin_comp *)cq->base)[cq->tail_idx];
313 
314 	if (!color_match(comp->color, cq->done_color))
315 		return false;
316 
317 	/* check for empty queue */
318 	if (q->tail_idx == q->head_idx)
319 		return false;
320 
321 	do {
322 		desc_info = &q->admin_info[q->tail_idx];
323 		index = q->tail_idx;
324 		q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
325 		if (likely(desc_info->ctx)) {
326 			struct ionic_admin_ctx *ctx = desc_info->ctx;
327 
328 			memcpy(&ctx->comp, comp, sizeof(*comp));
329 
330 			dev_dbg(q->dev, "comp admin queue command:\n");
331 			dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
332 					 &ctx->comp, sizeof(ctx->comp), true);
333 			complete_all(&ctx->work);
334 			desc_info->ctx = NULL;
335 		}
336 	} while (index != le16_to_cpu(comp->comp_index));
337 
338 	return true;
339 }
340 
341 bool ionic_adminq_poke_doorbell(struct ionic_queue *q)
342 {
343 	struct ionic_lif *lif = q->lif;
344 	unsigned long now, then, dif;
345 	unsigned long irqflags;
346 
347 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
348 
349 	if (q->tail_idx == q->head_idx) {
350 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
351 		return false;
352 	}
353 
354 	now = READ_ONCE(jiffies);
355 	then = q->dbell_jiffies;
356 	dif = now - then;
357 
358 	if (dif > q->dbell_deadline) {
359 		ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
360 				 q->dbval | q->head_idx);
361 
362 		q->dbell_jiffies = now;
363 	}
364 
365 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
366 
367 	return true;
368 }
369 
370 int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
371 {
372 	struct ionic_admin_desc_info *desc_info;
373 	struct ionic_admin_cmd *desc;
374 	unsigned long irqflags;
375 	struct ionic_queue *q;
376 	int err = 0;
377 
378 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
379 	if (!lif->adminqcq) {
380 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
381 		return -EIO;
382 	}
383 
384 	q = &lif->adminqcq->q;
385 
386 	if (!ionic_q_has_space(q, 1)) {
387 		err = -ENOSPC;
388 		goto err_out;
389 	}
390 
391 	err = ionic_heartbeat_check(lif->ionic);
392 	if (err)
393 		goto err_out;
394 
395 	desc_info = &q->admin_info[q->head_idx];
396 	desc_info->ctx = ctx;
397 
398 	desc = &q->adminq[q->head_idx];
399 	memcpy(desc, &ctx->cmd, sizeof(ctx->cmd));
400 
401 	dev_dbg(&lif->netdev->dev, "post admin queue command:\n");
402 	dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
403 			 &ctx->cmd, sizeof(ctx->cmd), true);
404 
405 	ionic_q_post(q, true);
406 
407 err_out:
408 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
409 
410 	return err;
411 }
412 
413 int ionic_adminq_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx,
414 		      const int err, const bool do_msg)
415 {
416 	struct net_device *netdev = lif->netdev;
417 	unsigned long time_limit;
418 	unsigned long time_start;
419 	unsigned long time_done;
420 	unsigned long remaining;
421 	const char *name;
422 
423 	name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
424 
425 	if (err) {
426 		if (do_msg && !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
427 			netdev_err(netdev, "Posting of %s (%d) failed: %d\n",
428 				   name, ctx->cmd.cmd.opcode, err);
429 		ctx->comp.comp.status = IONIC_RC_ERROR;
430 		return err;
431 	}
432 
433 	time_start = jiffies;
434 	time_limit = time_start + HZ * (ulong)DEVCMD_TIMEOUT;
435 	do {
436 		remaining = wait_for_completion_timeout(&ctx->work,
437 							IONIC_ADMINQ_TIME_SLICE);
438 
439 		/* check for done */
440 		if (remaining)
441 			break;
442 
443 		/* force a check of FW status and break out if FW reset */
444 		ionic_heartbeat_check(lif->ionic);
445 		if ((test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
446 		     !lif->ionic->idev.fw_status_ready) ||
447 		    test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
448 			if (do_msg)
449 				netdev_warn(netdev, "%s (%d) interrupted, FW in reset\n",
450 					    name, ctx->cmd.cmd.opcode);
451 			ctx->comp.comp.status = IONIC_RC_ERROR;
452 			return -ENXIO;
453 		}
454 
455 	} while (time_before(jiffies, time_limit));
456 	time_done = jiffies;
457 
458 	dev_dbg(lif->ionic->dev, "%s: elapsed %d msecs\n",
459 		__func__, jiffies_to_msecs(time_done - time_start));
460 
461 	return ionic_adminq_check_err(lif, ctx,
462 				      time_after_eq(time_done, time_limit),
463 				      do_msg);
464 }
465 
466 static int __ionic_adminq_post_wait(struct ionic_lif *lif,
467 				    struct ionic_admin_ctx *ctx,
468 				    const bool do_msg)
469 {
470 	int err;
471 
472 	if (!ionic_is_fw_running(&lif->ionic->idev))
473 		return 0;
474 
475 	err = ionic_adminq_post(lif, ctx);
476 
477 	return ionic_adminq_wait(lif, ctx, err, do_msg);
478 }
479 
480 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
481 {
482 	return __ionic_adminq_post_wait(lif, ctx, true);
483 }
484 EXPORT_SYMBOL_NS(ionic_adminq_post_wait, "NET_IONIC");
485 
486 int ionic_adminq_post_wait_nomsg(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
487 {
488 	return __ionic_adminq_post_wait(lif, ctx, false);
489 }
490 
491 static void ionic_dev_cmd_clean(struct ionic *ionic)
492 {
493 	struct ionic_dev *idev = &ionic->idev;
494 
495 	if (!idev->dev_cmd_regs)
496 		return;
497 
498 	iowrite32(0, &idev->dev_cmd_regs->doorbell);
499 	memset_io(&idev->dev_cmd_regs->cmd, 0, sizeof(idev->dev_cmd_regs->cmd));
500 }
501 
502 void ionic_dev_cmd_dev_err_print(struct ionic *ionic, u8 opcode, u8 status,
503 				 int err)
504 {
505 	const char *stat_str;
506 
507 	stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
508 					 ionic_error_to_str(status);
509 
510 	dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
511 		ionic_opcode_to_str(opcode), opcode, stat_str, err);
512 }
513 
514 static int __ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds,
515 				const bool do_msg)
516 {
517 	struct ionic_dev *idev = &ionic->idev;
518 	unsigned long start_time;
519 	unsigned long max_wait;
520 	unsigned long duration;
521 	bool fw_up;
522 	int opcode;
523 	bool done;
524 	int err;
525 
526 	/* Wait for dev cmd to complete, retrying if we get EAGAIN,
527 	 * but don't wait any longer than max_seconds.
528 	 */
529 	max_wait = jiffies + (max_seconds * HZ);
530 try_again:
531 	done = false;
532 	opcode = idev->opcode;
533 	start_time = jiffies;
534 	for (fw_up = ionic_is_fw_running(idev);
535 	     !done && fw_up && time_before(jiffies, max_wait);
536 	     fw_up = ionic_is_fw_running(idev)) {
537 		done = ionic_dev_cmd_done(idev);
538 		if (done)
539 			break;
540 		usleep_range(100, 200);
541 	}
542 	duration = jiffies - start_time;
543 
544 	dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
545 		ionic_opcode_to_str(opcode), opcode,
546 		done, duration / HZ, duration);
547 
548 	if (!done && !fw_up) {
549 		ionic_dev_cmd_clean(ionic);
550 		dev_warn(ionic->dev, "DEVCMD %s (%d) interrupted - FW is down\n",
551 			 ionic_opcode_to_str(opcode), opcode);
552 		return -ENXIO;
553 	}
554 
555 	if (!done && !time_before(jiffies, max_wait)) {
556 		ionic_dev_cmd_clean(ionic);
557 		dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
558 			 ionic_opcode_to_str(opcode), opcode, max_seconds);
559 		return -ETIMEDOUT;
560 	}
561 
562 	err = ionic_dev_cmd_status(&ionic->idev);
563 	if (err) {
564 		if (err == IONIC_RC_EAGAIN &&
565 		    time_before(jiffies, (max_wait - HZ))) {
566 			dev_dbg(ionic->dev, "DEV_CMD %s (%d), %s (%d) retrying...\n",
567 				ionic_opcode_to_str(opcode), opcode,
568 				ionic_error_to_str(err), err);
569 
570 			iowrite32(0, &idev->dev_cmd_regs->done);
571 			msleep(1000);
572 			iowrite32(1, &idev->dev_cmd_regs->doorbell);
573 			goto try_again;
574 		}
575 
576 		if (!(opcode == IONIC_CMD_FW_CONTROL && err == IONIC_RC_EAGAIN))
577 			if (do_msg)
578 				ionic_dev_cmd_dev_err_print(ionic, opcode, err,
579 							    ionic_error_to_errno(err));
580 
581 		return ionic_error_to_errno(err);
582 	}
583 
584 	ionic_dev_cmd_clean(ionic);
585 
586 	return 0;
587 }
588 
589 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
590 {
591 	return __ionic_dev_cmd_wait(ionic, max_seconds, true);
592 }
593 
594 int ionic_dev_cmd_wait_nomsg(struct ionic *ionic, unsigned long max_seconds)
595 {
596 	return __ionic_dev_cmd_wait(ionic, max_seconds, false);
597 }
598 
599 int ionic_setup(struct ionic *ionic)
600 {
601 	int err;
602 
603 	err = ionic_dev_setup(ionic);
604 	if (err)
605 		return err;
606 	ionic_reset(ionic);
607 
608 	return 0;
609 }
610 
611 int ionic_identify(struct ionic *ionic)
612 {
613 	struct ionic_identity *ident = &ionic->ident;
614 	struct ionic_dev *idev = &ionic->idev;
615 	size_t sz;
616 	int err;
617 
618 	memset(ident, 0, sizeof(*ident));
619 
620 	ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
621 	strscpy(ident->drv.driver_ver_str, UTS_RELEASE,
622 		sizeof(ident->drv.driver_ver_str));
623 
624 	mutex_lock(&ionic->dev_cmd_lock);
625 
626 	sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
627 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
628 
629 	ionic_dev_cmd_identify(idev, IONIC_DEV_IDENTITY_VERSION_2);
630 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
631 	if (!err) {
632 		sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
633 		memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
634 	}
635 	mutex_unlock(&ionic->dev_cmd_lock);
636 
637 	if (err) {
638 		dev_err(ionic->dev, "Cannot identify ionic: %d\n", err);
639 		goto err_out;
640 	}
641 
642 	if (isprint(idev->dev_info.fw_version[0]) &&
643 	    isascii(idev->dev_info.fw_version[0]))
644 		dev_info(ionic->dev, "FW: %.*s\n",
645 			 (int)(sizeof(idev->dev_info.fw_version) - 1),
646 			 idev->dev_info.fw_version);
647 	else
648 		dev_info(ionic->dev, "FW: (invalid string) 0x%02x 0x%02x 0x%02x 0x%02x ...\n",
649 			 (u8)idev->dev_info.fw_version[0],
650 			 (u8)idev->dev_info.fw_version[1],
651 			 (u8)idev->dev_info.fw_version[2],
652 			 (u8)idev->dev_info.fw_version[3]);
653 
654 	err = ionic_lif_identify(ionic, IONIC_LIF_TYPE_CLASSIC,
655 				 &ionic->ident.lif);
656 	if (err) {
657 		dev_err(ionic->dev, "Cannot identify LIFs: %d\n", err);
658 		goto err_out;
659 	}
660 
661 	return 0;
662 
663 err_out:
664 	return err;
665 }
666 
667 int ionic_init(struct ionic *ionic)
668 {
669 	struct ionic_dev *idev = &ionic->idev;
670 	int err;
671 
672 	mutex_lock(&ionic->dev_cmd_lock);
673 	ionic_dev_cmd_init(idev);
674 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
675 	mutex_unlock(&ionic->dev_cmd_lock);
676 
677 	return err;
678 }
679 
680 int ionic_reset(struct ionic *ionic)
681 {
682 	struct ionic_dev *idev = &ionic->idev;
683 	int err;
684 
685 	if (!ionic_is_fw_running(idev))
686 		return 0;
687 
688 	mutex_lock(&ionic->dev_cmd_lock);
689 	ionic_dev_cmd_reset(idev);
690 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
691 	mutex_unlock(&ionic->dev_cmd_lock);
692 
693 	return err;
694 }
695 
696 int ionic_port_identify(struct ionic *ionic)
697 {
698 	struct ionic_identity *ident = &ionic->ident;
699 	struct ionic_dev *idev = &ionic->idev;
700 	size_t sz;
701 	int err;
702 
703 	mutex_lock(&ionic->dev_cmd_lock);
704 
705 	ionic_dev_cmd_port_identify(idev);
706 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
707 	if (!err) {
708 		sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
709 		memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
710 	}
711 
712 	mutex_unlock(&ionic->dev_cmd_lock);
713 
714 	return err;
715 }
716 
717 int ionic_port_init(struct ionic *ionic)
718 {
719 	struct ionic_identity *ident = &ionic->ident;
720 	struct ionic_dev *idev = &ionic->idev;
721 	size_t sz;
722 	int err;
723 
724 	if (!idev->port_info) {
725 		idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
726 		idev->port_info = dma_alloc_coherent(ionic->dev,
727 						     idev->port_info_sz,
728 						     &idev->port_info_pa,
729 						     GFP_KERNEL);
730 		if (!idev->port_info)
731 			return -ENOMEM;
732 	}
733 
734 	sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
735 
736 	mutex_lock(&ionic->dev_cmd_lock);
737 
738 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
739 	ionic_dev_cmd_port_init(idev);
740 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
741 
742 	ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
743 	ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
744 
745 	mutex_unlock(&ionic->dev_cmd_lock);
746 	if (err) {
747 		dev_err(ionic->dev, "Failed to init port\n");
748 		dma_free_coherent(ionic->dev, idev->port_info_sz,
749 				  idev->port_info, idev->port_info_pa);
750 		idev->port_info = NULL;
751 		idev->port_info_pa = 0;
752 	}
753 
754 	return err;
755 }
756 
757 int ionic_port_reset(struct ionic *ionic)
758 {
759 	struct ionic_dev *idev = &ionic->idev;
760 	int err = 0;
761 
762 	if (!idev->port_info)
763 		return 0;
764 
765 	if (ionic_is_fw_running(idev)) {
766 		mutex_lock(&ionic->dev_cmd_lock);
767 		ionic_dev_cmd_port_reset(idev);
768 		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
769 		mutex_unlock(&ionic->dev_cmd_lock);
770 	}
771 
772 	dma_free_coherent(ionic->dev, idev->port_info_sz,
773 			  idev->port_info, idev->port_info_pa);
774 
775 	idev->port_info = NULL;
776 	idev->port_info_pa = 0;
777 
778 	return err;
779 }
780 
781 static int __init ionic_init_module(void)
782 {
783 	int ret;
784 
785 	ionic_debugfs_create();
786 	ret = ionic_bus_register_driver();
787 	if (ret)
788 		ionic_debugfs_destroy();
789 
790 	return ret;
791 }
792 
793 static void __exit ionic_cleanup_module(void)
794 {
795 	ionic_bus_unregister_driver();
796 	ionic_debugfs_destroy();
797 
798 	pr_info("%s removed\n", IONIC_DRV_NAME);
799 }
800 
801 module_init(ionic_init_module);
802 module_exit(ionic_cleanup_module);
803