driver.c 29 KB

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