1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* MHI Network driver - Network over MHI bus 3 * 4 * Copyright (C) 2020 Linaro Ltd <loic.poulain@linaro.org> 5 */ 6 7 #include <linux/if_arp.h> 8 #include <linux/mhi.h> 9 #include <linux/module.h> 10 #include <linux/netdevice.h> 11 #include <linux/skbuff.h> 12 #include <linux/u64_stats_sync.h> 13 14 #define MHI_NET_MIN_MTU ETH_MIN_MTU 15 #define MHI_NET_MAX_MTU 0xffff 16 #define MHI_NET_DEFAULT_MTU 0x4000 17 18 struct mhi_net_stats { 19 u64_stats_t rx_packets; 20 u64_stats_t rx_bytes; 21 u64_stats_t rx_errors; 22 u64_stats_t tx_packets; 23 u64_stats_t tx_bytes; 24 u64_stats_t tx_errors; 25 u64_stats_t tx_dropped; 26 struct u64_stats_sync tx_syncp; 27 struct u64_stats_sync rx_syncp; 28 }; 29 30 struct mhi_net_dev { 31 struct mhi_device *mdev; 32 struct net_device *ndev; 33 struct sk_buff *skbagg_head; 34 struct sk_buff *skbagg_tail; 35 struct delayed_work rx_refill; 36 struct mhi_net_stats stats; 37 u32 rx_queue_sz; 38 int msg_enable; 39 unsigned int mru; 40 }; 41 42 struct mhi_device_info { 43 const char *netname; 44 }; 45 46 static int mhi_ndo_open(struct net_device *ndev) 47 { 48 struct mhi_net_dev *mhi_netdev = netdev_priv(ndev); 49 50 /* Feed the rx buffer pool */ 51 schedule_delayed_work(&mhi_netdev->rx_refill, 0); 52 53 /* Carrier is established via out-of-band channel (e.g. qmi) */ 54 netif_carrier_on(ndev); 55 56 netif_start_queue(ndev); 57 58 return 0; 59 } 60 61 static int mhi_ndo_stop(struct net_device *ndev) 62 { 63 struct mhi_net_dev *mhi_netdev = netdev_priv(ndev); 64 65 netif_stop_queue(ndev); 66 netif_carrier_off(ndev); 67 cancel_delayed_work_sync(&mhi_netdev->rx_refill); 68 69 return 0; 70 } 71 72 static netdev_tx_t mhi_ndo_xmit(struct sk_buff *skb, struct net_device *ndev) 73 { 74 struct mhi_net_dev *mhi_netdev = netdev_priv(ndev); 75 struct mhi_device *mdev = mhi_netdev->mdev; 76 int err; 77 78 err = mhi_queue_skb(mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT); 79 if (unlikely(err)) { 80 net_err_ratelimited("%s: Failed to queue TX buf (%d)\n", 81 ndev->name, err); 82 dev_kfree_skb_any(skb); 83 goto exit_drop; 84 } 85 86 if (mhi_queue_is_full(mdev, DMA_TO_DEVICE)) 87 netif_stop_queue(ndev); 88 89 return NETDEV_TX_OK; 90 91 exit_drop: 92 u64_stats_update_begin(&mhi_netdev->stats.tx_syncp); 93 u64_stats_inc(&mhi_netdev->stats.tx_dropped); 94 u64_stats_update_end(&mhi_netdev->stats.tx_syncp); 95 96 return NETDEV_TX_OK; 97 } 98 99 static void mhi_ndo_get_stats64(struct net_device *ndev, 100 struct rtnl_link_stats64 *stats) 101 { 102 struct mhi_net_dev *mhi_netdev = netdev_priv(ndev); 103 unsigned int start; 104 105 do { 106 start = u64_stats_fetch_begin(&mhi_netdev->stats.rx_syncp); 107 stats->rx_packets = u64_stats_read(&mhi_netdev->stats.rx_packets); 108 stats->rx_bytes = u64_stats_read(&mhi_netdev->stats.rx_bytes); 109 stats->rx_errors = u64_stats_read(&mhi_netdev->stats.rx_errors); 110 } while (u64_stats_fetch_retry(&mhi_netdev->stats.rx_syncp, start)); 111 112 do { 113 start = u64_stats_fetch_begin(&mhi_netdev->stats.tx_syncp); 114 stats->tx_packets = u64_stats_read(&mhi_netdev->stats.tx_packets); 115 stats->tx_bytes = u64_stats_read(&mhi_netdev->stats.tx_bytes); 116 stats->tx_errors = u64_stats_read(&mhi_netdev->stats.tx_errors); 117 stats->tx_dropped = u64_stats_read(&mhi_netdev->stats.tx_dropped); 118 } while (u64_stats_fetch_retry(&mhi_netdev->stats.tx_syncp, start)); 119 } 120 121 static const struct net_device_ops mhi_netdev_ops = { 122 .ndo_open = mhi_ndo_open, 123 .ndo_stop = mhi_ndo_stop, 124 .ndo_start_xmit = mhi_ndo_xmit, 125 .ndo_get_stats64 = mhi_ndo_get_stats64, 126 }; 127 128 static void mhi_net_setup(struct net_device *ndev) 129 { 130 ndev->header_ops = NULL; /* No header */ 131 ndev->type = ARPHRD_RAWIP; 132 ndev->hard_header_len = 0; 133 ndev->addr_len = 0; 134 ndev->flags = IFF_POINTOPOINT | IFF_NOARP; 135 ndev->netdev_ops = &mhi_netdev_ops; 136 ndev->mtu = MHI_NET_DEFAULT_MTU; 137 ndev->min_mtu = MHI_NET_MIN_MTU; 138 ndev->max_mtu = MHI_NET_MAX_MTU; 139 ndev->tx_queue_len = 1000; 140 } 141 142 static struct sk_buff *mhi_net_skb_agg(struct mhi_net_dev *mhi_netdev, 143 struct sk_buff *skb) 144 { 145 struct sk_buff *head = mhi_netdev->skbagg_head; 146 struct sk_buff *tail = mhi_netdev->skbagg_tail; 147 148 /* This is non-paged skb chaining using frag_list */ 149 if (!head) { 150 mhi_netdev->skbagg_head = skb; 151 return skb; 152 } 153 154 if (!skb_shinfo(head)->frag_list) 155 skb_shinfo(head)->frag_list = skb; 156 else 157 tail->next = skb; 158 159 head->len += skb->len; 160 head->data_len += skb->len; 161 head->truesize += skb->truesize; 162 163 mhi_netdev->skbagg_tail = skb; 164 165 return mhi_netdev->skbagg_head; 166 } 167 168 static void mhi_net_dl_callback(struct mhi_device *mhi_dev, 169 struct mhi_result *mhi_res) 170 { 171 struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev); 172 struct sk_buff *skb = mhi_res->buf_addr; 173 int free_desc_count; 174 175 free_desc_count = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE); 176 177 if (unlikely(mhi_res->transaction_status)) { 178 switch (mhi_res->transaction_status) { 179 case -EOVERFLOW: 180 /* Packet can not fit in one MHI buffer and has been 181 * split over multiple MHI transfers, do re-aggregation. 182 * That usually means the device side MTU is larger than 183 * the host side MTU/MRU. Since this is not optimal, 184 * print a warning (once). 185 */ 186 netdev_warn_once(mhi_netdev->ndev, 187 "Fragmented packets received, fix MTU?\n"); 188 skb_put(skb, mhi_res->bytes_xferd); 189 mhi_net_skb_agg(mhi_netdev, skb); 190 break; 191 case -ENOTCONN: 192 /* MHI layer stopping/resetting the DL channel */ 193 dev_kfree_skb_any(skb); 194 return; 195 default: 196 /* Unknown error, simply drop */ 197 dev_kfree_skb_any(skb); 198 u64_stats_update_begin(&mhi_netdev->stats.rx_syncp); 199 u64_stats_inc(&mhi_netdev->stats.rx_errors); 200 u64_stats_update_end(&mhi_netdev->stats.rx_syncp); 201 } 202 } else { 203 skb_put(skb, mhi_res->bytes_xferd); 204 205 if (mhi_netdev->skbagg_head) { 206 /* Aggregate the final fragment */ 207 skb = mhi_net_skb_agg(mhi_netdev, skb); 208 mhi_netdev->skbagg_head = NULL; 209 } 210 211 switch (skb->data[0] & 0xf0) { 212 case 0x40: 213 skb->protocol = htons(ETH_P_IP); 214 break; 215 case 0x60: 216 skb->protocol = htons(ETH_P_IPV6); 217 break; 218 default: 219 skb->protocol = htons(ETH_P_MAP); 220 break; 221 } 222 223 u64_stats_update_begin(&mhi_netdev->stats.rx_syncp); 224 u64_stats_inc(&mhi_netdev->stats.rx_packets); 225 u64_stats_add(&mhi_netdev->stats.rx_bytes, skb->len); 226 u64_stats_update_end(&mhi_netdev->stats.rx_syncp); 227 __netif_rx(skb); 228 } 229 230 /* Refill if RX buffers queue becomes low */ 231 if (free_desc_count >= mhi_netdev->rx_queue_sz / 2) 232 schedule_delayed_work(&mhi_netdev->rx_refill, 0); 233 } 234 235 static void mhi_net_ul_callback(struct mhi_device *mhi_dev, 236 struct mhi_result *mhi_res) 237 { 238 struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev); 239 struct net_device *ndev = mhi_netdev->ndev; 240 struct mhi_device *mdev = mhi_netdev->mdev; 241 struct sk_buff *skb = mhi_res->buf_addr; 242 243 /* Hardware has consumed the buffer, so free the skb (which is not 244 * freed by the MHI stack) and perform accounting. 245 */ 246 dev_consume_skb_any(skb); 247 248 u64_stats_update_begin(&mhi_netdev->stats.tx_syncp); 249 if (unlikely(mhi_res->transaction_status)) { 250 /* MHI layer stopping/resetting the UL channel */ 251 if (mhi_res->transaction_status == -ENOTCONN) { 252 u64_stats_update_end(&mhi_netdev->stats.tx_syncp); 253 return; 254 } 255 256 u64_stats_inc(&mhi_netdev->stats.tx_errors); 257 } else { 258 u64_stats_inc(&mhi_netdev->stats.tx_packets); 259 u64_stats_add(&mhi_netdev->stats.tx_bytes, mhi_res->bytes_xferd); 260 } 261 u64_stats_update_end(&mhi_netdev->stats.tx_syncp); 262 263 if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mdev, DMA_TO_DEVICE)) 264 netif_wake_queue(ndev); 265 } 266 267 static void mhi_net_rx_refill_work(struct work_struct *work) 268 { 269 struct mhi_net_dev *mhi_netdev = container_of(work, struct mhi_net_dev, 270 rx_refill.work); 271 struct net_device *ndev = mhi_netdev->ndev; 272 struct mhi_device *mdev = mhi_netdev->mdev; 273 struct sk_buff *skb; 274 unsigned int size; 275 int err; 276 277 size = mhi_netdev->mru ? mhi_netdev->mru : READ_ONCE(ndev->mtu); 278 279 while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) { 280 skb = netdev_alloc_skb(ndev, size); 281 if (unlikely(!skb)) 282 break; 283 284 err = mhi_queue_skb(mdev, DMA_FROM_DEVICE, skb, size, MHI_EOT); 285 if (unlikely(err)) { 286 net_err_ratelimited("%s: Failed to queue RX buf (%d)\n", 287 ndev->name, err); 288 kfree_skb(skb); 289 break; 290 } 291 292 /* Do not hog the CPU if rx buffers are consumed faster than 293 * queued (unlikely). 294 */ 295 cond_resched(); 296 } 297 298 /* If we're still starved of rx buffers, reschedule later */ 299 if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mhi_netdev->rx_queue_sz) 300 schedule_delayed_work(&mhi_netdev->rx_refill, HZ / 2); 301 } 302 303 static int mhi_net_newlink(struct mhi_device *mhi_dev, struct net_device *ndev) 304 { 305 struct mhi_net_dev *mhi_netdev; 306 int err; 307 308 mhi_netdev = netdev_priv(ndev); 309 310 dev_set_drvdata(&mhi_dev->dev, mhi_netdev); 311 mhi_netdev->ndev = ndev; 312 mhi_netdev->mdev = mhi_dev; 313 mhi_netdev->skbagg_head = NULL; 314 mhi_netdev->mru = mhi_dev->mhi_cntrl->mru; 315 316 INIT_DELAYED_WORK(&mhi_netdev->rx_refill, mhi_net_rx_refill_work); 317 u64_stats_init(&mhi_netdev->stats.rx_syncp); 318 u64_stats_init(&mhi_netdev->stats.tx_syncp); 319 320 /* Start MHI channels */ 321 err = mhi_prepare_for_transfer(mhi_dev); 322 if (err) 323 return err; 324 325 /* Number of transfer descriptors determines size of the queue */ 326 mhi_netdev->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE); 327 328 err = register_netdev(ndev); 329 if (err) 330 return err; 331 332 return 0; 333 } 334 335 static void mhi_net_dellink(struct mhi_device *mhi_dev, struct net_device *ndev) 336 { 337 struct mhi_net_dev *mhi_netdev = netdev_priv(ndev); 338 339 unregister_netdev(ndev); 340 341 mhi_unprepare_from_transfer(mhi_dev); 342 343 kfree_skb(mhi_netdev->skbagg_head); 344 345 free_netdev(ndev); 346 347 dev_set_drvdata(&mhi_dev->dev, NULL); 348 } 349 350 static int mhi_net_probe(struct mhi_device *mhi_dev, 351 const struct mhi_device_id *id) 352 { 353 const struct mhi_device_info *info = (struct mhi_device_info *)id->driver_data; 354 struct net_device *ndev; 355 int err; 356 357 ndev = alloc_netdev(sizeof(struct mhi_net_dev), info->netname, 358 NET_NAME_PREDICTABLE, mhi_net_setup); 359 if (!ndev) 360 return -ENOMEM; 361 362 SET_NETDEV_DEV(ndev, &mhi_dev->dev); 363 364 err = mhi_net_newlink(mhi_dev, ndev); 365 if (err) { 366 free_netdev(ndev); 367 return err; 368 } 369 370 return 0; 371 } 372 373 static void mhi_net_remove(struct mhi_device *mhi_dev) 374 { 375 struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev); 376 377 mhi_net_dellink(mhi_dev, mhi_netdev->ndev); 378 } 379 380 static const struct mhi_device_info mhi_hwip0 = { 381 .netname = "mhi_hwip%d", 382 }; 383 384 static const struct mhi_device_info mhi_swip0 = { 385 .netname = "mhi_swip%d", 386 }; 387 388 static const struct mhi_device_id mhi_net_id_table[] = { 389 /* Hardware accelerated data PATH (to modem IPA), protocol agnostic */ 390 { .chan = "IP_HW0", .driver_data = (kernel_ulong_t)&mhi_hwip0 }, 391 /* Software data PATH (to modem CPU) */ 392 { .chan = "IP_SW0", .driver_data = (kernel_ulong_t)&mhi_swip0 }, 393 {} 394 }; 395 MODULE_DEVICE_TABLE(mhi, mhi_net_id_table); 396 397 static struct mhi_driver mhi_net_driver = { 398 .probe = mhi_net_probe, 399 .remove = mhi_net_remove, 400 .dl_xfer_cb = mhi_net_dl_callback, 401 .ul_xfer_cb = mhi_net_ul_callback, 402 .id_table = mhi_net_id_table, 403 .driver = { 404 .name = "mhi_net", 405 }, 406 }; 407 408 module_mhi_driver(mhi_net_driver); 409 410 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>"); 411 MODULE_DESCRIPTION("Network over MHI"); 412 MODULE_LICENSE("GPL v2"); 413