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.

nii.c 29KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. /** Current transmit buffer */
  165. struct io_buffer *txbuf;
  166. /** Current receive buffer */
  167. struct io_buffer *rxbuf;
  168. };
  169. /** Maximum number of received packets per poll */
  170. #define NII_RX_QUOTA 4
  171. /**
  172. * Open PCI I/O protocol and identify BARs
  173. *
  174. * @v nii NII NIC
  175. * @ret rc Return status code
  176. */
  177. static int nii_pci_open ( struct nii_nic *nii ) {
  178. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  179. EFI_HANDLE device = nii->efidev->device;
  180. EFI_HANDLE pci_device;
  181. union {
  182. EFI_PCI_IO_PROTOCOL *pci_io;
  183. void *interface;
  184. } pci_io;
  185. union {
  186. EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *acpi;
  187. void *resource;
  188. } desc;
  189. unsigned int bar;
  190. EFI_STATUS efirc;
  191. int rc;
  192. /* Locate PCI I/O protocol */
  193. if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid,
  194. &pci_device ) ) != 0 ) {
  195. DBGC ( nii, "NII %s could not locate PCI I/O protocol: %s\n",
  196. nii->dev.name, strerror ( rc ) );
  197. goto err_locate;
  198. }
  199. nii->pci_device = pci_device;
  200. /* Open PCI I/O protocol */
  201. if ( ( efirc = bs->OpenProtocol ( pci_device, &efi_pci_io_protocol_guid,
  202. &pci_io.interface, efi_image_handle,
  203. device,
  204. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  205. rc = -EEFI ( efirc );
  206. DBGC ( nii, "NII %s could not open PCI I/O protocol: %s\n",
  207. nii->dev.name, strerror ( rc ) );
  208. goto err_open;
  209. }
  210. nii->pci_io = pci_io.pci_io;
  211. /* Identify memory and I/O BARs */
  212. nii->mem_bar = PCI_MAX_BAR;
  213. nii->io_bar = PCI_MAX_BAR;
  214. for ( bar = 0 ; bar < PCI_MAX_BAR ; bar++ ) {
  215. efirc = nii->pci_io->GetBarAttributes ( nii->pci_io, bar, NULL,
  216. &desc.resource );
  217. if ( efirc == EFI_UNSUPPORTED ) {
  218. /* BAR not present; ignore */
  219. continue;
  220. }
  221. if ( efirc != 0 ) {
  222. rc = -EEFI ( efirc );
  223. DBGC ( nii, "NII %s could not get BAR %d attributes: "
  224. "%s\n", nii->dev.name, bar, strerror ( rc ) );
  225. goto err_get_bar_attributes;
  226. }
  227. if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM ) {
  228. nii->mem_bar = bar;
  229. } else if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_IO ) {
  230. nii->io_bar = bar;
  231. }
  232. bs->FreePool ( desc.resource );
  233. }
  234. DBGC ( nii, "NII %s has ", nii->dev.name );
  235. if ( nii->mem_bar < PCI_MAX_BAR ) {
  236. DBGC ( nii, "memory BAR %d and ", nii->mem_bar );
  237. } else {
  238. DBGC ( nii, "no memory BAR and " );
  239. }
  240. if ( nii->io_bar < PCI_MAX_BAR ) {
  241. DBGC ( nii, "I/O BAR %d\n", nii->io_bar );
  242. } else {
  243. DBGC ( nii, "no I/O BAR\n" );
  244. }
  245. return 0;
  246. err_get_bar_attributes:
  247. bs->CloseProtocol ( pci_device, &efi_pci_io_protocol_guid,
  248. efi_image_handle, device );
  249. err_open:
  250. err_locate:
  251. return rc;
  252. }
  253. /**
  254. * Close PCI I/O protocol
  255. *
  256. * @v nii NII NIC
  257. * @ret rc Return status code
  258. */
  259. static void nii_pci_close ( struct nii_nic *nii ) {
  260. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  261. bs->CloseProtocol ( nii->pci_device, &efi_pci_io_protocol_guid,
  262. efi_image_handle, nii->efidev->device );
  263. }
  264. /**
  265. * I/O callback
  266. *
  267. * @v unique_id NII NIC
  268. * @v op Operations
  269. * @v len Length of data
  270. * @v addr Address
  271. * @v data Data buffer
  272. */
  273. static EFIAPI VOID nii_io ( UINT64 unique_id, UINT8 op, UINT8 len, UINT64 addr,
  274. UINT64 data ) {
  275. struct nii_nic *nii = ( ( void * ) ( intptr_t ) unique_id );
  276. EFI_PCI_IO_PROTOCOL_ACCESS *access;
  277. EFI_PCI_IO_PROTOCOL_IO_MEM io;
  278. EFI_PCI_IO_PROTOCOL_WIDTH width;
  279. unsigned int bar;
  280. EFI_STATUS efirc;
  281. int rc;
  282. /* Determine accessor and BAR */
  283. if ( op & ( PXE_MEM_READ | PXE_MEM_WRITE ) ) {
  284. access = &nii->pci_io->Mem;
  285. bar = nii->mem_bar;
  286. } else {
  287. access = &nii->pci_io->Io;
  288. bar = nii->io_bar;
  289. }
  290. /* Determine operaton */
  291. io = ( ( op & ( PXE_IO_WRITE | PXE_MEM_WRITE ) ) ?
  292. access->Write : access->Read );
  293. /* Determine width */
  294. width = ( fls ( len ) - 1 );
  295. /* Issue operation */
  296. if ( ( efirc = io ( nii->pci_io, width, bar, addr, 1,
  297. ( ( void * ) ( intptr_t ) data ) ) ) != 0 ) {
  298. rc = -EEFI ( efirc );
  299. DBGC ( nii, "NII %s I/O operation %#x failed: %s\n",
  300. nii->dev.name, op, strerror ( rc ) );
  301. /* No way to report failure */
  302. return;
  303. }
  304. }
  305. /**
  306. * Delay callback
  307. *
  308. * @v unique_id NII NIC
  309. * @v microseconds Delay in microseconds
  310. */
  311. static EFIAPI VOID nii_delay ( UINT64 unique_id __unused, UINTN microseconds ) {
  312. udelay ( microseconds );
  313. }
  314. /**
  315. * Block callback
  316. *
  317. * @v unique_id NII NIC
  318. * @v acquire Acquire lock
  319. */
  320. static EFIAPI VOID nii_block ( UINT64 unique_id, UINT32 acquire ) {
  321. struct nii_nic *nii = ( ( void * ) ( intptr_t ) unique_id );
  322. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  323. /* This functionality (which is copied verbatim from the
  324. * SnpDxe implementation of this function) appears to be
  325. * totally brain-dead, since it produces no actual blocking
  326. * behaviour.
  327. */
  328. if ( acquire ) {
  329. nii->saved_tpl = bs->RaiseTPL ( TPL_NOTIFY );
  330. } else {
  331. bs->RestoreTPL ( nii->saved_tpl );
  332. }
  333. }
  334. /**
  335. * Construct operation from opcode and flags
  336. *
  337. * @v opcode Opcode
  338. * @v opflags Flags
  339. * @ret op Operation
  340. */
  341. #define NII_OP( opcode, opflags ) ( (opcode) | ( (opflags) << 16 ) )
  342. /**
  343. * Extract opcode from operation
  344. *
  345. * @v op Operation
  346. * @ret opcode Opcode
  347. */
  348. #define NII_OPCODE( op ) ( (op) & 0xffff )
  349. /**
  350. * Extract flags from operation
  351. *
  352. * @v op Operation
  353. * @ret opflags Flags
  354. */
  355. #define NII_OPFLAGS( op ) ( (op) >> 16 )
  356. /**
  357. * Issue command with parameter block and data block
  358. *
  359. * @v nii NII NIC
  360. * @v op Operation
  361. * @v cpb Command parameter block, or NULL
  362. * @v cpb_len Command parameter block length
  363. * @v db Data block, or NULL
  364. * @v db_len Data block length
  365. * @ret stat Status flags, or negative status code
  366. */
  367. static int nii_issue_cpb_db ( struct nii_nic *nii, unsigned int op, void *cpb,
  368. size_t cpb_len, void *db, size_t db_len ) {
  369. PXE_CDB cdb;
  370. /* Prepare command descriptor block */
  371. memset ( &cdb, 0, sizeof ( cdb ) );
  372. cdb.OpCode = NII_OPCODE ( op );
  373. cdb.OpFlags = NII_OPFLAGS ( op );
  374. cdb.CPBaddr = ( ( intptr_t ) cpb );
  375. cdb.CPBsize = cpb_len;
  376. cdb.DBaddr = ( ( intptr_t ) db );
  377. cdb.DBsize = db_len;
  378. cdb.IFnum = nii->nii->IfNum;
  379. /* Issue command */
  380. nii->issue ( ( intptr_t ) &cdb );
  381. /* Check completion status */
  382. if ( cdb.StatCode != PXE_STATCODE_SUCCESS )
  383. return -cdb.StatCode;
  384. /* Return command-specific status flags */
  385. return ( cdb.StatFlags & ~PXE_STATFLAGS_STATUS_MASK );
  386. }
  387. /**
  388. * Issue command with parameter block
  389. *
  390. * @v nii NII NIC
  391. * @v op Operation
  392. * @v cpb Command parameter block, or NULL
  393. * @v cpb_len Command parameter block length
  394. * @ret stat Status flags, or negative status code
  395. */
  396. static int nii_issue_cpb ( struct nii_nic *nii, unsigned int op, void *cpb,
  397. size_t cpb_len ) {
  398. return nii_issue_cpb_db ( nii, op, cpb, cpb_len, NULL, 0 );
  399. }
  400. /**
  401. * Issue command with data block
  402. *
  403. * @v nii NII NIC
  404. * @v op Operation
  405. * @v db Data block, or NULL
  406. * @v db_len Data block length
  407. * @ret stat Status flags, or negative status code
  408. */
  409. static int nii_issue_db ( struct nii_nic *nii, unsigned int op, void *db,
  410. size_t db_len ) {
  411. return nii_issue_cpb_db ( nii, op, NULL, 0, db, db_len );
  412. }
  413. /**
  414. * Issue command
  415. *
  416. *
  417. * @v nii NII NIC
  418. * @v op Operation
  419. * @ret stat Status flags, or negative status code
  420. */
  421. static int nii_issue ( struct nii_nic *nii, unsigned int op ) {
  422. return nii_issue_cpb_db ( nii, op, NULL, 0, NULL, 0 );
  423. }
  424. /**
  425. * Start UNDI
  426. *
  427. * @v nii NII NIC
  428. * @ret rc Return status code
  429. */
  430. static int nii_start_undi ( struct nii_nic *nii ) {
  431. PXE_CPB_START_31 cpb;
  432. int stat;
  433. int rc;
  434. /* Construct parameter block */
  435. memset ( &cpb, 0, sizeof ( cpb ) );
  436. cpb.Delay = ( ( intptr_t ) nii_delay );
  437. cpb.Block = ( ( intptr_t ) nii_block );
  438. cpb.Mem_IO = ( ( intptr_t ) nii_io );
  439. cpb.Unique_ID = ( ( intptr_t ) nii );
  440. /* Issue command */
  441. if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_START, &cpb,
  442. sizeof ( cpb ) ) ) < 0 ) {
  443. rc = -EIO_STAT ( stat );
  444. DBGC ( nii, "NII %s could not start: %s\n",
  445. nii->dev.name, strerror ( rc ) );
  446. return rc;
  447. }
  448. return 0;
  449. }
  450. /**
  451. * Stop UNDI
  452. *
  453. * @v nii NII NIC
  454. */
  455. static void nii_stop_undi ( struct nii_nic *nii ) {
  456. int stat;
  457. int rc;
  458. /* Issue command */
  459. if ( ( stat = nii_issue ( nii, PXE_OPCODE_STOP ) ) < 0 ) {
  460. rc = -EIO_STAT ( stat );
  461. DBGC ( nii, "NII %s could not stop: %s\n",
  462. nii->dev.name, strerror ( rc ) );
  463. /* Nothing we can do about it */
  464. return;
  465. }
  466. }
  467. /**
  468. * Get initialisation information
  469. *
  470. * @v nii NII NIC
  471. * @v netdev Network device to fill in
  472. * @ret rc Return status code
  473. */
  474. static int nii_get_init_info ( struct nii_nic *nii,
  475. struct net_device *netdev ) {
  476. PXE_DB_GET_INIT_INFO db;
  477. int stat;
  478. int rc;
  479. /* Issue command */
  480. if ( ( stat = nii_issue_db ( nii, PXE_OPCODE_GET_INIT_INFO, &db,
  481. sizeof ( db ) ) ) < 0 ) {
  482. rc = -EIO_STAT ( stat );
  483. DBGC ( nii, "NII %s could not get initialisation info: %s\n",
  484. nii->dev.name, strerror ( rc ) );
  485. return rc;
  486. }
  487. /* Determine link layer protocol */
  488. switch ( db.IFtype ) {
  489. case PXE_IFTYPE_ETHERNET :
  490. netdev->ll_protocol = &ethernet_protocol;
  491. break;
  492. default:
  493. DBGC ( nii, "NII %s unknown interface type %#02x\n",
  494. nii->dev.name, db.IFtype );
  495. return -ENOTSUP;
  496. }
  497. /* Sanity checks */
  498. assert ( db.MediaHeaderLen == netdev->ll_protocol->ll_header_len );
  499. assert ( db.HWaddrLen == netdev->ll_protocol->hw_addr_len );
  500. assert ( db.HWaddrLen == netdev->ll_protocol->ll_addr_len );
  501. /* Extract parameters */
  502. nii->buffer_len = db.MemoryRequired;
  503. nii->mtu = ( db.FrameDataLen + db.MediaHeaderLen );
  504. netdev->max_pkt_len = nii->mtu;
  505. return 0;
  506. }
  507. /**
  508. * Initialise UNDI
  509. *
  510. * @v nii NII NIC
  511. * @ret rc Return status code
  512. */
  513. static int nii_initialise ( struct nii_nic *nii ) {
  514. PXE_CPB_INITIALIZE cpb;
  515. unsigned int op;
  516. int stat;
  517. int rc;
  518. /* Allocate memory buffer */
  519. nii->buffer = umalloc ( nii->buffer_len );
  520. if ( ! nii->buffer ) {
  521. rc = -ENOMEM;
  522. goto err_alloc;
  523. }
  524. /* Construct parameter block */
  525. memset ( &cpb, 0, sizeof ( cpb ) );
  526. cpb.MemoryAddr = ( ( intptr_t ) nii->buffer );
  527. cpb.MemoryLength = nii->buffer_len;
  528. /* Issue command */
  529. op = NII_OP ( PXE_OPCODE_INITIALIZE,
  530. PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE );
  531. if ( ( stat = nii_issue_cpb ( nii, op, &cpb, sizeof ( cpb ) ) ) < 0 ) {
  532. rc = -EIO_STAT ( stat );
  533. DBGC ( nii, "NII %s could not initialise: %s\n",
  534. nii->dev.name, strerror ( rc ) );
  535. goto err_initialize;
  536. }
  537. return 0;
  538. err_initialize:
  539. ufree ( nii->buffer );
  540. err_alloc:
  541. return rc;
  542. }
  543. /**
  544. * Shut down UNDI
  545. *
  546. * @v nii NII NIC
  547. */
  548. static void nii_shutdown ( struct nii_nic *nii ) {
  549. int stat;
  550. int rc;
  551. /* Issue command */
  552. if ( ( stat = nii_issue ( nii, PXE_OPCODE_SHUTDOWN ) ) < 0 ) {
  553. rc = -EIO_STAT ( stat );
  554. DBGC ( nii, "NII %s could not shut down: %s\n",
  555. nii->dev.name, strerror ( rc ) );
  556. /* Leak memory to avoid corruption */
  557. return;
  558. }
  559. /* Free buffer */
  560. ufree ( nii->buffer );
  561. }
  562. /**
  563. * Get station addresses
  564. *
  565. * @v nii NII NIC
  566. * @v netdev Network device to fill in
  567. * @ret rc Return status code
  568. */
  569. static int nii_get_station_address ( struct nii_nic *nii,
  570. struct net_device *netdev ) {
  571. PXE_DB_STATION_ADDRESS db;
  572. int stat;
  573. int rc;
  574. /* Initialise UNDI */
  575. if ( ( rc = nii_initialise ( nii ) ) != 0 )
  576. goto err_initialise;
  577. /* Issue command */
  578. if ( ( stat = nii_issue_db ( nii, PXE_OPCODE_STATION_ADDRESS, &db,
  579. sizeof ( db ) ) ) < 0 ) {
  580. rc = -EIO_STAT ( stat );
  581. DBGC ( nii, "NII %s could not get station address: %s\n",
  582. nii->dev.name, strerror ( rc ) );
  583. goto err_station_address;
  584. }
  585. /* Copy MAC addresses */
  586. memcpy ( netdev->ll_addr, db.StationAddr,
  587. netdev->ll_protocol->ll_addr_len );
  588. memcpy ( netdev->hw_addr, db.PermanentAddr,
  589. netdev->ll_protocol->hw_addr_len );
  590. memcpy ( nii->broadcast, db.BroadcastAddr,
  591. sizeof ( nii->broadcast ) );
  592. err_station_address:
  593. nii_shutdown ( nii );
  594. err_initialise:
  595. return rc;
  596. }
  597. /**
  598. * Set station address
  599. *
  600. * @v nii NII NIC
  601. * @v netdev Network device
  602. * @ret rc Return status code
  603. */
  604. static int nii_set_station_address ( struct nii_nic *nii,
  605. struct net_device *netdev ) {
  606. PXE_CPB_STATION_ADDRESS cpb;
  607. int stat;
  608. int rc;
  609. /* Construct parameter block */
  610. memset ( &cpb, 0, sizeof ( cpb ) );
  611. memcpy ( cpb.StationAddr, netdev->ll_addr,
  612. netdev->ll_protocol->ll_addr_len );
  613. /* Issue command */
  614. if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_STATION_ADDRESS,
  615. &cpb, sizeof ( cpb ) ) ) < 0 ) {
  616. rc = -EIO_STAT ( stat );
  617. DBGC ( nii, "NII %s could not set station address: %s\n",
  618. nii->dev.name, strerror ( rc ) );
  619. return rc;
  620. }
  621. return 0;
  622. }
  623. /**
  624. * Set receive filters
  625. *
  626. * @v nii NII NIC
  627. * @ret rc Return status code
  628. */
  629. static int nii_set_rx_filters ( struct nii_nic *nii ) {
  630. unsigned int op;
  631. int stat;
  632. int rc;
  633. /* Issue command */
  634. op = NII_OP ( PXE_OPCODE_RECEIVE_FILTERS,
  635. ( PXE_OPFLAGS_RECEIVE_FILTER_ENABLE |
  636. PXE_OPFLAGS_RECEIVE_FILTER_UNICAST |
  637. PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST |
  638. PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS |
  639. PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST ) );
  640. if ( ( stat = nii_issue ( nii, op ) ) < 0 ) {
  641. rc = -EIO_STAT ( stat );
  642. DBGC ( nii, "NII %s could not set receive filters: %s\n",
  643. nii->dev.name, strerror ( rc ) );
  644. return rc;
  645. }
  646. return 0;
  647. }
  648. /**
  649. * Transmit packet
  650. *
  651. * @v netdev Network device
  652. * @v iobuf I/O buffer
  653. * @ret rc Return status code
  654. */
  655. static int nii_transmit ( struct net_device *netdev,
  656. struct io_buffer *iobuf ) {
  657. struct nii_nic *nii = netdev->priv;
  658. PXE_CPB_TRANSMIT cpb;
  659. int stat;
  660. int rc;
  661. /* Defer the packet if there is already a transmission in progress */
  662. if ( nii->txbuf ) {
  663. netdev_tx_defer ( netdev, iobuf );
  664. return 0;
  665. }
  666. /* Construct parameter block */
  667. memset ( &cpb, 0, sizeof ( cpb ) );
  668. cpb.FrameAddr = virt_to_bus ( iobuf->data );
  669. cpb.DataLen = iob_len ( iobuf );
  670. cpb.MediaheaderLen = netdev->ll_protocol->ll_header_len;
  671. /* Transmit packet */
  672. if ( ( stat = nii_issue_cpb ( nii, PXE_OPCODE_TRANSMIT, &cpb,
  673. sizeof ( cpb ) ) ) < 0 ) {
  674. rc = -EIO_STAT ( stat );
  675. DBGC ( nii, "NII %s could not transmit: %s\n",
  676. nii->dev.name, strerror ( rc ) );
  677. return rc;
  678. }
  679. nii->txbuf = iobuf;
  680. return 0;
  681. }
  682. /**
  683. * Poll for completed packets
  684. *
  685. * @v netdev Network device
  686. * @v stat Status flags
  687. */
  688. static void nii_poll_tx ( struct net_device *netdev, unsigned int stat ) {
  689. struct nii_nic *nii = netdev->priv;
  690. struct io_buffer *iobuf;
  691. /* Do nothing unless we have a completion */
  692. if ( stat & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN )
  693. return;
  694. /* Sanity check */
  695. if ( ! nii->txbuf ) {
  696. DBGC ( nii, "NII %s reported spurious TX completion\n",
  697. nii->dev.name );
  698. netdev_tx_err ( netdev, NULL, -EPIPE );
  699. return;
  700. }
  701. /* Complete transmission */
  702. iobuf = nii->txbuf;
  703. nii->txbuf = NULL;
  704. netdev_tx_complete ( netdev, iobuf );
  705. }
  706. /**
  707. * Poll for received packets
  708. *
  709. * @v netdev Network device
  710. */
  711. static void nii_poll_rx ( struct net_device *netdev ) {
  712. struct nii_nic *nii = netdev->priv;
  713. PXE_CPB_RECEIVE cpb;
  714. PXE_DB_RECEIVE db;
  715. unsigned int quota;
  716. int stat;
  717. int rc;
  718. /* Retrieve up to NII_RX_QUOTA packets */
  719. for ( quota = NII_RX_QUOTA ; quota ; quota-- ) {
  720. /* Allocate buffer, if required */
  721. if ( ! nii->rxbuf ) {
  722. nii->rxbuf = alloc_iob ( nii->mtu );
  723. if ( ! nii->rxbuf ) {
  724. /* Leave for next poll */
  725. break;
  726. }
  727. }
  728. /* Construct parameter block */
  729. memset ( &cpb, 0, sizeof ( cpb ) );
  730. cpb.BufferAddr = virt_to_bus ( nii->rxbuf->data );
  731. cpb.BufferLen = iob_tailroom ( nii->rxbuf );
  732. /* Issue command */
  733. if ( ( stat = nii_issue_cpb_db ( nii, PXE_OPCODE_RECEIVE,
  734. &cpb, sizeof ( cpb ),
  735. &db, sizeof ( db ) ) ) < 0 ) {
  736. /* PXE_STATCODE_NO_DATA is just the usual "no packet"
  737. * status indicator; ignore it.
  738. */
  739. if ( stat == -PXE_STATCODE_NO_DATA )
  740. break;
  741. /* Anything else is an error */
  742. rc = -EIO_STAT ( stat );
  743. DBGC ( nii, "NII %s could not receive: %s\n",
  744. nii->dev.name, strerror ( rc ) );
  745. netdev_rx_err ( netdev, NULL, rc );
  746. break;
  747. }
  748. /* Hand off to network stack */
  749. iob_put ( nii->rxbuf, db.FrameLen );
  750. netdev_rx ( netdev, nii->rxbuf );
  751. nii->rxbuf = NULL;
  752. }
  753. }
  754. /**
  755. * Check for link state changes
  756. *
  757. * @v netdev Network device
  758. * @v stat Status flags
  759. */
  760. static void nii_poll_link ( struct net_device *netdev, unsigned int stat ) {
  761. int no_media = ( stat & PXE_STATFLAGS_GET_STATUS_NO_MEDIA );
  762. if ( no_media && netdev_link_ok ( netdev ) ) {
  763. netdev_link_down ( netdev );
  764. } else if ( ( ! no_media ) && ( ! netdev_link_ok ( netdev ) ) ) {
  765. netdev_link_up ( netdev );
  766. }
  767. }
  768. /**
  769. * Poll for completed packets
  770. *
  771. * @v netdev Network device
  772. */
  773. static void nii_poll ( struct net_device *netdev ) {
  774. struct nii_nic *nii = netdev->priv;
  775. PXE_DB_GET_STATUS db;
  776. unsigned int op;
  777. int stat;
  778. int rc;
  779. /* Get status */
  780. op = NII_OP ( PXE_OPCODE_GET_STATUS,
  781. ( PXE_OPFLAGS_GET_INTERRUPT_STATUS |
  782. PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS |
  783. PXE_OPFLAGS_GET_MEDIA_STATUS ) );
  784. if ( ( stat = nii_issue_db ( nii, op, &db, sizeof ( db ) ) ) < 0 ) {
  785. rc = -EIO_STAT ( stat );
  786. DBGC ( nii, "NII %s could not get status: %s\n",
  787. nii->dev.name, strerror ( rc ) );
  788. return;
  789. }
  790. /* Process any TX completions */
  791. nii_poll_tx ( netdev, stat );
  792. /* Process any RX completions */
  793. nii_poll_rx ( netdev );
  794. /* Check for link state changes */
  795. nii_poll_link ( netdev, stat );
  796. }
  797. /**
  798. * Open network device
  799. *
  800. * @v netdev Network device
  801. * @ret rc Return status code
  802. */
  803. static int nii_open ( struct net_device *netdev ) {
  804. struct nii_nic *nii = netdev->priv;
  805. int rc;
  806. /* Initialise NIC */
  807. if ( ( rc = nii_initialise ( nii ) ) != 0 )
  808. goto err_initialise;
  809. /* Attempt to set station address */
  810. if ( ( rc = nii_set_station_address ( nii, netdev ) ) != 0 ) {
  811. DBGC ( nii, "NII %s could not set station address: %s\n",
  812. nii->dev.name, strerror ( rc ) );
  813. /* Treat as non-fatal */
  814. }
  815. /* Set receive filters */
  816. if ( ( rc = nii_set_rx_filters ( nii ) ) != 0 )
  817. goto err_set_rx_filters;
  818. return 0;
  819. err_set_rx_filters:
  820. nii_shutdown ( nii );
  821. err_initialise:
  822. return rc;
  823. }
  824. /**
  825. * Close network device
  826. *
  827. * @v netdev Network device
  828. */
  829. static void nii_close ( struct net_device *netdev ) {
  830. struct nii_nic *nii = netdev->priv;
  831. /* Shut down NIC */
  832. nii_shutdown ( nii );
  833. /* Discard transmit buffer, if applicable */
  834. if ( nii->txbuf ) {
  835. netdev_tx_complete_err ( netdev, nii->txbuf, -ECANCELED );
  836. nii->txbuf = NULL;
  837. }
  838. /* Discard receive buffer, if applicable */
  839. if ( nii->rxbuf ) {
  840. free_iob ( nii->rxbuf );
  841. nii->rxbuf = NULL;
  842. }
  843. }
  844. /** NII network device operations */
  845. static struct net_device_operations nii_operations = {
  846. .open = nii_open,
  847. .close = nii_close,
  848. .transmit = nii_transmit,
  849. .poll = nii_poll,
  850. };
  851. /**
  852. * Attach driver to device
  853. *
  854. * @v efidev EFI device
  855. * @ret rc Return status code
  856. */
  857. int nii_start ( struct efi_device *efidev ) {
  858. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  859. EFI_HANDLE device = efidev->device;
  860. struct net_device *netdev;
  861. struct nii_nic *nii;
  862. void *interface;
  863. EFI_STATUS efirc;
  864. int rc;
  865. /* Allocate and initialise structure */
  866. netdev = alloc_netdev ( sizeof ( *nii ) );
  867. if ( ! netdev ) {
  868. rc = -ENOMEM;
  869. goto err_alloc;
  870. }
  871. netdev_init ( netdev, &nii_operations );
  872. nii = netdev->priv;
  873. nii->efidev = efidev;
  874. netdev->ll_broadcast = nii->broadcast;
  875. efidev_set_drvdata ( efidev, netdev );
  876. /* Populate underlying device information */
  877. efi_device_info ( device, "NII", &nii->dev );
  878. nii->dev.driver_name = "NII";
  879. nii->dev.parent = &efidev->dev;
  880. list_add ( &nii->dev.siblings, &efidev->dev.children );
  881. INIT_LIST_HEAD ( &nii->dev.children );
  882. netdev->dev = &nii->dev;
  883. /* Open NII protocol */
  884. if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid,
  885. &interface, efi_image_handle, device,
  886. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  887. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  888. rc = -EEFI ( efirc );
  889. DBGC ( nii, "NII %s cannot open NII protocol: %s\n",
  890. nii->dev.name, strerror ( rc ) );
  891. DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid );
  892. goto err_open_protocol;
  893. }
  894. nii->nii = interface;
  895. /* Locate UNDI and entry point */
  896. nii->undi = ( ( void * ) ( intptr_t ) nii->nii->Id );
  897. if ( ! nii->undi ) {
  898. DBGC ( nii, "NII %s has no UNDI\n", nii->dev.name );
  899. rc = -ENODEV;
  900. goto err_no_undi;
  901. }
  902. if ( nii->undi->Implementation & PXE_ROMID_IMP_HW_UNDI ) {
  903. DBGC ( nii, "NII %s is a mythical hardware UNDI\n",
  904. nii->dev.name );
  905. rc = -ENOTSUP;
  906. goto err_hw_undi;
  907. }
  908. if ( nii->undi->Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR ) {
  909. nii->issue = ( ( void * ) ( intptr_t ) nii->undi->EntryPoint );
  910. } else {
  911. nii->issue = ( ( ( void * ) nii->undi ) +
  912. nii->undi->EntryPoint );
  913. }
  914. DBGC ( nii, "NII %s using UNDI v%x.%x at %p entry %p\n", nii->dev.name,
  915. nii->nii->MajorVer, nii->nii->MinorVer, nii->undi, nii->issue );
  916. /* Open PCI I/O protocols and locate BARs */
  917. if ( ( rc = nii_pci_open ( nii ) ) != 0 )
  918. goto err_pci_open;
  919. /* Start UNDI */
  920. if ( ( rc = nii_start_undi ( nii ) ) != 0 )
  921. goto err_start_undi;
  922. /* Get initialisation information */
  923. if ( ( rc = nii_get_init_info ( nii, netdev ) ) != 0 )
  924. goto err_get_init_info;
  925. /* Get MAC addresses */
  926. if ( ( rc = nii_get_station_address ( nii, netdev ) ) != 0 )
  927. goto err_get_station_address;
  928. /* Register network device */
  929. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  930. goto err_register_netdev;
  931. DBGC ( nii, "NII %s registered as %s for %p %s\n", nii->dev.name,
  932. netdev->name, device, efi_handle_name ( device ) );
  933. return 0;
  934. unregister_netdev ( netdev );
  935. err_register_netdev:
  936. err_get_station_address:
  937. err_get_init_info:
  938. nii_stop_undi ( nii );
  939. err_start_undi:
  940. nii_pci_close ( nii );
  941. err_pci_open:
  942. err_hw_undi:
  943. err_no_undi:
  944. bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
  945. efi_image_handle, device );
  946. err_open_protocol:
  947. list_del ( &nii->dev.siblings );
  948. netdev_nullify ( netdev );
  949. netdev_put ( netdev );
  950. err_alloc:
  951. return rc;
  952. }
  953. /**
  954. * Detach driver from device
  955. *
  956. * @v efidev EFI device
  957. */
  958. void nii_stop ( struct efi_device *efidev ) {
  959. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  960. struct net_device *netdev = efidev_get_drvdata ( efidev );
  961. struct nii_nic *nii = netdev->priv;
  962. EFI_HANDLE device = efidev->device;
  963. /* Unregister network device */
  964. unregister_netdev ( netdev );
  965. /* Stop UNDI */
  966. nii_stop_undi ( nii );
  967. /* Close PCI I/O protocols */
  968. nii_pci_close ( nii );
  969. /* Close NII protocol */
  970. bs->CloseProtocol ( device, &efi_nii31_protocol_guid,
  971. efi_image_handle, device );
  972. /* Free network device */
  973. list_del ( &nii->dev.siblings );
  974. netdev_nullify ( netdev );
  975. netdev_put ( netdev );
  976. }