driver.c 30 KB

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