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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * Copyright (C) 2014 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 <string.h>
  25. #include <strings.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <ipxe/netdevice.h>
  29. #include <ipxe/ethernet.h>
  30. #include <ipxe/umalloc.h>
  31. #include <ipxe/efi/efi.h>
  32. #include <ipxe/efi/efi_driver.h>
  33. #include <ipxe/efi/efi_pci.h>
  34. #include <ipxe/efi/efi_utils.h>
  35. #include <ipxe/efi/Protocol/NetworkInterfaceIdentifier.h>
  36. #include <ipxe/efi/IndustryStandard/Acpi10.h>
  37. #include "nii.h"
  38. /** @file
  39. *
  40. * NII driver
  41. *
  42. */
  43. /* Error numbers generated by NII */
  44. #define EIO_INVALID_CDB __einfo_error ( EINFO_EIO_INVALID_CDB )
  45. #define EINFO_EIO_INVALID_CDB \
  46. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_CDB, \
  47. "Invalid CDB" )
  48. #define EIO_INVALID_CPB __einfo_error ( EINFO_EIO_INVALID_CPB )
  49. #define EINFO_EIO_INVALID_CPB \
  50. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_CPB, \
  51. "Invalid CPB" )
  52. #define EIO_BUSY __einfo_error ( EINFO_EIO_BUSY )
  53. #define EINFO_EIO_BUSY \
  54. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_BUSY, \
  55. "Busy" )
  56. #define EIO_QUEUE_FULL __einfo_error ( EINFO_EIO_QUEUE_FULL )
  57. #define EINFO_EIO_QUEUE_FULL \
  58. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_QUEUE_FULL, \
  59. "Queue full" )
  60. #define EIO_ALREADY_STARTED __einfo_error ( EINFO_EIO_ALREADY_STARTED )
  61. #define EINFO_EIO_ALREADY_STARTED \
  62. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_ALREADY_STARTED, \
  63. "Already started" )
  64. #define EIO_NOT_STARTED __einfo_error ( EINFO_EIO_NOT_STARTED )
  65. #define EINFO_EIO_NOT_STARTED \
  66. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_STARTED, \
  67. "Not started" )
  68. #define EIO_NOT_SHUTDOWN __einfo_error ( EINFO_EIO_NOT_SHUTDOWN )
  69. #define EINFO_EIO_NOT_SHUTDOWN \
  70. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_SHUTDOWN, \
  71. "Not shutdown" )
  72. #define EIO_ALREADY_INITIALIZED __einfo_error ( EINFO_EIO_ALREADY_INITIALIZED )
  73. #define EINFO_EIO_ALREADY_INITIALIZED \
  74. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_ALREADY_INITIALIZED, \
  75. "Already initialized" )
  76. #define EIO_NOT_INITIALIZED __einfo_error ( EINFO_EIO_NOT_INITIALIZED )
  77. #define EINFO_EIO_NOT_INITIALIZED \
  78. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_INITIALIZED, \
  79. "Not initialized" )
  80. #define EIO_DEVICE_FAILURE __einfo_error ( EINFO_EIO_DEVICE_FAILURE )
  81. #define EINFO_EIO_DEVICE_FAILURE \
  82. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_DEVICE_FAILURE, \
  83. "Device failure" )
  84. #define EIO_NVDATA_FAILURE __einfo_error ( EINFO_EIO_NVDATA_FAILURE )
  85. #define EINFO_EIO_NVDATA_FAILURE \
  86. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NVDATA_FAILURE, \
  87. "Non-volatile data failure" )
  88. #define EIO_UNSUPPORTED __einfo_error ( EINFO_EIO_UNSUPPORTED )
  89. #define EINFO_EIO_UNSUPPORTED \
  90. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_UNSUPPORTED, \
  91. "Unsupported" )
  92. #define EIO_BUFFER_FULL __einfo_error ( EINFO_EIO_BUFFER_FULL )
  93. #define EINFO_EIO_BUFFER_FULL \
  94. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_BUFFER_FULL, \
  95. "Buffer full" )
  96. #define EIO_INVALID_PARAMETER __einfo_error ( EINFO_EIO_INVALID_PARAMETER )
  97. #define EINFO_EIO_INVALID_PARAMETER \
  98. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_PARAMETER, \
  99. "Invalid parameter" )
  100. #define EIO_INVALID_UNDI __einfo_error ( EINFO_EIO_INVALID_UNDI )
  101. #define EINFO_EIO_INVALID_UNDI \
  102. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_INVALID_UNDI, \
  103. "Invalid UNDI" )
  104. #define EIO_IPV4_NOT_SUPPORTED __einfo_error ( EINFO_EIO_IPV4_NOT_SUPPORTED )
  105. #define EINFO_EIO_IPV4_NOT_SUPPORTED \
  106. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_IPV4_NOT_SUPPORTED, \
  107. "IPv4 not supported" )
  108. #define EIO_IPV6_NOT_SUPPORTED __einfo_error ( EINFO_EIO_IPV6_NOT_SUPPORTED )
  109. #define EINFO_EIO_IPV6_NOT_SUPPORTED \
  110. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_IPV6_NOT_SUPPORTED, \
  111. "IPv6 not supported" )
  112. #define EIO_NOT_ENOUGH_MEMORY __einfo_error ( EINFO_EIO_NOT_ENOUGH_MEMORY )
  113. #define EINFO_EIO_NOT_ENOUGH_MEMORY \
  114. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NOT_ENOUGH_MEMORY, \
  115. "Not enough memory" )
  116. #define EIO_NO_DATA __einfo_error ( EINFO_EIO_NO_DATA )
  117. #define EINFO_EIO_NO_DATA \
  118. __einfo_uniqify ( EINFO_EIO, PXE_STATCODE_NO_DATA, \
  119. "No data" )
  120. #define EIO_STAT( stat ) \
  121. EUNIQ ( EINFO_EIO, -(stat), EIO_INVALID_CDB, EIO_INVALID_CPB, \
  122. EIO_BUSY, EIO_QUEUE_FULL, EIO_ALREADY_STARTED, \
  123. EIO_NOT_STARTED, EIO_NOT_SHUTDOWN, EIO_ALREADY_INITIALIZED, \
  124. EIO_NOT_INITIALIZED, EIO_DEVICE_FAILURE, EIO_NVDATA_FAILURE, \
  125. EIO_UNSUPPORTED, EIO_BUFFER_FULL, EIO_INVALID_PARAMETER, \
  126. EIO_INVALID_UNDI, EIO_IPV4_NOT_SUPPORTED, \
  127. EIO_IPV6_NOT_SUPPORTED, EIO_NOT_ENOUGH_MEMORY, EIO_NO_DATA )
  128. /** Maximum PCI BAR
  129. *
  130. * This is defined in <ipxe/efi/IndustryStandard/Pci22.h>, but we
  131. * can't #include that since it collides with <ipxe/pci.h>.
  132. */
  133. #define PCI_MAX_BAR 6
  134. /** An NII NIC */
  135. struct nii_nic {
  136. /** EFI device */
  137. struct efi_device *efidev;
  138. /** Network interface identifier protocol */
  139. EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *nii;
  140. /** !PXE structure */
  141. PXE_SW_UNDI *undi;
  142. /** Entry point */
  143. EFIAPI VOID ( * issue ) ( UINT64 cdb );
  144. /** Generic device */
  145. struct device dev;
  146. /** PCI device */
  147. EFI_HANDLE pci_device;
  148. /** PCI I/O protocol */
  149. EFI_PCI_IO_PROTOCOL *pci_io;
  150. /** Memory BAR */
  151. unsigned int mem_bar;
  152. /** I/O BAR */
  153. unsigned int io_bar;
  154. /** Broadcast address */
  155. PXE_MAC_ADDR broadcast;
  156. /** Maximum packet length */
  157. size_t mtu;
  158. /** Hardware transmit/receive buffer */
  159. userptr_t buffer;
  160. /** Hardware transmit/receive buffer length */
  161. size_t buffer_len;
  162. /** Saved task priority level */
  163. EFI_TPL saved_tpl;
  164. /** Media status is supported */
  165. int media;
  166. /** Current transmit buffer */
  167. struct io_buffer *txbuf;
  168. /** Current receive buffer */
  169. struct io_buffer *rxbuf;
  170. };
  171. /** Maximum number of received packets per poll */
  172. #define NII_RX_QUOTA 4
  173. /**
  174. * Open PCI I/O protocol and identify BARs
  175. *
  176. * @v nii NII NIC
  177. * @ret rc Return status code
  178. */
  179. static int nii_pci_open ( struct nii_nic *nii ) {
  180. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  181. EFI_HANDLE device = nii->efidev->device;
  182. EFI_HANDLE pci_device;
  183. union {
  184. EFI_PCI_IO_PROTOCOL *pci_io;
  185. void *interface;
  186. } pci_io;
  187. union {
  188. EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *acpi;
  189. void *resource;
  190. } desc;
  191. unsigned int bar;
  192. EFI_STATUS efirc;
  193. int rc;
  194. /* Locate PCI I/O protocol */
  195. if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid,
  196. &pci_device ) ) != 0 ) {
  197. DBGC ( nii, "NII %s could not locate PCI I/O protocol: %s\n",
  198. nii->dev.name, strerror ( rc ) );
  199. goto err_locate;
  200. }
  201. nii->pci_device = pci_device;
  202. /* Open PCI I/O protocol */
  203. if ( ( efirc = bs->OpenProtocol ( pci_device, &efi_pci_io_protocol_guid,
  204. &pci_io.interface, efi_image_handle,
  205. device,
  206. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  207. rc = -EEFI ( efirc );
  208. DBGC ( nii, "NII %s could not open PCI I/O protocol: %s\n",
  209. nii->dev.name, strerror ( rc ) );
  210. goto err_open;
  211. }
  212. nii->pci_io = pci_io.pci_io;
  213. /* Identify memory and I/O BARs */
  214. nii->mem_bar = PCI_MAX_BAR;
  215. nii->io_bar = PCI_MAX_BAR;
  216. for ( bar = 0 ; bar < PCI_MAX_BAR ; bar++ ) {
  217. efirc = nii->pci_io->GetBarAttributes ( nii->pci_io, bar, NULL,
  218. &desc.resource );
  219. if ( efirc == EFI_UNSUPPORTED ) {
  220. /* BAR not present; ignore */
  221. continue;
  222. }
  223. if ( efirc != 0 ) {
  224. rc = -EEFI ( efirc );
  225. DBGC ( nii, "NII %s could not get BAR %d attributes: "
  226. "%s\n", nii->dev.name, bar, strerror ( rc ) );
  227. goto err_get_bar_attributes;
  228. }
  229. if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM ) {
  230. nii->mem_bar = bar;
  231. } else if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_IO ) {
  232. nii->io_bar = bar;
  233. }
  234. bs->FreePool ( desc.resource );
  235. }
  236. DBGC ( nii, "NII %s has ", nii->dev.name );
  237. if ( nii->mem_bar < PCI_MAX_BAR ) {
  238. DBGC ( nii, "memory BAR %d and ", nii->mem_bar );
  239. } else {
  240. DBGC ( nii, "no memory BAR and " );
  241. }
  242. if ( nii->io_bar < PCI_MAX_BAR ) {
  243. DBGC ( nii, "I/O BAR %d\n", nii->io_bar );
  244. } else {
  245. DBGC ( nii, "no I/O BAR\n" );
  246. }
  247. return 0;
  248. err_get_bar_attributes:
  249. bs->CloseProtocol ( pci_device, &efi_pci_io_protocol_guid,
  250. efi_image_handle, device );
  251. err_open:
  252. err_locate:
  253. return rc;
  254. }
  255. /**
  256. * Close PCI I/O protocol
  257. *
  258. * @v nii NII NIC
  259. * @ret rc Return status code
  260. */
  261. static void nii_pci_close ( struct nii_nic *nii ) {
  262. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  263. bs->CloseProtocol ( nii->pci_device, &efi_pci_io_protocol_guid,
  264. efi_image_handle, nii->efidev->device );
  265. }
  266. /**
  267. * I/O callback
  268. *
  269. * @v unique_id NII NIC
  270. * @v op Operations
  271. * @v len Length of data
  272. * @v addr Address
  273. * @v data Data buffer
  274. */
  275. static EFIAPI VOID nii_io ( UINT64 unique_id, UINT8 op, UINT8 len, UINT64 addr,
  276. UINT64 data ) {
  277. struct nii_nic *nii = ( ( void * ) ( intptr_t ) unique_id );
  278. EFI_PCI_IO_PROTOCOL_ACCESS *access;
  279. EFI_PCI_IO_PROTOCOL_IO_MEM io;
  280. EFI_PCI_IO_PROTOCOL_WIDTH width;
  281. unsigned int bar;
  282. EFI_STATUS efirc;
  283. int rc;
  284. /* Determine accessor and BAR */
  285. if ( op & ( PXE_MEM_READ | PXE_MEM_WRITE ) ) {
  286. access = &nii->pci_io->Mem;
  287. bar = nii->mem_bar;
  288. } else {
  289. access = &nii->pci_io->Io;
  290. bar = nii->io_bar;
  291. }
  292. /* Determine operaton */
  293. io = ( ( op & ( PXE_IO_WRITE | PXE_MEM_WRITE ) ) ?
  294. access->Write : access->Read );
  295. /* Determine width */
  296. width = ( fls ( len ) - 1 );
  297. /* Issue operation */
  298. if ( ( efirc = io ( nii->pci_io, width, bar, addr, 1,
  299. ( ( void * ) ( intptr_t ) data ) ) ) != 0 ) {
  300. rc = -EEFI ( efirc );
  301. DBGC ( nii, "NII %s I/O operation %#x failed: %s\n",
  302. nii->dev.name, op, strerror ( rc ) );
  303. /* No way to report failure */
  304. return;
  305. }
  306. }
  307. /**
  308. * Delay callback
  309. *
  310. * @v unique_id NII NIC
  311. * @v microseconds Delay in microseconds
  312. */
  313. static EFIAPI VOID nii_delay ( UINT64 unique_id __unused, UINTN microseconds ) {
  314. udelay ( microseconds );
  315. }
  316. /**
  317. * Block callback
  318. *
  319. * @v unique_id NII NIC
  320. * @v acquire Acquire lock
  321. */
  322. static EFIAPI VOID nii_block ( UINT64 unique_id, UINT32 acquire ) {
  323. struct nii_nic *nii = ( ( void * ) ( intptr_t ) unique_id );
  324. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  325. /* This functionality (which is copied verbatim from the
  326. * SnpDxe implementation of this function) appears to be
  327. * totally brain-dead, since it produces no actual blocking
  328. * behaviour.
  329. */
  330. if ( acquire ) {
  331. nii->saved_tpl = bs->RaiseTPL ( TPL_NOTIFY );
  332. } else {
  333. bs->RestoreTPL ( nii->saved_tpl );
  334. }
  335. }
  336. /**
  337. * Construct operation from opcode and flags
  338. *
  339. * @v opcode Opcode
  340. * @v opflags Flags
  341. * @ret op Operation
  342. */
  343. #define NII_OP( opcode, opflags ) ( (opcode) | ( (opflags) << 16 ) )
  344. /**
  345. * Extract opcode from operation
  346. *
  347. * @v op Operation
  348. * @ret opcode Opcode
  349. */
  350. #define NII_OPCODE( op ) ( (op) & 0xffff )
  351. /**
  352. * Extract flags from operation
  353. *
  354. * @v op Operation
  355. * @ret opflags Flags
  356. */
  357. #define NII_OPFLAGS( op ) ( (op) >> 16 )
  358. /**
  359. * Issue command with parameter block and data block
  360. *
  361. * @v nii NII NIC
  362. * @v op Operation
  363. * @v cpb Command parameter block, or NULL
  364. * @v cpb_len Command parameter block length
  365. * @v db Data block, or NULL
  366. * @v db_len Data block length
  367. * @ret stat Status flags, or negative status code
  368. */
  369. static int nii_issue_cpb_db ( struct nii_nic *nii, unsigned int op, void *cpb,
  370. size_t cpb_len, void *db, size_t db_len ) {
  371. PXE_CDB cdb;
  372. /* Prepare command descriptor block */
  373. memset ( &cdb, 0, sizeof ( cdb ) );
  374. cdb.OpCode = NII_OPCODE ( op );
  375. cdb.OpFlags = NII_OPFLAGS ( op );
  376. cdb.CPBaddr = ( ( intptr_t ) cpb );
  377. cdb.CPBsize = cpb_len;
  378. cdb.DBaddr = ( ( intptr_t ) db );
  379. cdb.DBsize = db_len;
  380. cdb.IFnum = nii->nii->IfNum;
  381. /* Issue command */
  382. DBGC2 ( nii, "NII %s issuing %02x:%04x ifnum %d%s%s\n",
  383. nii->dev.name, cdb.OpCode, cdb.OpFlags, cdb.IFnum,
  384. ( cpb ? " cpb" : "" ), ( db ? " db" : "" ) );
  385. if ( cpb )
  386. DBGC2_HD ( nii, cpb, cpb_len );
  387. if ( db )
  388. DBGC2_HD ( nii, db, db_len );
  389. nii->issue ( ( intptr_t ) &cdb );
  390. /* Check completion status */
  391. if ( cdb.StatCode != PXE_STATCODE_SUCCESS )
  392. return -cdb.StatCode;
  393. /* Return command-specific status flags */
  394. return ( cdb.StatFlags & ~PXE_STATFLAGS_STATUS_MASK );
  395. }
  396. /**
  397. * Issue command with parameter block
  398. *
  399. * @v nii NII NIC
  400. * @v op Operation
  401. * @v cpb Command parameter block, or NULL
  402. * @v cpb_len Command parameter block length
  403. * @ret stat Status flags, or negative status code
  404. */
  405. static int nii_issue_cpb ( struct nii_nic *nii, unsigned int op, void *cpb,
  406. size_t cpb_len ) {
  407. return nii_issue_cpb_db ( nii, op, cpb, cpb_len, NULL, 0 );
  408. }
  409. /**
  410. * Issue command with data block
  411. *
  412. * @v nii NII NIC
  413. * @v op Operation
  414. * @v db Data block, or NULL
  415. * @v db_len Data block length
  416. * @ret stat Status flags, or negative status code
  417. */
  418. static int nii_issue_db ( struct nii_nic *nii, unsigned int op, void *db,
  419. size_t db_len ) {
  420. return nii_issue_cpb_db ( nii, op, NULL, 0, db, db_len );
  421. }
  422. /**
  423. * Issue command
  424. *
  425. *
  426. * @v nii NII NIC
  427. * @v op Operation
  428. * @ret stat Status flags, or negative status code
  429. */
  430. static int nii_issue ( struct nii_nic *nii, unsigned int op ) {
  431. return nii_issue_cpb_db ( nii, op, NULL, 0, NULL, 0 );
  432. }
  433. /**
  434. * Start UNDI
  435. *
  436. * @v nii NII NIC
  437. * @ret rc Return status code
  438. */
  439. static int nii_start_undi ( struct nii_nic *nii ) {
  440. PXE_CPB_START_31 cpb;
  441. int stat;
  442. int rc;
  443. /* Construct parameter block */
  444. memset ( &cpb, 0, sizeof ( cpb ) );
  445. cpb.Delay = ( ( intptr_t ) nii_delay );
  446. cpb.Block = ( ( intptr_t ) nii_block );
  447. cpb.Mem_IO = ( ( intptr_t ) nii_io );
  448. cpb.Unique_ID = ( ( intptr_t ) nii );
  449. /* Issue command */
  450. if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_START, &cpb,
  451. sizeof ( cpb ) ) ) < 0 ) {
  452. rc = -EIO_STAT ( stat );
  453. DBGC ( nii, "NII %s could not start: %s\n",
  454. nii->dev.name, strerror ( rc ) );
  455. return rc;
  456. }
  457. return 0;
  458. }
  459. /**
  460. * Stop UNDI
  461. *
  462. * @v nii NII NIC
  463. */
  464. static void nii_stop_undi ( struct nii_nic *nii ) {
  465. int stat;
  466. int rc;
  467. /* Issue command */
  468. if ( ( stat = nii_issue ( nii, PXE_OPCODE_STOP ) ) < 0 ) {
  469. rc = -EIO_STAT ( stat );
  470. DBGC ( nii, "NII %s could not stop: %s\n",
  471. nii->dev.name, strerror ( rc ) );
  472. /* Nothing we can do about it */
  473. return;
  474. }
  475. }
  476. /**
  477. * Get initialisation information
  478. *
  479. * @v nii NII NIC
  480. * @v netdev Network device to fill in
  481. * @ret rc Return status code
  482. */
  483. static int nii_get_init_info ( struct nii_nic *nii,
  484. struct net_device *netdev ) {
  485. PXE_DB_GET_INIT_INFO db;
  486. int stat;
  487. int rc;
  488. /* Issue command */
  489. if ( ( stat = nii_issue_db ( nii, PXE_OPCODE_GET_INIT_INFO, &db,
  490. sizeof ( db ) ) ) < 0 ) {
  491. rc = -EIO_STAT ( stat );
  492. DBGC ( nii, "NII %s could not get initialisation info: %s\n",
  493. nii->dev.name, strerror ( rc ) );
  494. return rc;
  495. }
  496. /* Determine link layer protocol */
  497. switch ( db.IFtype ) {
  498. case PXE_IFTYPE_ETHERNET :
  499. netdev->ll_protocol = &ethernet_protocol;
  500. break;
  501. default:
  502. DBGC ( nii, "NII %s unknown interface type %#02x\n",
  503. nii->dev.name, db.IFtype );
  504. return -ENOTSUP;
  505. }
  506. /* Sanity checks */
  507. assert ( db.MediaHeaderLen == netdev->ll_protocol->ll_header_len );
  508. assert ( db.HWaddrLen == netdev->ll_protocol->hw_addr_len );
  509. assert ( db.HWaddrLen == netdev->ll_protocol->ll_addr_len );
  510. /* Extract parameters */
  511. nii->buffer_len = db.MemoryRequired;
  512. nii->mtu = ( db.FrameDataLen + db.MediaHeaderLen );
  513. netdev->max_pkt_len = nii->mtu;
  514. nii->media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED );
  515. return 0;
  516. }
  517. /**
  518. * Initialise UNDI
  519. *
  520. * @v nii NII NIC
  521. * @v flags Flags
  522. * @ret rc Return status code
  523. */
  524. static int nii_initialise_flags ( struct nii_nic *nii, unsigned int flags ) {
  525. PXE_CPB_INITIALIZE cpb;
  526. PXE_DB_INITIALIZE db;
  527. unsigned int op;
  528. int stat;
  529. int rc;
  530. /* Allocate memory buffer */
  531. nii->buffer = umalloc ( nii->buffer_len );
  532. if ( ! nii->buffer ) {
  533. rc = -ENOMEM;
  534. goto err_alloc;
  535. }
  536. /* Construct parameter block */
  537. memset ( &cpb, 0, sizeof ( cpb ) );
  538. cpb.MemoryAddr = ( ( intptr_t ) nii->buffer );
  539. cpb.MemoryLength = nii->buffer_len;
  540. /* Construct data block */
  541. memset ( &db, 0, sizeof ( db ) );
  542. /* Issue command */
  543. op = NII_OP ( PXE_OPCODE_INITIALIZE, flags );
  544. if ( ( stat = nii_issue_cpb_db ( nii, op, &cpb, sizeof ( cpb ),
  545. &db, sizeof ( db ) ) ) < 0 ) {
  546. rc = -EIO_STAT ( stat );
  547. DBGC ( nii, "NII %s could not initialise: %s\n",
  548. nii->dev.name, strerror ( rc ) );
  549. goto err_initialize;
  550. }
  551. return 0;
  552. err_initialize:
  553. ufree ( nii->buffer );
  554. err_alloc:
  555. return rc;
  556. }
  557. /**
  558. * Initialise UNDI
  559. *
  560. * @v nii NII NIC
  561. * @ret rc Return status code
  562. */
  563. static int nii_initialise ( struct nii_nic *nii ) {
  564. unsigned int flags;
  565. /* Initialise UNDI */
  566. flags = PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE;
  567. return nii_initialise_flags ( nii, flags );
  568. }
  569. /**
  570. * Initialise UNDI and detect cable
  571. *
  572. * @v nii NII NIC
  573. * @ret rc Return status code
  574. */
  575. static int nii_initialise_and_detect ( struct nii_nic *nii ) {
  576. unsigned int flags;
  577. /* Initialise UNDI and detect cable. This is required to work
  578. * around bugs in some Emulex NII drivers.
  579. */
  580. flags = PXE_OPFLAGS_INITIALIZE_DETECT_CABLE;
  581. return nii_initialise_flags ( nii, flags );
  582. }
  583. /**
  584. * Shut down UNDI
  585. *
  586. * @v nii NII NIC
  587. */
  588. static void nii_shutdown ( struct nii_nic *nii ) {
  589. int stat;
  590. int rc;
  591. /* Issue command */
  592. if ( ( stat = nii_issue ( nii, PXE_OPCODE_SHUTDOWN ) ) < 0 ) {
  593. rc = -EIO_STAT ( stat );
  594. DBGC ( nii, "NII %s could not shut down: %s\n",
  595. nii->dev.name, strerror ( rc ) );
  596. /* Leak memory to avoid corruption */
  597. return;
  598. }
  599. /* Free buffer */
  600. ufree ( nii->buffer );
  601. }
  602. /**
  603. * Get station addresses
  604. *
  605. * @v nii NII NIC
  606. * @v netdev Network device to fill in
  607. * @ret rc Return status code
  608. */
  609. static int nii_get_station_address ( struct nii_nic *nii,
  610. struct net_device *netdev ) {
  611. PXE_DB_STATION_ADDRESS db;
  612. unsigned int op;
  613. int stat;
  614. int rc;
  615. /* Initialise UNDI */
  616. if ( ( rc = nii_initialise ( nii ) ) != 0 )
  617. goto err_initialise;
  618. /* Issue command */
  619. op = NII_OP ( PXE_OPCODE_STATION_ADDRESS,
  620. PXE_OPFLAGS_STATION_ADDRESS_READ );
  621. if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
  622. rc = -EIO_STAT ( stat );
  623. DBGC ( nii, "NII %s could not get station address: %s\n",
  624. nii->dev.name, strerror ( rc ) );
  625. goto err_station_address;
  626. }
  627. /* Copy MAC addresses */
  628. memcpy ( netdev->ll_addr, db.StationAddr,
  629. netdev->ll_protocol->ll_addr_len );
  630. memcpy ( netdev->hw_addr, db.PermanentAddr,
  631. netdev->ll_protocol->hw_addr_len );
  632. memcpy ( nii->broadcast, db.BroadcastAddr,
  633. sizeof ( nii->broadcast ) );
  634. err_station_address:
  635. nii_shutdown ( nii );
  636. err_initialise:
  637. return rc;
  638. }
  639. /**
  640. * Set station address
  641. *
  642. * @v nii NII NIC
  643. * @v netdev Network device
  644. * @ret rc Return status code
  645. */
  646. static int nii_set_station_address ( struct nii_nic *nii,
  647. struct net_device *netdev ) {
  648. uint32_t implementation = nii->undi->Implementation;
  649. PXE_CPB_STATION_ADDRESS cpb;
  650. unsigned int op;
  651. int stat;
  652. int rc;
  653. /* Fail if setting station address is unsupported */
  654. if ( ! ( implementation & PXE_ROMID_IMP_STATION_ADDR_SETTABLE ) )
  655. return -ENOTSUP;
  656. /* Construct parameter block */
  657. memset ( &cpb, 0, sizeof ( cpb ) );
  658. memcpy ( cpb.StationAddr, netdev->ll_addr,
  659. netdev->ll_protocol->ll_addr_len );
  660. /* Issue command */
  661. op = NII_OP ( PXE_OPCODE_STATION_ADDRESS,
  662. PXE_OPFLAGS_STATION_ADDRESS_WRITE );
  663. if ( ( stat = nii_issue_cpb ( nii, op, &cpb, sizeof ( cpb ) ) ) < 0 ) {
  664. rc = -EIO_STAT ( stat );
  665. DBGC ( nii, "NII %s could not set station address: %s\n",
  666. nii->dev.name, strerror ( rc ) );
  667. return rc;
  668. }
  669. return 0;
  670. }
  671. /**
  672. * Set receive filters
  673. *
  674. * @v nii NII NIC
  675. * @ret rc Return status code
  676. */
  677. static int nii_set_rx_filters ( struct nii_nic *nii ) {
  678. uint32_t implementation = nii->undi->Implementation;
  679. unsigned int flags;
  680. unsigned int op;
  681. int stat;
  682. int rc;
  683. /* Construct receive filter set */
  684. flags = ( PXE_OPFLAGS_RECEIVE_FILTER_ENABLE |
  685. PXE_OPFLAGS_RECEIVE_FILTER_UNICAST );
  686. if ( implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED )
  687. flags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
  688. if ( implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED )
  689. flags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS;
  690. if ( implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED )
  691. flags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
  692. /* Issue command */
  693. op = NII_OP ( PXE_OPCODE_RECEIVE_FILTERS, flags );
  694. if ( ( stat = nii_issue ( nii, op ) ) < 0 ) {
  695. rc = -EIO_STAT ( stat );
  696. DBGC ( nii, "NII %s could not set receive filters %#04x: %s\n",
  697. nii->dev.name, flags, strerror ( rc ) );
  698. return rc;
  699. }
  700. return 0;
  701. }
  702. /**
  703. * Transmit packet
  704. *
  705. * @v netdev Network device
  706. * @v iobuf I/O buffer
  707. * @ret rc Return status code
  708. */
  709. static int nii_transmit ( struct net_device *netdev,
  710. struct io_buffer *iobuf ) {
  711. struct nii_nic *nii = netdev->priv;
  712. PXE_CPB_TRANSMIT cpb;
  713. unsigned int op;
  714. int stat;
  715. int rc;
  716. /* Defer the packet if there is already a transmission in progress */
  717. if ( nii->txbuf ) {
  718. netdev_tx_defer ( netdev, iobuf );
  719. return 0;
  720. }
  721. /* Construct parameter block */
  722. memset ( &cpb, 0, sizeof ( cpb ) );
  723. cpb.FrameAddr = virt_to_bus ( iobuf->data );
  724. cpb.DataLen = iob_len ( iobuf );
  725. cpb.MediaheaderLen = netdev->ll_protocol->ll_header_len;
  726. /* Transmit packet */
  727. op = NII_OP ( PXE_OPCODE_TRANSMIT,
  728. ( PXE_OPFLAGS_TRANSMIT_WHOLE |
  729. PXE_OPFLAGS_TRANSMIT_DONT_BLOCK ) );
  730. if ( ( stat = nii_issue_cpb ( nii, op, &cpb, sizeof ( cpb ) ) ) < 0 ) {
  731. rc = -EIO_STAT ( stat );
  732. DBGC ( nii, "NII %s could not transmit: %s\n",
  733. nii->dev.name, strerror ( rc ) );
  734. return rc;
  735. }
  736. nii->txbuf = iobuf;
  737. return 0;
  738. }
  739. /**
  740. * Poll for completed packets
  741. *
  742. * @v netdev Network device
  743. * @v stat Status flags
  744. */
  745. static void nii_poll_tx ( struct net_device *netdev, unsigned int stat ) {
  746. struct nii_nic *nii = netdev->priv;
  747. struct io_buffer *iobuf;
  748. /* Do nothing unless we have a completion */
  749. if ( stat & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN )
  750. return;
  751. /* Sanity check */
  752. assert ( nii->txbuf != NULL );
  753. /* Complete transmission */
  754. iobuf = nii->txbuf;
  755. nii->txbuf = NULL;
  756. netdev_tx_complete ( netdev, iobuf );
  757. }
  758. /**
  759. * Poll for received packets
  760. *
  761. * @v netdev Network device
  762. */
  763. static void nii_poll_rx ( struct net_device *netdev ) {
  764. struct nii_nic *nii = netdev->priv;
  765. PXE_CPB_RECEIVE cpb;
  766. PXE_DB_RECEIVE db;
  767. unsigned int quota;
  768. int stat;
  769. int rc;
  770. /* Retrieve up to NII_RX_QUOTA packets */
  771. for ( quota = NII_RX_QUOTA ; quota ; quota-- ) {
  772. /* Allocate buffer, if required */
  773. if ( ! nii->rxbuf ) {
  774. nii->rxbuf = alloc_iob ( nii->mtu );
  775. if ( ! nii->rxbuf ) {
  776. /* Leave for next poll */
  777. break;
  778. }
  779. }
  780. /* Construct parameter block */
  781. memset ( &cpb, 0, sizeof ( cpb ) );
  782. cpb.BufferAddr = virt_to_bus ( nii->rxbuf->data );
  783. cpb.BufferLen = iob_tailroom ( nii->rxbuf );
  784. /* Issue command */
  785. if ( ( stat = nii_issue_cpb_db ( nii, PXE_OPCODE_RECEIVE,
  786. &cpb, sizeof ( cpb ),
  787. &db, sizeof ( db ) ) ) < 0 ) {
  788. /* PXE_STATCODE_NO_DATA is just the usual "no packet"
  789. * status indicator; ignore it.
  790. */
  791. if ( stat == -PXE_STATCODE_NO_DATA )
  792. break;
  793. /* Anything else is an error */
  794. rc = -EIO_STAT ( stat );
  795. DBGC ( nii, "NII %s could not receive: %s\n",
  796. nii->dev.name, strerror ( rc ) );
  797. netdev_rx_err ( netdev, NULL, rc );
  798. break;
  799. }
  800. /* Hand off to network stack */
  801. iob_put ( nii->rxbuf, db.FrameLen );
  802. netdev_rx ( netdev, nii->rxbuf );
  803. nii->rxbuf = NULL;
  804. }
  805. }
  806. /**
  807. * Check for link state changes
  808. *
  809. * @v netdev Network device
  810. * @v stat Status flags
  811. */
  812. static void nii_poll_link ( struct net_device *netdev, unsigned int stat ) {
  813. int no_media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA );
  814. if ( no_media && netdev_link_ok ( netdev ) ) {
  815. netdev_link_down ( netdev );
  816. } else if ( ( ! no_media ) && ( ! netdev_link_ok ( netdev ) ) ) {
  817. netdev_link_up ( netdev );
  818. }
  819. }
  820. /**
  821. * Poll for completed packets
  822. *
  823. * @v netdev Network device
  824. */
  825. static void nii_poll ( struct net_device *netdev ) {
  826. struct nii_nic *nii = netdev->priv;
  827. PXE_DB_GET_STATUS db;
  828. unsigned int op;
  829. int stat;
  830. int rc;
  831. /* Construct data block */
  832. memset ( &db, 0, sizeof ( db ) );
  833. /* Get status */
  834. op = NII_OP ( PXE_OPCODE_GET_STATUS,
  835. ( PXE_OPFLAGS_GET_INTERRUPT_STATUS |
  836. ( nii->txbuf ? PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS : 0)|
  837. ( nii->media ? PXE_OPFLAGS_GET_MEDIA_STATUS : 0 ) ) );
  838. if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
  839. rc = -EIO_STAT ( stat );
  840. DBGC ( nii, "NII %s could not get status: %s\n",
  841. nii->dev.name, strerror ( rc ) );
  842. return;
  843. }
  844. /* Process any TX completions */
  845. if ( nii->txbuf )
  846. nii_poll_tx ( netdev, stat );
  847. /* Process any RX completions */
  848. nii_poll_rx ( netdev );
  849. /* Check for link state changes */
  850. if ( nii->media )
  851. nii_poll_link ( netdev, stat );
  852. }
  853. /**
  854. * Open network device
  855. *
  856. * @v netdev Network device
  857. * @ret rc Return status code
  858. */
  859. static int nii_open ( struct net_device *netdev ) {
  860. struct nii_nic *nii = netdev->priv;
  861. int rc;
  862. /* Initialise NIC
  863. *
  864. * Some Emulex NII drivers have a bug which prevents packets
  865. * from being sent or received unless we specifically ask it
  866. * to detect cable presence during initialisation. Work
  867. * around these buggy drivers by requesting cable detection at
  868. * this point, even though we don't care about link state here
  869. * (and would prefer to have the NIC initialise even if no
  870. * cable is present, to match the behaviour of all other iPXE
  871. * drivers).
  872. */
  873. if ( ( rc = nii_initialise_and_detect ( nii ) ) != 0 )
  874. goto err_initialise;
  875. /* Attempt to set station address */
  876. if ( ( rc = nii_set_station_address ( nii, netdev ) ) != 0 ) {
  877. DBGC ( nii, "NII %s could not set station address: %s\n",
  878. nii->dev.name, strerror ( rc ) );
  879. /* Treat as non-fatal */
  880. }
  881. /* Set receive filters */
  882. if ( ( rc = nii_set_rx_filters ( nii ) ) != 0 )
  883. goto err_set_rx_filters;
  884. return 0;
  885. err_set_rx_filters:
  886. nii_shutdown ( nii );
  887. err_initialise:
  888. return rc;
  889. }
  890. /**
  891. * Close network device
  892. *
  893. * @v netdev Network device
  894. */
  895. static void nii_close ( struct net_device *netdev ) {
  896. struct nii_nic *nii = netdev->priv;
  897. /* Shut down NIC */
  898. nii_shutdown ( nii );
  899. /* Discard transmit buffer, if applicable */
  900. if ( nii->txbuf ) {
  901. netdev_tx_complete_err ( netdev, nii->txbuf, -ECANCELED );
  902. nii->txbuf = NULL;
  903. }
  904. /* Discard receive buffer, if applicable */
  905. if ( nii->rxbuf ) {
  906. free_iob ( nii->rxbuf );
  907. nii->rxbuf = NULL;
  908. }
  909. }
  910. /** NII network device operations */
  911. static struct net_device_operations nii_operations = {
  912. .open = nii_open,
  913. .close = nii_close,
  914. .transmit = nii_transmit,
  915. .poll = nii_poll,
  916. };
  917. /**
  918. * Attach driver to device
  919. *
  920. * @v efidev EFI device
  921. * @ret rc Return status code
  922. */
  923. int nii_start ( struct efi_device *efidev ) {
  924. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  925. EFI_HANDLE device = efidev->device;
  926. struct net_device *netdev;
  927. struct nii_nic *nii;
  928. void *interface;
  929. EFI_STATUS efirc;
  930. int rc;
  931. /* Allocate and initialise structure */
  932. netdev = alloc_netdev ( sizeof ( *nii ) );
  933. if ( ! netdev ) {
  934. rc = -ENOMEM;
  935. goto err_alloc;
  936. }
  937. netdev_init ( netdev, &nii_operations );
  938. nii = netdev->priv;
  939. nii->efidev = efidev;
  940. netdev->ll_broadcast = nii->broadcast;
  941. efidev_set_drvdata ( efidev, netdev );
  942. /* Populate underlying device information */
  943. efi_device_info ( device, "NII", &nii->dev );
  944. nii->dev.driver_name = "NII";
  945. nii->dev.parent = &efidev->dev;
  946. list_add ( &nii->dev.siblings, &efidev->dev.children );
  947. INIT_LIST_HEAD ( &nii->dev.children );
  948. netdev->dev = &nii->dev;
  949. /* Open NII protocol */
  950. if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid,
  951. &interface, efi_image_handle, device,
  952. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  953. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  954. rc = -EEFI ( efirc );
  955. DBGC ( nii, "NII %s cannot open NII protocol: %s\n",
  956. nii->dev.name, strerror ( rc ) );
  957. DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid );
  958. goto err_open_protocol;
  959. }
  960. nii->nii = interface;
  961. /* Locate UNDI and entry point */
  962. nii->undi = ( ( void * ) ( intptr_t ) nii->nii->Id );
  963. if ( ! nii->undi ) {
  964. DBGC ( nii, "NII %s has no UNDI\n", nii->dev.name );
  965. rc = -ENODEV;
  966. goto err_no_undi;
  967. }
  968. if ( nii->undi->Implementation & PXE_ROMID_IMP_HW_UNDI ) {
  969. DBGC ( nii, "NII %s is a mythical hardware UNDI\n",
  970. nii->dev.name );
  971. rc = -ENOTSUP;
  972. goto err_hw_undi;
  973. }
  974. if ( nii->undi->Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR ) {
  975. nii->issue = ( ( void * ) ( intptr_t ) nii->undi->EntryPoint );
  976. } else {
  977. nii->issue = ( ( ( void * ) nii->undi ) +
  978. nii->undi->EntryPoint );
  979. }
  980. DBGC ( nii, "NII %s using UNDI v%x.%x at %p entry %p impl %#08x\n",
  981. nii->dev.name, nii->nii->MajorVer, nii->nii->MinorVer,
  982. nii->undi, nii->issue, nii->undi->Implementation );
  983. /* Open PCI I/O protocols and locate BARs */
  984. if ( ( rc = nii_pci_open ( nii ) ) != 0 )
  985. goto err_pci_open;
  986. /* Start UNDI */
  987. if ( ( rc = nii_start_undi ( nii ) ) != 0 )
  988. goto err_start_undi;
  989. /* Get initialisation information */
  990. if ( ( rc = nii_get_init_info ( nii, netdev ) ) != 0 )
  991. goto err_get_init_info;
  992. /* Get MAC addresses */
  993. if ( ( rc = nii_get_station_address ( nii, netdev ) ) != 0 )
  994. goto err_get_station_address;
  995. /* Register network device */
  996. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  997. goto err_register_netdev;
  998. DBGC ( nii, "NII %s registered as %s for %p %s\n", nii->dev.name,
  999. netdev->name, device, efi_handle_name ( device ) );
  1000. /* Set initial link state (if media detection is not supported) */
  1001. if ( ! nii->media )
  1002. netdev_link_up ( netdev );
  1003. return 0;
  1004. unregister_netdev ( netdev );
  1005. err_register_netdev:
  1006. err_get_station_address:
  1007. err_get_init_info:
  1008. nii_stop_undi ( nii );
  1009. err_start_undi:
  1010. nii_pci_close ( nii );
  1011. err_pci_open:
  1012. err_hw_undi:
  1013. err_no_undi:
  1014. bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
  1015. efi_image_handle, device );
  1016. err_open_protocol:
  1017. list_del ( &nii->dev.siblings );
  1018. netdev_nullify ( netdev );
  1019. netdev_put ( netdev );
  1020. err_alloc:
  1021. return rc;
  1022. }
  1023. /**
  1024. * Detach driver from device
  1025. *
  1026. * @v efidev EFI device
  1027. */
  1028. void nii_stop ( struct efi_device *efidev ) {
  1029. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1030. struct net_device *netdev = efidev_get_drvdata ( efidev );
  1031. struct nii_nic *nii = netdev->priv;
  1032. EFI_HANDLE device = efidev->device;
  1033. /* Unregister network device */
  1034. unregister_netdev ( netdev );
  1035. /* Stop UNDI */
  1036. nii_stop_undi ( nii );
  1037. /* Close PCI I/O protocols */
  1038. nii_pci_close ( nii );
  1039. /* Close NII protocol */
  1040. bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
  1041. efi_image_handle, device );
  1042. /* Free network device */
  1043. list_del ( &nii->dev.siblings );
  1044. netdev_nullify ( netdev );
  1045. netdev_put ( netdev );
  1046. }