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