lanai.c 82 KB

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