2 * linux/drivers/ide/ide-proc.c Version 1.05 Mar 05, 2003
4 * Copyright (C) 1997-1998 Mark Lord
5 * Copyright (C) 2003 Red Hat <alan@redhat.com>
9 * This is the /proc/ide/ filesystem implementation.
11 * The major reason this exists is to provide sufficient access
12 * to driver and config data, such that user-mode programs can
13 * be developed to handle chipset tuning for most PCI interfaces.
14 * This should provide better utilities, and less kernel bloat.
16 * The entire pci config space for a PCI interface chipset can be
17 * retrieved by just reading it. e.g. "cat /proc/ide3/config"
19 * To modify registers *safely*, do something like:
20 * echo "P40:88" >/proc/ide/ide3/config
21 * That expression writes 0x88 to pci config register 0x40
22 * on the chip which controls ide3. Multiple tuples can be issued,
23 * and the writes will be completed as an atomic set:
24 * echo "P40:88 P41:35 P42:00 P43:00" >/proc/ide/ide3/config
26 * All numbers must be specified using pairs of ascii hex digits.
27 * It is important to note that these writes will be performed
28 * after waiting for the IDE controller (both interfaces)
29 * to be completely idle, to ensure no corruption of I/O in progress.
31 * Non-PCI registers can also be written, using "R" in place of "P"
32 * in the above examples. The size of the port transfer is determined
33 * by the number of pairs of hex digits given for the data. If a two
34 * digit value is given, the write will be a byte operation; if four
35 * digits are used, the write will be performed as a 16-bit operation;
36 * and if eight digits are specified, a 32-bit "dword" write will be
37 * performed. Odd numbers of digits are not permitted.
39 * If there is an error *anywhere* in the string of registers/data
40 * then *none* of the writes will be performed.
42 * Drive/Driver settings can be retrieved by reading the drive's
43 * "settings" files. e.g. "cat /proc/ide0/hda/settings"
44 * To write a new value "val" into a specific setting "name", use:
45 * echo "name:val" >/proc/ide/ide0/hda/settings
47 * Also useful, "cat /proc/ide0/hda/[identify, smart_values,
48 * smart_thresholds, capabilities]" will issue an IDENTIFY /
49 * PACKET_IDENTIFY / SMART_READ_VALUES / SMART_READ_THRESHOLDS /
50 * SENSE CAPABILITIES command to /dev/hda, and then dump out the
51 * returned data as 256 16-bit words. The "hdparm" utility will
52 * be updated someday soon to use this mechanism.
54 * Feel free to develop and distribute fancy GUI configuration
55 * utilities for your favorite PCI chipsets. I'll be working on
56 * one for the Promise 20246 someday soon. -ml
60 #include <linux/config.h>
61 #include <linux/module.h>
63 #include <asm/uaccess.h>
64 #include <linux/errno.h>
65 #include <linux/sched.h>
66 #include <linux/proc_fs.h>
67 #include <linux/stat.h>
69 #include <linux/pci.h>
70 #include <linux/ctype.h>
71 #include <linux/hdreg.h>
72 #include <linux/ide.h>
73 #include <linux/seq_file.h>
77 static int proc_ide_write_config(struct file *file, const char __user *buffer,
78 unsigned long count, void *data)
80 ide_hwif_t *hwif = (ide_hwif_t *)data;
81 ide_hwgroup_t *mygroup = (ide_hwgroup_t *)(hwif->hwgroup);
82 ide_hwgroup_t *mategroup = NULL;
83 unsigned long timeout;
85 const char *start = NULL, *msg = NULL;
86 struct entry { u32 val; u16 reg; u8 size; u8 pci; } *prog, *q, *r;
91 if (hwif->mate && hwif->mate->hwgroup)
92 mategroup = (ide_hwgroup_t *)(hwif->mate->hwgroup);
94 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
97 if (count >= PAGE_SIZE)
100 s = buf = (char *)__get_free_page(GFP_USER);
105 q = prog = (struct entry *)__get_free_page(GFP_USER);
110 if (copy_from_user(buf, buffer, count))
124 if ((char *)(q + 1) > (char *)prog + PAGE_SIZE) {
125 msg = "too many entries";
130 case 'R': q->pci = 0;
132 case 'P': q->pci = 1;
135 default: msg = "expected 'R' or 'P'";
139 q->reg = simple_strtoul(s, &p, 16);
141 if (!digits || digits > 4 || (q->pci && q->reg > 0xff)) {
142 msg = "bad/missing register number";
149 q->val = simple_strtoul(p, &s, 16);
151 if (digits != 2 && digits != 4 && digits != 8) {
152 msg = "bad data, 2/4/8 digits required";
155 q->size = digits / 2;
158 #ifdef CONFIG_BLK_DEV_IDEPCI
159 if (q->reg & (q->size - 1)) {
160 msg = "misaligned access";
164 msg = "not a PCI device";
166 #endif /* CONFIG_BLK_DEV_IDEPCI */
171 if (*s && !isspace(*s++)) {
172 msg = "expected whitespace after data";
180 * What follows below is fucking insane, even for IDE people.
181 * For now I've dealt with the obvious problems on the parsing
182 * side, but IMNSHO we should simply remove the write access
183 * to /proc/ide/.../config, killing that FPOS completely.
187 timeout = jiffies + (3 * HZ);
188 spin_lock_irqsave(&ide_lock, flags);
189 while (mygroup->busy ||
190 (mategroup && mategroup->busy)) {
191 spin_unlock_irqrestore(&ide_lock, flags);
192 if (time_after(jiffies, timeout)) {
193 printk("/proc/ide/%s/config: channel(s) busy, cannot write\n", hwif->name);
196 spin_lock_irqsave(&ide_lock, flags);
199 #ifdef CONFIG_BLK_DEV_IDEPCI
200 if (want_pci && (!hwif->pci_dev || hwif->pci_dev->vendor)) {
201 spin_unlock_irqrestore(&ide_lock, flags);
202 printk("proc_ide: PCI registers not accessible for %s\n",
207 #endif /* CONFIG_BLK_DEV_IDEPCI */
209 for (r = prog; r < q; r++) {
210 unsigned int reg = r->reg, val = r->val;
212 #ifdef CONFIG_BLK_DEV_IDEPCI
214 struct pci_dev *dev = hwif->pci_dev;
216 case 1: msg = "byte";
217 rc = pci_write_config_byte(dev, reg, val);
219 case 2: msg = "word";
220 rc = pci_write_config_word(dev, reg, val);
222 case 4: msg = "dword";
223 rc = pci_write_config_dword(dev, reg, val);
227 spin_unlock_irqrestore(&ide_lock, flags);
228 printk("proc_ide_write_config: error writing %s at bus %02x dev %02x reg 0x%x value 0x%x\n",
229 msg, dev->bus->number, dev->devfn, reg, val);
230 printk("proc_ide_write_config: error %d\n", rc);
234 #endif /* CONFIG_BLK_DEV_IDEPCI */
235 } else { /* not pci */
237 case 1: hwif->OUTB(val, reg);
239 case 2: hwif->OUTW(val, reg);
241 case 4: hwif->OUTL(val, reg);
246 spin_unlock_irqrestore(&ide_lock, flags);
249 free_page((unsigned long)prog);
251 free_page((unsigned long)buf);
255 printk("parse error\n");
256 printk("proc_ide: error: %s: '%s'\n", msg, start);
261 static int proc_ide_read_config
262 (char *page, char **start, off_t off, int count, int *eof, void *data)
267 #ifdef CONFIG_BLK_DEV_IDEPCI
268 ide_hwif_t *hwif = (ide_hwif_t *)data;
269 struct pci_dev *dev = hwif->pci_dev;
270 if ((hwif->pci_dev && hwif->pci_dev->vendor) && dev && dev->bus) {
273 out += sprintf(out, "pci bus %02x device %02x vendor %04x "
274 "device %04x channel %d\n",
275 dev->bus->number, dev->devfn,
276 hwif->pci_dev->vendor, hwif->pci_dev->device,
280 int rc = pci_read_config_byte(dev, reg, &val);
282 printk("proc_ide_read_config: error %d reading"
283 " bus %02x dev %02x reg 0x%02x\n",
284 rc, dev->bus->number, dev->devfn, reg);
285 out += sprintf(out, "??%c",
286 (++reg & 0xf) ? ' ' : '\n');
288 out += sprintf(out, "%02x%c",
289 val, (++reg & 0xf) ? ' ' : '\n');
290 } while (reg < 0x100);
292 #endif /* CONFIG_BLK_DEV_IDEPCI */
293 out += sprintf(out, "(none)\n");
295 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
298 static int proc_ide_read_imodel
299 (char *page, char **start, off_t off, int count, int *eof, void *data)
301 ide_hwif_t *hwif = (ide_hwif_t *) data;
306 * Neither ide_unknown nor ide_forced should be set at this point.
308 switch (hwif->chipset) {
309 case ide_generic: name = "generic"; break;
310 case ide_pci: name = "pci"; break;
311 case ide_cmd640: name = "cmd640"; break;
312 case ide_dtc2278: name = "dtc2278"; break;
313 case ide_ali14xx: name = "ali14xx"; break;
314 case ide_qd65xx: name = "qd65xx"; break;
315 case ide_umc8672: name = "umc8672"; break;
316 case ide_ht6560b: name = "ht6560b"; break;
317 case ide_pdc4030: name = "pdc4030"; break;
318 case ide_rz1000: name = "rz1000"; break;
319 case ide_trm290: name = "trm290"; break;
320 case ide_cmd646: name = "cmd646"; break;
321 case ide_cy82c693: name = "cy82c693"; break;
322 case ide_4drives: name = "4drives"; break;
323 case ide_pmac: name = "mac-io"; break;
324 case ide_pc9800: name = "pc9800"; break;
325 default: name = "(unknown)"; break;
327 len = sprintf(page, "%s\n", name);
328 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
331 static int proc_ide_read_mate
332 (char *page, char **start, off_t off, int count, int *eof, void *data)
334 ide_hwif_t *hwif = (ide_hwif_t *) data;
337 if (hwif && hwif->mate && hwif->mate->present)
338 len = sprintf(page, "%s\n", hwif->mate->name);
340 len = sprintf(page, "(none)\n");
341 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
344 static int proc_ide_read_channel
345 (char *page, char **start, off_t off, int count, int *eof, void *data)
347 ide_hwif_t *hwif = (ide_hwif_t *) data;
350 page[0] = hwif->channel ? '1' : '0';
353 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
356 static int proc_ide_read_identify
357 (char *page, char **start, off_t off, int count, int *eof, void *data)
359 ide_drive_t *drive = (ide_drive_t *)data;
363 len = sprintf(page, "\n");
367 unsigned short *val = (unsigned short *) page;
370 * The current code can't handle a driverless
371 * identify query taskfile. Now the right fix is
372 * to add a 'default' driver but that is a bit
375 * FIXME: this has to be fixed for hotswap devices
379 err = taskfile_lib_get_identify(drive, page);
380 else /* This relies on the ID changes */
381 val = (unsigned short *)drive->id;
385 char *out = ((char *)page) + (SECTOR_WORDS * 4);
388 out += sprintf(out, "%04x%c",
389 le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
391 } while (i < (SECTOR_WORDS * 2));
395 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
398 static int proc_ide_read_settings
399 (char *page, char **start, off_t off, int count, int *eof, void *data)
401 ide_drive_t *drive = (ide_drive_t *) data;
402 ide_settings_t *setting = (ide_settings_t *) drive->settings;
404 int len, rc, mul_factor, div_factor;
406 down(&ide_setting_sem);
407 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
408 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
410 mul_factor = setting->mul_factor;
411 div_factor = setting->div_factor;
412 out += sprintf(out, "%-24s", setting->name);
413 if ((rc = ide_read_setting(drive, setting)) >= 0)
414 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
416 out += sprintf(out, "%-16s", "write-only");
417 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
418 if (setting->rw & SETTING_READ)
419 out += sprintf(out, "r");
420 if (setting->rw & SETTING_WRITE)
421 out += sprintf(out, "w");
422 out += sprintf(out, "\n");
423 setting = setting->next;
426 up(&ide_setting_sem);
427 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
432 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
433 unsigned long count, void *data)
435 ide_drive_t *drive = (ide_drive_t *) data;
436 char name[MAX_LEN + 1];
439 ide_settings_t *setting;
442 if (!capable(CAP_SYS_ADMIN))
445 if (count >= PAGE_SIZE)
448 s = buf = (char *)__get_free_page(GFP_USER);
452 if (copy_from_user(buf, buffer, count)) {
453 free_page((unsigned long)buf);
460 * Skip over leading whitespace
462 while (count && isspace(*s)) {
467 * Do one full pass to verify all parameters,
468 * then do another to actually write the new settings.
477 while (n > 0 && *p != ':') {
485 memcpy(name, q, p - q);
494 val = simple_strtoul(p, &q, 10);
497 if (n > 0 && !isspace(*p))
499 while (n > 0 && isspace(*p)) {
504 down(&ide_setting_sem);
505 setting = ide_find_setting_by_name(drive, name);
508 up(&ide_setting_sem);
512 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
513 up(&ide_setting_sem);
515 } while (!for_real++);
516 free_page((unsigned long)buf);
519 free_page((unsigned long)buf);
520 printk("proc_ide_write_settings(): parse error\n");
524 int proc_ide_read_capacity
525 (char *page, char **start, off_t off, int count, int *eof, void *data)
527 ide_drive_t *drive = (ide_drive_t *) data;
530 len = sprintf(page,"%llu\n",
531 (long long) (DRIVER(drive)->capacity(drive)));
532 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
535 int proc_ide_read_geometry
536 (char *page, char **start, off_t off, int count, int *eof, void *data)
538 ide_drive_t *drive = (ide_drive_t *) data;
542 out += sprintf(out,"physical %d/%d/%d\n",
543 drive->cyl, drive->head, drive->sect);
544 out += sprintf(out,"logical %d/%d/%d\n",
545 drive->bios_cyl, drive->bios_head, drive->bios_sect);
548 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
551 EXPORT_SYMBOL(proc_ide_read_geometry);
553 static int proc_ide_read_dmodel
554 (char *page, char **start, off_t off, int count, int *eof, void *data)
556 ide_drive_t *drive = (ide_drive_t *) data;
557 struct hd_driveid *id = drive->id;
560 len = sprintf(page, "%.40s\n",
561 (id && id->model[0]) ? (char *)id->model : "(none)");
562 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
565 static int proc_ide_read_driver
566 (char *page, char **start, off_t off, int count, int *eof, void *data)
568 ide_drive_t *drive = (ide_drive_t *) data;
569 ide_driver_t *driver = drive->driver;
572 len = sprintf(page, "%s version %s\n",
573 driver->name, driver->version);
574 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
577 static int proc_ide_write_driver
578 (struct file *file, const char __user *buffer, unsigned long count, void *data)
580 ide_drive_t *drive = (ide_drive_t *) data;
583 if (!capable(CAP_SYS_ADMIN))
587 if (copy_from_user(name, buffer, count))
590 if (ide_replace_subdriver(drive, name))
595 static int proc_ide_read_media
596 (char *page, char **start, off_t off, int count, int *eof, void *data)
598 ide_drive_t *drive = (ide_drive_t *) data;
602 switch (drive->media) {
603 case ide_disk: media = "disk\n";
605 case ide_cdrom: media = "cdrom\n";
607 case ide_tape: media = "tape\n";
609 case ide_floppy:media = "floppy\n";
611 default: media = "UNKNOWN\n";
616 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
619 static ide_proc_entry_t generic_drive_entries[] = {
620 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, proc_ide_write_driver },
621 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
622 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
623 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
624 { "settings", S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
625 { NULL, 0, NULL, NULL }
628 void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
630 struct proc_dir_entry *ent;
634 while (p->name != NULL) {
635 ent = create_proc_entry(p->name, p->mode, dir);
639 ent->read_proc = p->read_proc;
640 ent->write_proc = p->write_proc;
645 void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
649 while (p->name != NULL) {
650 remove_proc_entry(p->name, dir);
655 static void create_proc_ide_drives(ide_hwif_t *hwif)
658 struct proc_dir_entry *ent;
659 struct proc_dir_entry *parent = hwif->proc;
662 for (d = 0; d < MAX_DRIVES; d++) {
663 ide_drive_t *drive = &hwif->drives[d];
670 drive->proc = proc_mkdir(drive->name, parent);
672 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
673 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
674 ent = proc_symlink(drive->name, proc_ide_root, name);
679 static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
681 ide_driver_t *driver = drive->driver;
684 ide_remove_proc_entries(drive->proc, driver->proc);
685 ide_remove_proc_entries(drive->proc, generic_drive_entries);
686 remove_proc_entry(drive->name, proc_ide_root);
687 remove_proc_entry(drive->name, hwif->proc);
692 void destroy_proc_ide_drives(ide_hwif_t *hwif)
696 for (d = 0; d < MAX_DRIVES; d++) {
697 ide_drive_t *drive = &hwif->drives[d];
699 destroy_proc_ide_device(hwif, drive);
703 static ide_proc_entry_t hwif_entries[] = {
704 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
705 { "config", S_IFREG|S_IRUGO|S_IWUSR,proc_ide_read_config, proc_ide_write_config },
706 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
707 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
708 { NULL, 0, NULL, NULL }
711 void create_proc_ide_interfaces(void)
715 for (h = 0; h < MAX_HWIFS; h++) {
716 ide_hwif_t *hwif = &ide_hwifs[h];
721 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
724 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
726 create_proc_ide_drives(hwif);
730 EXPORT_SYMBOL(create_proc_ide_interfaces);
732 #ifdef CONFIG_BLK_DEV_IDEPCI
733 void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
735 create_proc_info_entry(name, 0, proc_ide_root, get_info);
738 EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
741 void destroy_proc_ide_interfaces(void)
745 for (h = 0; h < MAX_HWIFS; h++) {
746 ide_hwif_t *hwif = &ide_hwifs[h];
747 int exist = (hwif->proc != NULL);
753 destroy_proc_ide_drives(hwif);
754 ide_remove_proc_entries(hwif->proc, hwif_entries);
755 remove_proc_entry(hwif->name, proc_ide_root);
762 EXPORT_SYMBOL(destroy_proc_ide_interfaces);
764 extern struct seq_operations ide_drivers_op;
765 static int ide_drivers_open(struct inode *inode, struct file *file)
767 return seq_open(file, &ide_drivers_op);
769 static struct file_operations ide_drivers_operations = {
770 .open = ide_drivers_open,
773 .release = seq_release,
776 void proc_ide_create(void)
778 struct proc_dir_entry *entry;
783 create_proc_ide_interfaces();
785 entry = create_proc_entry("drivers", 0, proc_ide_root);
787 entry->proc_fops = &ide_drivers_operations;
790 void proc_ide_destroy(void)
792 remove_proc_entry("ide/drivers", proc_ide_root);
793 destroy_proc_ide_interfaces();
794 remove_proc_entry("ide", 0);