net: hns: Use phy_driver to setup Phy loopback
[linux-2.6/btrfs-unstable.git] / drivers / net / caif / caif_hsi.c
blob438966bf51c21ce83f900b1a7e0df866910f7df8
1 /*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Daniel Martensson
4 * Dmitry.Tarnyagin / dmitry.tarnyagin@lockless.no
5 * License terms: GNU General Public License (GPL) version 2.
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME fmt
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/netdevice.h>
14 #include <linux/string.h>
15 #include <linux/list.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/if_arp.h>
20 #include <linux/timer.h>
21 #include <net/rtnetlink.h>
22 #include <linux/pkt_sched.h>
23 #include <net/caif/caif_layer.h>
24 #include <net/caif/caif_hsi.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Daniel Martensson");
28 MODULE_DESCRIPTION("CAIF HSI driver");
30 /* Returns the number of padding bytes for alignment. */
31 #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
32 (((pow)-((x)&((pow)-1)))))
34 static const struct cfhsi_config hsi_default_config = {
36 /* Inactivity timeout on HSI, ms */
37 .inactivity_timeout = HZ,
39 /* Aggregation timeout (ms) of zero means no aggregation is done*/
40 .aggregation_timeout = 1,
43 * HSI link layer flow-control thresholds.
44 * Threshold values for the HSI packet queue. Flow-control will be
45 * asserted when the number of packets exceeds q_high_mark. It will
46 * not be de-asserted before the number of packets drops below
47 * q_low_mark.
48 * Warning: A high threshold value might increase throughput but it
49 * will at the same time prevent channel prioritization and increase
50 * the risk of flooding the modem. The high threshold should be above
51 * the low.
53 .q_high_mark = 100,
54 .q_low_mark = 50,
57 * HSI padding options.
58 * Warning: must be a base of 2 (& operation used) and can not be zero !
60 .head_align = 4,
61 .tail_align = 4,
64 #define ON 1
65 #define OFF 0
67 static LIST_HEAD(cfhsi_list);
69 static void cfhsi_inactivity_tout(unsigned long arg)
71 struct cfhsi *cfhsi = (struct cfhsi *)arg;
73 netdev_dbg(cfhsi->ndev, "%s.\n",
74 __func__);
76 /* Schedule power down work queue. */
77 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
78 queue_work(cfhsi->wq, &cfhsi->wake_down_work);
81 static void cfhsi_update_aggregation_stats(struct cfhsi *cfhsi,
82 const struct sk_buff *skb,
83 int direction)
85 struct caif_payload_info *info;
86 int hpad, tpad, len;
88 info = (struct caif_payload_info *)&skb->cb;
89 hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
90 tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
91 len = skb->len + hpad + tpad;
93 if (direction > 0)
94 cfhsi->aggregation_len += len;
95 else if (direction < 0)
96 cfhsi->aggregation_len -= len;
99 static bool cfhsi_can_send_aggregate(struct cfhsi *cfhsi)
101 int i;
103 if (cfhsi->cfg.aggregation_timeout == 0)
104 return true;
106 for (i = 0; i < CFHSI_PRIO_BEBK; ++i) {
107 if (cfhsi->qhead[i].qlen)
108 return true;
111 /* TODO: Use aggregation_len instead */
112 if (cfhsi->qhead[CFHSI_PRIO_BEBK].qlen >= CFHSI_MAX_PKTS)
113 return true;
115 return false;
118 static struct sk_buff *cfhsi_dequeue(struct cfhsi *cfhsi)
120 struct sk_buff *skb;
121 int i;
123 for (i = 0; i < CFHSI_PRIO_LAST; ++i) {
124 skb = skb_dequeue(&cfhsi->qhead[i]);
125 if (skb)
126 break;
129 return skb;
132 static int cfhsi_tx_queue_len(struct cfhsi *cfhsi)
134 int i, len = 0;
135 for (i = 0; i < CFHSI_PRIO_LAST; ++i)
136 len += skb_queue_len(&cfhsi->qhead[i]);
137 return len;
140 static void cfhsi_abort_tx(struct cfhsi *cfhsi)
142 struct sk_buff *skb;
144 for (;;) {
145 spin_lock_bh(&cfhsi->lock);
146 skb = cfhsi_dequeue(cfhsi);
147 if (!skb)
148 break;
150 cfhsi->ndev->stats.tx_errors++;
151 cfhsi->ndev->stats.tx_dropped++;
152 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
153 spin_unlock_bh(&cfhsi->lock);
154 kfree_skb(skb);
156 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
157 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
158 mod_timer(&cfhsi->inactivity_timer,
159 jiffies + cfhsi->cfg.inactivity_timeout);
160 spin_unlock_bh(&cfhsi->lock);
163 static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
165 char buffer[32]; /* Any reasonable value */
166 size_t fifo_occupancy;
167 int ret;
169 netdev_dbg(cfhsi->ndev, "%s.\n",
170 __func__);
172 do {
173 ret = cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
174 &fifo_occupancy);
175 if (ret) {
176 netdev_warn(cfhsi->ndev,
177 "%s: can't get FIFO occupancy: %d.\n",
178 __func__, ret);
179 break;
180 } else if (!fifo_occupancy)
181 /* No more data, exitting normally */
182 break;
184 fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
185 set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
186 ret = cfhsi->ops->cfhsi_rx(buffer, fifo_occupancy,
187 cfhsi->ops);
188 if (ret) {
189 clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
190 netdev_warn(cfhsi->ndev,
191 "%s: can't read data: %d.\n",
192 __func__, ret);
193 break;
196 ret = 5 * HZ;
197 ret = wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
198 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
200 if (ret < 0) {
201 netdev_warn(cfhsi->ndev,
202 "%s: can't wait for flush complete: %d.\n",
203 __func__, ret);
204 break;
205 } else if (!ret) {
206 ret = -ETIMEDOUT;
207 netdev_warn(cfhsi->ndev,
208 "%s: timeout waiting for flush complete.\n",
209 __func__);
210 break;
212 } while (1);
214 return ret;
217 static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
219 int nfrms = 0;
220 int pld_len = 0;
221 struct sk_buff *skb;
222 u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
224 skb = cfhsi_dequeue(cfhsi);
225 if (!skb)
226 return 0;
228 /* Clear offset. */
229 desc->offset = 0;
231 /* Check if we can embed a CAIF frame. */
232 if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
233 struct caif_payload_info *info;
234 int hpad;
235 int tpad;
237 /* Calculate needed head alignment and tail alignment. */
238 info = (struct caif_payload_info *)&skb->cb;
240 hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
241 tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
243 /* Check if frame still fits with added alignment. */
244 if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
245 u8 *pemb = desc->emb_frm;
246 desc->offset = CFHSI_DESC_SHORT_SZ;
247 *pemb = (u8)(hpad - 1);
248 pemb += hpad;
250 /* Update network statistics. */
251 spin_lock_bh(&cfhsi->lock);
252 cfhsi->ndev->stats.tx_packets++;
253 cfhsi->ndev->stats.tx_bytes += skb->len;
254 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
255 spin_unlock_bh(&cfhsi->lock);
257 /* Copy in embedded CAIF frame. */
258 skb_copy_bits(skb, 0, pemb, skb->len);
260 /* Consume the SKB */
261 consume_skb(skb);
262 skb = NULL;
266 /* Create payload CAIF frames. */
267 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
268 while (nfrms < CFHSI_MAX_PKTS) {
269 struct caif_payload_info *info;
270 int hpad;
271 int tpad;
273 if (!skb)
274 skb = cfhsi_dequeue(cfhsi);
276 if (!skb)
277 break;
279 /* Calculate needed head alignment and tail alignment. */
280 info = (struct caif_payload_info *)&skb->cb;
282 hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
283 tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
285 /* Fill in CAIF frame length in descriptor. */
286 desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
288 /* Fill head padding information. */
289 *pfrm = (u8)(hpad - 1);
290 pfrm += hpad;
292 /* Update network statistics. */
293 spin_lock_bh(&cfhsi->lock);
294 cfhsi->ndev->stats.tx_packets++;
295 cfhsi->ndev->stats.tx_bytes += skb->len;
296 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
297 spin_unlock_bh(&cfhsi->lock);
299 /* Copy in CAIF frame. */
300 skb_copy_bits(skb, 0, pfrm, skb->len);
302 /* Update payload length. */
303 pld_len += desc->cffrm_len[nfrms];
305 /* Update frame pointer. */
306 pfrm += skb->len + tpad;
308 /* Consume the SKB */
309 consume_skb(skb);
310 skb = NULL;
312 /* Update number of frames. */
313 nfrms++;
316 /* Unused length fields should be zero-filled (according to SPEC). */
317 while (nfrms < CFHSI_MAX_PKTS) {
318 desc->cffrm_len[nfrms] = 0x0000;
319 nfrms++;
322 /* Check if we can piggy-back another descriptor. */
323 if (cfhsi_can_send_aggregate(cfhsi))
324 desc->header |= CFHSI_PIGGY_DESC;
325 else
326 desc->header &= ~CFHSI_PIGGY_DESC;
328 return CFHSI_DESC_SZ + pld_len;
331 static void cfhsi_start_tx(struct cfhsi *cfhsi)
333 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
334 int len, res;
336 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
338 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
339 return;
341 do {
342 /* Create HSI frame. */
343 len = cfhsi_tx_frm(desc, cfhsi);
344 if (!len) {
345 spin_lock_bh(&cfhsi->lock);
346 if (unlikely(cfhsi_tx_queue_len(cfhsi))) {
347 spin_unlock_bh(&cfhsi->lock);
348 res = -EAGAIN;
349 continue;
351 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
352 /* Start inactivity timer. */
353 mod_timer(&cfhsi->inactivity_timer,
354 jiffies + cfhsi->cfg.inactivity_timeout);
355 spin_unlock_bh(&cfhsi->lock);
356 break;
359 /* Set up new transfer. */
360 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
361 if (WARN_ON(res < 0))
362 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
363 __func__, res);
364 } while (res < 0);
367 static void cfhsi_tx_done(struct cfhsi *cfhsi)
369 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
371 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
372 return;
375 * Send flow on if flow off has been previously signalled
376 * and number of packets is below low water mark.
378 spin_lock_bh(&cfhsi->lock);
379 if (cfhsi->flow_off_sent &&
380 cfhsi_tx_queue_len(cfhsi) <= cfhsi->cfg.q_low_mark &&
381 cfhsi->cfdev.flowctrl) {
383 cfhsi->flow_off_sent = 0;
384 cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
387 if (cfhsi_can_send_aggregate(cfhsi)) {
388 spin_unlock_bh(&cfhsi->lock);
389 cfhsi_start_tx(cfhsi);
390 } else {
391 mod_timer(&cfhsi->aggregation_timer,
392 jiffies + cfhsi->cfg.aggregation_timeout);
393 spin_unlock_bh(&cfhsi->lock);
396 return;
399 static void cfhsi_tx_done_cb(struct cfhsi_cb_ops *cb_ops)
401 struct cfhsi *cfhsi;
403 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
404 netdev_dbg(cfhsi->ndev, "%s.\n",
405 __func__);
407 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
408 return;
409 cfhsi_tx_done(cfhsi);
412 static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
414 int xfer_sz = 0;
415 int nfrms = 0;
416 u16 *plen = NULL;
417 u8 *pfrm = NULL;
419 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
420 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
421 netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
422 __func__);
423 return -EPROTO;
426 /* Check for embedded CAIF frame. */
427 if (desc->offset) {
428 struct sk_buff *skb;
429 int len = 0;
430 pfrm = ((u8 *)desc) + desc->offset;
432 /* Remove offset padding. */
433 pfrm += *pfrm + 1;
435 /* Read length of CAIF frame (little endian). */
436 len = *pfrm;
437 len |= ((*(pfrm+1)) << 8) & 0xFF00;
438 len += 2; /* Add FCS fields. */
440 /* Sanity check length of CAIF frame. */
441 if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
442 netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
443 __func__);
444 return -EPROTO;
447 /* Allocate SKB (OK even in IRQ context). */
448 skb = alloc_skb(len + 1, GFP_ATOMIC);
449 if (!skb) {
450 netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
451 __func__);
452 return -ENOMEM;
454 caif_assert(skb != NULL);
456 skb_put_data(skb, pfrm, len);
458 skb->protocol = htons(ETH_P_CAIF);
459 skb_reset_mac_header(skb);
460 skb->dev = cfhsi->ndev;
463 * We are in a callback handler and
464 * unfortunately we don't know what context we're
465 * running in.
467 if (in_interrupt())
468 netif_rx(skb);
469 else
470 netif_rx_ni(skb);
472 /* Update network statistics. */
473 cfhsi->ndev->stats.rx_packets++;
474 cfhsi->ndev->stats.rx_bytes += len;
477 /* Calculate transfer length. */
478 plen = desc->cffrm_len;
479 while (nfrms < CFHSI_MAX_PKTS && *plen) {
480 xfer_sz += *plen;
481 plen++;
482 nfrms++;
485 /* Check for piggy-backed descriptor. */
486 if (desc->header & CFHSI_PIGGY_DESC)
487 xfer_sz += CFHSI_DESC_SZ;
489 if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
490 netdev_err(cfhsi->ndev,
491 "%s: Invalid payload len: %d, ignored.\n",
492 __func__, xfer_sz);
493 return -EPROTO;
495 return xfer_sz;
498 static int cfhsi_rx_desc_len(struct cfhsi_desc *desc)
500 int xfer_sz = 0;
501 int nfrms = 0;
502 u16 *plen;
504 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
505 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
507 pr_err("Invalid descriptor. %x %x\n", desc->header,
508 desc->offset);
509 return -EPROTO;
512 /* Calculate transfer length. */
513 plen = desc->cffrm_len;
514 while (nfrms < CFHSI_MAX_PKTS && *plen) {
515 xfer_sz += *plen;
516 plen++;
517 nfrms++;
520 if (xfer_sz % 4) {
521 pr_err("Invalid payload len: %d, ignored.\n", xfer_sz);
522 return -EPROTO;
524 return xfer_sz;
527 static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
529 int rx_sz = 0;
530 int nfrms = 0;
531 u16 *plen = NULL;
532 u8 *pfrm = NULL;
534 /* Sanity check header and offset. */
535 if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
536 (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
537 netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
538 __func__);
539 return -EPROTO;
542 /* Set frame pointer to start of payload. */
543 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
544 plen = desc->cffrm_len;
546 /* Skip already processed frames. */
547 while (nfrms < cfhsi->rx_state.nfrms) {
548 pfrm += *plen;
549 rx_sz += *plen;
550 plen++;
551 nfrms++;
554 /* Parse payload. */
555 while (nfrms < CFHSI_MAX_PKTS && *plen) {
556 struct sk_buff *skb;
557 u8 *pcffrm = NULL;
558 int len;
560 /* CAIF frame starts after head padding. */
561 pcffrm = pfrm + *pfrm + 1;
563 /* Read length of CAIF frame (little endian). */
564 len = *pcffrm;
565 len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
566 len += 2; /* Add FCS fields. */
568 /* Sanity check length of CAIF frames. */
569 if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
570 netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
571 __func__);
572 return -EPROTO;
575 /* Allocate SKB (OK even in IRQ context). */
576 skb = alloc_skb(len + 1, GFP_ATOMIC);
577 if (!skb) {
578 netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
579 __func__);
580 cfhsi->rx_state.nfrms = nfrms;
581 return -ENOMEM;
583 caif_assert(skb != NULL);
585 skb_put_data(skb, pcffrm, len);
587 skb->protocol = htons(ETH_P_CAIF);
588 skb_reset_mac_header(skb);
589 skb->dev = cfhsi->ndev;
592 * We're called in callback from HSI
593 * and don't know the context we're running in.
595 if (in_interrupt())
596 netif_rx(skb);
597 else
598 netif_rx_ni(skb);
600 /* Update network statistics. */
601 cfhsi->ndev->stats.rx_packets++;
602 cfhsi->ndev->stats.rx_bytes += len;
604 pfrm += *plen;
605 rx_sz += *plen;
606 plen++;
607 nfrms++;
610 return rx_sz;
613 static void cfhsi_rx_done(struct cfhsi *cfhsi)
615 int res;
616 int desc_pld_len = 0, rx_len, rx_state;
617 struct cfhsi_desc *desc = NULL;
618 u8 *rx_ptr, *rx_buf;
619 struct cfhsi_desc *piggy_desc = NULL;
621 desc = (struct cfhsi_desc *)cfhsi->rx_buf;
623 netdev_dbg(cfhsi->ndev, "%s\n", __func__);
625 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
626 return;
628 /* Update inactivity timer if pending. */
629 spin_lock_bh(&cfhsi->lock);
630 mod_timer_pending(&cfhsi->inactivity_timer,
631 jiffies + cfhsi->cfg.inactivity_timeout);
632 spin_unlock_bh(&cfhsi->lock);
634 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
635 desc_pld_len = cfhsi_rx_desc_len(desc);
637 if (desc_pld_len < 0)
638 goto out_of_sync;
640 rx_buf = cfhsi->rx_buf;
641 rx_len = desc_pld_len;
642 if (desc_pld_len > 0 && (desc->header & CFHSI_PIGGY_DESC))
643 rx_len += CFHSI_DESC_SZ;
644 if (desc_pld_len == 0)
645 rx_buf = cfhsi->rx_flip_buf;
646 } else {
647 rx_buf = cfhsi->rx_flip_buf;
649 rx_len = CFHSI_DESC_SZ;
650 if (cfhsi->rx_state.pld_len > 0 &&
651 (desc->header & CFHSI_PIGGY_DESC)) {
653 piggy_desc = (struct cfhsi_desc *)
654 (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
655 cfhsi->rx_state.pld_len);
657 cfhsi->rx_state.piggy_desc = true;
659 /* Extract payload len from piggy-backed descriptor. */
660 desc_pld_len = cfhsi_rx_desc_len(piggy_desc);
661 if (desc_pld_len < 0)
662 goto out_of_sync;
664 if (desc_pld_len > 0) {
665 rx_len = desc_pld_len;
666 if (piggy_desc->header & CFHSI_PIGGY_DESC)
667 rx_len += CFHSI_DESC_SZ;
671 * Copy needed information from the piggy-backed
672 * descriptor to the descriptor in the start.
674 memcpy(rx_buf, (u8 *)piggy_desc,
675 CFHSI_DESC_SHORT_SZ);
679 if (desc_pld_len) {
680 rx_state = CFHSI_RX_STATE_PAYLOAD;
681 rx_ptr = rx_buf + CFHSI_DESC_SZ;
682 } else {
683 rx_state = CFHSI_RX_STATE_DESC;
684 rx_ptr = rx_buf;
685 rx_len = CFHSI_DESC_SZ;
688 /* Initiate next read */
689 if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
690 /* Set up new transfer. */
691 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n",
692 __func__);
694 res = cfhsi->ops->cfhsi_rx(rx_ptr, rx_len,
695 cfhsi->ops);
696 if (WARN_ON(res < 0)) {
697 netdev_err(cfhsi->ndev, "%s: RX error %d.\n",
698 __func__, res);
699 cfhsi->ndev->stats.rx_errors++;
700 cfhsi->ndev->stats.rx_dropped++;
704 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
705 /* Extract payload from descriptor */
706 if (cfhsi_rx_desc(desc, cfhsi) < 0)
707 goto out_of_sync;
708 } else {
709 /* Extract payload */
710 if (cfhsi_rx_pld(desc, cfhsi) < 0)
711 goto out_of_sync;
712 if (piggy_desc) {
713 /* Extract any payload in piggyback descriptor. */
714 if (cfhsi_rx_desc(piggy_desc, cfhsi) < 0)
715 goto out_of_sync;
716 /* Mark no embedded frame after extracting it */
717 piggy_desc->offset = 0;
721 /* Update state info */
722 memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
723 cfhsi->rx_state.state = rx_state;
724 cfhsi->rx_ptr = rx_ptr;
725 cfhsi->rx_len = rx_len;
726 cfhsi->rx_state.pld_len = desc_pld_len;
727 cfhsi->rx_state.piggy_desc = desc->header & CFHSI_PIGGY_DESC;
729 if (rx_buf != cfhsi->rx_buf)
730 swap(cfhsi->rx_buf, cfhsi->rx_flip_buf);
731 return;
733 out_of_sync:
734 netdev_err(cfhsi->ndev, "%s: Out of sync.\n", __func__);
735 print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
736 cfhsi->rx_buf, CFHSI_DESC_SZ);
737 schedule_work(&cfhsi->out_of_sync_work);
740 static void cfhsi_rx_slowpath(unsigned long arg)
742 struct cfhsi *cfhsi = (struct cfhsi *)arg;
744 netdev_dbg(cfhsi->ndev, "%s.\n",
745 __func__);
747 cfhsi_rx_done(cfhsi);
750 static void cfhsi_rx_done_cb(struct cfhsi_cb_ops *cb_ops)
752 struct cfhsi *cfhsi;
754 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
755 netdev_dbg(cfhsi->ndev, "%s.\n",
756 __func__);
758 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
759 return;
761 if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
762 wake_up_interruptible(&cfhsi->flush_fifo_wait);
763 else
764 cfhsi_rx_done(cfhsi);
767 static void cfhsi_wake_up(struct work_struct *work)
769 struct cfhsi *cfhsi = NULL;
770 int res;
771 int len;
772 long ret;
774 cfhsi = container_of(work, struct cfhsi, wake_up_work);
776 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
777 return;
779 if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
780 /* It happenes when wakeup is requested by
781 * both ends at the same time. */
782 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
783 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
784 return;
787 /* Activate wake line. */
788 cfhsi->ops->cfhsi_wake_up(cfhsi->ops);
790 netdev_dbg(cfhsi->ndev, "%s: Start waiting.\n",
791 __func__);
793 /* Wait for acknowledge. */
794 ret = CFHSI_WAKE_TOUT;
795 ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
796 test_and_clear_bit(CFHSI_WAKE_UP_ACK,
797 &cfhsi->bits), ret);
798 if (unlikely(ret < 0)) {
799 /* Interrupted by signal. */
800 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
801 __func__, ret);
803 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
804 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
805 return;
806 } else if (!ret) {
807 bool ca_wake = false;
808 size_t fifo_occupancy = 0;
810 /* Wakeup timeout */
811 netdev_dbg(cfhsi->ndev, "%s: Timeout.\n",
812 __func__);
814 /* Check FIFO to check if modem has sent something. */
815 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
816 &fifo_occupancy));
818 netdev_dbg(cfhsi->ndev, "%s: Bytes in FIFO: %u.\n",
819 __func__, (unsigned) fifo_occupancy);
821 /* Check if we misssed the interrupt. */
822 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
823 &ca_wake));
825 if (ca_wake) {
826 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
827 __func__);
829 /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
830 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
832 /* Continue execution. */
833 goto wake_ack;
836 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
837 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
838 return;
840 wake_ack:
841 netdev_dbg(cfhsi->ndev, "%s: Woken.\n",
842 __func__);
844 /* Clear power up bit. */
845 set_bit(CFHSI_AWAKE, &cfhsi->bits);
846 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
848 /* Resume read operation. */
849 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n", __func__);
850 res = cfhsi->ops->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->ops);
852 if (WARN_ON(res < 0))
853 netdev_err(cfhsi->ndev, "%s: RX err %d.\n", __func__, res);
855 /* Clear power up acknowledment. */
856 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
858 spin_lock_bh(&cfhsi->lock);
860 /* Resume transmit if queues are not empty. */
861 if (!cfhsi_tx_queue_len(cfhsi)) {
862 netdev_dbg(cfhsi->ndev, "%s: Peer wake, start timer.\n",
863 __func__);
864 /* Start inactivity timer. */
865 mod_timer(&cfhsi->inactivity_timer,
866 jiffies + cfhsi->cfg.inactivity_timeout);
867 spin_unlock_bh(&cfhsi->lock);
868 return;
871 netdev_dbg(cfhsi->ndev, "%s: Host wake.\n",
872 __func__);
874 spin_unlock_bh(&cfhsi->lock);
876 /* Create HSI frame. */
877 len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
879 if (likely(len > 0)) {
880 /* Set up new transfer. */
881 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
882 if (WARN_ON(res < 0)) {
883 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
884 __func__, res);
885 cfhsi_abort_tx(cfhsi);
887 } else {
888 netdev_err(cfhsi->ndev,
889 "%s: Failed to create HSI frame: %d.\n",
890 __func__, len);
894 static void cfhsi_wake_down(struct work_struct *work)
896 long ret;
897 struct cfhsi *cfhsi = NULL;
898 size_t fifo_occupancy = 0;
899 int retry = CFHSI_WAKE_TOUT;
901 cfhsi = container_of(work, struct cfhsi, wake_down_work);
902 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
904 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
905 return;
907 /* Deactivate wake line. */
908 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
910 /* Wait for acknowledge. */
911 ret = CFHSI_WAKE_TOUT;
912 ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
913 test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
914 &cfhsi->bits), ret);
915 if (ret < 0) {
916 /* Interrupted by signal. */
917 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
918 __func__, ret);
919 return;
920 } else if (!ret) {
921 bool ca_wake = true;
923 /* Timeout */
924 netdev_err(cfhsi->ndev, "%s: Timeout.\n", __func__);
926 /* Check if we misssed the interrupt. */
927 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
928 &ca_wake));
929 if (!ca_wake)
930 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
931 __func__);
934 /* Check FIFO occupancy. */
935 while (retry) {
936 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
937 &fifo_occupancy));
939 if (!fifo_occupancy)
940 break;
942 set_current_state(TASK_INTERRUPTIBLE);
943 schedule_timeout(1);
944 retry--;
947 if (!retry)
948 netdev_err(cfhsi->ndev, "%s: FIFO Timeout.\n", __func__);
950 /* Clear AWAKE condition. */
951 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
953 /* Cancel pending RX requests. */
954 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
957 static void cfhsi_out_of_sync(struct work_struct *work)
959 struct cfhsi *cfhsi = NULL;
961 cfhsi = container_of(work, struct cfhsi, out_of_sync_work);
963 rtnl_lock();
964 dev_close(cfhsi->ndev);
965 rtnl_unlock();
968 static void cfhsi_wake_up_cb(struct cfhsi_cb_ops *cb_ops)
970 struct cfhsi *cfhsi = NULL;
972 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
973 netdev_dbg(cfhsi->ndev, "%s.\n",
974 __func__);
976 set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
977 wake_up_interruptible(&cfhsi->wake_up_wait);
979 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
980 return;
982 /* Schedule wake up work queue if the peer initiates. */
983 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
984 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
987 static void cfhsi_wake_down_cb(struct cfhsi_cb_ops *cb_ops)
989 struct cfhsi *cfhsi = NULL;
991 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
992 netdev_dbg(cfhsi->ndev, "%s.\n",
993 __func__);
995 /* Initiating low power is only permitted by the host (us). */
996 set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
997 wake_up_interruptible(&cfhsi->wake_down_wait);
1000 static void cfhsi_aggregation_tout(unsigned long arg)
1002 struct cfhsi *cfhsi = (struct cfhsi *)arg;
1004 netdev_dbg(cfhsi->ndev, "%s.\n",
1005 __func__);
1007 cfhsi_start_tx(cfhsi);
1010 static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
1012 struct cfhsi *cfhsi = NULL;
1013 int start_xfer = 0;
1014 int timer_active;
1015 int prio;
1017 if (!dev)
1018 return -EINVAL;
1020 cfhsi = netdev_priv(dev);
1022 switch (skb->priority) {
1023 case TC_PRIO_BESTEFFORT:
1024 case TC_PRIO_FILLER:
1025 case TC_PRIO_BULK:
1026 prio = CFHSI_PRIO_BEBK;
1027 break;
1028 case TC_PRIO_INTERACTIVE_BULK:
1029 prio = CFHSI_PRIO_VI;
1030 break;
1031 case TC_PRIO_INTERACTIVE:
1032 prio = CFHSI_PRIO_VO;
1033 break;
1034 case TC_PRIO_CONTROL:
1035 default:
1036 prio = CFHSI_PRIO_CTL;
1037 break;
1040 spin_lock_bh(&cfhsi->lock);
1042 /* Update aggregation statistics */
1043 cfhsi_update_aggregation_stats(cfhsi, skb, 1);
1045 /* Queue the SKB */
1046 skb_queue_tail(&cfhsi->qhead[prio], skb);
1048 /* Sanity check; xmit should not be called after unregister_netdev */
1049 if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
1050 spin_unlock_bh(&cfhsi->lock);
1051 cfhsi_abort_tx(cfhsi);
1052 return -EINVAL;
1055 /* Send flow off if number of packets is above high water mark. */
1056 if (!cfhsi->flow_off_sent &&
1057 cfhsi_tx_queue_len(cfhsi) > cfhsi->cfg.q_high_mark &&
1058 cfhsi->cfdev.flowctrl) {
1059 cfhsi->flow_off_sent = 1;
1060 cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
1063 if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
1064 cfhsi->tx_state = CFHSI_TX_STATE_XFER;
1065 start_xfer = 1;
1068 if (!start_xfer) {
1069 /* Send aggregate if it is possible */
1070 bool aggregate_ready =
1071 cfhsi_can_send_aggregate(cfhsi) &&
1072 del_timer(&cfhsi->aggregation_timer) > 0;
1073 spin_unlock_bh(&cfhsi->lock);
1074 if (aggregate_ready)
1075 cfhsi_start_tx(cfhsi);
1076 return 0;
1079 /* Delete inactivity timer if started. */
1080 timer_active = del_timer_sync(&cfhsi->inactivity_timer);
1082 spin_unlock_bh(&cfhsi->lock);
1084 if (timer_active) {
1085 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
1086 int len;
1087 int res;
1089 /* Create HSI frame. */
1090 len = cfhsi_tx_frm(desc, cfhsi);
1091 WARN_ON(!len);
1093 /* Set up new transfer. */
1094 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
1095 if (WARN_ON(res < 0)) {
1096 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
1097 __func__, res);
1098 cfhsi_abort_tx(cfhsi);
1100 } else {
1101 /* Schedule wake up work queue if the we initiate. */
1102 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
1103 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
1106 return 0;
1109 static const struct net_device_ops cfhsi_netdevops;
1111 static void cfhsi_setup(struct net_device *dev)
1113 int i;
1114 struct cfhsi *cfhsi = netdev_priv(dev);
1115 dev->features = 0;
1116 dev->type = ARPHRD_CAIF;
1117 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1118 dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
1119 dev->priv_flags |= IFF_NO_QUEUE;
1120 dev->needs_free_netdev = true;
1121 dev->netdev_ops = &cfhsi_netdevops;
1122 for (i = 0; i < CFHSI_PRIO_LAST; ++i)
1123 skb_queue_head_init(&cfhsi->qhead[i]);
1124 cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
1125 cfhsi->cfdev.use_frag = false;
1126 cfhsi->cfdev.use_stx = false;
1127 cfhsi->cfdev.use_fcs = false;
1128 cfhsi->ndev = dev;
1129 cfhsi->cfg = hsi_default_config;
1132 static int cfhsi_open(struct net_device *ndev)
1134 struct cfhsi *cfhsi = netdev_priv(ndev);
1135 int res;
1137 clear_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1139 /* Initialize state vaiables. */
1140 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
1141 cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
1143 /* Set flow info */
1144 cfhsi->flow_off_sent = 0;
1147 * Allocate a TX buffer with the size of a HSI packet descriptors
1148 * and the necessary room for CAIF payload frames.
1150 cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
1151 if (!cfhsi->tx_buf) {
1152 res = -ENODEV;
1153 goto err_alloc_tx;
1157 * Allocate a RX buffer with the size of two HSI packet descriptors and
1158 * the necessary room for CAIF payload frames.
1160 cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1161 if (!cfhsi->rx_buf) {
1162 res = -ENODEV;
1163 goto err_alloc_rx;
1166 cfhsi->rx_flip_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1167 if (!cfhsi->rx_flip_buf) {
1168 res = -ENODEV;
1169 goto err_alloc_rx_flip;
1172 /* Initialize aggregation timeout */
1173 cfhsi->cfg.aggregation_timeout = hsi_default_config.aggregation_timeout;
1175 /* Initialize recieve vaiables. */
1176 cfhsi->rx_ptr = cfhsi->rx_buf;
1177 cfhsi->rx_len = CFHSI_DESC_SZ;
1179 /* Initialize spin locks. */
1180 spin_lock_init(&cfhsi->lock);
1182 /* Set up the driver. */
1183 cfhsi->cb_ops.tx_done_cb = cfhsi_tx_done_cb;
1184 cfhsi->cb_ops.rx_done_cb = cfhsi_rx_done_cb;
1185 cfhsi->cb_ops.wake_up_cb = cfhsi_wake_up_cb;
1186 cfhsi->cb_ops.wake_down_cb = cfhsi_wake_down_cb;
1188 /* Initialize the work queues. */
1189 INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
1190 INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
1191 INIT_WORK(&cfhsi->out_of_sync_work, cfhsi_out_of_sync);
1193 /* Clear all bit fields. */
1194 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
1195 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
1196 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
1197 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
1199 /* Create work thread. */
1200 cfhsi->wq = alloc_ordered_workqueue(cfhsi->ndev->name, WQ_MEM_RECLAIM);
1201 if (!cfhsi->wq) {
1202 netdev_err(cfhsi->ndev, "%s: Failed to create work queue.\n",
1203 __func__);
1204 res = -ENODEV;
1205 goto err_create_wq;
1208 /* Initialize wait queues. */
1209 init_waitqueue_head(&cfhsi->wake_up_wait);
1210 init_waitqueue_head(&cfhsi->wake_down_wait);
1211 init_waitqueue_head(&cfhsi->flush_fifo_wait);
1213 /* Setup the inactivity timer. */
1214 init_timer(&cfhsi->inactivity_timer);
1215 cfhsi->inactivity_timer.data = (unsigned long)cfhsi;
1216 cfhsi->inactivity_timer.function = cfhsi_inactivity_tout;
1217 /* Setup the slowpath RX timer. */
1218 init_timer(&cfhsi->rx_slowpath_timer);
1219 cfhsi->rx_slowpath_timer.data = (unsigned long)cfhsi;
1220 cfhsi->rx_slowpath_timer.function = cfhsi_rx_slowpath;
1221 /* Setup the aggregation timer. */
1222 init_timer(&cfhsi->aggregation_timer);
1223 cfhsi->aggregation_timer.data = (unsigned long)cfhsi;
1224 cfhsi->aggregation_timer.function = cfhsi_aggregation_tout;
1226 /* Activate HSI interface. */
1227 res = cfhsi->ops->cfhsi_up(cfhsi->ops);
1228 if (res) {
1229 netdev_err(cfhsi->ndev,
1230 "%s: can't activate HSI interface: %d.\n",
1231 __func__, res);
1232 goto err_activate;
1235 /* Flush FIFO */
1236 res = cfhsi_flush_fifo(cfhsi);
1237 if (res) {
1238 netdev_err(cfhsi->ndev, "%s: Can't flush FIFO: %d.\n",
1239 __func__, res);
1240 goto err_net_reg;
1242 return res;
1244 err_net_reg:
1245 cfhsi->ops->cfhsi_down(cfhsi->ops);
1246 err_activate:
1247 destroy_workqueue(cfhsi->wq);
1248 err_create_wq:
1249 kfree(cfhsi->rx_flip_buf);
1250 err_alloc_rx_flip:
1251 kfree(cfhsi->rx_buf);
1252 err_alloc_rx:
1253 kfree(cfhsi->tx_buf);
1254 err_alloc_tx:
1255 return res;
1258 static int cfhsi_close(struct net_device *ndev)
1260 struct cfhsi *cfhsi = netdev_priv(ndev);
1261 u8 *tx_buf, *rx_buf, *flip_buf;
1263 /* going to shutdown driver */
1264 set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1266 /* Delete timers if pending */
1267 del_timer_sync(&cfhsi->inactivity_timer);
1268 del_timer_sync(&cfhsi->rx_slowpath_timer);
1269 del_timer_sync(&cfhsi->aggregation_timer);
1271 /* Cancel pending RX request (if any) */
1272 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
1274 /* Destroy workqueue */
1275 destroy_workqueue(cfhsi->wq);
1277 /* Store bufferes: will be freed later. */
1278 tx_buf = cfhsi->tx_buf;
1279 rx_buf = cfhsi->rx_buf;
1280 flip_buf = cfhsi->rx_flip_buf;
1281 /* Flush transmit queues. */
1282 cfhsi_abort_tx(cfhsi);
1284 /* Deactivate interface */
1285 cfhsi->ops->cfhsi_down(cfhsi->ops);
1287 /* Free buffers. */
1288 kfree(tx_buf);
1289 kfree(rx_buf);
1290 kfree(flip_buf);
1291 return 0;
1294 static void cfhsi_uninit(struct net_device *dev)
1296 struct cfhsi *cfhsi = netdev_priv(dev);
1297 ASSERT_RTNL();
1298 symbol_put(cfhsi_get_device);
1299 list_del(&cfhsi->list);
1302 static const struct net_device_ops cfhsi_netdevops = {
1303 .ndo_uninit = cfhsi_uninit,
1304 .ndo_open = cfhsi_open,
1305 .ndo_stop = cfhsi_close,
1306 .ndo_start_xmit = cfhsi_xmit
1309 static void cfhsi_netlink_parms(struct nlattr *data[], struct cfhsi *cfhsi)
1311 int i;
1313 if (!data) {
1314 pr_debug("no params data found\n");
1315 return;
1318 i = __IFLA_CAIF_HSI_INACTIVITY_TOUT;
1320 * Inactivity timeout in millisecs. Lowest possible value is 1,
1321 * and highest possible is NEXT_TIMER_MAX_DELTA.
1323 if (data[i]) {
1324 u32 inactivity_timeout = nla_get_u32(data[i]);
1325 /* Pre-calculate inactivity timeout. */
1326 cfhsi->cfg.inactivity_timeout = inactivity_timeout * HZ / 1000;
1327 if (cfhsi->cfg.inactivity_timeout == 0)
1328 cfhsi->cfg.inactivity_timeout = 1;
1329 else if (cfhsi->cfg.inactivity_timeout > NEXT_TIMER_MAX_DELTA)
1330 cfhsi->cfg.inactivity_timeout = NEXT_TIMER_MAX_DELTA;
1333 i = __IFLA_CAIF_HSI_AGGREGATION_TOUT;
1334 if (data[i])
1335 cfhsi->cfg.aggregation_timeout = nla_get_u32(data[i]);
1337 i = __IFLA_CAIF_HSI_HEAD_ALIGN;
1338 if (data[i])
1339 cfhsi->cfg.head_align = nla_get_u32(data[i]);
1341 i = __IFLA_CAIF_HSI_TAIL_ALIGN;
1342 if (data[i])
1343 cfhsi->cfg.tail_align = nla_get_u32(data[i]);
1345 i = __IFLA_CAIF_HSI_QHIGH_WATERMARK;
1346 if (data[i])
1347 cfhsi->cfg.q_high_mark = nla_get_u32(data[i]);
1349 i = __IFLA_CAIF_HSI_QLOW_WATERMARK;
1350 if (data[i])
1351 cfhsi->cfg.q_low_mark = nla_get_u32(data[i]);
1354 static int caif_hsi_changelink(struct net_device *dev, struct nlattr *tb[],
1355 struct nlattr *data[],
1356 struct netlink_ext_ack *extack)
1358 cfhsi_netlink_parms(data, netdev_priv(dev));
1359 netdev_state_change(dev);
1360 return 0;
1363 static const struct nla_policy caif_hsi_policy[__IFLA_CAIF_HSI_MAX + 1] = {
1364 [__IFLA_CAIF_HSI_INACTIVITY_TOUT] = { .type = NLA_U32, .len = 4 },
1365 [__IFLA_CAIF_HSI_AGGREGATION_TOUT] = { .type = NLA_U32, .len = 4 },
1366 [__IFLA_CAIF_HSI_HEAD_ALIGN] = { .type = NLA_U32, .len = 4 },
1367 [__IFLA_CAIF_HSI_TAIL_ALIGN] = { .type = NLA_U32, .len = 4 },
1368 [__IFLA_CAIF_HSI_QHIGH_WATERMARK] = { .type = NLA_U32, .len = 4 },
1369 [__IFLA_CAIF_HSI_QLOW_WATERMARK] = { .type = NLA_U32, .len = 4 },
1372 static size_t caif_hsi_get_size(const struct net_device *dev)
1374 int i;
1375 size_t s = 0;
1376 for (i = __IFLA_CAIF_HSI_UNSPEC + 1; i < __IFLA_CAIF_HSI_MAX; i++)
1377 s += nla_total_size(caif_hsi_policy[i].len);
1378 return s;
1381 static int caif_hsi_fill_info(struct sk_buff *skb, const struct net_device *dev)
1383 struct cfhsi *cfhsi = netdev_priv(dev);
1385 if (nla_put_u32(skb, __IFLA_CAIF_HSI_INACTIVITY_TOUT,
1386 cfhsi->cfg.inactivity_timeout) ||
1387 nla_put_u32(skb, __IFLA_CAIF_HSI_AGGREGATION_TOUT,
1388 cfhsi->cfg.aggregation_timeout) ||
1389 nla_put_u32(skb, __IFLA_CAIF_HSI_HEAD_ALIGN,
1390 cfhsi->cfg.head_align) ||
1391 nla_put_u32(skb, __IFLA_CAIF_HSI_TAIL_ALIGN,
1392 cfhsi->cfg.tail_align) ||
1393 nla_put_u32(skb, __IFLA_CAIF_HSI_QHIGH_WATERMARK,
1394 cfhsi->cfg.q_high_mark) ||
1395 nla_put_u32(skb, __IFLA_CAIF_HSI_QLOW_WATERMARK,
1396 cfhsi->cfg.q_low_mark))
1397 return -EMSGSIZE;
1399 return 0;
1402 static int caif_hsi_newlink(struct net *src_net, struct net_device *dev,
1403 struct nlattr *tb[], struct nlattr *data[],
1404 struct netlink_ext_ack *extack)
1406 struct cfhsi *cfhsi = NULL;
1407 struct cfhsi_ops *(*get_ops)(void);
1409 ASSERT_RTNL();
1411 cfhsi = netdev_priv(dev);
1412 cfhsi_netlink_parms(data, cfhsi);
1414 get_ops = symbol_get(cfhsi_get_ops);
1415 if (!get_ops) {
1416 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1417 return -ENODEV;
1420 /* Assign the HSI device. */
1421 cfhsi->ops = (*get_ops)();
1422 if (!cfhsi->ops) {
1423 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1424 goto err;
1427 /* Assign the driver to this HSI device. */
1428 cfhsi->ops->cb_ops = &cfhsi->cb_ops;
1429 if (register_netdevice(dev)) {
1430 pr_warn("%s: caif_hsi device registration failed\n", __func__);
1431 goto err;
1433 /* Add CAIF HSI device to list. */
1434 list_add_tail(&cfhsi->list, &cfhsi_list);
1436 return 0;
1437 err:
1438 symbol_put(cfhsi_get_ops);
1439 return -ENODEV;
1442 static struct rtnl_link_ops caif_hsi_link_ops __read_mostly = {
1443 .kind = "cfhsi",
1444 .priv_size = sizeof(struct cfhsi),
1445 .setup = cfhsi_setup,
1446 .maxtype = __IFLA_CAIF_HSI_MAX,
1447 .policy = caif_hsi_policy,
1448 .newlink = caif_hsi_newlink,
1449 .changelink = caif_hsi_changelink,
1450 .get_size = caif_hsi_get_size,
1451 .fill_info = caif_hsi_fill_info,
1454 static void __exit cfhsi_exit_module(void)
1456 struct list_head *list_node;
1457 struct list_head *n;
1458 struct cfhsi *cfhsi;
1460 rtnl_link_unregister(&caif_hsi_link_ops);
1462 rtnl_lock();
1463 list_for_each_safe(list_node, n, &cfhsi_list) {
1464 cfhsi = list_entry(list_node, struct cfhsi, list);
1465 unregister_netdev(cfhsi->ndev);
1467 rtnl_unlock();
1470 static int __init cfhsi_init_module(void)
1472 return rtnl_link_register(&caif_hsi_link_ops);
1475 module_init(cfhsi_init_module);
1476 module_exit(cfhsi_exit_module);