driver.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Generic probe/disconnect, reset and message passing
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  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, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. *
  23. *
  24. * See i2400m.h for driver documentation. This contains helpers for
  25. * the driver model glue [_setup()/_release()], handling device resets
  26. * [_dev_reset_handle()], and the backends for the WiMAX stack ops
  27. * reset [_op_reset()] and message from user [_op_msg_from_user()].
  28. *
  29. * ROADMAP:
  30. *
  31. * i2400m_op_msg_from_user()
  32. * i2400m_msg_to_dev()
  33. * wimax_msg_to_user_send()
  34. *
  35. * i2400m_op_reset()
  36. * i240m->bus_reset()
  37. *
  38. * i2400m_dev_reset_handle()
  39. * __i2400m_dev_reset_handle()
  40. * __i2400m_dev_stop()
  41. * __i2400m_dev_start()
  42. *
  43. * i2400m_setup()
  44. * i2400m->bus_setup()
  45. * i2400m_bootrom_init()
  46. * register_netdev()
  47. * wimax_dev_add()
  48. * i2400m_dev_start()
  49. * __i2400m_dev_start()
  50. * i2400m_dev_bootstrap()
  51. * i2400m_tx_setup()
  52. * i2400m->bus_dev_start()
  53. * i2400m_firmware_check()
  54. * i2400m_check_mac_addr()
  55. *
  56. * i2400m_release()
  57. * i2400m_dev_stop()
  58. * __i2400m_dev_stop()
  59. * i2400m_dev_shutdown()
  60. * i2400m->bus_dev_stop()
  61. * i2400m_tx_release()
  62. * i2400m->bus_release()
  63. * wimax_dev_rm()
  64. * unregister_netdev()
  65. */
  66. #include "i2400m.h"
  67. #include <linux/etherdevice.h>
  68. #include <linux/wimax/i2400m.h>
  69. #include <linux/module.h>
  70. #include <linux/moduleparam.h>
  71. #include <linux/suspend.h>
  72. #define D_SUBMODULE driver
  73. #include "debug-levels.h"
  74. int i2400m_idle_mode_disabled; /* 0 (idle mode enabled) by default */
  75. module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
  76. MODULE_PARM_DESC(idle_mode_disabled,
  77. "If true, the device will not enable idle mode negotiation "
  78. "with the base station (when connected) to save power.");
  79. int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
  80. module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
  81. MODULE_PARM_DESC(rx_reorder_disabled,
  82. "If true, RX reordering will be disabled.");
  83. int i2400m_power_save_disabled; /* 0 (power saving enabled) by default */
  84. module_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644);
  85. MODULE_PARM_DESC(power_save_disabled,
  86. "If true, the driver will not tell the device to enter "
  87. "power saving mode when it reports it is ready for it. "
  88. "False by default (so the device is told to do power "
  89. "saving).");
  90. static char i2400m_debug_params[128];
  91. module_param_string(debug, i2400m_debug_params, sizeof(i2400m_debug_params),
  92. 0644);
  93. MODULE_PARM_DESC(debug,
  94. "String of space-separated NAME:VALUE pairs, where NAMEs "
  95. "are the different debug submodules and VALUE are the "
  96. "initial debug value to set.");
  97. static char i2400m_barkers_params[128];
  98. module_param_string(barkers, i2400m_barkers_params,
  99. sizeof(i2400m_barkers_params), 0644);
  100. MODULE_PARM_DESC(barkers,
  101. "String of comma-separated 32-bit values; each is "
  102. "recognized as the value the device sends as a reboot "
  103. "signal; values are appended to a list--setting one value "
  104. "as zero cleans the existing list and starts a new one.");
  105. static
  106. struct i2400m_work *__i2400m_work_setup(
  107. struct i2400m *i2400m, void (*fn)(struct work_struct *),
  108. gfp_t gfp_flags, const void *pl, size_t pl_size)
  109. {
  110. struct i2400m_work *iw;
  111. iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
  112. if (iw == NULL)
  113. return NULL;
  114. iw->i2400m = i2400m_get(i2400m);
  115. iw->pl_size = pl_size;
  116. memcpy(iw->pl, pl, pl_size);
  117. INIT_WORK(&iw->ws, fn);
  118. return iw;
  119. }
  120. /**
  121. * i2400m_queue_work - schedule work on a i2400m's queue
  122. *
  123. * @i2400m: device descriptor
  124. *
  125. * @fn: function to run to execute work. It gets passed a 'struct
  126. * work_struct' that is wrapped in a 'struct i2400m_work'. Once
  127. * done, you have to (1) i2400m_put(i2400m_work->i2400m) and then
  128. * (2) kfree(i2400m_work).
  129. *
  130. * @gfp_flags: GFP flags for memory allocation.
  131. *
  132. * @pl: pointer to a payload buffer that you want to pass to the _work
  133. * function. Use this to pack (for example) a struct with extra
  134. * arguments.
  135. *
  136. * @pl_size: size of the payload buffer.
  137. *
  138. * We do this quite often, so this just saves typing; allocate a
  139. * wrapper for a i2400m, get a ref to it, pack arguments and launch
  140. * the work.
  141. *
  142. * A usual workflow is:
  143. *
  144. * struct my_work_args {
  145. * void *something;
  146. * int whatever;
  147. * };
  148. * ...
  149. *
  150. * struct my_work_args my_args = {
  151. * .something = FOO,
  152. * .whaetever = BLAH
  153. * };
  154. * i2400m_queue_work(i2400m, 1, my_work_function, GFP_KERNEL,
  155. * &args, sizeof(args))
  156. *
  157. * And now the work function can unpack the arguments and call the
  158. * real function (or do the job itself):
  159. *
  160. * static
  161. * void my_work_fn((struct work_struct *ws)
  162. * {
  163. * struct i2400m_work *iw =
  164. * container_of(ws, struct i2400m_work, ws);
  165. * struct my_work_args *my_args = (void *) iw->pl;
  166. *
  167. * my_work(iw->i2400m, my_args->something, my_args->whatevert);
  168. * }
  169. */
  170. int i2400m_queue_work(struct i2400m *i2400m,
  171. void (*fn)(struct work_struct *), gfp_t gfp_flags,
  172. const void *pl, size_t pl_size)
  173. {
  174. int result;
  175. struct i2400m_work *iw;
  176. BUG_ON(i2400m->work_queue == NULL);
  177. result = -ENOMEM;
  178. iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size);
  179. if (iw != NULL) {
  180. result = queue_work(i2400m->work_queue, &iw->ws);
  181. if (WARN_ON(result == 0))
  182. result = -ENXIO;
  183. }
  184. return result;
  185. }
  186. EXPORT_SYMBOL_GPL(i2400m_queue_work);
  187. /*
  188. * Schedule i2400m's specific work on the system's queue.
  189. *
  190. * Used for a few cases where we really need it; otherwise, identical
  191. * to i2400m_queue_work().
  192. *
  193. * Returns < 0 errno code on error, 1 if ok.
  194. *
  195. * If it returns zero, something really bad happened, as it means the
  196. * works struct was already queued, but we have just allocated it, so
  197. * it should not happen.
  198. */
  199. int i2400m_schedule_work(struct i2400m *i2400m,
  200. void (*fn)(struct work_struct *), gfp_t gfp_flags,
  201. const void *pl, size_t pl_size)
  202. {
  203. int result;
  204. struct i2400m_work *iw;
  205. result = -ENOMEM;
  206. iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size);
  207. if (iw != NULL) {
  208. result = schedule_work(&iw->ws);
  209. if (WARN_ON(result == 0))
  210. result = -ENXIO;
  211. }
  212. return result;
  213. }
  214. /*
  215. * WiMAX stack operation: relay a message from user space
  216. *
  217. * @wimax_dev: device descriptor
  218. * @pipe_name: named pipe the message is for
  219. * @msg_buf: pointer to the message bytes
  220. * @msg_len: length of the buffer
  221. * @genl_info: passed by the generic netlink layer
  222. *
  223. * The WiMAX stack will call this function when a message was received
  224. * from user space.
  225. *
  226. * For the i2400m, this is an L3L4 message, as specified in
  227. * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
  228. * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
  229. * coded in Little Endian.
  230. *
  231. * This function just verifies that the header declaration and the
  232. * payload are consistent and then deals with it, either forwarding it
  233. * to the device or procesing it locally.
  234. *
  235. * In the i2400m, messages are basically commands that will carry an
  236. * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
  237. * user space. The rx.c code might intercept the response and use it
  238. * to update the driver's state, but then it will pass it on so it can
  239. * be relayed back to user space.
  240. *
  241. * Note that asynchronous events from the device are processed and
  242. * sent to user space in rx.c.
  243. */
  244. static
  245. int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev,
  246. const char *pipe_name,
  247. const void *msg_buf, size_t msg_len,
  248. const struct genl_info *genl_info)
  249. {
  250. int result;
  251. struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  252. struct device *dev = i2400m_dev(i2400m);
  253. struct sk_buff *ack_skb;
  254. d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p "
  255. "msg_len %zu genl_info %p)\n", wimax_dev, i2400m,
  256. msg_buf, msg_len, genl_info);
  257. ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len);
  258. result = PTR_ERR(ack_skb);
  259. if (IS_ERR(ack_skb))
  260. goto error_msg_to_dev;
  261. result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
  262. error_msg_to_dev:
  263. d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
  264. "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
  265. genl_info, result);
  266. return result;
  267. }
  268. /*
  269. * Context to wait for a reset to finalize
  270. */
  271. struct i2400m_reset_ctx {
  272. struct completion completion;
  273. int result;
  274. };
  275. /*
  276. * WiMAX stack operation: reset a device
  277. *
  278. * @wimax_dev: device descriptor
  279. *
  280. * See the documentation for wimax_reset() and wimax_dev->op_reset for
  281. * the requirements of this function. The WiMAX stack guarantees
  282. * serialization on calls to this function.
  283. *
  284. * Do a warm reset on the device; if it fails, resort to a cold reset
  285. * and return -ENODEV. On successful warm reset, we need to block
  286. * until it is complete.
  287. *
  288. * The bus-driver implementation of reset takes care of falling back
  289. * to cold reset if warm fails.
  290. */
  291. static
  292. int i2400m_op_reset(struct wimax_dev *wimax_dev)
  293. {
  294. int result;
  295. struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  296. struct device *dev = i2400m_dev(i2400m);
  297. struct i2400m_reset_ctx ctx = {
  298. .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
  299. .result = 0,
  300. };
  301. d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
  302. mutex_lock(&i2400m->init_mutex);
  303. i2400m->reset_ctx = &ctx;
  304. mutex_unlock(&i2400m->init_mutex);
  305. result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
  306. if (result < 0)
  307. goto out;
  308. result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
  309. if (result == 0)
  310. result = -ETIMEDOUT;
  311. else if (result > 0)
  312. result = ctx.result;
  313. /* if result < 0, pass it on */
  314. mutex_lock(&i2400m->init_mutex);
  315. i2400m->reset_ctx = NULL;
  316. mutex_unlock(&i2400m->init_mutex);
  317. out:
  318. d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
  319. return result;
  320. }
  321. /*
  322. * Check the MAC address we got from boot mode is ok
  323. *
  324. * @i2400m: device descriptor
  325. *
  326. * Returns: 0 if ok, < 0 errno code on error.
  327. */
  328. static
  329. int i2400m_check_mac_addr(struct i2400m *i2400m)
  330. {
  331. int result;
  332. struct device *dev = i2400m_dev(i2400m);
  333. struct sk_buff *skb;
  334. const struct i2400m_tlv_detailed_device_info *ddi;
  335. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  336. const unsigned char zeromac[ETH_ALEN] = { 0 };
  337. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  338. skb = i2400m_get_device_info(i2400m);
  339. if (IS_ERR(skb)) {
  340. result = PTR_ERR(skb);
  341. dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
  342. result);
  343. goto error;
  344. }
  345. /* Extract MAC addresss */
  346. ddi = (void *) skb->data;
  347. BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
  348. d_printf(2, dev, "GET DEVICE INFO: mac addr "
  349. "%02x:%02x:%02x:%02x:%02x:%02x\n",
  350. ddi->mac_address[0], ddi->mac_address[1],
  351. ddi->mac_address[2], ddi->mac_address[3],
  352. ddi->mac_address[4], ddi->mac_address[5]);
  353. if (!memcmp(net_dev->perm_addr, ddi->mac_address,
  354. sizeof(ddi->mac_address)))
  355. goto ok;
  356. dev_warn(dev, "warning: device reports a different MAC address "
  357. "to that of boot mode's\n");
  358. dev_warn(dev, "device reports %02x:%02x:%02x:%02x:%02x:%02x\n",
  359. ddi->mac_address[0], ddi->mac_address[1],
  360. ddi->mac_address[2], ddi->mac_address[3],
  361. ddi->mac_address[4], ddi->mac_address[5]);
  362. dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n",
  363. net_dev->perm_addr[0], net_dev->perm_addr[1],
  364. net_dev->perm_addr[2], net_dev->perm_addr[3],
  365. net_dev->perm_addr[4], net_dev->perm_addr[5]);
  366. if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
  367. dev_err(dev, "device reports an invalid MAC address, "
  368. "not updating\n");
  369. else {
  370. dev_warn(dev, "updating MAC address\n");
  371. net_dev->addr_len = ETH_ALEN;
  372. memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
  373. memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
  374. }
  375. ok:
  376. result = 0;
  377. kfree_skb(skb);
  378. error:
  379. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  380. return result;
  381. }
  382. /**
  383. * __i2400m_dev_start - Bring up driver communication with the device
  384. *
  385. * @i2400m: device descriptor
  386. * @flags: boot mode flags
  387. *
  388. * Returns: 0 if ok, < 0 errno code on error.
  389. *
  390. * Uploads firmware and brings up all the resources needed to be able
  391. * to communicate with the device.
  392. *
  393. * The workqueue has to be setup early, at least before RX handling
  394. * (it's only real user for now) so it can process reports as they
  395. * arrive. We also want to destroy it if we retry, to make sure it is
  396. * flushed...easier like this.
  397. *
  398. * TX needs to be setup before the bus-specific code (otherwise on
  399. * shutdown, the bus-tx code could try to access it).
  400. */
  401. static
  402. int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
  403. {
  404. int result;
  405. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  406. struct net_device *net_dev = wimax_dev->net_dev;
  407. struct device *dev = i2400m_dev(i2400m);
  408. int times = i2400m->bus_bm_retries;
  409. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  410. retry:
  411. result = i2400m_dev_bootstrap(i2400m, flags);
  412. if (result < 0) {
  413. dev_err(dev, "cannot bootstrap device: %d\n", result);
  414. goto error_bootstrap;
  415. }
  416. result = i2400m_tx_setup(i2400m);
  417. if (result < 0)
  418. goto error_tx_setup;
  419. result = i2400m_rx_setup(i2400m);
  420. if (result < 0)
  421. goto error_rx_setup;
  422. i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
  423. if (i2400m->work_queue == NULL) {
  424. result = -ENOMEM;
  425. dev_err(dev, "cannot create workqueue\n");
  426. goto error_create_workqueue;
  427. }
  428. result = i2400m->bus_dev_start(i2400m);
  429. if (result < 0)
  430. goto error_bus_dev_start;
  431. i2400m->ready = 1;
  432. wmb(); /* see i2400m->ready's documentation */
  433. result = i2400m_firmware_check(i2400m); /* fw versions ok? */
  434. if (result < 0)
  435. goto error_fw_check;
  436. /* At this point is ok to send commands to the device */
  437. result = i2400m_check_mac_addr(i2400m);
  438. if (result < 0)
  439. goto error_check_mac_addr;
  440. wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
  441. result = i2400m_dev_initialize(i2400m);
  442. if (result < 0)
  443. goto error_dev_initialize;
  444. /* At this point, reports will come for the device and set it
  445. * to the right state if it is different than UNINITIALIZED */
  446. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  447. net_dev, i2400m, result);
  448. return result;
  449. error_dev_initialize:
  450. error_check_mac_addr:
  451. i2400m->ready = 0;
  452. wmb(); /* see i2400m->ready's documentation */
  453. flush_workqueue(i2400m->work_queue);
  454. error_fw_check:
  455. i2400m->bus_dev_stop(i2400m);
  456. error_bus_dev_start:
  457. destroy_workqueue(i2400m->work_queue);
  458. error_create_workqueue:
  459. i2400m_rx_release(i2400m);
  460. error_rx_setup:
  461. i2400m_tx_release(i2400m);
  462. error_tx_setup:
  463. error_bootstrap:
  464. if (result == -EL3RST && times-- > 0) {
  465. flags = I2400M_BRI_SOFT|I2400M_BRI_MAC_REINIT;
  466. goto retry;
  467. }
  468. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  469. net_dev, i2400m, result);
  470. return result;
  471. }
  472. static
  473. int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  474. {
  475. int result = 0;
  476. mutex_lock(&i2400m->init_mutex); /* Well, start the device */
  477. if (i2400m->updown == 0) {
  478. result = __i2400m_dev_start(i2400m, bm_flags);
  479. if (result >= 0) {
  480. i2400m->updown = 1;
  481. wmb(); /* see i2400m->updown's documentation */
  482. }
  483. }
  484. mutex_unlock(&i2400m->init_mutex);
  485. return result;
  486. }
  487. /**
  488. * i2400m_dev_stop - Tear down driver communication with the device
  489. *
  490. * @i2400m: device descriptor
  491. *
  492. * Returns: 0 if ok, < 0 errno code on error.
  493. *
  494. * Releases all the resources allocated to communicate with the
  495. * device. Note we cannot destroy the workqueue earlier as until RX is
  496. * fully destroyed, it could still try to schedule jobs.
  497. */
  498. static
  499. void __i2400m_dev_stop(struct i2400m *i2400m)
  500. {
  501. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  502. struct device *dev = i2400m_dev(i2400m);
  503. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  504. wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
  505. i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
  506. complete(&i2400m->msg_completion);
  507. i2400m_net_wake_stop(i2400m);
  508. i2400m_dev_shutdown(i2400m);
  509. /*
  510. * Make sure no report hooks are running *before* we stop the
  511. * communication infrastructure with the device.
  512. */
  513. i2400m->ready = 0; /* nobody can queue work anymore */
  514. wmb(); /* see i2400m->ready's documentation */
  515. flush_workqueue(i2400m->work_queue);
  516. i2400m->bus_dev_stop(i2400m);
  517. destroy_workqueue(i2400m->work_queue);
  518. i2400m_rx_release(i2400m);
  519. i2400m_tx_release(i2400m);
  520. wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
  521. d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
  522. }
  523. /*
  524. * Watch out -- we only need to stop if there is a need for it. The
  525. * device could have reset itself and failed to come up again (see
  526. * _i2400m_dev_reset_handle()).
  527. */
  528. static
  529. void i2400m_dev_stop(struct i2400m *i2400m)
  530. {
  531. mutex_lock(&i2400m->init_mutex);
  532. if (i2400m->updown) {
  533. __i2400m_dev_stop(i2400m);
  534. i2400m->updown = 0;
  535. wmb(); /* see i2400m->updown's documentation */
  536. }
  537. mutex_unlock(&i2400m->init_mutex);
  538. }
  539. /*
  540. * Listen to PM events to cache the firmware before suspend/hibernation
  541. *
  542. * When the device comes out of suspend, it might go into reset and
  543. * firmware has to be uploaded again. At resume, most of the times, we
  544. * can't load firmware images from disk, so we need to cache it.
  545. *
  546. * i2400m_fw_cache() will allocate a kobject and attach the firmware
  547. * to it; that way we don't have to worry too much about the fw loader
  548. * hitting a race condition.
  549. *
  550. * Note: modus operandi stolen from the Orinoco driver; thx.
  551. */
  552. static
  553. int i2400m_pm_notifier(struct notifier_block *notifier,
  554. unsigned long pm_event,
  555. void *unused)
  556. {
  557. struct i2400m *i2400m =
  558. container_of(notifier, struct i2400m, pm_notifier);
  559. struct device *dev = i2400m_dev(i2400m);
  560. d_fnstart(3, dev, "(i2400m %p pm_event %lx)\n", i2400m, pm_event);
  561. switch (pm_event) {
  562. case PM_HIBERNATION_PREPARE:
  563. case PM_SUSPEND_PREPARE:
  564. i2400m_fw_cache(i2400m);
  565. break;
  566. case PM_POST_RESTORE:
  567. /* Restore from hibernation failed. We need to clean
  568. * up in exactly the same way, so fall through. */
  569. case PM_POST_HIBERNATION:
  570. case PM_POST_SUSPEND:
  571. i2400m_fw_uncache(i2400m);
  572. break;
  573. case PM_RESTORE_PREPARE:
  574. default:
  575. break;
  576. }
  577. d_fnend(3, dev, "(i2400m %p pm_event %lx) = void\n", i2400m, pm_event);
  578. return NOTIFY_DONE;
  579. }
  580. /*
  581. * pre-reset is called before a device is going on reset
  582. *
  583. * This has to be followed by a call to i2400m_post_reset(), otherwise
  584. * bad things might happen.
  585. */
  586. int i2400m_pre_reset(struct i2400m *i2400m)
  587. {
  588. int result;
  589. struct device *dev = i2400m_dev(i2400m);
  590. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  591. d_printf(1, dev, "pre-reset shut down\n");
  592. result = 0;
  593. mutex_lock(&i2400m->init_mutex);
  594. if (i2400m->updown) {
  595. netif_tx_disable(i2400m->wimax_dev.net_dev);
  596. __i2400m_dev_stop(i2400m);
  597. result = 0;
  598. /* down't set updown to zero -- this way
  599. * post_reset can restore properly */
  600. }
  601. mutex_unlock(&i2400m->init_mutex);
  602. if (i2400m->bus_release)
  603. i2400m->bus_release(i2400m);
  604. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  605. return result;
  606. }
  607. EXPORT_SYMBOL_GPL(i2400m_pre_reset);
  608. /*
  609. * Restore device state after a reset
  610. *
  611. * Do the work needed after a device reset to bring it up to the same
  612. * state as it was before the reset.
  613. *
  614. * NOTE: this requires i2400m->init_mutex taken
  615. */
  616. int i2400m_post_reset(struct i2400m *i2400m)
  617. {
  618. int result = 0;
  619. struct device *dev = i2400m_dev(i2400m);
  620. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  621. d_printf(1, dev, "post-reset start\n");
  622. if (i2400m->bus_setup) {
  623. result = i2400m->bus_setup(i2400m);
  624. if (result < 0) {
  625. dev_err(dev, "bus-specific setup failed: %d\n",
  626. result);
  627. goto error_bus_setup;
  628. }
  629. }
  630. mutex_lock(&i2400m->init_mutex);
  631. if (i2400m->updown) {
  632. result = __i2400m_dev_start(
  633. i2400m, I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
  634. if (result < 0)
  635. goto error_dev_start;
  636. }
  637. mutex_unlock(&i2400m->init_mutex);
  638. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  639. return result;
  640. error_dev_start:
  641. if (i2400m->bus_release)
  642. i2400m->bus_release(i2400m);
  643. error_bus_setup:
  644. /* even if the device was up, it could not be recovered, so we
  645. * mark it as down. */
  646. i2400m->updown = 0;
  647. wmb(); /* see i2400m->updown's documentation */
  648. mutex_unlock(&i2400m->init_mutex);
  649. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  650. return result;
  651. }
  652. EXPORT_SYMBOL_GPL(i2400m_post_reset);
  653. /*
  654. * The device has rebooted; fix up the device and the driver
  655. *
  656. * Tear down the driver communication with the device, reload the
  657. * firmware and reinitialize the communication with the device.
  658. *
  659. * If someone calls a reset when the device's firmware is down, in
  660. * theory we won't see it because we are not listening. However, just
  661. * in case, leave the code to handle it.
  662. *
  663. * If there is a reset context, use it; this means someone is waiting
  664. * for us to tell him when the reset operation is complete and the
  665. * device is ready to rock again.
  666. *
  667. * NOTE: if we are in the process of bringing up or down the
  668. * communication with the device [running i2400m_dev_start() or
  669. * _stop()], don't do anything, let it fail and handle it.
  670. *
  671. * This function is ran always in a thread context
  672. *
  673. * This function gets passed, as payload to i2400m_work() a 'const
  674. * char *' ptr with a "reason" why the reset happened (for messages).
  675. */
  676. static
  677. void __i2400m_dev_reset_handle(struct work_struct *ws)
  678. {
  679. int result;
  680. struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
  681. const char *reason;
  682. struct i2400m *i2400m = iw->i2400m;
  683. struct device *dev = i2400m_dev(i2400m);
  684. struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
  685. if (WARN_ON(iw->pl_size != sizeof(reason)))
  686. reason = "SW BUG: reason n/a";
  687. else
  688. memcpy(&reason, iw->pl, sizeof(reason));
  689. d_fnstart(3, dev, "(ws %p i2400m %p reason %s)\n", ws, i2400m, reason);
  690. result = 0;
  691. if (mutex_trylock(&i2400m->init_mutex) == 0) {
  692. /* We are still in i2400m_dev_start() [let it fail] or
  693. * i2400m_dev_stop() [we are shutting down anyway, so
  694. * ignore it] or we are resetting somewhere else. */
  695. dev_err(dev, "device rebooted somewhere else?\n");
  696. i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
  697. complete(&i2400m->msg_completion);
  698. goto out;
  699. }
  700. if (i2400m->updown == 0) {
  701. dev_info(dev, "%s: device is down, doing nothing\n", reason);
  702. goto out_unlock;
  703. }
  704. dev_err(dev, "%s: reinitializing driver\n", reason);
  705. __i2400m_dev_stop(i2400m);
  706. result = __i2400m_dev_start(i2400m,
  707. I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
  708. if (result < 0) {
  709. i2400m->updown = 0;
  710. wmb(); /* see i2400m->updown's documentation */
  711. dev_err(dev, "%s: cannot start the device: %d\n",
  712. reason, result);
  713. result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
  714. if (result >= 0)
  715. result = -ENODEV;
  716. }
  717. out_unlock:
  718. if (i2400m->reset_ctx) {
  719. ctx->result = result;
  720. complete(&ctx->completion);
  721. }
  722. mutex_unlock(&i2400m->init_mutex);
  723. out:
  724. i2400m_put(i2400m);
  725. kfree(iw);
  726. d_fnend(3, dev, "(ws %p i2400m %p reason %s) = void\n",
  727. ws, i2400m, reason);
  728. return;
  729. }
  730. /**
  731. * i2400m_dev_reset_handle - Handle a device's reset in a thread context
  732. *
  733. * Schedule a device reset handling out on a thread context, so it
  734. * is safe to call from atomic context. We can't use the i2400m's
  735. * queue as we are going to destroy it and reinitialize it as part of
  736. * the driver bringup/bringup process.
  737. *
  738. * See __i2400m_dev_reset_handle() for details; that takes care of
  739. * reinitializing the driver to handle the reset, calling into the
  740. * bus-specific functions ops as needed.
  741. */
  742. int i2400m_dev_reset_handle(struct i2400m *i2400m, const char *reason)
  743. {
  744. i2400m->boot_mode = 1;
  745. wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
  746. return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
  747. GFP_ATOMIC, &reason, sizeof(reason));
  748. }
  749. EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
  750. /*
  751. * Alloc the command and ack buffers for boot mode
  752. *
  753. * Get the buffers needed to deal with boot mode messages. These
  754. * buffers need to be allocated before the sdio recieve irq is setup.
  755. */
  756. static
  757. int i2400m_bm_buf_alloc(struct i2400m *i2400m)
  758. {
  759. int result;
  760. result = -ENOMEM;
  761. i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
  762. if (i2400m->bm_cmd_buf == NULL)
  763. goto error_bm_cmd_kzalloc;
  764. i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
  765. if (i2400m->bm_ack_buf == NULL)
  766. goto error_bm_ack_buf_kzalloc;
  767. return 0;
  768. error_bm_ack_buf_kzalloc:
  769. kfree(i2400m->bm_cmd_buf);
  770. error_bm_cmd_kzalloc:
  771. return result;
  772. }
  773. /*
  774. * Free boot mode command and ack buffers.
  775. */
  776. static
  777. void i2400m_bm_buf_free(struct i2400m *i2400m)
  778. {
  779. kfree(i2400m->bm_ack_buf);
  780. kfree(i2400m->bm_cmd_buf);
  781. }
  782. /**
  783. * i2400m_setup - bus-generic setup function for the i2400m device
  784. *
  785. * @i2400m: device descriptor (bus-specific parts have been initialized)
  786. *
  787. * Returns: 0 if ok, < 0 errno code on error.
  788. *
  789. * Sets up basic device comunication infrastructure, boots the ROM to
  790. * read the MAC address, registers with the WiMAX and network stacks
  791. * and then brings up the device.
  792. */
  793. int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  794. {
  795. int result = -ENODEV;
  796. struct device *dev = i2400m_dev(i2400m);
  797. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  798. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  799. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  800. snprintf(wimax_dev->name, sizeof(wimax_dev->name),
  801. "i2400m-%s:%s", dev->bus->name, dev_name(dev));
  802. result = i2400m_bm_buf_alloc(i2400m);
  803. if (result < 0) {
  804. dev_err(dev, "cannot allocate bootmode scratch buffers\n");
  805. goto error_bm_buf_alloc;
  806. }
  807. if (i2400m->bus_setup) {
  808. result = i2400m->bus_setup(i2400m);
  809. if (result < 0) {
  810. dev_err(dev, "bus-specific setup failed: %d\n",
  811. result);
  812. goto error_bus_setup;
  813. }
  814. }
  815. result = i2400m_bootrom_init(i2400m, bm_flags);
  816. if (result < 0) {
  817. dev_err(dev, "read mac addr: bootrom init "
  818. "failed: %d\n", result);
  819. goto error_bootrom_init;
  820. }
  821. result = i2400m_read_mac_addr(i2400m);
  822. if (result < 0)
  823. goto error_read_mac_addr;
  824. random_ether_addr(i2400m->src_mac_addr);
  825. i2400m->pm_notifier.notifier_call = i2400m_pm_notifier;
  826. register_pm_notifier(&i2400m->pm_notifier);
  827. result = register_netdev(net_dev); /* Okey dokey, bring it up */
  828. if (result < 0) {
  829. dev_err(dev, "cannot register i2400m network device: %d\n",
  830. result);
  831. goto error_register_netdev;
  832. }
  833. netif_carrier_off(net_dev);
  834. i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
  835. i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
  836. i2400m->wimax_dev.op_reset = i2400m_op_reset;
  837. result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
  838. if (result < 0)
  839. goto error_wimax_dev_add;
  840. /* Now setup all that requires a registered net and wimax device. */
  841. result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
  842. if (result < 0) {
  843. dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
  844. goto error_sysfs_setup;
  845. }
  846. result = i2400m_debugfs_add(i2400m);
  847. if (result < 0) {
  848. dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
  849. goto error_debugfs_setup;
  850. }
  851. result = i2400m_dev_start(i2400m, bm_flags);
  852. if (result < 0)
  853. goto error_dev_start;
  854. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  855. return result;
  856. error_dev_start:
  857. i2400m_debugfs_rm(i2400m);
  858. error_debugfs_setup:
  859. sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
  860. &i2400m_dev_attr_group);
  861. error_sysfs_setup:
  862. wimax_dev_rm(&i2400m->wimax_dev);
  863. error_wimax_dev_add:
  864. unregister_netdev(net_dev);
  865. error_register_netdev:
  866. unregister_pm_notifier(&i2400m->pm_notifier);
  867. error_read_mac_addr:
  868. error_bootrom_init:
  869. if (i2400m->bus_release)
  870. i2400m->bus_release(i2400m);
  871. error_bus_setup:
  872. i2400m_bm_buf_free(i2400m);
  873. error_bm_buf_alloc:
  874. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  875. return result;
  876. }
  877. EXPORT_SYMBOL_GPL(i2400m_setup);
  878. /**
  879. * i2400m_release - release the bus-generic driver resources
  880. *
  881. * Sends a disconnect message and undoes any setup done by i2400m_setup()
  882. */
  883. void i2400m_release(struct i2400m *i2400m)
  884. {
  885. struct device *dev = i2400m_dev(i2400m);
  886. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  887. netif_stop_queue(i2400m->wimax_dev.net_dev);
  888. i2400m_dev_stop(i2400m);
  889. i2400m_debugfs_rm(i2400m);
  890. sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
  891. &i2400m_dev_attr_group);
  892. wimax_dev_rm(&i2400m->wimax_dev);
  893. unregister_netdev(i2400m->wimax_dev.net_dev);
  894. unregister_pm_notifier(&i2400m->pm_notifier);
  895. if (i2400m->bus_release)
  896. i2400m->bus_release(i2400m);
  897. i2400m_bm_buf_free(i2400m);
  898. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  899. }
  900. EXPORT_SYMBOL_GPL(i2400m_release);
  901. /*
  902. * Debug levels control; see debug.h
  903. */
  904. struct d_level D_LEVEL[] = {
  905. D_SUBMODULE_DEFINE(control),
  906. D_SUBMODULE_DEFINE(driver),
  907. D_SUBMODULE_DEFINE(debugfs),
  908. D_SUBMODULE_DEFINE(fw),
  909. D_SUBMODULE_DEFINE(netdev),
  910. D_SUBMODULE_DEFINE(rfkill),
  911. D_SUBMODULE_DEFINE(rx),
  912. D_SUBMODULE_DEFINE(sysfs),
  913. D_SUBMODULE_DEFINE(tx),
  914. };
  915. size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
  916. static
  917. int __init i2400m_driver_init(void)
  918. {
  919. d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params,
  920. "i2400m.debug");
  921. return i2400m_barker_db_init(i2400m_barkers_params);
  922. }
  923. module_init(i2400m_driver_init);
  924. static
  925. void __exit i2400m_driver_exit(void)
  926. {
  927. /* for scheds i2400m_dev_reset_handle() */
  928. flush_scheduled_work();
  929. i2400m_barker_db_exit();
  930. return;
  931. }
  932. module_exit(i2400m_driver_exit);
  933. MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
  934. MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
  935. MODULE_LICENSE("GPL");