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.2KB

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