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

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