driver.c 29 KB

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