pasemi_mac.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. /*
  2. * Copyright (C) 2006-2007 PA Semi, Inc
  3. *
  4. * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/delay.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/of_mdio.h>
  26. #include <linux/etherdevice.h>
  27. #include <asm/dma-mapping.h>
  28. #include <linux/in.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/ip.h>
  31. #include <net/checksum.h>
  32. #include <linux/prefetch.h>
  33. #include <asm/irq.h>
  34. #include <asm/firmware.h>
  35. #include <asm/pasemi_dma.h>
  36. #include "pasemi_mac.h"
  37. /* We have our own align, since ppc64 in general has it at 0 because
  38. * of design flaws in some of the server bridge chips. However, for
  39. * PWRficient doing the unaligned copies is more expensive than doing
  40. * unaligned DMA, so make sure the data is aligned instead.
  41. */
  42. #define LOCAL_SKB_ALIGN 2
  43. /* TODO list
  44. *
  45. * - Multicast support
  46. * - Large MTU support
  47. * - Multiqueue RX/TX
  48. */
  49. #define PE_MIN_MTU (ETH_ZLEN + ETH_HLEN)
  50. #define PE_MAX_MTU 9000
  51. #define PE_DEF_MTU ETH_DATA_LEN
  52. #define DEFAULT_MSG_ENABLE \
  53. (NETIF_MSG_DRV | \
  54. NETIF_MSG_PROBE | \
  55. NETIF_MSG_LINK | \
  56. NETIF_MSG_TIMER | \
  57. NETIF_MSG_IFDOWN | \
  58. NETIF_MSG_IFUP | \
  59. NETIF_MSG_RX_ERR | \
  60. NETIF_MSG_TX_ERR)
  61. MODULE_LICENSE("GPL");
  62. MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
  63. MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
  64. static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
  65. module_param(debug, int, 0);
  66. MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
  67. extern const struct ethtool_ops pasemi_mac_ethtool_ops;
  68. static int translation_enabled(void)
  69. {
  70. #if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
  71. return 1;
  72. #else
  73. return firmware_has_feature(FW_FEATURE_LPAR);
  74. #endif
  75. }
  76. static void write_iob_reg(unsigned int reg, unsigned int val)
  77. {
  78. pasemi_write_iob_reg(reg, val);
  79. }
  80. static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
  81. {
  82. return pasemi_read_mac_reg(mac->dma_if, reg);
  83. }
  84. static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
  85. unsigned int val)
  86. {
  87. pasemi_write_mac_reg(mac->dma_if, reg, val);
  88. }
  89. static unsigned int read_dma_reg(unsigned int reg)
  90. {
  91. return pasemi_read_dma_reg(reg);
  92. }
  93. static void write_dma_reg(unsigned int reg, unsigned int val)
  94. {
  95. pasemi_write_dma_reg(reg, val);
  96. }
  97. static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
  98. {
  99. return mac->rx;
  100. }
  101. static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
  102. {
  103. return mac->tx;
  104. }
  105. static inline void prefetch_skb(const struct sk_buff *skb)
  106. {
  107. const void *d = skb;
  108. prefetch(d);
  109. prefetch(d+64);
  110. prefetch(d+128);
  111. prefetch(d+192);
  112. }
  113. static int mac_to_intf(struct pasemi_mac *mac)
  114. {
  115. struct pci_dev *pdev = mac->pdev;
  116. u32 tmp;
  117. int nintf, off, i, j;
  118. int devfn = pdev->devfn;
  119. tmp = read_dma_reg(PAS_DMA_CAP_IFI);
  120. nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
  121. off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
  122. /* IOFF contains the offset to the registers containing the
  123. * DMA interface-to-MAC-pci-id mappings, and NIN contains number
  124. * of total interfaces. Each register contains 4 devfns.
  125. * Just do a linear search until we find the devfn of the MAC
  126. * we're trying to look up.
  127. */
  128. for (i = 0; i < (nintf+3)/4; i++) {
  129. tmp = read_dma_reg(off+4*i);
  130. for (j = 0; j < 4; j++) {
  131. if (((tmp >> (8*j)) & 0xff) == devfn)
  132. return i*4 + j;
  133. }
  134. }
  135. return -1;
  136. }
  137. static void pasemi_mac_intf_disable(struct pasemi_mac *mac)
  138. {
  139. unsigned int flags;
  140. flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
  141. flags &= ~PAS_MAC_CFG_PCFG_PE;
  142. write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
  143. }
  144. static void pasemi_mac_intf_enable(struct pasemi_mac *mac)
  145. {
  146. unsigned int flags;
  147. flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
  148. flags |= PAS_MAC_CFG_PCFG_PE;
  149. write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
  150. }
  151. static int pasemi_get_mac_addr(struct pasemi_mac *mac)
  152. {
  153. struct pci_dev *pdev = mac->pdev;
  154. struct device_node *dn = pci_device_to_OF_node(pdev);
  155. int len;
  156. const u8 *maddr;
  157. u8 addr[ETH_ALEN];
  158. if (!dn) {
  159. dev_dbg(&pdev->dev,
  160. "No device node for mac, not configuring\n");
  161. return -ENOENT;
  162. }
  163. maddr = of_get_property(dn, "local-mac-address", &len);
  164. if (maddr && len == ETH_ALEN) {
  165. memcpy(mac->mac_addr, maddr, ETH_ALEN);
  166. return 0;
  167. }
  168. /* Some old versions of firmware mistakenly uses mac-address
  169. * (and as a string) instead of a byte array in local-mac-address.
  170. */
  171. if (maddr == NULL)
  172. maddr = of_get_property(dn, "mac-address", NULL);
  173. if (maddr == NULL) {
  174. dev_warn(&pdev->dev,
  175. "no mac address in device tree, not configuring\n");
  176. return -ENOENT;
  177. }
  178. if (!mac_pton(maddr, addr)) {
  179. dev_warn(&pdev->dev,
  180. "can't parse mac address, not configuring\n");
  181. return -EINVAL;
  182. }
  183. memcpy(mac->mac_addr, addr, ETH_ALEN);
  184. return 0;
  185. }
  186. static int pasemi_mac_set_mac_addr(struct net_device *dev, void *p)
  187. {
  188. struct pasemi_mac *mac = netdev_priv(dev);
  189. struct sockaddr *addr = p;
  190. unsigned int adr0, adr1;
  191. if (!is_valid_ether_addr(addr->sa_data))
  192. return -EADDRNOTAVAIL;
  193. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  194. adr0 = dev->dev_addr[2] << 24 |
  195. dev->dev_addr[3] << 16 |
  196. dev->dev_addr[4] << 8 |
  197. dev->dev_addr[5];
  198. adr1 = read_mac_reg(mac, PAS_MAC_CFG_ADR1);
  199. adr1 &= ~0xffff;
  200. adr1 |= dev->dev_addr[0] << 8 | dev->dev_addr[1];
  201. pasemi_mac_intf_disable(mac);
  202. write_mac_reg(mac, PAS_MAC_CFG_ADR0, adr0);
  203. write_mac_reg(mac, PAS_MAC_CFG_ADR1, adr1);
  204. pasemi_mac_intf_enable(mac);
  205. return 0;
  206. }
  207. static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
  208. const int nfrags,
  209. struct sk_buff *skb,
  210. const dma_addr_t *dmas)
  211. {
  212. int f;
  213. struct pci_dev *pdev = mac->dma_pdev;
  214. pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
  215. for (f = 0; f < nfrags; f++) {
  216. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  217. pci_unmap_page(pdev, dmas[f+1], skb_frag_size(frag), PCI_DMA_TODEVICE);
  218. }
  219. dev_kfree_skb_irq(skb);
  220. /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
  221. * aligned up to a power of 2
  222. */
  223. return (nfrags + 3) & ~1;
  224. }
  225. static struct pasemi_mac_csring *pasemi_mac_setup_csring(struct pasemi_mac *mac)
  226. {
  227. struct pasemi_mac_csring *ring;
  228. u32 val;
  229. unsigned int cfg;
  230. int chno;
  231. ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_csring),
  232. offsetof(struct pasemi_mac_csring, chan));
  233. if (!ring) {
  234. dev_err(&mac->pdev->dev, "Can't allocate checksum channel\n");
  235. goto out_chan;
  236. }
  237. chno = ring->chan.chno;
  238. ring->size = CS_RING_SIZE;
  239. ring->next_to_fill = 0;
  240. /* Allocate descriptors */
  241. if (pasemi_dma_alloc_ring(&ring->chan, CS_RING_SIZE))
  242. goto out_ring_desc;
  243. write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
  244. PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
  245. val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
  246. val |= PAS_DMA_TXCHAN_BASEU_SIZ(CS_RING_SIZE >> 3);
  247. write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
  248. ring->events[0] = pasemi_dma_alloc_flag();
  249. ring->events[1] = pasemi_dma_alloc_flag();
  250. if (ring->events[0] < 0 || ring->events[1] < 0)
  251. goto out_flags;
  252. pasemi_dma_clear_flag(ring->events[0]);
  253. pasemi_dma_clear_flag(ring->events[1]);
  254. ring->fun = pasemi_dma_alloc_fun();
  255. if (ring->fun < 0)
  256. goto out_fun;
  257. cfg = PAS_DMA_TXCHAN_CFG_TY_FUNC | PAS_DMA_TXCHAN_CFG_UP |
  258. PAS_DMA_TXCHAN_CFG_TATTR(ring->fun) |
  259. PAS_DMA_TXCHAN_CFG_LPSQ | PAS_DMA_TXCHAN_CFG_LPDQ;
  260. if (translation_enabled())
  261. cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
  262. write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
  263. /* enable channel */
  264. pasemi_dma_start_chan(&ring->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
  265. PAS_DMA_TXCHAN_TCMDSTA_DB |
  266. PAS_DMA_TXCHAN_TCMDSTA_DE |
  267. PAS_DMA_TXCHAN_TCMDSTA_DA);
  268. return ring;
  269. out_fun:
  270. out_flags:
  271. if (ring->events[0] >= 0)
  272. pasemi_dma_free_flag(ring->events[0]);
  273. if (ring->events[1] >= 0)
  274. pasemi_dma_free_flag(ring->events[1]);
  275. pasemi_dma_free_ring(&ring->chan);
  276. out_ring_desc:
  277. pasemi_dma_free_chan(&ring->chan);
  278. out_chan:
  279. return NULL;
  280. }
  281. static void pasemi_mac_setup_csrings(struct pasemi_mac *mac)
  282. {
  283. int i;
  284. mac->cs[0] = pasemi_mac_setup_csring(mac);
  285. if (mac->type == MAC_TYPE_XAUI)
  286. mac->cs[1] = pasemi_mac_setup_csring(mac);
  287. else
  288. mac->cs[1] = 0;
  289. for (i = 0; i < MAX_CS; i++)
  290. if (mac->cs[i])
  291. mac->num_cs++;
  292. }
  293. static void pasemi_mac_free_csring(struct pasemi_mac_csring *csring)
  294. {
  295. pasemi_dma_stop_chan(&csring->chan);
  296. pasemi_dma_free_flag(csring->events[0]);
  297. pasemi_dma_free_flag(csring->events[1]);
  298. pasemi_dma_free_ring(&csring->chan);
  299. pasemi_dma_free_chan(&csring->chan);
  300. pasemi_dma_free_fun(csring->fun);
  301. }
  302. static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
  303. {
  304. struct pasemi_mac_rxring *ring;
  305. struct pasemi_mac *mac = netdev_priv(dev);
  306. int chno;
  307. unsigned int cfg;
  308. ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
  309. offsetof(struct pasemi_mac_rxring, chan));
  310. if (!ring) {
  311. dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
  312. goto out_chan;
  313. }
  314. chno = ring->chan.chno;
  315. spin_lock_init(&ring->lock);
  316. ring->size = RX_RING_SIZE;
  317. ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
  318. RX_RING_SIZE, GFP_KERNEL);
  319. if (!ring->ring_info)
  320. goto out_ring_info;
  321. /* Allocate descriptors */
  322. if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
  323. goto out_ring_desc;
  324. ring->buffers = dma_zalloc_coherent(&mac->dma_pdev->dev,
  325. RX_RING_SIZE * sizeof(u64),
  326. &ring->buf_dma, GFP_KERNEL);
  327. if (!ring->buffers)
  328. goto out_ring_desc;
  329. write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
  330. PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
  331. write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
  332. PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
  333. PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
  334. cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
  335. if (translation_enabled())
  336. cfg |= PAS_DMA_RXCHAN_CFG_CTR;
  337. write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
  338. write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
  339. PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
  340. write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
  341. PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
  342. PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
  343. cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
  344. PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
  345. PAS_DMA_RXINT_CFG_HEN;
  346. if (translation_enabled())
  347. cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
  348. write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
  349. ring->next_to_fill = 0;
  350. ring->next_to_clean = 0;
  351. ring->mac = mac;
  352. mac->rx = ring;
  353. return 0;
  354. out_ring_desc:
  355. kfree(ring->ring_info);
  356. out_ring_info:
  357. pasemi_dma_free_chan(&ring->chan);
  358. out_chan:
  359. return -ENOMEM;
  360. }
  361. static struct pasemi_mac_txring *
  362. pasemi_mac_setup_tx_resources(const struct net_device *dev)
  363. {
  364. struct pasemi_mac *mac = netdev_priv(dev);
  365. u32 val;
  366. struct pasemi_mac_txring *ring;
  367. unsigned int cfg;
  368. int chno;
  369. ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
  370. offsetof(struct pasemi_mac_txring, chan));
  371. if (!ring) {
  372. dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
  373. goto out_chan;
  374. }
  375. chno = ring->chan.chno;
  376. spin_lock_init(&ring->lock);
  377. ring->size = TX_RING_SIZE;
  378. ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
  379. TX_RING_SIZE, GFP_KERNEL);
  380. if (!ring->ring_info)
  381. goto out_ring_info;
  382. /* Allocate descriptors */
  383. if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
  384. goto out_ring_desc;
  385. write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
  386. PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
  387. val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
  388. val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
  389. write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
  390. cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
  391. PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
  392. PAS_DMA_TXCHAN_CFG_UP |
  393. PAS_DMA_TXCHAN_CFG_WT(4);
  394. if (translation_enabled())
  395. cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
  396. write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
  397. ring->next_to_fill = 0;
  398. ring->next_to_clean = 0;
  399. ring->mac = mac;
  400. return ring;
  401. out_ring_desc:
  402. kfree(ring->ring_info);
  403. out_ring_info:
  404. pasemi_dma_free_chan(&ring->chan);
  405. out_chan:
  406. return NULL;
  407. }
  408. static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
  409. {
  410. struct pasemi_mac_txring *txring = tx_ring(mac);
  411. unsigned int i, j;
  412. struct pasemi_mac_buffer *info;
  413. dma_addr_t dmas[MAX_SKB_FRAGS+1];
  414. int freed, nfrags;
  415. int start, limit;
  416. start = txring->next_to_clean;
  417. limit = txring->next_to_fill;
  418. /* Compensate for when fill has wrapped and clean has not */
  419. if (start > limit)
  420. limit += TX_RING_SIZE;
  421. for (i = start; i < limit; i += freed) {
  422. info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
  423. if (info->dma && info->skb) {
  424. nfrags = skb_shinfo(info->skb)->nr_frags;
  425. for (j = 0; j <= nfrags; j++)
  426. dmas[j] = txring->ring_info[(i+1+j) &
  427. (TX_RING_SIZE-1)].dma;
  428. freed = pasemi_mac_unmap_tx_skb(mac, nfrags,
  429. info->skb, dmas);
  430. } else {
  431. freed = 2;
  432. }
  433. }
  434. kfree(txring->ring_info);
  435. pasemi_dma_free_chan(&txring->chan);
  436. }
  437. static void pasemi_mac_free_rx_buffers(struct pasemi_mac *mac)
  438. {
  439. struct pasemi_mac_rxring *rx = rx_ring(mac);
  440. unsigned int i;
  441. struct pasemi_mac_buffer *info;
  442. for (i = 0; i < RX_RING_SIZE; i++) {
  443. info = &RX_DESC_INFO(rx, i);
  444. if (info->skb && info->dma) {
  445. pci_unmap_single(mac->dma_pdev,
  446. info->dma,
  447. info->skb->len,
  448. PCI_DMA_FROMDEVICE);
  449. dev_kfree_skb_any(info->skb);
  450. }
  451. info->dma = 0;
  452. info->skb = NULL;
  453. }
  454. for (i = 0; i < RX_RING_SIZE; i++)
  455. RX_BUFF(rx, i) = 0;
  456. }
  457. static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
  458. {
  459. pasemi_mac_free_rx_buffers(mac);
  460. dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
  461. rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
  462. kfree(rx_ring(mac)->ring_info);
  463. pasemi_dma_free_chan(&rx_ring(mac)->chan);
  464. mac->rx = NULL;
  465. }
  466. static void pasemi_mac_replenish_rx_ring(struct net_device *dev,
  467. const int limit)
  468. {
  469. const struct pasemi_mac *mac = netdev_priv(dev);
  470. struct pasemi_mac_rxring *rx = rx_ring(mac);
  471. int fill, count;
  472. if (limit <= 0)
  473. return;
  474. fill = rx_ring(mac)->next_to_fill;
  475. for (count = 0; count < limit; count++) {
  476. struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
  477. u64 *buff = &RX_BUFF(rx, fill);
  478. struct sk_buff *skb;
  479. dma_addr_t dma;
  480. /* Entry in use? */
  481. WARN_ON(*buff);
  482. skb = netdev_alloc_skb(dev, mac->bufsz);
  483. skb_reserve(skb, LOCAL_SKB_ALIGN);
  484. if (unlikely(!skb))
  485. break;
  486. dma = pci_map_single(mac->dma_pdev, skb->data,
  487. mac->bufsz - LOCAL_SKB_ALIGN,
  488. PCI_DMA_FROMDEVICE);
  489. if (unlikely(pci_dma_mapping_error(mac->dma_pdev, dma))) {
  490. dev_kfree_skb_irq(info->skb);
  491. break;
  492. }
  493. info->skb = skb;
  494. info->dma = dma;
  495. *buff = XCT_RXB_LEN(mac->bufsz) | XCT_RXB_ADDR(dma);
  496. fill++;
  497. }
  498. wmb();
  499. write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
  500. rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
  501. (RX_RING_SIZE - 1);
  502. }
  503. static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
  504. {
  505. struct pasemi_mac_rxring *rx = rx_ring(mac);
  506. unsigned int reg, pcnt;
  507. /* Re-enable packet count interrupts: finally
  508. * ack the packet count interrupt we got in rx_intr.
  509. */
  510. pcnt = *rx->chan.status & PAS_STATUS_PCNT_M;
  511. reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
  512. if (*rx->chan.status & PAS_STATUS_TIMER)
  513. reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
  514. write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
  515. }
  516. static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
  517. {
  518. unsigned int reg, pcnt;
  519. /* Re-enable packet count interrupts */
  520. pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
  521. reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
  522. write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
  523. }
  524. static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
  525. const u64 macrx)
  526. {
  527. unsigned int rcmdsta, ccmdsta;
  528. struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
  529. if (!netif_msg_rx_err(mac))
  530. return;
  531. rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
  532. ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
  533. printk(KERN_ERR "pasemi_mac: rx error. macrx %016llx, rx status %llx\n",
  534. macrx, *chan->status);
  535. printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
  536. rcmdsta, ccmdsta);
  537. }
  538. static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
  539. const u64 mactx)
  540. {
  541. unsigned int cmdsta;
  542. struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
  543. if (!netif_msg_tx_err(mac))
  544. return;
  545. cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
  546. printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016llx, "\
  547. "tx status 0x%016llx\n", mactx, *chan->status);
  548. printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
  549. }
  550. static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
  551. const int limit)
  552. {
  553. const struct pasemi_dmachan *chan = &rx->chan;
  554. struct pasemi_mac *mac = rx->mac;
  555. struct pci_dev *pdev = mac->dma_pdev;
  556. unsigned int n;
  557. int count, buf_index, tot_bytes, packets;
  558. struct pasemi_mac_buffer *info;
  559. struct sk_buff *skb;
  560. unsigned int len;
  561. u64 macrx, eval;
  562. dma_addr_t dma;
  563. tot_bytes = 0;
  564. packets = 0;
  565. spin_lock(&rx->lock);
  566. n = rx->next_to_clean;
  567. prefetch(&RX_DESC(rx, n));
  568. for (count = 0; count < limit; count++) {
  569. macrx = RX_DESC(rx, n);
  570. prefetch(&RX_DESC(rx, n+4));
  571. if ((macrx & XCT_MACRX_E) ||
  572. (*chan->status & PAS_STATUS_ERROR))
  573. pasemi_mac_rx_error(mac, macrx);
  574. if (!(macrx & XCT_MACRX_O))
  575. break;
  576. info = NULL;
  577. BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
  578. eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
  579. XCT_RXRES_8B_EVAL_S;
  580. buf_index = eval-1;
  581. dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
  582. info = &RX_DESC_INFO(rx, buf_index);
  583. skb = info->skb;
  584. prefetch_skb(skb);
  585. len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
  586. pci_unmap_single(pdev, dma, mac->bufsz - LOCAL_SKB_ALIGN,
  587. PCI_DMA_FROMDEVICE);
  588. if (macrx & XCT_MACRX_CRC) {
  589. /* CRC error flagged */
  590. mac->netdev->stats.rx_errors++;
  591. mac->netdev->stats.rx_crc_errors++;
  592. /* No need to free skb, it'll be reused */
  593. goto next;
  594. }
  595. info->skb = NULL;
  596. info->dma = 0;
  597. if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
  598. skb->ip_summed = CHECKSUM_UNNECESSARY;
  599. skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
  600. XCT_MACRX_CSUM_S;
  601. } else {
  602. skb_checksum_none_assert(skb);
  603. }
  604. packets++;
  605. tot_bytes += len;
  606. /* Don't include CRC */
  607. skb_put(skb, len-4);
  608. skb->protocol = eth_type_trans(skb, mac->netdev);
  609. napi_gro_receive(&mac->napi, skb);
  610. next:
  611. RX_DESC(rx, n) = 0;
  612. RX_DESC(rx, n+1) = 0;
  613. /* Need to zero it out since hardware doesn't, since the
  614. * replenish loop uses it to tell when it's done.
  615. */
  616. RX_BUFF(rx, buf_index) = 0;
  617. n += 4;
  618. }
  619. if (n > RX_RING_SIZE) {
  620. /* Errata 5971 workaround: L2 target of headers */
  621. write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
  622. n &= (RX_RING_SIZE-1);
  623. }
  624. rx_ring(mac)->next_to_clean = n;
  625. /* Increase is in number of 16-byte entries, and since each descriptor
  626. * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
  627. * count*2.
  628. */
  629. write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
  630. pasemi_mac_replenish_rx_ring(mac->netdev, count);
  631. mac->netdev->stats.rx_bytes += tot_bytes;
  632. mac->netdev->stats.rx_packets += packets;
  633. spin_unlock(&rx_ring(mac)->lock);
  634. return count;
  635. }
  636. /* Can't make this too large or we blow the kernel stack limits */
  637. #define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
  638. static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
  639. {
  640. struct pasemi_dmachan *chan = &txring->chan;
  641. struct pasemi_mac *mac = txring->mac;
  642. int i, j;
  643. unsigned int start, descr_count, buf_count, batch_limit;
  644. unsigned int ring_limit;
  645. unsigned int total_count;
  646. unsigned long flags;
  647. struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
  648. dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
  649. int nf[TX_CLEAN_BATCHSIZE];
  650. int nr_frags;
  651. total_count = 0;
  652. batch_limit = TX_CLEAN_BATCHSIZE;
  653. restart:
  654. spin_lock_irqsave(&txring->lock, flags);
  655. start = txring->next_to_clean;
  656. ring_limit = txring->next_to_fill;
  657. prefetch(&TX_DESC_INFO(txring, start+1).skb);
  658. /* Compensate for when fill has wrapped but clean has not */
  659. if (start > ring_limit)
  660. ring_limit += TX_RING_SIZE;
  661. buf_count = 0;
  662. descr_count = 0;
  663. for (i = start;
  664. descr_count < batch_limit && i < ring_limit;
  665. i += buf_count) {
  666. u64 mactx = TX_DESC(txring, i);
  667. struct sk_buff *skb;
  668. if ((mactx & XCT_MACTX_E) ||
  669. (*chan->status & PAS_STATUS_ERROR))
  670. pasemi_mac_tx_error(mac, mactx);
  671. /* Skip over control descriptors */
  672. if (!(mactx & XCT_MACTX_LLEN_M)) {
  673. TX_DESC(txring, i) = 0;
  674. TX_DESC(txring, i+1) = 0;
  675. buf_count = 2;
  676. continue;
  677. }
  678. skb = TX_DESC_INFO(txring, i+1).skb;
  679. nr_frags = TX_DESC_INFO(txring, i).dma;
  680. if (unlikely(mactx & XCT_MACTX_O))
  681. /* Not yet transmitted */
  682. break;
  683. buf_count = 2 + nr_frags;
  684. /* Since we always fill with an even number of entries, make
  685. * sure we skip any unused one at the end as well.
  686. */
  687. if (buf_count & 1)
  688. buf_count++;
  689. for (j = 0; j <= nr_frags; j++)
  690. dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
  691. skbs[descr_count] = skb;
  692. nf[descr_count] = nr_frags;
  693. TX_DESC(txring, i) = 0;
  694. TX_DESC(txring, i+1) = 0;
  695. descr_count++;
  696. }
  697. txring->next_to_clean = i & (TX_RING_SIZE-1);
  698. spin_unlock_irqrestore(&txring->lock, flags);
  699. netif_wake_queue(mac->netdev);
  700. for (i = 0; i < descr_count; i++)
  701. pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]);
  702. total_count += descr_count;
  703. /* If the batch was full, try to clean more */
  704. if (descr_count == batch_limit)
  705. goto restart;
  706. return total_count;
  707. }
  708. static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
  709. {
  710. const struct pasemi_mac_rxring *rxring = data;
  711. struct pasemi_mac *mac = rxring->mac;
  712. const struct pasemi_dmachan *chan = &rxring->chan;
  713. unsigned int reg;
  714. if (!(*chan->status & PAS_STATUS_CAUSE_M))
  715. return IRQ_NONE;
  716. /* Don't reset packet count so it won't fire again but clear
  717. * all others.
  718. */
  719. reg = 0;
  720. if (*chan->status & PAS_STATUS_SOFT)
  721. reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
  722. if (*chan->status & PAS_STATUS_ERROR)
  723. reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
  724. napi_schedule(&mac->napi);
  725. write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
  726. return IRQ_HANDLED;
  727. }
  728. #define TX_CLEAN_INTERVAL HZ
  729. static void pasemi_mac_tx_timer(struct timer_list *t)
  730. {
  731. struct pasemi_mac_txring *txring = from_timer(txring, t, clean_timer);
  732. struct pasemi_mac *mac = txring->mac;
  733. pasemi_mac_clean_tx(txring);
  734. mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
  735. pasemi_mac_restart_tx_intr(mac);
  736. }
  737. static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
  738. {
  739. struct pasemi_mac_txring *txring = data;
  740. const struct pasemi_dmachan *chan = &txring->chan;
  741. struct pasemi_mac *mac = txring->mac;
  742. unsigned int reg;
  743. if (!(*chan->status & PAS_STATUS_CAUSE_M))
  744. return IRQ_NONE;
  745. reg = 0;
  746. if (*chan->status & PAS_STATUS_SOFT)
  747. reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
  748. if (*chan->status & PAS_STATUS_ERROR)
  749. reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
  750. mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
  751. napi_schedule(&mac->napi);
  752. if (reg)
  753. write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
  754. return IRQ_HANDLED;
  755. }
  756. static void pasemi_adjust_link(struct net_device *dev)
  757. {
  758. struct pasemi_mac *mac = netdev_priv(dev);
  759. int msg;
  760. unsigned int flags;
  761. unsigned int new_flags;
  762. if (!dev->phydev->link) {
  763. /* If no link, MAC speed settings don't matter. Just report
  764. * link down and return.
  765. */
  766. if (mac->link && netif_msg_link(mac))
  767. printk(KERN_INFO "%s: Link is down.\n", dev->name);
  768. netif_carrier_off(dev);
  769. pasemi_mac_intf_disable(mac);
  770. mac->link = 0;
  771. return;
  772. } else {
  773. pasemi_mac_intf_enable(mac);
  774. netif_carrier_on(dev);
  775. }
  776. flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
  777. new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
  778. PAS_MAC_CFG_PCFG_TSR_M);
  779. if (!dev->phydev->duplex)
  780. new_flags |= PAS_MAC_CFG_PCFG_HD;
  781. switch (dev->phydev->speed) {
  782. case 1000:
  783. new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
  784. PAS_MAC_CFG_PCFG_TSR_1G;
  785. break;
  786. case 100:
  787. new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
  788. PAS_MAC_CFG_PCFG_TSR_100M;
  789. break;
  790. case 10:
  791. new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
  792. PAS_MAC_CFG_PCFG_TSR_10M;
  793. break;
  794. default:
  795. printk("Unsupported speed %d\n", dev->phydev->speed);
  796. }
  797. /* Print on link or speed/duplex change */
  798. msg = mac->link != dev->phydev->link || flags != new_flags;
  799. mac->duplex = dev->phydev->duplex;
  800. mac->speed = dev->phydev->speed;
  801. mac->link = dev->phydev->link;
  802. if (new_flags != flags)
  803. write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
  804. if (msg && netif_msg_link(mac))
  805. printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
  806. dev->name, mac->speed, mac->duplex ? "full" : "half");
  807. }
  808. static int pasemi_mac_phy_init(struct net_device *dev)
  809. {
  810. struct pasemi_mac *mac = netdev_priv(dev);
  811. struct device_node *dn, *phy_dn;
  812. struct phy_device *phydev;
  813. dn = pci_device_to_OF_node(mac->pdev);
  814. phy_dn = of_parse_phandle(dn, "phy-handle", 0);
  815. of_node_put(phy_dn);
  816. mac->link = 0;
  817. mac->speed = 0;
  818. mac->duplex = -1;
  819. phydev = of_phy_connect(dev, phy_dn, &pasemi_adjust_link, 0,
  820. PHY_INTERFACE_MODE_SGMII);
  821. if (!phydev) {
  822. printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
  823. return -ENODEV;
  824. }
  825. return 0;
  826. }
  827. static int pasemi_mac_open(struct net_device *dev)
  828. {
  829. struct pasemi_mac *mac = netdev_priv(dev);
  830. unsigned int flags;
  831. int i, ret;
  832. flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
  833. PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
  834. PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
  835. write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
  836. ret = pasemi_mac_setup_rx_resources(dev);
  837. if (ret)
  838. goto out_rx_resources;
  839. mac->tx = pasemi_mac_setup_tx_resources(dev);
  840. if (!mac->tx)
  841. goto out_tx_ring;
  842. /* We might already have allocated rings in case mtu was changed
  843. * before interface was brought up.
  844. */
  845. if (dev->mtu > 1500 && !mac->num_cs) {
  846. pasemi_mac_setup_csrings(mac);
  847. if (!mac->num_cs)
  848. goto out_tx_ring;
  849. }
  850. /* Zero out rmon counters */
  851. for (i = 0; i < 32; i++)
  852. write_mac_reg(mac, PAS_MAC_RMON(i), 0);
  853. /* 0x3ff with 33MHz clock is about 31us */
  854. write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
  855. PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
  856. write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
  857. PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
  858. write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
  859. PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
  860. write_mac_reg(mac, PAS_MAC_IPC_CHNL,
  861. PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
  862. PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
  863. /* enable rx if */
  864. write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
  865. PAS_DMA_RXINT_RCMDSTA_EN |
  866. PAS_DMA_RXINT_RCMDSTA_DROPS_M |
  867. PAS_DMA_RXINT_RCMDSTA_BP |
  868. PAS_DMA_RXINT_RCMDSTA_OO |
  869. PAS_DMA_RXINT_RCMDSTA_BT);
  870. /* enable rx channel */
  871. pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
  872. PAS_DMA_RXCHAN_CCMDSTA_OD |
  873. PAS_DMA_RXCHAN_CCMDSTA_FD |
  874. PAS_DMA_RXCHAN_CCMDSTA_DT);
  875. /* enable tx channel */
  876. pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
  877. PAS_DMA_TXCHAN_TCMDSTA_DB |
  878. PAS_DMA_TXCHAN_TCMDSTA_DE |
  879. PAS_DMA_TXCHAN_TCMDSTA_DA);
  880. pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
  881. write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
  882. RX_RING_SIZE>>1);
  883. /* Clear out any residual packet count state from firmware */
  884. pasemi_mac_restart_rx_intr(mac);
  885. pasemi_mac_restart_tx_intr(mac);
  886. flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
  887. if (mac->type == MAC_TYPE_GMAC)
  888. flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
  889. else
  890. flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
  891. /* Enable interface in MAC */
  892. write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
  893. ret = pasemi_mac_phy_init(dev);
  894. if (ret) {
  895. /* Since we won't get link notification, just enable RX */
  896. pasemi_mac_intf_enable(mac);
  897. if (mac->type == MAC_TYPE_GMAC) {
  898. /* Warn for missing PHY on SGMII (1Gig) ports */
  899. dev_warn(&mac->pdev->dev,
  900. "PHY init failed: %d.\n", ret);
  901. dev_warn(&mac->pdev->dev,
  902. "Defaulting to 1Gbit full duplex\n");
  903. }
  904. }
  905. netif_start_queue(dev);
  906. napi_enable(&mac->napi);
  907. snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
  908. dev->name);
  909. ret = request_irq(mac->tx->chan.irq, pasemi_mac_tx_intr, 0,
  910. mac->tx_irq_name, mac->tx);
  911. if (ret) {
  912. dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
  913. mac->tx->chan.irq, ret);
  914. goto out_tx_int;
  915. }
  916. snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
  917. dev->name);
  918. ret = request_irq(mac->rx->chan.irq, pasemi_mac_rx_intr, 0,
  919. mac->rx_irq_name, mac->rx);
  920. if (ret) {
  921. dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
  922. mac->rx->chan.irq, ret);
  923. goto out_rx_int;
  924. }
  925. if (dev->phydev)
  926. phy_start(dev->phydev);
  927. timer_setup(&mac->tx->clean_timer, pasemi_mac_tx_timer, 0);
  928. mod_timer(&mac->tx->clean_timer, jiffies + HZ);
  929. return 0;
  930. out_rx_int:
  931. free_irq(mac->tx->chan.irq, mac->tx);
  932. out_tx_int:
  933. napi_disable(&mac->napi);
  934. netif_stop_queue(dev);
  935. out_tx_ring:
  936. if (mac->tx)
  937. pasemi_mac_free_tx_resources(mac);
  938. pasemi_mac_free_rx_resources(mac);
  939. out_rx_resources:
  940. return ret;
  941. }
  942. #define MAX_RETRIES 5000
  943. static void pasemi_mac_pause_txchan(struct pasemi_mac *mac)
  944. {
  945. unsigned int sta, retries;
  946. int txch = tx_ring(mac)->chan.chno;
  947. write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
  948. PAS_DMA_TXCHAN_TCMDSTA_ST);
  949. for (retries = 0; retries < MAX_RETRIES; retries++) {
  950. sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
  951. if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
  952. break;
  953. cond_resched();
  954. }
  955. if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
  956. dev_err(&mac->dma_pdev->dev,
  957. "Failed to stop tx channel, tcmdsta %08x\n", sta);
  958. write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
  959. }
  960. static void pasemi_mac_pause_rxchan(struct pasemi_mac *mac)
  961. {
  962. unsigned int sta, retries;
  963. int rxch = rx_ring(mac)->chan.chno;
  964. write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
  965. PAS_DMA_RXCHAN_CCMDSTA_ST);
  966. for (retries = 0; retries < MAX_RETRIES; retries++) {
  967. sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
  968. if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
  969. break;
  970. cond_resched();
  971. }
  972. if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
  973. dev_err(&mac->dma_pdev->dev,
  974. "Failed to stop rx channel, ccmdsta 08%x\n", sta);
  975. write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
  976. }
  977. static void pasemi_mac_pause_rxint(struct pasemi_mac *mac)
  978. {
  979. unsigned int sta, retries;
  980. write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
  981. PAS_DMA_RXINT_RCMDSTA_ST);
  982. for (retries = 0; retries < MAX_RETRIES; retries++) {
  983. sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
  984. if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
  985. break;
  986. cond_resched();
  987. }
  988. if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
  989. dev_err(&mac->dma_pdev->dev,
  990. "Failed to stop rx interface, rcmdsta %08x\n", sta);
  991. write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
  992. }
  993. static int pasemi_mac_close(struct net_device *dev)
  994. {
  995. struct pasemi_mac *mac = netdev_priv(dev);
  996. unsigned int sta;
  997. int rxch, txch, i;
  998. rxch = rx_ring(mac)->chan.chno;
  999. txch = tx_ring(mac)->chan.chno;
  1000. if (dev->phydev) {
  1001. phy_stop(dev->phydev);
  1002. phy_disconnect(dev->phydev);
  1003. }
  1004. del_timer_sync(&mac->tx->clean_timer);
  1005. netif_stop_queue(dev);
  1006. napi_disable(&mac->napi);
  1007. sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
  1008. if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
  1009. PAS_DMA_RXINT_RCMDSTA_OO |
  1010. PAS_DMA_RXINT_RCMDSTA_BT))
  1011. printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
  1012. sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
  1013. if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
  1014. PAS_DMA_RXCHAN_CCMDSTA_OD |
  1015. PAS_DMA_RXCHAN_CCMDSTA_FD |
  1016. PAS_DMA_RXCHAN_CCMDSTA_DT))
  1017. printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
  1018. sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
  1019. if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
  1020. PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
  1021. printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
  1022. /* Clean out any pending buffers */
  1023. pasemi_mac_clean_tx(tx_ring(mac));
  1024. pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
  1025. pasemi_mac_pause_txchan(mac);
  1026. pasemi_mac_pause_rxint(mac);
  1027. pasemi_mac_pause_rxchan(mac);
  1028. pasemi_mac_intf_disable(mac);
  1029. free_irq(mac->tx->chan.irq, mac->tx);
  1030. free_irq(mac->rx->chan.irq, mac->rx);
  1031. for (i = 0; i < mac->num_cs; i++) {
  1032. pasemi_mac_free_csring(mac->cs[i]);
  1033. mac->cs[i] = NULL;
  1034. }
  1035. mac->num_cs = 0;
  1036. /* Free resources */
  1037. pasemi_mac_free_rx_resources(mac);
  1038. pasemi_mac_free_tx_resources(mac);
  1039. return 0;
  1040. }
  1041. static void pasemi_mac_queue_csdesc(const struct sk_buff *skb,
  1042. const dma_addr_t *map,
  1043. const unsigned int *map_size,
  1044. struct pasemi_mac_txring *txring,
  1045. struct pasemi_mac_csring *csring)
  1046. {
  1047. u64 fund;
  1048. dma_addr_t cs_dest;
  1049. const int nh_off = skb_network_offset(skb);
  1050. const int nh_len = skb_network_header_len(skb);
  1051. const int nfrags = skb_shinfo(skb)->nr_frags;
  1052. int cs_size, i, fill, hdr, cpyhdr, evt;
  1053. dma_addr_t csdma;
  1054. fund = XCT_FUN_ST | XCT_FUN_RR_8BRES |
  1055. XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
  1056. XCT_FUN_CRM_SIG | XCT_FUN_LLEN(skb->len - nh_off) |
  1057. XCT_FUN_SHL(nh_len >> 2) | XCT_FUN_SE;
  1058. switch (ip_hdr(skb)->protocol) {
  1059. case IPPROTO_TCP:
  1060. fund |= XCT_FUN_SIG_TCP4;
  1061. /* TCP checksum is 16 bytes into the header */
  1062. cs_dest = map[0] + skb_transport_offset(skb) + 16;
  1063. break;
  1064. case IPPROTO_UDP:
  1065. fund |= XCT_FUN_SIG_UDP4;
  1066. /* UDP checksum is 6 bytes into the header */
  1067. cs_dest = map[0] + skb_transport_offset(skb) + 6;
  1068. break;
  1069. default:
  1070. BUG();
  1071. }
  1072. /* Do the checksum offloaded */
  1073. fill = csring->next_to_fill;
  1074. hdr = fill;
  1075. CS_DESC(csring, fill++) = fund;
  1076. /* Room for 8BRES. Checksum result is really 2 bytes into it */
  1077. csdma = csring->chan.ring_dma + (fill & (CS_RING_SIZE-1)) * 8 + 2;
  1078. CS_DESC(csring, fill++) = 0;
  1079. CS_DESC(csring, fill) = XCT_PTR_LEN(map_size[0]-nh_off) | XCT_PTR_ADDR(map[0]+nh_off);
  1080. for (i = 1; i <= nfrags; i++)
  1081. CS_DESC(csring, fill+i) = XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
  1082. fill += i;
  1083. if (fill & 1)
  1084. fill++;
  1085. /* Copy the result into the TCP packet */
  1086. cpyhdr = fill;
  1087. CS_DESC(csring, fill++) = XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
  1088. XCT_FUN_LLEN(2) | XCT_FUN_SE;
  1089. CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(cs_dest) | XCT_PTR_T;
  1090. CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(csdma);
  1091. fill++;
  1092. evt = !csring->last_event;
  1093. csring->last_event = evt;
  1094. /* Event handshaking with MAC TX */
  1095. CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
  1096. CTRL_CMD_ETYPE_SET | CTRL_CMD_REG(csring->events[evt]);
  1097. CS_DESC(csring, fill++) = 0;
  1098. CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
  1099. CTRL_CMD_ETYPE_WCLR | CTRL_CMD_REG(csring->events[!evt]);
  1100. CS_DESC(csring, fill++) = 0;
  1101. csring->next_to_fill = fill & (CS_RING_SIZE-1);
  1102. cs_size = fill - hdr;
  1103. write_dma_reg(PAS_DMA_TXCHAN_INCR(csring->chan.chno), (cs_size) >> 1);
  1104. /* TX-side event handshaking */
  1105. fill = txring->next_to_fill;
  1106. TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
  1107. CTRL_CMD_ETYPE_WSET | CTRL_CMD_REG(csring->events[evt]);
  1108. TX_DESC(txring, fill++) = 0;
  1109. TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
  1110. CTRL_CMD_ETYPE_CLR | CTRL_CMD_REG(csring->events[!evt]);
  1111. TX_DESC(txring, fill++) = 0;
  1112. txring->next_to_fill = fill;
  1113. write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), 2);
  1114. }
  1115. static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
  1116. {
  1117. struct pasemi_mac * const mac = netdev_priv(dev);
  1118. struct pasemi_mac_txring * const txring = tx_ring(mac);
  1119. struct pasemi_mac_csring *csring;
  1120. u64 dflags = 0;
  1121. u64 mactx;
  1122. dma_addr_t map[MAX_SKB_FRAGS+1];
  1123. unsigned int map_size[MAX_SKB_FRAGS+1];
  1124. unsigned long flags;
  1125. int i, nfrags;
  1126. int fill;
  1127. const int nh_off = skb_network_offset(skb);
  1128. const int nh_len = skb_network_header_len(skb);
  1129. prefetch(&txring->ring_info);
  1130. dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
  1131. nfrags = skb_shinfo(skb)->nr_frags;
  1132. map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
  1133. PCI_DMA_TODEVICE);
  1134. map_size[0] = skb_headlen(skb);
  1135. if (pci_dma_mapping_error(mac->dma_pdev, map[0]))
  1136. goto out_err_nolock;
  1137. for (i = 0; i < nfrags; i++) {
  1138. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1139. map[i + 1] = skb_frag_dma_map(&mac->dma_pdev->dev, frag, 0,
  1140. skb_frag_size(frag), DMA_TO_DEVICE);
  1141. map_size[i+1] = skb_frag_size(frag);
  1142. if (dma_mapping_error(&mac->dma_pdev->dev, map[i + 1])) {
  1143. nfrags = i;
  1144. goto out_err_nolock;
  1145. }
  1146. }
  1147. if (skb->ip_summed == CHECKSUM_PARTIAL && skb->len <= 1540) {
  1148. switch (ip_hdr(skb)->protocol) {
  1149. case IPPROTO_TCP:
  1150. dflags |= XCT_MACTX_CSUM_TCP;
  1151. dflags |= XCT_MACTX_IPH(nh_len >> 2);
  1152. dflags |= XCT_MACTX_IPO(nh_off);
  1153. break;
  1154. case IPPROTO_UDP:
  1155. dflags |= XCT_MACTX_CSUM_UDP;
  1156. dflags |= XCT_MACTX_IPH(nh_len >> 2);
  1157. dflags |= XCT_MACTX_IPO(nh_off);
  1158. break;
  1159. default:
  1160. WARN_ON(1);
  1161. }
  1162. }
  1163. mactx = dflags | XCT_MACTX_LLEN(skb->len);
  1164. spin_lock_irqsave(&txring->lock, flags);
  1165. /* Avoid stepping on the same cache line that the DMA controller
  1166. * is currently about to send, so leave at least 8 words available.
  1167. * Total free space needed is mactx + fragments + 8
  1168. */
  1169. if (RING_AVAIL(txring) < nfrags + 14) {
  1170. /* no room -- stop the queue and wait for tx intr */
  1171. netif_stop_queue(dev);
  1172. goto out_err;
  1173. }
  1174. /* Queue up checksum + event descriptors, if needed */
  1175. if (mac->num_cs && skb->ip_summed == CHECKSUM_PARTIAL && skb->len > 1540) {
  1176. csring = mac->cs[mac->last_cs];
  1177. mac->last_cs = (mac->last_cs + 1) % mac->num_cs;
  1178. pasemi_mac_queue_csdesc(skb, map, map_size, txring, csring);
  1179. }
  1180. fill = txring->next_to_fill;
  1181. TX_DESC(txring, fill) = mactx;
  1182. TX_DESC_INFO(txring, fill).dma = nfrags;
  1183. fill++;
  1184. TX_DESC_INFO(txring, fill).skb = skb;
  1185. for (i = 0; i <= nfrags; i++) {
  1186. TX_DESC(txring, fill+i) =
  1187. XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
  1188. TX_DESC_INFO(txring, fill+i).dma = map[i];
  1189. }
  1190. /* We have to add an even number of 8-byte entries to the ring
  1191. * even if the last one is unused. That means always an odd number
  1192. * of pointers + one mactx descriptor.
  1193. */
  1194. if (nfrags & 1)
  1195. nfrags++;
  1196. txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
  1197. dev->stats.tx_packets++;
  1198. dev->stats.tx_bytes += skb->len;
  1199. spin_unlock_irqrestore(&txring->lock, flags);
  1200. write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
  1201. return NETDEV_TX_OK;
  1202. out_err:
  1203. spin_unlock_irqrestore(&txring->lock, flags);
  1204. out_err_nolock:
  1205. while (nfrags--)
  1206. pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
  1207. PCI_DMA_TODEVICE);
  1208. return NETDEV_TX_BUSY;
  1209. }
  1210. static void pasemi_mac_set_rx_mode(struct net_device *dev)
  1211. {
  1212. const struct pasemi_mac *mac = netdev_priv(dev);
  1213. unsigned int flags;
  1214. flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
  1215. /* Set promiscuous */
  1216. if (dev->flags & IFF_PROMISC)
  1217. flags |= PAS_MAC_CFG_PCFG_PR;
  1218. else
  1219. flags &= ~PAS_MAC_CFG_PCFG_PR;
  1220. write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
  1221. }
  1222. static int pasemi_mac_poll(struct napi_struct *napi, int budget)
  1223. {
  1224. struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
  1225. int pkts;
  1226. pasemi_mac_clean_tx(tx_ring(mac));
  1227. pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
  1228. if (pkts < budget) {
  1229. /* all done, no more packets present */
  1230. napi_complete_done(napi, pkts);
  1231. pasemi_mac_restart_rx_intr(mac);
  1232. pasemi_mac_restart_tx_intr(mac);
  1233. }
  1234. return pkts;
  1235. }
  1236. #ifdef CONFIG_NET_POLL_CONTROLLER
  1237. /*
  1238. * Polling 'interrupt' - used by things like netconsole to send skbs
  1239. * without having to re-enable interrupts. It's not called while
  1240. * the interrupt routine is executing.
  1241. */
  1242. static void pasemi_mac_netpoll(struct net_device *dev)
  1243. {
  1244. const struct pasemi_mac *mac = netdev_priv(dev);
  1245. disable_irq(mac->tx->chan.irq);
  1246. pasemi_mac_tx_intr(mac->tx->chan.irq, mac->tx);
  1247. enable_irq(mac->tx->chan.irq);
  1248. disable_irq(mac->rx->chan.irq);
  1249. pasemi_mac_rx_intr(mac->rx->chan.irq, mac->rx);
  1250. enable_irq(mac->rx->chan.irq);
  1251. }
  1252. #endif
  1253. static int pasemi_mac_change_mtu(struct net_device *dev, int new_mtu)
  1254. {
  1255. struct pasemi_mac *mac = netdev_priv(dev);
  1256. unsigned int reg;
  1257. unsigned int rcmdsta = 0;
  1258. int running;
  1259. int ret = 0;
  1260. running = netif_running(dev);
  1261. if (running) {
  1262. /* Need to stop the interface, clean out all already
  1263. * received buffers, free all unused buffers on the RX
  1264. * interface ring, then finally re-fill the rx ring with
  1265. * the new-size buffers and restart.
  1266. */
  1267. napi_disable(&mac->napi);
  1268. netif_tx_disable(dev);
  1269. pasemi_mac_intf_disable(mac);
  1270. rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
  1271. pasemi_mac_pause_rxint(mac);
  1272. pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
  1273. pasemi_mac_free_rx_buffers(mac);
  1274. }
  1275. /* Setup checksum channels if large MTU and none already allocated */
  1276. if (new_mtu > PE_DEF_MTU && !mac->num_cs) {
  1277. pasemi_mac_setup_csrings(mac);
  1278. if (!mac->num_cs) {
  1279. ret = -ENOMEM;
  1280. goto out;
  1281. }
  1282. }
  1283. /* Change maxf, i.e. what size frames are accepted.
  1284. * Need room for ethernet header and CRC word
  1285. */
  1286. reg = read_mac_reg(mac, PAS_MAC_CFG_MACCFG);
  1287. reg &= ~PAS_MAC_CFG_MACCFG_MAXF_M;
  1288. reg |= PAS_MAC_CFG_MACCFG_MAXF(new_mtu + ETH_HLEN + 4);
  1289. write_mac_reg(mac, PAS_MAC_CFG_MACCFG, reg);
  1290. dev->mtu = new_mtu;
  1291. /* MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
  1292. mac->bufsz = new_mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
  1293. out:
  1294. if (running) {
  1295. write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
  1296. rcmdsta | PAS_DMA_RXINT_RCMDSTA_EN);
  1297. rx_ring(mac)->next_to_fill = 0;
  1298. pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE-1);
  1299. napi_enable(&mac->napi);
  1300. netif_start_queue(dev);
  1301. pasemi_mac_intf_enable(mac);
  1302. }
  1303. return ret;
  1304. }
  1305. static const struct net_device_ops pasemi_netdev_ops = {
  1306. .ndo_open = pasemi_mac_open,
  1307. .ndo_stop = pasemi_mac_close,
  1308. .ndo_start_xmit = pasemi_mac_start_tx,
  1309. .ndo_set_rx_mode = pasemi_mac_set_rx_mode,
  1310. .ndo_set_mac_address = pasemi_mac_set_mac_addr,
  1311. .ndo_change_mtu = pasemi_mac_change_mtu,
  1312. .ndo_validate_addr = eth_validate_addr,
  1313. #ifdef CONFIG_NET_POLL_CONTROLLER
  1314. .ndo_poll_controller = pasemi_mac_netpoll,
  1315. #endif
  1316. };
  1317. static int
  1318. pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1319. {
  1320. struct net_device *dev;
  1321. struct pasemi_mac *mac;
  1322. int err, ret;
  1323. err = pci_enable_device(pdev);
  1324. if (err)
  1325. return err;
  1326. dev = alloc_etherdev(sizeof(struct pasemi_mac));
  1327. if (dev == NULL) {
  1328. err = -ENOMEM;
  1329. goto out_disable_device;
  1330. }
  1331. pci_set_drvdata(pdev, dev);
  1332. SET_NETDEV_DEV(dev, &pdev->dev);
  1333. mac = netdev_priv(dev);
  1334. mac->pdev = pdev;
  1335. mac->netdev = dev;
  1336. netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
  1337. dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
  1338. NETIF_F_HIGHDMA | NETIF_F_GSO;
  1339. mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
  1340. if (!mac->dma_pdev) {
  1341. dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
  1342. err = -ENODEV;
  1343. goto out;
  1344. }
  1345. mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
  1346. if (!mac->iob_pdev) {
  1347. dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
  1348. err = -ENODEV;
  1349. goto out;
  1350. }
  1351. /* get mac addr from device tree */
  1352. if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
  1353. err = -ENODEV;
  1354. goto out;
  1355. }
  1356. memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
  1357. ret = mac_to_intf(mac);
  1358. if (ret < 0) {
  1359. dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
  1360. err = -ENODEV;
  1361. goto out;
  1362. }
  1363. mac->dma_if = ret;
  1364. switch (pdev->device) {
  1365. case 0xa005:
  1366. mac->type = MAC_TYPE_GMAC;
  1367. break;
  1368. case 0xa006:
  1369. mac->type = MAC_TYPE_XAUI;
  1370. break;
  1371. default:
  1372. err = -ENODEV;
  1373. goto out;
  1374. }
  1375. dev->netdev_ops = &pasemi_netdev_ops;
  1376. dev->mtu = PE_DEF_MTU;
  1377. /* MTU range: 64 - 9000 */
  1378. dev->min_mtu = PE_MIN_MTU;
  1379. dev->max_mtu = PE_MAX_MTU;
  1380. /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
  1381. mac->bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
  1382. dev->ethtool_ops = &pasemi_mac_ethtool_ops;
  1383. if (err)
  1384. goto out;
  1385. mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  1386. /* Enable most messages by default */
  1387. mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  1388. err = register_netdev(dev);
  1389. if (err) {
  1390. dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
  1391. err);
  1392. goto out;
  1393. } else if (netif_msg_probe(mac)) {
  1394. printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %pM\n",
  1395. dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
  1396. mac->dma_if, dev->dev_addr);
  1397. }
  1398. return err;
  1399. out:
  1400. pci_dev_put(mac->iob_pdev);
  1401. pci_dev_put(mac->dma_pdev);
  1402. free_netdev(dev);
  1403. out_disable_device:
  1404. pci_disable_device(pdev);
  1405. return err;
  1406. }
  1407. static void pasemi_mac_remove(struct pci_dev *pdev)
  1408. {
  1409. struct net_device *netdev = pci_get_drvdata(pdev);
  1410. struct pasemi_mac *mac;
  1411. if (!netdev)
  1412. return;
  1413. mac = netdev_priv(netdev);
  1414. unregister_netdev(netdev);
  1415. pci_disable_device(pdev);
  1416. pci_dev_put(mac->dma_pdev);
  1417. pci_dev_put(mac->iob_pdev);
  1418. pasemi_dma_free_chan(&mac->tx->chan);
  1419. pasemi_dma_free_chan(&mac->rx->chan);
  1420. free_netdev(netdev);
  1421. }
  1422. static const struct pci_device_id pasemi_mac_pci_tbl[] = {
  1423. { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
  1424. { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
  1425. { },
  1426. };
  1427. MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
  1428. static struct pci_driver pasemi_mac_driver = {
  1429. .name = "pasemi_mac",
  1430. .id_table = pasemi_mac_pci_tbl,
  1431. .probe = pasemi_mac_probe,
  1432. .remove = pasemi_mac_remove,
  1433. };
  1434. static void __exit pasemi_mac_cleanup_module(void)
  1435. {
  1436. pci_unregister_driver(&pasemi_mac_driver);
  1437. }
  1438. int pasemi_mac_init_module(void)
  1439. {
  1440. int err;
  1441. err = pasemi_dma_init();
  1442. if (err)
  1443. return err;
  1444. return pci_register_driver(&pasemi_mac_driver);
  1445. }
  1446. module_init(pasemi_mac_init_module);
  1447. module_exit(pasemi_mac_cleanup_module);