pasemi_mac.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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_adjust_link(struct net_device *dev)
  671. {
  672. struct pasemi_mac *mac = netdev_priv(dev);
  673. int msg;
  674. unsigned int flags;
  675. unsigned int new_flags;
  676. if (!mac->phydev->link) {
  677. /* If no link, MAC speed settings don't matter. Just report
  678. * link down and return.
  679. */
  680. if (mac->link && netif_msg_link(mac))
  681. printk(KERN_INFO "%s: Link is down.\n", dev->name);
  682. netif_carrier_off(dev);
  683. mac->link = 0;
  684. return;
  685. } else
  686. netif_carrier_on(dev);
  687. flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
  688. new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
  689. PAS_MAC_CFG_PCFG_TSR_M);
  690. if (!mac->phydev->duplex)
  691. new_flags |= PAS_MAC_CFG_PCFG_HD;
  692. switch (mac->phydev->speed) {
  693. case 1000:
  694. new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
  695. PAS_MAC_CFG_PCFG_TSR_1G;
  696. break;
  697. case 100:
  698. new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
  699. PAS_MAC_CFG_PCFG_TSR_100M;
  700. break;
  701. case 10:
  702. new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
  703. PAS_MAC_CFG_PCFG_TSR_10M;
  704. break;
  705. default:
  706. printk("Unsupported speed %d\n", mac->phydev->speed);
  707. }
  708. /* Print on link or speed/duplex change */
  709. msg = mac->link != mac->phydev->link || flags != new_flags;
  710. mac->duplex = mac->phydev->duplex;
  711. mac->speed = mac->phydev->speed;
  712. mac->link = mac->phydev->link;
  713. if (new_flags != flags)
  714. write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
  715. if (msg && netif_msg_link(mac))
  716. printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
  717. dev->name, mac->speed, mac->duplex ? "full" : "half");
  718. }
  719. static int pasemi_mac_phy_init(struct net_device *dev)
  720. {
  721. struct pasemi_mac *mac = netdev_priv(dev);
  722. struct device_node *dn, *phy_dn;
  723. struct phy_device *phydev;
  724. unsigned int phy_id;
  725. const phandle *ph;
  726. const unsigned int *prop;
  727. struct resource r;
  728. int ret;
  729. dn = pci_device_to_OF_node(mac->pdev);
  730. ph = of_get_property(dn, "phy-handle", NULL);
  731. if (!ph)
  732. return -ENODEV;
  733. phy_dn = of_find_node_by_phandle(*ph);
  734. prop = of_get_property(phy_dn, "reg", NULL);
  735. ret = of_address_to_resource(phy_dn->parent, 0, &r);
  736. if (ret)
  737. goto err;
  738. phy_id = *prop;
  739. snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
  740. of_node_put(phy_dn);
  741. mac->link = 0;
  742. mac->speed = 0;
  743. mac->duplex = -1;
  744. phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
  745. if (IS_ERR(phydev)) {
  746. printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
  747. return PTR_ERR(phydev);
  748. }
  749. mac->phydev = phydev;
  750. return 0;
  751. err:
  752. of_node_put(phy_dn);
  753. return -ENODEV;
  754. }
  755. static int pasemi_mac_open(struct net_device *dev)
  756. {
  757. struct pasemi_mac *mac = netdev_priv(dev);
  758. unsigned int flags;
  759. int ret;
  760. /* enable rx section */
  761. write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
  762. /* enable tx section */
  763. write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
  764. flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
  765. PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
  766. PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
  767. write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
  768. ret = pasemi_mac_setup_rx_resources(dev);
  769. if (ret)
  770. goto out_rx_resources;
  771. mac->tx = pasemi_mac_setup_tx_resources(dev);
  772. if (!mac->tx)
  773. goto out_tx_ring;
  774. /* 0x3ff with 33MHz clock is about 31us */
  775. write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
  776. PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
  777. write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
  778. PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
  779. write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
  780. PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
  781. write_mac_reg(mac, PAS_MAC_IPC_CHNL,
  782. PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
  783. PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
  784. /* enable rx if */
  785. write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
  786. PAS_DMA_RXINT_RCMDSTA_EN |
  787. PAS_DMA_RXINT_RCMDSTA_DROPS_M |
  788. PAS_DMA_RXINT_RCMDSTA_BP |
  789. PAS_DMA_RXINT_RCMDSTA_OO |
  790. PAS_DMA_RXINT_RCMDSTA_BT);
  791. /* enable rx channel */
  792. pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
  793. PAS_DMA_RXCHAN_CCMDSTA_OD |
  794. PAS_DMA_RXCHAN_CCMDSTA_FD |
  795. PAS_DMA_RXCHAN_CCMDSTA_DT);
  796. /* enable tx channel */
  797. pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
  798. PAS_DMA_TXCHAN_TCMDSTA_DB |
  799. PAS_DMA_TXCHAN_TCMDSTA_DE |
  800. PAS_DMA_TXCHAN_TCMDSTA_DA);
  801. pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
  802. write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
  803. RX_RING_SIZE>>1);
  804. /* Clear out any residual packet count state from firmware */
  805. pasemi_mac_restart_rx_intr(mac);
  806. pasemi_mac_restart_tx_intr(mac);
  807. flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
  808. PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
  809. if (mac->type == MAC_TYPE_GMAC)
  810. flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
  811. else
  812. flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
  813. /* Enable interface in MAC */
  814. write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
  815. ret = pasemi_mac_phy_init(dev);
  816. /* Some configs don't have PHYs (XAUI etc), so don't complain about
  817. * failed init due to -ENODEV.
  818. */
  819. if (ret && ret != -ENODEV)
  820. dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
  821. netif_start_queue(dev);
  822. napi_enable(&mac->napi);
  823. snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
  824. dev->name);
  825. ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
  826. mac->tx_irq_name, mac->tx);
  827. if (ret) {
  828. dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
  829. mac->tx->chan.irq, ret);
  830. goto out_tx_int;
  831. }
  832. snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
  833. dev->name);
  834. ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
  835. mac->rx_irq_name, mac->rx);
  836. if (ret) {
  837. dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
  838. mac->rx->chan.irq, ret);
  839. goto out_rx_int;
  840. }
  841. if (mac->phydev)
  842. phy_start(mac->phydev);
  843. init_timer(&mac->tx->clean_timer);
  844. mac->tx->clean_timer.function = pasemi_mac_tx_timer;
  845. mac->tx->clean_timer.data = (unsigned long)mac->tx;
  846. mac->tx->clean_timer.expires = jiffies+HZ;
  847. add_timer(&mac->tx->clean_timer);
  848. return 0;
  849. out_rx_int:
  850. free_irq(mac->tx->chan.irq, mac->tx);
  851. out_tx_int:
  852. napi_disable(&mac->napi);
  853. netif_stop_queue(dev);
  854. out_tx_ring:
  855. if (mac->tx)
  856. pasemi_mac_free_tx_resources(mac);
  857. pasemi_mac_free_rx_resources(mac);
  858. out_rx_resources:
  859. return ret;
  860. }
  861. #define MAX_RETRIES 5000
  862. static int pasemi_mac_close(struct net_device *dev)
  863. {
  864. struct pasemi_mac *mac = netdev_priv(dev);
  865. unsigned int sta;
  866. int retries;
  867. int rxch, txch;
  868. rxch = rx_ring(mac)->chan.chno;
  869. txch = tx_ring(mac)->chan.chno;
  870. if (mac->phydev) {
  871. phy_stop(mac->phydev);
  872. phy_disconnect(mac->phydev);
  873. }
  874. del_timer_sync(&mac->tx->clean_timer);
  875. netif_stop_queue(dev);
  876. napi_disable(&mac->napi);
  877. sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
  878. if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
  879. PAS_DMA_RXINT_RCMDSTA_OO |
  880. PAS_DMA_RXINT_RCMDSTA_BT))
  881. printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
  882. sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
  883. if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
  884. PAS_DMA_RXCHAN_CCMDSTA_OD |
  885. PAS_DMA_RXCHAN_CCMDSTA_FD |
  886. PAS_DMA_RXCHAN_CCMDSTA_DT))
  887. printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
  888. sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
  889. if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
  890. PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
  891. printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
  892. /* Clean out any pending buffers */
  893. pasemi_mac_clean_tx(tx_ring(mac));
  894. pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
  895. /* Disable interface */
  896. write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
  897. PAS_DMA_TXCHAN_TCMDSTA_ST);
  898. write_dma_reg( PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
  899. PAS_DMA_RXINT_RCMDSTA_ST);
  900. write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
  901. PAS_DMA_RXCHAN_CCMDSTA_ST);
  902. for (retries = 0; retries < MAX_RETRIES; retries++) {
  903. sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(rxch));
  904. if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
  905. break;
  906. cond_resched();
  907. }
  908. if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
  909. dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
  910. for (retries = 0; retries < MAX_RETRIES; retries++) {
  911. sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
  912. if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
  913. break;
  914. cond_resched();
  915. }
  916. if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
  917. dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
  918. for (retries = 0; retries < MAX_RETRIES; retries++) {
  919. sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
  920. if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
  921. break;
  922. cond_resched();
  923. }
  924. if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
  925. dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
  926. /* Then, disable the channel. This must be done separately from
  927. * stopping, since you can't disable when active.
  928. */
  929. write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
  930. write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
  931. write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
  932. free_irq(mac->tx->chan.irq, mac->tx);
  933. free_irq(mac->rx->chan.irq, mac->rx);
  934. /* Free resources */
  935. pasemi_mac_free_rx_resources(mac);
  936. pasemi_mac_free_tx_resources(mac);
  937. return 0;
  938. }
  939. static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
  940. {
  941. struct pasemi_mac *mac = netdev_priv(dev);
  942. struct pasemi_mac_txring *txring;
  943. u64 dflags, mactx;
  944. dma_addr_t map[MAX_SKB_FRAGS+1];
  945. unsigned int map_size[MAX_SKB_FRAGS+1];
  946. unsigned long flags;
  947. int i, nfrags;
  948. int fill;
  949. dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
  950. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  951. const unsigned char *nh = skb_network_header(skb);
  952. switch (ip_hdr(skb)->protocol) {
  953. case IPPROTO_TCP:
  954. dflags |= XCT_MACTX_CSUM_TCP;
  955. dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
  956. dflags |= XCT_MACTX_IPO(nh - skb->data);
  957. break;
  958. case IPPROTO_UDP:
  959. dflags |= XCT_MACTX_CSUM_UDP;
  960. dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
  961. dflags |= XCT_MACTX_IPO(nh - skb->data);
  962. break;
  963. }
  964. }
  965. nfrags = skb_shinfo(skb)->nr_frags;
  966. map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
  967. PCI_DMA_TODEVICE);
  968. map_size[0] = skb_headlen(skb);
  969. if (dma_mapping_error(map[0]))
  970. goto out_err_nolock;
  971. for (i = 0; i < nfrags; i++) {
  972. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  973. map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
  974. frag->page_offset, frag->size,
  975. PCI_DMA_TODEVICE);
  976. map_size[i+1] = frag->size;
  977. if (dma_mapping_error(map[i+1])) {
  978. nfrags = i;
  979. goto out_err_nolock;
  980. }
  981. }
  982. mactx = dflags | XCT_MACTX_LLEN(skb->len);
  983. txring = tx_ring(mac);
  984. spin_lock_irqsave(&txring->lock, flags);
  985. fill = txring->next_to_fill;
  986. /* Avoid stepping on the same cache line that the DMA controller
  987. * is currently about to send, so leave at least 8 words available.
  988. * Total free space needed is mactx + fragments + 8
  989. */
  990. if (RING_AVAIL(txring) < nfrags + 10) {
  991. /* no room -- stop the queue and wait for tx intr */
  992. netif_stop_queue(dev);
  993. goto out_err;
  994. }
  995. TX_DESC(txring, fill) = mactx;
  996. TX_DESC_INFO(txring, fill).dma = nfrags;
  997. fill++;
  998. TX_DESC_INFO(txring, fill).skb = skb;
  999. for (i = 0; i <= nfrags; i++) {
  1000. TX_DESC(txring, fill+i) =
  1001. XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
  1002. TX_DESC_INFO(txring, fill+i).dma = map[i];
  1003. }
  1004. /* We have to add an even number of 8-byte entries to the ring
  1005. * even if the last one is unused. That means always an odd number
  1006. * of pointers + one mactx descriptor.
  1007. */
  1008. if (nfrags & 1)
  1009. nfrags++;
  1010. txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
  1011. dev->stats.tx_packets++;
  1012. dev->stats.tx_bytes += skb->len;
  1013. spin_unlock_irqrestore(&txring->lock, flags);
  1014. write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
  1015. return NETDEV_TX_OK;
  1016. out_err:
  1017. spin_unlock_irqrestore(&txring->lock, flags);
  1018. out_err_nolock:
  1019. while (nfrags--)
  1020. pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
  1021. PCI_DMA_TODEVICE);
  1022. return NETDEV_TX_BUSY;
  1023. }
  1024. static void pasemi_mac_set_rx_mode(struct net_device *dev)
  1025. {
  1026. const struct pasemi_mac *mac = netdev_priv(dev);
  1027. unsigned int flags;
  1028. flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
  1029. /* Set promiscuous */
  1030. if (dev->flags & IFF_PROMISC)
  1031. flags |= PAS_MAC_CFG_PCFG_PR;
  1032. else
  1033. flags &= ~PAS_MAC_CFG_PCFG_PR;
  1034. write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
  1035. }
  1036. static int pasemi_mac_poll(struct napi_struct *napi, int budget)
  1037. {
  1038. struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
  1039. struct net_device *dev = mac->netdev;
  1040. int pkts;
  1041. pasemi_mac_clean_tx(tx_ring(mac));
  1042. pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
  1043. if (pkts < budget) {
  1044. /* all done, no more packets present */
  1045. netif_rx_complete(dev, napi);
  1046. pasemi_mac_restart_rx_intr(mac);
  1047. pasemi_mac_restart_tx_intr(mac);
  1048. }
  1049. return pkts;
  1050. }
  1051. static int __devinit
  1052. pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1053. {
  1054. struct net_device *dev;
  1055. struct pasemi_mac *mac;
  1056. int err;
  1057. DECLARE_MAC_BUF(mac_buf);
  1058. err = pci_enable_device(pdev);
  1059. if (err)
  1060. return err;
  1061. dev = alloc_etherdev(sizeof(struct pasemi_mac));
  1062. if (dev == NULL) {
  1063. dev_err(&pdev->dev,
  1064. "pasemi_mac: Could not allocate ethernet device.\n");
  1065. err = -ENOMEM;
  1066. goto out_disable_device;
  1067. }
  1068. pci_set_drvdata(pdev, dev);
  1069. SET_NETDEV_DEV(dev, &pdev->dev);
  1070. mac = netdev_priv(dev);
  1071. mac->pdev = pdev;
  1072. mac->netdev = dev;
  1073. netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
  1074. dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
  1075. NETIF_F_HIGHDMA;
  1076. mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
  1077. mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
  1078. mac->lro_mgr.lro_arr = mac->lro_desc;
  1079. mac->lro_mgr.get_skb_header = get_skb_hdr;
  1080. mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
  1081. mac->lro_mgr.dev = mac->netdev;
  1082. mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
  1083. mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
  1084. mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
  1085. if (!mac->dma_pdev) {
  1086. dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
  1087. err = -ENODEV;
  1088. goto out;
  1089. }
  1090. mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
  1091. if (!mac->iob_pdev) {
  1092. dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
  1093. err = -ENODEV;
  1094. goto out;
  1095. }
  1096. /* get mac addr from device tree */
  1097. if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
  1098. err = -ENODEV;
  1099. goto out;
  1100. }
  1101. memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
  1102. mac->dma_if = mac_to_intf(mac);
  1103. if (mac->dma_if < 0) {
  1104. dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
  1105. err = -ENODEV;
  1106. goto out;
  1107. }
  1108. switch (pdev->device) {
  1109. case 0xa005:
  1110. mac->type = MAC_TYPE_GMAC;
  1111. break;
  1112. case 0xa006:
  1113. mac->type = MAC_TYPE_XAUI;
  1114. break;
  1115. default:
  1116. err = -ENODEV;
  1117. goto out;
  1118. }
  1119. dev->open = pasemi_mac_open;
  1120. dev->stop = pasemi_mac_close;
  1121. dev->hard_start_xmit = pasemi_mac_start_tx;
  1122. dev->set_multicast_list = pasemi_mac_set_rx_mode;
  1123. if (err)
  1124. goto out;
  1125. mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  1126. /* Enable most messages by default */
  1127. mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  1128. err = register_netdev(dev);
  1129. if (err) {
  1130. dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
  1131. err);
  1132. goto out;
  1133. } else if netif_msg_probe(mac)
  1134. printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
  1135. dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
  1136. mac->dma_if, print_mac(mac_buf, dev->dev_addr));
  1137. return err;
  1138. out:
  1139. if (mac->iob_pdev)
  1140. pci_dev_put(mac->iob_pdev);
  1141. if (mac->dma_pdev)
  1142. pci_dev_put(mac->dma_pdev);
  1143. free_netdev(dev);
  1144. out_disable_device:
  1145. pci_disable_device(pdev);
  1146. return err;
  1147. }
  1148. static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
  1149. {
  1150. struct net_device *netdev = pci_get_drvdata(pdev);
  1151. struct pasemi_mac *mac;
  1152. if (!netdev)
  1153. return;
  1154. mac = netdev_priv(netdev);
  1155. unregister_netdev(netdev);
  1156. pci_disable_device(pdev);
  1157. pci_dev_put(mac->dma_pdev);
  1158. pci_dev_put(mac->iob_pdev);
  1159. pasemi_dma_free_chan(&mac->tx->chan);
  1160. pasemi_dma_free_chan(&mac->rx->chan);
  1161. pci_set_drvdata(pdev, NULL);
  1162. free_netdev(netdev);
  1163. }
  1164. static struct pci_device_id pasemi_mac_pci_tbl[] = {
  1165. { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
  1166. { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
  1167. { },
  1168. };
  1169. MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
  1170. static struct pci_driver pasemi_mac_driver = {
  1171. .name = "pasemi_mac",
  1172. .id_table = pasemi_mac_pci_tbl,
  1173. .probe = pasemi_mac_probe,
  1174. .remove = __devexit_p(pasemi_mac_remove),
  1175. };
  1176. static void __exit pasemi_mac_cleanup_module(void)
  1177. {
  1178. pci_unregister_driver(&pasemi_mac_driver);
  1179. }
  1180. int pasemi_mac_init_module(void)
  1181. {
  1182. int err;
  1183. err = pasemi_dma_init();
  1184. if (err)
  1185. return err;
  1186. return pci_register_driver(&pasemi_mac_driver);
  1187. }
  1188. module_init(pasemi_mac_init_module);
  1189. module_exit(pasemi_mac_cleanup_module);