dscc4.c 53 KB

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