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 36KB

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