netdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * WUSB Wire Adapter: WLP interface
  3. * Driver for the Linux Network stack.
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * FIXME: docs
  24. *
  25. * Implementation of the netdevice linkage (except tx and rx related stuff).
  26. *
  27. * ROADMAP:
  28. *
  29. * ENTRY POINTS (Net device):
  30. *
  31. * i1480u_open(): Called when we ifconfig up the interface;
  32. * associates to a UWB host controller, reserves
  33. * bandwidth (MAS), sets up RX USB URB and starts
  34. * the queue.
  35. *
  36. * i1480u_stop(): Called when we ifconfig down a interface;
  37. * reverses _open().
  38. *
  39. * i1480u_set_config():
  40. */
  41. #include <linux/if_arp.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/uwb/debug.h>
  44. #include "i1480u-wlp.h"
  45. struct i1480u_cmd_set_ip_mas {
  46. struct uwb_rccb rccb;
  47. struct uwb_dev_addr addr;
  48. u8 stream;
  49. u8 owner;
  50. u8 type; /* enum uwb_drp_type */
  51. u8 baMAS[32];
  52. } __attribute__((packed));
  53. static
  54. int i1480u_set_ip_mas(
  55. struct uwb_rc *rc,
  56. const struct uwb_dev_addr *dstaddr,
  57. u8 stream, u8 owner, u8 type, unsigned long *mas)
  58. {
  59. int result;
  60. struct i1480u_cmd_set_ip_mas *cmd;
  61. struct uwb_rc_evt_confirm reply;
  62. result = -ENOMEM;
  63. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  64. if (cmd == NULL)
  65. goto error_kzalloc;
  66. cmd->rccb.bCommandType = 0xfd;
  67. cmd->rccb.wCommand = cpu_to_le16(0x000e);
  68. cmd->addr = *dstaddr;
  69. cmd->stream = stream;
  70. cmd->owner = owner;
  71. cmd->type = type;
  72. if (mas == NULL)
  73. memset(cmd->baMAS, 0x00, sizeof(cmd->baMAS));
  74. else
  75. memcpy(cmd->baMAS, mas, sizeof(cmd->baMAS));
  76. reply.rceb.bEventType = 0xfd;
  77. reply.rceb.wEvent = cpu_to_le16(0x000e);
  78. result = uwb_rc_cmd(rc, "SET-IP-MAS", &cmd->rccb, sizeof(*cmd),
  79. &reply.rceb, sizeof(reply));
  80. if (result < 0)
  81. goto error_cmd;
  82. if (reply.bResultCode != UWB_RC_RES_FAIL) {
  83. dev_err(&rc->uwb_dev.dev,
  84. "SET-IP-MAS: command execution failed: %d\n",
  85. reply.bResultCode);
  86. result = -EIO;
  87. }
  88. error_cmd:
  89. kfree(cmd);
  90. error_kzalloc:
  91. return result;
  92. }
  93. /*
  94. * Inform a WLP interface of a MAS reservation
  95. *
  96. * @rc is assumed refcnted.
  97. */
  98. /* FIXME: detect if remote device is WLP capable? */
  99. static int i1480u_mas_set_dev(struct uwb_dev *uwb_dev, struct uwb_rc *rc,
  100. u8 stream, u8 owner, u8 type, unsigned long *mas)
  101. {
  102. int result = 0;
  103. struct device *dev = &rc->uwb_dev.dev;
  104. result = i1480u_set_ip_mas(rc, &uwb_dev->dev_addr, stream, owner,
  105. type, mas);
  106. if (result < 0) {
  107. char rcaddrbuf[UWB_ADDR_STRSIZE], devaddrbuf[UWB_ADDR_STRSIZE];
  108. uwb_dev_addr_print(rcaddrbuf, sizeof(rcaddrbuf),
  109. &rc->uwb_dev.dev_addr);
  110. uwb_dev_addr_print(devaddrbuf, sizeof(devaddrbuf),
  111. &uwb_dev->dev_addr);
  112. dev_err(dev, "Set IP MAS (%s to %s) failed: %d\n",
  113. rcaddrbuf, devaddrbuf, result);
  114. }
  115. return result;
  116. }
  117. /**
  118. * Called by bandwidth allocator when change occurs in reservation.
  119. *
  120. * @rsv: The reservation that is being established, modified, or
  121. * terminated.
  122. *
  123. * When a reservation is established, modified, or terminated the upper layer
  124. * (WLP here) needs set/update the currently available Media Access Slots
  125. * that can be use for IP traffic.
  126. *
  127. * Our action taken during failure depends on how the reservation is being
  128. * changed:
  129. * - if reservation is being established we do nothing if we cannot set the
  130. * new MAS to be used
  131. * - if reservation is being terminated we revert back to PCA whether the
  132. * SET IP MAS command succeeds or not.
  133. */
  134. void i1480u_bw_alloc_cb(struct uwb_rsv *rsv)
  135. {
  136. int result = 0;
  137. struct i1480u *i1480u = rsv->pal_priv;
  138. struct device *dev = &i1480u->usb_iface->dev;
  139. struct uwb_dev *target_dev = rsv->target.dev;
  140. struct uwb_rc *rc = i1480u->wlp.rc;
  141. u8 stream = rsv->stream;
  142. int type = rsv->type;
  143. int is_owner = rsv->owner == &rc->uwb_dev;
  144. unsigned long *bmp = rsv->mas.bm;
  145. dev_err(dev, "WLP callback called - sending set ip mas\n");
  146. /*user cannot change options while setting configuration*/
  147. mutex_lock(&i1480u->options.mutex);
  148. switch (rsv->state) {
  149. case UWB_RSV_STATE_T_ACCEPTED:
  150. case UWB_RSV_STATE_O_ESTABLISHED:
  151. result = i1480u_mas_set_dev(target_dev, rc, stream, is_owner,
  152. type, bmp);
  153. if (result < 0) {
  154. dev_err(dev, "MAS reservation failed: %d\n", result);
  155. goto out;
  156. }
  157. if (is_owner) {
  158. wlp_tx_hdr_set_delivery_id_type(&i1480u->options.def_tx_hdr,
  159. WLP_DRP | stream);
  160. wlp_tx_hdr_set_rts_cts(&i1480u->options.def_tx_hdr, 0);
  161. }
  162. break;
  163. case UWB_RSV_STATE_NONE:
  164. /* revert back to PCA */
  165. result = i1480u_mas_set_dev(target_dev, rc, stream, is_owner,
  166. type, bmp);
  167. if (result < 0)
  168. dev_err(dev, "MAS reservation failed: %d\n", result);
  169. /* Revert to PCA even though SET IP MAS failed. */
  170. wlp_tx_hdr_set_delivery_id_type(&i1480u->options.def_tx_hdr,
  171. i1480u->options.pca_base_priority);
  172. wlp_tx_hdr_set_rts_cts(&i1480u->options.def_tx_hdr, 1);
  173. break;
  174. default:
  175. dev_err(dev, "unexpected WLP reservation state: %s (%d).\n",
  176. uwb_rsv_state_str(rsv->state), rsv->state);
  177. break;
  178. }
  179. out:
  180. mutex_unlock(&i1480u->options.mutex);
  181. return;
  182. }
  183. /**
  184. *
  185. * Called on 'ifconfig up'
  186. */
  187. int i1480u_open(struct net_device *net_dev)
  188. {
  189. int result;
  190. struct i1480u *i1480u = netdev_priv(net_dev);
  191. struct wlp *wlp = &i1480u->wlp;
  192. struct uwb_rc *rc;
  193. struct device *dev = &i1480u->usb_iface->dev;
  194. rc = wlp->rc;
  195. result = i1480u_rx_setup(i1480u); /* Alloc RX stuff */
  196. if (result < 0)
  197. goto error_rx_setup;
  198. netif_wake_queue(net_dev);
  199. #ifdef i1480u_FLOW_CONTROL
  200. result = usb_submit_urb(i1480u->notif_urb, GFP_KERNEL);;
  201. if (result < 0) {
  202. dev_err(dev, "Can't submit notification URB: %d\n", result);
  203. goto error_notif_urb_submit;
  204. }
  205. #endif
  206. i1480u->uwb_notifs_handler.cb = i1480u_uwb_notifs_cb;
  207. i1480u->uwb_notifs_handler.data = i1480u;
  208. if (uwb_bg_joined(rc))
  209. netif_carrier_on(net_dev);
  210. else
  211. netif_carrier_off(net_dev);
  212. uwb_notifs_register(rc, &i1480u->uwb_notifs_handler);
  213. /* Interface is up with an address, now we can create WSS */
  214. result = wlp_wss_setup(net_dev, &wlp->wss);
  215. if (result < 0) {
  216. dev_err(dev, "Can't create WSS: %d. \n", result);
  217. goto error_notif_deregister;
  218. }
  219. return 0;
  220. error_notif_deregister:
  221. uwb_notifs_deregister(rc, &i1480u->uwb_notifs_handler);
  222. #ifdef i1480u_FLOW_CONTROL
  223. error_notif_urb_submit:
  224. #endif
  225. netif_stop_queue(net_dev);
  226. i1480u_rx_release(i1480u);
  227. error_rx_setup:
  228. return result;
  229. }
  230. /**
  231. * Called on 'ifconfig down'
  232. */
  233. int i1480u_stop(struct net_device *net_dev)
  234. {
  235. struct i1480u *i1480u = netdev_priv(net_dev);
  236. struct wlp *wlp = &i1480u->wlp;
  237. struct uwb_rc *rc = wlp->rc;
  238. BUG_ON(wlp->rc == NULL);
  239. wlp_wss_remove(&wlp->wss);
  240. uwb_notifs_deregister(rc, &i1480u->uwb_notifs_handler);
  241. netif_carrier_off(net_dev);
  242. #ifdef i1480u_FLOW_CONTROL
  243. usb_kill_urb(i1480u->notif_urb);
  244. #endif
  245. netif_stop_queue(net_dev);
  246. i1480u_rx_release(i1480u);
  247. i1480u_tx_release(i1480u);
  248. return 0;
  249. }
  250. /** Report statistics */
  251. struct net_device_stats *i1480u_get_stats(struct net_device *net_dev)
  252. {
  253. struct i1480u *i1480u = netdev_priv(net_dev);
  254. return &i1480u->stats;
  255. }
  256. /**
  257. *
  258. * Change the interface config--we probably don't have to do anything.
  259. */
  260. int i1480u_set_config(struct net_device *net_dev, struct ifmap *map)
  261. {
  262. int result;
  263. struct i1480u *i1480u = netdev_priv(net_dev);
  264. BUG_ON(i1480u->wlp.rc == NULL);
  265. result = 0;
  266. return result;
  267. }
  268. /**
  269. * Change the MTU of the interface
  270. */
  271. int i1480u_change_mtu(struct net_device *net_dev, int mtu)
  272. {
  273. static union {
  274. struct wlp_tx_hdr tx;
  275. struct wlp_rx_hdr rx;
  276. } i1480u_all_hdrs;
  277. if (mtu < ETH_HLEN) /* We encap eth frames */
  278. return -ERANGE;
  279. if (mtu > 4000 - sizeof(i1480u_all_hdrs))
  280. return -ERANGE;
  281. net_dev->mtu = mtu;
  282. return 0;
  283. }
  284. /**
  285. * Callback function to handle events from UWB
  286. * When we see other devices we know the carrier is ok,
  287. * if we are the only device in the beacon group we set the carrier
  288. * state to off.
  289. * */
  290. void i1480u_uwb_notifs_cb(void *data, struct uwb_dev *uwb_dev,
  291. enum uwb_notifs event)
  292. {
  293. struct i1480u *i1480u = data;
  294. struct net_device *net_dev = i1480u->net_dev;
  295. struct device *dev = &i1480u->usb_iface->dev;
  296. switch (event) {
  297. case UWB_NOTIF_BG_JOIN:
  298. netif_carrier_on(net_dev);
  299. dev_info(dev, "Link is up\n");
  300. break;
  301. case UWB_NOTIF_BG_LEAVE:
  302. netif_carrier_off(net_dev);
  303. dev_info(dev, "Link is down\n");
  304. break;
  305. default:
  306. dev_err(dev, "don't know how to handle event %d from uwb\n",
  307. event);
  308. }
  309. }
  310. /**
  311. * Stop the network queue
  312. *
  313. * Enable WLP substack to stop network queue. We also set the flow control
  314. * threshold at this time to prevent the flow control from restarting the
  315. * queue.
  316. *
  317. * we are loosing the current threshold value here ... FIXME?
  318. */
  319. void i1480u_stop_queue(struct wlp *wlp)
  320. {
  321. struct i1480u *i1480u = container_of(wlp, struct i1480u, wlp);
  322. struct net_device *net_dev = i1480u->net_dev;
  323. i1480u->tx_inflight.threshold = 0;
  324. netif_stop_queue(net_dev);
  325. }
  326. /**
  327. * Start the network queue
  328. *
  329. * Enable WLP substack to start network queue. Also re-enable the flow
  330. * control to manage the queue again.
  331. *
  332. * We re-enable the flow control by storing the default threshold in the
  333. * flow control threshold. This means that if the user modified the
  334. * threshold before the queue was stopped and restarted that information
  335. * will be lost. FIXME?
  336. */
  337. void i1480u_start_queue(struct wlp *wlp)
  338. {
  339. struct i1480u *i1480u = container_of(wlp, struct i1480u, wlp);
  340. struct net_device *net_dev = i1480u->net_dev;
  341. i1480u->tx_inflight.threshold = i1480u_TX_INFLIGHT_THRESHOLD;
  342. netif_start_queue(net_dev);
  343. }