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.

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