pasemi_mac.c 38 KB

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