1 /***************************************************************************
2 * Copyright (c) 2005-2009, Broadcom Corporation.
4 * Name: crystalhd_misc . c
7 * BCM70012 Linux driver misc routines.
11 **********************************************************************
12 * This file is part of the crystalhd device driver.
14 * This driver is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, version 2 of the License.
18 * This driver is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this driver. If not, see <http://www.gnu.org/licenses/>.
25 **********************************************************************/
27 #include "crystalhd.h"
29 #include <linux/slab.h>
31 uint32_t g_linklog_level;
33 static inline uint32_t crystalhd_dram_rd(struct crystalhd_adp *adp,
36 crystalhd_reg_wr(adp, DCI_DRAM_BASE_ADDR, (mem_off >> 19));
37 return bc_dec_reg_rd(adp, (0x00380000 | (mem_off & 0x0007FFFF)));
40 static inline void crystalhd_dram_wr(struct crystalhd_adp *adp,
41 uint32_t mem_off, uint32_t val)
43 crystalhd_reg_wr(adp, DCI_DRAM_BASE_ADDR, (mem_off >> 19));
44 bc_dec_reg_wr(adp, (0x00380000 | (mem_off & 0x0007FFFF)), val);
47 static inline enum BC_STATUS bc_chk_dram_range(struct crystalhd_adp *adp,
48 uint32_t start_off, uint32_t cnt)
50 return BC_STS_SUCCESS;
53 static struct crystalhd_dio_req *crystalhd_alloc_dio(struct crystalhd_adp *adp)
55 unsigned long flags = 0;
56 struct crystalhd_dio_req *temp = NULL;
59 BCMLOG_ERR("Invalid Arg!!\n");
63 spin_lock_irqsave(&adp->lock, flags);
64 temp = adp->ua_map_free_head;
66 adp->ua_map_free_head = adp->ua_map_free_head->next;
67 spin_unlock_irqrestore(&adp->lock, flags);
72 static void crystalhd_free_dio(struct crystalhd_adp *adp,
73 struct crystalhd_dio_req *dio)
75 unsigned long flags = 0;
79 spin_lock_irqsave(&adp->lock, flags);
80 dio->sig = crystalhd_dio_inv;
83 memset(&dio->uinfo, 0, sizeof(dio->uinfo));
84 dio->next = adp->ua_map_free_head;
85 adp->ua_map_free_head = dio;
86 spin_unlock_irqrestore(&adp->lock, flags);
89 static struct crystalhd_elem *crystalhd_alloc_elem(struct crystalhd_adp *adp)
91 unsigned long flags = 0;
92 struct crystalhd_elem *temp = NULL;
96 spin_lock_irqsave(&adp->lock, flags);
97 temp = adp->elem_pool_head;
99 adp->elem_pool_head = adp->elem_pool_head->flink;
100 memset(temp, 0, sizeof(*temp));
102 spin_unlock_irqrestore(&adp->lock, flags);
106 static void crystalhd_free_elem(struct crystalhd_adp *adp,
107 struct crystalhd_elem *elem)
109 unsigned long flags = 0;
113 spin_lock_irqsave(&adp->lock, flags);
114 elem->flink = adp->elem_pool_head;
115 adp->elem_pool_head = elem;
116 spin_unlock_irqrestore(&adp->lock, flags);
119 static inline void crystalhd_set_sg(struct scatterlist *sg, struct page *page,
120 unsigned int len, unsigned int offset)
122 sg_set_page(sg, page, len, offset);
124 sg->dma_length = len;
128 static inline void crystalhd_init_sg(struct scatterlist *sg,
129 unsigned int entries)
131 /* http://lkml.org/lkml/2007/11/27/68 */
132 sg_init_table(sg, entries);
135 /*========================== Extern ========================================*/
137 * bc_dec_reg_rd - Read 7412's device register.
138 * @adp: Adapter instance
139 * @reg_off: Register offset.
144 * 7412's device register read routine. This interface use
145 * 7412's device access range mapped from BAR-2 (4M) of PCIe
146 * configuration space.
148 uint32_t bc_dec_reg_rd(struct crystalhd_adp *adp, uint32_t reg_off)
150 if (!adp || (reg_off > adp->pci_mem_len)) {
151 BCMLOG_ERR("dec_rd_reg_off outof range: 0x%08x\n", reg_off);
155 return readl(adp->addr + reg_off);
159 * bc_dec_reg_wr - Write 7412's device register
160 * @adp: Adapter instance
161 * @reg_off: Register offset.
162 * @val: Dword value to be written.
167 * 7412's device register write routine. This interface use
168 * 7412's device access range mapped from BAR-2 (4M) of PCIe
169 * configuration space.
171 void bc_dec_reg_wr(struct crystalhd_adp *adp, uint32_t reg_off, uint32_t val)
173 if (!adp || (reg_off > adp->pci_mem_len)) {
174 BCMLOG_ERR("dec_wr_reg_off outof range: 0x%08x\n", reg_off);
177 writel(val, adp->addr + reg_off);
182 * crystalhd_reg_rd - Read Link's device register.
183 * @adp: Adapter instance
184 * @reg_off: Register offset.
189 * Link device register read routine. This interface use
190 * Link's device access range mapped from BAR-1 (64K) of PCIe
191 * configuration space.
194 uint32_t crystalhd_reg_rd(struct crystalhd_adp *adp, uint32_t reg_off)
196 if (!adp || (reg_off > adp->pci_i2o_len)) {
197 BCMLOG_ERR("link_rd_reg_off outof range: 0x%08x\n", reg_off);
200 return readl(adp->i2o_addr + reg_off);
204 * crystalhd_reg_wr - Write Link's device register
205 * @adp: Adapter instance
206 * @reg_off: Register offset.
207 * @val: Dword value to be written.
212 * Link device register write routine. This interface use
213 * Link's device access range mapped from BAR-1 (64K) of PCIe
214 * configuration space.
217 void crystalhd_reg_wr(struct crystalhd_adp *adp, uint32_t reg_off,
220 if (!adp || (reg_off > adp->pci_i2o_len)) {
221 BCMLOG_ERR("link_wr_reg_off outof range: 0x%08x\n", reg_off);
224 writel(val, adp->i2o_addr + reg_off);
228 * crystalhd_mem_rd - Read data from 7412's DRAM area.
229 * @adp: Adapter instance
230 * @start_off: Start offset.
231 * @dw_cnt: Count in dwords.
232 * @rd_buff: Buffer to copy the data from dram.
237 * 7412's Dram read routine.
239 enum BC_STATUS crystalhd_mem_rd(struct crystalhd_adp *adp, uint32_t start_off,
240 uint32_t dw_cnt, uint32_t *rd_buff)
244 if (!adp || !rd_buff ||
245 (bc_chk_dram_range(adp, start_off, dw_cnt) != BC_STS_SUCCESS)) {
246 BCMLOG_ERR("Invalid arg\n");
247 return BC_STS_INV_ARG;
249 for (ix = 0; ix < dw_cnt; ix++)
250 rd_buff[ix] = crystalhd_dram_rd(adp, (start_off + (ix * 4)));
252 return BC_STS_SUCCESS;
256 * crystalhd_mem_wr - Write data to 7412's DRAM area.
257 * @adp: Adapter instance
258 * @start_off: Start offset.
259 * @dw_cnt: Count in dwords.
260 * @wr_buff: Data Buffer to be written.
265 * 7412's Dram write routine.
267 enum BC_STATUS crystalhd_mem_wr(struct crystalhd_adp *adp, uint32_t start_off,
268 uint32_t dw_cnt, uint32_t *wr_buff)
272 if (!adp || !wr_buff ||
273 (bc_chk_dram_range(adp, start_off, dw_cnt) != BC_STS_SUCCESS)) {
274 BCMLOG_ERR("Invalid arg\n");
275 return BC_STS_INV_ARG;
278 for (ix = 0; ix < dw_cnt; ix++)
279 crystalhd_dram_wr(adp, (start_off + (ix * 4)), wr_buff[ix]);
281 return BC_STS_SUCCESS;
284 * crystalhd_pci_cfg_rd - PCIe config read
285 * @adp: Adapter instance
286 * @off: PCI config space offset.
287 * @len: Size -- Byte, Word & dword.
293 * Get value from Link's PCIe config space.
295 enum BC_STATUS crystalhd_pci_cfg_rd(struct crystalhd_adp *adp, uint32_t off,
296 uint32_t len, uint32_t *val)
298 enum BC_STATUS sts = BC_STS_SUCCESS;
302 BCMLOG_ERR("Invalid arg\n");
303 return BC_STS_INV_ARG;
308 rc = pci_read_config_byte(adp->pdev, off, (u8 *)val);
311 rc = pci_read_config_word(adp->pdev, off, (u16 *)val);
314 rc = pci_read_config_dword(adp->pdev, off, (u32 *)val);
318 sts = BC_STS_INV_ARG;
319 BCMLOG_ERR("Invalid len:%d\n", len);
322 if (rc && (sts == BC_STS_SUCCESS))
329 * crystalhd_pci_cfg_wr - PCIe config write
330 * @adp: Adapter instance
331 * @off: PCI config space offset.
332 * @len: Size -- Byte, Word & dword.
333 * @val: Value to be written
338 * Set value to Link's PCIe config space.
340 enum BC_STATUS crystalhd_pci_cfg_wr(struct crystalhd_adp *adp, uint32_t off,
341 uint32_t len, uint32_t val)
343 enum BC_STATUS sts = BC_STS_SUCCESS;
347 BCMLOG_ERR("Invalid arg\n");
348 return BC_STS_INV_ARG;
353 rc = pci_write_config_byte(adp->pdev, off, (u8)val);
356 rc = pci_write_config_word(adp->pdev, off, (u16)val);
359 rc = pci_write_config_dword(adp->pdev, off, val);
363 sts = BC_STS_INV_ARG;
364 BCMLOG_ERR("Invalid len:%d\n", len);
367 if (rc && (sts == BC_STS_SUCCESS))
374 * bc_kern_dma_alloc - Allocate memory for Dma rings
375 * @adp: Adapter instance
376 * @sz: Size of the memory to allocate.
377 * @phy_addr: Physical address of the memory allocated.
378 * Typedef to system's dma_addr_t (u64)
381 * Pointer to allocated memory..
383 * Wrapper to Linux kernel interface.
386 void *bc_kern_dma_alloc(struct crystalhd_adp *adp, uint32_t sz,
387 dma_addr_t *phy_addr)
391 if (!adp || !sz || !phy_addr) {
392 BCMLOG_ERR("Invalide Arg..\n");
396 temp = pci_alloc_consistent(adp->pdev, sz, phy_addr);
404 * bc_kern_dma_free - Release Dma ring memory.
405 * @adp: Adapter instance
406 * @sz: Size of the memory to allocate.
407 * @ka: Kernel virtual address returned during _dio_alloc()
408 * @phy_addr: Physical address of the memory allocated.
409 * Typedef to system's dma_addr_t (u64)
414 void bc_kern_dma_free(struct crystalhd_adp *adp, uint32_t sz, void *ka,
417 if (!adp || !ka || !sz || !phy_addr) {
418 BCMLOG_ERR("Invalide Arg..\n");
422 pci_free_consistent(adp->pdev, sz, ka, phy_addr);
426 * crystalhd_create_dioq - Create Generic DIO queue
427 * @adp: Adapter instance
428 * @dioq_hnd: Handle to the dio queue created
429 * @cb : Optional - Call back To free the element.
430 * @cbctx: Context to pass to callback.
435 * Initialize Generic DIO queue to hold any data. Callback
436 * will be used to free elements while deleting the queue.
438 enum BC_STATUS crystalhd_create_dioq(struct crystalhd_adp *adp,
439 struct crystalhd_dioq **dioq_hnd,
440 crystalhd_data_free_cb cb, void *cbctx)
442 struct crystalhd_dioq *dioq = NULL;
444 if (!adp || !dioq_hnd) {
445 BCMLOG_ERR("Invalid arg!!\n");
446 return BC_STS_INV_ARG;
449 dioq = kzalloc(sizeof(*dioq), GFP_KERNEL);
451 return BC_STS_INSUFF_RES;
453 spin_lock_init(&dioq->lock);
454 dioq->sig = BC_LINK_DIOQ_SIG;
455 dioq->head = (struct crystalhd_elem *)&dioq->head;
456 dioq->tail = (struct crystalhd_elem *)&dioq->head;
457 crystalhd_create_event(&dioq->event);
459 dioq->data_rel_cb = cb;
460 dioq->cb_context = cbctx;
463 return BC_STS_SUCCESS;
467 * crystalhd_delete_dioq - Delete Generic DIO queue
468 * @adp: Adapter instance
469 * @dioq: DIOQ instance..
474 * Release Generic DIO queue. This function will remove
475 * all the entries from the Queue and will release data
476 * by calling the call back provided during creation.
479 void crystalhd_delete_dioq(struct crystalhd_adp *adp,
480 struct crystalhd_dioq *dioq)
484 if (!dioq || (dioq->sig != BC_LINK_DIOQ_SIG))
488 temp = crystalhd_dioq_fetch(dioq);
489 if (temp && dioq->data_rel_cb)
490 dioq->data_rel_cb(dioq->cb_context, temp);
497 * crystalhd_dioq_add - Add new DIO request element.
498 * @ioq: DIO queue instance
499 * @t: DIO request to be added.
500 * @wake: True - Wake up suspended process.
501 * @tag: Special tag to assign - For search and get.
506 * Insert new element to Q tail.
508 enum BC_STATUS crystalhd_dioq_add(struct crystalhd_dioq *ioq, void *data,
509 bool wake, uint32_t tag)
511 unsigned long flags = 0;
512 struct crystalhd_elem *tmp;
514 if (!ioq || (ioq->sig != BC_LINK_DIOQ_SIG) || !data) {
515 BCMLOG_ERR("Invalid arg!!\n");
516 return BC_STS_INV_ARG;
519 tmp = crystalhd_alloc_elem(ioq->adp);
521 BCMLOG_ERR("No free elements.\n");
522 return BC_STS_INSUFF_RES;
527 spin_lock_irqsave(&ioq->lock, flags);
528 tmp->flink = (struct crystalhd_elem *)&ioq->head;
529 tmp->blink = ioq->tail;
530 tmp->flink->blink = tmp;
531 tmp->blink->flink = tmp;
533 spin_unlock_irqrestore(&ioq->lock, flags);
536 crystalhd_set_event(&ioq->event);
538 return BC_STS_SUCCESS;
542 * crystalhd_dioq_fetch - Fetch element from head.
543 * @ioq: DIO queue instance
546 * data element from the head..
548 * Remove an element from Queue.
550 void *crystalhd_dioq_fetch(struct crystalhd_dioq *ioq)
552 unsigned long flags = 0;
553 struct crystalhd_elem *tmp;
554 struct crystalhd_elem *ret = NULL;
557 if (!ioq || (ioq->sig != BC_LINK_DIOQ_SIG)) {
558 BCMLOG_ERR("Invalid arg!!\n");
562 spin_lock_irqsave(&ioq->lock, flags);
564 if (tmp != (struct crystalhd_elem *)&ioq->head) {
566 tmp->flink->blink = tmp->blink;
567 tmp->blink->flink = tmp->flink;
570 spin_unlock_irqrestore(&ioq->lock, flags);
573 crystalhd_free_elem(ioq->adp, ret);
579 * crystalhd_dioq_find_and_fetch - Search the tag and Fetch element
580 * @ioq: DIO queue instance
581 * @tag: Tag to search for.
584 * element from the head..
586 * Search TAG and remove the element.
588 void *crystalhd_dioq_find_and_fetch(struct crystalhd_dioq *ioq, uint32_t tag)
590 unsigned long flags = 0;
591 struct crystalhd_elem *tmp;
592 struct crystalhd_elem *ret = NULL;
595 if (!ioq || (ioq->sig != BC_LINK_DIOQ_SIG)) {
596 BCMLOG_ERR("Invalid arg!!\n");
600 spin_lock_irqsave(&ioq->lock, flags);
602 while (tmp != (struct crystalhd_elem *)&ioq->head) {
603 if (tmp->tag == tag) {
605 tmp->flink->blink = tmp->blink;
606 tmp->blink->flink = tmp->flink;
612 spin_unlock_irqrestore(&ioq->lock, flags);
616 crystalhd_free_elem(ioq->adp, ret);
623 * crystalhd_dioq_fetch_wait - Fetch element from Head.
624 * @ioq: DIO queue instance
625 * @to_secs: Wait timeout in seconds..
628 * element from the head..
630 * Return element from head if Q is not empty. Wait for new element
631 * if Q is empty for Timeout seconds.
633 void *crystalhd_dioq_fetch_wait(struct crystalhd_dioq *ioq, uint32_t to_secs,
636 unsigned long flags = 0;
640 if (!ioq || (ioq->sig != BC_LINK_DIOQ_SIG) || !to_secs || !sig_pend) {
641 BCMLOG_ERR("Invalid arg!!\n");
646 spin_lock_irqsave(&ioq->lock, flags);
647 while ((ioq->count == 0) && count) {
648 spin_unlock_irqrestore(&ioq->lock, flags);
650 crystalhd_wait_on_event(&ioq->event,
651 (ioq->count > 0), 1000, rc, 0);
654 } else if (rc == -EINTR) {
655 BCMLOG(BCMLOG_INFO, "Cancelling fetch wait\n");
659 spin_lock_irqsave(&ioq->lock, flags);
662 spin_unlock_irqrestore(&ioq->lock, flags);
665 return crystalhd_dioq_fetch(ioq);
669 * crystalhd_map_dio - Map user address for DMA
670 * @adp: Adapter instance
671 * @ubuff: User buffer to map.
672 * @ubuff_sz: User buffer size.
673 * @uv_offset: UV buffer offset.
674 * @en_422mode: TRUE:422 FALSE:420 Capture mode.
675 * @dir_tx: TRUE for Tx (To device from host)
676 * @dio_hnd: Handle to mapped DIO request.
681 * This routine maps user address and lock pages for DMA.
684 enum BC_STATUS crystalhd_map_dio(struct crystalhd_adp *adp, void *ubuff,
685 uint32_t ubuff_sz, uint32_t uv_offset,
686 bool en_422mode, bool dir_tx,
687 struct crystalhd_dio_req **dio_hnd)
689 struct crystalhd_dio_req *dio;
690 /* FIXME: jarod: should some of these
691 unsigned longs be uint32_t or uintptr_t? */
692 unsigned long start = 0, end = 0, uaddr = 0, count = 0;
693 unsigned long spsz = 0, uv_start = 0;
694 int i = 0, rw = 0, res = 0, nr_pages = 0, skip_fb_sg = 0;
696 if (!adp || !ubuff || !ubuff_sz || !dio_hnd) {
697 BCMLOG_ERR("Invalid arg\n");
698 return BC_STS_INV_ARG;
701 uaddr = (unsigned long)ubuff;
702 count = (unsigned long)ubuff_sz;
703 end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
704 start = uaddr >> PAGE_SHIFT;
705 nr_pages = end - start;
707 if (!count || ((uaddr + count) < uaddr)) {
708 BCMLOG_ERR("User addr overflow!!\n");
709 return BC_STS_INV_ARG;
712 dio = crystalhd_alloc_dio(adp);
714 BCMLOG_ERR("dio pool empty..\n");
715 return BC_STS_INSUFF_RES;
720 dio->direction = DMA_TO_DEVICE;
723 dio->direction = DMA_FROM_DEVICE;
726 if (nr_pages > dio->max_pages) {
727 BCMLOG_ERR("max_pages(%d) exceeded(%d)!!\n",
728 dio->max_pages, nr_pages);
729 crystalhd_unmap_dio(adp, dio);
730 return BC_STS_INSUFF_RES;
734 uv_start = (uaddr + (unsigned long)uv_offset) >> PAGE_SHIFT;
735 dio->uinfo.uv_sg_ix = uv_start - start;
736 dio->uinfo.uv_sg_off = ((uaddr + (unsigned long)uv_offset) &
740 dio->fb_size = ubuff_sz & 0x03;
742 res = copy_from_user(dio->fb_va,
743 (void *)(uaddr + count - dio->fb_size),
746 BCMLOG_ERR("failed %d to copy %u fill bytes from %p\n",
748 (void *)(uaddr + count-dio->fb_size));
749 crystalhd_unmap_dio(adp, dio);
750 return BC_STS_INSUFF_RES;
754 down_read(¤t->mm->mmap_sem);
755 res = get_user_pages(current, current->mm, uaddr, nr_pages, rw == READ,
756 0, dio->pages, NULL);
757 up_read(¤t->mm->mmap_sem);
759 /* Save for release..*/
760 dio->sig = crystalhd_dio_locked;
761 if (res < nr_pages) {
762 BCMLOG_ERR("get pages failed: %d-%d\n", nr_pages, res);
764 crystalhd_unmap_dio(adp, dio);
768 dio->page_cnt = nr_pages;
769 /* Get scatter/gather */
770 crystalhd_init_sg(dio->sg, dio->page_cnt);
771 crystalhd_set_sg(&dio->sg[0], dio->pages[0], 0, uaddr & ~PAGE_MASK);
773 dio->sg[0].length = PAGE_SIZE - dio->sg[0].offset;
776 dio->sg[0].dma_length = dio->sg[0].length;
778 count -= dio->sg[0].length;
779 for (i = 1; i < nr_pages; i++) {
784 spsz = (count < PAGE_SIZE) ?
785 (count & ~0x03) : PAGE_SIZE;
787 crystalhd_set_sg(&dio->sg[i], dio->pages[i], spsz, 0);
792 dio->sg[0].length = count;
795 dio->sg[0].length = count - dio->fb_size;
798 dio->sg[0].dma_length = dio->sg[0].length;
801 dio->sg_cnt = pci_map_sg(adp->pdev, dio->sg,
802 dio->page_cnt, dio->direction);
803 if (dio->sg_cnt <= 0) {
804 BCMLOG_ERR("sg map %d-%d\n", dio->sg_cnt, dio->page_cnt);
805 crystalhd_unmap_dio(adp, dio);
808 if (dio->sg_cnt && skip_fb_sg)
810 dio->sig = crystalhd_dio_sg_mapped;
811 /* Fill in User info.. */
812 dio->uinfo.xfr_len = ubuff_sz;
813 dio->uinfo.xfr_buff = ubuff;
814 dio->uinfo.uv_offset = uv_offset;
815 dio->uinfo.b422mode = en_422mode;
816 dio->uinfo.dir_tx = dir_tx;
820 return BC_STS_SUCCESS;
824 * crystalhd_unmap_sgl - Release mapped resources
825 * @adp: Adapter instance
826 * @dio: DIO request instance
831 * This routine is to unmap the user buffer pages.
833 enum BC_STATUS crystalhd_unmap_dio(struct crystalhd_adp *adp,
834 struct crystalhd_dio_req *dio)
836 struct page *page = NULL;
840 BCMLOG_ERR("Invalid arg\n");
841 return BC_STS_INV_ARG;
844 if ((dio->page_cnt > 0) && (dio->sig != crystalhd_dio_inv)) {
845 for (j = 0; j < dio->page_cnt; j++) {
846 page = dio->pages[j];
848 if (!PageReserved(page) &&
849 (dio->direction == DMA_FROM_DEVICE))
851 page_cache_release(page);
855 if (dio->sig == crystalhd_dio_sg_mapped)
856 pci_unmap_sg(adp->pdev, dio->sg, dio->page_cnt,
859 crystalhd_free_dio(adp, dio);
861 return BC_STS_SUCCESS;
865 * crystalhd_create_dio_pool - Allocate mem pool for DIO management.
866 * @adp: Adapter instance
867 * @max_pages: Max pages for size calculation.
872 * This routine creates a memory pool to hold dio context for
873 * for HW Direct IO operation.
875 int crystalhd_create_dio_pool(struct crystalhd_adp *adp, uint32_t max_pages)
877 uint32_t asz = 0, i = 0;
879 struct crystalhd_dio_req *dio;
881 if (!adp || !max_pages) {
882 BCMLOG_ERR("Invalid Arg!!\n");
886 /* Get dma memory for fill byte handling..*/
887 adp->fill_byte_pool = pci_pool_create("crystalhd_fbyte",
889 if (!adp->fill_byte_pool) {
890 BCMLOG_ERR("failed to create fill byte pool\n");
894 /* Get the max size from user based on 420/422 modes */
895 asz = (sizeof(*dio->pages) * max_pages) +
896 (sizeof(*dio->sg) * max_pages) + sizeof(*dio);
898 BCMLOG(BCMLOG_DBG, "Initializing Dio pool %d %d %x %p\n",
899 BC_LINK_SG_POOL_SZ, max_pages, asz, adp->fill_byte_pool);
901 for (i = 0; i < BC_LINK_SG_POOL_SZ; i++) {
902 temp = kzalloc(asz, GFP_KERNEL);
903 if ((temp) == NULL) {
904 BCMLOG_ERR("Failed to alloc %d mem\n", asz);
908 dio = (struct crystalhd_dio_req *)temp;
909 temp += sizeof(*dio);
910 dio->pages = (struct page **)temp;
911 temp += (sizeof(*dio->pages) * max_pages);
912 dio->sg = (struct scatterlist *)temp;
913 dio->max_pages = max_pages;
914 dio->fb_va = pci_pool_alloc(adp->fill_byte_pool, GFP_KERNEL,
917 BCMLOG_ERR("fill byte alloc failed.\n");
921 crystalhd_free_dio(adp, dio);
928 * crystalhd_destroy_dio_pool - Release DIO mem pool.
929 * @adp: Adapter instance
934 * This routine releases dio memory pool during close.
936 void crystalhd_destroy_dio_pool(struct crystalhd_adp *adp)
938 struct crystalhd_dio_req *dio;
942 BCMLOG_ERR("Invalid Arg!!\n");
947 dio = crystalhd_alloc_dio(adp);
950 pci_pool_free(adp->fill_byte_pool,
951 dio->fb_va, dio->fb_pa);
957 if (adp->fill_byte_pool) {
958 pci_pool_destroy(adp->fill_byte_pool);
959 adp->fill_byte_pool = NULL;
962 BCMLOG(BCMLOG_DBG, "Released dio pool %d\n", count);
966 * crystalhd_create_elem_pool - List element pool creation.
967 * @adp: Adapter instance
968 * @pool_size: Number of elements in the pool.
971 * 0 - success, <0 error
973 * Create general purpose list element pool to hold pending,
974 * and active requests.
976 int crystalhd_create_elem_pool(struct crystalhd_adp *adp,
980 struct crystalhd_elem *temp;
982 if (!adp || !pool_size)
985 for (i = 0; i < pool_size; i++) {
986 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
988 BCMLOG_ERR("kalloc failed\n");
991 crystalhd_free_elem(adp, temp);
993 BCMLOG(BCMLOG_DBG, "allocated %d elem\n", pool_size);
998 * crystalhd_delete_elem_pool - List element pool deletion.
999 * @adp: Adapter instance
1004 * Delete general purpose list element pool.
1006 void crystalhd_delete_elem_pool(struct crystalhd_adp *adp)
1008 struct crystalhd_elem *temp;
1015 temp = crystalhd_alloc_elem(adp);
1022 BCMLOG(BCMLOG_DBG, "released %d elem\n", dbg_cnt);
1025 /*================ Debug support routines.. ================================*/
1026 void crystalhd_show_buffer(uint32_t off, uint8_t *buff, uint32_t dwcount)
1030 for (i = 0; i < dwcount; i++) {
1032 BCMLOG(BCMLOG_DATA, "0x%08X : ", off);
1034 BCMLOG(BCMLOG_DATA, " 0x%08X ", *((uint32_t *)buff));
1036 buff += sizeof(uint32_t);
1037 off += sizeof(uint32_t);
1039 if ((i == dwcount - 1) || (k > 4)) {
1040 BCMLOG(BCMLOG_DATA, "\n");