iscsi_ibft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. char *mac;
  351. int val;
  352. if (!nic)
  353. return 0;
  354. switch (attr->type) {
  355. case ibft_eth_index:
  356. str += sprintf(str, "%d\n", nic->hdr.index);
  357. break;
  358. case ibft_eth_flags:
  359. str += sprintf(str, "%d\n", nic->hdr.flags);
  360. break;
  361. case ibft_eth_ip_addr:
  362. str += sprintf_ipaddr(str, nic->ip_addr);
  363. break;
  364. case ibft_eth_subnet_mask:
  365. val = ~((1 << (32-nic->subnet_mask_prefix))-1);
  366. str += sprintf(str, NIPQUAD_FMT,
  367. (u8)(val >> 24), (u8)(val >> 16),
  368. (u8)(val >> 8), (u8)(val));
  369. break;
  370. case ibft_eth_origin:
  371. str += sprintf(str, "%d\n", nic->origin);
  372. break;
  373. case ibft_eth_gateway:
  374. str += sprintf_ipaddr(str, nic->gateway);
  375. break;
  376. case ibft_eth_primary_dns:
  377. str += sprintf_ipaddr(str, nic->primary_dns);
  378. break;
  379. case ibft_eth_secondary_dns:
  380. str += sprintf_ipaddr(str, nic->secondary_dns);
  381. break;
  382. case ibft_eth_dhcp:
  383. str += sprintf_ipaddr(str, nic->dhcp);
  384. break;
  385. case ibft_eth_vlan:
  386. str += sprintf(str, "%d\n", nic->vlan);
  387. break;
  388. case ibft_eth_mac:
  389. mac = nic->mac;
  390. str += sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  391. (u8)mac[0], (u8)mac[1], (u8)mac[2],
  392. (u8)mac[3], (u8)mac[4], (u8)mac[5]);
  393. break;
  394. case ibft_eth_hostname:
  395. str += sprintf_string(str, nic->hostname_len,
  396. (char *)ibft_loc + nic->hostname_off);
  397. break;
  398. default:
  399. break;
  400. }
  401. return str - buf;
  402. };
  403. static ssize_t ibft_attr_show_target(struct ibft_kobject *entry,
  404. struct ibft_attribute *attr,
  405. char *buf)
  406. {
  407. struct ibft_tgt *tgt = entry->tgt;
  408. void *ibft_loc = entry->header;
  409. char *str = buf;
  410. int i;
  411. if (!tgt)
  412. return 0;
  413. switch (attr->type) {
  414. case ibft_tgt_index:
  415. str += sprintf(str, "%d\n", tgt->hdr.index);
  416. break;
  417. case ibft_tgt_flags:
  418. str += sprintf(str, "%d\n", tgt->hdr.flags);
  419. break;
  420. case ibft_tgt_ip_addr:
  421. str += sprintf_ipaddr(str, tgt->ip_addr);
  422. break;
  423. case ibft_tgt_port:
  424. str += sprintf(str, "%d\n", tgt->port);
  425. break;
  426. case ibft_tgt_lun:
  427. for (i = 0; i < 8; i++)
  428. str += sprintf(str, "%x", (u8)tgt->lun[i]);
  429. str += sprintf(str, "\n");
  430. break;
  431. case ibft_tgt_nic_assoc:
  432. str += sprintf(str, "%d\n", tgt->nic_assoc);
  433. break;
  434. case ibft_tgt_chap_type:
  435. str += sprintf(str, "%d\n", tgt->chap_type);
  436. break;
  437. case ibft_tgt_name:
  438. str += sprintf_string(str, tgt->tgt_name_len,
  439. (char *)ibft_loc + tgt->tgt_name_off);
  440. break;
  441. case ibft_tgt_chap_name:
  442. str += sprintf_string(str, tgt->chap_name_len,
  443. (char *)ibft_loc + tgt->chap_name_off);
  444. break;
  445. case ibft_tgt_chap_secret:
  446. str += sprintf_string(str, tgt->chap_secret_len,
  447. (char *)ibft_loc + tgt->chap_secret_off);
  448. break;
  449. case ibft_tgt_rev_chap_name:
  450. str += sprintf_string(str, tgt->rev_chap_name_len,
  451. (char *)ibft_loc +
  452. tgt->rev_chap_name_off);
  453. break;
  454. case ibft_tgt_rev_chap_secret:
  455. str += sprintf_string(str, tgt->rev_chap_secret_len,
  456. (char *)ibft_loc +
  457. tgt->rev_chap_secret_off);
  458. break;
  459. default:
  460. break;
  461. }
  462. return str - buf;
  463. }
  464. /*
  465. * The routine called for all sysfs attributes.
  466. */
  467. static ssize_t ibft_show_attribute(struct kobject *kobj,
  468. struct attribute *attr,
  469. char *buf)
  470. {
  471. struct ibft_kobject *dev =
  472. container_of(kobj, struct ibft_kobject, kobj);
  473. struct ibft_attribute *ibft_attr =
  474. container_of(attr, struct ibft_attribute, attr);
  475. ssize_t ret = -EIO;
  476. char *str = buf;
  477. if (!capable(CAP_SYS_ADMIN))
  478. return -EACCES;
  479. if (ibft_attr->show)
  480. ret = ibft_attr->show(dev, ibft_attr, str);
  481. return ret;
  482. }
  483. static struct sysfs_ops ibft_attr_ops = {
  484. .show = ibft_show_attribute,
  485. };
  486. static struct kobj_type ibft_ktype = {
  487. .release = ibft_release,
  488. .sysfs_ops = &ibft_attr_ops,
  489. };
  490. static struct kset *ibft_kset;
  491. static int __init ibft_check_device(void)
  492. {
  493. int len;
  494. u8 *pos;
  495. u8 csum = 0;
  496. len = ibft_addr->length;
  497. /* Sanity checking of iBFT. */
  498. if (ibft_addr->revision != 1) {
  499. printk(KERN_ERR "iBFT module supports only revision 1, " \
  500. "while this is %d.\n", ibft_addr->revision);
  501. return -ENOENT;
  502. }
  503. for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
  504. csum += *pos;
  505. if (csum) {
  506. printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
  507. return -ENOENT;
  508. }
  509. return 0;
  510. }
  511. /*
  512. * Helper function for ibft_register_kobjects.
  513. */
  514. static int __init ibft_create_kobject(struct ibft_table_header *header,
  515. struct ibft_hdr *hdr,
  516. struct list_head *list)
  517. {
  518. struct ibft_kobject *ibft_kobj = NULL;
  519. struct ibft_nic *nic = (struct ibft_nic *)hdr;
  520. struct pci_dev *pci_dev;
  521. int rc = 0;
  522. ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
  523. if (!ibft_kobj)
  524. return -ENOMEM;
  525. ibft_kobj->header = header;
  526. ibft_kobj->hdr = hdr;
  527. switch (hdr->id) {
  528. case id_initiator:
  529. rc = ibft_verify_hdr("initiator", hdr, id_initiator,
  530. sizeof(*ibft_kobj->initiator));
  531. break;
  532. case id_nic:
  533. rc = ibft_verify_hdr("ethernet", hdr, id_nic,
  534. sizeof(*ibft_kobj->nic));
  535. break;
  536. case id_target:
  537. rc = ibft_verify_hdr("target", hdr, id_target,
  538. sizeof(*ibft_kobj->tgt));
  539. break;
  540. case id_reserved:
  541. case id_control:
  542. case id_extensions:
  543. /* Fields which we don't support. Ignore them */
  544. rc = 1;
  545. break;
  546. default:
  547. printk(KERN_ERR "iBFT has unknown structure type (%d). " \
  548. "Report this bug to %.6s!\n", hdr->id,
  549. header->oem_id);
  550. rc = 1;
  551. break;
  552. }
  553. if (rc) {
  554. /* Skip adding this kobject, but exit with non-fatal error. */
  555. kfree(ibft_kobj);
  556. goto out_invalid_struct;
  557. }
  558. ibft_kobj->kobj.kset = ibft_kset;
  559. rc = kobject_init_and_add(&ibft_kobj->kobj, &ibft_ktype,
  560. NULL, ibft_id_names[hdr->id], hdr->index);
  561. if (rc) {
  562. kfree(ibft_kobj);
  563. goto out;
  564. }
  565. kobject_uevent(&ibft_kobj->kobj, KOBJ_ADD);
  566. if (hdr->id == id_nic) {
  567. /*
  568. * We don't search for the device in other domains than
  569. * zero. This is because on x86 platforms the BIOS
  570. * executes only devices which are in domain 0. Furthermore, the
  571. * iBFT spec doesn't have a domain id field :-(
  572. */
  573. pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
  574. (nic->pci_bdf & 0xff));
  575. if (pci_dev) {
  576. rc = sysfs_create_link(&ibft_kobj->kobj,
  577. &pci_dev->dev.kobj, "device");
  578. pci_dev_put(pci_dev);
  579. }
  580. }
  581. /* Nothing broke so lets add it to the list. */
  582. list_add_tail(&ibft_kobj->node, list);
  583. out:
  584. return rc;
  585. out_invalid_struct:
  586. /* Unsupported structs are skipped. */
  587. return 0;
  588. }
  589. /*
  590. * Scan the IBFT table structure for the NIC and Target fields. When
  591. * found add them on the passed-in list. We do not support the other
  592. * fields at this point, so they are skipped.
  593. */
  594. static int __init ibft_register_kobjects(struct ibft_table_header *header,
  595. struct list_head *list)
  596. {
  597. struct ibft_control *control = NULL;
  598. void *ptr, *end;
  599. int rc = 0;
  600. u16 offset;
  601. u16 eot_offset;
  602. control = (void *)header + sizeof(*header);
  603. end = (void *)control + control->hdr.length;
  604. eot_offset = (void *)header + header->length - (void *)control;
  605. rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
  606. sizeof(*control));
  607. /* iBFT table safety checking */
  608. rc |= ((control->hdr.index) ? -ENODEV : 0);
  609. if (rc) {
  610. printk(KERN_ERR "iBFT error: Control header is invalid!\n");
  611. return rc;
  612. }
  613. for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
  614. offset = *(u16 *)ptr;
  615. if (offset && offset < header->length && offset < eot_offset) {
  616. rc = ibft_create_kobject(header,
  617. (void *)header + offset,
  618. list);
  619. if (rc)
  620. break;
  621. }
  622. }
  623. return rc;
  624. }
  625. static void ibft_unregister(struct list_head *attr_list,
  626. struct list_head *kobj_list)
  627. {
  628. struct ibft_kobject *data = NULL, *n;
  629. struct ibft_attribute *attr = NULL, *m;
  630. list_for_each_entry_safe(attr, m, attr_list, node) {
  631. sysfs_remove_file(attr->kobj, &attr->attr);
  632. list_del(&attr->node);
  633. kfree(attr);
  634. };
  635. list_del_init(attr_list);
  636. list_for_each_entry_safe(data, n, kobj_list, node) {
  637. list_del(&data->node);
  638. if (data->hdr->id == id_nic)
  639. sysfs_remove_link(&data->kobj, "device");
  640. kobject_put(&data->kobj);
  641. };
  642. list_del_init(kobj_list);
  643. }
  644. static int __init ibft_create_attribute(struct ibft_kobject *kobj_data,
  645. int type,
  646. const char *name,
  647. ssize_t (*show)(struct ibft_kobject *,
  648. struct ibft_attribute*,
  649. char *buf),
  650. struct list_head *list)
  651. {
  652. struct ibft_attribute *attr = NULL;
  653. struct ibft_hdr *hdr = kobj_data->hdr;
  654. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  655. if (!attr)
  656. return -ENOMEM;
  657. attr->attr.name = name;
  658. attr->attr.mode = S_IRUSR;
  659. attr->hdr = hdr;
  660. attr->show = show;
  661. attr->kobj = &kobj_data->kobj;
  662. attr->type = type;
  663. list_add_tail(&attr->node, list);
  664. return 0;
  665. }
  666. /*
  667. * Helper routiners to check to determine if the entry is valid
  668. * in the proper iBFT structure.
  669. */
  670. static int __init ibft_check_nic_for(struct ibft_nic *nic, int entry)
  671. {
  672. int rc = 0;
  673. switch (entry) {
  674. case ibft_eth_index:
  675. case ibft_eth_flags:
  676. rc = 1;
  677. break;
  678. case ibft_eth_ip_addr:
  679. if (!memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  680. rc = 1;
  681. break;
  682. case ibft_eth_subnet_mask:
  683. if (!memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  684. rc = 1;
  685. break;
  686. case ibft_eth_origin:
  687. rc = 1;
  688. break;
  689. case ibft_eth_gateway:
  690. if (memcmp(nic->gateway, nulls, sizeof(nic->gateway)))
  691. rc = 1;
  692. break;
  693. case ibft_eth_primary_dns:
  694. if (memcmp(nic->primary_dns, nulls,
  695. sizeof(nic->primary_dns)))
  696. rc = 1;
  697. break;
  698. case ibft_eth_secondary_dns:
  699. if (memcmp(nic->secondary_dns, nulls,
  700. sizeof(nic->secondary_dns)))
  701. rc = 1;
  702. break;
  703. case ibft_eth_dhcp:
  704. if (memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  705. rc = 1;
  706. break;
  707. case ibft_eth_vlan:
  708. case ibft_eth_mac:
  709. rc = 1;
  710. break;
  711. case ibft_eth_hostname:
  712. if (nic->hostname_off)
  713. rc = 1;
  714. break;
  715. default:
  716. break;
  717. }
  718. return rc;
  719. }
  720. static int __init ibft_check_tgt_for(struct ibft_tgt *tgt, int entry)
  721. {
  722. int rc = 0;
  723. switch (entry) {
  724. case ibft_tgt_index:
  725. case ibft_tgt_flags:
  726. case ibft_tgt_ip_addr:
  727. case ibft_tgt_port:
  728. case ibft_tgt_lun:
  729. case ibft_tgt_nic_assoc:
  730. case ibft_tgt_chap_type:
  731. rc = 1;
  732. case ibft_tgt_name:
  733. if (tgt->tgt_name_len)
  734. rc = 1;
  735. break;
  736. case ibft_tgt_chap_name:
  737. case ibft_tgt_chap_secret:
  738. if (tgt->chap_name_len)
  739. rc = 1;
  740. break;
  741. case ibft_tgt_rev_chap_name:
  742. case ibft_tgt_rev_chap_secret:
  743. if (tgt->rev_chap_name_len)
  744. rc = 1;
  745. break;
  746. default:
  747. break;
  748. }
  749. return rc;
  750. }
  751. static int __init ibft_check_initiator_for(struct ibft_initiator *init,
  752. int entry)
  753. {
  754. int rc = 0;
  755. switch (entry) {
  756. case ibft_init_index:
  757. case ibft_init_flags:
  758. rc = 1;
  759. break;
  760. case ibft_init_isns_server:
  761. if (memcmp(init->isns_server, nulls,
  762. sizeof(init->isns_server)))
  763. rc = 1;
  764. break;
  765. case ibft_init_slp_server:
  766. if (memcmp(init->slp_server, nulls,
  767. sizeof(init->slp_server)))
  768. rc = 1;
  769. break;
  770. case ibft_init_pri_radius_server:
  771. if (memcmp(init->pri_radius_server, nulls,
  772. sizeof(init->pri_radius_server)))
  773. rc = 1;
  774. break;
  775. case ibft_init_sec_radius_server:
  776. if (memcmp(init->sec_radius_server, nulls,
  777. sizeof(init->sec_radius_server)))
  778. rc = 1;
  779. break;
  780. case ibft_init_initiator_name:
  781. if (init->initiator_name_len)
  782. rc = 1;
  783. break;
  784. default:
  785. break;
  786. }
  787. return rc;
  788. }
  789. /*
  790. * Register the attributes for all of the kobjects.
  791. */
  792. static int __init ibft_register_attributes(struct list_head *kobject_list,
  793. struct list_head *attr_list)
  794. {
  795. int rc = 0, i = 0;
  796. struct ibft_kobject *data = NULL;
  797. struct ibft_attribute *attr = NULL, *m;
  798. list_for_each_entry(data, kobject_list, node) {
  799. switch (data->hdr->id) {
  800. case id_nic:
  801. for (i = 0; i < ibft_eth_end_marker && !rc; i++)
  802. if (ibft_check_nic_for(data->nic, i))
  803. rc = ibft_create_attribute(data, i,
  804. ibft_eth_properties[i],
  805. ibft_attr_show_nic, attr_list);
  806. break;
  807. case id_target:
  808. for (i = 0; i < ibft_tgt_end_marker && !rc; i++)
  809. if (ibft_check_tgt_for(data->tgt, i))
  810. rc = ibft_create_attribute(data, i,
  811. ibft_tgt_properties[i],
  812. ibft_attr_show_target,
  813. attr_list);
  814. break;
  815. case id_initiator:
  816. for (i = 0; i < ibft_init_end_marker && !rc; i++)
  817. if (ibft_check_initiator_for(
  818. data->initiator, i))
  819. rc = ibft_create_attribute(data, i,
  820. ibft_initiator_properties[i],
  821. ibft_attr_show_initiator,
  822. attr_list);
  823. break;
  824. default:
  825. break;
  826. }
  827. if (rc)
  828. break;
  829. }
  830. list_for_each_entry_safe(attr, m, attr_list, node) {
  831. rc = sysfs_create_file(attr->kobj, &attr->attr);
  832. if (rc) {
  833. list_del(&attr->node);
  834. kfree(attr);
  835. break;
  836. }
  837. }
  838. return rc;
  839. }
  840. /*
  841. * ibft_init() - creates sysfs tree entries for the iBFT data.
  842. */
  843. static int __init ibft_init(void)
  844. {
  845. int rc = 0;
  846. ibft_kset = kset_create_and_add("ibft", NULL, firmware_kobj);
  847. if (!ibft_kset)
  848. return -ENOMEM;
  849. if (ibft_addr) {
  850. printk(KERN_INFO "iBFT detected at 0x%lx.\n",
  851. virt_to_phys((void *)ibft_addr));
  852. rc = ibft_check_device();
  853. if (rc)
  854. goto out_firmware_unregister;
  855. /* Scan the IBFT for data and register the kobjects. */
  856. rc = ibft_register_kobjects(ibft_addr, &ibft_kobject_list);
  857. if (rc)
  858. goto out_free;
  859. /* Register the attributes */
  860. rc = ibft_register_attributes(&ibft_kobject_list,
  861. &ibft_attr_list);
  862. if (rc)
  863. goto out_free;
  864. } else
  865. printk(KERN_INFO "No iBFT detected.\n");
  866. return 0;
  867. out_free:
  868. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  869. out_firmware_unregister:
  870. kset_unregister(ibft_kset);
  871. return rc;
  872. }
  873. static void __exit ibft_exit(void)
  874. {
  875. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  876. kset_unregister(ibft_kset);
  877. }
  878. module_init(ibft_init);
  879. module_exit(ibft_exit);