driver.c 29 KB

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