lanai.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772
  1. /* lanai.c -- Copyright 1999-2003 by Mitchell Blank Jr <mitch@sfgoth.com>
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version
  6. * 2 of the License, or (at your option) any later version.
  7. *
  8. * This driver supports ATM cards based on the Efficient "Lanai"
  9. * chipset such as the Speedstream 3010 and the ENI-25p. The
  10. * Speedstream 3060 is currently not supported since we don't
  11. * have the code to drive the on-board Alcatel DSL chipset (yet).
  12. *
  13. * Thanks to Efficient for supporting this project with hardware,
  14. * documentation, and by answering my questions.
  15. *
  16. * Things not working yet:
  17. *
  18. * o We don't support the Speedstream 3060 yet - this card has
  19. * an on-board DSL modem chip by Alcatel and the driver will
  20. * need some extra code added to handle it
  21. *
  22. * o Note that due to limitations of the Lanai only one VCC can be
  23. * in CBR at once
  24. *
  25. * o We don't currently parse the EEPROM at all. The code is all
  26. * there as per the spec, but it doesn't actually work. I think
  27. * there may be some issues with the docs. Anyway, do NOT
  28. * enable it yet - bugs in that code may actually damage your
  29. * hardware! Because of this you should hardware an ESI before
  30. * trying to use this in a LANE or MPOA environment.
  31. *
  32. * o AAL0 is stubbed in but the actual rx/tx path isn't written yet:
  33. * vcc_tx_aal0() needs to send or queue a SKB
  34. * vcc_tx_unqueue_aal0() needs to attempt to send queued SKBs
  35. * vcc_rx_aal0() needs to handle AAL0 interrupts
  36. * This isn't too much work - I just wanted to get other things
  37. * done first.
  38. *
  39. * o lanai_change_qos() isn't written yet
  40. *
  41. * o There aren't any ioctl's yet -- I'd like to eventually support
  42. * setting loopback and LED modes that way. (see lanai_ioctl)
  43. *
  44. * o If the segmentation engine or DMA gets shut down we should restart
  45. * card as per section 17.0i. (see lanai_reset)
  46. *
  47. * o setsockopt(SO_CIRANGE) isn't done (although despite what the
  48. * API says it isn't exactly commonly implemented)
  49. */
  50. /* Version history:
  51. * v.1.00 -- 26-JUL-2003 -- PCI/DMA updates
  52. * v.0.02 -- 11-JAN-2000 -- Endian fixes
  53. * v.0.01 -- 30-NOV-1999 -- Initial release
  54. */
  55. #include <linux/module.h>
  56. #include <linux/mm.h>
  57. #include <linux/atmdev.h>
  58. #include <asm/io.h>
  59. #include <asm/byteorder.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/pci.h>
  62. #include <linux/dma-mapping.h>
  63. #include <linux/init.h>
  64. #include <linux/delay.h>
  65. #include <linux/interrupt.h>
  66. #include <linux/dma-mapping.h>
  67. /* -------------------- TUNABLE PARAMATERS: */
  68. /*
  69. * Maximum number of VCIs per card. Setting it lower could theoretically
  70. * save some memory, but since we allocate our vcc list with get_free_pages,
  71. * it's not really likely for most architectures
  72. */
  73. #define NUM_VCI (1024)
  74. /*
  75. * Enable extra debugging
  76. */
  77. #define DEBUG
  78. /*
  79. * Debug _all_ register operations with card, except the memory test.
  80. * Also disables the timed poll to prevent extra chattiness. This
  81. * isn't for normal use
  82. */
  83. #undef DEBUG_RW
  84. /*
  85. * The programming guide specifies a full test of the on-board SRAM
  86. * at initialization time. Undefine to remove this
  87. */
  88. #define FULL_MEMORY_TEST
  89. /*
  90. * This is the number of (4 byte) service entries that we will
  91. * try to allocate at startup. Note that we will end up with
  92. * one PAGE_SIZE's worth regardless of what this is set to
  93. */
  94. #define SERVICE_ENTRIES (1024)
  95. /* TODO: make above a module load-time option */
  96. /*
  97. * We normally read the onboard EEPROM in order to discover our MAC
  98. * address. Undefine to _not_ do this
  99. */
  100. /* #define READ_EEPROM */ /* ***DONT ENABLE YET*** */
  101. /* TODO: make above a module load-time option (also) */
  102. /*
  103. * Depth of TX fifo (in 128 byte units; range 2-31)
  104. * Smaller numbers are better for network latency
  105. * Larger numbers are better for PCI latency
  106. * I'm really sure where the best tradeoff is, but the BSD driver uses
  107. * 7 and it seems to work ok.
  108. */
  109. #define TX_FIFO_DEPTH (7)
  110. /* TODO: make above a module load-time option */
  111. /*
  112. * How often (in jiffies) we will try to unstick stuck connections -
  113. * shouldn't need to happen much
  114. */
  115. #define LANAI_POLL_PERIOD (10*HZ)
  116. /* TODO: make above a module load-time option */
  117. /*
  118. * When allocating an AAL5 receiving buffer, try to make it at least
  119. * large enough to hold this many max_sdu sized PDUs
  120. */
  121. #define AAL5_RX_MULTIPLIER (3)
  122. /* TODO: make above a module load-time option */
  123. /*
  124. * Same for transmitting buffer
  125. */
  126. #define AAL5_TX_MULTIPLIER (3)
  127. /* TODO: make above a module load-time option */
  128. /*
  129. * When allocating an AAL0 transmiting buffer, how many cells should fit.
  130. * Remember we'll end up with a PAGE_SIZE of them anyway, so this isn't
  131. * really critical
  132. */
  133. #define AAL0_TX_MULTIPLIER (40)
  134. /* TODO: make above a module load-time option */
  135. /*
  136. * How large should we make the AAL0 receiving buffer. Remember that this
  137. * is shared between all AAL0 VC's
  138. */
  139. #define AAL0_RX_BUFFER_SIZE (PAGE_SIZE)
  140. /* TODO: make above a module load-time option */
  141. /*
  142. * Should we use Lanai's "powerdown" feature when no vcc's are bound?
  143. */
  144. /* #define USE_POWERDOWN */
  145. /* TODO: make above a module load-time option (also) */
  146. /* -------------------- DEBUGGING AIDS: */
  147. #define DEV_LABEL "lanai"
  148. #ifdef DEBUG
  149. #define DPRINTK(format, args...) \
  150. printk(KERN_DEBUG DEV_LABEL ": " format, ##args)
  151. #define APRINTK(truth, format, args...) \
  152. do { \
  153. if (unlikely(!(truth))) \
  154. printk(KERN_ERR DEV_LABEL ": " format, ##args); \
  155. } while (0)
  156. #else /* !DEBUG */
  157. #define DPRINTK(format, args...)
  158. #define APRINTK(truth, format, args...)
  159. #endif /* DEBUG */
  160. #ifdef DEBUG_RW
  161. #define RWDEBUG(format, args...) \
  162. printk(KERN_DEBUG DEV_LABEL ": " format, ##args)
  163. #else /* !DEBUG_RW */
  164. #define RWDEBUG(format, args...)
  165. #endif
  166. /* -------------------- DATA DEFINITIONS: */
  167. #define LANAI_MAPPING_SIZE (0x40000)
  168. #define LANAI_EEPROM_SIZE (128)
  169. typedef int vci_t;
  170. typedef void __iomem *bus_addr_t;
  171. /* DMA buffer in host memory for TX, RX, or service list. */
  172. struct lanai_buffer {
  173. u32 *start; /* From get_free_pages */
  174. u32 *end; /* One past last byte */
  175. u32 *ptr; /* Pointer to current host location */
  176. dma_addr_t dmaaddr;
  177. };
  178. struct lanai_vcc_stats {
  179. unsigned rx_nomem;
  180. union {
  181. struct {
  182. unsigned rx_badlen;
  183. unsigned service_trash;
  184. unsigned service_stream;
  185. unsigned service_rxcrc;
  186. } aal5;
  187. struct {
  188. } aal0;
  189. } x;
  190. };
  191. struct lanai_dev; /* Forward declaration */
  192. /*
  193. * This is the card-specific per-vcc data. Note that unlike some other
  194. * drivers there is NOT a 1-to-1 correspondance between these and
  195. * atm_vcc's - each one of these represents an actual 2-way vcc, but
  196. * an atm_vcc can be 1-way and share with a 1-way vcc in the other
  197. * direction. To make it weirder, there can even be 0-way vccs
  198. * bound to us, waiting to do a change_qos
  199. */
  200. struct lanai_vcc {
  201. bus_addr_t vbase; /* Base of VCC's registers */
  202. struct lanai_vcc_stats stats;
  203. int nref; /* # of atm_vcc's who reference us */
  204. vci_t vci;
  205. struct {
  206. struct lanai_buffer buf;
  207. struct atm_vcc *atmvcc; /* atm_vcc who is receiver */
  208. } rx;
  209. struct {
  210. struct lanai_buffer buf;
  211. struct atm_vcc *atmvcc; /* atm_vcc who is transmitter */
  212. int endptr; /* last endptr from service entry */
  213. struct sk_buff_head backlog;
  214. void (*unqueue)(struct lanai_dev *, struct lanai_vcc *, int);
  215. } tx;
  216. };
  217. enum lanai_type {
  218. lanai2 = PCI_VENDOR_ID_EF_ATM_LANAI2,
  219. lanaihb = PCI_VENDOR_ID_EF_ATM_LANAIHB
  220. };
  221. struct lanai_dev_stats {
  222. unsigned ovfl_trash; /* # of cells dropped - buffer overflow */
  223. unsigned vci_trash; /* # of cells dropped - closed vci */
  224. unsigned hec_err; /* # of cells dropped - bad HEC */
  225. unsigned atm_ovfl; /* # of cells dropped - rx fifo overflow */
  226. unsigned pcierr_parity_detect;
  227. unsigned pcierr_serr_set;
  228. unsigned pcierr_master_abort;
  229. unsigned pcierr_m_target_abort;
  230. unsigned pcierr_s_target_abort;
  231. unsigned pcierr_master_parity;
  232. unsigned service_notx;
  233. unsigned service_norx;
  234. unsigned service_rxnotaal5;
  235. unsigned dma_reenable;
  236. unsigned card_reset;
  237. };
  238. struct lanai_dev {
  239. bus_addr_t base;
  240. struct lanai_dev_stats stats;
  241. struct lanai_buffer service;
  242. struct lanai_vcc **vccs;
  243. #ifdef USE_POWERDOWN
  244. int nbound; /* number of bound vccs */
  245. #endif
  246. enum lanai_type type;
  247. vci_t num_vci; /* Currently just NUM_VCI */
  248. u8 eeprom[LANAI_EEPROM_SIZE];
  249. u32 serialno, magicno;
  250. struct pci_dev *pci;
  251. DECLARE_BITMAP(backlog_vccs, NUM_VCI); /* VCCs with tx backlog */
  252. DECLARE_BITMAP(transmit_ready, NUM_VCI); /* VCCs with transmit space */
  253. struct timer_list timer;
  254. int naal0;
  255. struct lanai_buffer aal0buf; /* AAL0 RX buffers */
  256. u32 conf1, conf2; /* CONFIG[12] registers */
  257. u32 status; /* STATUS register */
  258. spinlock_t endtxlock;
  259. spinlock_t servicelock;
  260. struct atm_vcc *cbrvcc;
  261. int number;
  262. int board_rev;
  263. u8 pci_revision;
  264. /* TODO - look at race conditions with maintence of conf1/conf2 */
  265. /* TODO - transmit locking: should we use _irq not _irqsave? */
  266. /* TODO - organize above in some rational fashion (see <asm/cache.h>) */
  267. };
  268. /*
  269. * Each device has two bitmaps for each VCC (baclog_vccs and transmit_ready)
  270. * This function iterates one of these, calling a given function for each
  271. * vci with their bit set
  272. */
  273. static void vci_bitfield_iterate(struct lanai_dev *lanai,
  274. const unsigned long *lp,
  275. void (*func)(struct lanai_dev *,vci_t vci))
  276. {
  277. vci_t vci = find_first_bit(lp, NUM_VCI);
  278. while (vci < NUM_VCI) {
  279. func(lanai, vci);
  280. vci = find_next_bit(lp, NUM_VCI, vci + 1);
  281. }
  282. }
  283. /* -------------------- BUFFER UTILITIES: */
  284. /*
  285. * Lanai needs DMA buffers aligned to 256 bytes of at least 1024 bytes -
  286. * usually any page allocation will do. Just to be safe in case
  287. * PAGE_SIZE is insanely tiny, though...
  288. */
  289. #define LANAI_PAGE_SIZE ((PAGE_SIZE >= 1024) ? PAGE_SIZE : 1024)
  290. /*
  291. * Allocate a buffer in host RAM for service list, RX, or TX
  292. * Returns buf->start==NULL if no memory
  293. * Note that the size will be rounded up 2^n bytes, and
  294. * if we can't allocate that we'll settle for something smaller
  295. * until minbytes
  296. */
  297. static void lanai_buf_allocate(struct lanai_buffer *buf,
  298. size_t bytes, size_t minbytes, struct pci_dev *pci)
  299. {
  300. int size;
  301. if (bytes > (128 * 1024)) /* max lanai buffer size */
  302. bytes = 128 * 1024;
  303. for (size = LANAI_PAGE_SIZE; size < bytes; size *= 2)
  304. ;
  305. if (minbytes < LANAI_PAGE_SIZE)
  306. minbytes = LANAI_PAGE_SIZE;
  307. do {
  308. /*
  309. * Technically we could use non-consistent mappings for
  310. * everything, but the way the lanai uses DMA memory would
  311. * make that a terrific pain. This is much simpler.
  312. */
  313. buf->start = pci_alloc_consistent(pci, size, &buf->dmaaddr);
  314. if (buf->start != NULL) { /* Success */
  315. /* Lanai requires 256-byte alignment of DMA bufs */
  316. APRINTK((buf->dmaaddr & ~0xFFFFFF00) == 0,
  317. "bad dmaaddr: 0x%lx\n",
  318. (unsigned long) buf->dmaaddr);
  319. buf->ptr = buf->start;
  320. buf->end = (u32 *)
  321. (&((unsigned char *) buf->start)[size]);
  322. memset(buf->start, 0, size);
  323. break;
  324. }
  325. size /= 2;
  326. } while (size >= minbytes);
  327. }
  328. /* size of buffer in bytes */
  329. static inline size_t lanai_buf_size(const struct lanai_buffer *buf)
  330. {
  331. return ((unsigned long) buf->end) - ((unsigned long) buf->start);
  332. }
  333. static void lanai_buf_deallocate(struct lanai_buffer *buf,
  334. struct pci_dev *pci)
  335. {
  336. if (buf->start != NULL) {
  337. pci_free_consistent(pci, lanai_buf_size(buf),
  338. buf->start, buf->dmaaddr);
  339. buf->start = buf->end = buf->ptr = NULL;
  340. }
  341. }
  342. /* size of buffer as "card order" (0=1k .. 7=128k) */
  343. static int lanai_buf_size_cardorder(const struct lanai_buffer *buf)
  344. {
  345. int order = get_order(lanai_buf_size(buf)) + (PAGE_SHIFT - 10);
  346. /* This can only happen if PAGE_SIZE is gigantic, but just in case */
  347. if (order > 7)
  348. order = 7;
  349. return order;
  350. }
  351. /* -------------------- PORT I/O UTILITIES: */
  352. /* Registers (and their bit-fields) */
  353. enum lanai_register {
  354. Reset_Reg = 0x00, /* Reset; read for chip type; bits: */
  355. #define RESET_GET_BOARD_REV(x) (((x)>> 0)&0x03) /* Board revision */
  356. #define RESET_GET_BOARD_ID(x) (((x)>> 2)&0x03) /* Board ID */
  357. #define BOARD_ID_LANAI256 (0) /* 25.6M adapter card */
  358. Endian_Reg = 0x04, /* Endian setting */
  359. IntStatus_Reg = 0x08, /* Interrupt status */
  360. IntStatusMasked_Reg = 0x0C, /* Interrupt status (masked) */
  361. IntAck_Reg = 0x10, /* Interrupt acknowledge */
  362. IntAckMasked_Reg = 0x14, /* Interrupt acknowledge (masked) */
  363. IntStatusSet_Reg = 0x18, /* Get status + enable/disable */
  364. IntStatusSetMasked_Reg = 0x1C, /* Get status + en/di (masked) */
  365. IntControlEna_Reg = 0x20, /* Interrupt control enable */
  366. IntControlDis_Reg = 0x24, /* Interrupt control disable */
  367. Status_Reg = 0x28, /* Status */
  368. #define STATUS_PROMDATA (0x00000001) /* PROM_DATA pin */
  369. #define STATUS_WAITING (0x00000002) /* Interrupt being delayed */
  370. #define STATUS_SOOL (0x00000004) /* SOOL alarm */
  371. #define STATUS_LOCD (0x00000008) /* LOCD alarm */
  372. #define STATUS_LED (0x00000010) /* LED (HAPPI) output */
  373. #define STATUS_GPIN (0x00000020) /* GPIN pin */
  374. #define STATUS_BUTTBUSY (0x00000040) /* Butt register is pending */
  375. Config1_Reg = 0x2C, /* Config word 1; bits: */
  376. #define CONFIG1_PROMDATA (0x00000001) /* PROM_DATA pin */
  377. #define CONFIG1_PROMCLK (0x00000002) /* PROM_CLK pin */
  378. #define CONFIG1_SET_READMODE(x) ((x)*0x004) /* PCI BM reads; values: */
  379. #define READMODE_PLAIN (0) /* Plain memory read */
  380. #define READMODE_LINE (2) /* Memory read line */
  381. #define READMODE_MULTIPLE (3) /* Memory read multiple */
  382. #define CONFIG1_DMA_ENABLE (0x00000010) /* Turn on DMA */
  383. #define CONFIG1_POWERDOWN (0x00000020) /* Turn off clocks */
  384. #define CONFIG1_SET_LOOPMODE(x) ((x)*0x080) /* Clock&loop mode; values: */
  385. #define LOOPMODE_NORMAL (0) /* Normal - no loop */
  386. #define LOOPMODE_TIME (1)
  387. #define LOOPMODE_DIAG (2)
  388. #define LOOPMODE_LINE (3)
  389. #define CONFIG1_MASK_LOOPMODE (0x00000180)
  390. #define CONFIG1_SET_LEDMODE(x) ((x)*0x0200) /* Mode of LED; values: */
  391. #define LEDMODE_NOT_SOOL (0) /* !SOOL */
  392. #define LEDMODE_OFF (1) /* 0 */
  393. #define LEDMODE_ON (2) /* 1 */
  394. #define LEDMODE_NOT_LOCD (3) /* !LOCD */
  395. #define LEDMORE_GPIN (4) /* GPIN */
  396. #define LEDMODE_NOT_GPIN (7) /* !GPIN */
  397. #define CONFIG1_MASK_LEDMODE (0x00000E00)
  398. #define CONFIG1_GPOUT1 (0x00001000) /* Toggle for reset */
  399. #define CONFIG1_GPOUT2 (0x00002000) /* Loopback PHY */
  400. #define CONFIG1_GPOUT3 (0x00004000) /* Loopback lanai */
  401. Config2_Reg = 0x30, /* Config word 2; bits: */
  402. #define CONFIG2_HOWMANY (0x00000001) /* >512 VCIs? */
  403. #define CONFIG2_PTI7_MODE (0x00000002) /* Make PTI=7 RM, not OAM */
  404. #define CONFIG2_VPI_CHK_DIS (0x00000004) /* Ignore RX VPI value */
  405. #define CONFIG2_HEC_DROP (0x00000008) /* Drop cells w/ HEC errors */
  406. #define CONFIG2_VCI0_NORMAL (0x00000010) /* Treat VCI=0 normally */
  407. #define CONFIG2_CBR_ENABLE (0x00000020) /* Deal with CBR traffic */
  408. #define CONFIG2_TRASH_ALL (0x00000040) /* Trashing incoming cells */
  409. #define CONFIG2_TX_DISABLE (0x00000080) /* Trashing outgoing cells */
  410. #define CONFIG2_SET_TRASH (0x00000100) /* Turn trashing on */
  411. Statistics_Reg = 0x34, /* Statistics; bits: */
  412. #define STATS_GET_FIFO_OVFL(x) (((x)>> 0)&0xFF) /* FIFO overflowed */
  413. #define STATS_GET_HEC_ERR(x) (((x)>> 8)&0xFF) /* HEC was bad */
  414. #define STATS_GET_BAD_VCI(x) (((x)>>16)&0xFF) /* VCI not open */
  415. #define STATS_GET_BUF_OVFL(x) (((x)>>24)&0xFF) /* VCC buffer full */
  416. ServiceStuff_Reg = 0x38, /* Service stuff; bits: */
  417. #define SSTUFF_SET_SIZE(x) ((x)*0x20000000) /* size of service buffer */
  418. #define SSTUFF_SET_ADDR(x) ((x)>>8) /* set address of buffer */
  419. ServWrite_Reg = 0x3C, /* ServWrite Pointer */
  420. ServRead_Reg = 0x40, /* ServRead Pointer */
  421. TxDepth_Reg = 0x44, /* FIFO Transmit Depth */
  422. Butt_Reg = 0x48, /* Butt register */
  423. CBR_ICG_Reg = 0x50,
  424. CBR_PTR_Reg = 0x54,
  425. PingCount_Reg = 0x58, /* Ping count */
  426. DMA_Addr_Reg = 0x5C /* DMA address */
  427. };
  428. static inline bus_addr_t reg_addr(const struct lanai_dev *lanai,
  429. enum lanai_register reg)
  430. {
  431. return lanai->base + reg;
  432. }
  433. static inline u32 reg_read(const struct lanai_dev *lanai,
  434. enum lanai_register reg)
  435. {
  436. u32 t;
  437. t = readl(reg_addr(lanai, reg));
  438. RWDEBUG("R [0x%08X] 0x%02X = 0x%08X\n", (unsigned int) lanai->base,
  439. (int) reg, t);
  440. return t;
  441. }
  442. static inline void reg_write(const struct lanai_dev *lanai, u32 val,
  443. enum lanai_register reg)
  444. {
  445. RWDEBUG("W [0x%08X] 0x%02X < 0x%08X\n", (unsigned int) lanai->base,
  446. (int) reg, val);
  447. writel(val, reg_addr(lanai, reg));
  448. }
  449. static inline void conf1_write(const struct lanai_dev *lanai)
  450. {
  451. reg_write(lanai, lanai->conf1, Config1_Reg);
  452. }
  453. static inline void conf2_write(const struct lanai_dev *lanai)
  454. {
  455. reg_write(lanai, lanai->conf2, Config2_Reg);
  456. }
  457. /* Same as conf2_write(), but defers I/O if we're powered down */
  458. static inline void conf2_write_if_powerup(const struct lanai_dev *lanai)
  459. {
  460. #ifdef USE_POWERDOWN
  461. if (unlikely((lanai->conf1 & CONFIG1_POWERDOWN) != 0))
  462. return;
  463. #endif /* USE_POWERDOWN */
  464. conf2_write(lanai);
  465. }
  466. static inline void reset_board(const struct lanai_dev *lanai)
  467. {
  468. DPRINTK("about to reset board\n");
  469. reg_write(lanai, 0, Reset_Reg);
  470. /*
  471. * If we don't delay a little while here then we can end up
  472. * leaving the card in a VERY weird state and lock up the
  473. * PCI bus. This isn't documented anywhere but I've convinced
  474. * myself after a lot of painful experimentation
  475. */
  476. udelay(5);
  477. }
  478. /* -------------------- CARD SRAM UTILITIES: */
  479. /* The SRAM is mapped into normal PCI memory space - the only catch is
  480. * that it is only 16-bits wide but must be accessed as 32-bit. The
  481. * 16 high bits will be zero. We don't hide this, since they get
  482. * programmed mostly like discrete registers anyway
  483. */
  484. #define SRAM_START (0x20000)
  485. #define SRAM_BYTES (0x20000) /* Again, half don't really exist */
  486. static inline bus_addr_t sram_addr(const struct lanai_dev *lanai, int offset)
  487. {
  488. return lanai->base + SRAM_START + offset;
  489. }
  490. static inline u32 sram_read(const struct lanai_dev *lanai, int offset)
  491. {
  492. return readl(sram_addr(lanai, offset));
  493. }
  494. static inline void sram_write(const struct lanai_dev *lanai,
  495. u32 val, int offset)
  496. {
  497. writel(val, sram_addr(lanai, offset));
  498. }
  499. static int __init sram_test_word(
  500. const struct lanai_dev *lanai, int offset, u32 pattern)
  501. {
  502. u32 readback;
  503. sram_write(lanai, pattern, offset);
  504. readback = sram_read(lanai, offset);
  505. if (likely(readback == pattern))
  506. return 0;
  507. printk(KERN_ERR DEV_LABEL
  508. "(itf %d): SRAM word at %d bad: wrote 0x%X, read 0x%X\n",
  509. lanai->number, offset,
  510. (unsigned int) pattern, (unsigned int) readback);
  511. return -EIO;
  512. }
  513. static int __devinit sram_test_pass(const struct lanai_dev *lanai, u32 pattern)
  514. {
  515. int offset, result = 0;
  516. for (offset = 0; offset < SRAM_BYTES && result == 0; offset += 4)
  517. result = sram_test_word(lanai, offset, pattern);
  518. return result;
  519. }
  520. static int __devinit sram_test_and_clear(const struct lanai_dev *lanai)
  521. {
  522. #ifdef FULL_MEMORY_TEST
  523. int result;
  524. DPRINTK("testing SRAM\n");
  525. if ((result = sram_test_pass(lanai, 0x5555)) != 0)
  526. return result;
  527. if ((result = sram_test_pass(lanai, 0xAAAA)) != 0)
  528. return result;
  529. #endif
  530. DPRINTK("clearing SRAM\n");
  531. return sram_test_pass(lanai, 0x0000);
  532. }
  533. /* -------------------- CARD-BASED VCC TABLE UTILITIES: */
  534. /* vcc table */
  535. enum lanai_vcc_offset {
  536. vcc_rxaddr1 = 0x00, /* Location1, plus bits: */
  537. #define RXADDR1_SET_SIZE(x) ((x)*0x0000100) /* size of RX buffer */
  538. #define RXADDR1_SET_RMMODE(x) ((x)*0x00800) /* RM cell action; values: */
  539. #define RMMODE_TRASH (0) /* discard */
  540. #define RMMODE_PRESERVE (1) /* input as AAL0 */
  541. #define RMMODE_PIPE (2) /* pipe to coscheduler */
  542. #define RMMODE_PIPEALL (3) /* pipe non-RM too */
  543. #define RXADDR1_OAM_PRESERVE (0x00002000) /* Input OAM cells as AAL0 */
  544. #define RXADDR1_SET_MODE(x) ((x)*0x0004000) /* Reassembly mode */
  545. #define RXMODE_TRASH (0) /* discard */
  546. #define RXMODE_AAL0 (1) /* non-AAL5 mode */
  547. #define RXMODE_AAL5 (2) /* AAL5, intr. each PDU */
  548. #define RXMODE_AAL5_STREAM (3) /* AAL5 w/o per-PDU intr */
  549. vcc_rxaddr2 = 0x04, /* Location2 */
  550. vcc_rxcrc1 = 0x08, /* RX CRC claculation space */
  551. vcc_rxcrc2 = 0x0C,
  552. vcc_rxwriteptr = 0x10, /* RX writeptr, plus bits: */
  553. #define RXWRITEPTR_LASTEFCI (0x00002000) /* Last PDU had EFCI bit */
  554. #define RXWRITEPTR_DROPPING (0x00004000) /* Had error, dropping */
  555. #define RXWRITEPTR_TRASHING (0x00008000) /* Trashing */
  556. vcc_rxbufstart = 0x14, /* RX bufstart, plus bits: */
  557. #define RXBUFSTART_CLP (0x00004000)
  558. #define RXBUFSTART_CI (0x00008000)
  559. vcc_rxreadptr = 0x18, /* RX readptr */
  560. vcc_txicg = 0x1C, /* TX ICG */
  561. vcc_txaddr1 = 0x20, /* Location1, plus bits: */
  562. #define TXADDR1_SET_SIZE(x) ((x)*0x0000100) /* size of TX buffer */
  563. #define TXADDR1_ABR (0x00008000) /* use ABR (doesn't work) */
  564. vcc_txaddr2 = 0x24, /* Location2 */
  565. vcc_txcrc1 = 0x28, /* TX CRC claculation space */
  566. vcc_txcrc2 = 0x2C,
  567. vcc_txreadptr = 0x30, /* TX Readptr, plus bits: */
  568. #define TXREADPTR_GET_PTR(x) ((x)&0x01FFF)
  569. #define TXREADPTR_MASK_DELTA (0x0000E000) /* ? */
  570. vcc_txendptr = 0x34, /* TX Endptr, plus bits: */
  571. #define TXENDPTR_CLP (0x00002000)
  572. #define TXENDPTR_MASK_PDUMODE (0x0000C000) /* PDU mode; values: */
  573. #define PDUMODE_AAL0 (0*0x04000)
  574. #define PDUMODE_AAL5 (2*0x04000)
  575. #define PDUMODE_AAL5STREAM (3*0x04000)
  576. vcc_txwriteptr = 0x38, /* TX Writeptr */
  577. #define TXWRITEPTR_GET_PTR(x) ((x)&0x1FFF)
  578. vcc_txcbr_next = 0x3C /* # of next CBR VCI in ring */
  579. #define TXCBR_NEXT_BOZO (0x00008000) /* "bozo bit" */
  580. };
  581. #define CARDVCC_SIZE (0x40)
  582. static inline bus_addr_t cardvcc_addr(const struct lanai_dev *lanai,
  583. vci_t vci)
  584. {
  585. return sram_addr(lanai, vci * CARDVCC_SIZE);
  586. }
  587. static inline u32 cardvcc_read(const struct lanai_vcc *lvcc,
  588. enum lanai_vcc_offset offset)
  589. {
  590. u32 val;
  591. APRINTK(lvcc->vbase != NULL, "cardvcc_read: unbound vcc!\n");
  592. val= readl(lvcc->vbase + offset);
  593. RWDEBUG("VR vci=%04d 0x%02X = 0x%08X\n",
  594. lvcc->vci, (int) offset, val);
  595. return val;
  596. }
  597. static inline void cardvcc_write(const struct lanai_vcc *lvcc,
  598. u32 val, enum lanai_vcc_offset offset)
  599. {
  600. APRINTK(lvcc->vbase != NULL, "cardvcc_write: unbound vcc!\n");
  601. APRINTK((val & ~0xFFFF) == 0,
  602. "cardvcc_write: bad val 0x%X (vci=%d, addr=0x%02X)\n",
  603. (unsigned int) val, lvcc->vci, (unsigned int) offset);
  604. RWDEBUG("VW vci=%04d 0x%02X > 0x%08X\n",
  605. lvcc->vci, (unsigned int) offset, (unsigned int) val);
  606. writel(val, lvcc->vbase + offset);
  607. }
  608. /* -------------------- COMPUTE SIZE OF AN AAL5 PDU: */
  609. /* How many bytes will an AAL5 PDU take to transmit - remember that:
  610. * o we need to add 8 bytes for length, CPI, UU, and CRC
  611. * o we need to round up to 48 bytes for cells
  612. */
  613. static inline int aal5_size(int size)
  614. {
  615. int cells = (size + 8 + 47) / 48;
  616. return cells * 48;
  617. }
  618. /* How many bytes can we send if we have "space" space, assuming we have
  619. * to send full cells
  620. */
  621. static inline int aal5_spacefor(int space)
  622. {
  623. int cells = space / 48;
  624. return cells * 48;
  625. }
  626. /* -------------------- FREE AN ATM SKB: */
  627. static inline void lanai_free_skb(struct atm_vcc *atmvcc, struct sk_buff *skb)
  628. {
  629. if (atmvcc->pop != NULL)
  630. atmvcc->pop(atmvcc, skb);
  631. else
  632. dev_kfree_skb_any(skb);
  633. }
  634. /* -------------------- TURN VCCS ON AND OFF: */
  635. static void host_vcc_start_rx(const struct lanai_vcc *lvcc)
  636. {
  637. u32 addr1;
  638. if (lvcc->rx.atmvcc->qos.aal == ATM_AAL5) {
  639. dma_addr_t dmaaddr = lvcc->rx.buf.dmaaddr;
  640. cardvcc_write(lvcc, 0xFFFF, vcc_rxcrc1);
  641. cardvcc_write(lvcc, 0xFFFF, vcc_rxcrc2);
  642. cardvcc_write(lvcc, 0, vcc_rxwriteptr);
  643. cardvcc_write(lvcc, 0, vcc_rxbufstart);
  644. cardvcc_write(lvcc, 0, vcc_rxreadptr);
  645. cardvcc_write(lvcc, (dmaaddr >> 16) & 0xFFFF, vcc_rxaddr2);
  646. addr1 = ((dmaaddr >> 8) & 0xFF) |
  647. RXADDR1_SET_SIZE(lanai_buf_size_cardorder(&lvcc->rx.buf))|
  648. RXADDR1_SET_RMMODE(RMMODE_TRASH) | /* ??? */
  649. /* RXADDR1_OAM_PRESERVE | --- no OAM support yet */
  650. RXADDR1_SET_MODE(RXMODE_AAL5);
  651. } else
  652. addr1 = RXADDR1_SET_RMMODE(RMMODE_PRESERVE) | /* ??? */
  653. RXADDR1_OAM_PRESERVE | /* ??? */
  654. RXADDR1_SET_MODE(RXMODE_AAL0);
  655. /* This one must be last! */
  656. cardvcc_write(lvcc, addr1, vcc_rxaddr1);
  657. }
  658. static void host_vcc_start_tx(const struct lanai_vcc *lvcc)
  659. {
  660. dma_addr_t dmaaddr = lvcc->tx.buf.dmaaddr;
  661. cardvcc_write(lvcc, 0, vcc_txicg);
  662. cardvcc_write(lvcc, 0xFFFF, vcc_txcrc1);
  663. cardvcc_write(lvcc, 0xFFFF, vcc_txcrc2);
  664. cardvcc_write(lvcc, 0, vcc_txreadptr);
  665. cardvcc_write(lvcc, 0, vcc_txendptr);
  666. cardvcc_write(lvcc, 0, vcc_txwriteptr);
  667. cardvcc_write(lvcc,
  668. (lvcc->tx.atmvcc->qos.txtp.traffic_class == ATM_CBR) ?
  669. TXCBR_NEXT_BOZO | lvcc->vci : 0, vcc_txcbr_next);
  670. cardvcc_write(lvcc, (dmaaddr >> 16) & 0xFFFF, vcc_txaddr2);
  671. cardvcc_write(lvcc,
  672. ((dmaaddr >> 8) & 0xFF) |
  673. TXADDR1_SET_SIZE(lanai_buf_size_cardorder(&lvcc->tx.buf)),
  674. vcc_txaddr1);
  675. }
  676. /* Shutdown receiving on card */
  677. static void lanai_shutdown_rx_vci(const struct lanai_vcc *lvcc)
  678. {
  679. if (lvcc->vbase == NULL) /* We were never bound to a VCI */
  680. return;
  681. /* 15.1.1 - set to trashing, wait one cell time (15us) */
  682. cardvcc_write(lvcc,
  683. RXADDR1_SET_RMMODE(RMMODE_TRASH) |
  684. RXADDR1_SET_MODE(RXMODE_TRASH), vcc_rxaddr1);
  685. udelay(15);
  686. /* 15.1.2 - clear rest of entries */
  687. cardvcc_write(lvcc, 0, vcc_rxaddr2);
  688. cardvcc_write(lvcc, 0, vcc_rxcrc1);
  689. cardvcc_write(lvcc, 0, vcc_rxcrc2);
  690. cardvcc_write(lvcc, 0, vcc_rxwriteptr);
  691. cardvcc_write(lvcc, 0, vcc_rxbufstart);
  692. cardvcc_write(lvcc, 0, vcc_rxreadptr);
  693. }
  694. /* Shutdown transmitting on card.
  695. * Unfortunately the lanai needs us to wait until all the data
  696. * drains out of the buffer before we can dealloc it, so this
  697. * can take awhile -- up to 370ms for a full 128KB buffer
  698. * assuming everone else is quiet. In theory the time is
  699. * boundless if there's a CBR VCC holding things up.
  700. */
  701. static void lanai_shutdown_tx_vci(struct lanai_dev *lanai,
  702. struct lanai_vcc *lvcc)
  703. {
  704. struct sk_buff *skb;
  705. unsigned long flags, timeout;
  706. int read, write, lastread = -1;
  707. APRINTK(!in_interrupt(),
  708. "lanai_shutdown_tx_vci called w/o process context!\n");
  709. if (lvcc->vbase == NULL) /* We were never bound to a VCI */
  710. return;
  711. /* 15.2.1 - wait for queue to drain */
  712. while ((skb = skb_dequeue(&lvcc->tx.backlog)) != NULL)
  713. lanai_free_skb(lvcc->tx.atmvcc, skb);
  714. read_lock_irqsave(&vcc_sklist_lock, flags);
  715. __clear_bit(lvcc->vci, lanai->backlog_vccs);
  716. read_unlock_irqrestore(&vcc_sklist_lock, flags);
  717. /*
  718. * We need to wait for the VCC to drain but don't wait forever. We
  719. * give each 1K of buffer size 1/128th of a second to clear out.
  720. * TODO: maybe disable CBR if we're about to timeout?
  721. */
  722. timeout = jiffies +
  723. (((lanai_buf_size(&lvcc->tx.buf) / 1024) * HZ) >> 7);
  724. write = TXWRITEPTR_GET_PTR(cardvcc_read(lvcc, vcc_txwriteptr));
  725. for (;;) {
  726. read = TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr));
  727. if (read == write && /* Is TX buffer empty? */
  728. (lvcc->tx.atmvcc->qos.txtp.traffic_class != ATM_CBR ||
  729. (cardvcc_read(lvcc, vcc_txcbr_next) &
  730. TXCBR_NEXT_BOZO) == 0))
  731. break;
  732. if (read != lastread) { /* Has there been any progress? */
  733. lastread = read;
  734. timeout += HZ / 10;
  735. }
  736. if (unlikely(time_after(jiffies, timeout))) {
  737. printk(KERN_ERR DEV_LABEL "(itf %d): Timed out on "
  738. "backlog closing vci %d\n",
  739. lvcc->tx.atmvcc->dev->number, lvcc->vci);
  740. DPRINTK("read, write = %d, %d\n", read, write);
  741. break;
  742. }
  743. msleep(40);
  744. }
  745. /* 15.2.2 - clear out all tx registers */
  746. cardvcc_write(lvcc, 0, vcc_txreadptr);
  747. cardvcc_write(lvcc, 0, vcc_txwriteptr);
  748. cardvcc_write(lvcc, 0, vcc_txendptr);
  749. cardvcc_write(lvcc, 0, vcc_txcrc1);
  750. cardvcc_write(lvcc, 0, vcc_txcrc2);
  751. cardvcc_write(lvcc, 0, vcc_txaddr2);
  752. cardvcc_write(lvcc, 0, vcc_txaddr1);
  753. }
  754. /* -------------------- MANAGING AAL0 RX BUFFER: */
  755. static inline int aal0_buffer_allocate(struct lanai_dev *lanai)
  756. {
  757. DPRINTK("aal0_buffer_allocate: allocating AAL0 RX buffer\n");
  758. lanai_buf_allocate(&lanai->aal0buf, AAL0_RX_BUFFER_SIZE, 80,
  759. lanai->pci);
  760. return (lanai->aal0buf.start == NULL) ? -ENOMEM : 0;
  761. }
  762. static inline void aal0_buffer_free(struct lanai_dev *lanai)
  763. {
  764. DPRINTK("aal0_buffer_allocate: freeing AAL0 RX buffer\n");
  765. lanai_buf_deallocate(&lanai->aal0buf, lanai->pci);
  766. }
  767. /* -------------------- EEPROM UTILITIES: */
  768. /* Offsets of data in the EEPROM */
  769. #define EEPROM_COPYRIGHT (0)
  770. #define EEPROM_COPYRIGHT_LEN (44)
  771. #define EEPROM_CHECKSUM (62)
  772. #define EEPROM_CHECKSUM_REV (63)
  773. #define EEPROM_MAC (64)
  774. #define EEPROM_MAC_REV (70)
  775. #define EEPROM_SERIAL (112)
  776. #define EEPROM_SERIAL_REV (116)
  777. #define EEPROM_MAGIC (120)
  778. #define EEPROM_MAGIC_REV (124)
  779. #define EEPROM_MAGIC_VALUE (0x5AB478D2)
  780. #ifndef READ_EEPROM
  781. /* Stub functions to use if EEPROM reading is disabled */
  782. static int __devinit eeprom_read(struct lanai_dev *lanai)
  783. {
  784. printk(KERN_INFO DEV_LABEL "(itf %d): *NOT* reading EEPROM\n",
  785. lanai->number);
  786. memset(&lanai->eeprom[EEPROM_MAC], 0, 6);
  787. return 0;
  788. }
  789. static int __devinit eeprom_validate(struct lanai_dev *lanai)
  790. {
  791. lanai->serialno = 0;
  792. lanai->magicno = EEPROM_MAGIC_VALUE;
  793. return 0;
  794. }
  795. #else /* READ_EEPROM */
  796. static int __devinit eeprom_read(struct lanai_dev *lanai)
  797. {
  798. int i, address;
  799. u8 data;
  800. u32 tmp;
  801. #define set_config1(x) do { lanai->conf1 = x; conf1_write(lanai); \
  802. } while (0)
  803. #define clock_h() set_config1(lanai->conf1 | CONFIG1_PROMCLK)
  804. #define clock_l() set_config1(lanai->conf1 &~ CONFIG1_PROMCLK)
  805. #define data_h() set_config1(lanai->conf1 | CONFIG1_PROMDATA)
  806. #define data_l() set_config1(lanai->conf1 &~ CONFIG1_PROMDATA)
  807. #define pre_read() do { data_h(); clock_h(); udelay(5); } while (0)
  808. #define read_pin() (reg_read(lanai, Status_Reg) & STATUS_PROMDATA)
  809. #define send_stop() do { data_l(); udelay(5); clock_h(); udelay(5); \
  810. data_h(); udelay(5); } while (0)
  811. /* start with both clock and data high */
  812. data_h(); clock_h(); udelay(5);
  813. for (address = 0; address < LANAI_EEPROM_SIZE; address++) {
  814. data = (address << 1) | 1; /* Command=read + address */
  815. /* send start bit */
  816. data_l(); udelay(5);
  817. clock_l(); udelay(5);
  818. for (i = 128; i != 0; i >>= 1) { /* write command out */
  819. tmp = (lanai->conf1 & ~CONFIG1_PROMDATA) |
  820. (data & i) ? CONFIG1_PROMDATA : 0;
  821. if (lanai->conf1 != tmp) {
  822. set_config1(tmp);
  823. udelay(5); /* Let new data settle */
  824. }
  825. clock_h(); udelay(5); clock_l(); udelay(5);
  826. }
  827. /* look for ack */
  828. data_h(); clock_h(); udelay(5);
  829. if (read_pin() != 0)
  830. goto error; /* No ack seen */
  831. clock_l(); udelay(5);
  832. /* read back result */
  833. for (data = 0, i = 7; i >= 0; i--) {
  834. data_h(); clock_h(); udelay(5);
  835. data = (data << 1) | !!read_pin();
  836. clock_l(); udelay(5);
  837. }
  838. /* look again for ack */
  839. data_h(); clock_h(); udelay(5);
  840. if (read_pin() == 0)
  841. goto error; /* Spurious ack */
  842. clock_l(); udelay(5);
  843. send_stop();
  844. lanai->eeprom[address] = data;
  845. DPRINTK("EEPROM 0x%04X %02X\n",
  846. (unsigned int) address, (unsigned int) data);
  847. }
  848. return 0;
  849. error:
  850. clock_l(); udelay(5); /* finish read */
  851. send_stop();
  852. printk(KERN_ERR DEV_LABEL "(itf %d): error reading EEPROM byte %d\n",
  853. lanai->number, address);
  854. return -EIO;
  855. #undef set_config1
  856. #undef clock_h
  857. #undef clock_l
  858. #undef data_h
  859. #undef data_l
  860. #undef pre_read
  861. #undef read_pin
  862. #undef send_stop
  863. }
  864. /* read a big-endian 4-byte value out of eeprom */
  865. static inline u32 eeprom_be4(const struct lanai_dev *lanai, int address)
  866. {
  867. return be32_to_cpup((const u32 *) &lanai->eeprom[address]);
  868. }
  869. /* Checksum/validate EEPROM contents */
  870. static int __devinit eeprom_validate(struct lanai_dev *lanai)
  871. {
  872. int i, s;
  873. u32 v;
  874. const u8 *e = lanai->eeprom;
  875. #ifdef DEBUG
  876. /* First, see if we can get an ASCIIZ string out of the copyright */
  877. for (i = EEPROM_COPYRIGHT;
  878. i < (EEPROM_COPYRIGHT + EEPROM_COPYRIGHT_LEN); i++)
  879. if (e[i] < 0x20 || e[i] > 0x7E)
  880. break;
  881. if ( i != EEPROM_COPYRIGHT &&
  882. i != EEPROM_COPYRIGHT + EEPROM_COPYRIGHT_LEN && e[i] == '\0')
  883. DPRINTK("eeprom: copyright = \"%s\"\n",
  884. (char *) &e[EEPROM_COPYRIGHT]);
  885. else
  886. DPRINTK("eeprom: copyright not found\n");
  887. #endif
  888. /* Validate checksum */
  889. for (i = s = 0; i < EEPROM_CHECKSUM; i++)
  890. s += e[i];
  891. s &= 0xFF;
  892. if (s != e[EEPROM_CHECKSUM]) {
  893. printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM checksum bad "
  894. "(wanted 0x%02X, got 0x%02X)\n", lanai->number,
  895. (unsigned int) s, (unsigned int) e[EEPROM_CHECKSUM]);
  896. return -EIO;
  897. }
  898. s ^= 0xFF;
  899. if (s != e[EEPROM_CHECKSUM_REV]) {
  900. printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM inverse checksum "
  901. "bad (wanted 0x%02X, got 0x%02X)\n", lanai->number,
  902. (unsigned int) s, (unsigned int) e[EEPROM_CHECKSUM_REV]);
  903. return -EIO;
  904. }
  905. /* Verify MAC address */
  906. for (i = 0; i < 6; i++)
  907. if ((e[EEPROM_MAC + i] ^ e[EEPROM_MAC_REV + i]) != 0xFF) {
  908. printk(KERN_ERR DEV_LABEL
  909. "(itf %d) : EEPROM MAC addresses don't match "
  910. "(0x%02X, inverse 0x%02X)\n", lanai->number,
  911. (unsigned int) e[EEPROM_MAC + i],
  912. (unsigned int) e[EEPROM_MAC_REV + i]);
  913. return -EIO;
  914. }
  915. DPRINTK("eeprom: MAC address = %02X:%02X:%02X:%02X:%02X:%02X\n",
  916. e[EEPROM_MAC + 0], e[EEPROM_MAC + 1], e[EEPROM_MAC + 2],
  917. e[EEPROM_MAC + 3], e[EEPROM_MAC + 4], e[EEPROM_MAC + 5]);
  918. /* Verify serial number */
  919. lanai->serialno = eeprom_be4(lanai, EEPROM_SERIAL);
  920. v = eeprom_be4(lanai, EEPROM_SERIAL_REV);
  921. if ((lanai->serialno ^ v) != 0xFFFFFFFF) {
  922. printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM serial numbers "
  923. "don't match (0x%08X, inverse 0x%08X)\n", lanai->number,
  924. (unsigned int) lanai->serialno, (unsigned int) v);
  925. return -EIO;
  926. }
  927. DPRINTK("eeprom: Serial number = %d\n", (unsigned int) lanai->serialno);
  928. /* Verify magic number */
  929. lanai->magicno = eeprom_be4(lanai, EEPROM_MAGIC);
  930. v = eeprom_be4(lanai, EEPROM_MAGIC_REV);
  931. if ((lanai->magicno ^ v) != 0xFFFFFFFF) {
  932. printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM magic numbers "
  933. "don't match (0x%08X, inverse 0x%08X)\n", lanai->number,
  934. lanai->magicno, v);
  935. return -EIO;
  936. }
  937. DPRINTK("eeprom: Magic number = 0x%08X\n", lanai->magicno);
  938. if (lanai->magicno != EEPROM_MAGIC_VALUE)
  939. printk(KERN_WARNING DEV_LABEL "(itf %d): warning - EEPROM "
  940. "magic not what expected (got 0x%08X, not 0x%08X)\n",
  941. lanai->number, (unsigned int) lanai->magicno,
  942. (unsigned int) EEPROM_MAGIC_VALUE);
  943. return 0;
  944. }
  945. #endif /* READ_EEPROM */
  946. static inline const u8 *eeprom_mac(const struct lanai_dev *lanai)
  947. {
  948. return &lanai->eeprom[EEPROM_MAC];
  949. }
  950. /* -------------------- INTERRUPT HANDLING UTILITIES: */
  951. /* Interrupt types */
  952. #define INT_STATS (0x00000002) /* Statistics counter overflow */
  953. #define INT_SOOL (0x00000004) /* SOOL changed state */
  954. #define INT_LOCD (0x00000008) /* LOCD changed state */
  955. #define INT_LED (0x00000010) /* LED (HAPPI) changed state */
  956. #define INT_GPIN (0x00000020) /* GPIN changed state */
  957. #define INT_PING (0x00000040) /* PING_COUNT fulfilled */
  958. #define INT_WAKE (0x00000080) /* Lanai wants bus */
  959. #define INT_CBR0 (0x00000100) /* CBR sched hit VCI 0 */
  960. #define INT_LOCK (0x00000200) /* Service list overflow */
  961. #define INT_MISMATCH (0x00000400) /* TX magic list mismatch */
  962. #define INT_AAL0_STR (0x00000800) /* Non-AAL5 buffer half filled */
  963. #define INT_AAL0 (0x00001000) /* Non-AAL5 data available */
  964. #define INT_SERVICE (0x00002000) /* Service list entries available */
  965. #define INT_TABORTSENT (0x00004000) /* Target abort sent by lanai */
  966. #define INT_TABORTBM (0x00008000) /* Abort rcv'd as bus master */
  967. #define INT_TIMEOUTBM (0x00010000) /* No response to bus master */
  968. #define INT_PCIPARITY (0x00020000) /* Parity error on PCI */
  969. /* Sets of the above */
  970. #define INT_ALL (0x0003FFFE) /* All interrupts */
  971. #define INT_STATUS (0x0000003C) /* Some status pin changed */
  972. #define INT_DMASHUT (0x00038000) /* DMA engine got shut down */
  973. #define INT_SEGSHUT (0x00000700) /* Segmentation got shut down */
  974. static inline u32 intr_pending(const struct lanai_dev *lanai)
  975. {
  976. return reg_read(lanai, IntStatusMasked_Reg);
  977. }
  978. static inline void intr_enable(const struct lanai_dev *lanai, u32 i)
  979. {
  980. reg_write(lanai, i, IntControlEna_Reg);
  981. }
  982. static inline void intr_disable(const struct lanai_dev *lanai, u32 i)
  983. {
  984. reg_write(lanai, i, IntControlDis_Reg);
  985. }
  986. /* -------------------- CARD/PCI STATUS: */
  987. static void status_message(int itf, const char *name, int status)
  988. {
  989. static const char *onoff[2] = { "off to on", "on to off" };
  990. printk(KERN_INFO DEV_LABEL "(itf %d): %s changed from %s\n",
  991. itf, name, onoff[!status]);
  992. }
  993. static void lanai_check_status(struct lanai_dev *lanai)
  994. {
  995. u32 new = reg_read(lanai, Status_Reg);
  996. u32 changes = new ^ lanai->status;
  997. lanai->status = new;
  998. #define e(flag, name) \
  999. if (changes & flag) \
  1000. status_message(lanai->number, name, new & flag)
  1001. e(STATUS_SOOL, "SOOL");
  1002. e(STATUS_LOCD, "LOCD");
  1003. e(STATUS_LED, "LED");
  1004. e(STATUS_GPIN, "GPIN");
  1005. #undef e
  1006. }
  1007. static void pcistatus_got(int itf, const char *name)
  1008. {
  1009. printk(KERN_INFO DEV_LABEL "(itf %d): PCI got %s error\n", itf, name);
  1010. }
  1011. static void pcistatus_check(struct lanai_dev *lanai, int clearonly)
  1012. {
  1013. u16 s;
  1014. int result;
  1015. result = pci_read_config_word(lanai->pci, PCI_STATUS, &s);
  1016. if (result != PCIBIOS_SUCCESSFUL) {
  1017. printk(KERN_ERR DEV_LABEL "(itf %d): can't read PCI_STATUS: "
  1018. "%d\n", lanai->number, result);
  1019. return;
  1020. }
  1021. s &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
  1022. PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT |
  1023. PCI_STATUS_SIG_TARGET_ABORT | PCI_STATUS_PARITY;
  1024. if (s == 0)
  1025. return;
  1026. result = pci_write_config_word(lanai->pci, PCI_STATUS, s);
  1027. if (result != PCIBIOS_SUCCESSFUL)
  1028. printk(KERN_ERR DEV_LABEL "(itf %d): can't write PCI_STATUS: "
  1029. "%d\n", lanai->number, result);
  1030. if (clearonly)
  1031. return;
  1032. #define e(flag, name, stat) \
  1033. if (s & flag) { \
  1034. pcistatus_got(lanai->number, name); \
  1035. ++lanai->stats.pcierr_##stat; \
  1036. }
  1037. e(PCI_STATUS_DETECTED_PARITY, "parity", parity_detect);
  1038. e(PCI_STATUS_SIG_SYSTEM_ERROR, "signalled system", serr_set);
  1039. e(PCI_STATUS_REC_MASTER_ABORT, "master", master_abort);
  1040. e(PCI_STATUS_REC_TARGET_ABORT, "master target", m_target_abort);
  1041. e(PCI_STATUS_SIG_TARGET_ABORT, "slave", s_target_abort);
  1042. e(PCI_STATUS_PARITY, "master parity", master_parity);
  1043. #undef e
  1044. }
  1045. /* -------------------- VCC TX BUFFER UTILITIES: */
  1046. /* space left in tx buffer in bytes */
  1047. static inline int vcc_tx_space(const struct lanai_vcc *lvcc, int endptr)
  1048. {
  1049. int r;
  1050. r = endptr * 16;
  1051. r -= ((unsigned long) lvcc->tx.buf.ptr) -
  1052. ((unsigned long) lvcc->tx.buf.start);
  1053. r -= 16; /* Leave "bubble" - if start==end it looks empty */
  1054. if (r < 0)
  1055. r += lanai_buf_size(&lvcc->tx.buf);
  1056. return r;
  1057. }
  1058. /* test if VCC is currently backlogged */
  1059. static inline int vcc_is_backlogged(const struct lanai_vcc *lvcc)
  1060. {
  1061. return !skb_queue_empty(&lvcc->tx.backlog);
  1062. }
  1063. /* Bit fields in the segmentation buffer descriptor */
  1064. #define DESCRIPTOR_MAGIC (0xD0000000)
  1065. #define DESCRIPTOR_AAL5 (0x00008000)
  1066. #define DESCRIPTOR_AAL5_STREAM (0x00004000)
  1067. #define DESCRIPTOR_CLP (0x00002000)
  1068. /* Add 32-bit descriptor with its padding */
  1069. static inline void vcc_tx_add_aal5_descriptor(struct lanai_vcc *lvcc,
  1070. u32 flags, int len)
  1071. {
  1072. int pos;
  1073. APRINTK((((unsigned long) lvcc->tx.buf.ptr) & 15) == 0,
  1074. "vcc_tx_add_aal5_descriptor: bad ptr=%p\n", lvcc->tx.buf.ptr);
  1075. lvcc->tx.buf.ptr += 4; /* Hope the values REALLY don't matter */
  1076. pos = ((unsigned char *) lvcc->tx.buf.ptr) -
  1077. (unsigned char *) lvcc->tx.buf.start;
  1078. APRINTK((pos & ~0x0001FFF0) == 0,
  1079. "vcc_tx_add_aal5_descriptor: bad pos (%d) before, vci=%d, "
  1080. "start,ptr,end=%p,%p,%p\n", pos, lvcc->vci,
  1081. lvcc->tx.buf.start, lvcc->tx.buf.ptr, lvcc->tx.buf.end);
  1082. pos = (pos + len) & (lanai_buf_size(&lvcc->tx.buf) - 1);
  1083. APRINTK((pos & ~0x0001FFF0) == 0,
  1084. "vcc_tx_add_aal5_descriptor: bad pos (%d) after, vci=%d, "
  1085. "start,ptr,end=%p,%p,%p\n", pos, lvcc->vci,
  1086. lvcc->tx.buf.start, lvcc->tx.buf.ptr, lvcc->tx.buf.end);
  1087. lvcc->tx.buf.ptr[-1] =
  1088. cpu_to_le32(DESCRIPTOR_MAGIC | DESCRIPTOR_AAL5 |
  1089. ((lvcc->tx.atmvcc->atm_options & ATM_ATMOPT_CLP) ?
  1090. DESCRIPTOR_CLP : 0) | flags | pos >> 4);
  1091. if (lvcc->tx.buf.ptr >= lvcc->tx.buf.end)
  1092. lvcc->tx.buf.ptr = lvcc->tx.buf.start;
  1093. }
  1094. /* Add 32-bit AAL5 trailer and leave room for its CRC */
  1095. static inline void vcc_tx_add_aal5_trailer(struct lanai_vcc *lvcc,
  1096. int len, int cpi, int uu)
  1097. {
  1098. APRINTK((((unsigned long) lvcc->tx.buf.ptr) & 15) == 8,
  1099. "vcc_tx_add_aal5_trailer: bad ptr=%p\n", lvcc->tx.buf.ptr);
  1100. lvcc->tx.buf.ptr += 2;
  1101. lvcc->tx.buf.ptr[-2] = cpu_to_be32((uu << 24) | (cpi << 16) | len);
  1102. if (lvcc->tx.buf.ptr >= lvcc->tx.buf.end)
  1103. lvcc->tx.buf.ptr = lvcc->tx.buf.start;
  1104. }
  1105. static inline void vcc_tx_memcpy(struct lanai_vcc *lvcc,
  1106. const unsigned char *src, int n)
  1107. {
  1108. unsigned char *e;
  1109. int m;
  1110. e = ((unsigned char *) lvcc->tx.buf.ptr) + n;
  1111. m = e - (unsigned char *) lvcc->tx.buf.end;
  1112. if (m < 0)
  1113. m = 0;
  1114. memcpy(lvcc->tx.buf.ptr, src, n - m);
  1115. if (m != 0) {
  1116. memcpy(lvcc->tx.buf.start, src + n - m, m);
  1117. e = ((unsigned char *) lvcc->tx.buf.start) + m;
  1118. }
  1119. lvcc->tx.buf.ptr = (u32 *) e;
  1120. }
  1121. static inline void vcc_tx_memzero(struct lanai_vcc *lvcc, int n)
  1122. {
  1123. unsigned char *e;
  1124. int m;
  1125. if (n == 0)
  1126. return;
  1127. e = ((unsigned char *) lvcc->tx.buf.ptr) + n;
  1128. m = e - (unsigned char *) lvcc->tx.buf.end;
  1129. if (m < 0)
  1130. m = 0;
  1131. memset(lvcc->tx.buf.ptr, 0, n - m);
  1132. if (m != 0) {
  1133. memset(lvcc->tx.buf.start, 0, m);
  1134. e = ((unsigned char *) lvcc->tx.buf.start) + m;
  1135. }
  1136. lvcc->tx.buf.ptr = (u32 *) e;
  1137. }
  1138. /* Update "butt" register to specify new WritePtr */
  1139. static inline void lanai_endtx(struct lanai_dev *lanai,
  1140. const struct lanai_vcc *lvcc)
  1141. {
  1142. int i, ptr = ((unsigned char *) lvcc->tx.buf.ptr) -
  1143. (unsigned char *) lvcc->tx.buf.start;
  1144. APRINTK((ptr & ~0x0001FFF0) == 0,
  1145. "lanai_endtx: bad ptr (%d), vci=%d, start,ptr,end=%p,%p,%p\n",
  1146. ptr, lvcc->vci, lvcc->tx.buf.start, lvcc->tx.buf.ptr,
  1147. lvcc->tx.buf.end);
  1148. /*
  1149. * Since the "butt register" is a shared resounce on the card we
  1150. * serialize all accesses to it through this spinlock. This is
  1151. * mostly just paranoia sicne the register is rarely "busy" anyway
  1152. * but is needed for correctness.
  1153. */
  1154. spin_lock(&lanai->endtxlock);
  1155. /*
  1156. * We need to check if the "butt busy" bit is set before
  1157. * updating the butt register. In theory this should
  1158. * never happen because the ATM card is plenty fast at
  1159. * updating the register. Still, we should make sure
  1160. */
  1161. for (i = 0; reg_read(lanai, Status_Reg) & STATUS_BUTTBUSY; i++) {
  1162. if (unlikely(i > 50)) {
  1163. printk(KERN_ERR DEV_LABEL "(itf %d): butt register "
  1164. "always busy!\n", lanai->number);
  1165. break;
  1166. }
  1167. udelay(5);
  1168. }
  1169. /*
  1170. * Before we tall the card to start work we need to be sure 100% of
  1171. * the info in the service buffer has been written before we tell
  1172. * the card about it
  1173. */
  1174. wmb();
  1175. reg_write(lanai, (ptr << 12) | lvcc->vci, Butt_Reg);
  1176. spin_unlock(&lanai->endtxlock);
  1177. }
  1178. /*
  1179. * Add one AAL5 PDU to lvcc's transmit buffer. Caller garauntees there's
  1180. * space available. "pdusize" is the number of bytes the PDU will take
  1181. */
  1182. static void lanai_send_one_aal5(struct lanai_dev *lanai,
  1183. struct lanai_vcc *lvcc, struct sk_buff *skb, int pdusize)
  1184. {
  1185. int pad;
  1186. APRINTK(pdusize == aal5_size(skb->len),
  1187. "lanai_send_one_aal5: wrong size packet (%d != %d)\n",
  1188. pdusize, aal5_size(skb->len));
  1189. vcc_tx_add_aal5_descriptor(lvcc, 0, pdusize);
  1190. pad = pdusize - skb->len - 8;
  1191. APRINTK(pad >= 0, "pad is negative (%d)\n", pad);
  1192. APRINTK(pad < 48, "pad is too big (%d)\n", pad);
  1193. vcc_tx_memcpy(lvcc, skb->data, skb->len);
  1194. vcc_tx_memzero(lvcc, pad);
  1195. vcc_tx_add_aal5_trailer(lvcc, skb->len, 0, 0);
  1196. lanai_endtx(lanai, lvcc);
  1197. lanai_free_skb(lvcc->tx.atmvcc, skb);
  1198. atomic_inc(&lvcc->tx.atmvcc->stats->tx);
  1199. }
  1200. /* Try to fill the buffer - don't call unless there is backlog */
  1201. static void vcc_tx_unqueue_aal5(struct lanai_dev *lanai,
  1202. struct lanai_vcc *lvcc, int endptr)
  1203. {
  1204. int n;
  1205. struct sk_buff *skb;
  1206. int space = vcc_tx_space(lvcc, endptr);
  1207. APRINTK(vcc_is_backlogged(lvcc),
  1208. "vcc_tx_unqueue() called with empty backlog (vci=%d)\n",
  1209. lvcc->vci);
  1210. while (space >= 64) {
  1211. skb = skb_dequeue(&lvcc->tx.backlog);
  1212. if (skb == NULL)
  1213. goto no_backlog;
  1214. n = aal5_size(skb->len);
  1215. if (n + 16 > space) {
  1216. /* No room for this packet - put it back on queue */
  1217. skb_queue_head(&lvcc->tx.backlog, skb);
  1218. return;
  1219. }
  1220. lanai_send_one_aal5(lanai, lvcc, skb, n);
  1221. space -= n + 16;
  1222. }
  1223. if (!vcc_is_backlogged(lvcc)) {
  1224. no_backlog:
  1225. __clear_bit(lvcc->vci, lanai->backlog_vccs);
  1226. }
  1227. }
  1228. /* Given an skb that we want to transmit either send it now or queue */
  1229. static void vcc_tx_aal5(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
  1230. struct sk_buff *skb)
  1231. {
  1232. int space, n;
  1233. if (vcc_is_backlogged(lvcc)) /* Already backlogged */
  1234. goto queue_it;
  1235. space = vcc_tx_space(lvcc,
  1236. TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr)));
  1237. n = aal5_size(skb->len);
  1238. APRINTK(n + 16 >= 64, "vcc_tx_aal5: n too small (%d)\n", n);
  1239. if (space < n + 16) { /* No space for this PDU */
  1240. __set_bit(lvcc->vci, lanai->backlog_vccs);
  1241. queue_it:
  1242. skb_queue_tail(&lvcc->tx.backlog, skb);
  1243. return;
  1244. }
  1245. lanai_send_one_aal5(lanai, lvcc, skb, n);
  1246. }
  1247. static void vcc_tx_unqueue_aal0(struct lanai_dev *lanai,
  1248. struct lanai_vcc *lvcc, int endptr)
  1249. {
  1250. printk(KERN_INFO DEV_LABEL
  1251. ": vcc_tx_unqueue_aal0: not implemented\n");
  1252. }
  1253. static void vcc_tx_aal0(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
  1254. struct sk_buff *skb)
  1255. {
  1256. printk(KERN_INFO DEV_LABEL ": vcc_tx_aal0: not implemented\n");
  1257. /* Remember to increment lvcc->tx.atmvcc->stats->tx */
  1258. lanai_free_skb(lvcc->tx.atmvcc, skb);
  1259. }
  1260. /* -------------------- VCC RX BUFFER UTILITIES: */
  1261. /* unlike the _tx_ cousins, this doesn't update ptr */
  1262. static inline void vcc_rx_memcpy(unsigned char *dest,
  1263. const struct lanai_vcc *lvcc, int n)
  1264. {
  1265. int m = ((const unsigned char *) lvcc->rx.buf.ptr) + n -
  1266. ((const unsigned char *) (lvcc->rx.buf.end));
  1267. if (m < 0)
  1268. m = 0;
  1269. memcpy(dest, lvcc->rx.buf.ptr, n - m);
  1270. memcpy(dest + n - m, lvcc->rx.buf.start, m);
  1271. /* Make sure that these copies don't get reordered */
  1272. barrier();
  1273. }
  1274. /* Receive AAL5 data on a VCC with a particular endptr */
  1275. static void vcc_rx_aal5(struct lanai_vcc *lvcc, int endptr)
  1276. {
  1277. int size;
  1278. struct sk_buff *skb;
  1279. const u32 *x;
  1280. u32 *end = &lvcc->rx.buf.start[endptr * 4];
  1281. int n = ((unsigned long) end) - ((unsigned long) lvcc->rx.buf.ptr);
  1282. if (n < 0)
  1283. n += lanai_buf_size(&lvcc->rx.buf);
  1284. APRINTK(n >= 0 && n < lanai_buf_size(&lvcc->rx.buf) && !(n & 15),
  1285. "vcc_rx_aal5: n out of range (%d/%Zu)\n",
  1286. n, lanai_buf_size(&lvcc->rx.buf));
  1287. /* Recover the second-to-last word to get true pdu length */
  1288. if ((x = &end[-2]) < lvcc->rx.buf.start)
  1289. x = &lvcc->rx.buf.end[-2];
  1290. /*
  1291. * Before we actually read from the buffer, make sure the memory
  1292. * changes have arrived
  1293. */
  1294. rmb();
  1295. size = be32_to_cpup(x) & 0xffff;
  1296. if (unlikely(n != aal5_size(size))) {
  1297. /* Make sure size matches padding */
  1298. printk(KERN_INFO DEV_LABEL "(itf %d): Got bad AAL5 length "
  1299. "on vci=%d - size=%d n=%d\n",
  1300. lvcc->rx.atmvcc->dev->number, lvcc->vci, size, n);
  1301. lvcc->stats.x.aal5.rx_badlen++;
  1302. goto out;
  1303. }
  1304. skb = atm_alloc_charge(lvcc->rx.atmvcc, size, GFP_ATOMIC);
  1305. if (unlikely(skb == NULL)) {
  1306. lvcc->stats.rx_nomem++;
  1307. goto out;
  1308. }
  1309. skb_put(skb, size);
  1310. vcc_rx_memcpy(skb->data, lvcc, size);
  1311. ATM_SKB(skb)->vcc = lvcc->rx.atmvcc;
  1312. __net_timestamp(skb);
  1313. lvcc->rx.atmvcc->push(lvcc->rx.atmvcc, skb);
  1314. atomic_inc(&lvcc->rx.atmvcc->stats->rx);
  1315. out:
  1316. lvcc->rx.buf.ptr = end;
  1317. cardvcc_write(lvcc, endptr, vcc_rxreadptr);
  1318. }
  1319. static void vcc_rx_aal0(struct lanai_dev *lanai)
  1320. {
  1321. printk(KERN_INFO DEV_LABEL ": vcc_rx_aal0: not implemented\n");
  1322. /* Remember to get read_lock(&vcc_sklist_lock) while looking up VC */
  1323. /* Remember to increment lvcc->rx.atmvcc->stats->rx */
  1324. }
  1325. /* -------------------- MANAGING HOST-BASED VCC TABLE: */
  1326. /* Decide whether to use vmalloc or get_zeroed_page for VCC table */
  1327. #if (NUM_VCI * BITS_PER_LONG) <= PAGE_SIZE
  1328. #define VCCTABLE_GETFREEPAGE
  1329. #else
  1330. #include <linux/vmalloc.h>
  1331. #endif
  1332. static int __devinit vcc_table_allocate(struct lanai_dev *lanai)
  1333. {
  1334. #ifdef VCCTABLE_GETFREEPAGE
  1335. APRINTK((lanai->num_vci) * sizeof(struct lanai_vcc *) <= PAGE_SIZE,
  1336. "vcc table > PAGE_SIZE!");
  1337. lanai->vccs = (struct lanai_vcc **) get_zeroed_page(GFP_KERNEL);
  1338. return (lanai->vccs == NULL) ? -ENOMEM : 0;
  1339. #else
  1340. int bytes = (lanai->num_vci) * sizeof(struct lanai_vcc *);
  1341. lanai->vccs = (struct lanai_vcc **) vmalloc(bytes);
  1342. if (unlikely(lanai->vccs == NULL))
  1343. return -ENOMEM;
  1344. memset(lanai->vccs, 0, bytes);
  1345. return 0;
  1346. #endif
  1347. }
  1348. static inline void vcc_table_deallocate(const struct lanai_dev *lanai)
  1349. {
  1350. #ifdef VCCTABLE_GETFREEPAGE
  1351. free_page((unsigned long) lanai->vccs);
  1352. #else
  1353. vfree(lanai->vccs);
  1354. #endif
  1355. }
  1356. /* Allocate a fresh lanai_vcc, with the appropriate things cleared */
  1357. static inline struct lanai_vcc *new_lanai_vcc(void)
  1358. {
  1359. struct lanai_vcc *lvcc;
  1360. lvcc = (struct lanai_vcc *) kmalloc(sizeof(*lvcc), GFP_KERNEL);
  1361. if (likely(lvcc != NULL)) {
  1362. lvcc->vbase = NULL;
  1363. lvcc->rx.atmvcc = lvcc->tx.atmvcc = NULL;
  1364. lvcc->nref = 0;
  1365. memset(&lvcc->stats, 0, sizeof lvcc->stats);
  1366. lvcc->rx.buf.start = lvcc->tx.buf.start = NULL;
  1367. skb_queue_head_init(&lvcc->tx.backlog);
  1368. #ifdef DEBUG
  1369. lvcc->tx.unqueue = NULL;
  1370. lvcc->vci = -1;
  1371. #endif
  1372. }
  1373. return lvcc;
  1374. }
  1375. static int lanai_get_sized_buffer(struct lanai_dev *lanai,
  1376. struct lanai_buffer *buf, int max_sdu, int multiplier,
  1377. const char *name)
  1378. {
  1379. int size;
  1380. if (unlikely(max_sdu < 1))
  1381. max_sdu = 1;
  1382. max_sdu = aal5_size(max_sdu);
  1383. size = (max_sdu + 16) * multiplier + 16;
  1384. lanai_buf_allocate(buf, size, max_sdu + 32, lanai->pci);
  1385. if (unlikely(buf->start == NULL))
  1386. return -ENOMEM;
  1387. if (unlikely(lanai_buf_size(buf) < size))
  1388. printk(KERN_WARNING DEV_LABEL "(itf %d): wanted %d bytes "
  1389. "for %s buffer, got only %Zu\n", lanai->number, size,
  1390. name, lanai_buf_size(buf));
  1391. DPRINTK("Allocated %Zu byte %s buffer\n", lanai_buf_size(buf), name);
  1392. return 0;
  1393. }
  1394. /* Setup a RX buffer for a currently unbound AAL5 vci */
  1395. static inline int lanai_setup_rx_vci_aal5(struct lanai_dev *lanai,
  1396. struct lanai_vcc *lvcc, const struct atm_qos *qos)
  1397. {
  1398. return lanai_get_sized_buffer(lanai, &lvcc->rx.buf,
  1399. qos->rxtp.max_sdu, AAL5_RX_MULTIPLIER, "RX");
  1400. }
  1401. /* Setup a TX buffer for a currently unbound AAL5 vci */
  1402. static int lanai_setup_tx_vci(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
  1403. const struct atm_qos *qos)
  1404. {
  1405. int max_sdu, multiplier;
  1406. if (qos->aal == ATM_AAL0) {
  1407. lvcc->tx.unqueue = vcc_tx_unqueue_aal0;
  1408. max_sdu = ATM_CELL_SIZE - 1;
  1409. multiplier = AAL0_TX_MULTIPLIER;
  1410. } else {
  1411. lvcc->tx.unqueue = vcc_tx_unqueue_aal5;
  1412. max_sdu = qos->txtp.max_sdu;
  1413. multiplier = AAL5_TX_MULTIPLIER;
  1414. }
  1415. return lanai_get_sized_buffer(lanai, &lvcc->tx.buf, max_sdu,
  1416. multiplier, "TX");
  1417. }
  1418. static inline void host_vcc_bind(struct lanai_dev *lanai,
  1419. struct lanai_vcc *lvcc, vci_t vci)
  1420. {
  1421. if (lvcc->vbase != NULL)
  1422. return; /* We already were bound in the other direction */
  1423. DPRINTK("Binding vci %d\n", vci);
  1424. #ifdef USE_POWERDOWN
  1425. if (lanai->nbound++ == 0) {
  1426. DPRINTK("Coming out of powerdown\n");
  1427. lanai->conf1 &= ~CONFIG1_POWERDOWN;
  1428. conf1_write(lanai);
  1429. conf2_write(lanai);
  1430. }
  1431. #endif
  1432. lvcc->vbase = cardvcc_addr(lanai, vci);
  1433. lanai->vccs[lvcc->vci = vci] = lvcc;
  1434. }
  1435. static inline void host_vcc_unbind(struct lanai_dev *lanai,
  1436. struct lanai_vcc *lvcc)
  1437. {
  1438. if (lvcc->vbase == NULL)
  1439. return; /* This vcc was never bound */
  1440. DPRINTK("Unbinding vci %d\n", lvcc->vci);
  1441. lvcc->vbase = NULL;
  1442. lanai->vccs[lvcc->vci] = NULL;
  1443. #ifdef USE_POWERDOWN
  1444. if (--lanai->nbound == 0) {
  1445. DPRINTK("Going into powerdown\n");
  1446. lanai->conf1 |= CONFIG1_POWERDOWN;
  1447. conf1_write(lanai);
  1448. }
  1449. #endif
  1450. }
  1451. /* -------------------- RESET CARD: */
  1452. static void lanai_reset(struct lanai_dev *lanai)
  1453. {
  1454. printk(KERN_CRIT DEV_LABEL "(itf %d): *NOT* reseting - not "
  1455. "implemented\n", lanai->number);
  1456. /* TODO */
  1457. /* The following is just a hack until we write the real
  1458. * resetter - at least ack whatever interrupt sent us
  1459. * here
  1460. */
  1461. reg_write(lanai, INT_ALL, IntAck_Reg);
  1462. lanai->stats.card_reset++;
  1463. }
  1464. /* -------------------- SERVICE LIST UTILITIES: */
  1465. /*
  1466. * Allocate service buffer and tell card about it
  1467. */
  1468. static int __devinit service_buffer_allocate(struct lanai_dev *lanai)
  1469. {
  1470. lanai_buf_allocate(&lanai->service, SERVICE_ENTRIES * 4, 8,
  1471. lanai->pci);
  1472. if (unlikely(lanai->service.start == NULL))
  1473. return -ENOMEM;
  1474. DPRINTK("allocated service buffer at 0x%08lX, size %Zu(%d)\n",
  1475. (unsigned long) lanai->service.start,
  1476. lanai_buf_size(&lanai->service),
  1477. lanai_buf_size_cardorder(&lanai->service));
  1478. /* Clear ServWrite register to be safe */
  1479. reg_write(lanai, 0, ServWrite_Reg);
  1480. /* ServiceStuff register contains size and address of buffer */
  1481. reg_write(lanai,
  1482. SSTUFF_SET_SIZE(lanai_buf_size_cardorder(&lanai->service)) |
  1483. SSTUFF_SET_ADDR(lanai->service.dmaaddr),
  1484. ServiceStuff_Reg);
  1485. return 0;
  1486. }
  1487. static inline void service_buffer_deallocate(struct lanai_dev *lanai)
  1488. {
  1489. lanai_buf_deallocate(&lanai->service, lanai->pci);
  1490. }
  1491. /* Bitfields in service list */
  1492. #define SERVICE_TX (0x80000000) /* Was from transmission */
  1493. #define SERVICE_TRASH (0x40000000) /* RXed PDU was trashed */
  1494. #define SERVICE_CRCERR (0x20000000) /* RXed PDU had CRC error */
  1495. #define SERVICE_CI (0x10000000) /* RXed PDU had CI set */
  1496. #define SERVICE_CLP (0x08000000) /* RXed PDU had CLP set */
  1497. #define SERVICE_STREAM (0x04000000) /* RX Stream mode */
  1498. #define SERVICE_GET_VCI(x) (((x)>>16)&0x3FF)
  1499. #define SERVICE_GET_END(x) ((x)&0x1FFF)
  1500. /* Handle one thing from the service list - returns true if it marked a
  1501. * VCC ready for xmit
  1502. */
  1503. static int handle_service(struct lanai_dev *lanai, u32 s)
  1504. {
  1505. vci_t vci = SERVICE_GET_VCI(s);
  1506. struct lanai_vcc *lvcc;
  1507. read_lock(&vcc_sklist_lock);
  1508. lvcc = lanai->vccs[vci];
  1509. if (unlikely(lvcc == NULL)) {
  1510. read_unlock(&vcc_sklist_lock);
  1511. DPRINTK("(itf %d) got service entry 0x%X for nonexistent "
  1512. "vcc %d\n", lanai->number, (unsigned int) s, vci);
  1513. if (s & SERVICE_TX)
  1514. lanai->stats.service_notx++;
  1515. else
  1516. lanai->stats.service_norx++;
  1517. return 0;
  1518. }
  1519. if (s & SERVICE_TX) { /* segmentation interrupt */
  1520. if (unlikely(lvcc->tx.atmvcc == NULL)) {
  1521. read_unlock(&vcc_sklist_lock);
  1522. DPRINTK("(itf %d) got service entry 0x%X for non-TX "
  1523. "vcc %d\n", lanai->number, (unsigned int) s, vci);
  1524. lanai->stats.service_notx++;
  1525. return 0;
  1526. }
  1527. __set_bit(vci, lanai->transmit_ready);
  1528. lvcc->tx.endptr = SERVICE_GET_END(s);
  1529. read_unlock(&vcc_sklist_lock);
  1530. return 1;
  1531. }
  1532. if (unlikely(lvcc->rx.atmvcc == NULL)) {
  1533. read_unlock(&vcc_sklist_lock);
  1534. DPRINTK("(itf %d) got service entry 0x%X for non-RX "
  1535. "vcc %d\n", lanai->number, (unsigned int) s, vci);
  1536. lanai->stats.service_norx++;
  1537. return 0;
  1538. }
  1539. if (unlikely(lvcc->rx.atmvcc->qos.aal != ATM_AAL5)) {
  1540. read_unlock(&vcc_sklist_lock);
  1541. DPRINTK("(itf %d) got RX service entry 0x%X for non-AAL5 "
  1542. "vcc %d\n", lanai->number, (unsigned int) s, vci);
  1543. lanai->stats.service_rxnotaal5++;
  1544. atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
  1545. return 0;
  1546. }
  1547. if (likely(!(s & (SERVICE_TRASH | SERVICE_STREAM | SERVICE_CRCERR)))) {
  1548. vcc_rx_aal5(lvcc, SERVICE_GET_END(s));
  1549. read_unlock(&vcc_sklist_lock);
  1550. return 0;
  1551. }
  1552. if (s & SERVICE_TRASH) {
  1553. int bytes;
  1554. read_unlock(&vcc_sklist_lock);
  1555. DPRINTK("got trashed rx pdu on vci %d\n", vci);
  1556. atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
  1557. lvcc->stats.x.aal5.service_trash++;
  1558. bytes = (SERVICE_GET_END(s) * 16) -
  1559. (((unsigned long) lvcc->rx.buf.ptr) -
  1560. ((unsigned long) lvcc->rx.buf.start)) + 47;
  1561. if (bytes < 0)
  1562. bytes += lanai_buf_size(&lvcc->rx.buf);
  1563. lanai->stats.ovfl_trash += (bytes / 48);
  1564. return 0;
  1565. }
  1566. if (s & SERVICE_STREAM) {
  1567. read_unlock(&vcc_sklist_lock);
  1568. atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
  1569. lvcc->stats.x.aal5.service_stream++;
  1570. printk(KERN_ERR DEV_LABEL "(itf %d): Got AAL5 stream "
  1571. "PDU on VCI %d!\n", lanai->number, vci);
  1572. lanai_reset(lanai);
  1573. return 0;
  1574. }
  1575. DPRINTK("got rx crc error on vci %d\n", vci);
  1576. atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
  1577. lvcc->stats.x.aal5.service_rxcrc++;
  1578. lvcc->rx.buf.ptr = &lvcc->rx.buf.start[SERVICE_GET_END(s) * 4];
  1579. cardvcc_write(lvcc, SERVICE_GET_END(s), vcc_rxreadptr);
  1580. read_unlock(&vcc_sklist_lock);
  1581. return 0;
  1582. }
  1583. /* Try transmitting on all VCIs that we marked ready to serve */
  1584. static void iter_transmit(struct lanai_dev *lanai, vci_t vci)
  1585. {
  1586. struct lanai_vcc *lvcc = lanai->vccs[vci];
  1587. if (vcc_is_backlogged(lvcc))
  1588. lvcc->tx.unqueue(lanai, lvcc, lvcc->tx.endptr);
  1589. }
  1590. /* Run service queue -- called from interrupt context or with
  1591. * interrupts otherwise disabled and with the lanai->servicelock
  1592. * lock held
  1593. */
  1594. static void run_service(struct lanai_dev *lanai)
  1595. {
  1596. int ntx = 0;
  1597. u32 wreg = reg_read(lanai, ServWrite_Reg);
  1598. const u32 *end = lanai->service.start + wreg;
  1599. while (lanai->service.ptr != end) {
  1600. ntx += handle_service(lanai,
  1601. le32_to_cpup(lanai->service.ptr++));
  1602. if (lanai->service.ptr >= lanai->service.end)
  1603. lanai->service.ptr = lanai->service.start;
  1604. }
  1605. reg_write(lanai, wreg, ServRead_Reg);
  1606. if (ntx != 0) {
  1607. read_lock(&vcc_sklist_lock);
  1608. vci_bitfield_iterate(lanai, lanai->transmit_ready,
  1609. iter_transmit);
  1610. bitmap_zero(lanai->transmit_ready, NUM_VCI);
  1611. read_unlock(&vcc_sklist_lock);
  1612. }
  1613. }
  1614. /* -------------------- GATHER STATISTICS: */
  1615. static void get_statistics(struct lanai_dev *lanai)
  1616. {
  1617. u32 statreg = reg_read(lanai, Statistics_Reg);
  1618. lanai->stats.atm_ovfl += STATS_GET_FIFO_OVFL(statreg);
  1619. lanai->stats.hec_err += STATS_GET_HEC_ERR(statreg);
  1620. lanai->stats.vci_trash += STATS_GET_BAD_VCI(statreg);
  1621. lanai->stats.ovfl_trash += STATS_GET_BUF_OVFL(statreg);
  1622. }
  1623. /* -------------------- POLLING TIMER: */
  1624. #ifndef DEBUG_RW
  1625. /* Try to undequeue 1 backlogged vcc */
  1626. static void iter_dequeue(struct lanai_dev *lanai, vci_t vci)
  1627. {
  1628. struct lanai_vcc *lvcc = lanai->vccs[vci];
  1629. int endptr;
  1630. if (lvcc == NULL || lvcc->tx.atmvcc == NULL ||
  1631. !vcc_is_backlogged(lvcc)) {
  1632. __clear_bit(vci, lanai->backlog_vccs);
  1633. return;
  1634. }
  1635. endptr = TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr));
  1636. lvcc->tx.unqueue(lanai, lvcc, endptr);
  1637. }
  1638. #endif /* !DEBUG_RW */
  1639. static void lanai_timed_poll(unsigned long arg)
  1640. {
  1641. struct lanai_dev *lanai = (struct lanai_dev *) arg;
  1642. #ifndef DEBUG_RW
  1643. unsigned long flags;
  1644. #ifdef USE_POWERDOWN
  1645. if (lanai->conf1 & CONFIG1_POWERDOWN)
  1646. return;
  1647. #endif /* USE_POWERDOWN */
  1648. local_irq_save(flags);
  1649. /* If we can grab the spinlock, check if any services need to be run */
  1650. if (spin_trylock(&lanai->servicelock)) {
  1651. run_service(lanai);
  1652. spin_unlock(&lanai->servicelock);
  1653. }
  1654. /* ...and see if any backlogged VCs can make progress */
  1655. /* unfortunately linux has no read_trylock() currently */
  1656. read_lock(&vcc_sklist_lock);
  1657. vci_bitfield_iterate(lanai, lanai->backlog_vccs, iter_dequeue);
  1658. read_unlock(&vcc_sklist_lock);
  1659. local_irq_restore(flags);
  1660. get_statistics(lanai);
  1661. #endif /* !DEBUG_RW */
  1662. mod_timer(&lanai->timer, jiffies + LANAI_POLL_PERIOD);
  1663. }
  1664. static inline void lanai_timed_poll_start(struct lanai_dev *lanai)
  1665. {
  1666. init_timer(&lanai->timer);
  1667. lanai->timer.expires = jiffies + LANAI_POLL_PERIOD;
  1668. lanai->timer.data = (unsigned long) lanai;
  1669. lanai->timer.function = lanai_timed_poll;
  1670. add_timer(&lanai->timer);
  1671. }
  1672. static inline void lanai_timed_poll_stop(struct lanai_dev *lanai)
  1673. {
  1674. del_timer_sync(&lanai->timer);
  1675. }
  1676. /* -------------------- INTERRUPT SERVICE: */
  1677. static inline void lanai_int_1(struct lanai_dev *lanai, u32 reason)
  1678. {
  1679. u32 ack = 0;
  1680. if (reason & INT_SERVICE) {
  1681. ack = INT_SERVICE;
  1682. spin_lock(&lanai->servicelock);
  1683. run_service(lanai);
  1684. spin_unlock(&lanai->servicelock);
  1685. }
  1686. if (reason & (INT_AAL0_STR | INT_AAL0)) {
  1687. ack |= reason & (INT_AAL0_STR | INT_AAL0);
  1688. vcc_rx_aal0(lanai);
  1689. }
  1690. /* The rest of the interrupts are pretty rare */
  1691. if (ack == reason)
  1692. goto done;
  1693. if (reason & INT_STATS) {
  1694. reason &= ~INT_STATS; /* No need to ack */
  1695. get_statistics(lanai);
  1696. }
  1697. if (reason & INT_STATUS) {
  1698. ack |= reason & INT_STATUS;
  1699. lanai_check_status(lanai);
  1700. }
  1701. if (unlikely(reason & INT_DMASHUT)) {
  1702. printk(KERN_ERR DEV_LABEL "(itf %d): driver error - DMA "
  1703. "shutdown, reason=0x%08X, address=0x%08X\n",
  1704. lanai->number, (unsigned int) (reason & INT_DMASHUT),
  1705. (unsigned int) reg_read(lanai, DMA_Addr_Reg));
  1706. if (reason & INT_TABORTBM) {
  1707. lanai_reset(lanai);
  1708. return;
  1709. }
  1710. ack |= (reason & INT_DMASHUT);
  1711. printk(KERN_ERR DEV_LABEL "(itf %d): re-enabling DMA\n",
  1712. lanai->number);
  1713. conf1_write(lanai);
  1714. lanai->stats.dma_reenable++;
  1715. pcistatus_check(lanai, 0);
  1716. }
  1717. if (unlikely(reason & INT_TABORTSENT)) {
  1718. ack |= (reason & INT_TABORTSENT);
  1719. printk(KERN_ERR DEV_LABEL "(itf %d): sent PCI target abort\n",
  1720. lanai->number);
  1721. pcistatus_check(lanai, 0);
  1722. }
  1723. if (unlikely(reason & INT_SEGSHUT)) {
  1724. printk(KERN_ERR DEV_LABEL "(itf %d): driver error - "
  1725. "segmentation shutdown, reason=0x%08X\n", lanai->number,
  1726. (unsigned int) (reason & INT_SEGSHUT));
  1727. lanai_reset(lanai);
  1728. return;
  1729. }
  1730. if (unlikely(reason & (INT_PING | INT_WAKE))) {
  1731. printk(KERN_ERR DEV_LABEL "(itf %d): driver error - "
  1732. "unexpected interrupt 0x%08X, resetting\n",
  1733. lanai->number,
  1734. (unsigned int) (reason & (INT_PING | INT_WAKE)));
  1735. lanai_reset(lanai);
  1736. return;
  1737. }
  1738. #ifdef DEBUG
  1739. if (unlikely(ack != reason)) {
  1740. DPRINTK("unacked ints: 0x%08X\n",
  1741. (unsigned int) (reason & ~ack));
  1742. ack = reason;
  1743. }
  1744. #endif
  1745. done:
  1746. if (ack != 0)
  1747. reg_write(lanai, ack, IntAck_Reg);
  1748. }
  1749. static irqreturn_t lanai_int(int irq, void *devid, struct pt_regs *regs)
  1750. {
  1751. struct lanai_dev *lanai = (struct lanai_dev *) devid;
  1752. u32 reason;
  1753. (void) irq; (void) regs; /* unused variables */
  1754. #ifdef USE_POWERDOWN
  1755. /*
  1756. * If we're powered down we shouldn't be generating any interrupts -
  1757. * so assume that this is a shared interrupt line and it's for someone
  1758. * else
  1759. */
  1760. if (unlikely(lanai->conf1 & CONFIG1_POWERDOWN))
  1761. return IRQ_NONE;
  1762. #endif
  1763. reason = intr_pending(lanai);
  1764. if (reason == 0)
  1765. return IRQ_NONE; /* Must be for someone else */
  1766. do {
  1767. if (unlikely(reason == 0xFFFFFFFF))
  1768. break; /* Maybe we've been unplugged? */
  1769. lanai_int_1(lanai, reason);
  1770. reason = intr_pending(lanai);
  1771. } while (reason != 0);
  1772. return IRQ_HANDLED;
  1773. }
  1774. /* TODO - it would be nice if we could use the "delayed interrupt" system
  1775. * to some advantage
  1776. */
  1777. /* -------------------- CHECK BOARD ID/REV: */
  1778. /*
  1779. * The board id and revision are stored both in the reset register and
  1780. * in the PCI configuration space - the documentation says to check
  1781. * each of them. If revp!=NULL we store the revision there
  1782. */
  1783. static int check_board_id_and_rev(const char *name, u32 val, int *revp)
  1784. {
  1785. DPRINTK("%s says board_id=%d, board_rev=%d\n", name,
  1786. (int) RESET_GET_BOARD_ID(val),
  1787. (int) RESET_GET_BOARD_REV(val));
  1788. if (RESET_GET_BOARD_ID(val) != BOARD_ID_LANAI256) {
  1789. printk(KERN_ERR DEV_LABEL ": Found %s board-id %d -- not a "
  1790. "Lanai 25.6\n", name, (int) RESET_GET_BOARD_ID(val));
  1791. return -ENODEV;
  1792. }
  1793. if (revp != NULL)
  1794. *revp = RESET_GET_BOARD_REV(val);
  1795. return 0;
  1796. }
  1797. /* -------------------- PCI INITIALIZATION/SHUTDOWN: */
  1798. static int __devinit lanai_pci_start(struct lanai_dev *lanai)
  1799. {
  1800. struct pci_dev *pci = lanai->pci;
  1801. int result;
  1802. u16 w;
  1803. if (pci_enable_device(pci) != 0) {
  1804. printk(KERN_ERR DEV_LABEL "(itf %d): can't enable "
  1805. "PCI device", lanai->number);
  1806. return -ENXIO;
  1807. }
  1808. pci_set_master(pci);
  1809. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) != 0) {
  1810. printk(KERN_WARNING DEV_LABEL
  1811. "(itf %d): No suitable DMA available.\n", lanai->number);
  1812. return -EBUSY;
  1813. }
  1814. if (pci_set_consistent_dma_mask(pci, 0xFFFFFFFF) != 0) {
  1815. printk(KERN_WARNING DEV_LABEL
  1816. "(itf %d): No suitable DMA available.\n", lanai->number);
  1817. return -EBUSY;
  1818. }
  1819. /* Get the pci revision byte */
  1820. result = pci_read_config_byte(pci, PCI_REVISION_ID,
  1821. &lanai->pci_revision);
  1822. if (result != PCIBIOS_SUCCESSFUL) {
  1823. printk(KERN_ERR DEV_LABEL "(itf %d): can't read "
  1824. "PCI_REVISION_ID: %d\n", lanai->number, result);
  1825. return -EINVAL;
  1826. }
  1827. result = pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &w);
  1828. if (result != PCIBIOS_SUCCESSFUL) {
  1829. printk(KERN_ERR DEV_LABEL "(itf %d): can't read "
  1830. "PCI_SUBSYSTEM_ID: %d\n", lanai->number, result);
  1831. return -EINVAL;
  1832. }
  1833. result = check_board_id_and_rev("PCI", w, NULL);
  1834. if (result != 0)
  1835. return result;
  1836. /* Set latency timer to zero as per lanai docs */
  1837. result = pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0);
  1838. if (result != PCIBIOS_SUCCESSFUL) {
  1839. printk(KERN_ERR DEV_LABEL "(itf %d): can't write "
  1840. "PCI_LATENCY_TIMER: %d\n", lanai->number, result);
  1841. return -EINVAL;
  1842. }
  1843. pcistatus_check(lanai, 1);
  1844. pcistatus_check(lanai, 0);
  1845. return 0;
  1846. }
  1847. /* -------------------- VPI/VCI ALLOCATION: */
  1848. /*
  1849. * We _can_ use VCI==0 for normal traffic, but only for UBR (or we'll
  1850. * get a CBRZERO interrupt), and we can use it only if noone is receiving
  1851. * AAL0 traffic (since they will use the same queue) - according to the
  1852. * docs we shouldn't even use it for AAL0 traffic
  1853. */
  1854. static inline int vci0_is_ok(struct lanai_dev *lanai,
  1855. const struct atm_qos *qos)
  1856. {
  1857. if (qos->txtp.traffic_class == ATM_CBR || qos->aal == ATM_AAL0)
  1858. return 0;
  1859. if (qos->rxtp.traffic_class != ATM_NONE) {
  1860. if (lanai->naal0 != 0)
  1861. return 0;
  1862. lanai->conf2 |= CONFIG2_VCI0_NORMAL;
  1863. conf2_write_if_powerup(lanai);
  1864. }
  1865. return 1;
  1866. }
  1867. /* return true if vci is currently unused, or if requested qos is
  1868. * compatible
  1869. */
  1870. static int vci_is_ok(struct lanai_dev *lanai, vci_t vci,
  1871. const struct atm_vcc *atmvcc)
  1872. {
  1873. const struct atm_qos *qos = &atmvcc->qos;
  1874. const struct lanai_vcc *lvcc = lanai->vccs[vci];
  1875. if (vci == 0 && !vci0_is_ok(lanai, qos))
  1876. return 0;
  1877. if (unlikely(lvcc != NULL)) {
  1878. if (qos->rxtp.traffic_class != ATM_NONE &&
  1879. lvcc->rx.atmvcc != NULL && lvcc->rx.atmvcc != atmvcc)
  1880. return 0;
  1881. if (qos->txtp.traffic_class != ATM_NONE &&
  1882. lvcc->tx.atmvcc != NULL && lvcc->tx.atmvcc != atmvcc)
  1883. return 0;
  1884. if (qos->txtp.traffic_class == ATM_CBR &&
  1885. lanai->cbrvcc != NULL && lanai->cbrvcc != atmvcc)
  1886. return 0;
  1887. }
  1888. if (qos->aal == ATM_AAL0 && lanai->naal0 == 0 &&
  1889. qos->rxtp.traffic_class != ATM_NONE) {
  1890. const struct lanai_vcc *vci0 = lanai->vccs[0];
  1891. if (vci0 != NULL && vci0->rx.atmvcc != NULL)
  1892. return 0;
  1893. lanai->conf2 &= ~CONFIG2_VCI0_NORMAL;
  1894. conf2_write_if_powerup(lanai);
  1895. }
  1896. return 1;
  1897. }
  1898. static int lanai_normalize_ci(struct lanai_dev *lanai,
  1899. const struct atm_vcc *atmvcc, short *vpip, vci_t *vcip)
  1900. {
  1901. switch (*vpip) {
  1902. case ATM_VPI_ANY:
  1903. *vpip = 0;
  1904. /* FALLTHROUGH */
  1905. case 0:
  1906. break;
  1907. default:
  1908. return -EADDRINUSE;
  1909. }
  1910. switch (*vcip) {
  1911. case ATM_VCI_ANY:
  1912. for (*vcip = ATM_NOT_RSV_VCI; *vcip < lanai->num_vci;
  1913. (*vcip)++)
  1914. if (vci_is_ok(lanai, *vcip, atmvcc))
  1915. return 0;
  1916. return -EADDRINUSE;
  1917. default:
  1918. if (*vcip >= lanai->num_vci || *vcip < 0 ||
  1919. !vci_is_ok(lanai, *vcip, atmvcc))
  1920. return -EADDRINUSE;
  1921. }
  1922. return 0;
  1923. }
  1924. /* -------------------- MANAGE CBR: */
  1925. /*
  1926. * CBR ICG is stored as a fixed-point number with 4 fractional bits.
  1927. * Note that storing a number greater than 2046.0 will result in
  1928. * incorrect shaping
  1929. */
  1930. #define CBRICG_FRAC_BITS (4)
  1931. #define CBRICG_MAX (2046 << CBRICG_FRAC_BITS)
  1932. /*
  1933. * ICG is related to PCR with the formula PCR = MAXPCR / (ICG + 1)
  1934. * where MAXPCR is (according to the docs) 25600000/(54*8),
  1935. * which is equal to (3125<<9)/27.
  1936. *
  1937. * Solving for ICG, we get:
  1938. * ICG = MAXPCR/PCR - 1
  1939. * ICG = (3125<<9)/(27*PCR) - 1
  1940. * ICG = ((3125<<9) - (27*PCR)) / (27*PCR)
  1941. *
  1942. * The end result is supposed to be a fixed-point number with FRAC_BITS
  1943. * bits of a fractional part, so we keep everything in the numerator
  1944. * shifted by that much as we compute
  1945. *
  1946. */
  1947. static int pcr_to_cbricg(const struct atm_qos *qos)
  1948. {
  1949. int rounddown = 0; /* 1 = Round PCR down, i.e. round ICG _up_ */
  1950. int x, icg, pcr = atm_pcr_goal(&qos->txtp);
  1951. if (pcr == 0) /* Use maximum bandwidth */
  1952. return 0;
  1953. if (pcr < 0) {
  1954. rounddown = 1;
  1955. pcr = -pcr;
  1956. }
  1957. x = pcr * 27;
  1958. icg = (3125 << (9 + CBRICG_FRAC_BITS)) - (x << CBRICG_FRAC_BITS);
  1959. if (rounddown)
  1960. icg += x - 1;
  1961. icg /= x;
  1962. if (icg > CBRICG_MAX)
  1963. icg = CBRICG_MAX;
  1964. DPRINTK("pcr_to_cbricg: pcr=%d rounddown=%c icg=%d\n",
  1965. pcr, rounddown ? 'Y' : 'N', icg);
  1966. return icg;
  1967. }
  1968. static inline void lanai_cbr_setup(struct lanai_dev *lanai)
  1969. {
  1970. reg_write(lanai, pcr_to_cbricg(&lanai->cbrvcc->qos), CBR_ICG_Reg);
  1971. reg_write(lanai, lanai->cbrvcc->vci, CBR_PTR_Reg);
  1972. lanai->conf2 |= CONFIG2_CBR_ENABLE;
  1973. conf2_write(lanai);
  1974. }
  1975. static inline void lanai_cbr_shutdown(struct lanai_dev *lanai)
  1976. {
  1977. lanai->conf2 &= ~CONFIG2_CBR_ENABLE;
  1978. conf2_write(lanai);
  1979. }
  1980. /* -------------------- OPERATIONS: */
  1981. /* setup a newly detected device */
  1982. static int __devinit lanai_dev_open(struct atm_dev *atmdev)
  1983. {
  1984. struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
  1985. unsigned long raw_base;
  1986. int result;
  1987. DPRINTK("In lanai_dev_open()\n");
  1988. /* Basic device fields */
  1989. lanai->number = atmdev->number;
  1990. lanai->num_vci = NUM_VCI;
  1991. bitmap_zero(lanai->backlog_vccs, NUM_VCI);
  1992. bitmap_zero(lanai->transmit_ready, NUM_VCI);
  1993. lanai->naal0 = 0;
  1994. #ifdef USE_POWERDOWN
  1995. lanai->nbound = 0;
  1996. #endif
  1997. lanai->cbrvcc = NULL;
  1998. memset(&lanai->stats, 0, sizeof lanai->stats);
  1999. spin_lock_init(&lanai->endtxlock);
  2000. spin_lock_init(&lanai->servicelock);
  2001. atmdev->ci_range.vpi_bits = 0;
  2002. atmdev->ci_range.vci_bits = 0;
  2003. while (1 << atmdev->ci_range.vci_bits < lanai->num_vci)
  2004. atmdev->ci_range.vci_bits++;
  2005. atmdev->link_rate = ATM_25_PCR;
  2006. /* 3.2: PCI initialization */
  2007. if ((result = lanai_pci_start(lanai)) != 0)
  2008. goto error;
  2009. raw_base = lanai->pci->resource[0].start;
  2010. lanai->base = (bus_addr_t) ioremap(raw_base, LANAI_MAPPING_SIZE);
  2011. if (lanai->base == NULL) {
  2012. printk(KERN_ERR DEV_LABEL ": couldn't remap I/O space\n");
  2013. goto error_pci;
  2014. }
  2015. /* 3.3: Reset lanai and PHY */
  2016. reset_board(lanai);
  2017. lanai->conf1 = reg_read(lanai, Config1_Reg);
  2018. lanai->conf1 &= ~(CONFIG1_GPOUT1 | CONFIG1_POWERDOWN |
  2019. CONFIG1_MASK_LEDMODE);
  2020. lanai->conf1 |= CONFIG1_SET_LEDMODE(LEDMODE_NOT_SOOL);
  2021. reg_write(lanai, lanai->conf1 | CONFIG1_GPOUT1, Config1_Reg);
  2022. udelay(1000);
  2023. conf1_write(lanai);
  2024. /*
  2025. * 3.4: Turn on endian mode for big-endian hardware
  2026. * We don't actually want to do this - the actual bit fields
  2027. * in the endian register are not documented anywhere.
  2028. * Instead we do the bit-flipping ourselves on big-endian
  2029. * hardware.
  2030. *
  2031. * 3.5: get the board ID/rev by reading the reset register
  2032. */
  2033. result = check_board_id_and_rev("register",
  2034. reg_read(lanai, Reset_Reg), &lanai->board_rev);
  2035. if (result != 0)
  2036. goto error_unmap;
  2037. /* 3.6: read EEPROM */
  2038. if ((result = eeprom_read(lanai)) != 0)
  2039. goto error_unmap;
  2040. if ((result = eeprom_validate(lanai)) != 0)
  2041. goto error_unmap;
  2042. /* 3.7: re-reset PHY, do loopback tests, setup PHY */
  2043. reg_write(lanai, lanai->conf1 | CONFIG1_GPOUT1, Config1_Reg);
  2044. udelay(1000);
  2045. conf1_write(lanai);
  2046. /* TODO - loopback tests */
  2047. lanai->conf1 |= (CONFIG1_GPOUT2 | CONFIG1_GPOUT3 | CONFIG1_DMA_ENABLE);
  2048. conf1_write(lanai);
  2049. /* 3.8/3.9: test and initialize card SRAM */
  2050. if ((result = sram_test_and_clear(lanai)) != 0)
  2051. goto error_unmap;
  2052. /* 3.10: initialize lanai registers */
  2053. lanai->conf1 |= CONFIG1_DMA_ENABLE;
  2054. conf1_write(lanai);
  2055. if ((result = service_buffer_allocate(lanai)) != 0)
  2056. goto error_unmap;
  2057. if ((result = vcc_table_allocate(lanai)) != 0)
  2058. goto error_service;
  2059. lanai->conf2 = (lanai->num_vci >= 512 ? CONFIG2_HOWMANY : 0) |
  2060. CONFIG2_HEC_DROP | /* ??? */ CONFIG2_PTI7_MODE;
  2061. conf2_write(lanai);
  2062. reg_write(lanai, TX_FIFO_DEPTH, TxDepth_Reg);
  2063. reg_write(lanai, 0, CBR_ICG_Reg); /* CBR defaults to no limit */
  2064. if ((result = request_irq(lanai->pci->irq, lanai_int, SA_SHIRQ,
  2065. DEV_LABEL, lanai)) != 0) {
  2066. printk(KERN_ERR DEV_LABEL ": can't allocate interrupt\n");
  2067. goto error_vcctable;
  2068. }
  2069. mb(); /* Make sure that all that made it */
  2070. intr_enable(lanai, INT_ALL & ~(INT_PING | INT_WAKE));
  2071. /* 3.11: initialize loop mode (i.e. turn looping off) */
  2072. lanai->conf1 = (lanai->conf1 & ~CONFIG1_MASK_LOOPMODE) |
  2073. CONFIG1_SET_LOOPMODE(LOOPMODE_NORMAL) |
  2074. CONFIG1_GPOUT2 | CONFIG1_GPOUT3;
  2075. conf1_write(lanai);
  2076. lanai->status = reg_read(lanai, Status_Reg);
  2077. /* We're now done initializing this card */
  2078. #ifdef USE_POWERDOWN
  2079. lanai->conf1 |= CONFIG1_POWERDOWN;
  2080. conf1_write(lanai);
  2081. #endif
  2082. memcpy(atmdev->esi, eeprom_mac(lanai), ESI_LEN);
  2083. lanai_timed_poll_start(lanai);
  2084. printk(KERN_NOTICE DEV_LABEL "(itf %d): rev.%d, base=0x%lx, irq=%u "
  2085. "(%02X-%02X-%02X-%02X-%02X-%02X)\n", lanai->number,
  2086. (int) lanai->pci_revision, (unsigned long) lanai->base,
  2087. lanai->pci->irq,
  2088. atmdev->esi[0], atmdev->esi[1], atmdev->esi[2],
  2089. atmdev->esi[3], atmdev->esi[4], atmdev->esi[5]);
  2090. printk(KERN_NOTICE DEV_LABEL "(itf %d): LANAI%s, serialno=%u(0x%X), "
  2091. "board_rev=%d\n", lanai->number,
  2092. lanai->type==lanai2 ? "2" : "HB", (unsigned int) lanai->serialno,
  2093. (unsigned int) lanai->serialno, lanai->board_rev);
  2094. return 0;
  2095. error_vcctable:
  2096. vcc_table_deallocate(lanai);
  2097. error_service:
  2098. service_buffer_deallocate(lanai);
  2099. error_unmap:
  2100. reset_board(lanai);
  2101. #ifdef USE_POWERDOWN
  2102. lanai->conf1 = reg_read(lanai, Config1_Reg) | CONFIG1_POWERDOWN;
  2103. conf1_write(lanai);
  2104. #endif
  2105. iounmap(lanai->base);
  2106. error_pci:
  2107. pci_disable_device(lanai->pci);
  2108. error:
  2109. return result;
  2110. }
  2111. /* called when device is being shutdown, and all vcc's are gone - higher
  2112. * levels will deallocate the atm device for us
  2113. */
  2114. static void lanai_dev_close(struct atm_dev *atmdev)
  2115. {
  2116. struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
  2117. printk(KERN_INFO DEV_LABEL "(itf %d): shutting down interface\n",
  2118. lanai->number);
  2119. lanai_timed_poll_stop(lanai);
  2120. #ifdef USE_POWERDOWN
  2121. lanai->conf1 = reg_read(lanai, Config1_Reg) & ~CONFIG1_POWERDOWN;
  2122. conf1_write(lanai);
  2123. #endif
  2124. intr_disable(lanai, INT_ALL);
  2125. free_irq(lanai->pci->irq, lanai);
  2126. reset_board(lanai);
  2127. #ifdef USE_POWERDOWN
  2128. lanai->conf1 |= CONFIG1_POWERDOWN;
  2129. conf1_write(lanai);
  2130. #endif
  2131. pci_disable_device(lanai->pci);
  2132. vcc_table_deallocate(lanai);
  2133. service_buffer_deallocate(lanai);
  2134. iounmap(lanai->base);
  2135. kfree(lanai);
  2136. }
  2137. /* close a vcc */
  2138. static void lanai_close(struct atm_vcc *atmvcc)
  2139. {
  2140. struct lanai_vcc *lvcc = (struct lanai_vcc *) atmvcc->dev_data;
  2141. struct lanai_dev *lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
  2142. if (lvcc == NULL)
  2143. return;
  2144. clear_bit(ATM_VF_READY, &atmvcc->flags);
  2145. clear_bit(ATM_VF_PARTIAL, &atmvcc->flags);
  2146. if (lvcc->rx.atmvcc == atmvcc) {
  2147. lanai_shutdown_rx_vci(lvcc);
  2148. if (atmvcc->qos.aal == ATM_AAL0) {
  2149. if (--lanai->naal0 <= 0)
  2150. aal0_buffer_free(lanai);
  2151. } else
  2152. lanai_buf_deallocate(&lvcc->rx.buf, lanai->pci);
  2153. lvcc->rx.atmvcc = NULL;
  2154. }
  2155. if (lvcc->tx.atmvcc == atmvcc) {
  2156. if (atmvcc == lanai->cbrvcc) {
  2157. if (lvcc->vbase != NULL)
  2158. lanai_cbr_shutdown(lanai);
  2159. lanai->cbrvcc = NULL;
  2160. }
  2161. lanai_shutdown_tx_vci(lanai, lvcc);
  2162. lanai_buf_deallocate(&lvcc->tx.buf, lanai->pci);
  2163. lvcc->tx.atmvcc = NULL;
  2164. }
  2165. if (--lvcc->nref == 0) {
  2166. host_vcc_unbind(lanai, lvcc);
  2167. kfree(lvcc);
  2168. }
  2169. atmvcc->dev_data = NULL;
  2170. clear_bit(ATM_VF_ADDR, &atmvcc->flags);
  2171. }
  2172. /* open a vcc on the card to vpi/vci */
  2173. static int lanai_open(struct atm_vcc *atmvcc)
  2174. {
  2175. struct lanai_dev *lanai;
  2176. struct lanai_vcc *lvcc;
  2177. int result = 0;
  2178. int vci = atmvcc->vci;
  2179. short vpi = atmvcc->vpi;
  2180. /* we don't support partial open - it's not really useful anyway */
  2181. if ((test_bit(ATM_VF_PARTIAL, &atmvcc->flags)) ||
  2182. (vpi == ATM_VPI_UNSPEC) || (vci == ATM_VCI_UNSPEC))
  2183. return -EINVAL;
  2184. lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
  2185. result = lanai_normalize_ci(lanai, atmvcc, &vpi, &vci);
  2186. if (unlikely(result != 0))
  2187. goto out;
  2188. set_bit(ATM_VF_ADDR, &atmvcc->flags);
  2189. if (atmvcc->qos.aal != ATM_AAL0 && atmvcc->qos.aal != ATM_AAL5)
  2190. return -EINVAL;
  2191. DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n", lanai->number,
  2192. (int) vpi, vci);
  2193. lvcc = lanai->vccs[vci];
  2194. if (lvcc == NULL) {
  2195. lvcc = new_lanai_vcc();
  2196. if (unlikely(lvcc == NULL))
  2197. return -ENOMEM;
  2198. atmvcc->dev_data = lvcc;
  2199. }
  2200. lvcc->nref++;
  2201. if (atmvcc->qos.rxtp.traffic_class != ATM_NONE) {
  2202. APRINTK(lvcc->rx.atmvcc == NULL, "rx.atmvcc!=NULL, vci=%d\n",
  2203. vci);
  2204. if (atmvcc->qos.aal == ATM_AAL0) {
  2205. if (lanai->naal0 == 0)
  2206. result = aal0_buffer_allocate(lanai);
  2207. } else
  2208. result = lanai_setup_rx_vci_aal5(
  2209. lanai, lvcc, &atmvcc->qos);
  2210. if (unlikely(result != 0))
  2211. goto out_free;
  2212. lvcc->rx.atmvcc = atmvcc;
  2213. lvcc->stats.rx_nomem = 0;
  2214. lvcc->stats.x.aal5.rx_badlen = 0;
  2215. lvcc->stats.x.aal5.service_trash = 0;
  2216. lvcc->stats.x.aal5.service_stream = 0;
  2217. lvcc->stats.x.aal5.service_rxcrc = 0;
  2218. if (atmvcc->qos.aal == ATM_AAL0)
  2219. lanai->naal0++;
  2220. }
  2221. if (atmvcc->qos.txtp.traffic_class != ATM_NONE) {
  2222. APRINTK(lvcc->tx.atmvcc == NULL, "tx.atmvcc!=NULL, vci=%d\n",
  2223. vci);
  2224. result = lanai_setup_tx_vci(lanai, lvcc, &atmvcc->qos);
  2225. if (unlikely(result != 0))
  2226. goto out_free;
  2227. lvcc->tx.atmvcc = atmvcc;
  2228. if (atmvcc->qos.txtp.traffic_class == ATM_CBR) {
  2229. APRINTK(lanai->cbrvcc == NULL,
  2230. "cbrvcc!=NULL, vci=%d\n", vci);
  2231. lanai->cbrvcc = atmvcc;
  2232. }
  2233. }
  2234. host_vcc_bind(lanai, lvcc, vci);
  2235. /*
  2236. * Make sure everything made it to RAM before we tell the card about
  2237. * the VCC
  2238. */
  2239. wmb();
  2240. if (atmvcc == lvcc->rx.atmvcc)
  2241. host_vcc_start_rx(lvcc);
  2242. if (atmvcc == lvcc->tx.atmvcc) {
  2243. host_vcc_start_tx(lvcc);
  2244. if (lanai->cbrvcc == atmvcc)
  2245. lanai_cbr_setup(lanai);
  2246. }
  2247. set_bit(ATM_VF_READY, &atmvcc->flags);
  2248. return 0;
  2249. out_free:
  2250. lanai_close(atmvcc);
  2251. out:
  2252. return result;
  2253. }
  2254. #if 0
  2255. /* ioctl operations for card */
  2256. /* NOTE: these are all DEBUGGING ONLY currently */
  2257. static int lanai_ioctl(struct atm_dev *atmdev, unsigned int cmd, void __user *arg)
  2258. {
  2259. int result = 0;
  2260. struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
  2261. switch(cmd) {
  2262. case 2106275:
  2263. shutdown_atm_dev(atmdev);
  2264. return 0;
  2265. case 2200000: {
  2266. unsigned long flags;
  2267. spin_lock_irqsave(&lanai->servicelock, flags);
  2268. run_service(lanai);
  2269. spin_unlock_irqrestore(&lanai->servicelock, flags);
  2270. return 0; }
  2271. case 2200002:
  2272. get_statistics(lanai);
  2273. return 0;
  2274. case 2200003: {
  2275. unsigned int i;
  2276. for (i = 0; i <= 0x5C ; i += 4) {
  2277. if (i==0x48) /* Write-only butt reg */
  2278. continue;
  2279. printk(KERN_CRIT DEV_LABEL " 0x%02X: "
  2280. "0x%08X\n", i,
  2281. (unsigned int) readl(lanai->base + i));
  2282. barrier(); mb();
  2283. pcistatus_check(lanai, 0);
  2284. barrier(); mb();
  2285. }
  2286. return 0; }
  2287. case 2200004: {
  2288. u8 b;
  2289. u16 w;
  2290. u32 dw;
  2291. struct pci_dev *pci = lanai->pci;
  2292. (void) pci_read_config_word(pci, PCI_VENDOR_ID, &w);
  2293. DPRINTK("vendor = 0x%X\n", (unsigned int) w);
  2294. (void) pci_read_config_word(pci, PCI_DEVICE_ID, &w);
  2295. DPRINTK("device = 0x%X\n", (unsigned int) w);
  2296. (void) pci_read_config_word(pci, PCI_COMMAND, &w);
  2297. DPRINTK("command = 0x%X\n", (unsigned int) w);
  2298. (void) pci_read_config_word(pci, PCI_STATUS, &w);
  2299. DPRINTK("status = 0x%X\n", (unsigned int) w);
  2300. (void) pci_read_config_dword(pci,
  2301. PCI_CLASS_REVISION, &dw);
  2302. DPRINTK("class/revision = 0x%X\n", (unsigned int) dw);
  2303. (void) pci_read_config_byte(pci,
  2304. PCI_CACHE_LINE_SIZE, &b);
  2305. DPRINTK("cache line size = 0x%X\n", (unsigned int) b);
  2306. (void) pci_read_config_byte(pci, PCI_LATENCY_TIMER, &b);
  2307. DPRINTK("latency = %d (0x%X)\n",
  2308. (int) b, (unsigned int) b);
  2309. (void) pci_read_config_byte(pci, PCI_HEADER_TYPE, &b);
  2310. DPRINTK("header type = 0x%X\n", (unsigned int) b);
  2311. (void) pci_read_config_byte(pci, PCI_BIST, &b);
  2312. DPRINTK("bist = 0x%X\n", (unsigned int) b);
  2313. /* skipping a few here */
  2314. (void) pci_read_config_byte(pci,
  2315. PCI_INTERRUPT_LINE, &b);
  2316. DPRINTK("pci_int_line = 0x%X\n", (unsigned int) b);
  2317. (void) pci_read_config_byte(pci,
  2318. PCI_INTERRUPT_PIN, &b);
  2319. DPRINTK("pci_int_pin = 0x%X\n", (unsigned int) b);
  2320. (void) pci_read_config_byte(pci, PCI_MIN_GNT, &b);
  2321. DPRINTK("min_gnt = 0x%X\n", (unsigned int) b);
  2322. (void) pci_read_config_byte(pci, PCI_MAX_LAT, &b);
  2323. DPRINTK("max_lat = 0x%X\n", (unsigned int) b); }
  2324. return 0;
  2325. #ifdef USE_POWERDOWN
  2326. case 2200005:
  2327. DPRINTK("Coming out of powerdown\n");
  2328. lanai->conf1 &= ~CONFIG1_POWERDOWN;
  2329. conf1_write(lanai);
  2330. return 0;
  2331. #endif
  2332. default:
  2333. result = -ENOIOCTLCMD;
  2334. }
  2335. return result;
  2336. }
  2337. #else /* !0 */
  2338. #define lanai_ioctl NULL
  2339. #endif /* 0 */
  2340. static int lanai_send(struct atm_vcc *atmvcc, struct sk_buff *skb)
  2341. {
  2342. struct lanai_vcc *lvcc = (struct lanai_vcc *) atmvcc->dev_data;
  2343. struct lanai_dev *lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
  2344. unsigned long flags;
  2345. if (unlikely(lvcc == NULL || lvcc->vbase == NULL ||
  2346. lvcc->tx.atmvcc != atmvcc))
  2347. goto einval;
  2348. #ifdef DEBUG
  2349. if (unlikely(skb == NULL)) {
  2350. DPRINTK("lanai_send: skb==NULL for vci=%d\n", atmvcc->vci);
  2351. goto einval;
  2352. }
  2353. if (unlikely(lanai == NULL)) {
  2354. DPRINTK("lanai_send: lanai==NULL for vci=%d\n", atmvcc->vci);
  2355. goto einval;
  2356. }
  2357. #endif
  2358. ATM_SKB(skb)->vcc = atmvcc;
  2359. switch (atmvcc->qos.aal) {
  2360. case ATM_AAL5:
  2361. read_lock_irqsave(&vcc_sklist_lock, flags);
  2362. vcc_tx_aal5(lanai, lvcc, skb);
  2363. read_unlock_irqrestore(&vcc_sklist_lock, flags);
  2364. return 0;
  2365. case ATM_AAL0:
  2366. if (unlikely(skb->len != ATM_CELL_SIZE-1))
  2367. goto einval;
  2368. /* NOTE - this next line is technically invalid - we haven't unshared skb */
  2369. cpu_to_be32s((u32 *) skb->data);
  2370. read_lock_irqsave(&vcc_sklist_lock, flags);
  2371. vcc_tx_aal0(lanai, lvcc, skb);
  2372. read_unlock_irqrestore(&vcc_sklist_lock, flags);
  2373. return 0;
  2374. }
  2375. DPRINTK("lanai_send: bad aal=%d on vci=%d\n", (int) atmvcc->qos.aal,
  2376. atmvcc->vci);
  2377. einval:
  2378. lanai_free_skb(atmvcc, skb);
  2379. return -EINVAL;
  2380. }
  2381. static int lanai_change_qos(struct atm_vcc *atmvcc,
  2382. /*const*/ struct atm_qos *qos, int flags)
  2383. {
  2384. return -EBUSY; /* TODO: need to write this */
  2385. }
  2386. #ifndef CONFIG_PROC_FS
  2387. #define lanai_proc_read NULL
  2388. #else
  2389. static int lanai_proc_read(struct atm_dev *atmdev, loff_t *pos, char *page)
  2390. {
  2391. struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
  2392. loff_t left = *pos;
  2393. struct lanai_vcc *lvcc;
  2394. if (left-- == 0)
  2395. return sprintf(page, DEV_LABEL "(itf %d): chip=LANAI%s, "
  2396. "serial=%u, magic=0x%08X, num_vci=%d\n",
  2397. atmdev->number, lanai->type==lanai2 ? "2" : "HB",
  2398. (unsigned int) lanai->serialno,
  2399. (unsigned int) lanai->magicno, lanai->num_vci);
  2400. if (left-- == 0)
  2401. return sprintf(page, "revision: board=%d, pci_if=%d\n",
  2402. lanai->board_rev, (int) lanai->pci_revision);
  2403. if (left-- == 0)
  2404. return sprintf(page, "EEPROM ESI: "
  2405. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  2406. lanai->eeprom[EEPROM_MAC + 0],
  2407. lanai->eeprom[EEPROM_MAC + 1],
  2408. lanai->eeprom[EEPROM_MAC + 2],
  2409. lanai->eeprom[EEPROM_MAC + 3],
  2410. lanai->eeprom[EEPROM_MAC + 4],
  2411. lanai->eeprom[EEPROM_MAC + 5]);
  2412. if (left-- == 0)
  2413. return sprintf(page, "status: SOOL=%d, LOCD=%d, LED=%d, "
  2414. "GPIN=%d\n", (lanai->status & STATUS_SOOL) ? 1 : 0,
  2415. (lanai->status & STATUS_LOCD) ? 1 : 0,
  2416. (lanai->status & STATUS_LED) ? 1 : 0,
  2417. (lanai->status & STATUS_GPIN) ? 1 : 0);
  2418. if (left-- == 0)
  2419. return sprintf(page, "global buffer sizes: service=%Zu, "
  2420. "aal0_rx=%Zu\n", lanai_buf_size(&lanai->service),
  2421. lanai->naal0 ? lanai_buf_size(&lanai->aal0buf) : 0);
  2422. if (left-- == 0) {
  2423. get_statistics(lanai);
  2424. return sprintf(page, "cells in error: overflow=%u, "
  2425. "closed_vci=%u, bad_HEC=%u, rx_fifo=%u\n",
  2426. lanai->stats.ovfl_trash, lanai->stats.vci_trash,
  2427. lanai->stats.hec_err, lanai->stats.atm_ovfl);
  2428. }
  2429. if (left-- == 0)
  2430. return sprintf(page, "PCI errors: parity_detect=%u, "
  2431. "master_abort=%u, master_target_abort=%u,\n",
  2432. lanai->stats.pcierr_parity_detect,
  2433. lanai->stats.pcierr_serr_set,
  2434. lanai->stats.pcierr_m_target_abort);
  2435. if (left-- == 0)
  2436. return sprintf(page, " slave_target_abort=%u, "
  2437. "master_parity=%u\n", lanai->stats.pcierr_s_target_abort,
  2438. lanai->stats.pcierr_master_parity);
  2439. if (left-- == 0)
  2440. return sprintf(page, " no_tx=%u, "
  2441. "no_rx=%u, bad_rx_aal=%u\n", lanai->stats.service_norx,
  2442. lanai->stats.service_notx,
  2443. lanai->stats.service_rxnotaal5);
  2444. if (left-- == 0)
  2445. return sprintf(page, "resets: dma=%u, card=%u\n",
  2446. lanai->stats.dma_reenable, lanai->stats.card_reset);
  2447. /* At this point, "left" should be the VCI we're looking for */
  2448. read_lock(&vcc_sklist_lock);
  2449. for (; ; left++) {
  2450. if (left >= NUM_VCI) {
  2451. left = 0;
  2452. goto out;
  2453. }
  2454. if ((lvcc = lanai->vccs[left]) != NULL)
  2455. break;
  2456. (*pos)++;
  2457. }
  2458. /* Note that we re-use "left" here since we're done with it */
  2459. left = sprintf(page, "VCI %4d: nref=%d, rx_nomem=%u", (vci_t) left,
  2460. lvcc->nref, lvcc->stats.rx_nomem);
  2461. if (lvcc->rx.atmvcc != NULL) {
  2462. left += sprintf(&page[left], ",\n rx_AAL=%d",
  2463. lvcc->rx.atmvcc->qos.aal == ATM_AAL5 ? 5 : 0);
  2464. if (lvcc->rx.atmvcc->qos.aal == ATM_AAL5)
  2465. left += sprintf(&page[left], ", rx_buf_size=%Zu, "
  2466. "rx_bad_len=%u,\n rx_service_trash=%u, "
  2467. "rx_service_stream=%u, rx_bad_crc=%u",
  2468. lanai_buf_size(&lvcc->rx.buf),
  2469. lvcc->stats.x.aal5.rx_badlen,
  2470. lvcc->stats.x.aal5.service_trash,
  2471. lvcc->stats.x.aal5.service_stream,
  2472. lvcc->stats.x.aal5.service_rxcrc);
  2473. }
  2474. if (lvcc->tx.atmvcc != NULL)
  2475. left += sprintf(&page[left], ",\n tx_AAL=%d, "
  2476. "tx_buf_size=%Zu, tx_qos=%cBR, tx_backlogged=%c",
  2477. lvcc->tx.atmvcc->qos.aal == ATM_AAL5 ? 5 : 0,
  2478. lanai_buf_size(&lvcc->tx.buf),
  2479. lvcc->tx.atmvcc == lanai->cbrvcc ? 'C' : 'U',
  2480. vcc_is_backlogged(lvcc) ? 'Y' : 'N');
  2481. page[left++] = '\n';
  2482. page[left] = '\0';
  2483. out:
  2484. read_unlock(&vcc_sklist_lock);
  2485. return left;
  2486. }
  2487. #endif /* CONFIG_PROC_FS */
  2488. /* -------------------- HOOKS: */
  2489. static const struct atmdev_ops ops = {
  2490. .dev_close = lanai_dev_close,
  2491. .open = lanai_open,
  2492. .close = lanai_close,
  2493. .ioctl = lanai_ioctl,
  2494. .getsockopt = NULL,
  2495. .setsockopt = NULL,
  2496. .send = lanai_send,
  2497. .phy_put = NULL,
  2498. .phy_get = NULL,
  2499. .change_qos = lanai_change_qos,
  2500. .proc_read = lanai_proc_read,
  2501. .owner = THIS_MODULE
  2502. };
  2503. /* initialize one probed card */
  2504. static int __devinit lanai_init_one(struct pci_dev *pci,
  2505. const struct pci_device_id *ident)
  2506. {
  2507. struct lanai_dev *lanai;
  2508. struct atm_dev *atmdev;
  2509. int result;
  2510. lanai = (struct lanai_dev *) kmalloc(sizeof(*lanai), GFP_KERNEL);
  2511. if (lanai == NULL) {
  2512. printk(KERN_ERR DEV_LABEL
  2513. ": couldn't allocate dev_data structure!\n");
  2514. return -ENOMEM;
  2515. }
  2516. atmdev = atm_dev_register(DEV_LABEL, &ops, -1, NULL);
  2517. if (atmdev == NULL) {
  2518. printk(KERN_ERR DEV_LABEL
  2519. ": couldn't register atm device!\n");
  2520. kfree(lanai);
  2521. return -EBUSY;
  2522. }
  2523. atmdev->dev_data = lanai;
  2524. lanai->pci = pci;
  2525. lanai->type = (enum lanai_type) ident->device;
  2526. result = lanai_dev_open(atmdev);
  2527. if (result != 0) {
  2528. DPRINTK("lanai_start() failed, err=%d\n", -result);
  2529. atm_dev_deregister(atmdev);
  2530. kfree(lanai);
  2531. }
  2532. return result;
  2533. }
  2534. static struct pci_device_id lanai_pci_tbl[] = {
  2535. {
  2536. PCI_VENDOR_ID_EF, PCI_VENDOR_ID_EF_ATM_LANAI2,
  2537. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  2538. },
  2539. {
  2540. PCI_VENDOR_ID_EF, PCI_VENDOR_ID_EF_ATM_LANAIHB,
  2541. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  2542. },
  2543. { 0, } /* terminal entry */
  2544. };
  2545. MODULE_DEVICE_TABLE(pci, lanai_pci_tbl);
  2546. static struct pci_driver lanai_driver = {
  2547. .name = DEV_LABEL,
  2548. .id_table = lanai_pci_tbl,
  2549. .probe = lanai_init_one,
  2550. };
  2551. static int __init lanai_module_init(void)
  2552. {
  2553. int x;
  2554. x = pci_register_driver(&lanai_driver);
  2555. if (x != 0)
  2556. printk(KERN_ERR DEV_LABEL ": no adapter found\n");
  2557. return x;
  2558. }
  2559. static void __exit lanai_module_exit(void)
  2560. {
  2561. /* We'll only get called when all the interfaces are already
  2562. * gone, so there isn't much to do
  2563. */
  2564. DPRINTK("cleanup_module()\n");
  2565. pci_unregister_driver(&lanai_driver);
  2566. }
  2567. module_init(lanai_module_init);
  2568. module_exit(lanai_module_exit);
  2569. MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
  2570. MODULE_DESCRIPTION("Efficient Networks Speedstream 3010 driver");
  2571. MODULE_LICENSE("GPL");