sfp.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/debugfs.h>
  3. #include <linux/delay.h>
  4. #include <linux/gpio/consumer.h>
  5. #include <linux/hwmon.h>
  6. #include <linux/i2c.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/mdio/mdio-i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/of.h>
  13. #include <linux/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. #include "sfp.h"
  19. #include "swphy.h"
  20. enum {
  21. GPIO_MODDEF0,
  22. GPIO_LOS,
  23. GPIO_TX_FAULT,
  24. GPIO_TX_DISABLE,
  25. GPIO_RS0,
  26. GPIO_RS1,
  27. GPIO_MAX,
  28. SFP_F_PRESENT = BIT(GPIO_MODDEF0),
  29. SFP_F_LOS = BIT(GPIO_LOS),
  30. SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
  31. SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
  32. SFP_F_RS0 = BIT(GPIO_RS0),
  33. SFP_F_RS1 = BIT(GPIO_RS1),
  34. SFP_F_OUTPUTS = SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1,
  35. SFP_E_INSERT = 0,
  36. SFP_E_REMOVE,
  37. SFP_E_DEV_ATTACH,
  38. SFP_E_DEV_DETACH,
  39. SFP_E_DEV_DOWN,
  40. SFP_E_DEV_UP,
  41. SFP_E_TX_FAULT,
  42. SFP_E_TX_CLEAR,
  43. SFP_E_LOS_HIGH,
  44. SFP_E_LOS_LOW,
  45. SFP_E_TIMEOUT,
  46. SFP_MOD_EMPTY = 0,
  47. SFP_MOD_ERROR,
  48. SFP_MOD_PROBE,
  49. SFP_MOD_WAITDEV,
  50. SFP_MOD_HPOWER,
  51. SFP_MOD_WAITPWR,
  52. SFP_MOD_PRESENT,
  53. SFP_DEV_DETACHED = 0,
  54. SFP_DEV_DOWN,
  55. SFP_DEV_UP,
  56. SFP_S_DOWN = 0,
  57. SFP_S_FAIL,
  58. SFP_S_WAIT,
  59. SFP_S_INIT,
  60. SFP_S_INIT_PHY,
  61. SFP_S_INIT_TX_FAULT,
  62. SFP_S_WAIT_LOS,
  63. SFP_S_LINK_UP,
  64. SFP_S_TX_FAULT,
  65. SFP_S_REINIT,
  66. SFP_S_TX_DISABLE,
  67. };
  68. static const char * const mod_state_strings[] = {
  69. [SFP_MOD_EMPTY] = "empty",
  70. [SFP_MOD_ERROR] = "error",
  71. [SFP_MOD_PROBE] = "probe",
  72. [SFP_MOD_WAITDEV] = "waitdev",
  73. [SFP_MOD_HPOWER] = "hpower",
  74. [SFP_MOD_WAITPWR] = "waitpwr",
  75. [SFP_MOD_PRESENT] = "present",
  76. };
  77. static const char *mod_state_to_str(unsigned short mod_state)
  78. {
  79. if (mod_state >= ARRAY_SIZE(mod_state_strings))
  80. return "Unknown module state";
  81. return mod_state_strings[mod_state];
  82. }
  83. static const char * const dev_state_strings[] = {
  84. [SFP_DEV_DETACHED] = "detached",
  85. [SFP_DEV_DOWN] = "down",
  86. [SFP_DEV_UP] = "up",
  87. };
  88. static const char *dev_state_to_str(unsigned short dev_state)
  89. {
  90. if (dev_state >= ARRAY_SIZE(dev_state_strings))
  91. return "Unknown device state";
  92. return dev_state_strings[dev_state];
  93. }
  94. static const char * const event_strings[] = {
  95. [SFP_E_INSERT] = "insert",
  96. [SFP_E_REMOVE] = "remove",
  97. [SFP_E_DEV_ATTACH] = "dev_attach",
  98. [SFP_E_DEV_DETACH] = "dev_detach",
  99. [SFP_E_DEV_DOWN] = "dev_down",
  100. [SFP_E_DEV_UP] = "dev_up",
  101. [SFP_E_TX_FAULT] = "tx_fault",
  102. [SFP_E_TX_CLEAR] = "tx_clear",
  103. [SFP_E_LOS_HIGH] = "los_high",
  104. [SFP_E_LOS_LOW] = "los_low",
  105. [SFP_E_TIMEOUT] = "timeout",
  106. };
  107. static const char *event_to_str(unsigned short event)
  108. {
  109. if (event >= ARRAY_SIZE(event_strings))
  110. return "Unknown event";
  111. return event_strings[event];
  112. }
  113. static const char * const sm_state_strings[] = {
  114. [SFP_S_DOWN] = "down",
  115. [SFP_S_FAIL] = "fail",
  116. [SFP_S_WAIT] = "wait",
  117. [SFP_S_INIT] = "init",
  118. [SFP_S_INIT_PHY] = "init_phy",
  119. [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
  120. [SFP_S_WAIT_LOS] = "wait_los",
  121. [SFP_S_LINK_UP] = "link_up",
  122. [SFP_S_TX_FAULT] = "tx_fault",
  123. [SFP_S_REINIT] = "reinit",
  124. [SFP_S_TX_DISABLE] = "tx_disable",
  125. };
  126. static const char *sm_state_to_str(unsigned short sm_state)
  127. {
  128. if (sm_state >= ARRAY_SIZE(sm_state_strings))
  129. return "Unknown state";
  130. return sm_state_strings[sm_state];
  131. }
  132. static const char *gpio_names[] = {
  133. "mod-def0",
  134. "los",
  135. "tx-fault",
  136. "tx-disable",
  137. "rate-select0",
  138. "rate-select1",
  139. };
  140. static const enum gpiod_flags gpio_flags[] = {
  141. GPIOD_IN,
  142. GPIOD_IN,
  143. GPIOD_IN,
  144. GPIOD_ASIS,
  145. GPIOD_ASIS,
  146. GPIOD_ASIS,
  147. };
  148. /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a
  149. * non-cooled module to initialise its laser safety circuitry. We wait
  150. * an initial T_WAIT period before we check the tx fault to give any PHY
  151. * on board (for a copper SFP) time to initialise.
  152. */
  153. #define T_WAIT msecs_to_jiffies(50)
  154. #define T_START_UP msecs_to_jiffies(300)
  155. #define T_START_UP_BAD_GPON msecs_to_jiffies(60000)
  156. /* t_reset is the time required to assert the TX_DISABLE signal to reset
  157. * an indicated TX_FAULT.
  158. */
  159. #define T_RESET_US 10
  160. #define T_FAULT_RECOVER msecs_to_jiffies(1000)
  161. /* N_FAULT_INIT is the number of recovery attempts at module initialisation
  162. * time. If the TX_FAULT signal is not deasserted after this number of
  163. * attempts at clearing it, we decide that the module is faulty.
  164. * N_FAULT is the same but after the module has initialised.
  165. */
  166. #define N_FAULT_INIT 5
  167. #define N_FAULT 5
  168. /* T_PHY_RETRY is the time interval between attempts to probe the PHY.
  169. * R_PHY_RETRY is the number of attempts.
  170. */
  171. #define T_PHY_RETRY msecs_to_jiffies(50)
  172. #define R_PHY_RETRY 25
  173. /* SFP module presence detection is poor: the three MOD DEF signals are
  174. * the same length on the PCB, which means it's possible for MOD DEF 0 to
  175. * connect before the I2C bus on MOD DEF 1/2.
  176. *
  177. * The SFF-8472 specifies t_serial ("Time from power on until module is
  178. * ready for data transmission over the two wire serial bus.") as 300ms.
  179. */
  180. #define T_SERIAL msecs_to_jiffies(300)
  181. #define T_HPOWER_LEVEL msecs_to_jiffies(300)
  182. #define T_PROBE_RETRY_INIT msecs_to_jiffies(100)
  183. #define R_PROBE_RETRY_INIT 10
  184. #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000)
  185. #define R_PROBE_RETRY_SLOW 12
  186. /* SFP modules appear to always have their PHY configured for bus address
  187. * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
  188. * RollBall SFPs access phy via SFP Enhanced Digital Diagnostic Interface
  189. * via address 0x51 (mdio-i2c will use RollBall protocol on this address).
  190. */
  191. #define SFP_PHY_ADDR 22
  192. #define SFP_PHY_ADDR_ROLLBALL 17
  193. /* SFP_EEPROM_BLOCK_SIZE is the size of data chunk to read the EEPROM
  194. * at a time. Some SFP modules and also some Linux I2C drivers do not like
  195. * reads longer than 16 bytes.
  196. */
  197. #define SFP_EEPROM_BLOCK_SIZE 16
  198. struct sff_data {
  199. unsigned int gpios;
  200. bool (*module_supported)(const struct sfp_eeprom_id *id);
  201. };
  202. struct sfp {
  203. struct device *dev;
  204. struct i2c_adapter *i2c;
  205. struct mii_bus *i2c_mii;
  206. struct sfp_bus *sfp_bus;
  207. enum mdio_i2c_proto mdio_protocol;
  208. struct phy_device *mod_phy;
  209. const struct sff_data *type;
  210. size_t i2c_block_size;
  211. u32 max_power_mW;
  212. unsigned int (*get_state)(struct sfp *);
  213. void (*set_state)(struct sfp *, unsigned int);
  214. int (*read)(struct sfp *, bool, u8, void *, size_t);
  215. int (*write)(struct sfp *, bool, u8, void *, size_t);
  216. struct gpio_desc *gpio[GPIO_MAX];
  217. int gpio_irq[GPIO_MAX];
  218. bool need_poll;
  219. /* Access rules:
  220. * state_hw_drive: st_mutex held
  221. * state_hw_mask: st_mutex held
  222. * state_soft_mask: st_mutex held
  223. * state: st_mutex held unless reading input bits
  224. */
  225. struct mutex st_mutex; /* Protects state */
  226. unsigned int state_hw_drive;
  227. unsigned int state_hw_mask;
  228. unsigned int state_soft_mask;
  229. unsigned int state_ignore_mask;
  230. unsigned int state;
  231. struct delayed_work poll;
  232. struct delayed_work timeout;
  233. struct mutex sm_mutex; /* Protects state machine */
  234. unsigned char sm_mod_state;
  235. unsigned char sm_mod_tries_init;
  236. unsigned char sm_mod_tries;
  237. unsigned char sm_dev_state;
  238. unsigned short sm_state;
  239. unsigned char sm_fault_retries;
  240. unsigned char sm_phy_retries;
  241. struct sfp_eeprom_id id;
  242. unsigned int module_power_mW;
  243. unsigned int module_t_start_up;
  244. unsigned int module_t_wait;
  245. unsigned int phy_t_retry;
  246. unsigned int rate_kbd;
  247. unsigned int rs_threshold_kbd;
  248. unsigned int rs_state_mask;
  249. bool have_a2;
  250. const struct sfp_quirk *quirk;
  251. #if IS_ENABLED(CONFIG_HWMON)
  252. struct sfp_diag diag;
  253. struct delayed_work hwmon_probe;
  254. unsigned int hwmon_tries;
  255. struct device *hwmon_dev;
  256. char *hwmon_name;
  257. #endif
  258. #if IS_ENABLED(CONFIG_DEBUG_FS)
  259. struct dentry *debugfs_dir;
  260. #endif
  261. };
  262. static bool sff_module_supported(const struct sfp_eeprom_id *id)
  263. {
  264. return id->base.phys_id == SFF8024_ID_SFF_8472 &&
  265. id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
  266. }
  267. static const struct sff_data sff_data = {
  268. .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
  269. .module_supported = sff_module_supported,
  270. };
  271. static bool sfp_module_supported(const struct sfp_eeprom_id *id)
  272. {
  273. if (id->base.phys_id == SFF8024_ID_SFP &&
  274. id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP)
  275. return true;
  276. /* SFP GPON module Ubiquiti U-Fiber Instant has in its EEPROM stored
  277. * phys id SFF instead of SFP. Therefore mark this module explicitly
  278. * as supported based on vendor name and pn match.
  279. */
  280. if (id->base.phys_id == SFF8024_ID_SFF_8472 &&
  281. id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP &&
  282. !memcmp(id->base.vendor_name, "UBNT ", 16) &&
  283. !memcmp(id->base.vendor_pn, "UF-INSTANT ", 16))
  284. return true;
  285. return false;
  286. }
  287. static const struct sff_data sfp_data = {
  288. .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
  289. SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1,
  290. .module_supported = sfp_module_supported,
  291. };
  292. static const struct of_device_id sfp_of_match[] = {
  293. { .compatible = "sff,sff", .data = &sff_data, },
  294. { .compatible = "sff,sfp", .data = &sfp_data, },
  295. { },
  296. };
  297. MODULE_DEVICE_TABLE(of, sfp_of_match);
  298. static void sfp_fixup_long_startup(struct sfp *sfp)
  299. {
  300. sfp->module_t_start_up = T_START_UP_BAD_GPON;
  301. }
  302. static void sfp_fixup_ignore_los(struct sfp *sfp)
  303. {
  304. /* This forces LOS to zero, so we ignore transitions */
  305. sfp->state_ignore_mask |= SFP_F_LOS;
  306. /* Make sure that LOS options are clear */
  307. sfp->id.ext.options &= ~cpu_to_be16(SFP_OPTIONS_LOS_INVERTED |
  308. SFP_OPTIONS_LOS_NORMAL);
  309. }
  310. static void sfp_fixup_ignore_tx_fault(struct sfp *sfp)
  311. {
  312. sfp->state_ignore_mask |= SFP_F_TX_FAULT;
  313. }
  314. static void sfp_fixup_ignore_hw(struct sfp *sfp, unsigned int mask)
  315. {
  316. sfp->state_hw_mask &= ~mask;
  317. }
  318. static void sfp_fixup_nokia(struct sfp *sfp)
  319. {
  320. sfp_fixup_long_startup(sfp);
  321. sfp_fixup_ignore_los(sfp);
  322. }
  323. // For 10GBASE-T short-reach modules
  324. static void sfp_fixup_10gbaset_30m(struct sfp *sfp)
  325. {
  326. sfp->id.base.connector = SFF8024_CONNECTOR_RJ45;
  327. sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SR;
  328. }
  329. static void sfp_fixup_rollball(struct sfp *sfp)
  330. {
  331. sfp->mdio_protocol = MDIO_I2C_ROLLBALL;
  332. /* RollBall modules may disallow access to PHY registers for up to 25
  333. * seconds, and the reads return 0xffff before that. Increase the time
  334. * between PHY probe retries from 50ms to 1s so that we will wait for
  335. * the PHY for a sufficient amount of time.
  336. */
  337. sfp->phy_t_retry = msecs_to_jiffies(1000);
  338. }
  339. static void sfp_fixup_rollball_wait4s(struct sfp *sfp)
  340. {
  341. sfp_fixup_rollball(sfp);
  342. /* The RollBall fixup is not enough for FS modules, the PHY chip inside
  343. * them does not return 0xffff for PHY ID registers in all MMDs for the
  344. * while initializing. They need a 4 second wait before accessing PHY.
  345. */
  346. sfp->module_t_wait = msecs_to_jiffies(4000);
  347. }
  348. static void sfp_fixup_fs_10gt(struct sfp *sfp)
  349. {
  350. sfp_fixup_10gbaset_30m(sfp);
  351. sfp_fixup_rollball_wait4s(sfp);
  352. }
  353. static void sfp_fixup_halny_gsfp(struct sfp *sfp)
  354. {
  355. /* Ignore the TX_FAULT and LOS signals on this module.
  356. * these are possibly used for other purposes on this
  357. * module, e.g. a serial port.
  358. */
  359. sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS);
  360. }
  361. static void sfp_fixup_potron(struct sfp *sfp)
  362. {
  363. /*
  364. * The TX_FAULT and LOS pins on this device are used for serial
  365. * communication, so ignore them. Additionally, provide extra
  366. * time for this device to fully start up.
  367. */
  368. sfp_fixup_long_startup(sfp);
  369. sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS);
  370. }
  371. static void sfp_fixup_rollball_cc(struct sfp *sfp)
  372. {
  373. sfp_fixup_rollball(sfp);
  374. /* Some RollBall SFPs may have wrong (zero) extended compliance code
  375. * burned in EEPROM. For PHY probing we need the correct one.
  376. */
  377. sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SFI;
  378. }
  379. static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
  380. unsigned long *modes,
  381. unsigned long *interfaces)
  382. {
  383. linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, modes);
  384. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  385. }
  386. static void sfp_quirk_disable_autoneg(const struct sfp_eeprom_id *id,
  387. unsigned long *modes,
  388. unsigned long *interfaces)
  389. {
  390. linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, modes);
  391. }
  392. static void sfp_quirk_oem_2_5g(const struct sfp_eeprom_id *id,
  393. unsigned long *modes,
  394. unsigned long *interfaces)
  395. {
  396. /* Copper 2.5G SFP */
  397. linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, modes);
  398. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  399. sfp_quirk_disable_autoneg(id, modes, interfaces);
  400. }
  401. static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
  402. unsigned long *modes,
  403. unsigned long *interfaces)
  404. {
  405. /* Ubiquiti U-Fiber Instant module claims that support all transceiver
  406. * types including 10G Ethernet which is not truth. So clear all claimed
  407. * modes and set only one mode which module supports: 1000baseX_Full.
  408. */
  409. linkmode_zero(modes);
  410. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, modes);
  411. }
  412. #define SFP_QUIRK(_v, _p, _m, _f) \
  413. { .vendor = _v, .part = _p, .modes = _m, .fixup = _f, }
  414. #define SFP_QUIRK_M(_v, _p, _m) SFP_QUIRK(_v, _p, _m, NULL)
  415. #define SFP_QUIRK_F(_v, _p, _f) SFP_QUIRK(_v, _p, NULL, _f)
  416. static const struct sfp_quirk sfp_quirks[] = {
  417. // Alcatel Lucent G-010S-P can operate at 2500base-X, but incorrectly
  418. // report 2500MBd NRZ in their EEPROM
  419. SFP_QUIRK("ALCATELLUCENT", "G010SP", sfp_quirk_2500basex,
  420. sfp_fixup_ignore_tx_fault),
  421. // Alcatel Lucent G-010S-A can operate at 2500base-X, but report 3.2GBd
  422. // NRZ in their EEPROM
  423. SFP_QUIRK("ALCATELLUCENT", "3FE46541AA", sfp_quirk_2500basex,
  424. sfp_fixup_nokia),
  425. // FLYPRO SFP-10GT-CS-30M uses Rollball protocol to talk to the PHY.
  426. SFP_QUIRK_F("FLYPRO", "SFP-10GT-CS-30M", sfp_fixup_rollball),
  427. // Fiberstore SFP-10G-T doesn't identify as copper, uses the Rollball
  428. // protocol to talk to the PHY and needs 4 sec wait before probing the
  429. // PHY.
  430. SFP_QUIRK_F("FS", "SFP-10G-T", sfp_fixup_fs_10gt),
  431. // Fiberstore SFP-2.5G-T and SFP-10GM-T uses Rollball protocol to talk
  432. // to the PHY and needs 4 sec wait before probing the PHY.
  433. SFP_QUIRK_F("FS", "SFP-2.5G-T", sfp_fixup_rollball_wait4s),
  434. SFP_QUIRK_F("FS", "SFP-10GM-T", sfp_fixup_rollball_wait4s),
  435. // Fiberstore GPON-ONU-34-20BI can operate at 2500base-X, but report 1.2GBd
  436. // NRZ in their EEPROM
  437. SFP_QUIRK("FS", "GPON-ONU-34-20BI", sfp_quirk_2500basex,
  438. sfp_fixup_ignore_tx_fault),
  439. SFP_QUIRK_F("HALNy", "HL-GSFP", sfp_fixup_halny_gsfp),
  440. // HG MXPD-483II-F 2.5G supports 2500Base-X, but incorrectly reports
  441. // 2600MBd in their EERPOM
  442. SFP_QUIRK_M("HG GENUINE", "MXPD-483II", sfp_quirk_2500basex),
  443. // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd NRZ in
  444. // their EEPROM
  445. SFP_QUIRK("HUAWEI", "MA5671A", sfp_quirk_2500basex,
  446. sfp_fixup_ignore_tx_fault),
  447. // Lantech 8330-262D-E can operate at 2500base-X, but incorrectly report
  448. // 2500MBd NRZ in their EEPROM
  449. SFP_QUIRK_M("Lantech", "8330-262D-E", sfp_quirk_2500basex),
  450. SFP_QUIRK_M("UBNT", "UF-INSTANT", sfp_quirk_ubnt_uf_instant),
  451. // Walsun HXSX-ATR[CI]-1 don't identify as copper, and use the
  452. // Rollball protocol to talk to the PHY.
  453. SFP_QUIRK_F("Walsun", "HXSX-ATRC-1", sfp_fixup_fs_10gt),
  454. SFP_QUIRK_F("Walsun", "HXSX-ATRI-1", sfp_fixup_fs_10gt),
  455. SFP_QUIRK_F("YV", "SFP+ONU-XGSPON", sfp_fixup_potron),
  456. // OEM SFP-GE-T is a 1000Base-T module with broken TX_FAULT indicator
  457. SFP_QUIRK_F("OEM", "SFP-GE-T", sfp_fixup_ignore_tx_fault),
  458. SFP_QUIRK_F("OEM", "SFP-10G-T", sfp_fixup_rollball_cc),
  459. SFP_QUIRK_M("OEM", "SFP-2.5G-T", sfp_quirk_oem_2_5g),
  460. SFP_QUIRK_M("OEM", "SFP-2.5G-BX10-D", sfp_quirk_2500basex),
  461. SFP_QUIRK_M("OEM", "SFP-2.5G-BX10-U", sfp_quirk_2500basex),
  462. SFP_QUIRK_F("OEM", "RTSFP-10", sfp_fixup_rollball_cc),
  463. SFP_QUIRK_F("OEM", "RTSFP-10G", sfp_fixup_rollball_cc),
  464. SFP_QUIRK_F("Turris", "RTSFP-2.5G", sfp_fixup_rollball),
  465. SFP_QUIRK_F("Turris", "RTSFP-10", sfp_fixup_rollball),
  466. SFP_QUIRK_F("Turris", "RTSFP-10G", sfp_fixup_rollball),
  467. };
  468. static size_t sfp_strlen(const char *str, size_t maxlen)
  469. {
  470. size_t size, i;
  471. /* Trailing characters should be filled with space chars, but
  472. * some manufacturers can't read SFF-8472 and use NUL.
  473. */
  474. for (i = 0, size = 0; i < maxlen; i++)
  475. if (str[i] != ' ' && str[i] != '\0')
  476. size = i + 1;
  477. return size;
  478. }
  479. static bool sfp_match(const char *qs, const char *str, size_t len)
  480. {
  481. if (!qs)
  482. return true;
  483. if (strlen(qs) != len)
  484. return false;
  485. return !strncmp(qs, str, len);
  486. }
  487. static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
  488. {
  489. const struct sfp_quirk *q;
  490. unsigned int i;
  491. size_t vs, ps;
  492. vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
  493. ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
  494. for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
  495. if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
  496. sfp_match(q->part, id->base.vendor_pn, ps))
  497. return q;
  498. return NULL;
  499. }
  500. static unsigned long poll_jiffies;
  501. static unsigned int sfp_gpio_get_state(struct sfp *sfp)
  502. {
  503. unsigned int i, state, v;
  504. for (i = state = 0; i < GPIO_MAX; i++) {
  505. if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
  506. continue;
  507. v = gpiod_get_value_cansleep(sfp->gpio[i]);
  508. if (v)
  509. state |= BIT(i);
  510. }
  511. return state;
  512. }
  513. static unsigned int sff_gpio_get_state(struct sfp *sfp)
  514. {
  515. return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
  516. }
  517. static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
  518. {
  519. unsigned int drive;
  520. if (state & SFP_F_PRESENT)
  521. /* If the module is present, drive the requested signals */
  522. drive = sfp->state_hw_drive;
  523. else
  524. /* Otherwise, let them float to the pull-ups */
  525. drive = 0;
  526. if (sfp->gpio[GPIO_TX_DISABLE]) {
  527. if (drive & SFP_F_TX_DISABLE)
  528. gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
  529. state & SFP_F_TX_DISABLE);
  530. else
  531. gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
  532. }
  533. if (sfp->gpio[GPIO_RS0]) {
  534. if (drive & SFP_F_RS0)
  535. gpiod_direction_output(sfp->gpio[GPIO_RS0],
  536. state & SFP_F_RS0);
  537. else
  538. gpiod_direction_input(sfp->gpio[GPIO_RS0]);
  539. }
  540. if (sfp->gpio[GPIO_RS1]) {
  541. if (drive & SFP_F_RS1)
  542. gpiod_direction_output(sfp->gpio[GPIO_RS1],
  543. state & SFP_F_RS1);
  544. else
  545. gpiod_direction_input(sfp->gpio[GPIO_RS1]);
  546. }
  547. }
  548. static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
  549. size_t len)
  550. {
  551. struct i2c_msg msgs[2];
  552. u8 bus_addr = a2 ? 0x51 : 0x50;
  553. size_t block_size = sfp->i2c_block_size;
  554. size_t this_len;
  555. int ret;
  556. msgs[0].addr = bus_addr;
  557. msgs[0].flags = 0;
  558. msgs[0].len = 1;
  559. msgs[0].buf = &dev_addr;
  560. msgs[1].addr = bus_addr;
  561. msgs[1].flags = I2C_M_RD;
  562. msgs[1].len = len;
  563. msgs[1].buf = buf;
  564. while (len) {
  565. this_len = len;
  566. if (this_len > block_size)
  567. this_len = block_size;
  568. msgs[1].len = this_len;
  569. ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
  570. if (ret < 0)
  571. return ret;
  572. if (ret != ARRAY_SIZE(msgs))
  573. break;
  574. msgs[1].buf += this_len;
  575. dev_addr += this_len;
  576. len -= this_len;
  577. }
  578. return msgs[1].buf - (u8 *)buf;
  579. }
  580. static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
  581. size_t len)
  582. {
  583. struct i2c_msg msgs[1];
  584. u8 bus_addr = a2 ? 0x51 : 0x50;
  585. int ret;
  586. msgs[0].addr = bus_addr;
  587. msgs[0].flags = 0;
  588. msgs[0].len = 1 + len;
  589. msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
  590. if (!msgs[0].buf)
  591. return -ENOMEM;
  592. msgs[0].buf[0] = dev_addr;
  593. memcpy(&msgs[0].buf[1], buf, len);
  594. ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
  595. kfree(msgs[0].buf);
  596. if (ret < 0)
  597. return ret;
  598. return ret == ARRAY_SIZE(msgs) ? len : 0;
  599. }
  600. static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
  601. {
  602. if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
  603. return -EINVAL;
  604. sfp->i2c = i2c;
  605. sfp->read = sfp_i2c_read;
  606. sfp->write = sfp_i2c_write;
  607. return 0;
  608. }
  609. static int sfp_i2c_mdiobus_create(struct sfp *sfp)
  610. {
  611. struct mii_bus *i2c_mii;
  612. int ret;
  613. i2c_mii = mdio_i2c_alloc(sfp->dev, sfp->i2c, sfp->mdio_protocol);
  614. if (IS_ERR(i2c_mii))
  615. return PTR_ERR(i2c_mii);
  616. i2c_mii->name = "SFP I2C Bus";
  617. i2c_mii->phy_mask = ~0;
  618. ret = mdiobus_register(i2c_mii);
  619. if (ret < 0) {
  620. mdiobus_free(i2c_mii);
  621. return ret;
  622. }
  623. sfp->i2c_mii = i2c_mii;
  624. return 0;
  625. }
  626. static void sfp_i2c_mdiobus_destroy(struct sfp *sfp)
  627. {
  628. mdiobus_unregister(sfp->i2c_mii);
  629. sfp->i2c_mii = NULL;
  630. }
  631. /* Interface */
  632. static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
  633. {
  634. return sfp->read(sfp, a2, addr, buf, len);
  635. }
  636. static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
  637. {
  638. return sfp->write(sfp, a2, addr, buf, len);
  639. }
  640. static int sfp_modify_u8(struct sfp *sfp, bool a2, u8 addr, u8 mask, u8 val)
  641. {
  642. int ret;
  643. u8 old, v;
  644. ret = sfp_read(sfp, a2, addr, &old, sizeof(old));
  645. if (ret != sizeof(old))
  646. return ret;
  647. v = (old & ~mask) | (val & mask);
  648. if (v == old)
  649. return sizeof(v);
  650. return sfp_write(sfp, a2, addr, &v, sizeof(v));
  651. }
  652. static unsigned int sfp_soft_get_state(struct sfp *sfp)
  653. {
  654. unsigned int state = 0;
  655. u8 status;
  656. int ret;
  657. ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status));
  658. if (ret == sizeof(status)) {
  659. if (status & SFP_STATUS_RX_LOS)
  660. state |= SFP_F_LOS;
  661. if (status & SFP_STATUS_TX_FAULT)
  662. state |= SFP_F_TX_FAULT;
  663. } else {
  664. dev_err_ratelimited(sfp->dev,
  665. "failed to read SFP soft status: %pe\n",
  666. ERR_PTR(ret));
  667. /* Preserve the current state */
  668. state = sfp->state;
  669. }
  670. return state & sfp->state_soft_mask;
  671. }
  672. static void sfp_soft_set_state(struct sfp *sfp, unsigned int state,
  673. unsigned int soft)
  674. {
  675. u8 mask = 0;
  676. u8 val = 0;
  677. if (soft & SFP_F_TX_DISABLE)
  678. mask |= SFP_STATUS_TX_DISABLE_FORCE;
  679. if (state & SFP_F_TX_DISABLE)
  680. val |= SFP_STATUS_TX_DISABLE_FORCE;
  681. if (soft & SFP_F_RS0)
  682. mask |= SFP_STATUS_RS0_SELECT;
  683. if (state & SFP_F_RS0)
  684. val |= SFP_STATUS_RS0_SELECT;
  685. if (mask)
  686. sfp_modify_u8(sfp, true, SFP_STATUS, mask, val);
  687. val = mask = 0;
  688. if (soft & SFP_F_RS1)
  689. mask |= SFP_EXT_STATUS_RS1_SELECT;
  690. if (state & SFP_F_RS1)
  691. val |= SFP_EXT_STATUS_RS1_SELECT;
  692. if (mask)
  693. sfp_modify_u8(sfp, true, SFP_EXT_STATUS, mask, val);
  694. }
  695. static void sfp_soft_start_poll(struct sfp *sfp)
  696. {
  697. const struct sfp_eeprom_id *id = &sfp->id;
  698. unsigned int mask = 0;
  699. if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE)
  700. mask |= SFP_F_TX_DISABLE;
  701. if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT)
  702. mask |= SFP_F_TX_FAULT;
  703. if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS)
  704. mask |= SFP_F_LOS;
  705. if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RATE_SELECT)
  706. mask |= sfp->rs_state_mask;
  707. mutex_lock(&sfp->st_mutex);
  708. // Poll the soft state for hardware pins we want to ignore
  709. sfp->state_soft_mask = ~sfp->state_hw_mask & ~sfp->state_ignore_mask &
  710. mask;
  711. if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
  712. !sfp->need_poll)
  713. mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
  714. mutex_unlock(&sfp->st_mutex);
  715. }
  716. static void sfp_soft_stop_poll(struct sfp *sfp)
  717. {
  718. mutex_lock(&sfp->st_mutex);
  719. sfp->state_soft_mask = 0;
  720. mutex_unlock(&sfp->st_mutex);
  721. }
  722. /* sfp_get_state() - must be called with st_mutex held, or in the
  723. * initialisation path.
  724. */
  725. static unsigned int sfp_get_state(struct sfp *sfp)
  726. {
  727. unsigned int soft = sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT);
  728. unsigned int state;
  729. state = sfp->get_state(sfp) & sfp->state_hw_mask;
  730. if (state & SFP_F_PRESENT && soft)
  731. state |= sfp_soft_get_state(sfp);
  732. return state;
  733. }
  734. /* sfp_set_state() - must be called with st_mutex held, or in the
  735. * initialisation path.
  736. */
  737. static void sfp_set_state(struct sfp *sfp, unsigned int state)
  738. {
  739. unsigned int soft;
  740. sfp->set_state(sfp, state);
  741. soft = sfp->state_soft_mask & SFP_F_OUTPUTS;
  742. if (state & SFP_F_PRESENT && soft)
  743. sfp_soft_set_state(sfp, state, soft);
  744. }
  745. static void sfp_mod_state(struct sfp *sfp, unsigned int mask, unsigned int set)
  746. {
  747. mutex_lock(&sfp->st_mutex);
  748. sfp->state = (sfp->state & ~mask) | set;
  749. sfp_set_state(sfp, sfp->state);
  750. mutex_unlock(&sfp->st_mutex);
  751. }
  752. static unsigned int sfp_check(void *buf, size_t len)
  753. {
  754. u8 *p, check;
  755. for (p = buf, check = 0; len; p++, len--)
  756. check += *p;
  757. return check;
  758. }
  759. /* hwmon */
  760. #if IS_ENABLED(CONFIG_HWMON)
  761. static umode_t sfp_hwmon_is_visible(const void *data,
  762. enum hwmon_sensor_types type,
  763. u32 attr, int channel)
  764. {
  765. const struct sfp *sfp = data;
  766. switch (type) {
  767. case hwmon_temp:
  768. switch (attr) {
  769. case hwmon_temp_min_alarm:
  770. case hwmon_temp_max_alarm:
  771. case hwmon_temp_lcrit_alarm:
  772. case hwmon_temp_crit_alarm:
  773. case hwmon_temp_min:
  774. case hwmon_temp_max:
  775. case hwmon_temp_lcrit:
  776. case hwmon_temp_crit:
  777. if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
  778. return 0;
  779. fallthrough;
  780. case hwmon_temp_input:
  781. case hwmon_temp_label:
  782. return 0444;
  783. default:
  784. return 0;
  785. }
  786. case hwmon_in:
  787. switch (attr) {
  788. case hwmon_in_min_alarm:
  789. case hwmon_in_max_alarm:
  790. case hwmon_in_lcrit_alarm:
  791. case hwmon_in_crit_alarm:
  792. case hwmon_in_min:
  793. case hwmon_in_max:
  794. case hwmon_in_lcrit:
  795. case hwmon_in_crit:
  796. if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
  797. return 0;
  798. fallthrough;
  799. case hwmon_in_input:
  800. case hwmon_in_label:
  801. return 0444;
  802. default:
  803. return 0;
  804. }
  805. case hwmon_curr:
  806. switch (attr) {
  807. case hwmon_curr_min_alarm:
  808. case hwmon_curr_max_alarm:
  809. case hwmon_curr_lcrit_alarm:
  810. case hwmon_curr_crit_alarm:
  811. case hwmon_curr_min:
  812. case hwmon_curr_max:
  813. case hwmon_curr_lcrit:
  814. case hwmon_curr_crit:
  815. if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
  816. return 0;
  817. fallthrough;
  818. case hwmon_curr_input:
  819. case hwmon_curr_label:
  820. return 0444;
  821. default:
  822. return 0;
  823. }
  824. case hwmon_power:
  825. /* External calibration of receive power requires
  826. * floating point arithmetic. Doing that in the kernel
  827. * is not easy, so just skip it. If the module does
  828. * not require external calibration, we can however
  829. * show receiver power, since FP is then not needed.
  830. */
  831. if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
  832. channel == 1)
  833. return 0;
  834. switch (attr) {
  835. case hwmon_power_min_alarm:
  836. case hwmon_power_max_alarm:
  837. case hwmon_power_lcrit_alarm:
  838. case hwmon_power_crit_alarm:
  839. case hwmon_power_min:
  840. case hwmon_power_max:
  841. case hwmon_power_lcrit:
  842. case hwmon_power_crit:
  843. if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
  844. return 0;
  845. fallthrough;
  846. case hwmon_power_input:
  847. case hwmon_power_label:
  848. return 0444;
  849. default:
  850. return 0;
  851. }
  852. default:
  853. return 0;
  854. }
  855. }
  856. static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
  857. {
  858. __be16 val;
  859. int err;
  860. err = sfp_read(sfp, true, reg, &val, sizeof(val));
  861. if (err < 0)
  862. return err;
  863. *value = be16_to_cpu(val);
  864. return 0;
  865. }
  866. static void sfp_hwmon_to_rx_power(long *value)
  867. {
  868. *value = DIV_ROUND_CLOSEST(*value, 10);
  869. }
  870. static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
  871. long *value)
  872. {
  873. if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
  874. *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
  875. }
  876. static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
  877. {
  878. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
  879. be16_to_cpu(sfp->diag.cal_t_offset), value);
  880. if (*value >= 0x8000)
  881. *value -= 0x10000;
  882. *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
  883. }
  884. static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
  885. {
  886. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
  887. be16_to_cpu(sfp->diag.cal_v_offset), value);
  888. *value = DIV_ROUND_CLOSEST(*value, 10);
  889. }
  890. static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
  891. {
  892. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
  893. be16_to_cpu(sfp->diag.cal_txi_offset), value);
  894. *value = DIV_ROUND_CLOSEST(*value, 500);
  895. }
  896. static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
  897. {
  898. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
  899. be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
  900. *value = DIV_ROUND_CLOSEST(*value, 10);
  901. }
  902. static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
  903. {
  904. int err;
  905. err = sfp_hwmon_read_sensor(sfp, reg, value);
  906. if (err < 0)
  907. return err;
  908. sfp_hwmon_calibrate_temp(sfp, value);
  909. return 0;
  910. }
  911. static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
  912. {
  913. int err;
  914. err = sfp_hwmon_read_sensor(sfp, reg, value);
  915. if (err < 0)
  916. return err;
  917. sfp_hwmon_calibrate_vcc(sfp, value);
  918. return 0;
  919. }
  920. static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
  921. {
  922. int err;
  923. err = sfp_hwmon_read_sensor(sfp, reg, value);
  924. if (err < 0)
  925. return err;
  926. sfp_hwmon_calibrate_bias(sfp, value);
  927. return 0;
  928. }
  929. static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
  930. {
  931. int err;
  932. err = sfp_hwmon_read_sensor(sfp, reg, value);
  933. if (err < 0)
  934. return err;
  935. sfp_hwmon_calibrate_tx_power(sfp, value);
  936. return 0;
  937. }
  938. static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
  939. {
  940. int err;
  941. err = sfp_hwmon_read_sensor(sfp, reg, value);
  942. if (err < 0)
  943. return err;
  944. sfp_hwmon_to_rx_power(value);
  945. return 0;
  946. }
  947. static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
  948. {
  949. u8 status;
  950. int err;
  951. switch (attr) {
  952. case hwmon_temp_input:
  953. return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
  954. case hwmon_temp_lcrit:
  955. *value = be16_to_cpu(sfp->diag.temp_low_alarm);
  956. sfp_hwmon_calibrate_temp(sfp, value);
  957. return 0;
  958. case hwmon_temp_min:
  959. *value = be16_to_cpu(sfp->diag.temp_low_warn);
  960. sfp_hwmon_calibrate_temp(sfp, value);
  961. return 0;
  962. case hwmon_temp_max:
  963. *value = be16_to_cpu(sfp->diag.temp_high_warn);
  964. sfp_hwmon_calibrate_temp(sfp, value);
  965. return 0;
  966. case hwmon_temp_crit:
  967. *value = be16_to_cpu(sfp->diag.temp_high_alarm);
  968. sfp_hwmon_calibrate_temp(sfp, value);
  969. return 0;
  970. case hwmon_temp_lcrit_alarm:
  971. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  972. if (err < 0)
  973. return err;
  974. *value = !!(status & SFP_ALARM0_TEMP_LOW);
  975. return 0;
  976. case hwmon_temp_min_alarm:
  977. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  978. if (err < 0)
  979. return err;
  980. *value = !!(status & SFP_WARN0_TEMP_LOW);
  981. return 0;
  982. case hwmon_temp_max_alarm:
  983. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  984. if (err < 0)
  985. return err;
  986. *value = !!(status & SFP_WARN0_TEMP_HIGH);
  987. return 0;
  988. case hwmon_temp_crit_alarm:
  989. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  990. if (err < 0)
  991. return err;
  992. *value = !!(status & SFP_ALARM0_TEMP_HIGH);
  993. return 0;
  994. default:
  995. return -EOPNOTSUPP;
  996. }
  997. return -EOPNOTSUPP;
  998. }
  999. static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
  1000. {
  1001. u8 status;
  1002. int err;
  1003. switch (attr) {
  1004. case hwmon_in_input:
  1005. return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
  1006. case hwmon_in_lcrit:
  1007. *value = be16_to_cpu(sfp->diag.volt_low_alarm);
  1008. sfp_hwmon_calibrate_vcc(sfp, value);
  1009. return 0;
  1010. case hwmon_in_min:
  1011. *value = be16_to_cpu(sfp->diag.volt_low_warn);
  1012. sfp_hwmon_calibrate_vcc(sfp, value);
  1013. return 0;
  1014. case hwmon_in_max:
  1015. *value = be16_to_cpu(sfp->diag.volt_high_warn);
  1016. sfp_hwmon_calibrate_vcc(sfp, value);
  1017. return 0;
  1018. case hwmon_in_crit:
  1019. *value = be16_to_cpu(sfp->diag.volt_high_alarm);
  1020. sfp_hwmon_calibrate_vcc(sfp, value);
  1021. return 0;
  1022. case hwmon_in_lcrit_alarm:
  1023. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  1024. if (err < 0)
  1025. return err;
  1026. *value = !!(status & SFP_ALARM0_VCC_LOW);
  1027. return 0;
  1028. case hwmon_in_min_alarm:
  1029. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  1030. if (err < 0)
  1031. return err;
  1032. *value = !!(status & SFP_WARN0_VCC_LOW);
  1033. return 0;
  1034. case hwmon_in_max_alarm:
  1035. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  1036. if (err < 0)
  1037. return err;
  1038. *value = !!(status & SFP_WARN0_VCC_HIGH);
  1039. return 0;
  1040. case hwmon_in_crit_alarm:
  1041. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  1042. if (err < 0)
  1043. return err;
  1044. *value = !!(status & SFP_ALARM0_VCC_HIGH);
  1045. return 0;
  1046. default:
  1047. return -EOPNOTSUPP;
  1048. }
  1049. return -EOPNOTSUPP;
  1050. }
  1051. static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
  1052. {
  1053. u8 status;
  1054. int err;
  1055. switch (attr) {
  1056. case hwmon_curr_input:
  1057. return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
  1058. case hwmon_curr_lcrit:
  1059. *value = be16_to_cpu(sfp->diag.bias_low_alarm);
  1060. sfp_hwmon_calibrate_bias(sfp, value);
  1061. return 0;
  1062. case hwmon_curr_min:
  1063. *value = be16_to_cpu(sfp->diag.bias_low_warn);
  1064. sfp_hwmon_calibrate_bias(sfp, value);
  1065. return 0;
  1066. case hwmon_curr_max:
  1067. *value = be16_to_cpu(sfp->diag.bias_high_warn);
  1068. sfp_hwmon_calibrate_bias(sfp, value);
  1069. return 0;
  1070. case hwmon_curr_crit:
  1071. *value = be16_to_cpu(sfp->diag.bias_high_alarm);
  1072. sfp_hwmon_calibrate_bias(sfp, value);
  1073. return 0;
  1074. case hwmon_curr_lcrit_alarm:
  1075. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  1076. if (err < 0)
  1077. return err;
  1078. *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
  1079. return 0;
  1080. case hwmon_curr_min_alarm:
  1081. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  1082. if (err < 0)
  1083. return err;
  1084. *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
  1085. return 0;
  1086. case hwmon_curr_max_alarm:
  1087. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  1088. if (err < 0)
  1089. return err;
  1090. *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
  1091. return 0;
  1092. case hwmon_curr_crit_alarm:
  1093. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  1094. if (err < 0)
  1095. return err;
  1096. *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
  1097. return 0;
  1098. default:
  1099. return -EOPNOTSUPP;
  1100. }
  1101. return -EOPNOTSUPP;
  1102. }
  1103. static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
  1104. {
  1105. u8 status;
  1106. int err;
  1107. switch (attr) {
  1108. case hwmon_power_input:
  1109. return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
  1110. case hwmon_power_lcrit:
  1111. *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
  1112. sfp_hwmon_calibrate_tx_power(sfp, value);
  1113. return 0;
  1114. case hwmon_power_min:
  1115. *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
  1116. sfp_hwmon_calibrate_tx_power(sfp, value);
  1117. return 0;
  1118. case hwmon_power_max:
  1119. *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
  1120. sfp_hwmon_calibrate_tx_power(sfp, value);
  1121. return 0;
  1122. case hwmon_power_crit:
  1123. *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
  1124. sfp_hwmon_calibrate_tx_power(sfp, value);
  1125. return 0;
  1126. case hwmon_power_lcrit_alarm:
  1127. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  1128. if (err < 0)
  1129. return err;
  1130. *value = !!(status & SFP_ALARM0_TXPWR_LOW);
  1131. return 0;
  1132. case hwmon_power_min_alarm:
  1133. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  1134. if (err < 0)
  1135. return err;
  1136. *value = !!(status & SFP_WARN0_TXPWR_LOW);
  1137. return 0;
  1138. case hwmon_power_max_alarm:
  1139. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  1140. if (err < 0)
  1141. return err;
  1142. *value = !!(status & SFP_WARN0_TXPWR_HIGH);
  1143. return 0;
  1144. case hwmon_power_crit_alarm:
  1145. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  1146. if (err < 0)
  1147. return err;
  1148. *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
  1149. return 0;
  1150. default:
  1151. return -EOPNOTSUPP;
  1152. }
  1153. return -EOPNOTSUPP;
  1154. }
  1155. static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
  1156. {
  1157. u8 status;
  1158. int err;
  1159. switch (attr) {
  1160. case hwmon_power_input:
  1161. return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
  1162. case hwmon_power_lcrit:
  1163. *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
  1164. sfp_hwmon_to_rx_power(value);
  1165. return 0;
  1166. case hwmon_power_min:
  1167. *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
  1168. sfp_hwmon_to_rx_power(value);
  1169. return 0;
  1170. case hwmon_power_max:
  1171. *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
  1172. sfp_hwmon_to_rx_power(value);
  1173. return 0;
  1174. case hwmon_power_crit:
  1175. *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
  1176. sfp_hwmon_to_rx_power(value);
  1177. return 0;
  1178. case hwmon_power_lcrit_alarm:
  1179. err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
  1180. if (err < 0)
  1181. return err;
  1182. *value = !!(status & SFP_ALARM1_RXPWR_LOW);
  1183. return 0;
  1184. case hwmon_power_min_alarm:
  1185. err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
  1186. if (err < 0)
  1187. return err;
  1188. *value = !!(status & SFP_WARN1_RXPWR_LOW);
  1189. return 0;
  1190. case hwmon_power_max_alarm:
  1191. err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
  1192. if (err < 0)
  1193. return err;
  1194. *value = !!(status & SFP_WARN1_RXPWR_HIGH);
  1195. return 0;
  1196. case hwmon_power_crit_alarm:
  1197. err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
  1198. if (err < 0)
  1199. return err;
  1200. *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
  1201. return 0;
  1202. default:
  1203. return -EOPNOTSUPP;
  1204. }
  1205. return -EOPNOTSUPP;
  1206. }
  1207. static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  1208. u32 attr, int channel, long *value)
  1209. {
  1210. struct sfp *sfp = dev_get_drvdata(dev);
  1211. switch (type) {
  1212. case hwmon_temp:
  1213. return sfp_hwmon_temp(sfp, attr, value);
  1214. case hwmon_in:
  1215. return sfp_hwmon_vcc(sfp, attr, value);
  1216. case hwmon_curr:
  1217. return sfp_hwmon_bias(sfp, attr, value);
  1218. case hwmon_power:
  1219. switch (channel) {
  1220. case 0:
  1221. return sfp_hwmon_tx_power(sfp, attr, value);
  1222. case 1:
  1223. return sfp_hwmon_rx_power(sfp, attr, value);
  1224. default:
  1225. return -EOPNOTSUPP;
  1226. }
  1227. default:
  1228. return -EOPNOTSUPP;
  1229. }
  1230. }
  1231. static const char *const sfp_hwmon_power_labels[] = {
  1232. "TX_power",
  1233. "RX_power",
  1234. };
  1235. static int sfp_hwmon_read_string(struct device *dev,
  1236. enum hwmon_sensor_types type,
  1237. u32 attr, int channel, const char **str)
  1238. {
  1239. switch (type) {
  1240. case hwmon_curr:
  1241. switch (attr) {
  1242. case hwmon_curr_label:
  1243. *str = "bias";
  1244. return 0;
  1245. default:
  1246. return -EOPNOTSUPP;
  1247. }
  1248. break;
  1249. case hwmon_temp:
  1250. switch (attr) {
  1251. case hwmon_temp_label:
  1252. *str = "temperature";
  1253. return 0;
  1254. default:
  1255. return -EOPNOTSUPP;
  1256. }
  1257. break;
  1258. case hwmon_in:
  1259. switch (attr) {
  1260. case hwmon_in_label:
  1261. *str = "VCC";
  1262. return 0;
  1263. default:
  1264. return -EOPNOTSUPP;
  1265. }
  1266. break;
  1267. case hwmon_power:
  1268. switch (attr) {
  1269. case hwmon_power_label:
  1270. *str = sfp_hwmon_power_labels[channel];
  1271. return 0;
  1272. default:
  1273. return -EOPNOTSUPP;
  1274. }
  1275. break;
  1276. default:
  1277. return -EOPNOTSUPP;
  1278. }
  1279. return -EOPNOTSUPP;
  1280. }
  1281. static const struct hwmon_ops sfp_hwmon_ops = {
  1282. .is_visible = sfp_hwmon_is_visible,
  1283. .read = sfp_hwmon_read,
  1284. .read_string = sfp_hwmon_read_string,
  1285. };
  1286. static const struct hwmon_channel_info * const sfp_hwmon_info[] = {
  1287. HWMON_CHANNEL_INFO(chip,
  1288. HWMON_C_REGISTER_TZ),
  1289. HWMON_CHANNEL_INFO(in,
  1290. HWMON_I_INPUT |
  1291. HWMON_I_MAX | HWMON_I_MIN |
  1292. HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
  1293. HWMON_I_CRIT | HWMON_I_LCRIT |
  1294. HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
  1295. HWMON_I_LABEL),
  1296. HWMON_CHANNEL_INFO(temp,
  1297. HWMON_T_INPUT |
  1298. HWMON_T_MAX | HWMON_T_MIN |
  1299. HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
  1300. HWMON_T_CRIT | HWMON_T_LCRIT |
  1301. HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
  1302. HWMON_T_LABEL),
  1303. HWMON_CHANNEL_INFO(curr,
  1304. HWMON_C_INPUT |
  1305. HWMON_C_MAX | HWMON_C_MIN |
  1306. HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
  1307. HWMON_C_CRIT | HWMON_C_LCRIT |
  1308. HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
  1309. HWMON_C_LABEL),
  1310. HWMON_CHANNEL_INFO(power,
  1311. /* Transmit power */
  1312. HWMON_P_INPUT |
  1313. HWMON_P_MAX | HWMON_P_MIN |
  1314. HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
  1315. HWMON_P_CRIT | HWMON_P_LCRIT |
  1316. HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
  1317. HWMON_P_LABEL,
  1318. /* Receive power */
  1319. HWMON_P_INPUT |
  1320. HWMON_P_MAX | HWMON_P_MIN |
  1321. HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
  1322. HWMON_P_CRIT | HWMON_P_LCRIT |
  1323. HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
  1324. HWMON_P_LABEL),
  1325. NULL,
  1326. };
  1327. static const struct hwmon_chip_info sfp_hwmon_chip_info = {
  1328. .ops = &sfp_hwmon_ops,
  1329. .info = sfp_hwmon_info,
  1330. };
  1331. static void sfp_hwmon_probe(struct work_struct *work)
  1332. {
  1333. struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
  1334. int err;
  1335. /* hwmon interface needs to access 16bit registers in atomic way to
  1336. * guarantee coherency of the diagnostic monitoring data. If it is not
  1337. * possible to guarantee coherency because EEPROM is broken in such way
  1338. * that does not support atomic 16bit read operation then we have to
  1339. * skip registration of hwmon device.
  1340. */
  1341. if (sfp->i2c_block_size < 2) {
  1342. dev_info(sfp->dev,
  1343. "skipping hwmon device registration due to broken EEPROM\n");
  1344. dev_info(sfp->dev,
  1345. "diagnostic EEPROM area cannot be read atomically to guarantee data coherency\n");
  1346. return;
  1347. }
  1348. err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
  1349. if (err < 0) {
  1350. if (sfp->hwmon_tries--) {
  1351. mod_delayed_work(system_wq, &sfp->hwmon_probe,
  1352. T_PROBE_RETRY_SLOW);
  1353. } else {
  1354. dev_warn(sfp->dev, "hwmon probe failed: %pe\n",
  1355. ERR_PTR(err));
  1356. }
  1357. return;
  1358. }
  1359. sfp->hwmon_name = hwmon_sanitize_name(dev_name(sfp->dev));
  1360. if (IS_ERR(sfp->hwmon_name)) {
  1361. dev_err(sfp->dev, "out of memory for hwmon name\n");
  1362. return;
  1363. }
  1364. sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
  1365. sfp->hwmon_name, sfp,
  1366. &sfp_hwmon_chip_info,
  1367. NULL);
  1368. if (IS_ERR(sfp->hwmon_dev))
  1369. dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
  1370. PTR_ERR(sfp->hwmon_dev));
  1371. }
  1372. static int sfp_hwmon_insert(struct sfp *sfp)
  1373. {
  1374. if (sfp->have_a2 && sfp->id.ext.diagmon & SFP_DIAGMON_DDM) {
  1375. mod_delayed_work(system_wq, &sfp->hwmon_probe, 1);
  1376. sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
  1377. }
  1378. return 0;
  1379. }
  1380. static void sfp_hwmon_remove(struct sfp *sfp)
  1381. {
  1382. cancel_delayed_work_sync(&sfp->hwmon_probe);
  1383. if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
  1384. hwmon_device_unregister(sfp->hwmon_dev);
  1385. sfp->hwmon_dev = NULL;
  1386. kfree(sfp->hwmon_name);
  1387. }
  1388. }
  1389. static int sfp_hwmon_init(struct sfp *sfp)
  1390. {
  1391. INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
  1392. return 0;
  1393. }
  1394. static void sfp_hwmon_exit(struct sfp *sfp)
  1395. {
  1396. cancel_delayed_work_sync(&sfp->hwmon_probe);
  1397. }
  1398. #else
  1399. static int sfp_hwmon_insert(struct sfp *sfp)
  1400. {
  1401. return 0;
  1402. }
  1403. static void sfp_hwmon_remove(struct sfp *sfp)
  1404. {
  1405. }
  1406. static int sfp_hwmon_init(struct sfp *sfp)
  1407. {
  1408. return 0;
  1409. }
  1410. static void sfp_hwmon_exit(struct sfp *sfp)
  1411. {
  1412. }
  1413. #endif
  1414. /* Helpers */
  1415. static void sfp_module_tx_disable(struct sfp *sfp)
  1416. {
  1417. dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  1418. sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
  1419. sfp_mod_state(sfp, SFP_F_TX_DISABLE, SFP_F_TX_DISABLE);
  1420. }
  1421. static void sfp_module_tx_enable(struct sfp *sfp)
  1422. {
  1423. dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  1424. sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
  1425. sfp_mod_state(sfp, SFP_F_TX_DISABLE, 0);
  1426. }
  1427. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1428. static int sfp_debug_state_show(struct seq_file *s, void *data)
  1429. {
  1430. struct sfp *sfp = s->private;
  1431. seq_printf(s, "Module state: %s\n",
  1432. mod_state_to_str(sfp->sm_mod_state));
  1433. seq_printf(s, "Module probe attempts: %d %d\n",
  1434. R_PROBE_RETRY_INIT - sfp->sm_mod_tries_init,
  1435. R_PROBE_RETRY_SLOW - sfp->sm_mod_tries);
  1436. seq_printf(s, "Device state: %s\n",
  1437. dev_state_to_str(sfp->sm_dev_state));
  1438. seq_printf(s, "Main state: %s\n",
  1439. sm_state_to_str(sfp->sm_state));
  1440. seq_printf(s, "Fault recovery remaining retries: %d\n",
  1441. sfp->sm_fault_retries);
  1442. seq_printf(s, "PHY probe remaining retries: %d\n",
  1443. sfp->sm_phy_retries);
  1444. seq_printf(s, "Signalling rate: %u kBd\n", sfp->rate_kbd);
  1445. seq_printf(s, "Rate select threshold: %u kBd\n",
  1446. sfp->rs_threshold_kbd);
  1447. seq_printf(s, "moddef0: %d\n", !!(sfp->state & SFP_F_PRESENT));
  1448. seq_printf(s, "rx_los: %d\n", !!(sfp->state & SFP_F_LOS));
  1449. seq_printf(s, "tx_fault: %d\n", !!(sfp->state & SFP_F_TX_FAULT));
  1450. seq_printf(s, "tx_disable: %d\n", !!(sfp->state & SFP_F_TX_DISABLE));
  1451. seq_printf(s, "rs0: %d\n", !!(sfp->state & SFP_F_RS0));
  1452. seq_printf(s, "rs1: %d\n", !!(sfp->state & SFP_F_RS1));
  1453. return 0;
  1454. }
  1455. DEFINE_SHOW_ATTRIBUTE(sfp_debug_state);
  1456. static void sfp_debugfs_init(struct sfp *sfp)
  1457. {
  1458. sfp->debugfs_dir = debugfs_create_dir(dev_name(sfp->dev), NULL);
  1459. debugfs_create_file("state", 0600, sfp->debugfs_dir, sfp,
  1460. &sfp_debug_state_fops);
  1461. }
  1462. static void sfp_debugfs_exit(struct sfp *sfp)
  1463. {
  1464. debugfs_remove_recursive(sfp->debugfs_dir);
  1465. }
  1466. #else
  1467. static void sfp_debugfs_init(struct sfp *sfp)
  1468. {
  1469. }
  1470. static void sfp_debugfs_exit(struct sfp *sfp)
  1471. {
  1472. }
  1473. #endif
  1474. static void sfp_module_tx_fault_reset(struct sfp *sfp)
  1475. {
  1476. unsigned int state;
  1477. mutex_lock(&sfp->st_mutex);
  1478. state = sfp->state;
  1479. if (!(state & SFP_F_TX_DISABLE)) {
  1480. sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
  1481. udelay(T_RESET_US);
  1482. sfp_set_state(sfp, state);
  1483. }
  1484. mutex_unlock(&sfp->st_mutex);
  1485. }
  1486. /* SFP state machine */
  1487. static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
  1488. {
  1489. if (timeout)
  1490. mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
  1491. timeout);
  1492. else
  1493. cancel_delayed_work(&sfp->timeout);
  1494. }
  1495. static void sfp_sm_next(struct sfp *sfp, unsigned int state,
  1496. unsigned int timeout)
  1497. {
  1498. sfp->sm_state = state;
  1499. sfp_sm_set_timer(sfp, timeout);
  1500. }
  1501. static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
  1502. unsigned int timeout)
  1503. {
  1504. sfp->sm_mod_state = state;
  1505. sfp_sm_set_timer(sfp, timeout);
  1506. }
  1507. static void sfp_sm_phy_detach(struct sfp *sfp)
  1508. {
  1509. sfp_remove_phy(sfp->sfp_bus);
  1510. phy_device_remove(sfp->mod_phy);
  1511. phy_device_free(sfp->mod_phy);
  1512. sfp->mod_phy = NULL;
  1513. }
  1514. static int sfp_sm_probe_phy(struct sfp *sfp, int addr, bool is_c45)
  1515. {
  1516. struct phy_device *phy;
  1517. int err;
  1518. phy = get_phy_device(sfp->i2c_mii, addr, is_c45);
  1519. if (phy == ERR_PTR(-ENODEV))
  1520. return PTR_ERR(phy);
  1521. if (IS_ERR(phy)) {
  1522. dev_err(sfp->dev, "mdiobus scan returned %pe\n", phy);
  1523. return PTR_ERR(phy);
  1524. }
  1525. /* Mark this PHY as being on a SFP module */
  1526. phy->is_on_sfp_module = true;
  1527. err = phy_device_register(phy);
  1528. if (err) {
  1529. phy_device_free(phy);
  1530. dev_err(sfp->dev, "phy_device_register failed: %pe\n",
  1531. ERR_PTR(err));
  1532. return err;
  1533. }
  1534. err = sfp_add_phy(sfp->sfp_bus, phy);
  1535. if (err) {
  1536. phy_device_remove(phy);
  1537. phy_device_free(phy);
  1538. dev_err(sfp->dev, "sfp_add_phy failed: %pe\n", ERR_PTR(err));
  1539. return err;
  1540. }
  1541. sfp->mod_phy = phy;
  1542. return 0;
  1543. }
  1544. static void sfp_sm_link_up(struct sfp *sfp)
  1545. {
  1546. sfp_link_up(sfp->sfp_bus);
  1547. sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
  1548. }
  1549. static void sfp_sm_link_down(struct sfp *sfp)
  1550. {
  1551. sfp_link_down(sfp->sfp_bus);
  1552. }
  1553. static void sfp_sm_link_check_los(struct sfp *sfp)
  1554. {
  1555. const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
  1556. const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
  1557. __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
  1558. bool los = false;
  1559. /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
  1560. * are set, we assume that no LOS signal is available. If both are
  1561. * set, we assume LOS is not implemented (and is meaningless.)
  1562. */
  1563. if (los_options == los_inverted)
  1564. los = !(sfp->state & SFP_F_LOS);
  1565. else if (los_options == los_normal)
  1566. los = !!(sfp->state & SFP_F_LOS);
  1567. if (los)
  1568. sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
  1569. else
  1570. sfp_sm_link_up(sfp);
  1571. }
  1572. static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
  1573. {
  1574. const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
  1575. const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
  1576. __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
  1577. return (los_options == los_inverted && event == SFP_E_LOS_LOW) ||
  1578. (los_options == los_normal && event == SFP_E_LOS_HIGH);
  1579. }
  1580. static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
  1581. {
  1582. const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
  1583. const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
  1584. __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
  1585. return (los_options == los_inverted && event == SFP_E_LOS_HIGH) ||
  1586. (los_options == los_normal && event == SFP_E_LOS_LOW);
  1587. }
  1588. static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
  1589. {
  1590. if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) {
  1591. dev_err(sfp->dev,
  1592. "module persistently indicates fault, disabling\n");
  1593. sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
  1594. } else {
  1595. if (warn)
  1596. dev_err(sfp->dev, "module transmit fault indicated\n");
  1597. sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
  1598. }
  1599. }
  1600. static int sfp_sm_add_mdio_bus(struct sfp *sfp)
  1601. {
  1602. if (sfp->mdio_protocol != MDIO_I2C_NONE)
  1603. return sfp_i2c_mdiobus_create(sfp);
  1604. return 0;
  1605. }
  1606. /* Probe a SFP for a PHY device if the module supports copper - the PHY
  1607. * normally sits at I2C bus address 0x56, and may either be a clause 22
  1608. * or clause 45 PHY.
  1609. *
  1610. * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with
  1611. * negotiation enabled, but some may be in 1000base-X - which is for the
  1612. * PHY driver to determine.
  1613. *
  1614. * Clause 45 copper SFP+ modules (10G) appear to switch their interface
  1615. * mode according to the negotiated line speed.
  1616. */
  1617. static int sfp_sm_probe_for_phy(struct sfp *sfp)
  1618. {
  1619. int err = 0;
  1620. switch (sfp->mdio_protocol) {
  1621. case MDIO_I2C_NONE:
  1622. break;
  1623. case MDIO_I2C_MARVELL_C22:
  1624. err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, false);
  1625. break;
  1626. case MDIO_I2C_C45:
  1627. err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, true);
  1628. break;
  1629. case MDIO_I2C_ROLLBALL:
  1630. err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR_ROLLBALL, true);
  1631. break;
  1632. }
  1633. return err;
  1634. }
  1635. static int sfp_module_parse_power(struct sfp *sfp)
  1636. {
  1637. u32 power_mW = 1000;
  1638. bool supports_a2;
  1639. if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 &&
  1640. sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
  1641. power_mW = 1500;
  1642. /* Added in Rev 11.9, but there is no compliance code for this */
  1643. if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV11_4 &&
  1644. sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
  1645. power_mW = 2000;
  1646. /* Power level 1 modules (max. 1W) are always supported. */
  1647. if (power_mW <= 1000) {
  1648. sfp->module_power_mW = power_mW;
  1649. return 0;
  1650. }
  1651. supports_a2 = sfp->id.ext.sff8472_compliance !=
  1652. SFP_SFF8472_COMPLIANCE_NONE ||
  1653. sfp->id.ext.diagmon & SFP_DIAGMON_DDM;
  1654. if (power_mW > sfp->max_power_mW) {
  1655. /* Module power specification exceeds the allowed maximum. */
  1656. if (!supports_a2) {
  1657. /* The module appears not to implement bus address
  1658. * 0xa2, so assume that the module powers up in the
  1659. * indicated mode.
  1660. */
  1661. dev_err(sfp->dev,
  1662. "Host does not support %u.%uW modules\n",
  1663. power_mW / 1000, (power_mW / 100) % 10);
  1664. return -EINVAL;
  1665. } else {
  1666. dev_warn(sfp->dev,
  1667. "Host does not support %u.%uW modules, module left in power mode 1\n",
  1668. power_mW / 1000, (power_mW / 100) % 10);
  1669. return 0;
  1670. }
  1671. }
  1672. if (!supports_a2) {
  1673. /* The module power level is below the host maximum and the
  1674. * module appears not to implement bus address 0xa2, so assume
  1675. * that the module powers up in the indicated mode.
  1676. */
  1677. return 0;
  1678. }
  1679. /* If the module requires a higher power mode, but also requires
  1680. * an address change sequence, warn the user that the module may
  1681. * not be functional.
  1682. */
  1683. if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) {
  1684. dev_warn(sfp->dev,
  1685. "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
  1686. power_mW / 1000, (power_mW / 100) % 10);
  1687. return 0;
  1688. }
  1689. sfp->module_power_mW = power_mW;
  1690. return 0;
  1691. }
  1692. static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
  1693. {
  1694. int err;
  1695. err = sfp_modify_u8(sfp, true, SFP_EXT_STATUS,
  1696. SFP_EXT_STATUS_PWRLVL_SELECT,
  1697. enable ? SFP_EXT_STATUS_PWRLVL_SELECT : 0);
  1698. if (err != sizeof(u8)) {
  1699. dev_err(sfp->dev, "failed to %sable high power: %pe\n",
  1700. enable ? "en" : "dis", ERR_PTR(err));
  1701. return -EAGAIN;
  1702. }
  1703. if (enable)
  1704. dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
  1705. sfp->module_power_mW / 1000,
  1706. (sfp->module_power_mW / 100) % 10);
  1707. return 0;
  1708. }
  1709. static void sfp_module_parse_rate_select(struct sfp *sfp)
  1710. {
  1711. u8 rate_id;
  1712. sfp->rs_threshold_kbd = 0;
  1713. sfp->rs_state_mask = 0;
  1714. if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_RATE_SELECT)))
  1715. /* No support for RateSelect */
  1716. return;
  1717. /* Default to INF-8074 RateSelect operation. The signalling threshold
  1718. * rate is not well specified, so always select "Full Bandwidth", but
  1719. * SFF-8079 reveals that it is understood that RS0 will be low for
  1720. * 1.0625Gb/s and high for 2.125Gb/s. Choose a value half-way between.
  1721. * This method exists prior to SFF-8472.
  1722. */
  1723. sfp->rs_state_mask = SFP_F_RS0;
  1724. sfp->rs_threshold_kbd = 1594;
  1725. /* Parse the rate identifier, which is complicated due to history:
  1726. * SFF-8472 rev 9.5 marks this field as reserved.
  1727. * SFF-8079 references SFF-8472 rev 9.5 and defines bit 0. SFF-8472
  1728. * compliance is not required.
  1729. * SFF-8472 rev 10.2 defines this field using values 0..4
  1730. * SFF-8472 rev 11.0 redefines this field with bit 0 for SFF-8079
  1731. * and even values.
  1732. */
  1733. rate_id = sfp->id.base.rate_id;
  1734. if (rate_id == 0)
  1735. /* Unspecified */
  1736. return;
  1737. /* SFF-8472 rev 10.0..10.4 did not account for SFF-8079 using bit 0,
  1738. * and allocated value 3 to SFF-8431 independent tx/rx rate select.
  1739. * Convert this to a SFF-8472 rev 11.0 rate identifier.
  1740. */
  1741. if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 &&
  1742. sfp->id.ext.sff8472_compliance < SFP_SFF8472_COMPLIANCE_REV11_0 &&
  1743. rate_id == 3)
  1744. rate_id = SFF_RID_8431;
  1745. if (rate_id & SFF_RID_8079) {
  1746. /* SFF-8079 RateSelect / Application Select in conjunction with
  1747. * SFF-8472 rev 9.5. SFF-8079 defines rate_id as a bitfield
  1748. * with only bit 0 used, which takes precedence over SFF-8472.
  1749. */
  1750. if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_APP_SELECT_SFF8079)) {
  1751. /* SFF-8079 Part 1 - rate selection between Fibre
  1752. * Channel 1.0625/2.125/4.25 Gbd modes. Note that RS0
  1753. * is high for 2125, so we have to subtract 1 to
  1754. * include it.
  1755. */
  1756. sfp->rs_threshold_kbd = 2125 - 1;
  1757. sfp->rs_state_mask = SFP_F_RS0;
  1758. }
  1759. return;
  1760. }
  1761. /* SFF-8472 rev 9.5 does not define the rate identifier */
  1762. if (sfp->id.ext.sff8472_compliance <= SFP_SFF8472_COMPLIANCE_REV9_5)
  1763. return;
  1764. /* SFF-8472 rev 11.0 defines rate_id as a numerical value which will
  1765. * always have bit 0 clear due to SFF-8079's bitfield usage of rate_id.
  1766. */
  1767. switch (rate_id) {
  1768. case SFF_RID_8431_RX_ONLY:
  1769. sfp->rs_threshold_kbd = 4250;
  1770. sfp->rs_state_mask = SFP_F_RS0;
  1771. break;
  1772. case SFF_RID_8431_TX_ONLY:
  1773. sfp->rs_threshold_kbd = 4250;
  1774. sfp->rs_state_mask = SFP_F_RS1;
  1775. break;
  1776. case SFF_RID_8431:
  1777. sfp->rs_threshold_kbd = 4250;
  1778. sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1;
  1779. break;
  1780. case SFF_RID_10G8G:
  1781. sfp->rs_threshold_kbd = 9000;
  1782. sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1;
  1783. break;
  1784. }
  1785. }
  1786. /* GPON modules based on Realtek RTL8672 and RTL9601C chips (e.g. V-SOL
  1787. * V2801F, CarlitoxxPro CPGOS03-0490, Ubiquiti U-Fiber Instant, ...) do
  1788. * not support multibyte reads from the EEPROM. Each multi-byte read
  1789. * operation returns just one byte of EEPROM followed by zeros. There is
  1790. * no way to identify which modules are using Realtek RTL8672 and RTL9601C
  1791. * chips. Moreover every OEM of V-SOL V2801F module puts its own vendor
  1792. * name and vendor id into EEPROM, so there is even no way to detect if
  1793. * module is V-SOL V2801F. Therefore check for those zeros in the read
  1794. * data and then based on check switch to reading EEPROM to one byte
  1795. * at a time.
  1796. */
  1797. static bool sfp_id_needs_byte_io(struct sfp *sfp, void *buf, size_t len)
  1798. {
  1799. size_t i, block_size = sfp->i2c_block_size;
  1800. /* Already using byte IO */
  1801. if (block_size == 1)
  1802. return false;
  1803. for (i = 1; i < len; i += block_size) {
  1804. if (memchr_inv(buf + i, '\0', min(block_size - 1, len - i)))
  1805. return false;
  1806. }
  1807. return true;
  1808. }
  1809. static int sfp_cotsworks_fixup_check(struct sfp *sfp, struct sfp_eeprom_id *id)
  1810. {
  1811. u8 check;
  1812. int err;
  1813. if (id->base.phys_id != SFF8024_ID_SFF_8472 ||
  1814. id->base.phys_ext_id != SFP_PHYS_EXT_ID_SFP ||
  1815. id->base.connector != SFF8024_CONNECTOR_LC) {
  1816. dev_warn(sfp->dev, "Rewriting fiber module EEPROM with corrected values\n");
  1817. id->base.phys_id = SFF8024_ID_SFF_8472;
  1818. id->base.phys_ext_id = SFP_PHYS_EXT_ID_SFP;
  1819. id->base.connector = SFF8024_CONNECTOR_LC;
  1820. err = sfp_write(sfp, false, SFP_PHYS_ID, &id->base, 3);
  1821. if (err != 3) {
  1822. dev_err(sfp->dev,
  1823. "Failed to rewrite module EEPROM: %pe\n",
  1824. ERR_PTR(err));
  1825. return err;
  1826. }
  1827. /* Cotsworks modules have been found to require a delay between write operations. */
  1828. mdelay(50);
  1829. /* Update base structure checksum */
  1830. check = sfp_check(&id->base, sizeof(id->base) - 1);
  1831. err = sfp_write(sfp, false, SFP_CC_BASE, &check, 1);
  1832. if (err != 1) {
  1833. dev_err(sfp->dev,
  1834. "Failed to update base structure checksum in fiber module EEPROM: %pe\n",
  1835. ERR_PTR(err));
  1836. return err;
  1837. }
  1838. }
  1839. return 0;
  1840. }
  1841. static int sfp_module_parse_sff8472(struct sfp *sfp)
  1842. {
  1843. /* If the module requires address swap mode, warn about it */
  1844. if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
  1845. dev_warn(sfp->dev,
  1846. "module address swap to access page 0xA2 is not supported.\n");
  1847. else
  1848. sfp->have_a2 = true;
  1849. return 0;
  1850. }
  1851. static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
  1852. {
  1853. /* SFP module inserted - read I2C data */
  1854. struct sfp_eeprom_id id;
  1855. bool cotsworks_sfbg;
  1856. unsigned int mask;
  1857. bool cotsworks;
  1858. u8 check;
  1859. int ret;
  1860. sfp->i2c_block_size = SFP_EEPROM_BLOCK_SIZE;
  1861. ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
  1862. if (ret < 0) {
  1863. if (report)
  1864. dev_err(sfp->dev, "failed to read EEPROM: %pe\n",
  1865. ERR_PTR(ret));
  1866. return -EAGAIN;
  1867. }
  1868. if (ret != sizeof(id.base)) {
  1869. dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret));
  1870. return -EAGAIN;
  1871. }
  1872. /* Some SFP modules (e.g. Nokia 3FE46541AA) lock up if read from
  1873. * address 0x51 is just one byte at a time. Also SFF-8472 requires
  1874. * that EEPROM supports atomic 16bit read operation for diagnostic
  1875. * fields, so do not switch to one byte reading at a time unless it
  1876. * is really required and we have no other option.
  1877. */
  1878. if (sfp_id_needs_byte_io(sfp, &id.base, sizeof(id.base))) {
  1879. dev_info(sfp->dev,
  1880. "Detected broken RTL8672/RTL9601C emulated EEPROM\n");
  1881. dev_info(sfp->dev,
  1882. "Switching to reading EEPROM to one byte at a time\n");
  1883. sfp->i2c_block_size = 1;
  1884. ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
  1885. if (ret < 0) {
  1886. if (report)
  1887. dev_err(sfp->dev,
  1888. "failed to read EEPROM: %pe\n",
  1889. ERR_PTR(ret));
  1890. return -EAGAIN;
  1891. }
  1892. if (ret != sizeof(id.base)) {
  1893. dev_err(sfp->dev, "EEPROM short read: %pe\n",
  1894. ERR_PTR(ret));
  1895. return -EAGAIN;
  1896. }
  1897. }
  1898. /* Cotsworks do not seem to update the checksums when they
  1899. * do the final programming with the final module part number,
  1900. * serial number and date code.
  1901. */
  1902. cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
  1903. cotsworks_sfbg = !memcmp(id.base.vendor_pn, "SFBG", 4);
  1904. /* Cotsworks SFF module EEPROM do not always have valid phys_id,
  1905. * phys_ext_id, and connector bytes. Rewrite SFF EEPROM bytes if
  1906. * Cotsworks PN matches and bytes are not correct.
  1907. */
  1908. if (cotsworks && cotsworks_sfbg) {
  1909. ret = sfp_cotsworks_fixup_check(sfp, &id);
  1910. if (ret < 0)
  1911. return ret;
  1912. }
  1913. /* Validate the checksum over the base structure */
  1914. check = sfp_check(&id.base, sizeof(id.base) - 1);
  1915. if (check != id.base.cc_base) {
  1916. if (cotsworks) {
  1917. dev_warn(sfp->dev,
  1918. "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
  1919. check, id.base.cc_base);
  1920. } else {
  1921. dev_err(sfp->dev,
  1922. "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
  1923. check, id.base.cc_base);
  1924. print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
  1925. 16, 1, &id, sizeof(id), true);
  1926. return -EINVAL;
  1927. }
  1928. }
  1929. ret = sfp_read(sfp, false, SFP_CC_BASE + 1, &id.ext, sizeof(id.ext));
  1930. if (ret < 0) {
  1931. if (report)
  1932. dev_err(sfp->dev, "failed to read EEPROM: %pe\n",
  1933. ERR_PTR(ret));
  1934. return -EAGAIN;
  1935. }
  1936. if (ret != sizeof(id.ext)) {
  1937. dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret));
  1938. return -EAGAIN;
  1939. }
  1940. check = sfp_check(&id.ext, sizeof(id.ext) - 1);
  1941. if (check != id.ext.cc_ext) {
  1942. if (cotsworks) {
  1943. dev_warn(sfp->dev,
  1944. "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
  1945. check, id.ext.cc_ext);
  1946. } else {
  1947. dev_err(sfp->dev,
  1948. "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
  1949. check, id.ext.cc_ext);
  1950. print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
  1951. 16, 1, &id, sizeof(id), true);
  1952. memset(&id.ext, 0, sizeof(id.ext));
  1953. }
  1954. }
  1955. sfp->id = id;
  1956. dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
  1957. (int)sizeof(id.base.vendor_name), id.base.vendor_name,
  1958. (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
  1959. (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
  1960. (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
  1961. (int)sizeof(id.ext.datecode), id.ext.datecode);
  1962. /* Check whether we support this module */
  1963. if (!sfp->type->module_supported(&id)) {
  1964. dev_err(sfp->dev,
  1965. "module is not supported - phys id 0x%02x 0x%02x\n",
  1966. sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
  1967. return -EINVAL;
  1968. }
  1969. if (sfp->id.ext.sff8472_compliance != SFP_SFF8472_COMPLIANCE_NONE) {
  1970. ret = sfp_module_parse_sff8472(sfp);
  1971. if (ret < 0)
  1972. return ret;
  1973. }
  1974. /* Parse the module power requirement */
  1975. ret = sfp_module_parse_power(sfp);
  1976. if (ret < 0)
  1977. return ret;
  1978. sfp_module_parse_rate_select(sfp);
  1979. mask = SFP_F_PRESENT;
  1980. if (sfp->gpio[GPIO_TX_DISABLE])
  1981. mask |= SFP_F_TX_DISABLE;
  1982. if (sfp->gpio[GPIO_TX_FAULT])
  1983. mask |= SFP_F_TX_FAULT;
  1984. if (sfp->gpio[GPIO_LOS])
  1985. mask |= SFP_F_LOS;
  1986. if (sfp->gpio[GPIO_RS0])
  1987. mask |= SFP_F_RS0;
  1988. if (sfp->gpio[GPIO_RS1])
  1989. mask |= SFP_F_RS1;
  1990. sfp->module_t_start_up = T_START_UP;
  1991. sfp->module_t_wait = T_WAIT;
  1992. sfp->phy_t_retry = T_PHY_RETRY;
  1993. sfp->state_ignore_mask = 0;
  1994. if (sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SFI ||
  1995. sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SR ||
  1996. sfp->id.base.extended_cc == SFF8024_ECC_5GBASE_T ||
  1997. sfp->id.base.extended_cc == SFF8024_ECC_2_5GBASE_T)
  1998. sfp->mdio_protocol = MDIO_I2C_C45;
  1999. else if (sfp->id.base.e1000_base_t)
  2000. sfp->mdio_protocol = MDIO_I2C_MARVELL_C22;
  2001. else
  2002. sfp->mdio_protocol = MDIO_I2C_NONE;
  2003. sfp->quirk = sfp_lookup_quirk(&id);
  2004. mutex_lock(&sfp->st_mutex);
  2005. /* Initialise state bits to use from hardware */
  2006. sfp->state_hw_mask = mask;
  2007. /* We want to drive the rate select pins that the module is using */
  2008. sfp->state_hw_drive |= sfp->rs_state_mask;
  2009. if (sfp->quirk && sfp->quirk->fixup)
  2010. sfp->quirk->fixup(sfp);
  2011. sfp->state_hw_mask &= ~sfp->state_ignore_mask;
  2012. mutex_unlock(&sfp->st_mutex);
  2013. return 0;
  2014. }
  2015. static void sfp_sm_mod_remove(struct sfp *sfp)
  2016. {
  2017. if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
  2018. sfp_module_remove(sfp->sfp_bus);
  2019. sfp_hwmon_remove(sfp);
  2020. memset(&sfp->id, 0, sizeof(sfp->id));
  2021. sfp->module_power_mW = 0;
  2022. sfp->state_hw_drive = SFP_F_TX_DISABLE;
  2023. sfp->have_a2 = false;
  2024. dev_info(sfp->dev, "module removed\n");
  2025. }
  2026. /* This state machine tracks the upstream's state */
  2027. static void sfp_sm_device(struct sfp *sfp, unsigned int event)
  2028. {
  2029. switch (sfp->sm_dev_state) {
  2030. default:
  2031. if (event == SFP_E_DEV_ATTACH)
  2032. sfp->sm_dev_state = SFP_DEV_DOWN;
  2033. break;
  2034. case SFP_DEV_DOWN:
  2035. if (event == SFP_E_DEV_DETACH)
  2036. sfp->sm_dev_state = SFP_DEV_DETACHED;
  2037. else if (event == SFP_E_DEV_UP)
  2038. sfp->sm_dev_state = SFP_DEV_UP;
  2039. break;
  2040. case SFP_DEV_UP:
  2041. if (event == SFP_E_DEV_DETACH)
  2042. sfp->sm_dev_state = SFP_DEV_DETACHED;
  2043. else if (event == SFP_E_DEV_DOWN)
  2044. sfp->sm_dev_state = SFP_DEV_DOWN;
  2045. break;
  2046. }
  2047. }
  2048. /* This state machine tracks the insert/remove state of the module, probes
  2049. * the on-board EEPROM, and sets up the power level.
  2050. */
  2051. static void sfp_sm_module(struct sfp *sfp, unsigned int event)
  2052. {
  2053. int err;
  2054. /* Handle remove event globally, it resets this state machine */
  2055. if (event == SFP_E_REMOVE) {
  2056. sfp_sm_mod_remove(sfp);
  2057. sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
  2058. return;
  2059. }
  2060. /* Handle device detach globally */
  2061. if (sfp->sm_dev_state < SFP_DEV_DOWN &&
  2062. sfp->sm_mod_state > SFP_MOD_WAITDEV) {
  2063. if (sfp->module_power_mW > 1000 &&
  2064. sfp->sm_mod_state > SFP_MOD_HPOWER)
  2065. sfp_sm_mod_hpower(sfp, false);
  2066. sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
  2067. return;
  2068. }
  2069. switch (sfp->sm_mod_state) {
  2070. default:
  2071. if (event == SFP_E_INSERT) {
  2072. sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
  2073. sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
  2074. sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
  2075. }
  2076. break;
  2077. case SFP_MOD_PROBE:
  2078. /* Wait for T_PROBE_INIT to time out */
  2079. if (event != SFP_E_TIMEOUT)
  2080. break;
  2081. err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
  2082. if (err == -EAGAIN) {
  2083. if (sfp->sm_mod_tries_init &&
  2084. --sfp->sm_mod_tries_init) {
  2085. sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
  2086. break;
  2087. } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
  2088. if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
  2089. dev_warn(sfp->dev,
  2090. "please wait, module slow to respond\n");
  2091. sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
  2092. break;
  2093. }
  2094. }
  2095. if (err < 0) {
  2096. sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
  2097. break;
  2098. }
  2099. /* Force a poll to re-read the hardware signal state after
  2100. * sfp_sm_mod_probe() changed state_hw_mask.
  2101. */
  2102. mod_delayed_work(system_wq, &sfp->poll, 1);
  2103. err = sfp_hwmon_insert(sfp);
  2104. if (err)
  2105. dev_warn(sfp->dev, "hwmon probe failed: %pe\n",
  2106. ERR_PTR(err));
  2107. sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
  2108. fallthrough;
  2109. case SFP_MOD_WAITDEV:
  2110. /* Ensure that the device is attached before proceeding */
  2111. if (sfp->sm_dev_state < SFP_DEV_DOWN)
  2112. break;
  2113. /* Report the module insertion to the upstream device */
  2114. err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
  2115. sfp->quirk);
  2116. if (err < 0) {
  2117. sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
  2118. break;
  2119. }
  2120. /* If this is a power level 1 module, we are done */
  2121. if (sfp->module_power_mW <= 1000)
  2122. goto insert;
  2123. sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
  2124. fallthrough;
  2125. case SFP_MOD_HPOWER:
  2126. /* Enable high power mode */
  2127. err = sfp_sm_mod_hpower(sfp, true);
  2128. if (err < 0) {
  2129. if (err != -EAGAIN) {
  2130. sfp_module_remove(sfp->sfp_bus);
  2131. sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
  2132. } else {
  2133. sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
  2134. }
  2135. break;
  2136. }
  2137. sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
  2138. break;
  2139. case SFP_MOD_WAITPWR:
  2140. /* Wait for T_HPOWER_LEVEL to time out */
  2141. if (event != SFP_E_TIMEOUT)
  2142. break;
  2143. insert:
  2144. sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
  2145. break;
  2146. case SFP_MOD_PRESENT:
  2147. case SFP_MOD_ERROR:
  2148. break;
  2149. }
  2150. }
  2151. static void sfp_sm_main(struct sfp *sfp, unsigned int event)
  2152. {
  2153. unsigned long timeout;
  2154. int ret;
  2155. /* Some events are global */
  2156. if (sfp->sm_state != SFP_S_DOWN &&
  2157. (sfp->sm_mod_state != SFP_MOD_PRESENT ||
  2158. sfp->sm_dev_state != SFP_DEV_UP)) {
  2159. if (sfp->sm_state == SFP_S_LINK_UP &&
  2160. sfp->sm_dev_state == SFP_DEV_UP)
  2161. sfp_sm_link_down(sfp);
  2162. if (sfp->sm_state > SFP_S_INIT)
  2163. sfp_module_stop(sfp->sfp_bus);
  2164. if (sfp->mod_phy)
  2165. sfp_sm_phy_detach(sfp);
  2166. if (sfp->i2c_mii)
  2167. sfp_i2c_mdiobus_destroy(sfp);
  2168. sfp_module_tx_disable(sfp);
  2169. sfp_soft_stop_poll(sfp);
  2170. sfp_sm_next(sfp, SFP_S_DOWN, 0);
  2171. return;
  2172. }
  2173. /* The main state machine */
  2174. switch (sfp->sm_state) {
  2175. case SFP_S_DOWN:
  2176. if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
  2177. sfp->sm_dev_state != SFP_DEV_UP)
  2178. break;
  2179. /* Only use the soft state bits if we have access to the A2h
  2180. * memory, which implies that we have some level of SFF-8472
  2181. * compliance.
  2182. */
  2183. if (sfp->have_a2)
  2184. sfp_soft_start_poll(sfp);
  2185. sfp_module_tx_enable(sfp);
  2186. /* Initialise the fault clearance retries */
  2187. sfp->sm_fault_retries = N_FAULT_INIT;
  2188. /* We need to check the TX_FAULT state, which is not defined
  2189. * while TX_DISABLE is asserted. The earliest we want to do
  2190. * anything (such as probe for a PHY) is 50ms (or more on
  2191. * specific modules).
  2192. */
  2193. sfp_sm_next(sfp, SFP_S_WAIT, sfp->module_t_wait);
  2194. break;
  2195. case SFP_S_WAIT:
  2196. if (event != SFP_E_TIMEOUT)
  2197. break;
  2198. if (sfp->state & SFP_F_TX_FAULT) {
  2199. /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431)
  2200. * from the TX_DISABLE deassertion for the module to
  2201. * initialise, which is indicated by TX_FAULT
  2202. * deasserting.
  2203. */
  2204. timeout = sfp->module_t_start_up;
  2205. if (timeout > sfp->module_t_wait)
  2206. timeout -= sfp->module_t_wait;
  2207. else
  2208. timeout = 1;
  2209. sfp_sm_next(sfp, SFP_S_INIT, timeout);
  2210. } else {
  2211. /* TX_FAULT is not asserted, assume the module has
  2212. * finished initialising.
  2213. */
  2214. goto init_done;
  2215. }
  2216. break;
  2217. case SFP_S_INIT:
  2218. if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
  2219. /* TX_FAULT is still asserted after t_init
  2220. * or t_start_up, so assume there is a fault.
  2221. */
  2222. sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
  2223. sfp->sm_fault_retries == N_FAULT_INIT);
  2224. } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
  2225. init_done:
  2226. /* Create mdiobus and start trying for PHY */
  2227. ret = sfp_sm_add_mdio_bus(sfp);
  2228. if (ret < 0) {
  2229. sfp_sm_next(sfp, SFP_S_FAIL, 0);
  2230. break;
  2231. }
  2232. sfp->sm_phy_retries = R_PHY_RETRY;
  2233. goto phy_probe;
  2234. }
  2235. break;
  2236. case SFP_S_INIT_PHY:
  2237. if (event != SFP_E_TIMEOUT)
  2238. break;
  2239. phy_probe:
  2240. /* TX_FAULT deasserted or we timed out with TX_FAULT
  2241. * clear. Probe for the PHY and check the LOS state.
  2242. */
  2243. ret = sfp_sm_probe_for_phy(sfp);
  2244. if (ret == -ENODEV) {
  2245. if (--sfp->sm_phy_retries) {
  2246. sfp_sm_next(sfp, SFP_S_INIT_PHY,
  2247. sfp->phy_t_retry);
  2248. dev_dbg(sfp->dev,
  2249. "no PHY detected, %u tries left\n",
  2250. sfp->sm_phy_retries);
  2251. break;
  2252. } else {
  2253. dev_info(sfp->dev, "no PHY detected\n");
  2254. }
  2255. } else if (ret) {
  2256. sfp_sm_next(sfp, SFP_S_FAIL, 0);
  2257. break;
  2258. }
  2259. if (sfp_module_start(sfp->sfp_bus)) {
  2260. sfp_sm_next(sfp, SFP_S_FAIL, 0);
  2261. break;
  2262. }
  2263. sfp_sm_link_check_los(sfp);
  2264. /* Reset the fault retry count */
  2265. sfp->sm_fault_retries = N_FAULT;
  2266. break;
  2267. case SFP_S_INIT_TX_FAULT:
  2268. if (event == SFP_E_TIMEOUT) {
  2269. sfp_module_tx_fault_reset(sfp);
  2270. sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up);
  2271. }
  2272. break;
  2273. case SFP_S_WAIT_LOS:
  2274. if (event == SFP_E_TX_FAULT)
  2275. sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
  2276. else if (sfp_los_event_inactive(sfp, event))
  2277. sfp_sm_link_up(sfp);
  2278. break;
  2279. case SFP_S_LINK_UP:
  2280. if (event == SFP_E_TX_FAULT) {
  2281. sfp_sm_link_down(sfp);
  2282. sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
  2283. } else if (sfp_los_event_active(sfp, event)) {
  2284. sfp_sm_link_down(sfp);
  2285. sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
  2286. }
  2287. break;
  2288. case SFP_S_TX_FAULT:
  2289. if (event == SFP_E_TIMEOUT) {
  2290. sfp_module_tx_fault_reset(sfp);
  2291. sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up);
  2292. }
  2293. break;
  2294. case SFP_S_REINIT:
  2295. if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
  2296. sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
  2297. } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
  2298. dev_info(sfp->dev, "module transmit fault recovered\n");
  2299. sfp_sm_link_check_los(sfp);
  2300. }
  2301. break;
  2302. case SFP_S_TX_DISABLE:
  2303. break;
  2304. }
  2305. }
  2306. static void __sfp_sm_event(struct sfp *sfp, unsigned int event)
  2307. {
  2308. dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
  2309. mod_state_to_str(sfp->sm_mod_state),
  2310. dev_state_to_str(sfp->sm_dev_state),
  2311. sm_state_to_str(sfp->sm_state),
  2312. event_to_str(event));
  2313. sfp_sm_device(sfp, event);
  2314. sfp_sm_module(sfp, event);
  2315. sfp_sm_main(sfp, event);
  2316. dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
  2317. mod_state_to_str(sfp->sm_mod_state),
  2318. dev_state_to_str(sfp->sm_dev_state),
  2319. sm_state_to_str(sfp->sm_state));
  2320. }
  2321. static void sfp_sm_event(struct sfp *sfp, unsigned int event)
  2322. {
  2323. mutex_lock(&sfp->sm_mutex);
  2324. __sfp_sm_event(sfp, event);
  2325. mutex_unlock(&sfp->sm_mutex);
  2326. }
  2327. static void sfp_attach(struct sfp *sfp)
  2328. {
  2329. sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
  2330. }
  2331. static void sfp_detach(struct sfp *sfp)
  2332. {
  2333. sfp_sm_event(sfp, SFP_E_DEV_DETACH);
  2334. }
  2335. static void sfp_start(struct sfp *sfp)
  2336. {
  2337. sfp_sm_event(sfp, SFP_E_DEV_UP);
  2338. }
  2339. static void sfp_stop(struct sfp *sfp)
  2340. {
  2341. sfp_sm_event(sfp, SFP_E_DEV_DOWN);
  2342. }
  2343. static void sfp_set_signal_rate(struct sfp *sfp, unsigned int rate_kbd)
  2344. {
  2345. unsigned int set;
  2346. sfp->rate_kbd = rate_kbd;
  2347. if (rate_kbd > sfp->rs_threshold_kbd)
  2348. set = sfp->rs_state_mask;
  2349. else
  2350. set = 0;
  2351. sfp_mod_state(sfp, SFP_F_RS0 | SFP_F_RS1, set);
  2352. }
  2353. static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
  2354. {
  2355. /* locking... and check module is present */
  2356. if (sfp->id.ext.sff8472_compliance &&
  2357. !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
  2358. modinfo->type = ETH_MODULE_SFF_8472;
  2359. modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
  2360. } else {
  2361. modinfo->type = ETH_MODULE_SFF_8079;
  2362. modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
  2363. }
  2364. return 0;
  2365. }
  2366. static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
  2367. u8 *data)
  2368. {
  2369. unsigned int first, last, len;
  2370. int ret;
  2371. if (!(sfp->state & SFP_F_PRESENT))
  2372. return -ENODEV;
  2373. if (ee->len == 0)
  2374. return -EINVAL;
  2375. first = ee->offset;
  2376. last = ee->offset + ee->len;
  2377. if (first < ETH_MODULE_SFF_8079_LEN) {
  2378. len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
  2379. len -= first;
  2380. ret = sfp_read(sfp, false, first, data, len);
  2381. if (ret < 0)
  2382. return ret;
  2383. first += len;
  2384. data += len;
  2385. }
  2386. if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
  2387. len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
  2388. len -= first;
  2389. first -= ETH_MODULE_SFF_8079_LEN;
  2390. ret = sfp_read(sfp, true, first, data, len);
  2391. if (ret < 0)
  2392. return ret;
  2393. }
  2394. return 0;
  2395. }
  2396. static int sfp_module_eeprom_by_page(struct sfp *sfp,
  2397. const struct ethtool_module_eeprom *page,
  2398. struct netlink_ext_ack *extack)
  2399. {
  2400. if (!(sfp->state & SFP_F_PRESENT))
  2401. return -ENODEV;
  2402. if (page->bank) {
  2403. NL_SET_ERR_MSG(extack, "Banks not supported");
  2404. return -EOPNOTSUPP;
  2405. }
  2406. if (page->page) {
  2407. NL_SET_ERR_MSG(extack, "Only page 0 supported");
  2408. return -EOPNOTSUPP;
  2409. }
  2410. if (page->i2c_address != 0x50 &&
  2411. page->i2c_address != 0x51) {
  2412. NL_SET_ERR_MSG(extack, "Only address 0x50 and 0x51 supported");
  2413. return -EOPNOTSUPP;
  2414. }
  2415. return sfp_read(sfp, page->i2c_address == 0x51, page->offset,
  2416. page->data, page->length);
  2417. };
  2418. static const struct sfp_socket_ops sfp_module_ops = {
  2419. .attach = sfp_attach,
  2420. .detach = sfp_detach,
  2421. .start = sfp_start,
  2422. .stop = sfp_stop,
  2423. .set_signal_rate = sfp_set_signal_rate,
  2424. .module_info = sfp_module_info,
  2425. .module_eeprom = sfp_module_eeprom,
  2426. .module_eeprom_by_page = sfp_module_eeprom_by_page,
  2427. };
  2428. static void sfp_timeout(struct work_struct *work)
  2429. {
  2430. struct sfp *sfp = container_of(work, struct sfp, timeout.work);
  2431. rtnl_lock();
  2432. sfp_sm_event(sfp, SFP_E_TIMEOUT);
  2433. rtnl_unlock();
  2434. }
  2435. static void sfp_check_state(struct sfp *sfp)
  2436. {
  2437. unsigned int state, i, changed;
  2438. rtnl_lock();
  2439. mutex_lock(&sfp->st_mutex);
  2440. state = sfp_get_state(sfp);
  2441. changed = state ^ sfp->state;
  2442. changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
  2443. for (i = 0; i < GPIO_MAX; i++)
  2444. if (changed & BIT(i))
  2445. dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_names[i],
  2446. !!(sfp->state & BIT(i)), !!(state & BIT(i)));
  2447. state |= sfp->state & SFP_F_OUTPUTS;
  2448. sfp->state = state;
  2449. mutex_unlock(&sfp->st_mutex);
  2450. mutex_lock(&sfp->sm_mutex);
  2451. if (changed & SFP_F_PRESENT)
  2452. __sfp_sm_event(sfp, state & SFP_F_PRESENT ?
  2453. SFP_E_INSERT : SFP_E_REMOVE);
  2454. if (changed & SFP_F_TX_FAULT)
  2455. __sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
  2456. SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
  2457. if (changed & SFP_F_LOS)
  2458. __sfp_sm_event(sfp, state & SFP_F_LOS ?
  2459. SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
  2460. mutex_unlock(&sfp->sm_mutex);
  2461. rtnl_unlock();
  2462. }
  2463. static irqreturn_t sfp_irq(int irq, void *data)
  2464. {
  2465. struct sfp *sfp = data;
  2466. sfp_check_state(sfp);
  2467. return IRQ_HANDLED;
  2468. }
  2469. static void sfp_poll(struct work_struct *work)
  2470. {
  2471. struct sfp *sfp = container_of(work, struct sfp, poll.work);
  2472. sfp_check_state(sfp);
  2473. // st_mutex doesn't need to be held here for state_soft_mask,
  2474. // it's unimportant if we race while reading this.
  2475. if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
  2476. sfp->need_poll)
  2477. mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
  2478. }
  2479. static struct sfp *sfp_alloc(struct device *dev)
  2480. {
  2481. struct sfp *sfp;
  2482. sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
  2483. if (!sfp)
  2484. return ERR_PTR(-ENOMEM);
  2485. sfp->dev = dev;
  2486. sfp->i2c_block_size = SFP_EEPROM_BLOCK_SIZE;
  2487. mutex_init(&sfp->sm_mutex);
  2488. mutex_init(&sfp->st_mutex);
  2489. INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
  2490. INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
  2491. sfp_hwmon_init(sfp);
  2492. return sfp;
  2493. }
  2494. static void sfp_cleanup(void *data)
  2495. {
  2496. struct sfp *sfp = data;
  2497. sfp_hwmon_exit(sfp);
  2498. cancel_delayed_work_sync(&sfp->poll);
  2499. cancel_delayed_work_sync(&sfp->timeout);
  2500. if (sfp->i2c_mii) {
  2501. mdiobus_unregister(sfp->i2c_mii);
  2502. mdiobus_free(sfp->i2c_mii);
  2503. }
  2504. if (sfp->i2c)
  2505. i2c_put_adapter(sfp->i2c);
  2506. kfree(sfp);
  2507. }
  2508. static int sfp_i2c_get(struct sfp *sfp)
  2509. {
  2510. struct fwnode_handle *h;
  2511. struct i2c_adapter *i2c;
  2512. int err;
  2513. h = fwnode_find_reference(dev_fwnode(sfp->dev), "i2c-bus", 0);
  2514. if (IS_ERR(h)) {
  2515. dev_err(sfp->dev, "missing 'i2c-bus' property\n");
  2516. return -ENODEV;
  2517. }
  2518. i2c = i2c_get_adapter_by_fwnode(h);
  2519. if (!i2c) {
  2520. err = -EPROBE_DEFER;
  2521. goto put;
  2522. }
  2523. err = sfp_i2c_configure(sfp, i2c);
  2524. if (err)
  2525. i2c_put_adapter(i2c);
  2526. put:
  2527. fwnode_handle_put(h);
  2528. return err;
  2529. }
  2530. static int sfp_probe(struct platform_device *pdev)
  2531. {
  2532. const struct sff_data *sff;
  2533. char *sfp_irq_name;
  2534. struct sfp *sfp;
  2535. int err, i;
  2536. sfp = sfp_alloc(&pdev->dev);
  2537. if (IS_ERR(sfp))
  2538. return PTR_ERR(sfp);
  2539. platform_set_drvdata(pdev, sfp);
  2540. err = devm_add_action_or_reset(sfp->dev, sfp_cleanup, sfp);
  2541. if (err < 0)
  2542. return err;
  2543. sff = device_get_match_data(sfp->dev);
  2544. if (!sff)
  2545. sff = &sfp_data;
  2546. sfp->type = sff;
  2547. err = sfp_i2c_get(sfp);
  2548. if (err)
  2549. return err;
  2550. for (i = 0; i < GPIO_MAX; i++)
  2551. if (sff->gpios & BIT(i)) {
  2552. sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
  2553. gpio_names[i], gpio_flags[i]);
  2554. if (IS_ERR(sfp->gpio[i]))
  2555. return PTR_ERR(sfp->gpio[i]);
  2556. }
  2557. sfp->state_hw_mask = SFP_F_PRESENT;
  2558. sfp->state_hw_drive = SFP_F_TX_DISABLE;
  2559. sfp->get_state = sfp_gpio_get_state;
  2560. sfp->set_state = sfp_gpio_set_state;
  2561. /* Modules that have no detect signal are always present */
  2562. if (!(sfp->gpio[GPIO_MODDEF0]))
  2563. sfp->get_state = sff_gpio_get_state;
  2564. device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
  2565. &sfp->max_power_mW);
  2566. if (sfp->max_power_mW < 1000) {
  2567. if (sfp->max_power_mW)
  2568. dev_warn(sfp->dev,
  2569. "Firmware bug: host maximum power should be at least 1W\n");
  2570. sfp->max_power_mW = 1000;
  2571. }
  2572. dev_info(sfp->dev, "Host maximum power %u.%uW\n",
  2573. sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
  2574. /* Get the initial state, and always signal TX disable,
  2575. * since the network interface will not be up.
  2576. */
  2577. sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
  2578. if (sfp->gpio[GPIO_RS0] &&
  2579. gpiod_get_value_cansleep(sfp->gpio[GPIO_RS0]))
  2580. sfp->state |= SFP_F_RS0;
  2581. sfp_set_state(sfp, sfp->state);
  2582. sfp_module_tx_disable(sfp);
  2583. if (sfp->state & SFP_F_PRESENT) {
  2584. rtnl_lock();
  2585. sfp_sm_event(sfp, SFP_E_INSERT);
  2586. rtnl_unlock();
  2587. }
  2588. for (i = 0; i < GPIO_MAX; i++) {
  2589. if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
  2590. continue;
  2591. sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
  2592. if (sfp->gpio_irq[i] < 0) {
  2593. sfp->gpio_irq[i] = 0;
  2594. sfp->need_poll = true;
  2595. continue;
  2596. }
  2597. sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL,
  2598. "%s-%s", dev_name(sfp->dev),
  2599. gpio_names[i]);
  2600. if (!sfp_irq_name)
  2601. return -ENOMEM;
  2602. err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
  2603. NULL, sfp_irq,
  2604. IRQF_ONESHOT |
  2605. IRQF_TRIGGER_RISING |
  2606. IRQF_TRIGGER_FALLING,
  2607. sfp_irq_name, sfp);
  2608. if (err) {
  2609. sfp->gpio_irq[i] = 0;
  2610. sfp->need_poll = true;
  2611. }
  2612. }
  2613. if (sfp->need_poll)
  2614. mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
  2615. /* We could have an issue in cases no Tx disable pin is available or
  2616. * wired as modules using a laser as their light source will continue to
  2617. * be active when the fiber is removed. This could be a safety issue and
  2618. * we should at least warn the user about that.
  2619. */
  2620. if (!sfp->gpio[GPIO_TX_DISABLE])
  2621. dev_warn(sfp->dev,
  2622. "No tx_disable pin: SFP modules will always be emitting.\n");
  2623. sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
  2624. if (!sfp->sfp_bus)
  2625. return -ENOMEM;
  2626. sfp_debugfs_init(sfp);
  2627. return 0;
  2628. }
  2629. static void sfp_remove(struct platform_device *pdev)
  2630. {
  2631. struct sfp *sfp = platform_get_drvdata(pdev);
  2632. sfp_debugfs_exit(sfp);
  2633. sfp_unregister_socket(sfp->sfp_bus);
  2634. rtnl_lock();
  2635. sfp_sm_event(sfp, SFP_E_REMOVE);
  2636. rtnl_unlock();
  2637. }
  2638. static void sfp_shutdown(struct platform_device *pdev)
  2639. {
  2640. struct sfp *sfp = platform_get_drvdata(pdev);
  2641. int i;
  2642. for (i = 0; i < GPIO_MAX; i++) {
  2643. if (!sfp->gpio_irq[i])
  2644. continue;
  2645. devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
  2646. }
  2647. cancel_delayed_work_sync(&sfp->poll);
  2648. cancel_delayed_work_sync(&sfp->timeout);
  2649. }
  2650. static struct platform_driver sfp_driver = {
  2651. .probe = sfp_probe,
  2652. .remove_new = sfp_remove,
  2653. .shutdown = sfp_shutdown,
  2654. .driver = {
  2655. .name = "sfp",
  2656. .of_match_table = sfp_of_match,
  2657. },
  2658. };
  2659. static int sfp_init(void)
  2660. {
  2661. poll_jiffies = msecs_to_jiffies(100);
  2662. return platform_driver_register(&sfp_driver);
  2663. }
  2664. module_init(sfp_init);
  2665. static void sfp_exit(void)
  2666. {
  2667. platform_driver_unregister(&sfp_driver);
  2668. }
  2669. module_exit(sfp_exit);
  2670. MODULE_ALIAS("platform:sfp");
  2671. MODULE_AUTHOR("Russell King");
  2672. MODULE_LICENSE("GPL v2");
  2673. MODULE_DESCRIPTION("SFP cage support");