driver.c 29 KB

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