dscc4.c 53 KB

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