lanai.c 82 KB

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