pasemi_mac.c 41 KB

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