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