iscsi_ibft.c 21 KB

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