pasemi_mac.c 47 KB

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