more changes on original files
[linux-2.4.git] / arch / i386 / kernel / edd.c
1 /*
2  * linux/arch/i386/kernel/edd.c
3  *  Copyright (C) 2002, 2003 Dell, Inc.
4  *  by Matt Domsch <Matt_Domsch@dell.com>
5  *  disk80 signature by Matt Domsch, Andrew Wilks, and Sandeep K. Shandilya
6  *
7  * BIOS Enhanced Disk Drive Services (EDD)
8  * conformant to T13 Committee www.t13.org
9  *   projects 1572D, 1484D, 1386D, 1226DT
10  *
11  * This code takes information provided by BIOS EDD calls
12  * fn41 - Check Extensions Present and
13  * fn48 - Get Device Parametes with EDD extensions
14  * made in setup.S, copied to safe structures in setup.c,
15  * and presents it in /proc.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License v2.0 as published by
19  * the Free Software Foundation
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  */
27
28 /*
29  * TODO:
30  * - move edd.[ch] to better locations if/when one is decided
31  */
32
33 #include <linux/module.h>
34 #include <linux/string.h>
35 #include <linux/types.h>
36 #include <linux/init.h>
37 #include <linux/stat.h>
38 #include <linux/ctype.h>
39 #include <linux/slab.h>
40 #include <linux/limits.h>
41 #include <linux/pci.h>
42 #include <linux/proc_fs.h>
43 #include <asm/edd.h>
44
45 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
46 MODULE_DESCRIPTION("proc interface to BIOS EDD information");
47 MODULE_LICENSE("GPL");
48
49 #define EDD_VERSION "0.11 2004-Jun-21"
50 #define EDD_DEVICE_NAME_SIZE 16
51
52 #define left (count - (p - page) - 1)
53
54 static struct proc_dir_entry *bios_dir;
55
56 struct attr_entry {
57         struct proc_dir_entry *entry;
58         struct list_head node;
59 };
60
61 struct edd_device {
62         char name[EDD_DEVICE_NAME_SIZE];
63         struct edd_info *info;
64         struct proc_dir_entry *dir;
65         struct list_head attr_list;
66 };
67
68 static struct edd_device *edd_devices[EDDMAXNR];
69
70 struct edd_attribute {
71         char *name;
72         int (*show)(char *page, char **start, off_t off,
73                     int count, int *eof, void *data);
74         int (*test) (struct edd_device * edev);
75 };
76
77 #define EDD_DEVICE_ATTR(_name,_show,_test) \
78 struct edd_attribute edd_attr_##_name = {       \
79         .name = __stringify(_name), \
80         .show   = _show,            \
81         .test   = _test,            \
82 };
83
84 static inline struct edd_info *
85 edd_dev_get_info(struct edd_device *edev)
86 {
87         return edev->info;
88 }
89
90 static inline void
91 edd_dev_set_info(struct edd_device *edev, struct edd_info *info)
92 {
93         edev->info = info;
94 }
95
96 static int
97 proc_calc_metrics(char *page, char **start, off_t off,
98                   int count, int *eof, int len)
99 {
100         if (len <= off+count) *eof = 1;
101         *start = page + off;
102         len -= off;
103         if (len>count) len = count;
104         if (len<0) len = 0;
105         return len;
106 }
107
108 static int
109 edd_dump_raw_data(char *b, int count, void *data, int length)
110 {
111         char *orig_b = b;
112         char hexbuf[80], ascbuf[20], *h, *a, c;
113         unsigned char *p = data;
114         unsigned long column = 0;
115         int length_printed = 0, d;
116         const char maxcolumn = 16;
117         while (length_printed < length && count > 0) {
118                 h = hexbuf;
119                 a = ascbuf;
120                 for (column = 0;
121                      column < maxcolumn && length_printed < length; column++) {
122                         h += sprintf(h, "%02x ", (unsigned char) *p);
123                         if (!isprint(*p))
124                                 c = '.';
125                         else
126                                 c = *p;
127                         a += sprintf(a, "%c", c);
128                         p++;
129                         length_printed++;
130                 }
131                 /* pad out the line */
132                 for (; column < maxcolumn; column++) {
133                         h += sprintf(h, "   ");
134                         a += sprintf(a, " ");
135                 }
136                 d = snprintf(b, count, "%s\t%s\n", hexbuf, ascbuf);
137                 b += d;
138                 count -= d;
139         }
140         return (b - orig_b);
141 }
142
143 static int
144 edd_show_host_bus(char *page, char **start, off_t off, int count, int *eof, void *data)
145 {
146         struct edd_info *info = data;
147         char *p = page;
148         int i;
149
150         if (!info || !page || off) {
151                 return proc_calc_metrics(page, start, off, count, eof, 0);
152         }
153
154         for (i = 0; i < 4; i++) {
155                 if (isprint(info->params.host_bus_type[i])) {
156                         p += snprintf(p, left, "%c", info->params.host_bus_type[i]);
157                 } else {
158                         p += snprintf(p, left, " ");
159                 }
160         }
161
162         if (!strncmp(info->params.host_bus_type, "ISA", 3)) {
163                 p += snprintf(p, left, "\tbase_address: %x\n",
164                              info->params.interface_path.isa.base_address);
165         } else if (!strncmp(info->params.host_bus_type, "PCIX", 4) ||
166                    !strncmp(info->params.host_bus_type, "PCI", 3)) {
167                 p += snprintf(p, left,
168                              "\t%02x:%02x.%d  channel: %u\n",
169                              info->params.interface_path.pci.bus,
170                              info->params.interface_path.pci.slot,
171                              info->params.interface_path.pci.function,
172                              info->params.interface_path.pci.channel);
173         } else if (!strncmp(info->params.host_bus_type, "IBND", 4) ||
174                    !strncmp(info->params.host_bus_type, "XPRS", 4) ||
175                    !strncmp(info->params.host_bus_type, "HTPT", 4)) {
176                 p += snprintf(p, left,
177                              "\tTBD: %llx\n",
178                              info->params.interface_path.ibnd.reserved);
179
180         } else {
181                 p += snprintf(p, left, "\tunknown: %llx\n",
182                              info->params.interface_path.unknown.reserved);
183         }
184         return proc_calc_metrics(page, start, off, count, eof, (p - page));
185 }
186
187 static int
188 edd_show_interface(char *page, char **start, off_t off, int count, int *eof, void *data)
189 {
190         struct edd_info *info = data;
191         char *p = page;
192         int i;
193
194         if (!info || !page || off) {
195                 return proc_calc_metrics(page, start, off, count, eof, 0);
196         }
197
198         for (i = 0; i < 8; i++) {
199                 if (isprint(info->params.interface_type[i])) {
200                         p += snprintf(p, left, "%c", info->params.interface_type[i]);
201                 } else {
202                         p += snprintf(p, left, " ");
203                 }
204         }
205         if (!strncmp(info->params.interface_type, "ATAPI", 5)) {
206                 p += snprintf(p, left, "\tdevice: %u  lun: %u\n",
207                              info->params.device_path.atapi.device,
208                              info->params.device_path.atapi.lun);
209         } else if (!strncmp(info->params.interface_type, "ATA", 3)) {
210                 p += snprintf(p, left, "\tdevice: %u\n",
211                              info->params.device_path.ata.device);
212         } else if (!strncmp(info->params.interface_type, "SCSI", 4)) {
213                 p += snprintf(p, left, "\tid: %u  lun: %llu\n",
214                              info->params.device_path.scsi.id,
215                              info->params.device_path.scsi.lun);
216         } else if (!strncmp(info->params.interface_type, "USB", 3)) {
217                 p += snprintf(p, left, "\tserial_number: %llx\n",
218                              info->params.device_path.usb.serial_number);
219         } else if (!strncmp(info->params.interface_type, "1394", 4)) {
220                 p += snprintf(p, left, "\teui: %llx\n",
221                              info->params.device_path.i1394.eui);
222         } else if (!strncmp(info->params.interface_type, "FIBRE", 5)) {
223                 p += snprintf(p, left, "\twwid: %llx lun: %llx\n",
224                              info->params.device_path.fibre.wwid,
225                              info->params.device_path.fibre.lun);
226         } else if (!strncmp(info->params.interface_type, "I2O", 3)) {
227                 p += snprintf(p, left, "\tidentity_tag: %llx\n",
228                              info->params.device_path.i2o.identity_tag);
229         } else if (!strncmp(info->params.interface_type, "RAID", 4)) {
230                 p += snprintf(p, left, "\tidentity_tag: %x\n",
231                              info->params.device_path.raid.array_number);
232         } else if (!strncmp(info->params.interface_type, "SATA", 4)) {
233                 p += snprintf(p, left, "\tdevice: %u\n",
234                              info->params.device_path.sata.device);
235         } else {
236                 p += snprintf(p, left, "\tunknown: %llx %llx\n",
237                              info->params.device_path.unknown.reserved1,
238                              info->params.device_path.unknown.reserved2);
239         }
240
241         return proc_calc_metrics(page, start, off, count, eof, (p - page));
242 }
243
244 /**
245  * edd_show_raw_data() - unparses EDD information, returned to user-space
246  *
247  * Returns: number of bytes written, or 0 on failure
248  */
249 static int
250 edd_show_raw_data(char *page, char **start, off_t off, int count, int *eof, void *data)
251 {
252         struct edd_info *info = data;
253         char *p = page;
254         int i, warn_padding = 0, nonzero_path = 0,
255                 len = sizeof (*info) - 4;
256         uint8_t checksum = 0, c = 0;
257         if (!info || !page || off) {
258                 return proc_calc_metrics(page, start, off, count, eof, 0);
259         }
260
261         if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE))
262                 len = info->params.length;
263
264         p += snprintf(p, left, "int13 fn48 returned data:\n\n");
265         p += edd_dump_raw_data(p, left, ((char *) info) + 4, len);
266
267         /* Spec violation.  Adaptec AIC7899 returns 0xDDBE
268            here, when it should be 0xBEDD.
269          */
270         p += snprintf(p, left, "\n");
271         if (info->params.key == 0xDDBE) {
272                 p += snprintf(p, left,
273                              "Warning: Spec violation.  Key should be 0xBEDD, is 0xDDBE\n");
274         }
275
276         if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE)) {
277                 goto out;
278         }
279
280         for (i = 30; i <= 73; i++) {
281                 c = *(((uint8_t *) info) + i + 4);
282                 if (c)
283                         nonzero_path++;
284                 checksum += c;
285         }
286
287         if (checksum) {
288                 p += snprintf(p, left,
289                              "Warning: Spec violation.  Device Path checksum invalid.\n");
290         }
291
292         if (!nonzero_path) {
293                 p += snprintf(p, left, "Error: Spec violation.  Empty device path.\n");
294                 goto out;
295         }
296
297         for (i = 0; i < 4; i++) {
298                 if (!isprint(info->params.host_bus_type[i])) {
299                         warn_padding++;
300                 }
301         }
302         for (i = 0; i < 8; i++) {
303                 if (!isprint(info->params.interface_type[i])) {
304                         warn_padding++;
305                 }
306         }
307
308         if (warn_padding) {
309                 p += snprintf(p, left,
310                              "Warning: Spec violation.  Padding should be 0x20.\n");
311         }
312
313 out:
314         return proc_calc_metrics(page, start, off, count, eof, (p - page));
315 }
316
317 static int
318 edd_show_version(char *page, char **start, off_t off, int count, int *eof, void *data)
319 {
320         struct edd_info *info = data;
321         char *p = page;
322         if (!info || !page || off) {
323                 return proc_calc_metrics(page, start, off, count, eof, 0);
324         }
325
326         p += snprintf(p, left, "0x%02x\n", info->version);
327         return proc_calc_metrics(page, start, off, count, eof, (p - page));
328 }
329
330 static int
331 edd_show_disk80_sig(char *page, char **start, off_t off, int count, int *eof, void *data)
332 {
333         char *p = page;
334         if ( !page || off) {
335                 return proc_calc_metrics(page, start, off, count, eof, 0);
336         }
337
338         p += snprintf(p, left, "0x%08x\n", edd_disk80_sig);
339         return proc_calc_metrics(page, start, off, count, eof, (p - page));
340 }
341
342 static int
343 edd_show_extensions(char *page, char **start, off_t off, int count, int *eof, void *data)
344 {
345         struct edd_info *info = data;
346         char *p = page;
347         if (!info || !page || off) {
348                 return proc_calc_metrics(page, start, off, count, eof, 0);
349         }
350
351         if (info->interface_support & EDD_EXT_FIXED_DISK_ACCESS) {
352                 p += snprintf(p, left, "Fixed disk access\n");
353         }
354         if (info->interface_support & EDD_EXT_DEVICE_LOCKING_AND_EJECTING) {
355                 p += snprintf(p, left, "Device locking and ejecting\n");
356         }
357         if (info->interface_support & EDD_EXT_ENHANCED_DISK_DRIVE_SUPPORT) {
358                 p += snprintf(p, left, "Enhanced Disk Drive support\n");
359         }
360         if (info->interface_support & EDD_EXT_64BIT_EXTENSIONS) {
361                 p += snprintf(p, left, "64-bit extensions\n");
362         }
363         return proc_calc_metrics(page, start, off, count, eof, (p - page));
364 }
365
366 static int
367 edd_show_info_flags(char *page, char **start, off_t off, int count, int *eof, void *data)
368 {
369         struct edd_info *info = data;
370         char *p = page;
371         if (!info || !page || off) {
372                 return proc_calc_metrics(page, start, off, count, eof, 0);
373         }
374
375         if (info->params.info_flags & EDD_INFO_DMA_BOUNDRY_ERROR_TRANSPARENT)
376                 p += snprintf(p, left, "DMA boundry error transparent\n");
377         if (info->params.info_flags & EDD_INFO_GEOMETRY_VALID)
378                 p += snprintf(p, left, "geometry valid\n");
379         if (info->params.info_flags & EDD_INFO_REMOVABLE)
380                 p += snprintf(p, left, "removable\n");
381         if (info->params.info_flags & EDD_INFO_WRITE_VERIFY)
382                 p += snprintf(p, left, "write verify\n");
383         if (info->params.info_flags & EDD_INFO_MEDIA_CHANGE_NOTIFICATION)
384                 p += snprintf(p, left, "media change notification\n");
385         if (info->params.info_flags & EDD_INFO_LOCKABLE)
386                 p += snprintf(p, left, "lockable\n");
387         if (info->params.info_flags & EDD_INFO_NO_MEDIA_PRESENT)
388                 p += snprintf(p, left, "no media present\n");
389         if (info->params.info_flags & EDD_INFO_USE_INT13_FN50)
390                 p += snprintf(p, left, "use int13 fn50\n");
391         return proc_calc_metrics(page, start, off, count, eof, (p - page));
392 }
393
394 static int
395 edd_show_default_cylinders(char *page, char **start, off_t off, int count, int *eof, void *data)
396 {
397         struct edd_info *info = data;
398         char *p = page;
399         if (!info || !page || off) {
400                 return proc_calc_metrics(page, start, off, count, eof, 0);
401         }
402
403         p += snprintf(p, left, "%u\n", info->params.num_default_cylinders);
404         return proc_calc_metrics(page, start, off, count, eof, (p - page));
405 }
406
407 static int
408 edd_show_default_heads(char *page, char **start, off_t off, int count, int *eof, void *data)
409 {
410         struct edd_info *info = data;
411         char *p = page;
412         if (!info || !page || off) {
413                 return proc_calc_metrics(page, start, off, count, eof, 0);
414         }
415
416         p += snprintf(p, left, "%u\n", info->params.num_default_heads);
417         return proc_calc_metrics(page, start, off, count, eof, (p - page));
418 }
419
420 static int
421 edd_show_default_sectors_per_track(char *page, char **start, off_t off, int count, int *eof, void *data)
422 {
423         struct edd_info *info = data;
424         char *p = page;
425         if (!info || !page || off) {
426                 return proc_calc_metrics(page, start, off, count, eof, 0);
427         }
428
429         p += snprintf(p, left, "%u\n", info->params.sectors_per_track);
430         return proc_calc_metrics(page, start, off, count, eof, (p - page));
431 }
432
433 static int
434 edd_show_sectors(char *page, char **start, off_t off, int count, int *eof, void *data)
435 {
436         struct edd_info *info = data;
437         char *p = page;
438         if (!info || !page || off) {
439                 return proc_calc_metrics(page, start, off, count, eof, 0);
440         }
441
442         p += snprintf(p, left, "%llu\n", info->params.number_of_sectors);
443         return proc_calc_metrics(page, start, off, count, eof, (p - page));
444 }
445
446 static int
447 edd_has_default_cylinders(struct edd_device *edev)
448 {
449         struct edd_info *info = edd_dev_get_info(edev);
450         if (!edev || !info)
451                 return 0;
452         return info->params.num_default_cylinders > 0;
453 }
454
455 static int
456 edd_has_default_heads(struct edd_device *edev)
457 {
458         struct edd_info *info = edd_dev_get_info(edev);
459         if (!edev || !info)
460                 return 0;
461         return info->params.num_default_heads > 0;
462 }
463
464 static int
465 edd_has_default_sectors_per_track(struct edd_device *edev)
466 {
467         struct edd_info *info = edd_dev_get_info(edev);
468         if (!edev || !info)
469                 return 0;
470         return info->params.sectors_per_track > 0;
471 }
472
473 static int
474 edd_has_edd30(struct edd_device *edev)
475 {
476         struct edd_info *info = edd_dev_get_info(edev);
477         int i, nonzero_path = 0;
478         char c;
479
480         if (!edev || !info)
481                 return 0;
482
483         if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE)) {
484                 return 0;
485         }
486
487         for (i = 30; i <= 73; i++) {
488                 c = *(((uint8_t *) info) + i + 4);
489                 if (c) {
490                         nonzero_path++;
491                         break;
492                 }
493         }
494         if (!nonzero_path) {
495                 return 0;
496         }
497
498         return 1;
499 }
500
501 static int
502 edd_has_disk80_sig(struct edd_device *edev)
503 {
504         struct edd_info *info = edd_dev_get_info(edev);
505         if (!edev || !info)
506                 return 0;
507         return info->device == 0x80;
508 }
509
510 static EDD_DEVICE_ATTR(raw_data, edd_show_raw_data, NULL);
511 static EDD_DEVICE_ATTR(version, edd_show_version, NULL);
512 static EDD_DEVICE_ATTR(extensions, edd_show_extensions, NULL);
513 static EDD_DEVICE_ATTR(info_flags, edd_show_info_flags, NULL);
514 static EDD_DEVICE_ATTR(sectors, edd_show_sectors, NULL);
515 static EDD_DEVICE_ATTR(default_cylinders, edd_show_default_cylinders,
516                        edd_has_default_cylinders);
517 static EDD_DEVICE_ATTR(default_heads, edd_show_default_heads,
518                        edd_has_default_heads);
519 static EDD_DEVICE_ATTR(default_sectors_per_track,
520                        edd_show_default_sectors_per_track,
521                        edd_has_default_sectors_per_track);
522 static EDD_DEVICE_ATTR(interface, edd_show_interface,edd_has_edd30);
523 static EDD_DEVICE_ATTR(host_bus, edd_show_host_bus, edd_has_edd30);
524 static EDD_DEVICE_ATTR(mbr_signature, edd_show_disk80_sig, edd_has_disk80_sig);
525
526 static struct edd_attribute *def_attrs[] = {
527         &edd_attr_raw_data,
528         &edd_attr_version,
529         &edd_attr_extensions,
530         &edd_attr_info_flags,
531         &edd_attr_sectors,
532         &edd_attr_default_cylinders,
533         &edd_attr_default_heads,
534         &edd_attr_default_sectors_per_track,
535         &edd_attr_interface,
536         &edd_attr_host_bus,
537         &edd_attr_mbr_signature,
538         NULL,
539 };
540
541 static inline void
542 edd_device_unregister(struct edd_device *edev)
543 {
544         struct list_head *pos, *next;
545         struct attr_entry *ae;
546
547         list_for_each_safe(pos, next, &edev->attr_list) {
548                 ae = list_entry(pos, struct attr_entry, node);
549                 remove_proc_entry(ae->entry->name, edev->dir);
550                 list_del(&ae->node);
551                 kfree(ae);
552         }
553
554         remove_proc_entry(edev->dir->name, bios_dir);
555 }
556
557 static int
558 edd_populate_dir(struct edd_device *edev)
559 {
560         struct edd_attribute *attr;
561         struct attr_entry *ae;
562         int i;
563         int error = 0;
564
565         for (i = 0; (attr=def_attrs[i]); i++) {
566                 if (!attr->test || (attr->test && attr->test(edev))) {
567                         ae = kmalloc(sizeof (*ae), GFP_KERNEL);
568                         if (ae == NULL) {
569                                 error = 1;
570                                 break;
571                         }
572                         INIT_LIST_HEAD(&ae->node);
573                         ae->entry =
574                                 create_proc_read_entry(attr->name, 0444,
575                                                        edev->dir, attr->show,
576                                                        edd_dev_get_info(edev));
577                         if (ae->entry == NULL) {
578                                 error = 1;
579                                 break;
580                         }
581                         list_add(&ae->node, &edev->attr_list);
582                 }
583         }
584
585         if (error)
586                 return error;
587
588         return 0;
589 }
590
591 static int
592 edd_make_dir(struct edd_device *edev)
593 {
594         int error=1;
595
596         edev->dir = proc_mkdir(edev->name, bios_dir);
597         if (edev->dir != NULL) {
598                 edev->dir->mode = (S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
599                 error = edd_populate_dir(edev);
600         }
601         return error;
602 }
603
604 static int
605 edd_device_register(struct edd_device *edev, int i)
606 {
607         int error;
608
609         if (!edev)
610                 return 1;
611         memset(edev, 0, sizeof (*edev));
612         INIT_LIST_HEAD(&edev->attr_list);
613         edd_dev_set_info(edev, &edd[i]);
614         snprintf(edev->name, EDD_DEVICE_NAME_SIZE, "int13_dev%02x",
615                  edd[i].device);
616         error = edd_make_dir(edev);
617         return error;
618 }
619
620 /**
621  * edd_init() - creates /proc/bios tree of EDD data
622  *
623  * This assumes that eddnr and edd were
624  * assigned in setup.c already.
625  */
626 static int __init
627 edd_init(void)
628 {
629         unsigned int i;
630         int rc = 0;
631         struct edd_device *edev;
632
633         printk(KERN_INFO "BIOS EDD facility v%s, %d devices found\n",
634                EDD_VERSION, eddnr);
635
636         if (!eddnr) {
637                 printk(KERN_INFO "EDD information not available.\n");
638                 return 1;
639         }
640
641         bios_dir = proc_mkdir("bios", NULL);
642         if (bios_dir == NULL)
643                 return 1;
644
645         for (i = 0; i < eddnr && i < EDDMAXNR && !rc; i++) {
646                 edev = kmalloc(sizeof (*edev), GFP_KERNEL);
647                 if (!edev) {
648                         rc = 1;
649                         break;
650                 }
651
652                 rc = edd_device_register(edev, i);
653                 if (rc) {
654                         break;
655                 }
656                 edd_devices[i] = edev;
657         }
658
659         if (rc) {
660                 for (i = 0; i < eddnr && i < EDDMAXNR; i++) {
661                         if ((edev = edd_devices[i])) {
662                                 edd_device_unregister(edev);
663                                 kfree(edev);
664                         }
665                 }
666
667                 remove_proc_entry(bios_dir->name, NULL);
668         }
669
670         return rc;
671 }
672
673 static void __exit
674 edd_exit(void)
675 {
676         int i;
677         struct edd_device *edev;
678
679         for (i = 0; i < eddnr && i < EDDMAXNR; i++) {
680                 if ((edev = edd_devices[i])) {
681                         edd_device_unregister(edev);
682                         kfree(edev);
683                 }
684         }
685
686         remove_proc_entry(bios_dir->name, NULL);
687 }
688
689 module_init(edd_init);
690 module_exit(edd_exit);