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

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