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