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.

intelxl.c 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /*
  2. * Copyright (C) 2018 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 (at your option) 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 <stdint.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <byteswap.h>
  30. #include <ipxe/netdevice.h>
  31. #include <ipxe/ethernet.h>
  32. #include <ipxe/if_ether.h>
  33. #include <ipxe/vlan.h>
  34. #include <ipxe/iobuf.h>
  35. #include <ipxe/malloc.h>
  36. #include <ipxe/pci.h>
  37. #include <ipxe/version.h>
  38. #include "intelxl.h"
  39. /** @file
  40. *
  41. * Intel 40 Gigabit Ethernet network card driver
  42. *
  43. */
  44. /******************************************************************************
  45. *
  46. * Device reset
  47. *
  48. ******************************************************************************
  49. */
  50. /**
  51. * Reset hardware
  52. *
  53. * @v intelxl Intel device
  54. * @ret rc Return status code
  55. */
  56. static int intelxl_reset ( struct intelxl_nic *intelxl ) {
  57. uint32_t pfgen_ctrl;
  58. /* Perform a global software reset */
  59. pfgen_ctrl = readl ( intelxl->regs + INTELXL_PFGEN_CTRL );
  60. writel ( ( pfgen_ctrl | INTELXL_PFGEN_CTRL_PFSWR ),
  61. intelxl->regs + INTELXL_PFGEN_CTRL );
  62. mdelay ( INTELXL_RESET_DELAY_MS );
  63. return 0;
  64. }
  65. /******************************************************************************
  66. *
  67. * MAC address
  68. *
  69. ******************************************************************************
  70. */
  71. /**
  72. * Fetch initial MAC address and maximum frame size
  73. *
  74. * @v intelxl Intel device
  75. * @v netdev Network device
  76. * @ret rc Return status code
  77. */
  78. static int intelxl_fetch_mac ( struct intelxl_nic *intelxl,
  79. struct net_device *netdev ) {
  80. union intelxl_receive_address mac;
  81. uint32_t prtgl_sal;
  82. uint32_t prtgl_sah;
  83. size_t mfs;
  84. /* Read NVM-loaded address */
  85. prtgl_sal = readl ( intelxl->regs + INTELXL_PRTGL_SAL );
  86. prtgl_sah = readl ( intelxl->regs + INTELXL_PRTGL_SAH );
  87. mac.reg.low = cpu_to_le32 ( prtgl_sal );
  88. mac.reg.high = cpu_to_le32 ( prtgl_sah );
  89. /* Check that address is valid */
  90. if ( ! is_valid_ether_addr ( mac.raw ) ) {
  91. DBGC ( intelxl, "INTELXL %p has invalid MAC address (%s)\n",
  92. intelxl, eth_ntoa ( mac.raw ) );
  93. return -ENOENT;
  94. }
  95. /* Copy MAC address */
  96. DBGC ( intelxl, "INTELXL %p has autoloaded MAC address %s\n",
  97. intelxl, eth_ntoa ( mac.raw ) );
  98. memcpy ( netdev->hw_addr, mac.raw, ETH_ALEN );
  99. /* Get maximum frame size */
  100. mfs = INTELXL_PRTGL_SAH_MFS_GET ( prtgl_sah );
  101. netdev->max_pkt_len = ( mfs - 4 /* CRC */ );
  102. return 0;
  103. }
  104. /******************************************************************************
  105. *
  106. * Admin queue
  107. *
  108. ******************************************************************************
  109. */
  110. /** Admin queue register offsets */
  111. static const struct intelxl_admin_offsets intelxl_admin_offsets = {
  112. .bal = INTELXL_ADMIN_BAL,
  113. .bah = INTELXL_ADMIN_BAH,
  114. .len = INTELXL_ADMIN_LEN,
  115. .head = INTELXL_ADMIN_HEAD,
  116. .tail = INTELXL_ADMIN_TAIL,
  117. };
  118. /**
  119. * Create admin queue
  120. *
  121. * @v intelxl Intel device
  122. * @v admin Admin queue
  123. * @ret rc Return status code
  124. */
  125. static int intelxl_create_admin ( struct intelxl_nic *intelxl,
  126. struct intelxl_admin *admin ) {
  127. size_t len = ( sizeof ( admin->desc[0] ) * INTELXL_ADMIN_NUM_DESC );
  128. const struct intelxl_admin_offsets *regs = admin->regs;
  129. void *admin_regs = ( intelxl->regs + admin->base );
  130. physaddr_t address;
  131. /* Allocate admin queue */
  132. admin->desc = malloc_dma ( ( len + sizeof ( *admin->buffer ) ),
  133. INTELXL_ALIGN );
  134. if ( ! admin->desc )
  135. return -ENOMEM;
  136. admin->buffer = ( ( ( void * ) admin->desc ) + len );
  137. /* Initialise admin queue */
  138. memset ( admin->desc, 0, len );
  139. /* Reset head and tail registers */
  140. writel ( 0, admin_regs + regs->head );
  141. writel ( 0, admin_regs + regs->tail );
  142. /* Reset queue index */
  143. admin->index = 0;
  144. /* Program queue address */
  145. address = virt_to_bus ( admin->desc );
  146. writel ( ( address & 0xffffffffUL ), admin_regs + regs->bal );
  147. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
  148. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  149. admin_regs + regs->bah );
  150. } else {
  151. writel ( 0, admin_regs + regs->bah );
  152. }
  153. /* Program queue length and enable queue */
  154. writel ( ( INTELXL_ADMIN_LEN_LEN ( INTELXL_ADMIN_NUM_DESC ) |
  155. INTELXL_ADMIN_LEN_ENABLE ),
  156. admin_regs + regs->len );
  157. DBGC ( intelxl, "INTELXL %p A%cQ is at [%08llx,%08llx) buf "
  158. "[%08llx,%08llx)\n", intelxl,
  159. ( ( admin == &intelxl->command ) ? 'T' : 'R' ),
  160. ( ( unsigned long long ) address ),
  161. ( ( unsigned long long ) address + len ),
  162. ( ( unsigned long long ) virt_to_bus ( admin->buffer ) ),
  163. ( ( unsigned long long ) ( virt_to_bus ( admin->buffer ) +
  164. sizeof ( admin->buffer[0] ) ) ) );
  165. return 0;
  166. }
  167. /**
  168. * Destroy admin queue
  169. *
  170. * @v intelxl Intel device
  171. * @v admin Admin queue
  172. */
  173. static void intelxl_destroy_admin ( struct intelxl_nic *intelxl,
  174. struct intelxl_admin *admin ) {
  175. size_t len = ( sizeof ( admin->desc[0] ) * INTELXL_ADMIN_NUM_DESC );
  176. const struct intelxl_admin_offsets *regs = admin->regs;
  177. void *admin_regs = ( intelxl->regs + admin->base );
  178. /* Disable queue */
  179. writel ( 0, admin_regs + regs->len );
  180. /* Free queue */
  181. free_dma ( admin->desc, ( len + sizeof ( *admin->buffer ) ) );
  182. }
  183. /**
  184. * Issue admin queue command
  185. *
  186. * @v intelxl Intel device
  187. * @v cmd Command descriptor
  188. * @ret rc Return status code
  189. */
  190. static int intelxl_admin_command ( struct intelxl_nic *intelxl,
  191. struct intelxl_admin_descriptor *cmd ) {
  192. struct intelxl_admin *admin = &intelxl->command;
  193. const struct intelxl_admin_offsets *regs = admin->regs;
  194. void *admin_regs = ( intelxl->regs + admin->base );
  195. struct intelxl_admin_descriptor *desc;
  196. uint64_t buffer;
  197. unsigned int index;
  198. unsigned int tail;
  199. unsigned int i;
  200. int rc;
  201. /* Get next queue entry */
  202. index = admin->index++;
  203. tail = ( admin->index % INTELXL_ADMIN_NUM_DESC );
  204. desc = &admin->desc[index % INTELXL_ADMIN_NUM_DESC];
  205. /* Clear must-be-zero flags */
  206. cmd->flags &= ~cpu_to_le16 ( INTELXL_ADMIN_FL_DD |
  207. INTELXL_ADMIN_FL_CMP |
  208. INTELXL_ADMIN_FL_ERR );
  209. /* Clear return value */
  210. cmd->ret = 0;
  211. /* Populate cookie */
  212. cmd->cookie = cpu_to_le32 ( index );
  213. /* Populate data buffer address if applicable */
  214. if ( cmd->flags & cpu_to_le16 ( INTELXL_ADMIN_FL_BUF ) ) {
  215. buffer = virt_to_bus ( admin->buffer );
  216. cmd->params.buffer.high = cpu_to_le32 ( buffer >> 32 );
  217. cmd->params.buffer.low = cpu_to_le32 ( buffer & 0xffffffffUL );
  218. }
  219. /* Copy command descriptor to queue entry */
  220. memcpy ( desc, cmd, sizeof ( *desc ) );
  221. DBGC2 ( intelxl, "INTELXL %p admin command %#x:\n", intelxl, index );
  222. DBGC2_HDA ( intelxl, virt_to_phys ( desc ), desc, sizeof ( *desc ) );
  223. /* Post command descriptor */
  224. wmb();
  225. writel ( tail, admin_regs + regs->tail );
  226. /* Wait for completion */
  227. for ( i = 0 ; i < INTELXL_ADMIN_MAX_WAIT_MS ; i++ ) {
  228. /* If response is not complete, delay 1ms and retry */
  229. if ( ! ( desc->flags & INTELXL_ADMIN_FL_DD ) ) {
  230. mdelay ( 1 );
  231. continue;
  232. }
  233. DBGC2 ( intelxl, "INTELXL %p admin command %#x response:\n",
  234. intelxl, index );
  235. DBGC2_HDA ( intelxl, virt_to_phys ( desc ), desc,
  236. sizeof ( *desc ) );
  237. /* Check for cookie mismatch */
  238. if ( desc->cookie != cmd->cookie ) {
  239. DBGC ( intelxl, "INTELXL %p admin command %#x bad "
  240. "cookie %#x\n", intelxl, index,
  241. le32_to_cpu ( desc->cookie ) );
  242. rc = -EPROTO;
  243. goto err;
  244. }
  245. /* Check for errors */
  246. if ( desc->ret != 0 ) {
  247. DBGC ( intelxl, "INTELXL %p admin command %#x error "
  248. "%d\n", intelxl, index,
  249. le16_to_cpu ( desc->ret ) );
  250. rc = -EIO;
  251. goto err;
  252. }
  253. /* Copy response back to command descriptor */
  254. memcpy ( cmd, desc, sizeof ( *cmd ) );
  255. /* Success */
  256. return 0;
  257. }
  258. rc = -ETIMEDOUT;
  259. DBGC ( intelxl, "INTELXL %p timed out waiting for admin command %#x:\n",
  260. intelxl, index );
  261. err:
  262. DBGC_HDA ( intelxl, virt_to_phys ( desc ), cmd, sizeof ( *cmd ) );
  263. DBGC_HDA ( intelxl, virt_to_phys ( desc ), desc, sizeof ( *desc ) );
  264. return rc;
  265. }
  266. /**
  267. * Get firmware version
  268. *
  269. * @v intelxl Intel device
  270. * @ret rc Return status code
  271. */
  272. static int intelxl_admin_version ( struct intelxl_nic *intelxl ) {
  273. struct intelxl_admin_descriptor cmd;
  274. struct intelxl_admin_version_params *version = &cmd.params.version;
  275. unsigned int api;
  276. int rc;
  277. /* Populate descriptor */
  278. memset ( &cmd, 0, sizeof ( cmd ) );
  279. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_VERSION );
  280. /* Issue command */
  281. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  282. return rc;
  283. api = le16_to_cpu ( version->api.major );
  284. DBGC ( intelxl, "INTELXL %p firmware v%d.%d API v%d.%d\n",
  285. intelxl, le16_to_cpu ( version->firmware.major ),
  286. le16_to_cpu ( version->firmware.minor ),
  287. api, le16_to_cpu ( version->api.minor ) );
  288. /* Check for API compatibility */
  289. if ( api > INTELXL_ADMIN_API_MAJOR ) {
  290. DBGC ( intelxl, "INTELXL %p unsupported API v%d\n",
  291. intelxl, api );
  292. return -ENOTSUP;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * Report driver version
  298. *
  299. * @v intelxl Intel device
  300. * @ret rc Return status code
  301. */
  302. static int intelxl_admin_driver ( struct intelxl_nic *intelxl ) {
  303. struct intelxl_admin_descriptor cmd;
  304. struct intelxl_admin_driver_params *driver = &cmd.params.driver;
  305. struct intelxl_admin_driver_buffer *buf =
  306. &intelxl->command.buffer->driver;
  307. int rc;
  308. /* Populate descriptor */
  309. memset ( &cmd, 0, sizeof ( cmd ) );
  310. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_DRIVER );
  311. cmd.flags = cpu_to_le16 ( INTELXL_ADMIN_FL_RD | INTELXL_ADMIN_FL_BUF );
  312. cmd.len = cpu_to_le16 ( sizeof ( *buf ) );
  313. driver->major = product_major_version;
  314. driver->minor = product_minor_version;
  315. snprintf ( buf->name, sizeof ( buf->name ), "%s",
  316. ( product_name[0] ? product_name : product_short_name ) );
  317. /* Issue command */
  318. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  319. return rc;
  320. return 0;
  321. }
  322. /**
  323. * Shutdown admin queues
  324. *
  325. * @v intelxl Intel device
  326. * @ret rc Return status code
  327. */
  328. static int intelxl_admin_shutdown ( struct intelxl_nic *intelxl ) {
  329. struct intelxl_admin_descriptor cmd;
  330. struct intelxl_admin_shutdown_params *shutdown = &cmd.params.shutdown;
  331. int rc;
  332. /* Populate descriptor */
  333. memset ( &cmd, 0, sizeof ( cmd ) );
  334. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_SHUTDOWN );
  335. shutdown->unloading = INTELXL_ADMIN_SHUTDOWN_UNLOADING;
  336. /* Issue command */
  337. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  338. return rc;
  339. return 0;
  340. }
  341. /**
  342. * Get switch configuration
  343. *
  344. * @v intelxl Intel device
  345. * @ret rc Return status code
  346. */
  347. static int intelxl_admin_switch ( struct intelxl_nic *intelxl ) {
  348. struct intelxl_admin_descriptor cmd;
  349. struct intelxl_admin_switch_params *sw = &cmd.params.sw;
  350. struct intelxl_admin_switch_buffer *buf = &intelxl->command.buffer->sw;
  351. struct intelxl_admin_switch_config *cfg = &buf->cfg;
  352. int rc;
  353. /* Populate descriptor */
  354. memset ( &cmd, 0, sizeof ( cmd ) );
  355. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_SWITCH );
  356. cmd.flags = cpu_to_le16 ( INTELXL_ADMIN_FL_BUF );
  357. cmd.len = cpu_to_le16 ( sizeof ( *buf ) );
  358. /* Get each configuration in turn */
  359. do {
  360. /* Issue command */
  361. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  362. return rc;
  363. /* Dump raw configuration */
  364. DBGC2 ( intelxl, "INTELXL %p SEID %#04x:\n",
  365. intelxl, le16_to_cpu ( cfg->seid ) );
  366. DBGC2_HDA ( intelxl, 0, cfg, sizeof ( *cfg ) );
  367. /* Parse response */
  368. if ( cfg->type == INTELXL_ADMIN_SWITCH_TYPE_VSI ) {
  369. intelxl->vsi = le16_to_cpu ( cfg->seid );
  370. DBGC ( intelxl, "INTELXL %p VSI %#04x uplink %#04x "
  371. "downlink %#04x conn %#02x\n", intelxl,
  372. intelxl->vsi, le16_to_cpu ( cfg->uplink ),
  373. le16_to_cpu ( cfg->downlink ), cfg->connection );
  374. }
  375. } while ( sw->next );
  376. /* Check that we found a VSI */
  377. if ( ! intelxl->vsi ) {
  378. DBGC ( intelxl, "INTELXL %p has no VSI\n", intelxl );
  379. return -ENOENT;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * Get VSI parameters
  385. *
  386. * @v intelxl Intel device
  387. * @ret rc Return status code
  388. */
  389. static int intelxl_admin_vsi ( struct intelxl_nic *intelxl ) {
  390. struct intelxl_admin_descriptor cmd;
  391. struct intelxl_admin_vsi_params *vsi = &cmd.params.vsi;
  392. struct intelxl_admin_vsi_buffer *buf = &intelxl->command.buffer->vsi;
  393. int rc;
  394. /* Populate descriptor */
  395. memset ( &cmd, 0, sizeof ( cmd ) );
  396. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_VSI );
  397. cmd.flags = cpu_to_le16 ( INTELXL_ADMIN_FL_BUF );
  398. cmd.len = cpu_to_le16 ( sizeof ( *buf ) );
  399. vsi->vsi = cpu_to_le16 ( intelxl->vsi );
  400. /* Issue command */
  401. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  402. return rc;
  403. /* Parse response */
  404. intelxl->queue = le16_to_cpu ( buf->queue[0] );
  405. intelxl->qset = le16_to_cpu ( buf->qset[0] );
  406. DBGC ( intelxl, "INTELXL %p VSI %#04x queue %#04x qset %#04x\n",
  407. intelxl, intelxl->vsi, intelxl->queue, intelxl->qset );
  408. return 0;
  409. }
  410. /**
  411. * Set VSI promiscuous modes
  412. *
  413. * @v intelxl Intel device
  414. * @ret rc Return status code
  415. */
  416. static int intelxl_admin_promisc ( struct intelxl_nic *intelxl ) {
  417. struct intelxl_admin_descriptor cmd;
  418. struct intelxl_admin_promisc_params *promisc = &cmd.params.promisc;
  419. uint16_t flags;
  420. int rc;
  421. /* Populate descriptor */
  422. memset ( &cmd, 0, sizeof ( cmd ) );
  423. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_PROMISC );
  424. flags = ( INTELXL_ADMIN_PROMISC_FL_UNICAST |
  425. INTELXL_ADMIN_PROMISC_FL_MULTICAST |
  426. INTELXL_ADMIN_PROMISC_FL_BROADCAST |
  427. INTELXL_ADMIN_PROMISC_FL_VLAN );
  428. promisc->flags = cpu_to_le16 ( flags );
  429. promisc->valid = cpu_to_le16 ( flags );
  430. promisc->vsi = cpu_to_le16 ( intelxl->vsi );
  431. /* Issue command */
  432. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  433. return rc;
  434. return 0;
  435. }
  436. /**
  437. * Restart autonegotiation
  438. *
  439. * @v intelxl Intel device
  440. * @ret rc Return status code
  441. */
  442. static int intelxl_admin_autoneg ( struct intelxl_nic *intelxl ) {
  443. struct intelxl_admin_descriptor cmd;
  444. struct intelxl_admin_autoneg_params *autoneg = &cmd.params.autoneg;
  445. int rc;
  446. /* Populate descriptor */
  447. memset ( &cmd, 0, sizeof ( cmd ) );
  448. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_AUTONEG );
  449. autoneg->flags = ( INTELXL_ADMIN_AUTONEG_FL_RESTART |
  450. INTELXL_ADMIN_AUTONEG_FL_ENABLE );
  451. /* Issue command */
  452. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  453. return rc;
  454. return 0;
  455. }
  456. /**
  457. * Get link status
  458. *
  459. * @v netdev Network device
  460. * @ret rc Return status code
  461. */
  462. static int intelxl_admin_link ( struct net_device *netdev ) {
  463. struct intelxl_nic *intelxl = netdev->priv;
  464. struct intelxl_admin_descriptor cmd;
  465. struct intelxl_admin_link_params *link = &cmd.params.link;
  466. int rc;
  467. /* Populate descriptor */
  468. memset ( &cmd, 0, sizeof ( cmd ) );
  469. cmd.opcode = cpu_to_le16 ( INTELXL_ADMIN_LINK );
  470. link->notify = INTELXL_ADMIN_LINK_NOTIFY;
  471. /* Issue command */
  472. if ( ( rc = intelxl_admin_command ( intelxl, &cmd ) ) != 0 )
  473. return rc;
  474. DBGC ( intelxl, "INTELXL %p PHY %#02x speed %#02x status %#02x\n",
  475. intelxl, link->phy, link->speed, link->status );
  476. /* Update network device */
  477. if ( link->status & INTELXL_ADMIN_LINK_UP ) {
  478. netdev_link_up ( netdev );
  479. } else {
  480. netdev_link_down ( netdev );
  481. }
  482. return 0;
  483. }
  484. /**
  485. * Refill admin event queue
  486. *
  487. * @v intelxl Intel device
  488. */
  489. static void intelxl_refill_admin ( struct intelxl_nic *intelxl ) {
  490. struct intelxl_admin *admin = &intelxl->event;
  491. const struct intelxl_admin_offsets *regs = admin->regs;
  492. void *admin_regs = ( intelxl->regs + admin->base );
  493. unsigned int tail;
  494. /* Update tail pointer */
  495. tail = ( ( admin->index + INTELXL_ADMIN_NUM_DESC - 1 ) %
  496. INTELXL_ADMIN_NUM_DESC );
  497. writel ( tail, admin_regs + regs->tail );
  498. }
  499. /**
  500. * Poll admin event queue
  501. *
  502. * @v netdev Network device
  503. */
  504. static void intelxl_poll_admin ( struct net_device *netdev ) {
  505. struct intelxl_nic *intelxl = netdev->priv;
  506. struct intelxl_admin *admin = &intelxl->event;
  507. struct intelxl_admin_descriptor *desc;
  508. /* Check for events */
  509. while ( 1 ) {
  510. /* Get next event descriptor */
  511. desc = &admin->desc[admin->index % INTELXL_ADMIN_NUM_DESC];
  512. /* Stop if descriptor is not yet completed */
  513. if ( ! ( desc->flags & INTELXL_ADMIN_FL_DD ) )
  514. return;
  515. DBGC2 ( intelxl, "INTELXL %p admin event %#x:\n",
  516. intelxl, admin->index );
  517. DBGC2_HDA ( intelxl, virt_to_phys ( desc ), desc,
  518. sizeof ( *desc ) );
  519. /* Handle event */
  520. switch ( desc->opcode ) {
  521. case cpu_to_le16 ( INTELXL_ADMIN_LINK ):
  522. intelxl_admin_link ( netdev );
  523. break;
  524. default:
  525. DBGC ( intelxl, "INTELXL %p admin event %#x "
  526. "unrecognised opcode %#04x\n", intelxl,
  527. admin->index, le16_to_cpu ( desc->opcode ) );
  528. break;
  529. }
  530. /* Clear event completion flag */
  531. desc->flags = 0;
  532. wmb();
  533. /* Update index and refill queue */
  534. admin->index++;
  535. intelxl_refill_admin ( intelxl );
  536. }
  537. }
  538. /**
  539. * Open admin queues
  540. *
  541. * @v intelxl Intel device
  542. * @ret rc Return status code
  543. */
  544. static int intelxl_open_admin ( struct intelxl_nic *intelxl ) {
  545. int rc;
  546. /* Create admin event queue */
  547. if ( ( rc = intelxl_create_admin ( intelxl, &intelxl->event ) ) != 0 )
  548. goto err_create_event;
  549. /* Create admin command queue */
  550. if ( ( rc = intelxl_create_admin ( intelxl, &intelxl->command ) ) != 0 )
  551. goto err_create_command;
  552. /* Post all descriptors to event queue */
  553. intelxl_refill_admin ( intelxl );
  554. /* Get firmware version */
  555. if ( ( rc = intelxl_admin_version ( intelxl ) ) != 0 )
  556. goto err_version;
  557. /* Report driver version */
  558. if ( ( rc = intelxl_admin_driver ( intelxl ) ) != 0 )
  559. goto err_driver;
  560. return 0;
  561. err_driver:
  562. err_version:
  563. intelxl_destroy_admin ( intelxl, &intelxl->command );
  564. err_create_command:
  565. intelxl_destroy_admin ( intelxl, &intelxl->event );
  566. err_create_event:
  567. return rc;
  568. }
  569. /**
  570. * Close admin queues
  571. *
  572. * @v intelxl Intel device
  573. */
  574. static void intelxl_close_admin ( struct intelxl_nic *intelxl ) {
  575. /* Shut down admin queues */
  576. intelxl_admin_shutdown ( intelxl );
  577. /* Destroy admin command queue */
  578. intelxl_destroy_admin ( intelxl, &intelxl->command );
  579. /* Destroy admin event queue */
  580. intelxl_destroy_admin ( intelxl, &intelxl->event );
  581. }
  582. /******************************************************************************
  583. *
  584. * Descriptor rings
  585. *
  586. ******************************************************************************
  587. */
  588. /**
  589. * Dump queue context (for debugging)
  590. *
  591. * @v intelxl Intel device
  592. * @v op Context operation
  593. * @v len Size of context
  594. */
  595. static __attribute__ (( unused )) void
  596. intelxl_context_dump ( struct intelxl_nic *intelxl, uint32_t op, size_t len ) {
  597. struct intelxl_context_line line;
  598. uint32_t pfcm_lanctxctl;
  599. uint32_t pfcm_lanctxstat;
  600. unsigned int queue;
  601. unsigned int index;
  602. unsigned int i;
  603. /* Do nothing unless debug output is enabled */
  604. if ( ! DBG_EXTRA )
  605. return;
  606. /* Dump context */
  607. DBGC2 ( intelxl, "INTELXL %p context %#08x:\n", intelxl, op );
  608. for ( index = 0 ; ( sizeof ( line ) * index ) < len ; index++ ) {
  609. /* Start context operation */
  610. queue = ( intelxl->base + intelxl->queue );
  611. pfcm_lanctxctl =
  612. ( INTELXL_PFCM_LANCTXCTL_QUEUE_NUM ( queue ) |
  613. INTELXL_PFCM_LANCTXCTL_SUB_LINE ( index ) |
  614. INTELXL_PFCM_LANCTXCTL_OP_CODE_READ | op );
  615. writel ( pfcm_lanctxctl,
  616. intelxl->regs + INTELXL_PFCM_LANCTXCTL );
  617. /* Wait for operation to complete */
  618. for ( i = 0 ; i < INTELXL_CTX_MAX_WAIT_MS ; i++ ) {
  619. /* Check if operation is complete */
  620. pfcm_lanctxstat = readl ( intelxl->regs +
  621. INTELXL_PFCM_LANCTXSTAT );
  622. if ( pfcm_lanctxstat & INTELXL_PFCM_LANCTXSTAT_DONE )
  623. break;
  624. /* Delay */
  625. mdelay ( 1 );
  626. }
  627. /* Read context data */
  628. for ( i = 0 ; i < ( sizeof ( line ) /
  629. sizeof ( line.raw[0] ) ) ; i++ ) {
  630. line.raw[i] = readl ( intelxl->regs +
  631. INTELXL_PFCM_LANCTXDATA ( i ) );
  632. }
  633. DBGC2_HDA ( intelxl, ( sizeof ( line ) * index ),
  634. &line, sizeof ( line ) );
  635. }
  636. }
  637. /**
  638. * Program queue context line
  639. *
  640. * @v intelxl Intel device
  641. * @v line Queue context line
  642. * @v index Line number
  643. * @v op Context operation
  644. * @ret rc Return status code
  645. */
  646. static int intelxl_context_line ( struct intelxl_nic *intelxl,
  647. struct intelxl_context_line *line,
  648. unsigned int index, uint32_t op ) {
  649. uint32_t pfcm_lanctxctl;
  650. uint32_t pfcm_lanctxstat;
  651. unsigned int queue;
  652. unsigned int i;
  653. /* Write context data */
  654. for ( i = 0; i < ( sizeof ( *line ) / sizeof ( line->raw[0] ) ); i++ ) {
  655. writel ( le32_to_cpu ( line->raw[i] ),
  656. intelxl->regs + INTELXL_PFCM_LANCTXDATA ( i ) );
  657. }
  658. /* Start context operation */
  659. queue = ( intelxl->base + intelxl->queue );
  660. pfcm_lanctxctl = ( INTELXL_PFCM_LANCTXCTL_QUEUE_NUM ( queue ) |
  661. INTELXL_PFCM_LANCTXCTL_SUB_LINE ( index ) |
  662. INTELXL_PFCM_LANCTXCTL_OP_CODE_WRITE | op );
  663. writel ( pfcm_lanctxctl, intelxl->regs + INTELXL_PFCM_LANCTXCTL );
  664. /* Wait for operation to complete */
  665. for ( i = 0 ; i < INTELXL_CTX_MAX_WAIT_MS ; i++ ) {
  666. /* Check if operation is complete */
  667. pfcm_lanctxstat = readl ( intelxl->regs +
  668. INTELXL_PFCM_LANCTXSTAT );
  669. if ( pfcm_lanctxstat & INTELXL_PFCM_LANCTXSTAT_DONE )
  670. return 0;
  671. /* Delay */
  672. mdelay ( 1 );
  673. }
  674. DBGC ( intelxl, "INTELXL %p timed out waiting for context: %#08x\n",
  675. intelxl, pfcm_lanctxctl );
  676. return -ETIMEDOUT;
  677. }
  678. /**
  679. * Program queue context
  680. *
  681. * @v intelxl Intel device
  682. * @v line Queue context lines
  683. * @v len Size of context
  684. * @v op Context operation
  685. * @ret rc Return status code
  686. */
  687. static int intelxl_context ( struct intelxl_nic *intelxl,
  688. struct intelxl_context_line *line,
  689. size_t len, uint32_t op ) {
  690. unsigned int index;
  691. int rc;
  692. DBGC2 ( intelxl, "INTELXL %p context %#08x len %#zx:\n",
  693. intelxl, op, len );
  694. DBGC2_HDA ( intelxl, 0, line, len );
  695. /* Program one line at a time */
  696. for ( index = 0 ; ( sizeof ( *line ) * index ) < len ; index++ ) {
  697. if ( ( rc = intelxl_context_line ( intelxl, line++, index,
  698. op ) ) != 0 )
  699. return rc;
  700. }
  701. return 0;
  702. }
  703. /**
  704. * Program transmit queue context
  705. *
  706. * @v intelxl Intel device
  707. * @v address Descriptor ring base address
  708. * @ret rc Return status code
  709. */
  710. static int intelxl_context_tx ( struct intelxl_nic *intelxl,
  711. physaddr_t address ) {
  712. union {
  713. struct intelxl_context_tx tx;
  714. struct intelxl_context_line line;
  715. } ctx;
  716. int rc;
  717. /* Initialise context */
  718. memset ( &ctx, 0, sizeof ( ctx ) );
  719. ctx.tx.flags = cpu_to_le16 ( INTELXL_CTX_TX_FL_NEW );
  720. ctx.tx.base = cpu_to_le64 ( INTELXL_CTX_TX_BASE ( address ) );
  721. ctx.tx.count =
  722. cpu_to_le16 ( INTELXL_CTX_TX_COUNT ( INTELXL_TX_NUM_DESC ) );
  723. ctx.tx.qset = INTELXL_CTX_TX_QSET ( intelxl->qset );
  724. /* Program context */
  725. if ( ( rc = intelxl_context ( intelxl, &ctx.line, sizeof ( ctx ),
  726. INTELXL_PFCM_LANCTXCTL_TYPE_TX ) ) != 0 )
  727. return rc;
  728. return 0;
  729. }
  730. /**
  731. * Program receive queue context
  732. *
  733. * @v intelxl Intel device
  734. * @v address Descriptor ring base address
  735. * @ret rc Return status code
  736. */
  737. static int intelxl_context_rx ( struct intelxl_nic *intelxl,
  738. physaddr_t address ) {
  739. union {
  740. struct intelxl_context_rx rx;
  741. struct intelxl_context_line line;
  742. } ctx;
  743. uint64_t base_count;
  744. int rc;
  745. /* Initialise context */
  746. memset ( &ctx, 0, sizeof ( ctx ) );
  747. base_count = INTELXL_CTX_RX_BASE_COUNT ( address, INTELXL_RX_NUM_DESC );
  748. ctx.rx.base_count = cpu_to_le64 ( base_count );
  749. ctx.rx.len = cpu_to_le16 ( INTELXL_CTX_RX_LEN ( intelxl->mfs ) );
  750. ctx.rx.flags = INTELXL_CTX_RX_FL_CRCSTRIP;
  751. ctx.rx.mfs = cpu_to_le16 ( INTELXL_CTX_RX_MFS ( intelxl->mfs ) );
  752. /* Program context */
  753. if ( ( rc = intelxl_context ( intelxl, &ctx.line, sizeof ( ctx ),
  754. INTELXL_PFCM_LANCTXCTL_TYPE_RX ) ) != 0 )
  755. return rc;
  756. return 0;
  757. }
  758. /**
  759. * Enable descriptor ring
  760. *
  761. * @v intelxl Intel device
  762. * @v ring Descriptor ring
  763. * @ret rc Return status code
  764. */
  765. static int intelxl_enable_ring ( struct intelxl_nic *intelxl,
  766. struct intelxl_ring *ring ) {
  767. void *ring_regs = ( intelxl->regs + ring->reg );
  768. uint32_t qxx_ena;
  769. /* Enable ring */
  770. writel ( INTELXL_QXX_ENA_REQ, ( ring_regs + INTELXL_QXX_ENA ) );
  771. udelay ( INTELXL_QUEUE_ENABLE_DELAY_US );
  772. qxx_ena = readl ( ring_regs + INTELXL_QXX_ENA );
  773. if ( ! ( qxx_ena & INTELXL_QXX_ENA_STAT ) ) {
  774. DBGC ( intelxl, "INTELXL %p ring %06x failed to enable: "
  775. "%#08x\n", intelxl, ring->reg, qxx_ena );
  776. return -EIO;
  777. }
  778. return 0;
  779. }
  780. /**
  781. * Disable descriptor ring
  782. *
  783. * @v intelxl Intel device
  784. * @v ring Descriptor ring
  785. * @ret rc Return status code
  786. */
  787. static int intelxl_disable_ring ( struct intelxl_nic *intelxl,
  788. struct intelxl_ring *ring ) {
  789. void *ring_regs = ( intelxl->regs + ring->reg );
  790. uint32_t qxx_ena;
  791. unsigned int i;
  792. /* Disable ring */
  793. writel ( 0, ( ring_regs + INTELXL_QXX_ENA ) );
  794. /* Wait for ring to be disabled */
  795. for ( i = 0 ; i < INTELXL_QUEUE_DISABLE_MAX_WAIT_MS ; i++ ) {
  796. /* Check if ring is disabled */
  797. qxx_ena = readl ( ring_regs + INTELXL_QXX_ENA );
  798. if ( ! ( qxx_ena & INTELXL_QXX_ENA_STAT ) )
  799. return 0;
  800. /* Delay */
  801. mdelay ( 1 );
  802. }
  803. DBGC ( intelxl, "INTELXL %p ring %06x timed out waiting for disable: "
  804. "%#08x\n", intelxl, ring->reg, qxx_ena );
  805. return -ETIMEDOUT;
  806. }
  807. /**
  808. * Create descriptor ring
  809. *
  810. * @v intelxl Intel device
  811. * @v ring Descriptor ring
  812. * @ret rc Return status code
  813. */
  814. static int intelxl_create_ring ( struct intelxl_nic *intelxl,
  815. struct intelxl_ring *ring ) {
  816. void *ring_regs = ( intelxl->regs + ring->reg );
  817. physaddr_t address;
  818. int rc;
  819. /* Allocate descriptor ring */
  820. ring->desc = malloc_dma ( ring->len, INTELXL_ALIGN );
  821. if ( ! ring->desc ) {
  822. rc = -ENOMEM;
  823. goto err_alloc;
  824. }
  825. /* Initialise descriptor ring */
  826. memset ( ring->desc, 0, ring->len );
  827. /* Reset tail pointer */
  828. writel ( 0, ( ring_regs + INTELXL_QXX_TAIL ) );
  829. /* Program queue context */
  830. address = virt_to_bus ( ring->desc );
  831. if ( ( rc = ring->context ( intelxl, address ) ) != 0 )
  832. goto err_context;
  833. /* Enable ring */
  834. if ( ( rc = intelxl_enable_ring ( intelxl, ring ) ) != 0 )
  835. goto err_enable;
  836. /* Reset counters */
  837. ring->prod = 0;
  838. ring->cons = 0;
  839. DBGC ( intelxl, "INTELXL %p ring %06x is at [%08llx,%08llx)\n",
  840. intelxl, ring->reg, ( ( unsigned long long ) address ),
  841. ( ( unsigned long long ) address + ring->len ) );
  842. return 0;
  843. intelxl_disable_ring ( intelxl, ring );
  844. err_enable:
  845. err_context:
  846. free_dma ( ring->desc, ring->len );
  847. err_alloc:
  848. return rc;
  849. }
  850. /**
  851. * Destroy descriptor ring
  852. *
  853. * @v intelxl Intel device
  854. * @v ring Descriptor ring
  855. */
  856. static void intelxl_destroy_ring ( struct intelxl_nic *intelxl,
  857. struct intelxl_ring *ring ) {
  858. int rc;
  859. /* Disable ring */
  860. if ( ( rc = intelxl_disable_ring ( intelxl, ring ) ) != 0 ) {
  861. /* Leak memory; there's nothing else we can do */
  862. return;
  863. }
  864. /* Free descriptor ring */
  865. free_dma ( ring->desc, ring->len );
  866. ring->desc = NULL;
  867. }
  868. /**
  869. * Refill receive descriptor ring
  870. *
  871. * @v intelxl Intel device
  872. */
  873. static void intelxl_refill_rx ( struct intelxl_nic *intelxl ) {
  874. struct intelxl_rx_data_descriptor *rx;
  875. struct io_buffer *iobuf;
  876. unsigned int rx_idx;
  877. unsigned int rx_tail;
  878. physaddr_t address;
  879. unsigned int refilled = 0;
  880. /* Refill ring */
  881. while ( ( intelxl->rx.prod - intelxl->rx.cons ) < INTELXL_RX_FILL ) {
  882. /* Allocate I/O buffer */
  883. iobuf = alloc_iob ( intelxl->mfs );
  884. if ( ! iobuf ) {
  885. /* Wait for next refill */
  886. break;
  887. }
  888. /* Get next receive descriptor */
  889. rx_idx = ( intelxl->rx.prod++ % INTELXL_RX_NUM_DESC );
  890. rx = &intelxl->rx.desc[rx_idx].rx;
  891. /* Populate receive descriptor */
  892. address = virt_to_bus ( iobuf->data );
  893. rx->address = cpu_to_le64 ( address );
  894. rx->flags = 0;
  895. /* Record I/O buffer */
  896. assert ( intelxl->rx_iobuf[rx_idx] == NULL );
  897. intelxl->rx_iobuf[rx_idx] = iobuf;
  898. DBGC2 ( intelxl, "INTELXL %p RX %d is [%llx,%llx)\n", intelxl,
  899. rx_idx, ( ( unsigned long long ) address ),
  900. ( ( unsigned long long ) address + intelxl->mfs ) );
  901. refilled++;
  902. }
  903. /* Push descriptors to card, if applicable */
  904. if ( refilled ) {
  905. wmb();
  906. rx_tail = ( intelxl->rx.prod % INTELXL_RX_NUM_DESC );
  907. writel ( rx_tail,
  908. ( intelxl->regs + intelxl->rx.reg + INTELXL_QXX_TAIL));
  909. }
  910. }
  911. /******************************************************************************
  912. *
  913. * Network device interface
  914. *
  915. ******************************************************************************
  916. */
  917. /**
  918. * Open network device
  919. *
  920. * @v netdev Network device
  921. * @ret rc Return status code
  922. */
  923. static int intelxl_open ( struct net_device *netdev ) {
  924. struct intelxl_nic *intelxl = netdev->priv;
  925. union intelxl_receive_address mac;
  926. unsigned int queue;
  927. uint32_t prtgl_sal;
  928. uint32_t prtgl_sah;
  929. int rc;
  930. /* Calculate maximum frame size */
  931. intelxl->mfs = ( ( ETH_HLEN + netdev->mtu + 4 /* CRC */ +
  932. INTELXL_ALIGN - 1 ) & ~( INTELXL_ALIGN - 1 ) );
  933. /* Program MAC address and maximum frame size */
  934. memset ( &mac, 0, sizeof ( mac ) );
  935. memcpy ( mac.raw, netdev->ll_addr, sizeof ( mac.raw ) );
  936. prtgl_sal = le32_to_cpu ( mac.reg.low );
  937. prtgl_sah = ( le32_to_cpu ( mac.reg.high ) |
  938. INTELXL_PRTGL_SAH_MFS_SET ( intelxl->mfs ) );
  939. writel ( prtgl_sal, intelxl->regs + INTELXL_PRTGL_SAL );
  940. writel ( prtgl_sah, intelxl->regs + INTELXL_PRTGL_SAH );
  941. /* Associate transmit queue to PF */
  942. writel ( ( INTELXL_QXX_CTL_PFVF_Q_PF |
  943. INTELXL_QXX_CTL_PFVF_PF_INDX ( intelxl->pf ) ),
  944. ( intelxl->regs + intelxl->tx.reg + INTELXL_QXX_CTL ) );
  945. /* Clear transmit pre queue disable */
  946. queue = ( intelxl->base + intelxl->queue );
  947. writel ( ( INTELXL_GLLAN_TXPRE_QDIS_CLEAR_QDIS |
  948. INTELXL_GLLAN_TXPRE_QDIS_QINDX ( queue ) ),
  949. ( intelxl->regs + INTELXL_GLLAN_TXPRE_QDIS ( queue ) ) );
  950. /* Reset transmit queue head */
  951. writel ( 0, ( intelxl->regs + INTELXL_QTX_HEAD ( intelxl->queue ) ) );
  952. /* Create receive descriptor ring */
  953. if ( ( rc = intelxl_create_ring ( intelxl, &intelxl->rx ) ) != 0 )
  954. goto err_create_rx;
  955. /* Create transmit descriptor ring */
  956. if ( ( rc = intelxl_create_ring ( intelxl, &intelxl->tx ) ) != 0 )
  957. goto err_create_tx;
  958. /* Fill receive ring */
  959. intelxl_refill_rx ( intelxl );
  960. /* Restart autonegotiation */
  961. intelxl_admin_autoneg ( intelxl );
  962. /* Update link state */
  963. intelxl_admin_link ( netdev );
  964. return 0;
  965. writel ( ( INTELXL_GLLAN_TXPRE_QDIS_SET_QDIS |
  966. INTELXL_GLLAN_TXPRE_QDIS_QINDX ( queue ) ),
  967. ( intelxl->regs + INTELXL_GLLAN_TXPRE_QDIS ( queue ) ) );
  968. udelay ( INTELXL_QUEUE_PRE_DISABLE_DELAY_US );
  969. intelxl_destroy_ring ( intelxl, &intelxl->tx );
  970. err_create_tx:
  971. intelxl_destroy_ring ( intelxl, &intelxl->rx );
  972. err_create_rx:
  973. return rc;
  974. }
  975. /**
  976. * Close network device
  977. *
  978. * @v netdev Network device
  979. */
  980. static void intelxl_close ( struct net_device *netdev ) {
  981. struct intelxl_nic *intelxl = netdev->priv;
  982. unsigned int queue;
  983. unsigned int i;
  984. /* Dump contexts (for debugging) */
  985. intelxl_context_dump ( intelxl, INTELXL_PFCM_LANCTXCTL_TYPE_TX,
  986. sizeof ( struct intelxl_context_tx ) );
  987. intelxl_context_dump ( intelxl, INTELXL_PFCM_LANCTXCTL_TYPE_RX,
  988. sizeof ( struct intelxl_context_rx ) );
  989. /* Pre-disable transmit queue */
  990. queue = ( intelxl->base + intelxl->queue );
  991. writel ( ( INTELXL_GLLAN_TXPRE_QDIS_SET_QDIS |
  992. INTELXL_GLLAN_TXPRE_QDIS_QINDX ( queue ) ),
  993. ( intelxl->regs + INTELXL_GLLAN_TXPRE_QDIS ( queue ) ) );
  994. udelay ( INTELXL_QUEUE_PRE_DISABLE_DELAY_US );
  995. /* Destroy transmit descriptor ring */
  996. intelxl_destroy_ring ( intelxl, &intelxl->tx );
  997. /* Destroy receive descriptor ring */
  998. intelxl_destroy_ring ( intelxl, &intelxl->rx );
  999. /* Discard any unused receive buffers */
  1000. for ( i = 0 ; i < INTELXL_RX_NUM_DESC ; i++ ) {
  1001. if ( intelxl->rx_iobuf[i] )
  1002. free_iob ( intelxl->rx_iobuf[i] );
  1003. intelxl->rx_iobuf[i] = NULL;
  1004. }
  1005. }
  1006. /**
  1007. * Transmit packet
  1008. *
  1009. * @v netdev Network device
  1010. * @v iobuf I/O buffer
  1011. * @ret rc Return status code
  1012. */
  1013. static int intelxl_transmit ( struct net_device *netdev,
  1014. struct io_buffer *iobuf ) {
  1015. struct intelxl_nic *intelxl = netdev->priv;
  1016. struct intelxl_tx_data_descriptor *tx;
  1017. unsigned int tx_idx;
  1018. unsigned int tx_tail;
  1019. physaddr_t address;
  1020. size_t len;
  1021. /* Get next transmit descriptor */
  1022. if ( ( intelxl->tx.prod - intelxl->tx.cons ) >= INTELXL_TX_FILL ) {
  1023. DBGC ( intelxl, "INTELXL %p out of transmit descriptors\n",
  1024. intelxl );
  1025. return -ENOBUFS;
  1026. }
  1027. tx_idx = ( intelxl->tx.prod++ % INTELXL_TX_NUM_DESC );
  1028. tx_tail = ( intelxl->tx.prod % INTELXL_TX_NUM_DESC );
  1029. tx = &intelxl->tx.desc[tx_idx].tx;
  1030. /* Populate transmit descriptor */
  1031. address = virt_to_bus ( iobuf->data );
  1032. len = iob_len ( iobuf );
  1033. tx->address = cpu_to_le64 ( address );
  1034. tx->len = cpu_to_le32 ( INTELXL_TX_DATA_LEN ( len ) );
  1035. tx->flags = cpu_to_le32 ( INTELXL_TX_DATA_DTYP | INTELXL_TX_DATA_EOP |
  1036. INTELXL_TX_DATA_RS | INTELXL_TX_DATA_JFDI );
  1037. wmb();
  1038. /* Notify card that there are packets ready to transmit */
  1039. writel ( tx_tail,
  1040. ( intelxl->regs + intelxl->tx.reg + INTELXL_QXX_TAIL ) );
  1041. DBGC2 ( intelxl, "INTELXL %p TX %d is [%llx,%llx)\n", intelxl, tx_idx,
  1042. ( ( unsigned long long ) address ),
  1043. ( ( unsigned long long ) address + len ) );
  1044. return 0;
  1045. }
  1046. /**
  1047. * Poll for completed packets
  1048. *
  1049. * @v netdev Network device
  1050. */
  1051. static void intelxl_poll_tx ( struct net_device *netdev ) {
  1052. struct intelxl_nic *intelxl = netdev->priv;
  1053. struct intelxl_tx_writeback_descriptor *tx_wb;
  1054. unsigned int tx_idx;
  1055. /* Check for completed packets */
  1056. while ( intelxl->tx.cons != intelxl->tx.prod ) {
  1057. /* Get next transmit descriptor */
  1058. tx_idx = ( intelxl->tx.cons % INTELXL_TX_NUM_DESC );
  1059. tx_wb = &intelxl->tx.desc[tx_idx].tx_wb;
  1060. /* Stop if descriptor is still in use */
  1061. if ( ! ( tx_wb->flags & INTELXL_TX_WB_FL_DD ) )
  1062. return;
  1063. DBGC2 ( intelxl, "INTELXL %p TX %d complete\n",
  1064. intelxl, tx_idx );
  1065. /* Complete TX descriptor */
  1066. netdev_tx_complete_next ( netdev );
  1067. intelxl->tx.cons++;
  1068. }
  1069. }
  1070. /**
  1071. * Poll for received packets
  1072. *
  1073. * @v netdev Network device
  1074. */
  1075. static void intelxl_poll_rx ( struct net_device *netdev ) {
  1076. struct intelxl_nic *intelxl = netdev->priv;
  1077. struct intelxl_rx_writeback_descriptor *rx_wb;
  1078. struct io_buffer *iobuf;
  1079. unsigned int rx_idx;
  1080. unsigned int tag;
  1081. size_t len;
  1082. /* Check for received packets */
  1083. while ( intelxl->rx.cons != intelxl->rx.prod ) {
  1084. /* Get next receive descriptor */
  1085. rx_idx = ( intelxl->rx.cons % INTELXL_RX_NUM_DESC );
  1086. rx_wb = &intelxl->rx.desc[rx_idx].rx_wb;
  1087. /* Stop if descriptor is still in use */
  1088. if ( ! ( rx_wb->flags & cpu_to_le32 ( INTELXL_RX_WB_FL_DD ) ) )
  1089. return;
  1090. /* Populate I/O buffer */
  1091. iobuf = intelxl->rx_iobuf[rx_idx];
  1092. intelxl->rx_iobuf[rx_idx] = NULL;
  1093. len = INTELXL_RX_WB_LEN ( le32_to_cpu ( rx_wb->len ) );
  1094. iob_put ( iobuf, len );
  1095. /* Find VLAN device, if applicable */
  1096. if ( rx_wb->flags & cpu_to_le32 ( INTELXL_RX_WB_FL_VLAN ) ) {
  1097. tag = VLAN_TAG ( le16_to_cpu ( rx_wb->vlan ) );
  1098. } else {
  1099. tag = 0;
  1100. }
  1101. /* Hand off to network stack */
  1102. if ( rx_wb->flags & cpu_to_le32 ( INTELXL_RX_WB_FL_RXE ) ) {
  1103. DBGC ( intelxl, "INTELXL %p RX %d error (length %zd, "
  1104. "flags %08x)\n", intelxl, rx_idx, len,
  1105. le32_to_cpu ( rx_wb->flags ) );
  1106. vlan_netdev_rx_err ( netdev, tag, iobuf, -EIO );
  1107. } else {
  1108. DBGC2 ( intelxl, "INTELXL %p RX %d complete (length "
  1109. "%zd)\n", intelxl, rx_idx, len );
  1110. vlan_netdev_rx ( netdev, tag, iobuf );
  1111. }
  1112. intelxl->rx.cons++;
  1113. }
  1114. }
  1115. /**
  1116. * Poll for completed and received packets
  1117. *
  1118. * @v netdev Network device
  1119. */
  1120. static void intelxl_poll ( struct net_device *netdev ) {
  1121. struct intelxl_nic *intelxl = netdev->priv;
  1122. /* Acknowledge interrupts, if applicable */
  1123. if ( netdev_irq_enabled ( netdev ) ) {
  1124. writel ( ( INTELXL_PFINT_DYN_CTL0_CLEARPBA |
  1125. INTELXL_PFINT_DYN_CTL0_INTENA_MASK ),
  1126. intelxl->regs + INTELXL_PFINT_DYN_CTL0 );
  1127. }
  1128. /* Poll for completed packets */
  1129. intelxl_poll_tx ( netdev );
  1130. /* Poll for received packets */
  1131. intelxl_poll_rx ( netdev );
  1132. /* Poll for admin events */
  1133. intelxl_poll_admin ( netdev );
  1134. /* Refill RX ring */
  1135. intelxl_refill_rx ( intelxl );
  1136. }
  1137. /**
  1138. * Enable or disable interrupts
  1139. *
  1140. * @v netdev Network device
  1141. * @v enable Interrupts should be enabled
  1142. */
  1143. static void intelxl_irq ( struct net_device *netdev, int enable ) {
  1144. struct intelxl_nic *intelxl = netdev->priv;
  1145. if ( enable ) {
  1146. writel ( INTELXL_PFINT_DYN_CTL0_INTENA,
  1147. intelxl->regs + INTELXL_PFINT_DYN_CTL0 );
  1148. } else {
  1149. writel ( 0, intelxl->regs + INTELXL_PFINT_DYN_CTL0 );
  1150. }
  1151. }
  1152. /** Network device operations */
  1153. static struct net_device_operations intelxl_operations = {
  1154. .open = intelxl_open,
  1155. .close = intelxl_close,
  1156. .transmit = intelxl_transmit,
  1157. .poll = intelxl_poll,
  1158. .irq = intelxl_irq,
  1159. };
  1160. /******************************************************************************
  1161. *
  1162. * PCI interface
  1163. *
  1164. ******************************************************************************
  1165. */
  1166. /**
  1167. * Probe PCI device
  1168. *
  1169. * @v pci PCI device
  1170. * @ret rc Return status code
  1171. */
  1172. static int intelxl_probe ( struct pci_device *pci ) {
  1173. struct net_device *netdev;
  1174. struct intelxl_nic *intelxl;
  1175. uint32_t pfgen_portnum;
  1176. uint32_t pflan_qalloc;
  1177. int rc;
  1178. /* Allocate and initialise net device */
  1179. netdev = alloc_etherdev ( sizeof ( *intelxl ) );
  1180. if ( ! netdev ) {
  1181. rc = -ENOMEM;
  1182. goto err_alloc;
  1183. }
  1184. netdev_init ( netdev, &intelxl_operations );
  1185. intelxl = netdev->priv;
  1186. pci_set_drvdata ( pci, netdev );
  1187. netdev->dev = &pci->dev;
  1188. memset ( intelxl, 0, sizeof ( *intelxl ) );
  1189. intelxl->pf = PCI_FUNC ( pci->busdevfn );
  1190. intelxl_init_admin ( &intelxl->command, INTELXL_ADMIN_CMD,
  1191. &intelxl_admin_offsets );
  1192. intelxl_init_admin ( &intelxl->event, INTELXL_ADMIN_EVT,
  1193. &intelxl_admin_offsets );
  1194. intelxl_init_ring ( &intelxl->tx, INTELXL_TX_NUM_DESC,
  1195. intelxl_context_tx );
  1196. intelxl_init_ring ( &intelxl->rx, INTELXL_RX_NUM_DESC,
  1197. intelxl_context_rx );
  1198. /* Fix up PCI device */
  1199. adjust_pci_device ( pci );
  1200. /* Map registers */
  1201. intelxl->regs = ioremap ( pci->membase, INTELXL_BAR_SIZE );
  1202. if ( ! intelxl->regs ) {
  1203. rc = -ENODEV;
  1204. goto err_ioremap;
  1205. }
  1206. /* Reset the NIC */
  1207. if ( ( rc = intelxl_reset ( intelxl ) ) != 0 )
  1208. goto err_reset;
  1209. /* Get port number and base queue number */
  1210. pfgen_portnum = readl ( intelxl->regs + INTELXL_PFGEN_PORTNUM );
  1211. intelxl->port = INTELXL_PFGEN_PORTNUM_PORT_NUM ( pfgen_portnum );
  1212. pflan_qalloc = readl ( intelxl->regs + INTELXL_PFLAN_QALLOC );
  1213. intelxl->base = INTELXL_PFLAN_QALLOC_FIRSTQ ( pflan_qalloc );
  1214. DBGC ( intelxl, "INTELXL %p PF %d using port %d queues [%#04x-%#04x]\n",
  1215. intelxl, intelxl->pf, intelxl->port, intelxl->base,
  1216. INTELXL_PFLAN_QALLOC_LASTQ ( pflan_qalloc ) );
  1217. /* Fetch MAC address and maximum frame size */
  1218. if ( ( rc = intelxl_fetch_mac ( intelxl, netdev ) ) != 0 )
  1219. goto err_fetch_mac;
  1220. /* Open admin queues */
  1221. if ( ( rc = intelxl_open_admin ( intelxl ) ) != 0 )
  1222. goto err_open_admin;
  1223. /* Get switch configuration */
  1224. if ( ( rc = intelxl_admin_switch ( intelxl ) ) != 0 )
  1225. goto err_admin_switch;
  1226. /* Get VSI configuration */
  1227. if ( ( rc = intelxl_admin_vsi ( intelxl ) ) != 0 )
  1228. goto err_admin_vsi;
  1229. /* Configure switch for promiscuous mode */
  1230. if ( ( rc = intelxl_admin_promisc ( intelxl ) ) != 0 )
  1231. goto err_admin_promisc;
  1232. /* Configure queue register addresses */
  1233. intelxl->tx.reg = INTELXL_QTX ( intelxl->queue );
  1234. intelxl->rx.reg = INTELXL_QRX ( intelxl->queue );
  1235. /* Configure interrupt causes */
  1236. writel ( ( INTELXL_QINT_TQCTL_NEXTQ_INDX_NONE |
  1237. INTELXL_QINT_TQCTL_CAUSE_ENA ),
  1238. intelxl->regs + INTELXL_QINT_TQCTL ( intelxl->queue ) );
  1239. writel ( ( INTELXL_QINT_RQCTL_NEXTQ_INDX ( intelxl->queue ) |
  1240. INTELXL_QINT_RQCTL_NEXTQ_TYPE_TX |
  1241. INTELXL_QINT_RQCTL_CAUSE_ENA ),
  1242. intelxl->regs + INTELXL_QINT_RQCTL ( intelxl->queue ) );
  1243. writel ( ( INTELXL_PFINT_LNKLST0_FIRSTQ_INDX ( intelxl->queue ) |
  1244. INTELXL_PFINT_LNKLST0_FIRSTQ_TYPE_RX ),
  1245. intelxl->regs + INTELXL_PFINT_LNKLST0 );
  1246. writel ( INTELXL_PFINT_ICR0_ENA_ADMINQ,
  1247. intelxl->regs + INTELXL_PFINT_ICR0_ENA );
  1248. /* Register network device */
  1249. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  1250. goto err_register_netdev;
  1251. /* Set initial link state */
  1252. intelxl_admin_link ( netdev );
  1253. return 0;
  1254. unregister_netdev ( netdev );
  1255. err_register_netdev:
  1256. err_admin_promisc:
  1257. err_admin_vsi:
  1258. err_admin_switch:
  1259. intelxl_close_admin ( intelxl );
  1260. err_open_admin:
  1261. err_fetch_mac:
  1262. intelxl_reset ( intelxl );
  1263. err_reset:
  1264. iounmap ( intelxl->regs );
  1265. err_ioremap:
  1266. netdev_nullify ( netdev );
  1267. netdev_put ( netdev );
  1268. err_alloc:
  1269. return rc;
  1270. }
  1271. /**
  1272. * Remove PCI device
  1273. *
  1274. * @v pci PCI device
  1275. */
  1276. static void intelxl_remove ( struct pci_device *pci ) {
  1277. struct net_device *netdev = pci_get_drvdata ( pci );
  1278. struct intelxl_nic *intelxl = netdev->priv;
  1279. /* Unregister network device */
  1280. unregister_netdev ( netdev );
  1281. /* Close admin queues */
  1282. intelxl_close_admin ( intelxl );
  1283. /* Reset the NIC */
  1284. intelxl_reset ( intelxl );
  1285. /* Free network device */
  1286. iounmap ( intelxl->regs );
  1287. netdev_nullify ( netdev );
  1288. netdev_put ( netdev );
  1289. }
  1290. /** PCI device IDs */
  1291. static struct pci_device_id intelxl_nics[] = {
  1292. PCI_ROM ( 0x8086, 0x1572, "x710-sfp", "X710 10GbE SFP+", 0 ),
  1293. PCI_ROM ( 0x8086, 0x1574, "xl710-qemu", "Virtual XL710", 0 ),
  1294. PCI_ROM ( 0x8086, 0x1580, "xl710-kx-b", "XL710 40GbE backplane", 0 ),
  1295. PCI_ROM ( 0x8086, 0x1581, "xl710-kx-c", "XL710 10GbE backplane", 0 ),
  1296. PCI_ROM ( 0x8086, 0x1583, "xl710-qda2", "XL710 40GbE QSFP+", 0 ),
  1297. PCI_ROM ( 0x8086, 0x1584, "xl710-qda1", "XL710 40GbE QSFP+", 0 ),
  1298. PCI_ROM ( 0x8086, 0x1585, "x710-qsfp", "X710 10GbE QSFP+", 0 ),
  1299. PCI_ROM ( 0x8086, 0x1586, "x710-10gt", "X710 10GBASE-T", 0 ),
  1300. PCI_ROM ( 0x8086, 0x1587, "x710-kr2", "XL710 20GbE backplane", 0 ),
  1301. PCI_ROM ( 0x8086, 0x1588, "x710-kr2-a", "XL710 20GbE backplane", 0 ),
  1302. PCI_ROM ( 0x8086, 0x1589, "x710-10gt4", "X710 10GBASE-T4", 0 ),
  1303. PCI_ROM ( 0x8086, 0x158a, "xxv710", "XXV710 25GbE backplane", 0 ),
  1304. PCI_ROM ( 0x8086, 0x158b, "xxv710-sfp28", "XXV710 25GbE SFP28", 0 ),
  1305. PCI_ROM ( 0x8086, 0x37ce, "x722-kx", "X722 10GbE backplane", 0 ),
  1306. PCI_ROM ( 0x8086, 0x37cf, "x722-qsfp", "X722 10GbE QSFP+", 0 ),
  1307. PCI_ROM ( 0x8086, 0x37d0, "x722-sfp", "X722 10GbE SFP+", 0 ),
  1308. PCI_ROM ( 0x8086, 0x37d1, "x722-1gt", "X722 1GBASE-T", 0 ),
  1309. PCI_ROM ( 0x8086, 0x37d2, "x722-10gt", "X722 10GBASE-T", 0 ),
  1310. PCI_ROM ( 0x8086, 0x37d3, "x722-sfp-i", "X722 10GbE SFP+", 0 ),
  1311. };
  1312. /** PCI driver */
  1313. struct pci_driver intelxl_driver __pci_driver = {
  1314. .ids = intelxl_nics,
  1315. .id_count = ( sizeof ( intelxl_nics ) / sizeof ( intelxl_nics[0] ) ),
  1316. .probe = intelxl_probe,
  1317. .remove = intelxl_remove,
  1318. };