1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * Copyright (c) 2002-2006 Neterion, Inc. 22 */ 23 24 #include "xgehal-fifo.h" 25 #include "xgehal-device.h" 26 27 static xge_hal_status_e 28 __hal_fifo_mempool_item_alloc(xge_hal_mempool_h mempoolh, 29 void *memblock, 30 int memblock_index, 31 xge_hal_mempool_dma_t *dma_object, 32 void *item, 33 int index, 34 int is_last, 35 void *userdata) 36 { 37 int memblock_item_idx; 38 xge_hal_fifo_txdl_priv_t *txdl_priv; 39 xge_hal_fifo_txd_t *txdp = (xge_hal_fifo_txd_t *)item; 40 xge_hal_fifo_t *fifo = (xge_hal_fifo_t *)userdata; 41 42 xge_assert(item); 43 txdl_priv = (xge_hal_fifo_txdl_priv_t *) \ 44 __hal_mempool_item_priv((xge_hal_mempool_t *) mempoolh, 45 memblock_index, 46 item, 47 &memblock_item_idx); 48 xge_assert(txdl_priv); 49 50 /* pre-format HAL's TxDL's private */ 51 txdl_priv->dma_offset = (char*)item - (char*)memblock; 52 txdl_priv->dma_addr = dma_object->addr + txdl_priv->dma_offset; 53 txdl_priv->dma_handle = dma_object->handle; 54 txdl_priv->memblock = memblock; 55 txdl_priv->first_txdp = (xge_hal_fifo_txd_t *)item; 56 txdl_priv->next_txdl_priv = NULL; 57 txdl_priv->dang_txdl = NULL; 58 txdl_priv->dang_frags = 0; 59 txdl_priv->alloc_frags = 0; 60 61 #ifdef XGE_DEBUG_ASSERT 62 txdl_priv->dma_object = dma_object; 63 #endif 64 txdp->host_control = (u64)(ulong_t)txdl_priv; 65 66 #ifdef XGE_HAL_ALIGN_XMIT 67 txdl_priv->align_vaddr = NULL; 68 txdl_priv->align_dma_addr = (dma_addr_t)0; 69 70 #ifndef XGE_HAL_ALIGN_XMIT_ALLOC_RT 71 { 72 xge_hal_status_e status; 73 if (fifo->config->alignment_size) { 74 status =__hal_fifo_dtr_align_alloc_map(fifo, txdp); 75 if (status != XGE_HAL_OK) { 76 xge_debug_mm(XGE_ERR, 77 "align buffer[%d] %d bytes, status %d", 78 index, 79 fifo->align_size, 80 status); 81 return status; 82 } 83 } 84 } 85 #endif 86 #endif 87 88 if (fifo->channel.dtr_init) { 89 fifo->channel.dtr_init(fifo, (xge_hal_dtr_h)txdp, index, 90 fifo->channel.userdata, XGE_HAL_CHANNEL_OC_NORMAL); 91 } 92 93 return XGE_HAL_OK; 94 } 95 96 97 static xge_hal_status_e 98 __hal_fifo_mempool_item_free(xge_hal_mempool_h mempoolh, 99 void *memblock, 100 int memblock_index, 101 xge_hal_mempool_dma_t *dma_object, 102 void *item, 103 int index, 104 int is_last, 105 void *userdata) 106 { 107 int memblock_item_idx; 108 xge_hal_fifo_txdl_priv_t *txdl_priv; 109 #ifdef XGE_HAL_ALIGN_XMIT 110 xge_hal_fifo_t *fifo = (xge_hal_fifo_t *)userdata; 111 #endif 112 113 xge_assert(item); 114 115 txdl_priv = (xge_hal_fifo_txdl_priv_t *) \ 116 __hal_mempool_item_priv((xge_hal_mempool_t *) mempoolh, 117 memblock_index, 118 item, 119 &memblock_item_idx); 120 xge_assert(txdl_priv); 121 122 #ifdef XGE_HAL_ALIGN_XMIT 123 if (fifo->config->alignment_size) { 124 if (txdl_priv->align_dma_addr != 0) { 125 xge_os_dma_unmap(fifo->channel.pdev, 126 txdl_priv->align_dma_handle, 127 txdl_priv->align_dma_addr, 128 fifo->align_size, 129 XGE_OS_DMA_DIR_TODEVICE); 130 131 txdl_priv->align_dma_addr = 0; 132 } 133 134 if (txdl_priv->align_vaddr != NULL) { 135 xge_os_dma_free(fifo->channel.pdev, 136 txdl_priv->align_vaddr, 137 fifo->align_size, 138 &txdl_priv->align_dma_acch, 139 &txdl_priv->align_dma_handle); 140 141 txdl_priv->align_vaddr = NULL; 142 } 143 } 144 #endif 145 146 return XGE_HAL_OK; 147 } 148 149 xge_hal_status_e 150 __hal_fifo_open(xge_hal_channel_h channelh, xge_hal_channel_attr_t *attr) 151 { 152 xge_hal_device_t *hldev; 153 xge_hal_status_e status; 154 xge_hal_fifo_t *fifo = (xge_hal_fifo_t *)channelh; 155 xge_hal_fifo_queue_t *queue; 156 int i, txdl_size, max_arr_index, mid_point; 157 xge_hal_dtr_h dtrh; 158 159 hldev = (xge_hal_device_t *)fifo->channel.devh; 160 fifo->config = &hldev->config.fifo; 161 queue = &fifo->config->queue[attr->post_qid]; 162 163 #if defined(XGE_HAL_TX_MULTI_RESERVE) 164 xge_os_spin_lock_init(&fifo->channel.reserve_lock, hldev->pdev); 165 #elif defined(XGE_HAL_TX_MULTI_RESERVE_IRQ) 166 xge_os_spin_lock_init_irq(&fifo->channel.reserve_lock, hldev->irqh); 167 #endif 168 #if defined(XGE_HAL_TX_MULTI_POST) 169 if (xge_hal_device_check_id(hldev) == XGE_HAL_CARD_XENA) { 170 fifo->post_lock_ptr = &hldev->xena_post_lock; 171 } else { 172 xge_os_spin_lock_init(&fifo->channel.post_lock, hldev->pdev); 173 fifo->post_lock_ptr = &fifo->channel.post_lock; 174 } 175 #elif defined(XGE_HAL_TX_MULTI_POST_IRQ) 176 if (xge_hal_device_check_id(hldev) == XGE_HAL_CARD_XENA) { 177 fifo->post_lock_ptr = &hldev->xena_post_lock; 178 } else { 179 xge_os_spin_lock_init_irq(&fifo->channel.post_lock, 180 hldev->irqh); 181 fifo->post_lock_ptr = &fifo->channel.post_lock; 182 } 183 #endif 184 185 fifo->align_size = 186 fifo->config->alignment_size * fifo->config->max_aligned_frags; 187 188 /* Initializing the BAR1 address as the start of 189 * the FIFO queue pointer and as a location of FIFO control 190 * word. */ 191 fifo->hw_pair = 192 (xge_hal_fifo_hw_pair_t *) (void *)(hldev->bar1 + 193 (attr->post_qid * XGE_HAL_FIFO_HW_PAIR_OFFSET)); 194 195 /* apply "interrupts per txdl" attribute */ 196 fifo->interrupt_type = XGE_HAL_TXD_INT_TYPE_UTILZ; 197 if (queue->intr) { 198 fifo->interrupt_type = XGE_HAL_TXD_INT_TYPE_PER_LIST; 199 } 200 fifo->no_snoop_bits = 201 (int)(XGE_HAL_TX_FIFO_NO_SNOOP(queue->no_snoop_bits)); 202 203 /* 204 * FIFO memory management strategy: 205 * 206 * TxDL splitted into three independent parts: 207 * - set of TxD's 208 * - TxD HAL private part 209 * - upper layer private part 210 * 211 * Adaptative memory allocation used. i.e. Memory allocated on 212 * demand with the size which will fit into one memory block. 213 * One memory block may contain more than one TxDL. In simple case 214 * memory block size can be equal to CPU page size. On more 215 * sophisticated OS's memory block can be contigious across 216 * several pages. 217 * 218 * During "reserve" operations more memory can be allocated on demand 219 * for example due to FIFO full condition. 220 * 221 * Pool of memory memblocks never shrinks except __hal_fifo_close 222 * routine which will essentially stop channel and free the resources. 223 */ 224 225 /* TxDL common private size == TxDL private + ULD private */ 226 fifo->priv_size = sizeof(xge_hal_fifo_txdl_priv_t) + 227 attr->per_dtr_space; 228 fifo->priv_size = ((fifo->priv_size + __xge_os_cacheline_size -1) / 229 __xge_os_cacheline_size) * 230 __xge_os_cacheline_size; 231 232 /* recompute txdl size to be cacheline aligned */ 233 fifo->txdl_size = fifo->config->max_frags * sizeof(xge_hal_fifo_txd_t); 234 txdl_size = ((fifo->txdl_size + __xge_os_cacheline_size - 1) / 235 __xge_os_cacheline_size) * __xge_os_cacheline_size; 236 237 if (fifo->txdl_size != txdl_size) 238 xge_debug_fifo(XGE_ERR, "cacheline > 128 (??): %d, %d, %d, %d", 239 fifo->config->max_frags, fifo->txdl_size, txdl_size, 240 __xge_os_cacheline_size); 241 242 fifo->txdl_size = txdl_size; 243 244 /* since dtr_init() callback will be called from item_alloc(), 245 * the same way channels userdata might be used prior to 246 * channel_initialize() */ 247 fifo->channel.dtr_init = attr->dtr_init; 248 fifo->channel.userdata = attr->userdata; 249 fifo->txdl_per_memblock = fifo->config->memblock_size / 250 fifo->txdl_size; 251 252 fifo->mempool = __hal_mempool_create(hldev->pdev, 253 fifo->config->memblock_size, 254 fifo->txdl_size, 255 fifo->priv_size, 256 queue->initial, 257 queue->max, 258 __hal_fifo_mempool_item_alloc, 259 __hal_fifo_mempool_item_free, 260 fifo); 261 if (fifo->mempool == NULL) { 262 return XGE_HAL_ERR_OUT_OF_MEMORY; 263 } 264 265 status = __hal_channel_initialize(channelh, attr, 266 (void **) __hal_mempool_items_arr(fifo->mempool), 267 queue->initial, queue->max, 268 fifo->config->reserve_threshold); 269 if (status != XGE_HAL_OK) { 270 __hal_fifo_close(channelh); 271 return status; 272 } 273 xge_debug_fifo(XGE_TRACE, 274 "DTR reserve_length:%d reserve_top:%d\n" 275 "max_frags:%d reserve_threshold:%d\n" 276 "memblock_size:%d alignment_size:%d max_aligned_frags:%d\n", 277 fifo->channel.reserve_length, fifo->channel.reserve_top, 278 fifo->config->max_frags, fifo->config->reserve_threshold, 279 fifo->config->memblock_size, fifo->config->alignment_size, 280 fifo->config->max_aligned_frags); 281 282 #ifdef XGE_DEBUG_ASSERT 283 for ( i = 0; i < fifo->channel.reserve_length; i++) { 284 xge_debug_fifo(XGE_TRACE, "DTR before reversing index:%d" 285 " handle:%p\n", i, fifo->channel.reserve_arr[i]); 286 } 287 #endif 288 289 xge_assert(fifo->channel.reserve_length); 290 /* reverse the FIFO dtr array */ 291 max_arr_index = fifo->channel.reserve_length - 1; 292 max_arr_index -=fifo->channel.reserve_top; 293 xge_assert(max_arr_index); 294 mid_point = (fifo->channel.reserve_length - fifo->channel.reserve_top)/2; 295 for (i = 0; i < mid_point; i++) { 296 dtrh = fifo->channel.reserve_arr[i]; 297 fifo->channel.reserve_arr[i] = 298 fifo->channel.reserve_arr[max_arr_index - i]; 299 fifo->channel.reserve_arr[max_arr_index - i] = dtrh; 300 } 301 302 #ifdef XGE_DEBUG_ASSERT 303 for ( i = 0; i < fifo->channel.reserve_length; i++) { 304 xge_debug_fifo(XGE_TRACE, "DTR after reversing index:%d" 305 " handle:%p\n", i, fifo->channel.reserve_arr[i]); 306 } 307 #endif 308 309 return XGE_HAL_OK; 310 } 311 312 void 313 __hal_fifo_close(xge_hal_channel_h channelh) 314 { 315 xge_hal_fifo_t *fifo = (xge_hal_fifo_t *)channelh; 316 xge_hal_device_t *hldev = (xge_hal_device_t *)fifo->channel.devh; 317 318 if (fifo->mempool) { 319 __hal_mempool_destroy(fifo->mempool); 320 } 321 322 __hal_channel_terminate(channelh); 323 324 #if defined(XGE_HAL_TX_MULTI_RESERVE) 325 xge_os_spin_lock_destroy(&fifo->channel.reserve_lock, hldev->pdev); 326 #elif defined(XGE_HAL_TX_MULTI_RESERVE_IRQ) 327 xge_os_spin_lock_destroy_irq(&fifo->channel.reserve_lock, hldev->pdev); 328 #endif 329 if (xge_hal_device_check_id(hldev) == XGE_HAL_CARD_HERC) { 330 #if defined(XGE_HAL_TX_MULTI_POST) 331 xge_os_spin_lock_destroy(&fifo->channel.post_lock, hldev->pdev); 332 #elif defined(XGE_HAL_TX_MULTI_POST_IRQ) 333 xge_os_spin_lock_destroy_irq(&fifo->channel.post_lock, 334 hldev->pdev); 335 #endif 336 } 337 } 338 339 void 340 __hal_fifo_hw_initialize(xge_hal_device_h devh) 341 { 342 xge_hal_device_t *hldev = (xge_hal_device_t *)devh; 343 xge_hal_pci_bar0_t *bar0 = (xge_hal_pci_bar0_t *)(void 344 *)hldev->bar0; 345 u64* tx_fifo_partitions[4]; 346 u64* tx_fifo_wrr[5]; 347 u64 val64, part0; 348 int i; 349 350 /* Tx DMA Initialization */ 351 352 tx_fifo_partitions[0] = &bar0->tx_fifo_partition_0; 353 tx_fifo_partitions[1] = &bar0->tx_fifo_partition_1; 354 tx_fifo_partitions[2] = &bar0->tx_fifo_partition_2; 355 tx_fifo_partitions[3] = &bar0->tx_fifo_partition_3; 356 357 tx_fifo_wrr[0] = &bar0->tx_w_round_robin_0; 358 tx_fifo_wrr[1] = &bar0->tx_w_round_robin_1; 359 tx_fifo_wrr[2] = &bar0->tx_w_round_robin_2; 360 tx_fifo_wrr[3] = &bar0->tx_w_round_robin_3; 361 tx_fifo_wrr[4] = &bar0->tx_w_round_robin_4; 362 363 /* Note: WRR calendar must be configured before the transmit 364 FIFOs are enabled! page 6-77 user guide */ 365 366 /* all zeroes for Round-Robin */ 367 for (i = 0; i < XGE_HAL_FIFO_MAX_WRR; i++) { 368 xge_os_pio_mem_write64(hldev->pdev, hldev->regh0, 0, 369 tx_fifo_wrr[i]); 370 } 371 372 /* reset all of them but '0' */ 373 for (i=1; i < XGE_HAL_FIFO_MAX_PARTITION; i++) { 374 xge_os_pio_mem_write64(hldev->pdev, hldev->regh0, 0ULL, 375 tx_fifo_partitions[i]); 376 } 377 378 /* configure only configured FIFOs */ 379 val64 = 0; part0 = 0; 380 for (i = 0; i < XGE_HAL_MAX_FIFO_NUM; i++) { 381 int reg_half = i % 2; 382 int reg_num = i / 2; 383 384 if (hldev->config.fifo.queue[i].configured) { 385 int priority = hldev->config.fifo.queue[i].priority; 386 val64 |= 387 vBIT((hldev->config.fifo.queue[i].max-1), 388 (((reg_half) * 32) + 19), 389 13) | vBIT(priority, (((reg_half)*32) + 5), 3); 390 } 391 392 /* NOTE: do write operation for each second u64 half 393 or force for first one if configured number 394 is even */ 395 if (reg_half) { 396 if (reg_num == 0) { 397 /* skip partition '0', must write it once at 398 * the end */ 399 part0 = val64; 400 } else { 401 xge_os_pio_mem_write64(hldev->pdev, hldev->regh0, 402 val64, tx_fifo_partitions[reg_num]); 403 xge_debug_fifo(XGE_TRACE, 404 "fifo partition_%d at: " 405 "0x"XGE_OS_LLXFMT" is: 0x"XGE_OS_LLXFMT, 406 reg_num, (unsigned long long)(ulong_t) 407 tx_fifo_partitions[reg_num], 408 (unsigned long long)val64); 409 } 410 val64 = 0; 411 } 412 } 413 414 part0 |= BIT(0); /* to enable the FIFO partition. */ 415 __hal_pio_mem_write32_lower(hldev->pdev, hldev->regh0, (u32)part0, 416 tx_fifo_partitions[0]); 417 xge_os_wmb(); 418 __hal_pio_mem_write32_upper(hldev->pdev, hldev->regh0, (u32)(part0>>32), 419 tx_fifo_partitions[0]); 420 xge_debug_fifo(XGE_TRACE, "fifo partition_0 at: " 421 "0x"XGE_OS_LLXFMT" is: 0x"XGE_OS_LLXFMT, 422 (unsigned long long)(ulong_t) 423 tx_fifo_partitions[0], 424 (unsigned long long) part0); 425 426 /* 427 * Initialization of Tx_PA_CONFIG register to ignore packet 428 * integrity checking. 429 */ 430 val64 = xge_os_pio_mem_read64(hldev->pdev, hldev->regh0, 431 &bar0->tx_pa_cfg); 432 val64 |= XGE_HAL_TX_PA_CFG_IGNORE_FRM_ERR | 433 XGE_HAL_TX_PA_CFG_IGNORE_SNAP_OUI | 434 XGE_HAL_TX_PA_CFG_IGNORE_LLC_CTRL | 435 XGE_HAL_TX_PA_CFG_IGNORE_L2_ERR; 436 xge_os_pio_mem_write64(hldev->pdev, hldev->regh0, val64, 437 &bar0->tx_pa_cfg); 438 xge_debug_fifo(XGE_TRACE, "%s", "fifo channels initialized"); 439 } 440 441 #ifdef XGE_HAL_ALIGN_XMIT 442 void 443 __hal_fifo_dtr_align_free_unmap(xge_hal_channel_h channelh, xge_hal_dtr_h dtrh) 444 { 445 xge_hal_fifo_txdl_priv_t *txdl_priv; 446 xge_hal_fifo_txd_t *txdp = (xge_hal_fifo_txd_t *)dtrh; 447 xge_hal_fifo_t *fifo = (xge_hal_fifo_t *)channelh; 448 449 txdl_priv = __hal_fifo_txdl_priv(txdp); 450 451 if (txdl_priv->align_dma_addr != 0) { 452 xge_os_dma_unmap(fifo->channel.pdev, 453 txdl_priv->align_dma_handle, 454 txdl_priv->align_dma_addr, 455 fifo->align_size, 456 XGE_OS_DMA_DIR_TODEVICE); 457 458 txdl_priv->align_dma_addr = 0; 459 } 460 461 if (txdl_priv->align_vaddr != NULL) { 462 xge_os_dma_free(fifo->channel.pdev, 463 txdl_priv->align_vaddr, 464 fifo->align_size, 465 &txdl_priv->align_dma_acch, 466 &txdl_priv->align_dma_handle); 467 468 469 txdl_priv->align_vaddr = NULL; 470 } 471 } 472 473 xge_hal_status_e 474 __hal_fifo_dtr_align_alloc_map(xge_hal_channel_h channelh, xge_hal_dtr_h dtrh) 475 { 476 xge_hal_fifo_txdl_priv_t *txdl_priv; 477 xge_hal_fifo_txd_t *txdp = (xge_hal_fifo_txd_t *)dtrh; 478 xge_hal_fifo_t *fifo = (xge_hal_fifo_t *)channelh; 479 480 xge_assert(txdp); 481 482 txdl_priv = __hal_fifo_txdl_priv(txdp); 483 484 /* allocate alignment DMA-buffer */ 485 txdl_priv->align_vaddr = xge_os_dma_malloc(fifo->channel.pdev, 486 fifo->align_size, 487 XGE_OS_DMA_CACHELINE_ALIGNED | 488 XGE_OS_DMA_STREAMING, 489 &txdl_priv->align_dma_handle, 490 &txdl_priv->align_dma_acch); 491 if (txdl_priv->align_vaddr == NULL) { 492 return XGE_HAL_ERR_OUT_OF_MEMORY; 493 } 494 495 /* map it */ 496 txdl_priv->align_dma_addr = xge_os_dma_map(fifo->channel.pdev, 497 txdl_priv->align_dma_handle, txdl_priv->align_vaddr, 498 fifo->align_size, 499 XGE_OS_DMA_DIR_TODEVICE, XGE_OS_DMA_STREAMING); 500 501 if (txdl_priv->align_dma_addr == XGE_OS_INVALID_DMA_ADDR) { 502 __hal_fifo_dtr_align_free_unmap(channelh, dtrh); 503 return XGE_HAL_ERR_OUT_OF_MAPPING; 504 } 505 506 return XGE_HAL_OK; 507 } 508 #endif 509 510 511