wlp-lc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * WiMedia Logical Link Control Protocol (WLP)
  3. *
  4. * Copyright (C) 2005-2006 Intel Corporation
  5. * Reinette Chatre <reinette.chatre@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. *
  21. *
  22. * FIXME: docs
  23. */
  24. #include <linux/wlp.h>
  25. #include <linux/slab.h>
  26. #include "wlp-internal.h"
  27. static
  28. void wlp_neighbor_init(struct wlp_neighbor_e *neighbor)
  29. {
  30. INIT_LIST_HEAD(&neighbor->wssid);
  31. }
  32. /**
  33. * Create area for device information storage
  34. *
  35. * wlp->mutex must be held
  36. */
  37. int __wlp_alloc_device_info(struct wlp *wlp)
  38. {
  39. struct device *dev = &wlp->rc->uwb_dev.dev;
  40. BUG_ON(wlp->dev_info != NULL);
  41. wlp->dev_info = kzalloc(sizeof(struct wlp_device_info), GFP_KERNEL);
  42. if (wlp->dev_info == NULL) {
  43. dev_err(dev, "WLP: Unable to allocate memory for "
  44. "device information.\n");
  45. return -ENOMEM;
  46. }
  47. return 0;
  48. }
  49. /**
  50. * Fill in device information using function provided by driver
  51. *
  52. * wlp->mutex must be held
  53. */
  54. static
  55. void __wlp_fill_device_info(struct wlp *wlp)
  56. {
  57. wlp->fill_device_info(wlp, wlp->dev_info);
  58. }
  59. /**
  60. * Setup device information
  61. *
  62. * Allocate area for device information and populate it.
  63. *
  64. * wlp->mutex must be held
  65. */
  66. int __wlp_setup_device_info(struct wlp *wlp)
  67. {
  68. int result;
  69. struct device *dev = &wlp->rc->uwb_dev.dev;
  70. result = __wlp_alloc_device_info(wlp);
  71. if (result < 0) {
  72. dev_err(dev, "WLP: Unable to allocate area for "
  73. "device information.\n");
  74. return result;
  75. }
  76. __wlp_fill_device_info(wlp);
  77. return 0;
  78. }
  79. /**
  80. * Remove information about neighbor stored temporarily
  81. *
  82. * Information learned during discovey should only be stored when the
  83. * device enrolls in the neighbor's WSS. We do need to store this
  84. * information temporarily in order to present it to the user.
  85. *
  86. * We are only interested in keeping neighbor WSS information if that
  87. * neighbor is accepting enrollment.
  88. *
  89. * should be called with wlp->nbmutex held
  90. */
  91. void wlp_remove_neighbor_tmp_info(struct wlp_neighbor_e *neighbor)
  92. {
  93. struct wlp_wssid_e *wssid_e, *next;
  94. u8 keep;
  95. if (!list_empty(&neighbor->wssid)) {
  96. list_for_each_entry_safe(wssid_e, next, &neighbor->wssid,
  97. node) {
  98. if (wssid_e->info != NULL) {
  99. keep = wssid_e->info->accept_enroll;
  100. kfree(wssid_e->info);
  101. wssid_e->info = NULL;
  102. if (!keep) {
  103. list_del(&wssid_e->node);
  104. kfree(wssid_e);
  105. }
  106. }
  107. }
  108. }
  109. if (neighbor->info != NULL) {
  110. kfree(neighbor->info);
  111. neighbor->info = NULL;
  112. }
  113. }
  114. /*
  115. * Populate WLP neighborhood cache with neighbor information
  116. *
  117. * A new neighbor is found. If it is discoverable then we add it to the
  118. * neighborhood cache.
  119. *
  120. */
  121. static
  122. int wlp_add_neighbor(struct wlp *wlp, struct uwb_dev *dev)
  123. {
  124. int result = 0;
  125. int discoverable;
  126. struct wlp_neighbor_e *neighbor;
  127. /*
  128. * FIXME:
  129. * Use contents of WLP IE found in beacon cache to determine if
  130. * neighbor is discoverable.
  131. * The device does not support WLP IE yet so this still needs to be
  132. * done. Until then we assume all devices are discoverable.
  133. */
  134. discoverable = 1; /* will be changed when FIXME disappears */
  135. if (discoverable) {
  136. /* Add neighbor to cache for discovery */
  137. neighbor = kzalloc(sizeof(*neighbor), GFP_KERNEL);
  138. if (neighbor == NULL) {
  139. dev_err(&dev->dev, "Unable to create memory for "
  140. "new neighbor. \n");
  141. result = -ENOMEM;
  142. goto error_no_mem;
  143. }
  144. wlp_neighbor_init(neighbor);
  145. uwb_dev_get(dev);
  146. neighbor->uwb_dev = dev;
  147. list_add(&neighbor->node, &wlp->neighbors);
  148. }
  149. error_no_mem:
  150. return result;
  151. }
  152. /**
  153. * Remove one neighbor from cache
  154. */
  155. static
  156. void __wlp_neighbor_release(struct wlp_neighbor_e *neighbor)
  157. {
  158. struct wlp_wssid_e *wssid_e, *next_wssid_e;
  159. list_for_each_entry_safe(wssid_e, next_wssid_e,
  160. &neighbor->wssid, node) {
  161. list_del(&wssid_e->node);
  162. kfree(wssid_e);
  163. }
  164. uwb_dev_put(neighbor->uwb_dev);
  165. list_del(&neighbor->node);
  166. kfree(neighbor);
  167. }
  168. /**
  169. * Clear entire neighborhood cache.
  170. */
  171. static
  172. void __wlp_neighbors_release(struct wlp *wlp)
  173. {
  174. struct wlp_neighbor_e *neighbor, *next;
  175. if (list_empty(&wlp->neighbors))
  176. return;
  177. list_for_each_entry_safe(neighbor, next, &wlp->neighbors, node) {
  178. __wlp_neighbor_release(neighbor);
  179. }
  180. }
  181. static
  182. void wlp_neighbors_release(struct wlp *wlp)
  183. {
  184. mutex_lock(&wlp->nbmutex);
  185. __wlp_neighbors_release(wlp);
  186. mutex_unlock(&wlp->nbmutex);
  187. }
  188. /**
  189. * Send D1 message to neighbor, receive D2 message
  190. *
  191. * @neighbor: neighbor to which D1 message will be sent
  192. * @wss: if not NULL, it is an enrollment request for this WSS
  193. * @wssid: if wss not NULL, this is the wssid of the WSS in which we
  194. * want to enroll
  195. *
  196. * A D1/D2 exchange is done for one of two reasons: discovery or
  197. * enrollment. If done for discovery the D1 message is sent to the neighbor
  198. * and the contents of the D2 response is stored in a temporary cache.
  199. * If done for enrollment the @wss and @wssid are provided also. In this
  200. * case the D1 message is sent to the neighbor, the D2 response is parsed
  201. * for enrollment of the WSS with wssid.
  202. *
  203. * &wss->mutex is held
  204. */
  205. static
  206. int wlp_d1d2_exchange(struct wlp *wlp, struct wlp_neighbor_e *neighbor,
  207. struct wlp_wss *wss, struct wlp_uuid *wssid)
  208. {
  209. int result;
  210. struct device *dev = &wlp->rc->uwb_dev.dev;
  211. DECLARE_COMPLETION_ONSTACK(completion);
  212. struct wlp_session session;
  213. struct sk_buff *skb;
  214. struct wlp_frame_assoc *resp;
  215. struct uwb_dev_addr *dev_addr = &neighbor->uwb_dev->dev_addr;
  216. mutex_lock(&wlp->mutex);
  217. if (!wlp_uuid_is_set(&wlp->uuid)) {
  218. dev_err(dev, "WLP: UUID is not set. Set via sysfs to "
  219. "proceed.\n");
  220. result = -ENXIO;
  221. goto out;
  222. }
  223. /* Send D1 association frame */
  224. result = wlp_send_assoc_frame(wlp, wss, dev_addr, WLP_ASSOC_D1);
  225. if (result < 0) {
  226. dev_err(dev, "Unable to send D1 frame to neighbor "
  227. "%02x:%02x (%d)\n", dev_addr->data[1],
  228. dev_addr->data[0], result);
  229. goto out;
  230. }
  231. /* Create session, wait for response */
  232. session.exp_message = WLP_ASSOC_D2;
  233. session.cb = wlp_session_cb;
  234. session.cb_priv = &completion;
  235. session.neighbor_addr = *dev_addr;
  236. BUG_ON(wlp->session != NULL);
  237. wlp->session = &session;
  238. /* Wait for D2/F0 frame */
  239. result = wait_for_completion_interruptible_timeout(&completion,
  240. WLP_PER_MSG_TIMEOUT * HZ);
  241. if (result == 0) {
  242. result = -ETIMEDOUT;
  243. dev_err(dev, "Timeout while sending D1 to neighbor "
  244. "%02x:%02x.\n", dev_addr->data[1],
  245. dev_addr->data[0]);
  246. goto error_session;
  247. }
  248. if (result < 0) {
  249. dev_err(dev, "Unable to discover/enroll neighbor %02x:%02x.\n",
  250. dev_addr->data[1], dev_addr->data[0]);
  251. goto error_session;
  252. }
  253. /* Parse message in session->data: it will be either D2 or F0 */
  254. skb = session.data;
  255. resp = (void *) skb->data;
  256. if (resp->type == WLP_ASSOC_F0) {
  257. result = wlp_parse_f0(wlp, skb);
  258. if (result < 0)
  259. dev_err(dev, "WLP: Unable to parse F0 from neighbor "
  260. "%02x:%02x.\n", dev_addr->data[1],
  261. dev_addr->data[0]);
  262. result = -EINVAL;
  263. goto error_resp_parse;
  264. }
  265. if (wss == NULL) {
  266. /* Discovery */
  267. result = wlp_parse_d2_frame_to_cache(wlp, skb, neighbor);
  268. if (result < 0) {
  269. dev_err(dev, "WLP: Unable to parse D2 message from "
  270. "neighbor %02x:%02x for discovery.\n",
  271. dev_addr->data[1], dev_addr->data[0]);
  272. goto error_resp_parse;
  273. }
  274. } else {
  275. /* Enrollment */
  276. result = wlp_parse_d2_frame_to_enroll(wss, skb, neighbor,
  277. wssid);
  278. if (result < 0) {
  279. dev_err(dev, "WLP: Unable to parse D2 message from "
  280. "neighbor %02x:%02x for enrollment.\n",
  281. dev_addr->data[1], dev_addr->data[0]);
  282. goto error_resp_parse;
  283. }
  284. }
  285. error_resp_parse:
  286. kfree_skb(skb);
  287. error_session:
  288. wlp->session = NULL;
  289. out:
  290. mutex_unlock(&wlp->mutex);
  291. return result;
  292. }
  293. /**
  294. * Enroll into WSS of provided WSSID by using neighbor as registrar
  295. *
  296. * &wss->mutex is held
  297. */
  298. int wlp_enroll_neighbor(struct wlp *wlp, struct wlp_neighbor_e *neighbor,
  299. struct wlp_wss *wss, struct wlp_uuid *wssid)
  300. {
  301. int result = 0;
  302. struct device *dev = &wlp->rc->uwb_dev.dev;
  303. char buf[WLP_WSS_UUID_STRSIZE];
  304. struct uwb_dev_addr *dev_addr = &neighbor->uwb_dev->dev_addr;
  305. wlp_wss_uuid_print(buf, sizeof(buf), wssid);
  306. result = wlp_d1d2_exchange(wlp, neighbor, wss, wssid);
  307. if (result < 0) {
  308. dev_err(dev, "WLP: D1/D2 message exchange for enrollment "
  309. "failed. result = %d \n", result);
  310. goto out;
  311. }
  312. if (wss->state != WLP_WSS_STATE_PART_ENROLLED) {
  313. dev_err(dev, "WLP: Unable to enroll into WSS %s using "
  314. "neighbor %02x:%02x. \n", buf,
  315. dev_addr->data[1], dev_addr->data[0]);
  316. result = -EINVAL;
  317. goto out;
  318. }
  319. if (wss->secure_status == WLP_WSS_SECURE) {
  320. dev_err(dev, "FIXME: need to complete secure enrollment.\n");
  321. result = -EINVAL;
  322. goto error;
  323. } else {
  324. wss->state = WLP_WSS_STATE_ENROLLED;
  325. dev_dbg(dev, "WLP: Success Enrollment into unsecure WSS "
  326. "%s using neighbor %02x:%02x. \n",
  327. buf, dev_addr->data[1], dev_addr->data[0]);
  328. }
  329. out:
  330. return result;
  331. error:
  332. wlp_wss_reset(wss);
  333. return result;
  334. }
  335. /**
  336. * Discover WSS information of neighbor's active WSS
  337. */
  338. static
  339. int wlp_discover_neighbor(struct wlp *wlp,
  340. struct wlp_neighbor_e *neighbor)
  341. {
  342. return wlp_d1d2_exchange(wlp, neighbor, NULL, NULL);
  343. }
  344. /**
  345. * Each neighbor in the neighborhood cache is discoverable. Discover it.
  346. *
  347. * Discovery is done through sending of D1 association frame and parsing
  348. * the D2 association frame response. Only wssid from D2 will be included
  349. * in neighbor cache, rest is just displayed to user and forgotten.
  350. *
  351. * The discovery is not done in parallel. This is simple and enables us to
  352. * maintain only one association context.
  353. *
  354. * The discovery of one neighbor does not affect the other, but if the
  355. * discovery of a neighbor fails it is removed from the neighborhood cache.
  356. */
  357. static
  358. int wlp_discover_all_neighbors(struct wlp *wlp)
  359. {
  360. int result = 0;
  361. struct device *dev = &wlp->rc->uwb_dev.dev;
  362. struct wlp_neighbor_e *neighbor, *next;
  363. list_for_each_entry_safe(neighbor, next, &wlp->neighbors, node) {
  364. result = wlp_discover_neighbor(wlp, neighbor);
  365. if (result < 0) {
  366. dev_err(dev, "WLP: Unable to discover neighbor "
  367. "%02x:%02x, removing from neighborhood. \n",
  368. neighbor->uwb_dev->dev_addr.data[1],
  369. neighbor->uwb_dev->dev_addr.data[0]);
  370. __wlp_neighbor_release(neighbor);
  371. }
  372. }
  373. return result;
  374. }
  375. static int wlp_add_neighbor_helper(struct device *dev, void *priv)
  376. {
  377. struct wlp *wlp = priv;
  378. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  379. return wlp_add_neighbor(wlp, uwb_dev);
  380. }
  381. /**
  382. * Discover WLP neighborhood
  383. *
  384. * Will send D1 association frame to all devices in beacon group that have
  385. * discoverable bit set in WLP IE. D2 frames will be received, information
  386. * displayed to user in @buf. Partial information (from D2 association
  387. * frame) will be cached to assist with future association
  388. * requests.
  389. *
  390. * The discovery of the WLP neighborhood is triggered by the user. This
  391. * should occur infrequently and we thus free current cache and re-allocate
  392. * memory if needed.
  393. *
  394. * If one neighbor fails during initial discovery (determining if it is a
  395. * neighbor or not), we fail all - note that interaction with neighbor has
  396. * not occured at this point so if a failure occurs we know something went wrong
  397. * locally. We thus undo everything.
  398. */
  399. ssize_t wlp_discover(struct wlp *wlp)
  400. {
  401. int result = 0;
  402. struct device *dev = &wlp->rc->uwb_dev.dev;
  403. mutex_lock(&wlp->nbmutex);
  404. /* Clear current neighborhood cache. */
  405. __wlp_neighbors_release(wlp);
  406. /* Determine which devices in neighborhood. Repopulate cache. */
  407. result = uwb_dev_for_each(wlp->rc, wlp_add_neighbor_helper, wlp);
  408. if (result < 0) {
  409. /* May have partial neighbor information, release all. */
  410. __wlp_neighbors_release(wlp);
  411. goto error_dev_for_each;
  412. }
  413. /* Discover the properties of devices in neighborhood. */
  414. result = wlp_discover_all_neighbors(wlp);
  415. /* In case of failure we still print our partial results. */
  416. if (result < 0) {
  417. dev_err(dev, "Unable to fully discover neighborhood. \n");
  418. result = 0;
  419. }
  420. error_dev_for_each:
  421. mutex_unlock(&wlp->nbmutex);
  422. return result;
  423. }
  424. /**
  425. * Handle events from UWB stack
  426. *
  427. * We handle events conservatively. If a neighbor goes off the air we
  428. * remove it from the neighborhood. If an association process is in
  429. * progress this function will block waiting for the nbmutex to become
  430. * free. The association process will thus be allowed to complete before it
  431. * is removed.
  432. */
  433. static
  434. void wlp_uwb_notifs_cb(void *_wlp, struct uwb_dev *uwb_dev,
  435. enum uwb_notifs event)
  436. {
  437. struct wlp *wlp = _wlp;
  438. struct device *dev = &wlp->rc->uwb_dev.dev;
  439. struct wlp_neighbor_e *neighbor, *next;
  440. int result;
  441. switch (event) {
  442. case UWB_NOTIF_ONAIR:
  443. result = wlp_eda_create_node(&wlp->eda,
  444. uwb_dev->mac_addr.data,
  445. &uwb_dev->dev_addr);
  446. if (result < 0)
  447. dev_err(dev, "WLP: Unable to add new neighbor "
  448. "%02x:%02x to EDA cache.\n",
  449. uwb_dev->dev_addr.data[1],
  450. uwb_dev->dev_addr.data[0]);
  451. break;
  452. case UWB_NOTIF_OFFAIR:
  453. wlp_eda_rm_node(&wlp->eda, &uwb_dev->dev_addr);
  454. mutex_lock(&wlp->nbmutex);
  455. list_for_each_entry_safe(neighbor, next, &wlp->neighbors, node) {
  456. if (neighbor->uwb_dev == uwb_dev)
  457. __wlp_neighbor_release(neighbor);
  458. }
  459. mutex_unlock(&wlp->nbmutex);
  460. break;
  461. default:
  462. dev_err(dev, "don't know how to handle event %d from uwb\n",
  463. event);
  464. }
  465. }
  466. static void wlp_channel_changed(struct uwb_pal *pal, int channel)
  467. {
  468. struct wlp *wlp = container_of(pal, struct wlp, pal);
  469. if (channel < 0)
  470. netif_carrier_off(wlp->ndev);
  471. else
  472. netif_carrier_on(wlp->ndev);
  473. }
  474. int wlp_setup(struct wlp *wlp, struct uwb_rc *rc, struct net_device *ndev)
  475. {
  476. int result;
  477. BUG_ON(wlp->fill_device_info == NULL);
  478. BUG_ON(wlp->xmit_frame == NULL);
  479. BUG_ON(wlp->stop_queue == NULL);
  480. BUG_ON(wlp->start_queue == NULL);
  481. wlp->rc = rc;
  482. wlp->ndev = ndev;
  483. wlp_eda_init(&wlp->eda);/* Set up address cache */
  484. wlp->uwb_notifs_handler.cb = wlp_uwb_notifs_cb;
  485. wlp->uwb_notifs_handler.data = wlp;
  486. uwb_notifs_register(rc, &wlp->uwb_notifs_handler);
  487. uwb_pal_init(&wlp->pal);
  488. wlp->pal.rc = rc;
  489. wlp->pal.channel_changed = wlp_channel_changed;
  490. result = uwb_pal_register(&wlp->pal);
  491. if (result < 0)
  492. uwb_notifs_deregister(wlp->rc, &wlp->uwb_notifs_handler);
  493. return result;
  494. }
  495. EXPORT_SYMBOL_GPL(wlp_setup);
  496. void wlp_remove(struct wlp *wlp)
  497. {
  498. wlp_neighbors_release(wlp);
  499. uwb_pal_unregister(&wlp->pal);
  500. uwb_notifs_deregister(wlp->rc, &wlp->uwb_notifs_handler);
  501. wlp_eda_release(&wlp->eda);
  502. mutex_lock(&wlp->mutex);
  503. if (wlp->dev_info != NULL)
  504. kfree(wlp->dev_info);
  505. mutex_unlock(&wlp->mutex);
  506. wlp->rc = NULL;
  507. }
  508. EXPORT_SYMBOL_GPL(wlp_remove);
  509. /**
  510. * wlp_reset_all - reset the WLP hardware
  511. * @wlp: the WLP device to reset.
  512. *
  513. * This schedules a full hardware reset of the WLP device. The radio
  514. * controller and any other PALs will also be reset.
  515. */
  516. void wlp_reset_all(struct wlp *wlp)
  517. {
  518. uwb_rc_reset_all(wlp->rc);
  519. }
  520. EXPORT_SYMBOL_GPL(wlp_reset_all);