iscsi_ibft.c 21 KB

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