| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* MHI MBIM Network driver - Network/MBIM over MHI bus |
| 3 |
* |
| 4 |
* Copyright (C) 2021 Linaro Ltd <[email protected]> |
| 5 |
* |
| 6 |
* This driver copy some code from cdc_ncm, which is: |
| 7 |
* Copyright (C) ST-Ericsson 2010-2012 |
| 8 |
* and cdc_mbim, which is: |
| 9 |
* Copyright (c) 2012 Smith Micro Software, Inc. |
| 10 |
* Copyright (c) 2012 Bjørn Mork <[email protected]> |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <linux/ethtool.h> |
| 15 |
#include <linux/if_arp.h> |
| 16 |
#include <linux/if_vlan.h> |
| 17 |
#include <linux/ip.h> |
| 18 |
#include <linux/mhi.h> |
| 19 |
#include <linux/mii.h> |
| 20 |
#include <linux/mod_devicetable.h> |
| 21 |
#include <linux/module.h> |
| 22 |
#include <linux/netdevice.h> |
| 23 |
#include <linux/skbuff.h> |
| 24 |
#include <linux/u64_stats_sync.h> |
| 25 |
#include <linux/usb.h> |
| 26 |
#include <linux/usb/cdc.h> |
| 27 |
#include <linux/usb/usbnet.h> |
| 28 |
#include <linux/usb/cdc_ncm.h> |
| 29 |
#include <linux/wwan.h> |
| 30 |
|
| 31 |
/* 3500 allows to optimize skb allocation, the skbs will basically fit in |
| 32 |
* one 4K page. Large MBIM packets will simply be split over several MHI |
| 33 |
* transfers and chained by the MHI net layer (zerocopy). |
| 34 |
*/ |
| 35 |
#define MHI_DEFAULT_MRU 3500 |
| 36 |
|
| 37 |
#define MHI_MBIM_DEFAULT_MTU 1500 |
| 38 |
#define MHI_MAX_BUF_SZ 0xffff |
| 39 |
|
| 40 |
#define MBIM_NDP16_SIGN_MASK 0x00ffffff |
| 41 |
|
| 42 |
#define MHI_MBIM_LINK_HASH_SIZE 8 |
| 43 |
#define LINK_HASH(session) ((session) % MHI_MBIM_LINK_HASH_SIZE) |
| 44 |
|
| 45 |
#define WDS_BIND_MUX_DATA_PORT_MUX_ID 112 |
| 46 |
|
| 47 |
struct mhi_mbim_link { |
| 48 |
struct mhi_mbim_context *mbim; |
| 49 |
struct net_device *ndev; |
| 50 |
unsigned int session; |
| 51 |
|
| 52 |
/* stats */ |
| 53 |
u64_stats_t rx_packets; |
| 54 |
u64_stats_t rx_bytes; |
| 55 |
u64_stats_t rx_errors; |
| 56 |
u64_stats_t tx_packets; |
| 57 |
u64_stats_t tx_bytes; |
| 58 |
u64_stats_t tx_errors; |
| 59 |
u64_stats_t tx_dropped; |
| 60 |
struct u64_stats_sync tx_syncp; |
| 61 |
struct u64_stats_sync rx_syncp; |
| 62 |
|
| 63 |
struct hlist_node hlnode; |
| 64 |
}; |
| 65 |
|
| 66 |
struct mhi_mbim_context { |
| 67 |
struct mhi_device *mdev; |
| 68 |
struct sk_buff *skbagg_head; |
| 69 |
struct sk_buff *skbagg_tail; |
| 70 |
unsigned int mru; |
| 71 |
u32 rx_queue_sz; |
| 72 |
u16 rx_seq; |
| 73 |
u16 tx_seq; |
| 74 |
struct delayed_work rx_refill; |
| 75 |
spinlock_t tx_lock; |
| 76 |
struct hlist_head link_list[MHI_MBIM_LINK_HASH_SIZE]; |
| 77 |
}; |
| 78 |
|
| 79 |
struct mbim_tx_hdr { |
| 80 |
struct usb_cdc_ncm_nth16 nth16; |
| 81 |
struct usb_cdc_ncm_ndp16 ndp16; |
| 82 |
struct usb_cdc_ncm_dpe16 dpe16[2]; |
| 83 |
} __packed; |
| 84 |
|
| 85 |
static struct mhi_mbim_link *mhi_mbim_get_link_rcu(struct mhi_mbim_context *mbim, |
| 86 |
unsigned int session) |
| 87 |
{ |
| 88 |
struct mhi_mbim_link *link; |
| 89 |
|
| 90 |
hlist_for_each_entry_rcu(link, &mbim->link_list[LINK_HASH(session)], hlnode) { |
| 91 |
if (link->session == session) |
| 92 |
return link; |
| 93 |
} |
| 94 |
|
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
static int mhi_mbim_get_link_mux_id(struct mhi_controller *cntrl) |
| 99 |
{ |
| 100 |
if (strcmp(cntrl->name, "foxconn-dw5934e") == 0 || |
| 101 |
strcmp(cntrl->name, "foxconn-t99w640") == 0 || |
| 102 |
strcmp(cntrl->name, "foxconn-t99w760") == 0) |
| 103 |
return WDS_BIND_MUX_DATA_PORT_MUX_ID; |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static struct sk_buff *mbim_tx_fixup(struct sk_buff *skb, unsigned int session, |
| 109 |
u16 tx_seq) |
| 110 |
{ |
| 111 |
unsigned int dgram_size = skb->len; |
| 112 |
struct usb_cdc_ncm_nth16 *nth16; |
| 113 |
struct usb_cdc_ncm_ndp16 *ndp16; |
| 114 |
struct mbim_tx_hdr *mbim_hdr; |
| 115 |
|
| 116 |
/* Only one NDP is sent, containing the IP packet (no aggregation) */ |
| 117 |
|
| 118 |
/* Ensure we have enough headroom for crafting MBIM header */ |
| 119 |
if (skb_cow_head(skb, sizeof(struct mbim_tx_hdr))) { |
| 120 |
dev_kfree_skb_any(skb); |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
mbim_hdr = skb_push(skb, sizeof(struct mbim_tx_hdr)); |
| 125 |
|
| 126 |
/* Fill NTB header */ |
| 127 |
nth16 = &mbim_hdr->nth16; |
| 128 |
nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN); |
| 129 |
nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16)); |
| 130 |
nth16->wSequence = cpu_to_le16(tx_seq); |
| 131 |
nth16->wBlockLength = cpu_to_le16(skb->len); |
| 132 |
nth16->wNdpIndex = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16)); |
| 133 |
|
| 134 |
/* Fill the unique NDP */ |
| 135 |
ndp16 = &mbim_hdr->ndp16; |
| 136 |
ndp16->dwSignature = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN | (session << 24)); |
| 137 |
ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) |
| 138 |
+ sizeof(struct usb_cdc_ncm_dpe16) * 2); |
| 139 |
ndp16->wNextNdpIndex = 0; |
| 140 |
|
| 141 |
/* Datagram follows the mbim header */ |
| 142 |
ndp16->dpe16[0].wDatagramIndex = cpu_to_le16(sizeof(struct mbim_tx_hdr)); |
| 143 |
ndp16->dpe16[0].wDatagramLength = cpu_to_le16(dgram_size); |
| 144 |
|
| 145 |
/* null termination */ |
| 146 |
ndp16->dpe16[1].wDatagramIndex = 0; |
| 147 |
ndp16->dpe16[1].wDatagramLength = 0; |
| 148 |
|
| 149 |
return skb; |
| 150 |
} |
| 151 |
|
| 152 |
static netdev_tx_t mhi_mbim_ndo_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 153 |
{ |
| 154 |
struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev); |
| 155 |
struct mhi_mbim_context *mbim = link->mbim; |
| 156 |
unsigned long flags; |
| 157 |
int err = -ENOMEM; |
| 158 |
|
| 159 |
/* Serialize MHI channel queuing and MBIM seq */ |
| 160 |
spin_lock_irqsave(&mbim->tx_lock, flags); |
| 161 |
|
| 162 |
skb = mbim_tx_fixup(skb, link->session, mbim->tx_seq); |
| 163 |
if (unlikely(!skb)) |
| 164 |
goto exit_unlock; |
| 165 |
|
| 166 |
err = mhi_queue_skb(mbim->mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT); |
| 167 |
|
| 168 |
if (mhi_queue_is_full(mbim->mdev, DMA_TO_DEVICE)) |
| 169 |
netif_stop_queue(ndev); |
| 170 |
|
| 171 |
if (!err) |
| 172 |
mbim->tx_seq++; |
| 173 |
|
| 174 |
exit_unlock: |
| 175 |
spin_unlock_irqrestore(&mbim->tx_lock, flags); |
| 176 |
|
| 177 |
if (unlikely(err)) { |
| 178 |
net_err_ratelimited("%s: Failed to queue TX buf (%d)\n", |
| 179 |
ndev->name, err); |
| 180 |
dev_kfree_skb_any(skb); |
| 181 |
goto exit_drop; |
| 182 |
} |
| 183 |
|
| 184 |
return NETDEV_TX_OK; |
| 185 |
|
| 186 |
exit_drop: |
| 187 |
u64_stats_update_begin(&link->tx_syncp); |
| 188 |
u64_stats_inc(&link->tx_dropped); |
| 189 |
u64_stats_update_end(&link->tx_syncp); |
| 190 |
|
| 191 |
return NETDEV_TX_OK; |
| 192 |
} |
| 193 |
|
| 194 |
static int mbim_rx_verify_nth16(struct mhi_mbim_context *mbim, struct sk_buff *skb) |
| 195 |
{ |
| 196 |
struct usb_cdc_ncm_nth16 *nth16; |
| 197 |
int len; |
| 198 |
|
| 199 |
if (skb->len < sizeof(struct usb_cdc_ncm_nth16) + |
| 200 |
sizeof(struct usb_cdc_ncm_ndp16)) { |
| 201 |
net_err_ratelimited("frame too short\n"); |
| 202 |
return -EINVAL; |
| 203 |
} |
| 204 |
|
| 205 |
nth16 = (struct usb_cdc_ncm_nth16 *)skb->data; |
| 206 |
|
| 207 |
if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) { |
| 208 |
net_err_ratelimited("invalid NTH16 signature <%#010x>\n", |
| 209 |
le32_to_cpu(nth16->dwSignature)); |
| 210 |
return -EINVAL; |
| 211 |
} |
| 212 |
|
| 213 |
/* No limit on the block length, except the size of the data pkt */ |
| 214 |
len = le16_to_cpu(nth16->wBlockLength); |
| 215 |
if (len > skb->len) { |
| 216 |
net_err_ratelimited("NTB does not fit into the skb %u/%u\n", |
| 217 |
len, skb->len); |
| 218 |
return -EINVAL; |
| 219 |
} |
| 220 |
|
| 221 |
if (mbim->rx_seq + 1 != le16_to_cpu(nth16->wSequence) && |
| 222 |
(mbim->rx_seq || le16_to_cpu(nth16->wSequence)) && |
| 223 |
!(mbim->rx_seq == 0xffff && !le16_to_cpu(nth16->wSequence))) { |
| 224 |
net_dbg_ratelimited("sequence number glitch prev=%d curr=%d\n", |
| 225 |
mbim->rx_seq, le16_to_cpu(nth16->wSequence)); |
| 226 |
} |
| 227 |
mbim->rx_seq = le16_to_cpu(nth16->wSequence); |
| 228 |
|
| 229 |
return le16_to_cpu(nth16->wNdpIndex); |
| 230 |
} |
| 231 |
|
| 232 |
static int mbim_rx_verify_ndp16(struct sk_buff *skb, struct usb_cdc_ncm_ndp16 *ndp16) |
| 233 |
{ |
| 234 |
int ret; |
| 235 |
|
| 236 |
if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) { |
| 237 |
net_err_ratelimited("invalid DPT16 length <%u>\n", |
| 238 |
le16_to_cpu(ndp16->wLength)); |
| 239 |
return -EINVAL; |
| 240 |
} |
| 241 |
|
| 242 |
ret = ((le16_to_cpu(ndp16->wLength) - sizeof(struct usb_cdc_ncm_ndp16)) |
| 243 |
/ sizeof(struct usb_cdc_ncm_dpe16)); |
| 244 |
ret--; /* Last entry is always a NULL terminator */ |
| 245 |
|
| 246 |
if (sizeof(struct usb_cdc_ncm_ndp16) + |
| 247 |
ret * sizeof(struct usb_cdc_ncm_dpe16) > skb->len) { |
| 248 |
net_err_ratelimited("Invalid nframes = %d\n", ret); |
| 249 |
return -EINVAL; |
| 250 |
} |
| 251 |
|
| 252 |
return ret; |
| 253 |
} |
| 254 |
|
| 255 |
static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb) |
| 256 |
{ |
| 257 |
int ndpoffset; |
| 258 |
|
| 259 |
/* Check NTB header and retrieve first NDP offset */ |
| 260 |
ndpoffset = mbim_rx_verify_nth16(mbim, skb); |
| 261 |
if (ndpoffset < 0) { |
| 262 |
net_err_ratelimited("mbim: Incorrect NTB header\n"); |
| 263 |
goto error; |
| 264 |
} |
| 265 |
|
| 266 |
/* Process each NDP */ |
| 267 |
while (1) { |
| 268 |
struct usb_cdc_ncm_ndp16 ndp16; |
| 269 |
struct usb_cdc_ncm_dpe16 dpe16; |
| 270 |
struct mhi_mbim_link *link; |
| 271 |
int nframes, n, dpeoffset; |
| 272 |
unsigned int session; |
| 273 |
|
| 274 |
if (skb_copy_bits(skb, ndpoffset, &ndp16, sizeof(ndp16))) { |
| 275 |
net_err_ratelimited("mbim: Incorrect NDP offset (%u)\n", |
| 276 |
ndpoffset); |
| 277 |
goto error; |
| 278 |
} |
| 279 |
|
| 280 |
/* Check NDP header and retrieve number of datagrams */ |
| 281 |
nframes = mbim_rx_verify_ndp16(skb, &ndp16); |
| 282 |
if (nframes < 0) { |
| 283 |
net_err_ratelimited("mbim: Incorrect NDP16\n"); |
| 284 |
goto error; |
| 285 |
} |
| 286 |
|
| 287 |
/* Only IP data type supported, no DSS in MHI context */ |
| 288 |
if ((ndp16.dwSignature & cpu_to_le32(MBIM_NDP16_SIGN_MASK)) |
| 289 |
!= cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN)) { |
| 290 |
net_err_ratelimited("mbim: Unsupported NDP type\n"); |
| 291 |
goto next_ndp; |
| 292 |
} |
| 293 |
|
| 294 |
session = (le32_to_cpu(ndp16.dwSignature) & ~MBIM_NDP16_SIGN_MASK) >> 24; |
| 295 |
|
| 296 |
rcu_read_lock(); |
| 297 |
|
| 298 |
link = mhi_mbim_get_link_rcu(mbim, session); |
| 299 |
if (!link) { |
| 300 |
net_err_ratelimited("mbim: bad packet session (%u)\n", session); |
| 301 |
goto unlock; |
| 302 |
} |
| 303 |
|
| 304 |
/* de-aggregate and deliver IP packets */ |
| 305 |
dpeoffset = ndpoffset + sizeof(struct usb_cdc_ncm_ndp16); |
| 306 |
for (n = 0; n < nframes; n++, dpeoffset += sizeof(dpe16)) { |
| 307 |
u16 dgram_offset, dgram_len; |
| 308 |
struct sk_buff *skbn; |
| 309 |
|
| 310 |
if (skb_copy_bits(skb, dpeoffset, &dpe16, sizeof(dpe16))) |
| 311 |
break; |
| 312 |
|
| 313 |
dgram_offset = le16_to_cpu(dpe16.wDatagramIndex); |
| 314 |
dgram_len = le16_to_cpu(dpe16.wDatagramLength); |
| 315 |
|
| 316 |
if (!dgram_offset || !dgram_len) |
| 317 |
break; /* null terminator */ |
| 318 |
|
| 319 |
skbn = netdev_alloc_skb(link->ndev, dgram_len); |
| 320 |
if (!skbn) |
| 321 |
continue; |
| 322 |
|
| 323 |
skb_put(skbn, dgram_len); |
| 324 |
skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len); |
| 325 |
|
| 326 |
switch (skbn->data[0] & 0xf0) { |
| 327 |
case 0x40: |
| 328 |
skbn->protocol = htons(ETH_P_IP); |
| 329 |
break; |
| 330 |
case 0x60: |
| 331 |
skbn->protocol = htons(ETH_P_IPV6); |
| 332 |
break; |
| 333 |
default: |
| 334 |
net_err_ratelimited("%s: unknown protocol\n", |
| 335 |
link->ndev->name); |
| 336 |
dev_kfree_skb_any(skbn); |
| 337 |
u64_stats_update_begin(&link->rx_syncp); |
| 338 |
u64_stats_inc(&link->rx_errors); |
| 339 |
u64_stats_update_end(&link->rx_syncp); |
| 340 |
continue; |
| 341 |
} |
| 342 |
|
| 343 |
u64_stats_update_begin(&link->rx_syncp); |
| 344 |
u64_stats_inc(&link->rx_packets); |
| 345 |
u64_stats_add(&link->rx_bytes, skbn->len); |
| 346 |
u64_stats_update_end(&link->rx_syncp); |
| 347 |
|
| 348 |
netif_rx(skbn); |
| 349 |
} |
| 350 |
unlock: |
| 351 |
rcu_read_unlock(); |
| 352 |
next_ndp: |
| 353 |
/* Other NDP to process? */ |
| 354 |
ndpoffset = (int)le16_to_cpu(ndp16.wNextNdpIndex); |
| 355 |
if (!ndpoffset) |
| 356 |
break; |
| 357 |
} |
| 358 |
|
| 359 |
/* free skb */ |
| 360 |
dev_consume_skb_any(skb); |
| 361 |
return; |
| 362 |
error: |
| 363 |
dev_kfree_skb_any(skb); |
| 364 |
} |
| 365 |
|
| 366 |
static struct sk_buff *mhi_net_skb_agg(struct mhi_mbim_context *mbim, |
| 367 |
struct sk_buff *skb) |
| 368 |
{ |
| 369 |
struct sk_buff *head = mbim->skbagg_head; |
| 370 |
struct sk_buff *tail = mbim->skbagg_tail; |
| 371 |
|
| 372 |
/* This is non-paged skb chaining using frag_list */ |
| 373 |
if (!head) { |
| 374 |
mbim->skbagg_head = skb; |
| 375 |
return skb; |
| 376 |
} |
| 377 |
|
| 378 |
if (!skb_shinfo(head)->frag_list) |
| 379 |
skb_shinfo(head)->frag_list = skb; |
| 380 |
else |
| 381 |
tail->next = skb; |
| 382 |
|
| 383 |
head->len += skb->len; |
| 384 |
head->data_len += skb->len; |
| 385 |
head->truesize += skb->truesize; |
| 386 |
|
| 387 |
mbim->skbagg_tail = skb; |
| 388 |
|
| 389 |
return mbim->skbagg_head; |
| 390 |
} |
| 391 |
|
| 392 |
static void mhi_net_rx_refill_work(struct work_struct *work) |
| 393 |
{ |
| 394 |
struct mhi_mbim_context *mbim = container_of(work, struct mhi_mbim_context, |
| 395 |
rx_refill.work); |
| 396 |
struct mhi_device *mdev = mbim->mdev; |
| 397 |
int err; |
| 398 |
|
| 399 |
while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) { |
| 400 |
struct sk_buff *skb = alloc_skb(mbim->mru, GFP_KERNEL); |
| 401 |
|
| 402 |
if (unlikely(!skb)) |
| 403 |
break; |
| 404 |
|
| 405 |
err = mhi_queue_skb(mdev, DMA_FROM_DEVICE, skb, |
| 406 |
mbim->mru, MHI_EOT); |
| 407 |
if (unlikely(err)) { |
| 408 |
kfree_skb(skb); |
| 409 |
break; |
| 410 |
} |
| 411 |
|
| 412 |
/* Do not hog the CPU if rx buffers are consumed faster than |
| 413 |
* queued (unlikely). |
| 414 |
*/ |
| 415 |
cond_resched(); |
| 416 |
} |
| 417 |
|
| 418 |
/* If we're still starved of rx buffers, reschedule later */ |
| 419 |
if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mbim->rx_queue_sz) |
| 420 |
schedule_delayed_work(&mbim->rx_refill, HZ / 2); |
| 421 |
} |
| 422 |
|
| 423 |
static void mhi_mbim_dl_callback(struct mhi_device *mhi_dev, |
| 424 |
struct mhi_result *mhi_res) |
| 425 |
{ |
| 426 |
struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev); |
| 427 |
struct sk_buff *skb = mhi_res->buf_addr; |
| 428 |
int free_desc_count; |
| 429 |
|
| 430 |
free_desc_count = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE); |
| 431 |
|
| 432 |
if (unlikely(mhi_res->transaction_status)) { |
| 433 |
switch (mhi_res->transaction_status) { |
| 434 |
case -EOVERFLOW: |
| 435 |
/* Packet has been split over multiple transfers */ |
| 436 |
skb_put(skb, mhi_res->bytes_xferd); |
| 437 |
mhi_net_skb_agg(mbim, skb); |
| 438 |
break; |
| 439 |
case -ENOTCONN: |
| 440 |
/* MHI layer stopping/resetting the DL channel */ |
| 441 |
dev_kfree_skb_any(skb); |
| 442 |
return; |
| 443 |
default: |
| 444 |
/* Unknown error, simply drop */ |
| 445 |
dev_kfree_skb_any(skb); |
| 446 |
} |
| 447 |
} else { |
| 448 |
skb_put(skb, mhi_res->bytes_xferd); |
| 449 |
|
| 450 |
if (mbim->skbagg_head) { |
| 451 |
/* Aggregate the final fragment */ |
| 452 |
skb = mhi_net_skb_agg(mbim, skb); |
| 453 |
mbim->skbagg_head = NULL; |
| 454 |
} |
| 455 |
|
| 456 |
mhi_mbim_rx(mbim, skb); |
| 457 |
} |
| 458 |
|
| 459 |
/* Refill if RX buffers queue becomes low */ |
| 460 |
if (free_desc_count >= mbim->rx_queue_sz / 2) |
| 461 |
schedule_delayed_work(&mbim->rx_refill, 0); |
| 462 |
} |
| 463 |
|
| 464 |
static void mhi_mbim_ndo_get_stats64(struct net_device *ndev, |
| 465 |
struct rtnl_link_stats64 *stats) |
| 466 |
{ |
| 467 |
struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev); |
| 468 |
unsigned int start; |
| 469 |
|
| 470 |
do { |
| 471 |
start = u64_stats_fetch_begin(&link->rx_syncp); |
| 472 |
stats->rx_packets = u64_stats_read(&link->rx_packets); |
| 473 |
stats->rx_bytes = u64_stats_read(&link->rx_bytes); |
| 474 |
stats->rx_errors = u64_stats_read(&link->rx_errors); |
| 475 |
} while (u64_stats_fetch_retry(&link->rx_syncp, start)); |
| 476 |
|
| 477 |
do { |
| 478 |
start = u64_stats_fetch_begin(&link->tx_syncp); |
| 479 |
stats->tx_packets = u64_stats_read(&link->tx_packets); |
| 480 |
stats->tx_bytes = u64_stats_read(&link->tx_bytes); |
| 481 |
stats->tx_errors = u64_stats_read(&link->tx_errors); |
| 482 |
stats->tx_dropped = u64_stats_read(&link->tx_dropped); |
| 483 |
} while (u64_stats_fetch_retry(&link->tx_syncp, start)); |
| 484 |
} |
| 485 |
|
| 486 |
static void mhi_mbim_ul_callback(struct mhi_device *mhi_dev, |
| 487 |
struct mhi_result *mhi_res) |
| 488 |
{ |
| 489 |
struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev); |
| 490 |
struct sk_buff *skb = mhi_res->buf_addr; |
| 491 |
struct net_device *ndev = skb->dev; |
| 492 |
struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev); |
| 493 |
|
| 494 |
/* Hardware has consumed the buffer, so free the skb (which is not |
| 495 |
* freed by the MHI stack) and perform accounting. |
| 496 |
*/ |
| 497 |
dev_consume_skb_any(skb); |
| 498 |
|
| 499 |
u64_stats_update_begin(&link->tx_syncp); |
| 500 |
if (unlikely(mhi_res->transaction_status)) { |
| 501 |
/* MHI layer stopping/resetting the UL channel */ |
| 502 |
if (mhi_res->transaction_status == -ENOTCONN) { |
| 503 |
u64_stats_update_end(&link->tx_syncp); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
u64_stats_inc(&link->tx_errors); |
| 508 |
} else { |
| 509 |
u64_stats_inc(&link->tx_packets); |
| 510 |
u64_stats_add(&link->tx_bytes, mhi_res->bytes_xferd); |
| 511 |
} |
| 512 |
u64_stats_update_end(&link->tx_syncp); |
| 513 |
|
| 514 |
if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mbim->mdev, DMA_TO_DEVICE)) |
| 515 |
netif_wake_queue(ndev); |
| 516 |
} |
| 517 |
|
| 518 |
static int mhi_mbim_ndo_open(struct net_device *ndev) |
| 519 |
{ |
| 520 |
struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev); |
| 521 |
|
| 522 |
/* Feed the MHI rx buffer pool */ |
| 523 |
schedule_delayed_work(&link->mbim->rx_refill, 0); |
| 524 |
|
| 525 |
/* Carrier is established via out-of-band channel (e.g. qmi) */ |
| 526 |
netif_carrier_on(ndev); |
| 527 |
|
| 528 |
netif_start_queue(ndev); |
| 529 |
|
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
static int mhi_mbim_ndo_stop(struct net_device *ndev) |
| 534 |
{ |
| 535 |
netif_stop_queue(ndev); |
| 536 |
netif_carrier_off(ndev); |
| 537 |
|
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
static const struct net_device_ops mhi_mbim_ndo = { |
| 542 |
.ndo_open = mhi_mbim_ndo_open, |
| 543 |
.ndo_stop = mhi_mbim_ndo_stop, |
| 544 |
.ndo_start_xmit = mhi_mbim_ndo_xmit, |
| 545 |
.ndo_get_stats64 = mhi_mbim_ndo_get_stats64, |
| 546 |
}; |
| 547 |
|
| 548 |
static int mhi_mbim_newlink(void *ctxt, struct net_device *ndev, u32 if_id, |
| 549 |
struct netlink_ext_ack *extack) |
| 550 |
{ |
| 551 |
struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev); |
| 552 |
struct mhi_mbim_context *mbim = ctxt; |
| 553 |
|
| 554 |
link->mbim = mbim; |
| 555 |
link->session = mhi_mbim_get_link_mux_id(link->mbim->mdev->mhi_cntrl) + if_id; |
| 556 |
link->ndev = ndev; |
| 557 |
u64_stats_init(&link->rx_syncp); |
| 558 |
u64_stats_init(&link->tx_syncp); |
| 559 |
|
| 560 |
rcu_read_lock(); |
| 561 |
if (mhi_mbim_get_link_rcu(mbim, if_id)) { |
| 562 |
rcu_read_unlock(); |
| 563 |
return -EEXIST; |
| 564 |
} |
| 565 |
rcu_read_unlock(); |
| 566 |
|
| 567 |
/* Already protected by RTNL lock */ |
| 568 |
hlist_add_head_rcu(&link->hlnode, &mbim->link_list[LINK_HASH(if_id)]); |
| 569 |
|
| 570 |
return register_netdevice(ndev); |
| 571 |
} |
| 572 |
|
| 573 |
static void mhi_mbim_dellink(void *ctxt, struct net_device *ndev, |
| 574 |
struct list_head *head) |
| 575 |
{ |
| 576 |
struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev); |
| 577 |
|
| 578 |
hlist_del_init_rcu(&link->hlnode); |
| 579 |
synchronize_rcu(); |
| 580 |
|
| 581 |
unregister_netdevice_queue(ndev, head); |
| 582 |
} |
| 583 |
|
| 584 |
static void mhi_mbim_setup(struct net_device *ndev) |
| 585 |
{ |
| 586 |
ndev->header_ops = NULL; /* No header */ |
| 587 |
ndev->type = ARPHRD_RAWIP; |
| 588 |
ndev->needed_headroom = sizeof(struct mbim_tx_hdr); |
| 589 |
ndev->hard_header_len = 0; |
| 590 |
ndev->addr_len = 0; |
| 591 |
ndev->flags = IFF_POINTOPOINT | IFF_NOARP; |
| 592 |
ndev->netdev_ops = &mhi_mbim_ndo; |
| 593 |
ndev->mtu = MHI_MBIM_DEFAULT_MTU; |
| 594 |
ndev->min_mtu = ETH_MIN_MTU; |
| 595 |
ndev->max_mtu = MHI_MAX_BUF_SZ - ndev->needed_headroom; |
| 596 |
ndev->tx_queue_len = 1000; |
| 597 |
ndev->needs_free_netdev = true; |
| 598 |
} |
| 599 |
|
| 600 |
static const struct wwan_ops mhi_mbim_wwan_ops = { |
| 601 |
.priv_size = sizeof(struct mhi_mbim_link), |
| 602 |
.setup = mhi_mbim_setup, |
| 603 |
.newlink = mhi_mbim_newlink, |
| 604 |
.dellink = mhi_mbim_dellink, |
| 605 |
}; |
| 606 |
|
| 607 |
static int mhi_mbim_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id) |
| 608 |
{ |
| 609 |
struct mhi_controller *cntrl = mhi_dev->mhi_cntrl; |
| 610 |
struct mhi_mbim_context *mbim; |
| 611 |
int err; |
| 612 |
|
| 613 |
mbim = devm_kzalloc(&mhi_dev->dev, sizeof(*mbim), GFP_KERNEL); |
| 614 |
if (!mbim) |
| 615 |
return -ENOMEM; |
| 616 |
|
| 617 |
spin_lock_init(&mbim->tx_lock); |
| 618 |
dev_set_drvdata(&mhi_dev->dev, mbim); |
| 619 |
mbim->mdev = mhi_dev; |
| 620 |
mbim->mru = mhi_dev->mhi_cntrl->mru ? mhi_dev->mhi_cntrl->mru : MHI_DEFAULT_MRU; |
| 621 |
|
| 622 |
INIT_DELAYED_WORK(&mbim->rx_refill, mhi_net_rx_refill_work); |
| 623 |
|
| 624 |
/* Start MHI channels */ |
| 625 |
err = mhi_prepare_for_transfer(mhi_dev); |
| 626 |
if (err) |
| 627 |
return err; |
| 628 |
|
| 629 |
/* Number of transfer descriptors determines size of the queue */ |
| 630 |
mbim->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE); |
| 631 |
|
| 632 |
/* Register wwan link ops with MHI controller representing WWAN instance */ |
| 633 |
return wwan_register_ops(&cntrl->mhi_dev->dev, &mhi_mbim_wwan_ops, mbim, 0); |
| 634 |
} |
| 635 |
|
| 636 |
static void mhi_mbim_remove(struct mhi_device *mhi_dev) |
| 637 |
{ |
| 638 |
struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev); |
| 639 |
struct mhi_controller *cntrl = mhi_dev->mhi_cntrl; |
| 640 |
|
| 641 |
mhi_unprepare_from_transfer(mhi_dev); |
| 642 |
cancel_delayed_work_sync(&mbim->rx_refill); |
| 643 |
wwan_unregister_ops(&cntrl->mhi_dev->dev); |
| 644 |
kfree_skb(mbim->skbagg_head); |
| 645 |
dev_set_drvdata(&mhi_dev->dev, NULL); |
| 646 |
} |
| 647 |
|
| 648 |
static const struct mhi_device_id mhi_mbim_id_table[] = { |
| 649 |
/* Hardware accelerated data PATH (to modem IPA), MBIM protocol */ |
| 650 |
{ .chan = "IP_HW0_MBIM", .driver_data = 0 }, |
| 651 |
{} |
| 652 |
}; |
| 653 |
MODULE_DEVICE_TABLE(mhi, mhi_mbim_id_table); |
| 654 |
|
| 655 |
static struct mhi_driver mhi_mbim_driver = { |
| 656 |
.probe = mhi_mbim_probe, |
| 657 |
.remove = mhi_mbim_remove, |
| 658 |
.dl_xfer_cb = mhi_mbim_dl_callback, |
| 659 |
.ul_xfer_cb = mhi_mbim_ul_callback, |
| 660 |
.id_table = mhi_mbim_id_table, |
| 661 |
.driver = { |
| 662 |
.name = "mhi_wwan_mbim", |
| 663 |
}, |
| 664 |
}; |
| 665 |
|
| 666 |
module_mhi_driver(mhi_mbim_driver); |
| 667 |
|
| 668 |
MODULE_AUTHOR("Loic Poulain <[email protected]>"); |
| 669 |
MODULE_DESCRIPTION("Network/MBIM over MHI"); |
| 670 |
MODULE_LICENSE("GPL v2"); |