dscc4.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /*
  2. * drivers/net/wan/dscc4/dscc4.c: a DSCC4 HDLC driver for Linux
  3. *
  4. * This software may be used and distributed according to the terms of the
  5. * GNU General Public License.
  6. *
  7. * The author may be reached as romieu@cogenit.fr.
  8. * Specific bug reports/asian food will be welcome.
  9. *
  10. * Special thanks to the nice people at CS-Telecom for the hardware and the
  11. * access to the test/measure tools.
  12. *
  13. *
  14. * Theory of Operation
  15. *
  16. * I. Board Compatibility
  17. *
  18. * This device driver is designed for the Siemens PEB20534 4 ports serial
  19. * controller as found on Etinc PCISYNC cards. The documentation for the
  20. * chipset is available at http://www.infineon.com:
  21. * - Data Sheet "DSCC4, DMA Supported Serial Communication Controller with
  22. * 4 Channels, PEB 20534 Version 2.1, PEF 20534 Version 2.1";
  23. * - Application Hint "Management of DSCC4 on-chip FIFO resources".
  24. * - Errata sheet DS5 (courtesy of Michael Skerritt).
  25. * Jens David has built an adapter based on the same chipset. Take a look
  26. * at http://www.afthd.tu-darmstadt.de/~dg1kjd/pciscc4 for a specific
  27. * driver.
  28. * Sample code (2 revisions) is available at Infineon.
  29. *
  30. * II. Board-specific settings
  31. *
  32. * Pcisync can transmit some clock signal to the outside world on the
  33. * *first two* ports provided you put a quartz and a line driver on it and
  34. * remove the jumpers. The operation is described on Etinc web site. If you
  35. * go DCE on these ports, don't forget to use an adequate cable.
  36. *
  37. * Sharing of the PCI interrupt line for this board is possible.
  38. *
  39. * III. Driver operation
  40. *
  41. * The rx/tx operations are based on a linked list of descriptors. The driver
  42. * doesn't use HOLD mode any more. HOLD mode is definitely buggy and the more
  43. * I tried to fix it, the more it started to look like (convoluted) software
  44. * mutation of LxDA method. Errata sheet DS5 suggests to use LxDA: consider
  45. * this a rfc2119 MUST.
  46. *
  47. * Tx direction
  48. * When the tx ring is full, the xmit routine issues a call to netdev_stop.
  49. * The device is supposed to be enabled again during an ALLS irq (we could
  50. * use HI but as it's easy to lose events, it's fscked).
  51. *
  52. * Rx direction
  53. * The received frames aren't supposed to span over multiple receiving areas.
  54. * I may implement it some day but it isn't the highest ranked item.
  55. *
  56. * IV. Notes
  57. * The current error (XDU, RFO) recovery code is untested.
  58. * So far, RDO takes his RX channel down and the right sequence to enable it
  59. * again is still a mistery. If RDO happens, plan a reboot. More details
  60. * in the code (NB: as this happens, TX still works).
  61. * Don't mess the cables during operation, especially on DTE ports. I don't
  62. * suggest it for DCE either but at least one can get some messages instead
  63. * of a complete instant freeze.
  64. * Tests are done on Rev. 20 of the silicium. The RDO handling changes with
  65. * the documentation/chipset releases.
  66. *
  67. * TODO:
  68. * - test X25.
  69. * - use polling at high irq/s,
  70. * - performance analysis,
  71. * - endianness.
  72. *
  73. * 2001/12/10 Daniela Squassoni <daniela@cyclades.com>
  74. * - Contribution to support the new generic HDLC layer.
  75. *
  76. * 2002/01 Ueimor
  77. * - old style interface removal
  78. * - dscc4_release_ring fix (related to DMA mapping)
  79. * - hard_start_xmit fix (hint: TxSizeMax)
  80. * - misc crapectomy.
  81. */
  82. #include <linux/module.h>
  83. #include <linux/types.h>
  84. #include <linux/errno.h>
  85. #include <linux/list.h>
  86. #include <linux/ioport.h>
  87. #include <linux/pci.h>
  88. #include <linux/kernel.h>
  89. #include <linux/mm.h>
  90. #include <asm/system.h>
  91. #include <asm/cache.h>
  92. #include <asm/byteorder.h>
  93. #include <asm/uaccess.h>
  94. #include <asm/io.h>
  95. #include <asm/irq.h>
  96. #include <linux/init.h>
  97. #include <linux/string.h>
  98. #include <linux/if_arp.h>
  99. #include <linux/netdevice.h>
  100. #include <linux/skbuff.h>
  101. #include <linux/delay.h>
  102. #include <linux/hdlc.h>
  103. #include <linux/mutex.h>
  104. /* Version */
  105. static const char version[] = "$Id: dscc4.c,v 1.173 2003/09/20 23:55:34 romieu Exp $ for Linux\n";
  106. static int debug;
  107. static int quartz;
  108. #ifdef CONFIG_DSCC4_PCI_RST
  109. static DEFINE_MUTEX(dscc4_mutex);
  110. static u32 dscc4_pci_config_store[16];
  111. #endif
  112. #define DRV_NAME "dscc4"
  113. #undef DSCC4_POLLING
  114. /* Module parameters */
  115. MODULE_AUTHOR("Maintainer: Francois Romieu <romieu@cogenit.fr>");
  116. MODULE_DESCRIPTION("Siemens PEB20534 PCI Controler");
  117. MODULE_LICENSE("GPL");
  118. module_param(debug, int, 0);
  119. MODULE_PARM_DESC(debug,"Enable/disable extra messages");
  120. module_param(quartz, int, 0);
  121. MODULE_PARM_DESC(quartz,"If present, on-board quartz frequency (Hz)");
  122. /* Structures */
  123. struct thingie {
  124. int define;
  125. u32 bits;
  126. };
  127. struct TxFD {
  128. __le32 state;
  129. __le32 next;
  130. __le32 data;
  131. __le32 complete;
  132. u32 jiffies; /* Allows sizeof(TxFD) == sizeof(RxFD) + extra hack */
  133. /* FWIW, datasheet calls that "dummy" and says that card
  134. * never looks at it; neither does the driver */
  135. };
  136. struct RxFD {
  137. __le32 state1;
  138. __le32 next;
  139. __le32 data;
  140. __le32 state2;
  141. __le32 end;
  142. };
  143. #define DUMMY_SKB_SIZE 64
  144. #define TX_LOW 8
  145. #define TX_RING_SIZE 32
  146. #define RX_RING_SIZE 32
  147. #define TX_TOTAL_SIZE TX_RING_SIZE*sizeof(struct TxFD)
  148. #define RX_TOTAL_SIZE RX_RING_SIZE*sizeof(struct RxFD)
  149. #define IRQ_RING_SIZE 64 /* Keep it a multiple of 32 */
  150. #define TX_TIMEOUT (HZ/10)
  151. #define DSCC4_HZ_MAX 33000000
  152. #define BRR_DIVIDER_MAX 64*0x00004000 /* Cf errata DS5 p.10 */
  153. #define dev_per_card 4
  154. #define SCC_REGISTERS_MAX 23 /* Cf errata DS5 p.4 */
  155. #define SOURCE_ID(flags) (((flags) >> 28) & 0x03)
  156. #define TO_SIZE(state) (((state) >> 16) & 0x1fff)
  157. /*
  158. * Given the operating range of Linux HDLC, the 2 defines below could be
  159. * made simpler. However they are a fine reminder for the limitations of
  160. * the driver: it's better to stay < TxSizeMax and < RxSizeMax.
  161. */
  162. #define TO_STATE_TX(len) cpu_to_le32(((len) & TxSizeMax) << 16)
  163. #define TO_STATE_RX(len) cpu_to_le32((RX_MAX(len) % RxSizeMax) << 16)
  164. #define RX_MAX(len) ((((len) >> 5) + 1) << 5) /* Cf RLCR */
  165. #define SCC_REG_START(dpriv) (SCC_START+(dpriv->dev_id)*SCC_OFFSET)
  166. struct dscc4_pci_priv {
  167. __le32 *iqcfg;
  168. int cfg_cur;
  169. spinlock_t lock;
  170. struct pci_dev *pdev;
  171. struct dscc4_dev_priv *root;
  172. dma_addr_t iqcfg_dma;
  173. u32 xtal_hz;
  174. };
  175. struct dscc4_dev_priv {
  176. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  177. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  178. struct RxFD *rx_fd;
  179. struct TxFD *tx_fd;
  180. __le32 *iqrx;
  181. __le32 *iqtx;
  182. /* FIXME: check all the volatile are required */
  183. volatile u32 tx_current;
  184. u32 rx_current;
  185. u32 iqtx_current;
  186. u32 iqrx_current;
  187. volatile u32 tx_dirty;
  188. volatile u32 ltda;
  189. u32 rx_dirty;
  190. u32 lrda;
  191. dma_addr_t tx_fd_dma;
  192. dma_addr_t rx_fd_dma;
  193. dma_addr_t iqtx_dma;
  194. dma_addr_t iqrx_dma;
  195. u32 scc_regs[SCC_REGISTERS_MAX]; /* Cf errata DS5 p.4 */
  196. struct timer_list timer;
  197. struct dscc4_pci_priv *pci_priv;
  198. spinlock_t lock;
  199. int dev_id;
  200. volatile u32 flags;
  201. u32 timer_help;
  202. unsigned short encoding;
  203. unsigned short parity;
  204. struct net_device *dev;
  205. sync_serial_settings settings;
  206. void __iomem *base_addr;
  207. u32 __pad __attribute__ ((aligned (4)));
  208. };
  209. /* GLOBAL registers definitions */
  210. #define GCMDR 0x00
  211. #define GSTAR 0x04
  212. #define GMODE 0x08
  213. #define IQLENR0 0x0C
  214. #define IQLENR1 0x10
  215. #define IQRX0 0x14
  216. #define IQTX0 0x24
  217. #define IQCFG 0x3c
  218. #define FIFOCR1 0x44
  219. #define FIFOCR2 0x48
  220. #define FIFOCR3 0x4c
  221. #define FIFOCR4 0x34
  222. #define CH0CFG 0x50
  223. #define CH0BRDA 0x54
  224. #define CH0BTDA 0x58
  225. #define CH0FRDA 0x98
  226. #define CH0FTDA 0xb0
  227. #define CH0LRDA 0xc8
  228. #define CH0LTDA 0xe0
  229. /* SCC registers definitions */
  230. #define SCC_START 0x0100
  231. #define SCC_OFFSET 0x80
  232. #define CMDR 0x00
  233. #define STAR 0x04
  234. #define CCR0 0x08
  235. #define CCR1 0x0c
  236. #define CCR2 0x10
  237. #define BRR 0x2C
  238. #define RLCR 0x40
  239. #define IMR 0x54
  240. #define ISR 0x58
  241. #define GPDIR 0x0400
  242. #define GPDATA 0x0404
  243. #define GPIM 0x0408
  244. /* Bit masks */
  245. #define EncodingMask 0x00700000
  246. #define CrcMask 0x00000003
  247. #define IntRxScc0 0x10000000
  248. #define IntTxScc0 0x01000000
  249. #define TxPollCmd 0x00000400
  250. #define RxActivate 0x08000000
  251. #define MTFi 0x04000000
  252. #define Rdr 0x00400000
  253. #define Rdt 0x00200000
  254. #define Idr 0x00100000
  255. #define Idt 0x00080000
  256. #define TxSccRes 0x01000000
  257. #define RxSccRes 0x00010000
  258. #define TxSizeMax 0x1fff /* Datasheet DS1 - 11.1.1.1 */
  259. #define RxSizeMax 0x1ffc /* Datasheet DS1 - 11.1.2.1 */
  260. #define Ccr0ClockMask 0x0000003f
  261. #define Ccr1LoopMask 0x00000200
  262. #define IsrMask 0x000fffff
  263. #define BrrExpMask 0x00000f00
  264. #define BrrMultMask 0x0000003f
  265. #define EncodingMask 0x00700000
  266. #define Hold cpu_to_le32(0x40000000)
  267. #define SccBusy 0x10000000
  268. #define PowerUp 0x80000000
  269. #define Vis 0x00001000
  270. #define FrameOk (FrameVfr | FrameCrc)
  271. #define FrameVfr 0x80
  272. #define FrameRdo 0x40
  273. #define FrameCrc 0x20
  274. #define FrameRab 0x10
  275. #define FrameAborted cpu_to_le32(0x00000200)
  276. #define FrameEnd cpu_to_le32(0x80000000)
  277. #define DataComplete cpu_to_le32(0x40000000)
  278. #define LengthCheck 0x00008000
  279. #define SccEvt 0x02000000
  280. #define NoAck 0x00000200
  281. #define Action 0x00000001
  282. #define HiDesc cpu_to_le32(0x20000000)
  283. /* SCC events */
  284. #define RxEvt 0xf0000000
  285. #define TxEvt 0x0f000000
  286. #define Alls 0x00040000
  287. #define Xdu 0x00010000
  288. #define Cts 0x00004000
  289. #define Xmr 0x00002000
  290. #define Xpr 0x00001000
  291. #define Rdo 0x00000080
  292. #define Rfs 0x00000040
  293. #define Cd 0x00000004
  294. #define Rfo 0x00000002
  295. #define Flex 0x00000001
  296. /* DMA core events */
  297. #define Cfg 0x00200000
  298. #define Hi 0x00040000
  299. #define Fi 0x00020000
  300. #define Err 0x00010000
  301. #define Arf 0x00000002
  302. #define ArAck 0x00000001
  303. /* State flags */
  304. #define Ready 0x00000000
  305. #define NeedIDR 0x00000001
  306. #define NeedIDT 0x00000002
  307. #define RdoSet 0x00000004
  308. #define FakeReset 0x00000008
  309. /* Don't mask RDO. Ever. */
  310. #ifdef DSCC4_POLLING
  311. #define EventsMask 0xfffeef7f
  312. #else
  313. #define EventsMask 0xfffa8f7a
  314. #endif
  315. /* Functions prototypes */
  316. static void dscc4_rx_irq(struct dscc4_pci_priv *, struct dscc4_dev_priv *);
  317. static void dscc4_tx_irq(struct dscc4_pci_priv *, struct dscc4_dev_priv *);
  318. static int dscc4_found1(struct pci_dev *, void __iomem *ioaddr);
  319. static int dscc4_init_one(struct pci_dev *, const struct pci_device_id *ent);
  320. static int dscc4_open(struct net_device *);
  321. static int dscc4_start_xmit(struct sk_buff *, struct net_device *);
  322. static int dscc4_close(struct net_device *);
  323. static int dscc4_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  324. static int dscc4_init_ring(struct net_device *);
  325. static void dscc4_release_ring(struct dscc4_dev_priv *);
  326. static void dscc4_timer(unsigned long);
  327. static void dscc4_tx_timeout(struct net_device *);
  328. static irqreturn_t dscc4_irq(int irq, void *dev_id);
  329. static int dscc4_hdlc_attach(struct net_device *, unsigned short, unsigned short);
  330. static int dscc4_set_iface(struct dscc4_dev_priv *, struct net_device *);
  331. #ifdef DSCC4_POLLING
  332. static int dscc4_tx_poll(struct dscc4_dev_priv *, struct net_device *);
  333. #endif
  334. static inline struct dscc4_dev_priv *dscc4_priv(struct net_device *dev)
  335. {
  336. return dev_to_hdlc(dev)->priv;
  337. }
  338. static inline struct net_device *dscc4_to_dev(struct dscc4_dev_priv *p)
  339. {
  340. return p->dev;
  341. }
  342. static void scc_patchl(u32 mask, u32 value, struct dscc4_dev_priv *dpriv,
  343. struct net_device *dev, int offset)
  344. {
  345. u32 state;
  346. /* Cf scc_writel for concern regarding thread-safety */
  347. state = dpriv->scc_regs[offset >> 2];
  348. state &= ~mask;
  349. state |= value;
  350. dpriv->scc_regs[offset >> 2] = state;
  351. writel(state, dpriv->base_addr + SCC_REG_START(dpriv) + offset);
  352. }
  353. static void scc_writel(u32 bits, struct dscc4_dev_priv *dpriv,
  354. struct net_device *dev, int offset)
  355. {
  356. /*
  357. * Thread-UNsafe.
  358. * As of 2002/02/16, there are no thread racing for access.
  359. */
  360. dpriv->scc_regs[offset >> 2] = bits;
  361. writel(bits, dpriv->base_addr + SCC_REG_START(dpriv) + offset);
  362. }
  363. static inline u32 scc_readl(struct dscc4_dev_priv *dpriv, int offset)
  364. {
  365. return dpriv->scc_regs[offset >> 2];
  366. }
  367. static u32 scc_readl_star(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  368. {
  369. /* Cf errata DS5 p.4 */
  370. readl(dpriv->base_addr + SCC_REG_START(dpriv) + STAR);
  371. return readl(dpriv->base_addr + SCC_REG_START(dpriv) + STAR);
  372. }
  373. static inline void dscc4_do_tx(struct dscc4_dev_priv *dpriv,
  374. struct net_device *dev)
  375. {
  376. dpriv->ltda = dpriv->tx_fd_dma +
  377. ((dpriv->tx_current-1)%TX_RING_SIZE)*sizeof(struct TxFD);
  378. writel(dpriv->ltda, dpriv->base_addr + CH0LTDA + dpriv->dev_id*4);
  379. /* Flush posted writes *NOW* */
  380. readl(dpriv->base_addr + CH0LTDA + dpriv->dev_id*4);
  381. }
  382. static inline void dscc4_rx_update(struct dscc4_dev_priv *dpriv,
  383. struct net_device *dev)
  384. {
  385. dpriv->lrda = dpriv->rx_fd_dma +
  386. ((dpriv->rx_dirty - 1)%RX_RING_SIZE)*sizeof(struct RxFD);
  387. writel(dpriv->lrda, dpriv->base_addr + CH0LRDA + dpriv->dev_id*4);
  388. }
  389. static inline unsigned int dscc4_tx_done(struct dscc4_dev_priv *dpriv)
  390. {
  391. return dpriv->tx_current == dpriv->tx_dirty;
  392. }
  393. static inline unsigned int dscc4_tx_quiescent(struct dscc4_dev_priv *dpriv,
  394. struct net_device *dev)
  395. {
  396. return readl(dpriv->base_addr + CH0FTDA + dpriv->dev_id*4) == dpriv->ltda;
  397. }
  398. static int state_check(u32 state, struct dscc4_dev_priv *dpriv,
  399. struct net_device *dev, const char *msg)
  400. {
  401. int ret = 0;
  402. if (debug > 1) {
  403. if (SOURCE_ID(state) != dpriv->dev_id) {
  404. printk(KERN_DEBUG "%s (%s): Source Id=%d, state=%08x\n",
  405. dev->name, msg, SOURCE_ID(state), state );
  406. ret = -1;
  407. }
  408. if (state & 0x0df80c00) {
  409. printk(KERN_DEBUG "%s (%s): state=%08x (UFO alert)\n",
  410. dev->name, msg, state);
  411. ret = -1;
  412. }
  413. }
  414. return ret;
  415. }
  416. static void dscc4_tx_print(struct net_device *dev,
  417. struct dscc4_dev_priv *dpriv,
  418. char *msg)
  419. {
  420. printk(KERN_DEBUG "%s: tx_current=%02d tx_dirty=%02d (%s)\n",
  421. dev->name, dpriv->tx_current, dpriv->tx_dirty, msg);
  422. }
  423. static void dscc4_release_ring(struct dscc4_dev_priv *dpriv)
  424. {
  425. struct pci_dev *pdev = dpriv->pci_priv->pdev;
  426. struct TxFD *tx_fd = dpriv->tx_fd;
  427. struct RxFD *rx_fd = dpriv->rx_fd;
  428. struct sk_buff **skbuff;
  429. int i;
  430. pci_free_consistent(pdev, TX_TOTAL_SIZE, tx_fd, dpriv->tx_fd_dma);
  431. pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
  432. skbuff = dpriv->tx_skbuff;
  433. for (i = 0; i < TX_RING_SIZE; i++) {
  434. if (*skbuff) {
  435. pci_unmap_single(pdev, le32_to_cpu(tx_fd->data),
  436. (*skbuff)->len, PCI_DMA_TODEVICE);
  437. dev_kfree_skb(*skbuff);
  438. }
  439. skbuff++;
  440. tx_fd++;
  441. }
  442. skbuff = dpriv->rx_skbuff;
  443. for (i = 0; i < RX_RING_SIZE; i++) {
  444. if (*skbuff) {
  445. pci_unmap_single(pdev, le32_to_cpu(rx_fd->data),
  446. RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
  447. dev_kfree_skb(*skbuff);
  448. }
  449. skbuff++;
  450. rx_fd++;
  451. }
  452. }
  453. static inline int try_get_rx_skb(struct dscc4_dev_priv *dpriv,
  454. struct net_device *dev)
  455. {
  456. unsigned int dirty = dpriv->rx_dirty%RX_RING_SIZE;
  457. struct RxFD *rx_fd = dpriv->rx_fd + dirty;
  458. const int len = RX_MAX(HDLC_MAX_MRU);
  459. struct sk_buff *skb;
  460. int ret = 0;
  461. skb = dev_alloc_skb(len);
  462. dpriv->rx_skbuff[dirty] = skb;
  463. if (skb) {
  464. skb->protocol = hdlc_type_trans(skb, dev);
  465. rx_fd->data = cpu_to_le32(pci_map_single(dpriv->pci_priv->pdev,
  466. skb->data, len, PCI_DMA_FROMDEVICE));
  467. } else {
  468. rx_fd->data = 0;
  469. ret = -1;
  470. }
  471. return ret;
  472. }
  473. /*
  474. * IRQ/thread/whatever safe
  475. */
  476. static int dscc4_wait_ack_cec(struct dscc4_dev_priv *dpriv,
  477. struct net_device *dev, char *msg)
  478. {
  479. s8 i = 0;
  480. do {
  481. if (!(scc_readl_star(dpriv, dev) & SccBusy)) {
  482. printk(KERN_DEBUG "%s: %s ack (%d try)\n", dev->name,
  483. msg, i);
  484. goto done;
  485. }
  486. schedule_timeout_uninterruptible(10);
  487. rmb();
  488. } while (++i > 0);
  489. printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
  490. done:
  491. return (i >= 0) ? i : -EAGAIN;
  492. }
  493. static int dscc4_do_action(struct net_device *dev, char *msg)
  494. {
  495. void __iomem *ioaddr = dscc4_priv(dev)->base_addr;
  496. s16 i = 0;
  497. writel(Action, ioaddr + GCMDR);
  498. ioaddr += GSTAR;
  499. do {
  500. u32 state = readl(ioaddr);
  501. if (state & ArAck) {
  502. printk(KERN_DEBUG "%s: %s ack\n", dev->name, msg);
  503. writel(ArAck, ioaddr);
  504. goto done;
  505. } else if (state & Arf) {
  506. printk(KERN_ERR "%s: %s failed\n", dev->name, msg);
  507. writel(Arf, ioaddr);
  508. i = -1;
  509. goto done;
  510. }
  511. rmb();
  512. } while (++i > 0);
  513. printk(KERN_ERR "%s: %s timeout\n", dev->name, msg);
  514. done:
  515. return i;
  516. }
  517. static inline int dscc4_xpr_ack(struct dscc4_dev_priv *dpriv)
  518. {
  519. int cur = dpriv->iqtx_current%IRQ_RING_SIZE;
  520. s8 i = 0;
  521. do {
  522. if (!(dpriv->flags & (NeedIDR | NeedIDT)) ||
  523. (dpriv->iqtx[cur] & cpu_to_le32(Xpr)))
  524. break;
  525. smp_rmb();
  526. schedule_timeout_uninterruptible(10);
  527. } while (++i > 0);
  528. return (i >= 0 ) ? i : -EAGAIN;
  529. }
  530. #if 0 /* dscc4_{rx/tx}_reset are both unreliable - more tweak needed */
  531. static void dscc4_rx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  532. {
  533. unsigned long flags;
  534. spin_lock_irqsave(&dpriv->pci_priv->lock, flags);
  535. /* Cf errata DS5 p.6 */
  536. writel(0x00000000, dpriv->base_addr + CH0LRDA + dpriv->dev_id*4);
  537. scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
  538. readl(dpriv->base_addr + CH0LRDA + dpriv->dev_id*4);
  539. writel(MTFi|Rdr, dpriv->base_addr + dpriv->dev_id*0x0c + CH0CFG);
  540. writel(Action, dpriv->base_addr + GCMDR);
  541. spin_unlock_irqrestore(&dpriv->pci_priv->lock, flags);
  542. }
  543. #endif
  544. #if 0
  545. static void dscc4_tx_reset(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  546. {
  547. u16 i = 0;
  548. /* Cf errata DS5 p.7 */
  549. scc_patchl(PowerUp, 0, dpriv, dev, CCR0);
  550. scc_writel(0x00050000, dpriv, dev, CCR2);
  551. /*
  552. * Must be longer than the time required to fill the fifo.
  553. */
  554. while (!dscc4_tx_quiescent(dpriv, dev) && ++i) {
  555. udelay(1);
  556. wmb();
  557. }
  558. writel(MTFi|Rdt, dpriv->base_addr + dpriv->dev_id*0x0c + CH0CFG);
  559. if (dscc4_do_action(dev, "Rdt") < 0)
  560. printk(KERN_ERR "%s: Tx reset failed\n", dev->name);
  561. }
  562. #endif
  563. /* TODO: (ab)use this function to refill a completely depleted RX ring. */
  564. static inline void dscc4_rx_skb(struct dscc4_dev_priv *dpriv,
  565. struct net_device *dev)
  566. {
  567. struct RxFD *rx_fd = dpriv->rx_fd + dpriv->rx_current%RX_RING_SIZE;
  568. struct pci_dev *pdev = dpriv->pci_priv->pdev;
  569. struct sk_buff *skb;
  570. int pkt_len;
  571. skb = dpriv->rx_skbuff[dpriv->rx_current++%RX_RING_SIZE];
  572. if (!skb) {
  573. printk(KERN_DEBUG "%s: skb=0 (%s)\n", dev->name, __func__);
  574. goto refill;
  575. }
  576. pkt_len = TO_SIZE(le32_to_cpu(rx_fd->state2));
  577. pci_unmap_single(pdev, le32_to_cpu(rx_fd->data),
  578. RX_MAX(HDLC_MAX_MRU), PCI_DMA_FROMDEVICE);
  579. if ((skb->data[--pkt_len] & FrameOk) == FrameOk) {
  580. dev->stats.rx_packets++;
  581. dev->stats.rx_bytes += pkt_len;
  582. skb_put(skb, pkt_len);
  583. if (netif_running(dev))
  584. skb->protocol = hdlc_type_trans(skb, dev);
  585. skb->dev->last_rx = jiffies;
  586. netif_rx(skb);
  587. } else {
  588. if (skb->data[pkt_len] & FrameRdo)
  589. dev->stats.rx_fifo_errors++;
  590. else if (!(skb->data[pkt_len] | ~FrameCrc))
  591. dev->stats.rx_crc_errors++;
  592. else if (!(skb->data[pkt_len] | ~(FrameVfr | FrameRab)))
  593. dev->stats.rx_length_errors++;
  594. else
  595. dev->stats.rx_errors++;
  596. dev_kfree_skb_irq(skb);
  597. }
  598. refill:
  599. while ((dpriv->rx_dirty - dpriv->rx_current) % RX_RING_SIZE) {
  600. if (try_get_rx_skb(dpriv, dev) < 0)
  601. break;
  602. dpriv->rx_dirty++;
  603. }
  604. dscc4_rx_update(dpriv, dev);
  605. rx_fd->state2 = 0x00000000;
  606. rx_fd->end = cpu_to_le32(0xbabeface);
  607. }
  608. static void dscc4_free1(struct pci_dev *pdev)
  609. {
  610. struct dscc4_pci_priv *ppriv;
  611. struct dscc4_dev_priv *root;
  612. int i;
  613. ppriv = pci_get_drvdata(pdev);
  614. root = ppriv->root;
  615. for (i = 0; i < dev_per_card; i++)
  616. unregister_hdlc_device(dscc4_to_dev(root + i));
  617. pci_set_drvdata(pdev, NULL);
  618. for (i = 0; i < dev_per_card; i++)
  619. free_netdev(root[i].dev);
  620. kfree(root);
  621. kfree(ppriv);
  622. }
  623. static int __devinit dscc4_init_one(struct pci_dev *pdev,
  624. const struct pci_device_id *ent)
  625. {
  626. struct dscc4_pci_priv *priv;
  627. struct dscc4_dev_priv *dpriv;
  628. void __iomem *ioaddr;
  629. int i, rc;
  630. printk(KERN_DEBUG "%s", version);
  631. rc = pci_enable_device(pdev);
  632. if (rc < 0)
  633. goto out;
  634. rc = pci_request_region(pdev, 0, "registers");
  635. if (rc < 0) {
  636. printk(KERN_ERR "%s: can't reserve MMIO region (regs)\n",
  637. DRV_NAME);
  638. goto err_disable_0;
  639. }
  640. rc = pci_request_region(pdev, 1, "LBI interface");
  641. if (rc < 0) {
  642. printk(KERN_ERR "%s: can't reserve MMIO region (lbi)\n",
  643. DRV_NAME);
  644. goto err_free_mmio_region_1;
  645. }
  646. ioaddr = ioremap(pci_resource_start(pdev, 0),
  647. pci_resource_len(pdev, 0));
  648. if (!ioaddr) {
  649. printk(KERN_ERR "%s: cannot remap MMIO region %llx @ %llx\n",
  650. DRV_NAME, (unsigned long long)pci_resource_len(pdev, 0),
  651. (unsigned long long)pci_resource_start(pdev, 0));
  652. rc = -EIO;
  653. goto err_free_mmio_regions_2;
  654. }
  655. printk(KERN_DEBUG "Siemens DSCC4, MMIO at %#llx (regs), %#llx (lbi), IRQ %d\n",
  656. (unsigned long long)pci_resource_start(pdev, 0),
  657. (unsigned long long)pci_resource_start(pdev, 1), pdev->irq);
  658. /* Cf errata DS5 p.2 */
  659. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xf8);
  660. pci_set_master(pdev);
  661. rc = dscc4_found1(pdev, ioaddr);
  662. if (rc < 0)
  663. goto err_iounmap_3;
  664. priv = pci_get_drvdata(pdev);
  665. rc = request_irq(pdev->irq, dscc4_irq, IRQF_SHARED, DRV_NAME, priv->root);
  666. if (rc < 0) {
  667. printk(KERN_WARNING "%s: IRQ %d busy\n", DRV_NAME, pdev->irq);
  668. goto err_release_4;
  669. }
  670. /* power up/little endian/dma core controlled via lrda/ltda */
  671. writel(0x00000001, ioaddr + GMODE);
  672. /* Shared interrupt queue */
  673. {
  674. u32 bits;
  675. bits = (IRQ_RING_SIZE >> 5) - 1;
  676. bits |= bits << 4;
  677. bits |= bits << 8;
  678. bits |= bits << 16;
  679. writel(bits, ioaddr + IQLENR0);
  680. }
  681. /* Global interrupt queue */
  682. writel((u32)(((IRQ_RING_SIZE >> 5) - 1) << 20), ioaddr + IQLENR1);
  683. priv->iqcfg = (__le32 *) pci_alloc_consistent(pdev,
  684. IRQ_RING_SIZE*sizeof(__le32), &priv->iqcfg_dma);
  685. if (!priv->iqcfg)
  686. goto err_free_irq_5;
  687. writel(priv->iqcfg_dma, ioaddr + IQCFG);
  688. rc = -ENOMEM;
  689. /*
  690. * SCC 0-3 private rx/tx irq structures
  691. * IQRX/TXi needs to be set soon. Learned it the hard way...
  692. */
  693. for (i = 0; i < dev_per_card; i++) {
  694. dpriv = priv->root + i;
  695. dpriv->iqtx = (__le32 *) pci_alloc_consistent(pdev,
  696. IRQ_RING_SIZE*sizeof(u32), &dpriv->iqtx_dma);
  697. if (!dpriv->iqtx)
  698. goto err_free_iqtx_6;
  699. writel(dpriv->iqtx_dma, ioaddr + IQTX0 + i*4);
  700. }
  701. for (i = 0; i < dev_per_card; i++) {
  702. dpriv = priv->root + i;
  703. dpriv->iqrx = (__le32 *) pci_alloc_consistent(pdev,
  704. IRQ_RING_SIZE*sizeof(u32), &dpriv->iqrx_dma);
  705. if (!dpriv->iqrx)
  706. goto err_free_iqrx_7;
  707. writel(dpriv->iqrx_dma, ioaddr + IQRX0 + i*4);
  708. }
  709. /* Cf application hint. Beware of hard-lock condition on threshold. */
  710. writel(0x42104000, ioaddr + FIFOCR1);
  711. //writel(0x9ce69800, ioaddr + FIFOCR2);
  712. writel(0xdef6d800, ioaddr + FIFOCR2);
  713. //writel(0x11111111, ioaddr + FIFOCR4);
  714. writel(0x18181818, ioaddr + FIFOCR4);
  715. // FIXME: should depend on the chipset revision
  716. writel(0x0000000e, ioaddr + FIFOCR3);
  717. writel(0xff200001, ioaddr + GCMDR);
  718. rc = 0;
  719. out:
  720. return rc;
  721. err_free_iqrx_7:
  722. while (--i >= 0) {
  723. dpriv = priv->root + i;
  724. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  725. dpriv->iqrx, dpriv->iqrx_dma);
  726. }
  727. i = dev_per_card;
  728. err_free_iqtx_6:
  729. while (--i >= 0) {
  730. dpriv = priv->root + i;
  731. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  732. dpriv->iqtx, dpriv->iqtx_dma);
  733. }
  734. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), priv->iqcfg,
  735. priv->iqcfg_dma);
  736. err_free_irq_5:
  737. free_irq(pdev->irq, priv->root);
  738. err_release_4:
  739. dscc4_free1(pdev);
  740. err_iounmap_3:
  741. iounmap (ioaddr);
  742. err_free_mmio_regions_2:
  743. pci_release_region(pdev, 1);
  744. err_free_mmio_region_1:
  745. pci_release_region(pdev, 0);
  746. err_disable_0:
  747. pci_disable_device(pdev);
  748. goto out;
  749. };
  750. /*
  751. * Let's hope the default values are decent enough to protect my
  752. * feet from the user's gun - Ueimor
  753. */
  754. static void dscc4_init_registers(struct dscc4_dev_priv *dpriv,
  755. struct net_device *dev)
  756. {
  757. /* No interrupts, SCC core disabled. Let's relax */
  758. scc_writel(0x00000000, dpriv, dev, CCR0);
  759. scc_writel(LengthCheck | (HDLC_MAX_MRU >> 5), dpriv, dev, RLCR);
  760. /*
  761. * No address recognition/crc-CCITT/cts enabled
  762. * Shared flags transmission disabled - cf errata DS5 p.11
  763. * Carrier detect disabled - cf errata p.14
  764. * FIXME: carrier detection/polarity may be handled more gracefully.
  765. */
  766. scc_writel(0x02408000, dpriv, dev, CCR1);
  767. /* crc not forwarded - Cf errata DS5 p.11 */
  768. scc_writel(0x00050008 & ~RxActivate, dpriv, dev, CCR2);
  769. // crc forwarded
  770. //scc_writel(0x00250008 & ~RxActivate, dpriv, dev, CCR2);
  771. }
  772. static inline int dscc4_set_quartz(struct dscc4_dev_priv *dpriv, int hz)
  773. {
  774. int ret = 0;
  775. if ((hz < 0) || (hz > DSCC4_HZ_MAX))
  776. ret = -EOPNOTSUPP;
  777. else
  778. dpriv->pci_priv->xtal_hz = hz;
  779. return ret;
  780. }
  781. static int dscc4_found1(struct pci_dev *pdev, void __iomem *ioaddr)
  782. {
  783. struct dscc4_pci_priv *ppriv;
  784. struct dscc4_dev_priv *root;
  785. int i, ret = -ENOMEM;
  786. root = kcalloc(dev_per_card, sizeof(*root), GFP_KERNEL);
  787. if (!root) {
  788. printk(KERN_ERR "%s: can't allocate data\n", DRV_NAME);
  789. goto err_out;
  790. }
  791. for (i = 0; i < dev_per_card; i++) {
  792. root[i].dev = alloc_hdlcdev(root + i);
  793. if (!root[i].dev)
  794. goto err_free_dev;
  795. }
  796. ppriv = kzalloc(sizeof(*ppriv), GFP_KERNEL);
  797. if (!ppriv) {
  798. printk(KERN_ERR "%s: can't allocate private data\n", DRV_NAME);
  799. goto err_free_dev;
  800. }
  801. ppriv->root = root;
  802. spin_lock_init(&ppriv->lock);
  803. for (i = 0; i < dev_per_card; i++) {
  804. struct dscc4_dev_priv *dpriv = root + i;
  805. struct net_device *d = dscc4_to_dev(dpriv);
  806. hdlc_device *hdlc = dev_to_hdlc(d);
  807. d->base_addr = (unsigned long)ioaddr;
  808. d->init = NULL;
  809. d->irq = pdev->irq;
  810. d->open = dscc4_open;
  811. d->stop = dscc4_close;
  812. d->set_multicast_list = NULL;
  813. d->do_ioctl = dscc4_ioctl;
  814. d->tx_timeout = dscc4_tx_timeout;
  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. printk(KERN_ERR "%s: unable to register\n", DRV_NAME);
  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. printk(KERN_INFO "%s: loopback requires clock\n", dev->name);
  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) || !dev->hard_start_xmit)
  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 dependant 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. printk(KERN_INFO "%s: up again.\n", dev->name);
  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. printk(KERN_ERR "%s busy. Try later\n", dev->name);
  951. ret = -EAGAIN;
  952. goto err_out;
  953. } else
  954. printk(KERN_INFO "%s: available. Good\n", dev->name);
  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. printk(KERN_ERR "%s: %s timeout\n", DRV_NAME, "XPR");
  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 int dscc4_start_xmit(struct sk_buff *skb, struct net_device *dev)
  997. {
  998. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  999. struct dscc4_pci_priv *ppriv = dpriv->pci_priv;
  1000. struct TxFD *tx_fd;
  1001. int next;
  1002. next = dpriv->tx_current%TX_RING_SIZE;
  1003. dpriv->tx_skbuff[next] = skb;
  1004. tx_fd = dpriv->tx_fd + next;
  1005. tx_fd->state = FrameEnd | TO_STATE_TX(skb->len);
  1006. tx_fd->data = cpu_to_le32(pci_map_single(ppriv->pdev, skb->data, skb->len,
  1007. PCI_DMA_TODEVICE));
  1008. tx_fd->complete = 0x00000000;
  1009. tx_fd->jiffies = jiffies;
  1010. mb();
  1011. #ifdef DSCC4_POLLING
  1012. spin_lock(&dpriv->lock);
  1013. while (dscc4_tx_poll(dpriv, dev));
  1014. spin_unlock(&dpriv->lock);
  1015. #endif
  1016. dev->trans_start = jiffies;
  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 0;
  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 lazyness.
  1062. *
  1063. * The clock signals wiring can be (is ?) manufacturer dependant. 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. printk(KERN_INFO "%s: please reset the device"
  1165. " before this command\n", dev->name);
  1166. return -EPERM;
  1167. }
  1168. if (copy_from_user(&dpriv->settings, line, size))
  1169. return -EFAULT;
  1170. ret = dscc4_set_iface(dpriv, dev);
  1171. break;
  1172. default:
  1173. ret = hdlc_ioctl(dev, ifr, cmd);
  1174. break;
  1175. }
  1176. return ret;
  1177. }
  1178. static int dscc4_match(struct thingie *p, int value)
  1179. {
  1180. int i;
  1181. for (i = 0; p[i].define != -1; i++) {
  1182. if (value == p[i].define)
  1183. break;
  1184. }
  1185. if (p[i].define == -1)
  1186. return -1;
  1187. else
  1188. return i;
  1189. }
  1190. static int dscc4_clock_setting(struct dscc4_dev_priv *dpriv,
  1191. struct net_device *dev)
  1192. {
  1193. sync_serial_settings *settings = &dpriv->settings;
  1194. int ret = -EOPNOTSUPP;
  1195. u32 bps, state;
  1196. bps = settings->clock_rate;
  1197. state = scc_readl(dpriv, CCR0);
  1198. if (dscc4_set_clock(dev, &bps, &state) < 0)
  1199. goto done;
  1200. if (bps) { /* DCE */
  1201. printk(KERN_DEBUG "%s: generated RxClk (DCE)\n", dev->name);
  1202. if (settings->clock_rate != bps) {
  1203. printk(KERN_DEBUG "%s: clock adjusted (%08d -> %08d)\n",
  1204. dev->name, settings->clock_rate, bps);
  1205. settings->clock_rate = bps;
  1206. }
  1207. } else { /* DTE */
  1208. state |= PowerUp | Vis;
  1209. printk(KERN_DEBUG "%s: external RxClk (DTE)\n", dev->name);
  1210. }
  1211. scc_writel(state, dpriv, dev, CCR0);
  1212. ret = 0;
  1213. done:
  1214. return ret;
  1215. }
  1216. static int dscc4_encoding_setting(struct dscc4_dev_priv *dpriv,
  1217. struct net_device *dev)
  1218. {
  1219. struct thingie encoding[] = {
  1220. { ENCODING_NRZ, 0x00000000 },
  1221. { ENCODING_NRZI, 0x00200000 },
  1222. { ENCODING_FM_MARK, 0x00400000 },
  1223. { ENCODING_FM_SPACE, 0x00500000 },
  1224. { ENCODING_MANCHESTER, 0x00600000 },
  1225. { -1, 0}
  1226. };
  1227. int i, ret = 0;
  1228. i = dscc4_match(encoding, dpriv->encoding);
  1229. if (i >= 0)
  1230. scc_patchl(EncodingMask, encoding[i].bits, dpriv, dev, CCR0);
  1231. else
  1232. ret = -EOPNOTSUPP;
  1233. return ret;
  1234. }
  1235. static int dscc4_loopback_setting(struct dscc4_dev_priv *dpriv,
  1236. struct net_device *dev)
  1237. {
  1238. sync_serial_settings *settings = &dpriv->settings;
  1239. u32 state;
  1240. state = scc_readl(dpriv, CCR1);
  1241. if (settings->loopback) {
  1242. printk(KERN_DEBUG "%s: loopback\n", dev->name);
  1243. state |= 0x00000100;
  1244. } else {
  1245. printk(KERN_DEBUG "%s: normal\n", dev->name);
  1246. state &= ~0x00000100;
  1247. }
  1248. scc_writel(state, dpriv, dev, CCR1);
  1249. return 0;
  1250. }
  1251. static int dscc4_crc_setting(struct dscc4_dev_priv *dpriv,
  1252. struct net_device *dev)
  1253. {
  1254. struct thingie crc[] = {
  1255. { PARITY_CRC16_PR0_CCITT, 0x00000010 },
  1256. { PARITY_CRC16_PR1_CCITT, 0x00000000 },
  1257. { PARITY_CRC32_PR0_CCITT, 0x00000011 },
  1258. { PARITY_CRC32_PR1_CCITT, 0x00000001 }
  1259. };
  1260. int i, ret = 0;
  1261. i = dscc4_match(crc, dpriv->parity);
  1262. if (i >= 0)
  1263. scc_patchl(CrcMask, crc[i].bits, dpriv, dev, CCR1);
  1264. else
  1265. ret = -EOPNOTSUPP;
  1266. return ret;
  1267. }
  1268. static int dscc4_set_iface(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  1269. {
  1270. struct {
  1271. int (*action)(struct dscc4_dev_priv *, struct net_device *);
  1272. } *p, do_setting[] = {
  1273. { dscc4_encoding_setting },
  1274. { dscc4_clock_setting },
  1275. { dscc4_loopback_setting },
  1276. { dscc4_crc_setting },
  1277. { NULL }
  1278. };
  1279. int ret = 0;
  1280. for (p = do_setting; p->action; p++) {
  1281. if ((ret = p->action(dpriv, dev)) < 0)
  1282. break;
  1283. }
  1284. return ret;
  1285. }
  1286. static irqreturn_t dscc4_irq(int irq, void *token)
  1287. {
  1288. struct dscc4_dev_priv *root = token;
  1289. struct dscc4_pci_priv *priv;
  1290. struct net_device *dev;
  1291. void __iomem *ioaddr;
  1292. u32 state;
  1293. unsigned long flags;
  1294. int i, handled = 1;
  1295. priv = root->pci_priv;
  1296. dev = dscc4_to_dev(root);
  1297. spin_lock_irqsave(&priv->lock, flags);
  1298. ioaddr = root->base_addr;
  1299. state = readl(ioaddr + GSTAR);
  1300. if (!state) {
  1301. handled = 0;
  1302. goto out;
  1303. }
  1304. if (debug > 3)
  1305. printk(KERN_DEBUG "%s: GSTAR = 0x%08x\n", DRV_NAME, state);
  1306. writel(state, ioaddr + GSTAR);
  1307. if (state & Arf) {
  1308. printk(KERN_ERR "%s: failure (Arf). Harass the maintener\n",
  1309. dev->name);
  1310. goto out;
  1311. }
  1312. state &= ~ArAck;
  1313. if (state & Cfg) {
  1314. if (debug > 0)
  1315. printk(KERN_DEBUG "%s: CfgIV\n", DRV_NAME);
  1316. if (priv->iqcfg[priv->cfg_cur++%IRQ_RING_SIZE] & cpu_to_le32(Arf))
  1317. printk(KERN_ERR "%s: %s failed\n", dev->name, "CFG");
  1318. if (!(state &= ~Cfg))
  1319. goto out;
  1320. }
  1321. if (state & RxEvt) {
  1322. i = dev_per_card - 1;
  1323. do {
  1324. dscc4_rx_irq(priv, root + i);
  1325. } while (--i >= 0);
  1326. state &= ~RxEvt;
  1327. }
  1328. if (state & TxEvt) {
  1329. i = dev_per_card - 1;
  1330. do {
  1331. dscc4_tx_irq(priv, root + i);
  1332. } while (--i >= 0);
  1333. state &= ~TxEvt;
  1334. }
  1335. out:
  1336. spin_unlock_irqrestore(&priv->lock, flags);
  1337. return IRQ_RETVAL(handled);
  1338. }
  1339. static void dscc4_tx_irq(struct dscc4_pci_priv *ppriv,
  1340. struct dscc4_dev_priv *dpriv)
  1341. {
  1342. struct net_device *dev = dscc4_to_dev(dpriv);
  1343. u32 state;
  1344. int cur, loop = 0;
  1345. try:
  1346. cur = dpriv->iqtx_current%IRQ_RING_SIZE;
  1347. state = le32_to_cpu(dpriv->iqtx[cur]);
  1348. if (!state) {
  1349. if (debug > 4)
  1350. printk(KERN_DEBUG "%s: Tx ISR = 0x%08x\n", dev->name,
  1351. state);
  1352. if ((debug > 1) && (loop > 1))
  1353. printk(KERN_DEBUG "%s: Tx irq loop=%d\n", dev->name, loop);
  1354. if (loop && netif_queue_stopped(dev))
  1355. if ((dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE)
  1356. netif_wake_queue(dev);
  1357. if (netif_running(dev) && dscc4_tx_quiescent(dpriv, dev) &&
  1358. !dscc4_tx_done(dpriv))
  1359. dscc4_do_tx(dpriv, dev);
  1360. return;
  1361. }
  1362. loop++;
  1363. dpriv->iqtx[cur] = 0;
  1364. dpriv->iqtx_current++;
  1365. if (state_check(state, dpriv, dev, "Tx") < 0)
  1366. return;
  1367. if (state & SccEvt) {
  1368. if (state & Alls) {
  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, le32_to_cpu(tx_fd->data),
  1382. skb->len, PCI_DMA_TODEVICE);
  1383. if (tx_fd->state & FrameEnd) {
  1384. dev->stats.tx_packets++;
  1385. dev->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. dev->stats.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 = le32_to_cpu(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 = cpu_to_le32(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. dev->stats.rx_over_errors++;
  1621. rx_fd->state1 |= Hold;
  1622. rx_fd->state2 = 0x00000000;
  1623. rx_fd->end = cpu_to_le32(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 = cpu_to_le32(pci_map_single(dpriv->pci_priv->pdev,
  1690. skb->data, DUMMY_SKB_SIZE,
  1691. PCI_DMA_TODEVICE));
  1692. dpriv->tx_skbuff[last] = skb;
  1693. }
  1694. return skb;
  1695. }
  1696. static int dscc4_init_ring(struct net_device *dev)
  1697. {
  1698. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1699. struct pci_dev *pdev = dpriv->pci_priv->pdev;
  1700. struct TxFD *tx_fd;
  1701. struct RxFD *rx_fd;
  1702. void *ring;
  1703. int i;
  1704. ring = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &dpriv->rx_fd_dma);
  1705. if (!ring)
  1706. goto err_out;
  1707. dpriv->rx_fd = rx_fd = (struct RxFD *) ring;
  1708. ring = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &dpriv->tx_fd_dma);
  1709. if (!ring)
  1710. goto err_free_dma_rx;
  1711. dpriv->tx_fd = tx_fd = (struct TxFD *) ring;
  1712. memset(dpriv->tx_skbuff, 0, sizeof(struct sk_buff *)*TX_RING_SIZE);
  1713. dpriv->tx_dirty = 0xffffffff;
  1714. i = dpriv->tx_current = 0;
  1715. do {
  1716. tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
  1717. tx_fd->complete = 0x00000000;
  1718. /* FIXME: NULL should be ok - to be tried */
  1719. tx_fd->data = cpu_to_le32(dpriv->tx_fd_dma);
  1720. (tx_fd++)->next = cpu_to_le32(dpriv->tx_fd_dma +
  1721. (++i%TX_RING_SIZE)*sizeof(*tx_fd));
  1722. } while (i < TX_RING_SIZE);
  1723. if (!dscc4_init_dummy_skb(dpriv))
  1724. goto err_free_dma_tx;
  1725. memset(dpriv->rx_skbuff, 0, sizeof(struct sk_buff *)*RX_RING_SIZE);
  1726. i = dpriv->rx_dirty = dpriv->rx_current = 0;
  1727. do {
  1728. /* size set by the host. Multiple of 4 bytes please */
  1729. rx_fd->state1 = HiDesc;
  1730. rx_fd->state2 = 0x00000000;
  1731. rx_fd->end = cpu_to_le32(0xbabeface);
  1732. rx_fd->state1 |= TO_STATE_RX(HDLC_MAX_MRU);
  1733. // FIXME: return value verifiee mais traitement suspect
  1734. if (try_get_rx_skb(dpriv, dev) >= 0)
  1735. dpriv->rx_dirty++;
  1736. (rx_fd++)->next = cpu_to_le32(dpriv->rx_fd_dma +
  1737. (++i%RX_RING_SIZE)*sizeof(*rx_fd));
  1738. } while (i < RX_RING_SIZE);
  1739. return 0;
  1740. err_free_dma_tx:
  1741. pci_free_consistent(pdev, TX_TOTAL_SIZE, ring, dpriv->tx_fd_dma);
  1742. err_free_dma_rx:
  1743. pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
  1744. err_out:
  1745. return -ENOMEM;
  1746. }
  1747. static void __devexit dscc4_remove_one(struct pci_dev *pdev)
  1748. {
  1749. struct dscc4_pci_priv *ppriv;
  1750. struct dscc4_dev_priv *root;
  1751. void __iomem *ioaddr;
  1752. int i;
  1753. ppriv = pci_get_drvdata(pdev);
  1754. root = ppriv->root;
  1755. ioaddr = root->base_addr;
  1756. dscc4_pci_reset(pdev, ioaddr);
  1757. free_irq(pdev->irq, root);
  1758. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), ppriv->iqcfg,
  1759. ppriv->iqcfg_dma);
  1760. for (i = 0; i < dev_per_card; i++) {
  1761. struct dscc4_dev_priv *dpriv = root + i;
  1762. dscc4_release_ring(dpriv);
  1763. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  1764. dpriv->iqrx, dpriv->iqrx_dma);
  1765. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  1766. dpriv->iqtx, dpriv->iqtx_dma);
  1767. }
  1768. dscc4_free1(pdev);
  1769. iounmap(ioaddr);
  1770. pci_release_region(pdev, 1);
  1771. pci_release_region(pdev, 0);
  1772. pci_disable_device(pdev);
  1773. }
  1774. static int dscc4_hdlc_attach(struct net_device *dev, unsigned short encoding,
  1775. unsigned short parity)
  1776. {
  1777. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1778. if (encoding != ENCODING_NRZ &&
  1779. encoding != ENCODING_NRZI &&
  1780. encoding != ENCODING_FM_MARK &&
  1781. encoding != ENCODING_FM_SPACE &&
  1782. encoding != ENCODING_MANCHESTER)
  1783. return -EINVAL;
  1784. if (parity != PARITY_NONE &&
  1785. parity != PARITY_CRC16_PR0_CCITT &&
  1786. parity != PARITY_CRC16_PR1_CCITT &&
  1787. parity != PARITY_CRC32_PR0_CCITT &&
  1788. parity != PARITY_CRC32_PR1_CCITT)
  1789. return -EINVAL;
  1790. dpriv->encoding = encoding;
  1791. dpriv->parity = parity;
  1792. return 0;
  1793. }
  1794. #ifndef MODULE
  1795. static int __init dscc4_setup(char *str)
  1796. {
  1797. int *args[] = { &debug, &quartz, NULL }, **p = args;
  1798. while (*p && (get_option(&str, *p) == 2))
  1799. p++;
  1800. return 1;
  1801. }
  1802. __setup("dscc4.setup=", dscc4_setup);
  1803. #endif
  1804. static struct pci_device_id dscc4_pci_tbl[] = {
  1805. { PCI_VENDOR_ID_SIEMENS, PCI_DEVICE_ID_SIEMENS_DSCC4,
  1806. PCI_ANY_ID, PCI_ANY_ID, },
  1807. { 0,}
  1808. };
  1809. MODULE_DEVICE_TABLE(pci, dscc4_pci_tbl);
  1810. static struct pci_driver dscc4_driver = {
  1811. .name = DRV_NAME,
  1812. .id_table = dscc4_pci_tbl,
  1813. .probe = dscc4_init_one,
  1814. .remove = __devexit_p(dscc4_remove_one),
  1815. };
  1816. static int __init dscc4_init_module(void)
  1817. {
  1818. return pci_register_driver(&dscc4_driver);
  1819. }
  1820. static void __exit dscc4_cleanup_module(void)
  1821. {
  1822. pci_unregister_driver(&dscc4_driver);
  1823. }
  1824. module_init(dscc4_init_module);
  1825. module_exit(dscc4_cleanup_module);