ipa_modem.c 9.1 KB
Newer Older
1 2 3
// SPDX-License-Identifier: GPL-2.0

/* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4
 * Copyright (C) 2018-2021 Linaro Ltd.
5 6 7 8 9 10 11
 */

#include <linux/errno.h>
#include <linux/if_arp.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/if_rmnet.h>
12
#include <linux/remoteproc/qcom_rproc.h>
13 14 15 16 17 18 19 20 21

#include "ipa.h"
#include "ipa_data.h"
#include "ipa_endpoint.h"
#include "ipa_table.h"
#include "ipa_mem.h"
#include "ipa_modem.h"
#include "ipa_smp2p.h"
#include "ipa_qmi.h"
A
Alex Elder 已提交
22
#include "ipa_uc.h"
23
#include "ipa_clock.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#define IPA_NETDEV_NAME		"rmnet_ipa%d"
#define IPA_NETDEV_TAILROOM	0	/* for padding by mux layer */
#define IPA_NETDEV_TIMEOUT	10	/* seconds */

enum ipa_modem_state {
	IPA_MODEM_STATE_STOPPED	= 0,
	IPA_MODEM_STATE_STARTING,
	IPA_MODEM_STATE_RUNNING,
	IPA_MODEM_STATE_STOPPING,
};

/** struct ipa_priv - IPA network device private data */
struct ipa_priv {
	struct ipa *ipa;
};

/** ipa_open() - Opens the modem network interface */
static int ipa_open(struct net_device *netdev)
{
	struct ipa_priv *priv = netdev_priv(netdev);
	struct ipa *ipa = priv->ipa;
	int ret;

48 49
	ipa_clock_get(ipa);

50 51
	ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
	if (ret)
52 53
		goto err_clock_put;

54 55 56 57 58 59 60 61 62 63
	ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
	if (ret)
		goto err_disable_tx;

	netif_start_queue(netdev);

	return 0;

err_disable_tx:
	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
64 65
err_clock_put:
	ipa_clock_put(ipa);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

	return ret;
}

/** ipa_stop() - Stops the modem network interface. */
static int ipa_stop(struct net_device *netdev)
{
	struct ipa_priv *priv = netdev_priv(netdev);
	struct ipa *ipa = priv->ipa;

	netif_stop_queue(netdev);

	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);

81 82
	ipa_clock_put(ipa);

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	return 0;
}

/** ipa_start_xmit() - Transmits an skb.
 * @skb: skb to be transmitted
 * @dev: network device
 *
 * Return codes:
 * NETDEV_TX_OK: Success
 * NETDEV_TX_BUSY: Error while transmitting the skb. Try again later
 */
static int ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev)
{
	struct net_device_stats *stats = &netdev->stats;
	struct ipa_priv *priv = netdev_priv(netdev);
	struct ipa_endpoint *endpoint;
	struct ipa *ipa = priv->ipa;
	u32 skb_len = skb->len;
	int ret;

	if (!skb_len)
		goto err_drop_skb;

	endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
	if (endpoint->data->qmap && skb->protocol != htons(ETH_P_MAP))
		goto err_drop_skb;

	ret = ipa_endpoint_skb_tx(endpoint, skb);
	if (ret) {
		if (ret != -E2BIG)
			return NETDEV_TX_BUSY;
		goto err_drop_skb;
	}

	stats->tx_packets++;
	stats->tx_bytes += skb_len;

	return NETDEV_TX_OK;

err_drop_skb:
	dev_kfree_skb_any(skb);
	stats->tx_dropped++;

	return NETDEV_TX_OK;
}

void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb)
{
	struct net_device_stats *stats = &netdev->stats;

	if (skb) {
		skb->dev = netdev;
		skb->protocol = htons(ETH_P_MAP);
		stats->rx_packets++;
		stats->rx_bytes += skb->len;

		(void)netif_receive_skb(skb);
	} else {
		stats->rx_dropped++;
	}
}

