driver.c 23 KB

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