driver.c 27 KB

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