You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bofm.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <ipxe/uaccess.h>
  28. #include <ipxe/list.h>
  29. #include <ipxe/ethernet.h>
  30. #include <ipxe/bofm.h>
  31. /** @file
  32. *
  33. * IBM BladeCenter Open Fabric Manager (BOFM)
  34. *
  35. */
  36. /** List of BOFM devices */
  37. static LIST_HEAD ( bofmdevs );
  38. /**
  39. * Register BOFM device
  40. *
  41. * @v bofm BOFM device
  42. * @ret rc Return status code
  43. */
  44. int bofm_register ( struct bofm_device *bofm ) {
  45. list_add ( &bofm->list, &bofmdevs );
  46. DBG ( "BOFM: " PCI_FMT " registered using driver \"%s\"\n",
  47. PCI_ARGS ( bofm->pci ), bofm->pci->id->name );
  48. return 0;
  49. }
  50. /**
  51. * Unregister BOFM device
  52. *
  53. * @v bofm BOFM device
  54. */
  55. void bofm_unregister ( struct bofm_device *bofm ) {
  56. list_del ( &bofm->list );
  57. DBG ( "BOFM: " PCI_FMT " unregistered\n", PCI_ARGS ( bofm->pci ) );
  58. }
  59. /**
  60. * Find BOFM device matching PCI bus:dev.fn address
  61. *
  62. * @v busdevfn PCI bus:dev.fn address
  63. * @ret bofm BOFM device, or NULL
  64. */
  65. static struct bofm_device * bofm_find_busdevfn ( unsigned int busdevfn ) {
  66. struct bofm_device *bofm;
  67. list_for_each_entry ( bofm, &bofmdevs, list ) {
  68. if ( bofm->pci->busdevfn == busdevfn )
  69. return bofm;
  70. }
  71. return NULL;
  72. }
  73. /**
  74. * Find BOFM driver for PCI device
  75. *
  76. * @v pci PCI device
  77. * @ret rc Return status code
  78. */
  79. int bofm_find_driver ( struct pci_device *pci ) {
  80. struct pci_driver *driver;
  81. struct pci_device_id *id;
  82. unsigned int i;
  83. for_each_table_entry ( driver, BOFM_DRIVERS ) {
  84. for ( i = 0 ; i < driver->id_count ; i++ ) {
  85. id = &driver->ids[i];
  86. if ( ( id->vendor == pci->vendor ) &&
  87. ( id->device == pci->device ) ) {
  88. pci_set_driver ( pci, driver, id );
  89. return 0;
  90. }
  91. }
  92. }
  93. return -ENOENT;
  94. }
  95. /**
  96. * Probe PCI device for BOFM driver
  97. *
  98. * @v pci PCI device
  99. * @ret rc Return status code
  100. */
  101. static int bofm_probe ( struct pci_device *pci ) {
  102. int rc;
  103. /* Probe device */
  104. if ( ( rc = pci_probe ( pci ) ) != 0 ) {
  105. DBG ( "BOFM: " PCI_FMT " could not load driver: %s\n",
  106. PCI_ARGS ( pci ), strerror ( rc ) );
  107. return rc;
  108. }
  109. return 0;
  110. }
  111. /**
  112. * Remove PCI device
  113. *
  114. * @v pci PCI device
  115. */
  116. static void bofm_remove ( struct pci_device *pci ) {
  117. /* Note that the IBM BIOS may re-read the expansion ROM after
  118. * the BOFM initialisation call. The BOFM driver must ensure
  119. * that the card is left in a state in which expansion ROM
  120. * reads will succeed. (For example, if a card contains an
  121. * embedded CPU that may issue reads to the same underlying
  122. * flash device, and these reads are not locked against reads
  123. * via the expansion ROM BAR, then the CPU must be stopped.)
  124. *
  125. * If this is not done, then occasional corrupted reads from
  126. * the expansion ROM will be seen, and the BIOS may complain
  127. * about a ROM checksum error.
  128. */
  129. pci_remove ( pci );
  130. DBG ( "BOFM: " PCI_FMT " removed\n", PCI_ARGS ( pci ) );
  131. }
  132. /**
  133. * Locate BOFM table section
  134. *
  135. * @v bofmtab BOFM table
  136. * @v len Length of BOFM table
  137. * @v magic Section magic
  138. * @v bofmsec BOFM section header to fill in
  139. * @ret offset Offset to section, or 0 if not found
  140. */
  141. static size_t bofm_locate_section ( userptr_t bofmtab, size_t len,
  142. uint32_t magic,
  143. struct bofm_section_header *bofmsec ) {
  144. size_t offset = sizeof ( struct bofm_global_header );
  145. while ( offset < len ) {
  146. copy_from_user ( bofmsec, bofmtab, offset,
  147. sizeof ( *bofmsec ) );
  148. if ( bofmsec->magic == magic )
  149. return offset;
  150. if ( bofmsec->magic == BOFM_DONE_MAGIC )
  151. break;
  152. offset += ( sizeof ( *bofmsec ) + bofmsec->length );
  153. }
  154. return 0;
  155. }
  156. /**
  157. * Process BOFM Ethernet parameter entry
  158. *
  159. * @v bofm BOFM device
  160. * @v en EN parameter entry
  161. * @ret rc Return status code
  162. */
  163. static int bofm_en ( struct bofm_device *bofm, struct bofm_en *en ) {
  164. uint8_t mac[6];
  165. int rc;
  166. /* Retrieve current MAC address */
  167. if ( ( rc = bofm->op->harvest ( bofm, en->mport, mac ) ) != 0 ) {
  168. DBG ( "BOFM: " PCI_FMT " mport %d could not harvest: %s\n",
  169. PCI_ARGS ( bofm->pci ), en->mport, strerror ( rc ) );
  170. return rc;
  171. }
  172. /* Harvest MAC address if necessary */
  173. if ( en->options & BOFM_EN_RQ_HVST_MASK ) {
  174. DBG ( "BOFM: " PCI_FMT " mport %d harvested MAC %s\n",
  175. PCI_ARGS ( bofm->pci ), en->mport, eth_ntoa ( mac ) );
  176. memcpy ( en->mac_a, mac, sizeof ( en->mac_a ) );
  177. en->options |= ( BOFM_EN_EN_A | BOFM_EN_HVST );
  178. }
  179. /* Mark as changed if necessary */
  180. if ( ( en->options & BOFM_EN_EN_A ) &&
  181. ( memcmp ( en->mac_a, mac, sizeof ( en->mac_a ) ) != 0 ) ) {
  182. DBG ( "BOFM: " PCI_FMT " mport %d MAC %s",
  183. PCI_ARGS ( bofm->pci ), en->mport, eth_ntoa ( mac ) );
  184. DBG ( " changed to %s\n", eth_ntoa ( en->mac_a ) );
  185. en->options |= BOFM_EN_CHG_CHANGED;
  186. }
  187. /* Apply MAC address if necessary */
  188. if ( ( en->options & BOFM_EN_EN_A ) &&
  189. ( en->options & BOFM_EN_USAGE_ENTRY ) &&
  190. ( ! ( en->options & BOFM_EN_USAGE_HARVEST ) ) ) {
  191. DBG ( "BOFM: " PCI_FMT " mport %d applied MAC %s\n",
  192. PCI_ARGS ( bofm->pci ), en->mport,
  193. eth_ntoa ( en->mac_a ) );
  194. memcpy ( mac, en->mac_a, sizeof ( mac ) );
  195. }
  196. /* Store MAC address */
  197. if ( ( rc = bofm->op->update ( bofm, en->mport, mac ) ) != 0 ) {
  198. DBG ( "BOFM: " PCI_FMT " mport %d could not update: %s\n",
  199. PCI_ARGS ( bofm->pci ), en->mport, strerror ( rc ) );
  200. return rc;
  201. }
  202. return 0;
  203. }
  204. /**
  205. * Process BOFM table
  206. *
  207. * @v bofmtab BOFM table
  208. * @v pci PCI device
  209. * @ret bofmrc BOFM return status
  210. */
  211. int bofm ( userptr_t bofmtab, struct pci_device *pci ) {
  212. struct bofm_global_header bofmhdr;
  213. struct bofm_section_header bofmsec;
  214. struct bofm_en en;
  215. struct bofm_device *bofm;
  216. size_t en_region_offset;
  217. size_t en_offset;
  218. int skip;
  219. int rc;
  220. int bofmrc;
  221. /* Read BOFM structure */
  222. copy_from_user ( &bofmhdr, bofmtab, 0, sizeof ( bofmhdr ) );
  223. if ( bofmhdr.magic != BOFM_IOAA_MAGIC ) {
  224. DBG ( "BOFM: invalid table signature " BOFM_MAGIC_FMT "\n",
  225. BOFM_MAGIC_ARGS ( bofmhdr.magic ) );
  226. bofmrc = BOFM_ERR_INVALID_ACTION;
  227. goto err_bad_signature;
  228. }
  229. DBG ( "BOFM: " BOFM_MAGIC_FMT " (profile \"%s\")\n",
  230. BOFM_MAGIC_ARGS ( bofmhdr.action ), bofmhdr.profile );
  231. /* Determine whether or not we should skip normal POST
  232. * initialisation.
  233. */
  234. switch ( bofmhdr.action ) {
  235. case BOFM_ACTION_UPDT:
  236. case BOFM_ACTION_DFLT:
  237. case BOFM_ACTION_HVST:
  238. skip = BOFM_SKIP_INIT;
  239. break;
  240. case BOFM_ACTION_PARM:
  241. case BOFM_ACTION_NONE:
  242. skip = 0;
  243. break;
  244. default:
  245. DBG ( "BOFM: invalid action " BOFM_MAGIC_FMT "\n",
  246. BOFM_MAGIC_ARGS ( bofmhdr.action ) );
  247. bofmrc = BOFM_ERR_INVALID_ACTION;
  248. goto err_bad_action;
  249. }
  250. /* Find BOFM driver */
  251. if ( ( rc = bofm_find_driver ( pci ) ) != 0 ) {
  252. DBG ( "BOFM: " PCI_FMT " has no driver\n", PCI_ARGS ( pci ) );
  253. bofmrc = BOFM_ERR_DEVICE_ERROR;
  254. goto err_find_driver;
  255. }
  256. /* Probe driver for PCI device */
  257. if ( ( rc = bofm_probe ( pci ) ) != 0 ) {
  258. bofmrc = BOFM_ERR_DEVICE_ERROR;
  259. goto err_probe;
  260. }
  261. /* Locate EN section, if present */
  262. en_region_offset = bofm_locate_section ( bofmtab, bofmhdr.length,
  263. BOFM_EN_MAGIC, &bofmsec );
  264. if ( ! en_region_offset ) {
  265. DBG ( "BOFM: No EN section found\n" );
  266. bofmrc = ( BOFM_SUCCESS | skip );
  267. goto err_no_en_section;
  268. }
  269. /* Iterate through EN entries */
  270. for ( en_offset = ( en_region_offset + sizeof ( bofmsec ) ) ;
  271. en_offset < ( en_region_offset + sizeof ( bofmsec ) +
  272. bofmsec.length ) ; en_offset += sizeof ( en ) ) {
  273. copy_from_user ( &en, bofmtab, en_offset, sizeof ( en ) );
  274. DBG2 ( "BOFM: EN entry found:\n" );
  275. DBG2_HDA ( en_offset, &en, sizeof ( en ) );
  276. if ( ( en.options & BOFM_EN_MAP_MASK ) != BOFM_EN_MAP_PFA ) {
  277. DBG ( "BOFM: slot %d port %d has no PCI mapping\n",
  278. en.slot, ( en.port + 1 ) );
  279. continue;
  280. }
  281. DBG ( "BOFM: slot %d port %d%s is " PCI_FMT " mport %d\n",
  282. en.slot, ( en.port + 1 ),
  283. ( ( en.slot || en.port ) ? "" : "(?)" ), 0,
  284. PCI_BUS ( en.busdevfn ), PCI_SLOT ( en.busdevfn ),
  285. PCI_FUNC ( en.busdevfn ), en.mport );
  286. bofm = bofm_find_busdevfn ( en.busdevfn );
  287. if ( ! bofm ) {
  288. DBG ( "BOFM: " PCI_FMT " mport %d ignored\n", 0,
  289. PCI_BUS ( en.busdevfn ), PCI_SLOT ( en.busdevfn ),
  290. PCI_FUNC ( en.busdevfn ), en.mport );
  291. continue;
  292. }
  293. if ( ( rc = bofm_en ( bofm, &en ) ) == 0 ) {
  294. en.options |= BOFM_EN_CSM_SUCCESS;
  295. } else {
  296. en.options |= BOFM_EN_CSM_FAILED;
  297. }
  298. DBG2 ( "BOFM: EN entry after processing:\n" );
  299. DBG2_HDA ( en_offset, &en, sizeof ( en ) );
  300. copy_to_user ( bofmtab, en_offset, &en, sizeof ( en ) );
  301. }
  302. bofmrc = ( BOFM_SUCCESS | skip );
  303. err_no_en_section:
  304. bofm_remove ( pci );
  305. err_probe:
  306. err_find_driver:
  307. err_bad_action:
  308. err_bad_signature:
  309. return bofmrc;
  310. }