Thomas Gleixner | 1949739 | 2019-05-29 16:58:03 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * mm.c - Micro Memory(tm) PCI memory board block device driver - v2.3 |
| 4 | * |
| 5 | * (C) 2001 San Mehat <nettwerk@valinux.com> |
| 6 | * (C) 2001 Johannes Erdfelt <jerdfelt@valinux.com> |
| 7 | * (C) 2001 NeilBrown <neilb@cse.unsw.edu.au> |
| 8 | * |
| 9 | * This driver for the Micro Memory PCI Memory Module with Battery Backup |
| 10 | * is Copyright Micro Memory Inc 2001-2002. All rights reserved. |
| 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * This driver provides a standard block device interface for Micro Memory(tm) |
| 13 | * PCI based RAM boards. |
| 14 | * 10/05/01: Phap Nguyen - Rebuilt the driver |
| 15 | * 10/22/01: Phap Nguyen - v2.1 Added disk partitioning |
| 16 | * 29oct2001:NeilBrown - Use make_request_fn instead of request_fn |
| 17 | * - use stand disk partitioning (so fdisk works). |
| 18 | * 08nov2001:NeilBrown - change driver name from "mm" to "umem" |
| 19 | * - incorporate into main kernel |
| 20 | * 08apr2002:NeilBrown - Move some of interrupt handle to tasklet |
| 21 | * - use spin_lock_bh instead of _irq |
| 22 | * - Never block on make_request. queue |
| 23 | * bh's instead. |
| 24 | * - unregister umem from devfs at mod unload |
| 25 | * - Change version to 2.3 |
| 26 | * 07Nov2001:Phap Nguyen - Select pci read command: 06, 12, 15 (Decimal) |
| 27 | * 07Jan2002: P. Nguyen - Used PCI Memory Write & Invalidate for DMA |
| 28 | * 15May2002:NeilBrown - convert to bio for 2.5 |
| 29 | * 17May2002:NeilBrown - remove init_mem initialisation. Instead detect |
| 30 | * - a sequence of writes that cover the card, and |
| 31 | * - set initialised bit then. |
| 32 | */ |
| 33 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 34 | #undef DEBUG /* #define DEBUG if you want debugging info (pr_debug) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/fs.h> |
| 36 | #include <linux/bio.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/mman.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/ioctl.h> |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/timer.h> |
| 46 | #include <linux/pci.h> |
Matthias Gehre | 910638a | 2006-03-28 01:56:48 -0800 | [diff] [blame] | 47 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #include <linux/fcntl.h> /* O_ACCMODE */ |
| 50 | #include <linux/hdreg.h> /* HDIO_GETGEO */ |
| 51 | |
Jeff Garzik | 3084f0c | 2007-09-27 06:25:06 -0400 | [diff] [blame] | 52 | #include "umem.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 54 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <asm/io.h> |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #define MM_MAXCARDS 4 |
| 58 | #define MM_RAHEAD 2 /* two sectors */ |
| 59 | #define MM_BLKSIZE 1024 /* 1k blocks */ |
| 60 | #define MM_HARDSECT 512 /* 512-byte hardware sectors */ |
| 61 | #define MM_SHIFT 6 /* max 64 partitions on 4 cards */ |
| 62 | |
| 63 | /* |
| 64 | * Version Information |
| 65 | */ |
| 66 | |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 67 | #define DRIVER_NAME "umem" |
| 68 | #define DRIVER_VERSION "v2.3" |
| 69 | #define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown" |
| 70 | #define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | static int debug; |
| 73 | /* #define HW_TRACE(x) writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */ |
| 74 | #define HW_TRACE(x) |
| 75 | |
| 76 | #define DEBUG_LED_ON_TRANSFER 0x01 |
| 77 | #define DEBUG_BATTERY_POLLING 0x02 |
| 78 | |
| 79 | module_param(debug, int, 0644); |
| 80 | MODULE_PARM_DESC(debug, "Debug bitmask"); |
| 81 | |
| 82 | static int pci_read_cmd = 0x0C; /* Read Multiple */ |
| 83 | module_param(pci_read_cmd, int, 0); |
| 84 | MODULE_PARM_DESC(pci_read_cmd, "PCI read command"); |
| 85 | |
| 86 | static int pci_write_cmd = 0x0F; /* Write and Invalidate */ |
| 87 | module_param(pci_write_cmd, int, 0); |
| 88 | MODULE_PARM_DESC(pci_write_cmd, "PCI write command"); |
| 89 | |
| 90 | static int pci_cmds; |
| 91 | |
| 92 | static int major_nr; |
| 93 | |
| 94 | #include <linux/blkdev.h> |
| 95 | #include <linux/blkpg.h> |
| 96 | |
| 97 | struct cardinfo { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct pci_dev *dev; |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | unsigned char __iomem *csr_remap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | unsigned int mm_size; /* size in kbytes */ |
| 102 | |
| 103 | unsigned int init_size; /* initial segment, in sectors, |
| 104 | * that we know to |
| 105 | * have been written |
| 106 | */ |
| 107 | struct bio *bio, *currentbio, **biotail; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 108 | struct bvec_iter current_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 110 | struct request_queue *queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | struct mm_page { |
| 113 | dma_addr_t page_dma; |
| 114 | struct mm_dma_desc *desc; |
| 115 | int cnt, headcnt; |
| 116 | struct bio *bio, **biotail; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 117 | struct bvec_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } mm_pages[2]; |
| 119 | #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc)) |
| 120 | |
| 121 | int Active, Ready; |
| 122 | |
| 123 | struct tasklet_struct tasklet; |
| 124 | unsigned int dma_status; |
| 125 | |
| 126 | struct { |
| 127 | int good; |
| 128 | int warned; |
| 129 | unsigned long last_change; |
| 130 | } battery[2]; |
| 131 | |
| 132 | spinlock_t lock; |
| 133 | int check_batteries; |
| 134 | |
| 135 | int flags; |
| 136 | }; |
| 137 | |
| 138 | static struct cardinfo cards[MM_MAXCARDS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | static struct timer_list battery_timer; |
| 140 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 141 | static int num_cards; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | static struct gendisk *mm_gendisk[MM_MAXCARDS]; |
| 144 | |
| 145 | static void check_batteries(struct cardinfo *card); |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | static int get_userbit(struct cardinfo *card, int bit) |
| 148 | { |
| 149 | unsigned char led; |
| 150 | |
| 151 | led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL); |
| 152 | return led & bit; |
| 153 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | static int set_userbit(struct cardinfo *card, int bit, unsigned char state) |
| 156 | { |
| 157 | unsigned char led; |
| 158 | |
| 159 | led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL); |
| 160 | if (state) |
| 161 | led |= bit; |
| 162 | else |
| 163 | led &= ~bit; |
| 164 | writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL); |
| 165 | |
| 166 | return 0; |
| 167 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | /* |
| 170 | * NOTE: For the power LED, use the LED_POWER_* macros since they differ |
| 171 | */ |
| 172 | static void set_led(struct cardinfo *card, int shift, unsigned char state) |
| 173 | { |
| 174 | unsigned char led; |
| 175 | |
| 176 | led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL); |
| 177 | if (state == LED_FLIP) |
| 178 | led ^= (1<<shift); |
| 179 | else { |
| 180 | led &= ~(0x03 << shift); |
| 181 | led |= (state << shift); |
| 182 | } |
| 183 | writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | #ifdef MM_DIAG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static void dump_regs(struct cardinfo *card) |
| 189 | { |
| 190 | unsigned char *p; |
| 191 | int i, i1; |
| 192 | |
| 193 | p = card->csr_remap; |
| 194 | for (i = 0; i < 8; i++) { |
| 195 | printk(KERN_DEBUG "%p ", p); |
| 196 | |
| 197 | for (i1 = 0; i1 < 16; i1++) |
| 198 | printk("%02x ", *p++); |
| 199 | |
| 200 | printk("\n"); |
| 201 | } |
| 202 | } |
| 203 | #endif |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | static void dump_dmastat(struct cardinfo *card, unsigned int dmastat) |
| 206 | { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 207 | dev_printk(KERN_DEBUG, &card->dev->dev, "DMAstat - "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (dmastat & DMASCR_ANY_ERR) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 209 | printk(KERN_CONT "ANY_ERR "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (dmastat & DMASCR_MBE_ERR) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 211 | printk(KERN_CONT "MBE_ERR "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (dmastat & DMASCR_PARITY_ERR_REP) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 213 | printk(KERN_CONT "PARITY_ERR_REP "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (dmastat & DMASCR_PARITY_ERR_DET) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 215 | printk(KERN_CONT "PARITY_ERR_DET "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (dmastat & DMASCR_SYSTEM_ERR_SIG) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 217 | printk(KERN_CONT "SYSTEM_ERR_SIG "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (dmastat & DMASCR_TARGET_ABT) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 219 | printk(KERN_CONT "TARGET_ABT "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (dmastat & DMASCR_MASTER_ABT) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 221 | printk(KERN_CONT "MASTER_ABT "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if (dmastat & DMASCR_CHAIN_COMPLETE) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 223 | printk(KERN_CONT "CHAIN_COMPLETE "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (dmastat & DMASCR_DMA_COMPLETE) |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 225 | printk(KERN_CONT "DMA_COMPLETE "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | printk("\n"); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Theory of request handling |
| 231 | * |
| 232 | * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME |
| 233 | * We have two pages of mm_dma_desc, holding about 64 descriptors |
| 234 | * each. These are allocated at init time. |
| 235 | * One page is "Ready" and is either full, or can have request added. |
| 236 | * The other page might be "Active", which DMA is happening on it. |
| 237 | * |
| 238 | * Whenever IO on the active page completes, the Ready page is activated |
| 239 | * and the ex-Active page is clean out and made Ready. |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 240 | * Otherwise the Ready page is only activated when it becomes full. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | * |
| 242 | * If a request arrives while both pages a full, it is queued, and b_rdev is |
| 243 | * overloaded to record whether it was a read or a write. |
| 244 | * |
| 245 | * The interrupt handler only polls the device to clear the interrupt. |
| 246 | * The processing of the result is done in a tasklet. |
| 247 | */ |
| 248 | |
| 249 | static void mm_start_io(struct cardinfo *card) |
| 250 | { |
| 251 | /* we have the lock, we know there is |
| 252 | * no IO active, and we know that card->Active |
| 253 | * is set |
| 254 | */ |
| 255 | struct mm_dma_desc *desc; |
| 256 | struct mm_page *page; |
| 257 | int offset; |
| 258 | |
| 259 | /* make the last descriptor end the chain */ |
| 260 | page = &card->mm_pages[card->Active]; |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 261 | pr_debug("start_io: %d %d->%d\n", |
| 262 | card->Active, page->headcnt, page->cnt - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | desc = &page->desc[page->cnt-1]; |
| 264 | |
| 265 | desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN); |
| 266 | desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN); |
| 267 | desc->sem_control_bits = desc->control_bits; |
| 268 | |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | if (debug & DEBUG_LED_ON_TRANSFER) |
| 271 | set_led(card, LED_REMOVE, LED_ON); |
| 272 | |
| 273 | desc = &page->desc[page->headcnt]; |
| 274 | writel(0, card->csr_remap + DMA_PCI_ADDR); |
| 275 | writel(0, card->csr_remap + DMA_PCI_ADDR + 4); |
| 276 | |
| 277 | writel(0, card->csr_remap + DMA_LOCAL_ADDR); |
| 278 | writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4); |
| 279 | |
| 280 | writel(0, card->csr_remap + DMA_TRANSFER_SIZE); |
| 281 | writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4); |
| 282 | |
| 283 | writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR); |
| 284 | writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4); |
| 285 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 286 | offset = ((char *)desc) - ((char *)page->desc); |
| 287 | writel(cpu_to_le32((page->page_dma+offset) & 0xffffffff), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | card->csr_remap + DMA_DESCRIPTOR_ADDR); |
| 289 | /* Force the value to u64 before shifting otherwise >> 32 is undefined C |
| 290 | * and on some ports will do nothing ! */ |
| 291 | writel(cpu_to_le32(((u64)page->page_dma)>>32), |
| 292 | card->csr_remap + DMA_DESCRIPTOR_ADDR + 4); |
| 293 | |
| 294 | /* Go, go, go */ |
| 295 | writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds), |
| 296 | card->csr_remap + DMA_STATUS_CTRL); |
| 297 | } |
| 298 | |
| 299 | static int add_bio(struct cardinfo *card); |
| 300 | |
| 301 | static void activate(struct cardinfo *card) |
| 302 | { |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 303 | /* if No page is Active, and Ready is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | * not empty, then switch Ready page |
| 305 | * to active and start IO. |
| 306 | * Then add any bh's that are available to Ready |
| 307 | */ |
| 308 | |
| 309 | do { |
| 310 | while (add_bio(card)) |
| 311 | ; |
| 312 | |
| 313 | if (card->Active == -1 && |
| 314 | card->mm_pages[card->Ready].cnt > 0) { |
| 315 | card->Active = card->Ready; |
| 316 | card->Ready = 1-card->Ready; |
| 317 | mm_start_io(card); |
| 318 | } |
| 319 | |
| 320 | } while (card->Active == -1 && add_bio(card)); |
| 321 | } |
| 322 | |
| 323 | static inline void reset_page(struct mm_page *page) |
| 324 | { |
| 325 | page->cnt = 0; |
| 326 | page->headcnt = 0; |
| 327 | page->bio = NULL; |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 328 | page->biotail = &page->bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 331 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | * If there is room on Ready page, take |
| 333 | * one bh off list and add it. |
| 334 | * return 1 if there was room, else 0. |
| 335 | */ |
| 336 | static int add_bio(struct cardinfo *card) |
| 337 | { |
| 338 | struct mm_page *p; |
| 339 | struct mm_dma_desc *desc; |
| 340 | dma_addr_t dma_handle; |
| 341 | int offset; |
| 342 | struct bio *bio; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 343 | struct bio_vec vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | bio = card->currentbio; |
| 346 | if (!bio && card->bio) { |
| 347 | card->currentbio = card->bio; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 348 | card->current_iter = card->bio->bi_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | card->bio = card->bio->bi_next; |
| 350 | if (card->bio == NULL) |
| 351 | card->biotail = &card->bio; |
| 352 | card->currentbio->bi_next = NULL; |
| 353 | return 1; |
| 354 | } |
| 355 | if (!bio) |
| 356 | return 0; |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE) |
| 359 | return 0; |
| 360 | |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 361 | vec = bio_iter_iovec(bio, card->current_iter); |
| 362 | |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 363 | dma_handle = dma_map_page(&card->dev->dev, |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 364 | vec.bv_page, |
| 365 | vec.bv_offset, |
| 366 | vec.bv_len, |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 367 | bio_op(bio) == REQ_OP_READ ? |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 368 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | p = &card->mm_pages[card->Ready]; |
| 371 | desc = &p->desc[p->cnt]; |
| 372 | p->cnt++; |
NeilBrown | eea9bef | 2007-08-16 13:31:26 +0200 | [diff] [blame] | 373 | if (p->bio == NULL) |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 374 | p->iter = card->current_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if ((p->biotail) != &bio->bi_next) { |
| 376 | *(p->biotail) = bio; |
| 377 | p->biotail = &(bio->bi_next); |
| 378 | bio->bi_next = NULL; |
| 379 | } |
| 380 | |
| 381 | desc->data_dma_handle = dma_handle; |
| 382 | |
| 383 | desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle); |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 384 | desc->local_addr = cpu_to_le64(card->current_iter.bi_sector << 9); |
| 385 | desc->transfer_size = cpu_to_le32(vec.bv_len); |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 386 | offset = (((char *)&desc->sem_control_bits) - ((char *)p->desc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset)); |
| 388 | desc->zero1 = desc->zero2 = 0; |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 389 | offset = (((char *)(desc+1)) - ((char *)p->desc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | desc->next_desc_addr = cpu_to_le64(p->page_dma+offset); |
| 391 | desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN| |
| 392 | DMASCR_PARITY_INT_EN| |
| 393 | DMASCR_CHAIN_EN | |
| 394 | DMASCR_SEM_EN | |
| 395 | pci_cmds); |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 396 | if (bio_op(bio) == REQ_OP_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ); |
| 398 | desc->sem_control_bits = desc->control_bits; |
| 399 | |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 400 | |
| 401 | bio_advance_iter(bio, &card->current_iter, vec.bv_len); |
| 402 | if (!card->current_iter.bi_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | card->currentbio = NULL; |
| 404 | |
| 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | static void process_page(unsigned long data) |
| 409 | { |
| 410 | /* check if any of the requests in the page are DMA_COMPLETE, |
| 411 | * and deal with them appropriately. |
| 412 | * If we find a descriptor without DMA_COMPLETE in the semaphore, then |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 413 | * dma must have hit an error on that descriptor, so use dma_status |
| 414 | * instead and assume that all following descriptors must be re-tried. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | */ |
| 416 | struct mm_page *page; |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 417 | struct bio *return_bio = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | struct cardinfo *card = (struct cardinfo *)data; |
| 419 | unsigned int dma_status = card->dma_status; |
| 420 | |
jun qian | 902d539 | 2018-09-07 10:27:20 -0700 | [diff] [blame] | 421 | spin_lock(&card->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (card->Active < 0) |
| 423 | goto out_unlock; |
| 424 | page = &card->mm_pages[card->Active]; |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | while (page->headcnt < page->cnt) { |
| 427 | struct bio *bio = page->bio; |
| 428 | struct mm_dma_desc *desc = &page->desc[page->headcnt]; |
| 429 | int control = le32_to_cpu(desc->sem_control_bits); |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 430 | int last = 0; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 431 | struct bio_vec vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | if (!(control & DMASCR_DMA_COMPLETE)) { |
| 434 | control = dma_status; |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 435 | last = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | page->headcnt++; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 439 | vec = bio_iter_iovec(bio, page->iter); |
| 440 | bio_advance_iter(bio, &page->iter, vec.bv_len); |
| 441 | |
| 442 | if (!page->iter.bi_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | page->bio = bio->bi_next; |
Neil Brown | 794e64d | 2007-12-10 15:49:30 -0800 | [diff] [blame] | 444 | if (page->bio) |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 445 | page->iter = page->bio->bi_iter; |
NeilBrown | eea9bef | 2007-08-16 13:31:26 +0200 | [diff] [blame] | 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 448 | dma_unmap_page(&card->dev->dev, desc->data_dma_handle, |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 449 | vec.bv_len, |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 450 | (control & DMASCR_TRANSFER_READ) ? |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 451 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (control & DMASCR_HARD_ERROR) { |
| 453 | /* error */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 454 | bio->bi_status = BLK_STS_IOERR; |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 455 | dev_printk(KERN_WARNING, &card->dev->dev, |
| 456 | "I/O error on sector %d/%d\n", |
| 457 | le32_to_cpu(desc->local_addr)>>9, |
| 458 | le32_to_cpu(desc->transfer_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | dump_dmastat(card, control); |
Mike Christie | a8ebb05 | 2016-06-05 14:31:45 -0500 | [diff] [blame] | 460 | } else if (op_is_write(bio_op(bio)) && |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 461 | le32_to_cpu(desc->local_addr) >> 9 == |
| 462 | card->init_size) { |
| 463 | card->init_size += le32_to_cpu(desc->transfer_size) >> 9; |
| 464 | if (card->init_size >> 1 >= card->mm_size) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 465 | dev_printk(KERN_INFO, &card->dev->dev, |
| 466 | "memory now initialised\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | set_userbit(card, MEMORY_INITIALIZED, 1); |
| 468 | } |
| 469 | } |
| 470 | if (bio != page->bio) { |
| 471 | bio->bi_next = return_bio; |
| 472 | return_bio = bio; |
| 473 | } |
| 474 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 475 | if (last) |
| 476 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | if (debug & DEBUG_LED_ON_TRANSFER) |
| 480 | set_led(card, LED_REMOVE, LED_OFF); |
| 481 | |
| 482 | if (card->check_batteries) { |
| 483 | card->check_batteries = 0; |
| 484 | check_batteries(card); |
| 485 | } |
| 486 | if (page->headcnt >= page->cnt) { |
| 487 | reset_page(page); |
| 488 | card->Active = -1; |
| 489 | activate(card); |
| 490 | } else { |
| 491 | /* haven't finished with this one yet */ |
Domen Puncer | 46308c0 | 2005-09-10 00:27:09 -0700 | [diff] [blame] | 492 | pr_debug("do some more\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | mm_start_io(card); |
| 494 | } |
| 495 | out_unlock: |
jun qian | 902d539 | 2018-09-07 10:27:20 -0700 | [diff] [blame] | 496 | spin_unlock(&card->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 498 | while (return_bio) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | struct bio *bio = return_bio; |
| 500 | |
| 501 | return_bio = bio->bi_next; |
| 502 | bio->bi_next = NULL; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 503 | bio_endio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 507 | static void mm_unplug(struct blk_plug_cb *cb, bool from_schedule) |
Tao Guo | 3258737 | 2012-06-13 21:17:21 +0200 | [diff] [blame] | 508 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 509 | struct cardinfo *card = cb->data; |
Tao Guo | 3258737 | 2012-06-13 21:17:21 +0200 | [diff] [blame] | 510 | |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 511 | spin_lock_irq(&card->lock); |
| 512 | activate(card); |
| 513 | spin_unlock_irq(&card->lock); |
| 514 | kfree(cb); |
Tao Guo | 3258737 | 2012-06-13 21:17:21 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static int mm_check_plugged(struct cardinfo *card) |
| 518 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 519 | return !!blk_check_plugged(mm_unplug, card, sizeof(struct blk_plug_cb)); |
Tao Guo | 3258737 | 2012-06-13 21:17:21 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 522 | static blk_qc_t mm_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | struct cardinfo *card = q->queuedata; |
Zach Brown | f2b9ecc | 2006-10-03 01:16:07 -0700 | [diff] [blame] | 525 | pr_debug("mm_make_request %llu %u\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 526 | (unsigned long long)bio->bi_iter.bi_sector, |
| 527 | bio->bi_iter.bi_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
NeilBrown | af67c31 | 2017-06-18 14:38:57 +1000 | [diff] [blame] | 529 | blk_queue_split(q, &bio); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | spin_lock_irq(&card->lock); |
| 532 | *card->biotail = bio; |
| 533 | bio->bi_next = NULL; |
| 534 | card->biotail = &bio->bi_next; |
Christoph Hellwig | 03ea4af | 2016-11-01 07:40:04 -0600 | [diff] [blame] | 535 | if (op_is_sync(bio->bi_opf) || !mm_check_plugged(card)) |
Tao Guo | 3258737 | 2012-06-13 21:17:21 +0200 | [diff] [blame] | 536 | activate(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | spin_unlock_irq(&card->lock); |
| 538 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 539 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 542 | static irqreturn_t mm_interrupt(int irq, void *__card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
| 544 | struct cardinfo *card = (struct cardinfo *) __card; |
| 545 | unsigned int dma_status; |
| 546 | unsigned short cfg_status; |
| 547 | |
| 548 | HW_TRACE(0x30); |
| 549 | |
| 550 | dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL)); |
| 551 | |
| 552 | if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) { |
| 553 | /* interrupt wasn't for me ... */ |
| 554 | return IRQ_NONE; |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | /* clear COMPLETION interrupts */ |
| 558 | if (card->flags & UM_FLAG_NO_BYTE_STATUS) |
| 559 | writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE), |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 560 | card->csr_remap + DMA_STATUS_CTRL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | else |
| 562 | writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16, |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 563 | card->csr_remap + DMA_STATUS_CTRL + 2); |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | /* log errors and clear interrupt status */ |
| 566 | if (dma_status & DMASCR_ANY_ERR) { |
| 567 | unsigned int data_log1, data_log2; |
| 568 | unsigned int addr_log1, addr_log2; |
| 569 | unsigned char stat, count, syndrome, check; |
| 570 | |
| 571 | stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS); |
| 572 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 573 | data_log1 = le32_to_cpu(readl(card->csr_remap + |
| 574 | ERROR_DATA_LOG)); |
| 575 | data_log2 = le32_to_cpu(readl(card->csr_remap + |
| 576 | ERROR_DATA_LOG + 4)); |
| 577 | addr_log1 = le32_to_cpu(readl(card->csr_remap + |
| 578 | ERROR_ADDR_LOG)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4); |
| 580 | |
| 581 | count = readb(card->csr_remap + ERROR_COUNT); |
| 582 | syndrome = readb(card->csr_remap + ERROR_SYNDROME); |
| 583 | check = readb(card->csr_remap + ERROR_CHECK); |
| 584 | |
| 585 | dump_dmastat(card, dma_status); |
| 586 | |
| 587 | if (stat & 0x01) |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 588 | dev_printk(KERN_ERR, &card->dev->dev, |
| 589 | "Memory access error detected (err count %d)\n", |
| 590 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (stat & 0x02) |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 592 | dev_printk(KERN_ERR, &card->dev->dev, |
| 593 | "Multi-bit EDC error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 595 | dev_printk(KERN_ERR, &card->dev->dev, |
| 596 | "Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n", |
| 597 | addr_log2, addr_log1, data_log2, data_log1); |
| 598 | dev_printk(KERN_ERR, &card->dev->dev, |
| 599 | "Fault Check 0x%02x, Fault Syndrome 0x%02x\n", |
| 600 | check, syndrome); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | writeb(0, card->csr_remap + ERROR_COUNT); |
| 603 | } |
| 604 | |
| 605 | if (dma_status & DMASCR_PARITY_ERR_REP) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 606 | dev_printk(KERN_ERR, &card->dev->dev, |
| 607 | "PARITY ERROR REPORTED\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | pci_read_config_word(card->dev, PCI_STATUS, &cfg_status); |
| 609 | pci_write_config_word(card->dev, PCI_STATUS, cfg_status); |
| 610 | } |
| 611 | |
| 612 | if (dma_status & DMASCR_PARITY_ERR_DET) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 613 | dev_printk(KERN_ERR, &card->dev->dev, |
| 614 | "PARITY ERROR DETECTED\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | pci_read_config_word(card->dev, PCI_STATUS, &cfg_status); |
| 616 | pci_write_config_word(card->dev, PCI_STATUS, cfg_status); |
| 617 | } |
| 618 | |
| 619 | if (dma_status & DMASCR_SYSTEM_ERR_SIG) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 620 | dev_printk(KERN_ERR, &card->dev->dev, "SYSTEM ERROR\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | pci_read_config_word(card->dev, PCI_STATUS, &cfg_status); |
| 622 | pci_write_config_word(card->dev, PCI_STATUS, cfg_status); |
| 623 | } |
| 624 | |
| 625 | if (dma_status & DMASCR_TARGET_ABT) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 626 | dev_printk(KERN_ERR, &card->dev->dev, "TARGET ABORT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | pci_read_config_word(card->dev, PCI_STATUS, &cfg_status); |
| 628 | pci_write_config_word(card->dev, PCI_STATUS, cfg_status); |
| 629 | } |
| 630 | |
| 631 | if (dma_status & DMASCR_MASTER_ABT) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 632 | dev_printk(KERN_ERR, &card->dev->dev, "MASTER ABORT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | pci_read_config_word(card->dev, PCI_STATUS, &cfg_status); |
| 634 | pci_write_config_word(card->dev, PCI_STATUS, cfg_status); |
| 635 | } |
| 636 | |
| 637 | /* and process the DMA descriptors */ |
| 638 | card->dma_status = dma_status; |
| 639 | tasklet_schedule(&card->tasklet); |
| 640 | |
| 641 | HW_TRACE(0x36); |
| 642 | |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 643 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* |
| 647 | * If both batteries are good, no LED |
| 648 | * If either battery has been warned, solid LED |
| 649 | * If both batteries are bad, flash the LED quickly |
| 650 | * If either battery is bad, flash the LED semi quickly |
| 651 | */ |
| 652 | static void set_fault_to_battery_status(struct cardinfo *card) |
| 653 | { |
| 654 | if (card->battery[0].good && card->battery[1].good) |
| 655 | set_led(card, LED_FAULT, LED_OFF); |
| 656 | else if (card->battery[0].warned || card->battery[1].warned) |
| 657 | set_led(card, LED_FAULT, LED_ON); |
| 658 | else if (!card->battery[0].good && !card->battery[1].good) |
| 659 | set_led(card, LED_FAULT, LED_FLASH_7_0); |
| 660 | else |
| 661 | set_led(card, LED_FAULT, LED_FLASH_3_5); |
| 662 | } |
| 663 | |
| 664 | static void init_battery_timer(void); |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | static int check_battery(struct cardinfo *card, int battery, int status) |
| 667 | { |
| 668 | if (status != card->battery[battery].good) { |
| 669 | card->battery[battery].good = !card->battery[battery].good; |
| 670 | card->battery[battery].last_change = jiffies; |
| 671 | |
| 672 | if (card->battery[battery].good) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 673 | dev_printk(KERN_ERR, &card->dev->dev, |
| 674 | "Battery %d now good\n", battery + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | card->battery[battery].warned = 0; |
| 676 | } else |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 677 | dev_printk(KERN_ERR, &card->dev->dev, |
| 678 | "Battery %d now FAILED\n", battery + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
| 680 | return 1; |
| 681 | } else if (!card->battery[battery].good && |
| 682 | !card->battery[battery].warned && |
| 683 | time_after_eq(jiffies, card->battery[battery].last_change + |
| 684 | (HZ * 60 * 60 * 5))) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 685 | dev_printk(KERN_ERR, &card->dev->dev, |
| 686 | "Battery %d still FAILED after 5 hours\n", battery + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | card->battery[battery].warned = 1; |
| 688 | |
| 689 | return 1; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | static void check_batteries(struct cardinfo *card) |
| 696 | { |
| 697 | /* NOTE: this must *never* be called while the card |
| 698 | * is doing (bus-to-card) DMA, or you will need the |
| 699 | * reset switch |
| 700 | */ |
| 701 | unsigned char status; |
| 702 | int ret1, ret2; |
| 703 | |
| 704 | status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY); |
| 705 | if (debug & DEBUG_BATTERY_POLLING) |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 706 | dev_printk(KERN_DEBUG, &card->dev->dev, |
| 707 | "checking battery status, 1 = %s, 2 = %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK", |
| 709 | (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK"); |
| 710 | |
| 711 | ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE)); |
| 712 | ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE)); |
| 713 | |
| 714 | if (ret1 || ret2) |
| 715 | set_fault_to_battery_status(card); |
| 716 | } |
| 717 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 718 | static void check_all_batteries(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | { |
| 720 | int i; |
| 721 | |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 722 | for (i = 0; i < num_cards; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if (!(cards[i].flags & UM_FLAG_NO_BATT)) { |
| 724 | struct cardinfo *card = &cards[i]; |
| 725 | spin_lock_bh(&card->lock); |
| 726 | if (card->Active >= 0) |
| 727 | card->check_batteries = 1; |
| 728 | else |
| 729 | check_batteries(card); |
| 730 | spin_unlock_bh(&card->lock); |
| 731 | } |
| 732 | |
| 733 | init_battery_timer(); |
| 734 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | static void init_battery_timer(void) |
| 737 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 738 | timer_setup(&battery_timer, check_all_batteries, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | battery_timer.expires = jiffies + (HZ * 60); |
| 740 | add_timer(&battery_timer); |
| 741 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | static void del_battery_timer(void) |
| 744 | { |
| 745 | del_timer(&battery_timer); |
| 746 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | /* |
| 749 | * Note no locks taken out here. In a worst case scenario, we could drop |
| 750 | * a chunk of system memory. But that should never happen, since validation |
| 751 | * happens at open or mount time, when locks are held. |
| 752 | * |
| 753 | * That's crap, since doing that while some partitions are opened |
| 754 | * or mounted will give you really nasty results. |
| 755 | */ |
| 756 | static int mm_revalidate(struct gendisk *disk) |
| 757 | { |
| 758 | struct cardinfo *card = disk->private_data; |
| 759 | set_capacity(disk, card->mm_size << 1); |
| 760 | return 0; |
| 761 | } |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 762 | |
| 763 | static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 765 | struct cardinfo *card = bdev->bd_disk->private_data; |
| 766 | int size = card->mm_size * (1024 / MM_HARDSECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 768 | /* |
| 769 | * get geometry: we have to fake one... trim the size to a |
| 770 | * multiple of 2048 (1M): tell we have 32 sectors, 64 heads, |
| 771 | * whatever cylinders. |
| 772 | */ |
| 773 | geo->heads = 64; |
| 774 | geo->sectors = 32; |
| 775 | geo->cylinders = size / (geo->heads * geo->sectors); |
| 776 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 778 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 779 | static const struct block_device_operations mm_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | .owner = THIS_MODULE, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 781 | .getgeo = mm_getgeo, |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 782 | .revalidate_disk = mm_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | }; |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 784 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 785 | static int mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
| 787 | int ret = -ENODEV; |
| 788 | struct cardinfo *card = &cards[num_cards]; |
| 789 | unsigned char mem_present; |
| 790 | unsigned char batt_status; |
| 791 | unsigned int saved_bar, data; |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 792 | unsigned long csr_base; |
| 793 | unsigned long csr_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | int magic_number; |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 795 | static int printed_version; |
| 796 | |
| 797 | if (!printed_version++) |
| 798 | printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 800 | ret = pci_enable_device(dev); |
| 801 | if (ret) |
| 802 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8); |
| 805 | pci_set_master(dev); |
| 806 | |
| 807 | card->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 809 | csr_base = pci_resource_start(dev, 0); |
| 810 | csr_len = pci_resource_len(dev, 0); |
| 811 | if (!csr_base || !csr_len) |
| 812 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 814 | dev_printk(KERN_INFO, &dev->dev, |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 815 | "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 817 | if (dma_set_mask(&dev->dev, DMA_BIT_MASK(64)) && |
| 818 | dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 819 | dev_printk(KERN_WARNING, &dev->dev, "NO suitable DMA found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | return -ENOMEM; |
| 821 | } |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 822 | |
| 823 | ret = pci_request_regions(dev, DRIVER_NAME); |
| 824 | if (ret) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 825 | dev_printk(KERN_ERR, &card->dev->dev, |
| 826 | "Unable to request memory region\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | goto failed_req_csr; |
| 828 | } |
| 829 | |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 830 | card->csr_remap = ioremap_nocache(csr_base, csr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if (!card->csr_remap) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 832 | dev_printk(KERN_ERR, &card->dev->dev, |
| 833 | "Unable to remap memory region\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | ret = -ENOMEM; |
| 835 | |
| 836 | goto failed_remap_csr; |
| 837 | } |
| 838 | |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 839 | dev_printk(KERN_INFO, &card->dev->dev, |
| 840 | "CSR 0x%08lx -> 0x%p (0x%lx)\n", |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 841 | csr_base, card->csr_remap, csr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 843 | switch (card->dev->device) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | case 0x5415: |
| 845 | card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG; |
| 846 | magic_number = 0x59; |
| 847 | break; |
| 848 | |
| 849 | case 0x5425: |
| 850 | card->flags |= UM_FLAG_NO_BYTE_STATUS; |
| 851 | magic_number = 0x5C; |
| 852 | break; |
| 853 | |
| 854 | case 0x6155: |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 855 | card->flags |= UM_FLAG_NO_BYTE_STATUS | |
| 856 | UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | magic_number = 0x99; |
| 858 | break; |
| 859 | |
| 860 | default: |
| 861 | magic_number = 0x100; |
| 862 | break; |
| 863 | } |
| 864 | |
| 865 | if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 866 | dev_printk(KERN_ERR, &card->dev->dev, "Magic number invalid\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | ret = -ENOMEM; |
| 868 | goto failed_magic; |
| 869 | } |
| 870 | |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 871 | card->mm_pages[0].desc = dma_alloc_coherent(&card->dev->dev, |
| 872 | PAGE_SIZE * 2, &card->mm_pages[0].page_dma, GFP_KERNEL); |
| 873 | card->mm_pages[1].desc = dma_alloc_coherent(&card->dev->dev, |
| 874 | PAGE_SIZE * 2, &card->mm_pages[1].page_dma, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | if (card->mm_pages[0].desc == NULL || |
| 876 | card->mm_pages[1].desc == NULL) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 877 | dev_printk(KERN_ERR, &card->dev->dev, "alloc failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | goto failed_alloc; |
| 879 | } |
| 880 | reset_page(&card->mm_pages[0]); |
| 881 | reset_page(&card->mm_pages[1]); |
| 882 | card->Ready = 0; /* page 0 is ready */ |
| 883 | card->Active = -1; /* no page is active */ |
| 884 | card->bio = NULL; |
| 885 | card->biotail = &card->bio; |
Bart Van Assche | 498f665 | 2018-02-28 10:15:32 -0800 | [diff] [blame] | 886 | spin_lock_init(&card->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Christoph Hellwig | 6d46964 | 2018-11-14 17:02:18 +0100 | [diff] [blame] | 888 | card->queue = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | if (!card->queue) |
| 890 | goto failed_alloc; |
| 891 | |
| 892 | blk_queue_make_request(card->queue, mm_make_request); |
| 893 | card->queue->queuedata = card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | tasklet_init(&card->tasklet, process_page, (unsigned long)card); |
| 896 | |
| 897 | card->check_batteries = 0; |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY); |
| 900 | switch (mem_present) { |
| 901 | case MEM_128_MB: |
| 902 | card->mm_size = 1024 * 128; |
| 903 | break; |
| 904 | case MEM_256_MB: |
| 905 | card->mm_size = 1024 * 256; |
| 906 | break; |
| 907 | case MEM_512_MB: |
| 908 | card->mm_size = 1024 * 512; |
| 909 | break; |
| 910 | case MEM_1_GB: |
| 911 | card->mm_size = 1024 * 1024; |
| 912 | break; |
| 913 | case MEM_2_GB: |
| 914 | card->mm_size = 1024 * 2048; |
| 915 | break; |
| 916 | default: |
| 917 | card->mm_size = 0; |
| 918 | break; |
| 919 | } |
| 920 | |
| 921 | /* Clear the LED's we control */ |
| 922 | set_led(card, LED_REMOVE, LED_OFF); |
| 923 | set_led(card, LED_FAULT, LED_OFF); |
| 924 | |
| 925 | batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY); |
| 926 | |
| 927 | card->battery[0].good = !(batt_status & BATTERY_1_FAILURE); |
| 928 | card->battery[1].good = !(batt_status & BATTERY_2_FAILURE); |
| 929 | card->battery[0].last_change = card->battery[1].last_change = jiffies; |
| 930 | |
Jeff Garzik | 4e953a2 | 2007-09-27 07:41:50 -0400 | [diff] [blame] | 931 | if (card->flags & UM_FLAG_NO_BATT) |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 932 | dev_printk(KERN_INFO, &card->dev->dev, |
| 933 | "Size %d KB\n", card->mm_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | else { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 935 | dev_printk(KERN_INFO, &card->dev->dev, |
| 936 | "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n", |
| 937 | card->mm_size, |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 938 | batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | card->battery[0].good ? "OK" : "FAILURE", |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 940 | batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | card->battery[1].good ? "OK" : "FAILURE"); |
| 942 | |
| 943 | set_fault_to_battery_status(card); |
| 944 | } |
| 945 | |
| 946 | pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar); |
| 947 | data = 0xffffffff; |
| 948 | pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data); |
| 949 | pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data); |
| 950 | pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar); |
| 951 | data &= 0xfffffff0; |
| 952 | data = ~data; |
| 953 | data += 1; |
| 954 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 955 | if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, DRIVER_NAME, |
| 956 | card)) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 957 | dev_printk(KERN_ERR, &card->dev->dev, |
| 958 | "Unable to allocate IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | ret = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | goto failed_req_irq; |
| 961 | } |
| 962 | |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 963 | dev_printk(KERN_INFO, &card->dev->dev, |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 964 | "Window size %d bytes, IRQ %d\n", data, dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | pci_set_drvdata(dev, card); |
| 967 | |
| 968 | if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */ |
| 969 | pci_write_cmd = 0x07; /* then Memory Write command */ |
| 970 | |
| 971 | if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */ |
| 972 | unsigned short cfg_command; |
| 973 | pci_read_config_word(dev, PCI_COMMAND, &cfg_command); |
| 974 | cfg_command |= 0x10; /* Memory Write & Invalidate Enable */ |
| 975 | pci_write_config_word(dev, PCI_COMMAND, cfg_command); |
| 976 | } |
| 977 | pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24); |
| 978 | |
| 979 | num_cards++; |
| 980 | |
| 981 | if (!get_userbit(card, MEMORY_INITIALIZED)) { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 982 | dev_printk(KERN_INFO, &card->dev->dev, |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 983 | "memory NOT initialized. Consider over-writing whole device.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | card->init_size = 0; |
| 985 | } else { |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 986 | dev_printk(KERN_INFO, &card->dev->dev, |
| 987 | "memory already initialized\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | card->init_size = card->mm_size; |
| 989 | } |
| 990 | |
| 991 | /* Enable ECC */ |
| 992 | writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL); |
| 993 | |
| 994 | return 0; |
| 995 | |
| 996 | failed_req_irq: |
| 997 | failed_alloc: |
| 998 | if (card->mm_pages[0].desc) |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 999 | dma_free_coherent(&card->dev->dev, PAGE_SIZE * 2, |
| 1000 | card->mm_pages[0].desc, |
| 1001 | card->mm_pages[0].page_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | if (card->mm_pages[1].desc) |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 1003 | dma_free_coherent(&card->dev->dev, PAGE_SIZE * 2, |
| 1004 | card->mm_pages[1].desc, |
| 1005 | card->mm_pages[1].page_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | failed_magic: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | iounmap(card->csr_remap); |
| 1008 | failed_remap_csr: |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 1009 | pci_release_regions(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | failed_req_csr: |
| 1011 | |
| 1012 | return ret; |
| 1013 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | static void mm_pci_remove(struct pci_dev *dev) |
| 1016 | { |
| 1017 | struct cardinfo *card = pci_get_drvdata(dev); |
| 1018 | |
| 1019 | tasklet_kill(&card->tasklet); |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 1020 | free_irq(dev->irq, card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | iounmap(card->csr_remap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | |
| 1023 | if (card->mm_pages[0].desc) |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 1024 | dma_free_coherent(&card->dev->dev, PAGE_SIZE * 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | card->mm_pages[0].desc, |
| 1026 | card->mm_pages[0].page_dma); |
| 1027 | if (card->mm_pages[1].desc) |
Christoph Hellwig | b46d40d | 2018-10-18 15:15:14 +0200 | [diff] [blame] | 1028 | dma_free_coherent(&card->dev->dev, PAGE_SIZE * 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | card->mm_pages[1].desc, |
| 1030 | card->mm_pages[1].page_dma); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 1031 | blk_cleanup_queue(card->queue); |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 1032 | |
| 1033 | pci_release_regions(dev); |
| 1034 | pci_disable_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Neil Brown | 5874c18 | 2007-07-13 07:39:46 +0200 | [diff] [blame] | 1037 | static const struct pci_device_id mm_pci_ids[] = { |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 1038 | {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5415CN)}, |
| 1039 | {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5425CN)}, |
| 1040 | {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_6155)}, |
Neil Brown | 5874c18 | 2007-07-13 07:39:46 +0200 | [diff] [blame] | 1041 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | .vendor = 0x8086, |
| 1043 | .device = 0xB555, |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 1044 | .subvendor = 0x1332, |
| 1045 | .subdevice = 0x5460, |
| 1046 | .class = 0x050000, |
| 1047 | .class_mask = 0, |
Neil Brown | 5874c18 | 2007-07-13 07:39:46 +0200 | [diff] [blame] | 1048 | }, { /* end: all zeroes */ } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | MODULE_DEVICE_TABLE(pci, mm_pci_ids); |
| 1052 | |
| 1053 | static struct pci_driver mm_pci_driver = { |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 1054 | .name = DRIVER_NAME, |
| 1055 | .id_table = mm_pci_ids, |
| 1056 | .probe = mm_pci_probe, |
| 1057 | .remove = mm_pci_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | }; |
Jeff Garzik | ee4a7b6 | 2007-09-27 07:40:33 -0400 | [diff] [blame] | 1059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | static int __init mm_init(void) |
| 1061 | { |
| 1062 | int retval, i; |
| 1063 | int err; |
| 1064 | |
Richard Knutsson | 9bfab8c | 2005-11-30 00:59:34 +0100 | [diff] [blame] | 1065 | retval = pci_register_driver(&mm_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | if (retval) |
| 1067 | return -ENOMEM; |
| 1068 | |
Jeff Garzik | cb3503c | 2007-09-27 07:49:39 -0400 | [diff] [blame] | 1069 | err = major_nr = register_blkdev(0, DRIVER_NAME); |
NeilBrown | 5a243e0 | 2007-02-28 20:11:12 -0800 | [diff] [blame] | 1070 | if (err < 0) { |
| 1071 | pci_unregister_driver(&mm_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | return -EIO; |
NeilBrown | 5a243e0 | 2007-02-28 20:11:12 -0800 | [diff] [blame] | 1073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
| 1075 | for (i = 0; i < num_cards; i++) { |
| 1076 | mm_gendisk[i] = alloc_disk(1 << MM_SHIFT); |
| 1077 | if (!mm_gendisk[i]) |
| 1078 | goto out; |
| 1079 | } |
| 1080 | |
| 1081 | for (i = 0; i < num_cards; i++) { |
| 1082 | struct gendisk *disk = mm_gendisk[i]; |
| 1083 | sprintf(disk->disk_name, "umem%c", 'a'+i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | spin_lock_init(&cards[i].lock); |
| 1085 | disk->major = major_nr; |
| 1086 | disk->first_minor = i << MM_SHIFT; |
| 1087 | disk->fops = &mm_fops; |
| 1088 | disk->private_data = &cards[i]; |
| 1089 | disk->queue = cards[i].queue; |
| 1090 | set_capacity(disk, cards[i].mm_size << 1); |
| 1091 | add_disk(disk); |
| 1092 | } |
| 1093 | |
| 1094 | init_battery_timer(); |
Jeff Garzik | 4e0af88 | 2007-09-27 06:41:25 -0400 | [diff] [blame] | 1095 | printk(KERN_INFO "MM: desc_per_page = %ld\n", DESC_PER_PAGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | /* printk("mm_init: Done. 10-19-01 9:00\n"); */ |
| 1097 | return 0; |
| 1098 | |
| 1099 | out: |
NeilBrown | 5a243e0 | 2007-02-28 20:11:12 -0800 | [diff] [blame] | 1100 | pci_unregister_driver(&mm_pci_driver); |
Jeff Garzik | cb3503c | 2007-09-27 07:49:39 -0400 | [diff] [blame] | 1101 | unregister_blkdev(major_nr, DRIVER_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | while (i--) |
| 1103 | put_disk(mm_gendisk[i]); |
| 1104 | return -ENOMEM; |
| 1105 | } |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | static void __exit mm_cleanup(void) |
| 1108 | { |
| 1109 | int i; |
| 1110 | |
| 1111 | del_battery_timer(); |
| 1112 | |
Randy Dunlap | 458cf5e | 2007-12-17 20:24:20 +0100 | [diff] [blame] | 1113 | for (i = 0; i < num_cards ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | del_gendisk(mm_gendisk[i]); |
| 1115 | put_disk(mm_gendisk[i]); |
| 1116 | } |
| 1117 | |
| 1118 | pci_unregister_driver(&mm_pci_driver); |
| 1119 | |
Jeff Garzik | cb3503c | 2007-09-27 07:49:39 -0400 | [diff] [blame] | 1120 | unregister_blkdev(major_nr, DRIVER_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | module_init(mm_init); |
| 1124 | module_exit(mm_cleanup); |
| 1125 | |
| 1126 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1127 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1128 | MODULE_LICENSE("GPL"); |