Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Broadcom Corporation |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
Greg Kroah-Hartman | a1c16ed | 2010-10-21 11:17:44 -0700 | [diff] [blame] | 17 | #include <linux/delay.h> |
| 18 | #include <linux/fs.h> |
| 19 | #ifdef mips |
| 20 | #include <asm/paccess.h> |
| 21 | #endif /* mips */ |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 22 | #include <bcmendian.h> |
Brett Rudley | c6ac24e | 2010-10-26 11:55:23 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/sched.h> |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 27 | #include <bcmdefs.h> |
| 28 | #include <osl.h> |
| 29 | #include <bcmutils.h> |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 30 | #include <pcicfg.h> |
| 31 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 32 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 33 | #define OS_HANDLE_MAGIC 0x1234abcd /* Magic # to recognise osh */ |
| 34 | #define BCM_MEM_FILENAME_LEN 24 /* Mem. filename length */ |
| 35 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 36 | /* Global ASSERT type flag */ |
Greg Kroah-Hartman | 66cbd3a | 2010-10-08 11:05:47 -0700 | [diff] [blame] | 37 | u32 g_assert_type; |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 38 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 39 | struct osl_info *osl_attach(void *pdev, uint bustype) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 40 | { |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 41 | struct osl_info *osh; |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 42 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 43 | osh = kmalloc(sizeof(struct osl_info), GFP_ATOMIC); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 44 | ASSERT(osh); |
| 45 | |
Brett Rudley | 9249ede | 2010-11-30 20:09:49 -0800 | [diff] [blame^] | 46 | memset(osh, 0, sizeof(struct osl_info)); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 47 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 48 | osh->magic = OS_HANDLE_MAGIC; |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 49 | osh->pdev = pdev; |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 50 | osh->bustype = bustype; |
| 51 | |
| 52 | switch (bustype) { |
| 53 | case PCI_BUS: |
| 54 | case SI_BUS: |
nohee ko | 194c607 | 2010-10-06 11:56:28 -0700 | [diff] [blame] | 55 | case PCMCIA_BUS: |
Greg Kroah-Hartman | 0f0881b | 2010-10-12 12:15:18 -0700 | [diff] [blame] | 56 | osh->pub.mmbus = true; |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 57 | break; |
| 58 | case JTAG_BUS: |
| 59 | case SDIO_BUS: |
| 60 | case USB_BUS: |
| 61 | case SPI_BUS: |
| 62 | case RPC_BUS: |
Greg Kroah-Hartman | 0965ae8 | 2010-10-12 12:50:15 -0700 | [diff] [blame] | 63 | osh->pub.mmbus = false; |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 64 | break; |
| 65 | default: |
Greg Kroah-Hartman | 0965ae8 | 2010-10-12 12:50:15 -0700 | [diff] [blame] | 66 | ASSERT(false); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 70 | return osh; |
| 71 | } |
| 72 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 73 | void osl_detach(struct osl_info *osh) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 74 | { |
| 75 | if (osh == NULL) |
| 76 | return; |
| 77 | |
| 78 | ASSERT(osh->magic == OS_HANDLE_MAGIC); |
| 79 | kfree(osh); |
| 80 | } |
| 81 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 82 | void *BCMFASTPATH osl_pktget(struct osl_info *osh, uint len) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 83 | { |
| 84 | struct sk_buff *skb; |
| 85 | |
Jason Cooper | ca8c1e5 | 2010-09-14 09:45:33 -0400 | [diff] [blame] | 86 | skb = dev_alloc_skb(len); |
| 87 | if (skb) { |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 88 | skb_put(skb, len); |
| 89 | skb->priority = 0; |
| 90 | |
| 91 | osh->pub.pktalloced++; |
| 92 | } |
| 93 | |
Jason Cooper | 90ea229 | 2010-09-14 09:45:32 -0400 | [diff] [blame] | 94 | return (void *)skb; |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Free the driver packet. Free the tag if present */ |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 98 | void BCMFASTPATH osl_pktfree(struct osl_info *osh, void *p, bool send) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 99 | { |
| 100 | struct sk_buff *skb, *nskb; |
| 101 | int nest = 0; |
| 102 | |
| 103 | skb = (struct sk_buff *)p; |
| 104 | ASSERT(skb); |
| 105 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 106 | /* perversion: we use skb->next to chain multi-skb packets */ |
| 107 | while (skb) { |
| 108 | nskb = skb->next; |
| 109 | skb->next = NULL; |
| 110 | |
| 111 | if (skb->destructor) |
| 112 | /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if |
| 113 | * destructor exists |
| 114 | */ |
| 115 | dev_kfree_skb_any(skb); |
| 116 | else |
| 117 | /* can free immediately (even in_irq()) if destructor |
| 118 | * does not exist |
| 119 | */ |
| 120 | dev_kfree_skb(skb); |
| 121 | |
| 122 | osh->pub.pktalloced--; |
| 123 | nest++; |
| 124 | skb = nskb; |
| 125 | } |
| 126 | } |
| 127 | |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 128 | /* return bus # for the pci device pointed by osh->pdev */ |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 129 | uint osl_pci_bus(struct osl_info *osh) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 130 | { |
| 131 | ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev); |
| 132 | |
| 133 | return ((struct pci_dev *)osh->pdev)->bus->number; |
| 134 | } |
| 135 | |
| 136 | /* return slot # for the pci device pointed by osh->pdev */ |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 137 | uint osl_pci_slot(struct osl_info *osh) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 138 | { |
| 139 | ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev); |
| 140 | |
| 141 | return PCI_SLOT(((struct pci_dev *)osh->pdev)->devfn); |
| 142 | } |
| 143 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 144 | void *osl_dma_alloc_consistent(struct osl_info *osh, uint size, u16 align_bits, |
Greg Kroah-Hartman | 3deea90 | 2010-10-05 11:15:47 -0700 | [diff] [blame] | 145 | uint *alloced, unsigned long *pap) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 146 | { |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 147 | ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC))); |
| 148 | |
Brett Rudley | 40e5c96 | 2010-10-08 13:57:13 -0700 | [diff] [blame] | 149 | if (align_bits) { |
| 150 | u16 align = (1 << align_bits); |
Brett Rudley | 8fb6b18 | 2010-11-01 17:10:15 -0700 | [diff] [blame] | 151 | if (!IS_ALIGNED(PAGE_SIZE, align)) |
Brett Rudley | 40e5c96 | 2010-10-08 13:57:13 -0700 | [diff] [blame] | 152 | size += align; |
| 153 | *alloced = size; |
| 154 | } |
Jason Cooper | 90ea229 | 2010-09-14 09:45:32 -0400 | [diff] [blame] | 155 | return pci_alloc_consistent(osh->pdev, size, (dma_addr_t *) pap); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 158 | void osl_dma_free_consistent(struct osl_info *osh, void *va, uint size, |
| 159 | unsigned long pa) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 160 | { |
| 161 | ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC))); |
| 162 | |
| 163 | pci_free_consistent(osh->pdev, size, va, (dma_addr_t) pa); |
| 164 | } |
| 165 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 166 | uint BCMFASTPATH osl_dma_map(struct osl_info *osh, void *va, uint size, |
| 167 | int direction) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 168 | { |
| 169 | int dir; |
| 170 | |
| 171 | ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC))); |
| 172 | dir = (direction == DMA_TX) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE; |
Jason Cooper | 90ea229 | 2010-09-14 09:45:32 -0400 | [diff] [blame] | 173 | return pci_map_single(osh->pdev, va, size, dir); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Brett Rudley | e69284f | 2010-11-16 15:45:48 -0800 | [diff] [blame] | 176 | void BCMFASTPATH osl_dma_unmap(struct osl_info *osh, uint pa, uint size, |
| 177 | int direction) |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 178 | { |
| 179 | int dir; |
| 180 | |
| 181 | ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC))); |
| 182 | dir = (direction == DMA_TX) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE; |
Greg Kroah-Hartman | 66cbd3a | 2010-10-08 11:05:47 -0700 | [diff] [blame] | 183 | pci_unmap_single(osh->pdev, (u32) pa, size, dir); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | #if defined(BCMDBG_ASSERT) |
| 187 | void osl_assert(char *exp, char *file, int line) |
| 188 | { |
| 189 | char tempbuf[256]; |
| 190 | char *basename; |
| 191 | |
| 192 | basename = strrchr(file, '/'); |
| 193 | /* skip the '/' */ |
| 194 | if (basename) |
| 195 | basename++; |
| 196 | |
| 197 | if (!basename) |
| 198 | basename = file; |
| 199 | |
| 200 | #ifdef BCMDBG_ASSERT |
| 201 | snprintf(tempbuf, 256, |
| 202 | "assertion \"%s\" failed: file \"%s\", line %d\n", exp, |
| 203 | basename, line); |
| 204 | |
| 205 | /* Print assert message and give it time to be written to /var/log/messages */ |
| 206 | if (!in_interrupt()) { |
| 207 | const int delay = 3; |
nohee ko | 194c607 | 2010-10-06 11:56:28 -0700 | [diff] [blame] | 208 | printk(KERN_ERR "%s", tempbuf); |
| 209 | printk(KERN_ERR "panic in %d seconds\n", delay); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 210 | set_current_state(TASK_INTERRUPTIBLE); |
| 211 | schedule_timeout(delay * HZ); |
| 212 | } |
| 213 | |
| 214 | switch (g_assert_type) { |
| 215 | case 0: |
nohee ko | 194c607 | 2010-10-06 11:56:28 -0700 | [diff] [blame] | 216 | panic(KERN_ERR "%s", tempbuf); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 217 | break; |
| 218 | case 1: |
nohee ko | 194c607 | 2010-10-06 11:56:28 -0700 | [diff] [blame] | 219 | printk(KERN_ERR "%s", tempbuf); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 220 | BUG(); |
| 221 | break; |
| 222 | case 2: |
nohee ko | 194c607 | 2010-10-06 11:56:28 -0700 | [diff] [blame] | 223 | printk(KERN_ERR "%s", tempbuf); |
Henry Ptasinski | a9533e7 | 2010-09-08 21:04:42 -0700 | [diff] [blame] | 224 | break; |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | #endif /* BCMDBG_ASSERT */ |
| 229 | |
| 230 | } |
| 231 | #endif /* defined(BCMDBG_ASSERT) */ |
| 232 | |