1
0

ip175-switch.patch 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. diff -Nur linux-2.6.29.1.orig/drivers/net/phy/Kconfig linux-2.6.29.1/drivers/net/phy/Kconfig
  2. --- linux-2.6.29.1.orig/drivers/net/phy/Kconfig 2009-04-02 22:55:27.000000000 +0200
  3. +++ linux-2.6.29.1/drivers/net/phy/Kconfig 2009-04-17 12:23:55.049703765 +0200
  4. @@ -61,6 +61,11 @@
  5. ---help---
  6. Currently supports the IP175C PHY.
  7. +config IP175_PHY
  8. + tristate "Drivers for ICPlus PHYs (swconfig compatible)"
  9. + ---help---
  10. + Currently supports the IP175C PHY.
  11. +
  12. config REALTEK_PHY
  13. tristate "Drivers for Realtek PHYs"
  14. ---help---
  15. diff -Nur linux-2.6.29.1.orig/drivers/net/phy/Makefile linux-2.6.29.1/drivers/net/phy/Makefile
  16. --- linux-2.6.29.1.orig/drivers/net/phy/Makefile 2009-04-02 22:55:27.000000000 +0200
  17. +++ linux-2.6.29.1/drivers/net/phy/Makefile 2009-04-17 12:24:22.879180918 +0200
  18. @@ -12,6 +12,7 @@
  19. obj-$(CONFIG_VITESSE_PHY) += vitesse.o
  20. obj-$(CONFIG_BROADCOM_PHY) += broadcom.o
  21. obj-$(CONFIG_ICPLUS_PHY) += icplus.o
  22. +obj-$(CONFIG_IP175_PHY) += swconfig-ip175c.o
  23. obj-$(CONFIG_REALTEK_PHY) += realtek.o
  24. obj-$(CONFIG_LSI_ET1011C_PHY) += et1011c.o
  25. obj-$(CONFIG_FIXED_PHY) += fixed.o
  26. diff -Nur linux-2.6.29.1.orig/drivers/net/phy/swconfig-ip175c.c linux-2.6.29.1/drivers/net/phy/swconfig-ip175c.c
  27. --- linux-2.6.29.1.orig/drivers/net/phy/swconfig-ip175c.c 1970-01-01 01:00:00.000000000 +0100
  28. +++ linux-2.6.29.1/drivers/net/phy/swconfig-ip175c.c 2009-04-17 13:59:08.478770561 +0200
  29. @@ -0,0 +1,1334 @@
  30. +/*
  31. + * swconfig-ip175c.c: Swconfig configuration for IC+ IP175C switch
  32. + * Version 0.3
  33. + *
  34. + * Copyright (C) 2008 Patrick Horn <patrick.horn@gmail.com>
  35. + * Copyright (C) 2008 Martin Mares <mj@ucw.cz>
  36. + *
  37. + * This program is free software; you can redistribute it and/or
  38. + * modify it under the terms of the GNU General Public License
  39. + * as published by the Free Software Foundation; either version 2
  40. + * of the License, or (at your option) any later version.
  41. + *
  42. + * This program is distributed in the hope that it will be useful,
  43. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. + * GNU General Public License for more details.
  46. + */
  47. +
  48. +#include <linux/kernel.h>
  49. +#include <linux/module.h>
  50. +#include <linux/init.h>
  51. +#include <linux/list.h>
  52. +#include <linux/skbuff.h>
  53. +#include <linux/mii.h>
  54. +#include <linux/phy.h>
  55. +#include <linux/delay.h>
  56. +
  57. +#include <linux/switch.h>
  58. +
  59. +#define MAX_VLANS 16
  60. +#define MAX_PORTS 9
  61. +
  62. +#define MODULE_NAME "swconfig-ip175c"
  63. +#define DRIVER_NAME "ip175c"
  64. +#define DRIVER_VERSION "0.3"
  65. +
  66. +typedef struct ip175c_reg {
  67. + u16 p; // phy
  68. + u16 m; // mii
  69. +} reg;
  70. +typedef char bitnum;
  71. +
  72. +#define NOTSUPPORTED {-1,-1}
  73. +
  74. +#define REG_SUPP(x) (((x).m != ((u16)-1)) && ((x).p != (u16)-1))
  75. +
  76. +/*********** CONSTANTS ***********/
  77. +struct register_mappings {
  78. + char *NAME;
  79. + u16 MODEL_NO; // compare to bits 4-9 of MII register 0,3.
  80. + bitnum NUM_PORTS;
  81. + bitnum CPU_PORT;
  82. +
  83. +/* The default VLAN for each port.
  84. + Default: 0x0001 for Ports 0,1,2,3
  85. + 0x0002 for Ports 4,5 */
  86. + reg VLAN_DEFAULT_TAG_REG[MAX_PORTS];
  87. +
  88. +/* These ports are tagged.
  89. + Default: 0x00 */
  90. + reg ADD_TAG_REG;
  91. + reg REMOVE_TAG_REG;
  92. + bitnum ADD_TAG_BIT[MAX_PORTS];
  93. +/* These ports are untagged.
  94. + Default: 0x00 (i.e. do not alter any VLAN tags...)
  95. + Maybe set to 0 if user disables VLANs. */
  96. + bitnum REMOVE_TAG_BIT[MAX_PORTS];
  97. +
  98. +/* Port M and Port N are on the same VLAN.
  99. + Default: All ports on all VLANs. */
  100. +// Use register {29, 19+N/2}
  101. + reg VLAN_LOOKUP_REG;
  102. +// Port 5 uses register {30, 18} but same as odd bits.
  103. + reg VLAN_LOOKUP_REG_5; // in a different register on IP175C.
  104. + bitnum VLAN_LOOKUP_EVEN_BIT[MAX_PORTS];
  105. + bitnum VLAN_LOOKUP_ODD_BIT[MAX_PORTS];
  106. +
  107. +/* This VLAN corresponds to which ports.
  108. + Default: 0x2f,0x30,0x3f,0x3f... */
  109. + reg TAG_VLAN_MASK_REG;
  110. + bitnum TAG_VLAN_MASK_EVEN_BIT[MAX_PORTS];
  111. + bitnum TAG_VLAN_MASK_ODD_BIT[MAX_PORTS];
  112. +
  113. + int RESET_VAL;
  114. + reg RESET_REG;
  115. +
  116. +/* General flags */
  117. + reg ROUTER_CONTROL_REG;
  118. + reg VLAN_CONTROL_REG;
  119. + bitnum TAG_VLAN_BIT;
  120. + bitnum ROUTER_EN_BIT;
  121. + bitnum NUMLAN_GROUPS_MAX;
  122. + bitnum NUMLAN_GROUPS_BIT;
  123. +
  124. + reg MII_REGISTER_EN;
  125. + bitnum MII_REGISTER_EN_BIT;
  126. +
  127. + // set to 1 for 178C, 0 for 175C.
  128. + bitnum SIMPLE_VLAN_REGISTERS; // 175C has two vlans per register but 178C has only one.
  129. +};
  130. +
  131. +static const struct register_mappings IP178C = {
  132. + .NAME = "IP178C",
  133. + .MODEL_NO = 0x18,
  134. + .VLAN_DEFAULT_TAG_REG = {
  135. + {30,3},{30,4},{30,5},{30,6},{30,7},{30,8},
  136. + {30,9},{30,10},{30,11},
  137. + },
  138. +
  139. + .ADD_TAG_REG = {30,12},
  140. + .ADD_TAG_BIT = {0,1,2,3,4,5,6,7,8},
  141. + .REMOVE_TAG_REG = {30,13},
  142. + .REMOVE_TAG_BIT = {4,5,6,7,8,9,10,11,12},
  143. +
  144. + .SIMPLE_VLAN_REGISTERS = 1,
  145. +
  146. + .VLAN_LOOKUP_REG = {31,0},// +N
  147. + .VLAN_LOOKUP_REG_5 = NOTSUPPORTED, // not used with SIMPLE_VLAN_REGISTERS
  148. + .VLAN_LOOKUP_EVEN_BIT = {0,1,2,3,4,5,6,7,8},
  149. + .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,5,6,7,8},
  150. +
  151. + .TAG_VLAN_MASK_REG = {30,14}, // +N
  152. + .TAG_VLAN_MASK_EVEN_BIT = {0,1,2,3,4,5,6,7,8},
  153. + .TAG_VLAN_MASK_ODD_BIT = {0,1,2,3,4,5,6,7,8},
  154. +
  155. + .RESET_VAL = 0x55AA,
  156. + .RESET_REG = {30,0},
  157. +
  158. + .ROUTER_CONTROL_REG = {30,30},
  159. + .ROUTER_EN_BIT = 11,
  160. + .NUMLAN_GROUPS_MAX = 8,
  161. + .NUMLAN_GROUPS_BIT = 8, // {0-2}
  162. +
  163. + .VLAN_CONTROL_REG = {30,13},
  164. + .TAG_VLAN_BIT = 3,
  165. +
  166. + .CPU_PORT = 8,
  167. + .NUM_PORTS = 9,
  168. +
  169. + .MII_REGISTER_EN = NOTSUPPORTED,
  170. +
  171. +};
  172. +
  173. +static const struct register_mappings IP175C = {
  174. + .NAME = "IP175C",
  175. + .MODEL_NO = 0x18,
  176. + .VLAN_DEFAULT_TAG_REG = {
  177. + {29,24},{29,25},{29,26},{29,27},{29,28},{29,30},
  178. + NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED
  179. + },
  180. +
  181. + .ADD_TAG_REG = {29,23},
  182. + .REMOVE_TAG_REG = {29,23},
  183. + .ADD_TAG_BIT = {11,12,13,14,15,1,-1,-1,-1},
  184. + .REMOVE_TAG_BIT = {6,7,8,9,10,0,-1,-1,-1},
  185. +
  186. + .SIMPLE_VLAN_REGISTERS = 0,
  187. +
  188. + .VLAN_LOOKUP_REG = {29,19},// +N/2
  189. + .VLAN_LOOKUP_REG_5 = {30,18},
  190. + .VLAN_LOOKUP_EVEN_BIT = {8,9,10,11,12,15,-1,-1,-1},
  191. + .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,7,-1,-1,-1},
  192. +
  193. + .TAG_VLAN_MASK_REG = {30,1}, // +N/2
  194. + .TAG_VLAN_MASK_EVEN_BIT = {0,1,2,3,4,5,-1,-1,-1},
  195. + .TAG_VLAN_MASK_ODD_BIT = {8,9,10,11,12,13,-1,-1,-1},
  196. +
  197. + .RESET_VAL = 0x175C,
  198. + .RESET_REG = {30,0},
  199. +
  200. + .ROUTER_CONTROL_REG = {30,9},
  201. + .ROUTER_EN_BIT = 3,
  202. + .NUMLAN_GROUPS_MAX = 8,
  203. + .NUMLAN_GROUPS_BIT = 0, // {0-2}
  204. +
  205. + .VLAN_CONTROL_REG = {30,9},
  206. + .TAG_VLAN_BIT = 7,
  207. +
  208. + .NUM_PORTS = 6,
  209. + .CPU_PORT = 5,
  210. +
  211. + .MII_REGISTER_EN = NOTSUPPORTED,
  212. +
  213. +};
  214. +
  215. +static const struct register_mappings IP175A = {
  216. + .NAME = "IP175A",
  217. + .MODEL_NO = 0x05,
  218. + .VLAN_DEFAULT_TAG_REG = {
  219. + {0,24},{0,25},{0,26},{0,27},{0,28},NOTSUPPORTED,
  220. + NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED
  221. + },
  222. +
  223. + .ADD_TAG_REG = {0,23},
  224. + .REMOVE_TAG_REG = {0,23},
  225. + .ADD_TAG_BIT = {11,12,13,14,15,1,-1,-1,-1},
  226. + .REMOVE_TAG_BIT = {6,7,8,9,10,0,-1,-1,-1},
  227. +
  228. + .SIMPLE_VLAN_REGISTERS = 1,
  229. +
  230. + // Only programmable via. EEPROM
  231. + .VLAN_LOOKUP_REG = NOTSUPPORTED,// +N/2
  232. + .VLAN_LOOKUP_REG_5 = NOTSUPPORTED,
  233. + .VLAN_LOOKUP_EVEN_BIT = {8,9,10,11,12,15,-1,-1,-1},
  234. + .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,7,-1,-1,-1},
  235. +
  236. + .TAG_VLAN_MASK_REG = NOTSUPPORTED, // +N/2
  237. + .TAG_VLAN_MASK_EVEN_BIT = {0,1,2,3,4,5,-1,-1,-1},
  238. + .TAG_VLAN_MASK_ODD_BIT = {8,9,10,11,12,13,-1,-1,-1},
  239. +
  240. + .RESET_VAL = -1,
  241. + .RESET_REG = NOTSUPPORTED,
  242. +
  243. + .ROUTER_CONTROL_REG = NOTSUPPORTED,
  244. + .VLAN_CONTROL_REG = NOTSUPPORTED,
  245. + .TAG_VLAN_BIT = -1,
  246. + .ROUTER_EN_BIT = -1,
  247. + .NUMLAN_GROUPS_MAX = -1,
  248. + .NUMLAN_GROUPS_BIT = -1, // {0-2}
  249. +
  250. + .NUM_PORTS = 6,
  251. + .CPU_PORT = 5,
  252. +
  253. + .MII_REGISTER_EN = {0, 12},
  254. + .MII_REGISTER_EN_BIT = 7,
  255. +};
  256. +
  257. +struct ip175c_state {
  258. + struct list_head list;
  259. + struct mii_bus *mii_bus;
  260. + struct switch_dev *dev;
  261. +
  262. + int router_mode; // ROUTER_EN
  263. + int vlan_enabled; // TAG_VLAN_EN
  264. + struct port_state {
  265. + struct phy_device *phy;
  266. + unsigned int shareports;
  267. + u16 vlan_tag;
  268. + } ports[MAX_PORTS];
  269. + unsigned int add_tag;
  270. + unsigned int remove_tag;
  271. + int num_vlans;
  272. + unsigned int vlan_ports[MAX_VLANS];
  273. + const struct register_mappings *regs;
  274. + reg proc_mii; /*!< phy/reg for the low level register access via /proc */
  275. + int proc_errno; /*!< error code of the last read/write to "val" */
  276. +
  277. + char buf[80];
  278. +};
  279. +
  280. +static struct list_head state_list;
  281. +
  282. +static int getPhy (struct ip175c_state *state, reg mii)
  283. +{
  284. + struct mii_bus *bus = state->mii_bus;
  285. + int err;
  286. +
  287. + if (!REG_SUPP(mii))
  288. + return -EFAULT;
  289. + mutex_lock(&bus->mdio_lock);
  290. + err = bus->read(bus, mii.p, mii.m);
  291. + mutex_unlock(&bus->mdio_lock);
  292. + if (err < 0) {
  293. + state->proc_errno = err;
  294. + pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", mii.p,mii.m,-err);
  295. + return err;
  296. + }
  297. +
  298. + pr_debug("IP175C: Read MII register %d,%d -> %04x\n", mii.p, mii.m, err);
  299. + return err;
  300. +}
  301. +
  302. +static int setPhy (struct ip175c_state *state, reg mii, u16 value)
  303. +{
  304. + struct mii_bus *bus = state->mii_bus;
  305. + int err;
  306. +
  307. + if (!REG_SUPP(mii))
  308. + return -EFAULT;
  309. + mutex_lock(&bus->mdio_lock);
  310. + err = bus->write(bus, mii.p, mii.m, value);
  311. + mutex_unlock(&bus->mdio_lock);
  312. + if (err < 0) {
  313. + state->proc_errno = err;
  314. + pr_warning("IP175C: Unable to set MII register %d,%d to %d: error %d\n", mii.p,mii.m,value,-err);
  315. + return err;
  316. + }
  317. + mdelay(2);
  318. + getPhy(state, mii);
  319. + pr_debug("IP175C: Set MII register %d,%d to %04x\n", mii.p, mii.m, value);
  320. + return 0;
  321. +}
  322. +
  323. +/**
  324. + * These two macros are to simplify the mapping of logical bits to the bits in hardware.
  325. + * NOTE: these macros will return if there is an error!
  326. + */
  327. +
  328. +#define GET_PORT_BITS(state, bits, addr, bit_lookup) \
  329. + do { \
  330. + int i, val = getPhy((state), (addr)); \
  331. + if (val < 0) \
  332. + return val; \
  333. + (bits) = 0; \
  334. + for (i = 0; i < MAX_PORTS; i++) { \
  335. + if ((bit_lookup)[i] == -1) continue; \
  336. + if (val & (1<<(bit_lookup)[i])) \
  337. + (bits) |= (1<<i); \
  338. + } \
  339. + } while (0)
  340. +
  341. +#define SET_PORT_BITS(state, bits, addr, bit_lookup) \
  342. + do { \
  343. + int i, val = getPhy((state), (addr)); \
  344. + if (val < 0) \
  345. + return val; \
  346. + for (i = 0; i < MAX_PORTS; i++) { \
  347. + unsigned int newmask = ((bits)&(1<<i)); \
  348. + if ((bit_lookup)[i] == -1) continue; \
  349. + val &= ~(1<<(bit_lookup)[i]); \
  350. + val |= ((newmask>>i)<<(bit_lookup)[i]); \
  351. + } \
  352. + val = setPhy((state), (addr), val); \
  353. + if (val < 0) \
  354. + return val; \
  355. + } while (0)
  356. +
  357. +static int get_model(struct ip175c_state *state)
  358. +{
  359. + reg oui_id_reg = {0, 2};
  360. + int oui_id;
  361. + reg model_no_reg = {0, 3};
  362. + int model_no, model_no_orig;
  363. +
  364. + // 175 and 178 have the same oui ID.
  365. + reg oui_id_reg_178c = {5, 2}; // returns error on IP175C.
  366. + int is_178c = 0;
  367. +
  368. + oui_id = getPhy(state, oui_id_reg);
  369. + if (oui_id != 0x0243) {
  370. + // non
  371. + return -ENODEV; // Not a IC+ chip.
  372. + }
  373. + oui_id = getPhy(state, oui_id_reg_178c);
  374. + if (oui_id == 0x0243) {
  375. + is_178c = 1;
  376. + }
  377. +
  378. + model_no_orig = getPhy(state, model_no_reg);
  379. + if (model_no_orig < 0) {
  380. + return -ENODEV;
  381. + }
  382. + model_no = model_no_orig >> 4; // shift out revision number.
  383. + model_no &= 0x3f; // only take the model number (low 6 bits).
  384. + if (model_no == IP175A.MODEL_NO) {
  385. + state->regs = &IP175A;
  386. + } else if (model_no == IP175C.MODEL_NO) {
  387. + if (is_178c) {
  388. + state->regs = &IP178C;
  389. + } else {
  390. + state->regs = &IP175C;
  391. + }
  392. + } else {
  393. + printk(KERN_WARNING "ip175c: Found an unknown IC+ switch with model number %02Xh.\n", model_no_orig);
  394. + return -EPERM;
  395. + }
  396. + return 0;
  397. +}
  398. +
  399. +/** Get only the vlan and router flags on the router **/
  400. +static int get_flags(struct ip175c_state *state)
  401. +{
  402. + int val;
  403. +
  404. + state->router_mode = 0;
  405. + state->vlan_enabled = -1; // hack
  406. + state->num_vlans = 0;
  407. +
  408. + if (!REG_SUPP(state->regs->ROUTER_CONTROL_REG)) {
  409. + return 0; // not an error if it doesn't support enable vlan.
  410. + }
  411. +
  412. + val = getPhy(state, state->regs->ROUTER_CONTROL_REG);
  413. + if (val < 0) {
  414. + return val;
  415. + }
  416. + if (state->regs->ROUTER_EN_BIT >= 0)
  417. + state->router_mode = ((val>>state->regs->ROUTER_EN_BIT) & 1);
  418. +
  419. + if (state->regs->NUMLAN_GROUPS_BIT >= 0) {
  420. + state->num_vlans = (val >> state->regs->NUMLAN_GROUPS_BIT);
  421. + state->num_vlans &= (state->regs->NUMLAN_GROUPS_MAX-1);
  422. + state->num_vlans+=1; // does not include WAN.
  423. + }
  424. +
  425. +
  426. + val = getPhy(state, state->regs->VLAN_CONTROL_REG);
  427. + if (val < 0) {
  428. + return 0;
  429. + }
  430. + if (state->regs->TAG_VLAN_BIT >= 0)
  431. + state->vlan_enabled = ((val>>state->regs->TAG_VLAN_BIT) & 1);
  432. +
  433. + return 0;
  434. +}
  435. +/** Get all state variables for VLAN mappings and port-based tagging. **/
  436. +static int get_state(struct ip175c_state *state)
  437. +{
  438. + int i, j;
  439. + int ret;
  440. + ret = get_flags(state);
  441. + if (ret < 0) {
  442. + return ret;
  443. + }
  444. + GET_PORT_BITS(state, state->remove_tag,
  445. + state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
  446. + GET_PORT_BITS(state, state->add_tag,
  447. + state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
  448. +
  449. + if (state->vlan_enabled == -1) {
  450. + // not sure how to get this...
  451. + state->vlan_enabled = (!state->remove_tag && !state->add_tag);
  452. + }
  453. +
  454. + if (REG_SUPP(state->regs->VLAN_LOOKUP_REG)) {
  455. + for (j=0; j<MAX_PORTS; j++) {
  456. + state->ports[j].shareports = 0; // initialize them in case.
  457. + }
  458. + for (j=0; j<state->regs->NUM_PORTS; j++) {
  459. + reg addr;
  460. + const bitnum *bit_lookup = (j%2==0)?
  461. + state->regs->VLAN_LOOKUP_EVEN_BIT:
  462. + state->regs->VLAN_LOOKUP_ODD_BIT;
  463. + addr = state->regs->VLAN_LOOKUP_REG;
  464. + if (state->regs->SIMPLE_VLAN_REGISTERS) {
  465. + addr.m += j;
  466. + } else {
  467. + switch (j) {
  468. + case 0:
  469. + case 1:
  470. + break;
  471. + case 2:
  472. + case 3:
  473. + addr.m+=1;
  474. + break;
  475. + case 4:
  476. + addr.m+=2;
  477. + break;
  478. + case 5:
  479. + addr = state->regs->VLAN_LOOKUP_REG_5;
  480. + break;
  481. + }
  482. + }
  483. +
  484. + if (REG_SUPP(addr)) {
  485. + GET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
  486. + }
  487. + }
  488. + } else {
  489. + for (j=0; j<MAX_PORTS; j++) {
  490. + state->ports[j].shareports = 0xff;
  491. + }
  492. + }
  493. +
  494. + for (i=0; i<MAX_PORTS; i++) {
  495. + if (REG_SUPP(state->regs->VLAN_DEFAULT_TAG_REG[i])) {
  496. + int val = getPhy(state, state->regs->VLAN_DEFAULT_TAG_REG[i]);
  497. + if (val < 0) {
  498. + return val;
  499. + }
  500. + state->ports[i].vlan_tag = val;
  501. + } else {
  502. + state->ports[i].vlan_tag = 0;
  503. + }
  504. + }
  505. +
  506. + if (REG_SUPP(state->regs->TAG_VLAN_MASK_REG)) {
  507. + for (j=0; j<MAX_VLANS; j++) {
  508. + reg addr = state->regs->TAG_VLAN_MASK_REG;
  509. + const bitnum *bit_lookup = (j%2==0)?
  510. + state->regs->TAG_VLAN_MASK_EVEN_BIT:
  511. + state->regs->TAG_VLAN_MASK_ODD_BIT;
  512. + if (state->regs->SIMPLE_VLAN_REGISTERS) {
  513. + addr.m += j;
  514. + } else {
  515. + addr.m += j/2;
  516. + }
  517. + GET_PORT_BITS(state, state->vlan_ports[j], addr, bit_lookup);
  518. + }
  519. + } else {
  520. + for (j=0; j<MAX_VLANS; j++) {
  521. + state->vlan_ports[j] = 0;
  522. + for (i=0; i<state->regs->NUM_PORTS; i++) {
  523. + if ((state->ports[i].vlan_tag == j) ||
  524. + (state->ports[i].vlan_tag == 0)) {
  525. + state->vlan_ports[j] |= (1<<i);
  526. + }
  527. + }
  528. + }
  529. + }
  530. +
  531. + return 0;
  532. +}
  533. +
  534. +
  535. +/** Only update vlan and router flags in the switch **/
  536. +static int update_flags(struct ip175c_state *state)
  537. +{
  538. + int val;
  539. +
  540. + if (!REG_SUPP(state->regs->ROUTER_CONTROL_REG)) {
  541. + return 0;
  542. + }
  543. +
  544. + val = getPhy(state, state->regs->ROUTER_CONTROL_REG);
  545. + if (val < 0) {
  546. + return val;
  547. + }
  548. + if (state->regs->ROUTER_EN_BIT >= 0) {
  549. + if (state->router_mode) {
  550. + val |= (1<<state->regs->ROUTER_EN_BIT);
  551. + } else {
  552. + val &= (~(1<<state->regs->ROUTER_EN_BIT));
  553. + }
  554. + }
  555. + if (state->regs->TAG_VLAN_BIT >= 0) {
  556. + if (state->vlan_enabled) {
  557. + val |= (1<<state->regs->TAG_VLAN_BIT);
  558. + } else {
  559. + val &= (~(1<<state->regs->TAG_VLAN_BIT));
  560. + }
  561. + }
  562. + if (state->regs->NUMLAN_GROUPS_BIT >= 0) {
  563. + val &= (~((state->regs->NUMLAN_GROUPS_MAX-1)<<state->regs->NUMLAN_GROUPS_BIT));
  564. + if (state->num_vlans > state->regs->NUMLAN_GROUPS_MAX) {
  565. + val |= state->regs->NUMLAN_GROUPS_MAX << state->regs->NUMLAN_GROUPS_BIT;
  566. + } else if (state->num_vlans >= 1) {
  567. + val |= (state->num_vlans-1) << state->regs->NUMLAN_GROUPS_BIT;
  568. + }
  569. + }
  570. + return setPhy(state, state->regs->ROUTER_CONTROL_REG, val);
  571. +}
  572. +
  573. +/** Update all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
  574. +static int update_state(struct ip175c_state *state)
  575. +{
  576. + int j;
  577. + int i;
  578. + SET_PORT_BITS(state, state->add_tag,
  579. + state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
  580. + SET_PORT_BITS(state, state->remove_tag,
  581. + state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
  582. +
  583. + if (REG_SUPP(state->regs->VLAN_LOOKUP_REG)) {
  584. + for (j=0; j<state->regs->NUM_PORTS; j++) {
  585. + reg addr;
  586. + const bitnum *bit_lookup = (j%2==0)?
  587. + state->regs->VLAN_LOOKUP_EVEN_BIT:
  588. + state->regs->VLAN_LOOKUP_ODD_BIT;
  589. +
  590. + // duplicate code -- sorry
  591. + addr = state->regs->VLAN_LOOKUP_REG;
  592. + if (state->regs->SIMPLE_VLAN_REGISTERS) {
  593. + addr.m += j;
  594. + } else {
  595. + switch (j) {
  596. + case 0:
  597. + case 1:
  598. + break;
  599. + case 2:
  600. + case 3:
  601. + addr.m+=1;
  602. + break;
  603. + case 4:
  604. + addr.m+=2;
  605. + break;
  606. + case 5:
  607. + addr = state->regs->VLAN_LOOKUP_REG_5;
  608. + break;
  609. + default:
  610. + addr.m = -1; // shouldn't get here, but...
  611. + break;
  612. + }
  613. + }
  614. + //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
  615. + if (REG_SUPP(addr)) {
  616. + SET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
  617. + }
  618. + }
  619. + }
  620. + if (REG_SUPP(state->regs->TAG_VLAN_MASK_REG)) {
  621. + for (j=0; j<MAX_VLANS; j++) {
  622. + reg addr = state->regs->TAG_VLAN_MASK_REG;
  623. + const bitnum *bit_lookup = (j%2==0)?
  624. + state->regs->TAG_VLAN_MASK_EVEN_BIT:
  625. + state->regs->TAG_VLAN_MASK_ODD_BIT;
  626. + unsigned int vlan_mask;
  627. + if (state->regs->SIMPLE_VLAN_REGISTERS) {
  628. + addr.m += j;
  629. + } else {
  630. + addr.m += j/2;
  631. + }
  632. + vlan_mask = state->vlan_ports[j];
  633. + SET_PORT_BITS(state, vlan_mask, addr, bit_lookup);
  634. + }
  635. + }
  636. +
  637. + for (i=0; i<MAX_PORTS; i++) {
  638. + if (REG_SUPP(state->regs->VLAN_DEFAULT_TAG_REG[i])) {
  639. + int err = setPhy(state, state->regs->VLAN_DEFAULT_TAG_REG[i],
  640. + state->ports[i].vlan_tag);
  641. + if (err < 0) {
  642. + return err;
  643. + }
  644. + }
  645. + }
  646. +
  647. + return update_flags(state);
  648. +
  649. + // software reset: 30.0 = 0x175C
  650. + // wait 2ms
  651. + // reset ports 0,1,2,3,4
  652. +}
  653. +
  654. +/*
  655. + Uses only the VLAN port mask and the add tag mask to generate the other fields:
  656. + which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
  657. + */
  658. +static void correct_vlan_state(struct ip175c_state *state)
  659. +{
  660. + int i, j;
  661. + state->num_vlans = 0;
  662. + for (i=0; i<MAX_VLANS; i++) {
  663. + if (state->vlan_ports[i] != 0) {
  664. + state->num_vlans = i+1; //hack -- we need to store the "set" vlans somewhere...
  665. + }
  666. + }
  667. +
  668. + for (i=0; i<state->regs->NUM_PORTS; i++) {
  669. + int oldtag = state->ports[i].vlan_tag;
  670. + if (oldtag >= 0 && oldtag < MAX_VLANS) {
  671. + if (state->vlan_ports[oldtag] & (1<<i)) {
  672. + continue; // primary vlan is valid.
  673. + }
  674. + }
  675. + state->ports[i].vlan_tag = 0;
  676. + }
  677. +
  678. + for (i=0; i<state->regs->NUM_PORTS; i++) {
  679. + unsigned int portmask = (1<<i);
  680. + state->ports[i].shareports = portmask;
  681. + for (j=0; j<MAX_VLANS; j++) {
  682. + if (state->vlan_ports[j] & portmask) {
  683. + state->ports[i].shareports |= state->vlan_ports[j];
  684. + if (state->ports[i].vlan_tag == 0) {
  685. + state->ports[i].vlan_tag = j;
  686. + }
  687. + }
  688. + }
  689. + if (!state->vlan_enabled) {
  690. + // share with everybody!
  691. + state->ports[i].shareports = (1<<state->regs->NUM_PORTS)-1;
  692. + }
  693. + }
  694. + state->remove_tag = ((~state->add_tag) & ((1<<state->regs->NUM_PORTS)-1));
  695. +}
  696. +
  697. +static int ip175c_get_enable_vlan(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  698. +{
  699. + struct ip175c_state *state = dev->priv;
  700. + int err;
  701. +
  702. + err = get_state(state); // may be set in get_state.
  703. + if (err < 0)
  704. + return err;
  705. + val->value.i = state->vlan_enabled;
  706. + return 0;
  707. +}
  708. +
  709. +static int ip175c_set_enable_vlan(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  710. +{
  711. + struct ip175c_state *state = dev->priv;
  712. + int err;
  713. + int enable;
  714. + int i;
  715. +
  716. + err = get_state(state);
  717. + if (err < 0)
  718. + return err;
  719. + enable = val->value.i;
  720. + //printk(KERN_WARNING "set enable vlan to %d\n",enable);
  721. +
  722. + if (state->vlan_enabled == enable) {
  723. + // do not change any state.
  724. + return 0;
  725. + }
  726. + state->vlan_enabled = enable;
  727. +
  728. + // Otherwise, if we are switching state, set fields to a known default.
  729. + state->remove_tag = 0x0000;
  730. + state->add_tag = 0x0000;
  731. + for (i = 0; i < MAX_PORTS; i++) {
  732. + state->ports[i].vlan_tag = 0;
  733. + state->ports[i].shareports = 0xffff;
  734. + }
  735. + for (i = 0; i < MAX_VLANS; i++) {
  736. + state->vlan_ports[i] = 0x0;
  737. + }
  738. +
  739. + if (state->vlan_enabled) {
  740. + // updates other fields only based off vlan_ports and add_tag fields.
  741. + // Note that by default, no ports are in any vlans.
  742. + correct_vlan_state(state);
  743. + }
  744. + // ensure sane defaults?
  745. + return update_state(state);
  746. +}
  747. +
  748. +static int ip175c_get_ports(struct switch_dev *dev, struct switch_val *val)
  749. +{
  750. + struct ip175c_state *state = dev->priv;
  751. + int err;
  752. + int b;
  753. + int ind;
  754. + unsigned int ports;
  755. +
  756. + if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
  757. + return -EINVAL;
  758. +
  759. + err = get_state(state);
  760. + if (err<0)
  761. + return err;
  762. +
  763. + ports = state->vlan_ports[val->port_vlan];
  764. + b = 0;
  765. + ind = 0;
  766. + while (b < MAX_PORTS) {
  767. + if (ports&1) {
  768. + int istagged = ((state->add_tag >> b) & 1);
  769. + val->value.ports[ind].id = b;
  770. + val->value.ports[ind].flags = (istagged << SWITCH_PORT_FLAG_TAGGED);
  771. + ind++;
  772. + }
  773. + b++;
  774. + ports >>= 1;
  775. + }
  776. + val->len = ind;
  777. +
  778. + return 0;
  779. +}
  780. +
  781. +static int ip175c_set_ports(struct switch_dev *dev, struct switch_val *val)
  782. +{
  783. + struct ip175c_state *state = dev->priv;
  784. + int i;
  785. + int err;
  786. +
  787. + if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
  788. + return -EINVAL;
  789. +
  790. + err = get_state(state);
  791. + if (err < 0)
  792. + return err;
  793. +
  794. + state->vlan_ports[val->port_vlan] = 0;
  795. + for (i = 0; i < val->len; i++) {
  796. + int bitmask = (1<<val->value.ports[i].id);
  797. + state->vlan_ports[val->port_vlan] |= bitmask;
  798. + if (val->value.ports[i].flags & (1<<SWITCH_PORT_FLAG_TAGGED)) {
  799. + state->add_tag |= bitmask;
  800. + } else {
  801. + state->add_tag &= (~bitmask);
  802. + }
  803. + }
  804. + /*
  805. + // no primary vlan id support in swconfig?
  806. + // primary vlan will be set to the first non-zero vlan a port is a member of.
  807. + for (i = 0; i< state->regs->NUM_PORTS; i++) {
  808. + if (vlan_config->pvid & (1<<i)) {
  809. + state->ports[i].vlan_tag = nr;
  810. + }
  811. + }
  812. + */
  813. +
  814. + correct_vlan_state(state);
  815. + err = update_state(state);
  816. +
  817. + return err;
  818. +}
  819. +
  820. +static int ip175c_apply(struct switch_dev *dev)
  821. +{
  822. + struct ip175c_state *state = dev->priv;
  823. + int err;
  824. +
  825. + err = get_flags(state);
  826. + if (err < 0)
  827. + return err;
  828. +
  829. + if (REG_SUPP(state->regs->MII_REGISTER_EN)){
  830. + int val = getPhy(state, state->regs->MII_REGISTER_EN);
  831. + if (val < 0) {
  832. + return val;
  833. + }
  834. + val |= (1<<state->regs->MII_REGISTER_EN_BIT);
  835. + return setPhy(state, state->regs->MII_REGISTER_EN, val);
  836. + }
  837. + return 0;
  838. +}
  839. +
  840. +static int ip175c_reset(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  841. +{
  842. + struct ip175c_state *state = dev->priv;
  843. + int err;
  844. +
  845. + err = get_flags(state);
  846. + if (err < 0)
  847. + return err;
  848. +
  849. + if (REG_SUPP(state->regs->RESET_REG)) {
  850. + err = setPhy(state, state->regs->RESET_REG, state->regs->RESET_VAL);
  851. + if (err < 0)
  852. + return err;
  853. + err = getPhy(state, state->regs->RESET_REG);
  854. +
  855. + /* data sheet specifies reset period is 2 msec
  856. + (don't see any mention of the 2ms delay in the IP178C spec, only
  857. + in IP175C, but it can't hurt.) */
  858. + mdelay(2);
  859. + }
  860. + return 0;
  861. +}
  862. +
  863. +/*! get the current register number */
  864. +static int ip175c_get_tagged(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  865. +{
  866. + struct ip175c_state *state = dev->priv;
  867. + int err;
  868. +
  869. + err = get_state(state);
  870. + if (err < 0)
  871. + return err;
  872. +
  873. + if (state->add_tag & (1<<val->port_vlan)) {
  874. + if (state->remove_tag & (1<<val->port_vlan))
  875. + val->value.i = 3; // shouldn't ever happen.
  876. + else
  877. + val->value.i = 1;
  878. + } else {
  879. + if (state->remove_tag & (1<<val->port_vlan))
  880. + val->value.i = 0;
  881. + else
  882. + val->value.i = 2;
  883. + }
  884. + return 0;
  885. +}
  886. +
  887. +/*! set a new register address for low level access to registers */
  888. +static int ip175c_set_tagged(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  889. +{
  890. + struct ip175c_state *state = dev->priv;
  891. + int err;
  892. +
  893. + err = get_state(state);
  894. + if (err < 0)
  895. + return err;
  896. +
  897. + state->add_tag &= ~(1<<val->port_vlan);
  898. + state->remove_tag &= ~(1<<val->port_vlan);
  899. +
  900. + if (val->value.i == 0)
  901. + state->remove_tag |= (1<<val->port_vlan);
  902. + if (val->value.i == 1)
  903. + state->add_tag |= (1<<val->port_vlan);
  904. +
  905. + SET_PORT_BITS(state, state->add_tag,
  906. + state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
  907. + SET_PORT_BITS(state, state->remove_tag,
  908. + state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
  909. +
  910. + return err;
  911. +}
  912. +
  913. +
  914. +/* low level /proc procedures */
  915. +
  916. +/*! get the current phy address */
  917. +static int ip175c_get_phy(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  918. +{
  919. + struct ip175c_state *state = dev->priv;
  920. +
  921. + val->value.i = state->proc_mii.p;
  922. + return 0;
  923. +}
  924. +
  925. +/*! set a new phy address for low level access to registers */
  926. +static int ip175c_set_phy(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  927. +{
  928. + struct ip175c_state *state = dev->priv;
  929. + int new_reg = val->value.i;
  930. +
  931. + if (new_reg < 0 || new_reg > 31)
  932. + state->proc_mii.p = (u16)-1;
  933. + else
  934. + state->proc_mii.p = (u16)new_reg;
  935. + return 0;
  936. +}
  937. +
  938. +/*! get the current register number */
  939. +static int ip175c_get_reg(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  940. +{
  941. + struct ip175c_state *state = dev->priv;
  942. +
  943. + val->value.i = state->proc_mii.m;
  944. + return 0;
  945. +}
  946. +
  947. +/*! set a new register address for low level access to registers */
  948. +static int ip175c_set_reg(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  949. +{
  950. + struct ip175c_state *state = dev->priv;
  951. + int new_reg = val->value.i;
  952. +
  953. + if (new_reg < 0 || new_reg > 31)
  954. + state->proc_mii.m = (u16)-1;
  955. + else
  956. + state->proc_mii.m = (u16)new_reg;
  957. + return 0;
  958. +}
  959. +
  960. +/*! get the register content of state->proc_mii */
  961. +static int ip175c_get_val(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  962. +{
  963. + struct ip175c_state *state = dev->priv;
  964. + int retval = -EINVAL;
  965. + if (REG_SUPP(state->proc_mii))
  966. + retval = getPhy(state, state->proc_mii);
  967. +
  968. + if (retval < 0) {
  969. + state->proc_errno = retval;
  970. + return retval;
  971. + } else {
  972. + state->proc_errno = 0;
  973. + val->value.i = retval;
  974. + return 0;
  975. + }
  976. +}
  977. +
  978. +/*! write a value to the register defined by phy/reg above */
  979. +static int ip175c_set_val(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  980. +{
  981. + struct ip175c_state *state = dev->priv;
  982. + int myval;
  983. +
  984. + myval = val->value.i;
  985. + if (myval <= 0xffff && myval >= 0 && REG_SUPP(state->proc_mii)) {
  986. + state->proc_errno = setPhy(state, state->proc_mii, (u16)myval);
  987. + } else {
  988. + state->proc_errno = -EINVAL;
  989. + }
  990. + return state->proc_errno;
  991. +}
  992. +
  993. +/*! get the errno of the last read/write of "val" */
  994. +static int ip175c_get_errno(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  995. +{
  996. + struct ip175c_state *state = dev->priv;
  997. + val->value.i = state->proc_errno;
  998. + return 0;
  999. +}
  1000. +
  1001. +static int ip175c_read_name(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  1002. +{
  1003. + struct ip175c_state *state = dev->priv;
  1004. + val->value.s = state->regs->NAME; // just a const pointer, won't be freed by swconfig.
  1005. + return 0;
  1006. +}
  1007. +
  1008. +
  1009. +static int ip175c_set_port_speed(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  1010. +{
  1011. + int nr = val->port_vlan;
  1012. + struct ip175c_state *state = dev->priv;
  1013. + struct phy_device *phy;
  1014. + int ctrl;
  1015. + int autoneg;
  1016. + int speed;
  1017. + if (val->value.i == 100) {
  1018. + speed = 1;
  1019. + autoneg = 0;
  1020. + } else if (val->value.i == 10) {
  1021. + speed = 0;
  1022. + autoneg = 0;
  1023. + } else {
  1024. + autoneg = 1;
  1025. + speed = 1;
  1026. + }
  1027. +
  1028. + if (nr == state->regs->CPU_PORT) {
  1029. + return -EINVAL; // can't set speed for cpu port!
  1030. + }
  1031. +
  1032. + if (nr >= dev->ports || nr < 0)
  1033. + return -EINVAL;
  1034. + phy = state->ports[nr].phy;
  1035. + if (!phy)
  1036. + return -EINVAL;
  1037. +
  1038. + ctrl = phy_read(phy, 0);
  1039. + if (ctrl < 0)
  1040. + return -EIO;
  1041. +
  1042. + ctrl &= (~(1<<12));
  1043. + ctrl &= (~(1<<13));
  1044. + ctrl |= (autoneg<<12);
  1045. + ctrl |= (speed<<13);
  1046. +
  1047. + return phy_write(phy, 0, ctrl);
  1048. +}
  1049. +
  1050. +static int ip175c_get_port_speed(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  1051. +{
  1052. + int nr = val->port_vlan;
  1053. + struct ip175c_state *state = dev->priv;
  1054. + struct phy_device *phy;
  1055. + int speed, status;
  1056. +
  1057. + if (nr == state->regs->CPU_PORT) {
  1058. + val->value.i = 100;
  1059. + return 0;
  1060. + }
  1061. +
  1062. + if (nr >= dev->ports || nr < 0)
  1063. + return -EINVAL;
  1064. + phy = state->ports[nr].phy;
  1065. + if (!phy)
  1066. + return -EINVAL;
  1067. +
  1068. + status = phy_read(phy, 1);
  1069. + speed = phy_read(phy, 18);
  1070. + if (status < 0 || speed < 0)
  1071. + return -EIO;
  1072. +
  1073. + if (status & 4)
  1074. + val->value.i = ((speed & (1<<11)) ? 100 : 10);
  1075. + else
  1076. + val->value.i = 0;
  1077. +
  1078. + return 0;
  1079. +}
  1080. +
  1081. +
  1082. +static int ip175c_get_port_status(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  1083. +{
  1084. + int nr = val->port_vlan;
  1085. + struct ip175c_state *state = dev->priv;
  1086. + struct phy_device *phy;
  1087. + int ctrl, speed, status;
  1088. + int len;
  1089. + char *buf = state->buf; // fixed-length at 80.
  1090. +
  1091. + if (nr == state->regs->CPU_PORT) {
  1092. + sprintf(buf, "up, 100 Mbps, cpu port");
  1093. + val->value.s = buf;
  1094. + return 0;
  1095. + }
  1096. +
  1097. + if (nr >= dev->ports || nr < 0)
  1098. + return -EINVAL;
  1099. + phy = state->ports[nr].phy;
  1100. + if (!phy)
  1101. + return -EINVAL;
  1102. +
  1103. + ctrl = phy_read(phy, 0);
  1104. + status = phy_read(phy, 1);
  1105. + speed = phy_read(phy, 18);
  1106. + if (ctrl < 0 || status < 0 || speed < 0)
  1107. + return -EIO;
  1108. +
  1109. + if (status & 4)
  1110. + len = sprintf(buf, "up, %d Mbps, %s duplex",
  1111. + ((speed & (1<<11)) ? 100 : 10),
  1112. + ((speed & (1<<10)) ? "full" : "half"));
  1113. + else
  1114. + len = sprintf(buf, "down");
  1115. +
  1116. + if (ctrl & (1<<12)) {
  1117. + len += sprintf(buf+len, ", auto-negotiate");
  1118. + if (!(status & (1<<5)))
  1119. + len += sprintf(buf+len, " (in progress)");
  1120. + } else {
  1121. + len += sprintf(buf+len, ", fixed speed (%d)",
  1122. + ((ctrl & (1<<13)) ? 100 : 10));
  1123. + }
  1124. +
  1125. + buf[len] = '\0';
  1126. + val->value.s = buf;
  1127. + return 0;
  1128. +}
  1129. +
  1130. +enum Ports {
  1131. + IP175C_PORT_STATUS,
  1132. + IP175C_PORT_LINK,
  1133. + IP175C_PORT_TAGGED,
  1134. +};
  1135. +
  1136. +enum Globals {
  1137. + IP175C_RESET,
  1138. + IP175C_ENABLE_VLAN,
  1139. + IP175C_GET_NAME,
  1140. + IP175C_REGISTER_PHY,
  1141. + IP175C_REGISTER_MII,
  1142. + IP175C_REGISTER_VALUE,
  1143. + IP175C_REGISTER_ERRNO,
  1144. +};
  1145. +
  1146. +static const struct switch_attr ip175c_global[] = {
  1147. + [IP175C_RESET] = {
  1148. + .id = IP175C_RESET,
  1149. + .type = SWITCH_TYPE_NOVAL,
  1150. + .name = "reset",
  1151. + .get = NULL,
  1152. + .description = "Resets the switch but does not clear vlan configuration",
  1153. + .set = ip175c_reset,
  1154. + },
  1155. + [IP175C_ENABLE_VLAN] = {
  1156. + .id = IP175C_ENABLE_VLAN,
  1157. + .type = SWITCH_TYPE_INT,
  1158. + .name = "enable_vlan",
  1159. + .description = "Flag to enable or disable VLANs and tagging",
  1160. + .get = ip175c_get_enable_vlan,
  1161. + .set = ip175c_set_enable_vlan,
  1162. + },
  1163. + [IP175C_GET_NAME] = {
  1164. + .id = IP175C_GET_NAME,
  1165. + .type = SWITCH_TYPE_STRING,
  1166. + .description = "Returns the type of IC+ chip.",
  1167. + .name = "name",
  1168. + .get = ip175c_read_name,
  1169. + .set = NULL,
  1170. + },
  1171. + /* jal: added for low level debugging etc. */
  1172. + [IP175C_REGISTER_PHY] = {
  1173. + .id = IP175C_REGISTER_PHY,
  1174. + .type = SWITCH_TYPE_INT,
  1175. + .description = "Direct register access: set phy (0-4, or 29,30,31)",
  1176. + .name = "phy",
  1177. + .get = ip175c_get_phy,
  1178. + .set = ip175c_set_phy,
  1179. + },
  1180. + [IP175C_REGISTER_MII] = {
  1181. + .id = IP175C_REGISTER_MII,
  1182. + .type = SWITCH_TYPE_INT,
  1183. + .description = "Direct register access: set mii number (0-31)",
  1184. + .name = "reg",
  1185. + .get = ip175c_get_reg,
  1186. + .set = ip175c_set_reg,
  1187. + },
  1188. + [IP175C_REGISTER_VALUE] = {
  1189. + .id = IP175C_REGISTER_VALUE,
  1190. + .type = SWITCH_TYPE_INT,
  1191. + .description = "Direct register access: read/write to register (0-65535)",
  1192. + .name = "val",
  1193. + .get = ip175c_get_val,
  1194. + .set = ip175c_set_val,
  1195. + },
  1196. + [IP175C_REGISTER_ERRNO] = {
  1197. + .id = IP175C_REGISTER_ERRNO,
  1198. + .type = SWITCH_TYPE_INT,
  1199. + .description = "Direct register access: returns last read or write error",
  1200. + .name = "errno",
  1201. + .get = ip175c_get_errno,
  1202. + .set = NULL,
  1203. + },
  1204. +
  1205. +};
  1206. +
  1207. +static const struct switch_attr ip175c_vlan[] = {
  1208. +};
  1209. +
  1210. +static const struct switch_attr ip175c_port[] = {
  1211. + [IP175C_PORT_STATUS] = {
  1212. + .id = IP175C_PORT_STATUS,
  1213. + .type = SWITCH_TYPE_STRING,
  1214. + .description = "Returns Detailed port status",
  1215. + .name = "status",
  1216. + .get = ip175c_get_port_status,
  1217. + .set = NULL,
  1218. + },
  1219. + [IP175C_PORT_LINK] = {
  1220. + .id = IP175C_PORT_LINK,
  1221. + .type = SWITCH_TYPE_INT,
  1222. + .description = "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
  1223. + .name = "link",
  1224. + .get = ip175c_get_port_speed,
  1225. + .set = ip175c_set_port_speed,
  1226. + },
  1227. + [IP175C_PORT_TAGGED] = {
  1228. + .id = IP175C_PORT_LINK,
  1229. + .type = SWITCH_TYPE_INT,
  1230. + .description = "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
  1231. + .name = "tagged",
  1232. + .get = ip175c_get_tagged,
  1233. + .set = ip175c_set_tagged,
  1234. + },
  1235. +};
  1236. +
  1237. +static void ip175c_cleanup(struct ip175c_state *state)
  1238. +{
  1239. + int i;
  1240. +
  1241. + for (i=0; i<MAX_PORTS; i++)
  1242. + if (state->ports[i].phy)
  1243. + put_device(&state->ports[i].phy->dev);
  1244. + list_del(&state->list);
  1245. + kfree(state);
  1246. +}
  1247. +
  1248. +static struct ip175c_state *lookup_state(struct mii_bus *bus)
  1249. +{
  1250. + struct list_head *pos;
  1251. + struct ip175c_state *state;
  1252. + struct switch_dev *dev;
  1253. + int err;
  1254. +
  1255. + list_for_each(pos, &state_list) {
  1256. + state = list_entry(pos, struct ip175c_state, list);
  1257. + if (state->mii_bus == bus)
  1258. + return state;
  1259. + }
  1260. +
  1261. + dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
  1262. + if (!dev)
  1263. + return NULL;
  1264. + dev->name = DRIVER_NAME;
  1265. +
  1266. + // FIXME: passing const pointers gives warning with swconfig
  1267. + dev->attr_global.attr = &ip175c_global;
  1268. + dev->attr_global.n_attr = ARRAY_SIZE(ip175c_global);
  1269. + dev->attr_port.attr = &ip175c_port;
  1270. + dev->attr_port.n_attr = ARRAY_SIZE(ip175c_port);
  1271. + dev->attr_vlan.attr = &ip175c_vlan;
  1272. + dev->attr_vlan.n_attr = ARRAY_SIZE(ip175c_vlan);
  1273. +
  1274. + dev->get_vlan_ports = ip175c_get_ports;
  1275. + dev->set_vlan_ports = ip175c_set_ports;
  1276. + dev->apply_config = ip175c_apply;
  1277. +
  1278. + state = kcalloc(1, sizeof(*state), GFP_KERNEL);
  1279. + if (!state)
  1280. + goto bad2;
  1281. + list_add_tail(&state->list, &state_list);
  1282. + state->mii_bus = bus;
  1283. + state->dev = dev;
  1284. + dev->priv = state;
  1285. + dev->devname = bus->id;
  1286. +
  1287. + if ((err = get_model(state)))
  1288. + goto bad;
  1289. +
  1290. + dev->vlans = MAX_VLANS;
  1291. + dev->cpu_port = state->regs->CPU_PORT;
  1292. + dev->ports = state->regs->NUM_PORTS;
  1293. +
  1294. + printk(KERN_INFO MODULE_NAME ": Found a %s chip at %s, id %s\n", state->regs->NAME, bus->name, bus->id);
  1295. +
  1296. + if (register_switch(dev, NULL) < 0)
  1297. + goto bad;
  1298. +
  1299. + return state;
  1300. +
  1301. +bad:
  1302. + ip175c_cleanup(state);
  1303. +bad2:
  1304. + kfree(dev);
  1305. + return NULL;
  1306. +}
  1307. +
  1308. +#define PRINT_FOR_EACH_PHY // slightly increases loading times but could be useful.
  1309. +static int __init ip175c_init_dev(struct device *kdev, void *data)
  1310. +{
  1311. + struct ip175c_state *state;
  1312. + struct phy_device *phy;
  1313. +
  1314. + pr_debug("IP175C: Found PHY: bus_id=%s\n", kdev->bus_id);
  1315. + phy = to_phy_device(kdev);
  1316. + if (phy == NULL)
  1317. + return 0; // bad ip175c device.
  1318. + state = lookup_state(phy->bus);
  1319. + if (state) {
  1320. +#ifdef PRINT_FOR_EACH_PHY
  1321. + printk(KERN_DEBUG MODULE_NAME ": Found %s switch at bus id %s (PHY %d)\n", state->regs->NAME, kdev->bus_id, phy->addr);
  1322. +#endif
  1323. + if (phy->addr < state->regs->NUM_PORTS) {
  1324. + state->ports[phy->addr].phy = phy;
  1325. + get_device(kdev);
  1326. + }
  1327. + }
  1328. + return 0;
  1329. +}
  1330. +
  1331. +int __init ip175c_init(void)
  1332. +{
  1333. + struct device_driver *drv = driver_find("ICPlus IP175C", &mdio_bus_type);
  1334. + if (!drv) {
  1335. + pr_info("IP175C: No ICPlus driver found.\n");
  1336. + return -ENOENT;
  1337. + }
  1338. + INIT_LIST_HEAD(&state_list);
  1339. + driver_for_each_device(drv, NULL, NULL, ip175c_init_dev);
  1340. + put_driver(drv);
  1341. + return 0;
  1342. +}
  1343. +
  1344. +void __exit ip175c_exit(void)
  1345. +{
  1346. + struct list_head *pos;
  1347. + struct list_head *temp;
  1348. + struct ip175c_state *state;
  1349. +
  1350. + list_for_each_safe(pos, temp, &state_list) {
  1351. + state = list_entry(pos, struct ip175c_state, list);
  1352. + unregister_switch(state->dev);
  1353. + state->dev = NULL;
  1354. + ip175c_cleanup(state); // removes from list and frees.
  1355. + }
  1356. +}
  1357. +
  1358. +MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
  1359. +MODULE_LICENSE("GPL");
  1360. +
  1361. +module_init(ip175c_init);
  1362. +module_exit(ip175c_exit);
  1363. +