pch_gbe_main.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746
  1. /*
  2. * Copyright (C) 1999 - 2010 Intel Corporation.
  3. * Copyright (C) 2010 - 2012 LAPIS SEMICONDUCTOR CO., LTD.
  4. *
  5. * This code was derived from the Intel e1000e Linux driver.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "pch_gbe.h"
  20. #include "pch_gbe_phy.h"
  21. #include <linux/module.h>
  22. #include <linux/net_tstamp.h>
  23. #include <linux/ptp_classify.h>
  24. #include <linux/gpio.h>
  25. #define DRV_VERSION "1.01"
  26. const char pch_driver_version[] = DRV_VERSION;
  27. #define PCI_DEVICE_ID_INTEL_IOH1_GBE 0x8802 /* Pci device ID */
  28. #define PCH_GBE_MAR_ENTRIES 16
  29. #define PCH_GBE_SHORT_PKT 64
  30. #define DSC_INIT16 0xC000
  31. #define PCH_GBE_DMA_ALIGN 0
  32. #define PCH_GBE_DMA_PADDING 2
  33. #define PCH_GBE_WATCHDOG_PERIOD (5 * HZ) /* watchdog time */
  34. #define PCH_GBE_PCI_BAR 1
  35. #define PCH_GBE_RESERVE_MEMORY 0x200000 /* 2MB */
  36. /* Macros for ML7223 */
  37. #define PCI_VENDOR_ID_ROHM 0x10db
  38. #define PCI_DEVICE_ID_ROHM_ML7223_GBE 0x8013
  39. /* Macros for ML7831 */
  40. #define PCI_DEVICE_ID_ROHM_ML7831_GBE 0x8802
  41. #define PCH_GBE_TX_WEIGHT 64
  42. #define PCH_GBE_RX_WEIGHT 64
  43. #define PCH_GBE_RX_BUFFER_WRITE 16
  44. /* Initialize the wake-on-LAN settings */
  45. #define PCH_GBE_WL_INIT_SETTING (PCH_GBE_WLC_MP)
  46. #define PCH_GBE_MAC_RGMII_CTRL_SETTING ( \
  47. PCH_GBE_CHIP_TYPE_INTERNAL | \
  48. PCH_GBE_RGMII_MODE_RGMII \
  49. )
  50. /* Ethertype field values */
  51. #define PCH_GBE_MAX_RX_BUFFER_SIZE 0x2880
  52. #define PCH_GBE_MAX_JUMBO_FRAME_SIZE 10318
  53. #define PCH_GBE_FRAME_SIZE_2048 2048
  54. #define PCH_GBE_FRAME_SIZE_4096 4096
  55. #define PCH_GBE_FRAME_SIZE_8192 8192
  56. #define PCH_GBE_GET_DESC(R, i, type) (&(((struct type *)((R).desc))[i]))
  57. #define PCH_GBE_RX_DESC(R, i) PCH_GBE_GET_DESC(R, i, pch_gbe_rx_desc)
  58. #define PCH_GBE_TX_DESC(R, i) PCH_GBE_GET_DESC(R, i, pch_gbe_tx_desc)
  59. #define PCH_GBE_DESC_UNUSED(R) \
  60. ((((R)->next_to_clean > (R)->next_to_use) ? 0 : (R)->count) + \
  61. (R)->next_to_clean - (R)->next_to_use - 1)
  62. /* Pause packet value */
  63. #define PCH_GBE_PAUSE_PKT1_VALUE 0x00C28001
  64. #define PCH_GBE_PAUSE_PKT2_VALUE 0x00000100
  65. #define PCH_GBE_PAUSE_PKT4_VALUE 0x01000888
  66. #define PCH_GBE_PAUSE_PKT5_VALUE 0x0000FFFF
  67. /* This defines the bits that are set in the Interrupt Mask
  68. * Set/Read Register. Each bit is documented below:
  69. * o RXT0 = Receiver Timer Interrupt (ring 0)
  70. * o TXDW = Transmit Descriptor Written Back
  71. * o RXDMT0 = Receive Descriptor Minimum Threshold hit (ring 0)
  72. * o RXSEQ = Receive Sequence Error
  73. * o LSC = Link Status Change
  74. */
  75. #define PCH_GBE_INT_ENABLE_MASK ( \
  76. PCH_GBE_INT_RX_DMA_CMPLT | \
  77. PCH_GBE_INT_RX_DSC_EMP | \
  78. PCH_GBE_INT_RX_FIFO_ERR | \
  79. PCH_GBE_INT_WOL_DET | \
  80. PCH_GBE_INT_TX_CMPLT \
  81. )
  82. #define PCH_GBE_INT_DISABLE_ALL 0
  83. /* Macros for ieee1588 */
  84. /* 0x40 Time Synchronization Channel Control Register Bits */
  85. #define MASTER_MODE (1<<0)
  86. #define SLAVE_MODE (0)
  87. #define V2_MODE (1<<31)
  88. #define CAP_MODE0 (0)
  89. #define CAP_MODE2 (1<<17)
  90. /* 0x44 Time Synchronization Channel Event Register Bits */
  91. #define TX_SNAPSHOT_LOCKED (1<<0)
  92. #define RX_SNAPSHOT_LOCKED (1<<1)
  93. #define PTP_L4_MULTICAST_SA "01:00:5e:00:01:81"
  94. #define PTP_L2_MULTICAST_SA "01:1b:19:00:00:00"
  95. #define MINNOW_PHY_RESET_GPIO 13
  96. static int pch_gbe_mdio_read(struct net_device *netdev, int addr, int reg);
  97. static void pch_gbe_mdio_write(struct net_device *netdev, int addr, int reg,
  98. int data);
  99. static void pch_gbe_set_multi(struct net_device *netdev);
  100. static int pch_ptp_match(struct sk_buff *skb, u16 uid_hi, u32 uid_lo, u16 seqid)
  101. {
  102. u8 *data = skb->data;
  103. unsigned int offset;
  104. u16 *hi, *id;
  105. u32 lo;
  106. if (ptp_classify_raw(skb) == PTP_CLASS_NONE)
  107. return 0;
  108. offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  109. if (skb->len < offset + OFF_PTP_SEQUENCE_ID + sizeof(seqid))
  110. return 0;
  111. hi = (u16 *)(data + offset + OFF_PTP_SOURCE_UUID);
  112. id = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  113. memcpy(&lo, &hi[1], sizeof(lo));
  114. return (uid_hi == *hi &&
  115. uid_lo == lo &&
  116. seqid == *id);
  117. }
  118. static void
  119. pch_rx_timestamp(struct pch_gbe_adapter *adapter, struct sk_buff *skb)
  120. {
  121. struct skb_shared_hwtstamps *shhwtstamps;
  122. struct pci_dev *pdev;
  123. u64 ns;
  124. u32 hi, lo, val;
  125. u16 uid, seq;
  126. if (!adapter->hwts_rx_en)
  127. return;
  128. /* Get ieee1588's dev information */
  129. pdev = adapter->ptp_pdev;
  130. val = pch_ch_event_read(pdev);
  131. if (!(val & RX_SNAPSHOT_LOCKED))
  132. return;
  133. lo = pch_src_uuid_lo_read(pdev);
  134. hi = pch_src_uuid_hi_read(pdev);
  135. uid = hi & 0xffff;
  136. seq = (hi >> 16) & 0xffff;
  137. if (!pch_ptp_match(skb, htons(uid), htonl(lo), htons(seq)))
  138. goto out;
  139. ns = pch_rx_snap_read(pdev);
  140. shhwtstamps = skb_hwtstamps(skb);
  141. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  142. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  143. out:
  144. pch_ch_event_write(pdev, RX_SNAPSHOT_LOCKED);
  145. }
  146. static void
  147. pch_tx_timestamp(struct pch_gbe_adapter *adapter, struct sk_buff *skb)
  148. {
  149. struct skb_shared_hwtstamps shhwtstamps;
  150. struct pci_dev *pdev;
  151. struct skb_shared_info *shtx;
  152. u64 ns;
  153. u32 cnt, val;
  154. shtx = skb_shinfo(skb);
  155. if (likely(!(shtx->tx_flags & SKBTX_HW_TSTAMP && adapter->hwts_tx_en)))
  156. return;
  157. shtx->tx_flags |= SKBTX_IN_PROGRESS;
  158. /* Get ieee1588's dev information */
  159. pdev = adapter->ptp_pdev;
  160. /*
  161. * This really stinks, but we have to poll for the Tx time stamp.
  162. */
  163. for (cnt = 0; cnt < 100; cnt++) {
  164. val = pch_ch_event_read(pdev);
  165. if (val & TX_SNAPSHOT_LOCKED)
  166. break;
  167. udelay(1);
  168. }
  169. if (!(val & TX_SNAPSHOT_LOCKED)) {
  170. shtx->tx_flags &= ~SKBTX_IN_PROGRESS;
  171. return;
  172. }
  173. ns = pch_tx_snap_read(pdev);
  174. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  175. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  176. skb_tstamp_tx(skb, &shhwtstamps);
  177. pch_ch_event_write(pdev, TX_SNAPSHOT_LOCKED);
  178. }
  179. static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  180. {
  181. struct hwtstamp_config cfg;
  182. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  183. struct pci_dev *pdev;
  184. u8 station[20];
  185. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  186. return -EFAULT;
  187. if (cfg.flags) /* reserved for future extensions */
  188. return -EINVAL;
  189. /* Get ieee1588's dev information */
  190. pdev = adapter->ptp_pdev;
  191. if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
  192. return -ERANGE;
  193. switch (cfg.rx_filter) {
  194. case HWTSTAMP_FILTER_NONE:
  195. adapter->hwts_rx_en = 0;
  196. break;
  197. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  198. adapter->hwts_rx_en = 0;
  199. pch_ch_control_write(pdev, SLAVE_MODE | CAP_MODE0);
  200. break;
  201. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  202. adapter->hwts_rx_en = 1;
  203. pch_ch_control_write(pdev, MASTER_MODE | CAP_MODE0);
  204. break;
  205. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  206. adapter->hwts_rx_en = 1;
  207. pch_ch_control_write(pdev, V2_MODE | CAP_MODE2);
  208. strcpy(station, PTP_L4_MULTICAST_SA);
  209. pch_set_station_address(station, pdev);
  210. break;
  211. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  212. adapter->hwts_rx_en = 1;
  213. pch_ch_control_write(pdev, V2_MODE | CAP_MODE2);
  214. strcpy(station, PTP_L2_MULTICAST_SA);
  215. pch_set_station_address(station, pdev);
  216. break;
  217. default:
  218. return -ERANGE;
  219. }
  220. adapter->hwts_tx_en = cfg.tx_type == HWTSTAMP_TX_ON;
  221. /* Clear out any old time stamps. */
  222. pch_ch_event_write(pdev, TX_SNAPSHOT_LOCKED | RX_SNAPSHOT_LOCKED);
  223. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  224. }
  225. static inline void pch_gbe_mac_load_mac_addr(struct pch_gbe_hw *hw)
  226. {
  227. iowrite32(0x01, &hw->reg->MAC_ADDR_LOAD);
  228. }
  229. /**
  230. * pch_gbe_mac_read_mac_addr - Read MAC address
  231. * @hw: Pointer to the HW structure
  232. * Returns:
  233. * 0: Successful.
  234. */
  235. static s32 pch_gbe_mac_read_mac_addr(struct pch_gbe_hw *hw)
  236. {
  237. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  238. u32 adr1a, adr1b;
  239. adr1a = ioread32(&hw->reg->mac_adr[0].high);
  240. adr1b = ioread32(&hw->reg->mac_adr[0].low);
  241. hw->mac.addr[0] = (u8)(adr1a & 0xFF);
  242. hw->mac.addr[1] = (u8)((adr1a >> 8) & 0xFF);
  243. hw->mac.addr[2] = (u8)((adr1a >> 16) & 0xFF);
  244. hw->mac.addr[3] = (u8)((adr1a >> 24) & 0xFF);
  245. hw->mac.addr[4] = (u8)(adr1b & 0xFF);
  246. hw->mac.addr[5] = (u8)((adr1b >> 8) & 0xFF);
  247. netdev_dbg(adapter->netdev, "hw->mac.addr : %pM\n", hw->mac.addr);
  248. return 0;
  249. }
  250. /**
  251. * pch_gbe_wait_clr_bit - Wait to clear a bit
  252. * @reg: Pointer of register
  253. * @busy: Busy bit
  254. */
  255. static void pch_gbe_wait_clr_bit(void *reg, u32 bit)
  256. {
  257. u32 tmp;
  258. /* wait busy */
  259. tmp = 1000;
  260. while ((ioread32(reg) & bit) && --tmp)
  261. cpu_relax();
  262. if (!tmp)
  263. pr_err("Error: busy bit is not cleared\n");
  264. }
  265. /**
  266. * pch_gbe_mac_mar_set - Set MAC address register
  267. * @hw: Pointer to the HW structure
  268. * @addr: Pointer to the MAC address
  269. * @index: MAC address array register
  270. */
  271. static void pch_gbe_mac_mar_set(struct pch_gbe_hw *hw, u8 * addr, u32 index)
  272. {
  273. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  274. u32 mar_low, mar_high, adrmask;
  275. netdev_dbg(adapter->netdev, "index : 0x%x\n", index);
  276. /*
  277. * HW expects these in little endian so we reverse the byte order
  278. * from network order (big endian) to little endian
  279. */
  280. mar_high = ((u32) addr[0] | ((u32) addr[1] << 8) |
  281. ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
  282. mar_low = ((u32) addr[4] | ((u32) addr[5] << 8));
  283. /* Stop the MAC Address of index. */
  284. adrmask = ioread32(&hw->reg->ADDR_MASK);
  285. iowrite32((adrmask | (0x0001 << index)), &hw->reg->ADDR_MASK);
  286. /* wait busy */
  287. pch_gbe_wait_clr_bit(&hw->reg->ADDR_MASK, PCH_GBE_BUSY);
  288. /* Set the MAC address to the MAC address 1A/1B register */
  289. iowrite32(mar_high, &hw->reg->mac_adr[index].high);
  290. iowrite32(mar_low, &hw->reg->mac_adr[index].low);
  291. /* Start the MAC address of index */
  292. iowrite32((adrmask & ~(0x0001 << index)), &hw->reg->ADDR_MASK);
  293. /* wait busy */
  294. pch_gbe_wait_clr_bit(&hw->reg->ADDR_MASK, PCH_GBE_BUSY);
  295. }
  296. /**
  297. * pch_gbe_mac_reset_hw - Reset hardware
  298. * @hw: Pointer to the HW structure
  299. */
  300. static void pch_gbe_mac_reset_hw(struct pch_gbe_hw *hw)
  301. {
  302. /* Read the MAC address. and store to the private data */
  303. pch_gbe_mac_read_mac_addr(hw);
  304. iowrite32(PCH_GBE_ALL_RST, &hw->reg->RESET);
  305. iowrite32(PCH_GBE_MODE_GMII_ETHER, &hw->reg->MODE);
  306. pch_gbe_wait_clr_bit(&hw->reg->RESET, PCH_GBE_ALL_RST);
  307. /* Setup the receive addresses */
  308. pch_gbe_mac_mar_set(hw, hw->mac.addr, 0);
  309. return;
  310. }
  311. static void pch_gbe_disable_mac_rx(struct pch_gbe_hw *hw)
  312. {
  313. u32 rctl;
  314. /* Disables Receive MAC */
  315. rctl = ioread32(&hw->reg->MAC_RX_EN);
  316. iowrite32((rctl & ~PCH_GBE_MRE_MAC_RX_EN), &hw->reg->MAC_RX_EN);
  317. }
  318. static void pch_gbe_enable_mac_rx(struct pch_gbe_hw *hw)
  319. {
  320. u32 rctl;
  321. /* Enables Receive MAC */
  322. rctl = ioread32(&hw->reg->MAC_RX_EN);
  323. iowrite32((rctl | PCH_GBE_MRE_MAC_RX_EN), &hw->reg->MAC_RX_EN);
  324. }
  325. /**
  326. * pch_gbe_mac_init_rx_addrs - Initialize receive address's
  327. * @hw: Pointer to the HW structure
  328. * @mar_count: Receive address registers
  329. */
  330. static void pch_gbe_mac_init_rx_addrs(struct pch_gbe_hw *hw, u16 mar_count)
  331. {
  332. u32 i;
  333. /* Setup the receive address */
  334. pch_gbe_mac_mar_set(hw, hw->mac.addr, 0);
  335. /* Zero out the other receive addresses */
  336. for (i = 1; i < mar_count; i++) {
  337. iowrite32(0, &hw->reg->mac_adr[i].high);
  338. iowrite32(0, &hw->reg->mac_adr[i].low);
  339. }
  340. iowrite32(0xFFFE, &hw->reg->ADDR_MASK);
  341. /* wait busy */
  342. pch_gbe_wait_clr_bit(&hw->reg->ADDR_MASK, PCH_GBE_BUSY);
  343. }
  344. /**
  345. * pch_gbe_mac_force_mac_fc - Force the MAC's flow control settings
  346. * @hw: Pointer to the HW structure
  347. * Returns:
  348. * 0: Successful.
  349. * Negative value: Failed.
  350. */
  351. s32 pch_gbe_mac_force_mac_fc(struct pch_gbe_hw *hw)
  352. {
  353. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  354. struct pch_gbe_mac_info *mac = &hw->mac;
  355. u32 rx_fctrl;
  356. netdev_dbg(adapter->netdev, "mac->fc = %u\n", mac->fc);
  357. rx_fctrl = ioread32(&hw->reg->RX_FCTRL);
  358. switch (mac->fc) {
  359. case PCH_GBE_FC_NONE:
  360. rx_fctrl &= ~PCH_GBE_FL_CTRL_EN;
  361. mac->tx_fc_enable = false;
  362. break;
  363. case PCH_GBE_FC_RX_PAUSE:
  364. rx_fctrl |= PCH_GBE_FL_CTRL_EN;
  365. mac->tx_fc_enable = false;
  366. break;
  367. case PCH_GBE_FC_TX_PAUSE:
  368. rx_fctrl &= ~PCH_GBE_FL_CTRL_EN;
  369. mac->tx_fc_enable = true;
  370. break;
  371. case PCH_GBE_FC_FULL:
  372. rx_fctrl |= PCH_GBE_FL_CTRL_EN;
  373. mac->tx_fc_enable = true;
  374. break;
  375. default:
  376. netdev_err(adapter->netdev,
  377. "Flow control param set incorrectly\n");
  378. return -EINVAL;
  379. }
  380. if (mac->link_duplex == DUPLEX_HALF)
  381. rx_fctrl &= ~PCH_GBE_FL_CTRL_EN;
  382. iowrite32(rx_fctrl, &hw->reg->RX_FCTRL);
  383. netdev_dbg(adapter->netdev,
  384. "RX_FCTRL reg : 0x%08x mac->tx_fc_enable : %d\n",
  385. ioread32(&hw->reg->RX_FCTRL), mac->tx_fc_enable);
  386. return 0;
  387. }
  388. /**
  389. * pch_gbe_mac_set_wol_event - Set wake-on-lan event
  390. * @hw: Pointer to the HW structure
  391. * @wu_evt: Wake up event
  392. */
  393. static void pch_gbe_mac_set_wol_event(struct pch_gbe_hw *hw, u32 wu_evt)
  394. {
  395. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  396. u32 addr_mask;
  397. netdev_dbg(adapter->netdev, "wu_evt : 0x%08x ADDR_MASK reg : 0x%08x\n",
  398. wu_evt, ioread32(&hw->reg->ADDR_MASK));
  399. if (wu_evt) {
  400. /* Set Wake-On-Lan address mask */
  401. addr_mask = ioread32(&hw->reg->ADDR_MASK);
  402. iowrite32(addr_mask, &hw->reg->WOL_ADDR_MASK);
  403. /* wait busy */
  404. pch_gbe_wait_clr_bit(&hw->reg->WOL_ADDR_MASK, PCH_GBE_WLA_BUSY);
  405. iowrite32(0, &hw->reg->WOL_ST);
  406. iowrite32((wu_evt | PCH_GBE_WLC_WOL_MODE), &hw->reg->WOL_CTRL);
  407. iowrite32(0x02, &hw->reg->TCPIP_ACC);
  408. iowrite32(PCH_GBE_INT_ENABLE_MASK, &hw->reg->INT_EN);
  409. } else {
  410. iowrite32(0, &hw->reg->WOL_CTRL);
  411. iowrite32(0, &hw->reg->WOL_ST);
  412. }
  413. return;
  414. }
  415. /**
  416. * pch_gbe_mac_ctrl_miim - Control MIIM interface
  417. * @hw: Pointer to the HW structure
  418. * @addr: Address of PHY
  419. * @dir: Operetion. (Write or Read)
  420. * @reg: Access register of PHY
  421. * @data: Write data.
  422. *
  423. * Returns: Read date.
  424. */
  425. u16 pch_gbe_mac_ctrl_miim(struct pch_gbe_hw *hw, u32 addr, u32 dir, u32 reg,
  426. u16 data)
  427. {
  428. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  429. u32 data_out = 0;
  430. unsigned int i;
  431. unsigned long flags;
  432. spin_lock_irqsave(&hw->miim_lock, flags);
  433. for (i = 100; i; --i) {
  434. if ((ioread32(&hw->reg->MIIM) & PCH_GBE_MIIM_OPER_READY))
  435. break;
  436. udelay(20);
  437. }
  438. if (i == 0) {
  439. netdev_err(adapter->netdev, "pch-gbe.miim won't go Ready\n");
  440. spin_unlock_irqrestore(&hw->miim_lock, flags);
  441. return 0; /* No way to indicate timeout error */
  442. }
  443. iowrite32(((reg << PCH_GBE_MIIM_REG_ADDR_SHIFT) |
  444. (addr << PCH_GBE_MIIM_PHY_ADDR_SHIFT) |
  445. dir | data), &hw->reg->MIIM);
  446. for (i = 0; i < 100; i++) {
  447. udelay(20);
  448. data_out = ioread32(&hw->reg->MIIM);
  449. if ((data_out & PCH_GBE_MIIM_OPER_READY))
  450. break;
  451. }
  452. spin_unlock_irqrestore(&hw->miim_lock, flags);
  453. netdev_dbg(adapter->netdev, "PHY %s: reg=%d, data=0x%04X\n",
  454. dir == PCH_GBE_MIIM_OPER_READ ? "READ" : "WRITE", reg,
  455. dir == PCH_GBE_MIIM_OPER_READ ? data_out : data);
  456. return (u16) data_out;
  457. }
  458. /**
  459. * pch_gbe_mac_set_pause_packet - Set pause packet
  460. * @hw: Pointer to the HW structure
  461. */
  462. static void pch_gbe_mac_set_pause_packet(struct pch_gbe_hw *hw)
  463. {
  464. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  465. unsigned long tmp2, tmp3;
  466. /* Set Pause packet */
  467. tmp2 = hw->mac.addr[1];
  468. tmp2 = (tmp2 << 8) | hw->mac.addr[0];
  469. tmp2 = PCH_GBE_PAUSE_PKT2_VALUE | (tmp2 << 16);
  470. tmp3 = hw->mac.addr[5];
  471. tmp3 = (tmp3 << 8) | hw->mac.addr[4];
  472. tmp3 = (tmp3 << 8) | hw->mac.addr[3];
  473. tmp3 = (tmp3 << 8) | hw->mac.addr[2];
  474. iowrite32(PCH_GBE_PAUSE_PKT1_VALUE, &hw->reg->PAUSE_PKT1);
  475. iowrite32(tmp2, &hw->reg->PAUSE_PKT2);
  476. iowrite32(tmp3, &hw->reg->PAUSE_PKT3);
  477. iowrite32(PCH_GBE_PAUSE_PKT4_VALUE, &hw->reg->PAUSE_PKT4);
  478. iowrite32(PCH_GBE_PAUSE_PKT5_VALUE, &hw->reg->PAUSE_PKT5);
  479. /* Transmit Pause Packet */
  480. iowrite32(PCH_GBE_PS_PKT_RQ, &hw->reg->PAUSE_REQ);
  481. netdev_dbg(adapter->netdev,
  482. "PAUSE_PKT1-5 reg : 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
  483. ioread32(&hw->reg->PAUSE_PKT1),
  484. ioread32(&hw->reg->PAUSE_PKT2),
  485. ioread32(&hw->reg->PAUSE_PKT3),
  486. ioread32(&hw->reg->PAUSE_PKT4),
  487. ioread32(&hw->reg->PAUSE_PKT5));
  488. return;
  489. }
  490. /**
  491. * pch_gbe_alloc_queues - Allocate memory for all rings
  492. * @adapter: Board private structure to initialize
  493. * Returns:
  494. * 0: Successfully
  495. * Negative value: Failed
  496. */
  497. static int pch_gbe_alloc_queues(struct pch_gbe_adapter *adapter)
  498. {
  499. adapter->tx_ring = devm_kzalloc(&adapter->pdev->dev,
  500. sizeof(*adapter->tx_ring), GFP_KERNEL);
  501. if (!adapter->tx_ring)
  502. return -ENOMEM;
  503. adapter->rx_ring = devm_kzalloc(&adapter->pdev->dev,
  504. sizeof(*adapter->rx_ring), GFP_KERNEL);
  505. if (!adapter->rx_ring)
  506. return -ENOMEM;
  507. return 0;
  508. }
  509. /**
  510. * pch_gbe_init_stats - Initialize status
  511. * @adapter: Board private structure to initialize
  512. */
  513. static void pch_gbe_init_stats(struct pch_gbe_adapter *adapter)
  514. {
  515. memset(&adapter->stats, 0, sizeof(adapter->stats));
  516. return;
  517. }
  518. /**
  519. * pch_gbe_init_phy - Initialize PHY
  520. * @adapter: Board private structure to initialize
  521. * Returns:
  522. * 0: Successfully
  523. * Negative value: Failed
  524. */
  525. static int pch_gbe_init_phy(struct pch_gbe_adapter *adapter)
  526. {
  527. struct net_device *netdev = adapter->netdev;
  528. u32 addr;
  529. u16 bmcr, stat;
  530. /* Discover phy addr by searching addrs in order {1,0,2,..., 31} */
  531. for (addr = 0; addr < PCH_GBE_PHY_REGS_LEN; addr++) {
  532. adapter->mii.phy_id = (addr == 0) ? 1 : (addr == 1) ? 0 : addr;
  533. bmcr = pch_gbe_mdio_read(netdev, adapter->mii.phy_id, MII_BMCR);
  534. stat = pch_gbe_mdio_read(netdev, adapter->mii.phy_id, MII_BMSR);
  535. stat = pch_gbe_mdio_read(netdev, adapter->mii.phy_id, MII_BMSR);
  536. if (!((bmcr == 0xFFFF) || ((stat == 0) && (bmcr == 0))))
  537. break;
  538. }
  539. adapter->hw.phy.addr = adapter->mii.phy_id;
  540. netdev_dbg(netdev, "phy_addr = %d\n", adapter->mii.phy_id);
  541. if (addr == PCH_GBE_PHY_REGS_LEN)
  542. return -EAGAIN;
  543. /* Selected the phy and isolate the rest */
  544. for (addr = 0; addr < PCH_GBE_PHY_REGS_LEN; addr++) {
  545. if (addr != adapter->mii.phy_id) {
  546. pch_gbe_mdio_write(netdev, addr, MII_BMCR,
  547. BMCR_ISOLATE);
  548. } else {
  549. bmcr = pch_gbe_mdio_read(netdev, addr, MII_BMCR);
  550. pch_gbe_mdio_write(netdev, addr, MII_BMCR,
  551. bmcr & ~BMCR_ISOLATE);
  552. }
  553. }
  554. /* MII setup */
  555. adapter->mii.phy_id_mask = 0x1F;
  556. adapter->mii.reg_num_mask = 0x1F;
  557. adapter->mii.dev = adapter->netdev;
  558. adapter->mii.mdio_read = pch_gbe_mdio_read;
  559. adapter->mii.mdio_write = pch_gbe_mdio_write;
  560. adapter->mii.supports_gmii = mii_check_gmii_support(&adapter->mii);
  561. return 0;
  562. }
  563. /**
  564. * pch_gbe_mdio_read - The read function for mii
  565. * @netdev: Network interface device structure
  566. * @addr: Phy ID
  567. * @reg: Access location
  568. * Returns:
  569. * 0: Successfully
  570. * Negative value: Failed
  571. */
  572. static int pch_gbe_mdio_read(struct net_device *netdev, int addr, int reg)
  573. {
  574. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  575. struct pch_gbe_hw *hw = &adapter->hw;
  576. return pch_gbe_mac_ctrl_miim(hw, addr, PCH_GBE_HAL_MIIM_READ, reg,
  577. (u16) 0);
  578. }
  579. /**
  580. * pch_gbe_mdio_write - The write function for mii
  581. * @netdev: Network interface device structure
  582. * @addr: Phy ID (not used)
  583. * @reg: Access location
  584. * @data: Write data
  585. */
  586. static void pch_gbe_mdio_write(struct net_device *netdev,
  587. int addr, int reg, int data)
  588. {
  589. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  590. struct pch_gbe_hw *hw = &adapter->hw;
  591. pch_gbe_mac_ctrl_miim(hw, addr, PCH_GBE_HAL_MIIM_WRITE, reg, data);
  592. }
  593. /**
  594. * pch_gbe_reset_task - Reset processing at the time of transmission timeout
  595. * @work: Pointer of board private structure
  596. */
  597. static void pch_gbe_reset_task(struct work_struct *work)
  598. {
  599. struct pch_gbe_adapter *adapter;
  600. adapter = container_of(work, struct pch_gbe_adapter, reset_task);
  601. rtnl_lock();
  602. pch_gbe_reinit_locked(adapter);
  603. rtnl_unlock();
  604. }
  605. /**
  606. * pch_gbe_reinit_locked- Re-initialization
  607. * @adapter: Board private structure
  608. */
  609. void pch_gbe_reinit_locked(struct pch_gbe_adapter *adapter)
  610. {
  611. pch_gbe_down(adapter);
  612. pch_gbe_up(adapter);
  613. }
  614. /**
  615. * pch_gbe_reset - Reset GbE
  616. * @adapter: Board private structure
  617. */
  618. void pch_gbe_reset(struct pch_gbe_adapter *adapter)
  619. {
  620. struct net_device *netdev = adapter->netdev;
  621. struct pch_gbe_hw *hw = &adapter->hw;
  622. s32 ret_val;
  623. pch_gbe_mac_reset_hw(hw);
  624. /* reprogram multicast address register after reset */
  625. pch_gbe_set_multi(netdev);
  626. /* Setup the receive address. */
  627. pch_gbe_mac_init_rx_addrs(hw, PCH_GBE_MAR_ENTRIES);
  628. ret_val = pch_gbe_phy_get_id(hw);
  629. if (ret_val) {
  630. netdev_err(adapter->netdev, "pch_gbe_phy_get_id error\n");
  631. return;
  632. }
  633. pch_gbe_phy_init_setting(hw);
  634. /* Setup Mac interface option RGMII */
  635. pch_gbe_phy_set_rgmii(hw);
  636. }
  637. /**
  638. * pch_gbe_free_irq - Free an interrupt
  639. * @adapter: Board private structure
  640. */
  641. static void pch_gbe_free_irq(struct pch_gbe_adapter *adapter)
  642. {
  643. struct net_device *netdev = adapter->netdev;
  644. free_irq(adapter->irq, netdev);
  645. pci_free_irq_vectors(adapter->pdev);
  646. }
  647. /**
  648. * pch_gbe_irq_disable - Mask off interrupt generation on the NIC
  649. * @adapter: Board private structure
  650. */
  651. static void pch_gbe_irq_disable(struct pch_gbe_adapter *adapter)
  652. {
  653. struct pch_gbe_hw *hw = &adapter->hw;
  654. atomic_inc(&adapter->irq_sem);
  655. iowrite32(0, &hw->reg->INT_EN);
  656. ioread32(&hw->reg->INT_ST);
  657. synchronize_irq(adapter->irq);
  658. netdev_dbg(adapter->netdev, "INT_EN reg : 0x%08x\n",
  659. ioread32(&hw->reg->INT_EN));
  660. }
  661. /**
  662. * pch_gbe_irq_enable - Enable default interrupt generation settings
  663. * @adapter: Board private structure
  664. */
  665. static void pch_gbe_irq_enable(struct pch_gbe_adapter *adapter)
  666. {
  667. struct pch_gbe_hw *hw = &adapter->hw;
  668. if (likely(atomic_dec_and_test(&adapter->irq_sem)))
  669. iowrite32(PCH_GBE_INT_ENABLE_MASK, &hw->reg->INT_EN);
  670. ioread32(&hw->reg->INT_ST);
  671. netdev_dbg(adapter->netdev, "INT_EN reg : 0x%08x\n",
  672. ioread32(&hw->reg->INT_EN));
  673. }
  674. /**
  675. * pch_gbe_setup_tctl - configure the Transmit control registers
  676. * @adapter: Board private structure
  677. */
  678. static void pch_gbe_setup_tctl(struct pch_gbe_adapter *adapter)
  679. {
  680. struct pch_gbe_hw *hw = &adapter->hw;
  681. u32 tx_mode, tcpip;
  682. tx_mode = PCH_GBE_TM_LONG_PKT |
  683. PCH_GBE_TM_ST_AND_FD |
  684. PCH_GBE_TM_SHORT_PKT |
  685. PCH_GBE_TM_TH_TX_STRT_8 |
  686. PCH_GBE_TM_TH_ALM_EMP_4 | PCH_GBE_TM_TH_ALM_FULL_8;
  687. iowrite32(tx_mode, &hw->reg->TX_MODE);
  688. tcpip = ioread32(&hw->reg->TCPIP_ACC);
  689. tcpip |= PCH_GBE_TX_TCPIPACC_EN;
  690. iowrite32(tcpip, &hw->reg->TCPIP_ACC);
  691. return;
  692. }
  693. /**
  694. * pch_gbe_configure_tx - Configure Transmit Unit after Reset
  695. * @adapter: Board private structure
  696. */
  697. static void pch_gbe_configure_tx(struct pch_gbe_adapter *adapter)
  698. {
  699. struct pch_gbe_hw *hw = &adapter->hw;
  700. u32 tdba, tdlen, dctrl;
  701. netdev_dbg(adapter->netdev, "dma addr = 0x%08llx size = 0x%08x\n",
  702. (unsigned long long)adapter->tx_ring->dma,
  703. adapter->tx_ring->size);
  704. /* Setup the HW Tx Head and Tail descriptor pointers */
  705. tdba = adapter->tx_ring->dma;
  706. tdlen = adapter->tx_ring->size - 0x10;
  707. iowrite32(tdba, &hw->reg->TX_DSC_BASE);
  708. iowrite32(tdlen, &hw->reg->TX_DSC_SIZE);
  709. iowrite32(tdba, &hw->reg->TX_DSC_SW_P);
  710. /* Enables Transmission DMA */
  711. dctrl = ioread32(&hw->reg->DMA_CTRL);
  712. dctrl |= PCH_GBE_TX_DMA_EN;
  713. iowrite32(dctrl, &hw->reg->DMA_CTRL);
  714. }
  715. /**
  716. * pch_gbe_setup_rctl - Configure the receive control registers
  717. * @adapter: Board private structure
  718. */
  719. static void pch_gbe_setup_rctl(struct pch_gbe_adapter *adapter)
  720. {
  721. struct pch_gbe_hw *hw = &adapter->hw;
  722. u32 rx_mode, tcpip;
  723. rx_mode = PCH_GBE_ADD_FIL_EN | PCH_GBE_MLT_FIL_EN |
  724. PCH_GBE_RH_ALM_EMP_4 | PCH_GBE_RH_ALM_FULL_4 | PCH_GBE_RH_RD_TRG_8;
  725. iowrite32(rx_mode, &hw->reg->RX_MODE);
  726. tcpip = ioread32(&hw->reg->TCPIP_ACC);
  727. tcpip |= PCH_GBE_RX_TCPIPACC_OFF;
  728. tcpip &= ~PCH_GBE_RX_TCPIPACC_EN;
  729. iowrite32(tcpip, &hw->reg->TCPIP_ACC);
  730. return;
  731. }
  732. /**
  733. * pch_gbe_configure_rx - Configure Receive Unit after Reset
  734. * @adapter: Board private structure
  735. */
  736. static void pch_gbe_configure_rx(struct pch_gbe_adapter *adapter)
  737. {
  738. struct pch_gbe_hw *hw = &adapter->hw;
  739. u32 rdba, rdlen, rxdma;
  740. netdev_dbg(adapter->netdev, "dma adr = 0x%08llx size = 0x%08x\n",
  741. (unsigned long long)adapter->rx_ring->dma,
  742. adapter->rx_ring->size);
  743. pch_gbe_mac_force_mac_fc(hw);
  744. pch_gbe_disable_mac_rx(hw);
  745. /* Disables Receive DMA */
  746. rxdma = ioread32(&hw->reg->DMA_CTRL);
  747. rxdma &= ~PCH_GBE_RX_DMA_EN;
  748. iowrite32(rxdma, &hw->reg->DMA_CTRL);
  749. netdev_dbg(adapter->netdev,
  750. "MAC_RX_EN reg = 0x%08x DMA_CTRL reg = 0x%08x\n",
  751. ioread32(&hw->reg->MAC_RX_EN),
  752. ioread32(&hw->reg->DMA_CTRL));
  753. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  754. * the Base and Length of the Rx Descriptor Ring */
  755. rdba = adapter->rx_ring->dma;
  756. rdlen = adapter->rx_ring->size - 0x10;
  757. iowrite32(rdba, &hw->reg->RX_DSC_BASE);
  758. iowrite32(rdlen, &hw->reg->RX_DSC_SIZE);
  759. iowrite32((rdba + rdlen), &hw->reg->RX_DSC_SW_P);
  760. }
  761. /**
  762. * pch_gbe_unmap_and_free_tx_resource - Unmap and free tx socket buffer
  763. * @adapter: Board private structure
  764. * @buffer_info: Buffer information structure
  765. */
  766. static void pch_gbe_unmap_and_free_tx_resource(
  767. struct pch_gbe_adapter *adapter, struct pch_gbe_buffer *buffer_info)
  768. {
  769. if (buffer_info->mapped) {
  770. dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
  771. buffer_info->length, DMA_TO_DEVICE);
  772. buffer_info->mapped = false;
  773. }
  774. if (buffer_info->skb) {
  775. dev_kfree_skb_any(buffer_info->skb);
  776. buffer_info->skb = NULL;
  777. }
  778. }
  779. /**
  780. * pch_gbe_unmap_and_free_rx_resource - Unmap and free rx socket buffer
  781. * @adapter: Board private structure
  782. * @buffer_info: Buffer information structure
  783. */
  784. static void pch_gbe_unmap_and_free_rx_resource(
  785. struct pch_gbe_adapter *adapter,
  786. struct pch_gbe_buffer *buffer_info)
  787. {
  788. if (buffer_info->mapped) {
  789. dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
  790. buffer_info->length, DMA_FROM_DEVICE);
  791. buffer_info->mapped = false;
  792. }
  793. if (buffer_info->skb) {
  794. dev_kfree_skb_any(buffer_info->skb);
  795. buffer_info->skb = NULL;
  796. }
  797. }
  798. /**
  799. * pch_gbe_clean_tx_ring - Free Tx Buffers
  800. * @adapter: Board private structure
  801. * @tx_ring: Ring to be cleaned
  802. */
  803. static void pch_gbe_clean_tx_ring(struct pch_gbe_adapter *adapter,
  804. struct pch_gbe_tx_ring *tx_ring)
  805. {
  806. struct pch_gbe_hw *hw = &adapter->hw;
  807. struct pch_gbe_buffer *buffer_info;
  808. unsigned long size;
  809. unsigned int i;
  810. /* Free all the Tx ring sk_buffs */
  811. for (i = 0; i < tx_ring->count; i++) {
  812. buffer_info = &tx_ring->buffer_info[i];
  813. pch_gbe_unmap_and_free_tx_resource(adapter, buffer_info);
  814. }
  815. netdev_dbg(adapter->netdev,
  816. "call pch_gbe_unmap_and_free_tx_resource() %d count\n", i);
  817. size = (unsigned long)sizeof(struct pch_gbe_buffer) * tx_ring->count;
  818. memset(tx_ring->buffer_info, 0, size);
  819. /* Zero out the descriptor ring */
  820. memset(tx_ring->desc, 0, tx_ring->size);
  821. tx_ring->next_to_use = 0;
  822. tx_ring->next_to_clean = 0;
  823. iowrite32(tx_ring->dma, &hw->reg->TX_DSC_HW_P);
  824. iowrite32((tx_ring->size - 0x10), &hw->reg->TX_DSC_SIZE);
  825. }
  826. /**
  827. * pch_gbe_clean_rx_ring - Free Rx Buffers
  828. * @adapter: Board private structure
  829. * @rx_ring: Ring to free buffers from
  830. */
  831. static void
  832. pch_gbe_clean_rx_ring(struct pch_gbe_adapter *adapter,
  833. struct pch_gbe_rx_ring *rx_ring)
  834. {
  835. struct pch_gbe_hw *hw = &adapter->hw;
  836. struct pch_gbe_buffer *buffer_info;
  837. unsigned long size;
  838. unsigned int i;
  839. /* Free all the Rx ring sk_buffs */
  840. for (i = 0; i < rx_ring->count; i++) {
  841. buffer_info = &rx_ring->buffer_info[i];
  842. pch_gbe_unmap_and_free_rx_resource(adapter, buffer_info);
  843. }
  844. netdev_dbg(adapter->netdev,
  845. "call pch_gbe_unmap_and_free_rx_resource() %d count\n", i);
  846. size = (unsigned long)sizeof(struct pch_gbe_buffer) * rx_ring->count;
  847. memset(rx_ring->buffer_info, 0, size);
  848. /* Zero out the descriptor ring */
  849. memset(rx_ring->desc, 0, rx_ring->size);
  850. rx_ring->next_to_clean = 0;
  851. rx_ring->next_to_use = 0;
  852. iowrite32(rx_ring->dma, &hw->reg->RX_DSC_HW_P);
  853. iowrite32((rx_ring->size - 0x10), &hw->reg->RX_DSC_SIZE);
  854. }
  855. static void pch_gbe_set_rgmii_ctrl(struct pch_gbe_adapter *adapter, u16 speed,
  856. u16 duplex)
  857. {
  858. struct pch_gbe_hw *hw = &adapter->hw;
  859. unsigned long rgmii = 0;
  860. /* Set the RGMII control. */
  861. switch (speed) {
  862. case SPEED_10:
  863. rgmii = (PCH_GBE_RGMII_RATE_2_5M |
  864. PCH_GBE_MAC_RGMII_CTRL_SETTING);
  865. break;
  866. case SPEED_100:
  867. rgmii = (PCH_GBE_RGMII_RATE_25M |
  868. PCH_GBE_MAC_RGMII_CTRL_SETTING);
  869. break;
  870. case SPEED_1000:
  871. rgmii = (PCH_GBE_RGMII_RATE_125M |
  872. PCH_GBE_MAC_RGMII_CTRL_SETTING);
  873. break;
  874. }
  875. iowrite32(rgmii, &hw->reg->RGMII_CTRL);
  876. }
  877. static void pch_gbe_set_mode(struct pch_gbe_adapter *adapter, u16 speed,
  878. u16 duplex)
  879. {
  880. struct net_device *netdev = adapter->netdev;
  881. struct pch_gbe_hw *hw = &adapter->hw;
  882. unsigned long mode = 0;
  883. /* Set the communication mode */
  884. switch (speed) {
  885. case SPEED_10:
  886. mode = PCH_GBE_MODE_MII_ETHER;
  887. netdev->tx_queue_len = 10;
  888. break;
  889. case SPEED_100:
  890. mode = PCH_GBE_MODE_MII_ETHER;
  891. netdev->tx_queue_len = 100;
  892. break;
  893. case SPEED_1000:
  894. mode = PCH_GBE_MODE_GMII_ETHER;
  895. break;
  896. }
  897. if (duplex == DUPLEX_FULL)
  898. mode |= PCH_GBE_MODE_FULL_DUPLEX;
  899. else
  900. mode |= PCH_GBE_MODE_HALF_DUPLEX;
  901. iowrite32(mode, &hw->reg->MODE);
  902. }
  903. /**
  904. * pch_gbe_watchdog - Watchdog process
  905. * @data: Board private structure
  906. */
  907. static void pch_gbe_watchdog(struct timer_list *t)
  908. {
  909. struct pch_gbe_adapter *adapter = from_timer(adapter, t,
  910. watchdog_timer);
  911. struct net_device *netdev = adapter->netdev;
  912. struct pch_gbe_hw *hw = &adapter->hw;
  913. netdev_dbg(netdev, "right now = %ld\n", jiffies);
  914. pch_gbe_update_stats(adapter);
  915. if ((mii_link_ok(&adapter->mii)) && (!netif_carrier_ok(netdev))) {
  916. struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
  917. netdev->tx_queue_len = adapter->tx_queue_len;
  918. /* mii library handles link maintenance tasks */
  919. if (mii_ethtool_gset(&adapter->mii, &cmd)) {
  920. netdev_err(netdev, "ethtool get setting Error\n");
  921. mod_timer(&adapter->watchdog_timer,
  922. round_jiffies(jiffies +
  923. PCH_GBE_WATCHDOG_PERIOD));
  924. return;
  925. }
  926. hw->mac.link_speed = ethtool_cmd_speed(&cmd);
  927. hw->mac.link_duplex = cmd.duplex;
  928. /* Set the RGMII control. */
  929. pch_gbe_set_rgmii_ctrl(adapter, hw->mac.link_speed,
  930. hw->mac.link_duplex);
  931. /* Set the communication mode */
  932. pch_gbe_set_mode(adapter, hw->mac.link_speed,
  933. hw->mac.link_duplex);
  934. netdev_dbg(netdev,
  935. "Link is Up %d Mbps %s-Duplex\n",
  936. hw->mac.link_speed,
  937. cmd.duplex == DUPLEX_FULL ? "Full" : "Half");
  938. netif_carrier_on(netdev);
  939. netif_wake_queue(netdev);
  940. } else if ((!mii_link_ok(&adapter->mii)) &&
  941. (netif_carrier_ok(netdev))) {
  942. netdev_dbg(netdev, "NIC Link is Down\n");
  943. hw->mac.link_speed = SPEED_10;
  944. hw->mac.link_duplex = DUPLEX_HALF;
  945. netif_carrier_off(netdev);
  946. netif_stop_queue(netdev);
  947. }
  948. mod_timer(&adapter->watchdog_timer,
  949. round_jiffies(jiffies + PCH_GBE_WATCHDOG_PERIOD));
  950. }
  951. /**
  952. * pch_gbe_tx_queue - Carry out queuing of the transmission data
  953. * @adapter: Board private structure
  954. * @tx_ring: Tx descriptor ring structure
  955. * @skb: Sockt buffer structure
  956. */
  957. static void pch_gbe_tx_queue(struct pch_gbe_adapter *adapter,
  958. struct pch_gbe_tx_ring *tx_ring,
  959. struct sk_buff *skb)
  960. {
  961. struct pch_gbe_hw *hw = &adapter->hw;
  962. struct pch_gbe_tx_desc *tx_desc;
  963. struct pch_gbe_buffer *buffer_info;
  964. struct sk_buff *tmp_skb;
  965. unsigned int frame_ctrl;
  966. unsigned int ring_num;
  967. /*-- Set frame control --*/
  968. frame_ctrl = 0;
  969. if (unlikely(skb->len < PCH_GBE_SHORT_PKT))
  970. frame_ctrl |= PCH_GBE_TXD_CTRL_APAD;
  971. if (skb->ip_summed == CHECKSUM_NONE)
  972. frame_ctrl |= PCH_GBE_TXD_CTRL_TCPIP_ACC_OFF;
  973. /* Performs checksum processing */
  974. /*
  975. * It is because the hardware accelerator does not support a checksum,
  976. * when the received data size is less than 64 bytes.
  977. */
  978. if (skb->len < PCH_GBE_SHORT_PKT && skb->ip_summed != CHECKSUM_NONE) {
  979. frame_ctrl |= PCH_GBE_TXD_CTRL_APAD |
  980. PCH_GBE_TXD_CTRL_TCPIP_ACC_OFF;
  981. if (skb->protocol == htons(ETH_P_IP)) {
  982. struct iphdr *iph = ip_hdr(skb);
  983. unsigned int offset;
  984. offset = skb_transport_offset(skb);
  985. if (iph->protocol == IPPROTO_TCP) {
  986. skb->csum = 0;
  987. tcp_hdr(skb)->check = 0;
  988. skb->csum = skb_checksum(skb, offset,
  989. skb->len - offset, 0);
  990. tcp_hdr(skb)->check =
  991. csum_tcpudp_magic(iph->saddr,
  992. iph->daddr,
  993. skb->len - offset,
  994. IPPROTO_TCP,
  995. skb->csum);
  996. } else if (iph->protocol == IPPROTO_UDP) {
  997. skb->csum = 0;
  998. udp_hdr(skb)->check = 0;
  999. skb->csum =
  1000. skb_checksum(skb, offset,
  1001. skb->len - offset, 0);
  1002. udp_hdr(skb)->check =
  1003. csum_tcpudp_magic(iph->saddr,
  1004. iph->daddr,
  1005. skb->len - offset,
  1006. IPPROTO_UDP,
  1007. skb->csum);
  1008. }
  1009. }
  1010. }
  1011. ring_num = tx_ring->next_to_use;
  1012. if (unlikely((ring_num + 1) == tx_ring->count))
  1013. tx_ring->next_to_use = 0;
  1014. else
  1015. tx_ring->next_to_use = ring_num + 1;
  1016. buffer_info = &tx_ring->buffer_info[ring_num];
  1017. tmp_skb = buffer_info->skb;
  1018. /* [Header:14][payload] ---> [Header:14][paddong:2][payload] */
  1019. memcpy(tmp_skb->data, skb->data, ETH_HLEN);
  1020. tmp_skb->data[ETH_HLEN] = 0x00;
  1021. tmp_skb->data[ETH_HLEN + 1] = 0x00;
  1022. tmp_skb->len = skb->len;
  1023. memcpy(&tmp_skb->data[ETH_HLEN + 2], &skb->data[ETH_HLEN],
  1024. (skb->len - ETH_HLEN));
  1025. /*-- Set Buffer information --*/
  1026. buffer_info->length = tmp_skb->len;
  1027. buffer_info->dma = dma_map_single(&adapter->pdev->dev, tmp_skb->data,
  1028. buffer_info->length,
  1029. DMA_TO_DEVICE);
  1030. if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
  1031. netdev_err(adapter->netdev, "TX DMA map failed\n");
  1032. buffer_info->dma = 0;
  1033. buffer_info->time_stamp = 0;
  1034. tx_ring->next_to_use = ring_num;
  1035. return;
  1036. }
  1037. buffer_info->mapped = true;
  1038. buffer_info->time_stamp = jiffies;
  1039. /*-- Set Tx descriptor --*/
  1040. tx_desc = PCH_GBE_TX_DESC(*tx_ring, ring_num);
  1041. tx_desc->buffer_addr = (buffer_info->dma);
  1042. tx_desc->length = (tmp_skb->len);
  1043. tx_desc->tx_words_eob = ((tmp_skb->len + 3));
  1044. tx_desc->tx_frame_ctrl = (frame_ctrl);
  1045. tx_desc->gbec_status = (DSC_INIT16);
  1046. if (unlikely(++ring_num == tx_ring->count))
  1047. ring_num = 0;
  1048. /* Update software pointer of TX descriptor */
  1049. iowrite32(tx_ring->dma +
  1050. (int)sizeof(struct pch_gbe_tx_desc) * ring_num,
  1051. &hw->reg->TX_DSC_SW_P);
  1052. pch_tx_timestamp(adapter, skb);
  1053. dev_kfree_skb_any(skb);
  1054. }
  1055. /**
  1056. * pch_gbe_update_stats - Update the board statistics counters
  1057. * @adapter: Board private structure
  1058. */
  1059. void pch_gbe_update_stats(struct pch_gbe_adapter *adapter)
  1060. {
  1061. struct net_device *netdev = adapter->netdev;
  1062. struct pci_dev *pdev = adapter->pdev;
  1063. struct pch_gbe_hw_stats *stats = &adapter->stats;
  1064. unsigned long flags;
  1065. /*
  1066. * Prevent stats update while adapter is being reset, or if the pci
  1067. * connection is down.
  1068. */
  1069. if ((pdev->error_state) && (pdev->error_state != pci_channel_io_normal))
  1070. return;
  1071. spin_lock_irqsave(&adapter->stats_lock, flags);
  1072. /* Update device status "adapter->stats" */
  1073. stats->rx_errors = stats->rx_crc_errors + stats->rx_frame_errors;
  1074. stats->tx_errors = stats->tx_length_errors +
  1075. stats->tx_aborted_errors +
  1076. stats->tx_carrier_errors + stats->tx_timeout_count;
  1077. /* Update network device status "adapter->net_stats" */
  1078. netdev->stats.rx_packets = stats->rx_packets;
  1079. netdev->stats.rx_bytes = stats->rx_bytes;
  1080. netdev->stats.rx_dropped = stats->rx_dropped;
  1081. netdev->stats.tx_packets = stats->tx_packets;
  1082. netdev->stats.tx_bytes = stats->tx_bytes;
  1083. netdev->stats.tx_dropped = stats->tx_dropped;
  1084. /* Fill out the OS statistics structure */
  1085. netdev->stats.multicast = stats->multicast;
  1086. netdev->stats.collisions = stats->collisions;
  1087. /* Rx Errors */
  1088. netdev->stats.rx_errors = stats->rx_errors;
  1089. netdev->stats.rx_crc_errors = stats->rx_crc_errors;
  1090. netdev->stats.rx_frame_errors = stats->rx_frame_errors;
  1091. /* Tx Errors */
  1092. netdev->stats.tx_errors = stats->tx_errors;
  1093. netdev->stats.tx_aborted_errors = stats->tx_aborted_errors;
  1094. netdev->stats.tx_carrier_errors = stats->tx_carrier_errors;
  1095. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  1096. }
  1097. static void pch_gbe_disable_dma_rx(struct pch_gbe_hw *hw)
  1098. {
  1099. u32 rxdma;
  1100. /* Disable Receive DMA */
  1101. rxdma = ioread32(&hw->reg->DMA_CTRL);
  1102. rxdma &= ~PCH_GBE_RX_DMA_EN;
  1103. iowrite32(rxdma, &hw->reg->DMA_CTRL);
  1104. }
  1105. static void pch_gbe_enable_dma_rx(struct pch_gbe_hw *hw)
  1106. {
  1107. u32 rxdma;
  1108. /* Enables Receive DMA */
  1109. rxdma = ioread32(&hw->reg->DMA_CTRL);
  1110. rxdma |= PCH_GBE_RX_DMA_EN;
  1111. iowrite32(rxdma, &hw->reg->DMA_CTRL);
  1112. }
  1113. /**
  1114. * pch_gbe_intr - Interrupt Handler
  1115. * @irq: Interrupt number
  1116. * @data: Pointer to a network interface device structure
  1117. * Returns:
  1118. * - IRQ_HANDLED: Our interrupt
  1119. * - IRQ_NONE: Not our interrupt
  1120. */
  1121. static irqreturn_t pch_gbe_intr(int irq, void *data)
  1122. {
  1123. struct net_device *netdev = data;
  1124. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1125. struct pch_gbe_hw *hw = &adapter->hw;
  1126. u32 int_st;
  1127. u32 int_en;
  1128. /* Check request status */
  1129. int_st = ioread32(&hw->reg->INT_ST);
  1130. int_st = int_st & ioread32(&hw->reg->INT_EN);
  1131. /* When request status is no interruption factor */
  1132. if (unlikely(!int_st))
  1133. return IRQ_NONE; /* Not our interrupt. End processing. */
  1134. netdev_dbg(netdev, "%s occur int_st = 0x%08x\n", __func__, int_st);
  1135. if (int_st & PCH_GBE_INT_RX_FRAME_ERR)
  1136. adapter->stats.intr_rx_frame_err_count++;
  1137. if (int_st & PCH_GBE_INT_RX_FIFO_ERR)
  1138. if (!adapter->rx_stop_flag) {
  1139. adapter->stats.intr_rx_fifo_err_count++;
  1140. netdev_dbg(netdev, "Rx fifo over run\n");
  1141. adapter->rx_stop_flag = true;
  1142. int_en = ioread32(&hw->reg->INT_EN);
  1143. iowrite32((int_en & ~PCH_GBE_INT_RX_FIFO_ERR),
  1144. &hw->reg->INT_EN);
  1145. pch_gbe_disable_dma_rx(&adapter->hw);
  1146. int_st |= ioread32(&hw->reg->INT_ST);
  1147. int_st = int_st & ioread32(&hw->reg->INT_EN);
  1148. }
  1149. if (int_st & PCH_GBE_INT_RX_DMA_ERR)
  1150. adapter->stats.intr_rx_dma_err_count++;
  1151. if (int_st & PCH_GBE_INT_TX_FIFO_ERR)
  1152. adapter->stats.intr_tx_fifo_err_count++;
  1153. if (int_st & PCH_GBE_INT_TX_DMA_ERR)
  1154. adapter->stats.intr_tx_dma_err_count++;
  1155. if (int_st & PCH_GBE_INT_TCPIP_ERR)
  1156. adapter->stats.intr_tcpip_err_count++;
  1157. /* When Rx descriptor is empty */
  1158. if ((int_st & PCH_GBE_INT_RX_DSC_EMP)) {
  1159. adapter->stats.intr_rx_dsc_empty_count++;
  1160. netdev_dbg(netdev, "Rx descriptor is empty\n");
  1161. int_en = ioread32(&hw->reg->INT_EN);
  1162. iowrite32((int_en & ~PCH_GBE_INT_RX_DSC_EMP), &hw->reg->INT_EN);
  1163. if (hw->mac.tx_fc_enable) {
  1164. /* Set Pause packet */
  1165. pch_gbe_mac_set_pause_packet(hw);
  1166. }
  1167. }
  1168. /* When request status is Receive interruption */
  1169. if ((int_st & (PCH_GBE_INT_RX_DMA_CMPLT | PCH_GBE_INT_TX_CMPLT)) ||
  1170. (adapter->rx_stop_flag)) {
  1171. if (likely(napi_schedule_prep(&adapter->napi))) {
  1172. /* Enable only Rx Descriptor empty */
  1173. atomic_inc(&adapter->irq_sem);
  1174. int_en = ioread32(&hw->reg->INT_EN);
  1175. int_en &=
  1176. ~(PCH_GBE_INT_RX_DMA_CMPLT | PCH_GBE_INT_TX_CMPLT);
  1177. iowrite32(int_en, &hw->reg->INT_EN);
  1178. /* Start polling for NAPI */
  1179. __napi_schedule(&adapter->napi);
  1180. }
  1181. }
  1182. netdev_dbg(netdev, "return = 0x%08x INT_EN reg = 0x%08x\n",
  1183. IRQ_HANDLED, ioread32(&hw->reg->INT_EN));
  1184. return IRQ_HANDLED;
  1185. }
  1186. /**
  1187. * pch_gbe_alloc_rx_buffers - Replace used receive buffers; legacy & extended
  1188. * @adapter: Board private structure
  1189. * @rx_ring: Rx descriptor ring
  1190. * @cleaned_count: Cleaned count
  1191. */
  1192. static void
  1193. pch_gbe_alloc_rx_buffers(struct pch_gbe_adapter *adapter,
  1194. struct pch_gbe_rx_ring *rx_ring, int cleaned_count)
  1195. {
  1196. struct net_device *netdev = adapter->netdev;
  1197. struct pci_dev *pdev = adapter->pdev;
  1198. struct pch_gbe_hw *hw = &adapter->hw;
  1199. struct pch_gbe_rx_desc *rx_desc;
  1200. struct pch_gbe_buffer *buffer_info;
  1201. struct sk_buff *skb;
  1202. unsigned int i;
  1203. unsigned int bufsz;
  1204. bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
  1205. i = rx_ring->next_to_use;
  1206. while ((cleaned_count--)) {
  1207. buffer_info = &rx_ring->buffer_info[i];
  1208. skb = netdev_alloc_skb(netdev, bufsz);
  1209. if (unlikely(!skb)) {
  1210. /* Better luck next round */
  1211. adapter->stats.rx_alloc_buff_failed++;
  1212. break;
  1213. }
  1214. /* align */
  1215. skb_reserve(skb, NET_IP_ALIGN);
  1216. buffer_info->skb = skb;
  1217. buffer_info->dma = dma_map_single(&pdev->dev,
  1218. buffer_info->rx_buffer,
  1219. buffer_info->length,
  1220. DMA_FROM_DEVICE);
  1221. if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
  1222. dev_kfree_skb(skb);
  1223. buffer_info->skb = NULL;
  1224. buffer_info->dma = 0;
  1225. adapter->stats.rx_alloc_buff_failed++;
  1226. break; /* while !buffer_info->skb */
  1227. }
  1228. buffer_info->mapped = true;
  1229. rx_desc = PCH_GBE_RX_DESC(*rx_ring, i);
  1230. rx_desc->buffer_addr = (buffer_info->dma);
  1231. rx_desc->gbec_status = DSC_INIT16;
  1232. netdev_dbg(netdev,
  1233. "i = %d buffer_info->dma = 0x08%llx buffer_info->length = 0x%x\n",
  1234. i, (unsigned long long)buffer_info->dma,
  1235. buffer_info->length);
  1236. if (unlikely(++i == rx_ring->count))
  1237. i = 0;
  1238. }
  1239. if (likely(rx_ring->next_to_use != i)) {
  1240. rx_ring->next_to_use = i;
  1241. if (unlikely(i-- == 0))
  1242. i = (rx_ring->count - 1);
  1243. iowrite32(rx_ring->dma +
  1244. (int)sizeof(struct pch_gbe_rx_desc) * i,
  1245. &hw->reg->RX_DSC_SW_P);
  1246. }
  1247. return;
  1248. }
  1249. static int
  1250. pch_gbe_alloc_rx_buffers_pool(struct pch_gbe_adapter *adapter,
  1251. struct pch_gbe_rx_ring *rx_ring, int cleaned_count)
  1252. {
  1253. struct pci_dev *pdev = adapter->pdev;
  1254. struct pch_gbe_buffer *buffer_info;
  1255. unsigned int i;
  1256. unsigned int bufsz;
  1257. unsigned int size;
  1258. bufsz = adapter->rx_buffer_len;
  1259. size = rx_ring->count * bufsz + PCH_GBE_RESERVE_MEMORY;
  1260. rx_ring->rx_buff_pool =
  1261. dma_zalloc_coherent(&pdev->dev, size,
  1262. &rx_ring->rx_buff_pool_logic, GFP_KERNEL);
  1263. if (!rx_ring->rx_buff_pool)
  1264. return -ENOMEM;
  1265. rx_ring->rx_buff_pool_size = size;
  1266. for (i = 0; i < rx_ring->count; i++) {
  1267. buffer_info = &rx_ring->buffer_info[i];
  1268. buffer_info->rx_buffer = rx_ring->rx_buff_pool + bufsz * i;
  1269. buffer_info->length = bufsz;
  1270. }
  1271. return 0;
  1272. }
  1273. /**
  1274. * pch_gbe_alloc_tx_buffers - Allocate transmit buffers
  1275. * @adapter: Board private structure
  1276. * @tx_ring: Tx descriptor ring
  1277. */
  1278. static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter,
  1279. struct pch_gbe_tx_ring *tx_ring)
  1280. {
  1281. struct pch_gbe_buffer *buffer_info;
  1282. struct sk_buff *skb;
  1283. unsigned int i;
  1284. unsigned int bufsz;
  1285. struct pch_gbe_tx_desc *tx_desc;
  1286. bufsz =
  1287. adapter->hw.mac.max_frame_size + PCH_GBE_DMA_ALIGN + NET_IP_ALIGN;
  1288. for (i = 0; i < tx_ring->count; i++) {
  1289. buffer_info = &tx_ring->buffer_info[i];
  1290. skb = netdev_alloc_skb(adapter->netdev, bufsz);
  1291. skb_reserve(skb, PCH_GBE_DMA_ALIGN);
  1292. buffer_info->skb = skb;
  1293. tx_desc = PCH_GBE_TX_DESC(*tx_ring, i);
  1294. tx_desc->gbec_status = (DSC_INIT16);
  1295. }
  1296. return;
  1297. }
  1298. /**
  1299. * pch_gbe_clean_tx - Reclaim resources after transmit completes
  1300. * @adapter: Board private structure
  1301. * @tx_ring: Tx descriptor ring
  1302. * Returns:
  1303. * true: Cleaned the descriptor
  1304. * false: Not cleaned the descriptor
  1305. */
  1306. static bool
  1307. pch_gbe_clean_tx(struct pch_gbe_adapter *adapter,
  1308. struct pch_gbe_tx_ring *tx_ring)
  1309. {
  1310. struct pch_gbe_tx_desc *tx_desc;
  1311. struct pch_gbe_buffer *buffer_info;
  1312. struct sk_buff *skb;
  1313. unsigned int i;
  1314. unsigned int cleaned_count = 0;
  1315. bool cleaned = false;
  1316. int unused, thresh;
  1317. netdev_dbg(adapter->netdev, "next_to_clean : %d\n",
  1318. tx_ring->next_to_clean);
  1319. i = tx_ring->next_to_clean;
  1320. tx_desc = PCH_GBE_TX_DESC(*tx_ring, i);
  1321. netdev_dbg(adapter->netdev, "gbec_status:0x%04x dma_status:0x%04x\n",
  1322. tx_desc->gbec_status, tx_desc->dma_status);
  1323. unused = PCH_GBE_DESC_UNUSED(tx_ring);
  1324. thresh = tx_ring->count - PCH_GBE_TX_WEIGHT;
  1325. if ((tx_desc->gbec_status == DSC_INIT16) && (unused < thresh))
  1326. { /* current marked clean, tx queue filling up, do extra clean */
  1327. int j, k;
  1328. if (unused < 8) { /* tx queue nearly full */
  1329. netdev_dbg(adapter->netdev,
  1330. "clean_tx: transmit queue warning (%x,%x) unused=%d\n",
  1331. tx_ring->next_to_clean, tx_ring->next_to_use,
  1332. unused);
  1333. }
  1334. /* current marked clean, scan for more that need cleaning. */
  1335. k = i;
  1336. for (j = 0; j < PCH_GBE_TX_WEIGHT; j++)
  1337. {
  1338. tx_desc = PCH_GBE_TX_DESC(*tx_ring, k);
  1339. if (tx_desc->gbec_status != DSC_INIT16) break; /*found*/
  1340. if (++k >= tx_ring->count) k = 0; /*increment, wrap*/
  1341. }
  1342. if (j < PCH_GBE_TX_WEIGHT) {
  1343. netdev_dbg(adapter->netdev,
  1344. "clean_tx: unused=%d loops=%d found tx_desc[%x,%x:%x].gbec_status=%04x\n",
  1345. unused, j, i, k, tx_ring->next_to_use,
  1346. tx_desc->gbec_status);
  1347. i = k; /*found one to clean, usu gbec_status==2000.*/
  1348. }
  1349. }
  1350. while ((tx_desc->gbec_status & DSC_INIT16) == 0x0000) {
  1351. netdev_dbg(adapter->netdev, "gbec_status:0x%04x\n",
  1352. tx_desc->gbec_status);
  1353. buffer_info = &tx_ring->buffer_info[i];
  1354. skb = buffer_info->skb;
  1355. cleaned = true;
  1356. if ((tx_desc->gbec_status & PCH_GBE_TXD_GMAC_STAT_ABT)) {
  1357. adapter->stats.tx_aborted_errors++;
  1358. netdev_err(adapter->netdev, "Transfer Abort Error\n");
  1359. } else if ((tx_desc->gbec_status & PCH_GBE_TXD_GMAC_STAT_CRSER)
  1360. ) {
  1361. adapter->stats.tx_carrier_errors++;
  1362. netdev_err(adapter->netdev,
  1363. "Transfer Carrier Sense Error\n");
  1364. } else if ((tx_desc->gbec_status & PCH_GBE_TXD_GMAC_STAT_EXCOL)
  1365. ) {
  1366. adapter->stats.tx_aborted_errors++;
  1367. netdev_err(adapter->netdev,
  1368. "Transfer Collision Abort Error\n");
  1369. } else if ((tx_desc->gbec_status &
  1370. (PCH_GBE_TXD_GMAC_STAT_SNGCOL |
  1371. PCH_GBE_TXD_GMAC_STAT_MLTCOL))) {
  1372. adapter->stats.collisions++;
  1373. adapter->stats.tx_packets++;
  1374. adapter->stats.tx_bytes += skb->len;
  1375. netdev_dbg(adapter->netdev, "Transfer Collision\n");
  1376. } else if ((tx_desc->gbec_status & PCH_GBE_TXD_GMAC_STAT_CMPLT)
  1377. ) {
  1378. adapter->stats.tx_packets++;
  1379. adapter->stats.tx_bytes += skb->len;
  1380. }
  1381. if (buffer_info->mapped) {
  1382. netdev_dbg(adapter->netdev,
  1383. "unmap buffer_info->dma : %d\n", i);
  1384. dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
  1385. buffer_info->length, DMA_TO_DEVICE);
  1386. buffer_info->mapped = false;
  1387. }
  1388. if (buffer_info->skb) {
  1389. netdev_dbg(adapter->netdev,
  1390. "trim buffer_info->skb : %d\n", i);
  1391. skb_trim(buffer_info->skb, 0);
  1392. }
  1393. tx_desc->gbec_status = DSC_INIT16;
  1394. if (unlikely(++i == tx_ring->count))
  1395. i = 0;
  1396. tx_desc = PCH_GBE_TX_DESC(*tx_ring, i);
  1397. /* weight of a sort for tx, to avoid endless transmit cleanup */
  1398. if (cleaned_count++ == PCH_GBE_TX_WEIGHT) {
  1399. cleaned = false;
  1400. break;
  1401. }
  1402. }
  1403. netdev_dbg(adapter->netdev,
  1404. "called pch_gbe_unmap_and_free_tx_resource() %d count\n",
  1405. cleaned_count);
  1406. if (cleaned_count > 0) { /*skip this if nothing cleaned*/
  1407. /* Recover from running out of Tx resources in xmit_frame */
  1408. netif_tx_lock(adapter->netdev);
  1409. if (unlikely(cleaned && (netif_queue_stopped(adapter->netdev))))
  1410. {
  1411. netif_wake_queue(adapter->netdev);
  1412. adapter->stats.tx_restart_count++;
  1413. netdev_dbg(adapter->netdev, "Tx wake queue\n");
  1414. }
  1415. tx_ring->next_to_clean = i;
  1416. netdev_dbg(adapter->netdev, "next_to_clean : %d\n",
  1417. tx_ring->next_to_clean);
  1418. netif_tx_unlock(adapter->netdev);
  1419. }
  1420. return cleaned;
  1421. }
  1422. /**
  1423. * pch_gbe_clean_rx - Send received data up the network stack; legacy
  1424. * @adapter: Board private structure
  1425. * @rx_ring: Rx descriptor ring
  1426. * @work_done: Completed count
  1427. * @work_to_do: Request count
  1428. * Returns:
  1429. * true: Cleaned the descriptor
  1430. * false: Not cleaned the descriptor
  1431. */
  1432. static bool
  1433. pch_gbe_clean_rx(struct pch_gbe_adapter *adapter,
  1434. struct pch_gbe_rx_ring *rx_ring,
  1435. int *work_done, int work_to_do)
  1436. {
  1437. struct net_device *netdev = adapter->netdev;
  1438. struct pci_dev *pdev = adapter->pdev;
  1439. struct pch_gbe_buffer *buffer_info;
  1440. struct pch_gbe_rx_desc *rx_desc;
  1441. u32 length;
  1442. unsigned int i;
  1443. unsigned int cleaned_count = 0;
  1444. bool cleaned = false;
  1445. struct sk_buff *skb;
  1446. u8 dma_status;
  1447. u16 gbec_status;
  1448. u32 tcp_ip_status;
  1449. i = rx_ring->next_to_clean;
  1450. while (*work_done < work_to_do) {
  1451. /* Check Rx descriptor status */
  1452. rx_desc = PCH_GBE_RX_DESC(*rx_ring, i);
  1453. if (rx_desc->gbec_status == DSC_INIT16)
  1454. break;
  1455. cleaned = true;
  1456. cleaned_count++;
  1457. dma_status = rx_desc->dma_status;
  1458. gbec_status = rx_desc->gbec_status;
  1459. tcp_ip_status = rx_desc->tcp_ip_status;
  1460. rx_desc->gbec_status = DSC_INIT16;
  1461. buffer_info = &rx_ring->buffer_info[i];
  1462. skb = buffer_info->skb;
  1463. buffer_info->skb = NULL;
  1464. /* unmap dma */
  1465. dma_unmap_single(&pdev->dev, buffer_info->dma,
  1466. buffer_info->length, DMA_FROM_DEVICE);
  1467. buffer_info->mapped = false;
  1468. netdev_dbg(netdev,
  1469. "RxDecNo = 0x%04x Status[DMA:0x%02x GBE:0x%04x TCP:0x%08x] BufInf = 0x%p\n",
  1470. i, dma_status, gbec_status, tcp_ip_status,
  1471. buffer_info);
  1472. /* Error check */
  1473. if (unlikely(gbec_status & PCH_GBE_RXD_GMAC_STAT_NOTOCTAL)) {
  1474. adapter->stats.rx_frame_errors++;
  1475. netdev_err(netdev, "Receive Not Octal Error\n");
  1476. } else if (unlikely(gbec_status &
  1477. PCH_GBE_RXD_GMAC_STAT_NBLERR)) {
  1478. adapter->stats.rx_frame_errors++;
  1479. netdev_err(netdev, "Receive Nibble Error\n");
  1480. } else if (unlikely(gbec_status &
  1481. PCH_GBE_RXD_GMAC_STAT_CRCERR)) {
  1482. adapter->stats.rx_crc_errors++;
  1483. netdev_err(netdev, "Receive CRC Error\n");
  1484. } else {
  1485. /* get receive length */
  1486. /* length convert[-3], length includes FCS length */
  1487. length = (rx_desc->rx_words_eob) - 3 - ETH_FCS_LEN;
  1488. if (rx_desc->rx_words_eob & 0x02)
  1489. length = length - 4;
  1490. /*
  1491. * buffer_info->rx_buffer: [Header:14][payload]
  1492. * skb->data: [Reserve:2][Header:14][payload]
  1493. */
  1494. memcpy(skb->data, buffer_info->rx_buffer, length);
  1495. /* update status of driver */
  1496. adapter->stats.rx_bytes += length;
  1497. adapter->stats.rx_packets++;
  1498. if ((gbec_status & PCH_GBE_RXD_GMAC_STAT_MARMLT))
  1499. adapter->stats.multicast++;
  1500. /* Write meta date of skb */
  1501. skb_put(skb, length);
  1502. pch_rx_timestamp(adapter, skb);
  1503. skb->protocol = eth_type_trans(skb, netdev);
  1504. if (tcp_ip_status & PCH_GBE_RXD_ACC_STAT_TCPIPOK)
  1505. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1506. else
  1507. skb->ip_summed = CHECKSUM_NONE;
  1508. napi_gro_receive(&adapter->napi, skb);
  1509. (*work_done)++;
  1510. netdev_dbg(netdev,
  1511. "Receive skb->ip_summed: %d length: %d\n",
  1512. skb->ip_summed, length);
  1513. }
  1514. /* return some buffers to hardware, one at a time is too slow */
  1515. if (unlikely(cleaned_count >= PCH_GBE_RX_BUFFER_WRITE)) {
  1516. pch_gbe_alloc_rx_buffers(adapter, rx_ring,
  1517. cleaned_count);
  1518. cleaned_count = 0;
  1519. }
  1520. if (++i == rx_ring->count)
  1521. i = 0;
  1522. }
  1523. rx_ring->next_to_clean = i;
  1524. if (cleaned_count)
  1525. pch_gbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
  1526. return cleaned;
  1527. }
  1528. /**
  1529. * pch_gbe_setup_tx_resources - Allocate Tx resources (Descriptors)
  1530. * @adapter: Board private structure
  1531. * @tx_ring: Tx descriptor ring (for a specific queue) to setup
  1532. * Returns:
  1533. * 0: Successfully
  1534. * Negative value: Failed
  1535. */
  1536. int pch_gbe_setup_tx_resources(struct pch_gbe_adapter *adapter,
  1537. struct pch_gbe_tx_ring *tx_ring)
  1538. {
  1539. struct pci_dev *pdev = adapter->pdev;
  1540. struct pch_gbe_tx_desc *tx_desc;
  1541. int size;
  1542. int desNo;
  1543. size = (int)sizeof(struct pch_gbe_buffer) * tx_ring->count;
  1544. tx_ring->buffer_info = vzalloc(size);
  1545. if (!tx_ring->buffer_info)
  1546. return -ENOMEM;
  1547. tx_ring->size = tx_ring->count * (int)sizeof(struct pch_gbe_tx_desc);
  1548. tx_ring->desc = dma_zalloc_coherent(&pdev->dev, tx_ring->size,
  1549. &tx_ring->dma, GFP_KERNEL);
  1550. if (!tx_ring->desc) {
  1551. vfree(tx_ring->buffer_info);
  1552. return -ENOMEM;
  1553. }
  1554. tx_ring->next_to_use = 0;
  1555. tx_ring->next_to_clean = 0;
  1556. for (desNo = 0; desNo < tx_ring->count; desNo++) {
  1557. tx_desc = PCH_GBE_TX_DESC(*tx_ring, desNo);
  1558. tx_desc->gbec_status = DSC_INIT16;
  1559. }
  1560. netdev_dbg(adapter->netdev,
  1561. "tx_ring->desc = 0x%p tx_ring->dma = 0x%08llx next_to_clean = 0x%08x next_to_use = 0x%08x\n",
  1562. tx_ring->desc, (unsigned long long)tx_ring->dma,
  1563. tx_ring->next_to_clean, tx_ring->next_to_use);
  1564. return 0;
  1565. }
  1566. /**
  1567. * pch_gbe_setup_rx_resources - Allocate Rx resources (Descriptors)
  1568. * @adapter: Board private structure
  1569. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  1570. * Returns:
  1571. * 0: Successfully
  1572. * Negative value: Failed
  1573. */
  1574. int pch_gbe_setup_rx_resources(struct pch_gbe_adapter *adapter,
  1575. struct pch_gbe_rx_ring *rx_ring)
  1576. {
  1577. struct pci_dev *pdev = adapter->pdev;
  1578. struct pch_gbe_rx_desc *rx_desc;
  1579. int size;
  1580. int desNo;
  1581. size = (int)sizeof(struct pch_gbe_buffer) * rx_ring->count;
  1582. rx_ring->buffer_info = vzalloc(size);
  1583. if (!rx_ring->buffer_info)
  1584. return -ENOMEM;
  1585. rx_ring->size = rx_ring->count * (int)sizeof(struct pch_gbe_rx_desc);
  1586. rx_ring->desc = dma_zalloc_coherent(&pdev->dev, rx_ring->size,
  1587. &rx_ring->dma, GFP_KERNEL);
  1588. if (!rx_ring->desc) {
  1589. vfree(rx_ring->buffer_info);
  1590. return -ENOMEM;
  1591. }
  1592. rx_ring->next_to_clean = 0;
  1593. rx_ring->next_to_use = 0;
  1594. for (desNo = 0; desNo < rx_ring->count; desNo++) {
  1595. rx_desc = PCH_GBE_RX_DESC(*rx_ring, desNo);
  1596. rx_desc->gbec_status = DSC_INIT16;
  1597. }
  1598. netdev_dbg(adapter->netdev,
  1599. "rx_ring->desc = 0x%p rx_ring->dma = 0x%08llx next_to_clean = 0x%08x next_to_use = 0x%08x\n",
  1600. rx_ring->desc, (unsigned long long)rx_ring->dma,
  1601. rx_ring->next_to_clean, rx_ring->next_to_use);
  1602. return 0;
  1603. }
  1604. /**
  1605. * pch_gbe_free_tx_resources - Free Tx Resources
  1606. * @adapter: Board private structure
  1607. * @tx_ring: Tx descriptor ring for a specific queue
  1608. */
  1609. void pch_gbe_free_tx_resources(struct pch_gbe_adapter *adapter,
  1610. struct pch_gbe_tx_ring *tx_ring)
  1611. {
  1612. struct pci_dev *pdev = adapter->pdev;
  1613. pch_gbe_clean_tx_ring(adapter, tx_ring);
  1614. vfree(tx_ring->buffer_info);
  1615. tx_ring->buffer_info = NULL;
  1616. pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
  1617. tx_ring->desc = NULL;
  1618. }
  1619. /**
  1620. * pch_gbe_free_rx_resources - Free Rx Resources
  1621. * @adapter: Board private structure
  1622. * @rx_ring: Ring to clean the resources from
  1623. */
  1624. void pch_gbe_free_rx_resources(struct pch_gbe_adapter *adapter,
  1625. struct pch_gbe_rx_ring *rx_ring)
  1626. {
  1627. struct pci_dev *pdev = adapter->pdev;
  1628. pch_gbe_clean_rx_ring(adapter, rx_ring);
  1629. vfree(rx_ring->buffer_info);
  1630. rx_ring->buffer_info = NULL;
  1631. pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
  1632. rx_ring->desc = NULL;
  1633. }
  1634. /**
  1635. * pch_gbe_request_irq - Allocate an interrupt line
  1636. * @adapter: Board private structure
  1637. * Returns:
  1638. * 0: Successfully
  1639. * Negative value: Failed
  1640. */
  1641. static int pch_gbe_request_irq(struct pch_gbe_adapter *adapter)
  1642. {
  1643. struct net_device *netdev = adapter->netdev;
  1644. int err;
  1645. err = pci_alloc_irq_vectors(adapter->pdev, 1, 1, PCI_IRQ_ALL_TYPES);
  1646. if (err < 0)
  1647. return err;
  1648. adapter->irq = pci_irq_vector(adapter->pdev, 0);
  1649. err = request_irq(adapter->irq, &pch_gbe_intr, IRQF_SHARED,
  1650. netdev->name, netdev);
  1651. if (err)
  1652. netdev_err(netdev, "Unable to allocate interrupt Error: %d\n",
  1653. err);
  1654. netdev_dbg(netdev, "have_msi : %d return : 0x%04x\n",
  1655. pci_dev_msi_enabled(adapter->pdev), err);
  1656. return err;
  1657. }
  1658. /**
  1659. * pch_gbe_up - Up GbE network device
  1660. * @adapter: Board private structure
  1661. * Returns:
  1662. * 0: Successfully
  1663. * Negative value: Failed
  1664. */
  1665. int pch_gbe_up(struct pch_gbe_adapter *adapter)
  1666. {
  1667. struct net_device *netdev = adapter->netdev;
  1668. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  1669. struct pch_gbe_rx_ring *rx_ring = adapter->rx_ring;
  1670. int err = -EINVAL;
  1671. /* Ensure we have a valid MAC */
  1672. if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
  1673. netdev_err(netdev, "Error: Invalid MAC address\n");
  1674. goto out;
  1675. }
  1676. /* hardware has been reset, we need to reload some things */
  1677. pch_gbe_set_multi(netdev);
  1678. pch_gbe_setup_tctl(adapter);
  1679. pch_gbe_configure_tx(adapter);
  1680. pch_gbe_setup_rctl(adapter);
  1681. pch_gbe_configure_rx(adapter);
  1682. err = pch_gbe_request_irq(adapter);
  1683. if (err) {
  1684. netdev_err(netdev,
  1685. "Error: can't bring device up - irq request failed\n");
  1686. goto out;
  1687. }
  1688. err = pch_gbe_alloc_rx_buffers_pool(adapter, rx_ring, rx_ring->count);
  1689. if (err) {
  1690. netdev_err(netdev,
  1691. "Error: can't bring device up - alloc rx buffers pool failed\n");
  1692. goto freeirq;
  1693. }
  1694. pch_gbe_alloc_tx_buffers(adapter, tx_ring);
  1695. pch_gbe_alloc_rx_buffers(adapter, rx_ring, rx_ring->count);
  1696. adapter->tx_queue_len = netdev->tx_queue_len;
  1697. pch_gbe_enable_dma_rx(&adapter->hw);
  1698. pch_gbe_enable_mac_rx(&adapter->hw);
  1699. mod_timer(&adapter->watchdog_timer, jiffies);
  1700. napi_enable(&adapter->napi);
  1701. pch_gbe_irq_enable(adapter);
  1702. netif_start_queue(adapter->netdev);
  1703. return 0;
  1704. freeirq:
  1705. pch_gbe_free_irq(adapter);
  1706. out:
  1707. return err;
  1708. }
  1709. /**
  1710. * pch_gbe_down - Down GbE network device
  1711. * @adapter: Board private structure
  1712. */
  1713. void pch_gbe_down(struct pch_gbe_adapter *adapter)
  1714. {
  1715. struct net_device *netdev = adapter->netdev;
  1716. struct pci_dev *pdev = adapter->pdev;
  1717. struct pch_gbe_rx_ring *rx_ring = adapter->rx_ring;
  1718. /* signal that we're down so the interrupt handler does not
  1719. * reschedule our watchdog timer */
  1720. napi_disable(&adapter->napi);
  1721. atomic_set(&adapter->irq_sem, 0);
  1722. pch_gbe_irq_disable(adapter);
  1723. pch_gbe_free_irq(adapter);
  1724. del_timer_sync(&adapter->watchdog_timer);
  1725. netdev->tx_queue_len = adapter->tx_queue_len;
  1726. netif_carrier_off(netdev);
  1727. netif_stop_queue(netdev);
  1728. if ((pdev->error_state) && (pdev->error_state != pci_channel_io_normal))
  1729. pch_gbe_reset(adapter);
  1730. pch_gbe_clean_tx_ring(adapter, adapter->tx_ring);
  1731. pch_gbe_clean_rx_ring(adapter, adapter->rx_ring);
  1732. pci_free_consistent(adapter->pdev, rx_ring->rx_buff_pool_size,
  1733. rx_ring->rx_buff_pool, rx_ring->rx_buff_pool_logic);
  1734. rx_ring->rx_buff_pool_logic = 0;
  1735. rx_ring->rx_buff_pool_size = 0;
  1736. rx_ring->rx_buff_pool = NULL;
  1737. }
  1738. /**
  1739. * pch_gbe_sw_init - Initialize general software structures (struct pch_gbe_adapter)
  1740. * @adapter: Board private structure to initialize
  1741. * Returns:
  1742. * 0: Successfully
  1743. * Negative value: Failed
  1744. */
  1745. static int pch_gbe_sw_init(struct pch_gbe_adapter *adapter)
  1746. {
  1747. struct pch_gbe_hw *hw = &adapter->hw;
  1748. struct net_device *netdev = adapter->netdev;
  1749. adapter->rx_buffer_len = PCH_GBE_FRAME_SIZE_2048;
  1750. hw->mac.max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  1751. hw->mac.min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  1752. hw->phy.reset_delay_us = PCH_GBE_PHY_RESET_DELAY_US;
  1753. if (pch_gbe_alloc_queues(adapter)) {
  1754. netdev_err(netdev, "Unable to allocate memory for queues\n");
  1755. return -ENOMEM;
  1756. }
  1757. spin_lock_init(&adapter->hw.miim_lock);
  1758. spin_lock_init(&adapter->stats_lock);
  1759. spin_lock_init(&adapter->ethtool_lock);
  1760. atomic_set(&adapter->irq_sem, 0);
  1761. pch_gbe_irq_disable(adapter);
  1762. pch_gbe_init_stats(adapter);
  1763. netdev_dbg(netdev,
  1764. "rx_buffer_len : %d mac.min_frame_size : %d mac.max_frame_size : %d\n",
  1765. (u32) adapter->rx_buffer_len,
  1766. hw->mac.min_frame_size, hw->mac.max_frame_size);
  1767. return 0;
  1768. }
  1769. /**
  1770. * pch_gbe_open - Called when a network interface is made active
  1771. * @netdev: Network interface device structure
  1772. * Returns:
  1773. * 0: Successfully
  1774. * Negative value: Failed
  1775. */
  1776. static int pch_gbe_open(struct net_device *netdev)
  1777. {
  1778. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1779. struct pch_gbe_hw *hw = &adapter->hw;
  1780. int err;
  1781. /* allocate transmit descriptors */
  1782. err = pch_gbe_setup_tx_resources(adapter, adapter->tx_ring);
  1783. if (err)
  1784. goto err_setup_tx;
  1785. /* allocate receive descriptors */
  1786. err = pch_gbe_setup_rx_resources(adapter, adapter->rx_ring);
  1787. if (err)
  1788. goto err_setup_rx;
  1789. pch_gbe_phy_power_up(hw);
  1790. err = pch_gbe_up(adapter);
  1791. if (err)
  1792. goto err_up;
  1793. netdev_dbg(netdev, "Success End\n");
  1794. return 0;
  1795. err_up:
  1796. if (!adapter->wake_up_evt)
  1797. pch_gbe_phy_power_down(hw);
  1798. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  1799. err_setup_rx:
  1800. pch_gbe_free_tx_resources(adapter, adapter->tx_ring);
  1801. err_setup_tx:
  1802. pch_gbe_reset(adapter);
  1803. netdev_err(netdev, "Error End\n");
  1804. return err;
  1805. }
  1806. /**
  1807. * pch_gbe_stop - Disables a network interface
  1808. * @netdev: Network interface device structure
  1809. * Returns:
  1810. * 0: Successfully
  1811. */
  1812. static int pch_gbe_stop(struct net_device *netdev)
  1813. {
  1814. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1815. struct pch_gbe_hw *hw = &adapter->hw;
  1816. pch_gbe_down(adapter);
  1817. if (!adapter->wake_up_evt)
  1818. pch_gbe_phy_power_down(hw);
  1819. pch_gbe_free_tx_resources(adapter, adapter->tx_ring);
  1820. pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
  1821. return 0;
  1822. }
  1823. /**
  1824. * pch_gbe_xmit_frame - Packet transmitting start
  1825. * @skb: Socket buffer structure
  1826. * @netdev: Network interface device structure
  1827. * Returns:
  1828. * - NETDEV_TX_OK: Normal end
  1829. * - NETDEV_TX_BUSY: Error end
  1830. */
  1831. static int pch_gbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1832. {
  1833. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1834. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  1835. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  1836. netif_stop_queue(netdev);
  1837. netdev_dbg(netdev,
  1838. "Return : BUSY next_to use : 0x%08x next_to clean : 0x%08x\n",
  1839. tx_ring->next_to_use, tx_ring->next_to_clean);
  1840. return NETDEV_TX_BUSY;
  1841. }
  1842. /* CRC,ITAG no support */
  1843. pch_gbe_tx_queue(adapter, tx_ring, skb);
  1844. return NETDEV_TX_OK;
  1845. }
  1846. /**
  1847. * pch_gbe_set_multi - Multicast and Promiscuous mode set
  1848. * @netdev: Network interface device structure
  1849. */
  1850. static void pch_gbe_set_multi(struct net_device *netdev)
  1851. {
  1852. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1853. struct pch_gbe_hw *hw = &adapter->hw;
  1854. struct netdev_hw_addr *ha;
  1855. u32 rctl, adrmask;
  1856. int mc_count, i;
  1857. netdev_dbg(netdev, "netdev->flags : 0x%08x\n", netdev->flags);
  1858. /* By default enable address & multicast filtering */
  1859. rctl = ioread32(&hw->reg->RX_MODE);
  1860. rctl |= PCH_GBE_ADD_FIL_EN | PCH_GBE_MLT_FIL_EN;
  1861. /* Promiscuous mode disables all hardware address filtering */
  1862. if (netdev->flags & IFF_PROMISC)
  1863. rctl &= ~(PCH_GBE_ADD_FIL_EN | PCH_GBE_MLT_FIL_EN);
  1864. /* If we want to monitor more multicast addresses than the hardware can
  1865. * support then disable hardware multicast filtering.
  1866. */
  1867. mc_count = netdev_mc_count(netdev);
  1868. if ((netdev->flags & IFF_ALLMULTI) || mc_count >= PCH_GBE_MAR_ENTRIES)
  1869. rctl &= ~PCH_GBE_MLT_FIL_EN;
  1870. iowrite32(rctl, &hw->reg->RX_MODE);
  1871. /* If we're not using multicast filtering then there's no point
  1872. * configuring the unused MAC address registers.
  1873. */
  1874. if (!(rctl & PCH_GBE_MLT_FIL_EN))
  1875. return;
  1876. /* Load the first set of multicast addresses into MAC address registers
  1877. * for use by hardware filtering.
  1878. */
  1879. i = 1;
  1880. netdev_for_each_mc_addr(ha, netdev)
  1881. pch_gbe_mac_mar_set(hw, ha->addr, i++);
  1882. /* If there are spare MAC registers, mask & clear them */
  1883. for (; i < PCH_GBE_MAR_ENTRIES; i++) {
  1884. /* Clear MAC address mask */
  1885. adrmask = ioread32(&hw->reg->ADDR_MASK);
  1886. iowrite32(adrmask | BIT(i), &hw->reg->ADDR_MASK);
  1887. /* wait busy */
  1888. pch_gbe_wait_clr_bit(&hw->reg->ADDR_MASK, PCH_GBE_BUSY);
  1889. /* Clear MAC address */
  1890. iowrite32(0, &hw->reg->mac_adr[i].high);
  1891. iowrite32(0, &hw->reg->mac_adr[i].low);
  1892. }
  1893. netdev_dbg(netdev,
  1894. "RX_MODE reg(check bit31,30 ADD,MLT) : 0x%08x netdev->mc_count : 0x%08x\n",
  1895. ioread32(&hw->reg->RX_MODE), mc_count);
  1896. }
  1897. /**
  1898. * pch_gbe_set_mac - Change the Ethernet Address of the NIC
  1899. * @netdev: Network interface device structure
  1900. * @addr: Pointer to an address structure
  1901. * Returns:
  1902. * 0: Successfully
  1903. * -EADDRNOTAVAIL: Failed
  1904. */
  1905. static int pch_gbe_set_mac(struct net_device *netdev, void *addr)
  1906. {
  1907. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1908. struct sockaddr *skaddr = addr;
  1909. int ret_val;
  1910. if (!is_valid_ether_addr(skaddr->sa_data)) {
  1911. ret_val = -EADDRNOTAVAIL;
  1912. } else {
  1913. memcpy(netdev->dev_addr, skaddr->sa_data, netdev->addr_len);
  1914. memcpy(adapter->hw.mac.addr, skaddr->sa_data, netdev->addr_len);
  1915. pch_gbe_mac_mar_set(&adapter->hw, adapter->hw.mac.addr, 0);
  1916. ret_val = 0;
  1917. }
  1918. netdev_dbg(netdev, "ret_val : 0x%08x\n", ret_val);
  1919. netdev_dbg(netdev, "dev_addr : %pM\n", netdev->dev_addr);
  1920. netdev_dbg(netdev, "mac_addr : %pM\n", adapter->hw.mac.addr);
  1921. netdev_dbg(netdev, "MAC_ADR1AB reg : 0x%08x 0x%08x\n",
  1922. ioread32(&adapter->hw.reg->mac_adr[0].high),
  1923. ioread32(&adapter->hw.reg->mac_adr[0].low));
  1924. return ret_val;
  1925. }
  1926. /**
  1927. * pch_gbe_change_mtu - Change the Maximum Transfer Unit
  1928. * @netdev: Network interface device structure
  1929. * @new_mtu: New value for maximum frame size
  1930. * Returns:
  1931. * 0: Successfully
  1932. * -EINVAL: Failed
  1933. */
  1934. static int pch_gbe_change_mtu(struct net_device *netdev, int new_mtu)
  1935. {
  1936. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1937. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  1938. unsigned long old_rx_buffer_len = adapter->rx_buffer_len;
  1939. int err;
  1940. if (max_frame <= PCH_GBE_FRAME_SIZE_2048)
  1941. adapter->rx_buffer_len = PCH_GBE_FRAME_SIZE_2048;
  1942. else if (max_frame <= PCH_GBE_FRAME_SIZE_4096)
  1943. adapter->rx_buffer_len = PCH_GBE_FRAME_SIZE_4096;
  1944. else if (max_frame <= PCH_GBE_FRAME_SIZE_8192)
  1945. adapter->rx_buffer_len = PCH_GBE_FRAME_SIZE_8192;
  1946. else
  1947. adapter->rx_buffer_len = PCH_GBE_MAX_RX_BUFFER_SIZE;
  1948. if (netif_running(netdev)) {
  1949. pch_gbe_down(adapter);
  1950. err = pch_gbe_up(adapter);
  1951. if (err) {
  1952. adapter->rx_buffer_len = old_rx_buffer_len;
  1953. pch_gbe_up(adapter);
  1954. return err;
  1955. } else {
  1956. netdev->mtu = new_mtu;
  1957. adapter->hw.mac.max_frame_size = max_frame;
  1958. }
  1959. } else {
  1960. pch_gbe_reset(adapter);
  1961. netdev->mtu = new_mtu;
  1962. adapter->hw.mac.max_frame_size = max_frame;
  1963. }
  1964. netdev_dbg(netdev,
  1965. "max_frame : %d rx_buffer_len : %d mtu : %d max_frame_size : %d\n",
  1966. max_frame, (u32) adapter->rx_buffer_len, netdev->mtu,
  1967. adapter->hw.mac.max_frame_size);
  1968. return 0;
  1969. }
  1970. /**
  1971. * pch_gbe_set_features - Reset device after features changed
  1972. * @netdev: Network interface device structure
  1973. * @features: New features
  1974. * Returns:
  1975. * 0: HW state updated successfully
  1976. */
  1977. static int pch_gbe_set_features(struct net_device *netdev,
  1978. netdev_features_t features)
  1979. {
  1980. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  1981. netdev_features_t changed = features ^ netdev->features;
  1982. if (!(changed & NETIF_F_RXCSUM))
  1983. return 0;
  1984. if (netif_running(netdev))
  1985. pch_gbe_reinit_locked(adapter);
  1986. else
  1987. pch_gbe_reset(adapter);
  1988. return 0;
  1989. }
  1990. /**
  1991. * pch_gbe_ioctl - Controls register through a MII interface
  1992. * @netdev: Network interface device structure
  1993. * @ifr: Pointer to ifr structure
  1994. * @cmd: Control command
  1995. * Returns:
  1996. * 0: Successfully
  1997. * Negative value: Failed
  1998. */
  1999. static int pch_gbe_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2000. {
  2001. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2002. netdev_dbg(netdev, "cmd : 0x%04x\n", cmd);
  2003. if (cmd == SIOCSHWTSTAMP)
  2004. return hwtstamp_ioctl(netdev, ifr, cmd);
  2005. return generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
  2006. }
  2007. /**
  2008. * pch_gbe_tx_timeout - Respond to a Tx Hang
  2009. * @netdev: Network interface device structure
  2010. */
  2011. static void pch_gbe_tx_timeout(struct net_device *netdev)
  2012. {
  2013. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2014. /* Do the reset outside of interrupt context */
  2015. adapter->stats.tx_timeout_count++;
  2016. schedule_work(&adapter->reset_task);
  2017. }
  2018. /**
  2019. * pch_gbe_napi_poll - NAPI receive and transfer polling callback
  2020. * @napi: Pointer of polling device struct
  2021. * @budget: The maximum number of a packet
  2022. * Returns:
  2023. * false: Exit the polling mode
  2024. * true: Continue the polling mode
  2025. */
  2026. static int pch_gbe_napi_poll(struct napi_struct *napi, int budget)
  2027. {
  2028. struct pch_gbe_adapter *adapter =
  2029. container_of(napi, struct pch_gbe_adapter, napi);
  2030. int work_done = 0;
  2031. bool poll_end_flag = false;
  2032. bool cleaned = false;
  2033. netdev_dbg(adapter->netdev, "budget : %d\n", budget);
  2034. pch_gbe_clean_rx(adapter, adapter->rx_ring, &work_done, budget);
  2035. cleaned = pch_gbe_clean_tx(adapter, adapter->tx_ring);
  2036. if (cleaned)
  2037. work_done = budget;
  2038. /* If no Tx and not enough Rx work done,
  2039. * exit the polling mode
  2040. */
  2041. if (work_done < budget)
  2042. poll_end_flag = true;
  2043. if (poll_end_flag) {
  2044. napi_complete_done(napi, work_done);
  2045. pch_gbe_irq_enable(adapter);
  2046. }
  2047. if (adapter->rx_stop_flag) {
  2048. adapter->rx_stop_flag = false;
  2049. pch_gbe_enable_dma_rx(&adapter->hw);
  2050. }
  2051. netdev_dbg(adapter->netdev,
  2052. "poll_end_flag : %d work_done : %d budget : %d\n",
  2053. poll_end_flag, work_done, budget);
  2054. return work_done;
  2055. }
  2056. #ifdef CONFIG_NET_POLL_CONTROLLER
  2057. /**
  2058. * pch_gbe_netpoll - Used by things like netconsole to send skbs
  2059. * @netdev: Network interface device structure
  2060. */
  2061. static void pch_gbe_netpoll(struct net_device *netdev)
  2062. {
  2063. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2064. disable_irq(adapter->irq);
  2065. pch_gbe_intr(adapter->irq, netdev);
  2066. enable_irq(adapter->irq);
  2067. }
  2068. #endif
  2069. static const struct net_device_ops pch_gbe_netdev_ops = {
  2070. .ndo_open = pch_gbe_open,
  2071. .ndo_stop = pch_gbe_stop,
  2072. .ndo_start_xmit = pch_gbe_xmit_frame,
  2073. .ndo_set_mac_address = pch_gbe_set_mac,
  2074. .ndo_tx_timeout = pch_gbe_tx_timeout,
  2075. .ndo_change_mtu = pch_gbe_change_mtu,
  2076. .ndo_set_features = pch_gbe_set_features,
  2077. .ndo_do_ioctl = pch_gbe_ioctl,
  2078. .ndo_set_rx_mode = pch_gbe_set_multi,
  2079. #ifdef CONFIG_NET_POLL_CONTROLLER
  2080. .ndo_poll_controller = pch_gbe_netpoll,
  2081. #endif
  2082. };
  2083. static pci_ers_result_t pch_gbe_io_error_detected(struct pci_dev *pdev,
  2084. pci_channel_state_t state)
  2085. {
  2086. struct net_device *netdev = pci_get_drvdata(pdev);
  2087. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2088. netif_device_detach(netdev);
  2089. if (netif_running(netdev))
  2090. pch_gbe_down(adapter);
  2091. pci_disable_device(pdev);
  2092. /* Request a slot slot reset. */
  2093. return PCI_ERS_RESULT_NEED_RESET;
  2094. }
  2095. static pci_ers_result_t pch_gbe_io_slot_reset(struct pci_dev *pdev)
  2096. {
  2097. struct net_device *netdev = pci_get_drvdata(pdev);
  2098. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2099. struct pch_gbe_hw *hw = &adapter->hw;
  2100. if (pci_enable_device(pdev)) {
  2101. netdev_err(netdev, "Cannot re-enable PCI device after reset\n");
  2102. return PCI_ERS_RESULT_DISCONNECT;
  2103. }
  2104. pci_set_master(pdev);
  2105. pci_enable_wake(pdev, PCI_D0, 0);
  2106. pch_gbe_phy_power_up(hw);
  2107. pch_gbe_reset(adapter);
  2108. /* Clear wake up status */
  2109. pch_gbe_mac_set_wol_event(hw, 0);
  2110. return PCI_ERS_RESULT_RECOVERED;
  2111. }
  2112. static void pch_gbe_io_resume(struct pci_dev *pdev)
  2113. {
  2114. struct net_device *netdev = pci_get_drvdata(pdev);
  2115. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2116. if (netif_running(netdev)) {
  2117. if (pch_gbe_up(adapter)) {
  2118. netdev_dbg(netdev,
  2119. "can't bring device back up after reset\n");
  2120. return;
  2121. }
  2122. }
  2123. netif_device_attach(netdev);
  2124. }
  2125. static int __pch_gbe_suspend(struct pci_dev *pdev)
  2126. {
  2127. struct net_device *netdev = pci_get_drvdata(pdev);
  2128. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2129. struct pch_gbe_hw *hw = &adapter->hw;
  2130. u32 wufc = adapter->wake_up_evt;
  2131. int retval = 0;
  2132. netif_device_detach(netdev);
  2133. if (netif_running(netdev))
  2134. pch_gbe_down(adapter);
  2135. if (wufc) {
  2136. pch_gbe_set_multi(netdev);
  2137. pch_gbe_setup_rctl(adapter);
  2138. pch_gbe_configure_rx(adapter);
  2139. pch_gbe_set_rgmii_ctrl(adapter, hw->mac.link_speed,
  2140. hw->mac.link_duplex);
  2141. pch_gbe_set_mode(adapter, hw->mac.link_speed,
  2142. hw->mac.link_duplex);
  2143. pch_gbe_mac_set_wol_event(hw, wufc);
  2144. pci_disable_device(pdev);
  2145. } else {
  2146. pch_gbe_phy_power_down(hw);
  2147. pch_gbe_mac_set_wol_event(hw, wufc);
  2148. pci_disable_device(pdev);
  2149. }
  2150. return retval;
  2151. }
  2152. #ifdef CONFIG_PM
  2153. static int pch_gbe_suspend(struct device *device)
  2154. {
  2155. struct pci_dev *pdev = to_pci_dev(device);
  2156. return __pch_gbe_suspend(pdev);
  2157. }
  2158. static int pch_gbe_resume(struct device *device)
  2159. {
  2160. struct pci_dev *pdev = to_pci_dev(device);
  2161. struct net_device *netdev = pci_get_drvdata(pdev);
  2162. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2163. struct pch_gbe_hw *hw = &adapter->hw;
  2164. u32 err;
  2165. err = pci_enable_device(pdev);
  2166. if (err) {
  2167. netdev_err(netdev, "Cannot enable PCI device from suspend\n");
  2168. return err;
  2169. }
  2170. pci_set_master(pdev);
  2171. pch_gbe_phy_power_up(hw);
  2172. pch_gbe_reset(adapter);
  2173. /* Clear wake on lan control and status */
  2174. pch_gbe_mac_set_wol_event(hw, 0);
  2175. if (netif_running(netdev))
  2176. pch_gbe_up(adapter);
  2177. netif_device_attach(netdev);
  2178. return 0;
  2179. }
  2180. #endif /* CONFIG_PM */
  2181. static void pch_gbe_shutdown(struct pci_dev *pdev)
  2182. {
  2183. __pch_gbe_suspend(pdev);
  2184. if (system_state == SYSTEM_POWER_OFF) {
  2185. pci_wake_from_d3(pdev, true);
  2186. pci_set_power_state(pdev, PCI_D3hot);
  2187. }
  2188. }
  2189. static void pch_gbe_remove(struct pci_dev *pdev)
  2190. {
  2191. struct net_device *netdev = pci_get_drvdata(pdev);
  2192. struct pch_gbe_adapter *adapter = netdev_priv(netdev);
  2193. cancel_work_sync(&adapter->reset_task);
  2194. unregister_netdev(netdev);
  2195. pch_gbe_phy_hw_reset(&adapter->hw);
  2196. free_netdev(netdev);
  2197. }
  2198. static int pch_gbe_probe(struct pci_dev *pdev,
  2199. const struct pci_device_id *pci_id)
  2200. {
  2201. struct net_device *netdev;
  2202. struct pch_gbe_adapter *adapter;
  2203. int ret;
  2204. ret = pcim_enable_device(pdev);
  2205. if (ret)
  2206. return ret;
  2207. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
  2208. || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  2209. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2210. if (ret) {
  2211. ret = pci_set_consistent_dma_mask(pdev,
  2212. DMA_BIT_MASK(32));
  2213. if (ret) {
  2214. dev_err(&pdev->dev, "ERR: No usable DMA "
  2215. "configuration, aborting\n");
  2216. return ret;
  2217. }
  2218. }
  2219. }
  2220. ret = pcim_iomap_regions(pdev, 1 << PCH_GBE_PCI_BAR, pci_name(pdev));
  2221. if (ret) {
  2222. dev_err(&pdev->dev,
  2223. "ERR: Can't reserve PCI I/O and memory resources\n");
  2224. return ret;
  2225. }
  2226. pci_set_master(pdev);
  2227. netdev = alloc_etherdev((int)sizeof(struct pch_gbe_adapter));
  2228. if (!netdev)
  2229. return -ENOMEM;
  2230. SET_NETDEV_DEV(netdev, &pdev->dev);
  2231. pci_set_drvdata(pdev, netdev);
  2232. adapter = netdev_priv(netdev);
  2233. adapter->netdev = netdev;
  2234. adapter->pdev = pdev;
  2235. adapter->hw.back = adapter;
  2236. adapter->hw.reg = pcim_iomap_table(pdev)[PCH_GBE_PCI_BAR];
  2237. adapter->pdata = (struct pch_gbe_privdata *)pci_id->driver_data;
  2238. if (adapter->pdata && adapter->pdata->platform_init)
  2239. adapter->pdata->platform_init(pdev);
  2240. adapter->ptp_pdev =
  2241. pci_get_domain_bus_and_slot(pci_domain_nr(adapter->pdev->bus),
  2242. adapter->pdev->bus->number,
  2243. PCI_DEVFN(12, 4));
  2244. netdev->netdev_ops = &pch_gbe_netdev_ops;
  2245. netdev->watchdog_timeo = PCH_GBE_WATCHDOG_PERIOD;
  2246. netif_napi_add(netdev, &adapter->napi,
  2247. pch_gbe_napi_poll, PCH_GBE_RX_WEIGHT);
  2248. netdev->hw_features = NETIF_F_RXCSUM |
  2249. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  2250. netdev->features = netdev->hw_features;
  2251. pch_gbe_set_ethtool_ops(netdev);
  2252. /* MTU range: 46 - 10300 */
  2253. netdev->min_mtu = ETH_ZLEN - ETH_HLEN;
  2254. netdev->max_mtu = PCH_GBE_MAX_JUMBO_FRAME_SIZE -
  2255. (ETH_HLEN + ETH_FCS_LEN);
  2256. pch_gbe_mac_load_mac_addr(&adapter->hw);
  2257. pch_gbe_mac_reset_hw(&adapter->hw);
  2258. /* setup the private structure */
  2259. ret = pch_gbe_sw_init(adapter);
  2260. if (ret)
  2261. goto err_free_netdev;
  2262. /* Initialize PHY */
  2263. ret = pch_gbe_init_phy(adapter);
  2264. if (ret) {
  2265. dev_err(&pdev->dev, "PHY initialize error\n");
  2266. goto err_free_adapter;
  2267. }
  2268. /* Read the MAC address. and store to the private data */
  2269. ret = pch_gbe_mac_read_mac_addr(&adapter->hw);
  2270. if (ret) {
  2271. dev_err(&pdev->dev, "MAC address Read Error\n");
  2272. goto err_free_adapter;
  2273. }
  2274. memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
  2275. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2276. /*
  2277. * If the MAC is invalid (or just missing), display a warning
  2278. * but do not abort setting up the device. pch_gbe_up will
  2279. * prevent the interface from being brought up until a valid MAC
  2280. * is set.
  2281. */
  2282. dev_err(&pdev->dev, "Invalid MAC address, "
  2283. "interface disabled.\n");
  2284. }
  2285. timer_setup(&adapter->watchdog_timer, pch_gbe_watchdog, 0);
  2286. INIT_WORK(&adapter->reset_task, pch_gbe_reset_task);
  2287. pch_gbe_check_options(adapter);
  2288. /* initialize the wol settings based on the eeprom settings */
  2289. adapter->wake_up_evt = PCH_GBE_WL_INIT_SETTING;
  2290. dev_info(&pdev->dev, "MAC address : %pM\n", netdev->dev_addr);
  2291. /* reset the hardware with the new settings */
  2292. pch_gbe_reset(adapter);
  2293. ret = register_netdev(netdev);
  2294. if (ret)
  2295. goto err_free_adapter;
  2296. /* tell the stack to leave us alone until pch_gbe_open() is called */
  2297. netif_carrier_off(netdev);
  2298. netif_stop_queue(netdev);
  2299. dev_dbg(&pdev->dev, "PCH Network Connection\n");
  2300. /* Disable hibernation on certain platforms */
  2301. if (adapter->pdata && adapter->pdata->phy_disable_hibernate)
  2302. pch_gbe_phy_disable_hibernate(&adapter->hw);
  2303. device_set_wakeup_enable(&pdev->dev, 1);
  2304. return 0;
  2305. err_free_adapter:
  2306. pch_gbe_phy_hw_reset(&adapter->hw);
  2307. err_free_netdev:
  2308. free_netdev(netdev);
  2309. return ret;
  2310. }
  2311. /* The AR803X PHY on the MinnowBoard requires a physical pin to be toggled to
  2312. * ensure it is awake for probe and init. Request the line and reset the PHY.
  2313. */
  2314. static int pch_gbe_minnow_platform_init(struct pci_dev *pdev)
  2315. {
  2316. unsigned long flags = GPIOF_DIR_OUT | GPIOF_INIT_HIGH | GPIOF_EXPORT;
  2317. unsigned gpio = MINNOW_PHY_RESET_GPIO;
  2318. int ret;
  2319. ret = devm_gpio_request_one(&pdev->dev, gpio, flags,
  2320. "minnow_phy_reset");
  2321. if (ret) {
  2322. dev_err(&pdev->dev,
  2323. "ERR: Can't request PHY reset GPIO line '%d'\n", gpio);
  2324. return ret;
  2325. }
  2326. gpio_set_value(gpio, 0);
  2327. usleep_range(1250, 1500);
  2328. gpio_set_value(gpio, 1);
  2329. usleep_range(1250, 1500);
  2330. return ret;
  2331. }
  2332. static struct pch_gbe_privdata pch_gbe_minnow_privdata = {
  2333. .phy_tx_clk_delay = true,
  2334. .phy_disable_hibernate = true,
  2335. .platform_init = pch_gbe_minnow_platform_init,
  2336. };
  2337. static const struct pci_device_id pch_gbe_pcidev_id[] = {
  2338. {.vendor = PCI_VENDOR_ID_INTEL,
  2339. .device = PCI_DEVICE_ID_INTEL_IOH1_GBE,
  2340. .subvendor = PCI_VENDOR_ID_CIRCUITCO,
  2341. .subdevice = PCI_SUBSYSTEM_ID_CIRCUITCO_MINNOWBOARD,
  2342. .class = (PCI_CLASS_NETWORK_ETHERNET << 8),
  2343. .class_mask = (0xFFFF00),
  2344. .driver_data = (kernel_ulong_t)&pch_gbe_minnow_privdata
  2345. },
  2346. {.vendor = PCI_VENDOR_ID_INTEL,
  2347. .device = PCI_DEVICE_ID_INTEL_IOH1_GBE,
  2348. .subvendor = PCI_ANY_ID,
  2349. .subdevice = PCI_ANY_ID,
  2350. .class = (PCI_CLASS_NETWORK_ETHERNET << 8),
  2351. .class_mask = (0xFFFF00)
  2352. },
  2353. {.vendor = PCI_VENDOR_ID_ROHM,
  2354. .device = PCI_DEVICE_ID_ROHM_ML7223_GBE,
  2355. .subvendor = PCI_ANY_ID,
  2356. .subdevice = PCI_ANY_ID,
  2357. .class = (PCI_CLASS_NETWORK_ETHERNET << 8),
  2358. .class_mask = (0xFFFF00)
  2359. },
  2360. {.vendor = PCI_VENDOR_ID_ROHM,
  2361. .device = PCI_DEVICE_ID_ROHM_ML7831_GBE,
  2362. .subvendor = PCI_ANY_ID,
  2363. .subdevice = PCI_ANY_ID,
  2364. .class = (PCI_CLASS_NETWORK_ETHERNET << 8),
  2365. .class_mask = (0xFFFF00)
  2366. },
  2367. /* required last entry */
  2368. {0}
  2369. };
  2370. #ifdef CONFIG_PM
  2371. static const struct dev_pm_ops pch_gbe_pm_ops = {
  2372. .suspend = pch_gbe_suspend,
  2373. .resume = pch_gbe_resume,
  2374. .freeze = pch_gbe_suspend,
  2375. .thaw = pch_gbe_resume,
  2376. .poweroff = pch_gbe_suspend,
  2377. .restore = pch_gbe_resume,
  2378. };
  2379. #endif
  2380. static const struct pci_error_handlers pch_gbe_err_handler = {
  2381. .error_detected = pch_gbe_io_error_detected,
  2382. .slot_reset = pch_gbe_io_slot_reset,
  2383. .resume = pch_gbe_io_resume
  2384. };
  2385. static struct pci_driver pch_gbe_driver = {
  2386. .name = KBUILD_MODNAME,
  2387. .id_table = pch_gbe_pcidev_id,
  2388. .probe = pch_gbe_probe,
  2389. .remove = pch_gbe_remove,
  2390. #ifdef CONFIG_PM
  2391. .driver.pm = &pch_gbe_pm_ops,
  2392. #endif
  2393. .shutdown = pch_gbe_shutdown,
  2394. .err_handler = &pch_gbe_err_handler
  2395. };
  2396. module_pci_driver(pch_gbe_driver);
  2397. MODULE_DESCRIPTION("EG20T PCH Gigabit ethernet Driver");
  2398. MODULE_AUTHOR("LAPIS SEMICONDUCTOR, <tshimizu818@gmail.com>");
  2399. MODULE_LICENSE("GPL");
  2400. MODULE_VERSION(DRV_VERSION);
  2401. MODULE_DEVICE_TABLE(pci, pch_gbe_pcidev_id);
  2402. /* pch_gbe_main.c */