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
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_admin_desc_info *desc_info;
194 struct ionic_admin_cmd *desc;
195 unsigned long irqflags;
196 struct ionic_queue *q;
197
198 spin_lock_irqsave(&lif->adminq_lock, irqflags);
199 if (!lif->adminqcq) {
200 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
201 return;
202 }
203
204 q = &lif->adminqcq->q;
205
206 while (q->tail_idx != q->head_idx) {
207 desc = &q->adminq[q->tail_idx];
208 desc_info = &q->admin_info[q->tail_idx];
209 memset(desc, 0, sizeof(union ionic_adminq_cmd));
210 desc_info->ctx = NULL;
211 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
212 }
213 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
214 }
215
ionic_adminq_netdev_err_print(struct ionic_lif * lif,u8 opcode,u8 status,int err)216 void ionic_adminq_netdev_err_print(struct ionic_lif *lif, u8 opcode,
217 u8 status, int err)
218 {
219 const char *stat_str;
220
221 stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
222 ionic_error_to_str(status);
223
224 netdev_err(lif->netdev, "%s (%d) failed: %s (%d)\n",
225 ionic_opcode_to_str(opcode), opcode, stat_str, err);
226 }
227
ionic_adminq_check_err(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const bool timeout,const bool do_msg)228 static int ionic_adminq_check_err(struct ionic_lif *lif,
229 struct ionic_admin_ctx *ctx,
230 const bool timeout,
231 const bool do_msg)
232 {
233 int err = 0;
234
235 if (ctx->comp.comp.status || timeout) {
236 err = timeout ? -ETIMEDOUT :
237 ionic_error_to_errno(ctx->comp.comp.status);
238
239 if (do_msg)
240 ionic_adminq_netdev_err_print(lif, ctx->cmd.cmd.opcode,
241 ctx->comp.comp.status, err);
242
243 if (timeout)
244 ionic_adminq_flush(lif);
245 }
246
247 return err;
248 }
249
ionic_notifyq_service(struct ionic_cq * cq)250 bool ionic_notifyq_service(struct ionic_cq *cq)
251 {
252 struct ionic_deferred_work *work;
253 union ionic_notifyq_comp *comp;
254 struct net_device *netdev;
255 struct ionic_queue *q;
256 struct ionic_lif *lif;
257 u64 eid;
258
259 comp = &((union ionic_notifyq_comp *)cq->base)[cq->tail_idx];
260
261 q = cq->bound_q;
262 lif = q->admin_info[0].ctx;
263 netdev = lif->netdev;
264 eid = le64_to_cpu(comp->event.eid);
265
266 /* Have we run out of new completions to process? */
267 if ((s64)(eid - lif->last_eid) <= 0)
268 return false;
269
270 lif->last_eid = eid;
271
272 dev_dbg(lif->ionic->dev, "notifyq event:\n");
273 dynamic_hex_dump("event ", DUMP_PREFIX_OFFSET, 16, 1,
274 comp, sizeof(*comp), true);
275
276 switch (le16_to_cpu(comp->event.ecode)) {
277 case IONIC_EVENT_LINK_CHANGE:
278 ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
279 break;
280 case IONIC_EVENT_RESET:
281 if (lif->ionic->idev.fw_status_ready &&
282 !test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
283 !test_and_set_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
284 work = kzalloc(sizeof(*work), GFP_ATOMIC);
285 if (!work) {
286 netdev_err(lif->netdev, "Reset event dropped\n");
287 clear_bit(IONIC_LIF_F_FW_STOPPING, lif->state);
288 } else {
289 work->type = IONIC_DW_TYPE_LIF_RESET;
290 ionic_lif_deferred_enqueue(lif, work);
291 }
292 }
293 break;
294 default:
295 netdev_warn(netdev, "Notifyq event ecode=%d eid=%lld\n",
296 comp->event.ecode, eid);
297 break;
298 }
299
300 return true;
301 }
302
ionic_adminq_service(struct ionic_cq * cq)303 bool ionic_adminq_service(struct ionic_cq *cq)
304 {
305 struct ionic_admin_desc_info *desc_info;
306 struct ionic_queue *q = cq->bound_q;
307 struct ionic_admin_comp *comp;
308 u16 index;
309
310 comp = &((struct ionic_admin_comp *)cq->base)[cq->tail_idx];
311
312 if (!color_match(comp->color, cq->done_color))
313 return false;
314
315 /* check for empty queue */
316 if (q->tail_idx == q->head_idx)
317 return false;
318
319 do {
320 desc_info = &q->admin_info[q->tail_idx];
321 index = q->tail_idx;
322 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
323 if (likely(desc_info->ctx)) {
324 struct ionic_admin_ctx *ctx = desc_info->ctx;
325
326 memcpy(&ctx->comp, comp, sizeof(*comp));
327
328 dev_dbg(q->dev, "comp admin queue command:\n");
329 dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
330 &ctx->comp, sizeof(ctx->comp), true);
331 complete_all(&ctx->work);
332 desc_info->ctx = NULL;
333 }
334 } while (index != le16_to_cpu(comp->comp_index));
335
336 return true;
337 }
338
ionic_adminq_poke_doorbell(struct ionic_queue * q)339 bool ionic_adminq_poke_doorbell(struct ionic_queue *q)
340 {
341 struct ionic_lif *lif = q->lif;
342 unsigned long now, then, dif;
343 unsigned long irqflags;
344
345 spin_lock_irqsave(&lif->adminq_lock, irqflags);
346
347 if (q->tail_idx == q->head_idx) {
348 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
349 return false;
350 }
351
352 now = READ_ONCE(jiffies);
353 then = q->dbell_jiffies;
354 dif = now - then;
355
356 if (dif > q->dbell_deadline) {
357 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
358 q->dbval | q->head_idx);
359
360 q->dbell_jiffies = now;
361 }
362
363 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
364
365 return true;
366 }
367
ionic_adminq_post(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)368 int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
369 {
370 struct ionic_admin_desc_info *desc_info;
371 struct ionic_admin_cmd *desc;
372 unsigned long irqflags;
373 struct ionic_queue *q;
374 int err = 0;
375
376 spin_lock_irqsave(&lif->adminq_lock, irqflags);
377 if (!lif->adminqcq) {
378 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
379 return -EIO;
380 }
381
382 q = &lif->adminqcq->q;
383
384 if (!ionic_q_has_space(q, 1)) {
385 err = -ENOSPC;
386 goto err_out;
387 }
388
389 err = ionic_heartbeat_check(lif->ionic);
390 if (err)
391 goto err_out;
392
393 desc_info = &q->admin_info[q->head_idx];
394 desc_info->ctx = ctx;
395
396 desc = &q->adminq[q->head_idx];
397 memcpy(desc, &ctx->cmd, sizeof(ctx->cmd));
398
399 dev_dbg(&lif->netdev->dev, "post admin queue command:\n");
400 dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
401 &ctx->cmd, sizeof(ctx->cmd), true);
402
403 ionic_q_post(q, true);
404
405 err_out:
406 spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
407
408 return err;
409 }
410
ionic_adminq_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const int err,const bool do_msg)411 int ionic_adminq_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx,
412 const int err, const bool do_msg)
413 {
414 struct net_device *netdev = lif->netdev;
415 unsigned long time_limit;
416 unsigned long time_start;
417 unsigned long time_done;
418 unsigned long remaining;
419 const char *name;
420
421 name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
422
423 if (err) {
424 if (do_msg && !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
425 netdev_err(netdev, "Posting of %s (%d) failed: %d\n",
426 name, ctx->cmd.cmd.opcode, err);
427 ctx->comp.comp.status = IONIC_RC_ERROR;
428 return err;
429 }
430
431 time_start = jiffies;
432 time_limit = time_start + HZ * (ulong)DEVCMD_TIMEOUT;
433 do {
434 remaining = wait_for_completion_timeout(&ctx->work,
435 IONIC_ADMINQ_TIME_SLICE);
436
437 /* check for done */
438 if (remaining)
439 break;
440
441 /* force a check of FW status and break out if FW reset */
442 ionic_heartbeat_check(lif->ionic);
443 if ((test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
444 !lif->ionic->idev.fw_status_ready) ||
445 test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
446 if (do_msg)
447 netdev_warn(netdev, "%s (%d) interrupted, FW in reset\n",
448 name, ctx->cmd.cmd.opcode);
449 ctx->comp.comp.status = IONIC_RC_ERROR;
450 return -ENXIO;
451 }
452
453 } while (time_before(jiffies, time_limit));
454 time_done = jiffies;
455
456 dev_dbg(lif->ionic->dev, "%s: elapsed %d msecs\n",
457 __func__, jiffies_to_msecs(time_done - time_start));
458
459 return ionic_adminq_check_err(lif, ctx,
460 time_after_eq(time_done, time_limit),
461 do_msg);
462 }
463
__ionic_adminq_post_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx,const bool do_msg)464 static int __ionic_adminq_post_wait(struct ionic_lif *lif,
465 struct ionic_admin_ctx *ctx,
466 const bool do_msg)
467 {
468 int err;
469
470 if (!ionic_is_fw_running(&lif->ionic->idev))
471 return 0;
472
473 err = ionic_adminq_post(lif, ctx);
474
475 return ionic_adminq_wait(lif, ctx, err, do_msg);
476 }
477
ionic_adminq_post_wait(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)478 int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
479 {
480 return __ionic_adminq_post_wait(lif, ctx, true);
481 }
482
ionic_adminq_post_wait_nomsg(struct ionic_lif * lif,struct ionic_admin_ctx * ctx)483 int ionic_adminq_post_wait_nomsg(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
484 {
485 return __ionic_adminq_post_wait(lif, ctx, false);
486 }
487
ionic_dev_cmd_clean(struct ionic * ionic)488 static void ionic_dev_cmd_clean(struct ionic *ionic)
489 {
490 struct ionic_dev *idev = &ionic->idev;
491
492 if (!idev->dev_cmd_regs)
493 return;
494
495 iowrite32(0, &idev->dev_cmd_regs->doorbell);
496 memset_io(&idev->dev_cmd_regs->cmd, 0, sizeof(idev->dev_cmd_regs->cmd));
497 }
498
ionic_dev_cmd_dev_err_print(struct ionic * ionic,u8 opcode,u8 status,int err)499 void ionic_dev_cmd_dev_err_print(struct ionic *ionic, u8 opcode, u8 status,
500 int err)
501 {
502 const char *stat_str;
503
504 stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
505 ionic_error_to_str(status);
506
507 dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
508 ionic_opcode_to_str(opcode), opcode, stat_str, err);
509 }
510
__ionic_dev_cmd_wait(struct ionic * ionic,unsigned long max_seconds,const bool do_msg)511 static int __ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds,
512 const bool do_msg)
513 {
514 struct ionic_dev *idev = &ionic->idev;
515 unsigned long start_time;
516 unsigned long max_wait;
517 unsigned long duration;
518 bool fw_up;
519 int opcode;
520 bool done;
521 int err;
522
523 /* Wait for dev cmd to complete, retrying if we get EAGAIN,
524 * but don't wait any longer than max_seconds.
525 */
526 max_wait = jiffies + (max_seconds * HZ);
527 try_again:
528 done = false;
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