iscsi_ibft.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2007-2010 Red Hat, Inc.
  4. * by Peter Jones <[email protected]>
  5. * Copyright 2008 IBM, Inc.
  6. * by Konrad Rzeszutek <[email protected]>
  7. * Copyright 2008
  8. * by Konrad Rzeszutek <[email protected]>
  9. *
  10. * This code exposes the iSCSI Boot Format Table to userland via sysfs.
  11. *
  12. * Changelog:
  13. *
  14. * 06 Jan 2010 - Peter Jones <[email protected]>
  15. * New changelog entries are in the git log from now on. Not here.
  16. *
  17. * 14 Mar 2008 - Konrad Rzeszutek <[email protected]>
  18. * Updated comments and copyrights. (v0.4.9)
  19. *
  20. * 11 Feb 2008 - Konrad Rzeszutek <[email protected]>
  21. * Converted to using ibft_addr. (v0.4.8)
  22. *
  23. * 8 Feb 2008 - Konrad Rzeszutek <[email protected]>
  24. * Combined two functions in one: reserve_ibft_region. (v0.4.7)
  25. *
  26. * 30 Jan 2008 - Konrad Rzeszutek <[email protected]>
  27. * Added logic to handle IPv6 addresses. (v0.4.6)
  28. *
  29. * 25 Jan 2008 - Konrad Rzeszutek <[email protected]>
  30. * Added logic to handle badly not-to-spec iBFT. (v0.4.5)
  31. *
  32. * 4 Jan 2008 - Konrad Rzeszutek <[email protected]>
  33. * Added __init to function declarations. (v0.4.4)
  34. *
  35. * 21 Dec 2007 - Konrad Rzeszutek <[email protected]>
  36. * Updated kobject registration, combined unregister functions in one
  37. * and code and style cleanup. (v0.4.3)
  38. *
  39. * 5 Dec 2007 - Konrad Rzeszutek <[email protected]>
  40. * Added end-markers to enums and re-organized kobject registration. (v0.4.2)
  41. *
  42. * 4 Dec 2007 - Konrad Rzeszutek <[email protected]>
  43. * Created 'device' sysfs link to the NIC and style cleanup. (v0.4.1)
  44. *
  45. * 28 Nov 2007 - Konrad Rzeszutek <[email protected]>
  46. * Added sysfs-ibft documentation, moved 'find_ibft' function to
  47. * in its own file and added text attributes for every struct field. (v0.4)
  48. *
  49. * 21 Nov 2007 - Konrad Rzeszutek <[email protected]>
  50. * Added text attributes emulating OpenFirmware /proc/device-tree naming.
  51. * Removed binary /sysfs interface (v0.3)
  52. *
  53. * 29 Aug 2007 - Konrad Rzeszutek <[email protected]>
  54. * Added functionality in setup.c to reserve iBFT region. (v0.2)
  55. *
  56. * 27 Aug 2007 - Konrad Rzeszutek <[email protected]>
  57. * First version exposing iBFT data via a binary /sysfs. (v0.1)
  58. */
  59. #include <linux/blkdev.h>
  60. #include <linux/capability.h>
  61. #include <linux/ctype.h>
  62. #include <linux/device.h>
  63. #include <linux/err.h>
  64. #include <linux/init.h>
  65. #include <linux/iscsi_ibft.h>
  66. #include <linux/limits.h>
  67. #include <linux/module.h>
  68. #include <linux/pci.h>
  69. #include <linux/slab.h>
  70. #include <linux/stat.h>
  71. #include <linux/string.h>
  72. #include <linux/types.h>
  73. #include <linux/acpi.h>
  74. #include <linux/iscsi_boot_sysfs.h>
  75. #define IBFT_ISCSI_VERSION "0.5.0"
  76. #define IBFT_ISCSI_DATE "2010-Feb-25"
  77. MODULE_AUTHOR("Peter Jones <[email protected]> and "
  78. "Konrad Rzeszutek <[email protected]>");
  79. MODULE_DESCRIPTION("sysfs interface to BIOS iBFT information");
  80. MODULE_LICENSE("GPL");
  81. MODULE_VERSION(IBFT_ISCSI_VERSION);
  82. static struct acpi_table_ibft *ibft_addr;
  83. struct ibft_hdr {
  84. u8 id;
  85. u8 version;
  86. u16 length;
  87. u8 index;
  88. u8 flags;
  89. } __attribute__((__packed__));
  90. struct ibft_control {
  91. struct ibft_hdr hdr;
  92. u16 extensions;
  93. u16 initiator_off;
  94. u16 nic0_off;
  95. u16 tgt0_off;
  96. u16 nic1_off;
  97. u16 tgt1_off;
  98. u16 expansion[];
  99. } __attribute__((__packed__));
  100. struct ibft_initiator {
  101. struct ibft_hdr hdr;
  102. char isns_server[16];
  103. char slp_server[16];
  104. char pri_radius_server[16];
  105. char sec_radius_server[16];
  106. u16 initiator_name_len;
  107. u16 initiator_name_off;
  108. } __attribute__((__packed__));
  109. struct ibft_nic {
  110. struct ibft_hdr hdr;
  111. char ip_addr[16];
  112. u8 subnet_mask_prefix;
  113. u8 origin;
  114. char gateway[16];
  115. char primary_dns[16];
  116. char secondary_dns[16];
  117. char dhcp[16];
  118. u16 vlan;
  119. char mac[6];
  120. u16 pci_bdf;
  121. u16 hostname_len;
  122. u16 hostname_off;
  123. } __attribute__((__packed__));
  124. struct ibft_tgt {
  125. struct ibft_hdr hdr;
  126. char ip_addr[16];
  127. u16 port;
  128. char lun[8];
  129. u8 chap_type;
  130. u8 nic_assoc;
  131. u16 tgt_name_len;
  132. u16 tgt_name_off;
  133. u16 chap_name_len;
  134. u16 chap_name_off;
  135. u16 chap_secret_len;
  136. u16 chap_secret_off;
  137. u16 rev_chap_name_len;
  138. u16 rev_chap_name_off;
  139. u16 rev_chap_secret_len;
  140. u16 rev_chap_secret_off;
  141. } __attribute__((__packed__));
  142. /*
  143. * The kobject different types and its names.
  144. *
  145. */
  146. enum ibft_id {
  147. id_reserved = 0, /* We don't support. */
  148. id_control = 1, /* Should show up only once and is not exported. */
  149. id_initiator = 2,
  150. id_nic = 3,
  151. id_target = 4,
  152. id_extensions = 5, /* We don't support. */
  153. id_end_marker,
  154. };
  155. /*
  156. * The kobject and attribute structures.
  157. */
  158. struct ibft_kobject {
  159. struct acpi_table_ibft *header;
  160. union {
  161. struct ibft_initiator *initiator;
  162. struct ibft_nic *nic;
  163. struct ibft_tgt *tgt;
  164. struct ibft_hdr *hdr;
  165. };
  166. };
  167. static struct iscsi_boot_kset *boot_kset;
  168. /* fully null address */
  169. static const char nulls[16];
  170. /* IPv4-mapped IPv6 ::ffff:0.0.0.0 */
  171. static const char mapped_nulls[16] = { 0x00, 0x00, 0x00, 0x00,
  172. 0x00, 0x00, 0x00, 0x00,
  173. 0x00, 0x00, 0xff, 0xff,
  174. 0x00, 0x00, 0x00, 0x00 };
  175. static int address_not_null(u8 *ip)
  176. {
  177. return (memcmp(ip, nulls, 16) && memcmp(ip, mapped_nulls, 16));
  178. }
  179. /*
  180. * Helper functions to parse data properly.
  181. */
  182. static ssize_t sprintf_ipaddr(char *buf, u8 *ip)
  183. {
  184. char *str = buf;
  185. if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0 &&
  186. ip[4] == 0 && ip[5] == 0 && ip[6] == 0 && ip[7] == 0 &&
  187. ip[8] == 0 && ip[9] == 0 && ip[10] == 0xff && ip[11] == 0xff) {
  188. /*
  189. * IPV4
  190. */
  191. str += sprintf(buf, "%pI4", ip + 12);
  192. } else {
  193. /*
  194. * IPv6
  195. */
  196. str += sprintf(str, "%pI6", ip);
  197. }
  198. str += sprintf(str, "\n");
  199. return str - buf;
  200. }
  201. static ssize_t sprintf_string(char *str, int len, char *buf)
  202. {
  203. return sprintf(str, "%.*s\n", len, buf);
  204. }
  205. /*
  206. * Helper function to verify the IBFT header.
  207. */
  208. static int ibft_verify_hdr(char *t, struct ibft_hdr *hdr, int id, int length)
  209. {
  210. if (hdr->id != id) {
  211. printk(KERN_ERR "iBFT error: We expected the %s " \
  212. "field header.id to have %d but " \
  213. "found %d instead!\n", t, id, hdr->id);
  214. return -ENODEV;
  215. }
  216. if (length && hdr->length != length) {
  217. printk(KERN_ERR "iBFT error: We expected the %s " \
  218. "field header.length to have %d but " \
  219. "found %d instead!\n", t, length, hdr->length);
  220. return -ENODEV;
  221. }
  222. return 0;
  223. }
  224. /*
  225. * Routines for parsing the iBFT data to be human readable.
  226. */
  227. static ssize_t ibft_attr_show_initiator(void *data, int type, char *buf)
  228. {
  229. struct ibft_kobject *entry = data;
  230. struct ibft_initiator *initiator = entry->initiator;
  231. void *ibft_loc = entry->header;
  232. char *str = buf;
  233. if (!initiator)
  234. return 0;
  235. switch (type) {
  236. case ISCSI_BOOT_INI_INDEX:
  237. str += sprintf(str, "%d\n", initiator->hdr.index);
  238. break;
  239. case ISCSI_BOOT_INI_FLAGS:
  240. str += sprintf(str, "%d\n", initiator->hdr.flags);
  241. break;
  242. case ISCSI_BOOT_INI_ISNS_SERVER:
  243. str += sprintf_ipaddr(str, initiator->isns_server);
  244. break;
  245. case ISCSI_BOOT_INI_SLP_SERVER:
  246. str += sprintf_ipaddr(str, initiator->slp_server);
  247. break;
  248. case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
  249. str += sprintf_ipaddr(str, initiator->pri_radius_server);
  250. break;
  251. case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
  252. str += sprintf_ipaddr(str, initiator->sec_radius_server);
  253. break;
  254. case ISCSI_BOOT_INI_INITIATOR_NAME:
  255. str += sprintf_string(str, initiator->initiator_name_len,
  256. (char *)ibft_loc +
  257. initiator->initiator_name_off);
  258. break;
  259. default:
  260. break;
  261. }
  262. return str - buf;
  263. }
  264. static ssize_t ibft_attr_show_nic(void *data, int type, char *buf)
  265. {
  266. struct ibft_kobject *entry = data;
  267. struct ibft_nic *nic = entry->nic;
  268. void *ibft_loc = entry->header;
  269. char *str = buf;
  270. __be32 val;
  271. if (!nic)
  272. return 0;
  273. switch (type) {
  274. case ISCSI_BOOT_ETH_INDEX:
  275. str += sprintf(str, "%d\n", nic->hdr.index);
  276. break;
  277. case ISCSI_BOOT_ETH_FLAGS:
  278. str += sprintf(str, "%d\n", nic->hdr.flags);
  279. break;
  280. case ISCSI_BOOT_ETH_IP_ADDR:
  281. str += sprintf_ipaddr(str, nic->ip_addr);
  282. break;
  283. case ISCSI_BOOT_ETH_SUBNET_MASK:
  284. val = cpu_to_be32(~((1 << (32-nic->subnet_mask_prefix))-1));
  285. str += sprintf(str, "%pI4", &val);
  286. break;
  287. case ISCSI_BOOT_ETH_PREFIX_LEN:
  288. str += sprintf(str, "%d\n", nic->subnet_mask_prefix);
  289. break;
  290. case ISCSI_BOOT_ETH_ORIGIN:
  291. str += sprintf(str, "%d\n", nic->origin);
  292. break;
  293. case ISCSI_BOOT_ETH_GATEWAY:
  294. str += sprintf_ipaddr(str, nic->gateway);
  295. break;
  296. case ISCSI_BOOT_ETH_PRIMARY_DNS:
  297. str += sprintf_ipaddr(str, nic->primary_dns);
  298. break;
  299. case ISCSI_BOOT_ETH_SECONDARY_DNS:
  300. str += sprintf_ipaddr(str, nic->secondary_dns);
  301. break;
  302. case ISCSI_BOOT_ETH_DHCP:
  303. str += sprintf_ipaddr(str, nic->dhcp);
  304. break;
  305. case ISCSI_BOOT_ETH_VLAN:
  306. str += sprintf(str, "%d\n", nic->vlan);
  307. break;
  308. case ISCSI_BOOT_ETH_MAC:
  309. str += sprintf(str, "%pM\n", nic->mac);
  310. break;
  311. case ISCSI_BOOT_ETH_HOSTNAME:
  312. str += sprintf_string(str, nic->hostname_len,
  313. (char *)ibft_loc + nic->hostname_off);
  314. break;
  315. default:
  316. break;
  317. }
  318. return str - buf;
  319. };
  320. static ssize_t ibft_attr_show_target(void *data, int type, char *buf)
  321. {
  322. struct ibft_kobject *entry = data;
  323. struct ibft_tgt *tgt = entry->tgt;
  324. void *ibft_loc = entry->header;
  325. char *str = buf;
  326. int i;
  327. if (!tgt)
  328. return 0;
  329. switch (type) {
  330. case ISCSI_BOOT_TGT_INDEX:
  331. str += sprintf(str, "%d\n", tgt->hdr.index);
  332. break;
  333. case ISCSI_BOOT_TGT_FLAGS:
  334. str += sprintf(str, "%d\n", tgt->hdr.flags);
  335. break;
  336. case ISCSI_BOOT_TGT_IP_ADDR:
  337. str += sprintf_ipaddr(str, tgt->ip_addr);
  338. break;
  339. case ISCSI_BOOT_TGT_PORT:
  340. str += sprintf(str, "%d\n", tgt->port);
  341. break;
  342. case ISCSI_BOOT_TGT_LUN:
  343. for (i = 0; i < 8; i++)
  344. str += sprintf(str, "%x", (u8)tgt->lun[i]);
  345. str += sprintf(str, "\n");
  346. break;
  347. case ISCSI_BOOT_TGT_NIC_ASSOC:
  348. str += sprintf(str, "%d\n", tgt->nic_assoc);
  349. break;
  350. case ISCSI_BOOT_TGT_CHAP_TYPE:
  351. str += sprintf(str, "%d\n", tgt->chap_type);
  352. break;
  353. case ISCSI_BOOT_TGT_NAME:
  354. str += sprintf_string(str, tgt->tgt_name_len,
  355. (char *)ibft_loc + tgt->tgt_name_off);
  356. break;
  357. case ISCSI_BOOT_TGT_CHAP_NAME:
  358. str += sprintf_string(str, tgt->chap_name_len,
  359. (char *)ibft_loc + tgt->chap_name_off);
  360. break;
  361. case ISCSI_BOOT_TGT_CHAP_SECRET:
  362. str += sprintf_string(str, tgt->chap_secret_len,
  363. (char *)ibft_loc + tgt->chap_secret_off);
  364. break;
  365. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  366. str += sprintf_string(str, tgt->rev_chap_name_len,
  367. (char *)ibft_loc +
  368. tgt->rev_chap_name_off);
  369. break;
  370. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  371. str += sprintf_string(str, tgt->rev_chap_secret_len,
  372. (char *)ibft_loc +
  373. tgt->rev_chap_secret_off);
  374. break;
  375. default:
  376. break;
  377. }
  378. return str - buf;
  379. }
  380. static ssize_t ibft_attr_show_acpitbl(void *data, int type, char *buf)
  381. {
  382. struct ibft_kobject *entry = data;
  383. char *str = buf;
  384. switch (type) {
  385. case ISCSI_BOOT_ACPITBL_SIGNATURE:
  386. str += sprintf_string(str, ACPI_NAMESEG_SIZE,
  387. entry->header->header.signature);
  388. break;
  389. case ISCSI_BOOT_ACPITBL_OEM_ID:
  390. str += sprintf_string(str, ACPI_OEM_ID_SIZE,
  391. entry->header->header.oem_id);
  392. break;
  393. case ISCSI_BOOT_ACPITBL_OEM_TABLE_ID:
  394. str += sprintf_string(str, ACPI_OEM_TABLE_ID_SIZE,
  395. entry->header->header.oem_table_id);
  396. break;
  397. default:
  398. break;
  399. }
  400. return str - buf;
  401. }
  402. static int __init ibft_check_device(void)
  403. {
  404. int len;
  405. u8 *pos;
  406. u8 csum = 0;
  407. len = ibft_addr->header.length;
  408. /* Sanity checking of iBFT. */
  409. if (ibft_addr->header.revision != 1) {
  410. printk(KERN_ERR "iBFT module supports only revision 1, " \
  411. "while this is %d.\n",
  412. ibft_addr->header.revision);
  413. return -ENOENT;
  414. }
  415. for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
  416. csum += *pos;
  417. if (csum) {
  418. printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
  419. return -ENOENT;
  420. }
  421. return 0;
  422. }
  423. /*
  424. * Helper routiners to check to determine if the entry is valid
  425. * in the proper iBFT structure.
  426. */
  427. static umode_t ibft_check_nic_for(void *data, int type)
  428. {
  429. struct ibft_kobject *entry = data;
  430. struct ibft_nic *nic = entry->nic;
  431. umode_t rc = 0;
  432. switch (type) {
  433. case ISCSI_BOOT_ETH_INDEX:
  434. case ISCSI_BOOT_ETH_FLAGS:
  435. rc = S_IRUGO;
  436. break;
  437. case ISCSI_BOOT_ETH_IP_ADDR:
  438. if (address_not_null(nic->ip_addr))
  439. rc = S_IRUGO;
  440. break;
  441. case ISCSI_BOOT_ETH_PREFIX_LEN:
  442. case ISCSI_BOOT_ETH_SUBNET_MASK:
  443. if (nic->subnet_mask_prefix)
  444. rc = S_IRUGO;
  445. break;
  446. case ISCSI_BOOT_ETH_ORIGIN:
  447. rc = S_IRUGO;
  448. break;
  449. case ISCSI_BOOT_ETH_GATEWAY:
  450. if (address_not_null(nic->gateway))
  451. rc = S_IRUGO;
  452. break;
  453. case ISCSI_BOOT_ETH_PRIMARY_DNS:
  454. if (address_not_null(nic->primary_dns))
  455. rc = S_IRUGO;
  456. break;
  457. case ISCSI_BOOT_ETH_SECONDARY_DNS:
  458. if (address_not_null(nic->secondary_dns))
  459. rc = S_IRUGO;
  460. break;
  461. case ISCSI_BOOT_ETH_DHCP:
  462. if (address_not_null(nic->dhcp))
  463. rc = S_IRUGO;
  464. break;
  465. case ISCSI_BOOT_ETH_VLAN:
  466. case ISCSI_BOOT_ETH_MAC:
  467. rc = S_IRUGO;
  468. break;
  469. case ISCSI_BOOT_ETH_HOSTNAME:
  470. if (nic->hostname_off)
  471. rc = S_IRUGO;
  472. break;
  473. default:
  474. break;
  475. }
  476. return rc;
  477. }
  478. static umode_t __init ibft_check_tgt_for(void *data, int type)
  479. {
  480. struct ibft_kobject *entry = data;
  481. struct ibft_tgt *tgt = entry->tgt;
  482. umode_t rc = 0;
  483. switch (type) {
  484. case ISCSI_BOOT_TGT_INDEX:
  485. case ISCSI_BOOT_TGT_FLAGS:
  486. case ISCSI_BOOT_TGT_IP_ADDR:
  487. case ISCSI_BOOT_TGT_PORT:
  488. case ISCSI_BOOT_TGT_LUN:
  489. case ISCSI_BOOT_TGT_NIC_ASSOC:
  490. case ISCSI_BOOT_TGT_CHAP_TYPE:
  491. rc = S_IRUGO;
  492. break;
  493. case ISCSI_BOOT_TGT_NAME:
  494. if (tgt->tgt_name_len)
  495. rc = S_IRUGO;
  496. break;
  497. case ISCSI_BOOT_TGT_CHAP_NAME:
  498. case ISCSI_BOOT_TGT_CHAP_SECRET:
  499. if (tgt->chap_name_len)
  500. rc = S_IRUGO;
  501. break;
  502. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  503. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  504. if (tgt->rev_chap_name_len)
  505. rc = S_IRUGO;
  506. break;
  507. default:
  508. break;
  509. }
  510. return rc;
  511. }
  512. static umode_t __init ibft_check_initiator_for(void *data, int type)
  513. {
  514. struct ibft_kobject *entry = data;
  515. struct ibft_initiator *init = entry->initiator;
  516. umode_t rc = 0;
  517. switch (type) {
  518. case ISCSI_BOOT_INI_INDEX:
  519. case ISCSI_BOOT_INI_FLAGS:
  520. rc = S_IRUGO;
  521. break;
  522. case ISCSI_BOOT_INI_ISNS_SERVER:
  523. if (address_not_null(init->isns_server))
  524. rc = S_IRUGO;
  525. break;
  526. case ISCSI_BOOT_INI_SLP_SERVER:
  527. if (address_not_null(init->slp_server))
  528. rc = S_IRUGO;
  529. break;
  530. case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
  531. if (address_not_null(init->pri_radius_server))
  532. rc = S_IRUGO;
  533. break;
  534. case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
  535. if (address_not_null(init->sec_radius_server))
  536. rc = S_IRUGO;
  537. break;
  538. case ISCSI_BOOT_INI_INITIATOR_NAME:
  539. if (init->initiator_name_len)
  540. rc = S_IRUGO;
  541. break;
  542. default:
  543. break;
  544. }
  545. return rc;
  546. }
  547. static umode_t __init ibft_check_acpitbl_for(void *data, int type)
  548. {
  549. umode_t rc = 0;
  550. switch (type) {
  551. case ISCSI_BOOT_ACPITBL_SIGNATURE:
  552. case ISCSI_BOOT_ACPITBL_OEM_ID:
  553. case ISCSI_BOOT_ACPITBL_OEM_TABLE_ID:
  554. rc = S_IRUGO;
  555. break;
  556. default:
  557. break;
  558. }
  559. return rc;
  560. }
  561. static void ibft_kobj_release(void *data)
  562. {
  563. kfree(data);
  564. }
  565. /*
  566. * Helper function for ibft_register_kobjects.
  567. */
  568. static int __init ibft_create_kobject(struct acpi_table_ibft *header,
  569. struct ibft_hdr *hdr)
  570. {
  571. struct iscsi_boot_kobj *boot_kobj = NULL;
  572. struct ibft_kobject *ibft_kobj = NULL;
  573. struct ibft_nic *nic = (struct ibft_nic *)hdr;
  574. struct pci_dev *pci_dev;
  575. int rc = 0;
  576. ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
  577. if (!ibft_kobj)
  578. return -ENOMEM;
  579. ibft_kobj->header = header;
  580. ibft_kobj->hdr = hdr;
  581. switch (hdr->id) {
  582. case id_initiator:
  583. rc = ibft_verify_hdr("initiator", hdr, id_initiator,
  584. sizeof(*ibft_kobj->initiator));
  585. if (rc)
  586. break;
  587. boot_kobj = iscsi_boot_create_initiator(boot_kset, hdr->index,
  588. ibft_kobj,
  589. ibft_attr_show_initiator,
  590. ibft_check_initiator_for,
  591. ibft_kobj_release);
  592. if (!boot_kobj) {
  593. rc = -ENOMEM;
  594. goto free_ibft_obj;
  595. }
  596. break;
  597. case id_nic:
  598. rc = ibft_verify_hdr("ethernet", hdr, id_nic,
  599. sizeof(*ibft_kobj->nic));
  600. if (rc)
  601. break;
  602. boot_kobj = iscsi_boot_create_ethernet(boot_kset, hdr->index,
  603. ibft_kobj,
  604. ibft_attr_show_nic,
  605. ibft_check_nic_for,
  606. ibft_kobj_release);
  607. if (!boot_kobj) {
  608. rc = -ENOMEM;
  609. goto free_ibft_obj;
  610. }
  611. break;
  612. case id_target:
  613. rc = ibft_verify_hdr("target", hdr, id_target,
  614. sizeof(*ibft_kobj->tgt));
  615. if (rc)
  616. break;
  617. boot_kobj = iscsi_boot_create_target(boot_kset, hdr->index,
  618. ibft_kobj,
  619. ibft_attr_show_target,
  620. ibft_check_tgt_for,
  621. ibft_kobj_release);
  622. if (!boot_kobj) {
  623. rc = -ENOMEM;
  624. goto free_ibft_obj;
  625. }
  626. break;
  627. case id_reserved:
  628. case id_control:
  629. case id_extensions:
  630. /* Fields which we don't support. Ignore them */
  631. rc = 1;
  632. break;
  633. default:
  634. printk(KERN_ERR "iBFT has unknown structure type (%d). " \
  635. "Report this bug to %.6s!\n", hdr->id,
  636. header->header.oem_id);
  637. rc = 1;
  638. break;
  639. }
  640. if (rc) {
  641. /* Skip adding this kobject, but exit with non-fatal error. */
  642. rc = 0;
  643. goto free_ibft_obj;
  644. }
  645. if (hdr->id == id_nic) {
  646. /*
  647. * We don't search for the device in other domains than
  648. * zero. This is because on x86 platforms the BIOS
  649. * executes only devices which are in domain 0. Furthermore, the
  650. * iBFT spec doesn't have a domain id field :-(
  651. */
  652. pci_dev = pci_get_domain_bus_and_slot(0,
  653. (nic->pci_bdf & 0xff00) >> 8,
  654. (nic->pci_bdf & 0xff));
  655. if (pci_dev) {
  656. rc = sysfs_create_link(&boot_kobj->kobj,
  657. &pci_dev->dev.kobj, "device");
  658. pci_dev_put(pci_dev);
  659. }
  660. }
  661. return 0;
  662. free_ibft_obj:
  663. kfree(ibft_kobj);
  664. return rc;
  665. }
  666. /*
  667. * Scan the IBFT table structure for the NIC and Target fields. When
  668. * found add them on the passed-in list. We do not support the other
  669. * fields at this point, so they are skipped.
  670. */
  671. static int __init ibft_register_kobjects(struct acpi_table_ibft *header)
  672. {
  673. struct ibft_control *control = NULL;
  674. struct iscsi_boot_kobj *boot_kobj;
  675. struct ibft_kobject *ibft_kobj;
  676. void *ptr, *end;
  677. int rc = 0;
  678. u16 offset;
  679. u16 eot_offset;
  680. control = (void *)header + sizeof(*header);
  681. end = (void *)control + control->hdr.length;
  682. eot_offset = (void *)header + header->header.length - (void *)control;
  683. rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control, 0);
  684. /* iBFT table safety checking */
  685. rc |= ((control->hdr.index) ? -ENODEV : 0);
  686. rc |= ((control->hdr.length < sizeof(*control)) ? -ENODEV : 0);
  687. if (rc) {
  688. printk(KERN_ERR "iBFT error: Control header is invalid!\n");
  689. return rc;
  690. }
  691. for (ptr = &control->initiator_off; ptr + sizeof(u16) <= end; ptr += sizeof(u16)) {
  692. offset = *(u16 *)ptr;
  693. if (offset && offset < header->header.length &&
  694. offset < eot_offset) {
  695. rc = ibft_create_kobject(header,
  696. (void *)header + offset);
  697. if (rc)
  698. break;
  699. }
  700. }
  701. if (rc)
  702. return rc;
  703. ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
  704. if (!ibft_kobj)
  705. return -ENOMEM;
  706. ibft_kobj->header = header;
  707. ibft_kobj->hdr = NULL; /*for ibft_unregister*/
  708. boot_kobj = iscsi_boot_create_acpitbl(boot_kset, 0,
  709. ibft_kobj,
  710. ibft_attr_show_acpitbl,
  711. ibft_check_acpitbl_for,
  712. ibft_kobj_release);
  713. if (!boot_kobj) {
  714. kfree(ibft_kobj);
  715. rc = -ENOMEM;
  716. }
  717. return rc;
  718. }
  719. static void ibft_unregister(void)
  720. {
  721. struct iscsi_boot_kobj *boot_kobj, *tmp_kobj;
  722. struct ibft_kobject *ibft_kobj;
  723. list_for_each_entry_safe(boot_kobj, tmp_kobj,
  724. &boot_kset->kobj_list, list) {
  725. ibft_kobj = boot_kobj->data;
  726. if (ibft_kobj->hdr && ibft_kobj->hdr->id == id_nic)
  727. sysfs_remove_link(&boot_kobj->kobj, "device");
  728. };
  729. }
  730. static void ibft_cleanup(void)
  731. {
  732. if (boot_kset) {
  733. ibft_unregister();
  734. iscsi_boot_destroy_kset(boot_kset);
  735. }
  736. }
  737. static void __exit ibft_exit(void)
  738. {
  739. ibft_cleanup();
  740. }
  741. #ifdef CONFIG_ACPI
  742. static const struct {
  743. char *sign;
  744. } ibft_signs[] = {
  745. /*
  746. * One spec says "IBFT", the other says "iBFT". We have to check
  747. * for both.
  748. */
  749. { ACPI_SIG_IBFT },
  750. { "iBFT" },
  751. { "BIFT" }, /* Broadcom iSCSI Offload */
  752. };
  753. static void __init acpi_find_ibft_region(void)
  754. {
  755. int i;
  756. struct acpi_table_header *table = NULL;
  757. if (acpi_disabled)
  758. return;
  759. for (i = 0; i < ARRAY_SIZE(ibft_signs) && !ibft_addr; i++) {
  760. acpi_get_table(ibft_signs[i].sign, 0, &table);
  761. ibft_addr = (struct acpi_table_ibft *)table;
  762. }
  763. }
  764. #else
  765. static void __init acpi_find_ibft_region(void)
  766. {
  767. }
  768. #endif
  769. #ifdef CONFIG_ISCSI_IBFT_FIND
  770. static int __init acpi_find_isa_region(void)
  771. {
  772. if (ibft_phys_addr) {
  773. ibft_addr = isa_bus_to_virt(ibft_phys_addr);
  774. return 0;
  775. }
  776. return -ENODEV;
  777. }
  778. #else
  779. static int __init acpi_find_isa_region(void)
  780. {
  781. return -ENODEV;
  782. }
  783. #endif
  784. /*
  785. * ibft_init() - creates sysfs tree entries for the iBFT data.
  786. */
  787. static int __init ibft_init(void)
  788. {
  789. int rc = 0;
  790. /*
  791. As on UEFI systems the setup_arch()/reserve_ibft_region()
  792. is called before ACPI tables are parsed and it only does
  793. legacy finding.
  794. */
  795. if (acpi_find_isa_region())
  796. acpi_find_ibft_region();
  797. if (ibft_addr) {
  798. pr_info("iBFT detected.\n");
  799. rc = ibft_check_device();
  800. if (rc)
  801. return rc;
  802. boot_kset = iscsi_boot_create_kset("ibft");
  803. if (!boot_kset)
  804. return -ENOMEM;
  805. /* Scan the IBFT for data and register the kobjects. */
  806. rc = ibft_register_kobjects(ibft_addr);
  807. if (rc)
  808. goto out_free;
  809. } else
  810. printk(KERN_INFO "No iBFT detected.\n");
  811. return 0;
  812. out_free:
  813. ibft_cleanup();
  814. return rc;
  815. }
  816. module_init(ibft_init);
  817. module_exit(ibft_exit);