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

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