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 
ionic_error_to_str(enum ionic_status_code code)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 
ionic_error_to_errno(enum ionic_status_code code)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 
ionic_opcode_to_str(enum ionic_cmd_opcode opcode)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 
ionic_adminq_flush(struct ionic_lif * lif)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 
ionic_adminq_netdev_err_print(struct ionic_lif * lif,u8 opcode,u8 status,int err)215 void ionic_adminq_netdev_err_print(struct ionic_lif *lif, u8 opcode,
216 				   u8 status, int err)
217 {
218 	netdev_err(lif->netdev, "%s (%d) failed: %s (%d)\n",
219 		   ionic_opcode_to_str(opcode), opcode,
220 		   ionic_error_to_str(status), err);
221 }
222 
ionic_adminq_check_err(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const bool timeout,const bool do_msg)223 static int ionic_adminq_check_err(struct ionic_lif *lif,
224 				  struct ionic_admin_ctx *ctx,
225 				  const bool timeout,
226 				  const bool do_msg)
227 {
228 	int err = 0;
229 
230 	if (ctx->comp.comp.status || timeout) {
231 		err = timeout ? -ETIMEDOUT :
232 				ionic_error_to_errno(ctx->comp.comp.status);
233 
234 		if (do_msg)
235 			ionic_adminq_netdev_err_print(lif, ctx->cmd.cmd.opcode,
236 						      ctx->comp.comp.status, err);
237 
238 		if (timeout)
239 			ionic_adminq_flush(lif);
240 	}
241 
242 	return err;
243 }
244 
ionic_adminq_cb(struct ionic_queue * q,struct ionic_desc_info * desc_info,struct ionic_cq_info * cq_info,void * cb_arg)245 static void ionic_adminq_cb(struct ionic_queue *q,
246 			    struct ionic_desc_info *desc_info,
247 			    struct ionic_cq_info *cq_info, void *cb_arg)
248 {
249 	struct ionic_admin_ctx *ctx = cb_arg;
250 	struct ionic_admin_comp *comp;
251 
252 	if (!ctx)
253 		return;
254 
255 	comp = cq_info->cq_desc;
256 
257 	memcpy(&ctx->comp, comp, sizeof(*comp));
258 
259 	dev_dbg(q->dev, "comp admin queue command:\n");
260 	dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
261 			 &ctx->comp, sizeof(ctx->comp), true);
262 
263 	complete_all(&ctx->work);
264 }
265 
ionic_adminq_post(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)266 int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
267 {
268 	struct ionic_desc_info *desc_info;
269 	unsigned long irqflags;
270 	struct ionic_queue *q;
271 	int err = 0;
272 
273 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
274 	if (!lif->adminqcq) {
275 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
276 		return -EIO;
277 	}
278 
279 	q = &lif->adminqcq->q;
280 
281 	if (!ionic_q_has_space(q, 1)) {
282 		err = -ENOSPC;
283 		goto err_out;
284 	}
285 
286 	err = ionic_heartbeat_check(lif->ionic);
287 	if (err)
288 		goto err_out;
289 
290 	desc_info = &q->info[q->head_idx];
291 	memcpy(desc_info->desc, &ctx->cmd, sizeof(ctx->cmd));
292 
293 	dev_dbg(&lif->netdev->dev, "post admin queue command:\n");
294 	dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
295 			 &ctx->cmd, sizeof(ctx->cmd), true);
296 
297 	ionic_q_post(q, true, ionic_adminq_cb, ctx);
298 
299 err_out:
300 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
301 
302 	return err;
303 }
304 
ionic_adminq_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const int err,const bool do_msg)305 int ionic_adminq_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx,
306 		      const int err, const bool do_msg)
307 {
308 	struct net_device *netdev = lif->netdev;
309 	unsigned long time_limit;
310 	unsigned long time_start;
311 	unsigned long time_done;
312 	unsigned long remaining;
313 	const char *name;
314 
315 	name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
316 
317 	if (err) {
318 		if (do_msg && !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
319 			netdev_err(netdev, "Posting of %s (%d) failed: %d\n",
320 				   name, ctx->cmd.cmd.opcode, err);
321 		return err;
322 	}
323 
324 	time_start = jiffies;
325 	time_limit = time_start + HZ * (ulong)DEVCMD_TIMEOUT;
326 	do {
327 		remaining = wait_for_completion_timeout(&ctx->work,
328 							IONIC_ADMINQ_TIME_SLICE);
329 
330 		/* check for done */
331 		if (remaining)
332 			break;
333 
334 		/* interrupt the wait if FW stopped */
335 		if (test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
336 			if (do_msg)
337 				netdev_err(netdev, "%s (%d) interrupted, FW in reset\n",
338 					   name, ctx->cmd.cmd.opcode);
339 			return -ENXIO;
340 		}
341 
342 	} while (time_before(jiffies, time_limit));
343 	time_done = jiffies;
344 
345 	dev_dbg(lif->ionic->dev, "%s: elapsed %d msecs\n",
346 		__func__, jiffies_to_msecs(time_done - time_start));
347 
348 	return ionic_adminq_check_err(lif, ctx,
349 				      time_after_eq(time_done, time_limit),
350 				      do_msg);
351 }
352 
ionic_adminq_post_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)353 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
354 {
355 	int err;
356 
357 	err = ionic_adminq_post(lif, ctx);
358 
359 	return ionic_adminq_wait(lif, ctx, err, true);
360 }
361 
ionic_adminq_post_wait_nomsg(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)362 int ionic_adminq_post_wait_nomsg(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
363 {
364 	int err;
365 
366 	err = ionic_adminq_post(lif, ctx);
367 
368 	return ionic_adminq_wait(lif, ctx, err, false);
369 }
370 
ionic_dev_cmd_clean(struct ionic * ionic)371 static void ionic_dev_cmd_clean(struct ionic *ionic)
372 {
373 	union __iomem ionic_dev_cmd_regs *regs = ionic->idev.dev_cmd_regs;
374 
375 	iowrite32(0, &regs->doorbell);
376 	memset_io(&regs->cmd, 0, sizeof(regs->cmd));
377 }
378 
ionic_dev_cmd_wait(struct ionic * ionic,unsigned long max_seconds)379 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
380 {
381 	struct ionic_dev *idev = &ionic->idev;
382 	unsigned long start_time;
383 	unsigned long max_wait;
384 	unsigned long duration;
385 	int opcode;
386 	int hb = 0;
387 	int done;
388 	int err;
389 
390 	/* Wait for dev cmd to complete, retrying if we get EAGAIN,
391 	 * but don't wait any longer than max_seconds.
392 	 */
393 	max_wait = jiffies + (max_seconds * HZ);
394 try_again:
395 	opcode = readb(&idev->dev_cmd_regs->cmd.cmd.opcode);
396 	start_time = jiffies;
397 	do {
398 		done = ionic_dev_cmd_done(idev);
399 		if (done)
400 			break;
401 		usleep_range(100, 200);
402 
403 		/* Don't check the heartbeat on FW_CONTROL commands as they are
404 		 * notorious for interrupting the firmware's heartbeat update.
405 		 */
406 		if (opcode != IONIC_CMD_FW_CONTROL)
407 			hb = ionic_heartbeat_check(ionic);
408 	} while (!done && !hb && time_before(jiffies, max_wait));
409 	duration = jiffies - start_time;
410 
411 	dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
412 		ionic_opcode_to_str(opcode), opcode,
413 		done, duration / HZ, duration);
414 
415 	if (!done && hb) {
416 		/* It is possible (but unlikely) that FW was busy and missed a
417 		 * heartbeat check but is still alive and will process this
418 		 * request, so don't clean the dev_cmd in this case.
419 		 */
420 		dev_dbg(ionic->dev, "DEVCMD %s (%d) failed - FW halted\n",
421 			ionic_opcode_to_str(opcode), opcode);
422 		return -ENXIO;
423 	}
424 
425 	if (!done && !time_before(jiffies, max_wait)) {
426 		ionic_dev_cmd_clean(ionic);
427 		dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
428 			 ionic_opcode_to_str(opcode), opcode, max_seconds);
429 		return -ETIMEDOUT;
430 	}
431 
432 	err = ionic_dev_cmd_status(&ionic->idev);
433 	if (err) {
434 		if (err == IONIC_RC_EAGAIN &&
435 		    time_before(jiffies, (max_wait - HZ))) {
436 			dev_dbg(ionic->dev, "DEV_CMD %s (%d), %s (%d) retrying...\n",
437 				ionic_opcode_to_str(opcode), opcode,
438 				ionic_error_to_str(err), err);
439 
440 			msleep(1000);
441 			iowrite32(0, &idev->dev_cmd_regs->done);
442 			iowrite32(1, &idev->dev_cmd_regs->doorbell);
443 			goto try_again;
444 		}
445 
446 		if (!(opcode == IONIC_CMD_FW_CONTROL && err == IONIC_RC_EAGAIN))
447 			dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
448 				ionic_opcode_to_str(opcode), opcode,
449 				ionic_error_to_str(err), err);
450 
451 		return ionic_error_to_errno(err);
452 	}
453 
454 	return 0;
455 }
456 
ionic_setup(struct ionic * ionic)457 int ionic_setup(struct ionic *ionic)
458 {
459 	int err;
460 
461 	err = ionic_dev_setup(ionic);
462 	if (err)
463 		return err;
464 	ionic_reset(ionic);
465 
466 	return 0;
467 }
468 
ionic_identify(struct ionic * ionic)469 int ionic_identify(struct ionic *ionic)
470 {
471 	struct ionic_identity *ident = &ionic->ident;
472 	struct ionic_dev *idev = &ionic->idev;
473 	size_t sz;
474 	int err;
475 
476 	memset(ident, 0, sizeof(*ident));
477 
478 	ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
479 	strncpy(ident->drv.driver_ver_str, UTS_RELEASE,
480 		sizeof(ident->drv.driver_ver_str) - 1);
481 
482 	mutex_lock(&ionic->dev_cmd_lock);
483 
484 	sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
485 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
486 
487 	ionic_dev_cmd_identify(idev, IONIC_IDENTITY_VERSION_1);
488 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
489 	if (!err) {
490 		sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
491 		memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
492 	}
493 	mutex_unlock(&ionic->dev_cmd_lock);
494 
495 	if (err) {
496 		dev_err(ionic->dev, "Cannot identify ionic: %d\n", err);
497 		goto err_out;
498 	}
499 
500 	if (isprint(idev->dev_info.fw_version[0]) &&
501 	    isascii(idev->dev_info.fw_version[0]))
502 		dev_info(ionic->dev, "FW: %.*s\n",
503 			 (int)(sizeof(idev->dev_info.fw_version) - 1),
504 			 idev->dev_info.fw_version);
505 	else
506 		dev_info(ionic->dev, "FW: (invalid string) 0x%02x 0x%02x 0x%02x 0x%02x ...\n",
507 			 (u8)idev->dev_info.fw_version[0],
508 			 (u8)idev->dev_info.fw_version[1],
509 			 (u8)idev->dev_info.fw_version[2],
510 			 (u8)idev->dev_info.fw_version[3]);
511 
512 	err = ionic_lif_identify(ionic, IONIC_LIF_TYPE_CLASSIC,
513 				 &ionic->ident.lif);
514 	if (err) {
515 		dev_err(ionic->dev, "Cannot identify LIFs: %d\n", err);
516 		goto err_out;
517 	}
518 
519 	return 0;
520 
521 err_out:
522 	return err;
523 }
524 
ionic_init(struct ionic * ionic)525 int ionic_init(struct ionic *ionic)
526 {
527 	struct ionic_dev *idev = &ionic->idev;
528 	int err;
529 
530 	mutex_lock(&ionic->dev_cmd_lock);
531 	ionic_dev_cmd_init(idev);
532 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
533 	mutex_unlock(&ionic->dev_cmd_lock);
534 
535 	return err;
536 }
537 
ionic_reset(struct ionic * ionic)538 int ionic_reset(struct ionic *ionic)
539 {
540 	struct ionic_dev *idev = &ionic->idev;
541 	int err;
542 
543 	mutex_lock(&ionic->dev_cmd_lock);
544 	ionic_dev_cmd_reset(idev);
545 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
546 	mutex_unlock(&ionic->dev_cmd_lock);
547 
548 	return err;
549 }
550 
ionic_port_identify(struct ionic * ionic)551 int ionic_port_identify(struct ionic *ionic)
552 {
553 	struct ionic_identity *ident = &ionic->ident;
554 	struct ionic_dev *idev = &ionic->idev;
555 	size_t sz;
556 	int err;
557 
558 	mutex_lock(&ionic->dev_cmd_lock);
559 
560 	ionic_dev_cmd_port_identify(idev);
561 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
562 	if (!err) {
563 		sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
564 		memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
565 	}
566 
567 	mutex_unlock(&ionic->dev_cmd_lock);
568 
569 	return err;
570 }
571 
ionic_port_init(struct ionic * ionic)572 int ionic_port_init(struct ionic *ionic)
573 {
574 	struct ionic_identity *ident = &ionic->ident;
575 	struct ionic_dev *idev = &ionic->idev;
576 	size_t sz;
577 	int err;
578 
579 	if (!idev->port_info) {
580 		idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
581 		idev->port_info = dma_alloc_coherent(ionic->dev,
582 						     idev->port_info_sz,
583 						     &idev->port_info_pa,
584 						     GFP_KERNEL);
585 		if (!idev->port_info)
586 			return -ENOMEM;
587 	}
588 
589 	sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
590 
591 	mutex_lock(&ionic->dev_cmd_lock);
592 
593 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
594 	ionic_dev_cmd_port_init(idev);
595 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
596 
597 	ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
598 	(void)ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
599 
600 	mutex_unlock(&ionic->dev_cmd_lock);
601 	if (err) {
602 		dev_err(ionic->dev, "Failed to init port\n");
603 		dma_free_coherent(ionic->dev, idev->port_info_sz,
604 				  idev->port_info, idev->port_info_pa);
605 		idev->port_info = NULL;
606 		idev->port_info_pa = 0;
607 	}
608 
609 	return err;
610 }
611 
ionic_port_reset(struct ionic * ionic)612 int ionic_port_reset(struct ionic *ionic)
613 {
614 	struct ionic_dev *idev = &ionic->idev;
615 	int err;
616 
617 	if (!idev->port_info)
618 		return 0;
619 
620 	mutex_lock(&ionic->dev_cmd_lock);
621 	ionic_dev_cmd_port_reset(idev);
622 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
623 	mutex_unlock(&ionic->dev_cmd_lock);
624 
625 	dma_free_coherent(ionic->dev, idev->port_info_sz,
626 			  idev->port_info, idev->port_info_pa);
627 
628 	idev->port_info = NULL;
629 	idev->port_info_pa = 0;
630 
631 	if (err)
632 		dev_err(ionic->dev, "Failed to reset port\n");
633 
634 	return err;
635 }
636 
ionic_init_module(void)637 static int __init ionic_init_module(void)
638 {
639 	ionic_debugfs_create();
640 	return ionic_bus_register_driver();
641 }
642 
ionic_cleanup_module(void)643 static void __exit ionic_cleanup_module(void)
644 {
645 	ionic_bus_unregister_driver();
646 	ionic_debugfs_destroy();
647 
648 	pr_info("%s removed\n", IONIC_DRV_NAME);
649 }
650 
651 module_init(ionic_init_module);
652 module_exit(ionic_cleanup_module);
653