iscsi_ibft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * Copyright 2007 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. * 14 Mar 2008 - Konrad Rzeszutek <ketuzsezr@darnok.org>
  23. * Updated comments and copyrights. (v0.4.9)
  24. *
  25. * 11 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  26. * Converted to using ibft_addr. (v0.4.8)
  27. *
  28. * 8 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  29. * Combined two functions in one: reserve_ibft_region. (v0.4.7)
  30. *
  31. * 30 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  32. * Added logic to handle IPv6 addresses. (v0.4.6)
  33. *
  34. * 25 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  35. * Added logic to handle badly not-to-spec iBFT. (v0.4.5)
  36. *
  37. * 4 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  38. * Added __init to function declarations. (v0.4.4)
  39. *
  40. * 21 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  41. * Updated kobject registration, combined unregister functions in one
  42. * and code and style cleanup. (v0.4.3)
  43. *
  44. * 5 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  45. * Added end-markers to enums and re-organized kobject registration. (v0.4.2)
  46. *
  47. * 4 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  48. * Created 'device' sysfs link to the NIC and style cleanup. (v0.4.1)
  49. *
  50. * 28 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  51. * Added sysfs-ibft documentation, moved 'find_ibft' function to
  52. * in its own file and added text attributes for every struct field. (v0.4)
  53. *
  54. * 21 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  55. * Added text attributes emulating OpenFirmware /proc/device-tree naming.
  56. * Removed binary /sysfs interface (v0.3)
  57. *
  58. * 29 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  59. * Added functionality in setup.c to reserve iBFT region. (v0.2)
  60. *
  61. * 27 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  62. * First version exposing iBFT data via a binary /sysfs. (v0.1)
  63. *
  64. */
  65. #include <linux/blkdev.h>
  66. #include <linux/capability.h>
  67. #include <linux/ctype.h>
  68. #include <linux/device.h>
  69. #include <linux/err.h>
  70. #include <linux/init.h>
  71. #include <linux/iscsi_ibft.h>
  72. #include <linux/limits.h>
  73. #include <linux/module.h>
  74. #include <linux/pci.h>
  75. #include <linux/slab.h>
  76. #include <linux/stat.h>
  77. #include <linux/string.h>
  78. #include <linux/types.h>
  79. #define IBFT_ISCSI_VERSION "0.4.9"
  80. #define IBFT_ISCSI_DATE "2008-Mar-14"
  81. MODULE_AUTHOR("Peter Jones <pjones@redhat.com> and \
  82. Konrad Rzeszutek <ketuzsezr@darnok.org>");
  83. MODULE_DESCRIPTION("sysfs interface to BIOS iBFT information");
  84. MODULE_LICENSE("GPL");
  85. MODULE_VERSION(IBFT_ISCSI_VERSION);
  86. struct ibft_hdr {
  87. u8 id;
  88. u8 version;
  89. u16 length;
  90. u8 index;
  91. u8 flags;
  92. } __attribute__((__packed__));
  93. struct ibft_control {
  94. struct ibft_hdr hdr;
  95. u16 extensions;
  96. u16 initiator_off;
  97. u16 nic0_off;
  98. u16 tgt0_off;
  99. u16 nic1_off;
  100. u16 tgt1_off;
  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. * We do not support the other types, hence the usage of NULL.
  159. * This maps to the enum ibft_id.
  160. */
  161. static const char *ibft_id_names[] =
  162. {NULL, NULL, "initiator", "ethernet%d", "target%d", NULL, NULL};
  163. /*
  164. * The text attributes names for each of the kobjects.
  165. */
  166. enum ibft_eth_properties_enum {
  167. ibft_eth_index,
  168. ibft_eth_flags,
  169. ibft_eth_ip_addr,
  170. ibft_eth_subnet_mask,
  171. ibft_eth_origin,
  172. ibft_eth_gateway,
  173. ibft_eth_primary_dns,
  174. ibft_eth_secondary_dns,
  175. ibft_eth_dhcp,
  176. ibft_eth_vlan,
  177. ibft_eth_mac,
  178. /* ibft_eth_pci_bdf - this is replaced by link to the device itself. */
  179. ibft_eth_hostname,
  180. ibft_eth_end_marker,
  181. };
  182. static const char *ibft_eth_properties[] =
  183. {"index", "flags", "ip-addr", "subnet-mask", "origin", "gateway",
  184. "primary-dns", "secondary-dns", "dhcp", "vlan", "mac", "hostname",
  185. NULL};
  186. enum ibft_tgt_properties_enum {
  187. ibft_tgt_index,
  188. ibft_tgt_flags,
  189. ibft_tgt_ip_addr,
  190. ibft_tgt_port,
  191. ibft_tgt_lun,
  192. ibft_tgt_chap_type,
  193. ibft_tgt_nic_assoc,
  194. ibft_tgt_name,
  195. ibft_tgt_chap_name,
  196. ibft_tgt_chap_secret,
  197. ibft_tgt_rev_chap_name,
  198. ibft_tgt_rev_chap_secret,
  199. ibft_tgt_end_marker,
  200. };
  201. static const char *ibft_tgt_properties[] =
  202. {"index", "flags", "ip-addr", "port", "lun", "chap-type", "nic-assoc",
  203. "target-name", "chap-name", "chap-secret", "rev-chap-name",
  204. "rev-chap-name-secret", NULL};
  205. enum ibft_initiator_properties_enum {
  206. ibft_init_index,
  207. ibft_init_flags,
  208. ibft_init_isns_server,
  209. ibft_init_slp_server,
  210. ibft_init_pri_radius_server,
  211. ibft_init_sec_radius_server,
  212. ibft_init_initiator_name,
  213. ibft_init_end_marker,
  214. };
  215. static const char *ibft_initiator_properties[] =
  216. {"index", "flags", "isns-server", "slp-server", "pri-radius-server",
  217. "sec-radius-server", "initiator-name", NULL};
  218. /*
  219. * The kobject and attribute structures.
  220. */
  221. struct ibft_kobject {
  222. struct ibft_table_header *header;
  223. union {
  224. struct ibft_initiator *initiator;
  225. struct ibft_nic *nic;
  226. struct ibft_tgt *tgt;
  227. struct ibft_hdr *hdr;
  228. };
  229. struct kobject kobj;
  230. struct list_head node;
  231. };
  232. struct ibft_attribute {
  233. struct attribute attr;
  234. ssize_t (*show) (struct ibft_kobject *entry,
  235. struct ibft_attribute *attr, char *buf);
  236. union {
  237. struct ibft_initiator *initiator;
  238. struct ibft_nic *nic;
  239. struct ibft_tgt *tgt;
  240. struct ibft_hdr *hdr;
  241. };
  242. struct kobject *kobj;
  243. int type; /* The enum of the type. This can be any value of:
  244. ibft_eth_properties_enum, ibft_tgt_properties_enum,
  245. or ibft_initiator_properties_enum. */
  246. struct list_head node;
  247. };
  248. static LIST_HEAD(ibft_attr_list);
  249. static LIST_HEAD(ibft_kobject_list);
  250. static const char nulls[16];
  251. /*
  252. * Helper functions to parse data properly.
  253. */
  254. static ssize_t sprintf_ipaddr(char *buf, u8 *ip)
  255. {
  256. char *str = buf;
  257. if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0 &&
  258. ip[4] == 0 && ip[5] == 0 && ip[6] == 0 && ip[7] == 0 &&
  259. ip[8] == 0 && ip[9] == 0 && ip[10] == 0xff && ip[11] == 0xff) {
  260. /*
  261. * IPV4
  262. */
  263. str += sprintf(buf, "%pI4", ip + 12);
  264. } else {
  265. /*
  266. * IPv6
  267. */
  268. str += sprintf(str, "%pI6", ip);
  269. }
  270. str += sprintf(str, "\n");
  271. return str - buf;
  272. }
  273. static ssize_t sprintf_string(char *str, int len, char *buf)
  274. {
  275. return sprintf(str, "%.*s\n", len, buf);
  276. }
  277. /*
  278. * Helper function to verify the IBFT header.
  279. */
  280. static int ibft_verify_hdr(char *t, struct ibft_hdr *hdr, int id, int length)
  281. {
  282. if (hdr->id != id) {
  283. printk(KERN_ERR "iBFT error: We expected the " \
  284. "field header.id to have %d but " \
  285. "found %d instead!\n", id, hdr->id);
  286. return -ENODEV;
  287. }
  288. if (hdr->length != length) {
  289. printk(KERN_ERR "iBFT error: We expected the " \
  290. "field header.length to have %d but " \
  291. "found %d instead!\n", length, hdr->length);
  292. return -ENODEV;
  293. }
  294. return 0;
  295. }
  296. static void ibft_release(struct kobject *kobj)
  297. {
  298. struct ibft_kobject *ibft =
  299. container_of(kobj, struct ibft_kobject, kobj);
  300. kfree(ibft);
  301. }
  302. /*
  303. * Routines for parsing the iBFT data to be human readable.
  304. */
  305. static ssize_t ibft_attr_show_initiator(struct ibft_kobject *entry,
  306. struct ibft_attribute *attr,
  307. char *buf)
  308. {
  309. struct ibft_initiator *initiator = entry->initiator;
  310. void *ibft_loc = entry->header;
  311. char *str = buf;
  312. if (!initiator)
  313. return 0;
  314. switch (attr->type) {
  315. case ibft_init_index:
  316. str += sprintf(str, "%d\n", initiator->hdr.index);
  317. break;
  318. case ibft_init_flags:
  319. str += sprintf(str, "%d\n", initiator->hdr.flags);
  320. break;
  321. case ibft_init_isns_server:
  322. str += sprintf_ipaddr(str, initiator->isns_server);
  323. break;
  324. case ibft_init_slp_server:
  325. str += sprintf_ipaddr(str, initiator->slp_server);
  326. break;
  327. case ibft_init_pri_radius_server:
  328. str += sprintf_ipaddr(str, initiator->pri_radius_server);
  329. break;
  330. case ibft_init_sec_radius_server:
  331. str += sprintf_ipaddr(str, initiator->sec_radius_server);
  332. break;
  333. case ibft_init_initiator_name:
  334. str += sprintf_string(str, initiator->initiator_name_len,
  335. (char *)ibft_loc +
  336. initiator->initiator_name_off);
  337. break;
  338. default:
  339. break;
  340. }
  341. return str - buf;
  342. }
  343. static ssize_t ibft_attr_show_nic(struct ibft_kobject *entry,
  344. struct ibft_attribute *attr,
  345. char *buf)
  346. {
  347. struct ibft_nic *nic = entry->nic;
  348. void *ibft_loc = entry->header;
  349. char *str = buf;
  350. int val;
  351. if (!nic)
  352. return 0;
  353. switch (attr->type) {
  354. case ibft_eth_index:
  355. str += sprintf(str, "%d\n", nic->hdr.index);
  356. break;
  357. case ibft_eth_flags:
  358. str += sprintf(str, "%d\n", nic->hdr.flags);
  359. break;
  360. case ibft_eth_ip_addr:
  361. str += sprintf_ipaddr(str, nic->ip_addr);
  362. break;
  363. case ibft_eth_subnet_mask:
  364. val = ~((1 << (32-nic->subnet_mask_prefix))-1);
  365. str += sprintf(str, NIPQUAD_FMT,
  366. (u8)(val >> 24), (u8)(val >> 16),
  367. (u8)(val >> 8), (u8)(val));
  368. break;
  369. case ibft_eth_origin:
  370. str += sprintf(str, "%d\n", nic->origin);
  371. break;
  372. case ibft_eth_gateway:
  373. str += sprintf_ipaddr(str, nic->gateway);
  374. break;
  375. case ibft_eth_primary_dns:
  376. str += sprintf_ipaddr(str, nic->primary_dns);
  377. break;
  378. case ibft_eth_secondary_dns:
  379. str += sprintf_ipaddr(str, nic->secondary_dns);
  380. break;
  381. case ibft_eth_dhcp:
  382. str += sprintf_ipaddr(str, nic->dhcp);
  383. break;
  384. case ibft_eth_vlan:
  385. str += sprintf(str, "%d\n", nic->vlan);
  386. break;
  387. case ibft_eth_mac:
  388. str += sprintf(str, "%pM\n", nic->mac);
  389. break;
  390. case ibft_eth_hostname:
  391. str += sprintf_string(str, nic->hostname_len,
  392. (char *)ibft_loc + nic->hostname_off);
  393. break;
  394. default:
  395. break;
  396. }
  397. return str - buf;
  398. };
  399. static ssize_t ibft_attr_show_target(struct ibft_kobject *entry,
  400. struct ibft_attribute *attr,
  401. char *buf)
  402. {
  403. struct ibft_tgt *tgt = entry->tgt;
  404. void *ibft_loc = entry->header;
  405. char *str = buf;
  406. int i;
  407. if (!tgt)
  408. return 0;
  409. switch (attr->type) {
  410. case ibft_tgt_index:
  411. str += sprintf(str, "%d\n", tgt->hdr.index);
  412. break;
  413. case ibft_tgt_flags:
  414. str += sprintf(str, "%d\n", tgt->hdr.flags);
  415. break;
  416. case ibft_tgt_ip_addr:
  417. str += sprintf_ipaddr(str, tgt->ip_addr);
  418. break;
  419. case ibft_tgt_port:
  420. str += sprintf(str, "%d\n", tgt->port);
  421. break;
  422. case ibft_tgt_lun:
  423. for (i = 0; i < 8; i++)
  424. str += sprintf(str, "%x", (u8)tgt->lun[i]);
  425. str += sprintf(str, "\n");
  426. break;
  427. case ibft_tgt_nic_assoc:
  428. str += sprintf(str, "%d\n", tgt->nic_assoc);
  429. break;
  430. case ibft_tgt_chap_type:
  431. str += sprintf(str, "%d\n", tgt->chap_type);
  432. break;
  433. case ibft_tgt_name:
  434. str += sprintf_string(str, tgt->tgt_name_len,
  435. (char *)ibft_loc + tgt->tgt_name_off);
  436. break;
  437. case ibft_tgt_chap_name:
  438. str += sprintf_string(str, tgt->chap_name_len,
  439. (char *)ibft_loc + tgt->chap_name_off);
  440. break;
  441. case ibft_tgt_chap_secret:
  442. str += sprintf_string(str, tgt->chap_secret_len,
  443. (char *)ibft_loc + tgt->chap_secret_off);
  444. break;
  445. case ibft_tgt_rev_chap_name:
  446. str += sprintf_string(str, tgt->rev_chap_name_len,
  447. (char *)ibft_loc +
  448. tgt->rev_chap_name_off);
  449. break;
  450. case ibft_tgt_rev_chap_secret:
  451. str += sprintf_string(str, tgt->rev_chap_secret_len,
  452. (char *)ibft_loc +
  453. tgt->rev_chap_secret_off);
  454. break;
  455. default:
  456. break;
  457. }
  458. return str - buf;
  459. }
  460. /*
  461. * The routine called for all sysfs attributes.
  462. */
  463. static ssize_t ibft_show_attribute(struct kobject *kobj,
  464. struct attribute *attr,
  465. char *buf)
  466. {
  467. struct ibft_kobject *dev =
  468. container_of(kobj, struct ibft_kobject, kobj);
  469. struct ibft_attribute *ibft_attr =
  470. container_of(attr, struct ibft_attribute, attr);
  471. ssize_t ret = -EIO;
  472. char *str = buf;
  473. if (!capable(CAP_SYS_ADMIN))
  474. return -EACCES;
  475. if (ibft_attr->show)
  476. ret = ibft_attr->show(dev, ibft_attr, str);
  477. return ret;
  478. }
  479. static struct sysfs_ops ibft_attr_ops = {
  480. .show = ibft_show_attribute,
  481. };
  482. static struct kobj_type ibft_ktype = {
  483. .release = ibft_release,
  484. .sysfs_ops = &ibft_attr_ops,
  485. };
  486. static struct kset *ibft_kset;
  487. static int __init ibft_check_device(void)
  488. {
  489. int len;
  490. u8 *pos;
  491. u8 csum = 0;
  492. len = ibft_addr->length;
  493. /* Sanity checking of iBFT. */
  494. if (ibft_addr->revision != 1) {
  495. printk(KERN_ERR "iBFT module supports only revision 1, " \
  496. "while this is %d.\n", ibft_addr->revision);
  497. return -ENOENT;
  498. }
  499. for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
  500. csum += *pos;
  501. if (csum) {
  502. printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
  503. return -ENOENT;
  504. }
  505. return 0;
  506. }
  507. /*
  508. * Helper function for ibft_register_kobjects.
  509. */
  510. static int __init ibft_create_kobject(struct ibft_table_header *header,
  511. struct ibft_hdr *hdr,
  512. struct list_head *list)
  513. {
  514. struct ibft_kobject *ibft_kobj = NULL;
  515. struct ibft_nic *nic = (struct ibft_nic *)hdr;
  516. struct pci_dev *pci_dev;
  517. int rc = 0;
  518. ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
  519. if (!ibft_kobj)
  520. return -ENOMEM;
  521. ibft_kobj->header = header;
  522. ibft_kobj->hdr = hdr;
  523. switch (hdr->id) {
  524. case id_initiator:
  525. rc = ibft_verify_hdr("initiator", hdr, id_initiator,
  526. sizeof(*ibft_kobj->initiator));
  527. break;
  528. case id_nic:
  529. rc = ibft_verify_hdr("ethernet", hdr, id_nic,
  530. sizeof(*ibft_kobj->nic));
  531. break;
  532. case id_target:
  533. rc = ibft_verify_hdr("target", hdr, id_target,
  534. sizeof(*ibft_kobj->tgt));
  535. break;
  536. case id_reserved:
  537. case id_control:
  538. case id_extensions:
  539. /* Fields which we don't support. Ignore them */
  540. rc = 1;
  541. break;
  542. default:
  543. printk(KERN_ERR "iBFT has unknown structure type (%d). " \
  544. "Report this bug to %.6s!\n", hdr->id,
  545. header->oem_id);
  546. rc = 1;
  547. break;
  548. }
  549. if (rc) {
  550. /* Skip adding this kobject, but exit with non-fatal error. */
  551. kfree(ibft_kobj);
  552. goto out_invalid_struct;
  553. }
  554. ibft_kobj->kobj.kset = ibft_kset;
  555. rc = kobject_init_and_add(&ibft_kobj->kobj, &ibft_ktype,
  556. NULL, ibft_id_names[hdr->id], hdr->index);
  557. if (rc) {
  558. kfree(ibft_kobj);
  559. goto out;
  560. }
  561. kobject_uevent(&ibft_kobj->kobj, KOBJ_ADD);
  562. if (hdr->id == id_nic) {
  563. /*
  564. * We don't search for the device in other domains than
  565. * zero. This is because on x86 platforms the BIOS
  566. * executes only devices which are in domain 0. Furthermore, the
  567. * iBFT spec doesn't have a domain id field :-(
  568. */
  569. pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
  570. (nic->pci_bdf & 0xff));
  571. if (pci_dev) {
  572. rc = sysfs_create_link(&ibft_kobj->kobj,
  573. &pci_dev->dev.kobj, "device");
  574. pci_dev_put(pci_dev);
  575. }
  576. }
  577. /* Nothing broke so lets add it to the list. */
  578. list_add_tail(&ibft_kobj->node, list);
  579. out:
  580. return rc;
  581. out_invalid_struct:
  582. /* Unsupported structs are skipped. */
  583. return 0;
  584. }
  585. /*
  586. * Scan the IBFT table structure for the NIC and Target fields. When
  587. * found add them on the passed-in list. We do not support the other
  588. * fields at this point, so they are skipped.
  589. */
  590. static int __init ibft_register_kobjects(struct ibft_table_header *header,
  591. struct list_head *list)
  592. {
  593. struct ibft_control *control = NULL;
  594. void *ptr, *end;
  595. int rc = 0;
  596. u16 offset;
  597. u16 eot_offset;
  598. control = (void *)header + sizeof(*header);
  599. end = (void *)control + control->hdr.length;
  600. eot_offset = (void *)header + header->length - (void *)control;
  601. rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
  602. sizeof(*control));
  603. /* iBFT table safety checking */
  604. rc |= ((control->hdr.index) ? -ENODEV : 0);
  605. if (rc) {
  606. printk(KERN_ERR "iBFT error: Control header is invalid!\n");
  607. return rc;
  608. }
  609. for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
  610. offset = *(u16 *)ptr;
  611. if (offset && offset < header->length && offset < eot_offset) {
  612. rc = ibft_create_kobject(header,
  613. (void *)header + offset,
  614. list);
  615. if (rc)
  616. break;
  617. }
  618. }
  619. return rc;
  620. }
  621. static void ibft_unregister(struct list_head *attr_list,
  622. struct list_head *kobj_list)
  623. {
  624. struct ibft_kobject *data = NULL, *n;
  625. struct ibft_attribute *attr = NULL, *m;
  626. list_for_each_entry_safe(attr, m, attr_list, node) {
  627. sysfs_remove_file(attr->kobj, &attr->attr);
  628. list_del(&attr->node);
  629. kfree(attr);
  630. };
  631. list_del_init(attr_list);
  632. list_for_each_entry_safe(data, n, kobj_list, node) {
  633. list_del(&data->node);
  634. if (data->hdr->id == id_nic)
  635. sysfs_remove_link(&data->kobj, "device");
  636. kobject_put(&data->kobj);
  637. };
  638. list_del_init(kobj_list);
  639. }
  640. static int __init ibft_create_attribute(struct ibft_kobject *kobj_data,
  641. int type,
  642. const char *name,
  643. ssize_t (*show)(struct ibft_kobject *,
  644. struct ibft_attribute*,
  645. char *buf),
  646. struct list_head *list)
  647. {
  648. struct ibft_attribute *attr = NULL;
  649. struct ibft_hdr *hdr = kobj_data->hdr;
  650. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  651. if (!attr)
  652. return -ENOMEM;
  653. attr->attr.name = name;
  654. attr->attr.mode = S_IRUSR;
  655. attr->hdr = hdr;
  656. attr->show = show;
  657. attr->kobj = &kobj_data->kobj;
  658. attr->type = type;
  659. list_add_tail(&attr->node, list);
  660. return 0;
  661. }
  662. /*
  663. * Helper routiners to check to determine if the entry is valid
  664. * in the proper iBFT structure.
  665. */
  666. static int __init ibft_check_nic_for(struct ibft_nic *nic, int entry)
  667. {
  668. int rc = 0;
  669. switch (entry) {
  670. case ibft_eth_index:
  671. case ibft_eth_flags:
  672. rc = 1;
  673. break;
  674. case ibft_eth_ip_addr:
  675. if (memcmp(nic->ip_addr, nulls, sizeof(nic->ip_addr)))
  676. rc = 1;
  677. break;
  678. case ibft_eth_subnet_mask:
  679. if (nic->subnet_mask_prefix)
  680. rc = 1;
  681. break;
  682. case ibft_eth_origin:
  683. rc = 1;
  684. break;
  685. case ibft_eth_gateway:
  686. if (memcmp(nic->gateway, nulls, sizeof(nic->gateway)))
  687. rc = 1;
  688. break;
  689. case ibft_eth_primary_dns:
  690. if (memcmp(nic->primary_dns, nulls,
  691. sizeof(nic->primary_dns)))
  692. rc = 1;
  693. break;
  694. case ibft_eth_secondary_dns:
  695. if (memcmp(nic->secondary_dns, nulls,
  696. sizeof(nic->secondary_dns)))
  697. rc = 1;
  698. break;
  699. case ibft_eth_dhcp:
  700. if (memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  701. rc = 1;
  702. break;
  703. case ibft_eth_vlan:
  704. case ibft_eth_mac:
  705. rc = 1;
  706. break;
  707. case ibft_eth_hostname:
  708. if (nic->hostname_off)
  709. rc = 1;
  710. break;
  711. default:
  712. break;
  713. }
  714. return rc;
  715. }
  716. static int __init ibft_check_tgt_for(struct ibft_tgt *tgt, int entry)
  717. {
  718. int rc = 0;
  719. switch (entry) {
  720. case ibft_tgt_index:
  721. case ibft_tgt_flags:
  722. case ibft_tgt_ip_addr:
  723. case ibft_tgt_port:
  724. case ibft_tgt_lun:
  725. case ibft_tgt_nic_assoc:
  726. case ibft_tgt_chap_type:
  727. rc = 1;
  728. case ibft_tgt_name:
  729. if (tgt->tgt_name_len)
  730. rc = 1;
  731. break;
  732. case ibft_tgt_chap_name:
  733. case ibft_tgt_chap_secret:
  734. if (tgt->chap_name_len)
  735. rc = 1;
  736. break;
  737. case ibft_tgt_rev_chap_name:
  738. case ibft_tgt_rev_chap_secret:
  739. if (tgt->rev_chap_name_len)
  740. rc = 1;
  741. break;
  742. default:
  743. break;
  744. }
  745. return rc;
  746. }
  747. static int __init ibft_check_initiator_for(struct ibft_initiator *init,
  748. int entry)
  749. {
  750. int rc = 0;
  751. switch (entry) {
  752. case ibft_init_index:
  753. case ibft_init_flags:
  754. rc = 1;
  755. break;
  756. case ibft_init_isns_server:
  757. if (memcmp(init->isns_server, nulls,
  758. sizeof(init->isns_server)))
  759. rc = 1;
  760. break;
  761. case ibft_init_slp_server:
  762. if (memcmp(init->slp_server, nulls,
  763. sizeof(init->slp_server)))
  764. rc = 1;
  765. break;
  766. case ibft_init_pri_radius_server:
  767. if (memcmp(init->pri_radius_server, nulls,
  768. sizeof(init->pri_radius_server)))
  769. rc = 1;
  770. break;
  771. case ibft_init_sec_radius_server:
  772. if (memcmp(init->sec_radius_server, nulls,
  773. sizeof(init->sec_radius_server)))
  774. rc = 1;
  775. break;
  776. case ibft_init_initiator_name:
  777. if (init->initiator_name_len)
  778. rc = 1;
  779. break;
  780. default:
  781. break;
  782. }
  783. return rc;
  784. }
  785. /*
  786. * Register the attributes for all of the kobjects.
  787. */
  788. static int __init ibft_register_attributes(struct list_head *kobject_list,
  789. struct list_head *attr_list)
  790. {
  791. int rc = 0, i = 0;
  792. struct ibft_kobject *data = NULL;
  793. struct ibft_attribute *attr = NULL, *m;
  794. list_for_each_entry(data, kobject_list, node) {
  795. switch (data->hdr->id) {
  796. case id_nic:
  797. for (i = 0; i < ibft_eth_end_marker && !rc; i++)
  798. if (ibft_check_nic_for(data->nic, i))
  799. rc = ibft_create_attribute(data, i,
  800. ibft_eth_properties[i],
  801. ibft_attr_show_nic, attr_list);
  802. break;
  803. case id_target:
  804. for (i = 0; i < ibft_tgt_end_marker && !rc; i++)
  805. if (ibft_check_tgt_for(data->tgt, i))
  806. rc = ibft_create_attribute(data, i,
  807. ibft_tgt_properties[i],
  808. ibft_attr_show_target,
  809. attr_list);
  810. break;
  811. case id_initiator:
  812. for (i = 0; i < ibft_init_end_marker && !rc; i++)
  813. if (ibft_check_initiator_for(
  814. data->initiator, i))
  815. rc = ibft_create_attribute(data, i,
  816. ibft_initiator_properties[i],
  817. ibft_attr_show_initiator,
  818. attr_list);
  819. break;
  820. default:
  821. break;
  822. }
  823. if (rc)
  824. break;
  825. }
  826. list_for_each_entry_safe(attr, m, attr_list, node) {
  827. rc = sysfs_create_file(attr->kobj, &attr->attr);
  828. if (rc) {
  829. list_del(&attr->node);
  830. kfree(attr);
  831. break;
  832. }
  833. }
  834. return rc;
  835. }
  836. /*
  837. * ibft_init() - creates sysfs tree entries for the iBFT data.
  838. */
  839. static int __init ibft_init(void)
  840. {
  841. int rc = 0;
  842. ibft_kset = kset_create_and_add("ibft", NULL, firmware_kobj);
  843. if (!ibft_kset)
  844. return -ENOMEM;
  845. if (ibft_addr) {
  846. printk(KERN_INFO "iBFT detected at 0x%llx.\n",
  847. (u64)isa_virt_to_bus(ibft_addr));
  848. rc = ibft_check_device();
  849. if (rc)
  850. goto out_firmware_unregister;
  851. /* Scan the IBFT for data and register the kobjects. */
  852. rc = ibft_register_kobjects(ibft_addr, &ibft_kobject_list);
  853. if (rc)
  854. goto out_free;
  855. /* Register the attributes */
  856. rc = ibft_register_attributes(&ibft_kobject_list,
  857. &ibft_attr_list);
  858. if (rc)
  859. goto out_free;
  860. } else
  861. printk(KERN_INFO "No iBFT detected.\n");
  862. return 0;
  863. out_free:
  864. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  865. out_firmware_unregister:
  866. kset_unregister(ibft_kset);
  867. return rc;
  868. }
  869. static void __exit ibft_exit(void)
  870. {
  871. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  872. kset_unregister(ibft_kset);
  873. }
  874. module_init(ibft_init);
  875. module_exit(ibft_exit);