edd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * linux/drivers/firmware/edd.c
  3. * Copyright (C) 2002, 2003, 2004 Dell Inc.
  4. * by Matt Domsch <Matt_Domsch@dell.com>
  5. * disk signature by Matt Domsch, Andrew Wilks, and Sandeep K. Shandilya
  6. * legacy CHS by Patrick J. LoPresti <patl@users.sourceforge.net>
  7. *
  8. * BIOS Enhanced Disk Drive Services (EDD)
  9. * conformant to T13 Committee www.t13.org
  10. * projects 1572D, 1484D, 1386D, 1226DT
  11. *
  12. * This code takes information provided by BIOS EDD calls
  13. * fn41 - Check Extensions Present and
  14. * fn48 - Get Device Parameters with EDD extensions
  15. * made in setup.S, copied to safe structures in setup.c,
  16. * and presents it in sysfs.
  17. *
  18. * Please see http://linux.dell.com/edd/results.html for
  19. * the list of BIOSs which have been reported to implement EDD.
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License v2.0 as published by
  23. * the Free Software Foundation
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. */
  31. #include <linux/module.h>
  32. #include <linux/string.h>
  33. #include <linux/types.h>
  34. #include <linux/init.h>
  35. #include <linux/stat.h>
  36. #include <linux/err.h>
  37. #include <linux/ctype.h>
  38. #include <linux/slab.h>
  39. #include <linux/limits.h>
  40. #include <linux/device.h>
  41. #include <linux/pci.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/edd.h>
  44. #define EDD_VERSION "0.16"
  45. #define EDD_DATE "2004-Jun-25"
  46. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  47. MODULE_DESCRIPTION("sysfs interface to BIOS EDD information");
  48. MODULE_LICENSE("GPL");
  49. MODULE_VERSION(EDD_VERSION);
  50. #define left (PAGE_SIZE - (p - buf) - 1)
  51. struct edd_device {
  52. unsigned int index;
  53. unsigned int mbr_signature;
  54. struct edd_info *info;
  55. struct kobject kobj;
  56. };
  57. struct edd_attribute {
  58. struct attribute attr;
  59. ssize_t(*show) (struct edd_device * edev, char *buf);
  60. int (*test) (struct edd_device * edev);
  61. };
  62. /* forward declarations */
  63. static int edd_dev_is_type(struct edd_device *edev, const char *type);
  64. static struct pci_dev *edd_get_pci_dev(struct edd_device *edev);
  65. static struct edd_device *edd_devices[EDD_MBR_SIG_MAX];
  66. #define EDD_DEVICE_ATTR(_name,_mode,_show,_test) \
  67. struct edd_attribute edd_attr_##_name = { \
  68. .attr = {.name = __stringify(_name), .mode = _mode }, \
  69. .show = _show, \
  70. .test = _test, \
  71. };
  72. static int
  73. edd_has_mbr_signature(struct edd_device *edev)
  74. {
  75. return edev->index < min_t(unsigned char, edd.mbr_signature_nr, EDD_MBR_SIG_MAX);
  76. }
  77. static int
  78. edd_has_edd_info(struct edd_device *edev)
  79. {
  80. return edev->index < min_t(unsigned char, edd.edd_info_nr, EDDMAXNR);
  81. }
  82. static inline struct edd_info *
  83. edd_dev_get_info(struct edd_device *edev)
  84. {
  85. return edev->info;
  86. }
  87. static inline void
  88. edd_dev_set_info(struct edd_device *edev, int i)
  89. {
  90. edev->index = i;
  91. if (edd_has_mbr_signature(edev))
  92. edev->mbr_signature = edd.mbr_signature[i];
  93. if (edd_has_edd_info(edev))
  94. edev->info = &edd.edd_info[i];
  95. }
  96. #define to_edd_attr(_attr) container_of(_attr,struct edd_attribute,attr)
  97. #define to_edd_device(obj) container_of(obj,struct edd_device,kobj)
  98. static ssize_t
  99. edd_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
  100. {
  101. struct edd_device *dev = to_edd_device(kobj);
  102. struct edd_attribute *edd_attr = to_edd_attr(attr);
  103. ssize_t ret = -EIO;
  104. if (edd_attr->show)
  105. ret = edd_attr->show(dev, buf);
  106. return ret;
  107. }
  108. static const struct sysfs_ops edd_attr_ops = {
  109. .show = edd_attr_show,
  110. };
  111. static ssize_t
  112. edd_show_host_bus(struct edd_device *edev, char *buf)
  113. {
  114. struct edd_info *info;
  115. char *p = buf;
  116. int i;
  117. if (!edev)
  118. return -EINVAL;
  119. info = edd_dev_get_info(edev);
  120. if (!info || !buf)
  121. return -EINVAL;
  122. for (i = 0; i < 4; i++) {
  123. if (isprint(info->params.host_bus_type[i])) {
  124. p += scnprintf(p, left, "%c", info->params.host_bus_type[i]);
  125. } else {
  126. p += scnprintf(p, left, " ");
  127. }
  128. }
  129. if (!strncmp(info->params.host_bus_type, "ISA", 3)) {
  130. p += scnprintf(p, left, "\tbase_address: %x\n",
  131. info->params.interface_path.isa.base_address);
  132. } else if (!strncmp(info->params.host_bus_type, "PCIX", 4) ||
  133. !strncmp(info->params.host_bus_type, "PCI", 3) ||
  134. !strncmp(info->params.host_bus_type, "XPRS", 4)) {
  135. p += scnprintf(p, left,
  136. "\t%02x:%02x.%d channel: %u\n",
  137. info->params.interface_path.pci.bus,
  138. info->params.interface_path.pci.slot,
  139. info->params.interface_path.pci.function,
  140. info->params.interface_path.pci.channel);
  141. } else if (!strncmp(info->params.host_bus_type, "IBND", 4) ||
  142. !strncmp(info->params.host_bus_type, "HTPT", 4)) {
  143. p += scnprintf(p, left,
  144. "\tTBD: %llx\n",
  145. info->params.interface_path.ibnd.reserved);
  146. } else {
  147. p += scnprintf(p, left, "\tunknown: %llx\n",
  148. info->params.interface_path.unknown.reserved);
  149. }
  150. return (p - buf);
  151. }
  152. static ssize_t
  153. edd_show_interface(struct edd_device *edev, char *buf)
  154. {
  155. struct edd_info *info;
  156. char *p = buf;
  157. int i;
  158. if (!edev)
  159. return -EINVAL;
  160. info = edd_dev_get_info(edev);
  161. if (!info || !buf)
  162. return -EINVAL;
  163. for (i = 0; i < 8; i++) {
  164. if (isprint(info->params.interface_type[i])) {
  165. p += scnprintf(p, left, "%c", info->params.interface_type[i]);
  166. } else {
  167. p += scnprintf(p, left, " ");
  168. }
  169. }
  170. if (!strncmp(info->params.interface_type, "ATAPI", 5)) {
  171. p += scnprintf(p, left, "\tdevice: %u lun: %u\n",
  172. info->params.device_path.atapi.device,
  173. info->params.device_path.atapi.lun);
  174. } else if (!strncmp(info->params.interface_type, "ATA", 3)) {
  175. p += scnprintf(p, left, "\tdevice: %u\n",
  176. info->params.device_path.ata.device);
  177. } else if (!strncmp(info->params.interface_type, "SCSI", 4)) {
  178. p += scnprintf(p, left, "\tid: %u lun: %llu\n",
  179. info->params.device_path.scsi.id,
  180. info->params.device_path.scsi.lun);
  181. } else if (!strncmp(info->params.interface_type, "USB", 3)) {
  182. p += scnprintf(p, left, "\tserial_number: %llx\n",
  183. info->params.device_path.usb.serial_number);
  184. } else if (!strncmp(info->params.interface_type, "1394", 4)) {
  185. p += scnprintf(p, left, "\teui: %llx\n",
  186. info->params.device_path.i1394.eui);
  187. } else if (!strncmp(info->params.interface_type, "FIBRE", 5)) {
  188. p += scnprintf(p, left, "\twwid: %llx lun: %llx\n",
  189. info->params.device_path.fibre.wwid,
  190. info->params.device_path.fibre.lun);
  191. } else if (!strncmp(info->params.interface_type, "I2O", 3)) {
  192. p += scnprintf(p, left, "\tidentity_tag: %llx\n",
  193. info->params.device_path.i2o.identity_tag);
  194. } else if (!strncmp(info->params.interface_type, "RAID", 4)) {
  195. p += scnprintf(p, left, "\tidentity_tag: %x\n",
  196. info->params.device_path.raid.array_number);
  197. } else if (!strncmp(info->params.interface_type, "SATA", 4)) {
  198. p += scnprintf(p, left, "\tdevice: %u\n",
  199. info->params.device_path.sata.device);
  200. } else {
  201. p += scnprintf(p, left, "\tunknown: %llx %llx\n",
  202. info->params.device_path.unknown.reserved1,
  203. info->params.device_path.unknown.reserved2);
  204. }
  205. return (p - buf);
  206. }
  207. /**
  208. * edd_show_raw_data() - copies raw data to buffer for userspace to parse
  209. * @edev: target edd_device
  210. * @buf: output buffer
  211. *
  212. * Returns: number of bytes written, or -EINVAL on failure
  213. */
  214. static ssize_t
  215. edd_show_raw_data(struct edd_device *edev, char *buf)
  216. {
  217. struct edd_info *info;
  218. ssize_t len = sizeof (info->params);
  219. if (!edev)
  220. return -EINVAL;
  221. info = edd_dev_get_info(edev);
  222. if (!info || !buf)
  223. return -EINVAL;
  224. if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE))
  225. len = info->params.length;
  226. /* In case of buggy BIOSs */
  227. if (len > (sizeof(info->params)))
  228. len = sizeof(info->params);
  229. memcpy(buf, &info->params, len);
  230. return len;
  231. }
  232. static ssize_t
  233. edd_show_version(struct edd_device *edev, char *buf)
  234. {
  235. struct edd_info *info;
  236. char *p = buf;
  237. if (!edev)
  238. return -EINVAL;
  239. info = edd_dev_get_info(edev);
  240. if (!info || !buf)
  241. return -EINVAL;
  242. p += scnprintf(p, left, "0x%02x\n", info->version);
  243. return (p - buf);
  244. }
  245. static ssize_t
  246. edd_show_mbr_signature(struct edd_device *edev, char *buf)
  247. {
  248. char *p = buf;
  249. p += scnprintf(p, left, "0x%08x\n", edev->mbr_signature);
  250. return (p - buf);
  251. }
  252. static ssize_t
  253. edd_show_extensions(struct edd_device *edev, char *buf)
  254. {
  255. struct edd_info *info;
  256. char *p = buf;
  257. if (!edev)
  258. return -EINVAL;
  259. info = edd_dev_get_info(edev);
  260. if (!info || !buf)
  261. return -EINVAL;
  262. if (info->interface_support & EDD_EXT_FIXED_DISK_ACCESS) {
  263. p += scnprintf(p, left, "Fixed disk access\n");
  264. }
  265. if (info->interface_support & EDD_EXT_DEVICE_LOCKING_AND_EJECTING) {
  266. p += scnprintf(p, left, "Device locking and ejecting\n");
  267. }
  268. if (info->interface_support & EDD_EXT_ENHANCED_DISK_DRIVE_SUPPORT) {
  269. p += scnprintf(p, left, "Enhanced Disk Drive support\n");
  270. }
  271. if (info->interface_support & EDD_EXT_64BIT_EXTENSIONS) {
  272. p += scnprintf(p, left, "64-bit extensions\n");
  273. }
  274. return (p - buf);
  275. }
  276. static ssize_t
  277. edd_show_info_flags(struct edd_device *edev, char *buf)
  278. {
  279. struct edd_info *info;
  280. char *p = buf;
  281. if (!edev)
  282. return -EINVAL;
  283. info = edd_dev_get_info(edev);
  284. if (!info || !buf)
  285. return -EINVAL;
  286. if (info->params.info_flags & EDD_INFO_DMA_BOUNDARY_ERROR_TRANSPARENT)
  287. p += scnprintf(p, left, "DMA boundary error transparent\n");
  288. if (info->params.info_flags & EDD_INFO_GEOMETRY_VALID)
  289. p += scnprintf(p, left, "geometry valid\n");
  290. if (info->params.info_flags & EDD_INFO_REMOVABLE)
  291. p += scnprintf(p, left, "removable\n");
  292. if (info->params.info_flags & EDD_INFO_WRITE_VERIFY)
  293. p += scnprintf(p, left, "write verify\n");
  294. if (info->params.info_flags & EDD_INFO_MEDIA_CHANGE_NOTIFICATION)
  295. p += scnprintf(p, left, "media change notification\n");
  296. if (info->params.info_flags & EDD_INFO_LOCKABLE)
  297. p += scnprintf(p, left, "lockable\n");
  298. if (info->params.info_flags & EDD_INFO_NO_MEDIA_PRESENT)
  299. p += scnprintf(p, left, "no media present\n");
  300. if (info->params.info_flags & EDD_INFO_USE_INT13_FN50)
  301. p += scnprintf(p, left, "use int13 fn50\n");
  302. return (p - buf);
  303. }
  304. static ssize_t
  305. edd_show_legacy_max_cylinder(struct edd_device *edev, char *buf)
  306. {
  307. struct edd_info *info;
  308. char *p = buf;
  309. if (!edev)
  310. return -EINVAL;
  311. info = edd_dev_get_info(edev);
  312. if (!info || !buf)
  313. return -EINVAL;
  314. p += snprintf(p, left, "%u\n", info->legacy_max_cylinder);
  315. return (p - buf);
  316. }
  317. static ssize_t
  318. edd_show_legacy_max_head(struct edd_device *edev, char *buf)
  319. {
  320. struct edd_info *info;
  321. char *p = buf;
  322. if (!edev)
  323. return -EINVAL;
  324. info = edd_dev_get_info(edev);
  325. if (!info || !buf)
  326. return -EINVAL;
  327. p += snprintf(p, left, "%u\n", info->legacy_max_head);
  328. return (p - buf);
  329. }
  330. static ssize_t
  331. edd_show_legacy_sectors_per_track(struct edd_device *edev, char *buf)
  332. {
  333. struct edd_info *info;
  334. char *p = buf;
  335. if (!edev)
  336. return -EINVAL;
  337. info = edd_dev_get_info(edev);
  338. if (!info || !buf)
  339. return -EINVAL;
  340. p += snprintf(p, left, "%u\n", info->legacy_sectors_per_track);
  341. return (p - buf);
  342. }
  343. static ssize_t
  344. edd_show_default_cylinders(struct edd_device *edev, char *buf)
  345. {
  346. struct edd_info *info;
  347. char *p = buf;
  348. if (!edev)
  349. return -EINVAL;
  350. info = edd_dev_get_info(edev);
  351. if (!info || !buf)
  352. return -EINVAL;
  353. p += scnprintf(p, left, "%u\n", info->params.num_default_cylinders);
  354. return (p - buf);
  355. }
  356. static ssize_t
  357. edd_show_default_heads(struct edd_device *edev, char *buf)
  358. {
  359. struct edd_info *info;
  360. char *p = buf;
  361. if (!edev)
  362. return -EINVAL;
  363. info = edd_dev_get_info(edev);
  364. if (!info || !buf)
  365. return -EINVAL;
  366. p += scnprintf(p, left, "%u\n", info->params.num_default_heads);
  367. return (p - buf);
  368. }
  369. static ssize_t
  370. edd_show_default_sectors_per_track(struct edd_device *edev, char *buf)
  371. {
  372. struct edd_info *info;
  373. char *p = buf;
  374. if (!edev)
  375. return -EINVAL;
  376. info = edd_dev_get_info(edev);
  377. if (!info || !buf)
  378. return -EINVAL;
  379. p += scnprintf(p, left, "%u\n", info->params.sectors_per_track);
  380. return (p - buf);
  381. }
  382. static ssize_t
  383. edd_show_sectors(struct edd_device *edev, char *buf)
  384. {
  385. struct edd_info *info;
  386. char *p = buf;
  387. if (!edev)
  388. return -EINVAL;
  389. info = edd_dev_get_info(edev);
  390. if (!info || !buf)
  391. return -EINVAL;
  392. p += scnprintf(p, left, "%llu\n", info->params.number_of_sectors);
  393. return (p - buf);
  394. }
  395. /*
  396. * Some device instances may not have all the above attributes,
  397. * or the attribute values may be meaningless (i.e. if
  398. * the device is < EDD 3.0, it won't have host_bus and interface
  399. * information), so don't bother making files for them. Likewise
  400. * if the default_{cylinders,heads,sectors_per_track} values
  401. * are zero, the BIOS doesn't provide sane values, don't bother
  402. * creating files for them either.
  403. */
  404. static int
  405. edd_has_legacy_max_cylinder(struct edd_device *edev)
  406. {
  407. struct edd_info *info;
  408. if (!edev)
  409. return 0;
  410. info = edd_dev_get_info(edev);
  411. if (!info)
  412. return 0;
  413. return info->legacy_max_cylinder > 0;
  414. }
  415. static int
  416. edd_has_legacy_max_head(struct edd_device *edev)
  417. {
  418. struct edd_info *info;
  419. if (!edev)
  420. return 0;
  421. info = edd_dev_get_info(edev);
  422. if (!info)
  423. return 0;
  424. return info->legacy_max_head > 0;
  425. }
  426. static int
  427. edd_has_legacy_sectors_per_track(struct edd_device *edev)
  428. {
  429. struct edd_info *info;
  430. if (!edev)
  431. return 0;
  432. info = edd_dev_get_info(edev);
  433. if (!info)
  434. return 0;
  435. return info->legacy_sectors_per_track > 0;
  436. }
  437. static int
  438. edd_has_default_cylinders(struct edd_device *edev)
  439. {
  440. struct edd_info *info;
  441. if (!edev)
  442. return 0;
  443. info = edd_dev_get_info(edev);
  444. if (!info)
  445. return 0;
  446. return info->params.num_default_cylinders > 0;
  447. }
  448. static int
  449. edd_has_default_heads(struct edd_device *edev)
  450. {
  451. struct edd_info *info;
  452. if (!edev)
  453. return 0;
  454. info = edd_dev_get_info(edev);
  455. if (!info)
  456. return 0;
  457. return info->params.num_default_heads > 0;
  458. }
  459. static int
  460. edd_has_default_sectors_per_track(struct edd_device *edev)
  461. {
  462. struct edd_info *info;
  463. if (!edev)
  464. return 0;
  465. info = edd_dev_get_info(edev);
  466. if (!info)
  467. return 0;
  468. return info->params.sectors_per_track > 0;
  469. }
  470. static int
  471. edd_has_edd30(struct edd_device *edev)
  472. {
  473. struct edd_info *info;
  474. int i;
  475. u8 csum = 0;
  476. if (!edev)
  477. return 0;
  478. info = edd_dev_get_info(edev);
  479. if (!info)
  480. return 0;
  481. if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE)) {
  482. return 0;
  483. }
  484. /* We support only T13 spec */
  485. if (info->params.device_path_info_length != 44)
  486. return 0;
  487. for (i = 30; i < info->params.device_path_info_length + 30; i++)
  488. csum += *(((u8 *)&info->params) + i);
  489. if (csum)
  490. return 0;
  491. return 1;
  492. }
  493. static EDD_DEVICE_ATTR(raw_data, 0444, edd_show_raw_data, edd_has_edd_info);
  494. static EDD_DEVICE_ATTR(version, 0444, edd_show_version, edd_has_edd_info);
  495. static EDD_DEVICE_ATTR(extensions, 0444, edd_show_extensions, edd_has_edd_info);
  496. static EDD_DEVICE_ATTR(info_flags, 0444, edd_show_info_flags, edd_has_edd_info);
  497. static EDD_DEVICE_ATTR(sectors, 0444, edd_show_sectors, edd_has_edd_info);
  498. static EDD_DEVICE_ATTR(legacy_max_cylinder, 0444,
  499. edd_show_legacy_max_cylinder,
  500. edd_has_legacy_max_cylinder);
  501. static EDD_DEVICE_ATTR(legacy_max_head, 0444, edd_show_legacy_max_head,
  502. edd_has_legacy_max_head);
  503. static EDD_DEVICE_ATTR(legacy_sectors_per_track, 0444,
  504. edd_show_legacy_sectors_per_track,
  505. edd_has_legacy_sectors_per_track);
  506. static EDD_DEVICE_ATTR(default_cylinders, 0444, edd_show_default_cylinders,
  507. edd_has_default_cylinders);
  508. static EDD_DEVICE_ATTR(default_heads, 0444, edd_show_default_heads,
  509. edd_has_default_heads);
  510. static EDD_DEVICE_ATTR(default_sectors_per_track, 0444,
  511. edd_show_default_sectors_per_track,
  512. edd_has_default_sectors_per_track);
  513. static EDD_DEVICE_ATTR(interface, 0444, edd_show_interface, edd_has_edd30);
  514. static EDD_DEVICE_ATTR(host_bus, 0444, edd_show_host_bus, edd_has_edd30);
  515. static EDD_DEVICE_ATTR(mbr_signature, 0444, edd_show_mbr_signature, edd_has_mbr_signature);
  516. /* These are default attributes that are added for every edd
  517. * device discovered. There are none.
  518. */
  519. static struct attribute * def_attrs[] = {
  520. NULL,
  521. };
  522. /* These attributes are conditional and only added for some devices. */
  523. static struct edd_attribute * edd_attrs[] = {
  524. &edd_attr_raw_data,
  525. &edd_attr_version,
  526. &edd_attr_extensions,
  527. &edd_attr_info_flags,
  528. &edd_attr_sectors,
  529. &edd_attr_legacy_max_cylinder,
  530. &edd_attr_legacy_max_head,
  531. &edd_attr_legacy_sectors_per_track,
  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. * edd_release - free edd structure
  542. * @kobj: kobject of edd structure
  543. *
  544. * This is called when the refcount of the edd structure
  545. * reaches 0. This should happen right after we unregister,
  546. * but just in case, we use the release callback anyway.
  547. */
  548. static void edd_release(struct kobject * kobj)
  549. {
  550. struct edd_device * dev = to_edd_device(kobj);
  551. kfree(dev);
  552. }
  553. static struct kobj_type edd_ktype = {
  554. .release = edd_release,
  555. .sysfs_ops = &edd_attr_ops,
  556. .default_attrs = def_attrs,
  557. };
  558. static struct kset *edd_kset;
  559. /**
  560. * edd_dev_is_type() - is this EDD device a 'type' device?
  561. * @edev: target edd_device
  562. * @type: a host bus or interface identifier string per the EDD spec
  563. *
  564. * Returns 1 (TRUE) if it is a 'type' device, 0 otherwise.
  565. */
  566. static int
  567. edd_dev_is_type(struct edd_device *edev, const char *type)
  568. {
  569. struct edd_info *info;
  570. if (!edev)
  571. return 0;
  572. info = edd_dev_get_info(edev);
  573. if (type && info) {
  574. if (!strncmp(info->params.host_bus_type, type, strlen(type)) ||
  575. !strncmp(info->params.interface_type, type, strlen(type)))
  576. return 1;
  577. }
  578. return 0;
  579. }
  580. /**
  581. * edd_get_pci_dev() - finds pci_dev that matches edev
  582. * @edev: edd_device
  583. *
  584. * Returns pci_dev if found, or NULL
  585. */
  586. static struct pci_dev *
  587. edd_get_pci_dev(struct edd_device *edev)
  588. {
  589. struct edd_info *info = edd_dev_get_info(edev);
  590. if (edd_dev_is_type(edev, "PCI") || edd_dev_is_type(edev, "XPRS")) {
  591. return pci_get_bus_and_slot(info->params.interface_path.pci.bus,
  592. PCI_DEVFN(info->params.interface_path.pci.slot,
  593. info->params.interface_path.pci.
  594. function));
  595. }
  596. return NULL;
  597. }
  598. static int
  599. edd_create_symlink_to_pcidev(struct edd_device *edev)
  600. {
  601. struct pci_dev *pci_dev = edd_get_pci_dev(edev);
  602. int ret;
  603. if (!pci_dev)
  604. return 1;
  605. ret = sysfs_create_link(&edev->kobj,&pci_dev->dev.kobj,"pci_dev");
  606. pci_dev_put(pci_dev);
  607. return ret;
  608. }
  609. static inline void
  610. edd_device_unregister(struct edd_device *edev)
  611. {
  612. kobject_put(&edev->kobj);
  613. }
  614. static void edd_populate_dir(struct edd_device * edev)
  615. {
  616. struct edd_attribute * attr;
  617. int error = 0;
  618. int i;
  619. for (i = 0; (attr = edd_attrs[i]) && !error; i++) {
  620. if (!attr->test ||
  621. (attr->test && attr->test(edev)))
  622. error = sysfs_create_file(&edev->kobj,&attr->attr);
  623. }
  624. if (!error) {
  625. edd_create_symlink_to_pcidev(edev);
  626. }
  627. }
  628. static int
  629. edd_device_register(struct edd_device *edev, int i)
  630. {
  631. int error;
  632. if (!edev)
  633. return 1;
  634. edd_dev_set_info(edev, i);
  635. edev->kobj.kset = edd_kset;
  636. error = kobject_init_and_add(&edev->kobj, &edd_ktype, NULL,
  637. "int13_dev%02x", 0x80 + i);
  638. if (!error) {
  639. edd_populate_dir(edev);
  640. kobject_uevent(&edev->kobj, KOBJ_ADD);
  641. }
  642. return error;
  643. }
  644. static inline int edd_num_devices(void)
  645. {
  646. return max_t(unsigned char,
  647. min_t(unsigned char, EDD_MBR_SIG_MAX, edd.mbr_signature_nr),
  648. min_t(unsigned char, EDDMAXNR, edd.edd_info_nr));
  649. }
  650. /**
  651. * edd_init() - creates sysfs tree of EDD data
  652. */
  653. static int __init
  654. edd_init(void)
  655. {
  656. int i;
  657. int rc=0;
  658. struct edd_device *edev;
  659. printk(KERN_INFO "BIOS EDD facility v%s %s, %d devices found\n",
  660. EDD_VERSION, EDD_DATE, edd_num_devices());
  661. if (!edd_num_devices()) {
  662. printk(KERN_INFO "EDD information not available.\n");
  663. return -ENODEV;
  664. }
  665. edd_kset = kset_create_and_add("edd", NULL, firmware_kobj);
  666. if (!edd_kset)
  667. return -ENOMEM;
  668. for (i = 0; i < edd_num_devices(); i++) {
  669. edev = kzalloc(sizeof (*edev), GFP_KERNEL);
  670. if (!edev) {
  671. rc = -ENOMEM;
  672. goto out;
  673. }
  674. rc = edd_device_register(edev, i);
  675. if (rc) {
  676. kfree(edev);
  677. goto out;
  678. }
  679. edd_devices[i] = edev;
  680. }
  681. return 0;
  682. out:
  683. while (--i >= 0)
  684. edd_device_unregister(edd_devices[i]);
  685. kset_unregister(edd_kset);
  686. return rc;
  687. }
  688. static void __exit
  689. edd_exit(void)
  690. {
  691. int i;
  692. struct edd_device *edev;
  693. for (i = 0; i < edd_num_devices(); i++) {
  694. if ((edev = edd_devices[i]))
  695. edd_device_unregister(edev);
  696. }
  697. kset_unregister(edd_kset);
  698. }
  699. late_initcall(edd_init);
  700. module_exit(edd_exit);