driver.c 28 KB

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