cxgb2.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*****************************************************************************
  2. * *
  3. * File: cxgb2.c *
  4. * $Revision: 1.25 $ *
  5. * $Date: 2005/06/22 00:43:25 $ *
  6. * Description: *
  7. * Chelsio 10Gb Ethernet Driver. *
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License, version 2, as *
  11. * published by the Free Software Foundation. *
  12. * *
  13. * You should have received a copy of the GNU General Public License along *
  14. * with this program; if not, see <http://www.gnu.org/licenses/>. *
  15. * *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED *
  17. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF *
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *
  19. * *
  20. * http://www.chelsio.com *
  21. * *
  22. * Copyright (c) 2003 - 2005 Chelsio Communications, Inc. *
  23. * All rights reserved. *
  24. * *
  25. * Maintainers: maintainers@chelsio.com *
  26. * *
  27. * Authors: Dimitrios Michailidis <dm@chelsio.com> *
  28. * Tina Yang <tainay@chelsio.com> *
  29. * Felix Marti <felix@chelsio.com> *
  30. * Scott Bardone <sbardone@chelsio.com> *
  31. * Kurt Ottaway <kottaway@chelsio.com> *
  32. * Frank DiMambro <frank@chelsio.com> *
  33. * *
  34. * History: *
  35. * *
  36. ****************************************************************************/
  37. #include "common.h"
  38. #include <linux/module.h>
  39. #include <linux/pci.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/if_vlan.h>
  43. #include <linux/mii.h>
  44. #include <linux/sockios.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/uaccess.h>
  47. #include "cpl5_cmd.h"
  48. #include "regs.h"
  49. #include "gmac.h"
  50. #include "cphy.h"
  51. #include "sge.h"
  52. #include "tp.h"
  53. #include "espi.h"
  54. #include "elmer0.h"
  55. #include <linux/workqueue.h>
  56. static inline void schedule_mac_stats_update(struct adapter *ap, int secs)
  57. {
  58. schedule_delayed_work(&ap->stats_update_task, secs * HZ);
  59. }
  60. static inline void cancel_mac_stats_update(struct adapter *ap)
  61. {
  62. cancel_delayed_work(&ap->stats_update_task);
  63. }
  64. #define MAX_CMDQ_ENTRIES 16384
  65. #define MAX_CMDQ1_ENTRIES 1024
  66. #define MAX_RX_BUFFERS 16384
  67. #define MAX_RX_JUMBO_BUFFERS 16384
  68. #define MAX_TX_BUFFERS_HIGH 16384U
  69. #define MAX_TX_BUFFERS_LOW 1536U
  70. #define MAX_TX_BUFFERS 1460U
  71. #define MIN_FL_ENTRIES 32
  72. #define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
  73. NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
  74. NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
  75. /*
  76. * The EEPROM is actually bigger but only the first few bytes are used so we
  77. * only report those.
  78. */
  79. #define EEPROM_SIZE 32
  80. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  81. MODULE_AUTHOR("Chelsio Communications");
  82. MODULE_LICENSE("GPL");
  83. static int dflt_msg_enable = DFLT_MSG_ENABLE;
  84. module_param(dflt_msg_enable, int, 0);
  85. MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T1 default message enable bitmap");
  86. #define HCLOCK 0x0
  87. #define LCLOCK 0x1
  88. /* T1 cards powersave mode */
  89. static int t1_clock(struct adapter *adapter, int mode);
  90. static int t1powersave = 1; /* HW default is powersave mode. */
  91. module_param(t1powersave, int, 0);
  92. MODULE_PARM_DESC(t1powersave, "Enable/Disable T1 powersaving mode");
  93. static int disable_msi = 0;
  94. module_param(disable_msi, int, 0);
  95. MODULE_PARM_DESC(disable_msi, "Disable Message Signaled Interrupt (MSI)");
  96. /*
  97. * Setup MAC to receive the types of packets we want.
  98. */
  99. static void t1_set_rxmode(struct net_device *dev)
  100. {
  101. struct adapter *adapter = dev->ml_priv;
  102. struct cmac *mac = adapter->port[dev->if_port].mac;
  103. struct t1_rx_mode rm;
  104. rm.dev = dev;
  105. mac->ops->set_rx_mode(mac, &rm);
  106. }
  107. static void link_report(struct port_info *p)
  108. {
  109. if (!netif_carrier_ok(p->dev))
  110. netdev_info(p->dev, "link down\n");
  111. else {
  112. const char *s = "10Mbps";
  113. switch (p->link_config.speed) {
  114. case SPEED_10000: s = "10Gbps"; break;
  115. case SPEED_1000: s = "1000Mbps"; break;
  116. case SPEED_100: s = "100Mbps"; break;
  117. }
  118. netdev_info(p->dev, "link up, %s, %s-duplex\n",
  119. s, p->link_config.duplex == DUPLEX_FULL
  120. ? "full" : "half");
  121. }
  122. }
  123. void t1_link_negotiated(struct adapter *adapter, int port_id, int link_stat,
  124. int speed, int duplex, int pause)
  125. {
  126. struct port_info *p = &adapter->port[port_id];
  127. if (link_stat != netif_carrier_ok(p->dev)) {
  128. if (link_stat)
  129. netif_carrier_on(p->dev);
  130. else
  131. netif_carrier_off(p->dev);
  132. link_report(p);
  133. /* multi-ports: inform toe */
  134. if ((speed > 0) && (adapter->params.nports > 1)) {
  135. unsigned int sched_speed = 10;
  136. switch (speed) {
  137. case SPEED_1000:
  138. sched_speed = 1000;
  139. break;
  140. case SPEED_100:
  141. sched_speed = 100;
  142. break;
  143. case SPEED_10:
  144. sched_speed = 10;
  145. break;
  146. }
  147. t1_sched_update_parms(adapter->sge, port_id, 0, sched_speed);
  148. }
  149. }
  150. }
  151. static void link_start(struct port_info *p)
  152. {
  153. struct cmac *mac = p->mac;
  154. mac->ops->reset(mac);
  155. if (mac->ops->macaddress_set)
  156. mac->ops->macaddress_set(mac, p->dev->dev_addr);
  157. t1_set_rxmode(p->dev);
  158. t1_link_start(p->phy, mac, &p->link_config);
  159. mac->ops->enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
  160. }
  161. static void enable_hw_csum(struct adapter *adapter)
  162. {
  163. if (adapter->port[0].dev->hw_features & NETIF_F_TSO)
  164. t1_tp_set_ip_checksum_offload(adapter->tp, 1); /* for TSO only */
  165. t1_tp_set_tcp_checksum_offload(adapter->tp, 1);
  166. }
  167. /*
  168. * Things to do upon first use of a card.
  169. * This must run with the rtnl lock held.
  170. */
  171. static int cxgb_up(struct adapter *adapter)
  172. {
  173. int err = 0;
  174. if (!(adapter->flags & FULL_INIT_DONE)) {
  175. err = t1_init_hw_modules(adapter);
  176. if (err)
  177. goto out_err;
  178. enable_hw_csum(adapter);
  179. adapter->flags |= FULL_INIT_DONE;
  180. }
  181. t1_interrupts_clear(adapter);
  182. adapter->params.has_msi = !disable_msi && !pci_enable_msi(adapter->pdev);
  183. err = request_irq(adapter->pdev->irq, t1_interrupt,
  184. adapter->params.has_msi ? 0 : IRQF_SHARED,
  185. adapter->name, adapter);
  186. if (err) {
  187. if (adapter->params.has_msi)
  188. pci_disable_msi(adapter->pdev);
  189. goto out_err;
  190. }
  191. t1_sge_start(adapter->sge);
  192. t1_interrupts_enable(adapter);
  193. out_err:
  194. return err;
  195. }
  196. /*
  197. * Release resources when all the ports have been stopped.
  198. */
  199. static void cxgb_down(struct adapter *adapter)
  200. {
  201. t1_sge_stop(adapter->sge);
  202. t1_interrupts_disable(adapter);
  203. free_irq(adapter->pdev->irq, adapter);
  204. if (adapter->params.has_msi)
  205. pci_disable_msi(adapter->pdev);
  206. }
  207. static int cxgb_open(struct net_device *dev)
  208. {
  209. int err;
  210. struct adapter *adapter = dev->ml_priv;
  211. int other_ports = adapter->open_device_map & PORT_MASK;
  212. napi_enable(&adapter->napi);
  213. if (!adapter->open_device_map && (err = cxgb_up(adapter)) < 0) {
  214. napi_disable(&adapter->napi);
  215. return err;
  216. }
  217. __set_bit(dev->if_port, &adapter->open_device_map);
  218. link_start(&adapter->port[dev->if_port]);
  219. netif_start_queue(dev);
  220. if (!other_ports && adapter->params.stats_update_period)
  221. schedule_mac_stats_update(adapter,
  222. adapter->params.stats_update_period);
  223. t1_vlan_mode(adapter, dev->features);
  224. return 0;
  225. }
  226. static int cxgb_close(struct net_device *dev)
  227. {
  228. struct adapter *adapter = dev->ml_priv;
  229. struct port_info *p = &adapter->port[dev->if_port];
  230. struct cmac *mac = p->mac;
  231. netif_stop_queue(dev);
  232. napi_disable(&adapter->napi);
  233. mac->ops->disable(mac, MAC_DIRECTION_TX | MAC_DIRECTION_RX);
  234. netif_carrier_off(dev);
  235. clear_bit(dev->if_port, &adapter->open_device_map);
  236. if (adapter->params.stats_update_period &&
  237. !(adapter->open_device_map & PORT_MASK)) {
  238. /* Stop statistics accumulation. */
  239. smp_mb__after_atomic();
  240. spin_lock(&adapter->work_lock); /* sync with update task */
  241. spin_unlock(&adapter->work_lock);
  242. cancel_mac_stats_update(adapter);
  243. }
  244. if (!adapter->open_device_map)
  245. cxgb_down(adapter);
  246. return 0;
  247. }
  248. static struct net_device_stats *t1_get_stats(struct net_device *dev)
  249. {
  250. struct adapter *adapter = dev->ml_priv;
  251. struct port_info *p = &adapter->port[dev->if_port];
  252. struct net_device_stats *ns = &dev->stats;
  253. const struct cmac_statistics *pstats;
  254. /* Do a full update of the MAC stats */
  255. pstats = p->mac->ops->statistics_update(p->mac,
  256. MAC_STATS_UPDATE_FULL);
  257. ns->tx_packets = pstats->TxUnicastFramesOK +
  258. pstats->TxMulticastFramesOK + pstats->TxBroadcastFramesOK;
  259. ns->rx_packets = pstats->RxUnicastFramesOK +
  260. pstats->RxMulticastFramesOK + pstats->RxBroadcastFramesOK;
  261. ns->tx_bytes = pstats->TxOctetsOK;
  262. ns->rx_bytes = pstats->RxOctetsOK;
  263. ns->tx_errors = pstats->TxLateCollisions + pstats->TxLengthErrors +
  264. pstats->TxUnderrun + pstats->TxFramesAbortedDueToXSCollisions;
  265. ns->rx_errors = pstats->RxDataErrors + pstats->RxJabberErrors +
  266. pstats->RxFCSErrors + pstats->RxAlignErrors +
  267. pstats->RxSequenceErrors + pstats->RxFrameTooLongErrors +
  268. pstats->RxSymbolErrors + pstats->RxRuntErrors;
  269. ns->multicast = pstats->RxMulticastFramesOK;
  270. ns->collisions = pstats->TxTotalCollisions;
  271. /* detailed rx_errors */
  272. ns->rx_length_errors = pstats->RxFrameTooLongErrors +
  273. pstats->RxJabberErrors;
  274. ns->rx_over_errors = 0;
  275. ns->rx_crc_errors = pstats->RxFCSErrors;
  276. ns->rx_frame_errors = pstats->RxAlignErrors;
  277. ns->rx_fifo_errors = 0;
  278. ns->rx_missed_errors = 0;
  279. /* detailed tx_errors */
  280. ns->tx_aborted_errors = pstats->TxFramesAbortedDueToXSCollisions;
  281. ns->tx_carrier_errors = 0;
  282. ns->tx_fifo_errors = pstats->TxUnderrun;
  283. ns->tx_heartbeat_errors = 0;
  284. ns->tx_window_errors = pstats->TxLateCollisions;
  285. return ns;
  286. }
  287. static u32 get_msglevel(struct net_device *dev)
  288. {
  289. struct adapter *adapter = dev->ml_priv;
  290. return adapter->msg_enable;
  291. }
  292. static void set_msglevel(struct net_device *dev, u32 val)
  293. {
  294. struct adapter *adapter = dev->ml_priv;
  295. adapter->msg_enable = val;
  296. }
  297. static const char stats_strings[][ETH_GSTRING_LEN] = {
  298. "TxOctetsOK",
  299. "TxOctetsBad",
  300. "TxUnicastFramesOK",
  301. "TxMulticastFramesOK",
  302. "TxBroadcastFramesOK",
  303. "TxPauseFrames",
  304. "TxFramesWithDeferredXmissions",
  305. "TxLateCollisions",
  306. "TxTotalCollisions",
  307. "TxFramesAbortedDueToXSCollisions",
  308. "TxUnderrun",
  309. "TxLengthErrors",
  310. "TxInternalMACXmitError",
  311. "TxFramesWithExcessiveDeferral",
  312. "TxFCSErrors",
  313. "TxJumboFramesOk",
  314. "TxJumboOctetsOk",
  315. "RxOctetsOK",
  316. "RxOctetsBad",
  317. "RxUnicastFramesOK",
  318. "RxMulticastFramesOK",
  319. "RxBroadcastFramesOK",
  320. "RxPauseFrames",
  321. "RxFCSErrors",
  322. "RxAlignErrors",
  323. "RxSymbolErrors",
  324. "RxDataErrors",
  325. "RxSequenceErrors",
  326. "RxRuntErrors",
  327. "RxJabberErrors",
  328. "RxInternalMACRcvError",
  329. "RxInRangeLengthErrors",
  330. "RxOutOfRangeLengthField",
  331. "RxFrameTooLongErrors",
  332. "RxJumboFramesOk",
  333. "RxJumboOctetsOk",
  334. /* Port stats */
  335. "RxCsumGood",
  336. "TxCsumOffload",
  337. "TxTso",
  338. "RxVlan",
  339. "TxVlan",
  340. "TxNeedHeadroom",
  341. /* Interrupt stats */
  342. "rx drops",
  343. "pure_rsps",
  344. "unhandled irqs",
  345. "respQ_empty",
  346. "respQ_overflow",
  347. "freelistQ_empty",
  348. "pkt_too_big",
  349. "pkt_mismatch",
  350. "cmdQ_full0",
  351. "cmdQ_full1",
  352. "espi_DIP2ParityErr",
  353. "espi_DIP4Err",
  354. "espi_RxDrops",
  355. "espi_TxDrops",
  356. "espi_RxOvfl",
  357. "espi_ParityErr"
  358. };
  359. #define T2_REGMAP_SIZE (3 * 1024)
  360. static int get_regs_len(struct net_device *dev)
  361. {
  362. return T2_REGMAP_SIZE;
  363. }
  364. static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  365. {
  366. struct adapter *adapter = dev->ml_priv;
  367. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  368. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  369. strlcpy(info->bus_info, pci_name(adapter->pdev),
  370. sizeof(info->bus_info));
  371. }
  372. static int get_sset_count(struct net_device *dev, int sset)
  373. {
  374. switch (sset) {
  375. case ETH_SS_STATS:
  376. return ARRAY_SIZE(stats_strings);
  377. default:
  378. return -EOPNOTSUPP;
  379. }
  380. }
  381. static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
  382. {
  383. if (stringset == ETH_SS_STATS)
  384. memcpy(data, stats_strings, sizeof(stats_strings));
  385. }
  386. static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
  387. u64 *data)
  388. {
  389. struct adapter *adapter = dev->ml_priv;
  390. struct cmac *mac = adapter->port[dev->if_port].mac;
  391. const struct cmac_statistics *s;
  392. const struct sge_intr_counts *t;
  393. struct sge_port_stats ss;
  394. s = mac->ops->statistics_update(mac, MAC_STATS_UPDATE_FULL);
  395. t = t1_sge_get_intr_counts(adapter->sge);
  396. t1_sge_get_port_stats(adapter->sge, dev->if_port, &ss);
  397. *data++ = s->TxOctetsOK;
  398. *data++ = s->TxOctetsBad;
  399. *data++ = s->TxUnicastFramesOK;
  400. *data++ = s->TxMulticastFramesOK;
  401. *data++ = s->TxBroadcastFramesOK;
  402. *data++ = s->TxPauseFrames;
  403. *data++ = s->TxFramesWithDeferredXmissions;
  404. *data++ = s->TxLateCollisions;
  405. *data++ = s->TxTotalCollisions;
  406. *data++ = s->TxFramesAbortedDueToXSCollisions;
  407. *data++ = s->TxUnderrun;
  408. *data++ = s->TxLengthErrors;
  409. *data++ = s->TxInternalMACXmitError;
  410. *data++ = s->TxFramesWithExcessiveDeferral;
  411. *data++ = s->TxFCSErrors;
  412. *data++ = s->TxJumboFramesOK;
  413. *data++ = s->TxJumboOctetsOK;
  414. *data++ = s->RxOctetsOK;
  415. *data++ = s->RxOctetsBad;
  416. *data++ = s->RxUnicastFramesOK;
  417. *data++ = s->RxMulticastFramesOK;
  418. *data++ = s->RxBroadcastFramesOK;
  419. *data++ = s->RxPauseFrames;
  420. *data++ = s->RxFCSErrors;
  421. *data++ = s->RxAlignErrors;
  422. *data++ = s->RxSymbolErrors;
  423. *data++ = s->RxDataErrors;
  424. *data++ = s->RxSequenceErrors;
  425. *data++ = s->RxRuntErrors;
  426. *data++ = s->RxJabberErrors;
  427. *data++ = s->RxInternalMACRcvError;
  428. *data++ = s->RxInRangeLengthErrors;
  429. *data++ = s->RxOutOfRangeLengthField;
  430. *data++ = s->RxFrameTooLongErrors;
  431. *data++ = s->RxJumboFramesOK;
  432. *data++ = s->RxJumboOctetsOK;
  433. *data++ = ss.rx_cso_good;
  434. *data++ = ss.tx_cso;
  435. *data++ = ss.tx_tso;
  436. *data++ = ss.vlan_xtract;
  437. *data++ = ss.vlan_insert;
  438. *data++ = ss.tx_need_hdrroom;
  439. *data++ = t->rx_drops;
  440. *data++ = t->pure_rsps;
  441. *data++ = t->unhandled_irqs;
  442. *data++ = t->respQ_empty;
  443. *data++ = t->respQ_overflow;
  444. *data++ = t->freelistQ_empty;
  445. *data++ = t->pkt_too_big;
  446. *data++ = t->pkt_mismatch;
  447. *data++ = t->cmdQ_full[0];
  448. *data++ = t->cmdQ_full[1];
  449. if (adapter->espi) {
  450. const struct espi_intr_counts *e;
  451. e = t1_espi_get_intr_counts(adapter->espi);
  452. *data++ = e->DIP2_parity_err;
  453. *data++ = e->DIP4_err;
  454. *data++ = e->rx_drops;
  455. *data++ = e->tx_drops;
  456. *data++ = e->rx_ovflw;
  457. *data++ = e->parity_err;
  458. }
  459. }
  460. static inline void reg_block_dump(struct adapter *ap, void *buf,
  461. unsigned int start, unsigned int end)
  462. {
  463. u32 *p = buf + start;
  464. for ( ; start <= end; start += sizeof(u32))
  465. *p++ = readl(ap->regs + start);
  466. }
  467. static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
  468. void *buf)
  469. {
  470. struct adapter *ap = dev->ml_priv;
  471. /*
  472. * Version scheme: bits 0..9: chip version, bits 10..15: chip revision
  473. */
  474. regs->version = 2;
  475. memset(buf, 0, T2_REGMAP_SIZE);
  476. reg_block_dump(ap, buf, 0, A_SG_RESPACCUTIMER);
  477. reg_block_dump(ap, buf, A_MC3_CFG, A_MC4_INT_CAUSE);
  478. reg_block_dump(ap, buf, A_TPI_ADDR, A_TPI_PAR);
  479. reg_block_dump(ap, buf, A_TP_IN_CONFIG, A_TP_TX_DROP_COUNT);
  480. reg_block_dump(ap, buf, A_RAT_ROUTE_CONTROL, A_RAT_INTR_CAUSE);
  481. reg_block_dump(ap, buf, A_CSPI_RX_AE_WM, A_CSPI_INTR_ENABLE);
  482. reg_block_dump(ap, buf, A_ESPI_SCH_TOKEN0, A_ESPI_GOSTAT);
  483. reg_block_dump(ap, buf, A_ULP_ULIMIT, A_ULP_PIO_CTRL);
  484. reg_block_dump(ap, buf, A_PL_ENABLE, A_PL_CAUSE);
  485. reg_block_dump(ap, buf, A_MC5_CONFIG, A_MC5_MASK_WRITE_CMD);
  486. }
  487. static int get_link_ksettings(struct net_device *dev,
  488. struct ethtool_link_ksettings *cmd)
  489. {
  490. struct adapter *adapter = dev->ml_priv;
  491. struct port_info *p = &adapter->port[dev->if_port];
  492. u32 supported, advertising;
  493. supported = p->link_config.supported;
  494. advertising = p->link_config.advertising;
  495. if (netif_carrier_ok(dev)) {
  496. cmd->base.speed = p->link_config.speed;
  497. cmd->base.duplex = p->link_config.duplex;
  498. } else {
  499. cmd->base.speed = SPEED_UNKNOWN;
  500. cmd->base.duplex = DUPLEX_UNKNOWN;
  501. }
  502. cmd->base.port = (supported & SUPPORTED_TP) ? PORT_TP : PORT_FIBRE;
  503. cmd->base.phy_address = p->phy->mdio.prtad;
  504. cmd->base.autoneg = p->link_config.autoneg;
  505. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  506. supported);
  507. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  508. advertising);
  509. return 0;
  510. }
  511. static int speed_duplex_to_caps(int speed, int duplex)
  512. {
  513. int cap = 0;
  514. switch (speed) {
  515. case SPEED_10:
  516. if (duplex == DUPLEX_FULL)
  517. cap = SUPPORTED_10baseT_Full;
  518. else
  519. cap = SUPPORTED_10baseT_Half;
  520. break;
  521. case SPEED_100:
  522. if (duplex == DUPLEX_FULL)
  523. cap = SUPPORTED_100baseT_Full;
  524. else
  525. cap = SUPPORTED_100baseT_Half;
  526. break;
  527. case SPEED_1000:
  528. if (duplex == DUPLEX_FULL)
  529. cap = SUPPORTED_1000baseT_Full;
  530. else
  531. cap = SUPPORTED_1000baseT_Half;
  532. break;
  533. case SPEED_10000:
  534. if (duplex == DUPLEX_FULL)
  535. cap = SUPPORTED_10000baseT_Full;
  536. }
  537. return cap;
  538. }
  539. #define ADVERTISED_MASK (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | \
  540. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | \
  541. ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full | \
  542. ADVERTISED_10000baseT_Full)
  543. static int set_link_ksettings(struct net_device *dev,
  544. const struct ethtool_link_ksettings *cmd)
  545. {
  546. struct adapter *adapter = dev->ml_priv;
  547. struct port_info *p = &adapter->port[dev->if_port];
  548. struct link_config *lc = &p->link_config;
  549. u32 advertising;
  550. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  551. cmd->link_modes.advertising);
  552. if (!(lc->supported & SUPPORTED_Autoneg))
  553. return -EOPNOTSUPP; /* can't change speed/duplex */
  554. if (cmd->base.autoneg == AUTONEG_DISABLE) {
  555. u32 speed = cmd->base.speed;
  556. int cap = speed_duplex_to_caps(speed, cmd->base.duplex);
  557. if (!(lc->supported & cap) || (speed == SPEED_1000))
  558. return -EINVAL;
  559. lc->requested_speed = speed;
  560. lc->requested_duplex = cmd->base.duplex;
  561. lc->advertising = 0;
  562. } else {
  563. advertising &= ADVERTISED_MASK;
  564. if (advertising & (advertising - 1))
  565. advertising = lc->supported;
  566. advertising &= lc->supported;
  567. if (!advertising)
  568. return -EINVAL;
  569. lc->requested_speed = SPEED_INVALID;
  570. lc->requested_duplex = DUPLEX_INVALID;
  571. lc->advertising = advertising | ADVERTISED_Autoneg;
  572. }
  573. lc->autoneg = cmd->base.autoneg;
  574. if (netif_running(dev))
  575. t1_link_start(p->phy, p->mac, lc);
  576. return 0;
  577. }
  578. static void get_pauseparam(struct net_device *dev,
  579. struct ethtool_pauseparam *epause)
  580. {
  581. struct adapter *adapter = dev->ml_priv;
  582. struct port_info *p = &adapter->port[dev->if_port];
  583. epause->autoneg = (p->link_config.requested_fc & PAUSE_AUTONEG) != 0;
  584. epause->rx_pause = (p->link_config.fc & PAUSE_RX) != 0;
  585. epause->tx_pause = (p->link_config.fc & PAUSE_TX) != 0;
  586. }
  587. static int set_pauseparam(struct net_device *dev,
  588. struct ethtool_pauseparam *epause)
  589. {
  590. struct adapter *adapter = dev->ml_priv;
  591. struct port_info *p = &adapter->port[dev->if_port];
  592. struct link_config *lc = &p->link_config;
  593. if (epause->autoneg == AUTONEG_DISABLE)
  594. lc->requested_fc = 0;
  595. else if (lc->supported & SUPPORTED_Autoneg)
  596. lc->requested_fc = PAUSE_AUTONEG;
  597. else
  598. return -EINVAL;
  599. if (epause->rx_pause)
  600. lc->requested_fc |= PAUSE_RX;
  601. if (epause->tx_pause)
  602. lc->requested_fc |= PAUSE_TX;
  603. if (lc->autoneg == AUTONEG_ENABLE) {
  604. if (netif_running(dev))
  605. t1_link_start(p->phy, p->mac, lc);
  606. } else {
  607. lc->fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  608. if (netif_running(dev))
  609. p->mac->ops->set_speed_duplex_fc(p->mac, -1, -1,
  610. lc->fc);
  611. }
  612. return 0;
  613. }
  614. static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
  615. {
  616. struct adapter *adapter = dev->ml_priv;
  617. int jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
  618. e->rx_max_pending = MAX_RX_BUFFERS;
  619. e->rx_jumbo_max_pending = MAX_RX_JUMBO_BUFFERS;
  620. e->tx_max_pending = MAX_CMDQ_ENTRIES;
  621. e->rx_pending = adapter->params.sge.freelQ_size[!jumbo_fl];
  622. e->rx_jumbo_pending = adapter->params.sge.freelQ_size[jumbo_fl];
  623. e->tx_pending = adapter->params.sge.cmdQ_size[0];
  624. }
  625. static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
  626. {
  627. struct adapter *adapter = dev->ml_priv;
  628. int jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
  629. if (e->rx_pending > MAX_RX_BUFFERS || e->rx_mini_pending ||
  630. e->rx_jumbo_pending > MAX_RX_JUMBO_BUFFERS ||
  631. e->tx_pending > MAX_CMDQ_ENTRIES ||
  632. e->rx_pending < MIN_FL_ENTRIES ||
  633. e->rx_jumbo_pending < MIN_FL_ENTRIES ||
  634. e->tx_pending < (adapter->params.nports + 1) * (MAX_SKB_FRAGS + 1))
  635. return -EINVAL;
  636. if (adapter->flags & FULL_INIT_DONE)
  637. return -EBUSY;
  638. adapter->params.sge.freelQ_size[!jumbo_fl] = e->rx_pending;
  639. adapter->params.sge.freelQ_size[jumbo_fl] = e->rx_jumbo_pending;
  640. adapter->params.sge.cmdQ_size[0] = e->tx_pending;
  641. adapter->params.sge.cmdQ_size[1] = e->tx_pending > MAX_CMDQ1_ENTRIES ?
  642. MAX_CMDQ1_ENTRIES : e->tx_pending;
  643. return 0;
  644. }
  645. static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
  646. {
  647. struct adapter *adapter = dev->ml_priv;
  648. adapter->params.sge.rx_coalesce_usecs = c->rx_coalesce_usecs;
  649. adapter->params.sge.coalesce_enable = c->use_adaptive_rx_coalesce;
  650. adapter->params.sge.sample_interval_usecs = c->rate_sample_interval;
  651. t1_sge_set_coalesce_params(adapter->sge, &adapter->params.sge);
  652. return 0;
  653. }
  654. static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
  655. {
  656. struct adapter *adapter = dev->ml_priv;
  657. c->rx_coalesce_usecs = adapter->params.sge.rx_coalesce_usecs;
  658. c->rate_sample_interval = adapter->params.sge.sample_interval_usecs;
  659. c->use_adaptive_rx_coalesce = adapter->params.sge.coalesce_enable;
  660. return 0;
  661. }
  662. static int get_eeprom_len(struct net_device *dev)
  663. {
  664. struct adapter *adapter = dev->ml_priv;
  665. return t1_is_asic(adapter) ? EEPROM_SIZE : 0;
  666. }
  667. #define EEPROM_MAGIC(ap) \
  668. (PCI_VENDOR_ID_CHELSIO | ((ap)->params.chip_version << 16))
  669. static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
  670. u8 *data)
  671. {
  672. int i;
  673. u8 buf[EEPROM_SIZE] __attribute__((aligned(4)));
  674. struct adapter *adapter = dev->ml_priv;
  675. e->magic = EEPROM_MAGIC(adapter);
  676. for (i = e->offset & ~3; i < e->offset + e->len; i += sizeof(u32))
  677. t1_seeprom_read(adapter, i, (__le32 *)&buf[i]);
  678. memcpy(data, buf + e->offset, e->len);
  679. return 0;
  680. }
  681. static const struct ethtool_ops t1_ethtool_ops = {
  682. .get_drvinfo = get_drvinfo,
  683. .get_msglevel = get_msglevel,
  684. .set_msglevel = set_msglevel,
  685. .get_ringparam = get_sge_param,
  686. .set_ringparam = set_sge_param,
  687. .get_coalesce = get_coalesce,
  688. .set_coalesce = set_coalesce,
  689. .get_eeprom_len = get_eeprom_len,
  690. .get_eeprom = get_eeprom,
  691. .get_pauseparam = get_pauseparam,
  692. .set_pauseparam = set_pauseparam,
  693. .get_link = ethtool_op_get_link,
  694. .get_strings = get_strings,
  695. .get_sset_count = get_sset_count,
  696. .get_ethtool_stats = get_stats,
  697. .get_regs_len = get_regs_len,
  698. .get_regs = get_regs,
  699. .get_link_ksettings = get_link_ksettings,
  700. .set_link_ksettings = set_link_ksettings,
  701. };
  702. static int t1_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  703. {
  704. struct adapter *adapter = dev->ml_priv;
  705. struct mdio_if_info *mdio = &adapter->port[dev->if_port].phy->mdio;
  706. return mdio_mii_ioctl(mdio, if_mii(req), cmd);
  707. }
  708. static int t1_change_mtu(struct net_device *dev, int new_mtu)
  709. {
  710. int ret;
  711. struct adapter *adapter = dev->ml_priv;
  712. struct cmac *mac = adapter->port[dev->if_port].mac;
  713. if (!mac->ops->set_mtu)
  714. return -EOPNOTSUPP;
  715. if ((ret = mac->ops->set_mtu(mac, new_mtu)))
  716. return ret;
  717. dev->mtu = new_mtu;
  718. return 0;
  719. }
  720. static int t1_set_mac_addr(struct net_device *dev, void *p)
  721. {
  722. struct adapter *adapter = dev->ml_priv;
  723. struct cmac *mac = adapter->port[dev->if_port].mac;
  724. struct sockaddr *addr = p;
  725. if (!mac->ops->macaddress_set)
  726. return -EOPNOTSUPP;
  727. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  728. mac->ops->macaddress_set(mac, dev->dev_addr);
  729. return 0;
  730. }
  731. static netdev_features_t t1_fix_features(struct net_device *dev,
  732. netdev_features_t features)
  733. {
  734. /*
  735. * Since there is no support for separate rx/tx vlan accel
  736. * enable/disable make sure tx flag is always in same state as rx.
  737. */
  738. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  739. features |= NETIF_F_HW_VLAN_CTAG_TX;
  740. else
  741. features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  742. return features;
  743. }
  744. static int t1_set_features(struct net_device *dev, netdev_features_t features)
  745. {
  746. netdev_features_t changed = dev->features ^ features;
  747. struct adapter *adapter = dev->ml_priv;
  748. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  749. t1_vlan_mode(adapter, features);
  750. return 0;
  751. }
  752. #ifdef CONFIG_NET_POLL_CONTROLLER
  753. static void t1_netpoll(struct net_device *dev)
  754. {
  755. unsigned long flags;
  756. struct adapter *adapter = dev->ml_priv;
  757. local_irq_save(flags);
  758. t1_interrupt(adapter->pdev->irq, adapter);
  759. local_irq_restore(flags);
  760. }
  761. #endif
  762. /*
  763. * Periodic accumulation of MAC statistics. This is used only if the MAC
  764. * does not have any other way to prevent stats counter overflow.
  765. */
  766. static void mac_stats_task(struct work_struct *work)
  767. {
  768. int i;
  769. struct adapter *adapter =
  770. container_of(work, struct adapter, stats_update_task.work);
  771. for_each_port(adapter, i) {
  772. struct port_info *p = &adapter->port[i];
  773. if (netif_running(p->dev))
  774. p->mac->ops->statistics_update(p->mac,
  775. MAC_STATS_UPDATE_FAST);
  776. }
  777. /* Schedule the next statistics update if any port is active. */
  778. spin_lock(&adapter->work_lock);
  779. if (adapter->open_device_map & PORT_MASK)
  780. schedule_mac_stats_update(adapter,
  781. adapter->params.stats_update_period);
  782. spin_unlock(&adapter->work_lock);
  783. }
  784. /*
  785. * Processes elmer0 external interrupts in process context.
  786. */
  787. static void ext_intr_task(struct work_struct *work)
  788. {
  789. struct adapter *adapter =
  790. container_of(work, struct adapter, ext_intr_handler_task);
  791. t1_elmer0_ext_intr_handler(adapter);
  792. /* Now reenable external interrupts */
  793. spin_lock_irq(&adapter->async_lock);
  794. adapter->slow_intr_mask |= F_PL_INTR_EXT;
  795. writel(F_PL_INTR_EXT, adapter->regs + A_PL_CAUSE);
  796. writel(adapter->slow_intr_mask | F_PL_INTR_SGE_DATA,
  797. adapter->regs + A_PL_ENABLE);
  798. spin_unlock_irq(&adapter->async_lock);
  799. }
  800. /*
  801. * Interrupt-context handler for elmer0 external interrupts.
  802. */
  803. void t1_elmer0_ext_intr(struct adapter *adapter)
  804. {
  805. /*
  806. * Schedule a task to handle external interrupts as we require
  807. * a process context. We disable EXT interrupts in the interim
  808. * and let the task reenable them when it's done.
  809. */
  810. adapter->slow_intr_mask &= ~F_PL_INTR_EXT;
  811. writel(adapter->slow_intr_mask | F_PL_INTR_SGE_DATA,
  812. adapter->regs + A_PL_ENABLE);
  813. schedule_work(&adapter->ext_intr_handler_task);
  814. }
  815. void t1_fatal_err(struct adapter *adapter)
  816. {
  817. if (adapter->flags & FULL_INIT_DONE) {
  818. t1_sge_stop(adapter->sge);
  819. t1_interrupts_disable(adapter);
  820. }
  821. pr_alert("%s: encountered fatal error, operation suspended\n",
  822. adapter->name);
  823. }
  824. static const struct net_device_ops cxgb_netdev_ops = {
  825. .ndo_open = cxgb_open,
  826. .ndo_stop = cxgb_close,
  827. .ndo_start_xmit = t1_start_xmit,
  828. .ndo_get_stats = t1_get_stats,
  829. .ndo_validate_addr = eth_validate_addr,
  830. .ndo_set_rx_mode = t1_set_rxmode,
  831. .ndo_do_ioctl = t1_ioctl,
  832. .ndo_change_mtu = t1_change_mtu,
  833. .ndo_set_mac_address = t1_set_mac_addr,
  834. .ndo_fix_features = t1_fix_features,
  835. .ndo_set_features = t1_set_features,
  836. #ifdef CONFIG_NET_POLL_CONTROLLER
  837. .ndo_poll_controller = t1_netpoll,
  838. #endif
  839. };
  840. static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  841. {
  842. int i, err, pci_using_dac = 0;
  843. unsigned long mmio_start, mmio_len;
  844. const struct board_info *bi;
  845. struct adapter *adapter = NULL;
  846. struct port_info *pi;
  847. pr_info_once("%s - version %s\n", DRV_DESCRIPTION, DRV_VERSION);
  848. err = pci_enable_device(pdev);
  849. if (err)
  850. return err;
  851. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  852. pr_err("%s: cannot find PCI device memory base address\n",
  853. pci_name(pdev));
  854. err = -ENODEV;
  855. goto out_disable_pdev;
  856. }
  857. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  858. pci_using_dac = 1;
  859. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  860. pr_err("%s: unable to obtain 64-bit DMA for "
  861. "consistent allocations\n", pci_name(pdev));
  862. err = -ENODEV;
  863. goto out_disable_pdev;
  864. }
  865. } else if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) != 0) {
  866. pr_err("%s: no usable DMA configuration\n", pci_name(pdev));
  867. goto out_disable_pdev;
  868. }
  869. err = pci_request_regions(pdev, DRV_NAME);
  870. if (err) {
  871. pr_err("%s: cannot obtain PCI resources\n", pci_name(pdev));
  872. goto out_disable_pdev;
  873. }
  874. pci_set_master(pdev);
  875. mmio_start = pci_resource_start(pdev, 0);
  876. mmio_len = pci_resource_len(pdev, 0);
  877. bi = t1_get_board_info(ent->driver_data);
  878. for (i = 0; i < bi->port_number; ++i) {
  879. struct net_device *netdev;
  880. netdev = alloc_etherdev(adapter ? 0 : sizeof(*adapter));
  881. if (!netdev) {
  882. err = -ENOMEM;
  883. goto out_free_dev;
  884. }
  885. SET_NETDEV_DEV(netdev, &pdev->dev);
  886. if (!adapter) {
  887. adapter = netdev_priv(netdev);
  888. adapter->pdev = pdev;
  889. adapter->port[0].dev = netdev; /* so we don't leak it */
  890. adapter->regs = ioremap(mmio_start, mmio_len);
  891. if (!adapter->regs) {
  892. pr_err("%s: cannot map device registers\n",
  893. pci_name(pdev));
  894. err = -ENOMEM;
  895. goto out_free_dev;
  896. }
  897. if (t1_get_board_rev(adapter, bi, &adapter->params)) {
  898. err = -ENODEV; /* Can't handle this chip rev */
  899. goto out_free_dev;
  900. }
  901. adapter->name = pci_name(pdev);
  902. adapter->msg_enable = dflt_msg_enable;
  903. adapter->mmio_len = mmio_len;
  904. spin_lock_init(&adapter->tpi_lock);
  905. spin_lock_init(&adapter->work_lock);
  906. spin_lock_init(&adapter->async_lock);
  907. spin_lock_init(&adapter->mac_lock);
  908. INIT_WORK(&adapter->ext_intr_handler_task,
  909. ext_intr_task);
  910. INIT_DELAYED_WORK(&adapter->stats_update_task,
  911. mac_stats_task);
  912. pci_set_drvdata(pdev, netdev);
  913. }
  914. pi = &adapter->port[i];
  915. pi->dev = netdev;
  916. netif_carrier_off(netdev);
  917. netdev->irq = pdev->irq;
  918. netdev->if_port = i;
  919. netdev->mem_start = mmio_start;
  920. netdev->mem_end = mmio_start + mmio_len - 1;
  921. netdev->ml_priv = adapter;
  922. netdev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
  923. NETIF_F_RXCSUM;
  924. netdev->features |= NETIF_F_SG | NETIF_F_IP_CSUM |
  925. NETIF_F_RXCSUM | NETIF_F_LLTX;
  926. if (pci_using_dac)
  927. netdev->features |= NETIF_F_HIGHDMA;
  928. if (vlan_tso_capable(adapter)) {
  929. netdev->features |=
  930. NETIF_F_HW_VLAN_CTAG_TX |
  931. NETIF_F_HW_VLAN_CTAG_RX;
  932. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
  933. /* T204: disable TSO */
  934. if (!(is_T2(adapter)) || bi->port_number != 4) {
  935. netdev->hw_features |= NETIF_F_TSO;
  936. netdev->features |= NETIF_F_TSO;
  937. }
  938. }
  939. netdev->netdev_ops = &cxgb_netdev_ops;
  940. netdev->hard_header_len += (netdev->hw_features & NETIF_F_TSO) ?
  941. sizeof(struct cpl_tx_pkt_lso) : sizeof(struct cpl_tx_pkt);
  942. netif_napi_add(netdev, &adapter->napi, t1_poll, 64);
  943. netdev->ethtool_ops = &t1_ethtool_ops;
  944. switch (bi->board) {
  945. case CHBT_BOARD_CHT110:
  946. case CHBT_BOARD_N110:
  947. case CHBT_BOARD_N210:
  948. case CHBT_BOARD_CHT210:
  949. netdev->max_mtu = PM3393_MAX_FRAME_SIZE -
  950. (ETH_HLEN + ETH_FCS_LEN);
  951. break;
  952. case CHBT_BOARD_CHN204:
  953. netdev->max_mtu = VSC7326_MAX_MTU;
  954. break;
  955. default:
  956. netdev->max_mtu = ETH_DATA_LEN;
  957. break;
  958. }
  959. }
  960. if (t1_init_sw_modules(adapter, bi) < 0) {
  961. err = -ENODEV;
  962. goto out_free_dev;
  963. }
  964. /*
  965. * The card is now ready to go. If any errors occur during device
  966. * registration we do not fail the whole card but rather proceed only
  967. * with the ports we manage to register successfully. However we must
  968. * register at least one net device.
  969. */
  970. for (i = 0; i < bi->port_number; ++i) {
  971. err = register_netdev(adapter->port[i].dev);
  972. if (err)
  973. pr_warn("%s: cannot register net device %s, skipping\n",
  974. pci_name(pdev), adapter->port[i].dev->name);
  975. else {
  976. /*
  977. * Change the name we use for messages to the name of
  978. * the first successfully registered interface.
  979. */
  980. if (!adapter->registered_device_map)
  981. adapter->name = adapter->port[i].dev->name;
  982. __set_bit(i, &adapter->registered_device_map);
  983. }
  984. }
  985. if (!adapter->registered_device_map) {
  986. pr_err("%s: could not register any net devices\n",
  987. pci_name(pdev));
  988. goto out_release_adapter_res;
  989. }
  990. pr_info("%s: %s (rev %d), %s %dMHz/%d-bit\n",
  991. adapter->name, bi->desc, adapter->params.chip_revision,
  992. adapter->params.pci.is_pcix ? "PCIX" : "PCI",
  993. adapter->params.pci.speed, adapter->params.pci.width);
  994. /*
  995. * Set the T1B ASIC and memory clocks.
  996. */
  997. if (t1powersave)
  998. adapter->t1powersave = LCLOCK; /* HW default is powersave mode. */
  999. else
  1000. adapter->t1powersave = HCLOCK;
  1001. if (t1_is_T1B(adapter))
  1002. t1_clock(adapter, t1powersave);
  1003. return 0;
  1004. out_release_adapter_res:
  1005. t1_free_sw_modules(adapter);
  1006. out_free_dev:
  1007. if (adapter) {
  1008. if (adapter->regs)
  1009. iounmap(adapter->regs);
  1010. for (i = bi->port_number - 1; i >= 0; --i)
  1011. if (adapter->port[i].dev)
  1012. free_netdev(adapter->port[i].dev);
  1013. }
  1014. pci_release_regions(pdev);
  1015. out_disable_pdev:
  1016. pci_disable_device(pdev);
  1017. return err;
  1018. }
  1019. static void bit_bang(struct adapter *adapter, int bitdata, int nbits)
  1020. {
  1021. int data;
  1022. int i;
  1023. u32 val;
  1024. enum {
  1025. S_CLOCK = 1 << 3,
  1026. S_DATA = 1 << 4
  1027. };
  1028. for (i = (nbits - 1); i > -1; i--) {
  1029. udelay(50);
  1030. data = ((bitdata >> i) & 0x1);
  1031. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1032. if (data)
  1033. val |= S_DATA;
  1034. else
  1035. val &= ~S_DATA;
  1036. udelay(50);
  1037. /* Set SCLOCK low */
  1038. val &= ~S_CLOCK;
  1039. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1040. udelay(50);
  1041. /* Write SCLOCK high */
  1042. val |= S_CLOCK;
  1043. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1044. }
  1045. }
  1046. static int t1_clock(struct adapter *adapter, int mode)
  1047. {
  1048. u32 val;
  1049. int M_CORE_VAL;
  1050. int M_MEM_VAL;
  1051. enum {
  1052. M_CORE_BITS = 9,
  1053. T_CORE_VAL = 0,
  1054. T_CORE_BITS = 2,
  1055. N_CORE_VAL = 0,
  1056. N_CORE_BITS = 2,
  1057. M_MEM_BITS = 9,
  1058. T_MEM_VAL = 0,
  1059. T_MEM_BITS = 2,
  1060. N_MEM_VAL = 0,
  1061. N_MEM_BITS = 2,
  1062. NP_LOAD = 1 << 17,
  1063. S_LOAD_MEM = 1 << 5,
  1064. S_LOAD_CORE = 1 << 6,
  1065. S_CLOCK = 1 << 3
  1066. };
  1067. if (!t1_is_T1B(adapter))
  1068. return -ENODEV; /* Can't re-clock this chip. */
  1069. if (mode & 2)
  1070. return 0; /* show current mode. */
  1071. if ((adapter->t1powersave & 1) == (mode & 1))
  1072. return -EALREADY; /* ASIC already running in mode. */
  1073. if ((mode & 1) == HCLOCK) {
  1074. M_CORE_VAL = 0x14;
  1075. M_MEM_VAL = 0x18;
  1076. adapter->t1powersave = HCLOCK; /* overclock */
  1077. } else {
  1078. M_CORE_VAL = 0xe;
  1079. M_MEM_VAL = 0x10;
  1080. adapter->t1powersave = LCLOCK; /* underclock */
  1081. }
  1082. /* Don't interrupt this serial stream! */
  1083. spin_lock(&adapter->tpi_lock);
  1084. /* Initialize for ASIC core */
  1085. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1086. val |= NP_LOAD;
  1087. udelay(50);
  1088. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1089. udelay(50);
  1090. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1091. val &= ~S_LOAD_CORE;
  1092. val &= ~S_CLOCK;
  1093. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1094. udelay(50);
  1095. /* Serial program the ASIC clock synthesizer */
  1096. bit_bang(adapter, T_CORE_VAL, T_CORE_BITS);
  1097. bit_bang(adapter, N_CORE_VAL, N_CORE_BITS);
  1098. bit_bang(adapter, M_CORE_VAL, M_CORE_BITS);
  1099. udelay(50);
  1100. /* Finish ASIC core */
  1101. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1102. val |= S_LOAD_CORE;
  1103. udelay(50);
  1104. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1105. udelay(50);
  1106. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1107. val &= ~S_LOAD_CORE;
  1108. udelay(50);
  1109. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1110. udelay(50);
  1111. /* Initialize for memory */
  1112. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1113. val |= NP_LOAD;
  1114. udelay(50);
  1115. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1116. udelay(50);
  1117. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1118. val &= ~S_LOAD_MEM;
  1119. val &= ~S_CLOCK;
  1120. udelay(50);
  1121. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1122. udelay(50);
  1123. /* Serial program the memory clock synthesizer */
  1124. bit_bang(adapter, T_MEM_VAL, T_MEM_BITS);
  1125. bit_bang(adapter, N_MEM_VAL, N_MEM_BITS);
  1126. bit_bang(adapter, M_MEM_VAL, M_MEM_BITS);
  1127. udelay(50);
  1128. /* Finish memory */
  1129. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1130. val |= S_LOAD_MEM;
  1131. udelay(50);
  1132. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1133. udelay(50);
  1134. __t1_tpi_read(adapter, A_ELMER0_GPO, &val);
  1135. val &= ~S_LOAD_MEM;
  1136. udelay(50);
  1137. __t1_tpi_write(adapter, A_ELMER0_GPO, val);
  1138. spin_unlock(&adapter->tpi_lock);
  1139. return 0;
  1140. }
  1141. static inline void t1_sw_reset(struct pci_dev *pdev)
  1142. {
  1143. pci_write_config_dword(pdev, A_PCICFG_PM_CSR, 3);
  1144. pci_write_config_dword(pdev, A_PCICFG_PM_CSR, 0);
  1145. }
  1146. static void remove_one(struct pci_dev *pdev)
  1147. {
  1148. struct net_device *dev = pci_get_drvdata(pdev);
  1149. struct adapter *adapter = dev->ml_priv;
  1150. int i;
  1151. for_each_port(adapter, i) {
  1152. if (test_bit(i, &adapter->registered_device_map))
  1153. unregister_netdev(adapter->port[i].dev);
  1154. }
  1155. t1_free_sw_modules(adapter);
  1156. iounmap(adapter->regs);
  1157. while (--i >= 0) {
  1158. if (adapter->port[i].dev)
  1159. free_netdev(adapter->port[i].dev);
  1160. }
  1161. pci_release_regions(pdev);
  1162. pci_disable_device(pdev);
  1163. t1_sw_reset(pdev);
  1164. }
  1165. static struct pci_driver cxgb_pci_driver = {
  1166. .name = DRV_NAME,
  1167. .id_table = t1_pci_tbl,
  1168. .probe = init_one,
  1169. .remove = remove_one,
  1170. };
  1171. module_pci_driver(cxgb_pci_driver);