lanai.c 80 KB

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