xref: /linux/net/qrtr/mhi.c (revision 41f7adb676f6c4aef439d78b15c7e1119216bc2b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/mhi.h>
7 #include <linux/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <net/sock.h>
11 
12 #include "qrtr.h"
13 
14 struct qrtr_mhi_dev {
15 	struct qrtr_endpoint ep;
16 	struct mhi_device *mhi_dev;
17 	struct device *dev;
18 };
19 
20 /* From MHI to QRTR */
21 static void qcom_mhi_qrtr_dl_callback(struct mhi_device *mhi_dev,
22 				      struct mhi_result *mhi_res)
23 {
24 	struct qrtr_mhi_dev *qdev = dev_get_drvdata(&mhi_dev->dev);
25 	int rc;
26 
27 	if (!qdev || mhi_res->transaction_status)
28 		return;
29 
30 	rc = qrtr_endpoint_post(&qdev->ep, mhi_res->buf_addr,
31 				mhi_res->bytes_xferd);
32 	if (rc == -EINVAL)
33 		dev_err(qdev->dev, "invalid ipcrouter packet\n");
34 }
35 
36 /* From QRTR to MHI */
37 static void qcom_mhi_qrtr_ul_callback(struct mhi_device *mhi_dev,
38 				      struct mhi_result *mhi_res)
39 {
40 	struct sk_buff *skb = mhi_res->buf_addr;
41 
42 	if (skb->sk)
43 		sock_put(skb->sk);
44 	consume_skb(skb);
45 }
46 
47 /* Send data over MHI */
48 static int qcom_mhi_qrtr_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
49 {
50 	struct qrtr_mhi_dev *qdev = container_of(ep, struct qrtr_mhi_dev, ep);
51 	int rc;
52 
53 	if (skb->sk)
54 		sock_hold(skb->sk);
55 
56 	rc = skb_linearize(skb);
57 	if (rc)
58 		goto free_skb;
59 
60 	rc = mhi_queue_skb(qdev->mhi_dev, DMA_TO_DEVICE, skb, skb->len,
61 			   MHI_EOT);
62 	if (rc)
63 		goto free_skb;
64 
65 	return rc;
66 
67 free_skb:
68 	if (skb->sk)
69 		sock_put(skb->sk);
70 	kfree_skb(skb);
71 
72 	return rc;
73 }
74 
75 static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,
76 			       const struct mhi_device_id *id)
77 {
78 	struct qrtr_mhi_dev *qdev;
79 	int rc;
80 
81 	qdev = devm_kzalloc(&mhi_dev->dev, sizeof(*qdev), GFP_KERNEL);
82 	if (!qdev)
83 		return -ENOMEM;
84 
85 	qdev->mhi_dev = mhi_dev;
86 	qdev->dev = &mhi_dev->dev;
87 	qdev->ep.xmit = qcom_mhi_qrtr_send;
88 
89 	dev_set_drvdata(&mhi_dev->dev, qdev);
90 	rc = qrtr_endpoint_register(&qdev->ep, QRTR_EP_NID_AUTO);
91 	if (rc)
92 		return rc;
93 
94 	/* start channels */
95 	rc = mhi_prepare_for_transfer_autoqueue(mhi_dev);
96 	if (rc) {
97 		qrtr_endpoint_unregister(&qdev->ep);
98 		return rc;
99 	}
100 
101 	dev_dbg(qdev->dev, "Qualcomm MHI QRTR driver probed\n");
102 
103 	return 0;
104 }
105 
106 static void qcom_mhi_qrtr_remove(struct mhi_device *mhi_dev)
107 {
108 	struct qrtr_mhi_dev *qdev = dev_get_drvdata(&mhi_dev->dev);
109 
110 	qrtr_endpoint_unregister(&qdev->ep);
111 	mhi_unprepare_from_transfer(mhi_dev);
112 	dev_set_drvdata(&mhi_dev->dev, NULL);
113 }
114 
115 static const struct mhi_device_id qcom_mhi_qrtr_id_table[] = {
116 	{ .chan = "IPCR" },
117 	{}
118 };
119 MODULE_DEVICE_TABLE(mhi, qcom_mhi_qrtr_id_table);
120 
121 static int __maybe_unused qcom_mhi_qrtr_pm_suspend_late(struct device *dev)
122 {
123 	struct mhi_device *mhi_dev = container_of(dev, struct mhi_device, dev);
124 	enum mhi_state state;
125 
126 	state = mhi_get_mhi_state(mhi_dev->mhi_cntrl);
127 	/*
128 	 * If the device is in suspend state, then no need for the
129 	 * client driver to unprepare the channels.
130 	 */
131 	if (state == MHI_STATE_M3)
132 		return 0;
133 
134 	mhi_unprepare_from_transfer(mhi_dev);
135 
136 	return 0;
137 }
138 
139 static int __maybe_unused qcom_mhi_qrtr_pm_resume_early(struct device *dev)
140 {
141 	struct mhi_device *mhi_dev = container_of(dev, struct mhi_device, dev);
142 	enum mhi_state state;
143 	int rc;
144 
145 	state = mhi_get_mhi_state(mhi_dev->mhi_cntrl);
146 	/*
147 	 * If the device is in suspend state, we won't unprepare channels
148 	 * in suspend callback, therefore no need to prepare channels when
149 	 * resume.
150 	 */
151 	if (state == MHI_STATE_M3)
152 		return 0;
153 
154 	rc = mhi_prepare_for_transfer_autoqueue(mhi_dev);
155 	if (rc)
156 		dev_err(dev, "failed to prepare for autoqueue transfer %d\n", rc);
157 
158 	return rc;
159 }
160 
161 static const struct dev_pm_ops qcom_mhi_qrtr_pm_ops = {
162 	SET_LATE_SYSTEM_SLEEP_PM_OPS(qcom_mhi_qrtr_pm_suspend_late,
163 				     qcom_mhi_qrtr_pm_resume_early)
164 };
165 
166 static struct mhi_driver qcom_mhi_qrtr_driver = {
167 	.probe = qcom_mhi_qrtr_probe,
168 	.remove = qcom_mhi_qrtr_remove,
169 	.dl_xfer_cb = qcom_mhi_qrtr_dl_callback,
170 	.ul_xfer_cb = qcom_mhi_qrtr_ul_callback,
171 	.id_table = qcom_mhi_qrtr_id_table,
172 	.driver = {
173 		.name = "qcom_mhi_qrtr",
174 		.pm = &qcom_mhi_qrtr_pm_ops,
175 	},
176 };
177 
178 module_mhi_driver(qcom_mhi_qrtr_driver);
179 
180 MODULE_AUTHOR("Chris Lew <clew@codeaurora.org>");
181 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
182 MODULE_DESCRIPTION("Qualcomm IPC-Router MHI interface driver");
183 MODULE_LICENSE("GPL v2");
184