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