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 <bcmendian.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/netdevice.h>
26 #include <linux/sched.h>
33 #define OS_HANDLE_MAGIC 0x1234abcd /* Magic # to recognise osh */
34 #define BCM_MEM_FILENAME_LEN 24 /* Mem. filename length */
36 /* Global ASSERT type flag */
39 struct osl_info *osl_attach(void *pdev, uint bustype)
43 osh = kmalloc(sizeof(struct osl_info), GFP_ATOMIC);
46 memset(osh, 0, sizeof(struct osl_info));
48 osh->magic = OS_HANDLE_MAGIC;
50 osh->bustype = bustype;
73 void osl_detach(struct osl_info *osh)
78 ASSERT(osh->magic == OS_HANDLE_MAGIC);
82 struct sk_buff *BCMFASTPATH pkt_buf_get_skb(struct osl_info *osh, uint len)
86 skb = dev_alloc_skb(len);
97 /* Free the driver packet. Free the tag if present */
98 void BCMFASTPATH pkt_buf_free_skb(struct osl_info *osh, struct sk_buff *skb, bool send)
100 struct sk_buff *nskb;
105 /* perversion: we use skb->next to chain multi-skb packets */
111 /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if
114 dev_kfree_skb_any(skb);
116 /* can free immediately (even in_irq()) if destructor
127 /* return bus # for the pci device pointed by osh->pdev */
128 uint osl_pci_bus(struct osl_info *osh)
130 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
132 return ((struct pci_dev *)osh->pdev)->bus->number;
135 /* return slot # for the pci device pointed by osh->pdev */
136 uint osl_pci_slot(struct osl_info *osh)
138 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
140 return PCI_SLOT(((struct pci_dev *)osh->pdev)->devfn);
143 void *osl_dma_alloc_consistent(struct osl_info *osh, uint size, u16 align_bits,
144 uint *alloced, unsigned long *pap)
146 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
149 u16 align = (1 << align_bits);
150 if (!IS_ALIGNED(PAGE_SIZE, align))
154 return pci_alloc_consistent(osh->pdev, size, (dma_addr_t *) pap);
157 void osl_dma_free_consistent(struct osl_info *osh, void *va, uint size,
160 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
162 pci_free_consistent(osh->pdev, size, va, (dma_addr_t) pa);
165 uint BCMFASTPATH osl_dma_map(struct osl_info *osh, void *va, uint size,
170 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
171 dir = (direction == DMA_TX) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
172 return pci_map_single(osh->pdev, va, size, dir);
175 void BCMFASTPATH osl_dma_unmap(struct osl_info *osh, uint pa, uint size,
180 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
181 dir = (direction == DMA_TX) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE;
182 pci_unmap_single(osh->pdev, (u32) pa, size, dir);
185 #if defined(BCMDBG_ASSERT)
186 void osl_assert(char *exp, char *file, int line)
191 basename = strrchr(file, '/');
200 snprintf(tempbuf, 256,
201 "assertion \"%s\" failed: file \"%s\", line %d\n", exp,
204 /* Print assert message and give it time to be written to /var/log/messages */
205 if (!in_interrupt()) {
207 printk(KERN_ERR "%s", tempbuf);
208 printk(KERN_ERR "panic in %d seconds\n", delay);
209 set_current_state(TASK_INTERRUPTIBLE);
210 schedule_timeout(delay * HZ);
213 switch (g_assert_type) {
215 panic(KERN_ERR "%s", tempbuf);
218 printk(KERN_ERR "%s", tempbuf);
222 printk(KERN_ERR "%s", tempbuf);
227 #endif /* BCMDBG_ASSERT */
230 #endif /* defined(BCMDBG_ASSERT) */