static const struct net_device_ops ipa_modem_ops = {
	.ndo_open	= ipa_open,
	.ndo_stop	= ipa_stop,
	.ndo_start_xmit	= ipa_start_xmit,
};

/** ipa_modem_netdev_setup() - netdev setup function for the modem */
static void ipa_modem_netdev_setup(struct net_device *netdev)
{
	netdev->netdev_ops = &ipa_modem_ops;
	ether_setup(netdev);
	/* No header ops (override value set by ether_setup()) */
	netdev->header_ops = NULL;
	netdev->type = ARPHRD_RAWIP;
	netdev->hard_header_len = 0;
	netdev->max_mtu = IPA_MTU;
	netdev->mtu = netdev->max_mtu;
	netdev->addr_len = 0;
	netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
	/* The endpoint is configured for QMAP */
	netdev->needed_headroom = sizeof(struct rmnet_map_header);
	netdev->needed_tailroom = IPA_NETDEV_TAILROOM;
	netdev->watchdog_timeo = IPA_NETDEV_TIMEOUT * HZ;
	netdev->hw_features = NETIF_F_SG;
}

/** ipa_modem_suspend() - suspend callback
 * @netdev:	Network device
 *
 * Suspend the modem's endpoints.
 */
void ipa_modem_suspend(struct net_device *netdev)
{
	struct ipa_priv *priv = netdev_priv(netdev);
	struct ipa *ipa = priv->ipa;

	netif_stop_queue(netdev);

	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
}

/** ipa_modem_resume() - resume callback for runtime_pm
 * @dev: pointer to device
 *
 * Resume the modem's endpoints.
 */
void ipa_modem_resume(struct net_device *netdev)
{
	struct ipa_priv *priv = netdev_priv(netdev);
	struct ipa *ipa = priv->ipa;

	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);

	netif_wake_queue(netdev);
}

int ipa_modem_start(struct ipa *ipa)
{
	enum ipa_modem_state state;
	struct net_device *netdev;
	struct ipa_priv *priv;
	int ret;

	/* Only attempt to start the modem if it's stopped */
	state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_STOPPED,
			       IPA_MODEM_STATE_STARTING);

	/* Silently ignore attempts when running, or when changing state */
	if (state != IPA_MODEM_STATE_STOPPED)
		return 0;

	netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME,
			      NET_NAME_UNKNOWN, ipa_modem_netdev_setup);
	if (!netdev) {
		ret = -ENOMEM;
		goto out_set_state;
	}

225
	SET_NETDEV_DEV(netdev, &ipa->pdev->dev);
226 227 228 229
	priv = netdev_priv(netdev);
	priv->ipa = ipa;

	ret = register_netdev(netdev);
230
	if (!ret) {
231
		ipa->modem_netdev = netdev;
232 233 234 235 236
		ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = netdev;
		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = netdev;
	} else {
		free_netdev(netdev);
	}
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

out_set_state:
	if (ret)
		atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
	else
		atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING);
	smp_mb__after_atomic();

	return ret;
}

int ipa_modem_stop(struct ipa *ipa)
{
	struct net_device *netdev = ipa->modem_netdev;
	enum ipa_modem_state state;

	/* Only attempt to stop the modem if it's running */
	state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_RUNNING,
			       IPA_MODEM_STATE_STOPPING);

	/* Silently ignore attempts when already stopped */
	if (state == IPA_MODEM_STATE_STOPPED)
		return 0;

	/* If we're somewhere between stopped and starting, we're busy */
	if (state != IPA_MODEM_STATE_RUNNING)
		return -EBUSY;

	/* Prevent the modem from triggering a call to ipa_setup() */
	ipa_smp2p_disable(ipa);

268
	/* Stop the queue and disable the endpoints if it's open */
269
	if (netdev) {
270
		(void)ipa_stop(netdev);
271 272
		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL;
		ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL;
273 274 275 276 277
		ipa->modem_netdev = NULL;
		unregister_netdev(netdev);
		free_netdev(netdev);
	}

