iscsi_ibft.c 21 KB

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