dscc4.c 54 KB

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