Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Marvell Wireless LAN device driver: PCIE specific handling |
| 3 | * |
| 4 | * Copyright (C) 2011, Marvell International Ltd. |
| 5 | * |
| 6 | * This software file (the "File") is distributed by Marvell International |
| 7 | * Ltd. under the terms of the GNU General Public License Version 2, June 1991 |
| 8 | * (the "License"). You may use, redistribute and/or modify this File in |
| 9 | * accordance with the terms and conditions of the License, a copy of which |
| 10 | * is available by writing to the Free Software Foundation, Inc., |
| 11 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the |
| 12 | * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
| 13 | * |
| 14 | * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE |
| 16 | * ARE EXPRESSLY DISCLAIMED. The License provides additional details about |
| 17 | * this warranty disclaimer. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/firmware.h> |
| 21 | |
| 22 | #include "decl.h" |
| 23 | #include "ioctl.h" |
| 24 | #include "util.h" |
| 25 | #include "fw.h" |
| 26 | #include "main.h" |
| 27 | #include "wmm.h" |
| 28 | #include "11n.h" |
| 29 | #include "pcie.h" |
| 30 | |
| 31 | #define PCIE_VERSION "1.0" |
| 32 | #define DRV_NAME "Marvell mwifiex PCIe" |
| 33 | |
| 34 | static u8 user_rmmod; |
| 35 | |
| 36 | static struct mwifiex_if_ops pcie_ops; |
| 37 | |
| 38 | static struct semaphore add_remove_card_sem; |
| 39 | static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter); |
| 40 | static int mwifiex_pcie_resume(struct pci_dev *pdev); |
| 41 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 42 | static int |
| 43 | mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb, |
| 44 | int size, int flags) |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 45 | { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 46 | struct pcie_service_card *card = adapter->card; |
| 47 | dma_addr_t buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 48 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 49 | buf_pa = pci_map_single(card->dev, skb->data, size, flags); |
| 50 | if (pci_dma_mapping_error(card->dev, buf_pa)) { |
| 51 | dev_err(adapter->dev, "failed to map pci memory!\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | memcpy(skb->cb, &buf_pa, sizeof(dma_addr_t)); |
| 55 | return 0; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | * This function reads sleep cookie and checks if FW is ready |
| 60 | */ |
| 61 | static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter) |
| 62 | { |
| 63 | u32 *cookie_addr; |
| 64 | struct pcie_service_card *card = adapter->card; |
| 65 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 66 | if (card->sleep_cookie_vbase) { |
| 67 | cookie_addr = (u32 *)card->sleep_cookie_vbase; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 68 | dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n", |
| 69 | *cookie_addr); |
| 70 | if (*cookie_addr == FW_AWAKE_COOKIE) |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * This function probes an mwifiex device and registers it. It allocates |
| 79 | * the card structure, enables PCIE function number and initiates the |
| 80 | * device registration and initialization procedure by adding a logical |
| 81 | * interface. |
| 82 | */ |
| 83 | static int mwifiex_pcie_probe(struct pci_dev *pdev, |
| 84 | const struct pci_device_id *ent) |
| 85 | { |
| 86 | struct pcie_service_card *card; |
| 87 | |
| 88 | pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 89 | pdev->vendor, pdev->device, pdev->revision); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 90 | |
| 91 | card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL); |
Joe Perches | e404dec | 2012-01-29 12:56:23 +0000 | [diff] [blame] | 92 | if (!card) |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 93 | return -ENOMEM; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 94 | |
| 95 | card->dev = pdev; |
| 96 | |
| 97 | if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops, |
| 98 | MWIFIEX_PCIE)) { |
| 99 | pr_err("%s failed\n", __func__); |
| 100 | kfree(card); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * This function removes the interface and frees up the card structure. |
| 109 | */ |
| 110 | static void mwifiex_pcie_remove(struct pci_dev *pdev) |
| 111 | { |
| 112 | struct pcie_service_card *card; |
| 113 | struct mwifiex_adapter *adapter; |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 114 | struct mwifiex_private *priv; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 115 | int i; |
| 116 | |
| 117 | card = pci_get_drvdata(pdev); |
| 118 | if (!card) |
| 119 | return; |
| 120 | |
| 121 | adapter = card->adapter; |
| 122 | if (!adapter || !adapter->priv_num) |
| 123 | return; |
| 124 | |
Amitkumar Karwar | 59a4cc2 | 2012-04-09 20:06:57 -0700 | [diff] [blame] | 125 | /* In case driver is removed when asynchronous FW load is in progress */ |
| 126 | wait_for_completion(&adapter->fw_load); |
| 127 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 128 | if (user_rmmod) { |
| 129 | #ifdef CONFIG_PM |
| 130 | if (adapter->is_suspended) |
| 131 | mwifiex_pcie_resume(pdev); |
| 132 | #endif |
| 133 | |
| 134 | for (i = 0; i < adapter->priv_num; i++) |
| 135 | if ((GET_BSS_ROLE(adapter->priv[i]) == |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 136 | MWIFIEX_BSS_ROLE_STA) && |
| 137 | adapter->priv[i]->media_connected) |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 138 | mwifiex_deauthenticate(adapter->priv[i], NULL); |
| 139 | |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 140 | priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 141 | |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 142 | mwifiex_disable_auto_ds(priv); |
| 143 | |
| 144 | mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | mwifiex_remove_card(card->adapter, &add_remove_card_sem); |
| 148 | kfree(card); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Kernel needs to suspend all functions separately. Therefore all |
| 153 | * registered functions must have drivers with suspend and resume |
| 154 | * methods. Failing that the kernel simply removes the whole card. |
| 155 | * |
| 156 | * If already not suspended, this function allocates and sends a host |
| 157 | * sleep activate request to the firmware and turns off the traffic. |
| 158 | */ |
| 159 | static int mwifiex_pcie_suspend(struct pci_dev *pdev, pm_message_t state) |
| 160 | { |
| 161 | struct mwifiex_adapter *adapter; |
| 162 | struct pcie_service_card *card; |
| 163 | int hs_actived, i; |
| 164 | |
| 165 | if (pdev) { |
| 166 | card = (struct pcie_service_card *) pci_get_drvdata(pdev); |
| 167 | if (!card || card->adapter) { |
| 168 | pr_err("Card or adapter structure is not valid\n"); |
| 169 | return 0; |
| 170 | } |
| 171 | } else { |
| 172 | pr_err("PCIE device is not specified\n"); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | adapter = card->adapter; |
| 177 | |
| 178 | hs_actived = mwifiex_enable_hs(adapter); |
| 179 | |
| 180 | /* Indicate device suspended */ |
| 181 | adapter->is_suspended = true; |
| 182 | |
| 183 | for (i = 0; i < adapter->priv_num; i++) |
| 184 | netif_carrier_off(adapter->priv[i]->netdev); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Kernel needs to suspend all functions separately. Therefore all |
| 191 | * registered functions must have drivers with suspend and resume |
| 192 | * methods. Failing that the kernel simply removes the whole card. |
| 193 | * |
| 194 | * If already not resumed, this function turns on the traffic and |
| 195 | * sends a host sleep cancel request to the firmware. |
| 196 | */ |
| 197 | static int mwifiex_pcie_resume(struct pci_dev *pdev) |
| 198 | { |
| 199 | struct mwifiex_adapter *adapter; |
| 200 | struct pcie_service_card *card; |
| 201 | int i; |
| 202 | |
| 203 | if (pdev) { |
| 204 | card = (struct pcie_service_card *) pci_get_drvdata(pdev); |
| 205 | if (!card || !card->adapter) { |
| 206 | pr_err("Card or adapter structure is not valid\n"); |
| 207 | return 0; |
| 208 | } |
| 209 | } else { |
| 210 | pr_err("PCIE device is not specified\n"); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | adapter = card->adapter; |
| 215 | |
| 216 | if (!adapter->is_suspended) { |
| 217 | dev_warn(adapter->dev, "Device already resumed\n"); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | adapter->is_suspended = false; |
| 222 | |
| 223 | for (i = 0; i < adapter->priv_num; i++) |
| 224 | if (adapter->priv[i]->media_connected) |
| 225 | netif_carrier_on(adapter->priv[i]->netdev); |
| 226 | |
| 227 | mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA), |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 228 | MWIFIEX_ASYNC_CMD); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | #define PCIE_VENDOR_ID_MARVELL (0x11ab) |
| 234 | #define PCIE_DEVICE_ID_MARVELL_88W8766P (0x2b30) |
| 235 | |
| 236 | static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = { |
| 237 | { |
| 238 | PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P, |
| 239 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, |
| 240 | }, |
| 241 | {}, |
| 242 | }; |
| 243 | |
| 244 | MODULE_DEVICE_TABLE(pci, mwifiex_ids); |
| 245 | |
| 246 | /* PCI Device Driver */ |
| 247 | static struct pci_driver __refdata mwifiex_pcie = { |
| 248 | .name = "mwifiex_pcie", |
| 249 | .id_table = mwifiex_ids, |
| 250 | .probe = mwifiex_pcie_probe, |
| 251 | .remove = mwifiex_pcie_remove, |
| 252 | #ifdef CONFIG_PM |
| 253 | /* Power Management Hooks */ |
| 254 | .suspend = mwifiex_pcie_suspend, |
| 255 | .resume = mwifiex_pcie_resume, |
| 256 | #endif |
| 257 | }; |
| 258 | |
| 259 | /* |
| 260 | * This function writes data into PCIE card register. |
| 261 | */ |
| 262 | static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data) |
| 263 | { |
| 264 | struct pcie_service_card *card = adapter->card; |
| 265 | |
| 266 | iowrite32(data, card->pci_mmap1 + reg); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * This function reads data from PCIE card register. |
| 273 | */ |
| 274 | static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data) |
| 275 | { |
| 276 | struct pcie_service_card *card = adapter->card; |
| 277 | |
| 278 | *data = ioread32(card->pci_mmap1 + reg); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * This function wakes up the card. |
| 285 | * |
| 286 | * A host power up command is written to the card configuration |
| 287 | * register to wake up the card. |
| 288 | */ |
| 289 | static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter) |
| 290 | { |
| 291 | int i = 0; |
| 292 | |
| 293 | while (mwifiex_pcie_ok_to_access_hw(adapter)) { |
| 294 | i++; |
Yogesh Ashok Powar | e7891ba | 2012-03-12 19:35:11 -0700 | [diff] [blame] | 295 | usleep_range(10, 20); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 296 | /* 50ms max wait */ |
| 297 | if (i == 50000) |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | dev_dbg(adapter->dev, "event: Wakeup device...\n"); |
| 302 | |
| 303 | /* Enable interrupts or any chip access will wakeup device */ |
| 304 | if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK, HOST_INTR_MASK)) { |
| 305 | dev_warn(adapter->dev, "Enable host interrupt failed\n"); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n"); |
| 310 | adapter->ps_state = PS_STATE_AWAKE; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * This function is called after the card has woken up. |
| 317 | * |
| 318 | * The card configuration register is reset. |
| 319 | */ |
| 320 | static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter) |
| 321 | { |
| 322 | dev_dbg(adapter->dev, "cmd: Wakeup device completed\n"); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * This function disables the host interrupt. |
| 329 | * |
| 330 | * The host interrupt mask is read, the disable bit is reset and |
| 331 | * written back to the card host interrupt mask register. |
| 332 | */ |
| 333 | static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter) |
| 334 | { |
| 335 | if (mwifiex_pcie_ok_to_access_hw(adapter)) { |
| 336 | if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK, |
| 337 | 0x00000000)) { |
| 338 | dev_warn(adapter->dev, "Disable host interrupt failed\n"); |
| 339 | return -1; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * This function enables the host interrupt. |
| 348 | * |
| 349 | * The host interrupt enable mask is written to the card |
| 350 | * host interrupt mask register. |
| 351 | */ |
| 352 | static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter) |
| 353 | { |
| 354 | if (mwifiex_pcie_ok_to_access_hw(adapter)) { |
| 355 | /* Simply write the mask to the register */ |
| 356 | if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK, |
| 357 | HOST_INTR_MASK)) { |
| 358 | dev_warn(adapter->dev, "Enable host interrupt failed\n"); |
| 359 | return -1; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * This function creates buffer descriptor ring for TX |
| 368 | */ |
| 369 | static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter) |
| 370 | { |
| 371 | struct pcie_service_card *card = adapter->card; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 372 | int i; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * driver maintaines the write pointer and firmware maintaines the read |
| 376 | * pointer. The write pointer starts at 0 (zero) while the read pointer |
| 377 | * starts at zero with rollover bit set |
| 378 | */ |
| 379 | card->txbd_wrptr = 0; |
| 380 | card->txbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND; |
| 381 | |
| 382 | /* allocate shared memory for the BD ring and divide the same in to |
| 383 | several descriptors */ |
| 384 | card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) * |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 385 | MWIFIEX_MAX_TXRX_BD; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 386 | dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 387 | card->txbd_ring_size); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 388 | card->txbd_ring_vbase = pci_alloc_consistent(card->dev, |
| 389 | card->txbd_ring_size, |
| 390 | &card->txbd_ring_pbase); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 391 | if (!card->txbd_ring_vbase) { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 392 | dev_err(adapter->dev, |
| 393 | "allocate consistent memory (%d bytes) failed!\n", |
| 394 | card->txbd_ring_size); |
Amitkumar Karwar | 8c53e42 | 2011-12-08 20:41:03 -0800 | [diff] [blame] | 395 | return -ENOMEM; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 396 | } |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 397 | dev_dbg(adapter->dev, |
| 398 | "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n", |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 399 | card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase, |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 400 | (u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 401 | |
| 402 | for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) { |
| 403 | card->txbd_ring[i] = (struct mwifiex_pcie_buf_desc *) |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 404 | (card->txbd_ring_vbase + |
| 405 | (sizeof(struct mwifiex_pcie_buf_desc) |
| 406 | * i)); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 407 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 408 | card->tx_buf_list[i] = NULL; |
| 409 | card->txbd_ring[i]->paddr = 0; |
| 410 | card->txbd_ring[i]->len = 0; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 411 | card->txbd_ring[i]->flags = 0; |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter) |
| 418 | { |
| 419 | struct pcie_service_card *card = adapter->card; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 420 | struct sk_buff *skb; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 421 | int i; |
| 422 | |
| 423 | for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 424 | if (card->tx_buf_list[i]) { |
| 425 | skb = card->tx_buf_list[i]; |
| 426 | pci_unmap_single(card->dev, card->txbd_ring[i]->paddr, |
| 427 | skb->len, PCI_DMA_TODEVICE); |
| 428 | dev_kfree_skb_any(skb); |
| 429 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 430 | card->tx_buf_list[i] = NULL; |
| 431 | card->txbd_ring[i]->paddr = 0; |
| 432 | card->txbd_ring[i]->len = 0; |
| 433 | card->txbd_ring[i]->flags = 0; |
| 434 | card->txbd_ring[i] = NULL; |
| 435 | } |
| 436 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 437 | if (card->txbd_ring_vbase) |
| 438 | pci_free_consistent(card->dev, card->txbd_ring_size, |
| 439 | card->txbd_ring_vbase, |
| 440 | card->txbd_ring_pbase); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 441 | card->txbd_ring_size = 0; |
| 442 | card->txbd_wrptr = 0; |
| 443 | card->txbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND; |
| 444 | card->txbd_ring_vbase = NULL; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 445 | card->txbd_ring_pbase = 0; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * This function creates buffer descriptor ring for RX |
| 452 | */ |
| 453 | static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter) |
| 454 | { |
| 455 | struct pcie_service_card *card = adapter->card; |
| 456 | struct sk_buff *skb; |
| 457 | int i; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 458 | dma_addr_t buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 459 | |
| 460 | /* |
| 461 | * driver maintaines the read pointer and firmware maintaines the write |
| 462 | * pointer. The write pointer starts at 0 (zero) while the read pointer |
| 463 | * starts at zero with rollover bit set |
| 464 | */ |
| 465 | card->rxbd_wrptr = 0; |
| 466 | card->rxbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND; |
| 467 | |
| 468 | card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) * |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 469 | MWIFIEX_MAX_TXRX_BD; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 470 | dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 471 | card->rxbd_ring_size); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 472 | card->rxbd_ring_vbase = pci_alloc_consistent(card->dev, |
| 473 | card->rxbd_ring_size, |
| 474 | &card->rxbd_ring_pbase); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 475 | if (!card->rxbd_ring_vbase) { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 476 | dev_err(adapter->dev, |
| 477 | "allocate consistent memory (%d bytes) failed!\n", |
| 478 | card->rxbd_ring_size); |
Amitkumar Karwar | 8c53e42 | 2011-12-08 20:41:03 -0800 | [diff] [blame] | 479 | return -ENOMEM; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 480 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 481 | |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 482 | dev_dbg(adapter->dev, |
| 483 | "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n", |
| 484 | card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase, |
| 485 | (u32)((u64)card->rxbd_ring_pbase >> 32), |
| 486 | card->rxbd_ring_size); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 487 | |
| 488 | for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) { |
| 489 | card->rxbd_ring[i] = (struct mwifiex_pcie_buf_desc *) |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 490 | (card->rxbd_ring_vbase + |
| 491 | (sizeof(struct mwifiex_pcie_buf_desc) |
| 492 | * i)); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 493 | |
| 494 | /* Allocate skb here so that firmware can DMA data from it */ |
| 495 | skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE); |
| 496 | if (!skb) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 497 | dev_err(adapter->dev, |
| 498 | "Unable to allocate skb for RX ring.\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 499 | kfree(card->rxbd_ring_vbase); |
| 500 | return -ENOMEM; |
| 501 | } |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 502 | if (mwifiex_map_pci_memory(adapter, skb, |
| 503 | MWIFIEX_RX_DATA_BUF_SIZE, |
| 504 | PCI_DMA_FROMDEVICE)) |
| 505 | return -1; |
| 506 | |
| 507 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 508 | |
| 509 | dev_dbg(adapter->dev, "info: RX ring: add new skb base: %p, " |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 510 | "buf_base: %p, buf_pbase: %#x:%x, buf_len: %#x\n", |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 511 | skb, skb->data, (u32)buf_pa, (u32)((u64)buf_pa >> 32), |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 512 | skb->len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 513 | |
| 514 | card->rx_buf_list[i] = skb; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 515 | card->rxbd_ring[i]->paddr = buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 516 | card->rxbd_ring[i]->len = (u16)skb->len; |
| 517 | card->rxbd_ring[i]->flags = 0; |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * This function deletes Buffer descriptor ring for RX |
| 525 | */ |
| 526 | static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter) |
| 527 | { |
| 528 | struct pcie_service_card *card = adapter->card; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 529 | struct sk_buff *skb; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 530 | int i; |
| 531 | |
| 532 | for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 533 | if (card->rx_buf_list[i]) { |
| 534 | skb = card->rx_buf_list[i]; |
| 535 | pci_unmap_single(card->dev, card->rxbd_ring[i]->paddr , |
| 536 | MWIFIEX_RX_DATA_BUF_SIZE, |
| 537 | PCI_DMA_FROMDEVICE); |
| 538 | dev_kfree_skb_any(skb); |
| 539 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 540 | card->rx_buf_list[i] = NULL; |
| 541 | card->rxbd_ring[i]->paddr = 0; |
| 542 | card->rxbd_ring[i]->len = 0; |
| 543 | card->rxbd_ring[i]->flags = 0; |
| 544 | card->rxbd_ring[i] = NULL; |
| 545 | } |
| 546 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 547 | if (card->rxbd_ring_vbase) |
| 548 | pci_free_consistent(card->dev, card->rxbd_ring_size, |
| 549 | card->rxbd_ring_vbase, |
| 550 | card->rxbd_ring_pbase); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 551 | card->rxbd_ring_size = 0; |
| 552 | card->rxbd_wrptr = 0; |
| 553 | card->rxbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND; |
| 554 | card->rxbd_ring_vbase = NULL; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 555 | card->rxbd_ring_pbase = 0; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * This function creates buffer descriptor ring for Events |
| 562 | */ |
| 563 | static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter) |
| 564 | { |
| 565 | struct pcie_service_card *card = adapter->card; |
| 566 | struct sk_buff *skb; |
| 567 | int i; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 568 | dma_addr_t buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 569 | |
| 570 | /* |
| 571 | * driver maintaines the read pointer and firmware maintaines the write |
| 572 | * pointer. The write pointer starts at 0 (zero) while the read pointer |
| 573 | * starts at zero with rollover bit set |
| 574 | */ |
| 575 | card->evtbd_wrptr = 0; |
| 576 | card->evtbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND; |
| 577 | |
| 578 | card->evtbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) * |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 579 | MWIFIEX_MAX_EVT_BD; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 580 | dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 581 | card->evtbd_ring_size); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 582 | card->evtbd_ring_vbase = pci_alloc_consistent(card->dev, |
| 583 | card->evtbd_ring_size, |
| 584 | &card->evtbd_ring_pbase); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 585 | if (!card->evtbd_ring_vbase) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 586 | dev_err(adapter->dev, |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 587 | "allocate consistent memory (%d bytes) failed!\n", |
| 588 | card->evtbd_ring_size); |
Amitkumar Karwar | 8c53e42 | 2011-12-08 20:41:03 -0800 | [diff] [blame] | 589 | return -ENOMEM; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 590 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 591 | |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 592 | dev_dbg(adapter->dev, |
| 593 | "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n", |
| 594 | card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase, |
| 595 | (u32)((u64)card->evtbd_ring_pbase >> 32), |
| 596 | card->evtbd_ring_size); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 597 | |
| 598 | for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) { |
| 599 | card->evtbd_ring[i] = (struct mwifiex_pcie_buf_desc *) |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 600 | (card->evtbd_ring_vbase + |
| 601 | (sizeof(struct mwifiex_pcie_buf_desc) |
| 602 | * i)); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 603 | |
| 604 | /* Allocate skb here so that firmware can DMA data from it */ |
| 605 | skb = dev_alloc_skb(MAX_EVENT_SIZE); |
| 606 | if (!skb) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 607 | dev_err(adapter->dev, |
| 608 | "Unable to allocate skb for EVENT buf.\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 609 | kfree(card->evtbd_ring_vbase); |
| 610 | return -ENOMEM; |
| 611 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 612 | skb_put(skb, MAX_EVENT_SIZE); |
| 613 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 614 | if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE, |
| 615 | PCI_DMA_FROMDEVICE)) |
| 616 | return -1; |
| 617 | |
| 618 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 619 | dev_dbg(adapter->dev, "info: Evt ring: add new skb. base: %p, " |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 620 | "buf_base: %p, buf_pbase: %#x:%x, buf_len: %#x\n", |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 621 | skb, skb->data, (u32)buf_pa, (u32)((u64)buf_pa >> 32), |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 622 | skb->len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 623 | |
| 624 | card->evt_buf_list[i] = skb; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 625 | card->evtbd_ring[i]->paddr = buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 626 | card->evtbd_ring[i]->len = (u16)skb->len; |
| 627 | card->evtbd_ring[i]->flags = 0; |
| 628 | } |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * This function deletes Buffer descriptor ring for Events |
| 635 | */ |
| 636 | static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter) |
| 637 | { |
| 638 | struct pcie_service_card *card = adapter->card; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 639 | struct sk_buff *skb; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 640 | int i; |
| 641 | |
| 642 | for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 643 | if (card->evt_buf_list[i]) { |
| 644 | skb = card->evt_buf_list[i]; |
| 645 | pci_unmap_single(card->dev, card->evtbd_ring[i]->paddr, |
| 646 | MAX_EVENT_SIZE, PCI_DMA_FROMDEVICE); |
| 647 | dev_kfree_skb_any(skb); |
| 648 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 649 | card->evt_buf_list[i] = NULL; |
| 650 | card->evtbd_ring[i]->paddr = 0; |
| 651 | card->evtbd_ring[i]->len = 0; |
| 652 | card->evtbd_ring[i]->flags = 0; |
| 653 | card->evtbd_ring[i] = NULL; |
| 654 | } |
| 655 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 656 | if (card->evtbd_ring_vbase) |
| 657 | pci_free_consistent(card->dev, card->evtbd_ring_size, |
| 658 | card->evtbd_ring_vbase, |
| 659 | card->evtbd_ring_pbase); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 660 | card->evtbd_wrptr = 0; |
| 661 | card->evtbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND; |
| 662 | card->evtbd_ring_size = 0; |
| 663 | card->evtbd_ring_vbase = NULL; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 664 | card->evtbd_ring_pbase = 0; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * This function allocates a buffer for CMDRSP |
| 671 | */ |
| 672 | static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter) |
| 673 | { |
| 674 | struct pcie_service_card *card = adapter->card; |
| 675 | struct sk_buff *skb; |
| 676 | |
| 677 | /* Allocate memory for receiving command response data */ |
| 678 | skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE); |
| 679 | if (!skb) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 680 | dev_err(adapter->dev, |
| 681 | "Unable to allocate skb for command response data.\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 682 | return -ENOMEM; |
| 683 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 684 | skb_put(skb, MWIFIEX_UPLD_SIZE); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 685 | if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE, |
| 686 | PCI_DMA_FROMDEVICE)) |
| 687 | return -1; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 688 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 689 | card->cmdrsp_buf = skb; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * This function deletes a buffer for CMDRSP |
| 696 | */ |
| 697 | static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter) |
| 698 | { |
| 699 | struct pcie_service_card *card; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 700 | dma_addr_t buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 701 | |
| 702 | if (!adapter) |
| 703 | return 0; |
| 704 | |
| 705 | card = adapter->card; |
| 706 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 707 | if (card && card->cmdrsp_buf) { |
| 708 | MWIFIEX_SKB_PACB(card->cmdrsp_buf, &buf_pa); |
| 709 | pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE, |
| 710 | PCI_DMA_FROMDEVICE); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 711 | dev_kfree_skb_any(card->cmdrsp_buf); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 712 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 713 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 714 | if (card && card->cmd_buf) { |
| 715 | MWIFIEX_SKB_PACB(card->cmd_buf, &buf_pa); |
| 716 | pci_unmap_single(card->dev, buf_pa, MWIFIEX_SIZE_OF_CMD_BUFFER, |
| 717 | PCI_DMA_TODEVICE); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 718 | dev_kfree_skb_any(card->cmd_buf); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 719 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * This function allocates a buffer for sleep cookie |
| 725 | */ |
| 726 | static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter) |
| 727 | { |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 728 | struct pcie_service_card *card = adapter->card; |
| 729 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 730 | card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32), |
| 731 | &card->sleep_cookie_pbase); |
| 732 | if (!card->sleep_cookie_vbase) { |
| 733 | dev_err(adapter->dev, "pci_alloc_consistent failed!\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 734 | return -ENOMEM; |
| 735 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 736 | /* Init val of Sleep Cookie */ |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 737 | *(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 738 | |
| 739 | dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n", |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 740 | *((u32 *)card->sleep_cookie_vbase)); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * This function deletes buffer for sleep cookie |
| 747 | */ |
| 748 | static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter) |
| 749 | { |
| 750 | struct pcie_service_card *card; |
| 751 | |
| 752 | if (!adapter) |
| 753 | return 0; |
| 754 | |
| 755 | card = adapter->card; |
| 756 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 757 | if (card && card->sleep_cookie_vbase) { |
| 758 | pci_free_consistent(card->dev, sizeof(u32), |
| 759 | card->sleep_cookie_vbase, |
| 760 | card->sleep_cookie_pbase); |
| 761 | card->sleep_cookie_vbase = NULL; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Avinash Patil | fbd7e7a | 2013-01-03 21:21:31 -0800 | [diff] [blame^] | 767 | /* This function flushes the TX buffer descriptor ring |
| 768 | * This function defined as handler is also called while cleaning TXRX |
| 769 | * during disconnect/ bss stop. |
| 770 | */ |
| 771 | static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter) |
| 772 | { |
| 773 | struct pcie_service_card *card = adapter->card; |
| 774 | u32 rdptr; |
| 775 | |
| 776 | /* Read the TX ring read pointer set by firmware */ |
| 777 | if (mwifiex_read_reg(adapter, REG_TXBD_RDPTR, &rdptr)) { |
| 778 | dev_err(adapter->dev, |
| 779 | "Flush TXBD: failed to read REG_TXBD_RDPTR\n"); |
| 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | if (!mwifiex_pcie_txbd_empty(card, rdptr)) { |
| 784 | card->txbd_flush = 1; |
| 785 | /* write pointer already set at last send |
| 786 | * send dnld-rdy intr again, wait for completion. |
| 787 | */ |
| 788 | if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT, |
| 789 | CPU_INTR_DNLD_RDY)) { |
| 790 | dev_err(adapter->dev, |
| 791 | "failed to assert dnld-rdy interrupt.\n"); |
| 792 | return -1; |
| 793 | } |
| 794 | } |
| 795 | return 0; |
| 796 | } |
| 797 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 798 | /* |
| 799 | * This function sends data buffer to device |
| 800 | */ |
| 801 | static int |
| 802 | mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb) |
| 803 | { |
| 804 | struct pcie_service_card *card = adapter->card; |
| 805 | u32 wrindx, rdptr; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 806 | phys_addr_t buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 807 | __le16 *tmp; |
| 808 | |
| 809 | if (!mwifiex_pcie_ok_to_access_hw(adapter)) |
| 810 | mwifiex_pm_wakeup_card(adapter); |
| 811 | |
| 812 | /* Read the TX ring read pointer set by firmware */ |
| 813 | if (mwifiex_read_reg(adapter, REG_TXBD_RDPTR, &rdptr)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 814 | dev_err(adapter->dev, |
| 815 | "SEND DATA: failed to read REG_TXBD_RDPTR\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 816 | return -1; |
| 817 | } |
| 818 | |
| 819 | wrindx = card->txbd_wrptr & MWIFIEX_TXBD_MASK; |
| 820 | |
| 821 | dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n", rdptr, |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 822 | card->txbd_wrptr); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 823 | if (((card->txbd_wrptr & MWIFIEX_TXBD_MASK) != |
| 824 | (rdptr & MWIFIEX_TXBD_MASK)) || |
| 825 | ((card->txbd_wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) != |
| 826 | (rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) { |
| 827 | struct sk_buff *skb_data; |
| 828 | u8 *payload; |
| 829 | |
| 830 | adapter->data_sent = true; |
| 831 | skb_data = card->tx_buf_list[wrindx]; |
| 832 | memcpy(skb_data->data, skb->data, skb->len); |
| 833 | payload = skb_data->data; |
| 834 | tmp = (__le16 *)&payload[0]; |
| 835 | *tmp = cpu_to_le16((u16)skb->len); |
| 836 | tmp = (__le16 *)&payload[2]; |
| 837 | *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA); |
| 838 | skb_put(skb_data, MWIFIEX_RX_DATA_BUF_SIZE - skb_data->len); |
| 839 | skb_trim(skb_data, skb->len); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 840 | MWIFIEX_SKB_PACB(skb_data, &buf_pa); |
| 841 | card->txbd_ring[wrindx]->paddr = buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 842 | card->txbd_ring[wrindx]->len = (u16)skb_data->len; |
| 843 | card->txbd_ring[wrindx]->flags = MWIFIEX_BD_FLAG_FIRST_DESC | |
| 844 | MWIFIEX_BD_FLAG_LAST_DESC; |
| 845 | |
| 846 | if ((++card->txbd_wrptr & MWIFIEX_TXBD_MASK) == |
| 847 | MWIFIEX_MAX_TXRX_BD) |
| 848 | card->txbd_wrptr = ((card->txbd_wrptr & |
| 849 | MWIFIEX_BD_FLAG_ROLLOVER_IND) ^ |
| 850 | MWIFIEX_BD_FLAG_ROLLOVER_IND); |
| 851 | |
| 852 | /* Write the TX ring write pointer in to REG_TXBD_WRPTR */ |
| 853 | if (mwifiex_write_reg(adapter, REG_TXBD_WRPTR, |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 854 | card->txbd_wrptr)) { |
| 855 | dev_err(adapter->dev, |
| 856 | "SEND DATA: failed to write REG_TXBD_WRPTR\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | /* Send the TX ready interrupt */ |
| 861 | if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT, |
| 862 | CPU_INTR_DNLD_RDY)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 863 | dev_err(adapter->dev, |
| 864 | "SEND DATA: failed to assert door-bell intr\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 865 | return -1; |
| 866 | } |
| 867 | dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: " |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 868 | "%#x> and sent packet to firmware successfully\n", |
| 869 | rdptr, card->txbd_wrptr); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 870 | } else { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 871 | dev_dbg(adapter->dev, |
| 872 | "info: TX Ring full, can't send packets to fw\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 873 | adapter->data_sent = true; |
| 874 | /* Send the TX ready interrupt */ |
| 875 | if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT, |
| 876 | CPU_INTR_DNLD_RDY)) |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 877 | dev_err(adapter->dev, |
| 878 | "SEND DATA: failed to assert door-bell intr\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 879 | return -EBUSY; |
| 880 | } |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * This function handles received buffer ring and |
| 887 | * dispatches packets to upper |
| 888 | */ |
| 889 | static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter) |
| 890 | { |
| 891 | struct pcie_service_card *card = adapter->card; |
| 892 | u32 wrptr, rd_index; |
| 893 | int ret = 0; |
| 894 | struct sk_buff *skb_tmp = NULL; |
| 895 | |
| 896 | /* Read the RX ring Write pointer set by firmware */ |
| 897 | if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 898 | dev_err(adapter->dev, |
| 899 | "RECV DATA: failed to read REG_TXBD_RDPTR\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 900 | ret = -1; |
| 901 | goto done; |
| 902 | } |
| 903 | |
| 904 | while (((wrptr & MWIFIEX_RXBD_MASK) != |
| 905 | (card->rxbd_rdptr & MWIFIEX_RXBD_MASK)) || |
| 906 | ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) == |
| 907 | (card->rxbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) { |
| 908 | struct sk_buff *skb_data; |
| 909 | u16 rx_len; |
| 910 | |
| 911 | rd_index = card->rxbd_rdptr & MWIFIEX_RXBD_MASK; |
| 912 | skb_data = card->rx_buf_list[rd_index]; |
| 913 | |
| 914 | /* Get data length from interface header - |
| 915 | first byte is len, second byte is type */ |
| 916 | rx_len = *((u16 *)skb_data->data); |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 917 | dev_dbg(adapter->dev, |
| 918 | "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n", |
| 919 | card->rxbd_rdptr, wrptr, rx_len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 920 | skb_tmp = dev_alloc_skb(rx_len); |
| 921 | if (!skb_tmp) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 922 | dev_dbg(adapter->dev, |
| 923 | "info: Failed to alloc skb for RX\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 924 | ret = -EBUSY; |
| 925 | goto done; |
| 926 | } |
| 927 | |
| 928 | skb_put(skb_tmp, rx_len); |
| 929 | |
| 930 | memcpy(skb_tmp->data, skb_data->data + INTF_HEADER_LEN, rx_len); |
| 931 | if ((++card->rxbd_rdptr & MWIFIEX_RXBD_MASK) == |
| 932 | MWIFIEX_MAX_TXRX_BD) { |
| 933 | card->rxbd_rdptr = ((card->rxbd_rdptr & |
| 934 | MWIFIEX_BD_FLAG_ROLLOVER_IND) ^ |
| 935 | MWIFIEX_BD_FLAG_ROLLOVER_IND); |
| 936 | } |
| 937 | dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 938 | card->rxbd_rdptr, wrptr); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 939 | |
| 940 | /* Write the RX ring read pointer in to REG_RXBD_RDPTR */ |
| 941 | if (mwifiex_write_reg(adapter, REG_RXBD_RDPTR, |
| 942 | card->rxbd_rdptr)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 943 | dev_err(adapter->dev, |
| 944 | "RECV DATA: failed to write REG_RXBD_RDPTR\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 945 | ret = -1; |
| 946 | goto done; |
| 947 | } |
| 948 | |
| 949 | /* Read the RX ring Write pointer set by firmware */ |
| 950 | if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 951 | dev_err(adapter->dev, |
| 952 | "RECV DATA: failed to read REG_TXBD_RDPTR\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 953 | ret = -1; |
| 954 | goto done; |
| 955 | } |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 956 | dev_dbg(adapter->dev, |
| 957 | "info: RECV DATA: Rcvd packet from fw successfully\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 958 | mwifiex_handle_rx_packet(adapter, skb_tmp); |
| 959 | } |
| 960 | |
| 961 | done: |
| 962 | if (ret && skb_tmp) |
| 963 | dev_kfree_skb_any(skb_tmp); |
| 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * This function downloads the boot command to device |
| 969 | */ |
| 970 | static int |
| 971 | mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb) |
| 972 | { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 973 | dma_addr_t buf_pa; |
| 974 | struct pcie_service_card *card = adapter->card; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 975 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 976 | if (!(skb->data && skb->len)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 977 | dev_err(adapter->dev, |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 978 | "Invalid parameter in %s <%p. len %d>\n", |
| 979 | __func__, skb->data, skb->len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 980 | return -1; |
| 981 | } |
| 982 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 983 | if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE)) |
| 984 | return -1; |
| 985 | |
| 986 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
| 987 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 988 | /* Write the lower 32bits of the physical address to scratch |
| 989 | * register 0 */ |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 990 | if (mwifiex_write_reg(adapter, PCIE_SCRATCH_0_REG, (u32)buf_pa)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 991 | dev_err(adapter->dev, |
| 992 | "%s: failed to write download command to boot code.\n", |
| 993 | __func__); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 994 | pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE, |
| 995 | PCI_DMA_TODEVICE); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 996 | return -1; |
| 997 | } |
| 998 | |
| 999 | /* Write the upper 32bits of the physical address to scratch |
| 1000 | * register 1 */ |
| 1001 | if (mwifiex_write_reg(adapter, PCIE_SCRATCH_1_REG, |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1002 | (u32)((u64)buf_pa >> 32))) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1003 | dev_err(adapter->dev, |
| 1004 | "%s: failed to write download command to boot code.\n", |
| 1005 | __func__); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1006 | pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE, |
| 1007 | PCI_DMA_TODEVICE); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1008 | return -1; |
| 1009 | } |
| 1010 | |
| 1011 | /* Write the command length to scratch register 2 */ |
| 1012 | if (mwifiex_write_reg(adapter, PCIE_SCRATCH_2_REG, skb->len)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1013 | dev_err(adapter->dev, |
| 1014 | "%s: failed to write command len to scratch reg 2\n", |
| 1015 | __func__); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1016 | pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE, |
| 1017 | PCI_DMA_TODEVICE); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1018 | return -1; |
| 1019 | } |
| 1020 | |
| 1021 | /* Ring the door bell */ |
| 1022 | if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT, |
| 1023 | CPU_INTR_DOOR_BELL)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1024 | dev_err(adapter->dev, |
| 1025 | "%s: failed to assert door-bell intr\n", __func__); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1026 | pci_unmap_single(card->dev, buf_pa, |
| 1027 | MWIFIEX_UPLD_SIZE, PCI_DMA_TODEVICE); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1028 | return -1; |
| 1029 | } |
| 1030 | |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
Avinash Patil | c6d1d87 | 2013-01-03 21:21:29 -0800 | [diff] [blame] | 1034 | /* This function init rx port in firmware which in turn enables to receive data |
| 1035 | * from device before transmitting any packet. |
| 1036 | */ |
| 1037 | static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter) |
| 1038 | { |
| 1039 | struct pcie_service_card *card = adapter->card; |
| 1040 | |
| 1041 | /* Write the RX ring read pointer in to REG_RXBD_RDPTR */ |
| 1042 | if (mwifiex_write_reg(adapter, REG_RXBD_RDPTR, card->rxbd_rdptr | 0)) { |
| 1043 | dev_err(adapter->dev, |
| 1044 | "RECV DATA: failed to write REG_RXBD_RDPTR\n"); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | /* This function downloads commands to the device |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1051 | */ |
| 1052 | static int |
| 1053 | mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb) |
| 1054 | { |
| 1055 | struct pcie_service_card *card = adapter->card; |
| 1056 | int ret = 0; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1057 | dma_addr_t cmd_buf_pa, cmdrsp_buf_pa; |
| 1058 | u8 *payload = (u8 *)skb->data; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1059 | |
| 1060 | if (!(skb->data && skb->len)) { |
| 1061 | dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1062 | __func__, skb->data, skb->len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1063 | return -1; |
| 1064 | } |
| 1065 | |
| 1066 | /* Make sure a command response buffer is available */ |
| 1067 | if (!card->cmdrsp_buf) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1068 | dev_err(adapter->dev, |
| 1069 | "No response buffer available, send command failed\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1070 | return -EBUSY; |
| 1071 | } |
| 1072 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1073 | if (!mwifiex_pcie_ok_to_access_hw(adapter)) |
| 1074 | mwifiex_pm_wakeup_card(adapter); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1075 | |
| 1076 | adapter->cmd_sent = true; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1077 | |
| 1078 | *(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len); |
| 1079 | *(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD); |
| 1080 | |
| 1081 | if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE)) |
| 1082 | return -1; |
| 1083 | |
| 1084 | card->cmd_buf = skb; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1085 | |
| 1086 | /* To send a command, the driver will: |
| 1087 | 1. Write the 64bit physical address of the data buffer to |
| 1088 | SCRATCH1 + SCRATCH0 |
| 1089 | 2. Ring the door bell (i.e. set the door bell interrupt) |
| 1090 | |
| 1091 | In response to door bell interrupt, the firmware will perform |
| 1092 | the DMA of the command packet (first header to obtain the total |
| 1093 | length and then rest of the command). |
| 1094 | */ |
| 1095 | |
| 1096 | if (card->cmdrsp_buf) { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1097 | MWIFIEX_SKB_PACB(card->cmdrsp_buf, &cmdrsp_buf_pa); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1098 | /* Write the lower 32bits of the cmdrsp buffer physical |
| 1099 | address */ |
| 1100 | if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO, |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1101 | (u32)cmdrsp_buf_pa)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1102 | dev_err(adapter->dev, |
| 1103 | "Failed to write download cmd to boot code.\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1104 | ret = -1; |
| 1105 | goto done; |
| 1106 | } |
| 1107 | /* Write the upper 32bits of the cmdrsp buffer physical |
| 1108 | address */ |
| 1109 | if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI, |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1110 | (u32)((u64)cmdrsp_buf_pa >> 32))) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1111 | dev_err(adapter->dev, |
| 1112 | "Failed to write download cmd to boot code.\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1113 | ret = -1; |
| 1114 | goto done; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1118 | MWIFIEX_SKB_PACB(card->cmd_buf, &cmd_buf_pa); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1119 | /* Write the lower 32bits of the physical address to REG_CMD_ADDR_LO */ |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1120 | if (mwifiex_write_reg(adapter, REG_CMD_ADDR_LO, (u32)cmd_buf_pa)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1121 | dev_err(adapter->dev, |
| 1122 | "Failed to write download cmd to boot code.\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1123 | ret = -1; |
| 1124 | goto done; |
| 1125 | } |
| 1126 | /* Write the upper 32bits of the physical address to REG_CMD_ADDR_HI */ |
| 1127 | if (mwifiex_write_reg(adapter, REG_CMD_ADDR_HI, |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1128 | (u32)((u64)cmd_buf_pa >> 32))) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1129 | dev_err(adapter->dev, |
| 1130 | "Failed to write download cmd to boot code.\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1131 | ret = -1; |
| 1132 | goto done; |
| 1133 | } |
| 1134 | |
| 1135 | /* Write the command length to REG_CMD_SIZE */ |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1136 | if (mwifiex_write_reg(adapter, REG_CMD_SIZE, card->cmd_buf->len)) { |
| 1137 | dev_err(adapter->dev, |
| 1138 | "Failed to write cmd len to REG_CMD_SIZE\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1139 | ret = -1; |
| 1140 | goto done; |
| 1141 | } |
| 1142 | |
| 1143 | /* Ring the door bell */ |
| 1144 | if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT, |
| 1145 | CPU_INTR_DOOR_BELL)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1146 | dev_err(adapter->dev, |
| 1147 | "Failed to assert door-bell intr\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1148 | ret = -1; |
| 1149 | goto done; |
| 1150 | } |
| 1151 | |
| 1152 | done: |
| 1153 | if (ret) |
| 1154 | adapter->cmd_sent = false; |
| 1155 | |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * This function handles command complete interrupt |
| 1161 | */ |
| 1162 | static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter) |
| 1163 | { |
| 1164 | struct pcie_service_card *card = adapter->card; |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1165 | struct sk_buff *skb = card->cmdrsp_buf; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1166 | int count = 0; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1167 | u16 rx_len; |
| 1168 | __le16 pkt_len; |
| 1169 | dma_addr_t buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1170 | |
| 1171 | dev_dbg(adapter->dev, "info: Rx CMD Response\n"); |
| 1172 | |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1173 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
| 1174 | pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE, |
| 1175 | PCI_DMA_FROMDEVICE); |
| 1176 | |
| 1177 | pkt_len = *((__le16 *)skb->data); |
| 1178 | rx_len = le16_to_cpu(pkt_len); |
| 1179 | skb_trim(skb, rx_len); |
| 1180 | skb_pull(skb, INTF_HEADER_LEN); |
| 1181 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1182 | if (!adapter->curr_cmd) { |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1183 | if (adapter->ps_state == PS_STATE_SLEEP_CFM) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1184 | mwifiex_process_sleep_confirm_resp(adapter, skb->data, |
| 1185 | skb->len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1186 | while (mwifiex_pcie_ok_to_access_hw(adapter) && |
| 1187 | (count++ < 10)) |
Yogesh Ashok Powar | e7891ba | 2012-03-12 19:35:11 -0700 | [diff] [blame] | 1188 | usleep_range(50, 60); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1189 | } else { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1190 | dev_err(adapter->dev, |
| 1191 | "There is no command but got cmdrsp\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1192 | } |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1193 | memcpy(adapter->upld_buf, skb->data, |
| 1194 | min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len)); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1195 | if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE, |
| 1196 | PCI_DMA_FROMDEVICE)) |
| 1197 | return -1; |
| 1198 | |
| 1199 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1200 | } else if (mwifiex_pcie_ok_to_access_hw(adapter)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1201 | adapter->curr_cmd->resp_skb = skb; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1202 | adapter->cmd_resp_received = true; |
| 1203 | /* Take the pointer and set it to CMD node and will |
| 1204 | return in the response complete callback */ |
| 1205 | card->cmdrsp_buf = NULL; |
| 1206 | |
| 1207 | /* Clear the cmd-rsp buffer address in scratch registers. This |
| 1208 | will prevent firmware from writing to the same response |
| 1209 | buffer again. */ |
| 1210 | if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO, 0)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1211 | dev_err(adapter->dev, |
| 1212 | "cmd_done: failed to clear cmd_rsp_addr_lo\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1213 | return -1; |
| 1214 | } |
| 1215 | /* Write the upper 32bits of the cmdrsp buffer physical |
| 1216 | address */ |
| 1217 | if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI, 0)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1218 | dev_err(adapter->dev, |
| 1219 | "cmd_done: failed to clear cmd_rsp_addr_hi\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1220 | return -1; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | /* |
| 1228 | * Command Response processing complete handler |
| 1229 | */ |
| 1230 | static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter, |
| 1231 | struct sk_buff *skb) |
| 1232 | { |
| 1233 | struct pcie_service_card *card = adapter->card; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1234 | dma_addr_t buf_pa; |
| 1235 | struct sk_buff *skb_tmp; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1236 | |
| 1237 | if (skb) { |
| 1238 | card->cmdrsp_buf = skb; |
| 1239 | skb_push(card->cmdrsp_buf, INTF_HEADER_LEN); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1240 | if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE, |
| 1241 | PCI_DMA_FROMDEVICE)) |
| 1242 | return -1; |
| 1243 | } |
| 1244 | |
| 1245 | skb_tmp = card->cmd_buf; |
| 1246 | if (skb_tmp) { |
| 1247 | MWIFIEX_SKB_PACB(skb_tmp, &buf_pa); |
| 1248 | pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE, |
| 1249 | PCI_DMA_FROMDEVICE); |
| 1250 | card->cmd_buf = NULL; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * This function handles firmware event ready interrupt |
| 1258 | */ |
| 1259 | static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter) |
| 1260 | { |
| 1261 | struct pcie_service_card *card = adapter->card; |
| 1262 | u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK; |
| 1263 | u32 wrptr, event; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1264 | dma_addr_t buf_pa; |
| 1265 | |
| 1266 | if (!mwifiex_pcie_ok_to_access_hw(adapter)) |
| 1267 | mwifiex_pm_wakeup_card(adapter); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1268 | |
| 1269 | if (adapter->event_received) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1270 | dev_dbg(adapter->dev, "info: Event being processed, " |
| 1271 | "do not process this interrupt just yet\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | if (rdptr >= MWIFIEX_MAX_EVT_BD) { |
| 1276 | dev_dbg(adapter->dev, "info: Invalid read pointer...\n"); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | /* Read the event ring write pointer set by firmware */ |
| 1281 | if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1282 | dev_err(adapter->dev, |
| 1283 | "EventReady: failed to read REG_EVTBD_WRPTR\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1284 | return -1; |
| 1285 | } |
| 1286 | |
| 1287 | dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1288 | card->evtbd_rdptr, wrptr); |
| 1289 | if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr |
| 1290 | & MWIFIEX_EVTBD_MASK)) || |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1291 | ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) == |
| 1292 | (card->evtbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) { |
| 1293 | struct sk_buff *skb_cmd; |
| 1294 | __le16 data_len = 0; |
| 1295 | u16 evt_len; |
| 1296 | |
| 1297 | dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr); |
| 1298 | skb_cmd = card->evt_buf_list[rdptr]; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1299 | MWIFIEX_SKB_PACB(skb_cmd, &buf_pa); |
| 1300 | pci_unmap_single(card->dev, buf_pa, MAX_EVENT_SIZE, |
| 1301 | PCI_DMA_FROMDEVICE); |
| 1302 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1303 | /* Take the pointer and set it to event pointer in adapter |
| 1304 | and will return back after event handling callback */ |
| 1305 | card->evt_buf_list[rdptr] = NULL; |
| 1306 | card->evtbd_ring[rdptr]->paddr = 0; |
| 1307 | card->evtbd_ring[rdptr]->len = 0; |
| 1308 | card->evtbd_ring[rdptr]->flags = 0; |
| 1309 | |
| 1310 | event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN]; |
| 1311 | adapter->event_cause = event; |
| 1312 | /* The first 4bytes will be the event transfer header |
| 1313 | len is 2 bytes followed by type which is 2 bytes */ |
| 1314 | memcpy(&data_len, skb_cmd->data, sizeof(__le16)); |
| 1315 | evt_len = le16_to_cpu(data_len); |
| 1316 | |
| 1317 | skb_pull(skb_cmd, INTF_HEADER_LEN); |
| 1318 | dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len); |
| 1319 | |
| 1320 | if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE)) |
| 1321 | memcpy(adapter->event_body, skb_cmd->data + |
| 1322 | MWIFIEX_EVENT_HEADER_LEN, evt_len - |
| 1323 | MWIFIEX_EVENT_HEADER_LEN); |
| 1324 | |
| 1325 | adapter->event_received = true; |
| 1326 | adapter->event_skb = skb_cmd; |
| 1327 | |
| 1328 | /* Do not update the event read pointer here, wait till the |
| 1329 | buffer is released. This is just to make things simpler, |
| 1330 | we need to find a better method of managing these buffers. |
| 1331 | */ |
| 1332 | } |
| 1333 | |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Event processing complete handler |
| 1339 | */ |
| 1340 | static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter, |
| 1341 | struct sk_buff *skb) |
| 1342 | { |
| 1343 | struct pcie_service_card *card = adapter->card; |
| 1344 | int ret = 0; |
| 1345 | u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK; |
| 1346 | u32 wrptr; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1347 | dma_addr_t buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1348 | |
| 1349 | if (!skb) |
| 1350 | return 0; |
| 1351 | |
Dan Carpenter | 1eb54c8 | 2011-11-07 19:31:47 -0800 | [diff] [blame] | 1352 | if (rdptr >= MWIFIEX_MAX_EVT_BD) { |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1353 | dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1354 | rdptr); |
Amitkumar Karwar | 8c53e42 | 2011-12-08 20:41:03 -0800 | [diff] [blame] | 1355 | return -EINVAL; |
Dan Carpenter | 1eb54c8 | 2011-11-07 19:31:47 -0800 | [diff] [blame] | 1356 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1357 | |
| 1358 | /* Read the event ring write pointer set by firmware */ |
| 1359 | if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1360 | dev_err(adapter->dev, |
| 1361 | "event_complete: failed to read REG_EVTBD_WRPTR\n"); |
Amitkumar Karwar | 8c53e42 | 2011-12-08 20:41:03 -0800 | [diff] [blame] | 1362 | return -1; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | if (!card->evt_buf_list[rdptr]) { |
| 1366 | skb_push(skb, INTF_HEADER_LEN); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1367 | if (mwifiex_map_pci_memory(adapter, skb, |
| 1368 | MAX_EVENT_SIZE, |
| 1369 | PCI_DMA_FROMDEVICE)) |
| 1370 | return -1; |
| 1371 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1372 | card->evt_buf_list[rdptr] = skb; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1373 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
| 1374 | card->evtbd_ring[rdptr]->paddr = buf_pa; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1375 | card->evtbd_ring[rdptr]->len = (u16)skb->len; |
| 1376 | card->evtbd_ring[rdptr]->flags = 0; |
| 1377 | skb = NULL; |
| 1378 | } else { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1379 | dev_dbg(adapter->dev, |
| 1380 | "info: ERROR: buf still valid at index %d, <%p, %p>\n", |
| 1381 | rdptr, card->evt_buf_list[rdptr], skb); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) { |
| 1385 | card->evtbd_rdptr = ((card->evtbd_rdptr & |
| 1386 | MWIFIEX_BD_FLAG_ROLLOVER_IND) ^ |
| 1387 | MWIFIEX_BD_FLAG_ROLLOVER_IND); |
| 1388 | } |
| 1389 | |
| 1390 | dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1391 | card->evtbd_rdptr, wrptr); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1392 | |
| 1393 | /* Write the event ring read pointer in to REG_EVTBD_RDPTR */ |
| 1394 | if (mwifiex_write_reg(adapter, REG_EVTBD_RDPTR, card->evtbd_rdptr)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1395 | dev_err(adapter->dev, |
| 1396 | "event_complete: failed to read REG_EVTBD_RDPTR\n"); |
Amitkumar Karwar | 8c53e42 | 2011-12-08 20:41:03 -0800 | [diff] [blame] | 1397 | return -1; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1400 | dev_dbg(adapter->dev, "info: Check Events Again\n"); |
| 1401 | ret = mwifiex_pcie_process_event_ready(adapter); |
| 1402 | |
| 1403 | return ret; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * This function downloads the firmware to the card. |
| 1408 | * |
| 1409 | * Firmware is downloaded to the card in blocks. Every block download |
| 1410 | * is tested for CRC errors, and retried a number of times before |
| 1411 | * returning failure. |
| 1412 | */ |
| 1413 | static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter, |
| 1414 | struct mwifiex_fw_image *fw) |
| 1415 | { |
| 1416 | int ret; |
| 1417 | u8 *firmware = fw->fw_buf; |
| 1418 | u32 firmware_len = fw->fw_len; |
| 1419 | u32 offset = 0; |
| 1420 | struct sk_buff *skb; |
| 1421 | u32 txlen, tx_blocks = 0, tries, len; |
| 1422 | u32 block_retry_cnt = 0; |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1423 | dma_addr_t buf_pa; |
| 1424 | struct pcie_service_card *card = adapter->card; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1425 | |
| 1426 | if (!firmware || !firmware_len) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1427 | dev_err(adapter->dev, |
| 1428 | "No firmware image found! Terminating download\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1429 | return -1; |
| 1430 | } |
| 1431 | |
| 1432 | dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1433 | firmware_len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1434 | |
| 1435 | if (mwifiex_pcie_disable_host_int(adapter)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1436 | dev_err(adapter->dev, |
| 1437 | "%s: Disabling interrupts failed.\n", __func__); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1438 | return -1; |
| 1439 | } |
| 1440 | |
| 1441 | skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE); |
| 1442 | if (!skb) { |
| 1443 | ret = -ENOMEM; |
| 1444 | goto done; |
| 1445 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1446 | |
| 1447 | /* Perform firmware data transfer */ |
| 1448 | do { |
| 1449 | u32 ireg_intr = 0; |
| 1450 | |
| 1451 | /* More data? */ |
| 1452 | if (offset >= firmware_len) |
| 1453 | break; |
| 1454 | |
| 1455 | for (tries = 0; tries < MAX_POLL_TRIES; tries++) { |
| 1456 | ret = mwifiex_read_reg(adapter, PCIE_SCRATCH_2_REG, |
| 1457 | &len); |
| 1458 | if (ret) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1459 | dev_warn(adapter->dev, |
| 1460 | "Failed reading len from boot code\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1461 | goto done; |
| 1462 | } |
| 1463 | if (len) |
| 1464 | break; |
Yogesh Ashok Powar | e7891ba | 2012-03-12 19:35:11 -0700 | [diff] [blame] | 1465 | usleep_range(10, 20); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | if (!len) { |
| 1469 | break; |
| 1470 | } else if (len > MWIFIEX_UPLD_SIZE) { |
| 1471 | pr_err("FW download failure @ %d, invalid length %d\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1472 | offset, len); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1473 | ret = -1; |
| 1474 | goto done; |
| 1475 | } |
| 1476 | |
| 1477 | txlen = len; |
| 1478 | |
| 1479 | if (len & BIT(0)) { |
| 1480 | block_retry_cnt++; |
| 1481 | if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) { |
| 1482 | pr_err("FW download failure @ %d, over max " |
| 1483 | "retry count\n", offset); |
| 1484 | ret = -1; |
| 1485 | goto done; |
| 1486 | } |
| 1487 | dev_err(adapter->dev, "FW CRC error indicated by the " |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1488 | "helper: len = 0x%04X, txlen = %d\n", |
| 1489 | len, txlen); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1490 | len &= ~BIT(0); |
| 1491 | /* Setting this to 0 to resend from same offset */ |
| 1492 | txlen = 0; |
| 1493 | } else { |
| 1494 | block_retry_cnt = 0; |
| 1495 | /* Set blocksize to transfer - checking for |
| 1496 | last block */ |
| 1497 | if (firmware_len - offset < txlen) |
| 1498 | txlen = firmware_len - offset; |
| 1499 | |
| 1500 | dev_dbg(adapter->dev, "."); |
| 1501 | |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1502 | tx_blocks = (txlen + |
| 1503 | MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD - 1) / |
| 1504 | MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1505 | |
| 1506 | /* Copy payload to buffer */ |
| 1507 | memmove(skb->data, &firmware[offset], txlen); |
| 1508 | } |
| 1509 | |
| 1510 | skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len); |
| 1511 | skb_trim(skb, tx_blocks * MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD); |
| 1512 | |
| 1513 | /* Send the boot command to device */ |
| 1514 | if (mwifiex_pcie_send_boot_cmd(adapter, skb)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1515 | dev_err(adapter->dev, |
| 1516 | "Failed to send firmware download command\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1517 | ret = -1; |
| 1518 | goto done; |
| 1519 | } |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1520 | |
| 1521 | MWIFIEX_SKB_PACB(skb, &buf_pa); |
| 1522 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1523 | /* Wait for the command done interrupt */ |
| 1524 | do { |
| 1525 | if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS, |
| 1526 | &ireg_intr)) { |
| 1527 | dev_err(adapter->dev, "%s: Failed to read " |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1528 | "interrupt status during fw dnld.\n", |
| 1529 | __func__); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1530 | pci_unmap_single(card->dev, buf_pa, skb->len, |
| 1531 | PCI_DMA_TODEVICE); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1532 | ret = -1; |
| 1533 | goto done; |
| 1534 | } |
| 1535 | } while ((ireg_intr & CPU_INTR_DOOR_BELL) == |
| 1536 | CPU_INTR_DOOR_BELL); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1537 | |
| 1538 | pci_unmap_single(card->dev, buf_pa, skb->len, |
| 1539 | PCI_DMA_TODEVICE); |
| 1540 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1541 | offset += txlen; |
| 1542 | } while (true); |
| 1543 | |
| 1544 | dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1545 | offset); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1546 | |
| 1547 | ret = 0; |
| 1548 | |
| 1549 | done: |
| 1550 | dev_kfree_skb_any(skb); |
| 1551 | return ret; |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * This function checks the firmware status in card. |
| 1556 | * |
| 1557 | * The winner interface is also determined by this function. |
| 1558 | */ |
| 1559 | static int |
| 1560 | mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num) |
| 1561 | { |
| 1562 | int ret = 0; |
| 1563 | u32 firmware_stat, winner_status; |
| 1564 | u32 tries; |
| 1565 | |
| 1566 | /* Mask spurios interrupts */ |
| 1567 | if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK, |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1568 | HOST_INTR_MASK)) { |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1569 | dev_warn(adapter->dev, "Write register failed\n"); |
| 1570 | return -1; |
| 1571 | } |
| 1572 | |
| 1573 | dev_dbg(adapter->dev, "Setting driver ready signature\n"); |
| 1574 | if (mwifiex_write_reg(adapter, REG_DRV_READY, FIRMWARE_READY_PCIE)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1575 | dev_err(adapter->dev, |
| 1576 | "Failed to write driver ready signature\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1577 | return -1; |
| 1578 | } |
| 1579 | |
| 1580 | /* Wait for firmware initialization event */ |
| 1581 | for (tries = 0; tries < poll_num; tries++) { |
| 1582 | if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG, |
| 1583 | &firmware_stat)) |
| 1584 | ret = -1; |
| 1585 | else |
| 1586 | ret = 0; |
| 1587 | if (ret) |
| 1588 | continue; |
| 1589 | if (firmware_stat == FIRMWARE_READY_PCIE) { |
| 1590 | ret = 0; |
| 1591 | break; |
| 1592 | } else { |
| 1593 | mdelay(100); |
| 1594 | ret = -1; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | if (ret) { |
| 1599 | if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG, |
| 1600 | &winner_status)) |
| 1601 | ret = -1; |
| 1602 | else if (!winner_status) { |
| 1603 | dev_err(adapter->dev, "PCI-E is the winner\n"); |
| 1604 | adapter->winner = 1; |
| 1605 | ret = -1; |
| 1606 | } else { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1607 | dev_err(adapter->dev, |
| 1608 | "PCI-E is not the winner <%#x,%d>, exit dnld\n", |
| 1609 | ret, adapter->winner); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1610 | ret = 0; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | return ret; |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * This function reads the interrupt status from card. |
| 1619 | */ |
| 1620 | static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter) |
| 1621 | { |
| 1622 | u32 pcie_ireg; |
| 1623 | unsigned long flags; |
| 1624 | |
| 1625 | if (!mwifiex_pcie_ok_to_access_hw(adapter)) |
| 1626 | return; |
| 1627 | |
| 1628 | if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) { |
| 1629 | dev_warn(adapter->dev, "Read register failed\n"); |
| 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) { |
| 1634 | |
| 1635 | mwifiex_pcie_disable_host_int(adapter); |
| 1636 | |
| 1637 | /* Clear the pending interrupts */ |
| 1638 | if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS, |
| 1639 | ~pcie_ireg)) { |
| 1640 | dev_warn(adapter->dev, "Write register failed\n"); |
| 1641 | return; |
| 1642 | } |
| 1643 | spin_lock_irqsave(&adapter->int_lock, flags); |
| 1644 | adapter->int_status |= pcie_ireg; |
| 1645 | spin_unlock_irqrestore(&adapter->int_lock, flags); |
| 1646 | |
| 1647 | if (pcie_ireg & HOST_INTR_CMD_DONE) { |
| 1648 | if ((adapter->ps_state == PS_STATE_SLEEP_CFM) || |
| 1649 | (adapter->ps_state == PS_STATE_SLEEP)) { |
| 1650 | mwifiex_pcie_enable_host_int(adapter); |
| 1651 | if (mwifiex_write_reg(adapter, |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1652 | PCIE_CPU_INT_EVENT, |
| 1653 | CPU_INTR_SLEEP_CFM_DONE) |
| 1654 | ) { |
| 1655 | dev_warn(adapter->dev, |
| 1656 | "Write register failed\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1657 | return; |
| 1658 | |
| 1659 | } |
| 1660 | } |
| 1661 | } else if (!adapter->pps_uapsd_mode && |
| 1662 | adapter->ps_state == PS_STATE_SLEEP) { |
| 1663 | /* Potentially for PCIe we could get other |
| 1664 | * interrupts like shared. Don't change power |
| 1665 | * state until cookie is set */ |
| 1666 | if (mwifiex_pcie_ok_to_access_hw(adapter)) |
| 1667 | adapter->ps_state = PS_STATE_AWAKE; |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * Interrupt handler for PCIe root port |
| 1674 | * |
| 1675 | * This function reads the interrupt status from firmware and assigns |
| 1676 | * the main process in workqueue which will handle the interrupt. |
| 1677 | */ |
| 1678 | static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context) |
| 1679 | { |
| 1680 | struct pci_dev *pdev = (struct pci_dev *)context; |
| 1681 | struct pcie_service_card *card; |
| 1682 | struct mwifiex_adapter *adapter; |
| 1683 | |
| 1684 | if (!pdev) { |
| 1685 | pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev); |
| 1686 | goto exit; |
| 1687 | } |
| 1688 | |
| 1689 | card = (struct pcie_service_card *) pci_get_drvdata(pdev); |
| 1690 | if (!card || !card->adapter) { |
| 1691 | pr_debug("info: %s: card=%p adapter=%p\n", __func__, card, |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1692 | card ? card->adapter : NULL); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1693 | goto exit; |
| 1694 | } |
| 1695 | adapter = card->adapter; |
| 1696 | |
| 1697 | if (adapter->surprise_removed) |
| 1698 | goto exit; |
| 1699 | |
| 1700 | mwifiex_interrupt_status(adapter); |
| 1701 | queue_work(adapter->workqueue, &adapter->main_work); |
| 1702 | |
| 1703 | exit: |
| 1704 | return IRQ_HANDLED; |
| 1705 | } |
| 1706 | |
| 1707 | /* |
| 1708 | * This function checks the current interrupt status. |
| 1709 | * |
| 1710 | * The following interrupts are checked and handled by this function - |
| 1711 | * - Data sent |
| 1712 | * - Command sent |
| 1713 | * - Command received |
| 1714 | * - Packets received |
| 1715 | * - Events received |
| 1716 | * |
| 1717 | * In case of Rx packets received, the packets are uploaded from card to |
| 1718 | * host and processed accordingly. |
| 1719 | */ |
| 1720 | static int mwifiex_process_int_status(struct mwifiex_adapter *adapter) |
| 1721 | { |
| 1722 | int ret; |
Avinash Patil | 659c478 | 2013-01-03 21:21:28 -0800 | [diff] [blame] | 1723 | u32 pcie_ireg; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1724 | unsigned long flags; |
| 1725 | |
| 1726 | spin_lock_irqsave(&adapter->int_lock, flags); |
| 1727 | /* Clear out unused interrupts */ |
Avinash Patil | 659c478 | 2013-01-03 21:21:28 -0800 | [diff] [blame] | 1728 | pcie_ireg = adapter->int_status; |
| 1729 | adapter->int_status = 0; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1730 | spin_unlock_irqrestore(&adapter->int_lock, flags); |
| 1731 | |
Avinash Patil | 659c478 | 2013-01-03 21:21:28 -0800 | [diff] [blame] | 1732 | while (pcie_ireg & HOST_INTR_MASK) { |
| 1733 | if (pcie_ireg & HOST_INTR_DNLD_DONE) { |
| 1734 | pcie_ireg &= ~HOST_INTR_DNLD_DONE; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1735 | if (adapter->data_sent) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1736 | dev_dbg(adapter->dev, "info: DATA sent intr\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1737 | adapter->data_sent = false; |
| 1738 | } |
| 1739 | } |
Avinash Patil | 659c478 | 2013-01-03 21:21:28 -0800 | [diff] [blame] | 1740 | if (pcie_ireg & HOST_INTR_UPLD_RDY) { |
| 1741 | pcie_ireg &= ~HOST_INTR_UPLD_RDY; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1742 | dev_dbg(adapter->dev, "info: Rx DATA\n"); |
| 1743 | ret = mwifiex_pcie_process_recv_data(adapter); |
| 1744 | if (ret) |
| 1745 | return ret; |
| 1746 | } |
Avinash Patil | 659c478 | 2013-01-03 21:21:28 -0800 | [diff] [blame] | 1747 | if (pcie_ireg & HOST_INTR_EVENT_RDY) { |
| 1748 | pcie_ireg &= ~HOST_INTR_EVENT_RDY; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1749 | dev_dbg(adapter->dev, "info: Rx EVENT\n"); |
| 1750 | ret = mwifiex_pcie_process_event_ready(adapter); |
| 1751 | if (ret) |
| 1752 | return ret; |
| 1753 | } |
| 1754 | |
Avinash Patil | 659c478 | 2013-01-03 21:21:28 -0800 | [diff] [blame] | 1755 | if (pcie_ireg & HOST_INTR_CMD_DONE) { |
| 1756 | pcie_ireg &= ~HOST_INTR_CMD_DONE; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1757 | if (adapter->cmd_sent) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1758 | dev_dbg(adapter->dev, |
| 1759 | "info: CMD sent Interrupt\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1760 | adapter->cmd_sent = false; |
| 1761 | } |
| 1762 | /* Handle command response */ |
| 1763 | ret = mwifiex_pcie_process_cmd_complete(adapter); |
| 1764 | if (ret) |
| 1765 | return ret; |
| 1766 | } |
| 1767 | |
| 1768 | if (mwifiex_pcie_ok_to_access_hw(adapter)) { |
| 1769 | if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, |
| 1770 | &pcie_ireg)) { |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1771 | dev_warn(adapter->dev, |
| 1772 | "Read register failed\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1773 | return -1; |
| 1774 | } |
| 1775 | |
| 1776 | if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) { |
| 1777 | if (mwifiex_write_reg(adapter, |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1778 | PCIE_HOST_INT_STATUS, |
| 1779 | ~pcie_ireg)) { |
| 1780 | dev_warn(adapter->dev, |
| 1781 | "Write register failed\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1782 | return -1; |
| 1783 | } |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | } |
| 1787 | } |
| 1788 | dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n", |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1789 | adapter->cmd_sent, adapter->data_sent); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1790 | mwifiex_pcie_enable_host_int(adapter); |
| 1791 | |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | /* |
| 1796 | * This function downloads data from driver to card. |
| 1797 | * |
| 1798 | * Both commands and data packets are transferred to the card by this |
| 1799 | * function. |
| 1800 | * |
| 1801 | * This function adds the PCIE specific header to the front of the buffer |
| 1802 | * before transferring. The header contains the length of the packet and |
| 1803 | * the type. The firmware handles the packets based upon this set type. |
| 1804 | */ |
| 1805 | static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type, |
| 1806 | struct sk_buff *skb, |
| 1807 | struct mwifiex_tx_param *tx_param) |
| 1808 | { |
Dan Carpenter | fa161cb | 2011-11-07 19:31:45 -0800 | [diff] [blame] | 1809 | if (!skb) { |
| 1810 | dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1811 | return -1; |
| 1812 | } |
| 1813 | |
| 1814 | if (type == MWIFIEX_TYPE_DATA) |
| 1815 | return mwifiex_pcie_send_data(adapter, skb); |
| 1816 | else if (type == MWIFIEX_TYPE_CMD) |
| 1817 | return mwifiex_pcie_send_cmd(adapter, skb); |
| 1818 | |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * This function initializes the PCI-E host memory space, WCB rings, etc. |
| 1824 | * |
| 1825 | * The following initializations steps are followed - |
| 1826 | * - Allocate TXBD ring buffers |
| 1827 | * - Allocate RXBD ring buffers |
| 1828 | * - Allocate event BD ring buffers |
| 1829 | * - Allocate command response ring buffer |
| 1830 | * - Allocate sleep cookie buffer |
| 1831 | */ |
| 1832 | static int mwifiex_pcie_init(struct mwifiex_adapter *adapter) |
| 1833 | { |
| 1834 | struct pcie_service_card *card = adapter->card; |
| 1835 | int ret; |
| 1836 | struct pci_dev *pdev = card->dev; |
| 1837 | |
| 1838 | pci_set_drvdata(pdev, card); |
| 1839 | |
| 1840 | ret = pci_enable_device(pdev); |
| 1841 | if (ret) |
| 1842 | goto err_enable_dev; |
| 1843 | |
| 1844 | pci_set_master(pdev); |
| 1845 | |
| 1846 | dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n"); |
| 1847 | ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 1848 | if (ret) { |
| 1849 | dev_err(adapter->dev, "set_dma_mask(32) failed\n"); |
| 1850 | goto err_set_dma_mask; |
| 1851 | } |
| 1852 | |
| 1853 | ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 1854 | if (ret) { |
| 1855 | dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n"); |
| 1856 | goto err_set_dma_mask; |
| 1857 | } |
| 1858 | |
| 1859 | ret = pci_request_region(pdev, 0, DRV_NAME); |
| 1860 | if (ret) { |
| 1861 | dev_err(adapter->dev, "req_reg(0) error\n"); |
| 1862 | goto err_req_region0; |
| 1863 | } |
| 1864 | card->pci_mmap = pci_iomap(pdev, 0, 0); |
| 1865 | if (!card->pci_mmap) { |
| 1866 | dev_err(adapter->dev, "iomap(0) error\n"); |
| 1867 | goto err_iomap0; |
| 1868 | } |
| 1869 | ret = pci_request_region(pdev, 2, DRV_NAME); |
| 1870 | if (ret) { |
| 1871 | dev_err(adapter->dev, "req_reg(2) error\n"); |
| 1872 | goto err_req_region2; |
| 1873 | } |
| 1874 | card->pci_mmap1 = pci_iomap(pdev, 2, 0); |
| 1875 | if (!card->pci_mmap1) { |
| 1876 | dev_err(adapter->dev, "iomap(2) error\n"); |
| 1877 | goto err_iomap2; |
| 1878 | } |
| 1879 | |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1880 | dev_dbg(adapter->dev, |
| 1881 | "PCI memory map Virt0: %p PCI memory map Virt2: %p\n", |
| 1882 | card->pci_mmap, card->pci_mmap1); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1883 | |
| 1884 | card->cmdrsp_buf = NULL; |
| 1885 | ret = mwifiex_pcie_create_txbd_ring(adapter); |
| 1886 | if (ret) |
| 1887 | goto err_cre_txbd; |
| 1888 | ret = mwifiex_pcie_create_rxbd_ring(adapter); |
| 1889 | if (ret) |
| 1890 | goto err_cre_rxbd; |
| 1891 | ret = mwifiex_pcie_create_evtbd_ring(adapter); |
| 1892 | if (ret) |
| 1893 | goto err_cre_evtbd; |
| 1894 | ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter); |
| 1895 | if (ret) |
| 1896 | goto err_alloc_cmdbuf; |
| 1897 | ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter); |
| 1898 | if (ret) |
| 1899 | goto err_alloc_cookie; |
| 1900 | |
| 1901 | return ret; |
| 1902 | |
| 1903 | err_alloc_cookie: |
| 1904 | mwifiex_pcie_delete_cmdrsp_buf(adapter); |
| 1905 | err_alloc_cmdbuf: |
| 1906 | mwifiex_pcie_delete_evtbd_ring(adapter); |
| 1907 | err_cre_evtbd: |
| 1908 | mwifiex_pcie_delete_rxbd_ring(adapter); |
| 1909 | err_cre_rxbd: |
| 1910 | mwifiex_pcie_delete_txbd_ring(adapter); |
| 1911 | err_cre_txbd: |
| 1912 | pci_iounmap(pdev, card->pci_mmap1); |
| 1913 | err_iomap2: |
| 1914 | pci_release_region(pdev, 2); |
| 1915 | err_req_region2: |
| 1916 | pci_iounmap(pdev, card->pci_mmap); |
| 1917 | err_iomap0: |
| 1918 | pci_release_region(pdev, 0); |
| 1919 | err_req_region0: |
| 1920 | err_set_dma_mask: |
| 1921 | pci_disable_device(pdev); |
| 1922 | err_enable_dev: |
| 1923 | pci_set_drvdata(pdev, NULL); |
| 1924 | return ret; |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * This function cleans up the allocated card buffers. |
| 1929 | * |
| 1930 | * The following are freed by this function - |
| 1931 | * - TXBD ring buffers |
| 1932 | * - RXBD ring buffers |
| 1933 | * - Event BD ring buffers |
| 1934 | * - Command response ring buffer |
| 1935 | * - Sleep cookie buffer |
| 1936 | */ |
| 1937 | static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter) |
| 1938 | { |
| 1939 | struct pcie_service_card *card = adapter->card; |
| 1940 | struct pci_dev *pdev = card->dev; |
| 1941 | |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1942 | if (user_rmmod) { |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 1943 | dev_dbg(adapter->dev, "Clearing driver ready signature\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1944 | if (mwifiex_write_reg(adapter, REG_DRV_READY, 0x00000000)) |
Yogesh Ashok Powar | f57c1ed | 2012-03-13 19:22:37 -0700 | [diff] [blame] | 1945 | dev_err(adapter->dev, |
| 1946 | "Failed to write driver not-ready signature\n"); |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | if (pdev) { |
| 1950 | pci_iounmap(pdev, card->pci_mmap); |
| 1951 | pci_iounmap(pdev, card->pci_mmap1); |
| 1952 | |
| 1953 | pci_release_regions(pdev); |
| 1954 | pci_disable_device(pdev); |
| 1955 | pci_set_drvdata(pdev, NULL); |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | /* |
| 1960 | * This function registers the PCIE device. |
| 1961 | * |
| 1962 | * PCIE IRQ is claimed, block size is set and driver data is initialized. |
| 1963 | */ |
| 1964 | static int mwifiex_register_dev(struct mwifiex_adapter *adapter) |
| 1965 | { |
| 1966 | int ret; |
| 1967 | struct pcie_service_card *card = adapter->card; |
| 1968 | struct pci_dev *pdev = card->dev; |
| 1969 | |
| 1970 | /* save adapter pointer in card */ |
| 1971 | card->adapter = adapter; |
| 1972 | |
| 1973 | ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED, |
| 1974 | "MRVL_PCIE", pdev); |
| 1975 | if (ret) { |
| 1976 | pr_err("request_irq failed: ret=%d\n", ret); |
| 1977 | adapter->card = NULL; |
| 1978 | return -1; |
| 1979 | } |
| 1980 | |
| 1981 | adapter->dev = &pdev->dev; |
| 1982 | strcpy(adapter->fw_name, PCIE8766_DEFAULT_FW_NAME); |
| 1983 | |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * This function unregisters the PCIE device. |
| 1989 | * |
| 1990 | * The PCIE IRQ is released, the function is disabled and driver |
| 1991 | * data is set to null. |
| 1992 | */ |
| 1993 | static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) |
| 1994 | { |
| 1995 | struct pcie_service_card *card = adapter->card; |
| 1996 | |
| 1997 | if (card) { |
| 1998 | dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__); |
| 1999 | free_irq(card->dev->irq, card->dev); |
Avinash Patil | fc33146 | 2013-01-03 21:21:30 -0800 | [diff] [blame] | 2000 | |
| 2001 | mwifiex_pcie_delete_sleep_cookie_buf(adapter); |
| 2002 | mwifiex_pcie_delete_cmdrsp_buf(adapter); |
| 2003 | mwifiex_pcie_delete_evtbd_ring(adapter); |
| 2004 | mwifiex_pcie_delete_rxbd_ring(adapter); |
| 2005 | mwifiex_pcie_delete_txbd_ring(adapter); |
| 2006 | card->cmdrsp_buf = NULL; |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | static struct mwifiex_if_ops pcie_ops = { |
| 2011 | .init_if = mwifiex_pcie_init, |
| 2012 | .cleanup_if = mwifiex_pcie_cleanup, |
| 2013 | .check_fw_status = mwifiex_check_fw_status, |
| 2014 | .prog_fw = mwifiex_prog_fw_w_helper, |
| 2015 | .register_dev = mwifiex_register_dev, |
| 2016 | .unregister_dev = mwifiex_unregister_dev, |
| 2017 | .enable_int = mwifiex_pcie_enable_host_int, |
| 2018 | .process_int_status = mwifiex_process_int_status, |
| 2019 | .host_to_card = mwifiex_pcie_host_to_card, |
| 2020 | .wakeup = mwifiex_pm_wakeup_card, |
| 2021 | .wakeup_complete = mwifiex_pm_wakeup_card_complete, |
| 2022 | |
| 2023 | /* PCIE specific */ |
| 2024 | .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete, |
| 2025 | .event_complete = mwifiex_pcie_event_complete, |
| 2026 | .update_mp_end_port = NULL, |
| 2027 | .cleanup_mpa_buf = NULL, |
Avinash Patil | c6d1d87 | 2013-01-03 21:21:29 -0800 | [diff] [blame] | 2028 | .init_fw_port = mwifiex_pcie_init_fw_port, |
Avinash Patil | fbd7e7a | 2013-01-03 21:21:31 -0800 | [diff] [blame^] | 2029 | .clean_pcie_ring = mwifiex_clean_pcie_ring_buf, |
Amitkumar Karwar | d930fae | 2011-10-11 17:41:21 -0700 | [diff] [blame] | 2030 | }; |
| 2031 | |
| 2032 | /* |
| 2033 | * This function initializes the PCIE driver module. |
| 2034 | * |
| 2035 | * This initiates the semaphore and registers the device with |
| 2036 | * PCIE bus. |
| 2037 | */ |
| 2038 | static int mwifiex_pcie_init_module(void) |
| 2039 | { |
| 2040 | int ret; |
| 2041 | |
| 2042 | pr_debug("Marvell 8766 PCIe Driver\n"); |
| 2043 | |
| 2044 | sema_init(&add_remove_card_sem, 1); |
| 2045 | |
| 2046 | /* Clear the flag in case user removes the card. */ |
| 2047 | user_rmmod = 0; |
| 2048 | |
| 2049 | ret = pci_register_driver(&mwifiex_pcie); |
| 2050 | if (ret) |
| 2051 | pr_err("Driver register failed!\n"); |
| 2052 | else |
| 2053 | pr_debug("info: Driver registered successfully!\n"); |
| 2054 | |
| 2055 | return ret; |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * This function cleans up the PCIE driver. |
| 2060 | * |
| 2061 | * The following major steps are followed for cleanup - |
| 2062 | * - Resume the device if its suspended |
| 2063 | * - Disconnect the device if connected |
| 2064 | * - Shutdown the firmware |
| 2065 | * - Unregister the device from PCIE bus. |
| 2066 | */ |
| 2067 | static void mwifiex_pcie_cleanup_module(void) |
| 2068 | { |
| 2069 | if (!down_interruptible(&add_remove_card_sem)) |
| 2070 | up(&add_remove_card_sem); |
| 2071 | |
| 2072 | /* Set the flag as user is removing this module. */ |
| 2073 | user_rmmod = 1; |
| 2074 | |
| 2075 | pci_unregister_driver(&mwifiex_pcie); |
| 2076 | } |
| 2077 | |
| 2078 | module_init(mwifiex_pcie_init_module); |
| 2079 | module_exit(mwifiex_pcie_cleanup_module); |
| 2080 | |
| 2081 | MODULE_AUTHOR("Marvell International Ltd."); |
| 2082 | MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION); |
| 2083 | MODULE_VERSION(PCIE_VERSION); |
| 2084 | MODULE_LICENSE("GPL v2"); |
| 2085 | MODULE_FIRMWARE("mrvl/pcie8766_uapsta.bin"); |