1*8465def4SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2*8465def4SGreg Kroah-Hartman /*
3*8465def4SGreg Kroah-Hartman * Greybus operations
4*8465def4SGreg Kroah-Hartman *
5*8465def4SGreg Kroah-Hartman * Copyright 2014-2015 Google Inc.
6*8465def4SGreg Kroah-Hartman * Copyright 2014-2015 Linaro Ltd.
7*8465def4SGreg Kroah-Hartman */
8*8465def4SGreg Kroah-Hartman
9*8465def4SGreg Kroah-Hartman #include <linux/kernel.h>
10*8465def4SGreg Kroah-Hartman #include <linux/slab.h>
11*8465def4SGreg Kroah-Hartman #include <linux/module.h>
12*8465def4SGreg Kroah-Hartman #include <linux/sched.h>
13*8465def4SGreg Kroah-Hartman #include <linux/wait.h>
14*8465def4SGreg Kroah-Hartman #include <linux/workqueue.h>
15*8465def4SGreg Kroah-Hartman #include <linux/greybus.h>
16*8465def4SGreg Kroah-Hartman
17*8465def4SGreg Kroah-Hartman #include "greybus_trace.h"
18*8465def4SGreg Kroah-Hartman
19*8465def4SGreg Kroah-Hartman static struct kmem_cache *gb_operation_cache;
20*8465def4SGreg Kroah-Hartman static struct kmem_cache *gb_message_cache;
21*8465def4SGreg Kroah-Hartman
22*8465def4SGreg Kroah-Hartman /* Workqueue to handle Greybus operation completions. */
23*8465def4SGreg Kroah-Hartman static struct workqueue_struct *gb_operation_completion_wq;
24*8465def4SGreg Kroah-Hartman
25*8465def4SGreg Kroah-Hartman /* Wait queue for synchronous cancellations. */
26*8465def4SGreg Kroah-Hartman static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue);
27*8465def4SGreg Kroah-Hartman
28*8465def4SGreg Kroah-Hartman /*
29*8465def4SGreg Kroah-Hartman * Protects updates to operation->errno.
30*8465def4SGreg Kroah-Hartman */
31*8465def4SGreg Kroah-Hartman static DEFINE_SPINLOCK(gb_operations_lock);
32*8465def4SGreg Kroah-Hartman
33*8465def4SGreg Kroah-Hartman static int gb_operation_response_send(struct gb_operation *operation,
34*8465def4SGreg Kroah-Hartman int errno);
35*8465def4SGreg Kroah-Hartman
36*8465def4SGreg Kroah-Hartman /*
37*8465def4SGreg Kroah-Hartman * Increment operation active count and add to connection list unless the
38*8465def4SGreg Kroah-Hartman * connection is going away.
39*8465def4SGreg Kroah-Hartman *
40*8465def4SGreg Kroah-Hartman * Caller holds operation reference.
41*8465def4SGreg Kroah-Hartman */
gb_operation_get_active(struct gb_operation * operation)42*8465def4SGreg Kroah-Hartman static int gb_operation_get_active(struct gb_operation *operation)
43*8465def4SGreg Kroah-Hartman {
44*8465def4SGreg Kroah-Hartman struct gb_connection *connection = operation->connection;
45*8465def4SGreg Kroah-Hartman unsigned long flags;
46*8465def4SGreg Kroah-Hartman
47*8465def4SGreg Kroah-Hartman spin_lock_irqsave(&connection->lock, flags);
48*8465def4SGreg Kroah-Hartman switch (connection->state) {
49*8465def4SGreg Kroah-Hartman case GB_CONNECTION_STATE_ENABLED:
50*8465def4SGreg Kroah-Hartman break;
51*8465def4SGreg Kroah-Hartman case GB_CONNECTION_STATE_ENABLED_TX:
52*8465def4SGreg Kroah-Hartman if (gb_operation_is_incoming(operation))
53*8465def4SGreg Kroah-Hartman goto err_unlock;
54*8465def4SGreg Kroah-Hartman break;
55*8465def4SGreg Kroah-Hartman case GB_CONNECTION_STATE_DISCONNECTING:
56*8465def4SGreg Kroah-Hartman if (!gb_operation_is_core(operation))
57*8465def4SGreg Kroah-Hartman goto err_unlock;
58*8465def4SGreg Kroah-Hartman break;
59*8465def4SGreg Kroah-Hartman default:
60*8465def4SGreg Kroah-Hartman goto err_unlock;
61*8465def4SGreg Kroah-Hartman }
62*8465def4SGreg Kroah-Hartman
63*8465def4SGreg Kroah-Hartman if (operation->active++ == 0)
64*8465def4SGreg Kroah-Hartman list_add_tail(&operation->links, &connection->operations);
65*8465def4SGreg Kroah-Hartman
66*8465def4SGreg Kroah-Hartman trace_gb_operation_get_active(operation);
67*8465def4SGreg Kroah-Hartman
68*8465def4SGreg Kroah-Hartman spin_unlock_irqrestore(&connection->lock, flags);
69*8465def4SGreg Kroah-Hartman
70*8465def4SGreg Kroah-Hartman return 0;
71*8465def4SGreg Kroah-Hartman
72*8465def4SGreg Kroah-Hartman err_unlock:
73*8465def4SGreg Kroah-Hartman spin_unlock_irqrestore(&connection->lock, flags);
74*8465def4SGreg Kroah-Hartman
75*8465def4SGreg Kroah-Hartman return -ENOTCONN;
76*8465def4SGreg Kroah-Hartman }
77*8465def4SGreg Kroah-Hartman
78*8465def4SGreg Kroah-Hartman /* Caller holds operation reference. */
gb_operation_put_active(struct gb_operation * operation)79*8465def4SGreg Kroah-Hartman static void gb_operation_put_active(struct gb_operation *operation)
80*8465def4SGreg Kroah-Hartman {
81*8465def4SGreg Kroah-Hartman struct gb_connection *connection = operation->connection;
82*8465def4SGreg Kroah-Hartman unsigned long flags;
83*8465def4SGreg Kroah-Hartman
84*8465def4SGreg Kroah-Hartman spin_lock_irqsave(&connection->lock, flags);
85*8465def4SGreg Kroah-Hartman
86*8465def4SGreg Kroah-Hartman trace_gb_operation_put_active(operation);
87*8465def4SGreg Kroah-Hartman
88*8465def4SGreg Kroah-Hartman if (--operation->active == 0) {
89*8465def4SGreg Kroah-Hartman list_del(&operation->links);
90*8465def4SGreg Kroah-Hartman if (atomic_read(&operation->waiters))
91*8465def4SGreg Kroah-Hartman wake_up(&gb_operation_cancellation_queue);
92*8465def4SGreg Kroah-Hartman }
93*8465def4SGreg Kroah-Hartman spin_unlock_irqrestore(&connection->lock, flags);
94*8465def4SGreg Kroah-Hartman }
95*8465def4SGreg Kroah-Hartman
gb_operation_is_active(struct gb_operation * operation)96*8465def4SGreg Kroah-Hartman static bool gb_operation_is_active(struct gb_operation *operation)
97*8465def4SGreg Kroah-Hartman {
98*8465def4SGreg Kroah-Hartman struct gb_connection *connection = operation->connection;
99*8465def4SGreg Kroah-Hartman unsigned long flags;
100*8465def4SGreg Kroah-Hartman bool ret;
101*8465def4SGreg Kroah-Hartman
102*8465def4SGreg Kroah-Hartman spin_lock_irqsave(&connection->lock, flags);
103*8465def4SGreg Kroah-Hartman ret = operation->active;
104*8465def4SGreg Kroah-Hartman spin_unlock_irqrestore(&connection->lock, flags);
105*8465def4SGreg Kroah-Hartman
106*8465def4SGreg Kroah-Hartman return ret;
107*8465def4SGreg Kroah-Hartman }
108*8465def4SGreg Kroah-Hartman
109*8465def4SGreg Kroah-Hartman /*
110*8465def4SGreg Kroah-Hartman * Set an operation's result.
111*8465def4SGreg Kroah-Hartman *
112*8465def4SGreg Kroah-Hartman * Initially an outgoing operation's errno value is -EBADR.
113*8465def4SGreg Kroah-Hartman * If no error occurs before sending the request message the only
114*8465def4SGreg Kroah-Hartman * valid value operation->errno can be set to is -EINPROGRESS,
115*8465def4SGreg Kroah-Hartman * indicating the request has been (or rather is about to be) sent.
116*8465def4SGreg Kroah-Hartman * At that point nobody should be looking at the result until the
117*8465def4SGreg Kroah-Hartman * response arrives.
118*8465def4SGreg Kroah-Hartman *
119*8465def4SGreg Kroah-Hartman * The first time the result gets set after the request has been
120*8465def4SGreg Kroah-Hartman * sent, that result "sticks." That is, if two concurrent threads
121*8465def4SGreg Kroah-Hartman * race to set the result, the first one wins. The return value
122*8465def4SGreg Kroah-Hartman * tells the caller whether its result was recorded; if not the
123*8465def4SGreg Kroah-Hartman * caller has nothing more to do.
124*8465def4SGreg Kroah-Hartman *
125*8465def4SGreg Kroah-Hartman * The result value -EILSEQ is reserved to signal an implementation
126*8465def4SGreg Kroah-Hartman * error; if it's ever observed, the code performing the request has
127*8465def4SGreg Kroah-Hartman * done something fundamentally wrong. It is an error to try to set
128*8465def4SGreg Kroah-Hartman * the result to -EBADR, and attempts to do so result in a warning,
129*8465def4SGreg Kroah-Hartman * and -EILSEQ is used instead. Similarly, the only valid result
130*8465def4SGreg Kroah-Hartman * value to set for an operation in initial state is -EINPROGRESS.
131*8465def4SGreg Kroah-Hartman * Attempts to do otherwise will also record a (successful) -EILSEQ
132*8465def4SGreg Kroah-Hartman * operation result.
133*8465def4SGreg Kroah-Hartman */
gb_operation_result_set(struct gb_operation * operation,int result)134*8465def4SGreg Kroah-Hartman static bool gb_operation_result_set(struct gb_operation *operation, int result)
135*8465def4SGreg Kroah-Hartman {
136*8465def4SGreg Kroah-Hartman unsigned long flags;
137*8465def4SGreg Kroah-Hartman int prev;
138*8465def4SGreg Kroah-Hartman
139*8465def4SGreg Kroah-Hartman if (result == -EINPROGRESS) {
140*8465def4SGreg Kroah-Hartman /*
141*8465def4SGreg Kroah-Hartman * -EINPROGRESS is used to indicate the request is
142*8465def4SGreg Kroah-Hartman * in flight. It should be the first result value
143*8465def4SGreg Kroah-Hartman * set after the initial -EBADR. Issue a warning
144*8465def4SGreg Kroah-Hartman * and record an implementation error if it's
145*8465def4SGreg Kroah-Hartman * set at any other time.
146*8465def4SGreg Kroah-Hartman */
147*8465def4SGreg Kroah-Hartman spin_lock_irqsave(&gb_operations_lock, flags);
148*8465def4SGreg Kroah-Hartman prev = operation->errno;
149*8465def4SGreg Kroah-Hartman if (prev == -EBADR)
150*8465def4SGreg Kroah-Hartman operation->errno = result;
151*8465def4SGreg Kroah-Hartman else
152*8465def4SGreg Kroah-Hartman operation->errno = -EILSEQ;
153*8465def4SGreg Kroah-Hartman spin_unlock_irqrestore(&gb_operations_lock, flags);
154*8465def4SGreg Kroah-Hartman WARN_ON(prev != -EBADR);
155*8465def4SGreg Kroah-Hartman
156*8465def4SGreg Kroah-Hartman return true;
157*8465def4SGreg Kroah-Hartman }
158*8465def4SGreg Kroah-Hartman
159*8465def4SGreg Kroah-Hartman /*
160*8465def4SGreg Kroah-Hartman * The first result value set after a request has been sent
161*8465def4SGreg Kroah-Hartman * will be the final result of the operation. Subsequent
162*8465def4SGreg Kroah-Hartman * attempts to set the result are ignored.
163*8465def4SGreg Kroah-Hartman *
164*8465def4SGreg Kroah-Hartman * Note that -EBADR is a reserved "initial state" result
165*8465def4SGreg Kroah-Hartman * value. Attempts to set this value result in a warning,
166*8465def4SGreg Kroah-Hartman * and the result code is set to -EILSEQ instead.
167*8465def4SGreg Kroah-Hartman */
168*8465def4SGreg Kroah-Hartman if (WARN_ON(result == -EBADR))
169*8465def4SGreg Kroah-Hartman result = -EILSEQ; /* Nobody should be setting -EBADR */
170*8465def4SGreg Kroah-Hartman
171*8465def4SGreg Kroah-Hartman spin_lock_irqsave(&gb_operations_lock, flags);
172*8465def4SGreg Kroah-Hartman prev = operation->errno;
173*8465def4SGreg Kroah-Hartman if (prev == -EINPROGRESS)
174*8465def4SGreg Kroah-Hartman operation->errno = result; /* First and final result */
175*8465def4SGreg Kroah-Hartman spin_unlock_irqrestore(&gb_operations_lock, flags);
176*8465def4SGreg Kroah-Hartman
177*8465def4SGreg Kroah-Hartman return prev == -EINPROGRESS;
178*8465def4SGreg Kroah-Hartman }
179*8465def4SGreg Kroah-Hartman
gb_operation_result(struct gb_operation * operation)180*8465def4SGreg Kroah-Hartman int gb_operation_result(struct gb_operation *operation)
181*8465def4SGreg Kroah-Hartman {
182*8465def4SGreg Kroah-Hartman int result = operation->errno;
183*8465def4SGreg Kroah-Hartman
184*8465def4SGreg Kroah-Hartman WARN_ON(result == -EBADR);
185*8465def4SGreg Kroah-Hartman WARN_ON(result == -EINPROGRESS);
186*8465def4SGreg Kroah-Hartman
187*8465def4SGreg Kroah-Hartman return result;
188*8465def4SGreg Kroah-Hartman }
189*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_result);
190*8465def4SGreg Kroah-Hartman
191*8465def4SGreg Kroah-Hartman /*
192*8465def4SGreg Kroah-Hartman * Looks up an outgoing operation on a connection and returns a refcounted
193*8465def4SGreg Kroah-Hartman * pointer if found, or NULL otherwise.
194*8465def4SGreg Kroah-Hartman */
195*8465def4SGreg Kroah-Hartman static struct gb_operation *
gb_operation_find_outgoing(struct gb_connection * connection,u16 operation_id)196*8465def4SGreg Kroah-Hartman gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
197*8465def4SGreg Kroah-Hartman {
198*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
199*8465def4SGreg Kroah-Hartman unsigned long flags;
200*8465def4SGreg Kroah-Hartman bool found = false;
201*8465def4SGreg Kroah-Hartman
202*8465def4SGreg Kroah-Hartman spin_lock_irqsave(&connection->lock, flags);
203*8465def4SGreg Kroah-Hartman list_for_each_entry(operation, &connection->operations, links)
204*8465def4SGreg Kroah-Hartman if (operation->id == operation_id &&
205*8465def4SGreg Kroah-Hartman !gb_operation_is_incoming(operation)) {
206*8465def4SGreg Kroah-Hartman gb_operation_get(operation);
207*8465def4SGreg Kroah-Hartman found = true;
208*8465def4SGreg Kroah-Hartman break;
209*8465def4SGreg Kroah-Hartman }
210*8465def4SGreg Kroah-Hartman spin_unlock_irqrestore(&connection->lock, flags);
211*8465def4SGreg Kroah-Hartman
212*8465def4SGreg Kroah-Hartman return found ? operation : NULL;
213*8465def4SGreg Kroah-Hartman }
214*8465def4SGreg Kroah-Hartman
gb_message_send(struct gb_message * message,gfp_t gfp)215*8465def4SGreg Kroah-Hartman static int gb_message_send(struct gb_message *message, gfp_t gfp)
216*8465def4SGreg Kroah-Hartman {
217*8465def4SGreg Kroah-Hartman struct gb_connection *connection = message->operation->connection;
218*8465def4SGreg Kroah-Hartman
219*8465def4SGreg Kroah-Hartman trace_gb_message_send(message);
220*8465def4SGreg Kroah-Hartman return connection->hd->driver->message_send(connection->hd,
221*8465def4SGreg Kroah-Hartman connection->hd_cport_id,
222*8465def4SGreg Kroah-Hartman message,
223*8465def4SGreg Kroah-Hartman gfp);
224*8465def4SGreg Kroah-Hartman }
225*8465def4SGreg Kroah-Hartman
226*8465def4SGreg Kroah-Hartman /*
227*8465def4SGreg Kroah-Hartman * Cancel a message we have passed to the host device layer to be sent.
228*8465def4SGreg Kroah-Hartman */
gb_message_cancel(struct gb_message * message)229*8465def4SGreg Kroah-Hartman static void gb_message_cancel(struct gb_message *message)
230*8465def4SGreg Kroah-Hartman {
231*8465def4SGreg Kroah-Hartman struct gb_host_device *hd = message->operation->connection->hd;
232*8465def4SGreg Kroah-Hartman
233*8465def4SGreg Kroah-Hartman hd->driver->message_cancel(message);
234*8465def4SGreg Kroah-Hartman }
235*8465def4SGreg Kroah-Hartman
gb_operation_request_handle(struct gb_operation * operation)236*8465def4SGreg Kroah-Hartman static void gb_operation_request_handle(struct gb_operation *operation)
237*8465def4SGreg Kroah-Hartman {
238*8465def4SGreg Kroah-Hartman struct gb_connection *connection = operation->connection;
239*8465def4SGreg Kroah-Hartman int status;
240*8465def4SGreg Kroah-Hartman int ret;
241*8465def4SGreg Kroah-Hartman
242*8465def4SGreg Kroah-Hartman if (connection->handler) {
243*8465def4SGreg Kroah-Hartman status = connection->handler(operation);
244*8465def4SGreg Kroah-Hartman } else {
245*8465def4SGreg Kroah-Hartman dev_err(&connection->hd->dev,
246*8465def4SGreg Kroah-Hartman "%s: unexpected incoming request of type 0x%02x\n",
247*8465def4SGreg Kroah-Hartman connection->name, operation->type);
248*8465def4SGreg Kroah-Hartman
249*8465def4SGreg Kroah-Hartman status = -EPROTONOSUPPORT;
250*8465def4SGreg Kroah-Hartman }
251*8465def4SGreg Kroah-Hartman
252*8465def4SGreg Kroah-Hartman ret = gb_operation_response_send(operation, status);
253*8465def4SGreg Kroah-Hartman if (ret) {
254*8465def4SGreg Kroah-Hartman dev_err(&connection->hd->dev,
255*8465def4SGreg Kroah-Hartman "%s: failed to send response %d for type 0x%02x: %d\n",
256*8465def4SGreg Kroah-Hartman connection->name, status, operation->type, ret);
257*8465def4SGreg Kroah-Hartman return;
258*8465def4SGreg Kroah-Hartman }
259*8465def4SGreg Kroah-Hartman }
260*8465def4SGreg Kroah-Hartman
261*8465def4SGreg Kroah-Hartman /*
262*8465def4SGreg Kroah-Hartman * Process operation work.
263*8465def4SGreg Kroah-Hartman *
264*8465def4SGreg Kroah-Hartman * For incoming requests, call the protocol request handler. The operation
265*8465def4SGreg Kroah-Hartman * result should be -EINPROGRESS at this point.
266*8465def4SGreg Kroah-Hartman *
267*8465def4SGreg Kroah-Hartman * For outgoing requests, the operation result value should have
268*8465def4SGreg Kroah-Hartman * been set before queueing this. The operation callback function
269*8465def4SGreg Kroah-Hartman * allows the original requester to know the request has completed
270*8465def4SGreg Kroah-Hartman * and its result is available.
271*8465def4SGreg Kroah-Hartman */
gb_operation_work(struct work_struct * work)272*8465def4SGreg Kroah-Hartman static void gb_operation_work(struct work_struct *work)
273*8465def4SGreg Kroah-Hartman {
274*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
275*8465def4SGreg Kroah-Hartman int ret;
276*8465def4SGreg Kroah-Hartman
277*8465def4SGreg Kroah-Hartman operation = container_of(work, struct gb_operation, work);
278*8465def4SGreg Kroah-Hartman
279*8465def4SGreg Kroah-Hartman if (gb_operation_is_incoming(operation)) {
280*8465def4SGreg Kroah-Hartman gb_operation_request_handle(operation);
281*8465def4SGreg Kroah-Hartman } else {
282*8465def4SGreg Kroah-Hartman ret = del_timer_sync(&operation->timer);
283*8465def4SGreg Kroah-Hartman if (!ret) {
284*8465def4SGreg Kroah-Hartman /* Cancel request message if scheduled by timeout. */
285*8465def4SGreg Kroah-Hartman if (gb_operation_result(operation) == -ETIMEDOUT)
286*8465def4SGreg Kroah-Hartman gb_message_cancel(operation->request);
287*8465def4SGreg Kroah-Hartman }
288*8465def4SGreg Kroah-Hartman
289*8465def4SGreg Kroah-Hartman operation->callback(operation);
290*8465def4SGreg Kroah-Hartman }
291*8465def4SGreg Kroah-Hartman
292*8465def4SGreg Kroah-Hartman gb_operation_put_active(operation);
293*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
294*8465def4SGreg Kroah-Hartman }
295*8465def4SGreg Kroah-Hartman
gb_operation_timeout(struct timer_list * t)296*8465def4SGreg Kroah-Hartman static void gb_operation_timeout(struct timer_list *t)
297*8465def4SGreg Kroah-Hartman {
298*8465def4SGreg Kroah-Hartman struct gb_operation *operation = from_timer(operation, t, timer);
299*8465def4SGreg Kroah-Hartman
300*8465def4SGreg Kroah-Hartman if (gb_operation_result_set(operation, -ETIMEDOUT)) {
301*8465def4SGreg Kroah-Hartman /*
302*8465def4SGreg Kroah-Hartman * A stuck request message will be cancelled from the
303*8465def4SGreg Kroah-Hartman * workqueue.
304*8465def4SGreg Kroah-Hartman */
305*8465def4SGreg Kroah-Hartman queue_work(gb_operation_completion_wq, &operation->work);
306*8465def4SGreg Kroah-Hartman }
307*8465def4SGreg Kroah-Hartman }
308*8465def4SGreg Kroah-Hartman
gb_operation_message_init(struct gb_host_device * hd,struct gb_message * message,u16 operation_id,size_t payload_size,u8 type)309*8465def4SGreg Kroah-Hartman static void gb_operation_message_init(struct gb_host_device *hd,
310*8465def4SGreg Kroah-Hartman struct gb_message *message,
311*8465def4SGreg Kroah-Hartman u16 operation_id,
312*8465def4SGreg Kroah-Hartman size_t payload_size, u8 type)
313*8465def4SGreg Kroah-Hartman {
314*8465def4SGreg Kroah-Hartman struct gb_operation_msg_hdr *header;
315*8465def4SGreg Kroah-Hartman
316*8465def4SGreg Kroah-Hartman header = message->buffer;
317*8465def4SGreg Kroah-Hartman
318*8465def4SGreg Kroah-Hartman message->header = header;
319*8465def4SGreg Kroah-Hartman message->payload = payload_size ? header + 1 : NULL;
320*8465def4SGreg Kroah-Hartman message->payload_size = payload_size;
321*8465def4SGreg Kroah-Hartman
322*8465def4SGreg Kroah-Hartman /*
323*8465def4SGreg Kroah-Hartman * The type supplied for incoming message buffers will be
324*8465def4SGreg Kroah-Hartman * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by
325*8465def4SGreg Kroah-Hartman * arriving data so there's no need to initialize the message header.
326*8465def4SGreg Kroah-Hartman */
327*8465def4SGreg Kroah-Hartman if (type != GB_REQUEST_TYPE_INVALID) {
328*8465def4SGreg Kroah-Hartman u16 message_size = (u16)(sizeof(*header) + payload_size);
329*8465def4SGreg Kroah-Hartman
330*8465def4SGreg Kroah-Hartman /*
331*8465def4SGreg Kroah-Hartman * For a request, the operation id gets filled in
332*8465def4SGreg Kroah-Hartman * when the message is sent. For a response, it
333*8465def4SGreg Kroah-Hartman * will be copied from the request by the caller.
334*8465def4SGreg Kroah-Hartman *
335*8465def4SGreg Kroah-Hartman * The result field in a request message must be
336*8465def4SGreg Kroah-Hartman * zero. It will be set just prior to sending for
337*8465def4SGreg Kroah-Hartman * a response.
338*8465def4SGreg Kroah-Hartman */
339*8465def4SGreg Kroah-Hartman header->size = cpu_to_le16(message_size);
340*8465def4SGreg Kroah-Hartman header->operation_id = 0;
341*8465def4SGreg Kroah-Hartman header->type = type;
342*8465def4SGreg Kroah-Hartman header->result = 0;
343*8465def4SGreg Kroah-Hartman }
344*8465def4SGreg Kroah-Hartman }
345*8465def4SGreg Kroah-Hartman
346*8465def4SGreg Kroah-Hartman /*
347*8465def4SGreg Kroah-Hartman * Allocate a message to be used for an operation request or response.
348*8465def4SGreg Kroah-Hartman * Both types of message contain a common header. The request message
349*8465def4SGreg Kroah-Hartman * for an outgoing operation is outbound, as is the response message
350*8465def4SGreg Kroah-Hartman * for an incoming operation. The message header for an outbound
351*8465def4SGreg Kroah-Hartman * message is partially initialized here.
352*8465def4SGreg Kroah-Hartman *
353*8465def4SGreg Kroah-Hartman * The headers for inbound messages don't need to be initialized;
354*8465def4SGreg Kroah-Hartman * they'll be filled in by arriving data.
355*8465def4SGreg Kroah-Hartman *
356*8465def4SGreg Kroah-Hartman * Our message buffers have the following layout:
357*8465def4SGreg Kroah-Hartman * message header \_ these combined are
358*8465def4SGreg Kroah-Hartman * message payload / the message size
359*8465def4SGreg Kroah-Hartman */
360*8465def4SGreg Kroah-Hartman static struct gb_message *
gb_operation_message_alloc(struct gb_host_device * hd,u8 type,size_t payload_size,gfp_t gfp_flags)361*8465def4SGreg Kroah-Hartman gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
362*8465def4SGreg Kroah-Hartman size_t payload_size, gfp_t gfp_flags)
363*8465def4SGreg Kroah-Hartman {
364*8465def4SGreg Kroah-Hartman struct gb_message *message;
365*8465def4SGreg Kroah-Hartman struct gb_operation_msg_hdr *header;
366*8465def4SGreg Kroah-Hartman size_t message_size = payload_size + sizeof(*header);
367*8465def4SGreg Kroah-Hartman
368*8465def4SGreg Kroah-Hartman if (message_size > hd->buffer_size_max) {
369*8465def4SGreg Kroah-Hartman dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
370*8465def4SGreg Kroah-Hartman message_size, hd->buffer_size_max);
371*8465def4SGreg Kroah-Hartman return NULL;
372*8465def4SGreg Kroah-Hartman }
373*8465def4SGreg Kroah-Hartman
374*8465def4SGreg Kroah-Hartman /* Allocate the message structure and buffer. */
375*8465def4SGreg Kroah-Hartman message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
376*8465def4SGreg Kroah-Hartman if (!message)
377*8465def4SGreg Kroah-Hartman return NULL;
378*8465def4SGreg Kroah-Hartman
379*8465def4SGreg Kroah-Hartman message->buffer = kzalloc(message_size, gfp_flags);
380*8465def4SGreg Kroah-Hartman if (!message->buffer)
381*8465def4SGreg Kroah-Hartman goto err_free_message;
382*8465def4SGreg Kroah-Hartman
383*8465def4SGreg Kroah-Hartman /* Initialize the message. Operation id is filled in later. */
384*8465def4SGreg Kroah-Hartman gb_operation_message_init(hd, message, 0, payload_size, type);
385*8465def4SGreg Kroah-Hartman
386*8465def4SGreg Kroah-Hartman return message;
387*8465def4SGreg Kroah-Hartman
388*8465def4SGreg Kroah-Hartman err_free_message:
389*8465def4SGreg Kroah-Hartman kmem_cache_free(gb_message_cache, message);
390*8465def4SGreg Kroah-Hartman
391*8465def4SGreg Kroah-Hartman return NULL;
392*8465def4SGreg Kroah-Hartman }
393*8465def4SGreg Kroah-Hartman
gb_operation_message_free(struct gb_message * message)394*8465def4SGreg Kroah-Hartman static void gb_operation_message_free(struct gb_message *message)
395*8465def4SGreg Kroah-Hartman {
396*8465def4SGreg Kroah-Hartman kfree(message->buffer);
397*8465def4SGreg Kroah-Hartman kmem_cache_free(gb_message_cache, message);
398*8465def4SGreg Kroah-Hartman }
399*8465def4SGreg Kroah-Hartman
400*8465def4SGreg Kroah-Hartman /*
401*8465def4SGreg Kroah-Hartman * Map an enum gb_operation_status value (which is represented in a
402*8465def4SGreg Kroah-Hartman * message as a single byte) to an appropriate Linux negative errno.
403*8465def4SGreg Kroah-Hartman */
gb_operation_status_map(u8 status)404*8465def4SGreg Kroah-Hartman static int gb_operation_status_map(u8 status)
405*8465def4SGreg Kroah-Hartman {
406*8465def4SGreg Kroah-Hartman switch (status) {
407*8465def4SGreg Kroah-Hartman case GB_OP_SUCCESS:
408*8465def4SGreg Kroah-Hartman return 0;
409*8465def4SGreg Kroah-Hartman case GB_OP_INTERRUPTED:
410*8465def4SGreg Kroah-Hartman return -EINTR;
411*8465def4SGreg Kroah-Hartman case GB_OP_TIMEOUT:
412*8465def4SGreg Kroah-Hartman return -ETIMEDOUT;
413*8465def4SGreg Kroah-Hartman case GB_OP_NO_MEMORY:
414*8465def4SGreg Kroah-Hartman return -ENOMEM;
415*8465def4SGreg Kroah-Hartman case GB_OP_PROTOCOL_BAD:
416*8465def4SGreg Kroah-Hartman return -EPROTONOSUPPORT;
417*8465def4SGreg Kroah-Hartman case GB_OP_OVERFLOW:
418*8465def4SGreg Kroah-Hartman return -EMSGSIZE;
419*8465def4SGreg Kroah-Hartman case GB_OP_INVALID:
420*8465def4SGreg Kroah-Hartman return -EINVAL;
421*8465def4SGreg Kroah-Hartman case GB_OP_RETRY:
422*8465def4SGreg Kroah-Hartman return -EAGAIN;
423*8465def4SGreg Kroah-Hartman case GB_OP_NONEXISTENT:
424*8465def4SGreg Kroah-Hartman return -ENODEV;
425*8465def4SGreg Kroah-Hartman case GB_OP_MALFUNCTION:
426*8465def4SGreg Kroah-Hartman return -EILSEQ;
427*8465def4SGreg Kroah-Hartman case GB_OP_UNKNOWN_ERROR:
428*8465def4SGreg Kroah-Hartman default:
429*8465def4SGreg Kroah-Hartman return -EIO;
430*8465def4SGreg Kroah-Hartman }
431*8465def4SGreg Kroah-Hartman }
432*8465def4SGreg Kroah-Hartman
433*8465def4SGreg Kroah-Hartman /*
434*8465def4SGreg Kroah-Hartman * Map a Linux errno value (from operation->errno) into the value
435*8465def4SGreg Kroah-Hartman * that should represent it in a response message status sent
436*8465def4SGreg Kroah-Hartman * over the wire. Returns an enum gb_operation_status value (which
437*8465def4SGreg Kroah-Hartman * is represented in a message as a single byte).
438*8465def4SGreg Kroah-Hartman */
gb_operation_errno_map(int errno)439*8465def4SGreg Kroah-Hartman static u8 gb_operation_errno_map(int errno)
440*8465def4SGreg Kroah-Hartman {
441*8465def4SGreg Kroah-Hartman switch (errno) {
442*8465def4SGreg Kroah-Hartman case 0:
443*8465def4SGreg Kroah-Hartman return GB_OP_SUCCESS;
444*8465def4SGreg Kroah-Hartman case -EINTR:
445*8465def4SGreg Kroah-Hartman return GB_OP_INTERRUPTED;
446*8465def4SGreg Kroah-Hartman case -ETIMEDOUT:
447*8465def4SGreg Kroah-Hartman return GB_OP_TIMEOUT;
448*8465def4SGreg Kroah-Hartman case -ENOMEM:
449*8465def4SGreg Kroah-Hartman return GB_OP_NO_MEMORY;
450*8465def4SGreg Kroah-Hartman case -EPROTONOSUPPORT:
451*8465def4SGreg Kroah-Hartman return GB_OP_PROTOCOL_BAD;
452*8465def4SGreg Kroah-Hartman case -EMSGSIZE:
453*8465def4SGreg Kroah-Hartman return GB_OP_OVERFLOW; /* Could be underflow too */
454*8465def4SGreg Kroah-Hartman case -EINVAL:
455*8465def4SGreg Kroah-Hartman return GB_OP_INVALID;
456*8465def4SGreg Kroah-Hartman case -EAGAIN:
457*8465def4SGreg Kroah-Hartman return GB_OP_RETRY;
458*8465def4SGreg Kroah-Hartman case -EILSEQ:
459*8465def4SGreg Kroah-Hartman return GB_OP_MALFUNCTION;
460*8465def4SGreg Kroah-Hartman case -ENODEV:
461*8465def4SGreg Kroah-Hartman return GB_OP_NONEXISTENT;
462*8465def4SGreg Kroah-Hartman case -EIO:
463*8465def4SGreg Kroah-Hartman default:
464*8465def4SGreg Kroah-Hartman return GB_OP_UNKNOWN_ERROR;
465*8465def4SGreg Kroah-Hartman }
466*8465def4SGreg Kroah-Hartman }
467*8465def4SGreg Kroah-Hartman
gb_operation_response_alloc(struct gb_operation * operation,size_t response_size,gfp_t gfp)468*8465def4SGreg Kroah-Hartman bool gb_operation_response_alloc(struct gb_operation *operation,
469*8465def4SGreg Kroah-Hartman size_t response_size, gfp_t gfp)
470*8465def4SGreg Kroah-Hartman {
471*8465def4SGreg Kroah-Hartman struct gb_host_device *hd = operation->connection->hd;
472*8465def4SGreg Kroah-Hartman struct gb_operation_msg_hdr *request_header;
473*8465def4SGreg Kroah-Hartman struct gb_message *response;
474*8465def4SGreg Kroah-Hartman u8 type;
475*8465def4SGreg Kroah-Hartman
476*8465def4SGreg Kroah-Hartman type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
477*8465def4SGreg Kroah-Hartman response = gb_operation_message_alloc(hd, type, response_size, gfp);
478*8465def4SGreg Kroah-Hartman if (!response)
479*8465def4SGreg Kroah-Hartman return false;
480*8465def4SGreg Kroah-Hartman response->operation = operation;
481*8465def4SGreg Kroah-Hartman
482*8465def4SGreg Kroah-Hartman /*
483*8465def4SGreg Kroah-Hartman * Size and type get initialized when the message is
484*8465def4SGreg Kroah-Hartman * allocated. The errno will be set before sending. All
485*8465def4SGreg Kroah-Hartman * that's left is the operation id, which we copy from the
486*8465def4SGreg Kroah-Hartman * request message header (as-is, in little-endian order).
487*8465def4SGreg Kroah-Hartman */
488*8465def4SGreg Kroah-Hartman request_header = operation->request->header;
489*8465def4SGreg Kroah-Hartman response->header->operation_id = request_header->operation_id;
490*8465def4SGreg Kroah-Hartman operation->response = response;
491*8465def4SGreg Kroah-Hartman
492*8465def4SGreg Kroah-Hartman return true;
493*8465def4SGreg Kroah-Hartman }
494*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
495*8465def4SGreg Kroah-Hartman
496*8465def4SGreg Kroah-Hartman /*
497*8465def4SGreg Kroah-Hartman * Create a Greybus operation to be sent over the given connection.
498*8465def4SGreg Kroah-Hartman * The request buffer will be big enough for a payload of the given
499*8465def4SGreg Kroah-Hartman * size.
500*8465def4SGreg Kroah-Hartman *
501*8465def4SGreg Kroah-Hartman * For outgoing requests, the request message's header will be
502*8465def4SGreg Kroah-Hartman * initialized with the type of the request and the message size.
503*8465def4SGreg Kroah-Hartman * Outgoing operations must also specify the response buffer size,
504*8465def4SGreg Kroah-Hartman * which must be sufficient to hold all expected response data. The
505*8465def4SGreg Kroah-Hartman * response message header will eventually be overwritten, so there's
506*8465def4SGreg Kroah-Hartman * no need to initialize it here.
507*8465def4SGreg Kroah-Hartman *
508*8465def4SGreg Kroah-Hartman * Request messages for incoming operations can arrive in interrupt
509*8465def4SGreg Kroah-Hartman * context, so they must be allocated with GFP_ATOMIC. In this case
510*8465def4SGreg Kroah-Hartman * the request buffer will be immediately overwritten, so there is
511*8465def4SGreg Kroah-Hartman * no need to initialize the message header. Responsibility for
512*8465def4SGreg Kroah-Hartman * allocating a response buffer lies with the incoming request
513*8465def4SGreg Kroah-Hartman * handler for a protocol. So we don't allocate that here.
514*8465def4SGreg Kroah-Hartman *
515*8465def4SGreg Kroah-Hartman * Returns a pointer to the new operation or a null pointer if an
516*8465def4SGreg Kroah-Hartman * error occurs.
517*8465def4SGreg Kroah-Hartman */
518*8465def4SGreg Kroah-Hartman static struct gb_operation *
gb_operation_create_common(struct gb_connection * connection,u8 type,size_t request_size,size_t response_size,unsigned long op_flags,gfp_t gfp_flags)519*8465def4SGreg Kroah-Hartman gb_operation_create_common(struct gb_connection *connection, u8 type,
520*8465def4SGreg Kroah-Hartman size_t request_size, size_t response_size,
521*8465def4SGreg Kroah-Hartman unsigned long op_flags, gfp_t gfp_flags)
522*8465def4SGreg Kroah-Hartman {
523*8465def4SGreg Kroah-Hartman struct gb_host_device *hd = connection->hd;
524*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
525*8465def4SGreg Kroah-Hartman
526*8465def4SGreg Kroah-Hartman operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
527*8465def4SGreg Kroah-Hartman if (!operation)
528*8465def4SGreg Kroah-Hartman return NULL;
529*8465def4SGreg Kroah-Hartman operation->connection = connection;
530*8465def4SGreg Kroah-Hartman
531*8465def4SGreg Kroah-Hartman operation->request = gb_operation_message_alloc(hd, type, request_size,
532*8465def4SGreg Kroah-Hartman gfp_flags);
533*8465def4SGreg Kroah-Hartman if (!operation->request)
534*8465def4SGreg Kroah-Hartman goto err_cache;
535*8465def4SGreg Kroah-Hartman operation->request->operation = operation;
536*8465def4SGreg Kroah-Hartman
537*8465def4SGreg Kroah-Hartman /* Allocate the response buffer for outgoing operations */
538*8465def4SGreg Kroah-Hartman if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
539*8465def4SGreg Kroah-Hartman if (!gb_operation_response_alloc(operation, response_size,
540*8465def4SGreg Kroah-Hartman gfp_flags)) {
541*8465def4SGreg Kroah-Hartman goto err_request;
542*8465def4SGreg Kroah-Hartman }
543*8465def4SGreg Kroah-Hartman
544*8465def4SGreg Kroah-Hartman timer_setup(&operation->timer, gb_operation_timeout, 0);
545*8465def4SGreg Kroah-Hartman }
546*8465def4SGreg Kroah-Hartman
547*8465def4SGreg Kroah-Hartman operation->flags = op_flags;
548*8465def4SGreg Kroah-Hartman operation->type = type;
549*8465def4SGreg Kroah-Hartman operation->errno = -EBADR; /* Initial value--means "never set" */
550*8465def4SGreg Kroah-Hartman
551*8465def4SGreg Kroah-Hartman INIT_WORK(&operation->work, gb_operation_work);
552*8465def4SGreg Kroah-Hartman init_completion(&operation->completion);
553*8465def4SGreg Kroah-Hartman kref_init(&operation->kref);
554*8465def4SGreg Kroah-Hartman atomic_set(&operation->waiters, 0);
555*8465def4SGreg Kroah-Hartman
556*8465def4SGreg Kroah-Hartman return operation;
557*8465def4SGreg Kroah-Hartman
558*8465def4SGreg Kroah-Hartman err_request:
559*8465def4SGreg Kroah-Hartman gb_operation_message_free(operation->request);
560*8465def4SGreg Kroah-Hartman err_cache:
561*8465def4SGreg Kroah-Hartman kmem_cache_free(gb_operation_cache, operation);
562*8465def4SGreg Kroah-Hartman
563*8465def4SGreg Kroah-Hartman return NULL;
564*8465def4SGreg Kroah-Hartman }
565*8465def4SGreg Kroah-Hartman
566*8465def4SGreg Kroah-Hartman /*
567*8465def4SGreg Kroah-Hartman * Create a new operation associated with the given connection. The
568*8465def4SGreg Kroah-Hartman * request and response sizes provided are the number of bytes
569*8465def4SGreg Kroah-Hartman * required to hold the request/response payload only. Both of
570*8465def4SGreg Kroah-Hartman * these are allowed to be 0. Note that 0x00 is reserved as an
571*8465def4SGreg Kroah-Hartman * invalid operation type for all protocols, and this is enforced
572*8465def4SGreg Kroah-Hartman * here.
573*8465def4SGreg Kroah-Hartman */
574*8465def4SGreg Kroah-Hartman struct gb_operation *
gb_operation_create_flags(struct gb_connection * connection,u8 type,size_t request_size,size_t response_size,unsigned long flags,gfp_t gfp)575*8465def4SGreg Kroah-Hartman gb_operation_create_flags(struct gb_connection *connection,
576*8465def4SGreg Kroah-Hartman u8 type, size_t request_size,
577*8465def4SGreg Kroah-Hartman size_t response_size, unsigned long flags,
578*8465def4SGreg Kroah-Hartman gfp_t gfp)
579*8465def4SGreg Kroah-Hartman {
580*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
581*8465def4SGreg Kroah-Hartman
582*8465def4SGreg Kroah-Hartman if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
583*8465def4SGreg Kroah-Hartman return NULL;
584*8465def4SGreg Kroah-Hartman if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
585*8465def4SGreg Kroah-Hartman type &= ~GB_MESSAGE_TYPE_RESPONSE;
586*8465def4SGreg Kroah-Hartman
587*8465def4SGreg Kroah-Hartman if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
588*8465def4SGreg Kroah-Hartman flags &= GB_OPERATION_FLAG_USER_MASK;
589*8465def4SGreg Kroah-Hartman
590*8465def4SGreg Kroah-Hartman operation = gb_operation_create_common(connection, type,
591*8465def4SGreg Kroah-Hartman request_size, response_size,
592*8465def4SGreg Kroah-Hartman flags, gfp);
593*8465def4SGreg Kroah-Hartman if (operation)
594*8465def4SGreg Kroah-Hartman trace_gb_operation_create(operation);
595*8465def4SGreg Kroah-Hartman
596*8465def4SGreg Kroah-Hartman return operation;
597*8465def4SGreg Kroah-Hartman }
598*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_create_flags);
599*8465def4SGreg Kroah-Hartman
600*8465def4SGreg Kroah-Hartman struct gb_operation *
gb_operation_create_core(struct gb_connection * connection,u8 type,size_t request_size,size_t response_size,unsigned long flags,gfp_t gfp)601*8465def4SGreg Kroah-Hartman gb_operation_create_core(struct gb_connection *connection,
602*8465def4SGreg Kroah-Hartman u8 type, size_t request_size,
603*8465def4SGreg Kroah-Hartman size_t response_size, unsigned long flags,
604*8465def4SGreg Kroah-Hartman gfp_t gfp)
605*8465def4SGreg Kroah-Hartman {
606*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
607*8465def4SGreg Kroah-Hartman
608*8465def4SGreg Kroah-Hartman flags |= GB_OPERATION_FLAG_CORE;
609*8465def4SGreg Kroah-Hartman
610*8465def4SGreg Kroah-Hartman operation = gb_operation_create_common(connection, type,
611*8465def4SGreg Kroah-Hartman request_size, response_size,
612*8465def4SGreg Kroah-Hartman flags, gfp);
613*8465def4SGreg Kroah-Hartman if (operation)
614*8465def4SGreg Kroah-Hartman trace_gb_operation_create_core(operation);
615*8465def4SGreg Kroah-Hartman
616*8465def4SGreg Kroah-Hartman return operation;
617*8465def4SGreg Kroah-Hartman }
618*8465def4SGreg Kroah-Hartman
619*8465def4SGreg Kroah-Hartman /* Do not export this function. */
620*8465def4SGreg Kroah-Hartman
gb_operation_get_payload_size_max(struct gb_connection * connection)621*8465def4SGreg Kroah-Hartman size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
622*8465def4SGreg Kroah-Hartman {
623*8465def4SGreg Kroah-Hartman struct gb_host_device *hd = connection->hd;
624*8465def4SGreg Kroah-Hartman
625*8465def4SGreg Kroah-Hartman return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
626*8465def4SGreg Kroah-Hartman }
627*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
628*8465def4SGreg Kroah-Hartman
629*8465def4SGreg Kroah-Hartman static struct gb_operation *
gb_operation_create_incoming(struct gb_connection * connection,u16 id,u8 type,void * data,size_t size)630*8465def4SGreg Kroah-Hartman gb_operation_create_incoming(struct gb_connection *connection, u16 id,
631*8465def4SGreg Kroah-Hartman u8 type, void *data, size_t size)
632*8465def4SGreg Kroah-Hartman {
633*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
634*8465def4SGreg Kroah-Hartman size_t request_size;
635*8465def4SGreg Kroah-Hartman unsigned long flags = GB_OPERATION_FLAG_INCOMING;
636*8465def4SGreg Kroah-Hartman
637*8465def4SGreg Kroah-Hartman /* Caller has made sure we at least have a message header. */
638*8465def4SGreg Kroah-Hartman request_size = size - sizeof(struct gb_operation_msg_hdr);
639*8465def4SGreg Kroah-Hartman
640*8465def4SGreg Kroah-Hartman if (!id)
641*8465def4SGreg Kroah-Hartman flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
642*8465def4SGreg Kroah-Hartman
643*8465def4SGreg Kroah-Hartman operation = gb_operation_create_common(connection, type,
644*8465def4SGreg Kroah-Hartman request_size,
645*8465def4SGreg Kroah-Hartman GB_REQUEST_TYPE_INVALID,
646*8465def4SGreg Kroah-Hartman flags, GFP_ATOMIC);
647*8465def4SGreg Kroah-Hartman if (!operation)
648*8465def4SGreg Kroah-Hartman return NULL;
649*8465def4SGreg Kroah-Hartman
650*8465def4SGreg Kroah-Hartman operation->id = id;
651*8465def4SGreg Kroah-Hartman memcpy(operation->request->header, data, size);
652*8465def4SGreg Kroah-Hartman trace_gb_operation_create_incoming(operation);
653*8465def4SGreg Kroah-Hartman
654*8465def4SGreg Kroah-Hartman return operation;
655*8465def4SGreg Kroah-Hartman }
656*8465def4SGreg Kroah-Hartman
657*8465def4SGreg Kroah-Hartman /*
658*8465def4SGreg Kroah-Hartman * Get an additional reference on an operation.
659*8465def4SGreg Kroah-Hartman */
gb_operation_get(struct gb_operation * operation)660*8465def4SGreg Kroah-Hartman void gb_operation_get(struct gb_operation *operation)
661*8465def4SGreg Kroah-Hartman {
662*8465def4SGreg Kroah-Hartman kref_get(&operation->kref);
663*8465def4SGreg Kroah-Hartman }
664*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_get);
665*8465def4SGreg Kroah-Hartman
666*8465def4SGreg Kroah-Hartman /*
667*8465def4SGreg Kroah-Hartman * Destroy a previously created operation.
668*8465def4SGreg Kroah-Hartman */
_gb_operation_destroy(struct kref * kref)669*8465def4SGreg Kroah-Hartman static void _gb_operation_destroy(struct kref *kref)
670*8465def4SGreg Kroah-Hartman {
671*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
672*8465def4SGreg Kroah-Hartman
673*8465def4SGreg Kroah-Hartman operation = container_of(kref, struct gb_operation, kref);
674*8465def4SGreg Kroah-Hartman
675*8465def4SGreg Kroah-Hartman trace_gb_operation_destroy(operation);
676*8465def4SGreg Kroah-Hartman
677*8465def4SGreg Kroah-Hartman if (operation->response)
678*8465def4SGreg Kroah-Hartman gb_operation_message_free(operation->response);
679*8465def4SGreg Kroah-Hartman gb_operation_message_free(operation->request);
680*8465def4SGreg Kroah-Hartman
681*8465def4SGreg Kroah-Hartman kmem_cache_free(gb_operation_cache, operation);
682*8465def4SGreg Kroah-Hartman }
683*8465def4SGreg Kroah-Hartman
684*8465def4SGreg Kroah-Hartman /*
685*8465def4SGreg Kroah-Hartman * Drop a reference on an operation, and destroy it when the last
686*8465def4SGreg Kroah-Hartman * one is gone.
687*8465def4SGreg Kroah-Hartman */
gb_operation_put(struct gb_operation * operation)688*8465def4SGreg Kroah-Hartman void gb_operation_put(struct gb_operation *operation)
689*8465def4SGreg Kroah-Hartman {
690*8465def4SGreg Kroah-Hartman if (WARN_ON(!operation))
691*8465def4SGreg Kroah-Hartman return;
692*8465def4SGreg Kroah-Hartman
693*8465def4SGreg Kroah-Hartman kref_put(&operation->kref, _gb_operation_destroy);
694*8465def4SGreg Kroah-Hartman }
695*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_put);
696*8465def4SGreg Kroah-Hartman
697*8465def4SGreg Kroah-Hartman /* Tell the requester we're done */
gb_operation_sync_callback(struct gb_operation * operation)698*8465def4SGreg Kroah-Hartman static void gb_operation_sync_callback(struct gb_operation *operation)
699*8465def4SGreg Kroah-Hartman {
700*8465def4SGreg Kroah-Hartman complete(&operation->completion);
701*8465def4SGreg Kroah-Hartman }
702*8465def4SGreg Kroah-Hartman
703*8465def4SGreg Kroah-Hartman /**
704*8465def4SGreg Kroah-Hartman * gb_operation_request_send() - send an operation request message
705*8465def4SGreg Kroah-Hartman * @operation: the operation to initiate
706*8465def4SGreg Kroah-Hartman * @callback: the operation completion callback
707*8465def4SGreg Kroah-Hartman * @timeout: operation timeout in milliseconds, or zero for no timeout
708*8465def4SGreg Kroah-Hartman * @gfp: the memory flags to use for any allocations
709*8465def4SGreg Kroah-Hartman *
710*8465def4SGreg Kroah-Hartman * The caller has filled in any payload so the request message is ready to go.
711*8465def4SGreg Kroah-Hartman * The callback function supplied will be called when the response message has
712*8465def4SGreg Kroah-Hartman * arrived, a unidirectional request has been sent, or the operation is
713*8465def4SGreg Kroah-Hartman * cancelled, indicating that the operation is complete. The callback function
714*8465def4SGreg Kroah-Hartman * can fetch the result of the operation using gb_operation_result() if
715*8465def4SGreg Kroah-Hartman * desired.
716*8465def4SGreg Kroah-Hartman *
717*8465def4SGreg Kroah-Hartman * Return: 0 if the request was successfully queued in the host-driver queues,
718*8465def4SGreg Kroah-Hartman * or a negative errno.
719*8465def4SGreg Kroah-Hartman */
gb_operation_request_send(struct gb_operation * operation,gb_operation_callback callback,unsigned int timeout,gfp_t gfp)720*8465def4SGreg Kroah-Hartman int gb_operation_request_send(struct gb_operation *operation,
721*8465def4SGreg Kroah-Hartman gb_operation_callback callback,
722*8465def4SGreg Kroah-Hartman unsigned int timeout,
723*8465def4SGreg Kroah-Hartman gfp_t gfp)
724*8465def4SGreg Kroah-Hartman {
725*8465def4SGreg Kroah-Hartman struct gb_connection *connection = operation->connection;
726*8465def4SGreg Kroah-Hartman struct gb_operation_msg_hdr *header;
727*8465def4SGreg Kroah-Hartman unsigned int cycle;
728*8465def4SGreg Kroah-Hartman int ret;
729*8465def4SGreg Kroah-Hartman
730*8465def4SGreg Kroah-Hartman if (gb_connection_is_offloaded(connection))
731*8465def4SGreg Kroah-Hartman return -EBUSY;
732*8465def4SGreg Kroah-Hartman
733*8465def4SGreg Kroah-Hartman if (!callback)
734*8465def4SGreg Kroah-Hartman return -EINVAL;
735*8465def4SGreg Kroah-Hartman
736*8465def4SGreg Kroah-Hartman /*
737*8465def4SGreg Kroah-Hartman * Record the callback function, which is executed in
738*8465def4SGreg Kroah-Hartman * non-atomic (workqueue) context when the final result
739*8465def4SGreg Kroah-Hartman * of an operation has been set.
740*8465def4SGreg Kroah-Hartman */
741*8465def4SGreg Kroah-Hartman operation->callback = callback;
742*8465def4SGreg Kroah-Hartman
743*8465def4SGreg Kroah-Hartman /*
744*8465def4SGreg Kroah-Hartman * Assign the operation's id, and store it in the request header.
745*8465def4SGreg Kroah-Hartman * Zero is a reserved operation id for unidirectional operations.
746*8465def4SGreg Kroah-Hartman */
747*8465def4SGreg Kroah-Hartman if (gb_operation_is_unidirectional(operation)) {
748*8465def4SGreg Kroah-Hartman operation->id = 0;
749*8465def4SGreg Kroah-Hartman } else {
750*8465def4SGreg Kroah-Hartman cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
751*8465def4SGreg Kroah-Hartman operation->id = (u16)(cycle % U16_MAX + 1);
752*8465def4SGreg Kroah-Hartman }
753*8465def4SGreg Kroah-Hartman
754*8465def4SGreg Kroah-Hartman header = operation->request->header;
755*8465def4SGreg Kroah-Hartman header->operation_id = cpu_to_le16(operation->id);
756*8465def4SGreg Kroah-Hartman
757*8465def4SGreg Kroah-Hartman gb_operation_result_set(operation, -EINPROGRESS);
758*8465def4SGreg Kroah-Hartman
759*8465def4SGreg Kroah-Hartman /*
760*8465def4SGreg Kroah-Hartman * Get an extra reference on the operation. It'll be dropped when the
761*8465def4SGreg Kroah-Hartman * operation completes.
762*8465def4SGreg Kroah-Hartman */
763*8465def4SGreg Kroah-Hartman gb_operation_get(operation);
764*8465def4SGreg Kroah-Hartman ret = gb_operation_get_active(operation);
765*8465def4SGreg Kroah-Hartman if (ret)
766*8465def4SGreg Kroah-Hartman goto err_put;
767*8465def4SGreg Kroah-Hartman
768*8465def4SGreg Kroah-Hartman ret = gb_message_send(operation->request, gfp);
769*8465def4SGreg Kroah-Hartman if (ret)
770*8465def4SGreg Kroah-Hartman goto err_put_active;
771*8465def4SGreg Kroah-Hartman
772*8465def4SGreg Kroah-Hartman if (timeout) {
773*8465def4SGreg Kroah-Hartman operation->timer.expires = jiffies + msecs_to_jiffies(timeout);
774*8465def4SGreg Kroah-Hartman add_timer(&operation->timer);
775*8465def4SGreg Kroah-Hartman }
776*8465def4SGreg Kroah-Hartman
777*8465def4SGreg Kroah-Hartman return 0;
778*8465def4SGreg Kroah-Hartman
779*8465def4SGreg Kroah-Hartman err_put_active:
780*8465def4SGreg Kroah-Hartman gb_operation_put_active(operation);
781*8465def4SGreg Kroah-Hartman err_put:
782*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
783*8465def4SGreg Kroah-Hartman
784*8465def4SGreg Kroah-Hartman return ret;
785*8465def4SGreg Kroah-Hartman }
786*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_request_send);
787*8465def4SGreg Kroah-Hartman
788*8465def4SGreg Kroah-Hartman /*
789*8465def4SGreg Kroah-Hartman * Send a synchronous operation. This function is expected to
790*8465def4SGreg Kroah-Hartman * block, returning only when the response has arrived, (or when an
791*8465def4SGreg Kroah-Hartman * error is detected. The return value is the result of the
792*8465def4SGreg Kroah-Hartman * operation.
793*8465def4SGreg Kroah-Hartman */
gb_operation_request_send_sync_timeout(struct gb_operation * operation,unsigned int timeout)794*8465def4SGreg Kroah-Hartman int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
795*8465def4SGreg Kroah-Hartman unsigned int timeout)
796*8465def4SGreg Kroah-Hartman {
797*8465def4SGreg Kroah-Hartman int ret;
798*8465def4SGreg Kroah-Hartman
799*8465def4SGreg Kroah-Hartman ret = gb_operation_request_send(operation, gb_operation_sync_callback,
800*8465def4SGreg Kroah-Hartman timeout, GFP_KERNEL);
801*8465def4SGreg Kroah-Hartman if (ret)
802*8465def4SGreg Kroah-Hartman return ret;
803*8465def4SGreg Kroah-Hartman
804*8465def4SGreg Kroah-Hartman ret = wait_for_completion_interruptible(&operation->completion);
805*8465def4SGreg Kroah-Hartman if (ret < 0) {
806*8465def4SGreg Kroah-Hartman /* Cancel the operation if interrupted */
807*8465def4SGreg Kroah-Hartman gb_operation_cancel(operation, -ECANCELED);
808*8465def4SGreg Kroah-Hartman }
809*8465def4SGreg Kroah-Hartman
810*8465def4SGreg Kroah-Hartman return gb_operation_result(operation);
811*8465def4SGreg Kroah-Hartman }
812*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
813*8465def4SGreg Kroah-Hartman
814*8465def4SGreg Kroah-Hartman /*
815*8465def4SGreg Kroah-Hartman * Send a response for an incoming operation request. A non-zero
816*8465def4SGreg Kroah-Hartman * errno indicates a failed operation.
817*8465def4SGreg Kroah-Hartman *
818*8465def4SGreg Kroah-Hartman * If there is any response payload, the incoming request handler is
819*8465def4SGreg Kroah-Hartman * responsible for allocating the response message. Otherwise the
820*8465def4SGreg Kroah-Hartman * it can simply supply the result errno; this function will
821*8465def4SGreg Kroah-Hartman * allocate the response message if necessary.
822*8465def4SGreg Kroah-Hartman */
gb_operation_response_send(struct gb_operation * operation,int errno)823*8465def4SGreg Kroah-Hartman static int gb_operation_response_send(struct gb_operation *operation,
824*8465def4SGreg Kroah-Hartman int errno)
825*8465def4SGreg Kroah-Hartman {
826*8465def4SGreg Kroah-Hartman struct gb_connection *connection = operation->connection;
827*8465def4SGreg Kroah-Hartman int ret;
828*8465def4SGreg Kroah-Hartman
829*8465def4SGreg Kroah-Hartman if (!operation->response &&
830*8465def4SGreg Kroah-Hartman !gb_operation_is_unidirectional(operation)) {
831*8465def4SGreg Kroah-Hartman if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
832*8465def4SGreg Kroah-Hartman return -ENOMEM;
833*8465def4SGreg Kroah-Hartman }
834*8465def4SGreg Kroah-Hartman
835*8465def4SGreg Kroah-Hartman /* Record the result */
836*8465def4SGreg Kroah-Hartman if (!gb_operation_result_set(operation, errno)) {
837*8465def4SGreg Kroah-Hartman dev_err(&connection->hd->dev, "request result already set\n");
838*8465def4SGreg Kroah-Hartman return -EIO; /* Shouldn't happen */
839*8465def4SGreg Kroah-Hartman }
840*8465def4SGreg Kroah-Hartman
841*8465def4SGreg Kroah-Hartman /* Sender of request does not care about response. */
842*8465def4SGreg Kroah-Hartman if (gb_operation_is_unidirectional(operation))
843*8465def4SGreg Kroah-Hartman return 0;
844*8465def4SGreg Kroah-Hartman
845*8465def4SGreg Kroah-Hartman /* Reference will be dropped when message has been sent. */
846*8465def4SGreg Kroah-Hartman gb_operation_get(operation);
847*8465def4SGreg Kroah-Hartman ret = gb_operation_get_active(operation);
848*8465def4SGreg Kroah-Hartman if (ret)
849*8465def4SGreg Kroah-Hartman goto err_put;
850*8465def4SGreg Kroah-Hartman
851*8465def4SGreg Kroah-Hartman /* Fill in the response header and send it */
852*8465def4SGreg Kroah-Hartman operation->response->header->result = gb_operation_errno_map(errno);
853*8465def4SGreg Kroah-Hartman
854*8465def4SGreg Kroah-Hartman ret = gb_message_send(operation->response, GFP_KERNEL);
855*8465def4SGreg Kroah-Hartman if (ret)
856*8465def4SGreg Kroah-Hartman goto err_put_active;
857*8465def4SGreg Kroah-Hartman
858*8465def4SGreg Kroah-Hartman return 0;
859*8465def4SGreg Kroah-Hartman
860*8465def4SGreg Kroah-Hartman err_put_active:
861*8465def4SGreg Kroah-Hartman gb_operation_put_active(operation);
862*8465def4SGreg Kroah-Hartman err_put:
863*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
864*8465def4SGreg Kroah-Hartman
865*8465def4SGreg Kroah-Hartman return ret;
866*8465def4SGreg Kroah-Hartman }
867*8465def4SGreg Kroah-Hartman
868*8465def4SGreg Kroah-Hartman /*
869*8465def4SGreg Kroah-Hartman * This function is called when a message send request has completed.
870*8465def4SGreg Kroah-Hartman */
greybus_message_sent(struct gb_host_device * hd,struct gb_message * message,int status)871*8465def4SGreg Kroah-Hartman void greybus_message_sent(struct gb_host_device *hd,
872*8465def4SGreg Kroah-Hartman struct gb_message *message, int status)
873*8465def4SGreg Kroah-Hartman {
874*8465def4SGreg Kroah-Hartman struct gb_operation *operation = message->operation;
875*8465def4SGreg Kroah-Hartman struct gb_connection *connection = operation->connection;
876*8465def4SGreg Kroah-Hartman
877*8465def4SGreg Kroah-Hartman /*
878*8465def4SGreg Kroah-Hartman * If the message was a response, we just need to drop our
879*8465def4SGreg Kroah-Hartman * reference to the operation. If an error occurred, report
880*8465def4SGreg Kroah-Hartman * it.
881*8465def4SGreg Kroah-Hartman *
882*8465def4SGreg Kroah-Hartman * For requests, if there's no error and the operation in not
883*8465def4SGreg Kroah-Hartman * unidirectional, there's nothing more to do until the response
884*8465def4SGreg Kroah-Hartman * arrives. If an error occurred attempting to send it, or if the
885*8465def4SGreg Kroah-Hartman * operation is unidrectional, record the result of the operation and
886*8465def4SGreg Kroah-Hartman * schedule its completion.
887*8465def4SGreg Kroah-Hartman */
888*8465def4SGreg Kroah-Hartman if (message == operation->response) {
889*8465def4SGreg Kroah-Hartman if (status) {
890*8465def4SGreg Kroah-Hartman dev_err(&connection->hd->dev,
891*8465def4SGreg Kroah-Hartman "%s: error sending response 0x%02x: %d\n",
892*8465def4SGreg Kroah-Hartman connection->name, operation->type, status);
893*8465def4SGreg Kroah-Hartman }
894*8465def4SGreg Kroah-Hartman
895*8465def4SGreg Kroah-Hartman gb_operation_put_active(operation);
896*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
897*8465def4SGreg Kroah-Hartman } else if (status || gb_operation_is_unidirectional(operation)) {
898*8465def4SGreg Kroah-Hartman if (gb_operation_result_set(operation, status)) {
899*8465def4SGreg Kroah-Hartman queue_work(gb_operation_completion_wq,
900*8465def4SGreg Kroah-Hartman &operation->work);
901*8465def4SGreg Kroah-Hartman }
902*8465def4SGreg Kroah-Hartman }
903*8465def4SGreg Kroah-Hartman }
904*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(greybus_message_sent);
905*8465def4SGreg Kroah-Hartman
906*8465def4SGreg Kroah-Hartman /*
907*8465def4SGreg Kroah-Hartman * We've received data on a connection, and it doesn't look like a
908*8465def4SGreg Kroah-Hartman * response, so we assume it's a request.
909*8465def4SGreg Kroah-Hartman *
910*8465def4SGreg Kroah-Hartman * This is called in interrupt context, so just copy the incoming
911*8465def4SGreg Kroah-Hartman * data into the request buffer and handle the rest via workqueue.
912*8465def4SGreg Kroah-Hartman */
gb_connection_recv_request(struct gb_connection * connection,const struct gb_operation_msg_hdr * header,void * data,size_t size)913*8465def4SGreg Kroah-Hartman static void gb_connection_recv_request(struct gb_connection *connection,
914*8465def4SGreg Kroah-Hartman const struct gb_operation_msg_hdr *header,
915*8465def4SGreg Kroah-Hartman void *data, size_t size)
916*8465def4SGreg Kroah-Hartman {
917*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
918*8465def4SGreg Kroah-Hartman u16 operation_id;
919*8465def4SGreg Kroah-Hartman u8 type;
920*8465def4SGreg Kroah-Hartman int ret;
921*8465def4SGreg Kroah-Hartman
922*8465def4SGreg Kroah-Hartman operation_id = le16_to_cpu(header->operation_id);
923*8465def4SGreg Kroah-Hartman type = header->type;
924*8465def4SGreg Kroah-Hartman
925*8465def4SGreg Kroah-Hartman operation = gb_operation_create_incoming(connection, operation_id,
926*8465def4SGreg Kroah-Hartman type, data, size);
927*8465def4SGreg Kroah-Hartman if (!operation) {
928*8465def4SGreg Kroah-Hartman dev_err(&connection->hd->dev,
929*8465def4SGreg Kroah-Hartman "%s: can't create incoming operation\n",
930*8465def4SGreg Kroah-Hartman connection->name);
931*8465def4SGreg Kroah-Hartman return;
932*8465def4SGreg Kroah-Hartman }
933*8465def4SGreg Kroah-Hartman
934*8465def4SGreg Kroah-Hartman ret = gb_operation_get_active(operation);
935*8465def4SGreg Kroah-Hartman if (ret) {
936*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
937*8465def4SGreg Kroah-Hartman return;
938*8465def4SGreg Kroah-Hartman }
939*8465def4SGreg Kroah-Hartman trace_gb_message_recv_request(operation->request);
940*8465def4SGreg Kroah-Hartman
941*8465def4SGreg Kroah-Hartman /*
942*8465def4SGreg Kroah-Hartman * The initial reference to the operation will be dropped when the
943*8465def4SGreg Kroah-Hartman * request handler returns.
944*8465def4SGreg Kroah-Hartman */
945*8465def4SGreg Kroah-Hartman if (gb_operation_result_set(operation, -EINPROGRESS))
946*8465def4SGreg Kroah-Hartman queue_work(connection->wq, &operation->work);
947*8465def4SGreg Kroah-Hartman }
948*8465def4SGreg Kroah-Hartman
949*8465def4SGreg Kroah-Hartman /*
950*8465def4SGreg Kroah-Hartman * We've received data that appears to be an operation response
951*8465def4SGreg Kroah-Hartman * message. Look up the operation, and record that we've received
952*8465def4SGreg Kroah-Hartman * its response.
953*8465def4SGreg Kroah-Hartman *
954*8465def4SGreg Kroah-Hartman * This is called in interrupt context, so just copy the incoming
955*8465def4SGreg Kroah-Hartman * data into the response buffer and handle the rest via workqueue.
956*8465def4SGreg Kroah-Hartman */
gb_connection_recv_response(struct gb_connection * connection,const struct gb_operation_msg_hdr * header,void * data,size_t size)957*8465def4SGreg Kroah-Hartman static void gb_connection_recv_response(struct gb_connection *connection,
958*8465def4SGreg Kroah-Hartman const struct gb_operation_msg_hdr *header,
959*8465def4SGreg Kroah-Hartman void *data, size_t size)
960*8465def4SGreg Kroah-Hartman {
961*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
962*8465def4SGreg Kroah-Hartman struct gb_message *message;
963*8465def4SGreg Kroah-Hartman size_t message_size;
964*8465def4SGreg Kroah-Hartman u16 operation_id;
965*8465def4SGreg Kroah-Hartman int errno;
966*8465def4SGreg Kroah-Hartman
967*8465def4SGreg Kroah-Hartman operation_id = le16_to_cpu(header->operation_id);
968*8465def4SGreg Kroah-Hartman
969*8465def4SGreg Kroah-Hartman if (!operation_id) {
970*8465def4SGreg Kroah-Hartman dev_err_ratelimited(&connection->hd->dev,
971*8465def4SGreg Kroah-Hartman "%s: invalid response id 0 received\n",
972*8465def4SGreg Kroah-Hartman connection->name);
973*8465def4SGreg Kroah-Hartman return;
974*8465def4SGreg Kroah-Hartman }
975*8465def4SGreg Kroah-Hartman
976*8465def4SGreg Kroah-Hartman operation = gb_operation_find_outgoing(connection, operation_id);
977*8465def4SGreg Kroah-Hartman if (!operation) {
978*8465def4SGreg Kroah-Hartman dev_err_ratelimited(&connection->hd->dev,
979*8465def4SGreg Kroah-Hartman "%s: unexpected response id 0x%04x received\n",
980*8465def4SGreg Kroah-Hartman connection->name, operation_id);
981*8465def4SGreg Kroah-Hartman return;
982*8465def4SGreg Kroah-Hartman }
983*8465def4SGreg Kroah-Hartman
984*8465def4SGreg Kroah-Hartman errno = gb_operation_status_map(header->result);
985*8465def4SGreg Kroah-Hartman message = operation->response;
986*8465def4SGreg Kroah-Hartman message_size = sizeof(*header) + message->payload_size;
987*8465def4SGreg Kroah-Hartman if (!errno && size > message_size) {
988*8465def4SGreg Kroah-Hartman dev_err_ratelimited(&connection->hd->dev,
989*8465def4SGreg Kroah-Hartman "%s: malformed response 0x%02x received (%zu > %zu)\n",
990*8465def4SGreg Kroah-Hartman connection->name, header->type,
991*8465def4SGreg Kroah-Hartman size, message_size);
992*8465def4SGreg Kroah-Hartman errno = -EMSGSIZE;
993*8465def4SGreg Kroah-Hartman } else if (!errno && size < message_size) {
994*8465def4SGreg Kroah-Hartman if (gb_operation_short_response_allowed(operation)) {
995*8465def4SGreg Kroah-Hartman message->payload_size = size - sizeof(*header);
996*8465def4SGreg Kroah-Hartman } else {
997*8465def4SGreg Kroah-Hartman dev_err_ratelimited(&connection->hd->dev,
998*8465def4SGreg Kroah-Hartman "%s: short response 0x%02x received (%zu < %zu)\n",
999*8465def4SGreg Kroah-Hartman connection->name, header->type,
1000*8465def4SGreg Kroah-Hartman size, message_size);
1001*8465def4SGreg Kroah-Hartman errno = -EMSGSIZE;
1002*8465def4SGreg Kroah-Hartman }
1003*8465def4SGreg Kroah-Hartman }
1004*8465def4SGreg Kroah-Hartman
1005*8465def4SGreg Kroah-Hartman /* We must ignore the payload if a bad status is returned */
1006*8465def4SGreg Kroah-Hartman if (errno)
1007*8465def4SGreg Kroah-Hartman size = sizeof(*header);
1008*8465def4SGreg Kroah-Hartman
1009*8465def4SGreg Kroah-Hartman /* The rest will be handled in work queue context */
1010*8465def4SGreg Kroah-Hartman if (gb_operation_result_set(operation, errno)) {
1011*8465def4SGreg Kroah-Hartman memcpy(message->buffer, data, size);
1012*8465def4SGreg Kroah-Hartman
1013*8465def4SGreg Kroah-Hartman trace_gb_message_recv_response(message);
1014*8465def4SGreg Kroah-Hartman
1015*8465def4SGreg Kroah-Hartman queue_work(gb_operation_completion_wq, &operation->work);
1016*8465def4SGreg Kroah-Hartman }
1017*8465def4SGreg Kroah-Hartman
1018*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
1019*8465def4SGreg Kroah-Hartman }
1020*8465def4SGreg Kroah-Hartman
1021*8465def4SGreg Kroah-Hartman /*
1022*8465def4SGreg Kroah-Hartman * Handle data arriving on a connection. As soon as we return the
1023*8465def4SGreg Kroah-Hartman * supplied data buffer will be reused (so unless we do something
1024*8465def4SGreg Kroah-Hartman * with, it's effectively dropped).
1025*8465def4SGreg Kroah-Hartman */
gb_connection_recv(struct gb_connection * connection,void * data,size_t size)1026*8465def4SGreg Kroah-Hartman void gb_connection_recv(struct gb_connection *connection,
1027*8465def4SGreg Kroah-Hartman void *data, size_t size)
1028*8465def4SGreg Kroah-Hartman {
1029*8465def4SGreg Kroah-Hartman struct gb_operation_msg_hdr header;
1030*8465def4SGreg Kroah-Hartman struct device *dev = &connection->hd->dev;
1031*8465def4SGreg Kroah-Hartman size_t msg_size;
1032*8465def4SGreg Kroah-Hartman
1033*8465def4SGreg Kroah-Hartman if (connection->state == GB_CONNECTION_STATE_DISABLED ||
1034*8465def4SGreg Kroah-Hartman gb_connection_is_offloaded(connection)) {
1035*8465def4SGreg Kroah-Hartman dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
1036*8465def4SGreg Kroah-Hartman connection->name, size);
1037*8465def4SGreg Kroah-Hartman return;
1038*8465def4SGreg Kroah-Hartman }
1039*8465def4SGreg Kroah-Hartman
1040*8465def4SGreg Kroah-Hartman if (size < sizeof(header)) {
1041*8465def4SGreg Kroah-Hartman dev_err_ratelimited(dev, "%s: short message received\n",
1042*8465def4SGreg Kroah-Hartman connection->name);
1043*8465def4SGreg Kroah-Hartman return;
1044*8465def4SGreg Kroah-Hartman }
1045*8465def4SGreg Kroah-Hartman
1046*8465def4SGreg Kroah-Hartman /* Use memcpy as data may be unaligned */
1047*8465def4SGreg Kroah-Hartman memcpy(&header, data, sizeof(header));
1048*8465def4SGreg Kroah-Hartman msg_size = le16_to_cpu(header.size);
1049*8465def4SGreg Kroah-Hartman if (size < msg_size) {
1050*8465def4SGreg Kroah-Hartman dev_err_ratelimited(dev,
1051*8465def4SGreg Kroah-Hartman "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
1052*8465def4SGreg Kroah-Hartman connection->name,
1053*8465def4SGreg Kroah-Hartman le16_to_cpu(header.operation_id),
1054*8465def4SGreg Kroah-Hartman header.type, size, msg_size);
1055*8465def4SGreg Kroah-Hartman return; /* XXX Should still complete operation */
1056*8465def4SGreg Kroah-Hartman }
1057*8465def4SGreg Kroah-Hartman
1058*8465def4SGreg Kroah-Hartman if (header.type & GB_MESSAGE_TYPE_RESPONSE) {
1059*8465def4SGreg Kroah-Hartman gb_connection_recv_response(connection, &header, data,
1060*8465def4SGreg Kroah-Hartman msg_size);
1061*8465def4SGreg Kroah-Hartman } else {
1062*8465def4SGreg Kroah-Hartman gb_connection_recv_request(connection, &header, data,
1063*8465def4SGreg Kroah-Hartman msg_size);
1064*8465def4SGreg Kroah-Hartman }
1065*8465def4SGreg Kroah-Hartman }
1066*8465def4SGreg Kroah-Hartman
1067*8465def4SGreg Kroah-Hartman /*
1068*8465def4SGreg Kroah-Hartman * Cancel an outgoing operation synchronously, and record the given error to
1069*8465def4SGreg Kroah-Hartman * indicate why.
1070*8465def4SGreg Kroah-Hartman */
gb_operation_cancel(struct gb_operation * operation,int errno)1071*8465def4SGreg Kroah-Hartman void gb_operation_cancel(struct gb_operation *operation, int errno)
1072*8465def4SGreg Kroah-Hartman {
1073*8465def4SGreg Kroah-Hartman if (WARN_ON(gb_operation_is_incoming(operation)))
1074*8465def4SGreg Kroah-Hartman return;
1075*8465def4SGreg Kroah-Hartman
1076*8465def4SGreg Kroah-Hartman if (gb_operation_result_set(operation, errno)) {
1077*8465def4SGreg Kroah-Hartman gb_message_cancel(operation->request);
1078*8465def4SGreg Kroah-Hartman queue_work(gb_operation_completion_wq, &operation->work);
1079*8465def4SGreg Kroah-Hartman }
1080*8465def4SGreg Kroah-Hartman trace_gb_message_cancel_outgoing(operation->request);
1081*8465def4SGreg Kroah-Hartman
1082*8465def4SGreg Kroah-Hartman atomic_inc(&operation->waiters);
1083*8465def4SGreg Kroah-Hartman wait_event(gb_operation_cancellation_queue,
1084*8465def4SGreg Kroah-Hartman !gb_operation_is_active(operation));
1085*8465def4SGreg Kroah-Hartman atomic_dec(&operation->waiters);
1086*8465def4SGreg Kroah-Hartman }
1087*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_cancel);
1088*8465def4SGreg Kroah-Hartman
1089*8465def4SGreg Kroah-Hartman /*
1090*8465def4SGreg Kroah-Hartman * Cancel an incoming operation synchronously. Called during connection tear
1091*8465def4SGreg Kroah-Hartman * down.
1092*8465def4SGreg Kroah-Hartman */
gb_operation_cancel_incoming(struct gb_operation * operation,int errno)1093*8465def4SGreg Kroah-Hartman void gb_operation_cancel_incoming(struct gb_operation *operation, int errno)
1094*8465def4SGreg Kroah-Hartman {
1095*8465def4SGreg Kroah-Hartman if (WARN_ON(!gb_operation_is_incoming(operation)))
1096*8465def4SGreg Kroah-Hartman return;
1097*8465def4SGreg Kroah-Hartman
1098*8465def4SGreg Kroah-Hartman if (!gb_operation_is_unidirectional(operation)) {
1099*8465def4SGreg Kroah-Hartman /*
1100*8465def4SGreg Kroah-Hartman * Make sure the request handler has submitted the response
1101*8465def4SGreg Kroah-Hartman * before cancelling it.
1102*8465def4SGreg Kroah-Hartman */
1103*8465def4SGreg Kroah-Hartman flush_work(&operation->work);
1104*8465def4SGreg Kroah-Hartman if (!gb_operation_result_set(operation, errno))
1105*8465def4SGreg Kroah-Hartman gb_message_cancel(operation->response);
1106*8465def4SGreg Kroah-Hartman }
1107*8465def4SGreg Kroah-Hartman trace_gb_message_cancel_incoming(operation->response);
1108*8465def4SGreg Kroah-Hartman
1109*8465def4SGreg Kroah-Hartman atomic_inc(&operation->waiters);
1110*8465def4SGreg Kroah-Hartman wait_event(gb_operation_cancellation_queue,
1111*8465def4SGreg Kroah-Hartman !gb_operation_is_active(operation));
1112*8465def4SGreg Kroah-Hartman atomic_dec(&operation->waiters);
1113*8465def4SGreg Kroah-Hartman }
1114*8465def4SGreg Kroah-Hartman
1115*8465def4SGreg Kroah-Hartman /**
1116*8465def4SGreg Kroah-Hartman * gb_operation_sync_timeout() - implement a "simple" synchronous operation
1117*8465def4SGreg Kroah-Hartman * @connection: the Greybus connection to send this to
1118*8465def4SGreg Kroah-Hartman * @type: the type of operation to send
1119*8465def4SGreg Kroah-Hartman * @request: pointer to a memory buffer to copy the request from
1120*8465def4SGreg Kroah-Hartman * @request_size: size of @request
1121*8465def4SGreg Kroah-Hartman * @response: pointer to a memory buffer to copy the response to
1122*8465def4SGreg Kroah-Hartman * @response_size: the size of @response.
1123*8465def4SGreg Kroah-Hartman * @timeout: operation timeout in milliseconds
1124*8465def4SGreg Kroah-Hartman *
1125*8465def4SGreg Kroah-Hartman * This function implements a simple synchronous Greybus operation. It sends
1126*8465def4SGreg Kroah-Hartman * the provided operation request and waits (sleeps) until the corresponding
1127*8465def4SGreg Kroah-Hartman * operation response message has been successfully received, or an error
1128*8465def4SGreg Kroah-Hartman * occurs. @request and @response are buffers to hold the request and response
1129*8465def4SGreg Kroah-Hartman * data respectively, and if they are not NULL, their size must be specified in
1130*8465def4SGreg Kroah-Hartman * @request_size and @response_size.
1131*8465def4SGreg Kroah-Hartman *
1132*8465def4SGreg Kroah-Hartman * If a response payload is to come back, and @response is not NULL,
1133*8465def4SGreg Kroah-Hartman * @response_size number of bytes will be copied into @response if the operation
1134*8465def4SGreg Kroah-Hartman * is successful.
1135*8465def4SGreg Kroah-Hartman *
1136*8465def4SGreg Kroah-Hartman * If there is an error, the response buffer is left alone.
1137*8465def4SGreg Kroah-Hartman */
gb_operation_sync_timeout(struct gb_connection * connection,int type,void * request,int request_size,void * response,int response_size,unsigned int timeout)1138*8465def4SGreg Kroah-Hartman int gb_operation_sync_timeout(struct gb_connection *connection, int type,
1139*8465def4SGreg Kroah-Hartman void *request, int request_size,
1140*8465def4SGreg Kroah-Hartman void *response, int response_size,
1141*8465def4SGreg Kroah-Hartman unsigned int timeout)
1142*8465def4SGreg Kroah-Hartman {
1143*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
1144*8465def4SGreg Kroah-Hartman int ret;
1145*8465def4SGreg Kroah-Hartman
1146*8465def4SGreg Kroah-Hartman if ((response_size && !response) ||
1147*8465def4SGreg Kroah-Hartman (request_size && !request))
1148*8465def4SGreg Kroah-Hartman return -EINVAL;
1149*8465def4SGreg Kroah-Hartman
1150*8465def4SGreg Kroah-Hartman operation = gb_operation_create(connection, type,
1151*8465def4SGreg Kroah-Hartman request_size, response_size,
1152*8465def4SGreg Kroah-Hartman GFP_KERNEL);
1153*8465def4SGreg Kroah-Hartman if (!operation)
1154*8465def4SGreg Kroah-Hartman return -ENOMEM;
1155*8465def4SGreg Kroah-Hartman
1156*8465def4SGreg Kroah-Hartman if (request_size)
1157*8465def4SGreg Kroah-Hartman memcpy(operation->request->payload, request, request_size);
1158*8465def4SGreg Kroah-Hartman
1159*8465def4SGreg Kroah-Hartman ret = gb_operation_request_send_sync_timeout(operation, timeout);
1160*8465def4SGreg Kroah-Hartman if (ret) {
1161*8465def4SGreg Kroah-Hartman dev_err(&connection->hd->dev,
1162*8465def4SGreg Kroah-Hartman "%s: synchronous operation id 0x%04x of type 0x%02x failed: %d\n",
1163*8465def4SGreg Kroah-Hartman connection->name, operation->id, type, ret);
1164*8465def4SGreg Kroah-Hartman } else {
1165*8465def4SGreg Kroah-Hartman if (response_size) {
1166*8465def4SGreg Kroah-Hartman memcpy(response, operation->response->payload,
1167*8465def4SGreg Kroah-Hartman response_size);
1168*8465def4SGreg Kroah-Hartman }
1169*8465def4SGreg Kroah-Hartman }
1170*8465def4SGreg Kroah-Hartman
1171*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
1172*8465def4SGreg Kroah-Hartman
1173*8465def4SGreg Kroah-Hartman return ret;
1174*8465def4SGreg Kroah-Hartman }
1175*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
1176*8465def4SGreg Kroah-Hartman
1177*8465def4SGreg Kroah-Hartman /**
1178*8465def4SGreg Kroah-Hartman * gb_operation_unidirectional_timeout() - initiate a unidirectional operation
1179*8465def4SGreg Kroah-Hartman * @connection: connection to use
1180*8465def4SGreg Kroah-Hartman * @type: type of operation to send
1181*8465def4SGreg Kroah-Hartman * @request: memory buffer to copy the request from
1182*8465def4SGreg Kroah-Hartman * @request_size: size of @request
1183*8465def4SGreg Kroah-Hartman * @timeout: send timeout in milliseconds
1184*8465def4SGreg Kroah-Hartman *
1185*8465def4SGreg Kroah-Hartman * Initiate a unidirectional operation by sending a request message and
1186*8465def4SGreg Kroah-Hartman * waiting for it to be acknowledged as sent by the host device.
1187*8465def4SGreg Kroah-Hartman *
1188*8465def4SGreg Kroah-Hartman * Note that successful send of a unidirectional operation does not imply that
1189*8465def4SGreg Kroah-Hartman * the request as actually reached the remote end of the connection.
1190*8465def4SGreg Kroah-Hartman */
gb_operation_unidirectional_timeout(struct gb_connection * connection,int type,void * request,int request_size,unsigned int timeout)1191*8465def4SGreg Kroah-Hartman int gb_operation_unidirectional_timeout(struct gb_connection *connection,
1192*8465def4SGreg Kroah-Hartman int type, void *request,
1193*8465def4SGreg Kroah-Hartman int request_size,
1194*8465def4SGreg Kroah-Hartman unsigned int timeout)
1195*8465def4SGreg Kroah-Hartman {
1196*8465def4SGreg Kroah-Hartman struct gb_operation *operation;
1197*8465def4SGreg Kroah-Hartman int ret;
1198*8465def4SGreg Kroah-Hartman
1199*8465def4SGreg Kroah-Hartman if (request_size && !request)
1200*8465def4SGreg Kroah-Hartman return -EINVAL;
1201*8465def4SGreg Kroah-Hartman
1202*8465def4SGreg Kroah-Hartman operation = gb_operation_create_flags(connection, type,
1203*8465def4SGreg Kroah-Hartman request_size, 0,
1204*8465def4SGreg Kroah-Hartman GB_OPERATION_FLAG_UNIDIRECTIONAL,
1205*8465def4SGreg Kroah-Hartman GFP_KERNEL);
1206*8465def4SGreg Kroah-Hartman if (!operation)
1207*8465def4SGreg Kroah-Hartman return -ENOMEM;
1208*8465def4SGreg Kroah-Hartman
1209*8465def4SGreg Kroah-Hartman if (request_size)
1210*8465def4SGreg Kroah-Hartman memcpy(operation->request->payload, request, request_size);
1211*8465def4SGreg Kroah-Hartman
1212*8465def4SGreg Kroah-Hartman ret = gb_operation_request_send_sync_timeout(operation, timeout);
1213*8465def4SGreg Kroah-Hartman if (ret) {
1214*8465def4SGreg Kroah-Hartman dev_err(&connection->hd->dev,
1215*8465def4SGreg Kroah-Hartman "%s: unidirectional operation of type 0x%02x failed: %d\n",
1216*8465def4SGreg Kroah-Hartman connection->name, type, ret);
1217*8465def4SGreg Kroah-Hartman }
1218*8465def4SGreg Kroah-Hartman
1219*8465def4SGreg Kroah-Hartman gb_operation_put(operation);
1220*8465def4SGreg Kroah-Hartman
1221*8465def4SGreg Kroah-Hartman return ret;
1222*8465def4SGreg Kroah-Hartman }
1223*8465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
1224*8465def4SGreg Kroah-Hartman
gb_operation_init(void)1225*8465def4SGreg Kroah-Hartman int __init gb_operation_init(void)
1226*8465def4SGreg Kroah-Hartman {
1227*8465def4SGreg Kroah-Hartman gb_message_cache = kmem_cache_create("gb_message_cache",
1228*8465def4SGreg Kroah-Hartman sizeof(struct gb_message), 0, 0,
1229*8465def4SGreg Kroah-Hartman NULL);
1230*8465def4SGreg Kroah-Hartman if (!gb_message_cache)
1231*8465def4SGreg Kroah-Hartman return -ENOMEM;
1232*8465def4SGreg Kroah-Hartman
1233*8465def4SGreg Kroah-Hartman gb_operation_cache = kmem_cache_create("gb_operation_cache",
1234*8465def4SGreg Kroah-Hartman sizeof(struct gb_operation), 0,
1235*8465def4SGreg Kroah-Hartman 0, NULL);
1236*8465def4SGreg Kroah-Hartman if (!gb_operation_cache)
1237*8465def4SGreg Kroah-Hartman goto err_destroy_message_cache;
1238*8465def4SGreg Kroah-Hartman
1239*8465def4SGreg Kroah-Hartman gb_operation_completion_wq = alloc_workqueue("greybus_completion",
1240*8465def4SGreg Kroah-Hartman 0, 0);
1241*8465def4SGreg Kroah-Hartman if (!gb_operation_completion_wq)
1242*8465def4SGreg Kroah-Hartman goto err_destroy_operation_cache;
1243*8465def4SGreg Kroah-Hartman
1244*8465def4SGreg Kroah-Hartman return 0;
1245*8465def4SGreg Kroah-Hartman
1246*8465def4SGreg Kroah-Hartman err_destroy_operation_cache:
1247*8465def4SGreg Kroah-Hartman kmem_cache_destroy(gb_operation_cache);
1248*8465def4SGreg Kroah-Hartman gb_operation_cache = NULL;
1249*8465def4SGreg Kroah-Hartman err_destroy_message_cache:
1250*8465def4SGreg Kroah-Hartman kmem_cache_destroy(gb_message_cache);
1251*8465def4SGreg Kroah-Hartman gb_message_cache = NULL;
1252*8465def4SGreg Kroah-Hartman
1253*8465def4SGreg Kroah-Hartman return -ENOMEM;
1254*8465def4SGreg Kroah-Hartman }
1255*8465def4SGreg Kroah-Hartman
gb_operation_exit(void)1256*8465def4SGreg Kroah-Hartman void gb_operation_exit(void)
1257*8465def4SGreg Kroah-Hartman {
1258*8465def4SGreg Kroah-Hartman destroy_workqueue(gb_operation_completion_wq);
1259*8465def4SGreg Kroah-Hartman gb_operation_completion_wq = NULL;
1260*8465def4SGreg Kroah-Hartman kmem_cache_destroy(gb_operation_cache);
1261*8465def4SGreg Kroah-Hartman gb_operation_cache = NULL;
1262*8465def4SGreg Kroah-Hartman kmem_cache_destroy(gb_message_cache);
1263*8465def4SGreg Kroah-Hartman gb_message_cache = NULL;
1264*8465def4SGreg Kroah-Hartman }
1265