pasemi_mac.c 47 KB

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