1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Qualcomm BAM-DMUX WWAN network driver
4 * Copyright (c) 2020, Stephan Gerhold <stephan@gerhold.net>
5 */
6
7 #include <linux/atomic.h>
8 #include <linux/bitops.h>
9 #include <linux/completion.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmaengine.h>
12 #include <linux/if_arp.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/soc/qcom/smem_state.h>
19 #include <linux/spinlock.h>
20 #include <linux/wait.h>
21 #include <linux/workqueue.h>
22 #include <net/pkt_sched.h>
23
24 #define BAM_DMUX_BUFFER_SIZE SZ_2K
25 #define BAM_DMUX_HDR_SIZE sizeof(struct bam_dmux_hdr)
26 #define BAM_DMUX_MAX_DATA_SIZE (BAM_DMUX_BUFFER_SIZE - BAM_DMUX_HDR_SIZE)
27 #define BAM_DMUX_NUM_SKB 32
28
29 #define BAM_DMUX_HDR_MAGIC 0x33fc
30
31 #define BAM_DMUX_AUTOSUSPEND_DELAY 1000
32 #define BAM_DMUX_REMOTE_TIMEOUT msecs_to_jiffies(2000)
33
34 enum {
35 BAM_DMUX_CMD_DATA,
36 BAM_DMUX_CMD_OPEN,
37 BAM_DMUX_CMD_CLOSE,
38 };
39
40 enum {
41 BAM_DMUX_CH_DATA_0,
42 BAM_DMUX_CH_DATA_1,
43 BAM_DMUX_CH_DATA_2,
44 BAM_DMUX_CH_DATA_3,
45 BAM_DMUX_CH_DATA_4,
46 BAM_DMUX_CH_DATA_5,
47 BAM_DMUX_CH_DATA_6,
48 BAM_DMUX_CH_DATA_7,
49 BAM_DMUX_NUM_CH
50 };
51
52 struct bam_dmux_hdr {
53 u16 magic;
54 u8 signal;
55 u8 cmd;
56 u8 pad;
57 u8 ch;
58 u16 len;
59 };
60
61 struct bam_dmux_skb_dma {
62 struct bam_dmux *dmux;
63 struct sk_buff *skb;
64 dma_addr_t addr;
65 };
66
67 struct bam_dmux {
68 struct device *dev;
69
70 int pc_irq;
71 bool pc_state, pc_ack_state;
72 struct qcom_smem_state *pc, *pc_ack;
73 u32 pc_mask, pc_ack_mask;
74 wait_queue_head_t pc_wait;
75 struct completion pc_ack_completion;
76
77 struct dma_chan *rx, *tx;
78 struct bam_dmux_skb_dma rx_skbs[BAM_DMUX_NUM_SKB];
79 struct bam_dmux_skb_dma tx_skbs[BAM_DMUX_NUM_SKB];
80 spinlock_t tx_lock; /* Protect tx_skbs, tx_next_skb */
81 unsigned int tx_next_skb;
82 atomic_long_t tx_deferred_skb;
83 struct work_struct tx_wakeup_work;
84
85 DECLARE_BITMAP(remote_channels, BAM_DMUX_NUM_CH);
86 struct work_struct register_netdev_work;
87 struct net_device *netdevs[BAM_DMUX_NUM_CH];
88 };
89
90 struct bam_dmux_netdev {
91 struct bam_dmux *dmux;
92 u8 ch;
93 };
94
bam_dmux_pc_vote(struct bam_dmux * dmux,bool enable)95 static void bam_dmux_pc_vote(struct bam_dmux *dmux, bool enable)
96 {
97 reinit_completion(&dmux->pc_ack_completion);
98 qcom_smem_state_update_bits(dmux->pc, dmux->pc_mask,
99 enable ? dmux->pc_mask : 0);
100 }
101
bam_dmux_pc_ack(struct bam_dmux * dmux)102 static void bam_dmux_pc_ack(struct bam_dmux *dmux)
103 {
104 qcom_smem_state_update_bits(dmux->pc_ack, dmux->pc_ack_mask,
105 dmux->pc_ack_state ? 0 : dmux->pc_ack_mask);
106 dmux->pc_ack_state = !dmux->pc_ack_state;
107 }
108
bam_dmux_skb_dma_map(struct bam_dmux_skb_dma * skb_dma,enum dma_data_direction dir)109 static bool bam_dmux_skb_dma_map(struct bam_dmux_skb_dma *skb_dma,
110 enum dma_data_direction dir)
111 {
112 struct device *dev = skb_dma->dmux->dev;
113
114 skb_dma->addr = dma_map_single(dev, skb_dma->skb->data, skb_dma->skb->len, dir);
115 if (dma_mapping_error(dev, skb_dma->addr)) {
116 dev_err(dev, "Failed to DMA map buffer\n");
117 skb_dma->addr = 0;
118 return false;
119 }
120
121 return true;
122 }
123
bam_dmux_skb_dma_unmap(struct bam_dmux_skb_dma * skb_dma,enum dma_data_direction dir)124 static void bam_dmux_skb_dma_unmap(struct bam_dmux_skb_dma *skb_dma,
125 enum dma_data_direction dir)
126 {
127 dma_unmap_single(skb_dma->dmux->dev, skb_dma->addr, skb_dma->skb->len, dir);
128 skb_dma->addr = 0;
129 }
130
bam_dmux_tx_wake_queues(struct bam_dmux * dmux)131 static void bam_dmux_tx_wake_queues(struct bam_dmux *dmux)
132 {
133 int i;
134
135 dev_dbg(dmux->dev, "wake queues\n");
136
137 for (i = 0; i < BAM_DMUX_NUM_CH; ++i) {
138 struct net_device *netdev = dmux->netdevs[i];
139
140 if (netdev && netif_running(netdev))
141 netif_wake_queue(netdev);
142 }
143 }
144
bam_dmux_tx_stop_queues(struct bam_dmux * dmux)145 static void bam_dmux_tx_stop_queues(struct bam_dmux *dmux)
146 {
147 int i;
148
149 dev_dbg(dmux->dev, "stop queues\n");
150
151 for (i = 0; i < BAM_DMUX_NUM_CH; ++i) {
152 struct net_device *netdev = dmux->netdevs[i];
153
154 if (netdev)
155 netif_stop_queue(netdev);
156 }
157 }
158
bam_dmux_tx_done(struct bam_dmux_skb_dma * skb_dma)159 static void bam_dmux_tx_done(struct bam_dmux_skb_dma *skb_dma)
160 {
161 struct bam_dmux *dmux = skb_dma->dmux;
162 unsigned long flags;
163
164 pm_runtime_put_autosuspend(dmux->dev);
165
166 if (skb_dma->addr)
167 bam_dmux_skb_dma_unmap(skb_dma, DMA_TO_DEVICE);
168
169 spin_lock_irqsave(&dmux->tx_lock, flags);
170 skb_dma->skb = NULL;
171 if (skb_dma == &dmux->tx_skbs[dmux->tx_next_skb % BAM_DMUX_NUM_SKB])
172 bam_dmux_tx_wake_queues(dmux);
173 spin_unlock_irqrestore(&dmux->tx_lock, flags);
174 }
175
bam_dmux_tx_callback(void * data)176 static void bam_dmux_tx_callback(void *data)
177 {
178 struct bam_dmux_skb_dma *skb_dma = data;
179 struct sk_buff *skb = skb_dma->skb;
180
181 bam_dmux_tx_done(skb_dma);
182 dev_consume_skb_any(skb);
183 }
184
bam_dmux_skb_dma_submit_tx(struct bam_dmux_skb_dma * skb_dma)185 static bool bam_dmux_skb_dma_submit_tx(struct bam_dmux_skb_dma *skb_dma)
186 {
187 struct bam_dmux *dmux = skb_dma->dmux;
188 struct dma_async_tx_descriptor *desc;
189
190 desc = dmaengine_prep_slave_single(dmux->tx, skb_dma->addr,
191 skb_dma->skb->len, DMA_MEM_TO_DEV,
192 DMA_PREP_INTERRUPT);
193 if (!desc) {
194 dev_err(dmux->dev, "Failed to prepare TX DMA buffer\n");
195 return false;
196 }
197
198 desc->callback = bam_dmux_tx_callback;
199 desc->callback_param = skb_dma;
200 desc->cookie = dmaengine_submit(desc);
201 return true;
202 }
203
204 static struct bam_dmux_skb_dma *
bam_dmux_tx_queue(struct bam_dmux * dmux,struct sk_buff * skb)205 bam_dmux_tx_queue(struct bam_dmux *dmux, struct sk_buff *skb)
206 {
207 struct bam_dmux_skb_dma *skb_dma;
208 unsigned long flags;
209
210 spin_lock_irqsave(&dmux->tx_lock, flags);
211
212 skb_dma = &dmux->tx_skbs[dmux->tx_next_skb % BAM_DMUX_NUM_SKB];
213 if (skb_dma->skb) {
214 bam_dmux_tx_stop_queues(dmux);
215 spin_unlock_irqrestore(&dmux->tx_lock, flags);
216 return NULL;
217 }
218 skb_dma->skb = skb;
219
220 dmux->tx_next_skb++;
221 if (dmux->tx_skbs[dmux->tx_next_skb % BAM_DMUX_NUM_SKB].skb)
222 bam_dmux_tx_stop_queues(dmux);
223
224 spin_unlock_irqrestore(&dmux->tx_lock, flags);
225 return skb_dma;
226 }
227
bam_dmux_send_cmd(struct bam_dmux_netdev * bndev,u8 cmd)228 static int bam_dmux_send_cmd(struct bam_dmux_netdev *bndev, u8 cmd)
229 {
230 struct bam_dmux *dmux = bndev->dmux;
231 struct bam_dmux_skb_dma *skb_dma;
232 struct bam_dmux_hdr *hdr;
233 struct sk_buff *skb;
234 int ret;
235
236 skb = alloc_skb(sizeof(*hdr), GFP_KERNEL);
237 if (!skb)
238 return -ENOMEM;
239
240 hdr = skb_put_zero(skb, sizeof(*hdr));
241 hdr->magic = BAM_DMUX_HDR_MAGIC;
242 hdr->cmd = cmd;
243 hdr->ch = bndev->ch;
244
245 skb_dma = bam_dmux_tx_queue(dmux, skb);
246 if (!skb_dma) {
247 ret = -EAGAIN;
248 goto free_skb;
249 }
250
251 ret = pm_runtime_get_sync(dmux->dev);
252 if (ret < 0)
253 goto tx_fail;
254
255 if (!bam_dmux_skb_dma_map(skb_dma, DMA_TO_DEVICE)) {
256 ret = -ENOMEM;
257 goto tx_fail;
258 }
259
260 if (!bam_dmux_skb_dma_submit_tx(skb_dma)) {
261 ret = -EIO;
262 goto tx_fail;
263 }
264
265 dma_async_issue_pending(dmux->tx);
266 return 0;
267
268 tx_fail:
269 bam_dmux_tx_done(skb_dma);
270 free_skb:
271 dev_kfree_skb(skb);
272 return ret;
273 }
274
bam_dmux_netdev_open(struct net_device * netdev)275 static int bam_dmux_netdev_open(struct net_device *netdev)
276 {
277 struct bam_dmux_netdev *bndev = netdev_priv(netdev);
278 int ret;
279
280 ret = bam_dmux_send_cmd(bndev, BAM_DMUX_CMD_OPEN);
281 if (ret)
282 return ret;
283
284 netif_start_queue(netdev);
285 return 0;
286 }
287
bam_dmux_netdev_stop(struct net_device * netdev)288 static int bam_dmux_netdev_stop(struct net_device *netdev)
289 {
290 struct bam_dmux_netdev *bndev = netdev_priv(netdev);
291
292 netif_stop_queue(netdev);
293 bam_dmux_send_cmd(bndev, BAM_DMUX_CMD_CLOSE);
294 return 0;
295 }
296
needed_room(unsigned int avail,unsigned int needed)297 static unsigned int needed_room(unsigned int avail, unsigned int needed)
298 {
299 if (avail >= needed)
300 return 0;
301 return needed - avail;
302 }
303
bam_dmux_tx_prepare_skb(struct bam_dmux_netdev * bndev,struct sk_buff * skb)304 static int bam_dmux_tx_prepare_skb(struct bam_dmux_netdev *bndev,
305 struct sk_buff *skb)
306 {
307 unsigned int head = needed_room(skb_headroom(skb), BAM_DMUX_HDR_SIZE);
308 unsigned int pad = sizeof(u32) - skb->len % sizeof(u32);
309 unsigned int tail = needed_room(skb_tailroom(skb), pad);
310 struct bam_dmux_hdr *hdr;
311 int ret;
312
313 if (head || tail || skb_cloned(skb)) {
314 ret = pskb_expand_head(skb, head, tail, GFP_ATOMIC);
315 if (ret)
316 return ret;
317 }
318
319 hdr = skb_push(skb, sizeof(*hdr));
320 hdr->magic = BAM_DMUX_HDR_MAGIC;
321 hdr->signal = 0;
322 hdr->cmd = BAM_DMUX_CMD_DATA;
323 hdr->pad = pad;
324 hdr->ch = bndev->ch;
325 hdr->len = skb->len - sizeof(*hdr);
326 if (pad)
327 skb_put_zero(skb, pad);
328
329 return 0;
330 }
331
bam_dmux_netdev_start_xmit(struct sk_buff * skb,struct net_device * netdev)332 static netdev_tx_t bam_dmux_netdev_start_xmit(struct sk_buff *skb,
333 struct net_device *netdev)
334 {
335 struct bam_dmux_netdev *bndev = netdev_priv(netdev);
336 struct bam_dmux *dmux = bndev->dmux;
337 struct bam_dmux_skb_dma *skb_dma;
338 int active, ret;
339
340 skb_dma = bam_dmux_tx_queue(dmux, skb);
341 if (!skb_dma)
342 return NETDEV_TX_BUSY;
343
344 active = pm_runtime_get(dmux->dev);
345 if (active < 0 && active != -EINPROGRESS)
346 goto drop;
347
348 ret = bam_dmux_tx_prepare_skb(bndev, skb);
349 if (ret)
350 goto drop;
351
352 if (!bam_dmux_skb_dma_map(skb_dma, DMA_TO_DEVICE))
353 goto drop;
354
355 if (active <= 0) {
356 /* Cannot sleep here so mark skb for wakeup handler and return */
357 if (!atomic_long_fetch_or(BIT(skb_dma - dmux->tx_skbs),
358 &dmux->tx_deferred_skb))
359 queue_pm_work(&dmux->tx_wakeup_work);
360 return NETDEV_TX_OK;
361 }
362
363 if (!bam_dmux_skb_dma_submit_tx(skb_dma))
364 goto drop;
365
366 dma_async_issue_pending(dmux->tx);
367 return NETDEV_TX_OK;
368
369 drop:
370 bam_dmux_tx_done(skb_dma);
371 dev_kfree_skb_any(skb);
372 return NETDEV_TX_OK;
373 }
374
bam_dmux_tx_wakeup_work(struct work_struct * work)375 static void bam_dmux_tx_wakeup_work(struct work_struct *work)
376 {
377 struct bam_dmux *dmux = container_of(work, struct bam_dmux, tx_wakeup_work);
378 unsigned long pending;
379 int ret, i;
380
381 ret = pm_runtime_resume_and_get(dmux->dev);
382 if (ret < 0) {
383 dev_err(dmux->dev, "Failed to resume: %d\n", ret);
384 return;
385 }
386
387 pending = atomic_long_xchg(&dmux->tx_deferred_skb, 0);
388 if (!pending)
389 goto out;
390
391 dev_dbg(dmux->dev, "pending skbs after wakeup: %#lx\n", pending);
392 for_each_set_bit(i, &pending, BAM_DMUX_NUM_SKB) {
393 bam_dmux_skb_dma_submit_tx(&dmux->tx_skbs[i]);
394 }
395 dma_async_issue_pending(dmux->tx);
396
397 out:
398 pm_runtime_put_autosuspend(dmux->dev);
399 }
400
401 static const struct net_device_ops bam_dmux_ops = {
402 .ndo_open = bam_dmux_netdev_open,
403 .ndo_stop = bam_dmux_netdev_stop,
404 .ndo_start_xmit = bam_dmux_netdev_start_xmit,
405 };
406
407 static const struct device_type wwan_type = {
408 .name = "wwan",
409 };
410
bam_dmux_netdev_setup(struct net_device * dev)411 static void bam_dmux_netdev_setup(struct net_device *dev)
412 {
413 dev->netdev_ops = &bam_dmux_ops;
414
415 dev->type = ARPHRD_RAWIP;
416 SET_NETDEV_DEVTYPE(dev, &wwan_type);
417 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
418
419 dev->mtu = ETH_DATA_LEN;
420 dev->max_mtu = BAM_DMUX_MAX_DATA_SIZE;
421 dev->needed_headroom = sizeof(struct bam_dmux_hdr);
422 dev->needed_tailroom = sizeof(u32); /* word-aligned */
423 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
424
425 /* This perm addr will be used as interface identifier by IPv6 */
426 dev->addr_assign_type = NET_ADDR_RANDOM;
427 eth_random_addr(dev->perm_addr);
428 }
429
bam_dmux_register_netdev_work(struct work_struct * work)430 static void bam_dmux_register_netdev_work(struct work_struct *work)
431 {
432 struct bam_dmux *dmux = container_of(work, struct bam_dmux, register_netdev_work);
433 struct bam_dmux_netdev *bndev;
434 struct net_device *netdev;
435 int ch, ret;
436
437 for_each_set_bit(ch, dmux->remote_channels, BAM_DMUX_NUM_CH) {
438 if (dmux->netdevs[ch])
439 continue;
440
441 netdev = alloc_netdev(sizeof(*bndev), "wwan%d", NET_NAME_ENUM,
442 bam_dmux_netdev_setup);
443 if (!netdev)
444 return;
445
446 SET_NETDEV_DEV(netdev, dmux->dev);
447 netdev->dev_port = ch;
448
449 bndev = netdev_priv(netdev);
450 bndev->dmux = dmux;
451 bndev->ch = ch;
452
453 ret = register_netdev(netdev);
454 if (ret) {
455 dev_err(dmux->dev, "Failed to register netdev for channel %u: %d\n",
456 ch, ret);
457 free_netdev(netdev);
458 return;
459 }
460
461 dmux->netdevs[ch] = netdev;
462 }
463 }
464
465 static void bam_dmux_rx_callback(void *data);
466
bam_dmux_skb_dma_submit_rx(struct bam_dmux_skb_dma * skb_dma)467 static bool bam_dmux_skb_dma_submit_rx(struct bam_dmux_skb_dma *skb_dma)
468 {
469 struct bam_dmux *dmux = skb_dma->dmux;
470 struct dma_async_tx_descriptor *desc;
471
472 desc = dmaengine_prep_slave_single(dmux->rx, skb_dma->addr,
473 skb_dma->skb->len, DMA_DEV_TO_MEM,
474 DMA_PREP_INTERRUPT);
475 if (!desc) {
476 dev_err(dmux->dev, "Failed to prepare RX DMA buffer\n");
477 return false;
478 }
479
480 desc->callback = bam_dmux_rx_callback;
481 desc->callback_param = skb_dma;
482 desc->cookie = dmaengine_submit(desc);
483 return true;
484 }
485
bam_dmux_skb_dma_queue_rx(struct bam_dmux_skb_dma * skb_dma,gfp_t gfp)486 static bool bam_dmux_skb_dma_queue_rx(struct bam_dmux_skb_dma *skb_dma, gfp_t gfp)
487 {
488 if (!skb_dma->skb) {
489 skb_dma->skb = __netdev_alloc_skb(NULL, BAM_DMUX_BUFFER_SIZE, gfp);
490 if (!skb_dma->skb)
491 return false;
492 skb_put(skb_dma->skb, BAM_DMUX_BUFFER_SIZE);
493 }
494
495 return bam_dmux_skb_dma_map(skb_dma, DMA_FROM_DEVICE) &&
496 bam_dmux_skb_dma_submit_rx(skb_dma);
497 }
498
bam_dmux_cmd_data(struct bam_dmux_skb_dma * skb_dma)499 static void bam_dmux_cmd_data(struct bam_dmux_skb_dma *skb_dma)
500 {
501 struct bam_dmux *dmux = skb_dma->dmux;
502 struct sk_buff *skb = skb_dma->skb;
503 struct bam_dmux_hdr *hdr = (struct bam_dmux_hdr *)skb->data;
504 struct net_device *netdev = dmux->netdevs[hdr->ch];
505
506 if (!netdev || !netif_running(netdev)) {
507 dev_warn(dmux->dev, "Data for inactive channel %u\n", hdr->ch);
508 return;
509 }
510
511 if (hdr->len > BAM_DMUX_MAX_DATA_SIZE) {
512 dev_err(dmux->dev, "Data larger than buffer? (%u > %u)\n",
513 hdr->len, (u16)BAM_DMUX_MAX_DATA_SIZE);
514 return;
515 }
516
517 skb_dma->skb = NULL; /* Hand over to network stack */
518
519 skb_pull(skb, sizeof(*hdr));
520 skb_trim(skb, hdr->len);
521 skb->dev = netdev;
522
523 /* Only Raw-IP/QMAP is supported by this driver */
524 switch (skb->data[0] & 0xf0) {
525 case 0x40:
526 skb->protocol = htons(ETH_P_IP);
527 break;
528 case 0x60:
529 skb->protocol = htons(ETH_P_IPV6);
530 break;
531 default:
532 skb->protocol = htons(ETH_P_MAP);
533 break;
534 }
535
536 netif_receive_skb(skb);
537 }
538
bam_dmux_cmd_open(struct bam_dmux * dmux,struct bam_dmux_hdr * hdr)539 static void bam_dmux_cmd_open(struct bam_dmux *dmux, struct bam_dmux_hdr *hdr)
540 {
541 struct net_device *netdev = dmux->netdevs[hdr->ch];
542
543 dev_dbg(dmux->dev, "open channel: %u\n", hdr->ch);
544
545 if (__test_and_set_bit(hdr->ch, dmux->remote_channels)) {
546 dev_warn(dmux->dev, "Channel already open: %u\n", hdr->ch);
547 return;
548 }
549
550 if (netdev) {
551 netif_device_attach(netdev);
552 } else {
553 /* Cannot sleep here, schedule work to register the netdev */
554 schedule_work(&dmux->register_netdev_work);
555 }
556 }
557
bam_dmux_cmd_close(struct bam_dmux * dmux,struct bam_dmux_hdr * hdr)558 static void bam_dmux_cmd_close(struct bam_dmux *dmux, struct bam_dmux_hdr *hdr)
559 {
560 struct net_device *netdev = dmux->netdevs[hdr->ch];
561
562 dev_dbg(dmux->dev, "close channel: %u\n", hdr->ch);
563
564 if (!__test_and_clear_bit(hdr->ch, dmux->remote_channels)) {
565 dev_err(dmux->dev, "Channel not open: %u\n", hdr->ch);
566 return;
567 }
568
569 if (netdev)
570 netif_device_detach(netdev);
571 }
572
bam_dmux_rx_callback(void * data)573 static void bam_dmux_rx_callback(void *data)
574 {
575 struct bam_dmux_skb_dma *skb_dma = data;
576 struct bam_dmux *dmux = skb_dma->dmux;
577 struct sk_buff *skb = skb_dma->skb;
578 struct bam_dmux_hdr *hdr = (struct bam_dmux_hdr *)skb->data;
579
580 bam_dmux_skb_dma_unmap(skb_dma, DMA_FROM_DEVICE);
581
582 if (hdr->magic != BAM_DMUX_HDR_MAGIC) {
583 dev_err(dmux->dev, "Invalid magic in header: %#x\n", hdr->magic);
584 goto out;
585 }
586
587 if (hdr->ch >= BAM_DMUX_NUM_CH) {
588 dev_dbg(dmux->dev, "Unsupported channel: %u\n", hdr->ch);
589 goto out;
590 }
591
592 switch (hdr->cmd) {
593 case BAM_DMUX_CMD_DATA:
594 bam_dmux_cmd_data(skb_dma);
595 break;
596 case BAM_DMUX_CMD_OPEN:
597 bam_dmux_cmd_open(dmux, hdr);
598 break;
599 case BAM_DMUX_CMD_CLOSE:
600 bam_dmux_cmd_close(dmux, hdr);
601 break;
602 default:
603 dev_err(dmux->dev, "Unsupported command %u on channel %u\n",
604 hdr->cmd, hdr->ch);
605 break;
606 }
607
608 out:
609 if (bam_dmux_skb_dma_queue_rx(skb_dma, GFP_ATOMIC))
610 dma_async_issue_pending(dmux->rx);
611 }
612
bam_dmux_power_on(struct bam_dmux * dmux)613 static bool bam_dmux_power_on(struct bam_dmux *dmux)
614 {
615 struct device *dev = dmux->dev;
616 struct dma_slave_config dma_rx_conf = {
617 .direction = DMA_DEV_TO_MEM,
618 .src_maxburst = BAM_DMUX_BUFFER_SIZE,
619 };
620 int i;
621
622 dmux->rx = dma_request_chan(dev, "rx");
623 if (IS_ERR(dmux->rx)) {
624 dev_err(dev, "Failed to request RX DMA channel: %pe\n", dmux->rx);
625 dmux->rx = NULL;
626 return false;
627 }
628 dmaengine_slave_config(dmux->rx, &dma_rx_conf);
629
630 for (i = 0; i < BAM_DMUX_NUM_SKB; i++) {
631 if (!bam_dmux_skb_dma_queue_rx(&dmux->rx_skbs[i], GFP_KERNEL))
632 return false;
633 }
634 dma_async_issue_pending(dmux->rx);
635
636 return true;
637 }
638
bam_dmux_free_skbs(struct bam_dmux_skb_dma skbs[],enum dma_data_direction dir)639 static void bam_dmux_free_skbs(struct bam_dmux_skb_dma skbs[],
640 enum dma_data_direction dir)
641 {
642 int i;
643
644 for (i = 0; i < BAM_DMUX_NUM_SKB; i++) {
645 struct bam_dmux_skb_dma *skb_dma = &skbs[i];
646
647 if (skb_dma->addr)
648 bam_dmux_skb_dma_unmap(skb_dma, dir);
649 if (skb_dma->skb) {
650 dev_kfree_skb(skb_dma->skb);
651 skb_dma->skb = NULL;
652 }
653 }
654 }
655
bam_dmux_power_off(struct bam_dmux * dmux)656 static void bam_dmux_power_off(struct bam_dmux *dmux)
657 {
658 if (dmux->tx) {
659 dmaengine_terminate_sync(dmux->tx);
660 dma_release_channel(dmux->tx);
661 dmux->tx = NULL;
662 }
663
664 if (dmux->rx) {
665 dmaengine_terminate_sync(dmux->rx);
666 dma_release_channel(dmux->rx);
667 dmux->rx = NULL;
668 }
669
670 bam_dmux_free_skbs(dmux->rx_skbs, DMA_FROM_DEVICE);
671 }
672
bam_dmux_pc_irq(int irq,void * data)673 static irqreturn_t bam_dmux_pc_irq(int irq, void *data)
674 {
675 struct bam_dmux *dmux = data;
676 bool new_state = !dmux->pc_state;
677
678 dev_dbg(dmux->dev, "pc: %u\n", new_state);
679
680 if (new_state) {
681 if (bam_dmux_power_on(dmux))
682 bam_dmux_pc_ack(dmux);
683 else
684 bam_dmux_power_off(dmux);
685 } else {
686 bam_dmux_power_off(dmux);
687 bam_dmux_pc_ack(dmux);
688 }
689
690 dmux->pc_state = new_state;
691 wake_up_all(&dmux->pc_wait);
692
693 return IRQ_HANDLED;
694 }
695
bam_dmux_pc_ack_irq(int irq,void * data)696 static irqreturn_t bam_dmux_pc_ack_irq(int irq, void *data)
697 {
698 struct bam_dmux *dmux = data;
699
700 dev_dbg(dmux->dev, "pc ack\n");
701 complete_all(&dmux->pc_ack_completion);
702
703 return IRQ_HANDLED;
704 }
705
bam_dmux_runtime_suspend(struct device * dev)706 static int bam_dmux_runtime_suspend(struct device *dev)
707 {
708 struct bam_dmux *dmux = dev_get_drvdata(dev);
709
710 dev_dbg(dev, "runtime suspend\n");
711 bam_dmux_pc_vote(dmux, false);
712
713 return 0;
714 }
715
bam_dmux_runtime_resume(struct device * dev)716 static int __maybe_unused bam_dmux_runtime_resume(struct device *dev)
717 {
718 struct bam_dmux *dmux = dev_get_drvdata(dev);
719
720 dev_dbg(dev, "runtime resume\n");
721
722 /* Wait until previous power down was acked */
723 if (!wait_for_completion_timeout(&dmux->pc_ack_completion,
724 BAM_DMUX_REMOTE_TIMEOUT))
725 return -ETIMEDOUT;
726
727 /* Vote for power state */
728 bam_dmux_pc_vote(dmux, true);
729
730 /* Wait for ack */
731 if (!wait_for_completion_timeout(&dmux->pc_ack_completion,
732 BAM_DMUX_REMOTE_TIMEOUT)) {
733 bam_dmux_pc_vote(dmux, false);
734 return -ETIMEDOUT;
735 }
736
737 /* Wait until we're up */
738 if (!wait_event_timeout(dmux->pc_wait, dmux->pc_state,
739 BAM_DMUX_REMOTE_TIMEOUT)) {
740 bam_dmux_pc_vote(dmux, false);
741 return -ETIMEDOUT;
742 }
743
744 /* Ensure that we actually initialized successfully */
745 if (!dmux->rx) {
746 bam_dmux_pc_vote(dmux, false);
747 return -ENXIO;
748 }
749
750 /* Request TX channel if necessary */
751 if (dmux->tx)
752 return 0;
753
754 dmux->tx = dma_request_chan(dev, "tx");
755 if (IS_ERR(dmux->tx)) {
756 dev_err(dev, "Failed to request TX DMA channel: %pe\n", dmux->tx);
757 dmux->tx = NULL;
758 bam_dmux_runtime_suspend(dev);
759 return -ENXIO;
760 }
761
762 return 0;
763 }
764
bam_dmux_probe(struct platform_device * pdev)765 static int bam_dmux_probe(struct platform_device *pdev)
766 {
767 struct device *dev = &pdev->dev;
768 struct bam_dmux *dmux;
769 int ret, pc_ack_irq, i;
770 unsigned int bit;
771
772 dmux = devm_kzalloc(dev, sizeof(*dmux), GFP_KERNEL);
773 if (!dmux)
774 return -ENOMEM;
775
776 dmux->dev = dev;
777 platform_set_drvdata(pdev, dmux);
778
779 dmux->pc_irq = platform_get_irq_byname(pdev, "pc");
780 if (dmux->pc_irq < 0)
781 return dmux->pc_irq;
782
783 pc_ack_irq = platform_get_irq_byname(pdev, "pc-ack");
784 if (pc_ack_irq < 0)
785 return pc_ack_irq;
786
787 dmux->pc = devm_qcom_smem_state_get(dev, "pc", &bit);
788 if (IS_ERR(dmux->pc))
789 return dev_err_probe(dev, PTR_ERR(dmux->pc),
790 "Failed to get pc state\n");
791 dmux->pc_mask = BIT(bit);
792
793 dmux->pc_ack = devm_qcom_smem_state_get(dev, "pc-ack", &bit);
794 if (IS_ERR(dmux->pc_ack))
795 return dev_err_probe(dev, PTR_ERR(dmux->pc_ack),
796 "Failed to get pc-ack state\n");
797 dmux->pc_ack_mask = BIT(bit);
798
799 init_waitqueue_head(&dmux->pc_wait);
800 init_completion(&dmux->pc_ack_completion);
801 complete_all(&dmux->pc_ack_completion);
802
803 spin_lock_init(&dmux->tx_lock);
804 INIT_WORK(&dmux->tx_wakeup_work, bam_dmux_tx_wakeup_work);
805 INIT_WORK(&dmux->register_netdev_work, bam_dmux_register_netdev_work);
806
807 for (i = 0; i < BAM_DMUX_NUM_SKB; i++) {
808 dmux->rx_skbs[i].dmux = dmux;
809 dmux->tx_skbs[i].dmux = dmux;
810 }
811
812 /* Runtime PM manages our own power vote.
813 * Note that the RX path may be active even if we are runtime suspended,
814 * since it is controlled by the remote side.
815 */
816 pm_runtime_set_autosuspend_delay(dev, BAM_DMUX_AUTOSUSPEND_DELAY);
817 pm_runtime_use_autosuspend(dev);
818 pm_runtime_enable(dev);
819
820 ret = devm_request_threaded_irq(dev, pc_ack_irq, NULL, bam_dmux_pc_ack_irq,
821 IRQF_ONESHOT, NULL, dmux);
822 if (ret)
823 goto err_disable_pm;
824
825 ret = devm_request_threaded_irq(dev, dmux->pc_irq, NULL, bam_dmux_pc_irq,
826 IRQF_ONESHOT, NULL, dmux);
827 if (ret)
828 goto err_disable_pm;
829
830 ret = irq_get_irqchip_state(dmux->pc_irq, IRQCHIP_STATE_LINE_LEVEL,
831 &dmux->pc_state);
832 if (ret)
833 goto err_disable_pm;
834
835 /* Check if remote finished initialization before us */
836 if (dmux->pc_state) {
837 if (bam_dmux_power_on(dmux))
838 bam_dmux_pc_ack(dmux);
839 else
840 bam_dmux_power_off(dmux);
841 }
842
843 return 0;
844
845 err_disable_pm:
846 pm_runtime_disable(dev);
847 pm_runtime_dont_use_autosuspend(dev);
848 return ret;
849 }
850
bam_dmux_remove(struct platform_device * pdev)851 static void bam_dmux_remove(struct platform_device *pdev)
852 {
853 struct bam_dmux *dmux = platform_get_drvdata(pdev);
854 struct device *dev = dmux->dev;
855 LIST_HEAD(list);
856 int i;
857
858 /* Unregister network interfaces */
859 cancel_work_sync(&dmux->register_netdev_work);
860 rtnl_lock();
861 for (i = 0; i < BAM_DMUX_NUM_CH; ++i)
862 if (dmux->netdevs[i])
863 unregister_netdevice_queue(dmux->netdevs[i], &list);
864 unregister_netdevice_many(&list);
865 rtnl_unlock();
866 cancel_work_sync(&dmux->tx_wakeup_work);
867
868 /* Drop our own power vote */
869 pm_runtime_disable(dev);
870 pm_runtime_dont_use_autosuspend(dev);
871 bam_dmux_runtime_suspend(dev);
872 pm_runtime_set_suspended(dev);
873
874 /* Try to wait for remote side to drop power vote */
875 if (!wait_event_timeout(dmux->pc_wait, !dmux->rx, BAM_DMUX_REMOTE_TIMEOUT))
876 dev_err(dev, "Timed out waiting for remote side to suspend\n");
877
878 /* Make sure everything is cleaned up before we return */
879 disable_irq(dmux->pc_irq);
880 bam_dmux_power_off(dmux);
881 bam_dmux_free_skbs(dmux->tx_skbs, DMA_TO_DEVICE);
882 }
883
884 static const struct dev_pm_ops bam_dmux_pm_ops = {
885 SET_RUNTIME_PM_OPS(bam_dmux_runtime_suspend, bam_dmux_runtime_resume, NULL)
886 };
887
888 static const struct of_device_id bam_dmux_of_match[] = {
889 { .compatible = "qcom,bam-dmux" },
890 { /* sentinel */ }
891 };
892 MODULE_DEVICE_TABLE(of, bam_dmux_of_match);
893
894 static struct platform_driver bam_dmux_driver = {
895 .probe = bam_dmux_probe,
896 .remove = bam_dmux_remove,
897 .driver = {
898 .name = "bam-dmux",
899 .pm = &bam_dmux_pm_ops,
900 .of_match_table = bam_dmux_of_match,
901 },
902 };
903 module_platform_driver(bam_dmux_driver);
904
905 MODULE_LICENSE("GPL v2");
906 MODULE_DESCRIPTION("Qualcomm BAM-DMUX WWAN Network Driver");
907 MODULE_AUTHOR("Stephan Gerhold <stephan@gerhold.net>");
908