1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2018-2021 Linaro Ltd. 5 */ 6 7 #include <linux/errno.h> 8 #include <linux/if_arp.h> 9 #include <linux/netdevice.h> 10 #include <linux/skbuff.h> 11 #include <linux/if_rmnet.h> 12 #include <linux/remoteproc/qcom_rproc.h> 13 14 #include "ipa.h" 15 #include "ipa_data.h" 16 #include "ipa_endpoint.h" 17 #include "ipa_table.h" 18 #include "ipa_mem.h" 19 #include "ipa_modem.h" 20 #include "ipa_smp2p.h" 21 #include "ipa_qmi.h" 22 #include "ipa_uc.h" 23 #include "ipa_clock.h" 24 25 #define IPA_NETDEV_NAME "rmnet_ipa%d" 26 #define IPA_NETDEV_TAILROOM 0 /* for padding by mux layer */ 27 #define IPA_NETDEV_TIMEOUT 10 /* seconds */ 28 29 enum ipa_modem_state { 30 IPA_MODEM_STATE_STOPPED = 0, 31 IPA_MODEM_STATE_STARTING, 32 IPA_MODEM_STATE_RUNNING, 33 IPA_MODEM_STATE_STOPPING, 34 }; 35 36 /** struct ipa_priv - IPA network device private data */ 37 struct ipa_priv { 38 struct ipa *ipa; 39 }; 40 41 /** ipa_open() - Opens the modem network interface */ 42 static int ipa_open(struct net_device *netdev) 43 { 44 struct ipa_priv *priv = netdev_priv(netdev); 45 struct ipa *ipa = priv->ipa; 46 int ret; 47 48 ret = ipa_clock_get(ipa); 49 if (WARN_ON(ret < 0)) 50 goto err_clock_put; 51 52 ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]); 53 if (ret) 54 goto err_clock_put; 55 56 ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]); 57 if (ret) 58 goto err_disable_tx; 59 60 netif_start_queue(netdev); 61 62 return 0; 63 64 err_disable_tx: 65 ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]); 66 err_clock_put: 67 (void)ipa_clock_put(ipa); 68 69 return ret; 70 } 71 72 /** ipa_stop() - Stops the modem network interface. */ 73 static int ipa_stop(struct net_device *netdev) 74 { 75 struct ipa_priv *priv = netdev_priv(netdev); 76 struct ipa *ipa = priv->ipa; 77 78 netif_stop_queue(netdev); 79 80 ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]); 81 ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]); 82 83 (void)ipa_clock_put(ipa); 84 85 return 0; 86 } 87 88 /** ipa_start_xmit() - Transmits an skb. 89 * @skb: skb to be transmitted 90 * @dev: network device 91 * 92 * Return codes: 93 * NETDEV_TX_OK: Success 94 * NETDEV_TX_BUSY: Error while transmitting the skb. Try again later 95 */ 96 static int ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev) 97 { 98 struct net_device_stats *stats = &netdev->stats; 99 struct ipa_priv *priv = netdev_priv(netdev); 100 struct ipa_endpoint *endpoint; 101 struct ipa *ipa = priv->ipa; 102 u32 skb_len = skb->len; 103 int ret; 104 105 if (!skb_len) 106 goto err_drop_skb; 107 108 endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]; 109 if (endpoint->data->qmap && skb->protocol != htons(ETH_P_MAP)) 110 goto err_drop_skb; 111 112 ret = ipa_endpoint_skb_tx(endpoint, skb); 113 if (ret) { 114 if (ret != -E2BIG) 115 return NETDEV_TX_BUSY; 116 goto err_drop_skb; 117 } 118 119 stats->tx_packets++; 120 stats->tx_bytes += skb_len; 121 122 return NETDEV_TX_OK; 123 124 err_drop_skb: 125 dev_kfree_skb_any(skb); 126 stats->tx_dropped++; 127 128 return NETDEV_TX_OK; 129 } 130 131 void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb) 132 { 133 struct net_device_stats *stats = &netdev->stats; 134 135 if (skb) { 136 skb->dev = netdev; 137 skb->protocol = htons(ETH_P_MAP); 138 stats->rx_packets++; 139 stats->rx_bytes += skb->len; 140 141 (void)netif_receive_skb(skb); 142 } else { 143 stats->rx_dropped++; 144 } 145 } 146 147 static const struct net_device_ops ipa_modem_ops = { 148 .ndo_open = ipa_open, 149 .ndo_stop = ipa_stop, 150 .ndo_start_xmit = ipa_start_xmit, 151 }; 152 153 /** ipa_modem_netdev_setup() - netdev setup function for the modem */ 154 static void ipa_modem_netdev_setup(struct net_device *netdev) 155 { 156 netdev->netdev_ops = &ipa_modem_ops; 157 ether_setup(netdev); 158 /* No header ops (override value set by ether_setup()) */ 159 netdev->header_ops = NULL; 160 netdev->type = ARPHRD_RAWIP; 161 netdev->hard_header_len = 0; 162 netdev->max_mtu = IPA_MTU; 163 netdev->mtu = netdev->max_mtu; 164 netdev->addr_len = 0; 165 netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST); 166 /* The endpoint is configured for QMAP */ 167 netdev->needed_headroom = sizeof(struct rmnet_map_header); 168 netdev->needed_tailroom = IPA_NETDEV_TAILROOM; 169 netdev->watchdog_timeo = IPA_NETDEV_TIMEOUT * HZ; 170 netdev->hw_features = NETIF_F_SG; 171 } 172 173 /** ipa_modem_suspend() - suspend callback 174 * @netdev: Network device 175 * 176 * Suspend the modem's endpoints. 177 */ 178 void ipa_modem_suspend(struct net_device *netdev) 179 { 180 struct ipa_priv *priv = netdev_priv(netdev); 181 struct ipa *ipa = priv->ipa; 182 183 if (!(netdev->flags & IFF_UP)) 184 return; 185 186 netif_stop_queue(netdev); 187 188 ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]); 189 ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]); 190 } 191 192 /** ipa_modem_resume() - resume callback for runtime_pm 193 * @dev: pointer to device 194 * 195 * Resume the modem's endpoints. 196 */ 197 void ipa_modem_resume(struct net_device *netdev) 198 { 199 struct ipa_priv *priv = netdev_priv(netdev); 200 struct ipa *ipa = priv->ipa; 201 202 if (!(netdev->flags & IFF_UP)) 203 return; 204 205 ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]); 206 ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]); 207 208 netif_wake_queue(netdev); 209 } 210 211 int ipa_modem_start(struct ipa *ipa) 212 { 213 enum ipa_modem_state state; 214 struct net_device *netdev; 215 struct ipa_priv *priv; 216 int ret; 217 218 /* Only attempt to start the modem if it's stopped */ 219 state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_STOPPED, 220 IPA_MODEM_STATE_STARTING); 221 222 /* Silently ignore attempts when running, or when changing state */ 223 if (state != IPA_MODEM_STATE_STOPPED) 224 return 0; 225 226 netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME, 227 NET_NAME_UNKNOWN, ipa_modem_netdev_setup); 228 if (!netdev) { 229 ret = -ENOMEM; 230 goto out_set_state; 231 } 232 233 SET_NETDEV_DEV(netdev, &ipa->pdev->dev); 234 priv = netdev_priv(netdev); 235 priv->ipa = ipa; 236 ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = netdev; 237 ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = netdev; 238 ipa->modem_netdev = netdev; 239 240 ret = register_netdev(netdev); 241 if (ret) { 242 ipa->modem_netdev = NULL; 243 ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL; 244 ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL; 245 free_netdev(netdev); 246 } 247 248 out_set_state: 249 if (ret) 250 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED); 251 else 252 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING); 253 smp_mb__after_atomic(); 254 255 return ret; 256 } 257 258 int ipa_modem_stop(struct ipa *ipa) 259 { 260 struct net_device *netdev = ipa->modem_netdev; 261 enum ipa_modem_state state; 262 263 /* Only attempt to stop the modem if it's running */ 264 state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_RUNNING, 265 IPA_MODEM_STATE_STOPPING); 266 267 /* Silently ignore attempts when already stopped */ 268 if (state == IPA_MODEM_STATE_STOPPED) 269 return 0; 270 271 /* If we're somewhere between stopped and starting, we're busy */ 272 if (state != IPA_MODEM_STATE_RUNNING) 273 return -EBUSY; 274 275 /* Prevent the modem from triggering a call to ipa_setup() */ 276 ipa_smp2p_disable(ipa); 277 278 /* Clean up the netdev and endpoints if it was started */ 279 if (netdev) { 280 /* If it was opened, stop it first */ 281 if (netdev->flags & IFF_UP) 282 (void)ipa_stop(netdev); 283 unregister_netdev(netdev); 284 ipa->modem_netdev = NULL; 285 ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL; 286 ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL; 287 free_netdev(netdev); 288 } 289 290 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED); 291 smp_mb__after_atomic(); 292 293 return 0; 294 } 295 296 /* Treat a "clean" modem stop the same as a crash */ 297 static void ipa_modem_crashed(struct ipa *ipa) 298 { 299 struct device *dev = &ipa->pdev->dev; 300 int ret; 301 302 ret = ipa_clock_get(ipa); 303 if (WARN_ON(ret < 0)) 304 goto out_clock_put; 305 306 ipa_endpoint_modem_pause_all(ipa, true); 307 308 ipa_endpoint_modem_hol_block_clear_all(ipa); 309 310 ipa_table_reset(ipa, true); 311 312 ret = ipa_table_hash_flush(ipa); 313 if (ret) 314 dev_err(dev, "error %d flushing hash caches\n", ret); 315 316 ret = ipa_endpoint_modem_exception_reset_all(ipa); 317 if (ret) 318 dev_err(dev, "error %d resetting exception endpoint\n", ret); 319 320 ipa_endpoint_modem_pause_all(ipa, false); 321 322 ret = ipa_modem_stop(ipa); 323 if (ret) 324 dev_err(dev, "error %d stopping modem\n", ret); 325 326 /* Now prepare for the next modem boot */ 327 ret = ipa_mem_zero_modem(ipa); 328 if (ret) 329 dev_err(dev, "error %d zeroing modem memory regions\n", ret); 330 331 out_clock_put: 332 (void)ipa_clock_put(ipa); 333 } 334 335 static int ipa_modem_notify(struct notifier_block *nb, unsigned long action, 336 void *data) 337 { 338 struct ipa *ipa = container_of(nb, struct ipa, nb); 339 struct qcom_ssr_notify_data *notify_data = data; 340 struct device *dev = &ipa->pdev->dev; 341 342 switch (action) { 343 case QCOM_SSR_BEFORE_POWERUP: 344 dev_info(dev, "received modem starting event\n"); 345 ipa_uc_clock(ipa); 346 ipa_smp2p_notify_reset(ipa); 347 break; 348 349 case QCOM_SSR_AFTER_POWERUP: 350 dev_info(dev, "received modem running event\n"); 351 break; 352 353 case QCOM_SSR_BEFORE_SHUTDOWN: 354 dev_info(dev, "received modem %s event\n", 355 notify_data->crashed ? "crashed" : "stopping"); 356 if (ipa->setup_complete) 357 ipa_modem_crashed(ipa); 358 break; 359 360 case QCOM_SSR_AFTER_SHUTDOWN: 361 dev_info(dev, "received modem offline event\n"); 362 break; 363 364 default: 365 dev_err(dev, "received unrecognized event %lu\n", action); 366 break; 367 } 368 369 return NOTIFY_OK; 370 } 371 372 int ipa_modem_init(struct ipa *ipa, bool modem_init) 373 { 374 return ipa_smp2p_init(ipa, modem_init); 375 } 376 377 void ipa_modem_exit(struct ipa *ipa) 378 { 379 ipa_smp2p_exit(ipa); 380 } 381 382 int ipa_modem_config(struct ipa *ipa) 383 { 384 void *notifier; 385 386 ipa->nb.notifier_call = ipa_modem_notify; 387 388 notifier = qcom_register_ssr_notifier("mpss", &ipa->nb); 389 if (IS_ERR(notifier)) 390 return PTR_ERR(notifier); 391 392 ipa->notifier = notifier; 393 394 return 0; 395 } 396 397 void ipa_modem_deconfig(struct ipa *ipa) 398 { 399 struct device *dev = &ipa->pdev->dev; 400 int ret; 401 402 ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb); 403 if (ret) 404 dev_err(dev, "error %d unregistering notifier", ret); 405 406 ipa->notifier = NULL; 407 memset(&ipa->nb, 0, sizeof(ipa->nb)); 408 } 409