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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. 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 = ( PCI_MAX_BAR - 1 ) ; bar >= 0 ; 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. * Shut down UNDI
  571. *
  572. * @v nii NII NIC
  573. */
  574. static void nii_shutdown ( struct nii_nic *nii ) {
  575. int stat;
  576. int rc;
  577. /* Issue command */
  578. if ( ( stat = nii_issue ( nii, PXE_OPCODE_SHUTDOWN ) ) < 0 ) {
  579. rc = -EIO_STAT ( stat );
  580. DBGC ( nii, "NII %s could not shut down: %s\n",
  581. nii->dev.name, strerror ( rc ) );
  582. /* Leak memory to avoid corruption */
  583. return;
  584. }
  585. /* Free buffer */
  586. ufree ( nii->buffer );
  587. }
  588. /**
  589. * Get station addresses
  590. *
  591. * @v nii NII NIC
  592. * @v netdev Network device to fill in
  593. * @ret rc Return status code
  594. */
  595. static int nii_get_station_address ( struct nii_nic *nii,
  596. struct net_device *netdev ) {
  597. PXE_DB_STATION_ADDRESS db;
  598. unsigned int op;
  599. int stat;
  600. int rc;
  601. /* Initialise UNDI */
  602. if ( ( rc = nii_initialise ( nii ) ) != 0 )
  603. goto err_initialise;
  604. /* Issue command */
  605. op = NII_OP ( PXE_OPCODE_STATION_ADDRESS,
  606. PXE_OPFLAGS_STATION_ADDRESS_READ );
  607. if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
  608. rc = -EIO_STAT ( stat );
  609. DBGC ( nii, "NII %s could not get station address: %s\n",
  610. nii->dev.name, strerror ( rc ) );
  611. goto err_station_address;
  612. }
  613. /* Copy MAC addresses */
  614. memcpy ( netdev->ll_addr, db.StationAddr,
  615. netdev->ll_protocol->ll_addr_len );
  616. memcpy ( netdev->hw_addr, db.PermanentAddr,
  617. netdev->ll_protocol->hw_addr_len );
  618. memcpy ( nii->broadcast, db.BroadcastAddr,
  619. sizeof ( nii->broadcast ) );
  620. err_station_address:
  621. nii_shutdown ( nii );
  622. err_initialise:
  623. return rc;
  624. }
  625. /**
  626. * Set station address
  627. *
  628. * @v nii NII NIC
  629. * @v netdev Network device
  630. * @ret rc Return status code
  631. */
  632. static int nii_set_station_address ( struct nii_nic *nii,
  633. struct net_device *netdev ) {
  634. uint32_t implementation = nii->undi->Implementation;
  635. PXE_CPB_STATION_ADDRESS cpb;
  636. unsigned int op;
  637. int stat;
  638. int rc;
  639. /* Fail if setting station address is unsupported */
  640. if ( ! ( implementation & PXE_ROMID_IMP_STATION_ADDR_SETTABLE ) )
  641. return -ENOTSUP;
  642. /* Construct parameter block */
  643. memset ( &cpb, 0, sizeof ( cpb ) );
  644. memcpy ( cpb.StationAddr, netdev->ll_addr,
  645. netdev->ll_protocol->ll_addr_len );
  646. /* Issue command */
  647. op = NII_OP ( PXE_OPCODE_STATION_ADDRESS,
  648. PXE_OPFLAGS_STATION_ADDRESS_WRITE );
  649. if ( ( stat = nii_issue_cpb ( nii, op, &cpb, sizeof ( cpb ) ) ) < 0 ) {
  650. rc = -EIO_STAT ( stat );
  651. DBGC ( nii, "NII %s could not set station address: %s\n",
  652. nii->dev.name, strerror ( rc ) );
  653. return rc;
  654. }
  655. return 0;
  656. }
  657. /**
  658. * Set receive filters
  659. *
  660. * @v nii NII NIC
  661. * @ret rc Return status code
  662. */
  663. static int nii_set_rx_filters ( struct nii_nic *nii ) {
  664. uint32_t implementation = nii->undi->Implementation;
  665. unsigned int flags;
  666. unsigned int op;
  667. int stat;
  668. int rc;
  669. /* Construct receive filter set */
  670. flags = ( PXE_OPFLAGS_RECEIVE_FILTER_ENABLE |
  671. PXE_OPFLAGS_RECEIVE_FILTER_UNICAST );
  672. if ( implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED )
  673. flags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
  674. if ( implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED )
  675. flags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS;
  676. if ( implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED )
  677. flags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
  678. /* Issue command */
  679. op = NII_OP ( PXE_OPCODE_RECEIVE_FILTERS, flags );
  680. if ( ( stat = nii_issue ( nii, op ) ) < 0 ) {
  681. rc = -EIO_STAT ( stat );
  682. DBGC ( nii, "NII %s could not set receive filters %#04x: %s\n",
  683. nii->dev.name, flags, strerror ( rc ) );
  684. return rc;
  685. }
  686. return 0;
  687. }
  688. /**
  689. * Transmit packet
  690. *
  691. * @v netdev Network device
  692. * @v iobuf I/O buffer
  693. * @ret rc Return status code
  694. */
  695. static int nii_transmit ( struct net_device *netdev,
  696. struct io_buffer *iobuf ) {
  697. struct nii_nic *nii = netdev->priv;
  698. PXE_CPB_TRANSMIT cpb;
  699. unsigned int op;
  700. int stat;
  701. int rc;
  702. /* Defer the packet if there is already a transmission in progress */
  703. if ( nii->txbuf ) {
  704. netdev_tx_defer ( netdev, iobuf );
  705. return 0;
  706. }
  707. /* Construct parameter block */
  708. memset ( &cpb, 0, sizeof ( cpb ) );
  709. cpb.FrameAddr = virt_to_bus ( iobuf->data );
  710. cpb.DataLen = iob_len ( iobuf );
  711. cpb.MediaheaderLen = netdev->ll_protocol->ll_header_len;
  712. /* Transmit packet */
  713. op = NII_OP ( PXE_OPCODE_TRANSMIT,
  714. ( PXE_OPFLAGS_TRANSMIT_WHOLE |
  715. PXE_OPFLAGS_TRANSMIT_DONT_BLOCK ) );
  716. if ( ( stat = nii_issue_cpb ( nii, op, &cpb, sizeof ( cpb ) ) ) < 0 ) {
  717. rc = -EIO_STAT ( stat );
  718. DBGC ( nii, "NII %s could not transmit: %s\n",
  719. nii->dev.name, strerror ( rc ) );
  720. return rc;
  721. }
  722. nii->txbuf = iobuf;
  723. return 0;
  724. }
  725. /**
  726. * Poll for completed packets
  727. *
  728. * @v netdev Network device
  729. * @v stat Status flags
  730. */
  731. static void nii_poll_tx ( struct net_device *netdev, unsigned int stat ) {
  732. struct nii_nic *nii = netdev->priv;
  733. struct io_buffer *iobuf;
  734. /* Do nothing unless we have a completion */
  735. if ( stat & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN )
  736. return;
  737. /* Sanity check */
  738. assert ( nii->txbuf != NULL );
  739. /* Complete transmission */
  740. iobuf = nii->txbuf;
  741. nii->txbuf = NULL;
  742. netdev_tx_complete ( netdev, iobuf );
  743. }
  744. /**
  745. * Poll for received packets
  746. *
  747. * @v netdev Network device
  748. */
  749. static void nii_poll_rx ( struct net_device *netdev ) {
  750. struct nii_nic *nii = netdev->priv;
  751. PXE_CPB_RECEIVE cpb;
  752. PXE_DB_RECEIVE db;
  753. unsigned int quota;
  754. int stat;
  755. int rc;
  756. /* Retrieve up to NII_RX_QUOTA packets */
  757. for ( quota = NII_RX_QUOTA ; quota ; quota-- ) {
  758. /* Allocate buffer, if required */
  759. if ( ! nii->rxbuf ) {
  760. nii->rxbuf = alloc_iob ( nii->mtu );
  761. if ( ! nii->rxbuf ) {
  762. /* Leave for next poll */
  763. break;
  764. }
  765. }
  766. /* Construct parameter block */
  767. memset ( &cpb, 0, sizeof ( cpb ) );
  768. cpb.BufferAddr = virt_to_bus ( nii->rxbuf->data );
  769. cpb.BufferLen = iob_tailroom ( nii->rxbuf );
  770. /* Issue command */
  771. if ( ( stat = nii_issue_cpb_db ( nii, PXE_OPCODE_RECEIVE,
  772. &cpb, sizeof ( cpb ),
  773. &db, sizeof ( db ) ) ) < 0 ) {
  774. /* PXE_STATCODE_NO_DATA is just the usual "no packet"
  775. * status indicator; ignore it.
  776. */
  777. if ( stat == -PXE_STATCODE_NO_DATA )
  778. break;
  779. /* Anything else is an error */
  780. rc = -EIO_STAT ( stat );
  781. DBGC ( nii, "NII %s could not receive: %s\n",
  782. nii->dev.name, strerror ( rc ) );
  783. netdev_rx_err ( netdev, NULL, rc );
  784. break;
  785. }
  786. /* Hand off to network stack */
  787. iob_put ( nii->rxbuf, db.FrameLen );
  788. netdev_rx ( netdev, nii->rxbuf );
  789. nii->rxbuf = NULL;
  790. }
  791. }
  792. /**
  793. * Check for link state changes
  794. *
  795. * @v netdev Network device
  796. * @v stat Status flags
  797. */
  798. static void nii_poll_link ( struct net_device *netdev, unsigned int stat ) {
  799. int no_media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA );
  800. if ( no_media && netdev_link_ok ( netdev ) ) {
  801. netdev_link_down ( netdev );
  802. } else if ( ( ! no_media ) && ( ! netdev_link_ok ( netdev ) ) ) {
  803. netdev_link_up ( netdev );
  804. }
  805. }
  806. /**
  807. * Poll for completed packets
  808. *
  809. * @v netdev Network device
  810. */
  811. static void nii_poll ( struct net_device *netdev ) {
  812. struct nii_nic *nii = netdev->priv;
  813. PXE_DB_GET_STATUS db;
  814. unsigned int op;
  815. int stat;
  816. int rc;
  817. /* Construct data block */
  818. memset ( &db, 0, sizeof ( db ) );
  819. /* Get status */
  820. op = NII_OP ( PXE_OPCODE_GET_STATUS,
  821. ( PXE_OPFLAGS_GET_INTERRUPT_STATUS |
  822. ( nii->txbuf ? PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS : 0)|
  823. ( nii->media ? PXE_OPFLAGS_GET_MEDIA_STATUS : 0 ) ) );
  824. if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
  825. rc = -EIO_STAT ( stat );
  826. DBGC ( nii, "NII %s could not get status: %s\n",
  827. nii->dev.name, strerror ( rc ) );
  828. return;
  829. }
  830. /* Process any TX completions */
  831. if ( nii->txbuf )
  832. nii_poll_tx ( netdev, stat );
  833. /* Process any RX completions */
  834. nii_poll_rx ( netdev );
  835. /* Check for link state changes */
  836. if ( nii->media )
  837. nii_poll_link ( netdev, stat );
  838. }
  839. /**
  840. * Open network device
  841. *
  842. * @v netdev Network device
  843. * @ret rc Return status code
  844. */
  845. static int nii_open ( struct net_device *netdev ) {
  846. struct nii_nic *nii = netdev->priv;
  847. unsigned int flags;
  848. int rc;
  849. /* Initialise NIC
  850. *
  851. * We don't care about link state here, and would prefer to
  852. * have the NIC initialise even if no cable is present, to
  853. * match the behaviour of all other iPXE drivers.
  854. *
  855. * Some Emulex NII drivers have a bug which prevents packets
  856. * from being sent or received unless we specifically ask it
  857. * to detect cable presence during initialisation.
  858. *
  859. * Unfortunately, some other NII drivers (e.g. Mellanox) may
  860. * time out and report failure if asked to detect cable
  861. * presence during initialisation on links that are physically
  862. * slow to reach link-up.
  863. *
  864. * Attempt to work around both of these problems by requesting
  865. * cable detection at this point if any only if the driver is
  866. * not capable of reporting link status changes at runtime via
  867. * PXE_OPCODE_GET_STATUS.
  868. */
  869. flags = ( nii->media ? PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE
  870. : PXE_OPFLAGS_INITIALIZE_DETECT_CABLE );
  871. if ( ( rc = nii_initialise_flags ( nii, flags ) ) != 0 )
  872. goto err_initialise;
  873. /* Attempt to set station address */
  874. if ( ( rc = nii_set_station_address ( nii, netdev ) ) != 0 ) {
  875. DBGC ( nii, "NII %s could not set station address: %s\n",
  876. nii->dev.name, strerror ( rc ) );
  877. /* Treat as non-fatal */
  878. }
  879. /* Set receive filters */
  880. if ( ( rc = nii_set_rx_filters ( nii ) ) != 0 )
  881. goto err_set_rx_filters;
  882. return 0;
  883. err_set_rx_filters:
  884. nii_shutdown ( nii );
  885. err_initialise:
  886. return rc;
  887. }
  888. /**
  889. * Close network device
  890. *
  891. * @v netdev Network device
  892. */
  893. static void nii_close ( struct net_device *netdev ) {
  894. struct nii_nic *nii = netdev->priv;
  895. /* Shut down NIC */
  896. nii_shutdown ( nii );
  897. /* Discard transmit buffer, if applicable */
  898. if ( nii->txbuf ) {
  899. netdev_tx_complete_err ( netdev, nii->txbuf, -ECANCELED );
  900. nii->txbuf = NULL;
  901. }
  902. /* Discard receive buffer, if applicable */
  903. if ( nii->rxbuf ) {
  904. free_iob ( nii->rxbuf );
  905. nii->rxbuf = NULL;
  906. }
  907. }
  908. /** NII network device operations */
  909. static struct net_device_operations nii_operations = {
  910. .open = nii_open,
  911. .close = nii_close,
  912. .transmit = nii_transmit,
  913. .poll = nii_poll,
  914. };
  915. /**
  916. * Attach driver to device
  917. *
  918. * @v efidev EFI device
  919. * @ret rc Return status code
  920. */
  921. int nii_start ( struct efi_device *efidev ) {
  922. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  923. EFI_HANDLE device = efidev->device;
  924. struct net_device *netdev;
  925. struct nii_nic *nii;
  926. void *interface;
  927. EFI_STATUS efirc;
  928. int rc;
  929. /* Allocate and initialise structure */
  930. netdev = alloc_netdev ( sizeof ( *nii ) );
  931. if ( ! netdev ) {
  932. rc = -ENOMEM;
  933. goto err_alloc;
  934. }
  935. netdev_init ( netdev, &nii_operations );
  936. nii = netdev->priv;
  937. nii->efidev = efidev;
  938. netdev->ll_broadcast = nii->broadcast;
  939. efidev_set_drvdata ( efidev, netdev );
  940. /* Populate underlying device information */
  941. efi_device_info ( device, "NII", &nii->dev );
  942. nii->dev.driver_name = "NII";
  943. nii->dev.parent = &efidev->dev;
  944. list_add ( &nii->dev.siblings, &efidev->dev.children );
  945. INIT_LIST_HEAD ( &nii->dev.children );
  946. netdev->dev = &nii->dev;
  947. /* Open NII protocol */
  948. if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid,
  949. &interface, efi_image_handle, device,
  950. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  951. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  952. rc = -EEFI ( efirc );
  953. DBGC ( nii, "NII %s cannot open NII protocol: %s\n",
  954. nii->dev.name, strerror ( rc ) );
  955. DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid );
  956. goto err_open_protocol;
  957. }
  958. nii->nii = interface;
  959. /* Locate UNDI and entry point */
  960. nii->undi = ( ( void * ) ( intptr_t ) nii->nii->Id );
  961. if ( ! nii->undi ) {
  962. DBGC ( nii, "NII %s has no UNDI\n", nii->dev.name );
  963. rc = -ENODEV;
  964. goto err_no_undi;
  965. }
  966. if ( nii->undi->Implementation & PXE_ROMID_IMP_HW_UNDI ) {
  967. DBGC ( nii, "NII %s is a mythical hardware UNDI\n",
  968. nii->dev.name );
  969. rc = -ENOTSUP;
  970. goto err_hw_undi;
  971. }
  972. if ( nii->undi->Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR ) {
  973. nii->issue = ( ( void * ) ( intptr_t ) nii->undi->EntryPoint );
  974. } else {
  975. nii->issue = ( ( ( void * ) nii->undi ) +
  976. nii->undi->EntryPoint );
  977. }
  978. DBGC ( nii, "NII %s using UNDI v%x.%x at %p entry %p impl %#08x\n",
  979. nii->dev.name, nii->nii->MajorVer, nii->nii->MinorVer,
  980. nii->undi, nii->issue, nii->undi->Implementation );
  981. /* Open PCI I/O protocols and locate BARs */
  982. if ( ( rc = nii_pci_open ( nii ) ) != 0 )
  983. goto err_pci_open;
  984. /* Start UNDI */
  985. if ( ( rc = nii_start_undi ( nii ) ) != 0 )
  986. goto err_start_undi;
  987. /* Get initialisation information */
  988. if ( ( rc = nii_get_init_info ( nii, netdev ) ) != 0 )
  989. goto err_get_init_info;
  990. /* Get MAC addresses */
  991. if ( ( rc = nii_get_station_address ( nii, netdev ) ) != 0 )
  992. goto err_get_station_address;
  993. /* Register network device */
  994. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  995. goto err_register_netdev;
  996. DBGC ( nii, "NII %s registered as %s for %s\n", nii->dev.name,
  997. netdev->name, efi_handle_name ( device ) );
  998. /* Set initial link state (if media detection is not supported) */
  999. if ( ! nii->media )
  1000. netdev_link_up ( netdev );
  1001. return 0;
  1002. unregister_netdev ( netdev );
  1003. err_register_netdev:
  1004. err_get_station_address:
  1005. err_get_init_info:
  1006. nii_stop_undi ( nii );
  1007. err_start_undi:
  1008. nii_pci_close ( nii );
  1009. err_pci_open:
  1010. err_hw_undi:
  1011. err_no_undi:
  1012. bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
  1013. efi_image_handle, device );
  1014. err_open_protocol:
  1015. list_del ( &nii->dev.siblings );
  1016. netdev_nullify ( netdev );
  1017. netdev_put ( netdev );
  1018. err_alloc:
  1019. return rc;
  1020. }
  1021. /**
  1022. * Detach driver from device
  1023. *
  1024. * @v efidev EFI device
  1025. */
  1026. void nii_stop ( struct efi_device *efidev ) {
  1027. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1028. struct net_device *netdev = efidev_get_drvdata ( efidev );
  1029. struct nii_nic *nii = netdev->priv;
  1030. EFI_HANDLE device = efidev->device;
  1031. /* Unregister network device */
  1032. unregister_netdev ( netdev );
  1033. /* Stop UNDI */
  1034. nii_stop_undi ( nii );
  1035. /* Close PCI I/O protocols */
  1036. nii_pci_close ( nii );
  1037. /* Close NII protocol */
  1038. bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
  1039. efi_image_handle, device );
  1040. /* Free network device */
  1041. list_del ( &nii->dev.siblings );
  1042. netdev_nullify ( netdev );
  1043. netdev_put ( netdev );
  1044. }