driver.c 25 KB

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