netdev.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/slab.h>
  42. #include <linux/if_arp.h>
  43. #include <linux/etherdevice.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. result = uwb_radio_start(&wlp->pal);
  199. if (result < 0)
  200. goto error_radio_start;
  201. netif_wake_queue(net_dev);
  202. #ifdef i1480u_FLOW_CONTROL
  203. result = usb_submit_urb(i1480u->notif_urb, GFP_KERNEL);
  204. if (result < 0) {
  205. dev_err(dev, "Can't submit notification URB: %d\n", result);
  206. goto error_notif_urb_submit;
  207. }
  208. #endif
  209. /* Interface is up with an address, now we can create WSS */
  210. result = wlp_wss_setup(net_dev, &wlp->wss);
  211. if (result < 0) {
  212. dev_err(dev, "Can't create WSS: %d. \n", result);
  213. goto error_wss_setup;
  214. }
  215. return 0;
  216. error_wss_setup:
  217. #ifdef i1480u_FLOW_CONTROL
  218. usb_kill_urb(i1480u->notif_urb);
  219. error_notif_urb_submit:
  220. #endif
  221. uwb_radio_stop(&wlp->pal);
  222. error_radio_start:
  223. netif_stop_queue(net_dev);
  224. i1480u_rx_release(i1480u);
  225. error_rx_setup:
  226. return result;
  227. }
  228. /**
  229. * Called on 'ifconfig down'
  230. */
  231. int i1480u_stop(struct net_device *net_dev)
  232. {
  233. struct i1480u *i1480u = netdev_priv(net_dev);
  234. struct wlp *wlp = &i1480u->wlp;
  235. BUG_ON(wlp->rc == NULL);
  236. wlp_wss_remove(&wlp->wss);
  237. netif_carrier_off(net_dev);
  238. #ifdef i1480u_FLOW_CONTROL
  239. usb_kill_urb(i1480u->notif_urb);
  240. #endif
  241. netif_stop_queue(net_dev);
  242. uwb_radio_stop(&wlp->pal);
  243. i1480u_rx_release(i1480u);
  244. i1480u_tx_release(i1480u);
  245. return 0;
  246. }
  247. /**
  248. *
  249. * Change the interface config--we probably don't have to do anything.
  250. */
  251. int i1480u_set_config(struct net_device *net_dev, struct ifmap *map)
  252. {
  253. int result;
  254. struct i1480u *i1480u = netdev_priv(net_dev);
  255. BUG_ON(i1480u->wlp.rc == NULL);
  256. result = 0;
  257. return result;
  258. }
  259. /**
  260. * Change the MTU of the interface
  261. */
  262. int i1480u_change_mtu(struct net_device *net_dev, int mtu)
  263. {
  264. static union {
  265. struct wlp_tx_hdr tx;
  266. struct wlp_rx_hdr rx;
  267. } i1480u_all_hdrs;
  268. if (mtu < ETH_HLEN) /* We encap eth frames */
  269. return -ERANGE;
  270. if (mtu > 4000 - sizeof(i1480u_all_hdrs))
  271. return -ERANGE;
  272. net_dev->mtu = mtu;
  273. return 0;
  274. }
  275. /**
  276. * Stop the network queue
  277. *
  278. * Enable WLP substack to stop network queue. We also set the flow control
  279. * threshold at this time to prevent the flow control from restarting the
  280. * queue.
  281. *
  282. * we are loosing the current threshold value here ... FIXME?
  283. */
  284. void i1480u_stop_queue(struct wlp *wlp)
  285. {
  286. struct i1480u *i1480u = container_of(wlp, struct i1480u, wlp);
  287. struct net_device *net_dev = i1480u->net_dev;
  288. i1480u->tx_inflight.threshold = 0;
  289. netif_stop_queue(net_dev);
  290. }
  291. /**
  292. * Start the network queue
  293. *
  294. * Enable WLP substack to start network queue. Also re-enable the flow
  295. * control to manage the queue again.
  296. *
  297. * We re-enable the flow control by storing the default threshold in the
  298. * flow control threshold. This means that if the user modified the
  299. * threshold before the queue was stopped and restarted that information
  300. * will be lost. FIXME?
  301. */
  302. void i1480u_start_queue(struct wlp *wlp)
  303. {
  304. struct i1480u *i1480u = container_of(wlp, struct i1480u, wlp);
  305. struct net_device *net_dev = i1480u->net_dev;
  306. i1480u->tx_inflight.threshold = i1480u_TX_INFLIGHT_THRESHOLD;
  307. netif_start_queue(net_dev);
  308. }