netxen_nic_init.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2003 - 2009 NetXen, Inc.
  4. * Copyright (C) 2009 - QLogic Corporation.
  5. * All rights reserved.
  6. */
  7. #include <linux/netdevice.h>
  8. #include <linux/delay.h>
  9. #include <linux/slab.h>
  10. #include <linux/if_vlan.h>
  11. #include <net/checksum.h>
  12. #include "netxen_nic.h"
  13. #include "netxen_nic_hw.h"
  14. struct crb_addr_pair {
  15. u32 addr;
  16. u32 data;
  17. };
  18. #define NETXEN_MAX_CRB_XFORM 60
  19. static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
  20. #define NETXEN_ADDR_ERROR (0xffffffff)
  21. #define crb_addr_transform(name) \
  22. crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
  23. NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
  24. #define NETXEN_NIC_XDMA_RESET 0x8000ff
  25. static void
  26. netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
  27. struct nx_host_rds_ring *rds_ring);
  28. static int netxen_p3_has_mn(struct netxen_adapter *adapter);
  29. static void crb_addr_transform_setup(void)
  30. {
  31. crb_addr_transform(XDMA);
  32. crb_addr_transform(TIMR);
  33. crb_addr_transform(SRE);
  34. crb_addr_transform(SQN3);
  35. crb_addr_transform(SQN2);
  36. crb_addr_transform(SQN1);
  37. crb_addr_transform(SQN0);
  38. crb_addr_transform(SQS3);
  39. crb_addr_transform(SQS2);
  40. crb_addr_transform(SQS1);
  41. crb_addr_transform(SQS0);
  42. crb_addr_transform(RPMX7);
  43. crb_addr_transform(RPMX6);
  44. crb_addr_transform(RPMX5);
  45. crb_addr_transform(RPMX4);
  46. crb_addr_transform(RPMX3);
  47. crb_addr_transform(RPMX2);
  48. crb_addr_transform(RPMX1);
  49. crb_addr_transform(RPMX0);
  50. crb_addr_transform(ROMUSB);
  51. crb_addr_transform(SN);
  52. crb_addr_transform(QMN);
  53. crb_addr_transform(QMS);
  54. crb_addr_transform(PGNI);
  55. crb_addr_transform(PGND);
  56. crb_addr_transform(PGN3);
  57. crb_addr_transform(PGN2);
  58. crb_addr_transform(PGN1);
  59. crb_addr_transform(PGN0);
  60. crb_addr_transform(PGSI);
  61. crb_addr_transform(PGSD);
  62. crb_addr_transform(PGS3);
  63. crb_addr_transform(PGS2);
  64. crb_addr_transform(PGS1);
  65. crb_addr_transform(PGS0);
  66. crb_addr_transform(PS);
  67. crb_addr_transform(PH);
  68. crb_addr_transform(NIU);
  69. crb_addr_transform(I2Q);
  70. crb_addr_transform(EG);
  71. crb_addr_transform(MN);
  72. crb_addr_transform(MS);
  73. crb_addr_transform(CAS2);
  74. crb_addr_transform(CAS1);
  75. crb_addr_transform(CAS0);
  76. crb_addr_transform(CAM);
  77. crb_addr_transform(C2C1);
  78. crb_addr_transform(C2C0);
  79. crb_addr_transform(SMB);
  80. crb_addr_transform(OCM0);
  81. crb_addr_transform(I2C0);
  82. }
  83. void netxen_release_rx_buffers(struct netxen_adapter *adapter)
  84. {
  85. struct netxen_recv_context *recv_ctx;
  86. struct nx_host_rds_ring *rds_ring;
  87. struct netxen_rx_buffer *rx_buf;
  88. int i, ring;
  89. recv_ctx = &adapter->recv_ctx;
  90. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  91. rds_ring = &recv_ctx->rds_rings[ring];
  92. for (i = 0; i < rds_ring->num_desc; ++i) {
  93. rx_buf = &(rds_ring->rx_buf_arr[i]);
  94. if (rx_buf->state == NETXEN_BUFFER_FREE)
  95. continue;
  96. pci_unmap_single(adapter->pdev,
  97. rx_buf->dma,
  98. rds_ring->dma_size,
  99. PCI_DMA_FROMDEVICE);
  100. if (rx_buf->skb != NULL)
  101. dev_kfree_skb_any(rx_buf->skb);
  102. }
  103. }
  104. }
  105. void netxen_release_tx_buffers(struct netxen_adapter *adapter)
  106. {
  107. struct netxen_cmd_buffer *cmd_buf;
  108. struct netxen_skb_frag *buffrag;
  109. int i, j;
  110. struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
  111. spin_lock_bh(&adapter->tx_clean_lock);
  112. cmd_buf = tx_ring->cmd_buf_arr;
  113. for (i = 0; i < tx_ring->num_desc; i++) {
  114. buffrag = cmd_buf->frag_array;
  115. if (buffrag->dma) {
  116. pci_unmap_single(adapter->pdev, buffrag->dma,
  117. buffrag->length, PCI_DMA_TODEVICE);
  118. buffrag->dma = 0ULL;
  119. }
  120. for (j = 1; j < cmd_buf->frag_count; j++) {
  121. buffrag++;
  122. if (buffrag->dma) {
  123. pci_unmap_page(adapter->pdev, buffrag->dma,
  124. buffrag->length,
  125. PCI_DMA_TODEVICE);
  126. buffrag->dma = 0ULL;
  127. }
  128. }
  129. if (cmd_buf->skb) {
  130. dev_kfree_skb_any(cmd_buf->skb);
  131. cmd_buf->skb = NULL;
  132. }
  133. cmd_buf++;
  134. }
  135. spin_unlock_bh(&adapter->tx_clean_lock);
  136. }
  137. void netxen_free_sw_resources(struct netxen_adapter *adapter)
  138. {
  139. struct netxen_recv_context *recv_ctx;
  140. struct nx_host_rds_ring *rds_ring;
  141. struct nx_host_tx_ring *tx_ring;
  142. int ring;
  143. recv_ctx = &adapter->recv_ctx;
  144. if (recv_ctx->rds_rings == NULL)
  145. goto skip_rds;
  146. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  147. rds_ring = &recv_ctx->rds_rings[ring];
  148. vfree(rds_ring->rx_buf_arr);
  149. rds_ring->rx_buf_arr = NULL;
  150. }
  151. kfree(recv_ctx->rds_rings);
  152. skip_rds:
  153. if (adapter->tx_ring == NULL)
  154. return;
  155. tx_ring = adapter->tx_ring;
  156. vfree(tx_ring->cmd_buf_arr);
  157. kfree(tx_ring);
  158. adapter->tx_ring = NULL;
  159. }
  160. int netxen_alloc_sw_resources(struct netxen_adapter *adapter)
  161. {
  162. struct netxen_recv_context *recv_ctx;
  163. struct nx_host_rds_ring *rds_ring;
  164. struct nx_host_sds_ring *sds_ring;
  165. struct nx_host_tx_ring *tx_ring;
  166. struct netxen_rx_buffer *rx_buf;
  167. int ring, i;
  168. struct netxen_cmd_buffer *cmd_buf_arr;
  169. struct net_device *netdev = adapter->netdev;
  170. tx_ring = kzalloc(sizeof(struct nx_host_tx_ring), GFP_KERNEL);
  171. if (tx_ring == NULL)
  172. return -ENOMEM;
  173. adapter->tx_ring = tx_ring;
  174. tx_ring->num_desc = adapter->num_txd;
  175. tx_ring->txq = netdev_get_tx_queue(netdev, 0);
  176. cmd_buf_arr = vzalloc(TX_BUFF_RINGSIZE(tx_ring));
  177. if (cmd_buf_arr == NULL)
  178. goto err_out;
  179. tx_ring->cmd_buf_arr = cmd_buf_arr;
  180. recv_ctx = &adapter->recv_ctx;
  181. rds_ring = kcalloc(adapter->max_rds_rings,
  182. sizeof(struct nx_host_rds_ring), GFP_KERNEL);
  183. if (rds_ring == NULL)
  184. goto err_out;
  185. recv_ctx->rds_rings = rds_ring;
  186. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  187. rds_ring = &recv_ctx->rds_rings[ring];
  188. switch (ring) {
  189. case RCV_RING_NORMAL:
  190. rds_ring->num_desc = adapter->num_rxd;
  191. if (adapter->ahw.cut_through) {
  192. rds_ring->dma_size =
  193. NX_CT_DEFAULT_RX_BUF_LEN;
  194. rds_ring->skb_size =
  195. NX_CT_DEFAULT_RX_BUF_LEN;
  196. } else {
  197. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  198. rds_ring->dma_size =
  199. NX_P3_RX_BUF_MAX_LEN;
  200. else
  201. rds_ring->dma_size =
  202. NX_P2_RX_BUF_MAX_LEN;
  203. rds_ring->skb_size =
  204. rds_ring->dma_size + NET_IP_ALIGN;
  205. }
  206. break;
  207. case RCV_RING_JUMBO:
  208. rds_ring->num_desc = adapter->num_jumbo_rxd;
  209. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  210. rds_ring->dma_size =
  211. NX_P3_RX_JUMBO_BUF_MAX_LEN;
  212. else
  213. rds_ring->dma_size =
  214. NX_P2_RX_JUMBO_BUF_MAX_LEN;
  215. if (adapter->capabilities & NX_CAP0_HW_LRO)
  216. rds_ring->dma_size += NX_LRO_BUFFER_EXTRA;
  217. rds_ring->skb_size =
  218. rds_ring->dma_size + NET_IP_ALIGN;
  219. break;
  220. case RCV_RING_LRO:
  221. rds_ring->num_desc = adapter->num_lro_rxd;
  222. rds_ring->dma_size = NX_RX_LRO_BUFFER_LENGTH;
  223. rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
  224. break;
  225. }
  226. rds_ring->rx_buf_arr = vzalloc(RCV_BUFF_RINGSIZE(rds_ring));
  227. if (rds_ring->rx_buf_arr == NULL)
  228. /* free whatever was already allocated */
  229. goto err_out;
  230. INIT_LIST_HEAD(&rds_ring->free_list);
  231. /*
  232. * Now go through all of them, set reference handles
  233. * and put them in the queues.
  234. */
  235. rx_buf = rds_ring->rx_buf_arr;
  236. for (i = 0; i < rds_ring->num_desc; i++) {
  237. list_add_tail(&rx_buf->list,
  238. &rds_ring->free_list);
  239. rx_buf->ref_handle = i;
  240. rx_buf->state = NETXEN_BUFFER_FREE;
  241. rx_buf++;
  242. }
  243. spin_lock_init(&rds_ring->lock);
  244. }
  245. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  246. sds_ring = &recv_ctx->sds_rings[ring];
  247. sds_ring->irq = adapter->msix_entries[ring].vector;
  248. sds_ring->adapter = adapter;
  249. sds_ring->num_desc = adapter->num_rxd;
  250. for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
  251. INIT_LIST_HEAD(&sds_ring->free_list[i]);
  252. }
  253. return 0;
  254. err_out:
  255. netxen_free_sw_resources(adapter);
  256. return -ENOMEM;
  257. }
  258. /*
  259. * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
  260. * address to external PCI CRB address.
  261. */
  262. static u32 netxen_decode_crb_addr(u32 addr)
  263. {
  264. int i;
  265. u32 base_addr, offset, pci_base;
  266. crb_addr_transform_setup();
  267. pci_base = NETXEN_ADDR_ERROR;
  268. base_addr = addr & 0xfff00000;
  269. offset = addr & 0x000fffff;
  270. for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
  271. if (crb_addr_xform[i] == base_addr) {
  272. pci_base = i << 20;
  273. break;
  274. }
  275. }
  276. if (pci_base == NETXEN_ADDR_ERROR)
  277. return pci_base;
  278. else
  279. return pci_base + offset;
  280. }
  281. #define NETXEN_MAX_ROM_WAIT_USEC 100
  282. static int netxen_wait_rom_done(struct netxen_adapter *adapter)
  283. {
  284. long timeout = 0;
  285. long done = 0;
  286. cond_resched();
  287. while (done == 0) {
  288. done = NXRD32(adapter, NETXEN_ROMUSB_GLB_STATUS);
  289. done &= 2;
  290. if (++timeout >= NETXEN_MAX_ROM_WAIT_USEC) {
  291. dev_err(&adapter->pdev->dev,
  292. "Timeout reached waiting for rom done");
  293. return -EIO;
  294. }
  295. udelay(1);
  296. }
  297. return 0;
  298. }
  299. static int do_rom_fast_read(struct netxen_adapter *adapter,
  300. int addr, int *valp)
  301. {
  302. NXWR32(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
  303. NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
  304. NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
  305. NXWR32(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
  306. if (netxen_wait_rom_done(adapter)) {
  307. printk("Error waiting for rom done\n");
  308. return -EIO;
  309. }
  310. /* reset abyte_cnt and dummy_byte_cnt */
  311. NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
  312. udelay(10);
  313. NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
  314. *valp = NXRD32(adapter, NETXEN_ROMUSB_ROM_RDATA);
  315. return 0;
  316. }
  317. static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
  318. u8 *bytes, size_t size)
  319. {
  320. int addridx;
  321. int ret = 0;
  322. for (addridx = addr; addridx < (addr + size); addridx += 4) {
  323. int v;
  324. ret = do_rom_fast_read(adapter, addridx, &v);
  325. if (ret != 0)
  326. break;
  327. *(__le32 *)bytes = cpu_to_le32(v);
  328. bytes += 4;
  329. }
  330. return ret;
  331. }
  332. int
  333. netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
  334. u8 *bytes, size_t size)
  335. {
  336. int ret;
  337. ret = netxen_rom_lock(adapter);
  338. if (ret < 0)
  339. return ret;
  340. ret = do_rom_fast_read_words(adapter, addr, bytes, size);
  341. netxen_rom_unlock(adapter);
  342. return ret;
  343. }
  344. int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
  345. {
  346. int ret;
  347. if (netxen_rom_lock(adapter) != 0)
  348. return -EIO;
  349. ret = do_rom_fast_read(adapter, addr, valp);
  350. netxen_rom_unlock(adapter);
  351. return ret;
  352. }
  353. #define NETXEN_BOARDTYPE 0x4008
  354. #define NETXEN_BOARDNUM 0x400c
  355. #define NETXEN_CHIPNUM 0x4010
  356. int netxen_pinit_from_rom(struct netxen_adapter *adapter)
  357. {
  358. int addr, val;
  359. int i, n, init_delay = 0;
  360. struct crb_addr_pair *buf;
  361. unsigned offset;
  362. u32 off;
  363. /* resetall */
  364. netxen_rom_lock(adapter);
  365. NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0xfeffffff);
  366. netxen_rom_unlock(adapter);
  367. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  368. if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
  369. (n != 0xcafecafe) ||
  370. netxen_rom_fast_read(adapter, 4, &n) != 0) {
  371. printk(KERN_ERR "%s: ERROR Reading crb_init area: "
  372. "n: %08x\n", netxen_nic_driver_name, n);
  373. return -EIO;
  374. }
  375. offset = n & 0xffffU;
  376. n = (n >> 16) & 0xffffU;
  377. } else {
  378. if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
  379. !(n & 0x80000000)) {
  380. printk(KERN_ERR "%s: ERROR Reading crb_init area: "
  381. "n: %08x\n", netxen_nic_driver_name, n);
  382. return -EIO;
  383. }
  384. offset = 1;
  385. n &= ~0x80000000;
  386. }
  387. if (n >= 1024) {
  388. printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
  389. " initialized.\n", __func__, n);
  390. return -EIO;
  391. }
  392. buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
  393. if (buf == NULL)
  394. return -ENOMEM;
  395. for (i = 0; i < n; i++) {
  396. if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
  397. netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
  398. kfree(buf);
  399. return -EIO;
  400. }
  401. buf[i].addr = addr;
  402. buf[i].data = val;
  403. }
  404. for (i = 0; i < n; i++) {
  405. off = netxen_decode_crb_addr(buf[i].addr);
  406. if (off == NETXEN_ADDR_ERROR) {
  407. printk(KERN_ERR"CRB init value out of range %x\n",
  408. buf[i].addr);
  409. continue;
  410. }
  411. off += NETXEN_PCI_CRBSPACE;
  412. if (off & 1)
  413. continue;
  414. /* skipping cold reboot MAGIC */
  415. if (off == NETXEN_CAM_RAM(0x1fc))
  416. continue;
  417. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  418. if (off == (NETXEN_CRB_I2C0 + 0x1c))
  419. continue;
  420. /* do not reset PCI */
  421. if (off == (ROMUSB_GLB + 0xbc))
  422. continue;
  423. if (off == (ROMUSB_GLB + 0xa8))
  424. continue;
  425. if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
  426. continue;
  427. if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
  428. continue;
  429. if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
  430. continue;
  431. if ((off & 0x0ff00000) == NETXEN_CRB_DDR_NET)
  432. continue;
  433. if (off == (NETXEN_CRB_PEG_NET_1 + 0x18) &&
  434. !NX_IS_REVISION_P3P(adapter->ahw.revision_id))
  435. buf[i].data = 0x1020;
  436. /* skip the function enable register */
  437. if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
  438. continue;
  439. if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
  440. continue;
  441. if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
  442. continue;
  443. }
  444. init_delay = 1;
  445. /* After writing this register, HW needs time for CRB */
  446. /* to quiet down (else crb_window returns 0xffffffff) */
  447. if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
  448. init_delay = 1000;
  449. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  450. /* hold xdma in reset also */
  451. buf[i].data = NETXEN_NIC_XDMA_RESET;
  452. buf[i].data = 0x8000ff;
  453. }
  454. }
  455. NXWR32(adapter, off, buf[i].data);
  456. msleep(init_delay);
  457. }
  458. kfree(buf);
  459. /* disable_peg_cache_all */
  460. /* unreset_net_cache */
  461. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  462. val = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
  463. NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
  464. }
  465. /* p2dn replyCount */
  466. NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
  467. /* disable_peg_cache 0 */
  468. NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
  469. /* disable_peg_cache 1 */
  470. NXWR32(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
  471. /* peg_clr_all */
  472. /* peg_clr 0 */
  473. NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
  474. NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
  475. /* peg_clr 1 */
  476. NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
  477. NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
  478. /* peg_clr 2 */
  479. NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
  480. NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
  481. /* peg_clr 3 */
  482. NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
  483. NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
  484. return 0;
  485. }
  486. static struct uni_table_desc *nx_get_table_desc(const u8 *unirom, int section)
  487. {
  488. uint32_t i;
  489. struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
  490. __le32 entries = cpu_to_le32(directory->num_entries);
  491. for (i = 0; i < entries; i++) {
  492. __le32 offs = cpu_to_le32(directory->findex) +
  493. (i * cpu_to_le32(directory->entry_size));
  494. __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
  495. if (tab_type == section)
  496. return (struct uni_table_desc *) &unirom[offs];
  497. }
  498. return NULL;
  499. }
  500. #define QLCNIC_FILEHEADER_SIZE (14 * 4)
  501. static int
  502. netxen_nic_validate_header(struct netxen_adapter *adapter)
  503. {
  504. const u8 *unirom = adapter->fw->data;
  505. struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
  506. u32 fw_file_size = adapter->fw->size;
  507. u32 tab_size;
  508. __le32 entries;
  509. __le32 entry_size;
  510. if (fw_file_size < QLCNIC_FILEHEADER_SIZE)
  511. return -EINVAL;
  512. entries = cpu_to_le32(directory->num_entries);
  513. entry_size = cpu_to_le32(directory->entry_size);
  514. tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
  515. if (fw_file_size < tab_size)
  516. return -EINVAL;
  517. return 0;
  518. }
  519. static int
  520. netxen_nic_validate_bootld(struct netxen_adapter *adapter)
  521. {
  522. struct uni_table_desc *tab_desc;
  523. struct uni_data_desc *descr;
  524. const u8 *unirom = adapter->fw->data;
  525. __le32 idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
  526. NX_UNI_BOOTLD_IDX_OFF));
  527. u32 offs;
  528. u32 tab_size;
  529. u32 data_size;
  530. tab_desc = nx_get_table_desc(unirom, NX_UNI_DIR_SECT_BOOTLD);
  531. if (!tab_desc)
  532. return -EINVAL;
  533. tab_size = cpu_to_le32(tab_desc->findex) +
  534. (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
  535. if (adapter->fw->size < tab_size)
  536. return -EINVAL;
  537. offs = cpu_to_le32(tab_desc->findex) +
  538. (cpu_to_le32(tab_desc->entry_size) * (idx));
  539. descr = (struct uni_data_desc *)&unirom[offs];
  540. data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
  541. if (adapter->fw->size < data_size)
  542. return -EINVAL;
  543. return 0;
  544. }
  545. static int
  546. netxen_nic_validate_fw(struct netxen_adapter *adapter)
  547. {
  548. struct uni_table_desc *tab_desc;
  549. struct uni_data_desc *descr;
  550. const u8 *unirom = adapter->fw->data;
  551. __le32 idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
  552. NX_UNI_FIRMWARE_IDX_OFF));
  553. u32 offs;
  554. u32 tab_size;
  555. u32 data_size;
  556. tab_desc = nx_get_table_desc(unirom, NX_UNI_DIR_SECT_FW);
  557. if (!tab_desc)
  558. return -EINVAL;
  559. tab_size = cpu_to_le32(tab_desc->findex) +
  560. (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
  561. if (adapter->fw->size < tab_size)
  562. return -EINVAL;
  563. offs = cpu_to_le32(tab_desc->findex) +
  564. (cpu_to_le32(tab_desc->entry_size) * (idx));
  565. descr = (struct uni_data_desc *)&unirom[offs];
  566. data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
  567. if (adapter->fw->size < data_size)
  568. return -EINVAL;
  569. return 0;
  570. }
  571. static int
  572. netxen_nic_validate_product_offs(struct netxen_adapter *adapter)
  573. {
  574. struct uni_table_desc *ptab_descr;
  575. const u8 *unirom = adapter->fw->data;
  576. int mn_present = (NX_IS_REVISION_P2(adapter->ahw.revision_id)) ?
  577. 1 : netxen_p3_has_mn(adapter);
  578. __le32 entries;
  579. __le32 entry_size;
  580. u32 tab_size;
  581. u32 i;
  582. ptab_descr = nx_get_table_desc(unirom, NX_UNI_DIR_SECT_PRODUCT_TBL);
  583. if (ptab_descr == NULL)
  584. return -EINVAL;
  585. entries = cpu_to_le32(ptab_descr->num_entries);
  586. entry_size = cpu_to_le32(ptab_descr->entry_size);
  587. tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
  588. if (adapter->fw->size < tab_size)
  589. return -EINVAL;
  590. nomn:
  591. for (i = 0; i < entries; i++) {
  592. __le32 flags, file_chiprev, offs;
  593. u8 chiprev = adapter->ahw.revision_id;
  594. uint32_t flagbit;
  595. offs = cpu_to_le32(ptab_descr->findex) +
  596. (i * cpu_to_le32(ptab_descr->entry_size));
  597. flags = cpu_to_le32(*((int *)&unirom[offs] + NX_UNI_FLAGS_OFF));
  598. file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
  599. NX_UNI_CHIP_REV_OFF));
  600. flagbit = mn_present ? 1 : 2;
  601. if ((chiprev == file_chiprev) &&
  602. ((1ULL << flagbit) & flags)) {
  603. adapter->file_prd_off = offs;
  604. return 0;
  605. }
  606. }
  607. if (mn_present && NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  608. mn_present = 0;
  609. goto nomn;
  610. }
  611. return -EINVAL;
  612. }
  613. static int
  614. netxen_nic_validate_unified_romimage(struct netxen_adapter *adapter)
  615. {
  616. if (netxen_nic_validate_header(adapter)) {
  617. dev_err(&adapter->pdev->dev,
  618. "unified image: header validation failed\n");
  619. return -EINVAL;
  620. }
  621. if (netxen_nic_validate_product_offs(adapter)) {
  622. dev_err(&adapter->pdev->dev,
  623. "unified image: product validation failed\n");
  624. return -EINVAL;
  625. }
  626. if (netxen_nic_validate_bootld(adapter)) {
  627. dev_err(&adapter->pdev->dev,
  628. "unified image: bootld validation failed\n");
  629. return -EINVAL;
  630. }
  631. if (netxen_nic_validate_fw(adapter)) {
  632. dev_err(&adapter->pdev->dev,
  633. "unified image: firmware validation failed\n");
  634. return -EINVAL;
  635. }
  636. return 0;
  637. }
  638. static struct uni_data_desc *nx_get_data_desc(struct netxen_adapter *adapter,
  639. u32 section, u32 idx_offset)
  640. {
  641. const u8 *unirom = adapter->fw->data;
  642. int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
  643. idx_offset));
  644. struct uni_table_desc *tab_desc;
  645. __le32 offs;
  646. tab_desc = nx_get_table_desc(unirom, section);
  647. if (tab_desc == NULL)
  648. return NULL;
  649. offs = cpu_to_le32(tab_desc->findex) +
  650. (cpu_to_le32(tab_desc->entry_size) * idx);
  651. return (struct uni_data_desc *)&unirom[offs];
  652. }
  653. static u8 *
  654. nx_get_bootld_offs(struct netxen_adapter *adapter)
  655. {
  656. u32 offs = NETXEN_BOOTLD_START;
  657. if (adapter->fw_type == NX_UNIFIED_ROMIMAGE)
  658. offs = cpu_to_le32((nx_get_data_desc(adapter,
  659. NX_UNI_DIR_SECT_BOOTLD,
  660. NX_UNI_BOOTLD_IDX_OFF))->findex);
  661. return (u8 *)&adapter->fw->data[offs];
  662. }
  663. static u8 *
  664. nx_get_fw_offs(struct netxen_adapter *adapter)
  665. {
  666. u32 offs = NETXEN_IMAGE_START;
  667. if (adapter->fw_type == NX_UNIFIED_ROMIMAGE)
  668. offs = cpu_to_le32((nx_get_data_desc(adapter,
  669. NX_UNI_DIR_SECT_FW,
  670. NX_UNI_FIRMWARE_IDX_OFF))->findex);
  671. return (u8 *)&adapter->fw->data[offs];
  672. }
  673. static __le32
  674. nx_get_fw_size(struct netxen_adapter *adapter)
  675. {
  676. if (adapter->fw_type == NX_UNIFIED_ROMIMAGE)
  677. return cpu_to_le32((nx_get_data_desc(adapter,
  678. NX_UNI_DIR_SECT_FW,
  679. NX_UNI_FIRMWARE_IDX_OFF))->size);
  680. else
  681. return cpu_to_le32(
  682. *(u32 *)&adapter->fw->data[NX_FW_SIZE_OFFSET]);
  683. }
  684. static __le32
  685. nx_get_fw_version(struct netxen_adapter *adapter)
  686. {
  687. struct uni_data_desc *fw_data_desc;
  688. const struct firmware *fw = adapter->fw;
  689. __le32 major, minor, sub;
  690. const u8 *ver_str;
  691. int i, ret = 0;
  692. if (adapter->fw_type == NX_UNIFIED_ROMIMAGE) {
  693. fw_data_desc = nx_get_data_desc(adapter,
  694. NX_UNI_DIR_SECT_FW, NX_UNI_FIRMWARE_IDX_OFF);
  695. ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
  696. cpu_to_le32(fw_data_desc->size) - 17;
  697. for (i = 0; i < 12; i++) {
  698. if (!strncmp(&ver_str[i], "REV=", 4)) {
  699. ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
  700. &major, &minor, &sub);
  701. break;
  702. }
  703. }
  704. if (ret != 3)
  705. return 0;
  706. return major + (minor << 8) + (sub << 16);
  707. } else
  708. return cpu_to_le32(*(u32 *)&fw->data[NX_FW_VERSION_OFFSET]);
  709. }
  710. static __le32
  711. nx_get_bios_version(struct netxen_adapter *adapter)
  712. {
  713. const struct firmware *fw = adapter->fw;
  714. __le32 bios_ver, prd_off = adapter->file_prd_off;
  715. if (adapter->fw_type == NX_UNIFIED_ROMIMAGE) {
  716. bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
  717. + NX_UNI_BIOS_VERSION_OFF));
  718. return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) +
  719. (bios_ver >> 24);
  720. } else
  721. return cpu_to_le32(*(u32 *)&fw->data[NX_BIOS_VERSION_OFFSET]);
  722. }
  723. int
  724. netxen_need_fw_reset(struct netxen_adapter *adapter)
  725. {
  726. u32 count, old_count;
  727. u32 val, version, major, minor, build;
  728. int i, timeout;
  729. u8 fw_type;
  730. /* NX2031 firmware doesn't support heartbit */
  731. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  732. return 1;
  733. if (adapter->need_fw_reset)
  734. return 1;
  735. /* last attempt had failed */
  736. if (NXRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED)
  737. return 1;
  738. old_count = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
  739. for (i = 0; i < 10; i++) {
  740. timeout = msleep_interruptible(200);
  741. if (timeout) {
  742. NXWR32(adapter, CRB_CMDPEG_STATE,
  743. PHAN_INITIALIZE_FAILED);
  744. return -EINTR;
  745. }
  746. count = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
  747. if (count != old_count)
  748. break;
  749. }
  750. /* firmware is dead */
  751. if (count == old_count)
  752. return 1;
  753. /* check if we have got newer or different file firmware */
  754. if (adapter->fw) {
  755. val = nx_get_fw_version(adapter);
  756. version = NETXEN_DECODE_VERSION(val);
  757. major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
  758. minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
  759. build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
  760. if (version > NETXEN_VERSION_CODE(major, minor, build))
  761. return 1;
  762. if (version == NETXEN_VERSION_CODE(major, minor, build) &&
  763. adapter->fw_type != NX_UNIFIED_ROMIMAGE) {
  764. val = NXRD32(adapter, NETXEN_MIU_MN_CONTROL);
  765. fw_type = (val & 0x4) ?
  766. NX_P3_CT_ROMIMAGE : NX_P3_MN_ROMIMAGE;
  767. if (adapter->fw_type != fw_type)
  768. return 1;
  769. }
  770. }
  771. return 0;
  772. }
  773. #define NETXEN_MIN_P3_FW_SUPP NETXEN_VERSION_CODE(4, 0, 505)
  774. int
  775. netxen_check_flash_fw_compatibility(struct netxen_adapter *adapter)
  776. {
  777. u32 flash_fw_ver, min_fw_ver;
  778. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  779. return 0;
  780. if (netxen_rom_fast_read(adapter,
  781. NX_FW_VERSION_OFFSET, (int *)&flash_fw_ver)) {
  782. dev_err(&adapter->pdev->dev, "Unable to read flash fw"
  783. "version\n");
  784. return -EIO;
  785. }
  786. flash_fw_ver = NETXEN_DECODE_VERSION(flash_fw_ver);
  787. min_fw_ver = NETXEN_MIN_P3_FW_SUPP;
  788. if (flash_fw_ver >= min_fw_ver)
  789. return 0;
  790. dev_info(&adapter->pdev->dev, "Flash fw[%d.%d.%d] is < min fw supported"
  791. "[4.0.505]. Please update firmware on flash\n",
  792. _major(flash_fw_ver), _minor(flash_fw_ver),
  793. _build(flash_fw_ver));
  794. return -EINVAL;
  795. }
  796. static char *fw_name[] = {
  797. NX_P2_MN_ROMIMAGE_NAME,
  798. NX_P3_CT_ROMIMAGE_NAME,
  799. NX_P3_MN_ROMIMAGE_NAME,
  800. NX_UNIFIED_ROMIMAGE_NAME,
  801. NX_FLASH_ROMIMAGE_NAME,
  802. };
  803. int
  804. netxen_load_firmware(struct netxen_adapter *adapter)
  805. {
  806. u64 *ptr64;
  807. u32 i, flashaddr, size;
  808. const struct firmware *fw = adapter->fw;
  809. struct pci_dev *pdev = adapter->pdev;
  810. dev_info(&pdev->dev, "loading firmware from %s\n",
  811. fw_name[adapter->fw_type]);
  812. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  813. NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 1);
  814. if (fw) {
  815. __le64 data;
  816. size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 8;
  817. ptr64 = (u64 *)nx_get_bootld_offs(adapter);
  818. flashaddr = NETXEN_BOOTLD_START;
  819. for (i = 0; i < size; i++) {
  820. data = cpu_to_le64(ptr64[i]);
  821. if (adapter->pci_mem_write(adapter, flashaddr, data))
  822. return -EIO;
  823. flashaddr += 8;
  824. }
  825. size = (__force u32)nx_get_fw_size(adapter) / 8;
  826. ptr64 = (u64 *)nx_get_fw_offs(adapter);
  827. flashaddr = NETXEN_IMAGE_START;
  828. for (i = 0; i < size; i++) {
  829. data = cpu_to_le64(ptr64[i]);
  830. if (adapter->pci_mem_write(adapter,
  831. flashaddr, data))
  832. return -EIO;
  833. flashaddr += 8;
  834. }
  835. size = (__force u32)nx_get_fw_size(adapter) % 8;
  836. if (size) {
  837. data = cpu_to_le64(ptr64[i]);
  838. if (adapter->pci_mem_write(adapter,
  839. flashaddr, data))
  840. return -EIO;
  841. }
  842. } else {
  843. u64 data;
  844. u32 hi, lo;
  845. size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 8;
  846. flashaddr = NETXEN_BOOTLD_START;
  847. for (i = 0; i < size; i++) {
  848. if (netxen_rom_fast_read(adapter,
  849. flashaddr, (int *)&lo) != 0)
  850. return -EIO;
  851. if (netxen_rom_fast_read(adapter,
  852. flashaddr + 4, (int *)&hi) != 0)
  853. return -EIO;
  854. /* hi, lo are already in host endian byteorder */
  855. data = (((u64)hi << 32) | lo);
  856. if (adapter->pci_mem_write(adapter,
  857. flashaddr, data))
  858. return -EIO;
  859. flashaddr += 8;
  860. }
  861. }
  862. msleep(1);
  863. if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
  864. NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0x18, 0x1020);
  865. NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0x80001e);
  866. } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  867. NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0x80001d);
  868. else {
  869. NXWR32(adapter, NETXEN_ROMUSB_GLB_CHIP_CLK_CTRL, 0x3fff);
  870. NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 0);
  871. }
  872. return 0;
  873. }
  874. static int
  875. netxen_validate_firmware(struct netxen_adapter *adapter)
  876. {
  877. __le32 val;
  878. __le32 flash_fw_ver;
  879. u32 file_fw_ver, min_ver, bios;
  880. struct pci_dev *pdev = adapter->pdev;
  881. const struct firmware *fw = adapter->fw;
  882. u8 fw_type = adapter->fw_type;
  883. u32 crbinit_fix_fw;
  884. if (fw_type == NX_UNIFIED_ROMIMAGE) {
  885. if (netxen_nic_validate_unified_romimage(adapter))
  886. return -EINVAL;
  887. } else {
  888. val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_MAGIC_OFFSET]);
  889. if ((__force u32)val != NETXEN_BDINFO_MAGIC)
  890. return -EINVAL;
  891. if (fw->size < NX_FW_MIN_SIZE)
  892. return -EINVAL;
  893. }
  894. val = nx_get_fw_version(adapter);
  895. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  896. min_ver = NETXEN_MIN_P3_FW_SUPP;
  897. else
  898. min_ver = NETXEN_VERSION_CODE(3, 4, 216);
  899. file_fw_ver = NETXEN_DECODE_VERSION(val);
  900. if ((_major(file_fw_ver) > _NETXEN_NIC_LINUX_MAJOR) ||
  901. (file_fw_ver < min_ver)) {
  902. dev_err(&pdev->dev,
  903. "%s: firmware version %d.%d.%d unsupported\n",
  904. fw_name[fw_type], _major(file_fw_ver), _minor(file_fw_ver),
  905. _build(file_fw_ver));
  906. return -EINVAL;
  907. }
  908. val = nx_get_bios_version(adapter);
  909. if (netxen_rom_fast_read(adapter, NX_BIOS_VERSION_OFFSET, (int *)&bios))
  910. return -EIO;
  911. if ((__force u32)val != bios) {
  912. dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
  913. fw_name[fw_type]);
  914. return -EINVAL;
  915. }
  916. if (netxen_rom_fast_read(adapter,
  917. NX_FW_VERSION_OFFSET, (int *)&flash_fw_ver)) {
  918. dev_err(&pdev->dev, "Unable to read flash fw version\n");
  919. return -EIO;
  920. }
  921. flash_fw_ver = NETXEN_DECODE_VERSION(flash_fw_ver);
  922. /* New fw from file is not allowed, if fw on flash is < 4.0.554 */
  923. crbinit_fix_fw = NETXEN_VERSION_CODE(4, 0, 554);
  924. if (file_fw_ver >= crbinit_fix_fw && flash_fw_ver < crbinit_fix_fw &&
  925. NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  926. dev_err(&pdev->dev, "Incompatibility detected between driver "
  927. "and firmware version on flash. This configuration "
  928. "is not recommended. Please update the firmware on "
  929. "flash immediately\n");
  930. return -EINVAL;
  931. }
  932. /* check if flashed firmware is newer only for no-mn and P2 case*/
  933. if (!netxen_p3_has_mn(adapter) ||
  934. NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  935. if (flash_fw_ver > file_fw_ver) {
  936. dev_info(&pdev->dev, "%s: firmware is older than flash\n",
  937. fw_name[fw_type]);
  938. return -EINVAL;
  939. }
  940. }
  941. NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
  942. return 0;
  943. }
  944. static void
  945. nx_get_next_fwtype(struct netxen_adapter *adapter)
  946. {
  947. u8 fw_type;
  948. switch (adapter->fw_type) {
  949. case NX_UNKNOWN_ROMIMAGE:
  950. fw_type = NX_UNIFIED_ROMIMAGE;
  951. break;
  952. case NX_UNIFIED_ROMIMAGE:
  953. if (NX_IS_REVISION_P3P(adapter->ahw.revision_id))
  954. fw_type = NX_FLASH_ROMIMAGE;
  955. else if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  956. fw_type = NX_P2_MN_ROMIMAGE;
  957. else if (netxen_p3_has_mn(adapter))
  958. fw_type = NX_P3_MN_ROMIMAGE;
  959. else
  960. fw_type = NX_P3_CT_ROMIMAGE;
  961. break;
  962. case NX_P3_MN_ROMIMAGE:
  963. fw_type = NX_P3_CT_ROMIMAGE;
  964. break;
  965. case NX_P2_MN_ROMIMAGE:
  966. case NX_P3_CT_ROMIMAGE:
  967. default:
  968. fw_type = NX_FLASH_ROMIMAGE;
  969. break;
  970. }
  971. adapter->fw_type = fw_type;
  972. }
  973. static int
  974. netxen_p3_has_mn(struct netxen_adapter *adapter)
  975. {
  976. u32 capability, flashed_ver;
  977. capability = 0;
  978. /* NX2031 always had MN */
  979. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  980. return 1;
  981. netxen_rom_fast_read(adapter,
  982. NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
  983. flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
  984. if (flashed_ver >= NETXEN_VERSION_CODE(4, 0, 220)) {
  985. capability = NXRD32(adapter, NX_PEG_TUNE_CAPABILITY);
  986. if (capability & NX_PEG_TUNE_MN_PRESENT)
  987. return 1;
  988. }
  989. return 0;
  990. }
  991. void netxen_request_firmware(struct netxen_adapter *adapter)
  992. {
  993. struct pci_dev *pdev = adapter->pdev;
  994. int rc = 0;
  995. adapter->fw_type = NX_UNKNOWN_ROMIMAGE;
  996. next:
  997. nx_get_next_fwtype(adapter);
  998. if (adapter->fw_type == NX_FLASH_ROMIMAGE) {
  999. adapter->fw = NULL;
  1000. } else {
  1001. rc = request_firmware(&adapter->fw,
  1002. fw_name[adapter->fw_type], &pdev->dev);
  1003. if (rc != 0)
  1004. goto next;
  1005. rc = netxen_validate_firmware(adapter);
  1006. if (rc != 0) {
  1007. release_firmware(adapter->fw);
  1008. msleep(1);
  1009. goto next;
  1010. }
  1011. }
  1012. }
  1013. void
  1014. netxen_release_firmware(struct netxen_adapter *adapter)
  1015. {
  1016. release_firmware(adapter->fw);
  1017. adapter->fw = NULL;
  1018. }
  1019. int netxen_init_dummy_dma(struct netxen_adapter *adapter)
  1020. {
  1021. u64 addr;
  1022. u32 hi, lo;
  1023. if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
  1024. return 0;
  1025. adapter->dummy_dma.addr = pci_alloc_consistent(adapter->pdev,
  1026. NETXEN_HOST_DUMMY_DMA_SIZE,
  1027. &adapter->dummy_dma.phys_addr);
  1028. if (adapter->dummy_dma.addr == NULL) {
  1029. dev_err(&adapter->pdev->dev,
  1030. "ERROR: Could not allocate dummy DMA memory\n");
  1031. return -ENOMEM;
  1032. }
  1033. addr = (uint64_t) adapter->dummy_dma.phys_addr;
  1034. hi = (addr >> 32) & 0xffffffff;
  1035. lo = addr & 0xffffffff;
  1036. NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
  1037. NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
  1038. return 0;
  1039. }
  1040. /*
  1041. * NetXen DMA watchdog control:
  1042. *
  1043. * Bit 0 : enabled => R/O: 1 watchdog active, 0 inactive
  1044. * Bit 1 : disable_request => 1 req disable dma watchdog
  1045. * Bit 2 : enable_request => 1 req enable dma watchdog
  1046. * Bit 3-31 : unused
  1047. */
  1048. void netxen_free_dummy_dma(struct netxen_adapter *adapter)
  1049. {
  1050. int i = 100;
  1051. u32 ctrl;
  1052. if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
  1053. return;
  1054. if (!adapter->dummy_dma.addr)
  1055. return;
  1056. ctrl = NXRD32(adapter, NETXEN_DMA_WATCHDOG_CTRL);
  1057. if ((ctrl & 0x1) != 0) {
  1058. NXWR32(adapter, NETXEN_DMA_WATCHDOG_CTRL, (ctrl | 0x2));
  1059. while ((ctrl & 0x1) != 0) {
  1060. msleep(50);
  1061. ctrl = NXRD32(adapter, NETXEN_DMA_WATCHDOG_CTRL);
  1062. if (--i == 0)
  1063. break;
  1064. }
  1065. }
  1066. if (i) {
  1067. pci_free_consistent(adapter->pdev,
  1068. NETXEN_HOST_DUMMY_DMA_SIZE,
  1069. adapter->dummy_dma.addr,
  1070. adapter->dummy_dma.phys_addr);
  1071. adapter->dummy_dma.addr = NULL;
  1072. } else
  1073. dev_err(&adapter->pdev->dev, "dma_watchdog_shutdown failed\n");
  1074. }
  1075. int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
  1076. {
  1077. u32 val = 0;
  1078. int retries = 60;
  1079. if (pegtune_val)
  1080. return 0;
  1081. do {
  1082. val = NXRD32(adapter, CRB_CMDPEG_STATE);
  1083. switch (val) {
  1084. case PHAN_INITIALIZE_COMPLETE:
  1085. case PHAN_INITIALIZE_ACK:
  1086. return 0;
  1087. case PHAN_INITIALIZE_FAILED:
  1088. goto out_err;
  1089. default:
  1090. break;
  1091. }
  1092. msleep(500);
  1093. } while (--retries);
  1094. NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
  1095. out_err:
  1096. dev_warn(&adapter->pdev->dev, "firmware init failed\n");
  1097. return -EIO;
  1098. }
  1099. static int
  1100. netxen_receive_peg_ready(struct netxen_adapter *adapter)
  1101. {
  1102. u32 val = 0;
  1103. int retries = 2000;
  1104. do {
  1105. val = NXRD32(adapter, CRB_RCVPEG_STATE);
  1106. if (val == PHAN_PEG_RCV_INITIALIZED)
  1107. return 0;
  1108. msleep(10);
  1109. } while (--retries);
  1110. pr_err("Receive Peg initialization not complete, state: 0x%x.\n", val);
  1111. return -EIO;
  1112. }
  1113. int netxen_init_firmware(struct netxen_adapter *adapter)
  1114. {
  1115. int err;
  1116. err = netxen_receive_peg_ready(adapter);
  1117. if (err)
  1118. return err;
  1119. NXWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
  1120. NXWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
  1121. NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
  1122. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  1123. NXWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
  1124. return err;
  1125. }
  1126. static void
  1127. netxen_handle_linkevent(struct netxen_adapter *adapter, nx_fw_msg_t *msg)
  1128. {
  1129. u32 cable_OUI;
  1130. u16 cable_len;
  1131. u16 link_speed;
  1132. u8 link_status, module, duplex, autoneg;
  1133. struct net_device *netdev = adapter->netdev;
  1134. adapter->has_link_events = 1;
  1135. cable_OUI = msg->body[1] & 0xffffffff;
  1136. cable_len = (msg->body[1] >> 32) & 0xffff;
  1137. link_speed = (msg->body[1] >> 48) & 0xffff;
  1138. link_status = msg->body[2] & 0xff;
  1139. duplex = (msg->body[2] >> 16) & 0xff;
  1140. autoneg = (msg->body[2] >> 24) & 0xff;
  1141. module = (msg->body[2] >> 8) & 0xff;
  1142. if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) {
  1143. printk(KERN_INFO "%s: unsupported cable: OUI 0x%x, length %d\n",
  1144. netdev->name, cable_OUI, cable_len);
  1145. } else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) {
  1146. printk(KERN_INFO "%s: unsupported cable length %d\n",
  1147. netdev->name, cable_len);
  1148. }
  1149. /* update link parameters */
  1150. if (duplex == LINKEVENT_FULL_DUPLEX)
  1151. adapter->link_duplex = DUPLEX_FULL;
  1152. else
  1153. adapter->link_duplex = DUPLEX_HALF;
  1154. adapter->module_type = module;
  1155. adapter->link_autoneg = autoneg;
  1156. adapter->link_speed = link_speed;
  1157. netxen_advert_link_change(adapter, link_status);
  1158. }
  1159. static void
  1160. netxen_handle_fw_message(int desc_cnt, int index,
  1161. struct nx_host_sds_ring *sds_ring)
  1162. {
  1163. nx_fw_msg_t msg;
  1164. struct status_desc *desc;
  1165. int i = 0, opcode;
  1166. while (desc_cnt > 0 && i < 8) {
  1167. desc = &sds_ring->desc_head[index];
  1168. msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
  1169. msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
  1170. index = get_next_index(index, sds_ring->num_desc);
  1171. desc_cnt--;
  1172. }
  1173. opcode = netxen_get_nic_msg_opcode(msg.body[0]);
  1174. switch (opcode) {
  1175. case NX_NIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
  1176. netxen_handle_linkevent(sds_ring->adapter, &msg);
  1177. break;
  1178. default:
  1179. break;
  1180. }
  1181. }
  1182. static int
  1183. netxen_alloc_rx_skb(struct netxen_adapter *adapter,
  1184. struct nx_host_rds_ring *rds_ring,
  1185. struct netxen_rx_buffer *buffer)
  1186. {
  1187. struct sk_buff *skb;
  1188. dma_addr_t dma;
  1189. struct pci_dev *pdev = adapter->pdev;
  1190. buffer->skb = netdev_alloc_skb(adapter->netdev, rds_ring->skb_size);
  1191. if (!buffer->skb)
  1192. return 1;
  1193. skb = buffer->skb;
  1194. if (!adapter->ahw.cut_through)
  1195. skb_reserve(skb, 2);
  1196. dma = pci_map_single(pdev, skb->data,
  1197. rds_ring->dma_size, PCI_DMA_FROMDEVICE);
  1198. if (pci_dma_mapping_error(pdev, dma)) {
  1199. dev_kfree_skb_any(skb);
  1200. buffer->skb = NULL;
  1201. return 1;
  1202. }
  1203. buffer->skb = skb;
  1204. buffer->dma = dma;
  1205. buffer->state = NETXEN_BUFFER_BUSY;
  1206. return 0;
  1207. }
  1208. static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
  1209. struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
  1210. {
  1211. struct netxen_rx_buffer *buffer;
  1212. struct sk_buff *skb;
  1213. buffer = &rds_ring->rx_buf_arr[index];
  1214. pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
  1215. PCI_DMA_FROMDEVICE);
  1216. skb = buffer->skb;
  1217. if (!skb)
  1218. goto no_skb;
  1219. if (likely((adapter->netdev->features & NETIF_F_RXCSUM)
  1220. && cksum == STATUS_CKSUM_OK)) {
  1221. adapter->stats.csummed++;
  1222. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1223. } else
  1224. skb->ip_summed = CHECKSUM_NONE;
  1225. buffer->skb = NULL;
  1226. no_skb:
  1227. buffer->state = NETXEN_BUFFER_FREE;
  1228. return skb;
  1229. }
  1230. static struct netxen_rx_buffer *
  1231. netxen_process_rcv(struct netxen_adapter *adapter,
  1232. struct nx_host_sds_ring *sds_ring,
  1233. int ring, u64 sts_data0)
  1234. {
  1235. struct net_device *netdev = adapter->netdev;
  1236. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  1237. struct netxen_rx_buffer *buffer;
  1238. struct sk_buff *skb;
  1239. struct nx_host_rds_ring *rds_ring;
  1240. int index, length, cksum, pkt_offset;
  1241. if (unlikely(ring >= adapter->max_rds_rings))
  1242. return NULL;
  1243. rds_ring = &recv_ctx->rds_rings[ring];
  1244. index = netxen_get_sts_refhandle(sts_data0);
  1245. if (unlikely(index >= rds_ring->num_desc))
  1246. return NULL;
  1247. buffer = &rds_ring->rx_buf_arr[index];
  1248. length = netxen_get_sts_totallength(sts_data0);
  1249. cksum = netxen_get_sts_status(sts_data0);
  1250. pkt_offset = netxen_get_sts_pkt_offset(sts_data0);
  1251. skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
  1252. if (!skb)
  1253. return buffer;
  1254. if (length > rds_ring->skb_size)
  1255. skb_put(skb, rds_ring->skb_size);
  1256. else
  1257. skb_put(skb, length);
  1258. if (pkt_offset)
  1259. skb_pull(skb, pkt_offset);
  1260. skb->protocol = eth_type_trans(skb, netdev);
  1261. napi_gro_receive(&sds_ring->napi, skb);
  1262. adapter->stats.rx_pkts++;
  1263. adapter->stats.rxbytes += length;
  1264. return buffer;
  1265. }
  1266. #define TCP_HDR_SIZE 20
  1267. #define TCP_TS_OPTION_SIZE 12
  1268. #define TCP_TS_HDR_SIZE (TCP_HDR_SIZE + TCP_TS_OPTION_SIZE)
  1269. static struct netxen_rx_buffer *
  1270. netxen_process_lro(struct netxen_adapter *adapter,
  1271. struct nx_host_sds_ring *sds_ring,
  1272. int ring, u64 sts_data0, u64 sts_data1)
  1273. {
  1274. struct net_device *netdev = adapter->netdev;
  1275. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  1276. struct netxen_rx_buffer *buffer;
  1277. struct sk_buff *skb;
  1278. struct nx_host_rds_ring *rds_ring;
  1279. struct iphdr *iph;
  1280. struct tcphdr *th;
  1281. bool push, timestamp;
  1282. int l2_hdr_offset, l4_hdr_offset;
  1283. int index;
  1284. u16 lro_length, length, data_offset;
  1285. u32 seq_number;
  1286. u8 vhdr_len = 0;
  1287. if (unlikely(ring >= adapter->max_rds_rings))
  1288. return NULL;
  1289. rds_ring = &recv_ctx->rds_rings[ring];
  1290. index = netxen_get_lro_sts_refhandle(sts_data0);
  1291. if (unlikely(index >= rds_ring->num_desc))
  1292. return NULL;
  1293. buffer = &rds_ring->rx_buf_arr[index];
  1294. timestamp = netxen_get_lro_sts_timestamp(sts_data0);
  1295. lro_length = netxen_get_lro_sts_length(sts_data0);
  1296. l2_hdr_offset = netxen_get_lro_sts_l2_hdr_offset(sts_data0);
  1297. l4_hdr_offset = netxen_get_lro_sts_l4_hdr_offset(sts_data0);
  1298. push = netxen_get_lro_sts_push_flag(sts_data0);
  1299. seq_number = netxen_get_lro_sts_seq_number(sts_data1);
  1300. skb = netxen_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
  1301. if (!skb)
  1302. return buffer;
  1303. if (timestamp)
  1304. data_offset = l4_hdr_offset + TCP_TS_HDR_SIZE;
  1305. else
  1306. data_offset = l4_hdr_offset + TCP_HDR_SIZE;
  1307. skb_put(skb, lro_length + data_offset);
  1308. skb_pull(skb, l2_hdr_offset);
  1309. skb->protocol = eth_type_trans(skb, netdev);
  1310. if (skb->protocol == htons(ETH_P_8021Q))
  1311. vhdr_len = VLAN_HLEN;
  1312. iph = (struct iphdr *)(skb->data + vhdr_len);
  1313. th = (struct tcphdr *)((skb->data + vhdr_len) + (iph->ihl << 2));
  1314. length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
  1315. csum_replace2(&iph->check, iph->tot_len, htons(length));
  1316. iph->tot_len = htons(length);
  1317. th->psh = push;
  1318. th->seq = htonl(seq_number);
  1319. length = skb->len;
  1320. if (adapter->flags & NETXEN_FW_MSS_CAP)
  1321. skb_shinfo(skb)->gso_size = netxen_get_lro_sts_mss(sts_data1);
  1322. netif_receive_skb(skb);
  1323. adapter->stats.lro_pkts++;
  1324. adapter->stats.rxbytes += length;
  1325. return buffer;
  1326. }
  1327. #define netxen_merge_rx_buffers(list, head) \
  1328. do { list_splice_tail_init(list, head); } while (0);
  1329. int
  1330. netxen_process_rcv_ring(struct nx_host_sds_ring *sds_ring, int max)
  1331. {
  1332. struct netxen_adapter *adapter = sds_ring->adapter;
  1333. struct list_head *cur;
  1334. struct status_desc *desc;
  1335. struct netxen_rx_buffer *rxbuf;
  1336. u32 consumer = sds_ring->consumer;
  1337. int count = 0;
  1338. u64 sts_data0, sts_data1;
  1339. int opcode, ring = 0, desc_cnt;
  1340. while (count < max) {
  1341. desc = &sds_ring->desc_head[consumer];
  1342. sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
  1343. if (!(sts_data0 & STATUS_OWNER_HOST))
  1344. break;
  1345. desc_cnt = netxen_get_sts_desc_cnt(sts_data0);
  1346. opcode = netxen_get_sts_opcode(sts_data0);
  1347. switch (opcode) {
  1348. case NETXEN_NIC_RXPKT_DESC:
  1349. case NETXEN_OLD_RXPKT_DESC:
  1350. case NETXEN_NIC_SYN_OFFLOAD:
  1351. ring = netxen_get_sts_type(sts_data0);
  1352. rxbuf = netxen_process_rcv(adapter, sds_ring,
  1353. ring, sts_data0);
  1354. break;
  1355. case NETXEN_NIC_LRO_DESC:
  1356. ring = netxen_get_lro_sts_type(sts_data0);
  1357. sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
  1358. rxbuf = netxen_process_lro(adapter, sds_ring,
  1359. ring, sts_data0, sts_data1);
  1360. break;
  1361. case NETXEN_NIC_RESPONSE_DESC:
  1362. netxen_handle_fw_message(desc_cnt, consumer, sds_ring);
  1363. default:
  1364. goto skip;
  1365. }
  1366. WARN_ON(desc_cnt > 1);
  1367. if (rxbuf)
  1368. list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
  1369. skip:
  1370. for (; desc_cnt > 0; desc_cnt--) {
  1371. desc = &sds_ring->desc_head[consumer];
  1372. desc->status_desc_data[0] =
  1373. cpu_to_le64(STATUS_OWNER_PHANTOM);
  1374. consumer = get_next_index(consumer, sds_ring->num_desc);
  1375. }
  1376. count++;
  1377. }
  1378. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  1379. struct nx_host_rds_ring *rds_ring =
  1380. &adapter->recv_ctx.rds_rings[ring];
  1381. if (!list_empty(&sds_ring->free_list[ring])) {
  1382. list_for_each(cur, &sds_ring->free_list[ring]) {
  1383. rxbuf = list_entry(cur,
  1384. struct netxen_rx_buffer, list);
  1385. netxen_alloc_rx_skb(adapter, rds_ring, rxbuf);
  1386. }
  1387. spin_lock(&rds_ring->lock);
  1388. netxen_merge_rx_buffers(&sds_ring->free_list[ring],
  1389. &rds_ring->free_list);
  1390. spin_unlock(&rds_ring->lock);
  1391. }
  1392. netxen_post_rx_buffers_nodb(adapter, rds_ring);
  1393. }
  1394. if (count) {
  1395. sds_ring->consumer = consumer;
  1396. NXWRIO(adapter, sds_ring->crb_sts_consumer, consumer);
  1397. }
  1398. return count;
  1399. }
  1400. /* Process Command status ring */
  1401. int netxen_process_cmd_ring(struct netxen_adapter *adapter)
  1402. {
  1403. u32 sw_consumer, hw_consumer;
  1404. int count = 0, i;
  1405. struct netxen_cmd_buffer *buffer;
  1406. struct pci_dev *pdev = adapter->pdev;
  1407. struct net_device *netdev = adapter->netdev;
  1408. struct netxen_skb_frag *frag;
  1409. int done = 0;
  1410. struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
  1411. if (!spin_trylock_bh(&adapter->tx_clean_lock))
  1412. return 1;
  1413. sw_consumer = tx_ring->sw_consumer;
  1414. hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
  1415. while (sw_consumer != hw_consumer) {
  1416. buffer = &tx_ring->cmd_buf_arr[sw_consumer];
  1417. if (buffer->skb) {
  1418. frag = &buffer->frag_array[0];
  1419. pci_unmap_single(pdev, frag->dma, frag->length,
  1420. PCI_DMA_TODEVICE);
  1421. frag->dma = 0ULL;
  1422. for (i = 1; i < buffer->frag_count; i++) {
  1423. frag++; /* Get the next frag */
  1424. pci_unmap_page(pdev, frag->dma, frag->length,
  1425. PCI_DMA_TODEVICE);
  1426. frag->dma = 0ULL;
  1427. }
  1428. adapter->stats.xmitfinished++;
  1429. dev_kfree_skb_any(buffer->skb);
  1430. buffer->skb = NULL;
  1431. }
  1432. sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
  1433. if (++count >= MAX_STATUS_HANDLE)
  1434. break;
  1435. }
  1436. tx_ring->sw_consumer = sw_consumer;
  1437. if (count && netif_running(netdev)) {
  1438. smp_mb();
  1439. if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev))
  1440. if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
  1441. netif_wake_queue(netdev);
  1442. adapter->tx_timeo_cnt = 0;
  1443. }
  1444. /*
  1445. * If everything is freed up to consumer then check if the ring is full
  1446. * If the ring is full then check if more needs to be freed and
  1447. * schedule the call back again.
  1448. *
  1449. * This happens when there are 2 CPUs. One could be freeing and the
  1450. * other filling it. If the ring is full when we get out of here and
  1451. * the card has already interrupted the host then the host can miss the
  1452. * interrupt.
  1453. *
  1454. * There is still a possible race condition and the host could miss an
  1455. * interrupt. The card has to take care of this.
  1456. */
  1457. hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
  1458. done = (sw_consumer == hw_consumer);
  1459. spin_unlock_bh(&adapter->tx_clean_lock);
  1460. return done;
  1461. }
  1462. void
  1463. netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ringid,
  1464. struct nx_host_rds_ring *rds_ring)
  1465. {
  1466. struct rcv_desc *pdesc;
  1467. struct netxen_rx_buffer *buffer;
  1468. int producer, count = 0;
  1469. netxen_ctx_msg msg = 0;
  1470. struct list_head *head;
  1471. producer = rds_ring->producer;
  1472. head = &rds_ring->free_list;
  1473. while (!list_empty(head)) {
  1474. buffer = list_entry(head->next, struct netxen_rx_buffer, list);
  1475. if (!buffer->skb) {
  1476. if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
  1477. break;
  1478. }
  1479. count++;
  1480. list_del(&buffer->list);
  1481. /* make a rcv descriptor */
  1482. pdesc = &rds_ring->desc_head[producer];
  1483. pdesc->addr_buffer = cpu_to_le64(buffer->dma);
  1484. pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
  1485. pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
  1486. producer = get_next_index(producer, rds_ring->num_desc);
  1487. }
  1488. if (count) {
  1489. rds_ring->producer = producer;
  1490. NXWRIO(adapter, rds_ring->crb_rcv_producer,
  1491. (producer-1) & (rds_ring->num_desc-1));
  1492. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  1493. /*
  1494. * Write a doorbell msg to tell phanmon of change in
  1495. * receive ring producer
  1496. * Only for firmware version < 4.0.0
  1497. */
  1498. netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
  1499. netxen_set_msg_privid(msg);
  1500. netxen_set_msg_count(msg,
  1501. ((producer - 1) &
  1502. (rds_ring->num_desc - 1)));
  1503. netxen_set_msg_ctxid(msg, adapter->portnum);
  1504. netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
  1505. NXWRIO(adapter, DB_NORMALIZE(adapter,
  1506. NETXEN_RCV_PRODUCER_OFFSET), msg);
  1507. }
  1508. }
  1509. }
  1510. static void
  1511. netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
  1512. struct nx_host_rds_ring *rds_ring)
  1513. {
  1514. struct rcv_desc *pdesc;
  1515. struct netxen_rx_buffer *buffer;
  1516. int producer, count = 0;
  1517. struct list_head *head;
  1518. if (!spin_trylock(&rds_ring->lock))
  1519. return;
  1520. producer = rds_ring->producer;
  1521. head = &rds_ring->free_list;
  1522. while (!list_empty(head)) {
  1523. buffer = list_entry(head->next, struct netxen_rx_buffer, list);
  1524. if (!buffer->skb) {
  1525. if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
  1526. break;
  1527. }
  1528. count++;
  1529. list_del(&buffer->list);
  1530. /* make a rcv descriptor */
  1531. pdesc = &rds_ring->desc_head[producer];
  1532. pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
  1533. pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
  1534. pdesc->addr_buffer = cpu_to_le64(buffer->dma);
  1535. producer = get_next_index(producer, rds_ring->num_desc);
  1536. }
  1537. if (count) {
  1538. rds_ring->producer = producer;
  1539. NXWRIO(adapter, rds_ring->crb_rcv_producer,
  1540. (producer - 1) & (rds_ring->num_desc - 1));
  1541. }
  1542. spin_unlock(&rds_ring->lock);
  1543. }
  1544. void netxen_nic_clear_stats(struct netxen_adapter *adapter)
  1545. {
  1546. memset(&adapter->stats, 0, sizeof(adapter->stats));
  1547. }