dscc4.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  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. #include <linux/module.h>
  83. #include <linux/sched.h>
  84. #include <linux/types.h>
  85. #include <linux/errno.h>
  86. #include <linux/list.h>
  87. #include <linux/ioport.h>
  88. #include <linux/pci.h>
  89. #include <linux/kernel.h>
  90. #include <linux/mm.h>
  91. #include <linux/slab.h>
  92. #include <asm/system.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. printk(KERN_ERR "%s: %s timeout\n", dev->name, 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. printk(KERN_DEBUG "%s: %s ack\n", dev->name, msg);
  507. writel(ArAck, ioaddr);
  508. goto done;
  509. } else if (state & Arf) {
  510. printk(KERN_ERR "%s: %s failed\n", dev->name, msg);
  511. writel(Arf, ioaddr);
  512. i = -1;
  513. goto done;
  514. }
  515. rmb();
  516. } while (++i > 0);
  517. printk(KERN_ERR "%s: %s timeout\n", dev->name, 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. printk(KERN_ERR "%s: Tx reset failed\n", dev->name);
  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. printk(KERN_ERR "%s: can't reserve MMIO region (regs)\n",
  640. DRV_NAME);
  641. goto err_disable_0;
  642. }
  643. rc = pci_request_region(pdev, 1, "LBI interface");
  644. if (rc < 0) {
  645. printk(KERN_ERR "%s: can't reserve MMIO region (lbi)\n",
  646. DRV_NAME);
  647. goto err_free_mmio_region_1;
  648. }
  649. ioaddr = pci_ioremap_bar(pdev, 0);
  650. if (!ioaddr) {
  651. printk(KERN_ERR "%s: cannot remap MMIO region %llx @ %llx\n",
  652. DRV_NAME, (unsigned long long)pci_resource_len(pdev, 0),
  653. (unsigned long long)pci_resource_start(pdev, 0));
  654. rc = -EIO;
  655. goto err_free_mmio_regions_2;
  656. }
  657. printk(KERN_DEBUG "Siemens DSCC4, MMIO at %#llx (regs), %#llx (lbi), IRQ %d\n",
  658. (unsigned long long)pci_resource_start(pdev, 0),
  659. (unsigned long long)pci_resource_start(pdev, 1), pdev->irq);
  660. /* Cf errata DS5 p.2 */
  661. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xf8);
  662. pci_set_master(pdev);
  663. rc = dscc4_found1(pdev, ioaddr);
  664. if (rc < 0)
  665. goto err_iounmap_3;
  666. priv = pci_get_drvdata(pdev);
  667. rc = request_irq(pdev->irq, dscc4_irq, IRQF_SHARED, DRV_NAME, priv->root);
  668. if (rc < 0) {
  669. printk(KERN_WARNING "%s: IRQ %d busy\n", DRV_NAME, pdev->irq);
  670. goto err_release_4;
  671. }
  672. /* power up/little endian/dma core controlled via lrda/ltda */
  673. writel(0x00000001, ioaddr + GMODE);
  674. /* Shared interrupt queue */
  675. {
  676. u32 bits;
  677. bits = (IRQ_RING_SIZE >> 5) - 1;
  678. bits |= bits << 4;
  679. bits |= bits << 8;
  680. bits |= bits << 16;
  681. writel(bits, ioaddr + IQLENR0);
  682. }
  683. /* Global interrupt queue */
  684. writel((u32)(((IRQ_RING_SIZE >> 5) - 1) << 20), ioaddr + IQLENR1);
  685. priv->iqcfg = (__le32 *) pci_alloc_consistent(pdev,
  686. IRQ_RING_SIZE*sizeof(__le32), &priv->iqcfg_dma);
  687. if (!priv->iqcfg)
  688. goto err_free_irq_5;
  689. writel(priv->iqcfg_dma, ioaddr + IQCFG);
  690. rc = -ENOMEM;
  691. /*
  692. * SCC 0-3 private rx/tx irq structures
  693. * IQRX/TXi needs to be set soon. Learned it the hard way...
  694. */
  695. for (i = 0; i < dev_per_card; i++) {
  696. dpriv = priv->root + i;
  697. dpriv->iqtx = (__le32 *) pci_alloc_consistent(pdev,
  698. IRQ_RING_SIZE*sizeof(u32), &dpriv->iqtx_dma);
  699. if (!dpriv->iqtx)
  700. goto err_free_iqtx_6;
  701. writel(dpriv->iqtx_dma, ioaddr + IQTX0 + i*4);
  702. }
  703. for (i = 0; i < dev_per_card; i++) {
  704. dpriv = priv->root + i;
  705. dpriv->iqrx = (__le32 *) pci_alloc_consistent(pdev,
  706. IRQ_RING_SIZE*sizeof(u32), &dpriv->iqrx_dma);
  707. if (!dpriv->iqrx)
  708. goto err_free_iqrx_7;
  709. writel(dpriv->iqrx_dma, ioaddr + IQRX0 + i*4);
  710. }
  711. /* Cf application hint. Beware of hard-lock condition on threshold. */
  712. writel(0x42104000, ioaddr + FIFOCR1);
  713. //writel(0x9ce69800, ioaddr + FIFOCR2);
  714. writel(0xdef6d800, ioaddr + FIFOCR2);
  715. //writel(0x11111111, ioaddr + FIFOCR4);
  716. writel(0x18181818, ioaddr + FIFOCR4);
  717. // FIXME: should depend on the chipset revision
  718. writel(0x0000000e, ioaddr + FIFOCR3);
  719. writel(0xff200001, ioaddr + GCMDR);
  720. rc = 0;
  721. out:
  722. return rc;
  723. err_free_iqrx_7:
  724. while (--i >= 0) {
  725. dpriv = priv->root + i;
  726. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  727. dpriv->iqrx, dpriv->iqrx_dma);
  728. }
  729. i = dev_per_card;
  730. err_free_iqtx_6:
  731. while (--i >= 0) {
  732. dpriv = priv->root + i;
  733. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  734. dpriv->iqtx, dpriv->iqtx_dma);
  735. }
  736. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), priv->iqcfg,
  737. priv->iqcfg_dma);
  738. err_free_irq_5:
  739. free_irq(pdev->irq, priv->root);
  740. err_release_4:
  741. dscc4_free1(pdev);
  742. err_iounmap_3:
  743. iounmap (ioaddr);
  744. err_free_mmio_regions_2:
  745. pci_release_region(pdev, 1);
  746. err_free_mmio_region_1:
  747. pci_release_region(pdev, 0);
  748. err_disable_0:
  749. pci_disable_device(pdev);
  750. goto out;
  751. };
  752. /*
  753. * Let's hope the default values are decent enough to protect my
  754. * feet from the user's gun - Ueimor
  755. */
  756. static void dscc4_init_registers(struct dscc4_dev_priv *dpriv,
  757. struct net_device *dev)
  758. {
  759. /* No interrupts, SCC core disabled. Let's relax */
  760. scc_writel(0x00000000, dpriv, dev, CCR0);
  761. scc_writel(LengthCheck | (HDLC_MAX_MRU >> 5), dpriv, dev, RLCR);
  762. /*
  763. * No address recognition/crc-CCITT/cts enabled
  764. * Shared flags transmission disabled - cf errata DS5 p.11
  765. * Carrier detect disabled - cf errata p.14
  766. * FIXME: carrier detection/polarity may be handled more gracefully.
  767. */
  768. scc_writel(0x02408000, dpriv, dev, CCR1);
  769. /* crc not forwarded - Cf errata DS5 p.11 */
  770. scc_writel(0x00050008 & ~RxActivate, dpriv, dev, CCR2);
  771. // crc forwarded
  772. //scc_writel(0x00250008 & ~RxActivate, dpriv, dev, CCR2);
  773. }
  774. static inline int dscc4_set_quartz(struct dscc4_dev_priv *dpriv, int hz)
  775. {
  776. int ret = 0;
  777. if ((hz < 0) || (hz > DSCC4_HZ_MAX))
  778. ret = -EOPNOTSUPP;
  779. else
  780. dpriv->pci_priv->xtal_hz = hz;
  781. return ret;
  782. }
  783. static const struct net_device_ops dscc4_ops = {
  784. .ndo_open = dscc4_open,
  785. .ndo_stop = dscc4_close,
  786. .ndo_change_mtu = hdlc_change_mtu,
  787. .ndo_start_xmit = hdlc_start_xmit,
  788. .ndo_do_ioctl = dscc4_ioctl,
  789. .ndo_tx_timeout = dscc4_tx_timeout,
  790. };
  791. static int dscc4_found1(struct pci_dev *pdev, void __iomem *ioaddr)
  792. {
  793. struct dscc4_pci_priv *ppriv;
  794. struct dscc4_dev_priv *root;
  795. int i, ret = -ENOMEM;
  796. root = kcalloc(dev_per_card, sizeof(*root), GFP_KERNEL);
  797. if (!root) {
  798. printk(KERN_ERR "%s: can't allocate data\n", DRV_NAME);
  799. goto err_out;
  800. }
  801. for (i = 0; i < dev_per_card; i++) {
  802. root[i].dev = alloc_hdlcdev(root + i);
  803. if (!root[i].dev)
  804. goto err_free_dev;
  805. }
  806. ppriv = kzalloc(sizeof(*ppriv), GFP_KERNEL);
  807. if (!ppriv) {
  808. printk(KERN_ERR "%s: can't allocate private data\n", DRV_NAME);
  809. goto err_free_dev;
  810. }
  811. ppriv->root = root;
  812. spin_lock_init(&ppriv->lock);
  813. for (i = 0; i < dev_per_card; i++) {
  814. struct dscc4_dev_priv *dpriv = root + i;
  815. struct net_device *d = dscc4_to_dev(dpriv);
  816. hdlc_device *hdlc = dev_to_hdlc(d);
  817. d->base_addr = (unsigned long)ioaddr;
  818. d->irq = pdev->irq;
  819. d->netdev_ops = &dscc4_ops;
  820. d->watchdog_timeo = TX_TIMEOUT;
  821. SET_NETDEV_DEV(d, &pdev->dev);
  822. dpriv->dev_id = i;
  823. dpriv->pci_priv = ppriv;
  824. dpriv->base_addr = ioaddr;
  825. spin_lock_init(&dpriv->lock);
  826. hdlc->xmit = dscc4_start_xmit;
  827. hdlc->attach = dscc4_hdlc_attach;
  828. dscc4_init_registers(dpriv, d);
  829. dpriv->parity = PARITY_CRC16_PR0_CCITT;
  830. dpriv->encoding = ENCODING_NRZ;
  831. ret = dscc4_init_ring(d);
  832. if (ret < 0)
  833. goto err_unregister;
  834. ret = register_hdlc_device(d);
  835. if (ret < 0) {
  836. printk(KERN_ERR "%s: unable to register\n", DRV_NAME);
  837. dscc4_release_ring(dpriv);
  838. goto err_unregister;
  839. }
  840. }
  841. ret = dscc4_set_quartz(root, quartz);
  842. if (ret < 0)
  843. goto err_unregister;
  844. pci_set_drvdata(pdev, ppriv);
  845. return ret;
  846. err_unregister:
  847. while (i-- > 0) {
  848. dscc4_release_ring(root + i);
  849. unregister_hdlc_device(dscc4_to_dev(root + i));
  850. }
  851. kfree(ppriv);
  852. i = dev_per_card;
  853. err_free_dev:
  854. while (i-- > 0)
  855. free_netdev(root[i].dev);
  856. kfree(root);
  857. err_out:
  858. return ret;
  859. };
  860. /* FIXME: get rid of the unneeded code */
  861. static void dscc4_timer(unsigned long data)
  862. {
  863. struct net_device *dev = (struct net_device *)data;
  864. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  865. // struct dscc4_pci_priv *ppriv;
  866. goto done;
  867. done:
  868. dpriv->timer.expires = jiffies + TX_TIMEOUT;
  869. add_timer(&dpriv->timer);
  870. }
  871. static void dscc4_tx_timeout(struct net_device *dev)
  872. {
  873. /* FIXME: something is missing there */
  874. }
  875. static int dscc4_loopback_check(struct dscc4_dev_priv *dpriv)
  876. {
  877. sync_serial_settings *settings = &dpriv->settings;
  878. if (settings->loopback && (settings->clock_type != CLOCK_INT)) {
  879. struct net_device *dev = dscc4_to_dev(dpriv);
  880. printk(KERN_INFO "%s: loopback requires clock\n", dev->name);
  881. return -1;
  882. }
  883. return 0;
  884. }
  885. #ifdef CONFIG_DSCC4_PCI_RST
  886. /*
  887. * Some DSCC4-based cards wires the GPIO port and the PCI #RST pin together
  888. * so as to provide a safe way to reset the asic while not the whole machine
  889. * rebooting.
  890. *
  891. * This code doesn't need to be efficient. Keep It Simple
  892. */
  893. static void dscc4_pci_reset(struct pci_dev *pdev, void __iomem *ioaddr)
  894. {
  895. int i;
  896. mutex_lock(&dscc4_mutex);
  897. for (i = 0; i < 16; i++)
  898. pci_read_config_dword(pdev, i << 2, dscc4_pci_config_store + i);
  899. /* Maximal LBI clock divider (who cares ?) and whole GPIO range. */
  900. writel(0x001c0000, ioaddr + GMODE);
  901. /* Configure GPIO port as output */
  902. writel(0x0000ffff, ioaddr + GPDIR);
  903. /* Disable interruption */
  904. writel(0x0000ffff, ioaddr + GPIM);
  905. writel(0x0000ffff, ioaddr + GPDATA);
  906. writel(0x00000000, ioaddr + GPDATA);
  907. /* Flush posted writes */
  908. readl(ioaddr + GSTAR);
  909. schedule_timeout_uninterruptible(10);
  910. for (i = 0; i < 16; i++)
  911. pci_write_config_dword(pdev, i << 2, dscc4_pci_config_store[i]);
  912. mutex_unlock(&dscc4_mutex);
  913. }
  914. #else
  915. #define dscc4_pci_reset(pdev,ioaddr) do {} while (0)
  916. #endif /* CONFIG_DSCC4_PCI_RST */
  917. static int dscc4_open(struct net_device *dev)
  918. {
  919. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  920. struct dscc4_pci_priv *ppriv;
  921. int ret = -EAGAIN;
  922. if ((dscc4_loopback_check(dpriv) < 0))
  923. goto err;
  924. if ((ret = hdlc_open(dev)))
  925. goto err;
  926. ppriv = dpriv->pci_priv;
  927. /*
  928. * Due to various bugs, there is no way to reliably reset a
  929. * specific port (manufacturer's dependent special PCI #RST wiring
  930. * apart: it affects all ports). Thus the device goes in the best
  931. * silent mode possible at dscc4_close() time and simply claims to
  932. * be up if it's opened again. It still isn't possible to change
  933. * the HDLC configuration without rebooting but at least the ports
  934. * can be up/down ifconfig'ed without killing the host.
  935. */
  936. if (dpriv->flags & FakeReset) {
  937. dpriv->flags &= ~FakeReset;
  938. scc_patchl(0, PowerUp, dpriv, dev, CCR0);
  939. scc_patchl(0, 0x00050000, dpriv, dev, CCR2);
  940. scc_writel(EventsMask, dpriv, dev, IMR);
  941. printk(KERN_INFO "%s: up again.\n", dev->name);
  942. goto done;
  943. }
  944. /* IDT+IDR during XPR */
  945. dpriv->flags = NeedIDR | NeedIDT;
  946. scc_patchl(0, PowerUp | Vis, dpriv, dev, CCR0);
  947. /*
  948. * The following is a bit paranoid...
  949. *
  950. * NB: the datasheet "...CEC will stay active if the SCC is in
  951. * power-down mode or..." and CCR2.RAC = 1 are two different
  952. * situations.
  953. */
  954. if (scc_readl_star(dpriv, dev) & SccBusy) {
  955. printk(KERN_ERR "%s busy. Try later\n", dev->name);
  956. ret = -EAGAIN;
  957. goto err_out;
  958. } else
  959. printk(KERN_INFO "%s: available. Good\n", dev->name);
  960. scc_writel(EventsMask, dpriv, dev, IMR);
  961. /* Posted write is flushed in the wait_ack loop */
  962. scc_writel(TxSccRes | RxSccRes, dpriv, dev, CMDR);
  963. if ((ret = dscc4_wait_ack_cec(dpriv, dev, "Cec")) < 0)
  964. goto err_disable_scc_events;
  965. /*
  966. * I would expect XPR near CE completion (before ? after ?).
  967. * At worst, this code won't see a late XPR and people
  968. * will have to re-issue an ifconfig (this is harmless).
  969. * WARNING, a really missing XPR usually means a hardware
  970. * reset is needed. Suggestions anyone ?
  971. */
  972. if ((ret = dscc4_xpr_ack(dpriv)) < 0) {
  973. printk(KERN_ERR "%s: %s timeout\n", DRV_NAME, "XPR");
  974. goto err_disable_scc_events;
  975. }
  976. if (debug > 2)
  977. dscc4_tx_print(dev, dpriv, "Open");
  978. done:
  979. netif_start_queue(dev);
  980. init_timer(&dpriv->timer);
  981. dpriv->timer.expires = jiffies + 10*HZ;
  982. dpriv->timer.data = (unsigned long)dev;
  983. dpriv->timer.function = dscc4_timer;
  984. add_timer(&dpriv->timer);
  985. netif_carrier_on(dev);
  986. return 0;
  987. err_disable_scc_events:
  988. scc_writel(0xffffffff, dpriv, dev, IMR);
  989. scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
  990. err_out:
  991. hdlc_close(dev);
  992. err:
  993. return ret;
  994. }
  995. #ifdef DSCC4_POLLING
  996. static int dscc4_tx_poll(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  997. {
  998. /* FIXME: it's gonna be easy (TM), for sure */
  999. }
  1000. #endif /* DSCC4_POLLING */
  1001. static netdev_tx_t dscc4_start_xmit(struct sk_buff *skb,
  1002. struct net_device *dev)
  1003. {
  1004. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1005. struct dscc4_pci_priv *ppriv = dpriv->pci_priv;
  1006. struct TxFD *tx_fd;
  1007. int next;
  1008. next = dpriv->tx_current%TX_RING_SIZE;
  1009. dpriv->tx_skbuff[next] = skb;
  1010. tx_fd = dpriv->tx_fd + next;
  1011. tx_fd->state = FrameEnd | TO_STATE_TX(skb->len);
  1012. tx_fd->data = cpu_to_le32(pci_map_single(ppriv->pdev, skb->data, skb->len,
  1013. PCI_DMA_TODEVICE));
  1014. tx_fd->complete = 0x00000000;
  1015. tx_fd->jiffies = jiffies;
  1016. mb();
  1017. #ifdef DSCC4_POLLING
  1018. spin_lock(&dpriv->lock);
  1019. while (dscc4_tx_poll(dpriv, dev));
  1020. spin_unlock(&dpriv->lock);
  1021. #endif
  1022. if (debug > 2)
  1023. dscc4_tx_print(dev, dpriv, "Xmit");
  1024. /* To be cleaned(unsigned int)/optimized. Later, ok ? */
  1025. if (!((++dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE))
  1026. netif_stop_queue(dev);
  1027. if (dscc4_tx_quiescent(dpriv, dev))
  1028. dscc4_do_tx(dpriv, dev);
  1029. return NETDEV_TX_OK;
  1030. }
  1031. static int dscc4_close(struct net_device *dev)
  1032. {
  1033. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1034. del_timer_sync(&dpriv->timer);
  1035. netif_stop_queue(dev);
  1036. scc_patchl(PowerUp | Vis, 0, dpriv, dev, CCR0);
  1037. scc_patchl(0x00050000, 0, dpriv, dev, CCR2);
  1038. scc_writel(0xffffffff, dpriv, dev, IMR);
  1039. dpriv->flags |= FakeReset;
  1040. hdlc_close(dev);
  1041. return 0;
  1042. }
  1043. static inline int dscc4_check_clock_ability(int port)
  1044. {
  1045. int ret = 0;
  1046. #ifdef CONFIG_DSCC4_PCISYNC
  1047. if (port >= 2)
  1048. ret = -1;
  1049. #endif
  1050. return ret;
  1051. }
  1052. /*
  1053. * DS1 p.137: "There are a total of 13 different clocking modes..."
  1054. * ^^
  1055. * Design choices:
  1056. * - by default, assume a clock is provided on pin RxClk/TxClk (clock mode 0a).
  1057. * Clock mode 3b _should_ work but the testing seems to make this point
  1058. * dubious (DIY testing requires setting CCR0 at 0x00000033).
  1059. * This is supposed to provide least surprise "DTE like" behavior.
  1060. * - if line rate is specified, clocks are assumed to be locally generated.
  1061. * A quartz must be available (on pin XTAL1). Modes 6b/7b are used. Choosing
  1062. * between these it automagically done according on the required frequency
  1063. * scaling. Of course some rounding may take place.
  1064. * - no high speed mode (40Mb/s). May be trivial to do but I don't have an
  1065. * appropriate external clocking device for testing.
  1066. * - no time-slot/clock mode 5: shameless laziness.
  1067. *
  1068. * The clock signals wiring can be (is ?) manufacturer dependent. Good luck.
  1069. *
  1070. * BIG FAT WARNING: if the device isn't provided enough clocking signal, it
  1071. * won't pass the init sequence. For example, straight back-to-back DTE without
  1072. * external clock will fail when dscc4_open() (<- 'ifconfig hdlcx xxx') is
  1073. * called.
  1074. *
  1075. * Typos lurk in datasheet (missing divier in clock mode 7a figure 51 p.153
  1076. * DS0 for example)
  1077. *
  1078. * Clock mode related bits of CCR0:
  1079. * +------------ TOE: output TxClk (0b/2b/3a/3b/6b/7a/7b only)
  1080. * | +---------- SSEL: sub-mode select 0 -> a, 1 -> b
  1081. * | | +-------- High Speed: say 0
  1082. * | | | +-+-+-- Clock Mode: 0..7
  1083. * | | | | | |
  1084. * -+-+-+-+-+-+-+-+
  1085. * x|x|5|4|3|2|1|0| lower bits
  1086. *
  1087. * Division factor of BRR: k = (N+1)x2^M (total divider = 16xk in mode 6b)
  1088. * +-+-+-+------------------ M (0..15)
  1089. * | | | | +-+-+-+-+-+-- N (0..63)
  1090. * 0 0 0 0 | | | | 0 0 | | | | | |
  1091. * ...-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1092. * f|e|d|c|b|a|9|8|7|6|5|4|3|2|1|0| lower bits
  1093. *
  1094. */
  1095. static int dscc4_set_clock(struct net_device *dev, u32 *bps, u32 *state)
  1096. {
  1097. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1098. int ret = -1;
  1099. u32 brr;
  1100. *state &= ~Ccr0ClockMask;
  1101. if (*bps) { /* Clock generated - required for DCE */
  1102. u32 n = 0, m = 0, divider;
  1103. int xtal;
  1104. xtal = dpriv->pci_priv->xtal_hz;
  1105. if (!xtal)
  1106. goto done;
  1107. if (dscc4_check_clock_ability(dpriv->dev_id) < 0)
  1108. goto done;
  1109. divider = xtal / *bps;
  1110. if (divider > BRR_DIVIDER_MAX) {
  1111. divider >>= 4;
  1112. *state |= 0x00000036; /* Clock mode 6b (BRG/16) */
  1113. } else
  1114. *state |= 0x00000037; /* Clock mode 7b (BRG) */
  1115. if (divider >> 22) {
  1116. n = 63;
  1117. m = 15;
  1118. } else if (divider) {
  1119. /* Extraction of the 6 highest weighted bits */
  1120. m = 0;
  1121. while (0xffffffc0 & divider) {
  1122. m++;
  1123. divider >>= 1;
  1124. }
  1125. n = divider;
  1126. }
  1127. brr = (m << 8) | n;
  1128. divider = n << m;
  1129. if (!(*state & 0x00000001)) /* ?b mode mask => clock mode 6b */
  1130. divider <<= 4;
  1131. *bps = xtal / divider;
  1132. } else {
  1133. /*
  1134. * External clock - DTE
  1135. * "state" already reflects Clock mode 0a (CCR0 = 0xzzzzzz00).
  1136. * Nothing more to be done
  1137. */
  1138. brr = 0;
  1139. }
  1140. scc_writel(brr, dpriv, dev, BRR);
  1141. ret = 0;
  1142. done:
  1143. return ret;
  1144. }
  1145. static int dscc4_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1146. {
  1147. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1148. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1149. const size_t size = sizeof(dpriv->settings);
  1150. int ret = 0;
  1151. if (dev->flags & IFF_UP)
  1152. return -EBUSY;
  1153. if (cmd != SIOCWANDEV)
  1154. return -EOPNOTSUPP;
  1155. switch(ifr->ifr_settings.type) {
  1156. case IF_GET_IFACE:
  1157. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1158. if (ifr->ifr_settings.size < size) {
  1159. ifr->ifr_settings.size = size; /* data size wanted */
  1160. return -ENOBUFS;
  1161. }
  1162. if (copy_to_user(line, &dpriv->settings, size))
  1163. return -EFAULT;
  1164. break;
  1165. case IF_IFACE_SYNC_SERIAL:
  1166. if (!capable(CAP_NET_ADMIN))
  1167. return -EPERM;
  1168. if (dpriv->flags & FakeReset) {
  1169. printk(KERN_INFO "%s: please reset the device"
  1170. " before this command\n", dev->name);
  1171. return -EPERM;
  1172. }
  1173. if (copy_from_user(&dpriv->settings, line, size))
  1174. return -EFAULT;
  1175. ret = dscc4_set_iface(dpriv, dev);
  1176. break;
  1177. default:
  1178. ret = hdlc_ioctl(dev, ifr, cmd);
  1179. break;
  1180. }
  1181. return ret;
  1182. }
  1183. static int dscc4_match(const struct thingie *p, int value)
  1184. {
  1185. int i;
  1186. for (i = 0; p[i].define != -1; i++) {
  1187. if (value == p[i].define)
  1188. break;
  1189. }
  1190. if (p[i].define == -1)
  1191. return -1;
  1192. else
  1193. return i;
  1194. }
  1195. static int dscc4_clock_setting(struct dscc4_dev_priv *dpriv,
  1196. struct net_device *dev)
  1197. {
  1198. sync_serial_settings *settings = &dpriv->settings;
  1199. int ret = -EOPNOTSUPP;
  1200. u32 bps, state;
  1201. bps = settings->clock_rate;
  1202. state = scc_readl(dpriv, CCR0);
  1203. if (dscc4_set_clock(dev, &bps, &state) < 0)
  1204. goto done;
  1205. if (bps) { /* DCE */
  1206. printk(KERN_DEBUG "%s: generated RxClk (DCE)\n", dev->name);
  1207. if (settings->clock_rate != bps) {
  1208. printk(KERN_DEBUG "%s: clock adjusted (%08d -> %08d)\n",
  1209. dev->name, settings->clock_rate, bps);
  1210. settings->clock_rate = bps;
  1211. }
  1212. } else { /* DTE */
  1213. state |= PowerUp | Vis;
  1214. printk(KERN_DEBUG "%s: external RxClk (DTE)\n", dev->name);
  1215. }
  1216. scc_writel(state, dpriv, dev, CCR0);
  1217. ret = 0;
  1218. done:
  1219. return ret;
  1220. }
  1221. static int dscc4_encoding_setting(struct dscc4_dev_priv *dpriv,
  1222. struct net_device *dev)
  1223. {
  1224. static const struct thingie encoding[] = {
  1225. { ENCODING_NRZ, 0x00000000 },
  1226. { ENCODING_NRZI, 0x00200000 },
  1227. { ENCODING_FM_MARK, 0x00400000 },
  1228. { ENCODING_FM_SPACE, 0x00500000 },
  1229. { ENCODING_MANCHESTER, 0x00600000 },
  1230. { -1, 0}
  1231. };
  1232. int i, ret = 0;
  1233. i = dscc4_match(encoding, dpriv->encoding);
  1234. if (i >= 0)
  1235. scc_patchl(EncodingMask, encoding[i].bits, dpriv, dev, CCR0);
  1236. else
  1237. ret = -EOPNOTSUPP;
  1238. return ret;
  1239. }
  1240. static int dscc4_loopback_setting(struct dscc4_dev_priv *dpriv,
  1241. struct net_device *dev)
  1242. {
  1243. sync_serial_settings *settings = &dpriv->settings;
  1244. u32 state;
  1245. state = scc_readl(dpriv, CCR1);
  1246. if (settings->loopback) {
  1247. printk(KERN_DEBUG "%s: loopback\n", dev->name);
  1248. state |= 0x00000100;
  1249. } else {
  1250. printk(KERN_DEBUG "%s: normal\n", dev->name);
  1251. state &= ~0x00000100;
  1252. }
  1253. scc_writel(state, dpriv, dev, CCR1);
  1254. return 0;
  1255. }
  1256. static int dscc4_crc_setting(struct dscc4_dev_priv *dpriv,
  1257. struct net_device *dev)
  1258. {
  1259. static const struct thingie crc[] = {
  1260. { PARITY_CRC16_PR0_CCITT, 0x00000010 },
  1261. { PARITY_CRC16_PR1_CCITT, 0x00000000 },
  1262. { PARITY_CRC32_PR0_CCITT, 0x00000011 },
  1263. { PARITY_CRC32_PR1_CCITT, 0x00000001 }
  1264. };
  1265. int i, ret = 0;
  1266. i = dscc4_match(crc, dpriv->parity);
  1267. if (i >= 0)
  1268. scc_patchl(CrcMask, crc[i].bits, dpriv, dev, CCR1);
  1269. else
  1270. ret = -EOPNOTSUPP;
  1271. return ret;
  1272. }
  1273. static int dscc4_set_iface(struct dscc4_dev_priv *dpriv, struct net_device *dev)
  1274. {
  1275. struct {
  1276. int (*action)(struct dscc4_dev_priv *, struct net_device *);
  1277. } *p, do_setting[] = {
  1278. { dscc4_encoding_setting },
  1279. { dscc4_clock_setting },
  1280. { dscc4_loopback_setting },
  1281. { dscc4_crc_setting },
  1282. { NULL }
  1283. };
  1284. int ret = 0;
  1285. for (p = do_setting; p->action; p++) {
  1286. if ((ret = p->action(dpriv, dev)) < 0)
  1287. break;
  1288. }
  1289. return ret;
  1290. }
  1291. static irqreturn_t dscc4_irq(int irq, void *token)
  1292. {
  1293. struct dscc4_dev_priv *root = token;
  1294. struct dscc4_pci_priv *priv;
  1295. struct net_device *dev;
  1296. void __iomem *ioaddr;
  1297. u32 state;
  1298. unsigned long flags;
  1299. int i, handled = 1;
  1300. priv = root->pci_priv;
  1301. dev = dscc4_to_dev(root);
  1302. spin_lock_irqsave(&priv->lock, flags);
  1303. ioaddr = root->base_addr;
  1304. state = readl(ioaddr + GSTAR);
  1305. if (!state) {
  1306. handled = 0;
  1307. goto out;
  1308. }
  1309. if (debug > 3)
  1310. printk(KERN_DEBUG "%s: GSTAR = 0x%08x\n", DRV_NAME, state);
  1311. writel(state, ioaddr + GSTAR);
  1312. if (state & Arf) {
  1313. printk(KERN_ERR "%s: failure (Arf). Harass the maintener\n",
  1314. dev->name);
  1315. goto out;
  1316. }
  1317. state &= ~ArAck;
  1318. if (state & Cfg) {
  1319. if (debug > 0)
  1320. printk(KERN_DEBUG "%s: CfgIV\n", DRV_NAME);
  1321. if (priv->iqcfg[priv->cfg_cur++%IRQ_RING_SIZE] & cpu_to_le32(Arf))
  1322. printk(KERN_ERR "%s: %s failed\n", dev->name, "CFG");
  1323. if (!(state &= ~Cfg))
  1324. goto out;
  1325. }
  1326. if (state & RxEvt) {
  1327. i = dev_per_card - 1;
  1328. do {
  1329. dscc4_rx_irq(priv, root + i);
  1330. } while (--i >= 0);
  1331. state &= ~RxEvt;
  1332. }
  1333. if (state & TxEvt) {
  1334. i = dev_per_card - 1;
  1335. do {
  1336. dscc4_tx_irq(priv, root + i);
  1337. } while (--i >= 0);
  1338. state &= ~TxEvt;
  1339. }
  1340. out:
  1341. spin_unlock_irqrestore(&priv->lock, flags);
  1342. return IRQ_RETVAL(handled);
  1343. }
  1344. static void dscc4_tx_irq(struct dscc4_pci_priv *ppriv,
  1345. struct dscc4_dev_priv *dpriv)
  1346. {
  1347. struct net_device *dev = dscc4_to_dev(dpriv);
  1348. u32 state;
  1349. int cur, loop = 0;
  1350. try:
  1351. cur = dpriv->iqtx_current%IRQ_RING_SIZE;
  1352. state = le32_to_cpu(dpriv->iqtx[cur]);
  1353. if (!state) {
  1354. if (debug > 4)
  1355. printk(KERN_DEBUG "%s: Tx ISR = 0x%08x\n", dev->name,
  1356. state);
  1357. if ((debug > 1) && (loop > 1))
  1358. printk(KERN_DEBUG "%s: Tx irq loop=%d\n", dev->name, loop);
  1359. if (loop && netif_queue_stopped(dev))
  1360. if ((dpriv->tx_current - dpriv->tx_dirty)%TX_RING_SIZE)
  1361. netif_wake_queue(dev);
  1362. if (netif_running(dev) && dscc4_tx_quiescent(dpriv, dev) &&
  1363. !dscc4_tx_done(dpriv))
  1364. dscc4_do_tx(dpriv, dev);
  1365. return;
  1366. }
  1367. loop++;
  1368. dpriv->iqtx[cur] = 0;
  1369. dpriv->iqtx_current++;
  1370. if (state_check(state, dpriv, dev, "Tx") < 0)
  1371. return;
  1372. if (state & SccEvt) {
  1373. if (state & Alls) {
  1374. struct sk_buff *skb;
  1375. struct TxFD *tx_fd;
  1376. if (debug > 2)
  1377. dscc4_tx_print(dev, dpriv, "Alls");
  1378. /*
  1379. * DataComplete can't be trusted for Tx completion.
  1380. * Cf errata DS5 p.8
  1381. */
  1382. cur = dpriv->tx_dirty%TX_RING_SIZE;
  1383. tx_fd = dpriv->tx_fd + cur;
  1384. skb = dpriv->tx_skbuff[cur];
  1385. if (skb) {
  1386. pci_unmap_single(ppriv->pdev, le32_to_cpu(tx_fd->data),
  1387. skb->len, PCI_DMA_TODEVICE);
  1388. if (tx_fd->state & FrameEnd) {
  1389. dev->stats.tx_packets++;
  1390. dev->stats.tx_bytes += skb->len;
  1391. }
  1392. dev_kfree_skb_irq(skb);
  1393. dpriv->tx_skbuff[cur] = NULL;
  1394. ++dpriv->tx_dirty;
  1395. } else {
  1396. if (debug > 1)
  1397. printk(KERN_ERR "%s Tx: NULL skb %d\n",
  1398. dev->name, cur);
  1399. }
  1400. /*
  1401. * If the driver ends sending crap on the wire, it
  1402. * will be way easier to diagnose than the (not so)
  1403. * random freeze induced by null sized tx frames.
  1404. */
  1405. tx_fd->data = tx_fd->next;
  1406. tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
  1407. tx_fd->complete = 0x00000000;
  1408. tx_fd->jiffies = 0;
  1409. if (!(state &= ~Alls))
  1410. goto try;
  1411. }
  1412. /*
  1413. * Transmit Data Underrun
  1414. */
  1415. if (state & Xdu) {
  1416. printk(KERN_ERR "%s: XDU. Ask maintainer\n", DRV_NAME);
  1417. dpriv->flags = NeedIDT;
  1418. /* Tx reset */
  1419. writel(MTFi | Rdt,
  1420. dpriv->base_addr + 0x0c*dpriv->dev_id + CH0CFG);
  1421. writel(Action, dpriv->base_addr + GCMDR);
  1422. return;
  1423. }
  1424. if (state & Cts) {
  1425. printk(KERN_INFO "%s: CTS transition\n", dev->name);
  1426. if (!(state &= ~Cts)) /* DEBUG */
  1427. goto try;
  1428. }
  1429. if (state & Xmr) {
  1430. /* Frame needs to be sent again - FIXME */
  1431. printk(KERN_ERR "%s: Xmr. Ask maintainer\n", DRV_NAME);
  1432. if (!(state &= ~Xmr)) /* DEBUG */
  1433. goto try;
  1434. }
  1435. if (state & Xpr) {
  1436. void __iomem *scc_addr;
  1437. unsigned long ring;
  1438. int i;
  1439. /*
  1440. * - the busy condition happens (sometimes);
  1441. * - it doesn't seem to make the handler unreliable.
  1442. */
  1443. for (i = 1; i; i <<= 1) {
  1444. if (!(scc_readl_star(dpriv, dev) & SccBusy))
  1445. break;
  1446. }
  1447. if (!i)
  1448. printk(KERN_INFO "%s busy in irq\n", dev->name);
  1449. scc_addr = dpriv->base_addr + 0x0c*dpriv->dev_id;
  1450. /* Keep this order: IDT before IDR */
  1451. if (dpriv->flags & NeedIDT) {
  1452. if (debug > 2)
  1453. dscc4_tx_print(dev, dpriv, "Xpr");
  1454. ring = dpriv->tx_fd_dma +
  1455. (dpriv->tx_dirty%TX_RING_SIZE)*
  1456. sizeof(struct TxFD);
  1457. writel(ring, scc_addr + CH0BTDA);
  1458. dscc4_do_tx(dpriv, dev);
  1459. writel(MTFi | Idt, scc_addr + CH0CFG);
  1460. if (dscc4_do_action(dev, "IDT") < 0)
  1461. goto err_xpr;
  1462. dpriv->flags &= ~NeedIDT;
  1463. }
  1464. if (dpriv->flags & NeedIDR) {
  1465. ring = dpriv->rx_fd_dma +
  1466. (dpriv->rx_current%RX_RING_SIZE)*
  1467. sizeof(struct RxFD);
  1468. writel(ring, scc_addr + CH0BRDA);
  1469. dscc4_rx_update(dpriv, dev);
  1470. writel(MTFi | Idr, scc_addr + CH0CFG);
  1471. if (dscc4_do_action(dev, "IDR") < 0)
  1472. goto err_xpr;
  1473. dpriv->flags &= ~NeedIDR;
  1474. smp_wmb();
  1475. /* Activate receiver and misc */
  1476. scc_writel(0x08050008, dpriv, dev, CCR2);
  1477. }
  1478. err_xpr:
  1479. if (!(state &= ~Xpr))
  1480. goto try;
  1481. }
  1482. if (state & Cd) {
  1483. if (debug > 0)
  1484. printk(KERN_INFO "%s: CD transition\n", dev->name);
  1485. if (!(state &= ~Cd)) /* DEBUG */
  1486. goto try;
  1487. }
  1488. } else { /* ! SccEvt */
  1489. if (state & Hi) {
  1490. #ifdef DSCC4_POLLING
  1491. while (!dscc4_tx_poll(dpriv, dev));
  1492. #endif
  1493. printk(KERN_INFO "%s: Tx Hi\n", dev->name);
  1494. state &= ~Hi;
  1495. }
  1496. if (state & Err) {
  1497. printk(KERN_INFO "%s: Tx ERR\n", dev->name);
  1498. dev->stats.tx_errors++;
  1499. state &= ~Err;
  1500. }
  1501. }
  1502. goto try;
  1503. }
  1504. static void dscc4_rx_irq(struct dscc4_pci_priv *priv,
  1505. struct dscc4_dev_priv *dpriv)
  1506. {
  1507. struct net_device *dev = dscc4_to_dev(dpriv);
  1508. u32 state;
  1509. int cur;
  1510. try:
  1511. cur = dpriv->iqrx_current%IRQ_RING_SIZE;
  1512. state = le32_to_cpu(dpriv->iqrx[cur]);
  1513. if (!state)
  1514. return;
  1515. dpriv->iqrx[cur] = 0;
  1516. dpriv->iqrx_current++;
  1517. if (state_check(state, dpriv, dev, "Rx") < 0)
  1518. return;
  1519. if (!(state & SccEvt)){
  1520. struct RxFD *rx_fd;
  1521. if (debug > 4)
  1522. printk(KERN_DEBUG "%s: Rx ISR = 0x%08x\n", dev->name,
  1523. state);
  1524. state &= 0x00ffffff;
  1525. if (state & Err) { /* Hold or reset */
  1526. printk(KERN_DEBUG "%s: Rx ERR\n", dev->name);
  1527. cur = dpriv->rx_current%RX_RING_SIZE;
  1528. rx_fd = dpriv->rx_fd + cur;
  1529. /*
  1530. * Presume we're not facing a DMAC receiver reset.
  1531. * As We use the rx size-filtering feature of the
  1532. * DSCC4, the beginning of a new frame is waiting in
  1533. * the rx fifo. I bet a Receive Data Overflow will
  1534. * happen most of time but let's try and avoid it.
  1535. * Btw (as for RDO) if one experiences ERR whereas
  1536. * the system looks rather idle, there may be a
  1537. * problem with latency. In this case, increasing
  1538. * RX_RING_SIZE may help.
  1539. */
  1540. //while (dpriv->rx_needs_refill) {
  1541. while (!(rx_fd->state1 & Hold)) {
  1542. rx_fd++;
  1543. cur++;
  1544. if (!(cur = cur%RX_RING_SIZE))
  1545. rx_fd = dpriv->rx_fd;
  1546. }
  1547. //dpriv->rx_needs_refill--;
  1548. try_get_rx_skb(dpriv, dev);
  1549. if (!rx_fd->data)
  1550. goto try;
  1551. rx_fd->state1 &= ~Hold;
  1552. rx_fd->state2 = 0x00000000;
  1553. rx_fd->end = cpu_to_le32(0xbabeface);
  1554. //}
  1555. goto try;
  1556. }
  1557. if (state & Fi) {
  1558. dscc4_rx_skb(dpriv, dev);
  1559. goto try;
  1560. }
  1561. if (state & Hi ) { /* HI bit */
  1562. printk(KERN_INFO "%s: Rx Hi\n", dev->name);
  1563. state &= ~Hi;
  1564. goto try;
  1565. }
  1566. } else { /* SccEvt */
  1567. if (debug > 1) {
  1568. //FIXME: verifier la presence de tous les evenements
  1569. static struct {
  1570. u32 mask;
  1571. const char *irq_name;
  1572. } evts[] = {
  1573. { 0x00008000, "TIN"},
  1574. { 0x00000020, "RSC"},
  1575. { 0x00000010, "PCE"},
  1576. { 0x00000008, "PLLA"},
  1577. { 0, NULL}
  1578. }, *evt;
  1579. for (evt = evts; evt->irq_name; evt++) {
  1580. if (state & evt->mask) {
  1581. printk(KERN_DEBUG "%s: %s\n",
  1582. dev->name, evt->irq_name);
  1583. if (!(state &= ~evt->mask))
  1584. goto try;
  1585. }
  1586. }
  1587. } else {
  1588. if (!(state &= ~0x0000c03c))
  1589. goto try;
  1590. }
  1591. if (state & Cts) {
  1592. printk(KERN_INFO "%s: CTS transition\n", dev->name);
  1593. if (!(state &= ~Cts)) /* DEBUG */
  1594. goto try;
  1595. }
  1596. /*
  1597. * Receive Data Overflow (FIXME: fscked)
  1598. */
  1599. if (state & Rdo) {
  1600. struct RxFD *rx_fd;
  1601. void __iomem *scc_addr;
  1602. int cur;
  1603. //if (debug)
  1604. // dscc4_rx_dump(dpriv);
  1605. scc_addr = dpriv->base_addr + 0x0c*dpriv->dev_id;
  1606. scc_patchl(RxActivate, 0, dpriv, dev, CCR2);
  1607. /*
  1608. * This has no effect. Why ?
  1609. * ORed with TxSccRes, one sees the CFG ack (for
  1610. * the TX part only).
  1611. */
  1612. scc_writel(RxSccRes, dpriv, dev, CMDR);
  1613. dpriv->flags |= RdoSet;
  1614. /*
  1615. * Let's try and save something in the received data.
  1616. * rx_current must be incremented at least once to
  1617. * avoid HOLD in the BRDA-to-be-pointed desc.
  1618. */
  1619. do {
  1620. cur = dpriv->rx_current++%RX_RING_SIZE;
  1621. rx_fd = dpriv->rx_fd + cur;
  1622. if (!(rx_fd->state2 & DataComplete))
  1623. break;
  1624. if (rx_fd->state2 & FrameAborted) {
  1625. dev->stats.rx_over_errors++;
  1626. rx_fd->state1 |= Hold;
  1627. rx_fd->state2 = 0x00000000;
  1628. rx_fd->end = cpu_to_le32(0xbabeface);
  1629. } else
  1630. dscc4_rx_skb(dpriv, dev);
  1631. } while (1);
  1632. if (debug > 0) {
  1633. if (dpriv->flags & RdoSet)
  1634. printk(KERN_DEBUG
  1635. "%s: no RDO in Rx data\n", DRV_NAME);
  1636. }
  1637. #ifdef DSCC4_RDO_EXPERIMENTAL_RECOVERY
  1638. /*
  1639. * FIXME: must the reset be this violent ?
  1640. */
  1641. #warning "FIXME: CH0BRDA"
  1642. writel(dpriv->rx_fd_dma +
  1643. (dpriv->rx_current%RX_RING_SIZE)*
  1644. sizeof(struct RxFD), scc_addr + CH0BRDA);
  1645. writel(MTFi|Rdr|Idr, scc_addr + CH0CFG);
  1646. if (dscc4_do_action(dev, "RDR") < 0) {
  1647. printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
  1648. dev->name, "RDR");
  1649. goto rdo_end;
  1650. }
  1651. writel(MTFi|Idr, scc_addr + CH0CFG);
  1652. if (dscc4_do_action(dev, "IDR") < 0) {
  1653. printk(KERN_ERR "%s: RDO recovery failed(%s)\n",
  1654. dev->name, "IDR");
  1655. goto rdo_end;
  1656. }
  1657. rdo_end:
  1658. #endif
  1659. scc_patchl(0, RxActivate, dpriv, dev, CCR2);
  1660. goto try;
  1661. }
  1662. if (state & Cd) {
  1663. printk(KERN_INFO "%s: CD transition\n", dev->name);
  1664. if (!(state &= ~Cd)) /* DEBUG */
  1665. goto try;
  1666. }
  1667. if (state & Flex) {
  1668. printk(KERN_DEBUG "%s: Flex. Ttttt...\n", DRV_NAME);
  1669. if (!(state &= ~Flex))
  1670. goto try;
  1671. }
  1672. }
  1673. }
  1674. /*
  1675. * I had expected the following to work for the first descriptor
  1676. * (tx_fd->state = 0xc0000000)
  1677. * - Hold=1 (don't try and branch to the next descripto);
  1678. * - No=0 (I want an empty data section, i.e. size=0);
  1679. * - Fe=1 (required by No=0 or we got an Err irq and must reset).
  1680. * It failed and locked solid. Thus the introduction of a dummy skb.
  1681. * Problem is acknowledged in errata sheet DS5. Joy :o/
  1682. */
  1683. static struct sk_buff *dscc4_init_dummy_skb(struct dscc4_dev_priv *dpriv)
  1684. {
  1685. struct sk_buff *skb;
  1686. skb = dev_alloc_skb(DUMMY_SKB_SIZE);
  1687. if (skb) {
  1688. int last = dpriv->tx_dirty%TX_RING_SIZE;
  1689. struct TxFD *tx_fd = dpriv->tx_fd + last;
  1690. skb->len = DUMMY_SKB_SIZE;
  1691. skb_copy_to_linear_data(skb, version,
  1692. strlen(version) % DUMMY_SKB_SIZE);
  1693. tx_fd->state = FrameEnd | TO_STATE_TX(DUMMY_SKB_SIZE);
  1694. tx_fd->data = cpu_to_le32(pci_map_single(dpriv->pci_priv->pdev,
  1695. skb->data, DUMMY_SKB_SIZE,
  1696. PCI_DMA_TODEVICE));
  1697. dpriv->tx_skbuff[last] = skb;
  1698. }
  1699. return skb;
  1700. }
  1701. static int dscc4_init_ring(struct net_device *dev)
  1702. {
  1703. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1704. struct pci_dev *pdev = dpriv->pci_priv->pdev;
  1705. struct TxFD *tx_fd;
  1706. struct RxFD *rx_fd;
  1707. void *ring;
  1708. int i;
  1709. ring = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &dpriv->rx_fd_dma);
  1710. if (!ring)
  1711. goto err_out;
  1712. dpriv->rx_fd = rx_fd = (struct RxFD *) ring;
  1713. ring = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &dpriv->tx_fd_dma);
  1714. if (!ring)
  1715. goto err_free_dma_rx;
  1716. dpriv->tx_fd = tx_fd = (struct TxFD *) ring;
  1717. memset(dpriv->tx_skbuff, 0, sizeof(struct sk_buff *)*TX_RING_SIZE);
  1718. dpriv->tx_dirty = 0xffffffff;
  1719. i = dpriv->tx_current = 0;
  1720. do {
  1721. tx_fd->state = FrameEnd | TO_STATE_TX(2*DUMMY_SKB_SIZE);
  1722. tx_fd->complete = 0x00000000;
  1723. /* FIXME: NULL should be ok - to be tried */
  1724. tx_fd->data = cpu_to_le32(dpriv->tx_fd_dma);
  1725. (tx_fd++)->next = cpu_to_le32(dpriv->tx_fd_dma +
  1726. (++i%TX_RING_SIZE)*sizeof(*tx_fd));
  1727. } while (i < TX_RING_SIZE);
  1728. if (!dscc4_init_dummy_skb(dpriv))
  1729. goto err_free_dma_tx;
  1730. memset(dpriv->rx_skbuff, 0, sizeof(struct sk_buff *)*RX_RING_SIZE);
  1731. i = dpriv->rx_dirty = dpriv->rx_current = 0;
  1732. do {
  1733. /* size set by the host. Multiple of 4 bytes please */
  1734. rx_fd->state1 = HiDesc;
  1735. rx_fd->state2 = 0x00000000;
  1736. rx_fd->end = cpu_to_le32(0xbabeface);
  1737. rx_fd->state1 |= TO_STATE_RX(HDLC_MAX_MRU);
  1738. // FIXME: return value verifiee mais traitement suspect
  1739. if (try_get_rx_skb(dpriv, dev) >= 0)
  1740. dpriv->rx_dirty++;
  1741. (rx_fd++)->next = cpu_to_le32(dpriv->rx_fd_dma +
  1742. (++i%RX_RING_SIZE)*sizeof(*rx_fd));
  1743. } while (i < RX_RING_SIZE);
  1744. return 0;
  1745. err_free_dma_tx:
  1746. pci_free_consistent(pdev, TX_TOTAL_SIZE, ring, dpriv->tx_fd_dma);
  1747. err_free_dma_rx:
  1748. pci_free_consistent(pdev, RX_TOTAL_SIZE, rx_fd, dpriv->rx_fd_dma);
  1749. err_out:
  1750. return -ENOMEM;
  1751. }
  1752. static void __devexit dscc4_remove_one(struct pci_dev *pdev)
  1753. {
  1754. struct dscc4_pci_priv *ppriv;
  1755. struct dscc4_dev_priv *root;
  1756. void __iomem *ioaddr;
  1757. int i;
  1758. ppriv = pci_get_drvdata(pdev);
  1759. root = ppriv->root;
  1760. ioaddr = root->base_addr;
  1761. dscc4_pci_reset(pdev, ioaddr);
  1762. free_irq(pdev->irq, root);
  1763. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32), ppriv->iqcfg,
  1764. ppriv->iqcfg_dma);
  1765. for (i = 0; i < dev_per_card; i++) {
  1766. struct dscc4_dev_priv *dpriv = root + i;
  1767. dscc4_release_ring(dpriv);
  1768. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  1769. dpriv->iqrx, dpriv->iqrx_dma);
  1770. pci_free_consistent(pdev, IRQ_RING_SIZE*sizeof(u32),
  1771. dpriv->iqtx, dpriv->iqtx_dma);
  1772. }
  1773. dscc4_free1(pdev);
  1774. iounmap(ioaddr);
  1775. pci_release_region(pdev, 1);
  1776. pci_release_region(pdev, 0);
  1777. pci_disable_device(pdev);
  1778. }
  1779. static int dscc4_hdlc_attach(struct net_device *dev, unsigned short encoding,
  1780. unsigned short parity)
  1781. {
  1782. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1783. if (encoding != ENCODING_NRZ &&
  1784. encoding != ENCODING_NRZI &&
  1785. encoding != ENCODING_FM_MARK &&
  1786. encoding != ENCODING_FM_SPACE &&
  1787. encoding != ENCODING_MANCHESTER)
  1788. return -EINVAL;
  1789. if (parity != PARITY_NONE &&
  1790. parity != PARITY_CRC16_PR0_CCITT &&
  1791. parity != PARITY_CRC16_PR1_CCITT &&
  1792. parity != PARITY_CRC32_PR0_CCITT &&
  1793. parity != PARITY_CRC32_PR1_CCITT)
  1794. return -EINVAL;
  1795. dpriv->encoding = encoding;
  1796. dpriv->parity = parity;
  1797. return 0;
  1798. }
  1799. #ifndef MODULE
  1800. static int __init dscc4_setup(char *str)
  1801. {
  1802. int *args[] = { &debug, &quartz, NULL }, **p = args;
  1803. while (*p && (get_option(&str, *p) == 2))
  1804. p++;
  1805. return 1;
  1806. }
  1807. __setup("dscc4.setup=", dscc4_setup);
  1808. #endif
  1809. static DEFINE_PCI_DEVICE_TABLE(dscc4_pci_tbl) = {
  1810. { PCI_VENDOR_ID_SIEMENS, PCI_DEVICE_ID_SIEMENS_DSCC4,
  1811. PCI_ANY_ID, PCI_ANY_ID, },
  1812. { 0,}
  1813. };
  1814. MODULE_DEVICE_TABLE(pci, dscc4_pci_tbl);
  1815. static struct pci_driver dscc4_driver = {
  1816. .name = DRV_NAME,
  1817. .id_table = dscc4_pci_tbl,
  1818. .probe = dscc4_init_one,
  1819. .remove = __devexit_p(dscc4_remove_one),
  1820. };
  1821. static int __init dscc4_init_module(void)
  1822. {
  1823. return pci_register_driver(&dscc4_driver);
  1824. }
  1825. static void __exit dscc4_cleanup_module(void)
  1826. {
  1827. pci_unregister_driver(&dscc4_driver);
  1828. }
  1829. module_init(dscc4_init_module);
  1830. module_exit(dscc4_cleanup_module);