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.

phantom.c 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. * Copyright (C) 2008 NetXen, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <byteswap.h>
  26. #include <gpxe/pci.h>
  27. #include <gpxe/malloc.h>
  28. #include <gpxe/iobuf.h>
  29. #include <gpxe/netdevice.h>
  30. #include <gpxe/if_ether.h>
  31. #include <gpxe/ethernet.h>
  32. #include <gpxe/spi.h>
  33. #include "phantom.h"
  34. /**
  35. * @file
  36. *
  37. * NetXen Phantom NICs
  38. *
  39. */
  40. /** Maximum time to wait for SPI lock */
  41. #define PHN_SPI_LOCK_TIMEOUT_MS 100
  42. /** Maximum time to wait for SPI command to be issued */
  43. #define PHN_SPI_CMD_TIMEOUT_MS 100
  44. /** Maximum time to wait for command PEG to initialise
  45. *
  46. * BUGxxxx
  47. *
  48. * The command PEG will currently report initialisation complete only
  49. * when at least one PHY has detected a link (so that the global PHY
  50. * clock can be set to 10G/1G as appropriate). This can take a very,
  51. * very long time.
  52. *
  53. * A future firmware revision should decouple PHY initialisation from
  54. * firmware initialisation, at which point the command PEG will report
  55. * initialisation complete much earlier, and this timeout can be
  56. * reduced.
  57. */
  58. #define PHN_CMDPEG_INIT_TIMEOUT_SEC 50
  59. /** Maximum time to wait for receive PEG to initialise */
  60. #define PHN_RCVPEG_INIT_TIMEOUT_SEC 2
  61. /** Maximum time to wait for firmware to accept a command */
  62. #define PHN_ISSUE_CMD_TIMEOUT_MS 2000
  63. /** Maximum time to wait for test memory */
  64. #define PHN_TEST_MEM_TIMEOUT_MS 100
  65. /** Link state poll frequency
  66. *
  67. * The link state will be checked once in every N calls to poll().
  68. */
  69. #define PHN_LINK_POLL_FREQUENCY 4096
  70. /** Number of RX descriptors */
  71. #define PHN_NUM_RDS 32
  72. /** RX maximum fill level. Must be strictly less than PHN_NUM_RDS. */
  73. #define PHN_RDS_MAX_FILL 16
  74. /** RX buffer size */
  75. #define PHN_RX_BUFSIZE ( 32 /* max LL padding added by card */ + \
  76. ETH_FRAME_LEN )
  77. /** Number of RX status descriptors */
  78. #define PHN_NUM_SDS 32
  79. /** Number of TX descriptors */
  80. #define PHN_NUM_CDS 8
  81. /** A Phantom descriptor ring set */
  82. struct phantom_descriptor_rings {
  83. /** RX descriptors */
  84. struct phantom_rds rds[PHN_NUM_RDS];
  85. /** RX status descriptors */
  86. struct phantom_sds sds[PHN_NUM_SDS];
  87. /** TX descriptors */
  88. union phantom_cds cds[PHN_NUM_CDS];
  89. /** TX consumer index */
  90. volatile uint32_t cmd_cons;
  91. };
  92. /** A Phantom NIC port */
  93. struct phantom_nic_port {
  94. /** Phantom NIC containing this port */
  95. struct phantom_nic *phantom;
  96. /** Port number */
  97. unsigned int port;
  98. /** RX context ID */
  99. uint16_t rx_context_id;
  100. /** RX descriptor producer CRB offset */
  101. unsigned long rds_producer_crb;
  102. /** RX status descriptor consumer CRB offset */
  103. unsigned long sds_consumer_crb;
  104. /** RX producer index */
  105. unsigned int rds_producer_idx;
  106. /** RX consumer index */
  107. unsigned int rds_consumer_idx;
  108. /** RX status consumer index */
  109. unsigned int sds_consumer_idx;
  110. /** RX I/O buffers */
  111. struct io_buffer *rds_iobuf[PHN_RDS_MAX_FILL];
  112. /** TX context ID */
  113. uint16_t tx_context_id;
  114. /** TX descriptor producer CRB offset */
  115. unsigned long cds_producer_crb;
  116. /** TX producer index */
  117. unsigned int cds_producer_idx;
  118. /** TX consumer index */
  119. unsigned int cds_consumer_idx;
  120. /** TX I/O buffers */
  121. struct io_buffer *cds_iobuf[PHN_NUM_CDS];
  122. /** Link state poll timer */
  123. unsigned long link_poll_timer;
  124. /** Descriptor rings */
  125. struct phantom_descriptor_rings *desc;
  126. };
  127. /** RX context creation request and response buffers */
  128. struct phantom_create_rx_ctx_rqrsp {
  129. struct {
  130. struct nx_hostrq_rx_ctx_s rx_ctx;
  131. struct nx_hostrq_rds_ring_s rds;
  132. struct nx_hostrq_sds_ring_s sds;
  133. } __unm_dma_aligned hostrq;
  134. struct {
  135. struct nx_cardrsp_rx_ctx_s rx_ctx;
  136. struct nx_cardrsp_rds_ring_s rds;
  137. struct nx_cardrsp_sds_ring_s sds;
  138. } __unm_dma_aligned cardrsp;
  139. };
  140. /** TX context creation request and response buffers */
  141. struct phantom_create_tx_ctx_rqrsp {
  142. struct {
  143. struct nx_hostrq_tx_ctx_s tx_ctx;
  144. } __unm_dma_aligned hostrq;
  145. struct {
  146. struct nx_cardrsp_tx_ctx_s tx_ctx;
  147. } __unm_dma_aligned cardrsp;
  148. };
  149. /** A Phantom DMA buffer area */
  150. union phantom_dma_buffer {
  151. /** Dummy area required for (read-only) self-tests */
  152. uint8_t dummy_dma[UNM_DUMMY_DMA_SIZE];
  153. /** RX context creation request and response buffers */
  154. struct phantom_create_rx_ctx_rqrsp create_rx_ctx;
  155. /** TX context creation request and response buffers */
  156. struct phantom_create_tx_ctx_rqrsp create_tx_ctx;
  157. };
  158. /** A Phantom NIC */
  159. struct phantom_nic {
  160. /** BAR 0 */
  161. void *bar0;
  162. /** Current CRB window */
  163. unsigned long crb_window;
  164. /** CRB window access method */
  165. unsigned long ( *crb_access ) ( struct phantom_nic *phantom,
  166. unsigned long reg );
  167. /** Number of ports */
  168. int num_ports;
  169. /** Per-port network devices */
  170. struct net_device *netdev[UNM_FLASH_NUM_PORTS];
  171. /** DMA buffers */
  172. union phantom_dma_buffer *dma_buf;
  173. /** Flash memory SPI bus */
  174. struct spi_bus spi_bus;
  175. /** Flash memory SPI device */
  176. struct spi_device flash;
  177. /** Last known link state */
  178. uint32_t link_state;
  179. };
  180. /***************************************************************************
  181. *
  182. * CRB register access
  183. *
  184. */
  185. /**
  186. * Prepare for access to CRB register via 128MB BAR
  187. *
  188. * @v phantom Phantom NIC
  189. * @v reg Register offset within abstract address space
  190. * @ret offset Register offset within PCI BAR0
  191. */
  192. static unsigned long phantom_crb_access_128m ( struct phantom_nic *phantom,
  193. unsigned long reg ) {
  194. static const uint32_t reg_window[] = {
  195. [UNM_CRB_BLK_PCIE] = 0x0000000,
  196. [UNM_CRB_BLK_CAM] = 0x2000000,
  197. [UNM_CRB_BLK_ROMUSB] = 0x2000000,
  198. [UNM_CRB_BLK_TEST] = 0x0000000,
  199. };
  200. static const uint32_t reg_bases[] = {
  201. [UNM_CRB_BLK_PCIE] = 0x6100000,
  202. [UNM_CRB_BLK_CAM] = 0x6200000,
  203. [UNM_CRB_BLK_ROMUSB] = 0x7300000,
  204. [UNM_CRB_BLK_TEST] = 0x6200000,
  205. };
  206. unsigned int block = UNM_CRB_BLK ( reg );
  207. unsigned long offset = UNM_CRB_OFFSET ( reg );
  208. uint32_t window = reg_window[block];
  209. uint32_t verify_window;
  210. if ( phantom->crb_window != window ) {
  211. /* Write to the CRB window register */
  212. writel ( window, phantom->bar0 + UNM_128M_CRB_WINDOW );
  213. /* Ensure that the write has reached the card */
  214. verify_window = readl ( phantom->bar0 + UNM_128M_CRB_WINDOW );
  215. assert ( verify_window == window );
  216. /* Record new window */
  217. phantom->crb_window = window;
  218. }
  219. return ( reg_bases[block] + offset );
  220. }
  221. /**
  222. * Prepare for access to CRB register via 32MB BAR
  223. *
  224. * @v phantom Phantom NIC
  225. * @v reg Register offset within abstract address space
  226. * @ret offset Register offset within PCI BAR0
  227. */
  228. static unsigned long phantom_crb_access_32m ( struct phantom_nic *phantom,
  229. unsigned long reg ) {
  230. static const uint32_t reg_window[] = {
  231. [UNM_CRB_BLK_PCIE] = 0x0000000,
  232. [UNM_CRB_BLK_CAM] = 0x2000000,
  233. [UNM_CRB_BLK_ROMUSB] = 0x2000000,
  234. [UNM_CRB_BLK_TEST] = 0x0000000,
  235. };
  236. static const uint32_t reg_bases[] = {
  237. [UNM_CRB_BLK_PCIE] = 0x0100000,
  238. [UNM_CRB_BLK_CAM] = 0x0200000,
  239. [UNM_CRB_BLK_ROMUSB] = 0x1300000,
  240. [UNM_CRB_BLK_TEST] = 0x0200000,
  241. };
  242. unsigned int block = UNM_CRB_BLK ( reg );
  243. unsigned long offset = UNM_CRB_OFFSET ( reg );
  244. uint32_t window = reg_window[block];
  245. uint32_t verify_window;
  246. if ( phantom->crb_window != window ) {
  247. /* Write to the CRB window register */
  248. writel ( window, phantom->bar0 + UNM_32M_CRB_WINDOW );
  249. /* Ensure that the write has reached the card */
  250. verify_window = readl ( phantom->bar0 + UNM_32M_CRB_WINDOW );
  251. assert ( verify_window == window );
  252. /* Record new window */
  253. phantom->crb_window = window;
  254. }
  255. return ( reg_bases[block] + offset );
  256. }
  257. /**
  258. * Prepare for access to CRB register via 2MB BAR
  259. *
  260. * @v phantom Phantom NIC
  261. * @v reg Register offset within abstract address space
  262. * @ret offset Register offset within PCI BAR0
  263. */
  264. static unsigned long phantom_crb_access_2m ( struct phantom_nic *phantom,
  265. unsigned long reg ) {
  266. static const uint32_t reg_window_hi[] = {
  267. [UNM_CRB_BLK_PCIE] = 0x77300000,
  268. [UNM_CRB_BLK_CAM] = 0x41600000,
  269. [UNM_CRB_BLK_ROMUSB] = 0x42100000,
  270. [UNM_CRB_BLK_TEST] = 0x29500000,
  271. };
  272. unsigned int block = UNM_CRB_BLK ( reg );
  273. unsigned long offset = UNM_CRB_OFFSET ( reg );
  274. uint32_t window = ( reg_window_hi[block] | ( offset & 0x000f0000 ) );
  275. uint32_t verify_window;
  276. if ( phantom->crb_window != window ) {
  277. /* Write to the CRB window register */
  278. writel ( window, phantom->bar0 + UNM_2M_CRB_WINDOW );
  279. /* Ensure that the write has reached the card */
  280. verify_window = readl ( phantom->bar0 + UNM_2M_CRB_WINDOW );
  281. assert ( verify_window == window );
  282. /* Record new window */
  283. phantom->crb_window = window;
  284. }
  285. return ( 0x1e0000 + ( offset & 0xffff ) );
  286. }
  287. /**
  288. * Read from Phantom CRB register
  289. *
  290. * @v phantom Phantom NIC
  291. * @v reg Register offset within abstract address space
  292. * @ret value Register value
  293. */
  294. static uint32_t phantom_readl ( struct phantom_nic *phantom,
  295. unsigned long reg ) {
  296. unsigned long offset;
  297. offset = phantom->crb_access ( phantom, reg );
  298. return readl ( phantom->bar0 + offset );
  299. }
  300. /**
  301. * Write to Phantom CRB register
  302. *
  303. * @v phantom Phantom NIC
  304. * @v value Register value
  305. * @v reg Register offset within abstract address space
  306. */
  307. static void phantom_writel ( struct phantom_nic *phantom, uint32_t value,
  308. unsigned long reg ) {
  309. unsigned long offset;
  310. offset = phantom->crb_access ( phantom, reg );
  311. writel ( value, phantom->bar0 + offset );
  312. }
  313. /**
  314. * Write to Phantom CRB HI/LO register pair
  315. *
  316. * @v phantom Phantom NIC
  317. * @v value Register value
  318. * @v lo_offset LO register offset within CRB
  319. * @v hi_offset HI register offset within CRB
  320. */
  321. static inline void phantom_write_hilo ( struct phantom_nic *phantom,
  322. uint64_t value,
  323. unsigned long lo_offset,
  324. unsigned long hi_offset ) {
  325. uint32_t lo = ( value & 0xffffffffUL );
  326. uint32_t hi = ( value >> 32 );
  327. phantom_writel ( phantom, lo, lo_offset );
  328. phantom_writel ( phantom, hi, hi_offset );
  329. }
  330. /***************************************************************************
  331. *
  332. * Firmware message buffer access (for debug)
  333. *
  334. */
  335. /**
  336. * Read from Phantom test memory
  337. *
  338. * @v phantom Phantom NIC
  339. * @v offset Offset within test memory
  340. * @v buf 8-byte buffer to fill
  341. * @ret rc Return status code
  342. */
  343. static int phantom_read_test_mem ( struct phantom_nic *phantom,
  344. uint64_t offset, uint32_t buf[2] ) {
  345. unsigned int retries;
  346. uint32_t test_control;
  347. phantom_write_hilo ( phantom, offset, UNM_TEST_ADDR_LO,
  348. UNM_TEST_ADDR_HI );
  349. phantom_writel ( phantom, UNM_TEST_CONTROL_ENABLE, UNM_TEST_CONTROL );
  350. phantom_writel ( phantom,
  351. ( UNM_TEST_CONTROL_ENABLE | UNM_TEST_CONTROL_START ),
  352. UNM_TEST_CONTROL );
  353. for ( retries = 0 ; retries < PHN_TEST_MEM_TIMEOUT_MS ; retries++ ) {
  354. test_control = phantom_readl ( phantom, UNM_TEST_CONTROL );
  355. if ( ( test_control & UNM_TEST_CONTROL_BUSY ) == 0 ) {
  356. buf[0] = phantom_readl ( phantom, UNM_TEST_RDDATA_LO );
  357. buf[1] = phantom_readl ( phantom, UNM_TEST_RDDATA_HI );
  358. return 0;
  359. }
  360. mdelay ( 1 );
  361. }
  362. DBGC ( phantom, "Phantom %p timed out waiting for test memory\n",
  363. phantom );
  364. return -ETIMEDOUT;
  365. }
  366. /**
  367. * Dump Phantom firmware dmesg log
  368. *
  369. * @v phantom Phantom NIC
  370. * @v log Log number
  371. */
  372. static void phantom_dmesg ( struct phantom_nic *phantom, unsigned int log ) {
  373. uint32_t head;
  374. uint32_t tail;
  375. uint32_t len;
  376. uint32_t sig;
  377. uint32_t offset;
  378. union {
  379. uint8_t bytes[8];
  380. uint32_t dwords[2];
  381. } buf;
  382. unsigned int i;
  383. int rc;
  384. /* Optimise out for non-debug builds */
  385. if ( ! DBG_LOG )
  386. return;
  387. head = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_HEAD ( log ) );
  388. len = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_LEN ( log ) );
  389. tail = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_TAIL ( log ) );
  390. sig = phantom_readl ( phantom, UNM_CAM_RAM_DMESG_SIG ( log ) );
  391. DBGC ( phantom, "Phantom %p firmware dmesg buffer %d (%08lx-%08lx)\n",
  392. phantom, log, head, tail );
  393. assert ( ( head & 0x07 ) == 0 );
  394. if ( sig != UNM_CAM_RAM_DMESG_SIG_MAGIC ) {
  395. DBGC ( phantom, "Warning: bad signature %08lx (want %08lx)\n",
  396. sig, UNM_CAM_RAM_DMESG_SIG_MAGIC );
  397. }
  398. for ( offset = head ; offset < tail ; offset += 8 ) {
  399. if ( ( rc = phantom_read_test_mem ( phantom, offset,
  400. buf.dwords ) ) != 0 ) {
  401. DBGC ( phantom, "Phantom %p could not read from test "
  402. "memory: %s\n", phantom, strerror ( rc ) );
  403. break;
  404. }
  405. for ( i = 0 ; ( ( i < sizeof ( buf ) ) &&
  406. ( offset + i ) < tail ) ; i++ ) {
  407. DBG ( "%c", buf.bytes[i] );
  408. }
  409. }
  410. DBG ( "\n" );
  411. }
  412. /**
  413. * Dump Phantom firmware dmesg logs
  414. *
  415. * @v phantom Phantom NIC
  416. */
  417. static void __attribute__ (( unused ))
  418. phantom_dmesg_all ( struct phantom_nic *phantom ) {
  419. unsigned int i;
  420. for ( i = 0 ; i < UNM_CAM_RAM_NUM_DMESG_BUFFERS ; i++ )
  421. phantom_dmesg ( phantom, i );
  422. }
  423. /***************************************************************************
  424. *
  425. * SPI bus access (for flash memory)
  426. *
  427. */
  428. /**
  429. * Acquire Phantom SPI lock
  430. *
  431. * @v phantom Phantom NIC
  432. * @ret rc Return status code
  433. */
  434. static int phantom_spi_lock ( struct phantom_nic *phantom ) {
  435. unsigned int retries;
  436. uint32_t pcie_sem2_lock;
  437. for ( retries = 0 ; retries < PHN_SPI_LOCK_TIMEOUT_MS ; retries++ ) {
  438. pcie_sem2_lock = phantom_readl ( phantom, UNM_PCIE_SEM2_LOCK );
  439. if ( pcie_sem2_lock != 0 )
  440. return 0;
  441. mdelay ( 1 );
  442. }
  443. DBGC ( phantom, "Phantom %p timed out waiting for SPI lock\n",
  444. phantom );
  445. return -ETIMEDOUT;
  446. }
  447. /**
  448. * Wait for Phantom SPI command to complete
  449. *
  450. * @v phantom Phantom NIC
  451. * @ret rc Return status code
  452. */
  453. static int phantom_spi_wait ( struct phantom_nic *phantom ) {
  454. unsigned int retries;
  455. uint32_t glb_status;
  456. for ( retries = 0 ; retries < PHN_SPI_CMD_TIMEOUT_MS ; retries++ ) {
  457. glb_status = phantom_readl ( phantom, UNM_ROMUSB_GLB_STATUS );
  458. if ( glb_status & UNM_ROMUSB_GLB_STATUS_ROM_DONE )
  459. return 0;
  460. mdelay ( 1 );
  461. }
  462. DBGC ( phantom, "Phantom %p timed out waiting for SPI command\n",
  463. phantom );
  464. return -ETIMEDOUT;
  465. }
  466. /**
  467. * Release Phantom SPI lock
  468. *
  469. * @v phantom Phantom NIC
  470. */
  471. static void phantom_spi_unlock ( struct phantom_nic *phantom ) {
  472. phantom_readl ( phantom, UNM_PCIE_SEM2_UNLOCK );
  473. }
  474. /**
  475. * Read/write data via Phantom SPI bus
  476. *
  477. * @v bus SPI bus
  478. * @v device SPI device
  479. * @v command Command
  480. * @v address Address to read/write (<0 for no address)
  481. * @v data_out TX data buffer (or NULL)
  482. * @v data_in RX data buffer (or NULL)
  483. * @v len Length of data buffer(s)
  484. * @ret rc Return status code
  485. */
  486. static int phantom_spi_rw ( struct spi_bus *bus,
  487. struct spi_device *device,
  488. unsigned int command, int address,
  489. const void *data_out, void *data_in,
  490. size_t len ) {
  491. struct phantom_nic *phantom =
  492. container_of ( bus, struct phantom_nic, spi_bus );
  493. uint32_t data;
  494. int rc;
  495. DBGCP ( phantom, "Phantom %p SPI command %x at %x+%zx\n",
  496. phantom, command, address, len );
  497. if ( data_out )
  498. DBGCP_HDA ( phantom, address, data_out, len );
  499. /* We support only exactly 4-byte reads */
  500. if ( len != UNM_SPI_BLKSIZE ) {
  501. DBGC ( phantom, "Phantom %p invalid SPI length %zx\n",
  502. phantom, len );
  503. return -EINVAL;
  504. }
  505. /* Acquire SPI lock */
  506. if ( ( rc = phantom_spi_lock ( phantom ) ) != 0 )
  507. goto err_lock;
  508. /* Issue SPI command as per the PRM */
  509. if ( data_out ) {
  510. memcpy ( &data, data_out, sizeof ( data ) );
  511. phantom_writel ( phantom, data, UNM_ROMUSB_ROM_WDATA );
  512. }
  513. phantom_writel ( phantom, address, UNM_ROMUSB_ROM_ADDRESS );
  514. phantom_writel ( phantom, ( device->address_len / 8 ),
  515. UNM_ROMUSB_ROM_ABYTE_CNT );
  516. udelay ( 100 ); /* according to PRM */
  517. phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_DUMMY_BYTE_CNT );
  518. phantom_writel ( phantom, command, UNM_ROMUSB_ROM_INSTR_OPCODE );
  519. /* Wait for SPI command to complete */
  520. if ( ( rc = phantom_spi_wait ( phantom ) ) != 0 )
  521. goto err_wait;
  522. /* Reset address byte count and dummy byte count, because the
  523. * PRM asks us to.
  524. */
  525. phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_ABYTE_CNT );
  526. udelay ( 100 ); /* according to PRM */
  527. phantom_writel ( phantom, 0, UNM_ROMUSB_ROM_DUMMY_BYTE_CNT );
  528. /* Read data, if applicable */
  529. if ( data_in ) {
  530. data = phantom_readl ( phantom, UNM_ROMUSB_ROM_RDATA );
  531. memcpy ( data_in, &data, sizeof ( data ) );
  532. DBGCP_HDA ( phantom, address, data_in, len );
  533. }
  534. err_wait:
  535. phantom_spi_unlock ( phantom );
  536. err_lock:
  537. return rc;
  538. }
  539. /***************************************************************************
  540. *
  541. * Firmware interface
  542. *
  543. */
  544. /**
  545. * Wait for firmware to accept command
  546. *
  547. * @v phantom Phantom NIC
  548. * @ret rc Return status code
  549. */
  550. static int phantom_wait_for_cmd ( struct phantom_nic *phantom ) {
  551. unsigned int retries;
  552. uint32_t cdrp;
  553. for ( retries = 0 ; retries < PHN_ISSUE_CMD_TIMEOUT_MS ; retries++ ) {
  554. mdelay ( 1 );
  555. cdrp = phantom_readl ( phantom, UNM_NIC_REG_NX_CDRP );
  556. if ( NX_CDRP_IS_RSP ( cdrp ) ) {
  557. switch ( NX_CDRP_FORM_RSP ( cdrp ) ) {
  558. case NX_CDRP_RSP_OK:
  559. return 0;
  560. case NX_CDRP_RSP_FAIL:
  561. return -EIO;
  562. case NX_CDRP_RSP_TIMEOUT:
  563. return -ETIMEDOUT;
  564. default:
  565. return -EPROTO;
  566. }
  567. }
  568. }
  569. DBGC ( phantom, "Phantom %p timed out waiting for firmware to accept "
  570. "command\n", phantom );
  571. return -ETIMEDOUT;
  572. }
  573. /**
  574. * Issue command to firmware
  575. *
  576. * @v phantom_port Phantom NIC port
  577. * @v command Firmware command
  578. * @v arg1 Argument 1
  579. * @v arg2 Argument 2
  580. * @v arg3 Argument 3
  581. * @ret rc Return status code
  582. */
  583. static int phantom_issue_cmd ( struct phantom_nic_port *phantom_port,
  584. uint32_t command, uint32_t arg1, uint32_t arg2,
  585. uint32_t arg3 ) {
  586. struct phantom_nic *phantom = phantom_port->phantom;
  587. uint32_t signature;
  588. int rc;
  589. /* Issue command */
  590. signature = NX_CDRP_SIGNATURE_MAKE ( phantom_port->port,
  591. NXHAL_VERSION );
  592. DBGC2 ( phantom, "Phantom %p port %d issuing command %08lx (%08lx, "
  593. "%08lx, %08lx)\n", phantom, phantom_port->port,
  594. command, arg1, arg2, arg3 );
  595. phantom_writel ( phantom, signature, UNM_NIC_REG_NX_SIGN );
  596. phantom_writel ( phantom, arg1, UNM_NIC_REG_NX_ARG1 );
  597. phantom_writel ( phantom, arg2, UNM_NIC_REG_NX_ARG2 );
  598. phantom_writel ( phantom, arg3, UNM_NIC_REG_NX_ARG3 );
  599. phantom_writel ( phantom, NX_CDRP_FORM_CMD ( command ),
  600. UNM_NIC_REG_NX_CDRP );
  601. /* Wait for command to be accepted */
  602. if ( ( rc = phantom_wait_for_cmd ( phantom ) ) != 0 ) {
  603. DBGC ( phantom, "Phantom %p could not issue command: %s\n",
  604. phantom, strerror ( rc ) );
  605. return rc;
  606. }
  607. return 0;
  608. }
  609. /**
  610. * Issue buffer-format command to firmware
  611. *
  612. * @v phantom_port Phantom NIC port
  613. * @v command Firmware command
  614. * @v buffer Buffer to pass to firmware
  615. * @v len Length of buffer
  616. * @ret rc Return status code
  617. */
  618. static int phantom_issue_buf_cmd ( struct phantom_nic_port *phantom_port,
  619. uint32_t command, void *buffer,
  620. size_t len ) {
  621. uint64_t physaddr;
  622. physaddr = virt_to_bus ( buffer );
  623. return phantom_issue_cmd ( phantom_port, command, ( physaddr >> 32 ),
  624. ( physaddr & 0xffffffffUL ), len );
  625. }
  626. /**
  627. * Create Phantom RX context
  628. *
  629. * @v phantom_port Phantom NIC port
  630. * @ret rc Return status code
  631. */
  632. static int phantom_create_rx_ctx ( struct phantom_nic_port *phantom_port ) {
  633. struct phantom_nic *phantom = phantom_port->phantom;
  634. struct phantom_create_rx_ctx_rqrsp *buf;
  635. int rc;
  636. /* Prepare request */
  637. buf = &phantom->dma_buf->create_rx_ctx;
  638. memset ( buf, 0, sizeof ( *buf ) );
  639. buf->hostrq.rx_ctx.host_rsp_dma_addr =
  640. cpu_to_le64 ( virt_to_bus ( &buf->cardrsp ) );
  641. buf->hostrq.rx_ctx.capabilities[0] =
  642. cpu_to_le32 ( NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN );
  643. buf->hostrq.rx_ctx.host_int_crb_mode =
  644. cpu_to_le32 ( NX_HOST_INT_CRB_MODE_SHARED );
  645. buf->hostrq.rx_ctx.host_rds_crb_mode =
  646. cpu_to_le32 ( NX_HOST_RDS_CRB_MODE_UNIQUE );
  647. buf->hostrq.rx_ctx.rds_ring_offset = cpu_to_le32 ( 0 );
  648. buf->hostrq.rx_ctx.sds_ring_offset =
  649. cpu_to_le32 ( sizeof ( buf->hostrq.rds ) );
  650. buf->hostrq.rx_ctx.num_rds_rings = cpu_to_le16 ( 1 );
  651. buf->hostrq.rx_ctx.num_sds_rings = cpu_to_le16 ( 1 );
  652. buf->hostrq.rds.host_phys_addr =
  653. cpu_to_le64 ( virt_to_bus ( phantom_port->desc->rds ) );
  654. buf->hostrq.rds.buff_size = cpu_to_le64 ( PHN_RX_BUFSIZE );
  655. buf->hostrq.rds.ring_size = cpu_to_le32 ( PHN_NUM_RDS );
  656. buf->hostrq.rds.ring_kind = cpu_to_le32 ( NX_RDS_RING_TYPE_NORMAL );
  657. buf->hostrq.sds.host_phys_addr =
  658. cpu_to_le64 ( virt_to_bus ( phantom_port->desc->sds ) );
  659. buf->hostrq.sds.ring_size = cpu_to_le32 ( PHN_NUM_SDS );
  660. DBGC ( phantom, "Phantom %p port %d creating RX context\n",
  661. phantom, phantom_port->port );
  662. DBGC2_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
  663. &buf->hostrq, sizeof ( buf->hostrq ) );
  664. /* Issue request */
  665. if ( ( rc = phantom_issue_buf_cmd ( phantom_port,
  666. NX_CDRP_CMD_CREATE_RX_CTX,
  667. &buf->hostrq,
  668. sizeof ( buf->hostrq ) ) ) != 0 ) {
  669. DBGC ( phantom, "Phantom %p port %d could not create RX "
  670. "context: %s\n",
  671. phantom, phantom_port->port, strerror ( rc ) );
  672. DBGC ( phantom, "Request:\n" );
  673. DBGC_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
  674. &buf->hostrq, sizeof ( buf->hostrq ) );
  675. DBGC ( phantom, "Response:\n" );
  676. DBGC_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
  677. &buf->cardrsp, sizeof ( buf->cardrsp ) );
  678. return rc;
  679. }
  680. /* Retrieve context parameters */
  681. phantom_port->rx_context_id =
  682. le16_to_cpu ( buf->cardrsp.rx_ctx.context_id );
  683. phantom_port->rds_producer_crb =
  684. ( UNM_CAM_RAM +
  685. le32_to_cpu ( buf->cardrsp.rds.host_producer_crb ));
  686. phantom_port->sds_consumer_crb =
  687. ( UNM_CAM_RAM +
  688. le32_to_cpu ( buf->cardrsp.sds.host_consumer_crb ));
  689. DBGC ( phantom, "Phantom %p port %d created RX context (id %04x, "
  690. "port phys %02x virt %02x)\n", phantom, phantom_port->port,
  691. phantom_port->rx_context_id, buf->cardrsp.rx_ctx.phys_port,
  692. buf->cardrsp.rx_ctx.virt_port );
  693. DBGC2_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
  694. &buf->cardrsp, sizeof ( buf->cardrsp ) );
  695. DBGC ( phantom, "Phantom %p port %d RDS producer CRB is %08lx\n",
  696. phantom, phantom_port->port, phantom_port->rds_producer_crb );
  697. DBGC ( phantom, "Phantom %p port %d SDS consumer CRB is %08lx\n",
  698. phantom, phantom_port->port, phantom_port->sds_consumer_crb );
  699. return 0;
  700. }
  701. /**
  702. * Destroy Phantom RX context
  703. *
  704. * @v phantom_port Phantom NIC port
  705. * @ret rc Return status code
  706. */
  707. static void phantom_destroy_rx_ctx ( struct phantom_nic_port *phantom_port ) {
  708. struct phantom_nic *phantom = phantom_port->phantom;
  709. int rc;
  710. DBGC ( phantom, "Phantom %p port %d destroying RX context (id %04x)\n",
  711. phantom, phantom_port->port, phantom_port->rx_context_id );
  712. /* Issue request */
  713. if ( ( rc = phantom_issue_cmd ( phantom_port,
  714. NX_CDRP_CMD_DESTROY_RX_CTX,
  715. phantom_port->rx_context_id,
  716. NX_DESTROY_CTX_RESET, 0 ) ) != 0 ) {
  717. DBGC ( phantom, "Phantom %p port %d could not destroy RX "
  718. "context: %s\n",
  719. phantom, phantom_port->port, strerror ( rc ) );
  720. /* We're probably screwed */
  721. return;
  722. }
  723. /* Clear context parameters */
  724. phantom_port->rx_context_id = 0;
  725. phantom_port->rds_producer_crb = 0;
  726. phantom_port->sds_consumer_crb = 0;
  727. /* Reset software counters */
  728. phantom_port->rds_producer_idx = 0;
  729. phantom_port->rds_consumer_idx = 0;
  730. phantom_port->sds_consumer_idx = 0;
  731. }
  732. /**
  733. * Create Phantom TX context
  734. *
  735. * @v phantom_port Phantom NIC port
  736. * @ret rc Return status code
  737. */
  738. static int phantom_create_tx_ctx ( struct phantom_nic_port *phantom_port ) {
  739. struct phantom_nic *phantom = phantom_port->phantom;
  740. struct phantom_create_tx_ctx_rqrsp *buf;
  741. int rc;
  742. /* Prepare request */
  743. buf = &phantom->dma_buf->create_tx_ctx;
  744. memset ( buf, 0, sizeof ( *buf ) );
  745. buf->hostrq.tx_ctx.host_rsp_dma_addr =
  746. cpu_to_le64 ( virt_to_bus ( &buf->cardrsp ) );
  747. buf->hostrq.tx_ctx.cmd_cons_dma_addr =
  748. cpu_to_le64 ( virt_to_bus ( &phantom_port->desc->cmd_cons ) );
  749. buf->hostrq.tx_ctx.dummy_dma_addr =
  750. cpu_to_le64 ( virt_to_bus ( phantom->dma_buf->dummy_dma ) );
  751. buf->hostrq.tx_ctx.capabilities[0] =
  752. cpu_to_le32 ( NX_CAP0_LEGACY_CONTEXT | NX_CAP0_LEGACY_MN );
  753. buf->hostrq.tx_ctx.host_int_crb_mode =
  754. cpu_to_le32 ( NX_HOST_INT_CRB_MODE_SHARED );
  755. buf->hostrq.tx_ctx.cds_ring.host_phys_addr =
  756. cpu_to_le64 ( virt_to_bus ( phantom_port->desc->cds ) );
  757. buf->hostrq.tx_ctx.cds_ring.ring_size = cpu_to_le32 ( PHN_NUM_CDS );
  758. DBGC ( phantom, "Phantom %p port %d creating TX context\n",
  759. phantom, phantom_port->port );
  760. DBGC2_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
  761. &buf->hostrq, sizeof ( buf->hostrq ) );
  762. /* Issue request */
  763. if ( ( rc = phantom_issue_buf_cmd ( phantom_port,
  764. NX_CDRP_CMD_CREATE_TX_CTX,
  765. &buf->hostrq,
  766. sizeof ( buf->hostrq ) ) ) != 0 ) {
  767. DBGC ( phantom, "Phantom %p port %d could not create TX "
  768. "context: %s\n",
  769. phantom, phantom_port->port, strerror ( rc ) );
  770. DBGC ( phantom, "Request:\n" );
  771. DBGC_HDA ( phantom, virt_to_bus ( &buf->hostrq ),
  772. &buf->hostrq, sizeof ( buf->hostrq ) );
  773. DBGC ( phantom, "Response:\n" );
  774. DBGC_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
  775. &buf->cardrsp, sizeof ( buf->cardrsp ) );
  776. return rc;
  777. }
  778. /* Retrieve context parameters */
  779. phantom_port->tx_context_id =
  780. le16_to_cpu ( buf->cardrsp.tx_ctx.context_id );
  781. phantom_port->cds_producer_crb =
  782. ( UNM_CAM_RAM +
  783. le32_to_cpu(buf->cardrsp.tx_ctx.cds_ring.host_producer_crb));
  784. DBGC ( phantom, "Phantom %p port %d created TX context (id %04x, "
  785. "port phys %02x virt %02x)\n", phantom, phantom_port->port,
  786. phantom_port->tx_context_id, buf->cardrsp.tx_ctx.phys_port,
  787. buf->cardrsp.tx_ctx.virt_port );
  788. DBGC2_HDA ( phantom, virt_to_bus ( &buf->cardrsp ),
  789. &buf->cardrsp, sizeof ( buf->cardrsp ) );
  790. DBGC ( phantom, "Phantom %p port %d CDS producer CRB is %08lx\n",
  791. phantom, phantom_port->port, phantom_port->cds_producer_crb );
  792. return 0;
  793. }
  794. /**
  795. * Destroy Phantom TX context
  796. *
  797. * @v phantom_port Phantom NIC port
  798. * @ret rc Return status code
  799. */
  800. static void phantom_destroy_tx_ctx ( struct phantom_nic_port *phantom_port ) {
  801. struct phantom_nic *phantom = phantom_port->phantom;
  802. int rc;
  803. DBGC ( phantom, "Phantom %p port %d destroying TX context (id %04x)\n",
  804. phantom, phantom_port->port, phantom_port->tx_context_id );
  805. /* Issue request */
  806. if ( ( rc = phantom_issue_cmd ( phantom_port,
  807. NX_CDRP_CMD_DESTROY_TX_CTX,
  808. phantom_port->tx_context_id,
  809. NX_DESTROY_CTX_RESET, 0 ) ) != 0 ) {
  810. DBGC ( phantom, "Phantom %p port %d could not destroy TX "
  811. "context: %s\n",
  812. phantom, phantom_port->port, strerror ( rc ) );
  813. /* We're probably screwed */
  814. return;
  815. }
  816. /* Clear context parameters */
  817. phantom_port->tx_context_id = 0;
  818. phantom_port->cds_producer_crb = 0;
  819. /* Reset software counters */
  820. phantom_port->cds_producer_idx = 0;
  821. phantom_port->cds_consumer_idx = 0;
  822. }
  823. /***************************************************************************
  824. *
  825. * Descriptor ring management
  826. *
  827. */
  828. /**
  829. * Allocate Phantom RX descriptor
  830. *
  831. * @v phantom_port Phantom NIC port
  832. * @ret index RX descriptor index, or negative error
  833. */
  834. static int phantom_alloc_rds ( struct phantom_nic_port *phantom_port ) {
  835. struct phantom_nic *phantom = phantom_port->phantom;
  836. unsigned int rds_producer_idx;
  837. unsigned int next_rds_producer_idx;
  838. /* Check for space in the ring. RX descriptors are consumed
  839. * out of order, but they are *read* by the hardware in strict
  840. * order. We maintain a pessimistic consumer index, which is
  841. * guaranteed never to be an overestimate of the number of
  842. * descriptors read by the hardware.
  843. */
  844. rds_producer_idx = phantom_port->rds_producer_idx;
  845. next_rds_producer_idx = ( ( rds_producer_idx + 1 ) % PHN_NUM_RDS );
  846. if ( next_rds_producer_idx == phantom_port->rds_consumer_idx ) {
  847. DBGC ( phantom, "Phantom %p port %d RDS ring full (index %d "
  848. "not consumed)\n", phantom, phantom_port->port,
  849. next_rds_producer_idx );
  850. return -ENOBUFS;
  851. }
  852. return rds_producer_idx;
  853. }
  854. /**
  855. * Post Phantom RX descriptor
  856. *
  857. * @v phantom_port Phantom NIC port
  858. * @v rds RX descriptor
  859. */
  860. static void phantom_post_rds ( struct phantom_nic_port *phantom_port,
  861. struct phantom_rds *rds ) {
  862. struct phantom_nic *phantom = phantom_port->phantom;
  863. unsigned int rds_producer_idx;
  864. unsigned int next_rds_producer_idx;
  865. struct phantom_rds *entry;
  866. /* Copy descriptor to ring */
  867. rds_producer_idx = phantom_port->rds_producer_idx;
  868. entry = &phantom_port->desc->rds[rds_producer_idx];
  869. memcpy ( entry, rds, sizeof ( *entry ) );
  870. DBGC2 ( phantom, "Phantom %p port %d posting RDS %ld (slot %d):\n",
  871. phantom, phantom_port->port, NX_GET ( rds, handle ),
  872. rds_producer_idx );
  873. DBGC2_HDA ( phantom, virt_to_bus ( entry ), entry, sizeof ( *entry ) );
  874. /* Update producer index */
  875. next_rds_producer_idx = ( ( rds_producer_idx + 1 ) % PHN_NUM_RDS );
  876. phantom_port->rds_producer_idx = next_rds_producer_idx;
  877. wmb();
  878. phantom_writel ( phantom, phantom_port->rds_producer_idx,
  879. phantom_port->rds_producer_crb );
  880. }
  881. /**
  882. * Allocate Phantom TX descriptor
  883. *
  884. * @v phantom_port Phantom NIC port
  885. * @ret index TX descriptor index, or negative error
  886. */
  887. static int phantom_alloc_cds ( struct phantom_nic_port *phantom_port ) {
  888. struct phantom_nic *phantom = phantom_port->phantom;
  889. unsigned int cds_producer_idx;
  890. unsigned int next_cds_producer_idx;
  891. /* Check for space in the ring. TX descriptors are consumed
  892. * in strict order, so we just check for a collision against
  893. * the consumer index.
  894. */
  895. cds_producer_idx = phantom_port->cds_producer_idx;
  896. next_cds_producer_idx = ( ( cds_producer_idx + 1 ) % PHN_NUM_CDS );
  897. if ( next_cds_producer_idx == phantom_port->cds_consumer_idx ) {
  898. DBGC ( phantom, "Phantom %p port %d CDS ring full (index %d "
  899. "not consumed)\n", phantom, phantom_port->port,
  900. next_cds_producer_idx );
  901. return -ENOBUFS;
  902. }
  903. return cds_producer_idx;
  904. }
  905. /**
  906. * Post Phantom TX descriptor
  907. *
  908. * @v phantom_port Phantom NIC port
  909. * @v cds TX descriptor
  910. */
  911. static void phantom_post_cds ( struct phantom_nic_port *phantom_port,
  912. union phantom_cds *cds ) {
  913. struct phantom_nic *phantom = phantom_port->phantom;
  914. unsigned int cds_producer_idx;
  915. unsigned int next_cds_producer_idx;
  916. union phantom_cds *entry;
  917. /* Copy descriptor to ring */
  918. cds_producer_idx = phantom_port->cds_producer_idx;
  919. entry = &phantom_port->desc->cds[cds_producer_idx];
  920. memcpy ( entry, cds, sizeof ( *entry ) );
  921. DBGC2 ( phantom, "Phantom %p port %d posting CDS %d:\n",
  922. phantom, phantom_port->port, cds_producer_idx );
  923. DBGC2_HDA ( phantom, virt_to_bus ( entry ), entry, sizeof ( *entry ) );
  924. /* Update producer index */
  925. next_cds_producer_idx = ( ( cds_producer_idx + 1 ) % PHN_NUM_CDS );
  926. phantom_port->cds_producer_idx = next_cds_producer_idx;
  927. wmb();
  928. phantom_writel ( phantom, phantom_port->cds_producer_idx,
  929. phantom_port->cds_producer_crb );
  930. }
  931. /***************************************************************************
  932. *
  933. * MAC address management
  934. *
  935. */
  936. /**
  937. * Add/remove MAC address
  938. *
  939. * @v phantom_port Phantom NIC port
  940. * @v ll_addr MAC address to add or remove
  941. * @v opcode MAC request opcode
  942. * @ret rc Return status code
  943. */
  944. static int phantom_update_macaddr ( struct phantom_nic_port *phantom_port,
  945. const uint8_t *ll_addr,
  946. unsigned int opcode ) {
  947. union phantom_cds cds;
  948. int index;
  949. /* Get descriptor ring entry */
  950. index = phantom_alloc_cds ( phantom_port );
  951. if ( index < 0 )
  952. return index;
  953. /* Fill descriptor ring entry */
  954. memset ( &cds, 0, sizeof ( cds ) );
  955. NX_FILL_1 ( &cds, 0,
  956. nic_request.common.opcode, UNM_NIC_REQUEST );
  957. NX_FILL_2 ( &cds, 1,
  958. nic_request.header.opcode, UNM_MAC_EVENT,
  959. nic_request.header.context_id, phantom_port->port );
  960. NX_FILL_7 ( &cds, 2,
  961. nic_request.body.mac_request.opcode, opcode,
  962. nic_request.body.mac_request.mac_addr_0, ll_addr[0],
  963. nic_request.body.mac_request.mac_addr_1, ll_addr[1],
  964. nic_request.body.mac_request.mac_addr_2, ll_addr[2],
  965. nic_request.body.mac_request.mac_addr_3, ll_addr[3],
  966. nic_request.body.mac_request.mac_addr_4, ll_addr[4],
  967. nic_request.body.mac_request.mac_addr_5, ll_addr[5] );
  968. /* Post descriptor */
  969. phantom_post_cds ( phantom_port, &cds );
  970. return 0;
  971. }
  972. /**
  973. * Add MAC address
  974. *
  975. * @v phantom_port Phantom NIC port
  976. * @v ll_addr MAC address to add or remove
  977. * @ret rc Return status code
  978. */
  979. static inline int phantom_add_macaddr ( struct phantom_nic_port *phantom_port,
  980. const uint8_t *ll_addr ) {
  981. struct phantom_nic *phantom = phantom_port->phantom;
  982. DBGC ( phantom, "Phantom %p port %d adding MAC address %s\n",
  983. phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
  984. return phantom_update_macaddr ( phantom_port, ll_addr, UNM_MAC_ADD );
  985. }
  986. /**
  987. * Remove MAC address
  988. *
  989. * @v phantom_port Phantom NIC port
  990. * @v ll_addr MAC address to add or remove
  991. * @ret rc Return status code
  992. */
  993. static inline int phantom_del_macaddr ( struct phantom_nic_port *phantom_port,
  994. const uint8_t *ll_addr ) {
  995. struct phantom_nic *phantom = phantom_port->phantom;
  996. DBGC ( phantom, "Phantom %p port %d removing MAC address %s\n",
  997. phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
  998. return phantom_update_macaddr ( phantom_port, ll_addr, UNM_MAC_DEL );
  999. }
  1000. /***************************************************************************
  1001. *
  1002. * Link state detection
  1003. *
  1004. */
  1005. /**
  1006. * Poll link state
  1007. *
  1008. * @v phantom Phantom NIC
  1009. */
  1010. static void phantom_poll_link_state ( struct phantom_nic *phantom ) {
  1011. struct net_device *netdev;
  1012. struct phantom_nic_port *phantom_port;
  1013. uint32_t xg_state_p3;
  1014. unsigned int link;
  1015. int i;
  1016. /* Read link state */
  1017. xg_state_p3 = phantom_readl ( phantom, UNM_NIC_REG_XG_STATE_P3 );
  1018. /* If there is no change, do nothing */
  1019. if ( phantom->link_state == xg_state_p3 )
  1020. return;
  1021. /* Record new link state */
  1022. DBGC ( phantom, "Phantom %p new link state %08lx (was %08lx)\n",
  1023. phantom, xg_state_p3, phantom->link_state );
  1024. phantom->link_state = xg_state_p3;
  1025. /* Indicate per-port link state to gPXE */
  1026. for ( i = 0 ; i < phantom->num_ports ; i++ ) {
  1027. netdev = phantom->netdev[i];
  1028. phantom_port = netdev_priv ( netdev );
  1029. link = UNM_NIC_REG_XG_STATE_P3_LINK ( phantom_port->port,
  1030. phantom->link_state );
  1031. switch ( link ) {
  1032. case UNM_NIC_REG_XG_STATE_P3_LINK_UP:
  1033. DBGC ( phantom, "Phantom %p port %d link is up\n",
  1034. phantom, phantom_port->port );
  1035. netdev_link_up ( netdev );
  1036. break;
  1037. case UNM_NIC_REG_XG_STATE_P3_LINK_DOWN:
  1038. DBGC ( phantom, "Phantom %p port %d link is down\n",
  1039. phantom, phantom_port->port );
  1040. netdev_link_down ( netdev );
  1041. break;
  1042. default:
  1043. DBGC ( phantom, "Phantom %p port %d bad link state "
  1044. "%d\n", phantom, phantom_port->port, link );
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. /***************************************************************************
  1050. *
  1051. * Main driver body
  1052. *
  1053. */
  1054. /**
  1055. * Refill descriptor ring
  1056. *
  1057. * @v netdev Net device
  1058. */
  1059. static void phantom_refill_rx_ring ( struct net_device *netdev ) {
  1060. struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
  1061. struct io_buffer *iobuf;
  1062. struct phantom_rds rds;
  1063. unsigned int handle;
  1064. int index;
  1065. for ( handle = 0 ; handle < PHN_RDS_MAX_FILL ; handle++ ) {
  1066. /* Skip this index if the descriptor has not yet been
  1067. * consumed.
  1068. */
  1069. if ( phantom_port->rds_iobuf[handle] != NULL )
  1070. continue;
  1071. /* Allocate descriptor ring entry */
  1072. index = phantom_alloc_rds ( phantom_port );
  1073. assert ( PHN_RDS_MAX_FILL < PHN_NUM_RDS );
  1074. assert ( index >= 0 ); /* Guaranteed by MAX_FILL < NUM_RDS ) */
  1075. /* Try to allocate an I/O buffer */
  1076. iobuf = alloc_iob ( PHN_RX_BUFSIZE );
  1077. if ( ! iobuf ) {
  1078. /* Failure is non-fatal; we will retry later */
  1079. netdev_rx_err ( netdev, NULL, -ENOMEM );
  1080. break;
  1081. }
  1082. /* Fill descriptor ring entry */
  1083. memset ( &rds, 0, sizeof ( rds ) );
  1084. NX_FILL_2 ( &rds, 0,
  1085. handle, handle,
  1086. length, iob_len ( iobuf ) );
  1087. NX_FILL_1 ( &rds, 1,
  1088. dma_addr, virt_to_bus ( iobuf->data ) );
  1089. /* Record I/O buffer */
  1090. assert ( phantom_port->rds_iobuf[handle] == NULL );
  1091. phantom_port->rds_iobuf[handle] = iobuf;
  1092. /* Post descriptor */
  1093. phantom_post_rds ( phantom_port, &rds );
  1094. }
  1095. }
  1096. /**
  1097. * Open NIC
  1098. *
  1099. * @v netdev Net device
  1100. * @ret rc Return status code
  1101. */
  1102. static int phantom_open ( struct net_device *netdev ) {
  1103. struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
  1104. int rc;
  1105. /* Allocate and zero descriptor rings */
  1106. phantom_port->desc = malloc_dma ( sizeof ( *(phantom_port->desc) ),
  1107. UNM_DMA_BUFFER_ALIGN );
  1108. if ( ! phantom_port->desc ) {
  1109. rc = -ENOMEM;
  1110. goto err_alloc_desc;
  1111. }
  1112. memset ( phantom_port->desc, 0, sizeof ( *(phantom_port->desc) ) );
  1113. /* Create RX context */
  1114. if ( ( rc = phantom_create_rx_ctx ( phantom_port ) ) != 0 )
  1115. goto err_create_rx_ctx;
  1116. /* Create TX context */
  1117. if ( ( rc = phantom_create_tx_ctx ( phantom_port ) ) != 0 )
  1118. goto err_create_tx_ctx;
  1119. /* Fill the RX descriptor ring */
  1120. phantom_refill_rx_ring ( netdev );
  1121. /* Add MAC addresses
  1122. *
  1123. * BUG5583
  1124. *
  1125. * We would like to be able to enable receiving all multicast
  1126. * packets (or, failing that, promiscuous mode), but the
  1127. * firmware doesn't currently support this.
  1128. */
  1129. if ( ( rc = phantom_add_macaddr ( phantom_port,
  1130. netdev->ll_protocol->ll_broadcast ) ) != 0 )
  1131. goto err_add_macaddr_broadcast;
  1132. if ( ( rc = phantom_add_macaddr ( phantom_port,
  1133. netdev->ll_addr ) ) != 0 )
  1134. goto err_add_macaddr_unicast;
  1135. return 0;
  1136. phantom_del_macaddr ( phantom_port, netdev->ll_addr );
  1137. err_add_macaddr_unicast:
  1138. phantom_del_macaddr ( phantom_port,
  1139. netdev->ll_protocol->ll_broadcast );
  1140. err_add_macaddr_broadcast:
  1141. phantom_destroy_tx_ctx ( phantom_port );
  1142. err_create_tx_ctx:
  1143. phantom_destroy_rx_ctx ( phantom_port );
  1144. err_create_rx_ctx:
  1145. free_dma ( phantom_port->desc, sizeof ( *(phantom_port->desc) ) );
  1146. phantom_port->desc = NULL;
  1147. err_alloc_desc:
  1148. return rc;
  1149. }
  1150. /**
  1151. * Close NIC
  1152. *
  1153. * @v netdev Net device
  1154. */
  1155. static void phantom_close ( struct net_device *netdev ) {
  1156. struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
  1157. struct io_buffer *iobuf;
  1158. unsigned int i;
  1159. /* Shut down the port */
  1160. phantom_del_macaddr ( phantom_port, netdev->ll_addr );
  1161. phantom_del_macaddr ( phantom_port,
  1162. netdev->ll_protocol->ll_broadcast );
  1163. phantom_destroy_tx_ctx ( phantom_port );
  1164. phantom_destroy_rx_ctx ( phantom_port );
  1165. free_dma ( phantom_port->desc, sizeof ( *(phantom_port->desc) ) );
  1166. phantom_port->desc = NULL;
  1167. /* Flush any uncompleted descriptors */
  1168. for ( i = 0 ; i < PHN_RDS_MAX_FILL ; i++ ) {
  1169. iobuf = phantom_port->rds_iobuf[i];
  1170. if ( iobuf ) {
  1171. free_iob ( iobuf );
  1172. phantom_port->rds_iobuf[i] = NULL;
  1173. }
  1174. }
  1175. for ( i = 0 ; i < PHN_NUM_CDS ; i++ ) {
  1176. iobuf = phantom_port->cds_iobuf[i];
  1177. if ( iobuf ) {
  1178. netdev_tx_complete_err ( netdev, iobuf, -ECANCELED );
  1179. phantom_port->cds_iobuf[i] = NULL;
  1180. }
  1181. }
  1182. }
  1183. /**
  1184. * Transmit packet
  1185. *
  1186. * @v netdev Network device
  1187. * @v iobuf I/O buffer
  1188. * @ret rc Return status code
  1189. */
  1190. static int phantom_transmit ( struct net_device *netdev,
  1191. struct io_buffer *iobuf ) {
  1192. struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
  1193. union phantom_cds cds;
  1194. int index;
  1195. /* Get descriptor ring entry */
  1196. index = phantom_alloc_cds ( phantom_port );
  1197. if ( index < 0 )
  1198. return index;
  1199. /* Fill descriptor ring entry */
  1200. memset ( &cds, 0, sizeof ( cds ) );
  1201. NX_FILL_3 ( &cds, 0,
  1202. tx.opcode, UNM_TX_ETHER_PKT,
  1203. tx.num_buffers, 1,
  1204. tx.length, iob_len ( iobuf ) );
  1205. NX_FILL_2 ( &cds, 2,
  1206. tx.port, phantom_port->port,
  1207. tx.context_id, phantom_port->port );
  1208. NX_FILL_1 ( &cds, 4,
  1209. tx.buffer1_dma_addr, virt_to_bus ( iobuf->data ) );
  1210. NX_FILL_1 ( &cds, 5,
  1211. tx.buffer1_length, iob_len ( iobuf ) );
  1212. /* Record I/O buffer */
  1213. assert ( phantom_port->cds_iobuf[index] == NULL );
  1214. phantom_port->cds_iobuf[index] = iobuf;
  1215. /* Post descriptor */
  1216. phantom_post_cds ( phantom_port, &cds );
  1217. return 0;
  1218. }
  1219. /**
  1220. * Poll for received packets
  1221. *
  1222. * @v netdev Network device
  1223. */
  1224. static void phantom_poll ( struct net_device *netdev ) {
  1225. struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
  1226. struct phantom_nic *phantom = phantom_port->phantom;
  1227. struct io_buffer *iobuf;
  1228. unsigned int cds_consumer_idx;
  1229. unsigned int raw_new_cds_consumer_idx;
  1230. unsigned int new_cds_consumer_idx;
  1231. unsigned int rds_consumer_idx;
  1232. unsigned int sds_consumer_idx;
  1233. struct phantom_sds *sds;
  1234. unsigned int sds_handle;
  1235. unsigned int sds_opcode;
  1236. /* Check for TX completions */
  1237. cds_consumer_idx = phantom_port->cds_consumer_idx;
  1238. raw_new_cds_consumer_idx = phantom_port->desc->cmd_cons;
  1239. new_cds_consumer_idx = le32_to_cpu ( raw_new_cds_consumer_idx );
  1240. while ( cds_consumer_idx != new_cds_consumer_idx ) {
  1241. DBGC2 ( phantom, "Phantom %p port %d CDS %d complete\n",
  1242. phantom, phantom_port->port, cds_consumer_idx );
  1243. /* Completions may be for commands other than TX, so
  1244. * there may not always be an associated I/O buffer.
  1245. */
  1246. if ( ( iobuf = phantom_port->cds_iobuf[cds_consumer_idx] ) ) {
  1247. netdev_tx_complete ( netdev, iobuf );
  1248. phantom_port->cds_iobuf[cds_consumer_idx] = NULL;
  1249. }
  1250. cds_consumer_idx = ( ( cds_consumer_idx + 1 ) % PHN_NUM_CDS );
  1251. phantom_port->cds_consumer_idx = cds_consumer_idx;
  1252. }
  1253. /* Check for received packets */
  1254. rds_consumer_idx = phantom_port->rds_consumer_idx;
  1255. sds_consumer_idx = phantom_port->sds_consumer_idx;
  1256. while ( 1 ) {
  1257. sds = &phantom_port->desc->sds[sds_consumer_idx];
  1258. if ( NX_GET ( sds, owner ) == 0 )
  1259. break;
  1260. DBGC2 ( phantom, "Phantom %p port %d SDS %d status:\n",
  1261. phantom, phantom_port->port, sds_consumer_idx );
  1262. DBGC2_HDA ( phantom, virt_to_bus ( sds ), sds, sizeof (*sds) );
  1263. /* Check received opcode */
  1264. sds_opcode = NX_GET ( sds, opcode );
  1265. if ( ( sds_opcode == UNM_RXPKT_DESC ) ||
  1266. ( sds_opcode == UNM_SYN_OFFLOAD ) ) {
  1267. /* Sanity check: ensure that all of the SDS
  1268. * descriptor has been written.
  1269. */
  1270. if ( NX_GET ( sds, total_length ) == 0 ) {
  1271. DBGC ( phantom, "Phantom %p port %d SDS %d "
  1272. "incomplete; deferring\n", phantom,
  1273. phantom_port->port, sds_consumer_idx );
  1274. /* Leave for next poll() */
  1275. break;
  1276. }
  1277. /* Process received packet */
  1278. sds_handle = NX_GET ( sds, handle );
  1279. iobuf = phantom_port->rds_iobuf[sds_handle];
  1280. assert ( iobuf != NULL );
  1281. iob_put ( iobuf, NX_GET ( sds, total_length ) );
  1282. iob_pull ( iobuf, NX_GET ( sds, pkt_offset ) );
  1283. DBGC2 ( phantom, "Phantom %p port %d RDS %d "
  1284. "complete\n",
  1285. phantom, phantom_port->port, sds_handle );
  1286. netdev_rx ( netdev, iobuf );
  1287. phantom_port->rds_iobuf[sds_handle] = NULL;
  1288. /* Update RDS consumer counter. This is a
  1289. * lower bound for the number of descriptors
  1290. * that have been read by the hardware, since
  1291. * the hardware must have read at least one
  1292. * descriptor for each completion that we
  1293. * receive.
  1294. */
  1295. rds_consumer_idx =
  1296. ( ( rds_consumer_idx + 1 ) % PHN_NUM_RDS );
  1297. phantom_port->rds_consumer_idx = rds_consumer_idx;
  1298. } else {
  1299. DBGC ( phantom, "Phantom %p port %d unexpected SDS "
  1300. "opcode %02x\n",
  1301. phantom, phantom_port->port, sds_opcode );
  1302. DBGC_HDA ( phantom, virt_to_bus ( sds ),
  1303. sds, sizeof ( *sds ) );
  1304. }
  1305. /* Clear status descriptor */
  1306. memset ( sds, 0, sizeof ( *sds ) );
  1307. /* Update SDS consumer index */
  1308. sds_consumer_idx = ( ( sds_consumer_idx + 1 ) % PHN_NUM_SDS );
  1309. phantom_port->sds_consumer_idx = sds_consumer_idx;
  1310. wmb();
  1311. phantom_writel ( phantom, phantom_port->sds_consumer_idx,
  1312. phantom_port->sds_consumer_crb );
  1313. }
  1314. /* Refill the RX descriptor ring */
  1315. phantom_refill_rx_ring ( netdev );
  1316. /* Occasionally poll the link state */
  1317. if ( phantom_port->link_poll_timer-- == 0 ) {
  1318. phantom_poll_link_state ( phantom );
  1319. /* Reset the link poll timer */
  1320. phantom_port->link_poll_timer = PHN_LINK_POLL_FREQUENCY;
  1321. }
  1322. }
  1323. /**
  1324. * Enable/disable interrupts
  1325. *
  1326. * @v netdev Network device
  1327. * @v enable Interrupts should be enabled
  1328. */
  1329. static void phantom_irq ( struct net_device *netdev, int enable ) {
  1330. struct phantom_nic_port *phantom_port = netdev_priv ( netdev );
  1331. struct phantom_nic *phantom = phantom_port->phantom;
  1332. static const unsigned long sw_int_mask_reg[UNM_FLASH_NUM_PORTS] = {
  1333. UNM_NIC_REG_SW_INT_MASK_0,
  1334. UNM_NIC_REG_SW_INT_MASK_1,
  1335. UNM_NIC_REG_SW_INT_MASK_2,
  1336. UNM_NIC_REG_SW_INT_MASK_3
  1337. };
  1338. phantom_writel ( phantom,
  1339. ( enable ? 1 : 0 ),
  1340. sw_int_mask_reg[phantom_port->port] );
  1341. }
  1342. /** Phantom net device operations */
  1343. static struct net_device_operations phantom_operations = {
  1344. .open = phantom_open,
  1345. .close = phantom_close,
  1346. .transmit = phantom_transmit,
  1347. .poll = phantom_poll,
  1348. .irq = phantom_irq,
  1349. };
  1350. /**
  1351. * Map Phantom CRB window
  1352. *
  1353. * @v phantom Phantom NIC
  1354. * @ret rc Return status code
  1355. */
  1356. static int phantom_map_crb ( struct phantom_nic *phantom,
  1357. struct pci_device *pci ) {
  1358. unsigned long bar0_start;
  1359. unsigned long bar0_size;
  1360. bar0_start = pci_bar_start ( pci, PCI_BASE_ADDRESS_0 );
  1361. bar0_size = pci_bar_size ( pci, PCI_BASE_ADDRESS_0 );
  1362. DBGC ( phantom, "Phantom %p BAR0 is %08lx+%lx\n",
  1363. phantom, bar0_start, bar0_size );
  1364. switch ( bar0_size ) {
  1365. case ( 128 * 1024 * 1024 ) :
  1366. DBGC ( phantom, "Phantom %p has 128MB BAR\n", phantom );
  1367. phantom->crb_access = phantom_crb_access_128m;
  1368. break;
  1369. case ( 32 * 1024 * 1024 ) :
  1370. DBGC ( phantom, "Phantom %p has 32MB BAR\n", phantom );
  1371. phantom->crb_access = phantom_crb_access_32m;
  1372. break;
  1373. case ( 2 * 1024 * 1024 ) :
  1374. DBGC ( phantom, "Phantom %p has 2MB BAR\n", phantom );
  1375. phantom->crb_access = phantom_crb_access_2m;
  1376. break;
  1377. default:
  1378. DBGC ( phantom, "Phantom %p has bad BAR size\n", phantom );
  1379. return -EINVAL;
  1380. }
  1381. phantom->bar0 = ioremap ( bar0_start, bar0_size );
  1382. if ( ! phantom->bar0 ) {
  1383. DBGC ( phantom, "Phantom %p could not map BAR0\n", phantom );
  1384. return -EIO;
  1385. }
  1386. /* Mark current CRB window as invalid, so that the first
  1387. * read/write will set the current window.
  1388. */
  1389. phantom->crb_window = -1UL;
  1390. return 0;
  1391. }
  1392. /**
  1393. * Read Phantom flash contents
  1394. *
  1395. * @v phantom Phantom NIC
  1396. * @ret rc Return status code
  1397. */
  1398. static int phantom_read_flash ( struct phantom_nic *phantom ) {
  1399. struct unm_board_info board_info;
  1400. int rc;
  1401. /* Initialise flash access */
  1402. phantom->spi_bus.rw = phantom_spi_rw;
  1403. phantom->flash.bus = &phantom->spi_bus;
  1404. init_m25p32 ( &phantom->flash );
  1405. /* Phantom doesn't support greater than 4-byte block sizes */
  1406. phantom->flash.nvs.block_size = UNM_SPI_BLKSIZE;
  1407. /* Read and verify board information */
  1408. if ( ( rc = nvs_read ( &phantom->flash.nvs, UNM_BRDCFG_START,
  1409. &board_info, sizeof ( board_info ) ) ) != 0 ) {
  1410. DBGC ( phantom, "Phantom %p could not read board info: %s\n",
  1411. phantom, strerror ( rc ) );
  1412. return rc;
  1413. }
  1414. if ( board_info.magic != UNM_BDINFO_MAGIC ) {
  1415. DBGC ( phantom, "Phantom %p has bad board info magic %lx\n",
  1416. phantom, board_info.magic );
  1417. DBGC_HD ( phantom, &board_info, sizeof ( board_info ) );
  1418. return -EINVAL;
  1419. }
  1420. if ( board_info.header_version != UNM_BDINFO_VERSION ) {
  1421. DBGC ( phantom, "Phantom %p has bad board info version %lx\n",
  1422. phantom, board_info.header_version );
  1423. DBGC_HD ( phantom, &board_info, sizeof ( board_info ) );
  1424. return -EINVAL;
  1425. }
  1426. /* Identify board type and number of ports */
  1427. switch ( board_info.board_type ) {
  1428. case UNM_BRDTYPE_P3_4_GB:
  1429. case UNM_BRDTYPE_P3_4_GB_MM:
  1430. phantom->num_ports = 4;
  1431. break;
  1432. case UNM_BRDTYPE_P3_HMEZ:
  1433. case UNM_BRDTYPE_P3_IMEZ:
  1434. case UNM_BRDTYPE_P3_10G_CX4:
  1435. case UNM_BRDTYPE_P3_10G_CX4_LP:
  1436. case UNM_BRDTYPE_P3_10G_SFP_PLUS:
  1437. case UNM_BRDTYPE_P3_XG_LOM:
  1438. phantom->num_ports = 2;
  1439. break;
  1440. case UNM_BRDTYPE_P3_10000_BASE_T:
  1441. case UNM_BRDTYPE_P3_10G_XFP:
  1442. phantom->num_ports = 1;
  1443. break;
  1444. default:
  1445. DBGC ( phantom, "Phantom %p unrecognised board type %#lx; "
  1446. "assuming single-port\n",
  1447. phantom, board_info.board_type );
  1448. phantom->num_ports = 1;
  1449. break;
  1450. }
  1451. DBGC ( phantom, "Phantom %p board type is %#lx (%d ports)\n",
  1452. phantom, board_info.board_type, phantom->num_ports );
  1453. return 0;
  1454. }
  1455. /**
  1456. * Initialise the Phantom command PEG
  1457. *
  1458. * @v phantom Phantom NIC
  1459. * @ret rc Return status code
  1460. */
  1461. static int phantom_init_cmdpeg ( struct phantom_nic *phantom ) {
  1462. uint32_t cold_boot;
  1463. uint32_t sw_reset;
  1464. physaddr_t dummy_dma_phys;
  1465. unsigned int retries;
  1466. uint32_t cmdpeg_state;
  1467. uint32_t last_cmdpeg_state = 0;
  1468. /* If this was a cold boot, check that the hardware came up ok */
  1469. cold_boot = phantom_readl ( phantom, UNM_CAM_RAM_COLD_BOOT );
  1470. if ( cold_boot == UNM_CAM_RAM_COLD_BOOT_MAGIC ) {
  1471. DBGC ( phantom, "Phantom %p coming up from cold boot\n",
  1472. phantom );
  1473. sw_reset = phantom_readl ( phantom, UNM_ROMUSB_GLB_SW_RESET );
  1474. if ( sw_reset != UNM_ROMUSB_GLB_SW_RESET_MAGIC ) {
  1475. DBGC ( phantom, "Phantom %p reset failed: %08lx\n",
  1476. phantom, sw_reset );
  1477. return -EIO;
  1478. }
  1479. } else {
  1480. DBGC ( phantom, "Phantom %p coming up from warm boot "
  1481. "(%08lx)\n", phantom, cold_boot );
  1482. }
  1483. /* Clear cold-boot flag */
  1484. phantom_writel ( phantom, 0, UNM_CAM_RAM_COLD_BOOT );
  1485. /* Set port modes */
  1486. phantom_writel ( phantom, UNM_CAM_RAM_PORT_MODE_AUTO_NEG_1G,
  1487. UNM_CAM_RAM_WOL_PORT_MODE );
  1488. /* Pass dummy DMA area to card */
  1489. dummy_dma_phys = virt_to_bus ( phantom->dma_buf->dummy_dma );
  1490. DBGC ( phantom, "Phantom %p dummy DMA at %08lx\n",
  1491. phantom, dummy_dma_phys );
  1492. phantom_write_hilo ( phantom, dummy_dma_phys,
  1493. UNM_NIC_REG_DUMMY_BUF_ADDR_LO,
  1494. UNM_NIC_REG_DUMMY_BUF_ADDR_HI );
  1495. phantom_writel ( phantom, UNM_NIC_REG_DUMMY_BUF_INIT,
  1496. UNM_NIC_REG_DUMMY_BUF );
  1497. /* Tell the hardware that tuning is complete */
  1498. phantom_writel ( phantom, UNM_ROMUSB_GLB_PEGTUNE_DONE_MAGIC,
  1499. UNM_ROMUSB_GLB_PEGTUNE_DONE );
  1500. /* Wait for command PEG to finish initialising */
  1501. DBGC ( phantom, "Phantom %p initialising command PEG (will take up to "
  1502. "%d seconds)...\n", phantom, PHN_CMDPEG_INIT_TIMEOUT_SEC );
  1503. for ( retries = 0; retries < PHN_CMDPEG_INIT_TIMEOUT_SEC; retries++ ) {
  1504. cmdpeg_state = phantom_readl ( phantom,
  1505. UNM_NIC_REG_CMDPEG_STATE );
  1506. if ( cmdpeg_state != last_cmdpeg_state ) {
  1507. DBGC ( phantom, "Phantom %p command PEG state is "
  1508. "%08lx after %d seconds...\n",
  1509. phantom, cmdpeg_state, retries );
  1510. last_cmdpeg_state = cmdpeg_state;
  1511. }
  1512. if ( cmdpeg_state == UNM_NIC_REG_CMDPEG_STATE_INITIALIZED ) {
  1513. /* Acknowledge the PEG initialisation */
  1514. phantom_writel ( phantom,
  1515. UNM_NIC_REG_CMDPEG_STATE_INITIALIZE_ACK,
  1516. UNM_NIC_REG_CMDPEG_STATE );
  1517. return 0;
  1518. }
  1519. mdelay ( 1000 );
  1520. }
  1521. DBGC ( phantom, "Phantom %p timed out waiting for command PEG to "
  1522. "initialise (status %08lx)\n", phantom, cmdpeg_state );
  1523. return -ETIMEDOUT;
  1524. }
  1525. /**
  1526. * Read Phantom MAC address
  1527. *
  1528. * @v phanton_port Phantom NIC port
  1529. * @v ll_addr Buffer to fill with MAC address
  1530. */
  1531. static void phantom_get_macaddr ( struct phantom_nic_port *phantom_port,
  1532. uint8_t *ll_addr ) {
  1533. struct phantom_nic *phantom = phantom_port->phantom;
  1534. union {
  1535. uint8_t mac_addr[2][ETH_ALEN];
  1536. uint32_t dwords[3];
  1537. } u;
  1538. unsigned long offset;
  1539. int i;
  1540. /* Read the three dwords that include this MAC address and one other */
  1541. offset = ( UNM_CAM_RAM_MAC_ADDRS +
  1542. ( 12 * ( phantom_port->port / 2 ) ) );
  1543. for ( i = 0 ; i < 3 ; i++, offset += 4 ) {
  1544. u.dwords[i] = phantom_readl ( phantom, offset );
  1545. }
  1546. /* Copy out the relevant MAC address */
  1547. for ( i = 0 ; i < ETH_ALEN ; i++ ) {
  1548. ll_addr[ ETH_ALEN - i - 1 ] =
  1549. u.mac_addr[ phantom_port->port & 1 ][i];
  1550. }
  1551. DBGC ( phantom, "Phantom %p port %d MAC address is %s\n",
  1552. phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
  1553. }
  1554. /**
  1555. * Initialise Phantom receive PEG
  1556. *
  1557. * @v phantom Phantom NIC
  1558. * @ret rc Return status code
  1559. */
  1560. static int phantom_init_rcvpeg ( struct phantom_nic *phantom ) {
  1561. unsigned int retries;
  1562. uint32_t rcvpeg_state;
  1563. uint32_t last_rcvpeg_state = 0;
  1564. DBGC ( phantom, "Phantom %p initialising receive PEG (will take up to "
  1565. "%d seconds)...\n", phantom, PHN_RCVPEG_INIT_TIMEOUT_SEC );
  1566. for ( retries = 0; retries < PHN_RCVPEG_INIT_TIMEOUT_SEC; retries++ ) {
  1567. rcvpeg_state = phantom_readl ( phantom,
  1568. UNM_NIC_REG_RCVPEG_STATE );
  1569. if ( rcvpeg_state != last_rcvpeg_state ) {
  1570. DBGC ( phantom, "Phantom %p receive PEG state is "
  1571. "%08lx after %d seconds...\n",
  1572. phantom, rcvpeg_state, retries );
  1573. last_rcvpeg_state = rcvpeg_state;
  1574. }
  1575. if ( rcvpeg_state == UNM_NIC_REG_RCVPEG_STATE_INITIALIZED )
  1576. return 0;
  1577. mdelay ( 1000 );
  1578. }
  1579. DBGC ( phantom, "Phantom %p timed out waiting for receive PEG to "
  1580. "initialise (status %08lx)\n", phantom, rcvpeg_state );
  1581. return -ETIMEDOUT;
  1582. }
  1583. /**
  1584. * Probe PCI device
  1585. *
  1586. * @v pci PCI device
  1587. * @v id PCI ID
  1588. * @ret rc Return status code
  1589. */
  1590. static int phantom_probe ( struct pci_device *pci,
  1591. const struct pci_device_id *id __unused ) {
  1592. struct phantom_nic *phantom;
  1593. struct net_device *netdev;
  1594. struct phantom_nic_port *phantom_port;
  1595. int i;
  1596. int rc;
  1597. /* Phantom NICs expose multiple PCI functions, used for
  1598. * virtualisation. Ignore everything except function 0.
  1599. */
  1600. if ( PCI_FUNC ( pci->devfn ) != 0 )
  1601. return -ENODEV;
  1602. /* Allocate Phantom device */
  1603. phantom = zalloc ( sizeof ( *phantom ) );
  1604. if ( ! phantom ) {
  1605. rc = -ENOMEM;
  1606. goto err_alloc_phantom;
  1607. }
  1608. pci_set_drvdata ( pci, phantom );
  1609. /* Fix up PCI device */
  1610. adjust_pci_device ( pci );
  1611. /* Map CRB */
  1612. if ( ( rc = phantom_map_crb ( phantom, pci ) ) != 0 )
  1613. goto err_map_crb;
  1614. /* Read flash information */
  1615. if ( ( rc = phantom_read_flash ( phantom ) ) != 0 )
  1616. goto err_read_flash;
  1617. /* Allocate net devices for each port */
  1618. for ( i = 0 ; i < phantom->num_ports ; i++ ) {
  1619. netdev = alloc_etherdev ( sizeof ( *phantom_port ) );
  1620. if ( ! netdev ) {
  1621. rc = -ENOMEM;
  1622. goto err_alloc_etherdev;
  1623. }
  1624. phantom->netdev[i] = netdev;
  1625. netdev_init ( netdev, &phantom_operations );
  1626. phantom_port = netdev_priv ( netdev );
  1627. netdev->dev = &pci->dev;
  1628. phantom_port->phantom = phantom;
  1629. phantom_port->port = i;
  1630. }
  1631. /* BUG5945 - need to hack PCI config space on P3 B1 silicon.
  1632. * B2 will have this fixed; remove this hack when B1 is no
  1633. * longer in use.
  1634. */
  1635. for ( i = 0 ; i < 8 ; i++ ) {
  1636. uint32_t temp;
  1637. pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), i );
  1638. pci_read_config_dword ( pci, 0xc8, &temp );
  1639. pci_read_config_dword ( pci, 0xc8, &temp );
  1640. pci_write_config_dword ( pci, 0xc8, 0xf1000 );
  1641. }
  1642. pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), 0 );
  1643. /* Allocate dummy DMA buffer and perform initial hardware handshake */
  1644. phantom->dma_buf = malloc_dma ( sizeof ( *(phantom->dma_buf) ),
  1645. UNM_DMA_BUFFER_ALIGN );
  1646. if ( ! phantom->dma_buf )
  1647. goto err_dma_buf;
  1648. if ( ( rc = phantom_init_cmdpeg ( phantom ) ) != 0 )
  1649. goto err_init_cmdpeg;
  1650. /* Initialise the receive firmware */
  1651. if ( ( rc = phantom_init_rcvpeg ( phantom ) ) != 0 )
  1652. goto err_init_rcvpeg;
  1653. /* Read MAC addresses */
  1654. for ( i = 0 ; i < phantom->num_ports ; i++ ) {
  1655. phantom_port = netdev_priv ( phantom->netdev[i] );
  1656. phantom_get_macaddr ( phantom_port,
  1657. phantom->netdev[i]->ll_addr );
  1658. }
  1659. /* Register network devices */
  1660. for ( i = 0 ; i < phantom->num_ports ; i++ ) {
  1661. if ( ( rc = register_netdev ( phantom->netdev[i] ) ) != 0 ) {
  1662. DBGC ( phantom, "Phantom %p could not register port "
  1663. "%d: %s\n", phantom, i, strerror ( rc ) );
  1664. goto err_register_netdev;
  1665. }
  1666. }
  1667. return 0;
  1668. i = ( phantom->num_ports - 1 );
  1669. err_register_netdev:
  1670. for ( ; i >= 0 ; i-- )
  1671. unregister_netdev ( phantom->netdev[i] );
  1672. err_init_rcvpeg:
  1673. err_init_cmdpeg:
  1674. free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
  1675. phantom->dma_buf = NULL;
  1676. err_dma_buf:
  1677. i = ( phantom->num_ports - 1 );
  1678. err_alloc_etherdev:
  1679. for ( ; i >= 0 ; i-- ) {
  1680. netdev_nullify ( phantom->netdev[i] );
  1681. netdev_put ( phantom->netdev[i] );
  1682. }
  1683. err_read_flash:
  1684. err_map_crb:
  1685. free ( phantom );
  1686. err_alloc_phantom:
  1687. return rc;
  1688. }
  1689. /**
  1690. * Remove PCI device
  1691. *
  1692. * @v pci PCI device
  1693. */
  1694. static void phantom_remove ( struct pci_device *pci ) {
  1695. struct phantom_nic *phantom = pci_get_drvdata ( pci );
  1696. int i;
  1697. for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- )
  1698. unregister_netdev ( phantom->netdev[i] );
  1699. free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
  1700. phantom->dma_buf = NULL;
  1701. for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- ) {
  1702. netdev_nullify ( phantom->netdev[i] );
  1703. netdev_put ( phantom->netdev[i] );
  1704. }
  1705. free ( phantom );
  1706. }
  1707. /** Phantom PCI IDs */
  1708. static struct pci_device_id phantom_nics[] = {
  1709. PCI_ROM ( 0x4040, 0x0100, "nx", "NX" ),
  1710. };
  1711. /** Phantom PCI driver */
  1712. struct pci_driver phantom_driver __pci_driver = {
  1713. .ids = phantom_nics,
  1714. .id_count = ( sizeof ( phantom_nics ) / sizeof ( phantom_nics[0] ) ),
  1715. .probe = phantom_probe,
  1716. .remove = phantom_remove,
  1717. };