driver.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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_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 %pM\n",
  282. ddi->mac_address);
  283. if (!memcmp(net_dev->perm_addr, ddi->mac_address,
  284. sizeof(ddi->mac_address)))
  285. goto ok;
  286. dev_warn(dev, "warning: device reports a different MAC address "
  287. "to that of boot mode's\n");
  288. dev_warn(dev, "device reports %pM\n", ddi->mac_address);
  289. dev_warn(dev, "boot mode reported %pM\n", net_dev->perm_addr);
  290. if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
  291. dev_err(dev, "device reports an invalid MAC address, "
  292. "not updating\n");
  293. else {
  294. dev_warn(dev, "updating MAC address\n");
  295. net_dev->addr_len = ETH_ALEN;
  296. memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
  297. memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
  298. }
  299. ok:
  300. result = 0;
  301. kfree_skb(skb);
  302. error:
  303. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  304. return result;
  305. }
  306. /**
  307. * __i2400m_dev_start - Bring up driver communication with the device
  308. *
  309. * @i2400m: device descriptor
  310. * @flags: boot mode flags
  311. *
  312. * Returns: 0 if ok, < 0 errno code on error.
  313. *
  314. * Uploads firmware and brings up all the resources needed to be able
  315. * to communicate with the device.
  316. *
  317. * The workqueue has to be setup early, at least before RX handling
  318. * (it's only real user for now) so it can process reports as they
  319. * arrive. We also want to destroy it if we retry, to make sure it is
  320. * flushed...easier like this.
  321. *
  322. * TX needs to be setup before the bus-specific code (otherwise on
  323. * shutdown, the bus-tx code could try to access it).
  324. */
  325. static
  326. int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
  327. {
  328. int result;
  329. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  330. struct net_device *net_dev = wimax_dev->net_dev;
  331. struct device *dev = i2400m_dev(i2400m);
  332. int times = i2400m->bus_bm_retries;
  333. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  334. retry:
  335. result = i2400m_dev_bootstrap(i2400m, flags);
  336. if (result < 0) {
  337. dev_err(dev, "cannot bootstrap device: %d\n", result);
  338. goto error_bootstrap;
  339. }
  340. result = i2400m_tx_setup(i2400m);
  341. if (result < 0)
  342. goto error_tx_setup;
  343. result = i2400m_rx_setup(i2400m);
  344. if (result < 0)
  345. goto error_rx_setup;
  346. i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
  347. if (i2400m->work_queue == NULL) {
  348. result = -ENOMEM;
  349. dev_err(dev, "cannot create workqueue\n");
  350. goto error_create_workqueue;
  351. }
  352. if (i2400m->bus_dev_start) {
  353. result = i2400m->bus_dev_start(i2400m);
  354. if (result < 0)
  355. goto error_bus_dev_start;
  356. }
  357. i2400m->ready = 1;
  358. wmb(); /* see i2400m->ready's documentation */
  359. /* process pending reports from the device */
  360. queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
  361. result = i2400m_firmware_check(i2400m); /* fw versions ok? */
  362. if (result < 0)
  363. goto error_fw_check;
  364. /* At this point is ok to send commands to the device */
  365. result = i2400m_check_mac_addr(i2400m);
  366. if (result < 0)
  367. goto error_check_mac_addr;
  368. result = i2400m_dev_initialize(i2400m);
  369. if (result < 0)
  370. goto error_dev_initialize;
  371. /* At this point, reports will come for the device and set it
  372. * to the right state if it is different than UNINITIALIZED */
  373. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  374. net_dev, i2400m, result);
  375. return result;
  376. error_dev_initialize:
  377. error_check_mac_addr:
  378. i2400m->ready = 0;
  379. wmb(); /* see i2400m->ready's documentation */
  380. flush_workqueue(i2400m->work_queue);
  381. error_fw_check:
  382. if (i2400m->bus_dev_stop)
  383. i2400m->bus_dev_stop(i2400m);
  384. error_bus_dev_start:
  385. destroy_workqueue(i2400m->work_queue);
  386. error_create_workqueue:
  387. i2400m_rx_release(i2400m);
  388. error_rx_setup:
  389. i2400m_tx_release(i2400m);
  390. error_tx_setup:
  391. error_bootstrap:
  392. if (result == -EL3RST && times-- > 0) {
  393. flags = I2400M_BRI_SOFT|I2400M_BRI_MAC_REINIT;
  394. goto retry;
  395. }
  396. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  397. net_dev, i2400m, result);
  398. return result;
  399. }
  400. static
  401. int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  402. {
  403. int result = 0;
  404. mutex_lock(&i2400m->init_mutex); /* Well, start the device */
  405. if (i2400m->updown == 0) {
  406. result = __i2400m_dev_start(i2400m, bm_flags);
  407. if (result >= 0) {
  408. i2400m->updown = 1;
  409. wmb(); /* see i2400m->updown's documentation */
  410. }
  411. }
  412. mutex_unlock(&i2400m->init_mutex);
  413. return result;
  414. }
  415. /**
  416. * i2400m_dev_stop - Tear down driver communication with the device
  417. *
  418. * @i2400m: device descriptor
  419. *
  420. * Returns: 0 if ok, < 0 errno code on error.
  421. *
  422. * Releases all the resources allocated to communicate with the
  423. * device. Note we cannot destroy the workqueue earlier as until RX is
  424. * fully destroyed, it could still try to schedule jobs.
  425. */
  426. static
  427. void __i2400m_dev_stop(struct i2400m *i2400m)
  428. {
  429. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  430. struct device *dev = i2400m_dev(i2400m);
  431. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  432. wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
  433. i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
  434. complete(&i2400m->msg_completion);
  435. i2400m_net_wake_stop(i2400m);
  436. i2400m_dev_shutdown(i2400m);
  437. /*
  438. * Make sure no report hooks are running *before* we stop the
  439. * communication infrastructure with the device.
  440. */
  441. i2400m->ready = 0; /* nobody can queue work anymore */
  442. wmb(); /* see i2400m->ready's documentation */
  443. flush_workqueue(i2400m->work_queue);
  444. if (i2400m->bus_dev_stop)
  445. i2400m->bus_dev_stop(i2400m);
  446. destroy_workqueue(i2400m->work_queue);
  447. i2400m_rx_release(i2400m);
  448. i2400m_tx_release(i2400m);
  449. wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
  450. d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
  451. }
  452. /*
  453. * Watch out -- we only need to stop if there is a need for it. The
  454. * device could have reset itself and failed to come up again (see
  455. * _i2400m_dev_reset_handle()).
  456. */
  457. static
  458. void i2400m_dev_stop(struct i2400m *i2400m)
  459. {
  460. mutex_lock(&i2400m->init_mutex);
  461. if (i2400m->updown) {
  462. __i2400m_dev_stop(i2400m);
  463. i2400m->updown = 0;
  464. wmb(); /* see i2400m->updown's documentation */
  465. }
  466. mutex_unlock(&i2400m->init_mutex);
  467. }
  468. /*
  469. * Listen to PM events to cache the firmware before suspend/hibernation
  470. *
  471. * When the device comes out of suspend, it might go into reset and
  472. * firmware has to be uploaded again. At resume, most of the times, we
  473. * can't load firmware images from disk, so we need to cache it.
  474. *
  475. * i2400m_fw_cache() will allocate a kobject and attach the firmware
  476. * to it; that way we don't have to worry too much about the fw loader
  477. * hitting a race condition.
  478. *
  479. * Note: modus operandi stolen from the Orinoco driver; thx.
  480. */
  481. static
  482. int i2400m_pm_notifier(struct notifier_block *notifier,
  483. unsigned long pm_event,
  484. void *unused)
  485. {
  486. struct i2400m *i2400m =
  487. container_of(notifier, struct i2400m, pm_notifier);
  488. struct device *dev = i2400m_dev(i2400m);
  489. d_fnstart(3, dev, "(i2400m %p pm_event %lx)\n", i2400m, pm_event);
  490. switch (pm_event) {
  491. case PM_HIBERNATION_PREPARE:
  492. case PM_SUSPEND_PREPARE:
  493. i2400m_fw_cache(i2400m);
  494. break;
  495. case PM_POST_RESTORE:
  496. /* Restore from hibernation failed. We need to clean
  497. * up in exactly the same way, so fall through. */
  498. case PM_POST_HIBERNATION:
  499. case PM_POST_SUSPEND:
  500. i2400m_fw_uncache(i2400m);
  501. break;
  502. case PM_RESTORE_PREPARE:
  503. default:
  504. break;
  505. }
  506. d_fnend(3, dev, "(i2400m %p pm_event %lx) = void\n", i2400m, pm_event);
  507. return NOTIFY_DONE;
  508. }
  509. /*
  510. * pre-reset is called before a device is going on reset
  511. *
  512. * This has to be followed by a call to i2400m_post_reset(), otherwise
  513. * bad things might happen.
  514. */
  515. int i2400m_pre_reset(struct i2400m *i2400m)
  516. {
  517. int result;
  518. struct device *dev = i2400m_dev(i2400m);
  519. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  520. d_printf(1, dev, "pre-reset shut down\n");
  521. result = 0;
  522. mutex_lock(&i2400m->init_mutex);
  523. if (i2400m->updown) {
  524. netif_tx_disable(i2400m->wimax_dev.net_dev);
  525. __i2400m_dev_stop(i2400m);
  526. result = 0;
  527. /* down't set updown to zero -- this way
  528. * post_reset can restore properly */
  529. }
  530. mutex_unlock(&i2400m->init_mutex);
  531. if (i2400m->bus_release)
  532. i2400m->bus_release(i2400m);
  533. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  534. return result;
  535. }
  536. EXPORT_SYMBOL_GPL(i2400m_pre_reset);
  537. /*
  538. * Restore device state after a reset
  539. *
  540. * Do the work needed after a device reset to bring it up to the same
  541. * state as it was before the reset.
  542. *
  543. * NOTE: this requires i2400m->init_mutex taken
  544. */
  545. int i2400m_post_reset(struct i2400m *i2400m)
  546. {
  547. int result = 0;
  548. struct device *dev = i2400m_dev(i2400m);
  549. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  550. d_printf(1, dev, "post-reset start\n");
  551. if (i2400m->bus_setup) {
  552. result = i2400m->bus_setup(i2400m);
  553. if (result < 0) {
  554. dev_err(dev, "bus-specific setup failed: %d\n",
  555. result);
  556. goto error_bus_setup;
  557. }
  558. }
  559. mutex_lock(&i2400m->init_mutex);
  560. if (i2400m->updown) {
  561. result = __i2400m_dev_start(
  562. i2400m, I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
  563. if (result < 0)
  564. goto error_dev_start;
  565. }
  566. mutex_unlock(&i2400m->init_mutex);
  567. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  568. return result;
  569. error_dev_start:
  570. if (i2400m->bus_release)
  571. i2400m->bus_release(i2400m);
  572. error_bus_setup:
  573. /* even if the device was up, it could not be recovered, so we
  574. * mark it as down. */
  575. i2400m->updown = 0;
  576. wmb(); /* see i2400m->updown's documentation */
  577. mutex_unlock(&i2400m->init_mutex);
  578. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  579. return result;
  580. }
  581. EXPORT_SYMBOL_GPL(i2400m_post_reset);
  582. /*
  583. * The device has rebooted; fix up the device and the driver
  584. *
  585. * Tear down the driver communication with the device, reload the
  586. * firmware and reinitialize the communication with the device.
  587. *
  588. * If someone calls a reset when the device's firmware is down, in
  589. * theory we won't see it because we are not listening. However, just
  590. * in case, leave the code to handle it.
  591. *
  592. * If there is a reset context, use it; this means someone is waiting
  593. * for us to tell him when the reset operation is complete and the
  594. * device is ready to rock again.
  595. *
  596. * NOTE: if we are in the process of bringing up or down the
  597. * communication with the device [running i2400m_dev_start() or
  598. * _stop()], don't do anything, let it fail and handle it.
  599. *
  600. * This function is ran always in a thread context
  601. *
  602. * This function gets passed, as payload to i2400m_work() a 'const
  603. * char *' ptr with a "reason" why the reset happened (for messages).
  604. */
  605. static
  606. void __i2400m_dev_reset_handle(struct work_struct *ws)
  607. {
  608. int result;
  609. struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
  610. const char *reason;
  611. struct i2400m *i2400m = iw->i2400m;
  612. struct device *dev = i2400m_dev(i2400m);
  613. struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
  614. if (WARN_ON(iw->pl_size != sizeof(reason)))
  615. reason = "SW BUG: reason n/a";
  616. else
  617. memcpy(&reason, iw->pl, sizeof(reason));
  618. d_fnstart(3, dev, "(ws %p i2400m %p reason %s)\n", ws, i2400m, reason);
  619. result = 0;
  620. if (mutex_trylock(&i2400m->init_mutex) == 0) {
  621. /* We are still in i2400m_dev_start() [let it fail] or
  622. * i2400m_dev_stop() [we are shutting down anyway, so
  623. * ignore it] or we are resetting somewhere else. */
  624. dev_err(dev, "device rebooted somewhere else?\n");
  625. i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
  626. complete(&i2400m->msg_completion);
  627. goto out;
  628. }
  629. if (i2400m->updown == 0) {
  630. dev_info(dev, "%s: device is down, doing nothing\n", reason);
  631. goto out_unlock;
  632. }
  633. dev_err(dev, "%s: reinitializing driver\n", reason);
  634. __i2400m_dev_stop(i2400m);
  635. result = __i2400m_dev_start(i2400m,
  636. I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
  637. if (result < 0) {
  638. i2400m->updown = 0;
  639. wmb(); /* see i2400m->updown's documentation */
  640. dev_err(dev, "%s: cannot start the device: %d\n",
  641. reason, result);
  642. result = -EUCLEAN;
  643. }
  644. out_unlock:
  645. if (i2400m->reset_ctx) {
  646. ctx->result = result;
  647. complete(&ctx->completion);
  648. }
  649. mutex_unlock(&i2400m->init_mutex);
  650. if (result == -EUCLEAN) {
  651. /* ops, need to clean up [w/ init_mutex not held] */
  652. result = i2400m_reset(i2400m, I2400M_RT_BUS);
  653. if (result >= 0)
  654. result = -ENODEV;
  655. }
  656. out:
  657. i2400m_put(i2400m);
  658. kfree(iw);
  659. d_fnend(3, dev, "(ws %p i2400m %p reason %s) = void\n",
  660. ws, i2400m, reason);
  661. return;
  662. }
  663. /**
  664. * i2400m_dev_reset_handle - Handle a device's reset in a thread context
  665. *
  666. * Schedule a device reset handling out on a thread context, so it
  667. * is safe to call from atomic context. We can't use the i2400m's
  668. * queue as we are going to destroy it and reinitialize it as part of
  669. * the driver bringup/bringup process.
  670. *
  671. * See __i2400m_dev_reset_handle() for details; that takes care of
  672. * reinitializing the driver to handle the reset, calling into the
  673. * bus-specific functions ops as needed.
  674. */
  675. int i2400m_dev_reset_handle(struct i2400m *i2400m, const char *reason)
  676. {
  677. i2400m->boot_mode = 1;
  678. wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
  679. return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
  680. GFP_ATOMIC, &reason, sizeof(reason));
  681. }
  682. EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
  683. /*
  684. * Alloc the command and ack buffers for boot mode
  685. *
  686. * Get the buffers needed to deal with boot mode messages. These
  687. * buffers need to be allocated before the sdio recieve irq is setup.
  688. */
  689. static
  690. int i2400m_bm_buf_alloc(struct i2400m *i2400m)
  691. {
  692. int result;
  693. result = -ENOMEM;
  694. i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
  695. if (i2400m->bm_cmd_buf == NULL)
  696. goto error_bm_cmd_kzalloc;
  697. i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
  698. if (i2400m->bm_ack_buf == NULL)
  699. goto error_bm_ack_buf_kzalloc;
  700. return 0;
  701. error_bm_ack_buf_kzalloc:
  702. kfree(i2400m->bm_cmd_buf);
  703. error_bm_cmd_kzalloc:
  704. return result;
  705. }
  706. /*
  707. * Free boot mode command and ack buffers.
  708. */
  709. static
  710. void i2400m_bm_buf_free(struct i2400m *i2400m)
  711. {
  712. kfree(i2400m->bm_ack_buf);
  713. kfree(i2400m->bm_cmd_buf);
  714. }
  715. /**
  716. * i2400m_init - Initialize a 'struct i2400m' from all zeroes
  717. *
  718. * This is a bus-generic API call.
  719. */
  720. void i2400m_init(struct i2400m *i2400m)
  721. {
  722. wimax_dev_init(&i2400m->wimax_dev);
  723. i2400m->boot_mode = 1;
  724. i2400m->rx_reorder = 1;
  725. init_waitqueue_head(&i2400m->state_wq);
  726. spin_lock_init(&i2400m->tx_lock);
  727. i2400m->tx_pl_min = UINT_MAX;
  728. i2400m->tx_size_min = UINT_MAX;
  729. spin_lock_init(&i2400m->rx_lock);
  730. i2400m->rx_pl_min = UINT_MAX;
  731. i2400m->rx_size_min = UINT_MAX;
  732. INIT_LIST_HEAD(&i2400m->rx_reports);
  733. INIT_WORK(&i2400m->rx_report_ws, i2400m_report_hook_work);
  734. mutex_init(&i2400m->msg_mutex);
  735. init_completion(&i2400m->msg_completion);
  736. mutex_init(&i2400m->init_mutex);
  737. /* wake_tx_ws is initialized in i2400m_tx_setup() */
  738. }
  739. EXPORT_SYMBOL_GPL(i2400m_init);
  740. int i2400m_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
  741. {
  742. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  743. /*
  744. * Make sure we stop TXs and down the carrier before
  745. * resetting; this is needed to avoid things like
  746. * i2400m_wake_tx() scheduling stuff in parallel.
  747. */
  748. if (net_dev->reg_state == NETREG_REGISTERED) {
  749. netif_tx_disable(net_dev);
  750. netif_carrier_off(net_dev);
  751. }
  752. return i2400m->bus_reset(i2400m, rt);
  753. }
  754. EXPORT_SYMBOL_GPL(i2400m_reset);
  755. /**
  756. * i2400m_setup - bus-generic setup function for the i2400m device
  757. *
  758. * @i2400m: device descriptor (bus-specific parts have been initialized)
  759. *
  760. * Returns: 0 if ok, < 0 errno code on error.
  761. *
  762. * Sets up basic device comunication infrastructure, boots the ROM to
  763. * read the MAC address, registers with the WiMAX and network stacks
  764. * and then brings up the device.
  765. */
  766. int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  767. {
  768. int result = -ENODEV;
  769. struct device *dev = i2400m_dev(i2400m);
  770. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  771. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  772. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  773. snprintf(wimax_dev->name, sizeof(wimax_dev->name),
  774. "i2400m-%s:%s", dev->bus->name, dev_name(dev));
  775. result = i2400m_bm_buf_alloc(i2400m);
  776. if (result < 0) {
  777. dev_err(dev, "cannot allocate bootmode scratch buffers\n");
  778. goto error_bm_buf_alloc;
  779. }
  780. if (i2400m->bus_setup) {
  781. result = i2400m->bus_setup(i2400m);
  782. if (result < 0) {
  783. dev_err(dev, "bus-specific setup failed: %d\n",
  784. result);
  785. goto error_bus_setup;
  786. }
  787. }
  788. result = i2400m_bootrom_init(i2400m, bm_flags);
  789. if (result < 0) {
  790. dev_err(dev, "read mac addr: bootrom init "
  791. "failed: %d\n", result);
  792. goto error_bootrom_init;
  793. }
  794. result = i2400m_read_mac_addr(i2400m);
  795. if (result < 0)
  796. goto error_read_mac_addr;
  797. random_ether_addr(i2400m->src_mac_addr);
  798. i2400m->pm_notifier.notifier_call = i2400m_pm_notifier;
  799. register_pm_notifier(&i2400m->pm_notifier);
  800. result = register_netdev(net_dev); /* Okey dokey, bring it up */
  801. if (result < 0) {
  802. dev_err(dev, "cannot register i2400m network device: %d\n",
  803. result);
  804. goto error_register_netdev;
  805. }
  806. netif_carrier_off(net_dev);
  807. i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
  808. i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
  809. i2400m->wimax_dev.op_reset = i2400m_op_reset;
  810. result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
  811. if (result < 0)
  812. goto error_wimax_dev_add;
  813. /* Now setup all that requires a registered net and wimax device. */
  814. result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
  815. if (result < 0) {
  816. dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
  817. goto error_sysfs_setup;
  818. }
  819. result = i2400m_debugfs_add(i2400m);
  820. if (result < 0) {
  821. dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
  822. goto error_debugfs_setup;
  823. }
  824. result = i2400m_dev_start(i2400m, bm_flags);
  825. if (result < 0)
  826. goto error_dev_start;
  827. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  828. return result;
  829. error_dev_start:
  830. i2400m_debugfs_rm(i2400m);
  831. error_debugfs_setup:
  832. sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
  833. &i2400m_dev_attr_group);
  834. error_sysfs_setup:
  835. wimax_dev_rm(&i2400m->wimax_dev);
  836. error_wimax_dev_add:
  837. unregister_netdev(net_dev);
  838. error_register_netdev:
  839. unregister_pm_notifier(&i2400m->pm_notifier);
  840. error_read_mac_addr:
  841. error_bootrom_init:
  842. if (i2400m->bus_release)
  843. i2400m->bus_release(i2400m);
  844. error_bus_setup:
  845. i2400m_bm_buf_free(i2400m);
  846. error_bm_buf_alloc:
  847. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  848. return result;
  849. }
  850. EXPORT_SYMBOL_GPL(i2400m_setup);
  851. /**
  852. * i2400m_release - release the bus-generic driver resources
  853. *
  854. * Sends a disconnect message and undoes any setup done by i2400m_setup()
  855. */
  856. void i2400m_release(struct i2400m *i2400m)
  857. {
  858. struct device *dev = i2400m_dev(i2400m);
  859. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  860. netif_stop_queue(i2400m->wimax_dev.net_dev);
  861. i2400m_dev_stop(i2400m);
  862. i2400m_debugfs_rm(i2400m);
  863. sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
  864. &i2400m_dev_attr_group);
  865. wimax_dev_rm(&i2400m->wimax_dev);
  866. unregister_netdev(i2400m->wimax_dev.net_dev);
  867. unregister_pm_notifier(&i2400m->pm_notifier);
  868. if (i2400m->bus_release)
  869. i2400m->bus_release(i2400m);
  870. i2400m_bm_buf_free(i2400m);
  871. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  872. }
  873. EXPORT_SYMBOL_GPL(i2400m_release);
  874. /*
  875. * Debug levels control; see debug.h
  876. */
  877. struct d_level D_LEVEL[] = {
  878. D_SUBMODULE_DEFINE(control),
  879. D_SUBMODULE_DEFINE(driver),
  880. D_SUBMODULE_DEFINE(debugfs),
  881. D_SUBMODULE_DEFINE(fw),
  882. D_SUBMODULE_DEFINE(netdev),
  883. D_SUBMODULE_DEFINE(rfkill),
  884. D_SUBMODULE_DEFINE(rx),
  885. D_SUBMODULE_DEFINE(sysfs),
  886. D_SUBMODULE_DEFINE(tx),
  887. };
  888. size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
  889. static
  890. int __init i2400m_driver_init(void)
  891. {
  892. d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params,
  893. "i2400m.debug");
  894. return i2400m_barker_db_init(i2400m_barkers_params);
  895. }
  896. module_init(i2400m_driver_init);
  897. static
  898. void __exit i2400m_driver_exit(void)
  899. {
  900. /* for scheds i2400m_dev_reset_handle() */
  901. flush_scheduled_work();
  902. i2400m_barker_db_exit();
  903. return;
  904. }
  905. module_exit(i2400m_driver_exit);
  906. MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
  907. MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
  908. MODULE_LICENSE("GPL");