pasemi_mac.c 47 KB

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