dscc4.c 53 KB

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