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.

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