lanai.c 82 KB

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