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