uwb: add basic radio manager
[safe/jmp/linux-2.6] / drivers / uwb / drp.c
1 /*
2  * Ultra Wide Band
3  * Dynamic Reservation Protocol handling
4  *
5  * Copyright (C) 2005-2006 Intel Corporation
6  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7  * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version
11  * 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 #include <linux/kthread.h>
22 #include <linux/freezer.h>
23 #include <linux/delay.h>
24 #include "uwb-internal.h"
25
26 /**
27  * Construct and send the SET DRP IE
28  *
29  * @rc:         UWB Host controller
30  * @returns:    >= 0 number of bytes still available in the beacon
31  *              < 0 errno code on error.
32  *
33  * See WUSB[8.6.2.7]: The host must set all the DRP IEs that it wants the
34  * device to include in its beacon at the same time. We thus have to
35  * traverse all reservations and include the DRP IEs of all PENDING
36  * and NEGOTIATED reservations in a SET DRP command for transmission.
37  *
38  * A DRP Availability IE is appended.
39  *
40  * rc->rsvs_mutex is held
41  *
42  * FIXME We currently ignore the returned value indicating the remaining space
43  * in beacon. This could be used to deny reservation requests earlier if
44  * determined that they would cause the beacon space to be exceeded.
45  */
46 int uwb_rc_send_all_drp_ie(struct uwb_rc *rc)
47 {
48         int result;
49         struct device *dev = &rc->uwb_dev.dev;
50         struct uwb_rc_cmd_set_drp_ie *cmd;
51         struct uwb_rc_evt_set_drp_ie reply;
52         struct uwb_rsv *rsv;
53         int num_bytes = 0;
54         u8 *IEDataptr;
55
56         result = -ENOMEM;
57         /* First traverse all reservations to determine memory needed. */
58         list_for_each_entry(rsv, &rc->reservations, rc_node) {
59                 if (rsv->drp_ie != NULL)
60                         num_bytes += rsv->drp_ie->hdr.length + 2;
61         }
62         num_bytes += sizeof(rc->drp_avail.ie);
63         cmd = kzalloc(sizeof(*cmd) + num_bytes, GFP_KERNEL);
64         if (cmd == NULL)
65                 goto error;
66         cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
67         cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SET_DRP_IE);
68         cmd->wIELength = num_bytes;
69         IEDataptr = (u8 *)&cmd->IEData[0];
70
71         /* Next traverse all reservations to place IEs in allocated memory. */
72         list_for_each_entry(rsv, &rc->reservations, rc_node) {
73                 if (rsv->drp_ie != NULL) {
74                         memcpy(IEDataptr, rsv->drp_ie,
75                                rsv->drp_ie->hdr.length + 2);
76                         IEDataptr += rsv->drp_ie->hdr.length + 2;
77                 }
78         }
79         memcpy(IEDataptr, &rc->drp_avail.ie, sizeof(rc->drp_avail.ie));
80
81         reply.rceb.bEventType = UWB_RC_CET_GENERAL;
82         reply.rceb.wEvent = UWB_RC_CMD_SET_DRP_IE;
83         result = uwb_rc_cmd(rc, "SET-DRP-IE", &cmd->rccb,
84                         sizeof(*cmd) + num_bytes, &reply.rceb,
85                         sizeof(reply));
86         if (result < 0)
87                 goto error_cmd;
88         result = le16_to_cpu(reply.wRemainingSpace);
89         if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
90                 dev_err(&rc->uwb_dev.dev, "SET-DRP-IE: command execution "
91                                 "failed: %s (%d). RemainingSpace in beacon "
92                                 "= %d\n", uwb_rc_strerror(reply.bResultCode),
93                                 reply.bResultCode, result);
94                 result = -EIO;
95         } else {
96                 dev_dbg(dev, "SET-DRP-IE sent. RemainingSpace in beacon "
97                              "= %d.\n", result);
98                 result = 0;
99         }
100 error_cmd:
101         kfree(cmd);
102 error:
103         return result;
104 }
105
106 void uwb_drp_handle_timeout(struct uwb_rsv *rsv)
107 {
108         struct device *dev = &rsv->rc->uwb_dev.dev;
109
110         dev_dbg(dev, "reservation timeout in state %s (%d)\n",
111                 uwb_rsv_state_str(rsv->state), rsv->state);
112
113         switch (rsv->state) {
114         case UWB_RSV_STATE_O_INITIATED:
115                 if (rsv->is_multicast) {
116                         uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
117                         return;
118                 }
119                 break;
120         case UWB_RSV_STATE_O_ESTABLISHED:
121                 if (rsv->is_multicast)
122                         return;
123                 break;
124         default:
125                 break;
126         }
127         uwb_rsv_remove(rsv);
128 }
129
130 /*
131  * Based on the DRP IE, transition a target reservation to a new
132  * state.
133  */
134 static void uwb_drp_process_target(struct uwb_rc *rc, struct uwb_rsv *rsv,
135                                    struct uwb_ie_drp *drp_ie)
136 {
137         struct device *dev = &rc->uwb_dev.dev;
138         int status;
139         enum uwb_drp_reason reason_code;
140
141         status = uwb_ie_drp_status(drp_ie);
142         reason_code = uwb_ie_drp_reason_code(drp_ie);
143
144         if (status) {
145                 switch (reason_code) {
146                 case UWB_DRP_REASON_ACCEPTED:
147                         uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
148                         break;
149                 case UWB_DRP_REASON_MODIFIED:
150                         dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
151                                 reason_code, status);
152                         break;
153                 default:
154                         dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
155                                  reason_code, status);
156                 }
157         } else {
158                 switch (reason_code) {
159                 case UWB_DRP_REASON_ACCEPTED:
160                         /* New reservations are handled in uwb_rsv_find(). */
161                         break;
162                 case UWB_DRP_REASON_DENIED:
163                         uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
164                         break;
165                 case UWB_DRP_REASON_CONFLICT:
166                 case UWB_DRP_REASON_MODIFIED:
167                         dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
168                                 reason_code, status);
169                         break;
170                 default:
171                         dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
172                                  reason_code, status);
173                 }
174         }
175 }
176
177 /*
178  * Based on the DRP IE, transition an owner reservation to a new
179  * state.
180  */
181 static void uwb_drp_process_owner(struct uwb_rc *rc, struct uwb_rsv *rsv,
182                                   struct uwb_ie_drp *drp_ie)
183 {
184         struct device *dev = &rc->uwb_dev.dev;
185         int status;
186         enum uwb_drp_reason reason_code;
187
188         status = uwb_ie_drp_status(drp_ie);
189         reason_code = uwb_ie_drp_reason_code(drp_ie);
190
191         if (status) {
192                 switch (reason_code) {
193                 case UWB_DRP_REASON_ACCEPTED:
194                         uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
195                         break;
196                 case UWB_DRP_REASON_MODIFIED:
197                         dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
198                                 reason_code, status);
199                         break;
200                 default:
201                         dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
202                                  reason_code, status);
203                 }
204         } else {
205                 switch (reason_code) {
206                 case UWB_DRP_REASON_PENDING:
207                         uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_PENDING);
208                         break;
209                 case UWB_DRP_REASON_DENIED:
210                         uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
211                         break;
212                 case UWB_DRP_REASON_CONFLICT:
213                 case UWB_DRP_REASON_MODIFIED:
214                         dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
215                                 reason_code, status);
216                         break;
217                 default:
218                         dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
219                                  reason_code, status);
220                 }
221         }
222 }
223
224 /*
225  * Process a received DRP IE, it's either for a reservation owned by
226  * the RC or targeted at it (or it's for a WUSB cluster reservation).
227  */
228 static void uwb_drp_process(struct uwb_rc *rc, struct uwb_dev *src,
229                      struct uwb_ie_drp *drp_ie)
230 {
231         struct uwb_rsv *rsv;
232
233         rsv = uwb_rsv_find(rc, src, drp_ie);
234         if (!rsv) {
235                 /*
236                  * No reservation? It's either for a recently
237                  * terminated reservation; or the DRP IE couldn't be
238                  * processed (e.g., an invalid IE or out of memory).
239                  */
240                 return;
241         }
242
243         /*
244          * Do nothing with DRP IEs for reservations that have been
245          * terminated.
246          */
247         if (rsv->state == UWB_RSV_STATE_NONE) {
248                 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
249                 return;
250         }
251
252         if (uwb_ie_drp_owner(drp_ie))
253                 uwb_drp_process_target(rc, rsv, drp_ie);
254         else
255                 uwb_drp_process_owner(rc, rsv, drp_ie);
256 }
257
258
259 /*
260  * Process all the DRP IEs (both DRP IEs and the DRP Availability IE)
261  * from a device.
262  */
263 static
264 void uwb_drp_process_all(struct uwb_rc *rc, struct uwb_rc_evt_drp *drp_evt,
265                          size_t ielen, struct uwb_dev *src_dev)
266 {
267         struct device *dev = &rc->uwb_dev.dev;
268         struct uwb_ie_hdr *ie_hdr;
269         void *ptr;
270
271         ptr = drp_evt->ie_data;
272         for (;;) {
273                 ie_hdr = uwb_ie_next(&ptr, &ielen);
274                 if (!ie_hdr)
275                         break;
276
277                 switch (ie_hdr->element_id) {
278                 case UWB_IE_DRP_AVAILABILITY:
279                         /* FIXME: does something need to be done with this? */
280                         break;
281                 case UWB_IE_DRP:
282                         uwb_drp_process(rc, src_dev, (struct uwb_ie_drp *)ie_hdr);
283                         break;
284                 default:
285                         dev_warn(dev, "unexpected IE in DRP notification\n");
286                         break;
287                 }
288         }
289
290         if (ielen > 0)
291                 dev_warn(dev, "%d octets remaining in DRP notification\n",
292                          (int)ielen);
293 }
294
295
296 /*
297  * Go through all the DRP IEs and find the ones that conflict with our
298  * reservations.
299  *
300  * FIXME: must resolve the conflict according the the rules in
301  * [ECMA-368].
302  */
303 static
304 void uwb_drp_process_conflict_all(struct uwb_rc *rc, struct uwb_rc_evt_drp *drp_evt,
305                                   size_t ielen, struct uwb_dev *src_dev)
306 {
307         struct device *dev = &rc->uwb_dev.dev;
308         struct uwb_ie_hdr *ie_hdr;
309         struct uwb_ie_drp *drp_ie;
310         void *ptr;
311
312         ptr = drp_evt->ie_data;
313         for (;;) {
314                 ie_hdr = uwb_ie_next(&ptr, &ielen);
315                 if (!ie_hdr)
316                         break;
317
318                 drp_ie = container_of(ie_hdr, struct uwb_ie_drp, hdr);
319
320                 /* FIXME: check if this DRP IE conflicts. */
321         }
322
323         if (ielen > 0)
324                 dev_warn(dev, "%d octets remaining in DRP notification\n",
325                          (int)ielen);
326 }
327
328
329 /*
330  * Terminate all reservations owned by, or targeted at, 'uwb_dev'.
331  */
332 static void uwb_drp_terminate_all(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
333 {
334         struct uwb_rsv *rsv;
335
336         list_for_each_entry(rsv, &rc->reservations, rc_node) {
337                 if (rsv->owner == uwb_dev
338                     || (rsv->target.type == UWB_RSV_TARGET_DEV && rsv->target.dev == uwb_dev))
339                         uwb_rsv_remove(rsv);
340         }
341 }
342
343
344 /**
345  * uwbd_evt_handle_rc_drp - handle a DRP_IE event
346  * @evt: the DRP_IE event from the radio controller
347  *
348  * This processes DRP notifications from the radio controller, either
349  * initiating a new reservation or transitioning an existing
350  * reservation into a different state.
351  *
352  * DRP notifications can occur for three different reasons:
353  *
354  * - UWB_DRP_NOTIF_DRP_IE_RECVD: one or more DRP IEs with the RC as
355  *   the target or source have been recieved.
356  *
357  *   These DRP IEs could be new or for an existing reservation.
358  *
359  *   If the DRP IE for an existing reservation ceases to be to
360  *   recieved for at least mMaxLostBeacons, the reservation should be
361  *   considered to be terminated.  Note that the TERMINATE reason (see
362  *   below) may not always be signalled (e.g., the remote device has
363  *   two or more reservations established with the RC).
364  *
365  * - UWB_DRP_NOTIF_CONFLICT: DRP IEs from any device in the beacon
366  *   group conflict with the RC's reservations.
367  *
368  * - UWB_DRP_NOTIF_TERMINATE: DRP IEs are no longer being received
369  *   from a device (i.e., it's terminated all reservations).
370  *
371  * Only the software state of the reservations is changed; the setting
372  * of the radio controller's DRP IEs is done after all the events in
373  * an event buffer are processed.  This saves waiting multiple times
374  * for the SET_DRP_IE command to complete.
375  */
376 int uwbd_evt_handle_rc_drp(struct uwb_event *evt)
377 {
378         struct device *dev = &evt->rc->uwb_dev.dev;
379         struct uwb_rc *rc = evt->rc;
380         struct uwb_rc_evt_drp *drp_evt;
381         size_t ielength, bytes_left;
382         struct uwb_dev_addr src_addr;
383         struct uwb_dev *src_dev;
384         int reason;
385
386         /* Is there enough data to decode the event (and any IEs in
387            its payload)? */
388         if (evt->notif.size < sizeof(*drp_evt)) {
389                 dev_err(dev, "DRP event: Not enough data to decode event "
390                         "[%zu bytes left, %zu needed]\n",
391                         evt->notif.size, sizeof(*drp_evt));
392                 return 0;
393         }
394         bytes_left = evt->notif.size - sizeof(*drp_evt);
395         drp_evt = container_of(evt->notif.rceb, struct uwb_rc_evt_drp, rceb);
396         ielength = le16_to_cpu(drp_evt->ie_length);
397         if (bytes_left != ielength) {
398                 dev_err(dev, "DRP event: Not enough data in payload [%zu"
399                         "bytes left, %zu declared in the event]\n",
400                         bytes_left, ielength);
401                 return 0;
402         }
403
404         memcpy(src_addr.data, &drp_evt->src_addr, sizeof(src_addr));
405         src_dev = uwb_dev_get_by_devaddr(rc, &src_addr);
406         if (!src_dev) {
407                 /*
408                  * A DRP notification from an unrecognized device.
409                  *
410                  * This is probably from a WUSB device that doesn't
411                  * have an EUI-48 and therefore doesn't show up in the
412                  * UWB device database.  It's safe to simply ignore
413                  * these.
414                  */
415                 return 0;
416         }
417
418         mutex_lock(&rc->rsvs_mutex);
419
420         reason = uwb_rc_evt_drp_reason(drp_evt);
421
422         switch (reason) {
423         case UWB_DRP_NOTIF_DRP_IE_RCVD:
424                 uwb_drp_process_all(rc, drp_evt, ielength, src_dev);
425                 break;
426         case UWB_DRP_NOTIF_CONFLICT:
427                 uwb_drp_process_conflict_all(rc, drp_evt, ielength, src_dev);
428                 break;
429         case UWB_DRP_NOTIF_TERMINATE:
430                 uwb_drp_terminate_all(rc, src_dev);
431                 break;
432         default:
433                 dev_warn(dev, "ignored DRP event with reason code: %d\n", reason);
434                 break;
435         }
436
437         mutex_unlock(&rc->rsvs_mutex);
438
439         uwb_dev_put(src_dev);
440         return 0;
441 }