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_vf_attr_to_str(enum ionic_vf_attr attr)191 const char *ionic_vf_attr_to_str(enum ionic_vf_attr attr)
192 {
193 	switch (attr) {
194 	case IONIC_VF_ATTR_SPOOFCHK:
195 		return "IONIC_VF_ATTR_SPOOFCHK";
196 	case IONIC_VF_ATTR_TRUST:
197 		return "IONIC_VF_ATTR_TRUST";
198 	case IONIC_VF_ATTR_LINKSTATE:
199 		return "IONIC_VF_ATTR_LINKSTATE";
200 	case IONIC_VF_ATTR_MAC:
201 		return "IONIC_VF_ATTR_MAC";
202 	case IONIC_VF_ATTR_VLAN:
203 		return "IONIC_VF_ATTR_VLAN";
204 	case IONIC_VF_ATTR_RATE:
205 		return "IONIC_VF_ATTR_RATE";
206 	case IONIC_VF_ATTR_STATSADDR:
207 		return "IONIC_VF_ATTR_STATSADDR";
208 	default:
209 		return "IONIC_VF_ATTR_UNKNOWN";
210 	}
211 }
212 
ionic_adminq_flush(struct ionic_lif * lif)213 static void ionic_adminq_flush(struct ionic_lif *lif)
214 {
215 	struct ionic_desc_info *desc_info;
216 	unsigned long irqflags;
217 	struct ionic_queue *q;
218 
219 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
220 	if (!lif->adminqcq) {
221 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
222 		return;
223 	}
224 
225 	q = &lif->adminqcq->q;
226 
227 	while (q->tail_idx != q->head_idx) {
228 		desc_info = &q->info[q->tail_idx];
229 		memset(desc_info->desc, 0, sizeof(union ionic_adminq_cmd));
230 		desc_info->cb = NULL;
231 		desc_info->cb_arg = NULL;
232 		q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
233 	}
234 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
235 }
236 
ionic_adminq_netdev_err_print(struct ionic_lif * lif,u8 opcode,u8 status,int err)237 void ionic_adminq_netdev_err_print(struct ionic_lif *lif, u8 opcode,
238 				   u8 status, int err)
239 {
240 	const char *stat_str;
241 
242 	stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
243 					 ionic_error_to_str(status);
244 
245 	netdev_err(lif->netdev, "%s (%d) failed: %s (%d)\n",
246 		   ionic_opcode_to_str(opcode), opcode, stat_str, err);
247 }
248 
ionic_adminq_check_err(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const bool timeout,const bool do_msg)249 static int ionic_adminq_check_err(struct ionic_lif *lif,
250 				  struct ionic_admin_ctx *ctx,
251 				  const bool timeout,
252 				  const bool do_msg)
253 {
254 	int err = 0;
255 
256 	if (ctx->comp.comp.status || timeout) {
257 		err = timeout ? -ETIMEDOUT :
258 				ionic_error_to_errno(ctx->comp.comp.status);
259 
260 		if (do_msg)
261 			ionic_adminq_netdev_err_print(lif, ctx->cmd.cmd.opcode,
262 						      ctx->comp.comp.status, err);
263 
264 		if (timeout)
265 			ionic_adminq_flush(lif);
266 	}
267 
268 	return err;
269 }
270 
ionic_adminq_cb(struct ionic_queue * q,struct ionic_desc_info * desc_info,struct ionic_cq_info * cq_info,void * cb_arg)271 static void ionic_adminq_cb(struct ionic_queue *q,
272 			    struct ionic_desc_info *desc_info,
273 			    struct ionic_cq_info *cq_info, void *cb_arg)
274 {
275 	struct ionic_admin_ctx *ctx = cb_arg;
276 	struct ionic_admin_comp *comp;
277 
278 	if (!ctx)
279 		return;
280 
281 	comp = cq_info->cq_desc;
282 
283 	memcpy(&ctx->comp, comp, sizeof(*comp));
284 
285 	dev_dbg(q->dev, "comp admin queue command:\n");
286 	dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
287 			 &ctx->comp, sizeof(ctx->comp), true);
288 
289 	complete_all(&ctx->work);
290 }
291 
ionic_adminq_poke_doorbell(struct ionic_queue * q)292 bool ionic_adminq_poke_doorbell(struct ionic_queue *q)
293 {
294 	struct ionic_lif *lif = q->lif;
295 	unsigned long now, then, dif;
296 	unsigned long irqflags;
297 
298 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
299 
300 	if (q->tail_idx == q->head_idx) {
301 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
302 		return false;
303 	}
304 
305 	now = READ_ONCE(jiffies);
306 	then = q->dbell_jiffies;
307 	dif = now - then;
308 
309 	if (dif > q->dbell_deadline) {
310 		ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
311 				 q->dbval | q->head_idx);
312 
313 		q->dbell_jiffies = now;
314 	}
315 
316 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
317 
318 	return true;
319 }
320 
ionic_adminq_post(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)321 int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
322 {
323 	struct ionic_desc_info *desc_info;
324 	unsigned long irqflags;
325 	struct ionic_queue *q;
326 	int err = 0;
327 
328 	spin_lock_irqsave(&lif->adminq_lock, irqflags);
329 	if (!lif->adminqcq) {
330 		spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
331 		return -EIO;
332 	}
333 
334 	q = &lif->adminqcq->q;
335 
336 	if (!ionic_q_has_space(q, 1)) {
337 		err = -ENOSPC;
338 		goto err_out;
339 	}
340 
341 	err = ionic_heartbeat_check(lif->ionic);
342 	if (err)
343 		goto err_out;
344 
345 	desc_info = &q->info[q->head_idx];
346 	memcpy(desc_info->desc, &ctx->cmd, sizeof(ctx->cmd));
347 
348 	dev_dbg(&lif->netdev->dev, "post admin queue command:\n");
349 	dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
350 			 &ctx->cmd, sizeof(ctx->cmd), true);
351 
352 	ionic_q_post(q, true, ionic_adminq_cb, ctx);
353 
354 err_out:
355 	spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
356 
357 	return err;
358 }
359 
ionic_adminq_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const int err,const bool do_msg)360 int ionic_adminq_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx,
361 		      const int err, const bool do_msg)
362 {
363 	struct net_device *netdev = lif->netdev;
364 	unsigned long time_limit;
365 	unsigned long time_start;
366 	unsigned long time_done;
367 	unsigned long remaining;
368 	const char *name;
369 
370 	name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
371 
372 	if (err) {
373 		if (do_msg && !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
374 			netdev_err(netdev, "Posting of %s (%d) failed: %d\n",
375 				   name, ctx->cmd.cmd.opcode, err);
376 		ctx->comp.comp.status = IONIC_RC_ERROR;
377 		return err;
378 	}
379 
380 	time_start = jiffies;
381 	time_limit = time_start + HZ * (ulong)DEVCMD_TIMEOUT;
382 	do {
383 		remaining = wait_for_completion_timeout(&ctx->work,
384 							IONIC_ADMINQ_TIME_SLICE);
385 
386 		/* check for done */
387 		if (remaining)
388 			break;
389 
390 		/* force a check of FW status and break out if FW reset */
391 		ionic_heartbeat_check(lif->ionic);
392 		if ((test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
393 		     !lif->ionic->idev.fw_status_ready) ||
394 		    test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
395 			if (do_msg)
396 				netdev_warn(netdev, "%s (%d) interrupted, FW in reset\n",
397 					    name, ctx->cmd.cmd.opcode);
398 			ctx->comp.comp.status = IONIC_RC_ERROR;
399 			return -ENXIO;
400 		}
401 
402 	} while (time_before(jiffies, time_limit));
403 	time_done = jiffies;
404 
405 	dev_dbg(lif->ionic->dev, "%s: elapsed %d msecs\n",
406 		__func__, jiffies_to_msecs(time_done - time_start));
407 
408 	return ionic_adminq_check_err(lif, ctx,
409 				      time_after_eq(time_done, time_limit),
410 				      do_msg);
411 }
412 
__ionic_adminq_post_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const bool do_msg)413 static int __ionic_adminq_post_wait(struct ionic_lif *lif,
414 				    struct ionic_admin_ctx *ctx,
415 				    const bool do_msg)
416 {
417 	int err;
418 
419 	if (!ionic_is_fw_running(&lif->ionic->idev))
420 		return 0;
421 
422 	err = ionic_adminq_post(lif, ctx);
423 
424 	return ionic_adminq_wait(lif, ctx, err, do_msg);
425 }
426 
ionic_adminq_post_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)427 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
428 {
429 	return __ionic_adminq_post_wait(lif, ctx, true);
430 }
431 
ionic_adminq_post_wait_nomsg(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)432 int ionic_adminq_post_wait_nomsg(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
433 {
434 	return __ionic_adminq_post_wait(lif, ctx, false);
435 }
436 
ionic_dev_cmd_clean(struct ionic * ionic)437 static void ionic_dev_cmd_clean(struct ionic *ionic)
438 {
439 	struct ionic_dev *idev = &ionic->idev;
440 
441 	if (!idev->dev_cmd_regs)
442 		return;
443 
444 	iowrite32(0, &idev->dev_cmd_regs->doorbell);
445 	memset_io(&idev->dev_cmd_regs->cmd, 0, sizeof(idev->dev_cmd_regs->cmd));
446 }
447 
ionic_dev_cmd_dev_err_print(struct ionic * ionic,u8 opcode,u8 status,int err)448 void ionic_dev_cmd_dev_err_print(struct ionic *ionic, u8 opcode, u8 status,
449 				 int err)
450 {
451 	const char *stat_str;
452 
453 	stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
454 					 ionic_error_to_str(status);
455 
456 	dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
457 		ionic_opcode_to_str(opcode), opcode, stat_str, err);
458 }
459 
__ionic_dev_cmd_wait(struct ionic * ionic,unsigned long max_seconds,const bool do_msg)460 static int __ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds,
461 				const bool do_msg)
462 {
463 	struct ionic_dev *idev = &ionic->idev;
464 	unsigned long start_time;
465 	unsigned long max_wait;
466 	unsigned long duration;
467 	int done = 0;
468 	bool fw_up;
469 	int opcode;
470 	int err;
471 
472 	/* Wait for dev cmd to complete, retrying if we get EAGAIN,
473 	 * but don't wait any longer than max_seconds.
474 	 */
475 	max_wait = jiffies + (max_seconds * HZ);
476 try_again:
477 	opcode = idev->opcode;
478 	start_time = jiffies;
479 	for (fw_up = ionic_is_fw_running(idev);
480 	     !done && fw_up && time_before(jiffies, max_wait);
481 	     fw_up = ionic_is_fw_running(idev)) {
482 		done = ionic_dev_cmd_done(idev);
483 		if (done)
484 			break;
485 		usleep_range(100, 200);
486 	}
487 	duration = jiffies - start_time;
488 
489 	dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
490 		ionic_opcode_to_str(opcode), opcode,
491 		done, duration / HZ, duration);
492 
493 	if (!done && !fw_up) {
494 		ionic_dev_cmd_clean(ionic);
495 		dev_warn(ionic->dev, "DEVCMD %s (%d) interrupted - FW is down\n",
496 			 ionic_opcode_to_str(opcode), opcode);
497 		return -ENXIO;
498 	}
499 
500 	if (!done && !time_before(jiffies, max_wait)) {
501 		ionic_dev_cmd_clean(ionic);
502 		dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
503 			 ionic_opcode_to_str(opcode), opcode, max_seconds);
504 		return -ETIMEDOUT;
505 	}
506 
507 	err = ionic_dev_cmd_status(&ionic->idev);
508 	if (err) {
509 		if (err == IONIC_RC_EAGAIN &&
510 		    time_before(jiffies, (max_wait - HZ))) {
511 			dev_dbg(ionic->dev, "DEV_CMD %s (%d), %s (%d) retrying...\n",
512 				ionic_opcode_to_str(opcode), opcode,
513 				ionic_error_to_str(err), err);
514 
515 			iowrite32(0, &idev->dev_cmd_regs->done);
516 			msleep(1000);
517 			iowrite32(1, &idev->dev_cmd_regs->doorbell);
518 			goto try_again;
519 		}
520 
521 		if (!(opcode == IONIC_CMD_FW_CONTROL && err == IONIC_RC_EAGAIN))
522 			if (do_msg)
523 				ionic_dev_cmd_dev_err_print(ionic, opcode, err,
524 							    ionic_error_to_errno(err));
525 
526 		return ionic_error_to_errno(err);
527 	}
528 
529 	ionic_dev_cmd_clean(ionic);
530 
531 	return 0;
532 }
533 
ionic_dev_cmd_wait(struct ionic * ionic,unsigned long max_seconds)534 int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
535 {
536 	return __ionic_dev_cmd_wait(ionic, max_seconds, true);
537 }
538 
ionic_dev_cmd_wait_nomsg(struct ionic * ionic,unsigned long max_seconds)539 int ionic_dev_cmd_wait_nomsg(struct ionic *ionic, unsigned long max_seconds)
540 {
541 	return __ionic_dev_cmd_wait(ionic, max_seconds, false);
542 }
543 
ionic_setup(struct ionic * ionic)544 int ionic_setup(struct ionic *ionic)
545 {
546 	int err;
547 
548 	err = ionic_dev_setup(ionic);
549 	if (err)
550 		return err;
551 	ionic_reset(ionic);
552 
553 	return 0;
554 }
555 
ionic_identify(struct ionic * ionic)556 int ionic_identify(struct ionic *ionic)
557 {
558 	struct ionic_identity *ident = &ionic->ident;
559 	struct ionic_dev *idev = &ionic->idev;
560 	size_t sz;
561 	int err;
562 
563 	memset(ident, 0, sizeof(*ident));
564 
565 	ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
566 	strncpy(ident->drv.driver_ver_str, UTS_RELEASE,
567 		sizeof(ident->drv.driver_ver_str) - 1);
568 
569 	mutex_lock(&ionic->dev_cmd_lock);
570 
571 	sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
572 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
573 
574 	ionic_dev_cmd_identify(idev, IONIC_DEV_IDENTITY_VERSION_2);
575 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
576 	if (!err) {
577 		sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
578 		memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
579 	}
580 	mutex_unlock(&ionic->dev_cmd_lock);
581 
582 	if (err) {
583 		dev_err(ionic->dev, "Cannot identify ionic: %d\n", err);
584 		goto err_out;
585 	}
586 
587 	if (isprint(idev->dev_info.fw_version[0]) &&
588 	    isascii(idev->dev_info.fw_version[0]))
589 		dev_info(ionic->dev, "FW: %.*s\n",
590 			 (int)(sizeof(idev->dev_info.fw_version) - 1),
591 			 idev->dev_info.fw_version);
592 	else
593 		dev_info(ionic->dev, "FW: (invalid string) 0x%02x 0x%02x 0x%02x 0x%02x ...\n",
594 			 (u8)idev->dev_info.fw_version[0],
595 			 (u8)idev->dev_info.fw_version[1],
596 			 (u8)idev->dev_info.fw_version[2],
597 			 (u8)idev->dev_info.fw_version[3]);
598 
599 	err = ionic_lif_identify(ionic, IONIC_LIF_TYPE_CLASSIC,
600 				 &ionic->ident.lif);
601 	if (err) {
602 		dev_err(ionic->dev, "Cannot identify LIFs: %d\n", err);
603 		goto err_out;
604 	}
605 
606 	return 0;
607 
608 err_out:
609 	return err;
610 }
611 
ionic_init(struct ionic * ionic)612 int ionic_init(struct ionic *ionic)
613 {
614 	struct ionic_dev *idev = &ionic->idev;
615 	int err;
616 
617 	mutex_lock(&ionic->dev_cmd_lock);
618 	ionic_dev_cmd_init(idev);
619 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
620 	mutex_unlock(&ionic->dev_cmd_lock);
621 
622 	return err;
623 }
624 
ionic_reset(struct ionic * ionic)625 int ionic_reset(struct ionic *ionic)
626 {
627 	struct ionic_dev *idev = &ionic->idev;
628 	int err;
629 
630 	if (!ionic_is_fw_running(idev))
631 		return 0;
632 
633 	mutex_lock(&ionic->dev_cmd_lock);
634 	ionic_dev_cmd_reset(idev);
635 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
636 	mutex_unlock(&ionic->dev_cmd_lock);
637 
638 	return err;
639 }
640 
ionic_port_identify(struct ionic * ionic)641 int ionic_port_identify(struct ionic *ionic)
642 {
643 	struct ionic_identity *ident = &ionic->ident;
644 	struct ionic_dev *idev = &ionic->idev;
645 	size_t sz;
646 	int err;
647 
648 	mutex_lock(&ionic->dev_cmd_lock);
649 
650 	ionic_dev_cmd_port_identify(idev);
651 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
652 	if (!err) {
653 		sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
654 		memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
655 	}
656 
657 	mutex_unlock(&ionic->dev_cmd_lock);
658 
659 	return err;
660 }
661 
ionic_port_init(struct ionic * ionic)662 int ionic_port_init(struct ionic *ionic)
663 {
664 	struct ionic_identity *ident = &ionic->ident;
665 	struct ionic_dev *idev = &ionic->idev;
666 	size_t sz;
667 	int err;
668 
669 	if (!idev->port_info) {
670 		idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
671 		idev->port_info = dma_alloc_coherent(ionic->dev,
672 						     idev->port_info_sz,
673 						     &idev->port_info_pa,
674 						     GFP_KERNEL);
675 		if (!idev->port_info)
676 			return -ENOMEM;
677 	}
678 
679 	sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
680 
681 	mutex_lock(&ionic->dev_cmd_lock);
682 
683 	memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
684 	ionic_dev_cmd_port_init(idev);
685 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
686 
687 	ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
688 	ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
689 
690 	mutex_unlock(&ionic->dev_cmd_lock);
691 	if (err) {
692 		dev_err(ionic->dev, "Failed to init port\n");
693 		dma_free_coherent(ionic->dev, idev->port_info_sz,
694 				  idev->port_info, idev->port_info_pa);
695 		idev->port_info = NULL;
696 		idev->port_info_pa = 0;
697 	}
698 
699 	return err;
700 }
701 
ionic_port_reset(struct ionic * ionic)702 int ionic_port_reset(struct ionic *ionic)
703 {
704 	struct ionic_dev *idev = &ionic->idev;
705 	int err = 0;
706 
707 	if (!idev->port_info)
708 		return 0;
709 
710 	if (ionic_is_fw_running(idev)) {
711 		mutex_lock(&ionic->dev_cmd_lock);
712 		ionic_dev_cmd_port_reset(idev);
713 		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
714 		mutex_unlock(&ionic->dev_cmd_lock);
715 	}
716 
717 	dma_free_coherent(ionic->dev, idev->port_info_sz,
718 			  idev->port_info, idev->port_info_pa);
719 
720 	idev->port_info = NULL;
721 	idev->port_info_pa = 0;
722 
723 	return err;
724 }
725 
ionic_init_module(void)726 static int __init ionic_init_module(void)
727 {
728 	int ret;
729 
730 	ionic_debugfs_create();
731 	ret = ionic_bus_register_driver();
732 	if (ret)
733 		ionic_debugfs_destroy();
734 
735 	return ret;
736 }
737 
ionic_cleanup_module(void)738 static void __exit ionic_cleanup_module(void)
739 {
740 	ionic_bus_unregister_driver();
741 	ionic_debugfs_destroy();
742 
743 	pr_info("%s removed\n", IONIC_DRV_NAME);
744 }
745 
746 module_init(ionic_init_module);
747 module_exit(ionic_cleanup_module);
748