lanai.c 82 KB

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