]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
59bca8cc BZ |
2 | * Copyright (C) 1994-1998 Linus Torvalds & authors (see below) |
3 | * Copyright (C) 1998-2002 Linux ATA Development | |
4 | * Andre Hedrick <andre@linux-ide.org> | |
5 | * Copyright (C) 2003 Red Hat <alan@redhat.com> | |
6 | * Copyright (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz | |
1da177e4 LT |
7 | */ |
8 | ||
9 | /* | |
10 | * Mostly written by Mark Lord <mlord@pobox.com> | |
11 | * and Gadi Oxman <gadio@netvision.net.il> | |
12 | * and Andre Hedrick <andre@linux-ide.org> | |
13 | * | |
14 | * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c. | |
1da177e4 LT |
15 | */ |
16 | ||
17 | #define IDEDISK_VERSION "1.18" | |
18 | ||
1da177e4 LT |
19 | #include <linux/module.h> |
20 | #include <linux/types.h> | |
21 | #include <linux/string.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/timer.h> | |
24 | #include <linux/mm.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/major.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/genhd.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/delay.h> | |
cf8b8975 | 31 | #include <linux/mutex.h> |
2bfb646c | 32 | #include <linux/leds.h> |
1da177e4 | 33 | #include <linux/ide.h> |
3ceca727 | 34 | #include <linux/hdreg.h> |
1da177e4 LT |
35 | |
36 | #include <asm/byteorder.h> | |
37 | #include <asm/irq.h> | |
38 | #include <asm/uaccess.h> | |
39 | #include <asm/io.h> | |
40 | #include <asm/div64.h> | |
41 | ||
870d6656 | 42 | #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT) |
689d6fac | 43 | #define IDE_DISK_MINORS (1 << PARTN_BITS) |
870d6656 | 44 | #else |
3e1a7ff8 | 45 | #define IDE_DISK_MINORS 0 |
870d6656 TH |
46 | #endif |
47 | ||
1da177e4 LT |
48 | struct ide_disk_obj { |
49 | ide_drive_t *drive; | |
50 | ide_driver_t *driver; | |
51 | struct gendisk *disk; | |
52 | struct kref kref; | |
c94964a4 | 53 | unsigned int openers; /* protected by BKL for now */ |
1da177e4 LT |
54 | }; |
55 | ||
cf8b8975 | 56 | static DEFINE_MUTEX(idedisk_ref_mutex); |
1da177e4 LT |
57 | |
58 | #define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref) | |
59 | ||
60 | #define ide_disk_g(disk) \ | |
61 | container_of((disk)->private_data, struct ide_disk_obj, driver) | |
62 | ||
08da591e BZ |
63 | static void ide_disk_release(struct kref *); |
64 | ||
1da177e4 LT |
65 | static struct ide_disk_obj *ide_disk_get(struct gendisk *disk) |
66 | { | |
67 | struct ide_disk_obj *idkp = NULL; | |
68 | ||
cf8b8975 | 69 | mutex_lock(&idedisk_ref_mutex); |
1da177e4 | 70 | idkp = ide_disk_g(disk); |
08da591e | 71 | if (idkp) { |
d3e33ff5 | 72 | if (ide_device_get(idkp->drive)) |
08da591e | 73 | idkp = NULL; |
d3e33ff5 BZ |
74 | else |
75 | kref_get(&idkp->kref); | |
08da591e | 76 | } |
cf8b8975 | 77 | mutex_unlock(&idedisk_ref_mutex); |
1da177e4 LT |
78 | return idkp; |
79 | } | |
80 | ||
1da177e4 LT |
81 | static void ide_disk_put(struct ide_disk_obj *idkp) |
82 | { | |
d3e33ff5 BZ |
83 | ide_drive_t *drive = idkp->drive; |
84 | ||
cf8b8975 | 85 | mutex_lock(&idedisk_ref_mutex); |
1da177e4 | 86 | kref_put(&idkp->kref, ide_disk_release); |
d3e33ff5 | 87 | ide_device_put(drive); |
cf8b8975 | 88 | mutex_unlock(&idedisk_ref_mutex); |
1da177e4 LT |
89 | } |
90 | ||
ba76ae38 | 91 | static const u8 ide_rw_cmds[] = { |
aaaade3f BZ |
92 | ATA_CMD_READ_MULTI, |
93 | ATA_CMD_WRITE_MULTI, | |
94 | ATA_CMD_READ_MULTI_EXT, | |
95 | ATA_CMD_WRITE_MULTI_EXT, | |
96 | ATA_CMD_PIO_READ, | |
97 | ATA_CMD_PIO_WRITE, | |
98 | ATA_CMD_PIO_READ_EXT, | |
99 | ATA_CMD_PIO_WRITE_EXT, | |
100 | ATA_CMD_READ, | |
101 | ATA_CMD_WRITE, | |
102 | ATA_CMD_READ_EXT, | |
103 | ATA_CMD_WRITE_EXT, | |
ba76ae38 BZ |
104 | }; |
105 | ||
106 | static const u8 ide_data_phases[] = { | |
107 | TASKFILE_MULTI_IN, | |
108 | TASKFILE_MULTI_OUT, | |
109 | TASKFILE_IN, | |
110 | TASKFILE_OUT, | |
111 | TASKFILE_IN_DMA, | |
112 | TASKFILE_OUT_DMA, | |
113 | }; | |
114 | ||
115 | static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma) | |
116 | { | |
117 | u8 index, lba48, write; | |
118 | ||
119 | lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0; | |
120 | write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0; | |
121 | ||
122 | if (dma) | |
ba4b2e60 | 123 | index = 8; |
ba76ae38 BZ |
124 | else |
125 | index = drive->mult_count ? 0 : 4; | |
126 | ||
127 | task->tf.command = ide_rw_cmds[index + lba48 + write]; | |
128 | ||
129 | if (dma) | |
130 | index = 8; /* fixup index */ | |
131 | ||
132 | task->data_phase = ide_data_phases[index / 2 + write]; | |
133 | } | |
134 | ||
1da177e4 LT |
135 | /* |
136 | * __ide_do_rw_disk() issues READ and WRITE commands to a disk, | |
137 | * using LBA if supported, or CHS otherwise, to address sectors. | |
138 | */ | |
98416549 BZ |
139 | static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, |
140 | sector_t block) | |
1da177e4 LT |
141 | { |
142 | ide_hwif_t *hwif = HWIF(drive); | |
143 | unsigned int dma = drive->using_dma; | |
790d1239 | 144 | u16 nsectors = (u16)rq->nr_sectors; |
1da177e4 | 145 | u8 lba48 = (drive->addressing == 1) ? 1 : 0; |
9e42237f BZ |
146 | ide_task_t task; |
147 | struct ide_taskfile *tf = &task.tf; | |
f6e29e35 | 148 | ide_startstop_t rc; |
1da177e4 | 149 | |
238e4f14 | 150 | if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) { |
1da177e4 LT |
151 | if (block + rq->nr_sectors > 1ULL << 28) |
152 | dma = 0; | |
153 | else | |
154 | lba48 = 0; | |
155 | } | |
156 | ||
157 | if (!dma) { | |
158 | ide_init_sg_cmd(drive, rq); | |
159 | ide_map_sg(drive, rq); | |
160 | } | |
161 | ||
9e42237f | 162 | memset(&task, 0, sizeof(task)); |
9a410e79 | 163 | task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
2bd06b23 | 164 | |
1da177e4 LT |
165 | if (drive->select.b.lba) { |
166 | if (lba48) { | |
c2f8311d MR |
167 | pr_debug("%s: LBA=0x%012llx\n", drive->name, |
168 | (unsigned long long)block); | |
1da177e4 | 169 | |
790d1239 | 170 | tf->hob_nsect = (nsectors >> 8) & 0xff; |
2bd06b23 BZ |
171 | tf->hob_lbal = (u8)(block >> 24); |
172 | if (sizeof(block) != 4) { | |
173 | tf->hob_lbam = (u8)((u64)block >> 32); | |
174 | tf->hob_lbah = (u8)((u64)block >> 40); | |
1da177e4 | 175 | } |
2bd06b23 | 176 | |
790d1239 | 177 | tf->nsect = nsectors & 0xff; |
2bd06b23 BZ |
178 | tf->lbal = (u8) block; |
179 | tf->lbam = (u8)(block >> 8); | |
180 | tf->lbah = (u8)(block >> 16); | |
6dd9b837 | 181 | |
657cc1a8 | 182 | task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB); |
1da177e4 | 183 | } else { |
790d1239 | 184 | tf->nsect = nsectors & 0xff; |
2bd06b23 BZ |
185 | tf->lbal = block; |
186 | tf->lbam = block >>= 8; | |
187 | tf->lbah = block >>= 8; | |
188 | tf->device = (block >> 8) & 0xf; | |
1da177e4 LT |
189 | } |
190 | } else { | |
98416549 BZ |
191 | unsigned int sect, head, cyl, track; |
192 | ||
1da177e4 LT |
193 | track = (int)block / drive->sect; |
194 | sect = (int)block % drive->sect + 1; | |
1da177e4 LT |
195 | head = track % drive->head; |
196 | cyl = track / drive->head; | |
197 | ||
198 | pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect); | |
199 | ||
790d1239 | 200 | tf->nsect = nsectors & 0xff; |
2bd06b23 BZ |
201 | tf->lbal = sect; |
202 | tf->lbam = cyl; | |
203 | tf->lbah = cyl >> 8; | |
204 | tf->device = head; | |
1da177e4 LT |
205 | } |
206 | ||
ba76ae38 BZ |
207 | if (rq_data_dir(rq)) |
208 | task.tf_flags |= IDE_TFLAG_WRITE; | |
209 | ||
210 | ide_tf_set_cmd(drive, &task, dma); | |
f6e29e35 BZ |
211 | if (!dma) |
212 | hwif->data_phase = task.data_phase; | |
213 | task.rq = rq; | |
ba76ae38 | 214 | |
f6e29e35 | 215 | rc = do_rw_taskfile(drive, &task); |
2bd06b23 | 216 | |
f6e29e35 | 217 | if (rc == ide_stopped && dma) { |
1da177e4 | 218 | /* fallback to PIO */ |
f6e29e35 | 219 | task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK; |
ba76ae38 | 220 | ide_tf_set_cmd(drive, &task, 0); |
f6e29e35 | 221 | hwif->data_phase = task.data_phase; |
1da177e4 | 222 | ide_init_sg_cmd(drive, rq); |
f6e29e35 | 223 | rc = do_rw_taskfile(drive, &task); |
1da177e4 LT |
224 | } |
225 | ||
f6e29e35 | 226 | return rc; |
1da177e4 LT |
227 | } |
228 | ||
229 | /* | |
230 | * 268435455 == 137439 MB or 28bit limit | |
231 | * 320173056 == 163929 MB or 48bit addressing | |
232 | * 1073741822 == 549756 MB or 48bit addressing fake drive | |
233 | */ | |
234 | ||
98416549 BZ |
235 | static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq, |
236 | sector_t block) | |
1da177e4 LT |
237 | { |
238 | ide_hwif_t *hwif = HWIF(drive); | |
239 | ||
240 | BUG_ON(drive->blocked); | |
241 | ||
242 | if (!blk_fs_request(rq)) { | |
243 | blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command"); | |
244 | ide_end_request(drive, 0, 0); | |
245 | return ide_stopped; | |
246 | } | |
247 | ||
2bfb646c RP |
248 | ledtrig_ide_activity(); |
249 | ||
1da177e4 LT |
250 | pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n", |
251 | drive->name, rq_data_dir(rq) == READ ? "read" : "writ", | |
c2f8311d MR |
252 | (unsigned long long)block, rq->nr_sectors, |
253 | (unsigned long)rq->buffer); | |
1da177e4 LT |
254 | |
255 | if (hwif->rw_disk) | |
256 | hwif->rw_disk(drive, rq); | |
257 | ||
258 | return __ide_do_rw_disk(drive, rq, block); | |
259 | } | |
260 | ||
261 | /* | |
262 | * Queries for true maximum capacity of the drive. | |
263 | * Returns maximum LBA address (> 0) of the drive, 0 if failed. | |
264 | */ | |
7a3b7512 | 265 | static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48) |
1da177e4 LT |
266 | { |
267 | ide_task_t args; | |
650d841d | 268 | struct ide_taskfile *tf = &args.tf; |
7a3b7512 | 269 | u64 addr = 0; |
1da177e4 LT |
270 | |
271 | /* Create IDE/ATA command request structure */ | |
272 | memset(&args, 0, sizeof(ide_task_t)); | |
7a3b7512 | 273 | if (lba48) |
aaaade3f | 274 | tf->command = ATA_CMD_READ_NATIVE_MAX_EXT; |
7a3b7512 | 275 | else |
aaaade3f | 276 | tf->command = ATA_CMD_READ_NATIVE_MAX; |
650d841d | 277 | tf->device = ATA_LBA; |
657cc1a8 | 278 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
a3bbb9d8 | 279 | if (lba48) |
657cc1a8 | 280 | args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB); |
1da177e4 | 281 | /* submit command request */ |
9a3c49be | 282 | ide_no_data_taskfile(drive, &args); |
1da177e4 | 283 | |
1da177e4 | 284 | /* if OK, compute maximum address value */ |
a501633c BZ |
285 | if ((tf->status & 0x01) == 0) |
286 | addr = ide_get_lba_addr(tf, lba48) + 1; | |
650d841d | 287 | |
1da177e4 LT |
288 | return addr; |
289 | } | |
290 | ||
291 | /* | |
292 | * Sets maximum virtual LBA address of the drive. | |
293 | * Returns new maximum virtual LBA address (> 0) or 0 on failure. | |
294 | */ | |
7a3b7512 | 295 | static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48) |
1da177e4 LT |
296 | { |
297 | ide_task_t args; | |
650d841d | 298 | struct ide_taskfile *tf = &args.tf; |
7a3b7512 | 299 | u64 addr_set = 0; |
1da177e4 LT |
300 | |
301 | addr_req--; | |
302 | /* Create IDE/ATA command request structure */ | |
303 | memset(&args, 0, sizeof(ide_task_t)); | |
650d841d BZ |
304 | tf->lbal = (addr_req >> 0) & 0xff; |
305 | tf->lbam = (addr_req >>= 8) & 0xff; | |
306 | tf->lbah = (addr_req >>= 8) & 0xff; | |
7a3b7512 BZ |
307 | if (lba48) { |
308 | tf->hob_lbal = (addr_req >>= 8) & 0xff; | |
309 | tf->hob_lbam = (addr_req >>= 8) & 0xff; | |
310 | tf->hob_lbah = (addr_req >>= 8) & 0xff; | |
aaaade3f | 311 | tf->command = ATA_CMD_SET_MAX_EXT; |
7a3b7512 BZ |
312 | } else { |
313 | tf->device = (addr_req >>= 8) & 0x0f; | |
aaaade3f | 314 | tf->command = ATA_CMD_SET_MAX; |
7a3b7512 BZ |
315 | } |
316 | tf->device |= ATA_LBA; | |
657cc1a8 | 317 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
a3bbb9d8 | 318 | if (lba48) |
657cc1a8 | 319 | args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB); |
1da177e4 | 320 | /* submit command request */ |
9a3c49be | 321 | ide_no_data_taskfile(drive, &args); |
1da177e4 | 322 | /* if OK, compute maximum address value */ |
a501633c BZ |
323 | if ((tf->status & 0x01) == 0) |
324 | addr_set = ide_get_lba_addr(tf, lba48) + 1; | |
650d841d | 325 | |
1da177e4 LT |
326 | return addr_set; |
327 | } | |
328 | ||
329 | static unsigned long long sectors_to_MB(unsigned long long n) | |
330 | { | |
331 | n <<= 9; /* make it bytes */ | |
332 | do_div(n, 1000000); /* make it MB */ | |
333 | return n; | |
334 | } | |
335 | ||
b0244a00 BZ |
336 | /* |
337 | * Some disks report total number of sectors instead of | |
338 | * maximum sector address. We list them here. | |
339 | */ | |
340 | static const struct drive_list_entry hpa_list[] = { | |
341 | { "ST340823A", NULL }, | |
7062cdc5 | 342 | { "ST320413A", NULL }, |
b152fcd3 | 343 | { "ST310211A", NULL }, |
b0244a00 BZ |
344 | { NULL, NULL } |
345 | }; | |
346 | ||
858119e1 | 347 | static void idedisk_check_hpa(ide_drive_t *drive) |
1da177e4 LT |
348 | { |
349 | unsigned long long capacity, set_max; | |
942dcd85 | 350 | int lba48 = ata_id_lba48_enabled(drive->id); |
1da177e4 LT |
351 | |
352 | capacity = drive->capacity64; | |
7a3b7512 BZ |
353 | |
354 | set_max = idedisk_read_native_max_address(drive, lba48); | |
1da177e4 | 355 | |
b0244a00 BZ |
356 | if (ide_in_drive_list(drive->id, hpa_list)) { |
357 | /* | |
358 | * Since we are inclusive wrt to firmware revisions do this | |
359 | * extra check and apply the workaround only when needed. | |
360 | */ | |
361 | if (set_max == capacity + 1) | |
362 | set_max--; | |
363 | } | |
364 | ||
1da177e4 LT |
365 | if (set_max <= capacity) |
366 | return; | |
367 | ||
368 | printk(KERN_INFO "%s: Host Protected Area detected.\n" | |
369 | "\tcurrent capacity is %llu sectors (%llu MB)\n" | |
370 | "\tnative capacity is %llu sectors (%llu MB)\n", | |
371 | drive->name, | |
372 | capacity, sectors_to_MB(capacity), | |
373 | set_max, sectors_to_MB(set_max)); | |
374 | ||
7a3b7512 BZ |
375 | set_max = idedisk_set_max_address(drive, set_max, lba48); |
376 | ||
1da177e4 LT |
377 | if (set_max) { |
378 | drive->capacity64 = set_max; | |
379 | printk(KERN_INFO "%s: Host Protected Area disabled.\n", | |
380 | drive->name); | |
381 | } | |
382 | } | |
383 | ||
98416549 | 384 | static void init_idedisk_capacity(ide_drive_t *drive) |
1da177e4 | 385 | { |
4dde4492 | 386 | u16 *id = drive->id; |
1da177e4 LT |
387 | /* |
388 | * If this drive supports the Host Protected Area feature set, | |
389 | * then we may need to change our opinion about the drive's capacity. | |
390 | */ | |
f41891c1 | 391 | int hpa = ata_id_hpa_enabled(id); |
1da177e4 | 392 | |
942dcd85 | 393 | if (ata_id_lba48_enabled(id)) { |
1da177e4 LT |
394 | /* drive speaks 48-bit LBA */ |
395 | drive->select.b.lba = 1; | |
48fb2688 | 396 | drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2); |
1da177e4 LT |
397 | if (hpa) |
398 | idedisk_check_hpa(drive); | |
a02227c9 | 399 | } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) { |
1da177e4 LT |
400 | /* drive speaks 28-bit LBA */ |
401 | drive->select.b.lba = 1; | |
48fb2688 | 402 | drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY); |
1da177e4 LT |
403 | if (hpa) |
404 | idedisk_check_hpa(drive); | |
405 | } else { | |
406 | /* drive speaks boring old 28-bit CHS */ | |
407 | drive->capacity64 = drive->cyl * drive->head * drive->sect; | |
408 | } | |
409 | } | |
410 | ||
98416549 | 411 | static sector_t idedisk_capacity(ide_drive_t *drive) |
1da177e4 | 412 | { |
3c619ffd | 413 | return drive->capacity64; |
1da177e4 LT |
414 | } |
415 | ||
ecfd80e4 | 416 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 LT |
417 | static int smart_enable(ide_drive_t *drive) |
418 | { | |
419 | ide_task_t args; | |
650d841d | 420 | struct ide_taskfile *tf = &args.tf; |
1da177e4 LT |
421 | |
422 | memset(&args, 0, sizeof(ide_task_t)); | |
aaaade3f BZ |
423 | tf->feature = ATA_SMART_ENABLE; |
424 | tf->lbam = ATA_SMART_LBAM_PASS; | |
425 | tf->lbah = ATA_SMART_LBAH_PASS; | |
426 | tf->command = ATA_CMD_SMART; | |
657cc1a8 | 427 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
9a3c49be | 428 | return ide_no_data_taskfile(drive, &args); |
1da177e4 LT |
429 | } |
430 | ||
43e7c0c4 | 431 | static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd) |
1da177e4 LT |
432 | { |
433 | ide_task_t args; | |
650d841d | 434 | struct ide_taskfile *tf = &args.tf; |
1da177e4 LT |
435 | |
436 | memset(&args, 0, sizeof(ide_task_t)); | |
650d841d BZ |
437 | tf->feature = sub_cmd; |
438 | tf->nsect = 0x01; | |
aaaade3f BZ |
439 | tf->lbam = ATA_SMART_LBAM_PASS; |
440 | tf->lbah = ATA_SMART_LBAH_PASS; | |
441 | tf->command = ATA_CMD_SMART; | |
657cc1a8 | 442 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
ac026ff2 | 443 | args.data_phase = TASKFILE_IN; |
1da177e4 | 444 | (void) smart_enable(drive); |
ac026ff2 | 445 | return ide_raw_taskfile(drive, &args, buf, 1); |
1da177e4 LT |
446 | } |
447 | ||
448 | static int proc_idedisk_read_cache | |
449 | (char *page, char **start, off_t off, int count, int *eof, void *data) | |
450 | { | |
451 | ide_drive_t *drive = (ide_drive_t *) data; | |
452 | char *out = page; | |
453 | int len; | |
454 | ||
455 | if (drive->id_read) | |
4dde4492 | 456 | len = sprintf(out, "%i\n", drive->id[ATA_ID_BUF_SIZE] / 2); |
1da177e4 | 457 | else |
98416549 BZ |
458 | len = sprintf(out, "(none)\n"); |
459 | ||
460 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); | |
1da177e4 LT |
461 | } |
462 | ||
463 | static int proc_idedisk_read_capacity | |
464 | (char *page, char **start, off_t off, int count, int *eof, void *data) | |
465 | { | |
466 | ide_drive_t*drive = (ide_drive_t *)data; | |
467 | int len; | |
468 | ||
98416549 BZ |
469 | len = sprintf(page, "%llu\n", (long long)idedisk_capacity(drive)); |
470 | ||
471 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); | |
1da177e4 LT |
472 | } |
473 | ||
799ee57a BZ |
474 | static int proc_idedisk_read_smart(char *page, char **start, off_t off, |
475 | int count, int *eof, void *data, u8 sub_cmd) | |
1da177e4 LT |
476 | { |
477 | ide_drive_t *drive = (ide_drive_t *)data; | |
478 | int len = 0, i = 0; | |
479 | ||
799ee57a | 480 | if (get_smart_data(drive, page, sub_cmd) == 0) { |
1da177e4 | 481 | unsigned short *val = (unsigned short *) page; |
151a6701 BZ |
482 | char *out = (char *)val + SECTOR_SIZE; |
483 | ||
1da177e4 LT |
484 | page = out; |
485 | do { | |
98416549 BZ |
486 | out += sprintf(out, "%04x%c", le16_to_cpu(*val), |
487 | (++i & 7) ? ' ' : '\n'); | |
1da177e4 | 488 | val += 1; |
151a6701 | 489 | } while (i < SECTOR_SIZE / 2); |
1da177e4 LT |
490 | len = out - page; |
491 | } | |
98416549 BZ |
492 | |
493 | PROC_IDE_READ_RETURN(page, start, off, count, eof, len); | |
1da177e4 LT |
494 | } |
495 | ||
799ee57a | 496 | static int proc_idedisk_read_sv |
1da177e4 LT |
497 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
498 | { | |
799ee57a | 499 | return proc_idedisk_read_smart(page, start, off, count, eof, data, |
aaaade3f | 500 | ATA_SMART_READ_VALUES); |
799ee57a | 501 | } |
1da177e4 | 502 | |
799ee57a BZ |
503 | static int proc_idedisk_read_st |
504 | (char *page, char **start, off_t off, int count, int *eof, void *data) | |
505 | { | |
506 | return proc_idedisk_read_smart(page, start, off, count, eof, data, | |
aaaade3f | 507 | ATA_SMART_READ_THRESHOLDS); |
1da177e4 LT |
508 | } |
509 | ||
510 | static ide_proc_entry_t idedisk_proc[] = { | |
799ee57a BZ |
511 | { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL }, |
512 | { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL }, | |
513 | { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL }, | |
514 | { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_sv, NULL }, | |
515 | { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_st, NULL }, | |
1da177e4 LT |
516 | { NULL, 0, NULL, NULL } |
517 | }; | |
ecfd80e4 | 518 | #endif /* CONFIG_IDE_PROC_FS */ |
1da177e4 | 519 | |
165125e1 | 520 | static void idedisk_prepare_flush(struct request_queue *q, struct request *rq) |
1da177e4 LT |
521 | { |
522 | ide_drive_t *drive = q->queuedata; | |
395d8ef5 | 523 | ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC); |
1da177e4 | 524 | |
395d8ef5 BZ |
525 | /* FIXME: map struct ide_taskfile on rq->cmd[] */ |
526 | BUG_ON(task == NULL); | |
527 | ||
528 | memset(task, 0, sizeof(*task)); | |
ff2779b5 | 529 | if (ata_id_flush_ext_enabled(drive->id) && |
1da177e4 | 530 | (drive->capacity64 >= (1UL << 28))) |
aaaade3f | 531 | task->tf.command = ATA_CMD_FLUSH_EXT; |
1da177e4 | 532 | else |
aaaade3f | 533 | task->tf.command = ATA_CMD_FLUSH; |
395d8ef5 BZ |
534 | task->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE | |
535 | IDE_TFLAG_DYN; | |
536 | task->data_phase = TASKFILE_NO_DATA; | |
1da177e4 | 537 | |
813a0eb2 | 538 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; |
4aff5e23 | 539 | rq->cmd_flags |= REQ_SOFTBARRIER; |
395d8ef5 | 540 | rq->special = task; |
1da177e4 LT |
541 | } |
542 | ||
8185d5aa BZ |
543 | ide_devset_get(multcount, mult_count); |
544 | ||
1da177e4 LT |
545 | /* |
546 | * This is tightly woven into the driver->do_special can not touch. | |
547 | * DON'T do it again until a total personality rewrite is committed. | |
548 | */ | |
549 | static int set_multcount(ide_drive_t *drive, int arg) | |
550 | { | |
dd47087b FT |
551 | struct request *rq; |
552 | int error; | |
1da177e4 | 553 | |
48fb2688 | 554 | if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff)) |
1497943e BZ |
555 | return -EINVAL; |
556 | ||
1da177e4 LT |
557 | if (drive->special.b.set_multmode) |
558 | return -EBUSY; | |
852738f3 | 559 | |
dd47087b FT |
560 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
561 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; | |
852738f3 | 562 | |
1da177e4 LT |
563 | drive->mult_req = arg; |
564 | drive->special.b.set_multmode = 1; | |
dd47087b FT |
565 | error = blk_execute_rq(drive->queue, NULL, rq, 0); |
566 | blk_put_request(rq); | |
98416549 | 567 | |
1da177e4 LT |
568 | return (drive->mult_count == arg) ? 0 : -EIO; |
569 | } | |
570 | ||
8185d5aa BZ |
571 | ide_devset_get(nowerr, nowerr); |
572 | ||
1da177e4 LT |
573 | static int set_nowerr(ide_drive_t *drive, int arg) |
574 | { | |
1497943e BZ |
575 | if (arg < 0 || arg > 1) |
576 | return -EINVAL; | |
577 | ||
1da177e4 LT |
578 | drive->nowerr = arg; |
579 | drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT; | |
1da177e4 LT |
580 | return 0; |
581 | } | |
582 | ||
3e087b57 TH |
583 | static void update_ordered(ide_drive_t *drive) |
584 | { | |
4dde4492 | 585 | u16 *id = drive->id; |
3e087b57 TH |
586 | unsigned ordered = QUEUE_ORDERED_NONE; |
587 | prepare_flush_fn *prep_fn = NULL; | |
3e087b57 TH |
588 | |
589 | if (drive->wcache) { | |
590 | unsigned long long capacity; | |
591 | int barrier; | |
592 | /* | |
593 | * We must avoid issuing commands a drive does not | |
594 | * understand or we may crash it. We check flush cache | |
595 | * is supported. We also check we have the LBA48 flush | |
596 | * cache if the drive capacity is too large. By this | |
597 | * time we have trimmed the drive capacity if LBA48 is | |
598 | * not available so we don't need to recheck that. | |
599 | */ | |
600 | capacity = idedisk_capacity(drive); | |
4b58f17d | 601 | barrier = ata_id_flush_enabled(id) && !drive->noflush && |
3e087b57 | 602 | (drive->addressing == 0 || capacity <= (1ULL << 28) || |
ff2779b5 | 603 | ata_id_flush_ext_enabled(id)); |
3e087b57 TH |
604 | |
605 | printk(KERN_INFO "%s: cache flushes %ssupported\n", | |
f7ad836c | 606 | drive->name, barrier ? "" : "not "); |
3e087b57 TH |
607 | |
608 | if (barrier) { | |
609 | ordered = QUEUE_ORDERED_DRAIN_FLUSH; | |
610 | prep_fn = idedisk_prepare_flush; | |
3e087b57 TH |
611 | } |
612 | } else | |
613 | ordered = QUEUE_ORDERED_DRAIN; | |
614 | ||
615 | blk_queue_ordered(drive->queue, ordered, prep_fn); | |
3e087b57 TH |
616 | } |
617 | ||
8185d5aa BZ |
618 | ide_devset_get(wcache, wcache); |
619 | ||
620 | static int set_wcache(ide_drive_t *drive, int arg) | |
1da177e4 LT |
621 | { |
622 | ide_task_t args; | |
3e087b57 | 623 | int err = 1; |
1da177e4 | 624 | |
1497943e BZ |
625 | if (arg < 0 || arg > 1) |
626 | return -EINVAL; | |
627 | ||
4b58f17d | 628 | if (ata_id_flush_enabled(drive->id)) { |
3e087b57 | 629 | memset(&args, 0, sizeof(ide_task_t)); |
650d841d | 630 | args.tf.feature = arg ? |
aaaade3f BZ |
631 | SETFEATURES_WC_ON : SETFEATURES_WC_OFF; |
632 | args.tf.command = ATA_CMD_SET_FEATURES; | |
657cc1a8 | 633 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
9a3c49be | 634 | err = ide_no_data_taskfile(drive, &args); |
3e087b57 TH |
635 | if (err == 0) |
636 | drive->wcache = arg; | |
637 | } | |
1da177e4 | 638 | |
3e087b57 | 639 | update_ordered(drive); |
1da177e4 | 640 | |
3e087b57 | 641 | return err; |
1da177e4 LT |
642 | } |
643 | ||
98416549 | 644 | static int do_idedisk_flushcache(ide_drive_t *drive) |
1da177e4 LT |
645 | { |
646 | ide_task_t args; | |
647 | ||
648 | memset(&args, 0, sizeof(ide_task_t)); | |
ff2779b5 | 649 | if (ata_id_flush_ext_enabled(drive->id)) |
aaaade3f | 650 | args.tf.command = ATA_CMD_FLUSH_EXT; |
1da177e4 | 651 | else |
aaaade3f | 652 | args.tf.command = ATA_CMD_FLUSH; |
657cc1a8 | 653 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
9a3c49be | 654 | return ide_no_data_taskfile(drive, &args); |
1da177e4 LT |
655 | } |
656 | ||
8185d5aa BZ |
657 | ide_devset_get(acoustic, acoustic); |
658 | ||
98416549 | 659 | static int set_acoustic(ide_drive_t *drive, int arg) |
1da177e4 LT |
660 | { |
661 | ide_task_t args; | |
662 | ||
1497943e BZ |
663 | if (arg < 0 || arg > 254) |
664 | return -EINVAL; | |
665 | ||
1da177e4 | 666 | memset(&args, 0, sizeof(ide_task_t)); |
aaaade3f | 667 | args.tf.feature = arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF; |
650d841d | 668 | args.tf.nsect = arg; |
aaaade3f | 669 | args.tf.command = ATA_CMD_SET_FEATURES; |
657cc1a8 | 670 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
9a3c49be | 671 | ide_no_data_taskfile(drive, &args); |
1da177e4 LT |
672 | drive->acoustic = arg; |
673 | return 0; | |
674 | } | |
675 | ||
aa768773 | 676 | ide_devset_get(addressing, addressing); |
8185d5aa | 677 | |
1da177e4 LT |
678 | /* |
679 | * drive->addressing: | |
680 | * 0: 28-bit | |
681 | * 1: 48-bit | |
682 | * 2: 48-bit capable doing 28-bit | |
683 | */ | |
aa768773 | 684 | static int set_addressing(ide_drive_t *drive, int arg) |
1da177e4 | 685 | { |
1497943e BZ |
686 | if (arg < 0 || arg > 2) |
687 | return -EINVAL; | |
688 | ||
1da177e4 LT |
689 | drive->addressing = 0; |
690 | ||
238e4f14 | 691 | if (drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) |
1da177e4 LT |
692 | return 0; |
693 | ||
942dcd85 | 694 | if (ata_id_lba48_enabled(drive->id) == 0) |
98416549 | 695 | return -EIO; |
942dcd85 | 696 | |
1da177e4 | 697 | drive->addressing = arg; |
942dcd85 | 698 | |
1da177e4 LT |
699 | return 0; |
700 | } | |
701 | ||
92f1f8fd EO |
702 | ide_devset_rw(acoustic, acoustic); |
703 | ide_devset_rw(address, addressing); | |
704 | ide_devset_rw(multcount, multcount); | |
705 | ide_devset_rw(wcache, wcache); | |
706 | ||
707 | ide_devset_rw_sync(nowerr, nowerr); | |
708 | ||
7662d046 | 709 | #ifdef CONFIG_IDE_PROC_FS |
92f1f8fd EO |
710 | ide_devset_rw_field(bios_cyl, bios_cyl); |
711 | ide_devset_rw_field(bios_head, bios_head); | |
712 | ide_devset_rw_field(bios_sect, bios_sect); | |
713 | ide_devset_rw_field(failures, failures); | |
714 | ide_devset_rw_field(lun, lun); | |
715 | ide_devset_rw_field(max_failures, max_failures); | |
716 | ||
717 | static const struct ide_proc_devset idedisk_settings[] = { | |
718 | IDE_PROC_DEVSET(acoustic, 0, 254), | |
719 | IDE_PROC_DEVSET(address, 0, 2), | |
720 | IDE_PROC_DEVSET(bios_cyl, 0, 65535), | |
721 | IDE_PROC_DEVSET(bios_head, 0, 255), | |
722 | IDE_PROC_DEVSET(bios_sect, 0, 63), | |
723 | IDE_PROC_DEVSET(failures, 0, 65535), | |
724 | IDE_PROC_DEVSET(lun, 0, 7), | |
725 | IDE_PROC_DEVSET(max_failures, 0, 65535), | |
726 | IDE_PROC_DEVSET(multcount, 0, 16), | |
727 | IDE_PROC_DEVSET(nowerr, 0, 1), | |
728 | IDE_PROC_DEVSET(wcache, 0, 1), | |
729 | { 0 }, | |
8185d5aa | 730 | }; |
7662d046 | 731 | #endif |
1da177e4 | 732 | |
98416549 | 733 | static void idedisk_setup(ide_drive_t *drive) |
1da177e4 | 734 | { |
1e874f44 | 735 | struct ide_disk_obj *idkp = drive->driver_data; |
238e4f14 | 736 | ide_hwif_t *hwif = drive->hwif; |
4dde4492 BZ |
737 | u16 *id = drive->id; |
738 | char *m = (char *)&id[ATA_ID_PROD]; | |
1da177e4 | 739 | unsigned long long capacity; |
1da177e4 | 740 | |
1e874f44 | 741 | ide_proc_register_driver(drive, idkp->driver); |
1da177e4 LT |
742 | |
743 | if (drive->id_read == 0) | |
744 | return; | |
745 | ||
98109337 | 746 | if (drive->removable) { |
1da177e4 | 747 | /* |
98416549 | 748 | * Removable disks (eg. SYQUEST); ignore 'WD' drives |
1da177e4 | 749 | */ |
4dde4492 | 750 | if (m[0] != 'W' || m[1] != 'D') |
1da177e4 | 751 | drive->doorlocking = 1; |
1da177e4 LT |
752 | } |
753 | ||
aa768773 | 754 | (void)set_addressing(drive, 1); |
1da177e4 LT |
755 | |
756 | if (drive->addressing == 1) { | |
1da177e4 LT |
757 | int max_s = 2048; |
758 | ||
759 | if (max_s > hwif->rqsize) | |
760 | max_s = hwif->rqsize; | |
761 | ||
762 | blk_queue_max_sectors(drive->queue, max_s); | |
763 | } | |
764 | ||
98416549 BZ |
765 | printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, |
766 | drive->queue->max_sectors / 2); | |
1da177e4 LT |
767 | |
768 | /* calculate drive capacity, and select LBA if possible */ | |
98416549 | 769 | init_idedisk_capacity(drive); |
1da177e4 LT |
770 | |
771 | /* limit drive capacity to 137GB if LBA48 cannot be used */ | |
772 | if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) { | |
773 | printk(KERN_WARNING "%s: cannot use LBA48 - full capacity " | |
774 | "%llu sectors (%llu MB)\n", | |
775 | drive->name, (unsigned long long)drive->capacity64, | |
776 | sectors_to_MB(drive->capacity64)); | |
777 | drive->capacity64 = 1ULL << 28; | |
778 | } | |
779 | ||
238e4f14 | 780 | if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && drive->addressing) { |
1da177e4 | 781 | if (drive->capacity64 > 1ULL << 28) { |
98416549 BZ |
782 | printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode" |
783 | " will be used for accessing sectors " | |
784 | "> %u\n", drive->name, 1 << 28); | |
1da177e4 LT |
785 | } else |
786 | drive->addressing = 0; | |
787 | } | |
788 | ||
789 | /* | |
790 | * if possible, give fdisk access to more of the drive, | |
791 | * by correcting bios_cyls: | |
792 | */ | |
98416549 BZ |
793 | capacity = idedisk_capacity(drive); |
794 | ||
1da177e4 | 795 | if (!drive->forced_geom) { |
942dcd85 | 796 | if (ata_id_lba48_enabled(drive->id)) { |
1da177e4 LT |
797 | /* compatibility */ |
798 | drive->bios_sect = 63; | |
799 | drive->bios_head = 255; | |
800 | } | |
801 | ||
802 | if (drive->bios_sect && drive->bios_head) { | |
803 | unsigned int cap0 = capacity; /* truncate to 32 bits */ | |
804 | unsigned int cylsz, cyl; | |
805 | ||
806 | if (cap0 != capacity) | |
807 | drive->bios_cyl = 65535; | |
808 | else { | |
809 | cylsz = drive->bios_sect * drive->bios_head; | |
810 | cyl = cap0 / cylsz; | |
811 | if (cyl > 65535) | |
812 | cyl = 65535; | |
813 | if (cyl > drive->bios_cyl) | |
814 | drive->bios_cyl = cyl; | |
815 | } | |
816 | } | |
817 | } | |
818 | printk(KERN_INFO "%s: %llu sectors (%llu MB)", | |
819 | drive->name, capacity, sectors_to_MB(capacity)); | |
820 | ||
821 | /* Only print cache size when it was specified */ | |
4dde4492 BZ |
822 | if (id[ATA_ID_BUF_SIZE]) |
823 | printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2); | |
1da177e4 | 824 | |
3ab7efe8 BZ |
825 | printk(KERN_CONT ", CHS=%d/%d/%d\n", |
826 | drive->bios_cyl, drive->bios_head, drive->bios_sect); | |
1da177e4 | 827 | |
1da177e4 | 828 | /* write cache enabled? */ |
8a089c66 | 829 | if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id)) |
1da177e4 LT |
830 | drive->wcache = 1; |
831 | ||
8185d5aa | 832 | set_wcache(drive, 1); |
1da177e4 LT |
833 | } |
834 | ||
835 | static void ide_cacheflush_p(ide_drive_t *drive) | |
836 | { | |
4b58f17d | 837 | if (!drive->wcache || ata_id_flush_enabled(drive->id) == 0) |
1da177e4 LT |
838 | return; |
839 | ||
840 | if (do_idedisk_flushcache(drive)) | |
841 | printk(KERN_INFO "%s: wcache flush failed!\n", drive->name); | |
842 | } | |
843 | ||
4031bbe4 | 844 | static void ide_disk_remove(ide_drive_t *drive) |
1da177e4 LT |
845 | { |
846 | struct ide_disk_obj *idkp = drive->driver_data; | |
847 | struct gendisk *g = idkp->disk; | |
848 | ||
7662d046 | 849 | ide_proc_unregister_driver(drive, idkp->driver); |
8604affd | 850 | |
1da177e4 LT |
851 | del_gendisk(g); |
852 | ||
d36fef6f BZ |
853 | ide_cacheflush_p(drive); |
854 | ||
1da177e4 | 855 | ide_disk_put(idkp); |
1da177e4 LT |
856 | } |
857 | ||
858 | static void ide_disk_release(struct kref *kref) | |
859 | { | |
860 | struct ide_disk_obj *idkp = to_ide_disk(kref); | |
861 | ide_drive_t *drive = idkp->drive; | |
862 | struct gendisk *g = idkp->disk; | |
863 | ||
864 | drive->driver_data = NULL; | |
1da177e4 LT |
865 | g->private_data = NULL; |
866 | put_disk(g); | |
867 | kfree(idkp); | |
868 | } | |
869 | ||
4031bbe4 | 870 | static int ide_disk_probe(ide_drive_t *drive); |
1da177e4 | 871 | |
0d2157f7 LT |
872 | /* |
873 | * On HPA drives the capacity needs to be | |
874 | * reinitilized on resume otherwise the disk | |
875 | * can not be used and a hard reset is required | |
876 | */ | |
877 | static void ide_disk_resume(ide_drive_t *drive) | |
878 | { | |
f41891c1 | 879 | if (ata_id_hpa_enabled(drive->id)) |
0d2157f7 LT |
880 | init_idedisk_capacity(drive); |
881 | } | |
882 | ||
4031bbe4 | 883 | static void ide_device_shutdown(ide_drive_t *drive) |
1da177e4 | 884 | { |
1da177e4 LT |
885 | #ifdef CONFIG_ALPHA |
886 | /* On Alpha, halt(8) doesn't actually turn the machine off, | |
887 | it puts you into the sort of firmware monitor. Typically, | |
888 | it's used to boot another kernel image, so it's not much | |
889 | different from reboot(8). Therefore, we don't need to | |
890 | spin down the disk in this case, especially since Alpha | |
891 | firmware doesn't handle disks in standby mode properly. | |
892 | On the other hand, it's reasonably safe to turn the power | |
893 | off when the shutdown process reaches the firmware prompt, | |
894 | as the firmware initialization takes rather long time - | |
895 | at least 10 seconds, which should be sufficient for | |
896 | the disk to expire its write cache. */ | |
897 | if (system_state != SYSTEM_POWER_OFF) { | |
898 | #else | |
899 | if (system_state == SYSTEM_RESTART) { | |
900 | #endif | |
901 | ide_cacheflush_p(drive); | |
902 | return; | |
903 | } | |
904 | ||
d12faa27 BZ |
905 | printk(KERN_INFO "Shutdown: %s\n", drive->name); |
906 | ||
4031bbe4 | 907 | drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND); |
1da177e4 LT |
908 | } |
909 | ||
1da177e4 | 910 | static ide_driver_t idedisk_driver = { |
1da177e4 | 911 | .gen_driver = { |
4ef3b8f4 | 912 | .owner = THIS_MODULE, |
8604affd BZ |
913 | .name = "ide-disk", |
914 | .bus = &ide_bus_type, | |
1da177e4 | 915 | }, |
4031bbe4 RK |
916 | .probe = ide_disk_probe, |
917 | .remove = ide_disk_remove, | |
0d2157f7 | 918 | .resume = ide_disk_resume, |
4031bbe4 | 919 | .shutdown = ide_device_shutdown, |
1da177e4 LT |
920 | .version = IDEDISK_VERSION, |
921 | .media = ide_disk, | |
1da177e4 LT |
922 | .do_request = ide_do_rw_disk, |
923 | .end_request = ide_end_request, | |
924 | .error = __ide_error, | |
7662d046 | 925 | #ifdef CONFIG_IDE_PROC_FS |
1da177e4 | 926 | .proc = idedisk_proc, |
8185d5aa | 927 | .settings = idedisk_settings, |
7662d046 | 928 | #endif |
1da177e4 LT |
929 | }; |
930 | ||
29ec683f BZ |
931 | static int idedisk_set_doorlock(ide_drive_t *drive, int on) |
932 | { | |
933 | ide_task_t task; | |
934 | ||
935 | memset(&task, 0, sizeof(task)); | |
aaaade3f | 936 | task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK; |
657cc1a8 | 937 | task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
29ec683f BZ |
938 | |
939 | return ide_no_data_taskfile(drive, &task); | |
940 | } | |
941 | ||
1da177e4 LT |
942 | static int idedisk_open(struct inode *inode, struct file *filp) |
943 | { | |
944 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
945 | struct ide_disk_obj *idkp; | |
946 | ide_drive_t *drive; | |
947 | ||
98416549 BZ |
948 | idkp = ide_disk_get(disk); |
949 | if (idkp == NULL) | |
1da177e4 LT |
950 | return -ENXIO; |
951 | ||
952 | drive = idkp->drive; | |
953 | ||
c94964a4 BZ |
954 | idkp->openers++; |
955 | ||
956 | if (drive->removable && idkp->openers == 1) { | |
1da177e4 LT |
957 | check_disk_change(inode->i_bdev); |
958 | /* | |
959 | * Ignore the return code from door_lock, | |
960 | * since the open() has already succeeded, | |
961 | * and the door_lock is irrelevant at this point. | |
962 | */ | |
29ec683f | 963 | if (drive->doorlocking && idedisk_set_doorlock(drive, 1)) |
1da177e4 LT |
964 | drive->doorlocking = 0; |
965 | } | |
966 | return 0; | |
967 | } | |
968 | ||
969 | static int idedisk_release(struct inode *inode, struct file *filp) | |
970 | { | |
971 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
972 | struct ide_disk_obj *idkp = ide_disk_g(disk); | |
973 | ide_drive_t *drive = idkp->drive; | |
974 | ||
c94964a4 | 975 | if (idkp->openers == 1) |
1da177e4 | 976 | ide_cacheflush_p(drive); |
c94964a4 BZ |
977 | |
978 | if (drive->removable && idkp->openers == 1) { | |
29ec683f | 979 | if (drive->doorlocking && idedisk_set_doorlock(drive, 0)) |
1da177e4 LT |
980 | drive->doorlocking = 0; |
981 | } | |
c94964a4 BZ |
982 | |
983 | idkp->openers--; | |
1da177e4 LT |
984 | |
985 | ide_disk_put(idkp); | |
986 | ||
987 | return 0; | |
988 | } | |
989 | ||
a885c8c4 CH |
990 | static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
991 | { | |
992 | struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk); | |
993 | ide_drive_t *drive = idkp->drive; | |
994 | ||
995 | geo->heads = drive->bios_head; | |
996 | geo->sectors = drive->bios_sect; | |
997 | geo->cylinders = (u16)drive->bios_cyl; /* truncate */ | |
998 | return 0; | |
999 | } | |
1000 | ||
aa768773 | 1001 | static const struct ide_ioctl_devset ide_disk_ioctl_settings[] = { |
92f1f8fd EO |
1002 | { HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, &ide_devset_address }, |
1003 | { HDIO_GET_MULTCOUNT, HDIO_SET_MULTCOUNT, &ide_devset_multcount }, | |
1004 | { HDIO_GET_NOWERR, HDIO_SET_NOWERR, &ide_devset_nowerr }, | |
1005 | { HDIO_GET_WCACHE, HDIO_SET_WCACHE, &ide_devset_wcache }, | |
1006 | { HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, &ide_devset_acoustic }, | |
aa768773 BZ |
1007 | { 0 } |
1008 | }; | |
1009 | ||
1da177e4 LT |
1010 | static int idedisk_ioctl(struct inode *inode, struct file *file, |
1011 | unsigned int cmd, unsigned long arg) | |
1012 | { | |
1013 | struct block_device *bdev = inode->i_bdev; | |
1014 | struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk); | |
1497943e | 1015 | ide_drive_t *drive = idkp->drive; |
aa768773 | 1016 | int err; |
1497943e | 1017 | |
aa768773 BZ |
1018 | err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_disk_ioctl_settings); |
1019 | if (err != -EOPNOTSUPP) | |
1020 | return err; | |
1497943e | 1021 | |
aa768773 | 1022 | return generic_ide_ioctl(drive, file, bdev, cmd, arg); |
1da177e4 LT |
1023 | } |
1024 | ||
1025 | static int idedisk_media_changed(struct gendisk *disk) | |
1026 | { | |
1027 | struct ide_disk_obj *idkp = ide_disk_g(disk); | |
1028 | ide_drive_t *drive = idkp->drive; | |
1029 | ||
1030 | /* do not scan partitions twice if this is a removable device */ | |
1031 | if (drive->attach) { | |
1032 | drive->attach = 0; | |
1033 | return 0; | |
1034 | } | |
1035 | /* if removable, always assume it was changed */ | |
1036 | return drive->removable; | |
1037 | } | |
1038 | ||
1039 | static int idedisk_revalidate_disk(struct gendisk *disk) | |
1040 | { | |
1041 | struct ide_disk_obj *idkp = ide_disk_g(disk); | |
1042 | set_capacity(disk, idedisk_capacity(idkp->drive)); | |
1043 | return 0; | |
1044 | } | |
1045 | ||
1046 | static struct block_device_operations idedisk_ops = { | |
98416549 BZ |
1047 | .owner = THIS_MODULE, |
1048 | .open = idedisk_open, | |
1049 | .release = idedisk_release, | |
1050 | .ioctl = idedisk_ioctl, | |
1051 | .getgeo = idedisk_getgeo, | |
1052 | .media_changed = idedisk_media_changed, | |
1053 | .revalidate_disk = idedisk_revalidate_disk | |
1da177e4 LT |
1054 | }; |
1055 | ||
1056 | MODULE_DESCRIPTION("ATA DISK Driver"); | |
1057 | ||
4031bbe4 | 1058 | static int ide_disk_probe(ide_drive_t *drive) |
1da177e4 LT |
1059 | { |
1060 | struct ide_disk_obj *idkp; | |
1061 | struct gendisk *g; | |
1062 | ||
1063 | /* strstr("foo", "") is non-NULL */ | |
1064 | if (!strstr("ide-disk", drive->driver_req)) | |
1065 | goto failed; | |
2a924662 | 1066 | |
1da177e4 LT |
1067 | if (drive->media != ide_disk) |
1068 | goto failed; | |
1069 | ||
f5e3c2fa | 1070 | idkp = kzalloc(sizeof(*idkp), GFP_KERNEL); |
1da177e4 LT |
1071 | if (!idkp) |
1072 | goto failed; | |
1073 | ||
689d6fac | 1074 | g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif)); |
1da177e4 LT |
1075 | if (!g) |
1076 | goto out_free_idkp; | |
1077 | ||
1078 | ide_init_disk(g, drive); | |
1079 | ||
1da177e4 LT |
1080 | kref_init(&idkp->kref); |
1081 | ||
1082 | idkp->drive = drive; | |
1083 | idkp->driver = &idedisk_driver; | |
1084 | idkp->disk = g; | |
1085 | ||
1086 | g->private_data = &idkp->driver; | |
1087 | ||
1088 | drive->driver_data = idkp; | |
1089 | ||
1da177e4 LT |
1090 | idedisk_setup(drive); |
1091 | if ((!drive->head || drive->head > 16) && !drive->select.b.lba) { | |
1092 | printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n", | |
1093 | drive->name, drive->head); | |
1094 | drive->attach = 0; | |
1095 | } else | |
1096 | drive->attach = 1; | |
8604affd | 1097 | |
f615b48c | 1098 | g->minors = IDE_DISK_MINORS; |
1da177e4 | 1099 | g->driverfs_dev = &drive->gendev; |
689d6fac TH |
1100 | g->flags |= GENHD_FL_EXT_DEVT; |
1101 | if (drive->removable) | |
1102 | g->flags |= GENHD_FL_REMOVABLE; | |
1da177e4 LT |
1103 | set_capacity(g, idedisk_capacity(drive)); |
1104 | g->fops = &idedisk_ops; | |
1105 | add_disk(g); | |
1106 | return 0; | |
1107 | ||
1da177e4 LT |
1108 | out_free_idkp: |
1109 | kfree(idkp); | |
1110 | failed: | |
8604affd | 1111 | return -ENODEV; |
1da177e4 LT |
1112 | } |
1113 | ||
98416549 | 1114 | static void __exit idedisk_exit(void) |
1da177e4 | 1115 | { |
8604affd | 1116 | driver_unregister(&idedisk_driver.gen_driver); |
1da177e4 LT |
1117 | } |
1118 | ||
17514e8a | 1119 | static int __init idedisk_init(void) |
1da177e4 | 1120 | { |
8604affd | 1121 | return driver_register(&idedisk_driver.gen_driver); |
1da177e4 LT |
1122 | } |
1123 | ||
263756ec | 1124 | MODULE_ALIAS("ide:*m-disk*"); |
1da177e4 LT |
1125 | module_init(idedisk_init); |
1126 | module_exit(idedisk_exit); | |
1127 | MODULE_LICENSE("GPL"); |