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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  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. /* Check for a previous initialisation. This could have
  1469. * happened if, for example, the BIOS used the UNDI API to
  1470. * drive the NIC prior to a full PXE boot.
  1471. */
  1472. cmdpeg_state = phantom_readl ( phantom, UNM_NIC_REG_CMDPEG_STATE );
  1473. if ( cmdpeg_state == UNM_NIC_REG_CMDPEG_STATE_INITIALIZE_ACK ) {
  1474. DBGC ( phantom, "Phantom %p command PEG already initialized\n",
  1475. phantom );
  1476. return 0;
  1477. }
  1478. /* If this was a cold boot, check that the hardware came up ok */
  1479. cold_boot = phantom_readl ( phantom, UNM_CAM_RAM_COLD_BOOT );
  1480. if ( cold_boot == UNM_CAM_RAM_COLD_BOOT_MAGIC ) {
  1481. DBGC ( phantom, "Phantom %p coming up from cold boot\n",
  1482. phantom );
  1483. sw_reset = phantom_readl ( phantom, UNM_ROMUSB_GLB_SW_RESET );
  1484. if ( sw_reset != UNM_ROMUSB_GLB_SW_RESET_MAGIC ) {
  1485. DBGC ( phantom, "Phantom %p reset failed: %08lx\n",
  1486. phantom, sw_reset );
  1487. return -EIO;
  1488. }
  1489. } else {
  1490. DBGC ( phantom, "Phantom %p coming up from warm boot "
  1491. "(%08lx)\n", phantom, cold_boot );
  1492. }
  1493. /* Clear cold-boot flag */
  1494. phantom_writel ( phantom, 0, UNM_CAM_RAM_COLD_BOOT );
  1495. /* Set port modes */
  1496. phantom_writel ( phantom, UNM_CAM_RAM_PORT_MODE_AUTO_NEG_1G,
  1497. UNM_CAM_RAM_WOL_PORT_MODE );
  1498. /* Pass dummy DMA area to card */
  1499. dummy_dma_phys = virt_to_bus ( phantom->dma_buf->dummy_dma );
  1500. DBGC ( phantom, "Phantom %p dummy DMA at %08lx\n",
  1501. phantom, dummy_dma_phys );
  1502. phantom_write_hilo ( phantom, dummy_dma_phys,
  1503. UNM_NIC_REG_DUMMY_BUF_ADDR_LO,
  1504. UNM_NIC_REG_DUMMY_BUF_ADDR_HI );
  1505. phantom_writel ( phantom, UNM_NIC_REG_DUMMY_BUF_INIT,
  1506. UNM_NIC_REG_DUMMY_BUF );
  1507. /* Tell the hardware that tuning is complete */
  1508. phantom_writel ( phantom, UNM_ROMUSB_GLB_PEGTUNE_DONE_MAGIC,
  1509. UNM_ROMUSB_GLB_PEGTUNE_DONE );
  1510. /* Wait for command PEG to finish initialising */
  1511. DBGC ( phantom, "Phantom %p initialising command PEG (will take up to "
  1512. "%d seconds)...\n", phantom, PHN_CMDPEG_INIT_TIMEOUT_SEC );
  1513. for ( retries = 0; retries < PHN_CMDPEG_INIT_TIMEOUT_SEC; retries++ ) {
  1514. cmdpeg_state = phantom_readl ( phantom,
  1515. UNM_NIC_REG_CMDPEG_STATE );
  1516. if ( cmdpeg_state != last_cmdpeg_state ) {
  1517. DBGC ( phantom, "Phantom %p command PEG state is "
  1518. "%08lx after %d seconds...\n",
  1519. phantom, cmdpeg_state, retries );
  1520. last_cmdpeg_state = cmdpeg_state;
  1521. }
  1522. if ( cmdpeg_state == UNM_NIC_REG_CMDPEG_STATE_INITIALIZED ) {
  1523. /* Acknowledge the PEG initialisation */
  1524. phantom_writel ( phantom,
  1525. UNM_NIC_REG_CMDPEG_STATE_INITIALIZE_ACK,
  1526. UNM_NIC_REG_CMDPEG_STATE );
  1527. return 0;
  1528. }
  1529. mdelay ( 1000 );
  1530. }
  1531. DBGC ( phantom, "Phantom %p timed out waiting for command PEG to "
  1532. "initialise (status %08lx)\n", phantom, cmdpeg_state );
  1533. return -ETIMEDOUT;
  1534. }
  1535. /**
  1536. * Read Phantom MAC address
  1537. *
  1538. * @v phanton_port Phantom NIC port
  1539. * @v ll_addr Buffer to fill with MAC address
  1540. */
  1541. static void phantom_get_macaddr ( struct phantom_nic_port *phantom_port,
  1542. uint8_t *ll_addr ) {
  1543. struct phantom_nic *phantom = phantom_port->phantom;
  1544. union {
  1545. uint8_t mac_addr[2][ETH_ALEN];
  1546. uint32_t dwords[3];
  1547. } u;
  1548. unsigned long offset;
  1549. int i;
  1550. /* Read the three dwords that include this MAC address and one other */
  1551. offset = ( UNM_CAM_RAM_MAC_ADDRS +
  1552. ( 12 * ( phantom_port->port / 2 ) ) );
  1553. for ( i = 0 ; i < 3 ; i++, offset += 4 ) {
  1554. u.dwords[i] = phantom_readl ( phantom, offset );
  1555. }
  1556. /* Copy out the relevant MAC address */
  1557. for ( i = 0 ; i < ETH_ALEN ; i++ ) {
  1558. ll_addr[ ETH_ALEN - i - 1 ] =
  1559. u.mac_addr[ phantom_port->port & 1 ][i];
  1560. }
  1561. DBGC ( phantom, "Phantom %p port %d MAC address is %s\n",
  1562. phantom, phantom_port->port, eth_ntoa ( ll_addr ) );
  1563. }
  1564. /**
  1565. * Initialise Phantom receive PEG
  1566. *
  1567. * @v phantom Phantom NIC
  1568. * @ret rc Return status code
  1569. */
  1570. static int phantom_init_rcvpeg ( struct phantom_nic *phantom ) {
  1571. unsigned int retries;
  1572. uint32_t rcvpeg_state;
  1573. uint32_t last_rcvpeg_state = 0;
  1574. DBGC ( phantom, "Phantom %p initialising receive PEG (will take up to "
  1575. "%d seconds)...\n", phantom, PHN_RCVPEG_INIT_TIMEOUT_SEC );
  1576. for ( retries = 0; retries < PHN_RCVPEG_INIT_TIMEOUT_SEC; retries++ ) {
  1577. rcvpeg_state = phantom_readl ( phantom,
  1578. UNM_NIC_REG_RCVPEG_STATE );
  1579. if ( rcvpeg_state != last_rcvpeg_state ) {
  1580. DBGC ( phantom, "Phantom %p receive PEG state is "
  1581. "%08lx after %d seconds...\n",
  1582. phantom, rcvpeg_state, retries );
  1583. last_rcvpeg_state = rcvpeg_state;
  1584. }
  1585. if ( rcvpeg_state == UNM_NIC_REG_RCVPEG_STATE_INITIALIZED )
  1586. return 0;
  1587. mdelay ( 1000 );
  1588. }
  1589. DBGC ( phantom, "Phantom %p timed out waiting for receive PEG to "
  1590. "initialise (status %08lx)\n", phantom, rcvpeg_state );
  1591. return -ETIMEDOUT;
  1592. }
  1593. /**
  1594. * Probe PCI device
  1595. *
  1596. * @v pci PCI device
  1597. * @v id PCI ID
  1598. * @ret rc Return status code
  1599. */
  1600. static int phantom_probe ( struct pci_device *pci,
  1601. const struct pci_device_id *id __unused ) {
  1602. struct phantom_nic *phantom;
  1603. struct net_device *netdev;
  1604. struct phantom_nic_port *phantom_port;
  1605. int i;
  1606. int rc;
  1607. /* Phantom NICs expose multiple PCI functions, used for
  1608. * virtualisation. Ignore everything except function 0.
  1609. */
  1610. if ( PCI_FUNC ( pci->devfn ) != 0 )
  1611. return -ENODEV;
  1612. /* Allocate Phantom device */
  1613. phantom = zalloc ( sizeof ( *phantom ) );
  1614. if ( ! phantom ) {
  1615. rc = -ENOMEM;
  1616. goto err_alloc_phantom;
  1617. }
  1618. pci_set_drvdata ( pci, phantom );
  1619. /* Fix up PCI device */
  1620. adjust_pci_device ( pci );
  1621. /* Map CRB */
  1622. if ( ( rc = phantom_map_crb ( phantom, pci ) ) != 0 )
  1623. goto err_map_crb;
  1624. /* Read flash information */
  1625. if ( ( rc = phantom_read_flash ( phantom ) ) != 0 )
  1626. goto err_read_flash;
  1627. /* Allocate net devices for each port */
  1628. for ( i = 0 ; i < phantom->num_ports ; i++ ) {
  1629. netdev = alloc_etherdev ( sizeof ( *phantom_port ) );
  1630. if ( ! netdev ) {
  1631. rc = -ENOMEM;
  1632. goto err_alloc_etherdev;
  1633. }
  1634. phantom->netdev[i] = netdev;
  1635. netdev_init ( netdev, &phantom_operations );
  1636. phantom_port = netdev_priv ( netdev );
  1637. netdev->dev = &pci->dev;
  1638. phantom_port->phantom = phantom;
  1639. phantom_port->port = i;
  1640. }
  1641. /* BUG5945 - need to hack PCI config space on P3 B1 silicon.
  1642. * B2 will have this fixed; remove this hack when B1 is no
  1643. * longer in use.
  1644. */
  1645. for ( i = 0 ; i < 8 ; i++ ) {
  1646. uint32_t temp;
  1647. pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), i );
  1648. pci_read_config_dword ( pci, 0xc8, &temp );
  1649. pci_read_config_dword ( pci, 0xc8, &temp );
  1650. pci_write_config_dword ( pci, 0xc8, 0xf1000 );
  1651. }
  1652. pci->devfn = PCI_DEVFN ( PCI_SLOT ( pci->devfn ), 0 );
  1653. /* Allocate dummy DMA buffer and perform initial hardware handshake */
  1654. phantom->dma_buf = malloc_dma ( sizeof ( *(phantom->dma_buf) ),
  1655. UNM_DMA_BUFFER_ALIGN );
  1656. if ( ! phantom->dma_buf )
  1657. goto err_dma_buf;
  1658. if ( ( rc = phantom_init_cmdpeg ( phantom ) ) != 0 )
  1659. goto err_init_cmdpeg;
  1660. /* Initialise the receive firmware */
  1661. if ( ( rc = phantom_init_rcvpeg ( phantom ) ) != 0 )
  1662. goto err_init_rcvpeg;
  1663. /* Read MAC addresses */
  1664. for ( i = 0 ; i < phantom->num_ports ; i++ ) {
  1665. phantom_port = netdev_priv ( phantom->netdev[i] );
  1666. phantom_get_macaddr ( phantom_port,
  1667. phantom->netdev[i]->ll_addr );
  1668. }
  1669. /* Register network devices */
  1670. for ( i = 0 ; i < phantom->num_ports ; i++ ) {
  1671. if ( ( rc = register_netdev ( phantom->netdev[i] ) ) != 0 ) {
  1672. DBGC ( phantom, "Phantom %p could not register port "
  1673. "%d: %s\n", phantom, i, strerror ( rc ) );
  1674. goto err_register_netdev;
  1675. }
  1676. }
  1677. return 0;
  1678. i = ( phantom->num_ports - 1 );
  1679. err_register_netdev:
  1680. for ( ; i >= 0 ; i-- )
  1681. unregister_netdev ( phantom->netdev[i] );
  1682. err_init_rcvpeg:
  1683. err_init_cmdpeg:
  1684. free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
  1685. phantom->dma_buf = NULL;
  1686. err_dma_buf:
  1687. i = ( phantom->num_ports - 1 );
  1688. err_alloc_etherdev:
  1689. for ( ; i >= 0 ; i-- ) {
  1690. netdev_nullify ( phantom->netdev[i] );
  1691. netdev_put ( phantom->netdev[i] );
  1692. }
  1693. err_read_flash:
  1694. err_map_crb:
  1695. free ( phantom );
  1696. err_alloc_phantom:
  1697. return rc;
  1698. }
  1699. /**
  1700. * Remove PCI device
  1701. *
  1702. * @v pci PCI device
  1703. */
  1704. static void phantom_remove ( struct pci_device *pci ) {
  1705. struct phantom_nic *phantom = pci_get_drvdata ( pci );
  1706. int i;
  1707. for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- )
  1708. unregister_netdev ( phantom->netdev[i] );
  1709. free_dma ( phantom->dma_buf, sizeof ( *(phantom->dma_buf) ) );
  1710. phantom->dma_buf = NULL;
  1711. for ( i = ( phantom->num_ports - 1 ) ; i >= 0 ; i-- ) {
  1712. netdev_nullify ( phantom->netdev[i] );
  1713. netdev_put ( phantom->netdev[i] );
  1714. }
  1715. free ( phantom );
  1716. }
  1717. /** Phantom PCI IDs */
  1718. static struct pci_device_id phantom_nics[] = {
  1719. PCI_ROM ( 0x4040, 0x0100, "nx", "NX" ),
  1720. };
  1721. /** Phantom PCI driver */
  1722. struct pci_driver phantom_driver __pci_driver = {
  1723. .ids = phantom_nics,
  1724. .id_count = ( sizeof ( phantom_nics ) / sizeof ( phantom_nics[0] ) ),
  1725. .probe = phantom_probe,
  1726. .remove = phantom_remove,
  1727. };