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