1 /*****************************************************************************
3 (c) Cambridge Silicon Radio Limited 2012
4 All rights reserved and confidential information of CSR
6 Refer to LICENSE.txt included with this source for details
9 *****************************************************************************/
12 * ---------------------------------------------------------------------------
13 * FILE: csr_wifi_hip_card_sdio.c
15 * PURPOSE: Implementation of the Card API for SDIO.
18 * CardInit() is called from the SDIO probe callback when a card is
19 * inserted. This performs the basic SDIO initialisation, enabling i/o
22 * ---------------------------------------------------------------------------
24 #include <linux/slab.h>
25 #include "csr_wifi_hip_unifi.h"
26 #include "csr_wifi_hip_conversions.h"
27 #include "csr_wifi_hip_unifiversion.h"
28 #include "csr_wifi_hip_card.h"
29 #include "csr_wifi_hip_card_sdio.h"
30 #include "csr_wifi_hip_chiphelper.h"
33 /* Time to wait between attempts to read MAILBOX0 */
34 #define MAILBOX1_TIMEOUT 10 /* in millisecs */
35 #define MAILBOX1_ATTEMPTS 200 /* 2 seconds */
37 #define MAILBOX2_TIMEOUT 5 /* in millisecs */
38 #define MAILBOX2_ATTEMPTS 10 /* 50ms */
40 #define RESET_SETTLE_DELAY 25 /* in millisecs */
42 static CsrResult card_init_slots(card_t *card);
43 static CsrResult card_hw_init(card_t *card);
44 static CsrResult firmware_present_in_flash(card_t *card);
45 static void bootstrap_chip_hw(card_t *card);
46 static CsrResult unifi_reset_hardware(card_t *card);
47 static CsrResult unifi_hip_init(card_t *card);
48 static CsrResult card_access_panic(card_t *card);
49 static CsrResult unifi_read_chip_version(card_t *card);
52 * ---------------------------------------------------------------------------
55 * Allocate and initialise the card context structure.
58 * sdio Pointer to SDIO context pointer to pass to low
59 * level i/o functions.
60 * ospriv Pointer to O/S private struct to pass when calling
61 * callbacks to the higher level system.
64 * Pointer to card struct, which represents the driver context or
65 * NULL if the allocation failed.
66 * ---------------------------------------------------------------------------
68 card_t* unifi_alloc_card(CsrSdioFunction *sdio, void *ospriv)
76 card = kzalloc(sizeof(card_t), GFP_KERNEL);
83 card->ospriv = ospriv;
85 card->unifi_interrupt_seq = 1;
87 /* Make these invalid. */
88 card->proc_select = (u32)(-1);
89 card->dmem_page = (u32)(-1);
90 card->pmem_page = (u32)(-1);
92 card->bh_reason_host = 0;
93 card->bh_reason_unifi = 0;
95 for (i = 0; i < sizeof(card->tx_q_paused_flag) / sizeof(card->tx_q_paused_flag[0]); i++)
97 card->tx_q_paused_flag[i] = 0;
99 card->memory_resources_allocated = 0;
101 card->low_power_mode = UNIFI_LOW_POWER_DISABLED;
102 card->periodic_wake_mode = UNIFI_PERIODIC_WAKE_HOST_DISABLED;
104 card->host_state = UNIFI_HOST_STATE_AWAKE;
105 card->intmode = CSR_WIFI_INTMODE_DEFAULT;
108 * Memory resources for buffers are allocated when the chip is initialised
109 * because we need configuration information from the firmware.
113 * Initialise wait queues and lists
115 card->fh_command_queue.q_body = card->fh_command_q_body;
116 card->fh_command_queue.q_length = UNIFI_SOFT_COMMAND_Q_LENGTH;
118 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
120 card->fh_traffic_queue[i].q_body = card->fh_traffic_q_body[i];
121 card->fh_traffic_queue[i].q_length = UNIFI_SOFT_TRAFFIC_Q_LENGTH;
125 /* Initialise mini-coredump pointers in case no coredump buffers
126 * are requested by the OS layer.
128 card->request_coredump_on_reset = 0;
129 card->dump_next_write = NULL;
130 card->dump_cur_read = NULL;
131 card->dump_buf = NULL;
134 /* Determine offset of LSB in pointer for later alignment sanity check.
135 * Synergy integer types have specific widths, which cause compiler
136 * warnings when casting pointer types, e.g. on 64-bit systems.
139 u32 val = 0x01234567;
141 if (*((u8 *)&val) == 0x01)
143 card->lsb = sizeof(void *) - 1; /* BE */
147 card->lsb = 0; /* LE */
153 } /* unifi_alloc_card() */
157 * ---------------------------------------------------------------------------
160 * Reset the hardware and perform HIP initialization
163 * card Pointer to card struct
167 * CSR_RESULT_SUCCESS if successful
168 * ---------------------------------------------------------------------------
170 CsrResult unifi_init_card(card_t *card, s32 led_mask)
178 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
179 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
182 r = unifi_init(card);
183 if (r != CSR_RESULT_SUCCESS)
189 r = unifi_hip_init(card);
190 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
195 if (r != CSR_RESULT_SUCCESS)
197 unifi_error(card->ospriv, "Failed to start host protocol.\n");
203 return CSR_RESULT_SUCCESS;
208 * ---------------------------------------------------------------------------
214 * card Pointer to card struct
218 * CSR_RESULT_SUCCESS if successful
219 * ---------------------------------------------------------------------------
221 CsrResult unifi_init(card_t *card)
230 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
231 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
235 * Disable the SDIO interrupts while initialising UniFi.
236 * Re-enable them when f/w is running.
238 csrResult = CsrSdioInterruptDisable(card->sdio_if);
239 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
241 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
245 * UniFi's PLL may start with a slow clock (~ 1 MHz) so initially
246 * set the SDIO bus clock to a similar value or SDIO accesses may
249 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
250 if (csrResult != CSR_RESULT_SUCCESS)
252 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
256 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
259 * Reset UniFi. Note, this only resets the WLAN function part of the chip,
260 * the SDIO interface is not reset.
262 unifi_trace(card->ospriv, UDBG1, "Resetting UniFi\n");
263 r = unifi_reset_hardware(card);
264 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
268 if (r != CSR_RESULT_SUCCESS)
270 unifi_error(card->ospriv, "Failed to reset UniFi\n");
275 /* Reset the power save mode, to be active until the MLME-reset is complete */
276 r = unifi_configure_low_power_mode(card,
277 UNIFI_LOW_POWER_DISABLED, UNIFI_PERIODIC_WAKE_HOST_DISABLED);
278 if (r != CSR_RESULT_SUCCESS)
280 unifi_error(card->ospriv, "Failed to set power save mode\n");
286 * Set initial value of page registers.
287 * The page registers will be maintained by unifi_read...() and
290 card->proc_select = (u32)(-1);
291 card->dmem_page = (u32)(-1);
292 card->pmem_page = (u32)(-1);
293 r = unifi_write_direct16(card, ChipHelper_HOST_WINDOW3_PAGE(card->helper) * 2, 0);
294 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
298 if (r != CSR_RESULT_SUCCESS)
300 unifi_error(card->ospriv, "Failed to write SHARED_DMEM_PAGE\n");
304 r = unifi_write_direct16(card, ChipHelper_HOST_WINDOW2_PAGE(card->helper) * 2, 0);
305 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
309 if (r != CSR_RESULT_SUCCESS)
311 unifi_error(card->ospriv, "Failed to write PROG_MEM2_PAGE\n");
317 * If the driver has reset UniFi due to previous SDIO failure, this may
318 * have been due to a chip watchdog reset. In this case, the driver may
319 * have requested a mini-coredump which needs to be captured now the
320 * SDIO interface is alive.
322 (void)unifi_coredump_handle_request(card);
325 * Probe to see if the UniFi has ROM/flash to boot from. CSR6xxx should do.
327 r = firmware_present_in_flash(card);
328 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
332 if (r == CSR_WIFI_HIP_RESULT_NOT_FOUND)
334 unifi_error(card->ospriv, "No firmware found\n");
336 else if (r != CSR_RESULT_SUCCESS)
338 unifi_error(card->ospriv, "Probe for Flash failed\n");
347 * ---------------------------------------------------------------------------
353 * card Pointer to card struct
354 * led_mask Loader LED mask
357 * CSR_RESULT_SUCCESS on success
358 * CsrResult error code on failure.
359 * ---------------------------------------------------------------------------
361 CsrResult unifi_download(card_t *card, s32 led_mask)
370 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
371 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
374 /* Set the loader led mask */
375 card->loader_led_mask = led_mask;
377 /* Get the firmware file information */
378 unifi_trace(card->ospriv, UDBG1, "downloading firmware...\n");
380 dlpriv = unifi_dl_fw_read_start(card, UNIFI_FW_STA);
383 func_exit_r(CSR_WIFI_HIP_RESULT_NOT_FOUND);
384 return CSR_WIFI_HIP_RESULT_NOT_FOUND;
387 /* Download the firmware. */
388 r = unifi_dl_firmware(card, dlpriv);
389 if (r != CSR_RESULT_SUCCESS)
391 unifi_error(card->ospriv, "Failed to download firmware\n");
396 /* Free the firmware file information. */
397 unifi_fw_read_stop(card->ospriv, dlpriv);
401 return CSR_RESULT_SUCCESS;
402 } /* unifi_download() */
406 * ---------------------------------------------------------------------------
409 * This function performs the f/w initialisation sequence as described
410 * in the Unifi Host Interface Protocol Specification.
411 * It allocates memory for host-side slot data and signal queues.
414 * card Pointer to card struct
417 * CSR_RESULT_SUCCESS on success or else a CSR error code
420 * The firmware must have been downloaded.
421 * ---------------------------------------------------------------------------
423 static CsrResult unifi_hip_init(card_t *card)
430 r = card_hw_init(card);
431 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
435 if (r != CSR_RESULT_SUCCESS)
437 unifi_error(card->ospriv, "Failed to establish communication with UniFi\n");
441 #ifdef CSR_PRE_ALLOC_NET_DATA
442 /* if there is any preallocated netdata left from the prev session free it now */
443 prealloc_netdata_free(card);
446 * Allocate memory for host-side slot data and signal queues.
447 * We need the config info read from the firmware to know how much
448 * memory to allocate.
450 r = card_init_slots(card);
451 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
455 if (r != CSR_RESULT_SUCCESS)
457 unifi_error(card->ospriv, "Init slots failed: %d\n", r);
462 unifi_trace(card->ospriv, UDBG2, "Sending first UniFi interrupt\n");
464 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
465 if (r != CSR_RESULT_SUCCESS)
471 /* Enable the SDIO interrupts now that the f/w is running. */
472 csrResult = CsrSdioInterruptEnable(card->sdio_if);
473 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
475 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
478 /* Signal the UniFi to start handling messages */
479 r = CardGenInt(card);
480 if (r != CSR_RESULT_SUCCESS)
488 return CSR_RESULT_SUCCESS;
489 } /* unifi_hip_init() */
493 * ---------------------------------------------------------------------------
494 * _build_sdio_config_data
496 * Unpack the SDIO configuration information from a buffer read from
497 * UniFi into a host structure.
498 * The data is byte-swapped for a big-endian host if necessary by the
502 * card Pointer to card struct
503 * cfg_data Destination structure to unpack into.
504 * cfg_data_buf Source buffer to read from. This should be the raw
505 * data read from UniFi.
509 * ---------------------------------------------------------------------------
511 static void _build_sdio_config_data(sdio_config_data_t *cfg_data,
512 const u8 *cfg_data_buf)
516 cfg_data->version = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
517 offset += SIZEOF_UINT16;
519 cfg_data->sdio_ctrl_offset = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
520 offset += SIZEOF_UINT16;
522 cfg_data->fromhost_sigbuf_handle = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
523 offset += SIZEOF_UINT16;
525 cfg_data->tohost_sigbuf_handle = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
526 offset += SIZEOF_UINT16;
528 cfg_data->num_fromhost_sig_frags = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
529 offset += SIZEOF_UINT16;
531 cfg_data->num_tohost_sig_frags = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
532 offset += SIZEOF_UINT16;
534 cfg_data->num_fromhost_data_slots = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
535 offset += SIZEOF_UINT16;
537 cfg_data->num_tohost_data_slots = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
538 offset += SIZEOF_UINT16;
540 cfg_data->data_slot_size = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
541 offset += SIZEOF_UINT16;
543 cfg_data->initialised = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
544 offset += SIZEOF_UINT16;
546 cfg_data->overlay_size = CSR_GET_UINT32_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
547 offset += SIZEOF_UINT32;
549 cfg_data->data_slot_round = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
550 offset += SIZEOF_UINT16;
552 cfg_data->sig_frag_size = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
553 offset += SIZEOF_UINT16;
555 cfg_data->tohost_signal_padding = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
556 } /* _build_sdio_config_data() */
560 * - Function ----------------------------------------------------------------
563 * Perform the initialisation procedure described in the UniFi Host
564 * Interface Protocol document (section 3.3.8) and read the run-time
565 * configuration information from the UniFi. This is stuff like number
566 * of bulk data slots etc.
568 * The card enumeration and SD initialisation has already been done by
569 * the SDIO library, see card_sdio_init().
571 * The initialisation is done when firmware is ready, i.e. this may need
572 * to be called after a f/w download operation.
574 * The initialisation procedure goes like this:
575 * - Wait for UniFi to start-up by polling SHARED_MAILBOX1
576 * - Find the symbol table and look up SLT_SDIO_SLOT_CONFIG
577 * - Read the config structure
578 * - Check the "SDIO initialised" flag, if not zero do a h/w reset and
580 * - Decide the number of bulk data slots to allocate, allocate them and
581 * set "SDIO initialised" flag (and generate an interrupt) to say so.
584 * card Pointer to card struct
587 * CSR_RESULT_SUCEESS on success,
588 * a CSR error code on failure
591 * All data in the f/w is stored in a little endian format, without any
592 * padding bytes. Every read from this memory has to be transformed in
593 * host (cpu specific) format, before it is stored in driver's parameters
594 * or/and structures. Athough unifi_card_read16() and unifi_read32() do perform
595 * the convertion internally, unifi_readn() does not.
596 * ---------------------------------------------------------------------------
598 static CsrResult card_hw_init(card_t *card)
604 sdio_config_data_t *cfg_data;
605 u8 cfg_data_buf[SDIO_CONFIG_DATA_SIZE];
609 s16 search_4slut_again;
615 * The device revision from the TPLMID_MANF and TPLMID_CARD fields
616 * of the CIS are available as
617 * card->sdio_if->pDevice->ManfID
618 * card->sdio_if->pDevice->AppID
622 * Run in a loop so we can patch.
626 /* Reset these each time around the loop. */
627 search_4slut_again = 0;
630 r = card_wait_for_firmware_to_start(card, &slut_address);
631 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
635 if (r != CSR_RESULT_SUCCESS)
637 unifi_error(card->ospriv, "Firmware hasn't started\n");
641 unifi_trace(card->ospriv, UDBG4, "SLUT addr 0x%lX\n", slut_address);
644 * Firmware has started, but doesn't know full clock configuration yet
645 * as some of the information may be in the MIB. Therefore we set an
646 * initial SDIO clock speed, faster than UNIFI_SDIO_CLOCK_SAFE_HZ, for
647 * the patch download and subsequent firmware initialisation, and
648 * full speed UNIFI_SDIO_CLOCK_MAX_HZ will be set once the f/w tells us
651 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_INIT_HZ);
652 if (csrResult != CSR_RESULT_SUCCESS)
654 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
658 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_INIT_HZ;
661 * Check the SLUT fingerprint.
662 * The slut_address is a generic pointer so we must use unifi_card_read16().
664 unifi_trace(card->ospriv, UDBG4, "Looking for SLUT finger print\n");
666 r = unifi_card_read16(card, slut_address, &finger_print);
667 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
671 if (r != CSR_RESULT_SUCCESS)
673 unifi_error(card->ospriv, "Failed to read SLUT finger print\n");
678 if (finger_print != SLUT_FINGERPRINT)
680 unifi_error(card->ospriv, "Failed to find Symbol lookup table fingerprint\n");
681 func_exit_r(CSR_RESULT_FAILURE);
682 return CSR_RESULT_FAILURE;
685 /* Symbol table starts imedately after the fingerprint */
688 /* Search the table until either the end marker is found, or the
689 * loading of patch firmware invalidates the current table.
691 while (!search_4slut_again)
696 r = unifi_card_read16(card, slut_address, &s);
697 if (r != CSR_RESULT_SUCCESS)
704 if (s == CSR_SLT_END)
706 unifi_trace(card->ospriv, UDBG3, " found CSR_SLT_END\n");
710 r = unifi_read32(card, slut_address, &l);
711 if (r != CSR_RESULT_SUCCESS)
721 unifi_trace(card->ospriv, UDBG3, " found SLUT id %02d.%08lx\n", slut.id, slut.obj);
724 case CSR_SLT_SDIO_SLOT_CONFIG:
725 cfg_data = &card->config_data;
727 * unifi_card_readn reads n bytes from the card, where data is stored
728 * in a little endian format, without any padding bytes. So, we
729 * can not just pass the cfg_data pointer or use the
730 * sizeof(sdio_config_data_t) since the structure in the host can
731 * be big endian formatted or have padding bytes for alignment.
732 * We use a char buffer to read the data from the card.
734 r = unifi_card_readn(card, slut.obj, cfg_data_buf, SDIO_CONFIG_DATA_SIZE);
735 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
739 if (r != CSR_RESULT_SUCCESS)
741 unifi_error(card->ospriv, "Failed to read config data\n");
745 /* .. and then we copy the data to the host structure */
746 _build_sdio_config_data(cfg_data, cfg_data_buf);
748 /* Make sure the from host data slots are what we expect
749 we reserve 2 for commands and there should be at least
750 1 left for each access category */
751 if ((cfg_data->num_fromhost_data_slots < UNIFI_RESERVED_COMMAND_SLOTS)
752 || (cfg_data->num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS) / UNIFI_NO_OF_TX_QS == 0)
754 unifi_error(card->ospriv, "From host data slots %d\n", cfg_data->num_fromhost_data_slots);
755 unifi_error(card->ospriv, "need to be (queues * x + 2) (UNIFI_RESERVED_COMMAND_SLOTS for commands)\n");
756 func_exit_r(CSR_RESULT_FAILURE);
757 return CSR_RESULT_FAILURE;
760 /* Configure SDIO to-block-size padding */
761 if (card->sdio_io_block_pad)
764 * Firmware limits the maximum padding size via data_slot_round.
765 * Therefore when padding to whole block sizes, the block size
766 * must be configured correctly by adjusting CSR_WIFI_HIP_SDIO_BLOCK_SIZE.
768 if (cfg_data->data_slot_round < card->sdio_io_block_size)
770 unifi_error(card->ospriv,
771 "Configuration error: Block size of %d exceeds f/w data_slot_round of %d\n",
772 card->sdio_io_block_size, cfg_data->data_slot_round);
773 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
777 * To force the To-Host signals to be rounded up to the SDIO block
778 * size, we need to write the To-Host Signal Padding Fragments
779 * field of the SDIO configuration in UniFi.
781 if ((card->sdio_io_block_size % cfg_data->sig_frag_size) != 0)
783 unifi_error(card->ospriv, "Configuration error: Can not pad to-host signals.\n");
784 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
785 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
787 cfg_data->tohost_signal_padding = (u16) (card->sdio_io_block_size / cfg_data->sig_frag_size);
788 unifi_info(card->ospriv, "SDIO block size %d requires %d padding chunks\n",
789 card->sdio_io_block_size, cfg_data->tohost_signal_padding);
790 r = unifi_card_write16(card, slut.obj + SDIO_TO_HOST_SIG_PADDING_OFFSET, cfg_data->tohost_signal_padding);
791 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
795 if (r != CSR_RESULT_SUCCESS)
797 unifi_error(card->ospriv, "Failed to write To-Host Signal Padding Fragments\n");
803 /* Reconstruct the Generic Pointer address of the
804 * SDIO Control Data Struct.
806 card->sdio_ctrl_addr = cfg_data->sdio_ctrl_offset | (UNIFI_SH_DMEM << 24);
807 card->init_flag_addr = slut.obj + SDIO_INIT_FLAG_OFFSET;
810 case CSR_SLT_BUILD_ID_NUMBER:
813 r = unifi_read32(card, slut.obj, &n);
814 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
818 if (r != CSR_RESULT_SUCCESS)
820 unifi_error(card->ospriv, "Failed to read build id\n");
828 case CSR_SLT_BUILD_ID_STRING:
829 r = unifi_readnz(card, slut.obj, card->build_id_string,
830 sizeof(card->build_id_string));
831 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
835 if (r != CSR_RESULT_SUCCESS)
837 unifi_error(card->ospriv, "Failed to read build string\n");
843 case CSR_SLT_PERSISTENT_STORE_DB:
846 case CSR_SLT_BOOT_LOADER_CONTROL:
848 /* This command copies most of the station firmware
849 * image from ROM into program RAM. It also clears
850 * out the zerod data and sets up the initialised
852 r = unifi_do_loader_op(card, slut.obj + 6, UNIFI_BOOT_LOADER_LOAD_STA);
853 if (r != CSR_RESULT_SUCCESS)
855 unifi_error(card->ospriv, "Failed to write loader load image command\n");
860 dlpriv = unifi_dl_fw_read_start(card, UNIFI_FW_STA);
862 /* dlpriv might be NULL, we still need to do the do_loader_op step. */
865 /* Download the firmware. */
866 r = unifi_dl_patch(card, dlpriv, slut.obj);
868 /* Free the firmware file information. */
869 unifi_fw_read_stop(card->ospriv, dlpriv);
871 if (r != CSR_RESULT_SUCCESS)
873 unifi_error(card->ospriv, "Failed to patch firmware\n");
879 /* This command starts the firmware image that we want (the
880 * station by default) with any patches required applied. */
881 r = unifi_do_loader_op(card, slut.obj + 6, UNIFI_BOOT_LOADER_RESTART);
882 if (r != CSR_RESULT_SUCCESS)
884 unifi_error(card->ospriv, "Failed to write loader restart command\n");
889 /* The now running patch f/w defines a new SLUT data structure -
890 * the current one is no longer valid. We must drop out of the
891 * processing loop and enumerate the new SLUT (which may appear
892 * at a different offset).
894 search_4slut_again = 1;
897 case CSR_SLT_PANIC_DATA_PHY:
898 card->panic_data_phy_addr = slut.obj;
901 case CSR_SLT_PANIC_DATA_MAC:
902 card->panic_data_mac_addr = slut.obj;
910 } while (search_4slut_again);
912 /* Did we find the Config Data ? */
913 if (cfg_data == NULL)
915 unifi_error(card->ospriv, "Failed to find SDIO_SLOT_CONFIG Symbol\n");
916 func_exit_r(CSR_RESULT_FAILURE);
917 return CSR_RESULT_FAILURE;
921 * Has ths card already been initialised?
922 * If so, return an error so we do a h/w reset and start again.
924 r = unifi_card_read16(card, card->init_flag_addr, &initialised);
925 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
929 if (r != CSR_RESULT_SUCCESS)
931 unifi_error(card->ospriv, "Failed to read init flag at %08lx\n",
932 card->init_flag_addr);
936 if (initialised != 0)
938 func_exit_r(CSR_RESULT_FAILURE);
939 return CSR_RESULT_FAILURE;
944 * Now check the UniFi firmware version
946 major = (cfg_data->version >> 8) & 0xFF;
947 minor = cfg_data->version & 0xFF;
948 unifi_info(card->ospriv, "UniFi f/w protocol version %d.%d (driver %d.%d)\n",
950 UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
952 unifi_info(card->ospriv, "Firmware build %u: %s\n",
953 card->build_id, card->build_id_string);
955 if (major != UNIFI_HIP_MAJOR_VERSION)
957 unifi_error(card->ospriv, "UniFi f/w protocol major version (%d) is different from driver (v%d.%d)\n",
958 major, UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
959 #ifndef CSR_WIFI_DISABLE_HIP_VERSION_CHECK
960 func_exit_r(CSR_RESULT_FAILURE);
961 return CSR_RESULT_FAILURE;
964 if (minor < UNIFI_HIP_MINOR_VERSION)
966 unifi_error(card->ospriv, "UniFi f/w protocol version (v%d.%d) is older than minimum required by driver (v%d.%d).\n",
968 UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
969 #ifndef CSR_WIFI_DISABLE_HIP_VERSION_CHECK
970 func_exit_r(CSR_RESULT_FAILURE);
971 return CSR_RESULT_FAILURE;
975 /* Read panic codes from a previous firmware panic. If the firmware has
976 * not panicked since power was applied (e.g. power-off hard reset)
977 * the stored panic codes will not be updated.
979 unifi_read_panic(card);
982 return CSR_RESULT_SUCCESS;
983 } /* card_hw_init() */
987 * ---------------------------------------------------------------------------
988 * card_wait_for_unifi_to_reset
990 * Waits for a reset to complete by polling the WLAN function enable
991 * bit (which is cleared on reset).
994 * card Pointer to card struct
997 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
998 * ---------------------------------------------------------------------------
1000 static CsrResult card_wait_for_unifi_to_reset(card_t *card)
1005 CsrResult csrResult;
1009 r = CSR_RESULT_SUCCESS;
1010 for (i = 0; i < MAILBOX2_ATTEMPTS; i++)
1012 unifi_trace(card->ospriv, UDBG1, "waiting for reset to complete, attempt %d\n", i);
1013 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
1015 /* It's quite likely that this read will timeout for the
1016 * first few tries - especially if we have reset via
1019 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1020 unifi_debug_log_to_buf("m0@%02X=", SDIO_IO_READY);
1022 csrResult = CsrSdioF0Read8(card->sdio_if, SDIO_IO_READY, &io_enable);
1023 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1024 if (csrResult != CSR_RESULT_SUCCESS)
1026 unifi_debug_log_to_buf("error=%X\n", csrResult);
1030 unifi_debug_log_to_buf("%X\n", io_enable);
1033 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
1035 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
1037 r = CSR_RESULT_SUCCESS;
1038 if (csrResult != CSR_RESULT_SUCCESS)
1040 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1045 r = sdio_read_f0(card, SDIO_IO_ENABLE, &io_enable);
1047 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1051 if (r == CSR_RESULT_SUCCESS)
1054 s16 enabled = io_enable & (1 << card->function);
1058 unifi_trace(card->ospriv, UDBG1,
1059 "Reset complete (function %d is disabled) in ~ %u msecs\n",
1060 card->function, i * MAILBOX2_TIMEOUT);
1062 /* Enable WLAN function and verify MAILBOX2 is zero'd */
1063 csrResult = CsrSdioFunctionEnable(card->sdio_if);
1064 if (csrResult != CSR_RESULT_SUCCESS)
1066 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1067 unifi_error(card->ospriv, "CsrSdioFunctionEnable failed %d\n", r);
1072 r = unifi_read_direct16(card, ChipHelper_SDIO_HIP_HANDSHAKE(card->helper) * 2, &mbox2);
1073 if (r != CSR_RESULT_SUCCESS)
1075 unifi_error(card->ospriv, "read HIP_HANDSHAKE failed %d\n", r);
1080 unifi_error(card->ospriv, "MAILBOX2 non-zero after reset (mbox2 = %04x)\n", mbox2);
1081 r = CSR_RESULT_FAILURE;
1087 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
1089 /* We ignore read failures for the first few reads,
1090 * they are probably benign. */
1091 if (i > MAILBOX2_ATTEMPTS / 4)
1093 unifi_trace(card->ospriv, UDBG1, "Failed to read CCCR IO Ready register while polling for reset\n");
1098 unifi_trace(card->ospriv, UDBG1, "Failed to read CCCR IO Enable register while polling for reset\n");
1101 CsrThreadSleep(MAILBOX2_TIMEOUT);
1104 if (r == CSR_RESULT_SUCCESS && i == MAILBOX2_ATTEMPTS)
1106 unifi_trace(card->ospriv, UDBG1, "Timeout waiting for UniFi to complete reset\n");
1107 r = CSR_RESULT_FAILURE;
1112 } /* card_wait_for_unifi_to_reset() */
1116 * ---------------------------------------------------------------------------
1117 * card_wait_for_unifi_to_disable
1119 * Waits for the function to become disabled by polling the
1123 * card Pointer to card struct
1126 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1128 * Notes: This function can only be used with
1129 * card->chip_id > SDIO_CARD_ID_UNIFI_2
1130 * ---------------------------------------------------------------------------
1132 static CsrResult card_wait_for_unifi_to_disable(card_t *card)
1137 CsrResult csrResult;
1141 if (card->chip_id <= SDIO_CARD_ID_UNIFI_2)
1143 unifi_error(card->ospriv,
1144 "Function reset method not supported for chip_id=%d\n",
1147 return CSR_RESULT_FAILURE;
1150 r = CSR_RESULT_SUCCESS;
1151 for (i = 0; i < MAILBOX2_ATTEMPTS; i++)
1153 unifi_trace(card->ospriv, UDBG1, "waiting for disable to complete, attempt %d\n", i);
1156 * It's quite likely that this read will timeout for the
1157 * first few tries - especially if we have reset via
1160 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1161 unifi_debug_log_to_buf("r0@%02X=", SDIO_IO_READY);
1163 csrResult = CsrSdioF0Read8(card->sdio_if, SDIO_IO_READY, &io_enable);
1164 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1165 if (csrResult != CSR_RESULT_SUCCESS)
1167 unifi_debug_log_to_buf("error=%X\n", csrResult);
1171 unifi_debug_log_to_buf("%X\n", io_enable);
1174 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
1176 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
1178 if (csrResult == CSR_RESULT_SUCCESS)
1180 s16 enabled = io_enable & (1 << card->function);
1181 r = CSR_RESULT_SUCCESS;
1184 unifi_trace(card->ospriv, UDBG1,
1185 "Disable complete (function %d is disabled) in ~ %u msecs\n",
1186 card->function, i * MAILBOX2_TIMEOUT);
1194 * We ignore read failures for the first few reads,
1195 * they are probably benign.
1197 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1198 if (i > (MAILBOX2_ATTEMPTS / 4))
1200 unifi_trace(card->ospriv, UDBG1,
1201 "Failed to read CCCR IO Ready register while polling for disable\n");
1204 CsrThreadSleep(MAILBOX2_TIMEOUT);
1207 if ((r == CSR_RESULT_SUCCESS) && (i == MAILBOX2_ATTEMPTS))
1209 unifi_trace(card->ospriv, UDBG1, "Timeout waiting for UniFi to complete disable\n");
1210 r = CSR_RESULT_FAILURE;
1215 } /* card_wait_for_unifi_to_reset() */
1219 * ---------------------------------------------------------------------------
1220 * card_wait_for_firmware_to_start
1222 * Polls the MAILBOX1 register for a non-zero value.
1223 * Then reads MAILBOX0 and forms the two values into a 32-bit address
1224 * which is returned to the caller.
1227 * card Pointer to card struct
1228 * paddr Pointer to receive the UniFi address formed
1229 * by concatenating MAILBOX1 and MAILBOX0.
1232 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1233 * ---------------------------------------------------------------------------
1235 CsrResult card_wait_for_firmware_to_start(card_t *card, u32 *paddr)
1244 * Wait for UniFi to initialise its data structures by polling
1245 * the SHARED_MAILBOX1 register.
1246 * Experience shows this is typically 120ms.
1248 CsrThreadSleep(MAILBOX1_TIMEOUT);
1251 unifi_trace(card->ospriv, UDBG1, "waiting for MAILBOX1 to be non-zero...\n");
1252 for (i = 0; i < MAILBOX1_ATTEMPTS; i++)
1254 r = unifi_read_direct16(card, ChipHelper_MAILBOX1(card->helper) * 2, &mbox1);
1255 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1259 if (r != CSR_RESULT_SUCCESS)
1261 /* These reads can fail if UniFi isn't up yet, so try again */
1262 unifi_warning(card->ospriv, "Failed to read UniFi Mailbox1 register\n");
1265 if ((r == CSR_RESULT_SUCCESS) && (mbox1 != 0))
1267 unifi_trace(card->ospriv, UDBG1, "MAILBOX1 ready (0x%04X) in %u millisecs\n",
1268 mbox1, i * MAILBOX1_TIMEOUT);
1270 /* Read the MAILBOX1 again in case we caught the value as it
1272 r = unifi_read_direct16(card, ChipHelper_MAILBOX1(card->helper) * 2, &mbox1);
1273 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1277 if (r != CSR_RESULT_SUCCESS)
1279 unifi_error(card->ospriv, "Failed to read UniFi Mailbox1 register for second time\n");
1283 unifi_trace(card->ospriv, UDBG1, "MAILBOX1 value=0x%04X\n", mbox1);
1288 CsrThreadSleep(MAILBOX1_TIMEOUT);
1289 if ((i % 100) == 99)
1291 unifi_trace(card->ospriv, UDBG2, "MAILBOX1 not ready (0x%X), still trying...\n", mbox1);
1295 if ((r == CSR_RESULT_SUCCESS) && (mbox1 == 0))
1297 unifi_trace(card->ospriv, UDBG1, "Timeout waiting for firmware to start, Mailbox1 still 0 after %d ms\n",
1298 MAILBOX1_ATTEMPTS * MAILBOX1_TIMEOUT);
1299 func_exit_r(CSR_RESULT_FAILURE);
1300 return CSR_RESULT_FAILURE;
1305 * Complete the reset handshake by setting MAILBOX2 to 0xFFFF
1307 r = unifi_write_direct16(card, ChipHelper_SDIO_HIP_HANDSHAKE(card->helper) * 2, 0xFFFF);
1308 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1312 if (r != CSR_RESULT_SUCCESS)
1314 unifi_error(card->ospriv, "Failed to write f/w startup handshake to MAILBOX2\n");
1321 * Read the Symbol Look Up Table (SLUT) offset.
1322 * Top 16 bits are in mbox1, read the lower 16 bits from mbox0.
1325 r = unifi_read_direct16(card, ChipHelper_MAILBOX0(card->helper) * 2, &mbox0);
1326 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1330 if (r != CSR_RESULT_SUCCESS)
1332 unifi_error(card->ospriv, "Failed to read UniFi Mailbox0 register\n");
1337 *paddr = (((u32)mbox1 << 16) | mbox0);
1340 return CSR_RESULT_SUCCESS;
1341 } /* card_wait_for_firmware_to_start() */
1345 * ---------------------------------------------------------------------------
1346 * unifi_capture_panic
1348 * Attempt to capture panic codes from the firmware. This may involve
1349 * warm reset of the chip to regain access following a watchdog reset.
1352 * card Pointer to card struct
1355 * CSR_RESULT_SUCCESS if panic codes were captured, or none available
1356 * CSR_RESULT_FAILURE if the driver could not access function 1
1357 * ---------------------------------------------------------------------------
1359 CsrResult unifi_capture_panic(card_t *card)
1363 /* The firmware must have previously initialised to read the panic addresses
1366 if (!card->panic_data_phy_addr || !card->panic_data_mac_addr)
1369 return CSR_RESULT_SUCCESS;
1372 /* Ensure we can access function 1 following a panic/watchdog reset */
1373 if (card_access_panic(card) == CSR_RESULT_SUCCESS)
1375 /* Read the panic codes */
1376 unifi_read_panic(card);
1380 unifi_info(card->ospriv, "Unable to read panic codes");
1384 return CSR_RESULT_SUCCESS;
1389 * ---------------------------------------------------------------------------
1391 * Attempt to read the WLAN SDIO function in order to read panic codes
1392 * and perform various reset steps to regain access if the read fails.
1395 * card Pointer to card struct
1398 * CSR_RESULT_SUCCESS if panic codes can be read
1399 * CSR error code if panic codes can not be read
1400 * ---------------------------------------------------------------------------
1402 static CsrResult card_access_panic(card_t *card)
1410 /* A chip version of zero means that the version never got succesfully read
1411 * during reset. In this case give up because it will not be possible to
1412 * verify the chip version.
1414 if (!card->chip_version)
1416 unifi_info(card->ospriv, "Unknown chip version\n");
1417 return CSR_RESULT_FAILURE;
1420 /* Ensure chip is awake or access to function 1 will fail */
1421 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
1422 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1426 if (r != CSR_RESULT_SUCCESS)
1428 unifi_error(card->ospriv, "unifi_set_host_state() failed %d\n", r);
1429 return CSR_RESULT_FAILURE; /* Card is probably unpowered */
1433 for (i = 0; i < 3; i++)
1435 sr = CsrSdioRead16(card->sdio_if, CHIP_HELPER_UNIFI_GBL_CHIP_VERSION * 2, &data_u16);
1436 if (sr != CSR_RESULT_SUCCESS || data_u16 != card->chip_version)
1438 unifi_info(card->ospriv, "Failed to read valid chip version sr=%d (0x%04x want 0x%04x) try %d\n",
1439 sr, data_u16, card->chip_version, i);
1441 /* Set clock speed low */
1442 sr = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
1443 if (sr != CSR_RESULT_SUCCESS)
1445 unifi_error(card->ospriv, "CsrSdioMaxBusClockFrequencySet() failed1 %d\n", sr);
1446 r = ConvertCsrSdioToCsrHipResult(card, sr);
1448 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
1450 /* First try re-enabling function in case a f/w watchdog reset disabled it */
1453 unifi_info(card->ospriv, "Try function enable\n");
1454 sr = CsrSdioFunctionEnable(card->sdio_if);
1455 if (sr != CSR_RESULT_SUCCESS)
1457 r = ConvertCsrSdioToCsrHipResult(card, sr);
1458 unifi_error(card->ospriv, "CsrSdioFunctionEnable failed %d (HIP %d)\n", sr, r);
1463 /* Second try, set awake */
1464 unifi_info(card->ospriv, "Try set awake\n");
1466 /* Ensure chip is awake */
1467 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
1468 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1472 if (r != CSR_RESULT_SUCCESS)
1474 unifi_error(card->ospriv, "unifi_set_host_state() failed2 %d\n", r);
1477 /* Set clock speed low in case setting the host state raised it, which
1478 * would only happen if host state was previously TORPID
1480 sr = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
1481 if (sr != CSR_RESULT_SUCCESS)
1483 unifi_error(card->ospriv, "CsrSdioMaxBusClockFrequencySet() failed2 %d\n", sr);
1485 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
1492 /* Perform a s/w reset to preserve as much as the card state as possible,
1493 * (mainly the preserve RAM). The context will be lost for coredump - but as we
1494 * were unable to access the WLAN function for panic, the coredump would have
1495 * also failed without a reset.
1497 unifi_info(card->ospriv, "Try s/w reset\n");
1499 r = unifi_card_hard_reset(card);
1500 if (r != CSR_RESULT_SUCCESS)
1502 unifi_error(card->ospriv, "unifi_card_hard_reset() failed %d\n", r);
1509 unifi_info(card->ospriv, "Read chip version 0x%x after %d retries\n", data_u16, i);
1515 r = ConvertCsrSdioToCsrHipResult(card, sr);
1522 * ---------------------------------------------------------------------------
1524 * Reads, saves and prints panic codes stored by the firmware in UniFi's
1525 * preserve RAM by the last panic that occurred since chip was powered.
1526 * Nothing is saved if the panic codes are read as zero.
1529 * card Pointer to card struct
1532 * ---------------------------------------------------------------------------
1534 void unifi_read_panic(card_t *card)
1541 /* The firmware must have previously initialised to read the panic addresses
1544 if (!card->panic_data_phy_addr || !card->panic_data_mac_addr)
1549 /* Get the panic data from PHY */
1550 r = unifi_card_read16(card, card->panic_data_phy_addr, &p_code);
1551 if (r != CSR_RESULT_SUCCESS)
1553 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_phy_addr, r);
1558 r = unifi_card_read16(card, card->panic_data_phy_addr + 2, &p_arg);
1559 if (r != CSR_RESULT_SUCCESS)
1561 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_phy_addr + 2, r);
1563 unifi_error(card->ospriv, "Last UniFi PHY PANIC %04x arg %04x\n", p_code, p_arg);
1564 card->last_phy_panic_code = p_code;
1565 card->last_phy_panic_arg = p_arg;
1568 /* Get the panic data from MAC */
1569 r = unifi_card_read16(card, card->panic_data_mac_addr, &p_code);
1570 if (r != CSR_RESULT_SUCCESS)
1572 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_mac_addr, r);
1577 r = unifi_card_read16(card, card->panic_data_mac_addr + 2, &p_arg);
1578 if (r != CSR_RESULT_SUCCESS)
1580 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_mac_addr + 2, r);
1582 unifi_error(card->ospriv, "Last UniFi MAC PANIC %04x arg %04x\n", p_code, p_arg);
1583 card->last_mac_panic_code = p_code;
1584 card->last_mac_panic_arg = p_arg;
1592 * ---------------------------------------------------------------------------
1593 * card_allocate_memory_resources
1595 * Allocates memory for the from-host, to-host bulk data slots,
1596 * soft queue buffers and bulk data buffers.
1599 * card Pointer to card struct
1602 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1603 * ---------------------------------------------------------------------------
1605 static CsrResult card_allocate_memory_resources(card_t *card)
1608 sdio_config_data_t *cfg_data;
1612 /* Reset any state carried forward from a previous life */
1613 card->fh_command_queue.q_rd_ptr = 0;
1614 card->fh_command_queue.q_wr_ptr = 0;
1615 (void)CsrSnprintf(card->fh_command_queue.name, UNIFI_QUEUE_NAME_MAX_LENGTH,
1617 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1619 card->fh_traffic_queue[i].q_rd_ptr = 0;
1620 card->fh_traffic_queue[i].q_wr_ptr = 0;
1621 (void)CsrSnprintf(card->fh_traffic_queue[i].name,
1622 UNIFI_QUEUE_NAME_MAX_LENGTH, "fh_data_q%d", i);
1624 #ifndef CSR_WIFI_HIP_TA_DISABLE
1625 unifi_ta_sampling_init(card);
1627 /* Convenience short-cut */
1628 cfg_data = &card->config_data;
1631 * Allocate memory for the from-host and to-host signal buffers.
1633 card->fh_buffer.buf = kmalloc(UNIFI_FH_BUF_SIZE, GFP_KERNEL);
1634 if (card->fh_buffer.buf == NULL)
1636 unifi_error(card->ospriv, "Failed to allocate memory for F-H signals\n");
1637 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1638 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1640 card->fh_buffer.bufsize = UNIFI_FH_BUF_SIZE;
1641 card->fh_buffer.ptr = card->fh_buffer.buf;
1642 card->fh_buffer.count = 0;
1644 card->th_buffer.buf = kmalloc(UNIFI_FH_BUF_SIZE, GFP_KERNEL);
1645 if (card->th_buffer.buf == NULL)
1647 unifi_error(card->ospriv, "Failed to allocate memory for T-H signals\n");
1648 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1649 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1651 card->th_buffer.bufsize = UNIFI_FH_BUF_SIZE;
1652 card->th_buffer.ptr = card->th_buffer.buf;
1653 card->th_buffer.count = 0;
1657 * Allocate memory for the from-host and to-host bulk data slots.
1658 * This is done as separate kmallocs because lots of smaller
1659 * allocations are more likely to succeed than one huge one.
1662 /* Allocate memory for the array of pointers */
1663 n = cfg_data->num_fromhost_data_slots;
1665 unifi_trace(card->ospriv, UDBG3, "Alloc from-host resources, %d slots.\n", n);
1666 card->from_host_data = kmalloc(n * sizeof(slot_desc_t), GFP_KERNEL);
1667 if (card->from_host_data == NULL)
1669 unifi_error(card->ospriv, "Failed to allocate memory for F-H bulk data array\n");
1670 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1671 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1674 /* Initialise from-host bulk data slots */
1675 for (i = 0; i < n; i++)
1677 UNIFI_INIT_BULK_DATA(&card->from_host_data[i].bd);
1680 /* Allocate memory for the array used for slot host tag mapping */
1681 card->fh_slot_host_tag_record = kmalloc(n * sizeof(u32), GFP_KERNEL);
1683 if (card->fh_slot_host_tag_record == NULL)
1685 unifi_error(card->ospriv, "Failed to allocate memory for F-H slot host tag mapping array\n");
1686 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1687 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1690 /* Initialise host tag entries for from-host bulk data slots */
1691 for (i = 0; i < n; i++)
1693 card->fh_slot_host_tag_record[i] = CSR_WIFI_HIP_RESERVED_HOST_TAG;
1697 /* Allocate memory for the array of pointers */
1698 n = cfg_data->num_tohost_data_slots;
1700 unifi_trace(card->ospriv, UDBG3, "Alloc to-host resources, %d slots.\n", n);
1701 card->to_host_data = kmalloc(n * sizeof(bulk_data_desc_t), GFP_KERNEL);
1702 if (card->to_host_data == NULL)
1704 unifi_error(card->ospriv, "Failed to allocate memory for T-H bulk data array\n");
1705 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1706 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1709 /* Initialise to-host bulk data slots */
1710 for (i = 0; i < n; i++)
1712 UNIFI_INIT_BULK_DATA(&card->to_host_data[i]);
1716 * Initialise buffers for soft Q
1718 for (i = 0; i < UNIFI_SOFT_COMMAND_Q_LENGTH; i++)
1720 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1722 UNIFI_INIT_BULK_DATA(&card->fh_command_q_body[i].bulkdata[r]);
1726 for (k = 0; k < UNIFI_NO_OF_TX_QS; k++)
1728 for (i = 0; i < UNIFI_SOFT_TRAFFIC_Q_LENGTH; i++)
1730 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1732 UNIFI_INIT_BULK_DATA(&card->fh_traffic_q_body[k][i].bulkdata[r]);
1737 card->memory_resources_allocated = 1;
1740 return CSR_RESULT_SUCCESS;
1741 } /* card_allocate_memory_resources() */
1745 * ---------------------------------------------------------------------------
1746 * unifi_free_bulk_data
1748 * Free the data associated to a bulk data structure.
1751 * card Pointer to card struct
1752 * bulk_data_slot Pointer to bulk data structure
1757 * ---------------------------------------------------------------------------
1759 static void unifi_free_bulk_data(card_t *card, bulk_data_desc_t *bulk_data_slot)
1761 if (bulk_data_slot->data_length != 0)
1763 unifi_net_data_free(card->ospriv, bulk_data_slot);
1765 } /* unifi_free_bulk_data() */
1769 * ---------------------------------------------------------------------------
1770 * card_free_memory_resources
1772 * Frees memory allocated for the from-host, to-host bulk data slots,
1773 * soft queue buffers and bulk data buffers.
1776 * card Pointer to card struct
1780 * ---------------------------------------------------------------------------
1782 static void card_free_memory_resources(card_t *card)
1786 unifi_trace(card->ospriv, UDBG1, "Freeing card memory resources.\n");
1788 /* Clear our internal queues */
1789 unifi_cancel_pending_signals(card);
1792 kfree(card->to_host_data);
1793 card->to_host_data = NULL;
1795 kfree(card->from_host_data);
1796 card->from_host_data = NULL;
1798 /* free the memory for slot host tag mapping array */
1799 kfree(card->fh_slot_host_tag_record);
1800 card->fh_slot_host_tag_record = NULL;
1802 kfree(card->fh_buffer.buf);
1803 card->fh_buffer.ptr = card->fh_buffer.buf = NULL;
1804 card->fh_buffer.bufsize = 0;
1805 card->fh_buffer.count = 0;
1807 kfree(card->th_buffer.buf);
1808 card->th_buffer.ptr = card->th_buffer.buf = NULL;
1809 card->th_buffer.bufsize = 0;
1810 card->th_buffer.count = 0;
1813 card->memory_resources_allocated = 0;
1816 } /* card_free_memory_resources() */
1819 static void card_init_soft_queues(card_t *card)
1825 unifi_trace(card->ospriv, UDBG1, "Initialising internal signal queues.\n");
1826 /* Reset any state carried forward from a previous life */
1827 card->fh_command_queue.q_rd_ptr = 0;
1828 card->fh_command_queue.q_wr_ptr = 0;
1829 (void)CsrSnprintf(card->fh_command_queue.name, UNIFI_QUEUE_NAME_MAX_LENGTH,
1831 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1833 card->fh_traffic_queue[i].q_rd_ptr = 0;
1834 card->fh_traffic_queue[i].q_wr_ptr = 0;
1835 (void)CsrSnprintf(card->fh_traffic_queue[i].name,
1836 UNIFI_QUEUE_NAME_MAX_LENGTH, "fh_data_q%d", i);
1838 #ifndef CSR_WIFI_HIP_TA_DISABLE
1839 unifi_ta_sampling_init(card);
1846 * ---------------------------------------------------------------------------
1847 * unifi_cancel_pending_signals
1849 * Free the signals and associated bulk data, pending in the core.
1852 * card Pointer to card struct
1856 * ---------------------------------------------------------------------------
1858 void unifi_cancel_pending_signals(card_t *card)
1863 unifi_trace(card->ospriv, UDBG1, "Canceling pending signals.\n");
1865 if (card->to_host_data)
1868 * Free any bulk data buffers allocated for the t-h slots
1869 * This will clear all buffers that did not make it to
1870 * unifi_receive_event() before cancel was request.
1872 n = card->config_data.num_tohost_data_slots;
1873 unifi_trace(card->ospriv, UDBG3, "Freeing to-host resources, %d slots.\n", n);
1874 for (i = 0; i < n; i++)
1876 unifi_free_bulk_data(card, &card->to_host_data[i]);
1881 * If any of the from-host bulk data has reached the card->from_host_data
1882 * but not UniFi, we need to free the buffers here.
1884 if (card->from_host_data)
1886 /* Free any bulk data buffers allocated for the f-h slots */
1887 n = card->config_data.num_fromhost_data_slots;
1888 unifi_trace(card->ospriv, UDBG3, "Freeing from-host resources, %d slots.\n", n);
1889 for (i = 0; i < n; i++)
1891 unifi_free_bulk_data(card, &card->from_host_data[i].bd);
1894 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1896 card->dynamic_slot_data.from_host_used_slots[i] = 0;
1897 card->dynamic_slot_data.from_host_max_slots[i] = 0;
1898 card->dynamic_slot_data.from_host_reserved_slots[i] = 0;
1903 * Free any bulk data buffers allocated in the soft queues.
1904 * This covers the case where a bulk data pointer has reached the soft queue
1905 * but not the card->from_host_data.
1907 unifi_trace(card->ospriv, UDBG3, "Freeing cmd q resources.\n");
1908 for (i = 0; i < UNIFI_SOFT_COMMAND_Q_LENGTH; i++)
1910 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1912 unifi_free_bulk_data(card, &card->fh_command_q_body[i].bulkdata[r]);
1916 unifi_trace(card->ospriv, UDBG3, "Freeing traffic q resources.\n");
1917 for (n = 0; n < UNIFI_NO_OF_TX_QS; n++)
1919 for (i = 0; i < UNIFI_SOFT_TRAFFIC_Q_LENGTH; i++)
1921 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1923 unifi_free_bulk_data(card, &card->fh_traffic_q_body[n][i].bulkdata[r]);
1928 card_init_soft_queues(card);
1931 } /* unifi_cancel_pending_signals() */
1935 * ---------------------------------------------------------------------------
1938 * Free the memory allocated for the card structure and buffers.
1941 * The porting layer is responsible for freeing any mini-coredump buffers
1942 * allocated when it called unifi_coredump_init(), by calling
1943 * unifi_coredump_free() before calling this function.
1946 * card Pointer to card struct
1950 * ---------------------------------------------------------------------------
1952 void unifi_free_card(card_t *card)
1955 #ifdef CSR_PRE_ALLOC_NET_DATA
1956 prealloc_netdata_free(card);
1958 /* Free any memory allocated. */
1959 card_free_memory_resources(card);
1961 /* Warn if caller didn't free coredump buffers */
1964 unifi_error(card->ospriv, "Caller should call unifi_coredump_free()\n");
1965 unifi_coredump_free(card); /* free anyway to prevent memory leak */
1971 } /* unifi_free_card() */
1975 * ---------------------------------------------------------------------------
1978 * Allocate memory for host-side slot data and signal queues.
1981 * card Pointer to card object
1985 * ---------------------------------------------------------------------------
1987 static CsrResult card_init_slots(card_t *card)
1994 /* Allocate the buffers we need, only once. */
1995 if (card->memory_resources_allocated == 1)
1997 card_free_memory_resources(card);
2001 /* Initialise our internal command and traffic queues */
2002 card_init_soft_queues(card);
2005 r = card_allocate_memory_resources(card);
2006 if (r != CSR_RESULT_SUCCESS)
2008 unifi_error(card->ospriv, "Failed to allocate card memory resources.\n");
2009 card_free_memory_resources(card);
2014 if (card->sdio_ctrl_addr == 0)
2016 unifi_error(card->ospriv, "Failed to find config struct!\n");
2017 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
2018 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2022 * Set initial counts.
2025 card->from_host_data_head = 0;
2027 /* Get initial signal counts from UniFi, in case it has not been reset. */
2031 /* Get the from-host-signals-written count */
2032 r = unifi_card_read16(card, card->sdio_ctrl_addr + 0, &s);
2033 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2037 if (r != CSR_RESULT_SUCCESS)
2039 unifi_error(card->ospriv, "Failed to read from-host sig written count\n");
2043 card->from_host_signals_w = (s16)s;
2045 /* Get the to-host-signals-written count */
2046 r = unifi_card_read16(card, card->sdio_ctrl_addr + 6, &s);
2047 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2051 if (r != CSR_RESULT_SUCCESS)
2053 unifi_error(card->ospriv, "Failed to read to-host sig read count\n");
2057 card->to_host_signals_r = (s16)s;
2060 /* Set Initialised flag. */
2061 r = unifi_card_write16(card, card->init_flag_addr, 0x0001);
2062 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2066 if (r != CSR_RESULT_SUCCESS)
2068 unifi_error(card->ospriv, "Failed to write initialised flag\n");
2073 /* Dynamic queue reservation */
2074 memset(&card->dynamic_slot_data, 0, sizeof(card_dynamic_slot_t));
2076 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2078 card->dynamic_slot_data.from_host_max_slots[i] = card->config_data.num_fromhost_data_slots -
2079 UNIFI_RESERVED_COMMAND_SLOTS;
2080 card->dynamic_slot_data.queue_stable[i] = FALSE;
2083 card->dynamic_slot_data.packets_interval = UNIFI_PACKETS_INTERVAL;
2086 return CSR_RESULT_SUCCESS;
2087 } /* card_init_slots() */
2091 * ---------------------------------------------------------------------------
2092 * unifi_set_udi_hook
2094 * Registers the udi hook that reports the sent signals to the core.
2097 * card Pointer to the card context struct
2098 * udi_fn Pointer to the callback function.
2101 * CSR_WIFI_HIP_RESULT_INVALID_VALUE if the card pointer is invalid,
2102 * CSR_RESULT_SUCCESS on success.
2103 * ---------------------------------------------------------------------------
2105 CsrResult unifi_set_udi_hook(card_t *card, udi_func_t udi_fn)
2109 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2112 if (card->udi_hook == NULL)
2114 card->udi_hook = udi_fn;
2117 return CSR_RESULT_SUCCESS;
2118 } /* unifi_set_udi_hook() */
2122 * ---------------------------------------------------------------------------
2123 * unifi_remove_udi_hook
2125 * Removes the udi hook that reports the sent signals from the core.
2128 * card Pointer to the card context struct
2129 * udi_fn Pointer to the callback function.
2132 * CSR_WIFI_HIP_RESULT_INVALID_VALUE if the card pointer is invalid,
2133 * CSR_RESULT_SUCCESS on success.
2134 * ---------------------------------------------------------------------------
2136 CsrResult unifi_remove_udi_hook(card_t *card, udi_func_t udi_fn)
2140 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2143 if (card->udi_hook == udi_fn)
2145 card->udi_hook = NULL;
2148 return CSR_RESULT_SUCCESS;
2149 } /* unifi_remove_udi_hook() */
2152 static void CardReassignDynamicReservation(card_t *card)
2158 unifi_trace(card->ospriv, UDBG5, "Packets Txed %d %d %d %d\n",
2159 card->dynamic_slot_data.packets_txed[0],
2160 card->dynamic_slot_data.packets_txed[1],
2161 card->dynamic_slot_data.packets_txed[2],
2162 card->dynamic_slot_data.packets_txed[3]);
2164 /* Clear reservation and recalculate max slots */
2165 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2167 card->dynamic_slot_data.queue_stable[i] = FALSE;
2168 card->dynamic_slot_data.from_host_reserved_slots[i] = 0;
2169 card->dynamic_slot_data.from_host_max_slots[i] = card->config_data.num_fromhost_data_slots -
2170 UNIFI_RESERVED_COMMAND_SLOTS;
2171 card->dynamic_slot_data.packets_txed[i] = 0;
2173 unifi_trace(card->ospriv, UDBG5, "CardReassignDynamicReservation: queue %d reserved %d Max %d\n", i,
2174 card->dynamic_slot_data.from_host_reserved_slots[i],
2175 card->dynamic_slot_data.from_host_max_slots[i]);
2178 card->dynamic_slot_data.total_packets_txed = 0;
2183 /* Algorithm to dynamically reserve slots. The logic is based mainly on the outstanding queue
2184 * length. Slots are reserved for particular queues during an interval and cleared after the interval.
2185 * Each queue has three associated variables.. a) used slots - the number of slots currently occupied
2186 * by the queue b) reserved slots - number of slots reserved specifically for the queue c) max slots - total
2187 * slots that this queue can actually use (may be higher than reserved slots and is dependent on reserved slots
2188 * for other queues).
2189 * This function is called when there are no slots available for a queue. It checks to see if there are enough
2190 * unreserved slots sufficient for this request. If available these slots are reserved for the queue.
2191 * If there are not enough unreserved slots, a fair share for each queue is calculated based on the total slots
2192 * and the number of active queues (any queue with existing reservation is considered active). Queues needing
2193 * less than their fair share are allowed to have the previously reserved slots. The remaining slots are
2194 * distributed evenly among queues that need more than the fair share
2196 * A better scheme would take current bandwidth per AC into consideration when reserving slots. An
2197 * implementation scheme could consider the relative time/service period for slots in an AC. If the firmware
2198 * services other ACs faster than a particular AC (packets wait in the slots longer) then it is fair to reserve
2199 * less slots for the AC
2201 static void CardCheckDynamicReservation(card_t *card, unifi_TrafficQueue queue)
2203 u16 q_len, active_queues = 0, excess_queue_slots, div_extra_slots,
2204 queue_fair_share, reserved_slots = 0, q, excess_need_queues = 0, unmovable_slots = 0;
2207 u16 num_data_slots = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
2211 /* Calculate the pending queue length */
2212 sigq = &card->fh_traffic_queue[queue];
2213 q_len = CSR_WIFI_HIP_Q_SLOTS_USED(sigq);
2215 if (q_len <= card->dynamic_slot_data.from_host_reserved_slots[queue])
2217 unifi_trace(card->ospriv, UDBG5, "queue %d q_len %d already has that many reserved slots, exiting\n", queue, q_len);
2223 if (q_len > num_data_slots)
2225 q_len = num_data_slots;
2228 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2230 if (i != (s32)queue)
2232 reserved_slots += card->dynamic_slot_data.from_host_reserved_slots[i];
2234 if ((i == (s32)queue) || (card->dynamic_slot_data.from_host_reserved_slots[i] > 0))
2240 unifi_trace(card->ospriv, UDBG5, "CardCheckDynamicReservation: queue %d q_len %d\n", queue, q_len);
2241 unifi_trace(card->ospriv, UDBG5, "Active queues %d reserved slots on other queues %d\n",
2242 active_queues, reserved_slots);
2244 if (reserved_slots + q_len <= num_data_slots)
2246 card->dynamic_slot_data.from_host_reserved_slots[queue] = q_len;
2247 if (q_len == num_data_slots)
2249 /* This is the common case when just 1 stream is going */
2250 card->dynamic_slot_data.queue_stable[queue] = TRUE;
2255 queue_fair_share = num_data_slots / active_queues;
2256 unifi_trace(card->ospriv, UDBG5, "queue fair share %d\n", queue_fair_share);
2258 /* Evenly distribute slots among active queues */
2259 /* Find out the queues that need excess of fair share. Also find slots allocated
2260 * to queues less than their fair share, these slots cannot be reallocated (unmovable slots) */
2262 card->dynamic_slot_data.from_host_reserved_slots[queue] = q_len;
2264 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2266 if (card->dynamic_slot_data.from_host_reserved_slots[i] > queue_fair_share)
2268 excess_need_queues++;
2272 unmovable_slots += card->dynamic_slot_data.from_host_reserved_slots[i];
2276 unifi_trace(card->ospriv, UDBG5, "Excess need queues %d\n", excess_need_queues);
2278 /* Now find the slots per excess demand queue */
2279 excess_queue_slots = (num_data_slots - unmovable_slots) / excess_need_queues;
2280 div_extra_slots = (num_data_slots - unmovable_slots) - excess_queue_slots * excess_need_queues;
2281 for (i = UNIFI_NO_OF_TX_QS - 1; i >= 0; i--)
2283 if (card->dynamic_slot_data.from_host_reserved_slots[i] > excess_queue_slots)
2285 card->dynamic_slot_data.from_host_reserved_slots[i] = excess_queue_slots;
2286 if (div_extra_slots > 0)
2288 card->dynamic_slot_data.from_host_reserved_slots[i]++;
2291 /* No more slots will be allocated to this queue during the current interval */
2292 card->dynamic_slot_data.queue_stable[i] = TRUE;
2293 unifi_trace(card->ospriv, UDBG5, "queue stable %d\n", i);
2298 /* Redistribute max slots */
2299 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2302 for (q = 0; q < UNIFI_NO_OF_TX_QS; q++)
2306 reserved_slots += card->dynamic_slot_data.from_host_reserved_slots[q];
2310 card->dynamic_slot_data.from_host_max_slots[i] = num_data_slots - reserved_slots;
2311 unifi_trace(card->ospriv, UDBG5, "queue %d reserved %d Max %d\n", i,
2312 card->dynamic_slot_data.from_host_reserved_slots[i],
2313 card->dynamic_slot_data.from_host_max_slots[i]);
2321 * ---------------------------------------------------------------------------
2322 * CardClearFromHostDataSlot
2324 * Clear a the given data slot, making it available again.
2327 * card Pointer to Card object
2328 * slot Index of the signal slot to clear.
2332 * ---------------------------------------------------------------------------
2334 void CardClearFromHostDataSlot(card_t *card, const s16 slot)
2336 u8 queue = card->from_host_data[slot].queue;
2337 const void *os_data_ptr = card->from_host_data[slot].bd.os_data_ptr;
2341 if (card->from_host_data[slot].bd.data_length == 0)
2343 unifi_warning(card->ospriv,
2344 "Surprise: request to clear an already free FH data slot: %d\n",
2350 if (os_data_ptr == NULL)
2352 unifi_warning(card->ospriv,
2353 "Clearing FH data slot %d: has null payload, len=%d\n",
2354 slot, card->from_host_data[slot].bd.data_length);
2357 /* Free card->from_host_data[slot].bd.os_net_ptr here. */
2358 /* Mark slot as free by setting length to 0. */
2359 unifi_free_bulk_data(card, &card->from_host_data[slot].bd);
2360 if (queue < UNIFI_NO_OF_TX_QS)
2362 if (card->dynamic_slot_data.from_host_used_slots[queue] == 0)
2364 unifi_error(card->ospriv, "Goofed up used slots q = %d used slots = %d\n",
2366 card->dynamic_slot_data.from_host_used_slots[queue]);
2370 card->dynamic_slot_data.from_host_used_slots[queue]--;
2372 card->dynamic_slot_data.packets_txed[queue]++;
2373 card->dynamic_slot_data.total_packets_txed++;
2374 if (card->dynamic_slot_data.total_packets_txed >= card->dynamic_slot_data.packets_interval)
2376 CardReassignDynamicReservation(card);
2380 unifi_trace(card->ospriv, UDBG4, "CardClearFromHostDataSlot: slot %d recycled %p\n", slot, os_data_ptr);
2383 } /* CardClearFromHostDataSlot() */
2386 #ifdef CSR_WIFI_REQUEUE_PACKET_TO_HAL
2388 * ---------------------------------------------------------------------------
2389 * CardClearFromHostDataSlotWithoutFreeingBulkData
2391 * Clear the given data slot with out freeing the bulk data.
2394 * card Pointer to Card object
2395 * slot Index of the signal slot to clear.
2399 * ---------------------------------------------------------------------------
2401 void CardClearFromHostDataSlotWithoutFreeingBulkData(card_t *card, const s16 slot)
2403 u8 queue = card->from_host_data[slot].queue;
2405 /* Initialise the from_host data slot so it can be re-used,
2406 * Set length field in from_host_data array to 0.
2408 UNIFI_INIT_BULK_DATA(&card->from_host_data[slot].bd);
2410 queue = card->from_host_data[slot].queue;
2412 if (queue < UNIFI_NO_OF_TX_QS)
2414 if (card->dynamic_slot_data.from_host_used_slots[queue] == 0)
2416 unifi_error(card->ospriv, "Goofed up used slots q = %d used slots = %d\n",
2418 card->dynamic_slot_data.from_host_used_slots[queue]);
2422 card->dynamic_slot_data.from_host_used_slots[queue]--;
2424 card->dynamic_slot_data.packets_txed[queue]++;
2425 card->dynamic_slot_data.total_packets_txed++;
2426 if (card->dynamic_slot_data.total_packets_txed >=
2427 card->dynamic_slot_data.packets_interval)
2429 CardReassignDynamicReservation(card);
2432 } /* CardClearFromHostDataSlotWithoutFreeingBulkData() */
2437 u16 CardGetDataSlotSize(card_t *card)
2439 return card->config_data.data_slot_size;
2440 } /* CardGetDataSlotSize() */
2444 * ---------------------------------------------------------------------------
2445 * CardGetFreeFromHostDataSlots
2447 * Retrieve the number of from-host bulk data slots available.
2450 * card Pointer to the card context struct
2453 * Number of free from-host bulk data slots.
2454 * ---------------------------------------------------------------------------
2456 u16 CardGetFreeFromHostDataSlots(card_t *card)
2462 /* First two slots reserved for MLME */
2463 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
2465 if (card->from_host_data[i].bd.data_length == 0)
2474 } /* CardGetFreeFromHostDataSlots() */
2478 * ---------------------------------------------------------------------------
2479 * CardAreAllFromHostDataSlotsEmpty
2481 * Returns the state of from-host bulk data slots.
2484 * card Pointer to the card context struct
2487 * 1 The from-host bulk data slots are all empty (available).
2488 * 0 Some or all the from-host bulk data slots are in use.
2489 * ---------------------------------------------------------------------------
2491 u16 CardAreAllFromHostDataSlotsEmpty(card_t *card)
2495 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
2497 if (card->from_host_data[i].bd.data_length != 0)
2504 } /* CardGetFreeFromHostDataSlots() */
2507 static CsrResult unifi_identify_hw(card_t *card)
2511 card->chip_id = card->sdio_if->sdioId.cardId;
2512 card->function = card->sdio_if->sdioId.sdioFunction;
2513 card->sdio_io_block_size = card->sdio_if->blockSize;
2515 /* If SDIO controller doesn't support byte mode CMD53, pad transfers to block sizes */
2516 card->sdio_io_block_pad = (card->sdio_if->features & CSR_SDIO_FEATURE_BYTE_MODE)?FALSE : TRUE;
2519 * Setup the chip helper so that we can access the registers (and
2520 * also tell what sub-type of HIP we should use).
2522 card->helper = ChipHelper_GetVersionSdio((u8)card->chip_id);
2525 unifi_error(card->ospriv, "Null ChipHelper\n");
2528 unifi_info(card->ospriv, "Chip ID 0x%02X Function %u Block Size %u Name %s(%s)\n",
2529 card->chip_id, card->function, card->sdio_io_block_size,
2530 ChipHelper_MarketingName(card->helper),
2531 ChipHelper_FriendlyName(card->helper));
2534 return CSR_RESULT_SUCCESS;
2535 } /* unifi_identify_hw() */
2538 static CsrResult unifi_prepare_hw(card_t *card)
2541 CsrResult csrResult;
2542 enum unifi_host_state old_state = card->host_state;
2546 r = unifi_identify_hw(card);
2547 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2551 if (r != CSR_RESULT_SUCCESS)
2553 unifi_error(card->ospriv, "Failed to identify hw\n");
2558 unifi_trace(card->ospriv, UDBG1,
2559 "%s mode SDIO\n", card->sdio_io_block_pad?"Block" : "Byte");
2561 * Chip must be a awake or blocks that are asleep may not get
2562 * reset. We can only do this after we have read the chip_id.
2564 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
2565 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2570 if (old_state == UNIFI_HOST_STATE_TORPID)
2572 /* Ensure the initial clock rate is set; if a reset occured when the chip was
2573 * TORPID, unifi_set_host_state() may have raised it to MAX.
2575 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_INIT_HZ);
2576 if (csrResult != CSR_RESULT_SUCCESS)
2578 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2582 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_INIT_HZ;
2586 * The WLAN function must be enabled to access MAILBOX2 and DEBUG_RST
2589 csrResult = CsrSdioFunctionEnable(card->sdio_if);
2590 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2592 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2594 if (csrResult != CSR_RESULT_SUCCESS)
2596 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2597 /* Can't enable WLAN function. Try resetting the SDIO block. */
2598 unifi_error(card->ospriv, "Failed to re-enable function %d.\n", card->function);
2604 * Poke some registers to make sure the PLL has started,
2605 * otherwise memory accesses are likely to fail.
2607 bootstrap_chip_hw(card);
2609 /* Try to read the chip version from register. */
2610 r = unifi_read_chip_version(card);
2611 if (r != CSR_RESULT_SUCCESS)
2618 return CSR_RESULT_SUCCESS;
2619 } /* unifi_prepare_hw() */
2622 static CsrResult unifi_read_chip_version(card_t *card)
2624 u32 gbl_chip_version;
2630 gbl_chip_version = ChipHelper_GBL_CHIP_VERSION(card->helper);
2632 /* Try to read the chip version from register. */
2633 if (gbl_chip_version != 0)
2635 r = unifi_read_direct16(card, gbl_chip_version * 2, &ver);
2636 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2640 if (r != CSR_RESULT_SUCCESS)
2642 unifi_error(card->ospriv, "Failed to read GBL_CHIP_VERSION\n");
2646 card->chip_version = ver;
2650 unifi_info(card->ospriv, "Unknown Chip ID, cannot locate GBL_CHIP_VERSION\n");
2651 r = CSR_RESULT_FAILURE;
2654 unifi_info(card->ospriv, "Chip Version 0x%04X\n", card->chip_version);
2658 } /* unifi_read_chip_version() */
2662 * ---------------------------------------------------------------------------
2663 * unifi_reset_hardware
2665 * Execute the UniFi reset sequence.
2667 * Note: This may fail if the chip is going TORPID so retry at
2671 * card - pointer to card context structure
2674 * CSR_RESULT_SUCCESS on success, CSR error otherwise.
2677 * Some platforms (e.g. Windows Vista) do not allow access to registers
2678 * that are necessary for a software soft reset.
2679 * ---------------------------------------------------------------------------
2681 static CsrResult unifi_reset_hardware(card_t *card)
2684 u16 new_block_size = UNIFI_IO_BLOCK_SIZE;
2685 CsrResult csrResult;
2689 /* Errors returned by unifi_prepare_hw() are not critical at this point */
2690 r = unifi_prepare_hw(card);
2691 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2696 /* First try SDIO controller reset, which may power cycle the UniFi, assert
2697 * its reset line, or not be implemented depending on the platform.
2699 unifi_info(card->ospriv, "Calling CsrSdioHardReset\n");
2700 csrResult = CsrSdioHardReset(card->sdio_if);
2701 if (csrResult == CSR_RESULT_SUCCESS)
2703 unifi_info(card->ospriv, "CsrSdioHardReset succeeded on reseting UniFi\n");
2704 r = unifi_prepare_hw(card);
2705 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2709 if (r != CSR_RESULT_SUCCESS)
2711 unifi_error(card->ospriv, "unifi_prepare_hw failed after hard reset\n");
2716 else if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2718 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2722 /* Falling back to software hard reset methods */
2723 unifi_info(card->ospriv, "Falling back to software hard reset\n");
2724 r = unifi_card_hard_reset(card);
2725 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2729 if (r != CSR_RESULT_SUCCESS)
2731 unifi_error(card->ospriv, "software hard reset failed\n");
2736 /* If we fell back to unifi_card_hard_reset() methods, chip version may
2737 * not have been read. (Note in the unlikely event that it is zero,
2738 * it will be harmlessly read again)
2740 if (card->chip_version == 0)
2742 r = unifi_read_chip_version(card);
2743 if (r != CSR_RESULT_SUCCESS)
2751 #ifdef CSR_WIFI_HIP_SDIO_BLOCK_SIZE
2752 new_block_size = CSR_WIFI_HIP_SDIO_BLOCK_SIZE;
2755 /* After hard reset, we need to restore the SDIO block size */
2756 csrResult = CsrSdioBlockSizeSet(card->sdio_if, new_block_size);
2757 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2759 /* Warn if a different block size was achieved by the transport */
2760 if (card->sdio_if->blockSize != new_block_size)
2762 unifi_info(card->ospriv,
2763 "Actually got block size %d\n", card->sdio_if->blockSize);
2766 /* sdio_io_block_size always needs be updated from the achieved block size,
2767 * as it is used by the OS layer to allocate memory in unifi_net_malloc().
2768 * Controllers which don't support block mode (e.g. CSPI) will report a
2769 * block size of zero.
2771 if (card->sdio_if->blockSize == 0)
2773 unifi_info(card->ospriv, "Block size 0, block mode not available\n");
2775 /* Set sdio_io_block_size to 1 so that unifi_net_data_malloc() has a
2776 * sensible rounding value. Elsewhere padding will already be
2777 * disabled because the controller supports byte mode.
2779 card->sdio_io_block_size = 1;
2781 /* Controller features must declare support for byte mode */
2782 if (!(card->sdio_if->features & CSR_SDIO_FEATURE_BYTE_MODE))
2784 unifi_error(card->ospriv, "Requires byte mode\n");
2785 r = CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2790 /* Padding will be enabled if CSR_SDIO_FEATURE_BYTE_MODE isn't set */
2791 card->sdio_io_block_size = card->sdio_if->blockSize;
2797 } /* unifi_reset_hardware() */
2801 * ---------------------------------------------------------------------------
2802 * card_reset_method_io_enable
2804 * Issue a hard reset to the hw writing the IO_ENABLE.
2807 * card Pointer to Card object
2811 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
2812 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
2813 * was not seen in the expected time
2814 * ---------------------------------------------------------------------------
2816 static CsrResult card_reset_method_io_enable(card_t *card)
2819 CsrResult csrResult;
2824 * This resets only function 1, so should be used in
2825 * preference to the method below (CSR_FUNC_EN)
2827 unifi_trace(card->ospriv, UDBG1, "Hard reset (IO_ENABLE)\n");
2829 csrResult = CsrSdioFunctionDisable(card->sdio_if);
2830 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2832 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2834 if (csrResult != CSR_RESULT_SUCCESS)
2836 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2837 unifi_warning(card->ospriv, "SDIO error writing IO_ENABLE: %d\n", r);
2841 /* Delay here to let the reset take affect. */
2842 CsrThreadSleep(RESET_SETTLE_DELAY);
2844 r = card_wait_for_unifi_to_disable(card);
2845 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2850 if (r == CSR_RESULT_SUCCESS)
2852 r = card_wait_for_unifi_to_reset(card);
2853 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2860 if (r != CSR_RESULT_SUCCESS)
2862 unifi_trace(card->ospriv, UDBG1, "Hard reset (CSR_FUNC_EN)\n");
2864 r = sdio_write_f0(card, SDIO_CSR_FUNC_EN, 0);
2865 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2869 if (r != CSR_RESULT_SUCCESS)
2871 unifi_warning(card->ospriv, "SDIO error writing SDIO_CSR_FUNC_EN: %d\n", r);
2877 /* Delay here to let the reset take affect. */
2878 CsrThreadSleep(RESET_SETTLE_DELAY);
2880 r = card_wait_for_unifi_to_reset(card);
2881 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2888 if (r != CSR_RESULT_SUCCESS)
2890 unifi_warning(card->ospriv, "card_reset_method_io_enable failed to reset UniFi\n");
2895 } /* card_reset_method_io_enable() */
2899 * ---------------------------------------------------------------------------
2900 * card_reset_method_dbg_reset
2902 * Issue a hard reset to the hw writing the DBG_RESET.
2905 * card Pointer to Card object
2908 * CSR_RESULT_SUCCESS on success,
2909 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
2910 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
2911 * was not seen in the expected time
2912 * ---------------------------------------------------------------------------
2914 static CsrResult card_reset_method_dbg_reset(card_t *card)
2921 * Prepare UniFi for h/w reset
2923 if (card->host_state == UNIFI_HOST_STATE_TORPID)
2925 r = unifi_set_host_state(card, UNIFI_HOST_STATE_DROWSY);
2926 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2930 if (r != CSR_RESULT_SUCCESS)
2932 unifi_error(card->ospriv, "Failed to set UNIFI_HOST_STATE_DROWSY\n");
2939 r = unifi_card_stop_processor(card, UNIFI_PROC_BOTH);
2940 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2944 if (r != CSR_RESULT_SUCCESS)
2946 unifi_error(card->ospriv, "Can't stop processors\n");
2951 unifi_trace(card->ospriv, UDBG1, "Hard reset (DBG_RESET)\n");
2954 * This register write may fail. The debug reset resets
2955 * parts of the Function 0 sections of the chip, and
2956 * therefore the response cannot be sent back to the host.
2958 r = unifi_write_direct_8_or_16(card, ChipHelper_DBG_RESET(card->helper) * 2, 1);
2959 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2963 if (r != CSR_RESULT_SUCCESS)
2965 unifi_warning(card->ospriv, "SDIO error writing DBG_RESET: %d\n", r);
2970 /* Delay here to let the reset take affect. */
2971 CsrThreadSleep(RESET_SETTLE_DELAY);
2973 r = card_wait_for_unifi_to_reset(card);
2974 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2978 if (r != CSR_RESULT_SUCCESS)
2980 unifi_warning(card->ospriv, "card_reset_method_dbg_reset failed to reset UniFi\n");
2985 } /* card_reset_method_dbg_reset() */
2989 * ---------------------------------------------------------------------------
2990 * unifi_card_hard_reset
2992 * Issue reset to hardware, by writing to registers on the card.
2993 * Power to the card is preserved.
2996 * card Pointer to Card object
2999 * CSR_RESULT_SUCCESS on success,
3000 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3001 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
3002 * was not seen in the expected time
3003 * ---------------------------------------------------------------------------
3005 CsrResult unifi_card_hard_reset(card_t *card)
3008 const struct chip_helper_reset_values *init_data;
3013 /* Clear cache of page registers */
3014 card->proc_select = (u32)(-1);
3015 card->dmem_page = (u32)(-1);
3016 card->pmem_page = (u32)(-1);
3019 * We need to have a valid card->helper before we use software hard reset.
3020 * If unifi_identify_hw() fails to get the card ID, it probably means
3021 * that there is no way to talk to the h/w.
3023 r = unifi_identify_hw(card);
3024 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3028 if (r != CSR_RESULT_SUCCESS)
3030 unifi_error(card->ospriv, "unifi_card_hard_reset failed to identify h/w\n");
3035 /* Search for some reset code. */
3036 chunks = ChipHelper_HostResetSequence(card->helper, &init_data);
3039 unifi_error(card->ospriv,
3040 "Hard reset (Code download) is unsupported\n");
3042 func_exit_r(CSR_RESULT_FAILURE);
3043 return CSR_RESULT_FAILURE;
3046 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3048 /* The HIP spec considers this a bus-specific reset.
3049 * This resets only function 1, so should be used in
3050 * preference to the method below (CSR_FUNC_EN)
3051 * If this method fails, it means that the f/w is probably
3052 * not running. In this case, try the DBG_RESET method.
3054 r = card_reset_method_io_enable(card);
3055 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3059 if (r == CSR_RESULT_SUCCESS)
3066 /* Software hard reset */
3067 r = card_reset_method_dbg_reset(card);
3071 } /* unifi_card_hard_reset() */
3075 * ---------------------------------------------------------------------------
3080 * This function causes an internal interrupt to be raised in the
3081 * UniFi chip. It is used to signal the firmware that some action has
3083 * The UniFi Host Interface asks that the value used increments for
3084 * debugging purposes.
3087 * card Pointer to Card object
3090 * CSR_RESULT_SUCCESS on success,
3091 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3092 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
3093 * was not seen in the expected time
3094 * ---------------------------------------------------------------------------
3096 CsrResult CardGenInt(card_t *card)
3102 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3104 r = sdio_write_f0(card, SDIO_CSR_FROM_HOST_SCRATCH0,
3105 (u8)card->unifi_interrupt_seq);
3109 r = unifi_write_direct_8_or_16(card,
3110 ChipHelper_SHARED_IO_INTERRUPT(card->helper) * 2,
3111 (u8)card->unifi_interrupt_seq);
3113 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3117 if (r != CSR_RESULT_SUCCESS)
3119 unifi_error(card->ospriv, "SDIO error writing UNIFI_SHARED_IO_INTERRUPT: %d\n", r);
3124 card->unifi_interrupt_seq++;
3127 return CSR_RESULT_SUCCESS;
3128 } /* CardGenInt() */
3132 * ---------------------------------------------------------------------------
3135 * Enable the outgoing SDIO interrupt from UniFi to the host.
3138 * card Pointer to Card object
3141 * CSR_RESULT_SUCCESS on success,
3142 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3143 * CSR_RESULT_FAILURE if an SDIO error occurred,
3144 * ---------------------------------------------------------------------------
3146 CsrResult CardEnableInt(card_t *card)
3151 r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3152 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3156 if (r != CSR_RESULT_SUCCESS)
3158 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3162 int_enable |= (1 << card->function) | UNIFI_SD_INT_ENABLE_IENM;
3164 r = sdio_write_f0(card, SDIO_INT_ENABLE, int_enable);
3165 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3169 if (r != CSR_RESULT_SUCCESS)
3171 unifi_error(card->ospriv, "SDIO error writing SDIO_INT_ENABLE\n");
3175 return CSR_RESULT_SUCCESS;
3176 } /* CardEnableInt() */
3180 * ---------------------------------------------------------------------------
3183 * Disable the outgoing SDIO interrupt from UniFi to the host.
3186 * card Pointer to Card object
3189 * CSR_RESULT_SUCCESS on success,
3190 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3191 * CSR_RESULT_FAILURE if an SDIO error occurred,
3192 * ---------------------------------------------------------------------------
3194 CsrResult CardDisableInt(card_t *card)
3199 r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3200 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3204 if (r != CSR_RESULT_SUCCESS)
3206 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3210 int_enable &= ~(1 << card->function);
3212 r = sdio_write_f0(card, SDIO_INT_ENABLE, int_enable);
3213 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3217 if (r != CSR_RESULT_SUCCESS)
3219 unifi_error(card->ospriv, "SDIO error writing SDIO_INT_ENABLE\n");
3223 return CSR_RESULT_SUCCESS;
3224 } /* CardDisableInt() */
3228 * ---------------------------------------------------------------------------
3231 * Determine whether UniFi is currently asserting the SDIO interrupt
3235 * card Pointer to Card object
3236 * pintr Pointer to location to write interrupt status,
3237 * TRUE if interrupt pending,
3238 * FALSE if no interrupt pending.
3240 * CSR_RESULT_SUCCESS interrupt status read successfully
3241 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3242 * CSR_RESULT_FAILURE if an SDIO error occurred,
3243 * ---------------------------------------------------------------------------
3245 CsrResult CardPendingInt(card_t *card, u8 *pintr)
3252 r = sdio_read_f0(card, SDIO_INT_PENDING, &pending);
3253 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3257 if (r != CSR_RESULT_SUCCESS)
3259 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_PENDING\n");
3263 *pintr = (pending & (1 << card->function))?TRUE : FALSE;
3265 return CSR_RESULT_SUCCESS;
3266 } /* CardPendingInt() */
3270 * ---------------------------------------------------------------------------
3273 * Clear the UniFi SDIO interrupt request.
3276 * card Pointer to Card object
3279 * CSR_RESULT_SUCCESS if pending interrupt was cleared, or no pending interrupt.
3280 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3281 * CSR_RESULT_FAILURE if an SDIO error occurred,
3282 * ---------------------------------------------------------------------------
3284 CsrResult CardClearInt(card_t *card)
3289 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3291 /* CardPendingInt() sets intr, if there is a pending interrupt */
3292 r = CardPendingInt(card, &intr);
3298 r = sdio_write_f0(card, SDIO_CSR_HOST_INT_CLEAR, 1);
3299 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3303 if (r != CSR_RESULT_SUCCESS)
3305 unifi_error(card->ospriv, "SDIO error writing SDIO_CSR_HOST_INT_CLEAR\n");
3310 r = unifi_write_direct_8_or_16(card,
3311 ChipHelper_SDIO_HOST_INT(card->helper) * 2,
3313 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3317 if (r != CSR_RESULT_SUCCESS)
3319 unifi_error(card->ospriv, "SDIO error writing UNIFI_SDIO_HOST_INT\n");
3324 } /* CardClearInt() */
3328 * ---------------------------------------------------------------------------
3331 * Determine whether UniFi is currently asserting the SDIO interrupt
3335 * card Pointer to Card object
3336 * enabled Pointer to location to write interrupt enable status,
3337 * TRUE if interrupts enabled,
3338 * FALSE if interupts disabled.
3341 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3342 * CSR_RESULT_FAILURE if an SDIO error occurred,
3343 * ---------------------------------------------------------------------------
3345 CsrResult CardIntEnabled(card_t *card, u8 *enabled)
3350 r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3351 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3355 if (r != CSR_RESULT_SUCCESS)
3357 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3361 *enabled = (int_enable & (1 << card->function))?TRUE : FALSE;
3363 return CSR_RESULT_SUCCESS;
3364 } /* CardIntEnabled() */
3368 * ---------------------------------------------------------------------------
3370 * Allocate slot in the pending bulkdata arrays and assign it to a signal's
3371 * bulkdata reference. The slot is then ready for UniFi's bulkdata commands
3372 * to transfer the data to/from the host.
3375 * card Pointer to Card object
3376 * csptr Pending signal pointer, including bulkdata ref
3377 * queue Traffic queue that this signal is using
3380 * CSR_RESULT_SUCCESS if a free slot was assigned
3381 * CSR_RESULT_FAILURE if no slot was available
3382 * ---------------------------------------------------------------------------
3384 CsrResult CardWriteBulkData(card_t *card, card_signal_t *csptr, unifi_TrafficQueue queue)
3386 u16 i, slots[UNIFI_MAX_DATA_REFERENCES], j = 0;
3387 u8 *packed_sigptr, num_slots_required = 0;
3388 bulk_data_desc_t *bulkdata = csptr->bulkdata;
3393 /* Count the number of slots required */
3394 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++)
3396 if (bulkdata[i].data_length != 0)
3398 num_slots_required++;
3402 /* Get the slot numbers */
3403 if (num_slots_required != 0)
3405 /* Last 2 slots for MLME */
3406 if (queue == UNIFI_TRAFFIC_Q_MLME)
3408 h = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
3409 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
3411 if (card->from_host_data[h].bd.data_length == 0)
3413 /* Free data slot, claim it */
3415 if (j == num_slots_required)
3421 if (++h >= card->config_data.num_fromhost_data_slots)
3429 if (card->dynamic_slot_data.from_host_used_slots[queue]
3430 < card->dynamic_slot_data.from_host_max_slots[queue])
3432 /* Data commands get a free slot only after a few checks */
3433 nslots = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
3435 h = card->from_host_data_head;
3437 for (i = 0; i < nslots; i++)
3439 if (card->from_host_data[h].bd.data_length == 0)
3441 /* Free data slot, claim it */
3443 if (j == num_slots_required)
3454 card->from_host_data_head = h;
3458 /* Required number of slots are not available, bail out */
3459 if (j != num_slots_required)
3461 unifi_trace(card->ospriv, UDBG5, "CardWriteBulkData: didn't find free slot/s\n");
3463 /* If we haven't already reached the stable state we can ask for reservation */
3464 if ((queue != UNIFI_TRAFFIC_Q_MLME) && (card->dynamic_slot_data.queue_stable[queue] == FALSE))
3466 CardCheckDynamicReservation(card, queue);
3469 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
3471 unifi_trace(card->ospriv, UDBG5, "fh data slot %d: %d\n", i, card->from_host_data[i].bd.data_length);
3474 return CSR_RESULT_FAILURE;
3478 packed_sigptr = csptr->sigbuf;
3480 /* Fill in the slots with data */
3482 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++)
3484 if (bulkdata[i].data_length == 0)
3486 /* Zero-out the DATAREF in the signal */
3487 SET_PACKED_DATAREF_SLOT(packed_sigptr, i, 0);
3488 SET_PACKED_DATAREF_LEN(packed_sigptr, i, 0);
3493 * Fill in the slot number in the SIGNAL structure but
3494 * preserve the offset already in there
3496 SET_PACKED_DATAREF_SLOT(packed_sigptr, i, slots[j] | (((u16)packed_sigptr[SIZEOF_SIGNAL_HEADER + (i * SIZEOF_DATAREF) + 1]) << 8));
3497 SET_PACKED_DATAREF_LEN(packed_sigptr, i, bulkdata[i].data_length);
3499 /* Do not copy the data, just store the information to them */
3500 card->from_host_data[slots[j]].bd.os_data_ptr = bulkdata[i].os_data_ptr;
3501 card->from_host_data[slots[j]].bd.os_net_buf_ptr = bulkdata[i].os_net_buf_ptr;
3502 card->from_host_data[slots[j]].bd.data_length = bulkdata[i].data_length;
3503 card->from_host_data[slots[j]].bd.net_buf_length = bulkdata[i].net_buf_length;
3504 card->from_host_data[slots[j]].queue = queue;
3506 unifi_trace(card->ospriv, UDBG4, "CardWriteBulkData sig=0x%x, fh slot %d = %p\n",
3507 GET_SIGNAL_ID(packed_sigptr), i, bulkdata[i].os_data_ptr);
3509 /* Sanity-check that the bulk data desc being assigned to the slot
3510 * actually has a payload.
3512 if (!bulkdata[i].os_data_ptr)
3514 unifi_error(card->ospriv, "Assign null os_data_ptr (len=%d) fh slot %d, i=%d, q=%d, sig=0x%x",
3515 bulkdata[i].data_length, slots[j], i, queue, GET_SIGNAL_ID(packed_sigptr));
3519 if (queue < UNIFI_NO_OF_TX_QS)
3521 card->dynamic_slot_data.from_host_used_slots[queue]++;
3528 return CSR_RESULT_SUCCESS;
3529 } /* CardWriteBulkData() */
3533 * ---------------------------------------------------------------------------
3534 * card_find_data_slot
3536 * Dereference references to bulk data slots into pointers to real data.
3539 * card Pointer to the card struct.
3540 * slot Slot number from a signal structure
3543 * Pointer to entry in bulk_data_slot array.
3544 * ---------------------------------------------------------------------------
3546 bulk_data_desc_t* card_find_data_slot(card_t *card, s16 slot)
3549 bulk_data_desc_t *bd;
3553 /* ?? check sanity of slot number ?? */
3555 if (slot & SLOT_DIR_TO_HOST)
3557 bd = &card->to_host_data[sn];
3561 bd = &card->from_host_data[sn].bd;
3565 } /* card_find_data_slot() */
3569 * ---------------------------------------------------------------------------
3570 * firmware_present_in_flash
3572 * Probe for external Flash that looks like it might contain firmware.
3574 * If Flash is not present, reads always return 0x0008.
3575 * If Flash is present, but empty, reads return 0xFFFF.
3576 * Anything else is considered to be firmware.
3579 * card Pointer to card struct
3582 * CSR_RESULT_SUCCESS firmware is present in ROM or flash
3583 * CSR_WIFI_HIP_RESULT_NOT_FOUND firmware is not present in ROM or flash
3584 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3585 * CSR_RESULT_FAILURE if an SDIO error occurred
3586 * ---------------------------------------------------------------------------
3588 static CsrResult firmware_present_in_flash(card_t *card)
3593 if (ChipHelper_HasRom(card->helper))
3595 return CSR_RESULT_SUCCESS;
3597 if (!ChipHelper_HasFlash(card->helper))
3599 return CSR_WIFI_HIP_RESULT_NOT_FOUND;
3603 * Examine the Flash locations that are the power-on default reset
3604 * vectors of the XAP processors.
3605 * These are words 1 and 5 in Flash.
3607 r = unifi_card_read16(card, UNIFI_MAKE_GP(EXT_FLASH, 2), &m1);
3608 if (r != CSR_RESULT_SUCCESS)
3613 r = unifi_card_read16(card, UNIFI_MAKE_GP(EXT_FLASH, 10), &m5);
3614 if (r != CSR_RESULT_SUCCESS)
3619 /* Check for uninitialised/missing flash */
3620 if ((m1 == 0x0008) || (m1 == 0xFFFF) ||
3621 (m1 == 0x0004) || (m5 == 0x0004) ||
3622 (m5 == 0x0008) || (m5 == 0xFFFF))
3624 return CSR_WIFI_HIP_RESULT_NOT_FOUND;
3627 return CSR_RESULT_SUCCESS;
3628 } /* firmware_present_in_flash() */
3632 * ---------------------------------------------------------------------------
3635 * Perform chip specific magic to "Get It Working" TM. This will
3636 * increase speed of PLLs in analogue and maybe enable some
3637 * on-chip regulators.
3640 * card Pointer to card struct
3644 * ---------------------------------------------------------------------------
3646 static void bootstrap_chip_hw(card_t *card)
3648 const struct chip_helper_init_values *vals;
3650 void *sdio = card->sdio_if;
3651 CsrResult csrResult;
3653 len = ChipHelper_ClockStartupSequence(card->helper, &vals);
3656 for (i = 0; i < len; i++)
3658 csrResult = CsrSdioWrite16(sdio, vals[i].addr * 2, vals[i].value);
3659 if (csrResult != CSR_RESULT_SUCCESS)
3661 unifi_warning(card->ospriv, "Failed to write bootstrap value %d\n", i);
3662 /* Might not be fatal */
3668 } /* bootstrap_chip_hw() */
3672 * ---------------------------------------------------------------------------
3673 * unifi_card_stop_processor
3675 * Stop the UniFi XAP processors.
3678 * card Pointer to card struct
3679 * which One of UNIFI_PROC_MAC, UNIFI_PROC_PHY, UNIFI_PROC_BOTH
3682 * CSR_RESULT_SUCCESS if successful, or CSR error code
3683 * ---------------------------------------------------------------------------
3685 CsrResult unifi_card_stop_processor(card_t *card, enum unifi_dbg_processors_select which)
3687 CsrResult r = CSR_RESULT_SUCCESS;
3693 /* Select both XAPs */
3694 r = unifi_set_proc_select(card, which);
3695 if (r != CSR_RESULT_SUCCESS)
3700 /* Stop processors */
3701 r = unifi_write_direct16(card, ChipHelper_DBG_EMU_CMD(card->helper) * 2, 2);
3702 if (r != CSR_RESULT_SUCCESS)
3708 r = unifi_read_direct_8_or_16(card,
3709 ChipHelper_DBG_HOST_STOP_STATUS(card->helper) * 2,
3711 if (r != CSR_RESULT_SUCCESS)
3716 if ((status & 1) == 1)
3719 return CSR_RESULT_SUCCESS;
3722 /* Processors didn't stop, try again */
3725 if (r != CSR_RESULT_SUCCESS)
3727 /* An SDIO error occurred */
3728 unifi_error(card->ospriv, "Failed to stop processors: SDIO error\n");
3732 /* If we reach here, we didn't the status in time. */
3733 unifi_error(card->ospriv, "Failed to stop processors: timeout waiting for stopped status\n");
3734 r = CSR_RESULT_FAILURE;
3738 } /* unifi_card_stop_processor() */
3742 * ---------------------------------------------------------------------------
3743 * card_start_processor
3745 * Start the UniFi XAP processors.
3748 * card Pointer to card struct
3749 * which One of UNIFI_PROC_MAC, UNIFI_PROC_PHY, UNIFI_PROC_BOTH
3752 * CSR_RESULT_SUCCESS or CSR error code
3753 * ---------------------------------------------------------------------------
3755 CsrResult card_start_processor(card_t *card, enum unifi_dbg_processors_select which)
3759 /* Select both XAPs */
3760 r = unifi_set_proc_select(card, which);
3761 if (r != CSR_RESULT_SUCCESS)
3763 unifi_error(card->ospriv, "unifi_set_proc_select failed: %d.\n", r);
3768 r = unifi_write_direct_8_or_16(card,
3769 ChipHelper_DBG_EMU_CMD(card->helper) * 2, 8);
3770 if (r != CSR_RESULT_SUCCESS)
3775 r = unifi_write_direct_8_or_16(card,
3776 ChipHelper_DBG_EMU_CMD(card->helper) * 2, 0);
3777 if (r != CSR_RESULT_SUCCESS)
3782 return CSR_RESULT_SUCCESS;
3783 } /* card_start_processor() */
3787 * ---------------------------------------------------------------------------
3788 * unifi_set_interrupt_mode
3790 * Configure the interrupt processing mode used by the HIP
3793 * card Pointer to card struct
3794 * mode Interrupt mode to apply
3798 * ---------------------------------------------------------------------------
3800 void unifi_set_interrupt_mode(card_t *card, u32 mode)
3802 if (mode == CSR_WIFI_INTMODE_RUN_BH_ONCE)
3804 unifi_info(card->ospriv, "Scheduled interrupt mode");
3806 card->intmode = mode;
3807 } /* unifi_set_interrupt_mode() */
3811 * ---------------------------------------------------------------------------
3812 * unifi_start_processors
3814 * Start all UniFi XAP processors.
3817 * card Pointer to card struct
3820 * CSR_RESULT_SUCCESS on success, CSR error code on error
3821 * ---------------------------------------------------------------------------
3823 CsrResult unifi_start_processors(card_t *card)
3825 return card_start_processor(card, UNIFI_PROC_BOTH);
3826 } /* unifi_start_processors() */
3830 * ---------------------------------------------------------------------------
3831 * unifi_request_max_sdio_clock
3833 * Requests that the maximum SDIO clock rate is set at the next suitable
3834 * opportunity (e.g. when the BH next runs, so as not to interfere with
3835 * any current operation).
3838 * card Pointer to card struct
3842 * ---------------------------------------------------------------------------
3844 void unifi_request_max_sdio_clock(card_t *card)
3846 card->request_max_clock = 1;
3847 } /* unifi_request_max_sdio_clock() */
3851 * ---------------------------------------------------------------------------
3852 * unifi_set_host_state
3854 * Set the host deep-sleep state.
3856 * If transitioning to TORPID, the SDIO driver will be notified
3857 * that the SD bus will be unused (idle) and conversely, when
3858 * transitioning from TORPID that the bus will be used (active).
3861 * card Pointer to card struct
3862 * state New deep-sleep state.
3865 * CSR_RESULT_SUCCESS on success
3866 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3867 * CSR_RESULT_FAILURE if an SDIO error occurred
3870 * We need to reduce the SDIO clock speed before trying to wake up the
3871 * chip. Actually, in the implementation below we reduce the clock speed
3872 * not just before we try to wake up the chip, but when we put the chip to
3873 * deep sleep. This means that if the f/w wakes up on its' own, we waste
3874 * a reduce/increace cycle. However, trying to eliminate this overhead is
3875 * proved difficult, as the current state machine in the HIP lib does at
3876 * least a CMD52 to disable the interrupts before we configure the host
3878 * ---------------------------------------------------------------------------
3880 CsrResult unifi_set_host_state(card_t *card, enum unifi_host_state state)
3882 CsrResult r = CSR_RESULT_SUCCESS;
3883 CsrResult csrResult;
3884 static const char *const states[] = {
3885 "AWAKE", "DROWSY", "TORPID"
3887 static const u8 state_csr_host_wakeup[] = {
3890 static const u8 state_io_abort[] = {
3894 unifi_trace(card->ospriv, UDBG4, "State %s to %s\n",
3895 states[card->host_state], states[state]);
3897 if (card->host_state == UNIFI_HOST_STATE_TORPID)
3899 CsrSdioFunctionActive(card->sdio_if);
3902 /* Write the new state to UniFi. */
3903 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3905 r = sdio_write_f0(card, SDIO_CSR_HOST_WAKEUP,
3906 (u8)((card->function << 4) | state_csr_host_wakeup[state]));
3910 r = sdio_write_f0(card, SDIO_IO_ABORT, state_io_abort[state]);
3913 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3917 if (r != CSR_RESULT_SUCCESS)
3919 unifi_error(card->ospriv, "Failed to write UniFi deep sleep state\n");
3924 * If the chip was in state TORPID then we can now increase
3925 * the maximum bus clock speed.
3927 if (card->host_state == UNIFI_HOST_STATE_TORPID)
3929 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if,
3930 UNIFI_SDIO_CLOCK_MAX_HZ);
3931 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
3932 /* Non-fatal error */
3933 if (r != CSR_RESULT_SUCCESS && r != CSR_WIFI_HIP_RESULT_NO_DEVICE)
3935 unifi_warning(card->ospriv,
3936 "Failed to increase the SDIO clock speed\n");
3940 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_MAX_HZ;
3945 * Cache the current state in the card structure to avoid
3946 * unnecessary SDIO reads.
3948 card->host_state = state;
3950 if (state == UNIFI_HOST_STATE_TORPID)
3953 * If the chip is now in state TORPID then we must now decrease
3954 * the maximum bus clock speed.
3956 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if,
3957 UNIFI_SDIO_CLOCK_SAFE_HZ);
3958 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
3959 if (r != CSR_RESULT_SUCCESS && r != CSR_WIFI_HIP_RESULT_NO_DEVICE)
3961 unifi_warning(card->ospriv,
3962 "Failed to decrease the SDIO clock speed\n");
3966 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
3968 CsrSdioFunctionIdle(card->sdio_if);
3973 } /* unifi_set_host_state() */
3977 * ---------------------------------------------------------------------------
3980 * Update the card information data structure
3983 * card Pointer to card struct
3984 * card_info Pointer to info structure to update
3988 * ---------------------------------------------------------------------------
3990 void unifi_card_info(card_t *card, card_info_t *card_info)
3992 card_info->chip_id = card->chip_id;
3993 card_info->chip_version = card->chip_version;
3994 card_info->fw_build = card->build_id;
3995 card_info->fw_hip_version = card->config_data.version;
3996 card_info->sdio_block_size = card->sdio_io_block_size;
3997 } /* unifi_card_info() */
4001 * ---------------------------------------------------------------------------
4002 * unifi_check_io_status
4004 * Check UniFi for spontaneous reset and pending interrupt.
4007 * card Pointer to card struct
4008 * status Pointer to location to write chip status:
4009 * 0 if UniFi is running, and no interrupt pending
4010 * 1 if UniFi has spontaneously reset
4011 * 2 if there is a pending interrupt
4013 * CSR_RESULT_SUCCESS if OK, or CSR error
4014 * ---------------------------------------------------------------------------
4016 CsrResult unifi_check_io_status(card_t *card, s32 *status)
4024 r = sdio_read_f0(card, SDIO_IO_ENABLE, &io_en);
4025 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
4029 if (r != CSR_RESULT_SUCCESS)
4031 unifi_error(card->ospriv, "Failed to read SDIO_IO_ENABLE to check for spontaneous reset\n");
4035 if ((io_en & (1 << card->function)) == 0)
4039 unifi_error(card->ospriv, "UniFi has spontaneously reset.\n");
4042 * These reads are very likely to fail. We want to know if the function is really
4043 * disabled or the SDIO driver just returns rubbish.
4045 fw_count = unifi_read_shared_count(card, card->sdio_ctrl_addr + 4);
4048 unifi_error(card->ospriv, "Failed to read to-host sig written count\n");
4052 unifi_error(card->ospriv, "thsw: %u (driver thinks is %u)\n",
4053 fw_count, card->to_host_signals_w);
4055 fw_count = unifi_read_shared_count(card, card->sdio_ctrl_addr + 2);
4058 unifi_error(card->ospriv, "Failed to read from-host sig read count\n");
4062 unifi_error(card->ospriv, "fhsr: %u (driver thinks is %u)\n",
4063 fw_count, card->from_host_signals_r);
4069 unifi_info(card->ospriv, "UniFi function %d is enabled.\n", card->function);
4071 /* See if we missed an SDIO interrupt */
4072 r = CardPendingInt(card, &pending);
4075 unifi_error(card->ospriv, "There is an unhandled pending interrupt.\n");
4081 } /* unifi_check_io_status() */
4084 void unifi_get_hip_qos_info(card_t *card, unifi_HipQosInfo *hipqosinfo)
4093 memset(hipqosinfo, 0, sizeof(unifi_HipQosInfo));
4095 nslots = card->config_data.num_fromhost_data_slots;
4097 for (i = 0; i < nslots; i++)
4099 if (card->from_host_data[i].bd.data_length == 0)
4101 hipqosinfo->free_fh_bulkdata_slots++;
4105 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
4107 sigq = &card->fh_traffic_queue[i];
4108 t = sigq->q_wr_ptr - sigq->q_rd_ptr;
4111 t += sigq->q_length;
4113 hipqosinfo->free_fh_sig_queue_slots[i] = (sigq->q_length - t) - 1;
4116 count_fhr = unifi_read_shared_count(card, card->sdio_ctrl_addr + 2);
4119 unifi_error(card->ospriv, "Failed to read from-host sig read count - %d\n", count_fhr);
4120 hipqosinfo->free_fh_fw_slots = 0xfa;
4124 occupied_fh = (card->from_host_signals_w - count_fhr) % 128;
4126 hipqosinfo->free_fh_fw_slots = (u16)(card->config_data.num_fromhost_sig_frags - occupied_fh);
4131 CsrResult ConvertCsrSdioToCsrHipResult(card_t *card, CsrResult csrResult)
4133 CsrResult r = CSR_RESULT_FAILURE;
4137 case CSR_RESULT_SUCCESS:
4138 r = CSR_RESULT_SUCCESS;
4140 /* Timeout errors */
4141 case CSR_SDIO_RESULT_TIMEOUT:
4142 /* Integrity errors */
4143 case CSR_SDIO_RESULT_CRC_ERROR:
4144 r = CSR_RESULT_FAILURE;
4146 case CSR_SDIO_RESULT_NO_DEVICE:
4147 r = CSR_WIFI_HIP_RESULT_NO_DEVICE;
4149 case CSR_SDIO_RESULT_INVALID_VALUE:
4150 r = CSR_WIFI_HIP_RESULT_INVALID_VALUE;
4152 case CSR_RESULT_FAILURE:
4153 r = CSR_RESULT_FAILURE;
4156 unifi_warning(card->ospriv, "Unrecognised csrResult error code: %d\n", csrResult);
4161 } /* ConvertCsrSdioToCsrHipResult() */