pasemi_mac.c 46 KB

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