2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/delay.h>
20 #include <asm/paccess.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/netdevice.h>
25 #include <linux/sched.h>
32 #define OS_HANDLE_MAGIC 0x1234abcd /* Magic # to recognise osh */
33 #define BCM_MEM_FILENAME_LEN 24 /* Mem. filename length */
35 /* Global ASSERT type flag */
38 struct osl_info *osl_attach(void *pdev, uint bustype)
42 osh = kmalloc(sizeof(struct osl_info), GFP_ATOMIC);
45 memset(osh, 0, sizeof(struct osl_info));
47 osh->magic = OS_HANDLE_MAGIC;
49 osh->bustype = bustype;
72 void osl_detach(struct osl_info *osh)
77 ASSERT(osh->magic == OS_HANDLE_MAGIC);
81 struct sk_buff *BCMFASTPATH pkt_buf_get_skb(struct osl_info *osh, uint len)
85 skb = dev_alloc_skb(len);
96 /* Free the driver packet. Free the tag if present */
97 void BCMFASTPATH pkt_buf_free_skb(struct osl_info *osh, struct sk_buff *skb, bool send)
104 /* perversion: we use skb->next to chain multi-skb packets */
110 /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if
113 dev_kfree_skb_any(skb);
115 /* can free immediately (even in_irq()) if destructor
126 /* return bus # for the pci device pointed by osh->pdev */
127 uint osl_pci_bus(struct osl_info *osh)
129 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
131 return ((struct pci_dev *)osh->pdev)->bus->number;
134 /* return slot # for the pci device pointed by osh->pdev */
135 uint osl_pci_slot(struct osl_info *osh)
137 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
139 return PCI_SLOT(((struct pci_dev *)osh->pdev)->devfn);
142 void *osl_dma_alloc_consistent(struct osl_info *osh, uint size, u16 align_bits,
143 uint *alloced, unsigned long *pap)
145 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
148 u16 align = (1 << align_bits);
149 if (!IS_ALIGNED(PAGE_SIZE, align))
153 return pci_alloc_consistent(osh->pdev, size, (dma_addr_t *) pap);
156 void osl_dma_free_consistent(struct osl_info *osh, void *va, uint size,
159 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
161 pci_free_consistent(osh->pdev, size, va, (dma_addr_t) pa);
164 uint BCMFASTPATH osl_dma_map(struct osl_info *osh, void *va, uint size,
169 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
170 dir = (direction == DMA_TX) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
171 return pci_map_single(osh->pdev, va, size, dir);
174 void BCMFASTPATH osl_dma_unmap(struct osl_info *osh, uint pa, uint size,
179 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
180 dir = (direction == DMA_TX) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
181 pci_unmap_single(osh->pdev, (u32) pa, size, dir);
184 #if defined(BCMDBG_ASSERT)
185 void osl_assert(char *exp, char *file, int line)
190 basename = strrchr(file, '/');
199 snprintf(tempbuf, 256,
200 "assertion \"%s\" failed: file \"%s\", line %d\n", exp,
203 /* Print assert message and give it time to be written to /var/log/messages */
204 if (!in_interrupt()) {
206 printk(KERN_ERR "%s", tempbuf);
207 printk(KERN_ERR "panic in %d seconds\n", delay);
208 set_current_state(TASK_INTERRUPTIBLE);
209 schedule_timeout(delay * HZ);
212 switch (g_assert_type) {
214 panic(KERN_ERR "%s", tempbuf);
217 printk(KERN_ERR "%s", tempbuf);
221 printk(KERN_ERR "%s", tempbuf);
226 #endif /* BCMDBG_ASSERT */
229 #endif /* defined(BCMDBG_ASSERT) */