278
	atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
279 280
	smp_mb__after_atomic();

281
	return 0;
282 283 284 285 286 287 288 289
}

/* Treat a "clean" modem stop the same as a crash */
static void ipa_modem_crashed(struct ipa *ipa)
{
	struct device *dev = &ipa->pdev->dev;
	int ret;

290 291
	ipa_clock_get(ipa);

292 293 294 295 296 297 298 299
	ipa_endpoint_modem_pause_all(ipa, true);

	ipa_endpoint_modem_hol_block_clear_all(ipa);

	ipa_table_reset(ipa, true);

	ret = ipa_table_hash_flush(ipa);
	if (ret)
300
		dev_err(dev, "error %d flushing hash caches\n", ret);
301 302 303

	ret = ipa_endpoint_modem_exception_reset_all(ipa);
	if (ret)
304
		dev_err(dev, "error %d resetting exception endpoint\n", ret);
305 306 307 308 309

	ipa_endpoint_modem_pause_all(ipa, false);

	ret = ipa_modem_stop(ipa);
	if (ret)
310
		dev_err(dev, "error %d stopping modem\n", ret);
311 312 313 314 315

	/* Now prepare for the next modem boot */
	ret = ipa_mem_zero_modem(ipa);
	if (ret)
		dev_err(dev, "error %d zeroing modem memory regions\n", ret);
316 317

	ipa_clock_put(ipa);
318 319
}

320 321
static int ipa_modem_notify(struct notifier_block *nb, unsigned long action,
			    void *data)
322
{
323 324 325
	struct ipa *ipa = container_of(nb, struct ipa, nb);
	struct qcom_ssr_notify_data *notify_data = data;
	struct device *dev = &ipa->pdev->dev;
326

327 328
	switch (action) {
	case QCOM_SSR_BEFORE_POWERUP:
329
		dev_info(dev, "received modem starting event\n");
A
Alex Elder 已提交
330
		ipa_uc_clock(ipa);
331 332 333
		ipa_smp2p_notify_reset(ipa);
		break;

334
	case QCOM_SSR_AFTER_POWERUP:
335 336 337
		dev_info(dev, "received modem running event\n");
		break;

338
	case QCOM_SSR_BEFORE_SHUTDOWN:
339
		dev_info(dev, "received modem %s event\n",
340
			 notify_data->crashed ? "crashed" : "stopping");
341 342 343 344
		if (ipa->setup_complete)
			ipa_modem_crashed(ipa);
		break;

345
	case QCOM_SSR_AFTER_SHUTDOWN:
346 347 348 349
		dev_info(dev, "received modem offline event\n");
		break;

	default:
350
		dev_err(dev, "received unrecognized event %lu\n", action);
351 352
		break;
	}
353 354

	return NOTIFY_OK;
355 356 357 358 359 360 361 362 363 364 365 366 367 368
}

int ipa_modem_init(struct ipa *ipa, bool modem_init)
{
	return ipa_smp2p_init(ipa, modem_init);
}

void ipa_modem_exit(struct ipa *ipa)
{
	ipa_smp2p_exit(ipa);
}

int ipa_modem_config(struct ipa *ipa)
{
369 370 371 372 373 374 375 376 377 378 379
	void *notifier;

	ipa->nb.notifier_call = ipa_modem_notify;

	notifier = qcom_register_ssr_notifier("mpss", &ipa->nb);
	if (IS_ERR(notifier))
		return PTR_ERR(notifier);

	ipa->notifier = notifier;

	return 0;
380 381 382 383
}

void ipa_modem_deconfig(struct ipa *ipa)
{
384 385 386 387 388 389 390 391 392
	struct device *dev = &ipa->pdev->dev;
	int ret;

	ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb);
	if (ret)
		dev_err(dev, "error %d unregistering notifier", ret);

	ipa->notifier = NULL;
	memset(&ipa->nb, 0, sizeof(ipa->nb));
393
}