dscc4.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. /*
  2. * drivers/net/wan/dscc4/dscc4.c: a DSCC4 HDLC driver for Linux
  3. *
  4. * This software may be used and distributed according to the terms of the
  5. * GNU General Public License.
  6. *
  7. * The author may be reached as romieu@cogenit.fr.
  8. * Specific bug reports/asian food will be welcome.
  9. *
  10. * Special thanks to the nice people at CS-Telecom for the hardware and the
  11. * access to the test/measure tools.
  12. *
  13. *
  14. * Theory of Operation
  15. *
  16. * I. Board Compatibility
  17. *
  18. * This device driver is designed for the Siemens PEB20534 4 ports serial
  19. * controller as found on Etinc PCISYNC cards. The documentation for the
  20. * chipset is available at http://www.infineon.com:
  21. * - Data Sheet "DSCC4, DMA Supported Serial Communication Controller with
  22. * 4 Channels, PEB 20534 Version 2.1, PEF 20534 Version 2.1";
  23. * - Application Hint "Management of DSCC4 on-chip FIFO resources".
  24. * - Errata sheet DS5 (courtesy of Michael Skerritt).
  25. * Jens David has built an adapter based on the same chipset. Take a look
  26. * at http://www.afthd.tu-darmstadt.de/~dg1kjd/pciscc4 for a specific
  27. * driver.
  28. * Sample code (2 revisions) is available at Infineon.
  29. *
  30. * II. Board-specific settings
  31. *
  32. * Pcisync can transmit some clock signal to the outside world on the
  33. * *first two* ports provided you put a quartz and a line driver on it and
  34. * remove the jumpers. The operation is described on Etinc web site. If you
  35. * go DCE on these ports, don't forget to use an adequate cable.
  36. *
  37. * Sharing of the PCI interrupt line for this board is possible.
  38. *
  39. * III. Driver operation
  40. *
  41. * The rx/tx operations are based on a linked list of descriptors. The driver
  42. * doesn't use HOLD mode any more. HOLD mode is definitely buggy and the more
  43. * I tried to fix it, the more it started to look like (convoluted) software
  44. * mutation of LxDA method. Errata sheet DS5 suggests to use LxDA: consider
  45. * this a rfc2119 MUST.
  46. *
  47. * Tx direction
  48. * When the tx ring is full, the xmit routine issues a call to netdev_stop.
  49. * The device is supposed to be enabled again during an ALLS irq (we could
  50. * use HI but as it's easy to lose events, it's fscked).
  51. *
  52. * Rx direction
  53. * The received frames aren't supposed to span over multiple receiving areas.
  54. * I may implement it some day but it isn't the highest ranked item.
  55. *
  56. * IV. Notes
  57. * The current error (XDU, RFO) recovery code is untested.
  58. * So far, RDO takes his RX channel down and the right sequence to enable it
  59. * again is still a mistery. If RDO happens, plan a reboot. More details
  60. * in the code (NB: as this happens, TX still works).
  61. * Don't mess the cables during operation, especially on DTE ports. I don't
  62. * suggest it for DCE either but at least one can get some messages instead
  63. * of a complete instant freeze.
  64. * Tests are done on Rev. 20 of the silicium. The RDO handling changes with
  65. * the documentation/chipset releases.
  66. *
  67. * TODO:
  68. * - test X25.
  69. * - use polling at high irq/s,
  70. * - performance analysis,
  71. * - endianness.
  72. *
  73. * 2001/12/10 Daniela Squassoni <daniela@cyclades.com>
  74. * - Contribution to support the new generic HDLC layer.
  75. *
  76. * 2002/01 Ueimor
  77. * - old style interface removal
  78. * - dscc4_release_ring fix (related to DMA mapping)
  79. * - hard_start_xmit fix (hint: TxSizeMax)
  80. * - misc crapectomy.
  81. */
  82. #include <linux/module.h>
  83. #include <linux/types.h>
  84. #include <linux/errno.h>
  85. #include <linux/list.h>
  86. #include <linux/ioport.h>
  87. #include <linux/pci.h>
  88. #include <linux/kernel.h>
  89. #include <linux/mm.h>
  90. #include <asm/system.h>
  91. #include <asm/cache.h>
  92. #include <asm/byteorder.h>
  93. #include <asm/uaccess.h>
  94. #include <asm/io.h>
  95. #include <asm/irq.h>
  96. #include <linux/init.h>
  97. #include <linux/string.h>
  98. #include <linux/if_arp.h>
  99. #include <linux/netdevice.h>
  100. #include <linux/skbuff.h>
  101. #include <linux/delay.h>
  102. #include <linux/hdlc.h>
  103. #include <linux/mutex.h>
  104. /* Version */
  105. static const char version[] = "$Id: dscc4.c,v 1.173 2003/09/20 23:55:34 romieu Exp $ for Linux\n";
  106. static int debug;
  107. static int quartz;
  108. #ifdef CONFIG_DSCC4_PCI_RST
  109. static DEFINE_MUTEX(dscc4_mutex);
  110. static u32 dscc4_pci_config_store[16];
  111. #endif
  112. #define DRV_NAME "dscc4"
  113. #undef DSCC4_POLLING
  114. /* Module parameters */
  115. MODULE_AUTHOR("Maintainer: Francois Romieu <romieu@cogenit.fr>");
  116. MODULE_DESCRIPTION("Siemens PEB20534 PCI Controler");
  117. MODULE_LICENSE("GPL");
  118. module_param(debug, int, 0);
  119. MODULE_PARM_DESC(debug,"Enable/disable extra messages");
  120. module_param(quartz, int, 0);
  121. MODULE_PARM_DESC(quartz,"If present, on-board quartz frequency (Hz)");
  122. /* Structures */
  123. struct thingie {
  124. int define;
  125. u32 bits;
  126. };
  127. struct TxFD {
  128. __le32 state;
  129. __le32 next;
  130. __le32 data;
  131. __le32 complete;
  132. u32 jiffies; /* Allows sizeof(TxFD) == sizeof(RxFD) + extra hack */
  133. /* FWIW, datasheet calls that "dummy" and says that card
  134. * never looks at it; neither does the driver */
  135. };
  136. struct RxFD {
  137. __le32 state1;
  138. __le32 next;
  139. __le32 data;
  140. __le32 state2;
  141. __le32 end;
  142. };
  143. #define DUMMY_SKB_SIZE 64
  144. #define TX_LOW 8
  145. #define TX_RING_SIZE 32
  146. #define RX_RING_SIZE 32
  147. #define TX_TOTAL_SIZE TX_RING_SIZE*sizeof(struct TxFD)
  148. #define RX_TOTAL_SIZE RX_RING_SIZE*sizeof(struct RxFD)
  149. #define IRQ_RING_SIZE 64 /* Keep it a multiple of 32 */
  150. #define TX_TIMEOUT (HZ/10)
  151. #define DSCC4_HZ_MAX 33000000
  152. #define BRR_DIVIDER_MAX 64*0x00004000 /* Cf errata DS5 p.10 */
  153. #define dev_per_card 4
  154. #define SCC_REGISTERS_MAX 23 /* Cf errata DS5 p.4 */
  155. #define SOURCE_ID(flags) (((flags) >> 28) & 0x03)
  156. #define TO_SIZE(state) (((state) >> 16) & 0x1fff)
  157. /*
  158. * Given the operating range of Linux HDLC, the 2 defines below could be
  159. * made simpler. However they are a fine reminder for the limitations of
  160. * the driver: it's better to stay < TxSizeMax and < RxSizeMax.
  161. */
  162. #define TO_STATE_TX(len) cpu_to_le32(((len) & TxSizeMax) << 16)
  163. #define TO_STATE_RX(len) cpu_to_le32((RX_MAX(len) % RxSizeMax) << 16)
  164. #define RX_MAX(len) ((((len) >> 5) + 1) << 5) /* Cf RLCR */
  165. #define SCC_REG_START(dpriv) (SCC_START+(dpriv->dev_id)*SCC_OFFSET)
  166. struct dscc4_pci_priv {
  167. __le32 *iqcfg;
  168. int cfg_cur;
  169. spinlock_t lock;
  170. struct pci_dev *pdev;
  171. struct dscc4_dev_priv *root;
  172. dma_addr_t iqcfg_dma;
  173. u32 xtal_hz;
  174. };
  175. struct dscc4_dev_priv {
  176. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  177. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  178. struct RxFD *rx_fd;
  179. struct TxFD *tx_fd;
  180. __le32 *iqrx;
  181. __le32 *iqtx;
  182. /* FIXME: check all the volatile are required */
  183. volatile u32 tx_current;
  184. u32 rx_current;
  185. u32 iqtx_current;
  186. u32 iqrx_current;
  187. volatile u32 tx_dirty;
  188. volatile u32 ltda;
  189. u32 rx_dirty;
  190. u32 lrda;
  191. dma_addr_t tx_fd_dma;
  192. dma_addr_t rx_fd_dma;
  193. dma_addr_t iqtx_dma;
  194. dma_addr_t iqrx_dma;
  195. u32 scc_regs[SCC_REGISTERS_MAX]; /* Cf errata DS5 p.4 */
  196. struct timer_list timer;
  197. struct dscc4_pci_priv *pci_priv;
  198. spinlock_t lock;
  199. int dev_id;
  200. volatile u32 flags;
  201. u32 timer_help;
  202. unsigned short encoding;
  203. unsigned short parity;
  204. struct net_device *dev;
  205. sync_serial_settings settings;
  206. void __iomem *base_addr;
  207. u32 __pad __attribute__ ((aligned (4)));
  208. };
  209. /* GLOBAL registers definitions */
  210. #define GCMDR 0x00
  211. #define GSTAR 0x04
  212. #define GMODE 0x08
  213. #define IQLENR0 0x0C
  214. #define IQLENR1 0x10
  215. #define IQRX0 0x14
  216. #define IQTX0 0x24
  217. #define IQCFG 0x3c
  218. #define FIFOCR1 0x44
  219. #define FIFOCR2 0x48
  220. #define FIFOCR3 0x4c
  221. #define FIFOCR4 0x34
  222. #define CH0CFG 0x50
  223. #define CH0BRDA 0x54
  224. #define CH0BTDA 0x58
  225. #define CH0FRDA 0x98
  226. #define CH0FTDA 0xb0
  227. #define CH0LRDA 0xc8
  228. #define CH0LTDA 0xe0
  229. /* SCC registers definitions */
  230. #define SCC_START 0x0100
  231. #define SCC_OFFSET 0x80
  232. #define CMDR 0x00
  233. #define STAR 0x04
  234. #define CCR0 0x08
  235. #define CCR1 0x0c
  236. #define CCR2 0x10
  237. #define BRR 0x2C
  238. #define RLCR 0x40
  239. #define IMR 0x54
  240. #define ISR 0x58
  241. #define GPDIR 0x0400
  242. #define GPDATA 0x0404
  243. #define GPIM 0x0408
  244. /* Bit masks */
  245. #define EncodingMask 0x00700000
  246. #define CrcMask 0x00000003
  247. #define IntRxScc0 0x10000000
  248. #define IntTxScc0 0x01000000
  249. #define TxPollCmd 0x00000400
  250. #define RxActivate 0x08000000
  251. #define MTFi 0x04000000
  252. #define Rdr 0x00400000
  253. #define Rdt 0x00200000
  254. #define Idr 0x00100000
  255. #define Idt 0x00080000
  256. #define TxSccRes 0x01000000
  257. #define RxSccRes 0x00010000
  258. #define TxSizeMax 0x1fff /* Datasheet DS1 - 11.1.1.1 */
  259. #define RxSizeMax 0x1ffc /* Datasheet DS1 - 11.1.2.1 */
  260. #define Ccr0ClockMask 0x0000003f
  261. #define Ccr1LoopMask 0x00000200
  262. #define IsrMask 0x000fffff
  263. #define BrrExpMask 0x00000f00
  264. #define BrrMultMask 0x0000003f
  265. #define EncodingMask 0x00700000
  266. #define Hold cpu_to_le32(0x40000000)
  267. #define SccBusy 0x10000000
  268. #define PowerUp 0x80000000
  269. #define Vis 0x00001000
  270. #define FrameOk (FrameVfr | FrameCrc)
  271. #define FrameVfr 0x80
  272. #define FrameRdo 0x40
  273. #define FrameCrc 0x20
  274. #define FrameRab 0x10
  275. #define FrameAborted cpu_to_le32(0x00000200)
  276. #define FrameEnd cpu_to_le32(0x80000000)
  277. #define DataComplete cpu_to_le32(0x40000000)
  278. #define LengthCheck 0x00008000
  279. #define SccEvt 0x02000000
  280. #define NoAck 0x00000200
  281. #define Action 0x00000001
  282. #define HiDesc cpu_to_le32(0x20000000)
  283. /* SCC events */
  284. #define RxEvt 0xf0000000
  285. #define TxEvt 0x0f000000
  286. #define Alls 0x00040000
  287. #define Xdu 0x00010000
  288. #define Cts 0x00004000
  289. #define Xmr 0x00002000
  290. #define Xpr 0x00001000
  291. #define Rdo 0x00000080
  292. #define Rfs 0x00000040
  293. #define Cd 0x00000004
  294. #define Rfo 0x00000002
  295. #define Flex 0x00000001
  296. /* DMA core events */
  297. #define Cfg 0x00200000
  298. #define Hi 0x00040000
  299. #define Fi 0x00020000
  300. #define Err 0x00010000
  301. #define Arf 0x00000002
  302. #define ArAck 0x00000001
  303. /* State flags */
  304. #define Ready 0x00000000
  305. #define NeedIDR 0x00000001
  306. #define NeedIDT 0x00000002
  307. #define RdoSet 0x00000004
  308. #define FakeReset 0x00000008
  309. /* Don't mask RDO. Ever. */
  310. #ifdef DSCC4_POLLING
  311. #define EventsMask 0xfffeef7f
  312. #else
  313. #define EventsMask 0xfffa8f7a
  314. #endif
  315. /* Functions prototypes */
  316. static void dscc4_rx_irq(struct dscc4_pci_priv *, struct dscc4_dev_priv *);
  317. static void dscc4_tx_irq(struct dscc4_pci_priv *, struct dscc4_dev_priv *);
  318. static int dscc4_found1(struct pci_dev *, void __iomem *ioaddr);
  319. static int dscc4_init_one(struct pci_dev *, const struct pci_device_id *ent);
  320. static int dscc4_open(struct net_device *);
  321. static int dscc4_start_xmit(struct sk_buff *, struct net_device *);
  322. static int dscc4_close(struct net_device *);
  323. static int dscc4_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  324. static int dscc4_init_ring(struct net_device *);
  325. static void dscc4_release_ring(struct dscc4_dev_priv *);
  326. static void dscc4_timer(unsigned long);
  327. static void dscc4_tx_timeout(struct net_device *);
  328. static irqreturn_t dscc4_irq(int irq, void *dev_id);
  329. static int dscc4_hdlc_attach(struct net_device *, unsigned short, unsigned short);
  330. static int dscc4_set_iface(struct dscc4_dev_priv *, struct net_device *);
  331. #ifdef DSCC4_POLLING
  332. static int dscc4_tx_poll(struct dscc4_dev_priv *, struct net_device *);
  333. #endif
  334. static inline struct dscc4_dev_priv *dscc4_priv(struct net_device *dev)
  335. {
  336. return dev_to_hdlc(dev)->priv;
  337. }
  338. static inline struct net_device *dscc4_to_dev(struct dscc4_dev_priv *p)
  339. {
  340. return p->dev;
  341. }
  342. static void scc_patchl(u32 mask, u32 value, struct dscc4_dev_priv *dpriv,
  343. struct net_device *dev, int offset)
  344. {
  345. u32 state;
  346. /* Cf scc_writel for concern regarding thread-safety */
  347. state = dpriv->scc_regs[offset >> 2];
  348. state &= ~mask;
  349. state |= value;
  350. dpriv->scc_regs[offset >> 2] = state;
  351. writel(state, dpriv->base_addr + SCC_REG_START(dpriv) + offset);
  352. }
  353. static void scc_writel(u32 bits, struct dscc4_dev_priv *dpriv,
  354. struct net_device *dev, int offset)
  355. {
  356. /*
  357. * Thread-UNsafe.
  358. * As of 2002/02/16, there are no thread racing for access.
  359. */
  360. dpriv->scc_regs[offset >> 2] = bits;
  361. writel(bits, dpriv->base_addr + SCC_REG_START(dpriv) + offset);
  362. }
  363. static inline u32 scc_readl(struct dscc4_dev_priv *dpriv, int offset)
  364. {
  365. return dpriv->scc_regs[offset >> 2];
  366. }
  367. static u32 scc_readl_star(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  368. {
  369. /* Cf errata DS5 p.4 */
  370. readl(dpriv->base_addr + SCC_REG_START(dpriv) + STAR);
  371. return readl(dpriv->base_addr + SCC_REG_START(dpriv) + STAR);
  372. }
  373. static inline void dscc4_do_tx(struct dscc4_dev_priv *dpriv,
  374. struct net_device *dev)
  375. {
  376. dpriv->ltda = dpriv->tx_fd_dma +
  377. ((dpriv->tx_current-1)%TX_RING_SIZE)*sizeof(struct TxFD);
  378. writel(dpriv->ltda, dpriv->base_addr + CH0LTDA + dpriv->dev_id*4);
  379. /* Flush posted writes *NOW* */
  380. readl(dpriv->base_addr + CH0LTDA + dpriv->dev_id*4);
  381. }
  382. static inline void dscc4_rx_update(struct dscc4_dev_priv *dpriv,
  383. struct net_device *dev)
  384. {
  385. dpriv->lrda = dpriv->rx_fd_dma +
  386. ((dpriv->rx_dirty - 1)%RX_RING_SIZE)*sizeof(struct RxFD);
  387. writel(dpriv->lrda, dpriv->base_addr + CH0LRDA + dpriv->dev_id*4);
  388. }
  389. static inline unsigned int dscc4_tx_done(struct dscc4_dev_priv *dpriv)
  390. {
  391. return dpriv->tx_current == dpriv->tx_dirty;
  392. }
  393. static inline unsigned int dscc4_tx_quiescent(struct dscc4_dev_priv *dpriv,
  394. struct net_device *dev)
  395. {
  396. return readl(dpriv->base_addr + CH0FTDA + dpriv->dev_id*4) == dpriv->ltda;
  397. }
  398. static int state_check(u32 state, struct dscc4_dev_priv *dpriv,
  399. struct net_device *dev, const char *msg)
  400. {
  401. int ret = 0;
  402. if (debug > 1) {
  403. if (SOURCE_ID(state) != dpriv->dev_id) {
  404. printk(KERN_DEBUG "%s (%s): Source Id=%d, state=%08x\n",
  405. dev->name, msg, SOURCE_ID(state), state );
  406. ret = -1;
  407. }
  408. if (state & 0x0df80c00) {
  409. printk(KERN_DEBUG "%s (%s): state=%08x (UFO alert)\n",
  410. dev->name, msg, state);
  411. ret = -1;
  412. }
  413. }
  414. return ret;
  415. }
  416. static void dscc4_tx_print(struct net_device *dev,
  417. struct dscc4_dev_priv *dpriv,
  418. char *msg)
  419. {
  420. printk(KERN_DEBUG "%s: tx_current=%02d tx_dirty=%02d (%s)\n",
  421. dev->name, dpriv->tx_current, dpriv->tx_dirty, msg);
  422. }
  423. static void dscc4_release_ring(struct dscc4_dev_priv *dpriv)
  424. {
  425. struct pci_dev *pdev = dpriv->pci_priv->pdev;
  426. struct TxFD *tx_fd = dpriv->tx_fd;
  427. struct RxFD *rx_fd = dpriv->rx_fd;
  428. struct sk_buff **skbuff;
  429. int i;
  430. pci_free_consistent(pdev, TX_TOTAL_SIZE, tx_fd, dpriv->tx_fd_dma);
  431. pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
  432. skbuff = dpriv->tx_skbuff;
  433. for (i = 0; i < TX_RING_SIZE; i++) {
  434. if (*skbuff) {
  435. pci_unmap_single(pdev, le32_to_cpu(tx_fd->data),
  436. (*skbuff)->len, PCI_DMA_TODEVICE);
  437. dev_kfree_skb(*skbuff);
  438. }
  439. skbuff++;
  440. tx_fd++;
  441. }
  442. skbuff = dpriv->rx_skbuff;
  443. for (i = 0; i < RX_RING_SIZE; i++) {
  444. if (*skbuff) {
  445. pci_unmap_single(pdev, le32_to_cpu(rx_fd->data),
  446. RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
  447. dev_kfree_skb(*skbuff);
  448. }
  449. skbuff++;
  450. rx_fd++;
  451. }
  452. }
  453. static inline int try_get_rx_skb(struct dscc4_dev_priv *dpriv,
  454. struct net_device *dev)
  455. {
  456. unsigned int dirty = dpriv->rx_dirty%RX_RING_SIZE;
  457. struct RxFD *rx_fd = dpriv->rx_fd + dirty;
  458. const int len = RX_MAX(HDLC_MAX_MRU);
  459. struct sk_buff *skb;
  460. int ret = 0;
  461. skb = dev_alloc_skb(len);
  462. dpriv->rx_skbuff[dirty] = skb;
  463. if (skb) {
  464. skb->protocol = hdlc_type_trans(skb, dev);
  465. rx_fd->data = cpu_to_le32(pci_map_single(dpriv->pci_priv->pdev,
  466. skb->data, len, PCI_DMA_FROMDEVICE));
  467. } else {
  468. rx_fd->data = 0;
  469. ret = -1;
  470. }
  471. return ret;
  472. }
  473. /*
  474. * IRQ/thread/whatever safe
  475. */
  476. static int dscc4_wait_ack_cec(struct dscc4_dev_priv *dpriv,
  477. struct net_device *dev, char *msg)
  478. {
  479. s8 i = 0;
  480. do {
  481. if (!(scc_readl_star(dpriv, dev) & SccBusy)) {
  482. printk(KERN_DEBUG "%s: %s ack (%d try)\n", dev->name,
  483. msg, i);
  484. goto done;
  485. }
  486. schedule_timeout_uninterruptible(10);
  487. rmb();
  488. } while (++i > 0);
  489. printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
  490. done:
  491. return (i >= 0) ? i : -EAGAIN;
  492. }
  493. static int dscc4_do_action(struct net_device *dev, char *msg)
  494. {
  495. void __iomem *ioaddr = dscc4_priv(dev)->base_addr;
  496. s16 i = 0;
  497. writel(Action, ioaddr + GCMDR);
  498. ioaddr += GSTAR;
  499. do {
  500. u32 state = readl(ioaddr);
  501. if (state & ArAck) {
  502. printk(KERN_DEBUG "%s: %s ack\n", dev->name, msg);
  503. writel(ArAck, ioaddr);
  504. goto done;
  505. } else if (state & Arf) {
  506. printk(KERN_ERR "%s: %s failed\n", dev->name, msg);
  507. writel(Arf, ioaddr);
  508. i = -1;
  509. goto done;
  510. }
  511. rmb();
  512. } while (++i > 0);
  513. printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
  514. done:
  515. return i;
  516. }
  517. static inline int dscc4_xpr_ack(struct dscc4_dev_priv *dpriv)
  518. {
  519. int cur = dpriv->iqtx_current%IRQ_RING_SIZE;
  520. s8 i = 0;
  521. do {
  522. if (!(dpriv->flags & (NeedIDR | NeedIDT)) ||
  523. (dpriv->iqtx[cur] & cpu_to_le32(Xpr)))
  524. break;
  525. smp_rmb();
  526. schedule_timeout_uninterruptible(10);
  527. } while (++i > 0);
  528. return (i >= 0 ) ? i : -EAGAIN;
  529. }
  530. #if 0 /* dscc4_{rx/tx}_reset are both unreliable - more tweak needed */
  531. static void dscc4_rx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  532. {
  533. unsigned long flags;
  534. spin_lock_irqsave(&dpriv->pci_priv->lock, flags);
  535. /* Cf errata DS5 p.6 */
  536. writel(0x00000000, dpriv->base_addr + CH0LRDA + dpriv->dev_id*4);
  537. scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
  538. readl(dpriv->base_addr + CH0LRDA + dpriv->dev_id*4);
  539. writel(MTFi|Rdr, dpriv->base_addr + dpriv->dev_id*0x0c + CH0CFG);
  540. writel(Action, dpriv->base_addr + GCMDR);
  541. spin_unlock_irqrestore(&dpriv->pci_priv->lock, flags);
  542. }
  543. #endif
  544. #if 0
  545. static void dscc4_tx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  546. {
  547. u16 i = 0;
  548. /* Cf errata DS5 p.7 */
  549. scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
  550. scc_writel(0x00050000, dpriv, dev, CCR2);
  551. /*
  552. * Must be longer than the time required to fill the fifo.
  553. */
  554. while (!dscc4_tx_quiescent(dpriv, dev) && ++i) {
  555. udelay(1);
  556. wmb();
  557. }
  558. writel(MTFi|Rdt, dpriv->base_addr + dpriv->dev_id*0x0c + CH0CFG);
  559. if (dscc4_do_action(dev, "Rdt") < 0)
  560. printk(KERN_ERR "%s: Tx reset failed\n", dev->name);
  561. }
  562. #endif
  563. /* TODO: (ab)use this function to refill a completely depleted RX ring. */
  564. static inline void dscc4_rx_skb(struct dscc4_dev_priv *dpriv,
  565. struct net_device *dev)
  566. {
  567. struct RxFD *rx_fd = dpriv->rx_fd + dpriv->rx_current%RX_RING_SIZE;
  568. struct pci_dev *pdev = dpriv->pci_priv->pdev;
  569. struct sk_buff *skb;
  570. int pkt_len;
  571. skb = dpriv->rx_skbuff[dpriv->rx_current++%RX_RING_SIZE];
  572. if (!skb) {
  573. printk(KERN_DEBUG "%s: skb=0 (%s)\n", dev->name, __func__);
  574. goto refill;
  575. }
  576. pkt_len = TO_SIZE(le32_to_cpu(rx_fd->state2));
  577. pci_unmap_single(pdev, le32_to_cpu(rx_fd->data),
  578. RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
  579. if ((skb->data[--pkt_len] & FrameOk) == FrameOk) {
  580. dev->stats.rx_packets++;
  581. dev->stats.rx_bytes += pkt_len;
  582. skb_put(skb, pkt_len);
  583. if (netif_running(dev))
  584. skb->protocol = hdlc_type_trans(skb, dev);
  585. netif_rx(skb);
  586. } else {
  587. if (skb->data[pkt_len] & FrameRdo)
  588. dev->stats.rx_fifo_errors++;
  589. else if (!(skb->data[pkt_len] | ~FrameCrc))
  590. dev->stats.rx_crc_errors++;
  591. else if (!(skb->data[pkt_len] | ~(FrameVfr | FrameRab)))
  592. dev->stats.rx_length_errors++;
  593. else
  594. dev->stats.rx_errors++;
  595. dev_kfree_skb_irq(skb);
  596. }
  597. refill:
  598. while ((dpriv->rx_dirty - dpriv->rx_current) % RX_RING_SIZE) {
  599. if (try_get_rx_skb(dpriv, dev) < 0)
  600. break;
  601. dpriv->rx_dirty++;
  602. }
  603. dscc4_rx_update(dpriv, dev);
  604. rx_fd->state2 = 0x00000000;
  605. rx_fd->end = cpu_to_le32(0xbabeface);
  606. }
  607. static void dscc4_free1(struct pci_dev *pdev)
  608. {
  609. struct dscc4_pci_priv *ppriv;
  610. struct dscc4_dev_priv *root;
  611. int i;
  612. ppriv = pci_get_drvdata(pdev);
  613. root = ppriv->root;
  614. for (i = 0; i < dev_per_card; i++)
  615. unregister_hdlc_device(dscc4_to_dev(root + i));
  616. pci_set_drvdata(pdev, NULL);
  617. for (i = 0; i < dev_per_card; i++)
  618. free_netdev(root[i].dev);
  619. kfree(root);
  620. kfree(ppriv);
  621. }
  622. static int __devinit dscc4_init_one(struct pci_dev *pdev,
  623. const struct pci_device_id *ent)
  624. {
  625. struct dscc4_pci_priv *priv;
  626. struct dscc4_dev_priv *dpriv;
  627. void __iomem *ioaddr;
  628. int i, rc;
  629. printk(KERN_DEBUG "%s", version);
  630. rc = pci_enable_device(pdev);
  631. if (rc < 0)
  632. goto out;
  633. rc = pci_request_region(pdev, 0, "registers");
  634. if (rc < 0) {
  635. printk(KERN_ERR "%s: can't reserve MMIO region (regs)\n",
  636. DRV_NAME);
  637. goto err_disable_0;
  638. }
  639. rc = pci_request_region(pdev, 1, "LBI interface");
  640. if (rc < 0) {
  641. printk(KERN_ERR "%s: can't reserve MMIO region (lbi)\n",
  642. DRV_NAME);
  643. goto err_free_mmio_region_1;
  644. }
  645. ioaddr = pci_ioremap_bar(pdev, 0);
  646. if (!ioaddr) {
  647. printk(KERN_ERR "%s: cannot remap MMIO region %llx @ %llx\n",
  648. DRV_NAME, (unsigned long long)pci_resource_len(pdev, 0),
  649. (unsigned long long)pci_resource_start(pdev, 0));
  650. rc = -EIO;
  651. goto err_free_mmio_regions_2;
  652. }
  653. printk(KERN_DEBUG "Siemens DSCC4, MMIO at %#llx (regs), %#llx (lbi), IRQ %d\n",
  654. (unsigned long long)pci_resource_start(pdev, 0),
  655. (unsigned long long)pci_resource_start(pdev, 1), pdev->irq);
  656. /* Cf errata DS5 p.2 */
  657. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xf8);
  658. pci_set_master(pdev);
  659. rc = dscc4_found1(pdev, ioaddr);
  660. if (rc < 0)
  661. goto err_iounmap_3;
  662. priv = pci_get_drvdata(pdev);
  663. rc = request_irq(pdev->irq, dscc4_irq, IRQF_SHARED, DRV_NAME, priv->root);
  664. if (rc < 0) {
  665. printk(KERN_WARNING "%s: IRQ %d busy\n", DRV_NAME, pdev->irq);
  666. goto err_release_4;
  667. }
  668. /* power up/little endian/dma core controlled via lrda/ltda */
  669. writel(0x00000001, ioaddr + GMODE);
  670. /* Shared interrupt queue */
  671. {
  672. u32 bits;
  673. bits = (IRQ_RING_SIZE >> 5) - 1;
  674. bits |= bits << 4;
  675. bits |= bits << 8;
  676. bits |= bits << 16;
  677. writel(bits, ioaddr + IQLENR0);
  678. }
  679. /* Global interrupt queue */
  680. writel((u32)(((IRQ_RING_SIZE >> 5) - 1) << 20), ioaddr + IQLENR1);
  681. priv->iqcfg = (__le32 *) pci_alloc_consistent(pdev,
  682. IRQ_RING_SIZE*sizeof(__le32), &priv->iqcfg_dma);
  683. if (!priv->iqcfg)
  684. goto err_free_irq_5;
  685. writel(priv->iqcfg_dma, ioaddr + IQCFG);
  686. rc = -ENOMEM;
  687. /*
  688. * SCC 0-3 private rx/tx irq structures
  689. * IQRX/TXi needs to be set soon. Learned it the hard way...
  690. */
  691. for (i = 0; i < dev_per_card; i++) {
  692. dpriv = priv->root + i;
  693. dpriv->iqtx = (__le32 *) pci_alloc_consistent(pdev,
  694. IRQ_RING_SIZE*sizeof(u32), &dpriv->iqtx_dma);
  695. if (!dpriv->iqtx)
  696. goto err_free_iqtx_6;
  697. writel(dpriv->iqtx_dma, ioaddr + IQTX0 + i*4);
  698. }
  699. for (i = 0; i < dev_per_card; i++) {
  700. dpriv = priv->root + i;
  701. dpriv->iqrx = (__le32 *) pci_alloc_consistent(pdev,
  702. IRQ_RING_SIZE*sizeof(u32), &dpriv->iqrx_dma);
  703. if (!dpriv->iqrx)
  704. goto err_free_iqrx_7;
  705. writel(dpriv->iqrx_dma, ioaddr + IQRX0 + i*4);
  706. }
  707. /* Cf application hint. Beware of hard-lock condition on threshold. */
  708. writel(0x42104000, ioaddr + FIFOCR1);
  709. //writel(0x9ce69800, ioaddr + FIFOCR2);
  710. writel(0xdef6d800, ioaddr + FIFOCR2);
  711. //writel(0x11111111, ioaddr + FIFOCR4);
  712. writel(0x18181818, ioaddr + FIFOCR4);
  713. // FIXME: should depend on the chipset revision
  714. writel(0x0000000e, ioaddr + FIFOCR3);
  715. writel(0xff200001, ioaddr + GCMDR);
  716. rc = 0;
  717. out:
  718. return rc;
  719. err_free_iqrx_7:
  720. while (--i >= 0) {
  721. dpriv = priv->root + i;
  722. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  723. dpriv->iqrx, dpriv->iqrx_dma);
  724. }
  725. i = dev_per_card;
  726. err_free_iqtx_6:
  727. while (--i >= 0) {
  728. dpriv = priv->root + i;
  729. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  730. dpriv->iqtx, dpriv->iqtx_dma);
  731. }
  732. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), priv->iqcfg,
  733. priv->iqcfg_dma);
  734. err_free_irq_5:
  735. free_irq(pdev->irq, priv->root);
  736. err_release_4:
  737. dscc4_free1(pdev);
  738. err_iounmap_3:
  739. iounmap (ioaddr);
  740. err_free_mmio_regions_2:
  741. pci_release_region(pdev, 1);
  742. err_free_mmio_region_1:
  743. pci_release_region(pdev, 0);
  744. err_disable_0:
  745. pci_disable_device(pdev);
  746. goto out;
  747. };
  748. /*
  749. * Let's hope the default values are decent enough to protect my
  750. * feet from the user's gun - Ueimor
  751. */
  752. static void dscc4_init_registers(struct dscc4_dev_priv *dpriv,
  753. struct net_device *dev)
  754. {
  755. /* No interrupts, SCC core disabled. Let's relax */
  756. scc_writel(0x00000000, dpriv, dev, CCR0);
  757. scc_writel(LengthCheck | (HDLC_MAX_MRU >> 5), dpriv, dev, RLCR);
  758. /*
  759. * No address recognition/crc-CCITT/cts enabled
  760. * Shared flags transmission disabled - cf errata DS5 p.11
  761. * Carrier detect disabled - cf errata p.14
  762. * FIXME: carrier detection/polarity may be handled more gracefully.
  763. */
  764. scc_writel(0x02408000, dpriv, dev, CCR1);
  765. /* crc not forwarded - Cf errata DS5 p.11 */
  766. scc_writel(0x00050008 & ~RxActivate, dpriv, dev, CCR2);
  767. // crc forwarded
  768. //scc_writel(0x00250008 & ~RxActivate, dpriv, dev, CCR2);
  769. }
  770. static inline int dscc4_set_quartz(struct dscc4_dev_priv *dpriv, int hz)
  771. {
  772. int ret = 0;
  773. if ((hz < 0) || (hz > DSCC4_HZ_MAX))
  774. ret = -EOPNOTSUPP;
  775. else
  776. dpriv->pci_priv->xtal_hz = hz;
  777. return ret;
  778. }
  779. static const struct net_device_ops dscc4_ops = {
  780. .ndo_open = dscc4_open,
  781. .ndo_stop = dscc4_close,
  782. .ndo_change_mtu = hdlc_change_mtu,
  783. .ndo_start_xmit = hdlc_start_xmit,
  784. .ndo_do_ioctl = dscc4_ioctl,
  785. .ndo_tx_timeout = dscc4_tx_timeout,
  786. };
  787. static int dscc4_found1(struct pci_dev *pdev, void __iomem *ioaddr)
  788. {
  789. struct dscc4_pci_priv *ppriv;
  790. struct dscc4_dev_priv *root;
  791. int i, ret = -ENOMEM;
  792. root = kcalloc(dev_per_card, sizeof(*root), GFP_KERNEL);
  793. if (!root) {
  794. printk(KERN_ERR "%s: can't allocate data\n", DRV_NAME);
  795. goto err_out;
  796. }
  797. for (i = 0; i < dev_per_card; i++) {
  798. root[i].dev = alloc_hdlcdev(root + i);
  799. if (!root[i].dev)
  800. goto err_free_dev;
  801. }
  802. ppriv = kzalloc(sizeof(*ppriv), GFP_KERNEL);
  803. if (!ppriv) {
  804. printk(KERN_ERR "%s: can't allocate private data\n", DRV_NAME);
  805. goto err_free_dev;
  806. }
  807. ppriv->root = root;
  808. spin_lock_init(&ppriv->lock);
  809. for (i = 0; i < dev_per_card; i++) {
  810. struct dscc4_dev_priv *dpriv = root + i;
  811. struct net_device *d = dscc4_to_dev(dpriv);
  812. hdlc_device *hdlc = dev_to_hdlc(d);
  813. d->base_addr = (unsigned long)ioaddr;
  814. d->irq = pdev->irq;
  815. d->netdev_ops = &dscc4_ops;
  816. d->watchdog_timeo = TX_TIMEOUT;
  817. SET_NETDEV_DEV(d, &pdev->dev);
  818. dpriv->dev_id = i;
  819. dpriv->pci_priv = ppriv;
  820. dpriv->base_addr = ioaddr;
  821. spin_lock_init(&dpriv->lock);
  822. hdlc->xmit = dscc4_start_xmit;
  823. hdlc->attach = dscc4_hdlc_attach;
  824. dscc4_init_registers(dpriv, d);
  825. dpriv->parity = PARITY_CRC16_PR0_CCITT;
  826. dpriv->encoding = ENCODING_NRZ;
  827. ret = dscc4_init_ring(d);
  828. if (ret < 0)
  829. goto err_unregister;
  830. ret = register_hdlc_device(d);
  831. if (ret < 0) {
  832. printk(KERN_ERR "%s: unable to register\n", DRV_NAME);
  833. dscc4_release_ring(dpriv);
  834. goto err_unregister;
  835. }
  836. }
  837. ret = dscc4_set_quartz(root, quartz);
  838. if (ret < 0)
  839. goto err_unregister;
  840. pci_set_drvdata(pdev, ppriv);
  841. return ret;
  842. err_unregister:
  843. while (i-- > 0) {
  844. dscc4_release_ring(root + i);
  845. unregister_hdlc_device(dscc4_to_dev(root + i));
  846. }
  847. kfree(ppriv);
  848. i = dev_per_card;
  849. err_free_dev:
  850. while (i-- > 0)
  851. free_netdev(root[i].dev);
  852. kfree(root);
  853. err_out:
  854. return ret;
  855. };
  856. /* FIXME: get rid of the unneeded code */
  857. static void dscc4_timer(unsigned long data)
  858. {
  859. struct net_device *dev = (struct net_device *)data;
  860. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  861. // struct dscc4_pci_priv *ppriv;
  862. goto done;
  863. done:
  864. dpriv->timer.expires = jiffies + TX_TIMEOUT;
  865. add_timer(&dpriv->timer);
  866. }
  867. static void dscc4_tx_timeout(struct net_device *dev)
  868. {
  869. /* FIXME: something is missing there */
  870. }
  871. static int dscc4_loopback_check(struct dscc4_dev_priv *dpriv)
  872. {
  873. sync_serial_settings *settings = &dpriv->settings;
  874. if (settings->loopback && (settings->clock_type != CLOCK_INT)) {
  875. struct net_device *dev = dscc4_to_dev(dpriv);
  876. printk(KERN_INFO "%s: loopback requires clock\n", dev->name);
  877. return -1;
  878. }
  879. return 0;
  880. }
  881. #ifdef CONFIG_DSCC4_PCI_RST
  882. /*
  883. * Some DSCC4-based cards wires the GPIO port and the PCI #RST pin together
  884. * so as to provide a safe way to reset the asic while not the whole machine
  885. * rebooting.
  886. *
  887. * This code doesn't need to be efficient. Keep It Simple
  888. */
  889. static void dscc4_pci_reset(struct pci_dev *pdev, void __iomem *ioaddr)
  890. {
  891. int i;
  892. mutex_lock(&dscc4_mutex);
  893. for (i = 0; i < 16; i++)
  894. pci_read_config_dword(pdev, i << 2, dscc4_pci_config_store + i);
  895. /* Maximal LBI clock divider (who cares ?) and whole GPIO range. */
  896. writel(0x001c0000, ioaddr + GMODE);
  897. /* Configure GPIO port as output */
  898. writel(0x0000ffff, ioaddr + GPDIR);
  899. /* Disable interruption */
  900. writel(0x0000ffff, ioaddr + GPIM);
  901. writel(0x0000ffff, ioaddr + GPDATA);
  902. writel(0x00000000, ioaddr + GPDATA);
  903. /* Flush posted writes */
  904. readl(ioaddr + GSTAR);
  905. schedule_timeout_uninterruptible(10);
  906. for (i = 0; i < 16; i++)
  907. pci_write_config_dword(pdev, i << 2, dscc4_pci_config_store[i]);
  908. mutex_unlock(&dscc4_mutex);
  909. }
  910. #else
  911. #define dscc4_pci_reset(pdev,ioaddr) do {} while (0)
  912. #endif /* CONFIG_DSCC4_PCI_RST */
  913. static int dscc4_open(struct net_device *dev)
  914. {
  915. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  916. struct dscc4_pci_priv *ppriv;
  917. int ret = -EAGAIN;
  918. if ((dscc4_loopback_check(dpriv) < 0))
  919. goto err;
  920. if ((ret = hdlc_open(dev)))
  921. goto err;
  922. ppriv = dpriv->pci_priv;
  923. /*
  924. * Due to various bugs, there is no way to reliably reset a
  925. * specific port (manufacturer's dependant special PCI #RST wiring
  926. * apart: it affects all ports). Thus the device goes in the best
  927. * silent mode possible at dscc4_close() time and simply claims to
  928. * be up if it's opened again. It still isn't possible to change
  929. * the HDLC configuration without rebooting but at least the ports
  930. * can be up/down ifconfig'ed without killing the host.
  931. */
  932. if (dpriv->flags & FakeReset) {
  933. dpriv->flags &= ~FakeReset;
  934. scc_patchl(0, PowerUp, dpriv, dev, CCR0);
  935. scc_patchl(0, 0x00050000, dpriv, dev, CCR2);
  936. scc_writel(EventsMask, dpriv, dev, IMR);
  937. printk(KERN_INFO "%s: up again.\n", dev->name);
  938. goto done;
  939. }
  940. /* IDT+IDR during XPR */
  941. dpriv->flags = NeedIDR | NeedIDT;
  942. scc_patchl(0, PowerUp | Vis, dpriv, dev, CCR0);
  943. /*
  944. * The following is a bit paranoid...
  945. *
  946. * NB: the datasheet "...CEC will stay active if the SCC is in
  947. * power-down mode or..." and CCR2.RAC = 1 are two different
  948. * situations.
  949. */
  950. if (scc_readl_star(dpriv, dev) & SccBusy) {
  951. printk(KERN_ERR "%s busy. Try later\n", dev->name);
  952. ret = -EAGAIN;
  953. goto err_out;
  954. } else
  955. printk(KERN_INFO "%s: available. Good\n", dev->name);
  956. scc_writel(EventsMask, dpriv, dev, IMR);
  957. /* Posted write is flushed in the wait_ack loop */
  958. scc_writel(TxSccRes | RxSccRes, dpriv, dev, CMDR);
  959. if ((ret = dscc4_wait_ack_cec(dpriv, dev, "Cec")) < 0)
  960. goto err_disable_scc_events;
  961. /*
  962. * I would expect XPR near CE completion (before ? after ?).
  963. * At worst, this code won't see a late XPR and people
  964. * will have to re-issue an ifconfig (this is harmless).
  965. * WARNING, a really missing XPR usually means a hardware
  966. * reset is needed. Suggestions anyone ?
  967. */
  968. if ((ret = dscc4_xpr_ack(dpriv)) < 0) {
  969. printk(KERN_ERR "%s: %s timeout\n", DRV_NAME, "XPR");
  970. goto err_disable_scc_events;
  971. }
  972. if (debug > 2)
  973. dscc4_tx_print(dev, dpriv, "Open");
  974. done:
  975. netif_start_queue(dev);
  976. init_timer(&dpriv->timer);
  977. dpriv->timer.expires = jiffies + 10*HZ;
  978. dpriv->timer.data = (unsigned long)dev;
  979. dpriv->timer.function = &dscc4_timer;
  980. add_timer(&dpriv->timer);
  981. netif_carrier_on(dev);
  982. return 0;
  983. err_disable_scc_events:
  984. scc_writel(0xffffffff, dpriv, dev, IMR);
  985. scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
  986. err_out:
  987. hdlc_close(dev);
  988. err:
  989. return ret;
  990. }
  991. #ifdef DSCC4_POLLING
  992. static int dscc4_tx_poll(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  993. {
  994. /* FIXME: it's gonna be easy (TM), for sure */
  995. }
  996. #endif /* DSCC4_POLLING */
  997. static int dscc4_start_xmit(struct sk_buff *skb, struct net_device *dev)
  998. {
  999. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1000. struct dscc4_pci_priv *ppriv = dpriv->pci_priv;
  1001. struct TxFD *tx_fd;
  1002. int next;
  1003. next = dpriv->tx_current%TX_RING_SIZE;
  1004. dpriv->tx_skbuff[next] = skb;
  1005. tx_fd = dpriv->tx_fd + next;
  1006. tx_fd->state = FrameEnd | TO_STATE_TX(skb->len);
  1007. tx_fd->data = cpu_to_le32(pci_map_single(ppriv->pdev, skb->data, skb->len,
  1008. PCI_DMA_TODEVICE));
  1009. tx_fd->complete = 0x00000000;
  1010. tx_fd->jiffies = jiffies;
  1011. mb();
  1012. #ifdef DSCC4_POLLING
  1013. spin_lock(&dpriv->lock);
  1014. while (dscc4_tx_poll(dpriv, dev));
  1015. spin_unlock(&dpriv->lock);
  1016. #endif
  1017. dev->trans_start = jiffies;
  1018. if (debug > 2)
  1019. dscc4_tx_print(dev, dpriv, "Xmit");
  1020. /* To be cleaned(unsigned int)/optimized. Later, ok ? */
  1021. if (!((++dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE))
  1022. netif_stop_queue(dev);
  1023. if (dscc4_tx_quiescent(dpriv, dev))
  1024. dscc4_do_tx(dpriv, dev);
  1025. return 0;
  1026. }
  1027. static int dscc4_close(struct net_device *dev)
  1028. {
  1029. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1030. del_timer_sync(&dpriv->timer);
  1031. netif_stop_queue(dev);
  1032. scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
  1033. scc_patchl(0x00050000, 0, dpriv, dev, CCR2);
  1034. scc_writel(0xffffffff, dpriv, dev, IMR);
  1035. dpriv->flags |= FakeReset;
  1036. hdlc_close(dev);
  1037. return 0;
  1038. }
  1039. static inline int dscc4_check_clock_ability(int port)
  1040. {
  1041. int ret = 0;
  1042. #ifdef CONFIG_DSCC4_PCISYNC
  1043. if (port >= 2)
  1044. ret = -1;
  1045. #endif
  1046. return ret;
  1047. }
  1048. /*
  1049. * DS1 p.137: "There are a total of 13 different clocking modes..."
  1050. * ^^
  1051. * Design choices:
  1052. * - by default, assume a clock is provided on pin RxClk/TxClk (clock mode 0a).
  1053. * Clock mode 3b _should_ work but the testing seems to make this point
  1054. * dubious (DIY testing requires setting CCR0 at 0x00000033).
  1055. * This is supposed to provide least surprise "DTE like" behavior.
  1056. * - if line rate is specified, clocks are assumed to be locally generated.
  1057. * A quartz must be available (on pin XTAL1). Modes 6b/7b are used. Choosing
  1058. * between these it automagically done according on the required frequency
  1059. * scaling. Of course some rounding may take place.
  1060. * - no high speed mode (40Mb/s). May be trivial to do but I don't have an
  1061. * appropriate external clocking device for testing.
  1062. * - no time-slot/clock mode 5: shameless lazyness.
  1063. *
  1064. * The clock signals wiring can be (is ?) manufacturer dependant. Good luck.
  1065. *
  1066. * BIG FAT WARNING: if the device isn't provided enough clocking signal, it
  1067. * won't pass the init sequence. For example, straight back-to-back DTE without
  1068. * external clock will fail when dscc4_open() (<- 'ifconfig hdlcx xxx') is
  1069. * called.
  1070. *
  1071. * Typos lurk in datasheet (missing divier in clock mode 7a figure 51 p.153
  1072. * DS0 for example)
  1073. *
  1074. * Clock mode related bits of CCR0:
  1075. * +------------ TOE: output TxClk (0b/2b/3a/3b/6b/7a/7b only)
  1076. * | +---------- SSEL: sub-mode select 0 -> a, 1 -> b
  1077. * | | +-------- High Speed: say 0
  1078. * | | | +-+-+-- Clock Mode: 0..7
  1079. * | | | | | |
  1080. * -+-+-+-+-+-+-+-+
  1081. * x|x|5|4|3|2|1|0| lower bits
  1082. *
  1083. * Division factor of BRR: k = (N+1)x2^M (total divider = 16xk in mode 6b)
  1084. * +-+-+-+------------------ M (0..15)
  1085. * | | | | +-+-+-+-+-+-- N (0..63)
  1086. * 0 0 0 0 | | | | 0 0 | | | | | |
  1087. * ...-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1088. * f|e|d|c|b|a|9|8|7|6|5|4|3|2|1|0| lower bits
  1089. *
  1090. */
  1091. static int dscc4_set_clock(struct net_device *dev, u32 *bps, u32 *state)
  1092. {
  1093. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1094. int ret = -1;
  1095. u32 brr;
  1096. *state &= ~Ccr0ClockMask;
  1097. if (*bps) { /* Clock generated - required for DCE */
  1098. u32 n = 0, m = 0, divider;
  1099. int xtal;
  1100. xtal = dpriv->pci_priv->xtal_hz;
  1101. if (!xtal)
  1102. goto done;
  1103. if (dscc4_check_clock_ability(dpriv->dev_id) < 0)
  1104. goto done;
  1105. divider = xtal / *bps;
  1106. if (divider > BRR_DIVIDER_MAX) {
  1107. divider >>= 4;
  1108. *state |= 0x00000036; /* Clock mode 6b (BRG/16) */
  1109. } else
  1110. *state |= 0x00000037; /* Clock mode 7b (BRG) */
  1111. if (divider >> 22) {
  1112. n = 63;
  1113. m = 15;
  1114. } else if (divider) {
  1115. /* Extraction of the 6 highest weighted bits */
  1116. m = 0;
  1117. while (0xffffffc0 & divider) {
  1118. m++;
  1119. divider >>= 1;
  1120. }
  1121. n = divider;
  1122. }
  1123. brr = (m << 8) | n;
  1124. divider = n << m;
  1125. if (!(*state & 0x00000001)) /* ?b mode mask => clock mode 6b */
  1126. divider <<= 4;
  1127. *bps = xtal / divider;
  1128. } else {
  1129. /*
  1130. * External clock - DTE
  1131. * "state" already reflects Clock mode 0a (CCR0 = 0xzzzzzz00).
  1132. * Nothing more to be done
  1133. */
  1134. brr = 0;
  1135. }
  1136. scc_writel(brr, dpriv, dev, BRR);
  1137. ret = 0;
  1138. done:
  1139. return ret;
  1140. }
  1141. static int dscc4_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1142. {
  1143. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1144. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1145. const size_t size = sizeof(dpriv->settings);
  1146. int ret = 0;
  1147. if (dev->flags & IFF_UP)
  1148. return -EBUSY;
  1149. if (cmd != SIOCWANDEV)
  1150. return -EOPNOTSUPP;
  1151. switch(ifr->ifr_settings.type) {
  1152. case IF_GET_IFACE:
  1153. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1154. if (ifr->ifr_settings.size < size) {
  1155. ifr->ifr_settings.size = size; /* data size wanted */
  1156. return -ENOBUFS;
  1157. }
  1158. if (copy_to_user(line, &dpriv->settings, size))
  1159. return -EFAULT;
  1160. break;
  1161. case IF_IFACE_SYNC_SERIAL:
  1162. if (!capable(CAP_NET_ADMIN))
  1163. return -EPERM;
  1164. if (dpriv->flags & FakeReset) {
  1165. printk(KERN_INFO "%s: please reset the device"
  1166. " before this command\n", dev->name);
  1167. return -EPERM;
  1168. }
  1169. if (copy_from_user(&dpriv->settings, line, size))
  1170. return -EFAULT;
  1171. ret = dscc4_set_iface(dpriv, dev);
  1172. break;
  1173. default:
  1174. ret = hdlc_ioctl(dev, ifr, cmd);
  1175. break;
  1176. }
  1177. return ret;
  1178. }
  1179. static int dscc4_match(struct thingie *p, int value)
  1180. {
  1181. int i;
  1182. for (i = 0; p[i].define != -1; i++) {
  1183. if (value == p[i].define)
  1184. break;
  1185. }
  1186. if (p[i].define == -1)
  1187. return -1;
  1188. else
  1189. return i;
  1190. }
  1191. static int dscc4_clock_setting(struct dscc4_dev_priv *dpriv,
  1192. struct net_device *dev)
  1193. {
  1194. sync_serial_settings *settings = &dpriv->settings;
  1195. int ret = -EOPNOTSUPP;
  1196. u32 bps, state;
  1197. bps = settings->clock_rate;
  1198. state = scc_readl(dpriv, CCR0);
  1199. if (dscc4_set_clock(dev, &bps, &state) < 0)
  1200. goto done;
  1201. if (bps) { /* DCE */
  1202. printk(KERN_DEBUG "%s: generated RxClk (DCE)\n", dev->name);
  1203. if (settings->clock_rate != bps) {
  1204. printk(KERN_DEBUG "%s: clock adjusted (%08d -> %08d)\n",
  1205. dev->name, settings->clock_rate, bps);
  1206. settings->clock_rate = bps;
  1207. }
  1208. } else { /* DTE */
  1209. state |= PowerUp | Vis;
  1210. printk(KERN_DEBUG "%s: external RxClk (DTE)\n", dev->name);
  1211. }
  1212. scc_writel(state, dpriv, dev, CCR0);
  1213. ret = 0;
  1214. done:
  1215. return ret;
  1216. }
  1217. static int dscc4_encoding_setting(struct dscc4_dev_priv *dpriv,
  1218. struct net_device *dev)
  1219. {
  1220. struct thingie encoding[] = {
  1221. { ENCODING_NRZ, 0x00000000 },
  1222. { ENCODING_NRZI, 0x00200000 },
  1223. { ENCODING_FM_MARK, 0x00400000 },
  1224. { ENCODING_FM_SPACE, 0x00500000 },
  1225. { ENCODING_MANCHESTER, 0x00600000 },
  1226. { -1, 0}
  1227. };
  1228. int i, ret = 0;
  1229. i = dscc4_match(encoding, dpriv->encoding);
  1230. if (i >= 0)
  1231. scc_patchl(EncodingMask, encoding[i].bits, dpriv, dev, CCR0);
  1232. else
  1233. ret = -EOPNOTSUPP;
  1234. return ret;
  1235. }
  1236. static int dscc4_loopback_setting(struct dscc4_dev_priv *dpriv,
  1237. struct net_device *dev)
  1238. {
  1239. sync_serial_settings *settings = &dpriv->settings;
  1240. u32 state;
  1241. state = scc_readl(dpriv, CCR1);
  1242. if (settings->loopback) {
  1243. printk(KERN_DEBUG "%s: loopback\n", dev->name);
  1244. state |= 0x00000100;
  1245. } else {
  1246. printk(KERN_DEBUG "%s: normal\n", dev->name);
  1247. state &= ~0x00000100;
  1248. }
  1249. scc_writel(state, dpriv, dev, CCR1);
  1250. return 0;
  1251. }
  1252. static int dscc4_crc_setting(struct dscc4_dev_priv *dpriv,
  1253. struct net_device *dev)
  1254. {
  1255. struct thingie crc[] = {
  1256. { PARITY_CRC16_PR0_CCITT, 0x00000010 },
  1257. { PARITY_CRC16_PR1_CCITT, 0x00000000 },
  1258. { PARITY_CRC32_PR0_CCITT, 0x00000011 },
  1259. { PARITY_CRC32_PR1_CCITT, 0x00000001 }
  1260. };
  1261. int i, ret = 0;
  1262. i = dscc4_match(crc, dpriv->parity);
  1263. if (i >= 0)
  1264. scc_patchl(CrcMask, crc[i].bits, dpriv, dev, CCR1);
  1265. else
  1266. ret = -EOPNOTSUPP;
  1267. return ret;
  1268. }
  1269. static int dscc4_set_iface(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  1270. {
  1271. struct {
  1272. int (*action)(struct dscc4_dev_priv *, struct net_device *);
  1273. } *p, do_setting[] = {
  1274. { dscc4_encoding_setting },
  1275. { dscc4_clock_setting },
  1276. { dscc4_loopback_setting },
  1277. { dscc4_crc_setting },
  1278. { NULL }
  1279. };
  1280. int ret = 0;
  1281. for (p = do_setting; p->action; p++) {
  1282. if ((ret = p->action(dpriv, dev)) < 0)
  1283. break;
  1284. }
  1285. return ret;
  1286. }
  1287. static irqreturn_t dscc4_irq(int irq, void *token)
  1288. {
  1289. struct dscc4_dev_priv *root = token;
  1290. struct dscc4_pci_priv *priv;
  1291. struct net_device *dev;
  1292. void __iomem *ioaddr;
  1293. u32 state;
  1294. unsigned long flags;
  1295. int i, handled = 1;
  1296. priv = root->pci_priv;
  1297. dev = dscc4_to_dev(root);
  1298. spin_lock_irqsave(&priv->lock, flags);
  1299. ioaddr = root->base_addr;
  1300. state = readl(ioaddr + GSTAR);
  1301. if (!state) {
  1302. handled = 0;
  1303. goto out;
  1304. }
  1305. if (debug > 3)
  1306. printk(KERN_DEBUG "%s: GSTAR = 0x%08x\n", DRV_NAME, state);
  1307. writel(state, ioaddr + GSTAR);
  1308. if (state & Arf) {
  1309. printk(KERN_ERR "%s: failure (Arf). Harass the maintener\n",
  1310. dev->name);
  1311. goto out;
  1312. }
  1313. state &= ~ArAck;
  1314. if (state & Cfg) {
  1315. if (debug > 0)
  1316. printk(KERN_DEBUG "%s: CfgIV\n", DRV_NAME);
  1317. if (priv->iqcfg[priv->cfg_cur++%IRQ_RING_SIZE] & cpu_to_le32(Arf))
  1318. printk(KERN_ERR "%s: %s failed\n", dev->name, "CFG");
  1319. if (!(state &= ~Cfg))
  1320. goto out;
  1321. }
  1322. if (state & RxEvt) {
  1323. i = dev_per_card - 1;
  1324. do {
  1325. dscc4_rx_irq(priv, root + i);
  1326. } while (--i >= 0);
  1327. state &= ~RxEvt;
  1328. }
  1329. if (state & TxEvt) {
  1330. i = dev_per_card - 1;
  1331. do {
  1332. dscc4_tx_irq(priv, root + i);
  1333. } while (--i >= 0);
  1334. state &= ~TxEvt;
  1335. }
  1336. out:
  1337. spin_unlock_irqrestore(&priv->lock, flags);
  1338. return IRQ_RETVAL(handled);
  1339. }
  1340. static void dscc4_tx_irq(struct dscc4_pci_priv *ppriv,
  1341. struct dscc4_dev_priv *dpriv)
  1342. {
  1343. struct net_device *dev = dscc4_to_dev(dpriv);
  1344. u32 state;
  1345. int cur, loop = 0;
  1346. try:
  1347. cur = dpriv->iqtx_current%IRQ_RING_SIZE;
  1348. state = le32_to_cpu(dpriv->iqtx[cur]);
  1349. if (!state) {
  1350. if (debug > 4)
  1351. printk(KERN_DEBUG "%s: Tx ISR = 0x%08x\n", dev->name,
  1352. state);
  1353. if ((debug > 1) && (loop > 1))
  1354. printk(KERN_DEBUG "%s: Tx irq loop=%d\n", dev->name, loop);
  1355. if (loop && netif_queue_stopped(dev))
  1356. if ((dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE)
  1357. netif_wake_queue(dev);
  1358. if (netif_running(dev) && dscc4_tx_quiescent(dpriv, dev) &&
  1359. !dscc4_tx_done(dpriv))
  1360. dscc4_do_tx(dpriv, dev);
  1361. return;
  1362. }
  1363. loop++;
  1364. dpriv->iqtx[cur] = 0;
  1365. dpriv->iqtx_current++;
  1366. if (state_check(state, dpriv, dev, "Tx") < 0)
  1367. return;
  1368. if (state & SccEvt) {
  1369. if (state & Alls) {
  1370. struct sk_buff *skb;
  1371. struct TxFD *tx_fd;
  1372. if (debug > 2)
  1373. dscc4_tx_print(dev, dpriv, "Alls");
  1374. /*
  1375. * DataComplete can't be trusted for Tx completion.
  1376. * Cf errata DS5 p.8
  1377. */
  1378. cur = dpriv->tx_dirty%TX_RING_SIZE;
  1379. tx_fd = dpriv->tx_fd + cur;
  1380. skb = dpriv->tx_skbuff[cur];
  1381. if (skb) {
  1382. pci_unmap_single(ppriv->pdev, le32_to_cpu(tx_fd->data),
  1383. skb->len, PCI_DMA_TODEVICE);
  1384. if (tx_fd->state & FrameEnd) {
  1385. dev->stats.tx_packets++;
  1386. dev->stats.tx_bytes += skb->len;
  1387. }
  1388. dev_kfree_skb_irq(skb);
  1389. dpriv->tx_skbuff[cur] = NULL;
  1390. ++dpriv->tx_dirty;
  1391. } else {
  1392. if (debug > 1)
  1393. printk(KERN_ERR "%s Tx: NULL skb %d\n",
  1394. dev->name, cur);
  1395. }
  1396. /*
  1397. * If the driver ends sending crap on the wire, it
  1398. * will be way easier to diagnose than the (not so)
  1399. * random freeze induced by null sized tx frames.
  1400. */
  1401. tx_fd->data = tx_fd->next;
  1402. tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
  1403. tx_fd->complete = 0x00000000;
  1404. tx_fd->jiffies = 0;
  1405. if (!(state &= ~Alls))
  1406. goto try;
  1407. }
  1408. /*
  1409. * Transmit Data Underrun
  1410. */
  1411. if (state & Xdu) {
  1412. printk(KERN_ERR "%s: XDU. Ask maintainer\n", DRV_NAME);
  1413. dpriv->flags = NeedIDT;
  1414. /* Tx reset */
  1415. writel(MTFi | Rdt,
  1416. dpriv->base_addr + 0x0c*dpriv->dev_id + CH0CFG);
  1417. writel(Action, dpriv->base_addr + GCMDR);
  1418. return;
  1419. }
  1420. if (state & Cts) {
  1421. printk(KERN_INFO "%s: CTS transition\n", dev->name);
  1422. if (!(state &= ~Cts)) /* DEBUG */
  1423. goto try;
  1424. }
  1425. if (state & Xmr) {
  1426. /* Frame needs to be sent again - FIXME */
  1427. printk(KERN_ERR "%s: Xmr. Ask maintainer\n", DRV_NAME);
  1428. if (!(state &= ~Xmr)) /* DEBUG */
  1429. goto try;
  1430. }
  1431. if (state & Xpr) {
  1432. void __iomem *scc_addr;
  1433. unsigned long ring;
  1434. int i;
  1435. /*
  1436. * - the busy condition happens (sometimes);
  1437. * - it doesn't seem to make the handler unreliable.
  1438. */
  1439. for (i = 1; i; i <<= 1) {
  1440. if (!(scc_readl_star(dpriv, dev) & SccBusy))
  1441. break;
  1442. }
  1443. if (!i)
  1444. printk(KERN_INFO "%s busy in irq\n", dev->name);
  1445. scc_addr = dpriv->base_addr + 0x0c*dpriv->dev_id;
  1446. /* Keep this order: IDT before IDR */
  1447. if (dpriv->flags & NeedIDT) {
  1448. if (debug > 2)
  1449. dscc4_tx_print(dev, dpriv, "Xpr");
  1450. ring = dpriv->tx_fd_dma +
  1451. (dpriv->tx_dirty%TX_RING_SIZE)*
  1452. sizeof(struct TxFD);
  1453. writel(ring, scc_addr + CH0BTDA);
  1454. dscc4_do_tx(dpriv, dev);
  1455. writel(MTFi | Idt, scc_addr + CH0CFG);
  1456. if (dscc4_do_action(dev, "IDT") < 0)
  1457. goto err_xpr;
  1458. dpriv->flags &= ~NeedIDT;
  1459. }
  1460. if (dpriv->flags & NeedIDR) {
  1461. ring = dpriv->rx_fd_dma +
  1462. (dpriv->rx_current%RX_RING_SIZE)*
  1463. sizeof(struct RxFD);
  1464. writel(ring, scc_addr + CH0BRDA);
  1465. dscc4_rx_update(dpriv, dev);
  1466. writel(MTFi | Idr, scc_addr + CH0CFG);
  1467. if (dscc4_do_action(dev, "IDR") < 0)
  1468. goto err_xpr;
  1469. dpriv->flags &= ~NeedIDR;
  1470. smp_wmb();
  1471. /* Activate receiver and misc */
  1472. scc_writel(0x08050008, dpriv, dev, CCR2);
  1473. }
  1474. err_xpr:
  1475. if (!(state &= ~Xpr))
  1476. goto try;
  1477. }
  1478. if (state & Cd) {
  1479. if (debug > 0)
  1480. printk(KERN_INFO "%s: CD transition\n", dev->name);
  1481. if (!(state &= ~Cd)) /* DEBUG */
  1482. goto try;
  1483. }
  1484. } else { /* ! SccEvt */
  1485. if (state & Hi) {
  1486. #ifdef DSCC4_POLLING
  1487. while (!dscc4_tx_poll(dpriv, dev));
  1488. #endif
  1489. printk(KERN_INFO "%s: Tx Hi\n", dev->name);
  1490. state &= ~Hi;
  1491. }
  1492. if (state & Err) {
  1493. printk(KERN_INFO "%s: Tx ERR\n", dev->name);
  1494. dev->stats.tx_errors++;
  1495. state &= ~Err;
  1496. }
  1497. }
  1498. goto try;
  1499. }
  1500. static void dscc4_rx_irq(struct dscc4_pci_priv *priv,
  1501. struct dscc4_dev_priv *dpriv)
  1502. {
  1503. struct net_device *dev = dscc4_to_dev(dpriv);
  1504. u32 state;
  1505. int cur;
  1506. try:
  1507. cur = dpriv->iqrx_current%IRQ_RING_SIZE;
  1508. state = le32_to_cpu(dpriv->iqrx[cur]);
  1509. if (!state)
  1510. return;
  1511. dpriv->iqrx[cur] = 0;
  1512. dpriv->iqrx_current++;
  1513. if (state_check(state, dpriv, dev, "Rx") < 0)
  1514. return;
  1515. if (!(state & SccEvt)){
  1516. struct RxFD *rx_fd;
  1517. if (debug > 4)
  1518. printk(KERN_DEBUG "%s: Rx ISR = 0x%08x\n", dev->name,
  1519. state);
  1520. state &= 0x00ffffff;
  1521. if (state & Err) { /* Hold or reset */
  1522. printk(KERN_DEBUG "%s: Rx ERR\n", dev->name);
  1523. cur = dpriv->rx_current%RX_RING_SIZE;
  1524. rx_fd = dpriv->rx_fd + cur;
  1525. /*
  1526. * Presume we're not facing a DMAC receiver reset.
  1527. * As We use the rx size-filtering feature of the
  1528. * DSCC4, the beginning of a new frame is waiting in
  1529. * the rx fifo. I bet a Receive Data Overflow will
  1530. * happen most of time but let's try and avoid it.
  1531. * Btw (as for RDO) if one experiences ERR whereas
  1532. * the system looks rather idle, there may be a
  1533. * problem with latency. In this case, increasing
  1534. * RX_RING_SIZE may help.
  1535. */
  1536. //while (dpriv->rx_needs_refill) {
  1537. while (!(rx_fd->state1 & Hold)) {
  1538. rx_fd++;
  1539. cur++;
  1540. if (!(cur = cur%RX_RING_SIZE))
  1541. rx_fd = dpriv->rx_fd;
  1542. }
  1543. //dpriv->rx_needs_refill--;
  1544. try_get_rx_skb(dpriv, dev);
  1545. if (!rx_fd->data)
  1546. goto try;
  1547. rx_fd->state1 &= ~Hold;
  1548. rx_fd->state2 = 0x00000000;
  1549. rx_fd->end = cpu_to_le32(0xbabeface);
  1550. //}
  1551. goto try;
  1552. }
  1553. if (state & Fi) {
  1554. dscc4_rx_skb(dpriv, dev);
  1555. goto try;
  1556. }
  1557. if (state & Hi ) { /* HI bit */
  1558. printk(KERN_INFO "%s: Rx Hi\n", dev->name);
  1559. state &= ~Hi;
  1560. goto try;
  1561. }
  1562. } else { /* SccEvt */
  1563. if (debug > 1) {
  1564. //FIXME: verifier la presence de tous les evenements
  1565. static struct {
  1566. u32 mask;
  1567. const char *irq_name;
  1568. } evts[] = {
  1569. { 0x00008000, "TIN"},
  1570. { 0x00000020, "RSC"},
  1571. { 0x00000010, "PCE"},
  1572. { 0x00000008, "PLLA"},
  1573. { 0, NULL}
  1574. }, *evt;
  1575. for (evt = evts; evt->irq_name; evt++) {
  1576. if (state & evt->mask) {
  1577. printk(KERN_DEBUG "%s: %s\n",
  1578. dev->name, evt->irq_name);
  1579. if (!(state &= ~evt->mask))
  1580. goto try;
  1581. }
  1582. }
  1583. } else {
  1584. if (!(state &= ~0x0000c03c))
  1585. goto try;
  1586. }
  1587. if (state & Cts) {
  1588. printk(KERN_INFO "%s: CTS transition\n", dev->name);
  1589. if (!(state &= ~Cts)) /* DEBUG */
  1590. goto try;
  1591. }
  1592. /*
  1593. * Receive Data Overflow (FIXME: fscked)
  1594. */
  1595. if (state & Rdo) {
  1596. struct RxFD *rx_fd;
  1597. void __iomem *scc_addr;
  1598. int cur;
  1599. //if (debug)
  1600. // dscc4_rx_dump(dpriv);
  1601. scc_addr = dpriv->base_addr + 0x0c*dpriv->dev_id;
  1602. scc_patchl(RxActivate, 0, dpriv, dev, CCR2);
  1603. /*
  1604. * This has no effect. Why ?
  1605. * ORed with TxSccRes, one sees the CFG ack (for
  1606. * the TX part only).
  1607. */
  1608. scc_writel(RxSccRes, dpriv, dev, CMDR);
  1609. dpriv->flags |= RdoSet;
  1610. /*
  1611. * Let's try and save something in the received data.
  1612. * rx_current must be incremented at least once to
  1613. * avoid HOLD in the BRDA-to-be-pointed desc.
  1614. */
  1615. do {
  1616. cur = dpriv->rx_current++%RX_RING_SIZE;
  1617. rx_fd = dpriv->rx_fd + cur;
  1618. if (!(rx_fd->state2 & DataComplete))
  1619. break;
  1620. if (rx_fd->state2 & FrameAborted) {
  1621. dev->stats.rx_over_errors++;
  1622. rx_fd->state1 |= Hold;
  1623. rx_fd->state2 = 0x00000000;
  1624. rx_fd->end = cpu_to_le32(0xbabeface);
  1625. } else
  1626. dscc4_rx_skb(dpriv, dev);
  1627. } while (1);
  1628. if (debug > 0) {
  1629. if (dpriv->flags & RdoSet)
  1630. printk(KERN_DEBUG
  1631. "%s: no RDO in Rx data\n", DRV_NAME);
  1632. }
  1633. #ifdef DSCC4_RDO_EXPERIMENTAL_RECOVERY
  1634. /*
  1635. * FIXME: must the reset be this violent ?
  1636. */
  1637. #warning "FIXME: CH0BRDA"
  1638. writel(dpriv->rx_fd_dma +
  1639. (dpriv->rx_current%RX_RING_SIZE)*
  1640. sizeof(struct RxFD), scc_addr + CH0BRDA);
  1641. writel(MTFi|Rdr|Idr, scc_addr + CH0CFG);
  1642. if (dscc4_do_action(dev, "RDR") < 0) {
  1643. printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
  1644. dev->name, "RDR");
  1645. goto rdo_end;
  1646. }
  1647. writel(MTFi|Idr, scc_addr + CH0CFG);
  1648. if (dscc4_do_action(dev, "IDR") < 0) {
  1649. printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
  1650. dev->name, "IDR");
  1651. goto rdo_end;
  1652. }
  1653. rdo_end:
  1654. #endif
  1655. scc_patchl(0, RxActivate, dpriv, dev, CCR2);
  1656. goto try;
  1657. }
  1658. if (state & Cd) {
  1659. printk(KERN_INFO "%s: CD transition\n", dev->name);
  1660. if (!(state &= ~Cd)) /* DEBUG */
  1661. goto try;
  1662. }
  1663. if (state & Flex) {
  1664. printk(KERN_DEBUG "%s: Flex. Ttttt...\n", DRV_NAME);
  1665. if (!(state &= ~Flex))
  1666. goto try;
  1667. }
  1668. }
  1669. }
  1670. /*
  1671. * I had expected the following to work for the first descriptor
  1672. * (tx_fd->state = 0xc0000000)
  1673. * - Hold=1 (don't try and branch to the next descripto);
  1674. * - No=0 (I want an empty data section, i.e. size=0);
  1675. * - Fe=1 (required by No=0 or we got an Err irq and must reset).
  1676. * It failed and locked solid. Thus the introduction of a dummy skb.
  1677. * Problem is acknowledged in errata sheet DS5. Joy :o/
  1678. */
  1679. static struct sk_buff *dscc4_init_dummy_skb(struct dscc4_dev_priv *dpriv)
  1680. {
  1681. struct sk_buff *skb;
  1682. skb = dev_alloc_skb(DUMMY_SKB_SIZE);
  1683. if (skb) {
  1684. int last = dpriv->tx_dirty%TX_RING_SIZE;
  1685. struct TxFD *tx_fd = dpriv->tx_fd + last;
  1686. skb->len = DUMMY_SKB_SIZE;
  1687. skb_copy_to_linear_data(skb, version,
  1688. strlen(version) % DUMMY_SKB_SIZE);
  1689. tx_fd->state = FrameEnd | TO_STATE_TX(DUMMY_SKB_SIZE);
  1690. tx_fd->data = cpu_to_le32(pci_map_single(dpriv->pci_priv->pdev,
  1691. skb->data, DUMMY_SKB_SIZE,
  1692. PCI_DMA_TODEVICE));
  1693. dpriv->tx_skbuff[last] = skb;
  1694. }
  1695. return skb;
  1696. }
  1697. static int dscc4_init_ring(struct net_device *dev)
  1698. {
  1699. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1700. struct pci_dev *pdev = dpriv->pci_priv->pdev;
  1701. struct TxFD *tx_fd;
  1702. struct RxFD *rx_fd;
  1703. void *ring;
  1704. int i;
  1705. ring = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &dpriv->rx_fd_dma);
  1706. if (!ring)
  1707. goto err_out;
  1708. dpriv->rx_fd = rx_fd = (struct RxFD *) ring;
  1709. ring = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &dpriv->tx_fd_dma);
  1710. if (!ring)
  1711. goto err_free_dma_rx;
  1712. dpriv->tx_fd = tx_fd = (struct TxFD *) ring;
  1713. memset(dpriv->tx_skbuff, 0, sizeof(struct sk_buff *)*TX_RING_SIZE);
  1714. dpriv->tx_dirty = 0xffffffff;
  1715. i = dpriv->tx_current = 0;
  1716. do {
  1717. tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
  1718. tx_fd->complete = 0x00000000;
  1719. /* FIXME: NULL should be ok - to be tried */
  1720. tx_fd->data = cpu_to_le32(dpriv->tx_fd_dma);
  1721. (tx_fd++)->next = cpu_to_le32(dpriv->tx_fd_dma +
  1722. (++i%TX_RING_SIZE)*sizeof(*tx_fd));
  1723. } while (i < TX_RING_SIZE);
  1724. if (!dscc4_init_dummy_skb(dpriv))
  1725. goto err_free_dma_tx;
  1726. memset(dpriv->rx_skbuff, 0, sizeof(struct sk_buff *)*RX_RING_SIZE);
  1727. i = dpriv->rx_dirty = dpriv->rx_current = 0;
  1728. do {
  1729. /* size set by the host. Multiple of 4 bytes please */
  1730. rx_fd->state1 = HiDesc;
  1731. rx_fd->state2 = 0x00000000;
  1732. rx_fd->end = cpu_to_le32(0xbabeface);
  1733. rx_fd->state1 |= TO_STATE_RX(HDLC_MAX_MRU);
  1734. // FIXME: return value verifiee mais traitement suspect
  1735. if (try_get_rx_skb(dpriv, dev) >= 0)
  1736. dpriv->rx_dirty++;
  1737. (rx_fd++)->next = cpu_to_le32(dpriv->rx_fd_dma +
  1738. (++i%RX_RING_SIZE)*sizeof(*rx_fd));
  1739. } while (i < RX_RING_SIZE);
  1740. return 0;
  1741. err_free_dma_tx:
  1742. pci_free_consistent(pdev, TX_TOTAL_SIZE, ring, dpriv->tx_fd_dma);
  1743. err_free_dma_rx:
  1744. pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
  1745. err_out:
  1746. return -ENOMEM;
  1747. }
  1748. static void __devexit dscc4_remove_one(struct pci_dev *pdev)
  1749. {
  1750. struct dscc4_pci_priv *ppriv;
  1751. struct dscc4_dev_priv *root;
  1752. void __iomem *ioaddr;
  1753. int i;
  1754. ppriv = pci_get_drvdata(pdev);
  1755. root = ppriv->root;
  1756. ioaddr = root->base_addr;
  1757. dscc4_pci_reset(pdev, ioaddr);
  1758. free_irq(pdev->irq, root);
  1759. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), ppriv->iqcfg,
  1760. ppriv->iqcfg_dma);
  1761. for (i = 0; i < dev_per_card; i++) {
  1762. struct dscc4_dev_priv *dpriv = root + i;
  1763. dscc4_release_ring(dpriv);
  1764. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  1765. dpriv->iqrx, dpriv->iqrx_dma);
  1766. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  1767. dpriv->iqtx, dpriv->iqtx_dma);
  1768. }
  1769. dscc4_free1(pdev);
  1770. iounmap(ioaddr);
  1771. pci_release_region(pdev, 1);
  1772. pci_release_region(pdev, 0);
  1773. pci_disable_device(pdev);
  1774. }
  1775. static int dscc4_hdlc_attach(struct net_device *dev, unsigned short encoding,
  1776. unsigned short parity)
  1777. {
  1778. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1779. if (encoding != ENCODING_NRZ &&
  1780. encoding != ENCODING_NRZI &&
  1781. encoding != ENCODING_FM_MARK &&
  1782. encoding != ENCODING_FM_SPACE &&
  1783. encoding != ENCODING_MANCHESTER)
  1784. return -EINVAL;
  1785. if (parity != PARITY_NONE &&
  1786. parity != PARITY_CRC16_PR0_CCITT &&
  1787. parity != PARITY_CRC16_PR1_CCITT &&
  1788. parity != PARITY_CRC32_PR0_CCITT &&
  1789. parity != PARITY_CRC32_PR1_CCITT)
  1790. return -EINVAL;
  1791. dpriv->encoding = encoding;
  1792. dpriv->parity = parity;
  1793. return 0;
  1794. }
  1795. #ifndef MODULE
  1796. static int __init dscc4_setup(char *str)
  1797. {
  1798. int *args[] = { &debug, &quartz, NULL }, **p = args;
  1799. while (*p && (get_option(&str, *p) == 2))
  1800. p++;
  1801. return 1;
  1802. }
  1803. __setup("dscc4.setup=", dscc4_setup);
  1804. #endif
  1805. static struct pci_device_id dscc4_pci_tbl[] = {
  1806. { PCI_VENDOR_ID_SIEMENS, PCI_DEVICE_ID_SIEMENS_DSCC4,
  1807. PCI_ANY_ID, PCI_ANY_ID, },
  1808. { 0,}
  1809. };
  1810. MODULE_DEVICE_TABLE(pci, dscc4_pci_tbl);
  1811. static struct pci_driver dscc4_driver = {
  1812. .name = DRV_NAME,
  1813. .id_table = dscc4_pci_tbl,
  1814. .probe = dscc4_init_one,
  1815. .remove = __devexit_p(dscc4_remove_one),
  1816. };
  1817. static int __init dscc4_init_module(void)
  1818. {
  1819. return pci_register_driver(&dscc4_driver);
  1820. }
  1821. static void __exit dscc4_cleanup_module(void)
  1822. {
  1823. pci_unregister_driver(&dscc4_driver);
  1824. }
  1825. module_init(dscc4_init_module);
  1826. module_exit(dscc4_cleanup_module);