xref: /openbmc/linux/drivers/soc/qcom/qmi_interface.c (revision ae0be8de)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2017 Linaro Ltd.
4  */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/device.h>
8 #include <linux/qrtr.h>
9 #include <linux/net.h>
10 #include <linux/completion.h>
11 #include <linux/idr.h>
12 #include <linux/string.h>
13 #include <net/sock.h>
14 #include <linux/workqueue.h>
15 #include <linux/soc/qcom/qmi.h>
16 
17 static struct socket *qmi_sock_create(struct qmi_handle *qmi,
18 				      struct sockaddr_qrtr *sq);
19 
20 /**
21  * qmi_recv_new_server() - handler of NEW_SERVER control message
22  * @qmi:	qmi handle
23  * @service:	service id of the new server
24  * @instance:	instance id of the new server
25  * @node:	node of the new server
26  * @port:	port of the new server
27  *
28  * Calls the new_server callback to inform the client about a newly registered
29  * server matching the currently registered service lookup.
30  */
31 static void qmi_recv_new_server(struct qmi_handle *qmi,
32 				unsigned int service, unsigned int instance,
33 				unsigned int node, unsigned int port)
34 {
35 	struct qmi_ops *ops = &qmi->ops;
36 	struct qmi_service *svc;
37 	int ret;
38 
39 	if (!ops->new_server)
40 		return;
41 
42 	/* Ignore EOF marker */
43 	if (!node && !port)
44 		return;
45 
46 	svc = kzalloc(sizeof(*svc), GFP_KERNEL);
47 	if (!svc)
48 		return;
49 
50 	svc->service = service;
51 	svc->version = instance & 0xff;
52 	svc->instance = instance >> 8;
53 	svc->node = node;
54 	svc->port = port;
55 
56 	ret = ops->new_server(qmi, svc);
57 	if (ret < 0)
58 		kfree(svc);
59 	else
60 		list_add(&svc->list_node, &qmi->lookup_results);
61 }
62 
63 /**
64  * qmi_recv_del_server() - handler of DEL_SERVER control message
65  * @qmi:	qmi handle
66  * @node:	node of the dying server, a value of -1 matches all nodes
67  * @port:	port of the dying server, a value of -1 matches all ports
68  *
69  * Calls the del_server callback for each previously seen server, allowing the
70  * client to react to the disappearing server.
71  */
72 static void qmi_recv_del_server(struct qmi_handle *qmi,
73 				unsigned int node, unsigned int port)
74 {
75 	struct qmi_ops *ops = &qmi->ops;
76 	struct qmi_service *svc;
77 	struct qmi_service *tmp;
78 
79 	list_for_each_entry_safe(svc, tmp, &qmi->lookup_results, list_node) {
80 		if (node != -1 && svc->node != node)
81 			continue;
82 		if (port != -1 && svc->port != port)
83 			continue;
84 
85 		if (ops->del_server)
86 			ops->del_server(qmi, svc);
87 
88 		list_del(&svc->list_node);
89 		kfree(svc);
90 	}
91 }
92 
93 /**
94  * qmi_recv_bye() - handler of BYE control message
95  * @qmi:	qmi handle
96  * @node:	id of the dying node
97  *
98  * Signals the client that all previously registered services on this node are
99  * now gone and then calls the bye callback to allow the client client further
100  * cleaning up resources associated with this remote.
101  */
102 static void qmi_recv_bye(struct qmi_handle *qmi,
103 			 unsigned int node)
104 {
105 	struct qmi_ops *ops = &qmi->ops;
106 
107 	qmi_recv_del_server(qmi, node, -1);
108 
109 	if (ops->bye)
110 		ops->bye(qmi, node);
111 }
112 
113 /**
114  * qmi_recv_del_client() - handler of DEL_CLIENT control message
115  * @qmi:	qmi handle
116  * @node:	node of the dying client
117  * @port:	port of the dying client
118  *
119  * Signals the client about a dying client, by calling the del_client callback.
120  */
121 static void qmi_recv_del_client(struct qmi_handle *qmi,
122 				unsigned int node, unsigned int port)
123 {
124 	struct qmi_ops *ops = &qmi->ops;
125 
126 	if (ops->del_client)
127 		ops->del_client(qmi, node, port);
128 }
129 
130 static void qmi_recv_ctrl_pkt(struct qmi_handle *qmi,
131 			      const void *buf, size_t len)
132 {
133 	const struct qrtr_ctrl_pkt *pkt = buf;
134 
135 	if (len < sizeof(struct qrtr_ctrl_pkt)) {
136 		pr_debug("ignoring short control packet\n");
137 		return;
138 	}
139 
140 	switch (le32_to_cpu(pkt->cmd)) {
141 	case QRTR_TYPE_BYE:
142 		qmi_recv_bye(qmi, le32_to_cpu(pkt->client.node));
143 		break;
144 	case QRTR_TYPE_NEW_SERVER:
145 		qmi_recv_new_server(qmi,
146 				    le32_to_cpu(pkt->server.service),
147 				    le32_to_cpu(pkt->server.instance),
148 				    le32_to_cpu(pkt->server.node),
149 				    le32_to_cpu(pkt->server.port));
150 		break;
151 	case QRTR_TYPE_DEL_SERVER:
152 		qmi_recv_del_server(qmi,
153 				    le32_to_cpu(pkt->server.node),
154 				    le32_to_cpu(pkt->server.port));
155 		break;
156 	case QRTR_TYPE_DEL_CLIENT:
157 		qmi_recv_del_client(qmi,
158 				    le32_to_cpu(pkt->client.node),
159 				    le32_to_cpu(pkt->client.port));
160 		break;
161 	}
162 }
163 
164 static void qmi_send_new_lookup(struct qmi_handle *qmi, struct qmi_service *svc)
165 {
166 	struct qrtr_ctrl_pkt pkt;
167 	struct sockaddr_qrtr sq;
168 	struct msghdr msg = { };
169 	struct kvec iv = { &pkt, sizeof(pkt) };
170 	int ret;
171 
172 	memset(&pkt, 0, sizeof(pkt));
173 	pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_LOOKUP);
174 	pkt.server.service = cpu_to_le32(svc->service);
175 	pkt.server.instance = cpu_to_le32(svc->version | svc->instance << 8);
176 
177 	sq.sq_family = qmi->sq.sq_family;
178 	sq.sq_node = qmi->sq.sq_node;
179 	sq.sq_port = QRTR_PORT_CTRL;
180 
181 	msg.msg_name = &sq;
182 	msg.msg_namelen = sizeof(sq);
183 
184 	mutex_lock(&qmi->sock_lock);
185 	if (qmi->sock) {
186 		ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt));
187 		if (ret < 0)
188 			pr_err("failed to send lookup registration: %d\n", ret);
189 	}
190 	mutex_unlock(&qmi->sock_lock);
191 }
192 
193 /**
194  * qmi_add_lookup() - register a new lookup with the name service
195  * @qmi:	qmi handle
196  * @service:	service id of the request
197  * @instance:	instance id of the request
198  * @version:	version number of the request
199  *
200  * Registering a lookup query with the name server will cause the name server
201  * to send NEW_SERVER and DEL_SERVER control messages to this socket as
202  * matching services are registered.
203  *
204  * Return: 0 on success, negative errno on failure.
205  */
206 int qmi_add_lookup(struct qmi_handle *qmi, unsigned int service,
207 		   unsigned int version, unsigned int instance)
208 {
209 	struct qmi_service *svc;
210 
211 	svc = kzalloc(sizeof(*svc), GFP_KERNEL);
212 	if (!svc)
213 		return -ENOMEM;
214 
215 	svc->service = service;
216 	svc->version = version;
217 	svc->instance = instance;
218 
219 	list_add(&svc->list_node, &qmi->lookups);
220 
221 	qmi_send_new_lookup(qmi, svc);
222 
223 	return 0;
224 }
225 EXPORT_SYMBOL(qmi_add_lookup);
226 
227 static void qmi_send_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
228 {
229 	struct qrtr_ctrl_pkt pkt;
230 	struct sockaddr_qrtr sq;
231 	struct msghdr msg = { };
232 	struct kvec iv = { &pkt, sizeof(pkt) };
233 	int ret;
234 
235 	memset(&pkt, 0, sizeof(pkt));
236 	pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_SERVER);
237 	pkt.server.service = cpu_to_le32(svc->service);
238 	pkt.server.instance = cpu_to_le32(svc->version | svc->instance << 8);
239 	pkt.server.node = cpu_to_le32(qmi->sq.sq_node);
240 	pkt.server.port = cpu_to_le32(qmi->sq.sq_port);
241 
242 	sq.sq_family = qmi->sq.sq_family;
243 	sq.sq_node = qmi->sq.sq_node;
244 	sq.sq_port = QRTR_PORT_CTRL;
245 
246 	msg.msg_name = &sq;
247 	msg.msg_namelen = sizeof(sq);
248 
249 	mutex_lock(&qmi->sock_lock);
250 	if (qmi->sock) {
251 		ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt));
252 		if (ret < 0)
253 			pr_err("send service registration failed: %d\n", ret);
254 	}
255 	mutex_unlock(&qmi->sock_lock);
256 }
257 
258 /**
259  * qmi_add_server() - register a service with the name service
260  * @qmi:	qmi handle
261  * @service:	type of the service
262  * @instance:	instance of the service
263  * @version:	version of the service
264  *
265  * Register a new service with the name service. This allows clients to find
266  * and start sending messages to the client associated with @qmi.
267  *
268  * Return: 0 on success, negative errno on failure.
269  */
270 int qmi_add_server(struct qmi_handle *qmi, unsigned int service,
271 		   unsigned int version, unsigned int instance)
272 {
273 	struct qmi_service *svc;
274 
275 	svc = kzalloc(sizeof(*svc), GFP_KERNEL);
276 	if (!svc)
277 		return -ENOMEM;
278 
279 	svc->service = service;
280 	svc->version = version;
281 	svc->instance = instance;
282 
283 	list_add(&svc->list_node, &qmi->services);
284 
285 	qmi_send_new_server(qmi, svc);
286 
287 	return 0;
288 }
289 EXPORT_SYMBOL(qmi_add_server);
290 
291 /**
292  * qmi_txn_init() - allocate transaction id within the given QMI handle
293  * @qmi:	QMI handle
294  * @txn:	transaction context
295  * @ei:		description of how to decode a matching response (optional)
296  * @c_struct:	pointer to the object to decode the response into (optional)
297  *
298  * This allocates a transaction id within the QMI handle. If @ei and @c_struct
299  * are specified any responses to this transaction will be decoded as described
300  * by @ei into @c_struct.
301  *
302  * A client calling qmi_txn_init() must call either qmi_txn_wait() or
303  * qmi_txn_cancel() to free up the allocated resources.
304  *
305  * Return: Transaction id on success, negative errno on failure.
306  */
307 int qmi_txn_init(struct qmi_handle *qmi, struct qmi_txn *txn,
308 		 struct qmi_elem_info *ei, void *c_struct)
309 {
310 	int ret;
311 
312 	memset(txn, 0, sizeof(*txn));
313 
314 	mutex_init(&txn->lock);
315 	init_completion(&txn->completion);
316 	txn->qmi = qmi;
317 	txn->ei = ei;
318 	txn->dest = c_struct;
319 
320 	mutex_lock(&qmi->txn_lock);
321 	ret = idr_alloc_cyclic(&qmi->txns, txn, 0, U16_MAX, GFP_KERNEL);
322 	if (ret < 0)
323 		pr_err("failed to allocate transaction id\n");
324 
325 	txn->id = ret;
326 	mutex_unlock(&qmi->txn_lock);
327 
328 	return ret;
329 }
330 EXPORT_SYMBOL(qmi_txn_init);
331 
332 /**
333  * qmi_txn_wait() - wait for a response on a transaction
334  * @txn:	transaction handle
335  * @timeout:	timeout, in jiffies
336  *
337  * If the transaction is decoded by the means of @ei and @c_struct the return
338  * value will be the returned value of qmi_decode_message(), otherwise it's up
339  * to the specified message handler to fill out the result.
340  *
341  * Return: the transaction response on success, negative errno on failure.
342  */
343 int qmi_txn_wait(struct qmi_txn *txn, unsigned long timeout)
344 {
345 	struct qmi_handle *qmi = txn->qmi;
346 	int ret;
347 
348 	ret = wait_for_completion_interruptible_timeout(&txn->completion,
349 							timeout);
350 
351 	mutex_lock(&qmi->txn_lock);
352 	mutex_lock(&txn->lock);
353 	idr_remove(&qmi->txns, txn->id);
354 	mutex_unlock(&txn->lock);
355 	mutex_unlock(&qmi->txn_lock);
356 
357 	if (ret < 0)
358 		return ret;
359 	else if (ret == 0)
360 		return -ETIMEDOUT;
361 	else
362 		return txn->result;
363 }
364 EXPORT_SYMBOL(qmi_txn_wait);
365 
366 /**
367  * qmi_txn_cancel() - cancel an ongoing transaction
368  * @txn:	transaction id
369  */
370 void qmi_txn_cancel(struct qmi_txn *txn)
371 {
372 	struct qmi_handle *qmi = txn->qmi;
373 
374 	mutex_lock(&qmi->txn_lock);
375 	mutex_lock(&txn->lock);
376 	idr_remove(&qmi->txns, txn->id);
377 	mutex_unlock(&txn->lock);
378 	mutex_unlock(&qmi->txn_lock);
379 }
380 EXPORT_SYMBOL(qmi_txn_cancel);
381 
382 /**
383  * qmi_invoke_handler() - find and invoke a handler for a message
384  * @qmi:	qmi handle
385  * @sq:		sockaddr of the sender
386  * @txn:	transaction object for the message
387  * @buf:	buffer containing the message
388  * @len:	length of @buf
389  *
390  * Find handler and invoke handler for the incoming message.
391  */
392 static void qmi_invoke_handler(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
393 			       struct qmi_txn *txn, const void *buf, size_t len)
394 {
395 	const struct qmi_msg_handler *handler;
396 	const struct qmi_header *hdr = buf;
397 	void *dest;
398 	int ret;
399 
400 	if (!qmi->handlers)
401 		return;
402 
403 	for (handler = qmi->handlers; handler->fn; handler++) {
404 		if (handler->type == hdr->type &&
405 		    handler->msg_id == hdr->msg_id)
406 			break;
407 	}
408 
409 	if (!handler->fn)
410 		return;
411 
412 	dest = kzalloc(handler->decoded_size, GFP_KERNEL);
413 	if (!dest)
414 		return;
415 
416 	ret = qmi_decode_message(buf, len, handler->ei, dest);
417 	if (ret < 0)
418 		pr_err("failed to decode incoming message\n");
419 	else
420 		handler->fn(qmi, sq, txn, dest);
421 
422 	kfree(dest);
423 }
424 
425 /**
426  * qmi_handle_net_reset() - invoked to handle ENETRESET on a QMI handle
427  * @qmi:	the QMI context
428  *
429  * As a result of registering a name service with the QRTR all open sockets are
430  * flagged with ENETRESET and this function will be called. The typical case is
431  * the initial boot, where this signals that the local node id has been
432  * configured and as such any bound sockets needs to be rebound. So close the
433  * socket, inform the client and re-initialize the socket.
434  *
435  * For clients it's generally sufficient to react to the del_server callbacks,
436  * but server code is expected to treat the net_reset callback as a "bye" from
437  * all nodes.
438  *
439  * Finally the QMI handle will send out registration requests for any lookups
440  * and services.
441  */
442 static void qmi_handle_net_reset(struct qmi_handle *qmi)
443 {
444 	struct sockaddr_qrtr sq;
445 	struct qmi_service *svc;
446 	struct socket *sock;
447 
448 	sock = qmi_sock_create(qmi, &sq);
449 	if (IS_ERR(sock))
450 		return;
451 
452 	mutex_lock(&qmi->sock_lock);
453 	sock_release(qmi->sock);
454 	qmi->sock = NULL;
455 	mutex_unlock(&qmi->sock_lock);
456 
457 	qmi_recv_del_server(qmi, -1, -1);
458 
459 	if (qmi->ops.net_reset)
460 		qmi->ops.net_reset(qmi);
461 
462 	mutex_lock(&qmi->sock_lock);
463 	qmi->sock = sock;
464 	qmi->sq = sq;
465 	mutex_unlock(&qmi->sock_lock);
466 
467 	list_for_each_entry(svc, &qmi->lookups, list_node)
468 		qmi_send_new_lookup(qmi, svc);
469 
470 	list_for_each_entry(svc, &qmi->services, list_node)
471 		qmi_send_new_server(qmi, svc);
472 }
473 
474 static void qmi_handle_message(struct qmi_handle *qmi,
475 			       struct sockaddr_qrtr *sq,
476 			       const void *buf, size_t len)
477 {
478 	const struct qmi_header *hdr;
479 	struct qmi_txn tmp_txn;
480 	struct qmi_txn *txn = NULL;
481 	int ret;
482 
483 	if (len < sizeof(*hdr)) {
484 		pr_err("ignoring short QMI packet\n");
485 		return;
486 	}
487 
488 	hdr = buf;
489 
490 	/* If this is a response, find the matching transaction handle */
491 	if (hdr->type == QMI_RESPONSE) {
492 		mutex_lock(&qmi->txn_lock);
493 		txn = idr_find(&qmi->txns, hdr->txn_id);
494 
495 		/* Ignore unexpected responses */
496 		if (!txn) {
497 			mutex_unlock(&qmi->txn_lock);
498 			return;
499 		}
500 
501 		mutex_lock(&txn->lock);
502 		mutex_unlock(&qmi->txn_lock);
503 
504 		if (txn->dest && txn->ei) {
505 			ret = qmi_decode_message(buf, len, txn->ei, txn->dest);
506 			if (ret < 0)
507 				pr_err("failed to decode incoming message\n");
508 
509 			txn->result = ret;
510 			complete(&txn->completion);
511 		} else  {
512 			qmi_invoke_handler(qmi, sq, txn, buf, len);
513 		}
514 
515 		mutex_unlock(&txn->lock);
516 	} else {
517 		/* Create a txn based on the txn_id of the incoming message */
518 		memset(&tmp_txn, 0, sizeof(tmp_txn));
519 		tmp_txn.id = hdr->txn_id;
520 
521 		qmi_invoke_handler(qmi, sq, &tmp_txn, buf, len);
522 	}
523 }
524 
525 static void qmi_data_ready_work(struct work_struct *work)
526 {
527 	struct qmi_handle *qmi = container_of(work, struct qmi_handle, work);
528 	struct qmi_ops *ops = &qmi->ops;
529 	struct sockaddr_qrtr sq;
530 	struct msghdr msg = { .msg_name = &sq, .msg_namelen = sizeof(sq) };
531 	struct kvec iv;
532 	ssize_t msglen;
533 
534 	for (;;) {
535 		iv.iov_base = qmi->recv_buf;
536 		iv.iov_len = qmi->recv_buf_size;
537 
538 		mutex_lock(&qmi->sock_lock);
539 		if (qmi->sock)
540 			msglen = kernel_recvmsg(qmi->sock, &msg, &iv, 1,
541 						iv.iov_len, MSG_DONTWAIT);
542 		else
543 			msglen = -EPIPE;
544 		mutex_unlock(&qmi->sock_lock);
545 		if (msglen == -EAGAIN)
546 			break;
547 
548 		if (msglen == -ENETRESET) {
549 			qmi_handle_net_reset(qmi);
550 
551 			/* The old qmi->sock is gone, our work is done */
552 			break;
553 		}
554 
555 		if (msglen < 0) {
556 			pr_err("qmi recvmsg failed: %zd\n", msglen);
557 			break;
558 		}
559 
560 		if (sq.sq_node == qmi->sq.sq_node &&
561 		    sq.sq_port == QRTR_PORT_CTRL) {
562 			qmi_recv_ctrl_pkt(qmi, qmi->recv_buf, msglen);
563 		} else if (ops->msg_handler) {
564 			ops->msg_handler(qmi, &sq, qmi->recv_buf, msglen);
565 		} else {
566 			qmi_handle_message(qmi, &sq, qmi->recv_buf, msglen);
567 		}
568 	}
569 }
570 
571 static void qmi_data_ready(struct sock *sk)
572 {
573 	struct qmi_handle *qmi = sk->sk_user_data;
574 
575 	/*
576 	 * This will be NULL if we receive data while being in
577 	 * qmi_handle_release()
578 	 */
579 	if (!qmi)
580 		return;
581 
582 	queue_work(qmi->wq, &qmi->work);
583 }
584 
585 static struct socket *qmi_sock_create(struct qmi_handle *qmi,
586 				      struct sockaddr_qrtr *sq)
587 {
588 	struct socket *sock;
589 	int ret;
590 
591 	ret = sock_create_kern(&init_net, AF_QIPCRTR, SOCK_DGRAM,
592 			       PF_QIPCRTR, &sock);
593 	if (ret < 0)
594 		return ERR_PTR(ret);
595 
596 	ret = kernel_getsockname(sock, (struct sockaddr *)sq);
597 	if (ret < 0) {
598 		sock_release(sock);
599 		return ERR_PTR(ret);
600 	}
601 
602 	sock->sk->sk_user_data = qmi;
603 	sock->sk->sk_data_ready = qmi_data_ready;
604 	sock->sk->sk_error_report = qmi_data_ready;
605 
606 	return sock;
607 }
608 
609 /**
610  * qmi_handle_init() - initialize a QMI client handle
611  * @qmi:	QMI handle to initialize
612  * @recv_buf_size: maximum size of incoming message
613  * @ops:	reference to callbacks for QRTR notifications
614  * @handlers:	NULL-terminated list of QMI message handlers
615  *
616  * This initializes the QMI client handle to allow sending and receiving QMI
617  * messages. As messages are received the appropriate handler will be invoked.
618  *
619  * Return: 0 on success, negative errno on failure.
620  */
621 int qmi_handle_init(struct qmi_handle *qmi, size_t recv_buf_size,
622 		    const struct qmi_ops *ops,
623 		    const struct qmi_msg_handler *handlers)
624 {
625 	int ret;
626 
627 	mutex_init(&qmi->txn_lock);
628 	mutex_init(&qmi->sock_lock);
629 
630 	idr_init(&qmi->txns);
631 
632 	INIT_LIST_HEAD(&qmi->lookups);
633 	INIT_LIST_HEAD(&qmi->lookup_results);
634 	INIT_LIST_HEAD(&qmi->services);
635 
636 	INIT_WORK(&qmi->work, qmi_data_ready_work);
637 
638 	qmi->handlers = handlers;
639 	if (ops)
640 		qmi->ops = *ops;
641 
642 	/* Make room for the header */
643 	recv_buf_size += sizeof(struct qmi_header);
644 	/* Must also be sufficient to hold a control packet */
645 	if (recv_buf_size < sizeof(struct qrtr_ctrl_pkt))
646 		recv_buf_size = sizeof(struct qrtr_ctrl_pkt);
647 
648 	qmi->recv_buf_size = recv_buf_size;
649 	qmi->recv_buf = kzalloc(recv_buf_size, GFP_KERNEL);
650 	if (!qmi->recv_buf)
651 		return -ENOMEM;
652 
653 	qmi->wq = alloc_workqueue("qmi_msg_handler", WQ_UNBOUND, 1);
654 	if (!qmi->wq) {
655 		ret = -ENOMEM;
656 		goto err_free_recv_buf;
657 	}
658 
659 	qmi->sock = qmi_sock_create(qmi, &qmi->sq);
660 	if (IS_ERR(qmi->sock)) {
661 		pr_err("failed to create QMI socket\n");
662 		ret = PTR_ERR(qmi->sock);
663 		goto err_destroy_wq;
664 	}
665 
666 	return 0;
667 
668 err_destroy_wq:
669 	destroy_workqueue(qmi->wq);
670 err_free_recv_buf:
671 	kfree(qmi->recv_buf);
672 
673 	return ret;
674 }
675 EXPORT_SYMBOL(qmi_handle_init);
676 
677 /**
678  * qmi_handle_release() - release the QMI client handle
679  * @qmi:	QMI client handle
680  *
681  * This closes the underlying socket and stops any handling of QMI messages.
682  */
683 void qmi_handle_release(struct qmi_handle *qmi)
684 {
685 	struct socket *sock = qmi->sock;
686 	struct qmi_service *svc, *tmp;
687 
688 	sock->sk->sk_user_data = NULL;
689 	cancel_work_sync(&qmi->work);
690 
691 	qmi_recv_del_server(qmi, -1, -1);
692 
693 	mutex_lock(&qmi->sock_lock);
694 	sock_release(sock);
695 	qmi->sock = NULL;
696 	mutex_unlock(&qmi->sock_lock);
697 
698 	destroy_workqueue(qmi->wq);
699 
700 	idr_destroy(&qmi->txns);
701 
702 	kfree(qmi->recv_buf);
703 
704 	/* Free registered lookup requests */
705 	list_for_each_entry_safe(svc, tmp, &qmi->lookups, list_node) {
706 		list_del(&svc->list_node);
707 		kfree(svc);
708 	}
709 
710 	/* Free registered service information */
711 	list_for_each_entry_safe(svc, tmp, &qmi->services, list_node) {
712 		list_del(&svc->list_node);
713 		kfree(svc);
714 	}
715 }
716 EXPORT_SYMBOL(qmi_handle_release);
717 
718 /**
719  * qmi_send_message() - send a QMI message
720  * @qmi:	QMI client handle
721  * @sq:		destination sockaddr
722  * @txn:	transaction object to use for the message
723  * @type:	type of message to send
724  * @msg_id:	message id
725  * @len:	max length of the QMI message
726  * @ei:		QMI message description
727  * @c_struct:	object to be encoded
728  *
729  * This function encodes @c_struct using @ei into a message of type @type,
730  * with @msg_id and @txn into a buffer of maximum size @len, and sends this to
731  * @sq.
732  *
733  * Return: 0 on success, negative errno on failure.
734  */
735 static ssize_t qmi_send_message(struct qmi_handle *qmi,
736 				struct sockaddr_qrtr *sq, struct qmi_txn *txn,
737 				int type, int msg_id, size_t len,
738 				struct qmi_elem_info *ei, const void *c_struct)
739 {
740 	struct msghdr msghdr = {};
741 	struct kvec iv;
742 	void *msg;
743 	int ret;
744 
745 	msg = qmi_encode_message(type,
746 				 msg_id, &len,
747 				 txn->id, ei,
748 				 c_struct);
749 	if (IS_ERR(msg))
750 		return PTR_ERR(msg);
751 
752 	iv.iov_base = msg;
753 	iv.iov_len = len;
754 
755 	if (sq) {
756 		msghdr.msg_name = sq;
757 		msghdr.msg_namelen = sizeof(*sq);
758 	}
759 
760 	mutex_lock(&qmi->sock_lock);
761 	if (qmi->sock) {
762 		ret = kernel_sendmsg(qmi->sock, &msghdr, &iv, 1, len);
763 		if (ret < 0)
764 			pr_err("failed to send QMI message\n");
765 	} else {
766 		ret = -EPIPE;
767 	}
768 	mutex_unlock(&qmi->sock_lock);
769 
770 	kfree(msg);
771 
772 	return ret < 0 ? ret : 0;
773 }
774 
775 /**
776  * qmi_send_request() - send a request QMI message
777  * @qmi:	QMI client handle
778  * @sq:		destination sockaddr
779  * @txn:	transaction object to use for the message
780  * @msg_id:	message id
781  * @len:	max length of the QMI message
782  * @ei:		QMI message description
783  * @c_struct:	object to be encoded
784  *
785  * Return: 0 on success, negative errno on failure.
786  */
787 ssize_t qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
788 			 struct qmi_txn *txn, int msg_id, size_t len,
789 			 struct qmi_elem_info *ei, const void *c_struct)
790 {
791 	return qmi_send_message(qmi, sq, txn, QMI_REQUEST, msg_id, len, ei,
792 				c_struct);
793 }
794 EXPORT_SYMBOL(qmi_send_request);
795 
796 /**
797  * qmi_send_response() - send a response QMI message
798  * @qmi:	QMI client handle
799  * @sq:		destination sockaddr
800  * @txn:	transaction object to use for the message
801  * @msg_id:	message id
802  * @len:	max length of the QMI message
803  * @ei:		QMI message description
804  * @c_struct:	object to be encoded
805  *
806  * Return: 0 on success, negative errno on failure.
807  */
808 ssize_t qmi_send_response(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
809 			  struct qmi_txn *txn, int msg_id, size_t len,
810 			  struct qmi_elem_info *ei, const void *c_struct)
811 {
812 	return qmi_send_message(qmi, sq, txn, QMI_RESPONSE, msg_id, len, ei,
813 				c_struct);
814 }
815 EXPORT_SYMBOL(qmi_send_response);
816 
817 /**
818  * qmi_send_indication() - send an indication QMI message
819  * @qmi:	QMI client handle
820  * @sq:		destination sockaddr
821  * @msg_id:	message id
822  * @len:	max length of the QMI message
823  * @ei:		QMI message description
824  * @c_struct:	object to be encoded
825  *
826  * Return: 0 on success, negative errno on failure.
827  */
828 ssize_t qmi_send_indication(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
829 			    int msg_id, size_t len, struct qmi_elem_info *ei,
830 			    const void *c_struct)
831 {
832 	struct qmi_txn txn;
833 	ssize_t rval;
834 	int ret;
835 
836 	ret = qmi_txn_init(qmi, &txn, NULL, NULL);
837 	if (ret < 0)
838 		return ret;
839 
840 	rval = qmi_send_message(qmi, sq, &txn, QMI_INDICATION, msg_id, len, ei,
841 				c_struct);
842 
843 	/* We don't care about future messages on this txn */
844 	qmi_txn_cancel(&txn);
845 
846 	return rval;
847 }
848 EXPORT_SYMBOL(qmi_send_indication);
849