driver.c 30 KB

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