iscsi_ibft.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. 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. if (nic->subnet_mask_prefix > 32)
  285. val = cpu_to_be32(~0);
  286. else
  287. val = cpu_to_be32(~((1 << (32-nic->subnet_mask_prefix))-1));
  288. str += sprintf(str, "%pI4", &val);
  289. break;
  290. case ISCSI_BOOT_ETH_PREFIX_LEN:
  291. str += sprintf(str, "%d\n", nic->subnet_mask_prefix);
  292. break;
  293. case ISCSI_BOOT_ETH_ORIGIN:
  294. str += sprintf(str, "%d\n", nic->origin);
  295. break;
  296. case ISCSI_BOOT_ETH_GATEWAY:
  297. str += sprintf_ipaddr(str, nic->gateway);
  298. break;
  299. case ISCSI_BOOT_ETH_PRIMARY_DNS:
  300. str += sprintf_ipaddr(str, nic->primary_dns);
  301. break;
  302. case ISCSI_BOOT_ETH_SECONDARY_DNS:
  303. str += sprintf_ipaddr(str, nic->secondary_dns);
  304. break;
  305. case ISCSI_BOOT_ETH_DHCP:
  306. str += sprintf_ipaddr(str, nic->dhcp);
  307. break;
  308. case ISCSI_BOOT_ETH_VLAN:
  309. str += sprintf(str, "%d\n", nic->vlan);
  310. break;
  311. case ISCSI_BOOT_ETH_MAC:
  312. str += sprintf(str, "%pM\n", nic->mac);
  313. break;
  314. case ISCSI_BOOT_ETH_HOSTNAME:
  315. str += sprintf_string(str, nic->hostname_len,
  316. (char *)ibft_loc + nic->hostname_off);
  317. break;
  318. default:
  319. break;
  320. }
  321. return str - buf;
  322. };
  323. static ssize_t ibft_attr_show_target(void *data, int type, char *buf)
  324. {
  325. struct ibft_kobject *entry = data;
  326. struct ibft_tgt *tgt = entry->tgt;
  327. void *ibft_loc = entry->header;
  328. char *str = buf;
  329. int i;
  330. if (!tgt)
  331. return 0;
  332. switch (type) {
  333. case ISCSI_BOOT_TGT_INDEX:
  334. str += sprintf(str, "%d\n", tgt->hdr.index);
  335. break;
  336. case ISCSI_BOOT_TGT_FLAGS:
  337. str += sprintf(str, "%d\n", tgt->hdr.flags);
  338. break;
  339. case ISCSI_BOOT_TGT_IP_ADDR:
  340. str += sprintf_ipaddr(str, tgt->ip_addr);
  341. break;
  342. case ISCSI_BOOT_TGT_PORT:
  343. str += sprintf(str, "%d\n", tgt->port);
  344. break;
  345. case ISCSI_BOOT_TGT_LUN:
  346. for (i = 0; i < 8; i++)
  347. str += sprintf(str, "%x", (u8)tgt->lun[i]);
  348. str += sprintf(str, "\n");
  349. break;
  350. case ISCSI_BOOT_TGT_NIC_ASSOC:
  351. str += sprintf(str, "%d\n", tgt->nic_assoc);
  352. break;
  353. case ISCSI_BOOT_TGT_CHAP_TYPE:
  354. str += sprintf(str, "%d\n", tgt->chap_type);
  355. break;
  356. case ISCSI_BOOT_TGT_NAME:
  357. str += sprintf_string(str, tgt->tgt_name_len,
  358. (char *)ibft_loc + tgt->tgt_name_off);
  359. break;
  360. case ISCSI_BOOT_TGT_CHAP_NAME:
  361. str += sprintf_string(str, tgt->chap_name_len,
  362. (char *)ibft_loc + tgt->chap_name_off);
  363. break;
  364. case ISCSI_BOOT_TGT_CHAP_SECRET:
  365. str += sprintf_string(str, tgt->chap_secret_len,
  366. (char *)ibft_loc + tgt->chap_secret_off);
  367. break;
  368. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  369. str += sprintf_string(str, tgt->rev_chap_name_len,
  370. (char *)ibft_loc +
  371. tgt->rev_chap_name_off);
  372. break;
  373. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  374. str += sprintf_string(str, tgt->rev_chap_secret_len,
  375. (char *)ibft_loc +
  376. tgt->rev_chap_secret_off);
  377. break;
  378. default:
  379. break;
  380. }
  381. return str - buf;
  382. }
  383. static ssize_t ibft_attr_show_acpitbl(void *data, int type, char *buf)
  384. {
  385. struct ibft_kobject *entry = data;
  386. char *str = buf;
  387. switch (type) {
  388. case ISCSI_BOOT_ACPITBL_SIGNATURE:
  389. str += sprintf_string(str, ACPI_NAMESEG_SIZE,
  390. entry->header->header.signature);
  391. break;
  392. case ISCSI_BOOT_ACPITBL_OEM_ID:
  393. str += sprintf_string(str, ACPI_OEM_ID_SIZE,
  394. entry->header->header.oem_id);
  395. break;
  396. case ISCSI_BOOT_ACPITBL_OEM_TABLE_ID:
  397. str += sprintf_string(str, ACPI_OEM_TABLE_ID_SIZE,
  398. entry->header->header.oem_table_id);
  399. break;
  400. default:
  401. break;
  402. }
  403. return str - buf;
  404. }
  405. static int __init ibft_check_device(void)
  406. {
  407. int len;
  408. u8 *pos;
  409. u8 csum = 0;
  410. len = ibft_addr->header.length;
  411. /* Sanity checking of iBFT. */
  412. if (ibft_addr->header.revision != 1) {
  413. printk(KERN_ERR "iBFT module supports only revision 1, " \
  414. "while this is %d.\n",
  415. ibft_addr->header.revision);
  416. return -ENOENT;
  417. }
  418. for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
  419. csum += *pos;
  420. if (csum) {
  421. printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
  422. return -ENOENT;
  423. }
  424. return 0;
  425. }
  426. /*
  427. * Helper routiners to check to determine if the entry is valid
  428. * in the proper iBFT structure.
  429. */
  430. static umode_t ibft_check_nic_for(void *data, int type)
  431. {
  432. struct ibft_kobject *entry = data;
  433. struct ibft_nic *nic = entry->nic;
  434. umode_t rc = 0;
  435. switch (type) {
  436. case ISCSI_BOOT_ETH_INDEX:
  437. case ISCSI_BOOT_ETH_FLAGS:
  438. rc = S_IRUGO;
  439. break;
  440. case ISCSI_BOOT_ETH_IP_ADDR:
  441. if (address_not_null(nic->ip_addr))
  442. rc = S_IRUGO;
  443. break;
  444. case ISCSI_BOOT_ETH_PREFIX_LEN:
  445. case ISCSI_BOOT_ETH_SUBNET_MASK:
  446. if (nic->subnet_mask_prefix)
  447. rc = S_IRUGO;
  448. break;
  449. case ISCSI_BOOT_ETH_ORIGIN:
  450. rc = S_IRUGO;
  451. break;
  452. case ISCSI_BOOT_ETH_GATEWAY:
  453. if (address_not_null(nic->gateway))
  454. rc = S_IRUGO;
  455. break;
  456. case ISCSI_BOOT_ETH_PRIMARY_DNS:
  457. if (address_not_null(nic->primary_dns))
  458. rc = S_IRUGO;
  459. break;
  460. case ISCSI_BOOT_ETH_SECONDARY_DNS:
  461. if (address_not_null(nic->secondary_dns))
  462. rc = S_IRUGO;
  463. break;
  464. case ISCSI_BOOT_ETH_DHCP:
  465. if (address_not_null(nic->dhcp))
  466. rc = S_IRUGO;
  467. break;
  468. case ISCSI_BOOT_ETH_VLAN:
  469. case ISCSI_BOOT_ETH_MAC:
  470. rc = S_IRUGO;
  471. break;
  472. case ISCSI_BOOT_ETH_HOSTNAME:
  473. if (nic->hostname_off)
  474. rc = S_IRUGO;
  475. break;
  476. default:
  477. break;
  478. }
  479. return rc;
  480. }
  481. static umode_t __init ibft_check_tgt_for(void *data, int type)
  482. {
  483. struct ibft_kobject *entry = data;
  484. struct ibft_tgt *tgt = entry->tgt;
  485. umode_t rc = 0;
  486. switch (type) {
  487. case ISCSI_BOOT_TGT_INDEX:
  488. case ISCSI_BOOT_TGT_FLAGS:
  489. case ISCSI_BOOT_TGT_IP_ADDR:
  490. case ISCSI_BOOT_TGT_PORT:
  491. case ISCSI_BOOT_TGT_LUN:
  492. case ISCSI_BOOT_TGT_NIC_ASSOC:
  493. case ISCSI_BOOT_TGT_CHAP_TYPE:
  494. rc = S_IRUGO;
  495. break;
  496. case ISCSI_BOOT_TGT_NAME:
  497. if (tgt->tgt_name_len)
  498. rc = S_IRUGO;
  499. break;
  500. case ISCSI_BOOT_TGT_CHAP_NAME:
  501. case ISCSI_BOOT_TGT_CHAP_SECRET:
  502. if (tgt->chap_name_len)
  503. rc = S_IRUGO;
  504. break;
  505. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  506. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  507. if (tgt->rev_chap_name_len)
  508. rc = S_IRUGO;
  509. break;
  510. default:
  511. break;
  512. }
  513. return rc;
  514. }
  515. static umode_t __init ibft_check_initiator_for(void *data, int type)
  516. {
  517. struct ibft_kobject *entry = data;
  518. struct ibft_initiator *init = entry->initiator;
  519. umode_t rc = 0;
  520. switch (type) {
  521. case ISCSI_BOOT_INI_INDEX:
  522. case ISCSI_BOOT_INI_FLAGS:
  523. rc = S_IRUGO;
  524. break;
  525. case ISCSI_BOOT_INI_ISNS_SERVER:
  526. if (address_not_null(init->isns_server))
  527. rc = S_IRUGO;
  528. break;
  529. case ISCSI_BOOT_INI_SLP_SERVER:
  530. if (address_not_null(init->slp_server))
  531. rc = S_IRUGO;
  532. break;
  533. case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
  534. if (address_not_null(init->pri_radius_server))
  535. rc = S_IRUGO;
  536. break;
  537. case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
  538. if (address_not_null(init->sec_radius_server))
  539. rc = S_IRUGO;
  540. break;
  541. case ISCSI_BOOT_INI_INITIATOR_NAME:
  542. if (init->initiator_name_len)
  543. rc = S_IRUGO;
  544. break;
  545. default:
  546. break;
  547. }
  548. return rc;
  549. }
  550. static umode_t __init ibft_check_acpitbl_for(void *data, int type)
  551. {
  552. umode_t rc = 0;
  553. switch (type) {
  554. case ISCSI_BOOT_ACPITBL_SIGNATURE:
  555. case ISCSI_BOOT_ACPITBL_OEM_ID:
  556. case ISCSI_BOOT_ACPITBL_OEM_TABLE_ID:
  557. rc = S_IRUGO;
  558. break;
  559. default:
  560. break;
  561. }
  562. return rc;
  563. }
  564. static void ibft_kobj_release(void *data)
  565. {
  566. kfree(data);
  567. }
  568. /*
  569. * Helper function for ibft_register_kobjects.
  570. */
  571. static int __init ibft_create_kobject(struct acpi_table_ibft *header,
  572. struct ibft_hdr *hdr)
  573. {
  574. struct iscsi_boot_kobj *boot_kobj = NULL;
  575. struct ibft_kobject *ibft_kobj = NULL;
  576. struct ibft_nic *nic = (struct ibft_nic *)hdr;
  577. struct pci_dev *pci_dev;
  578. int rc = 0;
  579. ibft_kobj = kzalloc_obj(*ibft_kobj);
  580. if (!ibft_kobj)
  581. return -ENOMEM;
  582. ibft_kobj->header = header;
  583. ibft_kobj->hdr = hdr;
  584. switch (hdr->id) {
  585. case id_initiator:
  586. rc = ibft_verify_hdr("initiator", hdr, id_initiator,
  587. sizeof(*ibft_kobj->initiator));
  588. if (rc)
  589. break;
  590. boot_kobj = iscsi_boot_create_initiator(boot_kset, hdr->index,
  591. ibft_kobj,
  592. ibft_attr_show_initiator,
  593. ibft_check_initiator_for,
  594. ibft_kobj_release);
  595. if (!boot_kobj) {
  596. rc = -ENOMEM;
  597. goto free_ibft_obj;
  598. }
  599. break;
  600. case id_nic:
  601. rc = ibft_verify_hdr("ethernet", hdr, id_nic,
  602. sizeof(*ibft_kobj->nic));
  603. if (rc)
  604. break;
  605. boot_kobj = iscsi_boot_create_ethernet(boot_kset, hdr->index,
  606. ibft_kobj,
  607. ibft_attr_show_nic,
  608. ibft_check_nic_for,
  609. ibft_kobj_release);
  610. if (!boot_kobj) {
  611. rc = -ENOMEM;
  612. goto free_ibft_obj;
  613. }
  614. break;
  615. case id_target:
  616. rc = ibft_verify_hdr("target", hdr, id_target,
  617. sizeof(*ibft_kobj->tgt));
  618. if (rc)
  619. break;
  620. boot_kobj = iscsi_boot_create_target(boot_kset, hdr->index,
  621. ibft_kobj,
  622. ibft_attr_show_target,
  623. ibft_check_tgt_for,
  624. ibft_kobj_release);
  625. if (!boot_kobj) {
  626. rc = -ENOMEM;
  627. goto free_ibft_obj;
  628. }
  629. break;
  630. case id_reserved:
  631. case id_control:
  632. case id_extensions:
  633. /* Fields which we don't support. Ignore them */
  634. rc = 1;
  635. break;
  636. default:
  637. printk(KERN_ERR "iBFT has unknown structure type (%d). " \
  638. "Report this bug to %.6s!\n", hdr->id,
  639. header->header.oem_id);
  640. rc = 1;
  641. break;
  642. }
  643. if (rc) {
  644. /* Skip adding this kobject, but exit with non-fatal error. */
  645. rc = 0;
  646. goto free_ibft_obj;
  647. }
  648. if (hdr->id == id_nic) {
  649. /*
  650. * We don't search for the device in other domains than
  651. * zero. This is because on x86 platforms the BIOS
  652. * executes only devices which are in domain 0. Furthermore, the
  653. * iBFT spec doesn't have a domain id field :-(
  654. */
  655. pci_dev = pci_get_domain_bus_and_slot(0,
  656. (nic->pci_bdf & 0xff00) >> 8,
  657. (nic->pci_bdf & 0xff));
  658. if (pci_dev) {
  659. rc = sysfs_create_link(&boot_kobj->kobj,
  660. &pci_dev->dev.kobj, "device");
  661. pci_dev_put(pci_dev);
  662. }
  663. }
  664. return 0;
  665. free_ibft_obj:
  666. kfree(ibft_kobj);
  667. return rc;
  668. }
  669. /*
  670. * Scan the IBFT table structure for the NIC and Target fields. When
  671. * found add them on the passed-in list. We do not support the other
  672. * fields at this point, so they are skipped.
  673. */
  674. static int __init ibft_register_kobjects(struct acpi_table_ibft *header)
  675. {
  676. struct ibft_control *control = NULL;
  677. struct iscsi_boot_kobj *boot_kobj;
  678. struct ibft_kobject *ibft_kobj;
  679. void *ptr, *end;
  680. int rc = 0;
  681. u16 offset;
  682. u16 eot_offset;
  683. control = (void *)header + sizeof(*header);
  684. end = (void *)control + control->hdr.length;
  685. eot_offset = (void *)header + header->header.length - (void *)control;
  686. rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control, 0);
  687. /* iBFT table safety checking */
  688. rc |= ((control->hdr.index) ? -ENODEV : 0);
  689. rc |= ((control->hdr.length < sizeof(*control)) ? -ENODEV : 0);
  690. if (rc) {
  691. printk(KERN_ERR "iBFT error: Control header is invalid!\n");
  692. return rc;
  693. }
  694. for (ptr = &control->initiator_off; ptr + sizeof(u16) <= end; ptr += sizeof(u16)) {
  695. offset = *(u16 *)ptr;
  696. if (offset && offset < header->header.length &&
  697. offset < eot_offset) {
  698. rc = ibft_create_kobject(header,
  699. (void *)header + offset);
  700. if (rc)
  701. break;
  702. }
  703. }
  704. if (rc)
  705. return rc;
  706. ibft_kobj = kzalloc_obj(*ibft_kobj);
  707. if (!ibft_kobj)
  708. return -ENOMEM;
  709. ibft_kobj->header = header;
  710. ibft_kobj->hdr = NULL; /*for ibft_unregister*/
  711. boot_kobj = iscsi_boot_create_acpitbl(boot_kset, 0,
  712. ibft_kobj,
  713. ibft_attr_show_acpitbl,
  714. ibft_check_acpitbl_for,
  715. ibft_kobj_release);
  716. if (!boot_kobj) {
  717. kfree(ibft_kobj);
  718. rc = -ENOMEM;
  719. }
  720. return rc;
  721. }
  722. static void ibft_unregister(void)
  723. {
  724. struct iscsi_boot_kobj *boot_kobj, *tmp_kobj;
  725. struct ibft_kobject *ibft_kobj;
  726. list_for_each_entry_safe(boot_kobj, tmp_kobj,
  727. &boot_kset->kobj_list, list) {
  728. ibft_kobj = boot_kobj->data;
  729. if (ibft_kobj->hdr && ibft_kobj->hdr->id == id_nic)
  730. sysfs_remove_link(&boot_kobj->kobj, "device");
  731. };
  732. }
  733. static void ibft_cleanup(void)
  734. {
  735. if (boot_kset) {
  736. ibft_unregister();
  737. iscsi_boot_destroy_kset(boot_kset);
  738. }
  739. }
  740. static void __exit ibft_exit(void)
  741. {
  742. ibft_cleanup();
  743. }
  744. #ifdef CONFIG_ACPI
  745. static const struct {
  746. char *sign;
  747. } ibft_signs[] = {
  748. /*
  749. * One spec says "IBFT", the other says "iBFT". We have to check
  750. * for both.
  751. */
  752. { ACPI_SIG_IBFT },
  753. { "iBFT" },
  754. { "BIFT" }, /* Broadcom iSCSI Offload */
  755. };
  756. static void __init acpi_find_ibft_region(void)
  757. {
  758. int i;
  759. struct acpi_table_header *table = NULL;
  760. if (acpi_disabled)
  761. return;
  762. for (i = 0; i < ARRAY_SIZE(ibft_signs) && !ibft_addr; i++) {
  763. acpi_get_table(ibft_signs[i].sign, 0, &table);
  764. ibft_addr = (struct acpi_table_ibft *)table;
  765. }
  766. }
  767. #else
  768. static void __init acpi_find_ibft_region(void)
  769. {
  770. }
  771. #endif
  772. #ifdef CONFIG_ISCSI_IBFT_FIND
  773. static int __init acpi_find_isa_region(void)
  774. {
  775. if (ibft_phys_addr) {
  776. ibft_addr = isa_bus_to_virt(ibft_phys_addr);
  777. return 0;
  778. }
  779. return -ENODEV;
  780. }
  781. #else
  782. static int __init acpi_find_isa_region(void)
  783. {
  784. return -ENODEV;
  785. }
  786. #endif
  787. /*
  788. * ibft_init() - creates sysfs tree entries for the iBFT data.
  789. */
  790. static int __init ibft_init(void)
  791. {
  792. int rc = 0;
  793. /*
  794. As on UEFI systems the setup_arch()/reserve_ibft_region()
  795. is called before ACPI tables are parsed and it only does
  796. legacy finding.
  797. */
  798. if (acpi_find_isa_region())
  799. acpi_find_ibft_region();
  800. if (ibft_addr) {
  801. pr_info("iBFT detected.\n");
  802. rc = ibft_check_device();
  803. if (rc)
  804. return rc;
  805. boot_kset = iscsi_boot_create_kset("ibft");
  806. if (!boot_kset)
  807. return -ENOMEM;
  808. /* Scan the IBFT for data and register the kobjects. */
  809. rc = ibft_register_kobjects(ibft_addr);
  810. if (rc)
  811. goto out_free;
  812. } else
  813. printk(KERN_INFO "No iBFT detected.\n");
  814. return 0;
  815. out_free:
  816. ibft_cleanup();
  817. return rc;
  818. }
  819. module_init(ibft_init);
  820. module_exit(ibft_exit);