Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 - QLogic Corporation. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 18 | * MA 02111-1307, USA. |
| 19 | * |
| 20 | * The full GNU General Public License is included in this distribution |
| 21 | * in the file called "COPYING". |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include "qlcnic.h" |
| 28 | |
| 29 | struct crb_addr_pair { |
| 30 | u32 addr; |
| 31 | u32 data; |
| 32 | }; |
| 33 | |
| 34 | #define QLCNIC_MAX_CRB_XFORM 60 |
| 35 | static unsigned int crb_addr_xform[QLCNIC_MAX_CRB_XFORM]; |
| 36 | |
| 37 | #define crb_addr_transform(name) \ |
| 38 | (crb_addr_xform[QLCNIC_HW_PX_MAP_CRB_##name] = \ |
| 39 | QLCNIC_HW_CRB_HUB_AGT_ADR_##name << 20) |
| 40 | |
| 41 | #define QLCNIC_ADDR_ERROR (0xffffffff) |
| 42 | |
| 43 | static void |
| 44 | qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter, |
| 45 | struct qlcnic_host_rds_ring *rds_ring); |
| 46 | |
| 47 | static void crb_addr_transform_setup(void) |
| 48 | { |
| 49 | crb_addr_transform(XDMA); |
| 50 | crb_addr_transform(TIMR); |
| 51 | crb_addr_transform(SRE); |
| 52 | crb_addr_transform(SQN3); |
| 53 | crb_addr_transform(SQN2); |
| 54 | crb_addr_transform(SQN1); |
| 55 | crb_addr_transform(SQN0); |
| 56 | crb_addr_transform(SQS3); |
| 57 | crb_addr_transform(SQS2); |
| 58 | crb_addr_transform(SQS1); |
| 59 | crb_addr_transform(SQS0); |
| 60 | crb_addr_transform(RPMX7); |
| 61 | crb_addr_transform(RPMX6); |
| 62 | crb_addr_transform(RPMX5); |
| 63 | crb_addr_transform(RPMX4); |
| 64 | crb_addr_transform(RPMX3); |
| 65 | crb_addr_transform(RPMX2); |
| 66 | crb_addr_transform(RPMX1); |
| 67 | crb_addr_transform(RPMX0); |
| 68 | crb_addr_transform(ROMUSB); |
| 69 | crb_addr_transform(SN); |
| 70 | crb_addr_transform(QMN); |
| 71 | crb_addr_transform(QMS); |
| 72 | crb_addr_transform(PGNI); |
| 73 | crb_addr_transform(PGND); |
| 74 | crb_addr_transform(PGN3); |
| 75 | crb_addr_transform(PGN2); |
| 76 | crb_addr_transform(PGN1); |
| 77 | crb_addr_transform(PGN0); |
| 78 | crb_addr_transform(PGSI); |
| 79 | crb_addr_transform(PGSD); |
| 80 | crb_addr_transform(PGS3); |
| 81 | crb_addr_transform(PGS2); |
| 82 | crb_addr_transform(PGS1); |
| 83 | crb_addr_transform(PGS0); |
| 84 | crb_addr_transform(PS); |
| 85 | crb_addr_transform(PH); |
| 86 | crb_addr_transform(NIU); |
| 87 | crb_addr_transform(I2Q); |
| 88 | crb_addr_transform(EG); |
| 89 | crb_addr_transform(MN); |
| 90 | crb_addr_transform(MS); |
| 91 | crb_addr_transform(CAS2); |
| 92 | crb_addr_transform(CAS1); |
| 93 | crb_addr_transform(CAS0); |
| 94 | crb_addr_transform(CAM); |
| 95 | crb_addr_transform(C2C1); |
| 96 | crb_addr_transform(C2C0); |
| 97 | crb_addr_transform(SMB); |
| 98 | crb_addr_transform(OCM0); |
| 99 | crb_addr_transform(I2C0); |
| 100 | } |
| 101 | |
| 102 | void qlcnic_release_rx_buffers(struct qlcnic_adapter *adapter) |
| 103 | { |
| 104 | struct qlcnic_recv_context *recv_ctx; |
| 105 | struct qlcnic_host_rds_ring *rds_ring; |
| 106 | struct qlcnic_rx_buffer *rx_buf; |
| 107 | int i, ring; |
| 108 | |
| 109 | recv_ctx = &adapter->recv_ctx; |
| 110 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 111 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 112 | for (i = 0; i < rds_ring->num_desc; ++i) { |
| 113 | rx_buf = &(rds_ring->rx_buf_arr[i]); |
| 114 | if (rx_buf->state == QLCNIC_BUFFER_FREE) |
| 115 | continue; |
| 116 | pci_unmap_single(adapter->pdev, |
| 117 | rx_buf->dma, |
| 118 | rds_ring->dma_size, |
| 119 | PCI_DMA_FROMDEVICE); |
| 120 | if (rx_buf->skb != NULL) |
| 121 | dev_kfree_skb_any(rx_buf->skb); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void qlcnic_release_tx_buffers(struct qlcnic_adapter *adapter) |
| 127 | { |
| 128 | struct qlcnic_cmd_buffer *cmd_buf; |
| 129 | struct qlcnic_skb_frag *buffrag; |
| 130 | int i, j; |
| 131 | struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring; |
| 132 | |
| 133 | cmd_buf = tx_ring->cmd_buf_arr; |
| 134 | for (i = 0; i < tx_ring->num_desc; i++) { |
| 135 | buffrag = cmd_buf->frag_array; |
| 136 | if (buffrag->dma) { |
| 137 | pci_unmap_single(adapter->pdev, buffrag->dma, |
| 138 | buffrag->length, PCI_DMA_TODEVICE); |
| 139 | buffrag->dma = 0ULL; |
| 140 | } |
| 141 | for (j = 0; j < cmd_buf->frag_count; j++) { |
| 142 | buffrag++; |
| 143 | if (buffrag->dma) { |
| 144 | pci_unmap_page(adapter->pdev, buffrag->dma, |
| 145 | buffrag->length, |
| 146 | PCI_DMA_TODEVICE); |
| 147 | buffrag->dma = 0ULL; |
| 148 | } |
| 149 | } |
| 150 | if (cmd_buf->skb) { |
| 151 | dev_kfree_skb_any(cmd_buf->skb); |
| 152 | cmd_buf->skb = NULL; |
| 153 | } |
| 154 | cmd_buf++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void qlcnic_free_sw_resources(struct qlcnic_adapter *adapter) |
| 159 | { |
| 160 | struct qlcnic_recv_context *recv_ctx; |
| 161 | struct qlcnic_host_rds_ring *rds_ring; |
| 162 | struct qlcnic_host_tx_ring *tx_ring; |
| 163 | int ring; |
| 164 | |
| 165 | recv_ctx = &adapter->recv_ctx; |
| 166 | |
| 167 | if (recv_ctx->rds_rings == NULL) |
| 168 | goto skip_rds; |
| 169 | |
| 170 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 171 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 172 | vfree(rds_ring->rx_buf_arr); |
| 173 | rds_ring->rx_buf_arr = NULL; |
| 174 | } |
| 175 | kfree(recv_ctx->rds_rings); |
| 176 | |
| 177 | skip_rds: |
| 178 | if (adapter->tx_ring == NULL) |
| 179 | return; |
| 180 | |
| 181 | tx_ring = adapter->tx_ring; |
| 182 | vfree(tx_ring->cmd_buf_arr); |
| 183 | kfree(adapter->tx_ring); |
| 184 | } |
| 185 | |
| 186 | int qlcnic_alloc_sw_resources(struct qlcnic_adapter *adapter) |
| 187 | { |
| 188 | struct qlcnic_recv_context *recv_ctx; |
| 189 | struct qlcnic_host_rds_ring *rds_ring; |
| 190 | struct qlcnic_host_sds_ring *sds_ring; |
| 191 | struct qlcnic_host_tx_ring *tx_ring; |
| 192 | struct qlcnic_rx_buffer *rx_buf; |
| 193 | int ring, i, size; |
| 194 | |
| 195 | struct qlcnic_cmd_buffer *cmd_buf_arr; |
| 196 | struct net_device *netdev = adapter->netdev; |
| 197 | |
| 198 | size = sizeof(struct qlcnic_host_tx_ring); |
| 199 | tx_ring = kzalloc(size, GFP_KERNEL); |
| 200 | if (tx_ring == NULL) { |
| 201 | dev_err(&netdev->dev, "failed to allocate tx ring struct\n"); |
| 202 | return -ENOMEM; |
| 203 | } |
| 204 | adapter->tx_ring = tx_ring; |
| 205 | |
| 206 | tx_ring->num_desc = adapter->num_txd; |
| 207 | tx_ring->txq = netdev_get_tx_queue(netdev, 0); |
| 208 | |
| 209 | cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring)); |
| 210 | if (cmd_buf_arr == NULL) { |
| 211 | dev_err(&netdev->dev, "failed to allocate cmd buffer ring\n"); |
| 212 | return -ENOMEM; |
| 213 | } |
| 214 | memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring)); |
| 215 | tx_ring->cmd_buf_arr = cmd_buf_arr; |
| 216 | |
| 217 | recv_ctx = &adapter->recv_ctx; |
| 218 | |
| 219 | size = adapter->max_rds_rings * sizeof(struct qlcnic_host_rds_ring); |
| 220 | rds_ring = kzalloc(size, GFP_KERNEL); |
| 221 | if (rds_ring == NULL) { |
| 222 | dev_err(&netdev->dev, "failed to allocate rds ring struct\n"); |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | recv_ctx->rds_rings = rds_ring; |
| 226 | |
| 227 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 228 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 229 | switch (ring) { |
| 230 | case RCV_RING_NORMAL: |
| 231 | rds_ring->num_desc = adapter->num_rxd; |
| 232 | if (adapter->ahw.cut_through) { |
| 233 | rds_ring->dma_size = |
| 234 | QLCNIC_CT_DEFAULT_RX_BUF_LEN; |
| 235 | rds_ring->skb_size = |
| 236 | QLCNIC_CT_DEFAULT_RX_BUF_LEN; |
| 237 | } else { |
| 238 | rds_ring->dma_size = |
| 239 | QLCNIC_P3_RX_BUF_MAX_LEN; |
| 240 | rds_ring->skb_size = |
| 241 | rds_ring->dma_size + NET_IP_ALIGN; |
| 242 | } |
| 243 | break; |
| 244 | |
| 245 | case RCV_RING_JUMBO: |
| 246 | rds_ring->num_desc = adapter->num_jumbo_rxd; |
| 247 | rds_ring->dma_size = |
| 248 | QLCNIC_P3_RX_JUMBO_BUF_MAX_LEN; |
| 249 | |
| 250 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO) |
| 251 | rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA; |
| 252 | |
| 253 | rds_ring->skb_size = |
| 254 | rds_ring->dma_size + NET_IP_ALIGN; |
| 255 | break; |
| 256 | |
| 257 | case RCV_RING_LRO: |
| 258 | rds_ring->num_desc = adapter->num_lro_rxd; |
| 259 | rds_ring->dma_size = QLCNIC_RX_LRO_BUFFER_LENGTH; |
| 260 | rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN; |
| 261 | break; |
| 262 | |
| 263 | } |
| 264 | rds_ring->rx_buf_arr = (struct qlcnic_rx_buffer *) |
| 265 | vmalloc(RCV_BUFF_RINGSIZE(rds_ring)); |
| 266 | if (rds_ring->rx_buf_arr == NULL) { |
| 267 | dev_err(&netdev->dev, "Failed to allocate " |
| 268 | "rx buffer ring %d\n", ring); |
| 269 | goto err_out; |
| 270 | } |
| 271 | memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring)); |
| 272 | INIT_LIST_HEAD(&rds_ring->free_list); |
| 273 | /* |
| 274 | * Now go through all of them, set reference handles |
| 275 | * and put them in the queues. |
| 276 | */ |
| 277 | rx_buf = rds_ring->rx_buf_arr; |
| 278 | for (i = 0; i < rds_ring->num_desc; i++) { |
| 279 | list_add_tail(&rx_buf->list, |
| 280 | &rds_ring->free_list); |
| 281 | rx_buf->ref_handle = i; |
| 282 | rx_buf->state = QLCNIC_BUFFER_FREE; |
| 283 | rx_buf++; |
| 284 | } |
| 285 | spin_lock_init(&rds_ring->lock); |
| 286 | } |
| 287 | |
| 288 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 289 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 290 | sds_ring->irq = adapter->msix_entries[ring].vector; |
| 291 | sds_ring->adapter = adapter; |
| 292 | sds_ring->num_desc = adapter->num_rxd; |
| 293 | |
| 294 | for (i = 0; i < NUM_RCV_DESC_RINGS; i++) |
| 295 | INIT_LIST_HEAD(&sds_ring->free_list[i]); |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | |
| 300 | err_out: |
| 301 | qlcnic_free_sw_resources(adapter); |
| 302 | return -ENOMEM; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Utility to translate from internal Phantom CRB address |
| 307 | * to external PCI CRB address. |
| 308 | */ |
| 309 | static u32 qlcnic_decode_crb_addr(u32 addr) |
| 310 | { |
| 311 | int i; |
| 312 | u32 base_addr, offset, pci_base; |
| 313 | |
| 314 | crb_addr_transform_setup(); |
| 315 | |
| 316 | pci_base = QLCNIC_ADDR_ERROR; |
| 317 | base_addr = addr & 0xfff00000; |
| 318 | offset = addr & 0x000fffff; |
| 319 | |
| 320 | for (i = 0; i < QLCNIC_MAX_CRB_XFORM; i++) { |
| 321 | if (crb_addr_xform[i] == base_addr) { |
| 322 | pci_base = i << 20; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | if (pci_base == QLCNIC_ADDR_ERROR) |
| 327 | return pci_base; |
| 328 | else |
| 329 | return pci_base + offset; |
| 330 | } |
| 331 | |
| 332 | #define QLCNIC_MAX_ROM_WAIT_USEC 100 |
| 333 | |
| 334 | static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter) |
| 335 | { |
| 336 | long timeout = 0; |
| 337 | long done = 0; |
| 338 | |
| 339 | cond_resched(); |
| 340 | |
| 341 | while (done == 0) { |
| 342 | done = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_STATUS); |
| 343 | done &= 2; |
| 344 | if (++timeout >= QLCNIC_MAX_ROM_WAIT_USEC) { |
| 345 | dev_err(&adapter->pdev->dev, |
| 346 | "Timeout reached waiting for rom done"); |
| 347 | return -EIO; |
| 348 | } |
| 349 | udelay(1); |
| 350 | } |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int do_rom_fast_read(struct qlcnic_adapter *adapter, |
| 355 | int addr, int *valp) |
| 356 | { |
| 357 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ADDRESS, addr); |
| 358 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0); |
| 359 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 3); |
| 360 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_INSTR_OPCODE, 0xb); |
| 361 | if (qlcnic_wait_rom_done(adapter)) { |
| 362 | dev_err(&adapter->pdev->dev, "Error waiting for rom done\n"); |
| 363 | return -EIO; |
| 364 | } |
| 365 | /* reset abyte_cnt and dummy_byte_cnt */ |
| 366 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 0); |
| 367 | udelay(10); |
| 368 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0); |
| 369 | |
| 370 | *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr, |
| 375 | u8 *bytes, size_t size) |
| 376 | { |
| 377 | int addridx; |
| 378 | int ret = 0; |
| 379 | |
| 380 | for (addridx = addr; addridx < (addr + size); addridx += 4) { |
| 381 | int v; |
| 382 | ret = do_rom_fast_read(adapter, addridx, &v); |
| 383 | if (ret != 0) |
| 384 | break; |
| 385 | *(__le32 *)bytes = cpu_to_le32(v); |
| 386 | bytes += 4; |
| 387 | } |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | int |
| 393 | qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr, |
| 394 | u8 *bytes, size_t size) |
| 395 | { |
| 396 | int ret; |
| 397 | |
| 398 | ret = qlcnic_rom_lock(adapter); |
| 399 | if (ret < 0) |
| 400 | return ret; |
| 401 | |
| 402 | ret = do_rom_fast_read_words(adapter, addr, bytes, size); |
| 403 | |
| 404 | qlcnic_rom_unlock(adapter); |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, int addr, int *valp) |
| 409 | { |
| 410 | int ret; |
| 411 | |
| 412 | if (qlcnic_rom_lock(adapter) != 0) |
| 413 | return -EIO; |
| 414 | |
| 415 | ret = do_rom_fast_read(adapter, addr, valp); |
| 416 | qlcnic_rom_unlock(adapter); |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | int qlcnic_pinit_from_rom(struct qlcnic_adapter *adapter) |
| 421 | { |
| 422 | int addr, val; |
| 423 | int i, n, init_delay; |
| 424 | struct crb_addr_pair *buf; |
| 425 | unsigned offset; |
| 426 | u32 off; |
| 427 | struct pci_dev *pdev = adapter->pdev; |
| 428 | |
| 429 | /* resetall */ |
| 430 | qlcnic_rom_lock(adapter); |
| 431 | QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xffffffff); |
| 432 | qlcnic_rom_unlock(adapter); |
| 433 | |
| 434 | if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) || |
| 435 | qlcnic_rom_fast_read(adapter, 4, &n) != 0) { |
| 436 | dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n); |
| 437 | return -EIO; |
| 438 | } |
| 439 | offset = n & 0xffffU; |
| 440 | n = (n >> 16) & 0xffffU; |
| 441 | |
| 442 | if (n >= 1024) { |
| 443 | dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n"); |
| 444 | return -EIO; |
| 445 | } |
| 446 | |
| 447 | buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL); |
| 448 | if (buf == NULL) { |
| 449 | dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n"); |
| 450 | return -ENOMEM; |
| 451 | } |
| 452 | |
| 453 | for (i = 0; i < n; i++) { |
| 454 | if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 || |
| 455 | qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) { |
| 456 | kfree(buf); |
| 457 | return -EIO; |
| 458 | } |
| 459 | |
| 460 | buf[i].addr = addr; |
| 461 | buf[i].data = val; |
| 462 | } |
| 463 | |
| 464 | for (i = 0; i < n; i++) { |
| 465 | |
| 466 | off = qlcnic_decode_crb_addr(buf[i].addr); |
| 467 | if (off == QLCNIC_ADDR_ERROR) { |
| 468 | dev_err(&pdev->dev, "CRB init value out of range %x\n", |
| 469 | buf[i].addr); |
| 470 | continue; |
| 471 | } |
| 472 | off += QLCNIC_PCI_CRBSPACE; |
| 473 | |
| 474 | if (off & 1) |
| 475 | continue; |
| 476 | |
| 477 | /* skipping cold reboot MAGIC */ |
| 478 | if (off == QLCNIC_CAM_RAM(0x1fc)) |
| 479 | continue; |
| 480 | if (off == (QLCNIC_CRB_I2C0 + 0x1c)) |
| 481 | continue; |
| 482 | if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */ |
| 483 | continue; |
| 484 | if (off == (ROMUSB_GLB + 0xa8)) |
| 485 | continue; |
| 486 | if (off == (ROMUSB_GLB + 0xc8)) /* core clock */ |
| 487 | continue; |
| 488 | if (off == (ROMUSB_GLB + 0x24)) /* MN clock */ |
| 489 | continue; |
| 490 | if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */ |
| 491 | continue; |
| 492 | if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET) |
| 493 | continue; |
| 494 | /* skip the function enable register */ |
| 495 | if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION)) |
| 496 | continue; |
| 497 | if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION2)) |
| 498 | continue; |
| 499 | if ((off & 0x0ff00000) == QLCNIC_CRB_SMB) |
| 500 | continue; |
| 501 | |
| 502 | init_delay = 1; |
| 503 | /* After writing this register, HW needs time for CRB */ |
| 504 | /* to quiet down (else crb_window returns 0xffffffff) */ |
| 505 | if (off == QLCNIC_ROMUSB_GLB_SW_RESET) |
| 506 | init_delay = 1000; |
| 507 | |
| 508 | QLCWR32(adapter, off, buf[i].data); |
| 509 | |
| 510 | msleep(init_delay); |
| 511 | } |
| 512 | kfree(buf); |
| 513 | |
| 514 | /* p2dn replyCount */ |
| 515 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e); |
| 516 | /* disable_peg_cache 0 & 1*/ |
| 517 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8); |
| 518 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8); |
| 519 | |
| 520 | /* peg_clr_all */ |
| 521 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0); |
| 522 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0); |
| 523 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0); |
| 524 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0); |
| 525 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0); |
| 526 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0); |
| 527 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0); |
| 528 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int |
| 533 | qlcnic_has_mn(struct qlcnic_adapter *adapter) |
| 534 | { |
| 535 | u32 capability, flashed_ver; |
| 536 | capability = 0; |
| 537 | |
| 538 | qlcnic_rom_fast_read(adapter, |
| 539 | QLCNIC_FW_VERSION_OFFSET, (int *)&flashed_ver); |
| 540 | flashed_ver = QLCNIC_DECODE_VERSION(flashed_ver); |
| 541 | |
| 542 | if (flashed_ver >= QLCNIC_VERSION_CODE(4, 0, 220)) { |
| 543 | |
| 544 | capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY); |
| 545 | if (capability & QLCNIC_PEG_TUNE_MN_PRESENT) |
| 546 | return 1; |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static |
| 552 | struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section) |
| 553 | { |
| 554 | u32 i; |
| 555 | struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0]; |
| 556 | __le32 entries = cpu_to_le32(directory->num_entries); |
| 557 | |
| 558 | for (i = 0; i < entries; i++) { |
| 559 | |
| 560 | __le32 offs = cpu_to_le32(directory->findex) + |
| 561 | (i * cpu_to_le32(directory->entry_size)); |
| 562 | __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8)); |
| 563 | |
| 564 | if (tab_type == section) |
| 565 | return (struct uni_table_desc *) &unirom[offs]; |
| 566 | } |
| 567 | |
| 568 | return NULL; |
| 569 | } |
| 570 | |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 571 | #define FILEHEADER_SIZE (14 * 4) |
| 572 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 573 | static int |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 574 | qlcnic_validate_header(struct qlcnic_adapter *adapter) |
| 575 | { |
| 576 | const u8 *unirom = adapter->fw->data; |
| 577 | struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0]; |
| 578 | __le32 fw_file_size = adapter->fw->size; |
| 579 | __le32 entries; |
| 580 | __le32 entry_size; |
| 581 | __le32 tab_size; |
| 582 | |
| 583 | if (fw_file_size < FILEHEADER_SIZE) |
| 584 | return -EINVAL; |
| 585 | |
| 586 | entries = cpu_to_le32(directory->num_entries); |
| 587 | entry_size = cpu_to_le32(directory->entry_size); |
| 588 | tab_size = cpu_to_le32(directory->findex) + (entries * entry_size); |
| 589 | |
| 590 | if (fw_file_size < tab_size) |
| 591 | return -EINVAL; |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int |
| 597 | qlcnic_validate_bootld(struct qlcnic_adapter *adapter) |
| 598 | { |
| 599 | struct uni_table_desc *tab_desc; |
| 600 | struct uni_data_desc *descr; |
| 601 | const u8 *unirom = adapter->fw->data; |
| 602 | int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] + |
| 603 | QLCNIC_UNI_BOOTLD_IDX_OFF)); |
| 604 | __le32 offs; |
| 605 | __le32 tab_size; |
| 606 | __le32 data_size; |
| 607 | |
| 608 | tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD); |
| 609 | |
| 610 | if (!tab_desc) |
| 611 | return -EINVAL; |
| 612 | |
| 613 | tab_size = cpu_to_le32(tab_desc->findex) + |
| 614 | (cpu_to_le32(tab_desc->entry_size * (idx + 1))); |
| 615 | |
| 616 | if (adapter->fw->size < tab_size) |
| 617 | return -EINVAL; |
| 618 | |
| 619 | offs = cpu_to_le32(tab_desc->findex) + |
| 620 | (cpu_to_le32(tab_desc->entry_size) * (idx)); |
| 621 | descr = (struct uni_data_desc *)&unirom[offs]; |
| 622 | |
| 623 | data_size = descr->findex + cpu_to_le32(descr->size); |
| 624 | |
| 625 | if (adapter->fw->size < data_size) |
| 626 | return -EINVAL; |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int |
| 632 | qlcnic_validate_fw(struct qlcnic_adapter *adapter) |
| 633 | { |
| 634 | struct uni_table_desc *tab_desc; |
| 635 | struct uni_data_desc *descr; |
| 636 | const u8 *unirom = adapter->fw->data; |
| 637 | int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] + |
| 638 | QLCNIC_UNI_FIRMWARE_IDX_OFF)); |
| 639 | __le32 offs; |
| 640 | __le32 tab_size; |
| 641 | __le32 data_size; |
| 642 | |
| 643 | tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW); |
| 644 | |
| 645 | if (!tab_desc) |
| 646 | return -EINVAL; |
| 647 | |
| 648 | tab_size = cpu_to_le32(tab_desc->findex) + |
| 649 | (cpu_to_le32(tab_desc->entry_size * (idx + 1))); |
| 650 | |
| 651 | if (adapter->fw->size < tab_size) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | offs = cpu_to_le32(tab_desc->findex) + |
| 655 | (cpu_to_le32(tab_desc->entry_size) * (idx)); |
| 656 | descr = (struct uni_data_desc *)&unirom[offs]; |
| 657 | data_size = descr->findex + cpu_to_le32(descr->size); |
| 658 | |
| 659 | if (adapter->fw->size < data_size) |
| 660 | return -EINVAL; |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int |
| 666 | qlcnic_validate_product_offs(struct qlcnic_adapter *adapter) |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 667 | { |
| 668 | struct uni_table_desc *ptab_descr; |
| 669 | const u8 *unirom = adapter->fw->data; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 670 | int mn_present = qlcnic_has_mn(adapter); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 671 | __le32 entries; |
| 672 | __le32 entry_size; |
| 673 | __le32 tab_size; |
| 674 | u32 i; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 675 | |
| 676 | ptab_descr = qlcnic_get_table_desc(unirom, |
| 677 | QLCNIC_UNI_DIR_SECT_PRODUCT_TBL); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 678 | if (!ptab_descr) |
| 679 | return -EINVAL; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 680 | |
| 681 | entries = cpu_to_le32(ptab_descr->num_entries); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 682 | entry_size = cpu_to_le32(ptab_descr->entry_size); |
| 683 | tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size); |
| 684 | |
| 685 | if (adapter->fw->size < tab_size) |
| 686 | return -EINVAL; |
| 687 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 688 | nomn: |
| 689 | for (i = 0; i < entries; i++) { |
| 690 | |
| 691 | __le32 flags, file_chiprev, offs; |
| 692 | u8 chiprev = adapter->ahw.revision_id; |
| 693 | u32 flagbit; |
| 694 | |
| 695 | offs = cpu_to_le32(ptab_descr->findex) + |
| 696 | (i * cpu_to_le32(ptab_descr->entry_size)); |
| 697 | flags = cpu_to_le32(*((int *)&unirom[offs] + |
| 698 | QLCNIC_UNI_FLAGS_OFF)); |
| 699 | file_chiprev = cpu_to_le32(*((int *)&unirom[offs] + |
| 700 | QLCNIC_UNI_CHIP_REV_OFF)); |
| 701 | |
| 702 | flagbit = mn_present ? 1 : 2; |
| 703 | |
| 704 | if ((chiprev == file_chiprev) && |
| 705 | ((1ULL << flagbit) & flags)) { |
| 706 | adapter->file_prd_off = offs; |
| 707 | return 0; |
| 708 | } |
| 709 | } |
| 710 | if (mn_present) { |
| 711 | mn_present = 0; |
| 712 | goto nomn; |
| 713 | } |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 714 | return -EINVAL; |
| 715 | } |
| 716 | |
| 717 | static int |
| 718 | qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter) |
| 719 | { |
| 720 | if (qlcnic_validate_header(adapter)) { |
| 721 | dev_err(&adapter->pdev->dev, |
| 722 | "unified image: header validation failed\n"); |
| 723 | return -EINVAL; |
| 724 | } |
| 725 | |
| 726 | if (qlcnic_validate_product_offs(adapter)) { |
| 727 | dev_err(&adapter->pdev->dev, |
| 728 | "unified image: product validation failed\n"); |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | |
| 732 | if (qlcnic_validate_bootld(adapter)) { |
| 733 | dev_err(&adapter->pdev->dev, |
| 734 | "unified image: bootld validation failed\n"); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
| 738 | if (qlcnic_validate_fw(adapter)) { |
| 739 | dev_err(&adapter->pdev->dev, |
| 740 | "unified image: firmware validation failed\n"); |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | |
| 744 | return 0; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static |
| 748 | struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter, |
| 749 | u32 section, u32 idx_offset) |
| 750 | { |
| 751 | const u8 *unirom = adapter->fw->data; |
| 752 | int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] + |
| 753 | idx_offset)); |
| 754 | struct uni_table_desc *tab_desc; |
| 755 | __le32 offs; |
| 756 | |
| 757 | tab_desc = qlcnic_get_table_desc(unirom, section); |
| 758 | |
| 759 | if (tab_desc == NULL) |
| 760 | return NULL; |
| 761 | |
| 762 | offs = cpu_to_le32(tab_desc->findex) + |
| 763 | (cpu_to_le32(tab_desc->entry_size) * idx); |
| 764 | |
| 765 | return (struct uni_data_desc *)&unirom[offs]; |
| 766 | } |
| 767 | |
| 768 | static u8 * |
| 769 | qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter) |
| 770 | { |
| 771 | u32 offs = QLCNIC_BOOTLD_START; |
| 772 | |
| 773 | if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE) |
| 774 | offs = cpu_to_le32((qlcnic_get_data_desc(adapter, |
| 775 | QLCNIC_UNI_DIR_SECT_BOOTLD, |
| 776 | QLCNIC_UNI_BOOTLD_IDX_OFF))->findex); |
| 777 | |
| 778 | return (u8 *)&adapter->fw->data[offs]; |
| 779 | } |
| 780 | |
| 781 | static u8 * |
| 782 | qlcnic_get_fw_offs(struct qlcnic_adapter *adapter) |
| 783 | { |
| 784 | u32 offs = QLCNIC_IMAGE_START; |
| 785 | |
| 786 | if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE) |
| 787 | offs = cpu_to_le32((qlcnic_get_data_desc(adapter, |
| 788 | QLCNIC_UNI_DIR_SECT_FW, |
| 789 | QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex); |
| 790 | |
| 791 | return (u8 *)&adapter->fw->data[offs]; |
| 792 | } |
| 793 | |
| 794 | static __le32 |
| 795 | qlcnic_get_fw_size(struct qlcnic_adapter *adapter) |
| 796 | { |
| 797 | if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE) |
| 798 | return cpu_to_le32((qlcnic_get_data_desc(adapter, |
| 799 | QLCNIC_UNI_DIR_SECT_FW, |
| 800 | QLCNIC_UNI_FIRMWARE_IDX_OFF))->size); |
| 801 | else |
| 802 | return cpu_to_le32( |
| 803 | *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]); |
| 804 | } |
| 805 | |
| 806 | static __le32 |
| 807 | qlcnic_get_fw_version(struct qlcnic_adapter *adapter) |
| 808 | { |
| 809 | struct uni_data_desc *fw_data_desc; |
| 810 | const struct firmware *fw = adapter->fw; |
| 811 | __le32 major, minor, sub; |
| 812 | const u8 *ver_str; |
| 813 | int i, ret; |
| 814 | |
| 815 | if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE) |
| 816 | return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]); |
| 817 | |
| 818 | fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW, |
| 819 | QLCNIC_UNI_FIRMWARE_IDX_OFF); |
| 820 | ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) + |
| 821 | cpu_to_le32(fw_data_desc->size) - 17; |
| 822 | |
| 823 | for (i = 0; i < 12; i++) { |
| 824 | if (!strncmp(&ver_str[i], "REV=", 4)) { |
| 825 | ret = sscanf(&ver_str[i+4], "%u.%u.%u ", |
| 826 | &major, &minor, &sub); |
| 827 | if (ret != 3) |
| 828 | return 0; |
| 829 | else |
| 830 | return major + (minor << 8) + (sub << 16); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | static __le32 |
| 838 | qlcnic_get_bios_version(struct qlcnic_adapter *adapter) |
| 839 | { |
| 840 | const struct firmware *fw = adapter->fw; |
| 841 | __le32 bios_ver, prd_off = adapter->file_prd_off; |
| 842 | |
| 843 | if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE) |
| 844 | return cpu_to_le32( |
| 845 | *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]); |
| 846 | |
| 847 | bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off]) |
| 848 | + QLCNIC_UNI_BIOS_VERSION_OFF)); |
| 849 | |
Amit Kumar Salecha | addd5ab | 2010-03-08 00:14:49 +0000 | [diff] [blame] | 850 | return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | int |
| 854 | qlcnic_need_fw_reset(struct qlcnic_adapter *adapter) |
| 855 | { |
| 856 | u32 count, old_count; |
| 857 | u32 val, version, major, minor, build; |
| 858 | int i, timeout; |
| 859 | |
| 860 | if (adapter->need_fw_reset) |
| 861 | return 1; |
| 862 | |
| 863 | /* last attempt had failed */ |
| 864 | if (QLCRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED) |
| 865 | return 1; |
| 866 | |
| 867 | old_count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER); |
| 868 | |
| 869 | for (i = 0; i < 10; i++) { |
| 870 | |
| 871 | timeout = msleep_interruptible(200); |
| 872 | if (timeout) { |
| 873 | QLCWR32(adapter, CRB_CMDPEG_STATE, |
| 874 | PHAN_INITIALIZE_FAILED); |
| 875 | return -EINTR; |
| 876 | } |
| 877 | |
| 878 | count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER); |
| 879 | if (count != old_count) |
| 880 | break; |
| 881 | } |
| 882 | |
| 883 | /* firmware is dead */ |
| 884 | if (count == old_count) |
| 885 | return 1; |
| 886 | |
| 887 | /* check if we have got newer or different file firmware */ |
| 888 | if (adapter->fw) { |
| 889 | |
| 890 | val = qlcnic_get_fw_version(adapter); |
| 891 | |
| 892 | version = QLCNIC_DECODE_VERSION(val); |
| 893 | |
| 894 | major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR); |
| 895 | minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR); |
| 896 | build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB); |
| 897 | |
| 898 | if (version > QLCNIC_VERSION_CODE(major, minor, build)) |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static const char *fw_name[] = { |
| 906 | QLCNIC_UNIFIED_ROMIMAGE_NAME, |
| 907 | QLCNIC_FLASH_ROMIMAGE_NAME, |
| 908 | }; |
| 909 | |
| 910 | int |
| 911 | qlcnic_load_firmware(struct qlcnic_adapter *adapter) |
| 912 | { |
| 913 | u64 *ptr64; |
| 914 | u32 i, flashaddr, size; |
| 915 | const struct firmware *fw = adapter->fw; |
| 916 | struct pci_dev *pdev = adapter->pdev; |
| 917 | |
| 918 | dev_info(&pdev->dev, "loading firmware from %s\n", |
| 919 | fw_name[adapter->fw_type]); |
| 920 | |
| 921 | if (fw) { |
| 922 | __le64 data; |
| 923 | |
| 924 | size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8; |
| 925 | |
| 926 | ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter); |
| 927 | flashaddr = QLCNIC_BOOTLD_START; |
| 928 | |
| 929 | for (i = 0; i < size; i++) { |
| 930 | data = cpu_to_le64(ptr64[i]); |
| 931 | |
| 932 | if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data)) |
| 933 | return -EIO; |
| 934 | |
| 935 | flashaddr += 8; |
| 936 | } |
| 937 | |
| 938 | size = (__force u32)qlcnic_get_fw_size(adapter) / 8; |
| 939 | |
| 940 | ptr64 = (u64 *)qlcnic_get_fw_offs(adapter); |
| 941 | flashaddr = QLCNIC_IMAGE_START; |
| 942 | |
| 943 | for (i = 0; i < size; i++) { |
| 944 | data = cpu_to_le64(ptr64[i]); |
| 945 | |
| 946 | if (qlcnic_pci_mem_write_2M(adapter, |
| 947 | flashaddr, data)) |
| 948 | return -EIO; |
| 949 | |
| 950 | flashaddr += 8; |
| 951 | } |
Amit Kumar Salecha | 0bc92b5 | 2010-04-01 19:01:28 +0000 | [diff] [blame^] | 952 | |
| 953 | size = (__force u32)qlcnic_get_fw_size(adapter) % 8; |
| 954 | if (size) { |
| 955 | data = cpu_to_le64(ptr64[i]); |
| 956 | |
| 957 | if (qlcnic_pci_mem_write_2M(adapter, |
| 958 | flashaddr, data)) |
| 959 | return -EIO; |
| 960 | } |
| 961 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 962 | } else { |
| 963 | u64 data; |
| 964 | u32 hi, lo; |
| 965 | |
| 966 | size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8; |
| 967 | flashaddr = QLCNIC_BOOTLD_START; |
| 968 | |
| 969 | for (i = 0; i < size; i++) { |
| 970 | if (qlcnic_rom_fast_read(adapter, |
| 971 | flashaddr, (int *)&lo) != 0) |
| 972 | return -EIO; |
| 973 | if (qlcnic_rom_fast_read(adapter, |
| 974 | flashaddr + 4, (int *)&hi) != 0) |
| 975 | return -EIO; |
| 976 | |
| 977 | data = (((u64)hi << 32) | lo); |
| 978 | |
| 979 | if (qlcnic_pci_mem_write_2M(adapter, |
| 980 | flashaddr, data)) |
| 981 | return -EIO; |
| 982 | |
| 983 | flashaddr += 8; |
| 984 | } |
| 985 | } |
| 986 | msleep(1); |
| 987 | |
| 988 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020); |
| 989 | QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e); |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | static int |
| 994 | qlcnic_validate_firmware(struct qlcnic_adapter *adapter) |
| 995 | { |
| 996 | __le32 val; |
| 997 | u32 ver, min_ver, bios, min_size; |
| 998 | struct pci_dev *pdev = adapter->pdev; |
| 999 | const struct firmware *fw = adapter->fw; |
| 1000 | u8 fw_type = adapter->fw_type; |
| 1001 | |
| 1002 | if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) { |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 1003 | if (qlcnic_validate_unified_romimage(adapter)) |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1004 | return -EINVAL; |
| 1005 | |
| 1006 | min_size = QLCNIC_UNI_FW_MIN_SIZE; |
| 1007 | } else { |
| 1008 | val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]); |
| 1009 | if ((__force u32)val != QLCNIC_BDINFO_MAGIC) |
| 1010 | return -EINVAL; |
| 1011 | |
| 1012 | min_size = QLCNIC_FW_MIN_SIZE; |
| 1013 | } |
| 1014 | |
| 1015 | if (fw->size < min_size) |
| 1016 | return -EINVAL; |
| 1017 | |
| 1018 | val = qlcnic_get_fw_version(adapter); |
| 1019 | |
| 1020 | min_ver = QLCNIC_VERSION_CODE(4, 0, 216); |
| 1021 | |
| 1022 | ver = QLCNIC_DECODE_VERSION(val); |
| 1023 | |
| 1024 | if ((_major(ver) > _QLCNIC_LINUX_MAJOR) || (ver < min_ver)) { |
| 1025 | dev_err(&pdev->dev, |
| 1026 | "%s: firmware version %d.%d.%d unsupported\n", |
| 1027 | fw_name[fw_type], _major(ver), _minor(ver), _build(ver)); |
| 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | |
| 1031 | val = qlcnic_get_bios_version(adapter); |
| 1032 | qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios); |
| 1033 | if ((__force u32)val != bios) { |
| 1034 | dev_err(&pdev->dev, "%s: firmware bios is incompatible\n", |
| 1035 | fw_name[fw_type]); |
| 1036 | return -EINVAL; |
| 1037 | } |
| 1038 | |
| 1039 | /* check if flashed firmware is newer */ |
| 1040 | if (qlcnic_rom_fast_read(adapter, |
| 1041 | QLCNIC_FW_VERSION_OFFSET, (int *)&val)) |
| 1042 | return -EIO; |
| 1043 | |
| 1044 | val = QLCNIC_DECODE_VERSION(val); |
| 1045 | if (val > ver) { |
| 1046 | dev_info(&pdev->dev, "%s: firmware is older than flash\n", |
| 1047 | fw_name[fw_type]); |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | |
| 1051 | QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC); |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter) |
| 1057 | { |
| 1058 | u8 fw_type; |
| 1059 | |
| 1060 | switch (adapter->fw_type) { |
| 1061 | case QLCNIC_UNKNOWN_ROMIMAGE: |
| 1062 | fw_type = QLCNIC_UNIFIED_ROMIMAGE; |
| 1063 | break; |
| 1064 | |
| 1065 | case QLCNIC_UNIFIED_ROMIMAGE: |
| 1066 | default: |
| 1067 | fw_type = QLCNIC_FLASH_ROMIMAGE; |
| 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | adapter->fw_type = fw_type; |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | void qlcnic_request_firmware(struct qlcnic_adapter *adapter) |
| 1077 | { |
| 1078 | struct pci_dev *pdev = adapter->pdev; |
| 1079 | int rc; |
| 1080 | |
| 1081 | adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE; |
| 1082 | |
| 1083 | next: |
| 1084 | qlcnic_get_next_fwtype(adapter); |
| 1085 | |
| 1086 | if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) { |
| 1087 | adapter->fw = NULL; |
| 1088 | } else { |
| 1089 | rc = request_firmware(&adapter->fw, |
| 1090 | fw_name[adapter->fw_type], &pdev->dev); |
| 1091 | if (rc != 0) |
| 1092 | goto next; |
| 1093 | |
| 1094 | rc = qlcnic_validate_firmware(adapter); |
| 1095 | if (rc != 0) { |
| 1096 | release_firmware(adapter->fw); |
| 1097 | msleep(1); |
| 1098 | goto next; |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | void |
| 1105 | qlcnic_release_firmware(struct qlcnic_adapter *adapter) |
| 1106 | { |
| 1107 | if (adapter->fw) |
| 1108 | release_firmware(adapter->fw); |
| 1109 | adapter->fw = NULL; |
| 1110 | } |
| 1111 | |
| 1112 | int qlcnic_phantom_init(struct qlcnic_adapter *adapter) |
| 1113 | { |
| 1114 | u32 val; |
| 1115 | int retries = 60; |
| 1116 | |
| 1117 | do { |
| 1118 | val = QLCRD32(adapter, CRB_CMDPEG_STATE); |
| 1119 | |
| 1120 | switch (val) { |
| 1121 | case PHAN_INITIALIZE_COMPLETE: |
| 1122 | case PHAN_INITIALIZE_ACK: |
| 1123 | return 0; |
| 1124 | case PHAN_INITIALIZE_FAILED: |
| 1125 | goto out_err; |
| 1126 | default: |
| 1127 | break; |
| 1128 | } |
| 1129 | |
| 1130 | msleep(500); |
| 1131 | |
| 1132 | } while (--retries); |
| 1133 | |
| 1134 | QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED); |
| 1135 | |
| 1136 | out_err: |
| 1137 | dev_err(&adapter->pdev->dev, "firmware init failed\n"); |
| 1138 | return -EIO; |
| 1139 | } |
| 1140 | |
| 1141 | static int |
| 1142 | qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter) |
| 1143 | { |
| 1144 | u32 val; |
| 1145 | int retries = 2000; |
| 1146 | |
| 1147 | do { |
| 1148 | val = QLCRD32(adapter, CRB_RCVPEG_STATE); |
| 1149 | |
| 1150 | if (val == PHAN_PEG_RCV_INITIALIZED) |
| 1151 | return 0; |
| 1152 | |
| 1153 | msleep(10); |
| 1154 | |
| 1155 | } while (--retries); |
| 1156 | |
| 1157 | if (!retries) { |
| 1158 | dev_err(&adapter->pdev->dev, "Receive Peg initialization not " |
| 1159 | "complete, state: 0x%x.\n", val); |
| 1160 | return -EIO; |
| 1161 | } |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | int qlcnic_init_firmware(struct qlcnic_adapter *adapter) |
| 1167 | { |
| 1168 | int err; |
| 1169 | |
| 1170 | err = qlcnic_receive_peg_ready(adapter); |
| 1171 | if (err) |
| 1172 | return err; |
| 1173 | |
| 1174 | QLCWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT); |
| 1175 | QLCWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC); |
| 1176 | QLCWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE); |
| 1177 | QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK); |
| 1178 | |
| 1179 | return err; |
| 1180 | } |
| 1181 | |
| 1182 | static void |
| 1183 | qlcnic_handle_linkevent(struct qlcnic_adapter *adapter, |
| 1184 | struct qlcnic_fw_msg *msg) |
| 1185 | { |
| 1186 | u32 cable_OUI; |
| 1187 | u16 cable_len; |
| 1188 | u16 link_speed; |
| 1189 | u8 link_status, module, duplex, autoneg; |
| 1190 | struct net_device *netdev = adapter->netdev; |
| 1191 | |
| 1192 | adapter->has_link_events = 1; |
| 1193 | |
| 1194 | cable_OUI = msg->body[1] & 0xffffffff; |
| 1195 | cable_len = (msg->body[1] >> 32) & 0xffff; |
| 1196 | link_speed = (msg->body[1] >> 48) & 0xffff; |
| 1197 | |
| 1198 | link_status = msg->body[2] & 0xff; |
| 1199 | duplex = (msg->body[2] >> 16) & 0xff; |
| 1200 | autoneg = (msg->body[2] >> 24) & 0xff; |
| 1201 | |
| 1202 | module = (msg->body[2] >> 8) & 0xff; |
| 1203 | if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) |
| 1204 | dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, " |
| 1205 | "length %d\n", cable_OUI, cable_len); |
| 1206 | else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) |
| 1207 | dev_info(&netdev->dev, "unsupported cable length %d\n", |
| 1208 | cable_len); |
| 1209 | |
| 1210 | qlcnic_advert_link_change(adapter, link_status); |
| 1211 | |
| 1212 | if (duplex == LINKEVENT_FULL_DUPLEX) |
| 1213 | adapter->link_duplex = DUPLEX_FULL; |
| 1214 | else |
| 1215 | adapter->link_duplex = DUPLEX_HALF; |
| 1216 | |
| 1217 | adapter->module_type = module; |
| 1218 | adapter->link_autoneg = autoneg; |
| 1219 | adapter->link_speed = link_speed; |
| 1220 | } |
| 1221 | |
| 1222 | static void |
| 1223 | qlcnic_handle_fw_message(int desc_cnt, int index, |
| 1224 | struct qlcnic_host_sds_ring *sds_ring) |
| 1225 | { |
| 1226 | struct qlcnic_fw_msg msg; |
| 1227 | struct status_desc *desc; |
| 1228 | int i = 0, opcode; |
| 1229 | |
| 1230 | while (desc_cnt > 0 && i < 8) { |
| 1231 | desc = &sds_ring->desc_head[index]; |
| 1232 | msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]); |
| 1233 | msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]); |
| 1234 | |
| 1235 | index = get_next_index(index, sds_ring->num_desc); |
| 1236 | desc_cnt--; |
| 1237 | } |
| 1238 | |
| 1239 | opcode = qlcnic_get_nic_msg_opcode(msg.body[0]); |
| 1240 | switch (opcode) { |
| 1241 | case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE: |
| 1242 | qlcnic_handle_linkevent(sds_ring->adapter, &msg); |
| 1243 | break; |
| 1244 | default: |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | static int |
| 1250 | qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter, |
| 1251 | struct qlcnic_host_rds_ring *rds_ring, |
| 1252 | struct qlcnic_rx_buffer *buffer) |
| 1253 | { |
| 1254 | struct sk_buff *skb; |
| 1255 | dma_addr_t dma; |
| 1256 | struct pci_dev *pdev = adapter->pdev; |
| 1257 | |
| 1258 | buffer->skb = dev_alloc_skb(rds_ring->skb_size); |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1259 | if (!buffer->skb) { |
| 1260 | adapter->stats.skb_alloc_failure++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1261 | return -ENOMEM; |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1262 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1263 | |
| 1264 | skb = buffer->skb; |
| 1265 | |
| 1266 | if (!adapter->ahw.cut_through) |
| 1267 | skb_reserve(skb, 2); |
| 1268 | |
| 1269 | dma = pci_map_single(pdev, skb->data, |
| 1270 | rds_ring->dma_size, PCI_DMA_FROMDEVICE); |
| 1271 | |
| 1272 | if (pci_dma_mapping_error(pdev, dma)) { |
| 1273 | dev_kfree_skb_any(skb); |
| 1274 | buffer->skb = NULL; |
| 1275 | return -ENOMEM; |
| 1276 | } |
| 1277 | |
| 1278 | buffer->skb = skb; |
| 1279 | buffer->dma = dma; |
| 1280 | buffer->state = QLCNIC_BUFFER_BUSY; |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter, |
| 1286 | struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum) |
| 1287 | { |
| 1288 | struct qlcnic_rx_buffer *buffer; |
| 1289 | struct sk_buff *skb; |
| 1290 | |
| 1291 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1292 | |
| 1293 | pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size, |
| 1294 | PCI_DMA_FROMDEVICE); |
| 1295 | |
| 1296 | skb = buffer->skb; |
| 1297 | if (!skb) |
| 1298 | goto no_skb; |
| 1299 | |
| 1300 | if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) { |
| 1301 | adapter->stats.csummed++; |
| 1302 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1303 | } else { |
| 1304 | skb->ip_summed = CHECKSUM_NONE; |
| 1305 | } |
| 1306 | |
| 1307 | skb->dev = adapter->netdev; |
| 1308 | |
| 1309 | buffer->skb = NULL; |
| 1310 | no_skb: |
| 1311 | buffer->state = QLCNIC_BUFFER_FREE; |
| 1312 | return skb; |
| 1313 | } |
| 1314 | |
| 1315 | static struct qlcnic_rx_buffer * |
| 1316 | qlcnic_process_rcv(struct qlcnic_adapter *adapter, |
| 1317 | struct qlcnic_host_sds_ring *sds_ring, |
| 1318 | int ring, u64 sts_data0) |
| 1319 | { |
| 1320 | struct net_device *netdev = adapter->netdev; |
| 1321 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 1322 | struct qlcnic_rx_buffer *buffer; |
| 1323 | struct sk_buff *skb; |
| 1324 | struct qlcnic_host_rds_ring *rds_ring; |
| 1325 | int index, length, cksum, pkt_offset; |
| 1326 | |
| 1327 | if (unlikely(ring >= adapter->max_rds_rings)) |
| 1328 | return NULL; |
| 1329 | |
| 1330 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 1331 | |
| 1332 | index = qlcnic_get_sts_refhandle(sts_data0); |
| 1333 | if (unlikely(index >= rds_ring->num_desc)) |
| 1334 | return NULL; |
| 1335 | |
| 1336 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1337 | |
| 1338 | length = qlcnic_get_sts_totallength(sts_data0); |
| 1339 | cksum = qlcnic_get_sts_status(sts_data0); |
| 1340 | pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0); |
| 1341 | |
| 1342 | skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum); |
| 1343 | if (!skb) |
| 1344 | return buffer; |
| 1345 | |
| 1346 | if (length > rds_ring->skb_size) |
| 1347 | skb_put(skb, rds_ring->skb_size); |
| 1348 | else |
| 1349 | skb_put(skb, length); |
| 1350 | |
| 1351 | if (pkt_offset) |
| 1352 | skb_pull(skb, pkt_offset); |
| 1353 | |
| 1354 | skb->truesize = skb->len + sizeof(struct sk_buff); |
| 1355 | skb->protocol = eth_type_trans(skb, netdev); |
| 1356 | |
| 1357 | napi_gro_receive(&sds_ring->napi, skb); |
| 1358 | |
| 1359 | adapter->stats.rx_pkts++; |
| 1360 | adapter->stats.rxbytes += length; |
| 1361 | |
| 1362 | return buffer; |
| 1363 | } |
| 1364 | |
| 1365 | #define QLC_TCP_HDR_SIZE 20 |
| 1366 | #define QLC_TCP_TS_OPTION_SIZE 12 |
| 1367 | #define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE) |
| 1368 | |
| 1369 | static struct qlcnic_rx_buffer * |
| 1370 | qlcnic_process_lro(struct qlcnic_adapter *adapter, |
| 1371 | struct qlcnic_host_sds_ring *sds_ring, |
| 1372 | int ring, u64 sts_data0, u64 sts_data1) |
| 1373 | { |
| 1374 | struct net_device *netdev = adapter->netdev; |
| 1375 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 1376 | struct qlcnic_rx_buffer *buffer; |
| 1377 | struct sk_buff *skb; |
| 1378 | struct qlcnic_host_rds_ring *rds_ring; |
| 1379 | struct iphdr *iph; |
| 1380 | struct tcphdr *th; |
| 1381 | bool push, timestamp; |
| 1382 | int l2_hdr_offset, l4_hdr_offset; |
| 1383 | int index; |
| 1384 | u16 lro_length, length, data_offset; |
| 1385 | u32 seq_number; |
| 1386 | |
| 1387 | if (unlikely(ring > adapter->max_rds_rings)) |
| 1388 | return NULL; |
| 1389 | |
| 1390 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 1391 | |
| 1392 | index = qlcnic_get_lro_sts_refhandle(sts_data0); |
| 1393 | if (unlikely(index > rds_ring->num_desc)) |
| 1394 | return NULL; |
| 1395 | |
| 1396 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1397 | |
| 1398 | timestamp = qlcnic_get_lro_sts_timestamp(sts_data0); |
| 1399 | lro_length = qlcnic_get_lro_sts_length(sts_data0); |
| 1400 | l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0); |
| 1401 | l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0); |
| 1402 | push = qlcnic_get_lro_sts_push_flag(sts_data0); |
| 1403 | seq_number = qlcnic_get_lro_sts_seq_number(sts_data1); |
| 1404 | |
| 1405 | skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK); |
| 1406 | if (!skb) |
| 1407 | return buffer; |
| 1408 | |
| 1409 | if (timestamp) |
| 1410 | data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE; |
| 1411 | else |
| 1412 | data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE; |
| 1413 | |
| 1414 | skb_put(skb, lro_length + data_offset); |
| 1415 | |
| 1416 | skb->truesize = skb->len + sizeof(struct sk_buff) + skb_headroom(skb); |
| 1417 | |
| 1418 | skb_pull(skb, l2_hdr_offset); |
| 1419 | skb->protocol = eth_type_trans(skb, netdev); |
| 1420 | |
| 1421 | iph = (struct iphdr *)skb->data; |
| 1422 | th = (struct tcphdr *)(skb->data + (iph->ihl << 2)); |
| 1423 | |
| 1424 | length = (iph->ihl << 2) + (th->doff << 2) + lro_length; |
| 1425 | iph->tot_len = htons(length); |
| 1426 | iph->check = 0; |
| 1427 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
| 1428 | th->psh = push; |
| 1429 | th->seq = htonl(seq_number); |
| 1430 | |
| 1431 | length = skb->len; |
| 1432 | |
| 1433 | netif_receive_skb(skb); |
| 1434 | |
| 1435 | adapter->stats.lro_pkts++; |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1436 | adapter->stats.lrobytes += length; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1437 | |
| 1438 | return buffer; |
| 1439 | } |
| 1440 | |
| 1441 | int |
| 1442 | qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max) |
| 1443 | { |
| 1444 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1445 | struct list_head *cur; |
| 1446 | struct status_desc *desc; |
| 1447 | struct qlcnic_rx_buffer *rxbuf; |
| 1448 | u64 sts_data0, sts_data1; |
| 1449 | |
| 1450 | int count = 0; |
| 1451 | int opcode, ring, desc_cnt; |
| 1452 | u32 consumer = sds_ring->consumer; |
| 1453 | |
| 1454 | while (count < max) { |
| 1455 | desc = &sds_ring->desc_head[consumer]; |
| 1456 | sts_data0 = le64_to_cpu(desc->status_desc_data[0]); |
| 1457 | |
| 1458 | if (!(sts_data0 & STATUS_OWNER_HOST)) |
| 1459 | break; |
| 1460 | |
| 1461 | desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0); |
| 1462 | opcode = qlcnic_get_sts_opcode(sts_data0); |
| 1463 | |
| 1464 | switch (opcode) { |
| 1465 | case QLCNIC_RXPKT_DESC: |
| 1466 | case QLCNIC_OLD_RXPKT_DESC: |
| 1467 | case QLCNIC_SYN_OFFLOAD: |
| 1468 | ring = qlcnic_get_sts_type(sts_data0); |
| 1469 | rxbuf = qlcnic_process_rcv(adapter, sds_ring, |
| 1470 | ring, sts_data0); |
| 1471 | break; |
| 1472 | case QLCNIC_LRO_DESC: |
| 1473 | ring = qlcnic_get_lro_sts_type(sts_data0); |
| 1474 | sts_data1 = le64_to_cpu(desc->status_desc_data[1]); |
| 1475 | rxbuf = qlcnic_process_lro(adapter, sds_ring, |
| 1476 | ring, sts_data0, sts_data1); |
| 1477 | break; |
| 1478 | case QLCNIC_RESPONSE_DESC: |
| 1479 | qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring); |
| 1480 | default: |
| 1481 | goto skip; |
| 1482 | } |
| 1483 | |
| 1484 | WARN_ON(desc_cnt > 1); |
| 1485 | |
| 1486 | if (rxbuf) |
| 1487 | list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]); |
| 1488 | |
| 1489 | skip: |
| 1490 | for (; desc_cnt > 0; desc_cnt--) { |
| 1491 | desc = &sds_ring->desc_head[consumer]; |
| 1492 | desc->status_desc_data[0] = |
| 1493 | cpu_to_le64(STATUS_OWNER_PHANTOM); |
| 1494 | consumer = get_next_index(consumer, sds_ring->num_desc); |
| 1495 | } |
| 1496 | count++; |
| 1497 | } |
| 1498 | |
| 1499 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 1500 | struct qlcnic_host_rds_ring *rds_ring = |
| 1501 | &adapter->recv_ctx.rds_rings[ring]; |
| 1502 | |
| 1503 | if (!list_empty(&sds_ring->free_list[ring])) { |
| 1504 | list_for_each(cur, &sds_ring->free_list[ring]) { |
| 1505 | rxbuf = list_entry(cur, |
| 1506 | struct qlcnic_rx_buffer, list); |
| 1507 | qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf); |
| 1508 | } |
| 1509 | spin_lock(&rds_ring->lock); |
| 1510 | list_splice_tail_init(&sds_ring->free_list[ring], |
| 1511 | &rds_ring->free_list); |
| 1512 | spin_unlock(&rds_ring->lock); |
| 1513 | } |
| 1514 | |
| 1515 | qlcnic_post_rx_buffers_nodb(adapter, rds_ring); |
| 1516 | } |
| 1517 | |
| 1518 | if (count) { |
| 1519 | sds_ring->consumer = consumer; |
| 1520 | writel(consumer, sds_ring->crb_sts_consumer); |
| 1521 | } |
| 1522 | |
| 1523 | return count; |
| 1524 | } |
| 1525 | |
| 1526 | void |
| 1527 | qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter, u32 ringid, |
| 1528 | struct qlcnic_host_rds_ring *rds_ring) |
| 1529 | { |
| 1530 | struct rcv_desc *pdesc; |
| 1531 | struct qlcnic_rx_buffer *buffer; |
| 1532 | int producer, count = 0; |
| 1533 | struct list_head *head; |
| 1534 | |
| 1535 | producer = rds_ring->producer; |
| 1536 | |
| 1537 | spin_lock(&rds_ring->lock); |
| 1538 | head = &rds_ring->free_list; |
| 1539 | while (!list_empty(head)) { |
| 1540 | |
| 1541 | buffer = list_entry(head->next, struct qlcnic_rx_buffer, list); |
| 1542 | |
| 1543 | if (!buffer->skb) { |
| 1544 | if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer)) |
| 1545 | break; |
| 1546 | } |
| 1547 | |
| 1548 | count++; |
| 1549 | list_del(&buffer->list); |
| 1550 | |
| 1551 | /* make a rcv descriptor */ |
| 1552 | pdesc = &rds_ring->desc_head[producer]; |
| 1553 | pdesc->addr_buffer = cpu_to_le64(buffer->dma); |
| 1554 | pdesc->reference_handle = cpu_to_le16(buffer->ref_handle); |
| 1555 | pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size); |
| 1556 | |
| 1557 | producer = get_next_index(producer, rds_ring->num_desc); |
| 1558 | } |
| 1559 | spin_unlock(&rds_ring->lock); |
| 1560 | |
| 1561 | if (count) { |
| 1562 | rds_ring->producer = producer; |
| 1563 | writel((producer-1) & (rds_ring->num_desc-1), |
| 1564 | rds_ring->crb_rcv_producer); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | static void |
| 1569 | qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter, |
| 1570 | struct qlcnic_host_rds_ring *rds_ring) |
| 1571 | { |
| 1572 | struct rcv_desc *pdesc; |
| 1573 | struct qlcnic_rx_buffer *buffer; |
| 1574 | int producer, count = 0; |
| 1575 | struct list_head *head; |
| 1576 | |
| 1577 | producer = rds_ring->producer; |
| 1578 | if (!spin_trylock(&rds_ring->lock)) |
| 1579 | return; |
| 1580 | |
| 1581 | head = &rds_ring->free_list; |
| 1582 | while (!list_empty(head)) { |
| 1583 | |
| 1584 | buffer = list_entry(head->next, struct qlcnic_rx_buffer, list); |
| 1585 | |
| 1586 | if (!buffer->skb) { |
| 1587 | if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer)) |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | count++; |
| 1592 | list_del(&buffer->list); |
| 1593 | |
| 1594 | /* make a rcv descriptor */ |
| 1595 | pdesc = &rds_ring->desc_head[producer]; |
| 1596 | pdesc->reference_handle = cpu_to_le16(buffer->ref_handle); |
| 1597 | pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size); |
| 1598 | pdesc->addr_buffer = cpu_to_le64(buffer->dma); |
| 1599 | |
| 1600 | producer = get_next_index(producer, rds_ring->num_desc); |
| 1601 | } |
| 1602 | |
| 1603 | if (count) { |
| 1604 | rds_ring->producer = producer; |
| 1605 | writel((producer - 1) & (rds_ring->num_desc - 1), |
| 1606 | rds_ring->crb_rcv_producer); |
| 1607 | } |
| 1608 | spin_unlock(&rds_ring->lock); |
| 1609 | } |
| 1610 | |
Amit Kumar Salecha | cdaff18 | 2010-02-01 05:25:00 +0000 | [diff] [blame] | 1611 | static struct qlcnic_rx_buffer * |
| 1612 | qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter, |
| 1613 | struct qlcnic_host_sds_ring *sds_ring, |
| 1614 | int ring, u64 sts_data0) |
| 1615 | { |
| 1616 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 1617 | struct qlcnic_rx_buffer *buffer; |
| 1618 | struct sk_buff *skb; |
| 1619 | struct qlcnic_host_rds_ring *rds_ring; |
| 1620 | int index, length, cksum, pkt_offset; |
| 1621 | |
| 1622 | if (unlikely(ring >= adapter->max_rds_rings)) |
| 1623 | return NULL; |
| 1624 | |
| 1625 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 1626 | |
| 1627 | index = qlcnic_get_sts_refhandle(sts_data0); |
| 1628 | if (unlikely(index >= rds_ring->num_desc)) |
| 1629 | return NULL; |
| 1630 | |
| 1631 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1632 | |
| 1633 | length = qlcnic_get_sts_totallength(sts_data0); |
| 1634 | cksum = qlcnic_get_sts_status(sts_data0); |
| 1635 | pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0); |
| 1636 | |
| 1637 | skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum); |
| 1638 | if (!skb) |
| 1639 | return buffer; |
| 1640 | |
| 1641 | skb_put(skb, rds_ring->skb_size); |
| 1642 | |
| 1643 | if (pkt_offset) |
| 1644 | skb_pull(skb, pkt_offset); |
| 1645 | |
| 1646 | skb->truesize = skb->len + sizeof(struct sk_buff); |
| 1647 | |
| 1648 | if (!qlcnic_check_loopback_buff(skb->data)) |
| 1649 | adapter->diag_cnt++; |
| 1650 | |
| 1651 | dev_kfree_skb_any(skb); |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1652 | adapter->stats.rx_pkts++; |
| 1653 | adapter->stats.rxbytes += length; |
Amit Kumar Salecha | cdaff18 | 2010-02-01 05:25:00 +0000 | [diff] [blame] | 1654 | |
| 1655 | return buffer; |
| 1656 | } |
| 1657 | |
| 1658 | void |
| 1659 | qlcnic_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring) |
| 1660 | { |
| 1661 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1662 | struct status_desc *desc; |
| 1663 | struct qlcnic_rx_buffer *rxbuf; |
| 1664 | u64 sts_data0; |
| 1665 | |
| 1666 | int opcode, ring, desc_cnt; |
| 1667 | u32 consumer = sds_ring->consumer; |
| 1668 | |
| 1669 | desc = &sds_ring->desc_head[consumer]; |
| 1670 | sts_data0 = le64_to_cpu(desc->status_desc_data[0]); |
| 1671 | |
| 1672 | if (!(sts_data0 & STATUS_OWNER_HOST)) |
| 1673 | return; |
| 1674 | |
| 1675 | desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0); |
| 1676 | opcode = qlcnic_get_sts_opcode(sts_data0); |
| 1677 | |
| 1678 | ring = qlcnic_get_sts_type(sts_data0); |
| 1679 | rxbuf = qlcnic_process_rcv_diag(adapter, sds_ring, |
| 1680 | ring, sts_data0); |
| 1681 | |
| 1682 | desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM); |
| 1683 | consumer = get_next_index(consumer, sds_ring->num_desc); |
| 1684 | |
| 1685 | sds_ring->consumer = consumer; |
| 1686 | writel(consumer, sds_ring->crb_sts_consumer); |
| 1687 | } |