pasemi_mac.c 48 KB

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