3 * Dynamic Reservation Protocol handling
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
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.
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.
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/>.
21 #include <linux/kthread.h>
22 #include <linux/freezer.h>
23 #include <linux/delay.h>
24 #include "uwb-internal.h"
27 * Construct and send the SET DRP IE
29 * @rc: UWB Host controller
30 * @returns: >= 0 number of bytes still available in the beacon
31 * < 0 errno code on error.
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.
38 * A DRP Availability IE is appended.
40 * rc->rsvs_mutex is held
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.
46 int uwb_rc_send_all_drp_ie(struct uwb_rc *rc)
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;
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;
62 num_bytes += sizeof(rc->drp_avail.ie);
63 cmd = kzalloc(sizeof(*cmd) + num_bytes, GFP_KERNEL);
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];
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;
79 memcpy(IEDataptr, &rc->drp_avail.ie, sizeof(rc->drp_avail.ie));
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,
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);
96 dev_dbg(dev, "SET-DRP-IE sent. RemainingSpace in beacon "
106 void uwb_drp_handle_timeout(struct uwb_rsv *rsv)
108 struct device *dev = &rsv->rc->uwb_dev.dev;
110 dev_dbg(dev, "reservation timeout in state %s (%d)\n",
111 uwb_rsv_state_str(rsv->state), rsv->state);
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);
120 case UWB_RSV_STATE_O_ESTABLISHED:
121 if (rsv->is_multicast)
131 * Based on the DRP IE, transition a target reservation to a new
134 static void uwb_drp_process_target(struct uwb_rc *rc, struct uwb_rsv *rsv,
135 struct uwb_ie_drp *drp_ie)
137 struct device *dev = &rc->uwb_dev.dev;
139 enum uwb_drp_reason reason_code;
141 status = uwb_ie_drp_status(drp_ie);
142 reason_code = uwb_ie_drp_reason_code(drp_ie);
145 switch (reason_code) {
146 case UWB_DRP_REASON_ACCEPTED:
147 uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
149 case UWB_DRP_REASON_MODIFIED:
150 dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
151 reason_code, status);
154 dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
155 reason_code, status);
158 switch (reason_code) {
159 case UWB_DRP_REASON_ACCEPTED:
160 /* New reservations are handled in uwb_rsv_find(). */
162 case UWB_DRP_REASON_DENIED:
163 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
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);
171 dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
172 reason_code, status);
178 * Based on the DRP IE, transition an owner reservation to a new
181 static void uwb_drp_process_owner(struct uwb_rc *rc, struct uwb_rsv *rsv,
182 struct uwb_ie_drp *drp_ie)
184 struct device *dev = &rc->uwb_dev.dev;
186 enum uwb_drp_reason reason_code;
188 status = uwb_ie_drp_status(drp_ie);
189 reason_code = uwb_ie_drp_reason_code(drp_ie);
192 switch (reason_code) {
193 case UWB_DRP_REASON_ACCEPTED:
194 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
196 case UWB_DRP_REASON_MODIFIED:
197 dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
198 reason_code, status);
201 dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
202 reason_code, status);
205 switch (reason_code) {
206 case UWB_DRP_REASON_PENDING:
207 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_PENDING);
209 case UWB_DRP_REASON_DENIED:
210 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
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);
218 dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
219 reason_code, status);
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).
228 static void uwb_drp_process(struct uwb_rc *rc, struct uwb_dev *src,
229 struct uwb_ie_drp *drp_ie)
233 rsv = uwb_rsv_find(rc, src, drp_ie);
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).
244 * Do nothing with DRP IEs for reservations that have been
247 if (rsv->state == UWB_RSV_STATE_NONE) {
248 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
252 if (uwb_ie_drp_owner(drp_ie))
253 uwb_drp_process_target(rc, rsv, drp_ie);
255 uwb_drp_process_owner(rc, rsv, drp_ie);
260 * Process all the DRP IEs (both DRP IEs and the DRP Availability IE)
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)
267 struct device *dev = &rc->uwb_dev.dev;
268 struct uwb_ie_hdr *ie_hdr;
271 ptr = drp_evt->ie_data;
273 ie_hdr = uwb_ie_next(&ptr, &ielen);
277 switch (ie_hdr->element_id) {
278 case UWB_IE_DRP_AVAILABILITY:
279 /* FIXME: does something need to be done with this? */
282 uwb_drp_process(rc, src_dev, (struct uwb_ie_drp *)ie_hdr);
285 dev_warn(dev, "unexpected IE in DRP notification\n");
291 dev_warn(dev, "%d octets remaining in DRP notification\n",
297 * Go through all the DRP IEs and find the ones that conflict with our
300 * FIXME: must resolve the conflict according the the rules in
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)
307 struct device *dev = &rc->uwb_dev.dev;
308 struct uwb_ie_hdr *ie_hdr;
309 struct uwb_ie_drp *drp_ie;
312 ptr = drp_evt->ie_data;
314 ie_hdr = uwb_ie_next(&ptr, &ielen);
318 drp_ie = container_of(ie_hdr, struct uwb_ie_drp, hdr);
320 /* FIXME: check if this DRP IE conflicts. */
324 dev_warn(dev, "%d octets remaining in DRP notification\n",
330 * Terminate all reservations owned by, or targeted at, 'uwb_dev'.
332 static void uwb_drp_terminate_all(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
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))
345 * uwbd_evt_handle_rc_drp - handle a DRP_IE event
346 * @evt: the DRP_IE event from the radio controller
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.
352 * DRP notifications can occur for three different reasons:
354 * - UWB_DRP_NOTIF_DRP_IE_RECVD: one or more DRP IEs with the RC as
355 * the target or source have been recieved.
357 * These DRP IEs could be new or for an existing reservation.
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).
365 * - UWB_DRP_NOTIF_CONFLICT: DRP IEs from any device in the beacon
366 * group conflict with the RC's reservations.
368 * - UWB_DRP_NOTIF_TERMINATE: DRP IEs are no longer being received
369 * from a device (i.e., it's terminated all reservations).
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.
376 int uwbd_evt_handle_rc_drp(struct uwb_event *evt)
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;
386 /* Is there enough data to decode the event (and any IEs in
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));
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);
404 memcpy(src_addr.data, &drp_evt->src_addr, sizeof(src_addr));
405 src_dev = uwb_dev_get_by_devaddr(rc, &src_addr);
408 * A DRP notification from an unrecognized device.
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
418 mutex_lock(&rc->rsvs_mutex);
420 reason = uwb_rc_evt_drp_reason(drp_evt);
423 case UWB_DRP_NOTIF_DRP_IE_RCVD:
424 uwb_drp_process_all(rc, drp_evt, ielength, src_dev);
426 case UWB_DRP_NOTIF_CONFLICT:
427 uwb_drp_process_conflict_all(rc, drp_evt, ielength, src_dev);
429 case UWB_DRP_NOTIF_TERMINATE:
430 uwb_drp_terminate_all(rc, src_dev);
433 dev_warn(dev, "ignored DRP event with reason code: %d\n", reason);
437 mutex_unlock(&rc->rsvs_mutex);
439 uwb_dev_put(src_dev);