1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
3
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/errno.h>
7 #include <linux/io.h>
8 #include <linux/slab.h>
9 #include <linux/etherdevice.h>
10 #include "ionic.h"
11 #include "ionic_dev.h"
12 #include "ionic_lif.h"
13
ionic_watchdog_cb(struct timer_list * t)14 static void ionic_watchdog_cb(struct timer_list *t)
15 {
16 struct ionic *ionic = from_timer(ionic, t, watchdog_timer);
17 struct ionic_lif *lif = ionic->lif;
18 struct ionic_deferred_work *work;
19 int hb;
20
21 mod_timer(&ionic->watchdog_timer,
22 round_jiffies(jiffies + ionic->watchdog_period));
23
24 if (!lif)
25 return;
26
27 hb = ionic_heartbeat_check(ionic);
28 dev_dbg(ionic->dev, "%s: hb %d running %d UP %d\n",
29 __func__, hb, netif_running(lif->netdev),
30 test_bit(IONIC_LIF_F_UP, lif->state));
31
32 if (hb >= 0 &&
33 !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
34 ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
35
36 if (test_bit(IONIC_LIF_F_FILTER_SYNC_NEEDED, lif->state) &&
37 !test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
38 work = kzalloc(sizeof(*work), GFP_ATOMIC);
39 if (!work) {
40 netdev_err(lif->netdev, "rxmode change dropped\n");
41 return;
42 }
43
44 work->type = IONIC_DW_TYPE_RX_MODE;
45 netdev_dbg(lif->netdev, "deferred: rx_mode\n");
46 ionic_lif_deferred_enqueue(&lif->deferred, work);
47 }
48 }
49
ionic_watchdog_init(struct ionic * ionic)50 static void ionic_watchdog_init(struct ionic *ionic)
51 {
52 struct ionic_dev *idev = &ionic->idev;
53
54 timer_setup(&ionic->watchdog_timer, ionic_watchdog_cb, 0);
55 ionic->watchdog_period = IONIC_WATCHDOG_SECS * HZ;
56
57 /* set times to ensure the first check will proceed */
58 atomic_long_set(&idev->last_check_time, jiffies - 2 * HZ);
59 idev->last_hb_time = jiffies - 2 * ionic->watchdog_period;
60 /* init as ready, so no transition if the first check succeeds */
61 idev->last_fw_hb = 0;
62 idev->fw_hb_ready = true;
63 idev->fw_status_ready = true;
64 idev->fw_generation = IONIC_FW_STS_F_GENERATION &
65 ioread8(&idev->dev_info_regs->fw_status);
66 }
67
ionic_init_devinfo(struct ionic * ionic)68 void ionic_init_devinfo(struct ionic *ionic)
69 {
70 struct ionic_dev *idev = &ionic->idev;
71
72 idev->dev_info.asic_type = ioread8(&idev->dev_info_regs->asic_type);
73 idev->dev_info.asic_rev = ioread8(&idev->dev_info_regs->asic_rev);
74
75 memcpy_fromio(idev->dev_info.fw_version,
76 idev->dev_info_regs->fw_version,
77 IONIC_DEVINFO_FWVERS_BUFLEN);
78
79 memcpy_fromio(idev->dev_info.serial_num,
80 idev->dev_info_regs->serial_num,
81 IONIC_DEVINFO_SERIAL_BUFLEN);
82
83 idev->dev_info.fw_version[IONIC_DEVINFO_FWVERS_BUFLEN] = 0;
84 idev->dev_info.serial_num[IONIC_DEVINFO_SERIAL_BUFLEN] = 0;
85
86 dev_dbg(ionic->dev, "fw_version %s\n", idev->dev_info.fw_version);
87 }
88
ionic_dev_setup(struct ionic * ionic)89 int ionic_dev_setup(struct ionic *ionic)
90 {
91 struct ionic_dev_bar *bar = ionic->bars;
92 unsigned int num_bars = ionic->num_bars;
93 struct ionic_dev *idev = &ionic->idev;
94 struct device *dev = ionic->dev;
95 u32 sig;
96
97 /* BAR0: dev_cmd and interrupts */
98 if (num_bars < 1) {
99 dev_err(dev, "No bars found, aborting\n");
100 return -EFAULT;
101 }
102
103 if (bar->len < IONIC_BAR0_SIZE) {
104 dev_err(dev, "Resource bar size %lu too small, aborting\n",
105 bar->len);
106 return -EFAULT;
107 }
108
109 idev->dev_info_regs = bar->vaddr + IONIC_BAR0_DEV_INFO_REGS_OFFSET;
110 idev->dev_cmd_regs = bar->vaddr + IONIC_BAR0_DEV_CMD_REGS_OFFSET;
111 idev->intr_status = bar->vaddr + IONIC_BAR0_INTR_STATUS_OFFSET;
112 idev->intr_ctrl = bar->vaddr + IONIC_BAR0_INTR_CTRL_OFFSET;
113
114 idev->hwstamp_regs = &idev->dev_info_regs->hwstamp;
115
116 sig = ioread32(&idev->dev_info_regs->signature);
117 if (sig != IONIC_DEV_INFO_SIGNATURE) {
118 dev_err(dev, "Incompatible firmware signature %x", sig);
119 return -EFAULT;
120 }
121
122 ionic_init_devinfo(ionic);
123
124 /* BAR1: doorbells */
125 bar++;
126 if (num_bars < 2) {
127 dev_err(dev, "Doorbell bar missing, aborting\n");
128 return -EFAULT;
129 }
130
131 ionic_watchdog_init(ionic);
132
133 idev->db_pages = bar->vaddr;
134 idev->phy_db_pages = bar->bus_addr;
135
136 return 0;
137 }
138
139 /* Devcmd Interface */
ionic_is_fw_running(struct ionic_dev * idev)140 bool ionic_is_fw_running(struct ionic_dev *idev)
141 {
142 u8 fw_status = ioread8(&idev->dev_info_regs->fw_status);
143
144 /* firmware is useful only if the running bit is set and
145 * fw_status != 0xff (bad PCI read)
146 */
147 return (fw_status != 0xff) && (fw_status & IONIC_FW_STS_F_RUNNING);
148 }
149
ionic_heartbeat_check(struct ionic * ionic)150 int ionic_heartbeat_check(struct ionic *ionic)
151 {
152 unsigned long check_time, last_check_time;
153 struct ionic_dev *idev = &ionic->idev;
154 struct ionic_lif *lif = ionic->lif;
155 bool fw_status_ready = true;
156 bool fw_hb_ready;
157 u8 fw_generation;
158 u8 fw_status;
159 u32 fw_hb;
160
161 /* wait a least one second before testing again */
162 check_time = jiffies;
163 last_check_time = atomic_long_read(&idev->last_check_time);
164 do_check_time:
165 if (time_before(check_time, last_check_time + HZ))
166 return 0;
167 if (!atomic_long_try_cmpxchg_relaxed(&idev->last_check_time,
168 &last_check_time, check_time)) {
169 /* if called concurrently, only the first should proceed. */
170 dev_dbg(ionic->dev, "%s: do_check_time again\n", __func__);
171 goto do_check_time;
172 }
173
174 fw_status = ioread8(&idev->dev_info_regs->fw_status);
175
176 /* If fw_status is not ready don't bother with the generation */
177 if (!ionic_is_fw_running(idev)) {
178 fw_status_ready = false;
179 } else {
180 fw_generation = fw_status & IONIC_FW_STS_F_GENERATION;
181 if (idev->fw_generation != fw_generation) {
182 dev_info(ionic->dev, "FW generation 0x%02x -> 0x%02x\n",
183 idev->fw_generation, fw_generation);
184
185 idev->fw_generation = fw_generation;
186
187 /* If the generation changed, the fw status is not
188 * ready so we need to trigger a fw-down cycle. After
189 * the down, the next watchdog will see the fw is up
190 * and the generation value stable, so will trigger
191 * the fw-up activity.
192 *
193 * If we had already moved to FW_RESET from a RESET event,
194 * it is possible that we never saw the fw_status go to 0,
195 * so we fake the current idev->fw_status_ready here to
196 * force the transition and get FW up again.
197 */
198 if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
199 idev->fw_status_ready = false; /* go to running */
200 else
201 fw_status_ready = false; /* go to down */
202 }
203 }
204
205 dev_dbg(ionic->dev, "fw_status 0x%02x ready %d idev->ready %d last_hb 0x%x state 0x%02lx\n",
206 fw_status, fw_status_ready, idev->fw_status_ready,
207 idev->last_fw_hb, lif->state[0]);
208
209 /* is this a transition? */
210 if (fw_status_ready != idev->fw_status_ready &&
211 !test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
212 bool trigger = false;
213
214 idev->fw_status_ready = fw_status_ready;
215
216 if (!fw_status_ready &&
217 !test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
218 !test_and_set_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
219 dev_info(ionic->dev, "FW stopped 0x%02x\n", fw_status);
220 trigger = true;
221
222 } else if (fw_status_ready &&
223 test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
224 dev_info(ionic->dev, "FW running 0x%02x\n", fw_status);
225 trigger = true;
226 }
227
228 if (trigger) {
229 struct ionic_deferred_work *work;
230
231 work = kzalloc(sizeof(*work), GFP_ATOMIC);
232 if (work) {
233 work->type = IONIC_DW_TYPE_LIF_RESET;
234 work->fw_status = fw_status_ready;
235 ionic_lif_deferred_enqueue(&lif->deferred, work);
236 }
237 }
238 }
239
240 if (!idev->fw_status_ready)
241 return -ENXIO;
242
243 /* Because of some variability in the actual FW heartbeat, we
244 * wait longer than the DEVCMD_TIMEOUT before checking again.
245 */
246 last_check_time = idev->last_hb_time;
247 if (time_before(check_time, last_check_time + DEVCMD_TIMEOUT * 2 * HZ))
248 return 0;
249
250 fw_hb = ioread32(&idev->dev_info_regs->fw_heartbeat);
251 fw_hb_ready = fw_hb != idev->last_fw_hb;
252
253 /* early FW version had no heartbeat, so fake it */
254 if (!fw_hb_ready && !fw_hb)
255 fw_hb_ready = true;
256
257 dev_dbg(ionic->dev, "%s: fw_hb %u last_fw_hb %u ready %u\n",
258 __func__, fw_hb, idev->last_fw_hb, fw_hb_ready);
259
260 idev->last_fw_hb = fw_hb;
261
262 /* log a transition */
263 if (fw_hb_ready != idev->fw_hb_ready) {
264 idev->fw_hb_ready = fw_hb_ready;
265 if (!fw_hb_ready)
266 dev_info(ionic->dev, "FW heartbeat stalled at %d\n", fw_hb);
267 else
268 dev_info(ionic->dev, "FW heartbeat restored at %d\n", fw_hb);
269 }
270
271 if (!fw_hb_ready)
272 return -ENXIO;
273
274 idev->last_hb_time = check_time;
275
276 return 0;
277 }
278
ionic_dev_cmd_status(struct ionic_dev * idev)279 u8 ionic_dev_cmd_status(struct ionic_dev *idev)
280 {
281 return ioread8(&idev->dev_cmd_regs->comp.comp.status);
282 }
283
ionic_dev_cmd_done(struct ionic_dev * idev)284 bool ionic_dev_cmd_done(struct ionic_dev *idev)
285 {
286 return ioread32(&idev->dev_cmd_regs->done) & IONIC_DEV_CMD_DONE;
287 }
288
ionic_dev_cmd_comp(struct ionic_dev * idev,union ionic_dev_cmd_comp * comp)289 void ionic_dev_cmd_comp(struct ionic_dev *idev, union ionic_dev_cmd_comp *comp)
290 {
291 memcpy_fromio(comp, &idev->dev_cmd_regs->comp, sizeof(*comp));
292 }
293
ionic_dev_cmd_go(struct ionic_dev * idev,union ionic_dev_cmd * cmd)294 void ionic_dev_cmd_go(struct ionic_dev *idev, union ionic_dev_cmd *cmd)
295 {
296 idev->opcode = cmd->cmd.opcode;
297 memcpy_toio(&idev->dev_cmd_regs->cmd, cmd, sizeof(*cmd));
298 iowrite32(0, &idev->dev_cmd_regs->done);
299 iowrite32(1, &idev->dev_cmd_regs->doorbell);
300 }
301
302 /* Device commands */
ionic_dev_cmd_identify(struct ionic_dev * idev,u8 ver)303 void ionic_dev_cmd_identify(struct ionic_dev *idev, u8 ver)
304 {
305 union ionic_dev_cmd cmd = {
306 .identify.opcode = IONIC_CMD_IDENTIFY,
307 .identify.ver = ver,
308 };
309
310 ionic_dev_cmd_go(idev, &cmd);
311 }
312
ionic_dev_cmd_init(struct ionic_dev * idev)313 void ionic_dev_cmd_init(struct ionic_dev *idev)
314 {
315 union ionic_dev_cmd cmd = {
316 .init.opcode = IONIC_CMD_INIT,
317 .init.type = 0,
318 };
319
320 ionic_dev_cmd_go(idev, &cmd);
321 }
322
ionic_dev_cmd_reset(struct ionic_dev * idev)323 void ionic_dev_cmd_reset(struct ionic_dev *idev)
324 {
325 union ionic_dev_cmd cmd = {
326 .reset.opcode = IONIC_CMD_RESET,
327 };
328
329 ionic_dev_cmd_go(idev, &cmd);
330 }
331
332 /* Port commands */
ionic_dev_cmd_port_identify(struct ionic_dev * idev)333 void ionic_dev_cmd_port_identify(struct ionic_dev *idev)
334 {
335 union ionic_dev_cmd cmd = {
336 .port_init.opcode = IONIC_CMD_PORT_IDENTIFY,
337 .port_init.index = 0,
338 };
339
340 ionic_dev_cmd_go(idev, &cmd);
341 }
342
ionic_dev_cmd_port_init(struct ionic_dev * idev)343 void ionic_dev_cmd_port_init(struct ionic_dev *idev)
344 {
345 union ionic_dev_cmd cmd = {
346 .port_init.opcode = IONIC_CMD_PORT_INIT,
347 .port_init.index = 0,
348 .port_init.info_pa = cpu_to_le64(idev->port_info_pa),
349 };
350
351 ionic_dev_cmd_go(idev, &cmd);
352 }
353
ionic_dev_cmd_port_reset(struct ionic_dev * idev)354 void ionic_dev_cmd_port_reset(struct ionic_dev *idev)
355 {
356 union ionic_dev_cmd cmd = {
357 .port_reset.opcode = IONIC_CMD_PORT_RESET,
358 .port_reset.index = 0,
359 };
360
361 ionic_dev_cmd_go(idev, &cmd);
362 }
363
ionic_dev_cmd_port_state(struct ionic_dev * idev,u8 state)364 void ionic_dev_cmd_port_state(struct ionic_dev *idev, u8 state)
365 {
366 union ionic_dev_cmd cmd = {
367 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
368 .port_setattr.index = 0,
369 .port_setattr.attr = IONIC_PORT_ATTR_STATE,
370 .port_setattr.state = state,
371 };
372
373 ionic_dev_cmd_go(idev, &cmd);
374 }
375
ionic_dev_cmd_port_speed(struct ionic_dev * idev,u32 speed)376 void ionic_dev_cmd_port_speed(struct ionic_dev *idev, u32 speed)
377 {
378 union ionic_dev_cmd cmd = {
379 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
380 .port_setattr.index = 0,
381 .port_setattr.attr = IONIC_PORT_ATTR_SPEED,
382 .port_setattr.speed = cpu_to_le32(speed),
383 };
384
385 ionic_dev_cmd_go(idev, &cmd);
386 }
387
ionic_dev_cmd_port_autoneg(struct ionic_dev * idev,u8 an_enable)388 void ionic_dev_cmd_port_autoneg(struct ionic_dev *idev, u8 an_enable)
389 {
390 union ionic_dev_cmd cmd = {
391 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
392 .port_setattr.index = 0,
393 .port_setattr.attr = IONIC_PORT_ATTR_AUTONEG,
394 .port_setattr.an_enable = an_enable,
395 };
396
397 ionic_dev_cmd_go(idev, &cmd);
398 }
399
ionic_dev_cmd_port_fec(struct ionic_dev * idev,u8 fec_type)400 void ionic_dev_cmd_port_fec(struct ionic_dev *idev, u8 fec_type)
401 {
402 union ionic_dev_cmd cmd = {
403 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
404 .port_setattr.index = 0,
405 .port_setattr.attr = IONIC_PORT_ATTR_FEC,
406 .port_setattr.fec_type = fec_type,
407 };
408
409 ionic_dev_cmd_go(idev, &cmd);
410 }
411
ionic_dev_cmd_port_pause(struct ionic_dev * idev,u8 pause_type)412 void ionic_dev_cmd_port_pause(struct ionic_dev *idev, u8 pause_type)
413 {
414 union ionic_dev_cmd cmd = {
415 .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
416 .port_setattr.index = 0,
417 .port_setattr.attr = IONIC_PORT_ATTR_PAUSE,
418 .port_setattr.pause_type = pause_type,
419 };
420
421 ionic_dev_cmd_go(idev, &cmd);
422 }
423
424 /* VF commands */
ionic_set_vf_config(struct ionic * ionic,int vf,struct ionic_vf_setattr_cmd * vfc)425 int ionic_set_vf_config(struct ionic *ionic, int vf,
426 struct ionic_vf_setattr_cmd *vfc)
427 {
428 union ionic_dev_cmd cmd = {
429 .vf_setattr.opcode = IONIC_CMD_VF_SETATTR,
430 .vf_setattr.attr = vfc->attr,
431 .vf_setattr.vf_index = cpu_to_le16(vf),
432 };
433 int err;
434
435 memcpy(cmd.vf_setattr.pad, vfc->pad, sizeof(vfc->pad));
436
437 mutex_lock(&ionic->dev_cmd_lock);
438 ionic_dev_cmd_go(&ionic->idev, &cmd);
439 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
440 mutex_unlock(&ionic->dev_cmd_lock);
441
442 return err;
443 }
444
ionic_dev_cmd_vf_getattr(struct ionic * ionic,int vf,u8 attr,struct ionic_vf_getattr_comp * comp)445 int ionic_dev_cmd_vf_getattr(struct ionic *ionic, int vf, u8 attr,
446 struct ionic_vf_getattr_comp *comp)
447 {
448 union ionic_dev_cmd cmd = {
449 .vf_getattr.opcode = IONIC_CMD_VF_GETATTR,
450 .vf_getattr.attr = attr,
451 .vf_getattr.vf_index = cpu_to_le16(vf),
452 };
453 int err;
454
455 if (vf >= ionic->num_vfs)
456 return -EINVAL;
457
458 switch (attr) {
459 case IONIC_VF_ATTR_SPOOFCHK:
460 case IONIC_VF_ATTR_TRUST:
461 case IONIC_VF_ATTR_LINKSTATE:
462 case IONIC_VF_ATTR_MAC:
463 case IONIC_VF_ATTR_VLAN:
464 case IONIC_VF_ATTR_RATE:
465 break;
466 case IONIC_VF_ATTR_STATSADDR:
467 default:
468 return -EINVAL;
469 }
470
471 mutex_lock(&ionic->dev_cmd_lock);
472 ionic_dev_cmd_go(&ionic->idev, &cmd);
473 err = ionic_dev_cmd_wait_nomsg(ionic, DEVCMD_TIMEOUT);
474 memcpy_fromio(comp, &ionic->idev.dev_cmd_regs->comp.vf_getattr,
475 sizeof(*comp));
476 mutex_unlock(&ionic->dev_cmd_lock);
477
478 if (err && comp->status != IONIC_RC_ENOSUPP)
479 ionic_dev_cmd_dev_err_print(ionic, cmd.vf_getattr.opcode,
480 comp->status, err);
481
482 return err;
483 }
484
485 /* LIF commands */
ionic_dev_cmd_queue_identify(struct ionic_dev * idev,u16 lif_type,u8 qtype,u8 qver)486 void ionic_dev_cmd_queue_identify(struct ionic_dev *idev,
487 u16 lif_type, u8 qtype, u8 qver)
488 {
489 union ionic_dev_cmd cmd = {
490 .q_identify.opcode = IONIC_CMD_Q_IDENTIFY,
491 .q_identify.lif_type = cpu_to_le16(lif_type),
492 .q_identify.type = qtype,
493 .q_identify.ver = qver,
494 };
495
496 ionic_dev_cmd_go(idev, &cmd);
497 }
498
ionic_dev_cmd_lif_identify(struct ionic_dev * idev,u8 type,u8 ver)499 void ionic_dev_cmd_lif_identify(struct ionic_dev *idev, u8 type, u8 ver)
500 {
501 union ionic_dev_cmd cmd = {
502 .lif_identify.opcode = IONIC_CMD_LIF_IDENTIFY,
503 .lif_identify.type = type,
504 .lif_identify.ver = ver,
505 };
506
507 ionic_dev_cmd_go(idev, &cmd);
508 }
509
ionic_dev_cmd_lif_init(struct ionic_dev * idev,u16 lif_index,dma_addr_t info_pa)510 void ionic_dev_cmd_lif_init(struct ionic_dev *idev, u16 lif_index,
511 dma_addr_t info_pa)
512 {
513 union ionic_dev_cmd cmd = {
514 .lif_init.opcode = IONIC_CMD_LIF_INIT,
515 .lif_init.index = cpu_to_le16(lif_index),
516 .lif_init.info_pa = cpu_to_le64(info_pa),
517 };
518
519 ionic_dev_cmd_go(idev, &cmd);
520 }
521
ionic_dev_cmd_lif_reset(struct ionic_dev * idev,u16 lif_index)522 void ionic_dev_cmd_lif_reset(struct ionic_dev *idev, u16 lif_index)
523 {
524 union ionic_dev_cmd cmd = {
525 .lif_init.opcode = IONIC_CMD_LIF_RESET,
526 .lif_init.index = cpu_to_le16(lif_index),
527 };
528
529 ionic_dev_cmd_go(idev, &cmd);
530 }
531
ionic_dev_cmd_adminq_init(struct ionic_dev * idev,struct ionic_qcq * qcq,u16 lif_index,u16 intr_index)532 void ionic_dev_cmd_adminq_init(struct ionic_dev *idev, struct ionic_qcq *qcq,
533 u16 lif_index, u16 intr_index)
534 {
535 struct ionic_queue *q = &qcq->q;
536 struct ionic_cq *cq = &qcq->cq;
537
538 union ionic_dev_cmd cmd = {
539 .q_init.opcode = IONIC_CMD_Q_INIT,
540 .q_init.lif_index = cpu_to_le16(lif_index),
541 .q_init.type = q->type,
542 .q_init.ver = qcq->q.lif->qtype_info[q->type].version,
543 .q_init.index = cpu_to_le32(q->index),
544 .q_init.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
545 IONIC_QINIT_F_ENA),
546 .q_init.pid = cpu_to_le16(q->pid),
547 .q_init.intr_index = cpu_to_le16(intr_index),
548 .q_init.ring_size = ilog2(q->num_descs),
549 .q_init.ring_base = cpu_to_le64(q->base_pa),
550 .q_init.cq_ring_base = cpu_to_le64(cq->base_pa),
551 };
552
553 ionic_dev_cmd_go(idev, &cmd);
554 }
555
ionic_db_page_num(struct ionic_lif * lif,int pid)556 int ionic_db_page_num(struct ionic_lif *lif, int pid)
557 {
558 return (lif->hw_index * lif->dbid_count) + pid;
559 }
560
ionic_cq_init(struct ionic_lif * lif,struct ionic_cq * cq,struct ionic_intr_info * intr,unsigned int num_descs,size_t desc_size)561 int ionic_cq_init(struct ionic_lif *lif, struct ionic_cq *cq,
562 struct ionic_intr_info *intr,
563 unsigned int num_descs, size_t desc_size)
564 {
565 unsigned int ring_size;
566
567 if (desc_size == 0 || !is_power_of_2(num_descs))
568 return -EINVAL;
569
570 ring_size = ilog2(num_descs);
571 if (ring_size < 2 || ring_size > 16)
572 return -EINVAL;
573
574 cq->lif = lif;
575 cq->bound_intr = intr;
576 cq->num_descs = num_descs;
577 cq->desc_size = desc_size;
578 cq->tail_idx = 0;
579 cq->done_color = 1;
580
581 return 0;
582 }
583
ionic_cq_map(struct ionic_cq * cq,void * base,dma_addr_t base_pa)584 void ionic_cq_map(struct ionic_cq *cq, void *base, dma_addr_t base_pa)
585 {
586 struct ionic_cq_info *cur;
587 unsigned int i;
588
589 cq->base = base;
590 cq->base_pa = base_pa;
591
592 for (i = 0, cur = cq->info; i < cq->num_descs; i++, cur++)
593 cur->cq_desc = base + (i * cq->desc_size);
594 }
595
ionic_cq_bind(struct ionic_cq * cq,struct ionic_queue * q)596 void ionic_cq_bind(struct ionic_cq *cq, struct ionic_queue *q)
597 {
598 cq->bound_q = q;
599 }
600
ionic_cq_service(struct ionic_cq * cq,unsigned int work_to_do,ionic_cq_cb cb,ionic_cq_done_cb done_cb,void * done_arg)601 unsigned int ionic_cq_service(struct ionic_cq *cq, unsigned int work_to_do,
602 ionic_cq_cb cb, ionic_cq_done_cb done_cb,
603 void *done_arg)
604 {
605 struct ionic_cq_info *cq_info;
606 unsigned int work_done = 0;
607
608 if (work_to_do == 0)
609 return 0;
610
611 cq_info = &cq->info[cq->tail_idx];
612 while (cb(cq, cq_info)) {
613 if (cq->tail_idx == cq->num_descs - 1)
614 cq->done_color = !cq->done_color;
615 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
616 cq_info = &cq->info[cq->tail_idx];
617
618 if (++work_done >= work_to_do)
619 break;
620 }
621
622 if (work_done && done_cb)
623 done_cb(done_arg);
624
625 return work_done;
626 }
627
ionic_q_init(struct ionic_lif * lif,struct ionic_dev * idev,struct ionic_queue * q,unsigned int index,const char * name,unsigned int num_descs,size_t desc_size,size_t sg_desc_size,unsigned int pid)628 int ionic_q_init(struct ionic_lif *lif, struct ionic_dev *idev,
629 struct ionic_queue *q, unsigned int index, const char *name,
630 unsigned int num_descs, size_t desc_size,
631 size_t sg_desc_size, unsigned int pid)
632 {
633 unsigned int ring_size;
634
635 if (desc_size == 0 || !is_power_of_2(num_descs))
636 return -EINVAL;
637
638 ring_size = ilog2(num_descs);
639 if (ring_size < 2 || ring_size > 16)
640 return -EINVAL;
641
642 q->lif = lif;
643 q->idev = idev;
644 q->index = index;
645 q->num_descs = num_descs;
646 q->desc_size = desc_size;
647 q->sg_desc_size = sg_desc_size;
648 q->tail_idx = 0;
649 q->head_idx = 0;
650 q->pid = pid;
651
652 snprintf(q->name, sizeof(q->name), "L%d-%s%u", lif->index, name, index);
653
654 return 0;
655 }
656
ionic_q_map(struct ionic_queue * q,void * base,dma_addr_t base_pa)657 void ionic_q_map(struct ionic_queue *q, void *base, dma_addr_t base_pa)
658 {
659 struct ionic_desc_info *cur;
660 unsigned int i;
661
662 q->base = base;
663 q->base_pa = base_pa;
664
665 for (i = 0, cur = q->info; i < q->num_descs; i++, cur++)
666 cur->desc = base + (i * q->desc_size);
667 }
668
ionic_q_sg_map(struct ionic_queue * q,void * base,dma_addr_t base_pa)669 void ionic_q_sg_map(struct ionic_queue *q, void *base, dma_addr_t base_pa)
670 {
671 struct ionic_desc_info *cur;
672 unsigned int i;
673
674 q->sg_base = base;
675 q->sg_base_pa = base_pa;
676
677 for (i = 0, cur = q->info; i < q->num_descs; i++, cur++)
678 cur->sg_desc = base + (i * q->sg_desc_size);
679 }
680
ionic_q_post(struct ionic_queue * q,bool ring_doorbell,ionic_desc_cb cb,void * cb_arg)681 void ionic_q_post(struct ionic_queue *q, bool ring_doorbell, ionic_desc_cb cb,
682 void *cb_arg)
683 {
684 struct ionic_desc_info *desc_info;
685 struct ionic_lif *lif = q->lif;
686 struct device *dev = q->dev;
687
688 desc_info = &q->info[q->head_idx];
689 desc_info->cb = cb;
690 desc_info->cb_arg = cb_arg;
691
692 q->head_idx = (q->head_idx + 1) & (q->num_descs - 1);
693
694 dev_dbg(dev, "lif=%d qname=%s qid=%d qtype=%d p_index=%d ringdb=%d\n",
695 q->lif->index, q->name, q->hw_type, q->hw_index,
696 q->head_idx, ring_doorbell);
697
698 if (ring_doorbell) {
699 ionic_dbell_ring(lif->kern_dbpage, q->hw_type,
700 q->dbval | q->head_idx);
701
702 q->dbell_jiffies = jiffies;
703
704 if (q_to_qcq(q)->napi_qcq)
705 mod_timer(&q_to_qcq(q)->napi_qcq->napi_deadline,
706 jiffies + IONIC_NAPI_DEADLINE);
707 }
708 }
709
ionic_q_is_posted(struct ionic_queue * q,unsigned int pos)710 static bool ionic_q_is_posted(struct ionic_queue *q, unsigned int pos)
711 {
712 unsigned int mask, tail, head;
713
714 mask = q->num_descs - 1;
715 tail = q->tail_idx;
716 head = q->head_idx;
717
718 return ((pos - tail) & mask) < ((head - tail) & mask);
719 }
720
ionic_q_service(struct ionic_queue * q,struct ionic_cq_info * cq_info,unsigned int stop_index)721 void ionic_q_service(struct ionic_queue *q, struct ionic_cq_info *cq_info,
722 unsigned int stop_index)
723 {
724 struct ionic_desc_info *desc_info;
725 ionic_desc_cb cb;
726 void *cb_arg;
727 u16 index;
728
729 /* check for empty queue */
730 if (q->tail_idx == q->head_idx)
731 return;
732
733 /* stop index must be for a descriptor that is not yet completed */
734 if (unlikely(!ionic_q_is_posted(q, stop_index)))
735 dev_err(q->dev,
736 "ionic stop is not posted %s stop %u tail %u head %u\n",
737 q->name, stop_index, q->tail_idx, q->head_idx);
738
739 do {
740 desc_info = &q->info[q->tail_idx];
741 index = q->tail_idx;
742 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
743
744 cb = desc_info->cb;
745 cb_arg = desc_info->cb_arg;
746
747 desc_info->cb = NULL;
748 desc_info->cb_arg = NULL;
749
750 if (cb)
751 cb(q, desc_info, cq_info, cb_arg);
752 } while (index != stop_index);
753 }
754