root/drivers/net/wimax/i2400m/driver.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. i2400m_op_msg_from_user
  2. i2400m_op_reset
  3. i2400m_check_mac_addr
  4. __i2400m_dev_start
  5. i2400m_dev_start
  6. __i2400m_dev_stop
  7. i2400m_dev_stop
  8. i2400m_pm_notifier
  9. i2400m_pre_reset
  10. i2400m_post_reset
  11. __i2400m_dev_reset_handle
  12. i2400m_dev_reset_handle
  13. __i2400m_error_recovery
  14. i2400m_error_recovery
  15. i2400m_bm_buf_alloc
  16. i2400m_bm_buf_free
  17. i2400m_init
  18. i2400m_reset
  19. i2400m_setup
  20. i2400m_release
  21. i2400m_driver_init
  22. i2400m_driver_exit

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

/* [<][>][^][v][top][bottom][index][help] */