ohci.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490
  1. /*
  2. * Driver for OHCI 1394 controllers
  3. *
  4. * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/bitops.h>
  21. #include <linux/bug.h>
  22. #include <linux/compiler.h>
  23. #include <linux/delay.h>
  24. #include <linux/device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/firewire.h>
  27. #include <linux/firewire-constants.h>
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/io.h>
  31. #include <linux/kernel.h>
  32. #include <linux/list.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/mutex.h>
  37. #include <linux/pci.h>
  38. #include <linux/pci_ids.h>
  39. #include <linux/slab.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/string.h>
  42. #include <linux/time.h>
  43. #include <linux/vmalloc.h>
  44. #include <asm/byteorder.h>
  45. #include <asm/page.h>
  46. #include <asm/system.h>
  47. #ifdef CONFIG_PPC_PMAC
  48. #include <asm/pmac_feature.h>
  49. #endif
  50. #include "core.h"
  51. #include "ohci.h"
  52. #define DESCRIPTOR_OUTPUT_MORE 0
  53. #define DESCRIPTOR_OUTPUT_LAST (1 << 12)
  54. #define DESCRIPTOR_INPUT_MORE (2 << 12)
  55. #define DESCRIPTOR_INPUT_LAST (3 << 12)
  56. #define DESCRIPTOR_STATUS (1 << 11)
  57. #define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
  58. #define DESCRIPTOR_PING (1 << 7)
  59. #define DESCRIPTOR_YY (1 << 6)
  60. #define DESCRIPTOR_NO_IRQ (0 << 4)
  61. #define DESCRIPTOR_IRQ_ERROR (1 << 4)
  62. #define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
  63. #define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
  64. #define DESCRIPTOR_WAIT (3 << 0)
  65. struct descriptor {
  66. __le16 req_count;
  67. __le16 control;
  68. __le32 data_address;
  69. __le32 branch_address;
  70. __le16 res_count;
  71. __le16 transfer_status;
  72. } __attribute__((aligned(16)));
  73. #define CONTROL_SET(regs) (regs)
  74. #define CONTROL_CLEAR(regs) ((regs) + 4)
  75. #define COMMAND_PTR(regs) ((regs) + 12)
  76. #define CONTEXT_MATCH(regs) ((regs) + 16)
  77. #define AR_BUFFER_SIZE (32*1024)
  78. #define AR_BUFFERS_MIN DIV_ROUND_UP(AR_BUFFER_SIZE, PAGE_SIZE)
  79. /* we need at least two pages for proper list management */
  80. #define AR_BUFFERS (AR_BUFFERS_MIN >= 2 ? AR_BUFFERS_MIN : 2)
  81. #define MAX_ASYNC_PAYLOAD 4096
  82. #define MAX_AR_PACKET_SIZE (16 + MAX_ASYNC_PAYLOAD + 4)
  83. #define AR_WRAPAROUND_PAGES DIV_ROUND_UP(MAX_AR_PACKET_SIZE, PAGE_SIZE)
  84. struct ar_context {
  85. struct fw_ohci *ohci;
  86. struct page *pages[AR_BUFFERS];
  87. void *buffer;
  88. struct descriptor *descriptors;
  89. dma_addr_t descriptors_bus;
  90. void *pointer;
  91. unsigned int last_buffer_index;
  92. u32 regs;
  93. struct tasklet_struct tasklet;
  94. };
  95. struct context;
  96. typedef int (*descriptor_callback_t)(struct context *ctx,
  97. struct descriptor *d,
  98. struct descriptor *last);
  99. /*
  100. * A buffer that contains a block of DMA-able coherent memory used for
  101. * storing a portion of a DMA descriptor program.
  102. */
  103. struct descriptor_buffer {
  104. struct list_head list;
  105. dma_addr_t buffer_bus;
  106. size_t buffer_size;
  107. size_t used;
  108. struct descriptor buffer[0];
  109. };
  110. struct context {
  111. struct fw_ohci *ohci;
  112. u32 regs;
  113. int total_allocation;
  114. bool running;
  115. bool flushing;
  116. /*
  117. * List of page-sized buffers for storing DMA descriptors.
  118. * Head of list contains buffers in use and tail of list contains
  119. * free buffers.
  120. */
  121. struct list_head buffer_list;
  122. /*
  123. * Pointer to a buffer inside buffer_list that contains the tail
  124. * end of the current DMA program.
  125. */
  126. struct descriptor_buffer *buffer_tail;
  127. /*
  128. * The descriptor containing the branch address of the first
  129. * descriptor that has not yet been filled by the device.
  130. */
  131. struct descriptor *last;
  132. /*
  133. * The last descriptor in the DMA program. It contains the branch
  134. * address that must be updated upon appending a new descriptor.
  135. */
  136. struct descriptor *prev;
  137. descriptor_callback_t callback;
  138. struct tasklet_struct tasklet;
  139. };
  140. #define IT_HEADER_SY(v) ((v) << 0)
  141. #define IT_HEADER_TCODE(v) ((v) << 4)
  142. #define IT_HEADER_CHANNEL(v) ((v) << 8)
  143. #define IT_HEADER_TAG(v) ((v) << 14)
  144. #define IT_HEADER_SPEED(v) ((v) << 16)
  145. #define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
  146. struct iso_context {
  147. struct fw_iso_context base;
  148. struct context context;
  149. int excess_bytes;
  150. void *header;
  151. size_t header_length;
  152. u8 sync;
  153. u8 tags;
  154. };
  155. #define CONFIG_ROM_SIZE 1024
  156. struct fw_ohci {
  157. struct fw_card card;
  158. __iomem char *registers;
  159. int node_id;
  160. int generation;
  161. int request_generation; /* for timestamping incoming requests */
  162. unsigned quirks;
  163. unsigned int pri_req_max;
  164. u32 bus_time;
  165. bool is_root;
  166. bool csr_state_setclear_abdicate;
  167. int n_ir;
  168. int n_it;
  169. /*
  170. * Spinlock for accessing fw_ohci data. Never call out of
  171. * this driver with this lock held.
  172. */
  173. spinlock_t lock;
  174. struct mutex phy_reg_mutex;
  175. void *misc_buffer;
  176. dma_addr_t misc_buffer_bus;
  177. struct ar_context ar_request_ctx;
  178. struct ar_context ar_response_ctx;
  179. struct context at_request_ctx;
  180. struct context at_response_ctx;
  181. u32 it_context_support;
  182. u32 it_context_mask; /* unoccupied IT contexts */
  183. struct iso_context *it_context_list;
  184. u64 ir_context_channels; /* unoccupied channels */
  185. u32 ir_context_support;
  186. u32 ir_context_mask; /* unoccupied IR contexts */
  187. struct iso_context *ir_context_list;
  188. u64 mc_channels; /* channels in use by the multichannel IR context */
  189. bool mc_allocated;
  190. __be32 *config_rom;
  191. dma_addr_t config_rom_bus;
  192. __be32 *next_config_rom;
  193. dma_addr_t next_config_rom_bus;
  194. __be32 next_header;
  195. __le32 *self_id_cpu;
  196. dma_addr_t self_id_bus;
  197. struct tasklet_struct bus_reset_tasklet;
  198. u32 self_id_buffer[512];
  199. };
  200. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  201. {
  202. return container_of(card, struct fw_ohci, card);
  203. }
  204. #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  205. #define IR_CONTEXT_BUFFER_FILL 0x80000000
  206. #define IR_CONTEXT_ISOCH_HEADER 0x40000000
  207. #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
  208. #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
  209. #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
  210. #define CONTEXT_RUN 0x8000
  211. #define CONTEXT_WAKE 0x1000
  212. #define CONTEXT_DEAD 0x0800
  213. #define CONTEXT_ACTIVE 0x0400
  214. #define OHCI1394_MAX_AT_REQ_RETRIES 0xf
  215. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  216. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  217. #define OHCI1394_REGISTER_SIZE 0x800
  218. #define OHCI_LOOP_COUNT 500
  219. #define OHCI1394_PCI_HCI_Control 0x40
  220. #define SELF_ID_BUF_SIZE 0x800
  221. #define OHCI_TCODE_PHY_PACKET 0x0e
  222. #define OHCI_VERSION_1_1 0x010010
  223. static char ohci_driver_name[] = KBUILD_MODNAME;
  224. #define PCI_DEVICE_ID_AGERE_FW643 0x5901
  225. #define PCI_DEVICE_ID_JMICRON_JMB38X_FW 0x2380
  226. #define PCI_DEVICE_ID_TI_TSB12LV22 0x8009
  227. #define PCI_VENDOR_ID_PINNACLE_SYSTEMS 0x11bd
  228. #define QUIRK_CYCLE_TIMER 1
  229. #define QUIRK_RESET_PACKET 2
  230. #define QUIRK_BE_HEADERS 4
  231. #define QUIRK_NO_1394A 8
  232. #define QUIRK_NO_MSI 16
  233. /* In case of multiple matches in ohci_quirks[], only the first one is used. */
  234. static const struct {
  235. unsigned short vendor, device, revision, flags;
  236. } ohci_quirks[] = {
  237. {PCI_VENDOR_ID_AL, PCI_ANY_ID, PCI_ANY_ID,
  238. QUIRK_CYCLE_TIMER},
  239. {PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_FW, PCI_ANY_ID,
  240. QUIRK_BE_HEADERS},
  241. {PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_AGERE_FW643, 6,
  242. QUIRK_NO_MSI},
  243. {PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_FW, PCI_ANY_ID,
  244. QUIRK_NO_MSI},
  245. {PCI_VENDOR_ID_NEC, PCI_ANY_ID, PCI_ANY_ID,
  246. QUIRK_CYCLE_TIMER},
  247. {PCI_VENDOR_ID_RICOH, PCI_ANY_ID, PCI_ANY_ID,
  248. QUIRK_CYCLE_TIMER},
  249. {PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV22, PCI_ANY_ID,
  250. QUIRK_CYCLE_TIMER | QUIRK_RESET_PACKET | QUIRK_NO_1394A},
  251. {PCI_VENDOR_ID_TI, PCI_ANY_ID, PCI_ANY_ID,
  252. QUIRK_RESET_PACKET},
  253. {PCI_VENDOR_ID_VIA, PCI_ANY_ID, PCI_ANY_ID,
  254. QUIRK_CYCLE_TIMER | QUIRK_NO_MSI},
  255. };
  256. /* This overrides anything that was found in ohci_quirks[]. */
  257. static int param_quirks;
  258. module_param_named(quirks, param_quirks, int, 0644);
  259. MODULE_PARM_DESC(quirks, "Chip quirks (default = 0"
  260. ", nonatomic cycle timer = " __stringify(QUIRK_CYCLE_TIMER)
  261. ", reset packet generation = " __stringify(QUIRK_RESET_PACKET)
  262. ", AR/selfID endianess = " __stringify(QUIRK_BE_HEADERS)
  263. ", no 1394a enhancements = " __stringify(QUIRK_NO_1394A)
  264. ", disable MSI = " __stringify(QUIRK_NO_MSI)
  265. ")");
  266. #define OHCI_PARAM_DEBUG_AT_AR 1
  267. #define OHCI_PARAM_DEBUG_SELFIDS 2
  268. #define OHCI_PARAM_DEBUG_IRQS 4
  269. #define OHCI_PARAM_DEBUG_BUSRESETS 8 /* only effective before chip init */
  270. #ifdef CONFIG_FIREWIRE_OHCI_DEBUG
  271. static int param_debug;
  272. module_param_named(debug, param_debug, int, 0644);
  273. MODULE_PARM_DESC(debug, "Verbose logging (default = 0"
  274. ", AT/AR events = " __stringify(OHCI_PARAM_DEBUG_AT_AR)
  275. ", self-IDs = " __stringify(OHCI_PARAM_DEBUG_SELFIDS)
  276. ", IRQs = " __stringify(OHCI_PARAM_DEBUG_IRQS)
  277. ", busReset events = " __stringify(OHCI_PARAM_DEBUG_BUSRESETS)
  278. ", or a combination, or all = -1)");
  279. static void log_irqs(u32 evt)
  280. {
  281. if (likely(!(param_debug &
  282. (OHCI_PARAM_DEBUG_IRQS | OHCI_PARAM_DEBUG_BUSRESETS))))
  283. return;
  284. if (!(param_debug & OHCI_PARAM_DEBUG_IRQS) &&
  285. !(evt & OHCI1394_busReset))
  286. return;
  287. fw_notify("IRQ %08x%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", evt,
  288. evt & OHCI1394_selfIDComplete ? " selfID" : "",
  289. evt & OHCI1394_RQPkt ? " AR_req" : "",
  290. evt & OHCI1394_RSPkt ? " AR_resp" : "",
  291. evt & OHCI1394_reqTxComplete ? " AT_req" : "",
  292. evt & OHCI1394_respTxComplete ? " AT_resp" : "",
  293. evt & OHCI1394_isochRx ? " IR" : "",
  294. evt & OHCI1394_isochTx ? " IT" : "",
  295. evt & OHCI1394_postedWriteErr ? " postedWriteErr" : "",
  296. evt & OHCI1394_cycleTooLong ? " cycleTooLong" : "",
  297. evt & OHCI1394_cycle64Seconds ? " cycle64Seconds" : "",
  298. evt & OHCI1394_cycleInconsistent ? " cycleInconsistent" : "",
  299. evt & OHCI1394_regAccessFail ? " regAccessFail" : "",
  300. evt & OHCI1394_unrecoverableError ? " unrecoverableError" : "",
  301. evt & OHCI1394_busReset ? " busReset" : "",
  302. evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt |
  303. OHCI1394_RSPkt | OHCI1394_reqTxComplete |
  304. OHCI1394_respTxComplete | OHCI1394_isochRx |
  305. OHCI1394_isochTx | OHCI1394_postedWriteErr |
  306. OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds |
  307. OHCI1394_cycleInconsistent |
  308. OHCI1394_regAccessFail | OHCI1394_busReset)
  309. ? " ?" : "");
  310. }
  311. static const char *speed[] = {
  312. [0] = "S100", [1] = "S200", [2] = "S400", [3] = "beta",
  313. };
  314. static const char *power[] = {
  315. [0] = "+0W", [1] = "+15W", [2] = "+30W", [3] = "+45W",
  316. [4] = "-3W", [5] = " ?W", [6] = "-3..-6W", [7] = "-3..-10W",
  317. };
  318. static const char port[] = { '.', '-', 'p', 'c', };
  319. static char _p(u32 *s, int shift)
  320. {
  321. return port[*s >> shift & 3];
  322. }
  323. static void log_selfids(int node_id, int generation, int self_id_count, u32 *s)
  324. {
  325. if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS)))
  326. return;
  327. fw_notify("%d selfIDs, generation %d, local node ID %04x\n",
  328. self_id_count, generation, node_id);
  329. for (; self_id_count--; ++s)
  330. if ((*s & 1 << 23) == 0)
  331. fw_notify("selfID 0: %08x, phy %d [%c%c%c] "
  332. "%s gc=%d %s %s%s%s\n",
  333. *s, *s >> 24 & 63, _p(s, 6), _p(s, 4), _p(s, 2),
  334. speed[*s >> 14 & 3], *s >> 16 & 63,
  335. power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "",
  336. *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : "");
  337. else
  338. fw_notify("selfID n: %08x, phy %d [%c%c%c%c%c%c%c%c]\n",
  339. *s, *s >> 24 & 63,
  340. _p(s, 16), _p(s, 14), _p(s, 12), _p(s, 10),
  341. _p(s, 8), _p(s, 6), _p(s, 4), _p(s, 2));
  342. }
  343. static const char *evts[] = {
  344. [0x00] = "evt_no_status", [0x01] = "-reserved-",
  345. [0x02] = "evt_long_packet", [0x03] = "evt_missing_ack",
  346. [0x04] = "evt_underrun", [0x05] = "evt_overrun",
  347. [0x06] = "evt_descriptor_read", [0x07] = "evt_data_read",
  348. [0x08] = "evt_data_write", [0x09] = "evt_bus_reset",
  349. [0x0a] = "evt_timeout", [0x0b] = "evt_tcode_err",
  350. [0x0c] = "-reserved-", [0x0d] = "-reserved-",
  351. [0x0e] = "evt_unknown", [0x0f] = "evt_flushed",
  352. [0x10] = "-reserved-", [0x11] = "ack_complete",
  353. [0x12] = "ack_pending ", [0x13] = "-reserved-",
  354. [0x14] = "ack_busy_X", [0x15] = "ack_busy_A",
  355. [0x16] = "ack_busy_B", [0x17] = "-reserved-",
  356. [0x18] = "-reserved-", [0x19] = "-reserved-",
  357. [0x1a] = "-reserved-", [0x1b] = "ack_tardy",
  358. [0x1c] = "-reserved-", [0x1d] = "ack_data_error",
  359. [0x1e] = "ack_type_error", [0x1f] = "-reserved-",
  360. [0x20] = "pending/cancelled",
  361. };
  362. static const char *tcodes[] = {
  363. [0x0] = "QW req", [0x1] = "BW req",
  364. [0x2] = "W resp", [0x3] = "-reserved-",
  365. [0x4] = "QR req", [0x5] = "BR req",
  366. [0x6] = "QR resp", [0x7] = "BR resp",
  367. [0x8] = "cycle start", [0x9] = "Lk req",
  368. [0xa] = "async stream packet", [0xb] = "Lk resp",
  369. [0xc] = "-reserved-", [0xd] = "-reserved-",
  370. [0xe] = "link internal", [0xf] = "-reserved-",
  371. };
  372. static void log_ar_at_event(char dir, int speed, u32 *header, int evt)
  373. {
  374. int tcode = header[0] >> 4 & 0xf;
  375. char specific[12];
  376. if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR)))
  377. return;
  378. if (unlikely(evt >= ARRAY_SIZE(evts)))
  379. evt = 0x1f;
  380. if (evt == OHCI1394_evt_bus_reset) {
  381. fw_notify("A%c evt_bus_reset, generation %d\n",
  382. dir, (header[2] >> 16) & 0xff);
  383. return;
  384. }
  385. switch (tcode) {
  386. case 0x0: case 0x6: case 0x8:
  387. snprintf(specific, sizeof(specific), " = %08x",
  388. be32_to_cpu((__force __be32)header[3]));
  389. break;
  390. case 0x1: case 0x5: case 0x7: case 0x9: case 0xb:
  391. snprintf(specific, sizeof(specific), " %x,%x",
  392. header[3] >> 16, header[3] & 0xffff);
  393. break;
  394. default:
  395. specific[0] = '\0';
  396. }
  397. switch (tcode) {
  398. case 0xa:
  399. fw_notify("A%c %s, %s\n", dir, evts[evt], tcodes[tcode]);
  400. break;
  401. case 0xe:
  402. fw_notify("A%c %s, PHY %08x %08x\n",
  403. dir, evts[evt], header[1], header[2]);
  404. break;
  405. case 0x0: case 0x1: case 0x4: case 0x5: case 0x9:
  406. fw_notify("A%c spd %x tl %02x, "
  407. "%04x -> %04x, %s, "
  408. "%s, %04x%08x%s\n",
  409. dir, speed, header[0] >> 10 & 0x3f,
  410. header[1] >> 16, header[0] >> 16, evts[evt],
  411. tcodes[tcode], header[1] & 0xffff, header[2], specific);
  412. break;
  413. default:
  414. fw_notify("A%c spd %x tl %02x, "
  415. "%04x -> %04x, %s, "
  416. "%s%s\n",
  417. dir, speed, header[0] >> 10 & 0x3f,
  418. header[1] >> 16, header[0] >> 16, evts[evt],
  419. tcodes[tcode], specific);
  420. }
  421. }
  422. #else
  423. #define param_debug 0
  424. static inline void log_irqs(u32 evt) {}
  425. static inline void log_selfids(int node_id, int generation, int self_id_count, u32 *s) {}
  426. static inline void log_ar_at_event(char dir, int speed, u32 *header, int evt) {}
  427. #endif /* CONFIG_FIREWIRE_OHCI_DEBUG */
  428. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  429. {
  430. writel(data, ohci->registers + offset);
  431. }
  432. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  433. {
  434. return readl(ohci->registers + offset);
  435. }
  436. static inline void flush_writes(const struct fw_ohci *ohci)
  437. {
  438. /* Do a dummy read to flush writes. */
  439. reg_read(ohci, OHCI1394_Version);
  440. }
  441. static int read_phy_reg(struct fw_ohci *ohci, int addr)
  442. {
  443. u32 val;
  444. int i;
  445. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  446. for (i = 0; i < 3 + 100; i++) {
  447. val = reg_read(ohci, OHCI1394_PhyControl);
  448. if (val & OHCI1394_PhyControl_ReadDone)
  449. return OHCI1394_PhyControl_ReadData(val);
  450. /*
  451. * Try a few times without waiting. Sleeping is necessary
  452. * only when the link/PHY interface is busy.
  453. */
  454. if (i >= 3)
  455. msleep(1);
  456. }
  457. fw_error("failed to read phy reg\n");
  458. return -EBUSY;
  459. }
  460. static int write_phy_reg(const struct fw_ohci *ohci, int addr, u32 val)
  461. {
  462. int i;
  463. reg_write(ohci, OHCI1394_PhyControl,
  464. OHCI1394_PhyControl_Write(addr, val));
  465. for (i = 0; i < 3 + 100; i++) {
  466. val = reg_read(ohci, OHCI1394_PhyControl);
  467. if (!(val & OHCI1394_PhyControl_WritePending))
  468. return 0;
  469. if (i >= 3)
  470. msleep(1);
  471. }
  472. fw_error("failed to write phy reg\n");
  473. return -EBUSY;
  474. }
  475. static int update_phy_reg(struct fw_ohci *ohci, int addr,
  476. int clear_bits, int set_bits)
  477. {
  478. int ret = read_phy_reg(ohci, addr);
  479. if (ret < 0)
  480. return ret;
  481. /*
  482. * The interrupt status bits are cleared by writing a one bit.
  483. * Avoid clearing them unless explicitly requested in set_bits.
  484. */
  485. if (addr == 5)
  486. clear_bits |= PHY_INT_STATUS_BITS;
  487. return write_phy_reg(ohci, addr, (ret & ~clear_bits) | set_bits);
  488. }
  489. static int read_paged_phy_reg(struct fw_ohci *ohci, int page, int addr)
  490. {
  491. int ret;
  492. ret = update_phy_reg(ohci, 7, PHY_PAGE_SELECT, page << 5);
  493. if (ret < 0)
  494. return ret;
  495. return read_phy_reg(ohci, addr);
  496. }
  497. static int ohci_read_phy_reg(struct fw_card *card, int addr)
  498. {
  499. struct fw_ohci *ohci = fw_ohci(card);
  500. int ret;
  501. mutex_lock(&ohci->phy_reg_mutex);
  502. ret = read_phy_reg(ohci, addr);
  503. mutex_unlock(&ohci->phy_reg_mutex);
  504. return ret;
  505. }
  506. static int ohci_update_phy_reg(struct fw_card *card, int addr,
  507. int clear_bits, int set_bits)
  508. {
  509. struct fw_ohci *ohci = fw_ohci(card);
  510. int ret;
  511. mutex_lock(&ohci->phy_reg_mutex);
  512. ret = update_phy_reg(ohci, addr, clear_bits, set_bits);
  513. mutex_unlock(&ohci->phy_reg_mutex);
  514. return ret;
  515. }
  516. static inline dma_addr_t ar_buffer_bus(struct ar_context *ctx, unsigned int i)
  517. {
  518. return page_private(ctx->pages[i]);
  519. }
  520. static void ar_context_link_page(struct ar_context *ctx, unsigned int index)
  521. {
  522. struct descriptor *d;
  523. d = &ctx->descriptors[index];
  524. d->branch_address &= cpu_to_le32(~0xf);
  525. d->res_count = cpu_to_le16(PAGE_SIZE);
  526. d->transfer_status = 0;
  527. wmb(); /* finish init of new descriptors before branch_address update */
  528. d = &ctx->descriptors[ctx->last_buffer_index];
  529. d->branch_address |= cpu_to_le32(1);
  530. ctx->last_buffer_index = index;
  531. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  532. flush_writes(ctx->ohci);
  533. }
  534. static void ar_context_release(struct ar_context *ctx)
  535. {
  536. unsigned int i;
  537. if (ctx->buffer)
  538. vm_unmap_ram(ctx->buffer, AR_BUFFERS + AR_WRAPAROUND_PAGES);
  539. for (i = 0; i < AR_BUFFERS; i++)
  540. if (ctx->pages[i]) {
  541. dma_unmap_page(ctx->ohci->card.device,
  542. ar_buffer_bus(ctx, i),
  543. PAGE_SIZE, DMA_FROM_DEVICE);
  544. __free_page(ctx->pages[i]);
  545. }
  546. }
  547. static void ar_context_abort(struct ar_context *ctx, const char *error_msg)
  548. {
  549. if (reg_read(ctx->ohci, CONTROL_CLEAR(ctx->regs)) & CONTEXT_RUN) {
  550. reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  551. flush_writes(ctx->ohci);
  552. fw_error("AR error: %s; DMA stopped\n", error_msg);
  553. }
  554. /* FIXME: restart? */
  555. }
  556. static inline unsigned int ar_next_buffer_index(unsigned int index)
  557. {
  558. return (index + 1) % AR_BUFFERS;
  559. }
  560. static inline unsigned int ar_prev_buffer_index(unsigned int index)
  561. {
  562. return (index - 1 + AR_BUFFERS) % AR_BUFFERS;
  563. }
  564. static inline unsigned int ar_first_buffer_index(struct ar_context *ctx)
  565. {
  566. return ar_next_buffer_index(ctx->last_buffer_index);
  567. }
  568. /*
  569. * We search for the buffer that contains the last AR packet DMA data written
  570. * by the controller.
  571. */
  572. static unsigned int ar_search_last_active_buffer(struct ar_context *ctx,
  573. unsigned int *buffer_offset)
  574. {
  575. unsigned int i, next_i, last = ctx->last_buffer_index;
  576. __le16 res_count, next_res_count;
  577. i = ar_first_buffer_index(ctx);
  578. res_count = ACCESS_ONCE(ctx->descriptors[i].res_count);
  579. /* A buffer that is not yet completely filled must be the last one. */
  580. while (i != last && res_count == 0) {
  581. /* Peek at the next descriptor. */
  582. next_i = ar_next_buffer_index(i);
  583. rmb(); /* read descriptors in order */
  584. next_res_count = ACCESS_ONCE(
  585. ctx->descriptors[next_i].res_count);
  586. /*
  587. * If the next descriptor is still empty, we must stop at this
  588. * descriptor.
  589. */
  590. if (next_res_count == cpu_to_le16(PAGE_SIZE)) {
  591. /*
  592. * The exception is when the DMA data for one packet is
  593. * split over three buffers; in this case, the middle
  594. * buffer's descriptor might be never updated by the
  595. * controller and look still empty, and we have to peek
  596. * at the third one.
  597. */
  598. if (MAX_AR_PACKET_SIZE > PAGE_SIZE && i != last) {
  599. next_i = ar_next_buffer_index(next_i);
  600. rmb();
  601. next_res_count = ACCESS_ONCE(
  602. ctx->descriptors[next_i].res_count);
  603. if (next_res_count != cpu_to_le16(PAGE_SIZE))
  604. goto next_buffer_is_active;
  605. }
  606. break;
  607. }
  608. next_buffer_is_active:
  609. i = next_i;
  610. res_count = next_res_count;
  611. }
  612. rmb(); /* read res_count before the DMA data */
  613. *buffer_offset = PAGE_SIZE - le16_to_cpu(res_count);
  614. if (*buffer_offset > PAGE_SIZE) {
  615. *buffer_offset = 0;
  616. ar_context_abort(ctx, "corrupted descriptor");
  617. }
  618. return i;
  619. }
  620. static void ar_sync_buffers_for_cpu(struct ar_context *ctx,
  621. unsigned int end_buffer_index,
  622. unsigned int end_buffer_offset)
  623. {
  624. unsigned int i;
  625. i = ar_first_buffer_index(ctx);
  626. while (i != end_buffer_index) {
  627. dma_sync_single_for_cpu(ctx->ohci->card.device,
  628. ar_buffer_bus(ctx, i),
  629. PAGE_SIZE, DMA_FROM_DEVICE);
  630. i = ar_next_buffer_index(i);
  631. }
  632. if (end_buffer_offset > 0)
  633. dma_sync_single_for_cpu(ctx->ohci->card.device,
  634. ar_buffer_bus(ctx, i),
  635. end_buffer_offset, DMA_FROM_DEVICE);
  636. }
  637. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  638. #define cond_le32_to_cpu(v) \
  639. (ohci->quirks & QUIRK_BE_HEADERS ? (__force __u32)(v) : le32_to_cpu(v))
  640. #else
  641. #define cond_le32_to_cpu(v) le32_to_cpu(v)
  642. #endif
  643. static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
  644. {
  645. struct fw_ohci *ohci = ctx->ohci;
  646. struct fw_packet p;
  647. u32 status, length, tcode;
  648. int evt;
  649. p.header[0] = cond_le32_to_cpu(buffer[0]);
  650. p.header[1] = cond_le32_to_cpu(buffer[1]);
  651. p.header[2] = cond_le32_to_cpu(buffer[2]);
  652. tcode = (p.header[0] >> 4) & 0x0f;
  653. switch (tcode) {
  654. case TCODE_WRITE_QUADLET_REQUEST:
  655. case TCODE_READ_QUADLET_RESPONSE:
  656. p.header[3] = (__force __u32) buffer[3];
  657. p.header_length = 16;
  658. p.payload_length = 0;
  659. break;
  660. case TCODE_READ_BLOCK_REQUEST :
  661. p.header[3] = cond_le32_to_cpu(buffer[3]);
  662. p.header_length = 16;
  663. p.payload_length = 0;
  664. break;
  665. case TCODE_WRITE_BLOCK_REQUEST:
  666. case TCODE_READ_BLOCK_RESPONSE:
  667. case TCODE_LOCK_REQUEST:
  668. case TCODE_LOCK_RESPONSE:
  669. p.header[3] = cond_le32_to_cpu(buffer[3]);
  670. p.header_length = 16;
  671. p.payload_length = p.header[3] >> 16;
  672. if (p.payload_length > MAX_ASYNC_PAYLOAD) {
  673. ar_context_abort(ctx, "invalid packet length");
  674. return NULL;
  675. }
  676. break;
  677. case TCODE_WRITE_RESPONSE:
  678. case TCODE_READ_QUADLET_REQUEST:
  679. case OHCI_TCODE_PHY_PACKET:
  680. p.header_length = 12;
  681. p.payload_length = 0;
  682. break;
  683. default:
  684. ar_context_abort(ctx, "invalid tcode");
  685. return NULL;
  686. }
  687. p.payload = (void *) buffer + p.header_length;
  688. /* FIXME: What to do about evt_* errors? */
  689. length = (p.header_length + p.payload_length + 3) / 4;
  690. status = cond_le32_to_cpu(buffer[length]);
  691. evt = (status >> 16) & 0x1f;
  692. p.ack = evt - 16;
  693. p.speed = (status >> 21) & 0x7;
  694. p.timestamp = status & 0xffff;
  695. p.generation = ohci->request_generation;
  696. log_ar_at_event('R', p.speed, p.header, evt);
  697. /*
  698. * Several controllers, notably from NEC and VIA, forget to
  699. * write ack_complete status at PHY packet reception.
  700. */
  701. if (evt == OHCI1394_evt_no_status &&
  702. (p.header[0] & 0xff) == (OHCI1394_phy_tcode << 4))
  703. p.ack = ACK_COMPLETE;
  704. /*
  705. * The OHCI bus reset handler synthesizes a PHY packet with
  706. * the new generation number when a bus reset happens (see
  707. * section 8.4.2.3). This helps us determine when a request
  708. * was received and make sure we send the response in the same
  709. * generation. We only need this for requests; for responses
  710. * we use the unique tlabel for finding the matching
  711. * request.
  712. *
  713. * Alas some chips sometimes emit bus reset packets with a
  714. * wrong generation. We set the correct generation for these
  715. * at a slightly incorrect time (in bus_reset_tasklet).
  716. */
  717. if (evt == OHCI1394_evt_bus_reset) {
  718. if (!(ohci->quirks & QUIRK_RESET_PACKET))
  719. ohci->request_generation = (p.header[2] >> 16) & 0xff;
  720. } else if (ctx == &ohci->ar_request_ctx) {
  721. fw_core_handle_request(&ohci->card, &p);
  722. } else {
  723. fw_core_handle_response(&ohci->card, &p);
  724. }
  725. return buffer + length + 1;
  726. }
  727. static void *handle_ar_packets(struct ar_context *ctx, void *p, void *end)
  728. {
  729. void *next;
  730. while (p < end) {
  731. next = handle_ar_packet(ctx, p);
  732. if (!next)
  733. return p;
  734. p = next;
  735. }
  736. return p;
  737. }
  738. static void ar_recycle_buffers(struct ar_context *ctx, unsigned int end_buffer)
  739. {
  740. unsigned int i;
  741. i = ar_first_buffer_index(ctx);
  742. while (i != end_buffer) {
  743. dma_sync_single_for_device(ctx->ohci->card.device,
  744. ar_buffer_bus(ctx, i),
  745. PAGE_SIZE, DMA_FROM_DEVICE);
  746. ar_context_link_page(ctx, i);
  747. i = ar_next_buffer_index(i);
  748. }
  749. }
  750. static void ar_context_tasklet(unsigned long data)
  751. {
  752. struct ar_context *ctx = (struct ar_context *)data;
  753. unsigned int end_buffer_index, end_buffer_offset;
  754. void *p, *end;
  755. p = ctx->pointer;
  756. if (!p)
  757. return;
  758. end_buffer_index = ar_search_last_active_buffer(ctx,
  759. &end_buffer_offset);
  760. ar_sync_buffers_for_cpu(ctx, end_buffer_index, end_buffer_offset);
  761. end = ctx->buffer + end_buffer_index * PAGE_SIZE + end_buffer_offset;
  762. if (end_buffer_index < ar_first_buffer_index(ctx)) {
  763. /*
  764. * The filled part of the overall buffer wraps around; handle
  765. * all packets up to the buffer end here. If the last packet
  766. * wraps around, its tail will be visible after the buffer end
  767. * because the buffer start pages are mapped there again.
  768. */
  769. void *buffer_end = ctx->buffer + AR_BUFFERS * PAGE_SIZE;
  770. p = handle_ar_packets(ctx, p, buffer_end);
  771. if (p < buffer_end)
  772. goto error;
  773. /* adjust p to point back into the actual buffer */
  774. p -= AR_BUFFERS * PAGE_SIZE;
  775. }
  776. p = handle_ar_packets(ctx, p, end);
  777. if (p != end) {
  778. if (p > end)
  779. ar_context_abort(ctx, "inconsistent descriptor");
  780. goto error;
  781. }
  782. ctx->pointer = p;
  783. ar_recycle_buffers(ctx, end_buffer_index);
  784. return;
  785. error:
  786. ctx->pointer = NULL;
  787. }
  788. static int ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci,
  789. unsigned int descriptors_offset, u32 regs)
  790. {
  791. unsigned int i;
  792. dma_addr_t dma_addr;
  793. struct page *pages[AR_BUFFERS + AR_WRAPAROUND_PAGES];
  794. struct descriptor *d;
  795. ctx->regs = regs;
  796. ctx->ohci = ohci;
  797. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  798. for (i = 0; i < AR_BUFFERS; i++) {
  799. ctx->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32);
  800. if (!ctx->pages[i])
  801. goto out_of_memory;
  802. dma_addr = dma_map_page(ohci->card.device, ctx->pages[i],
  803. 0, PAGE_SIZE, DMA_FROM_DEVICE);
  804. if (dma_mapping_error(ohci->card.device, dma_addr)) {
  805. __free_page(ctx->pages[i]);
  806. ctx->pages[i] = NULL;
  807. goto out_of_memory;
  808. }
  809. set_page_private(ctx->pages[i], dma_addr);
  810. }
  811. for (i = 0; i < AR_BUFFERS; i++)
  812. pages[i] = ctx->pages[i];
  813. for (i = 0; i < AR_WRAPAROUND_PAGES; i++)
  814. pages[AR_BUFFERS + i] = ctx->pages[i];
  815. ctx->buffer = vm_map_ram(pages, AR_BUFFERS + AR_WRAPAROUND_PAGES,
  816. -1, PAGE_KERNEL);
  817. if (!ctx->buffer)
  818. goto out_of_memory;
  819. ctx->descriptors = ohci->misc_buffer + descriptors_offset;
  820. ctx->descriptors_bus = ohci->misc_buffer_bus + descriptors_offset;
  821. for (i = 0; i < AR_BUFFERS; i++) {
  822. d = &ctx->descriptors[i];
  823. d->req_count = cpu_to_le16(PAGE_SIZE);
  824. d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  825. DESCRIPTOR_STATUS |
  826. DESCRIPTOR_BRANCH_ALWAYS);
  827. d->data_address = cpu_to_le32(ar_buffer_bus(ctx, i));
  828. d->branch_address = cpu_to_le32(ctx->descriptors_bus +
  829. ar_next_buffer_index(i) * sizeof(struct descriptor));
  830. }
  831. return 0;
  832. out_of_memory:
  833. ar_context_release(ctx);
  834. return -ENOMEM;
  835. }
  836. static void ar_context_run(struct ar_context *ctx)
  837. {
  838. unsigned int i;
  839. for (i = 0; i < AR_BUFFERS; i++)
  840. ar_context_link_page(ctx, i);
  841. ctx->pointer = ctx->buffer;
  842. reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ctx->descriptors_bus | 1);
  843. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
  844. flush_writes(ctx->ohci);
  845. }
  846. static struct descriptor *find_branch_descriptor(struct descriptor *d, int z)
  847. {
  848. __le16 branch;
  849. branch = d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS);
  850. /* figure out which descriptor the branch address goes in */
  851. if (z == 2 && branch == cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
  852. return d;
  853. else
  854. return d + z - 1;
  855. }
  856. static void context_tasklet(unsigned long data)
  857. {
  858. struct context *ctx = (struct context *) data;
  859. struct descriptor *d, *last;
  860. u32 address;
  861. int z;
  862. struct descriptor_buffer *desc;
  863. desc = list_entry(ctx->buffer_list.next,
  864. struct descriptor_buffer, list);
  865. last = ctx->last;
  866. while (last->branch_address != 0) {
  867. struct descriptor_buffer *old_desc = desc;
  868. address = le32_to_cpu(last->branch_address);
  869. z = address & 0xf;
  870. address &= ~0xf;
  871. /* If the branch address points to a buffer outside of the
  872. * current buffer, advance to the next buffer. */
  873. if (address < desc->buffer_bus ||
  874. address >= desc->buffer_bus + desc->used)
  875. desc = list_entry(desc->list.next,
  876. struct descriptor_buffer, list);
  877. d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
  878. last = find_branch_descriptor(d, z);
  879. if (!ctx->callback(ctx, d, last))
  880. break;
  881. if (old_desc != desc) {
  882. /* If we've advanced to the next buffer, move the
  883. * previous buffer to the free list. */
  884. unsigned long flags;
  885. old_desc->used = 0;
  886. spin_lock_irqsave(&ctx->ohci->lock, flags);
  887. list_move_tail(&old_desc->list, &ctx->buffer_list);
  888. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  889. }
  890. ctx->last = last;
  891. }
  892. }
  893. /*
  894. * Allocate a new buffer and add it to the list of free buffers for this
  895. * context. Must be called with ohci->lock held.
  896. */
  897. static int context_add_buffer(struct context *ctx)
  898. {
  899. struct descriptor_buffer *desc;
  900. dma_addr_t uninitialized_var(bus_addr);
  901. int offset;
  902. /*
  903. * 16MB of descriptors should be far more than enough for any DMA
  904. * program. This will catch run-away userspace or DoS attacks.
  905. */
  906. if (ctx->total_allocation >= 16*1024*1024)
  907. return -ENOMEM;
  908. desc = dma_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE,
  909. &bus_addr, GFP_ATOMIC);
  910. if (!desc)
  911. return -ENOMEM;
  912. offset = (void *)&desc->buffer - (void *)desc;
  913. desc->buffer_size = PAGE_SIZE - offset;
  914. desc->buffer_bus = bus_addr + offset;
  915. desc->used = 0;
  916. list_add_tail(&desc->list, &ctx->buffer_list);
  917. ctx->total_allocation += PAGE_SIZE;
  918. return 0;
  919. }
  920. static int context_init(struct context *ctx, struct fw_ohci *ohci,
  921. u32 regs, descriptor_callback_t callback)
  922. {
  923. ctx->ohci = ohci;
  924. ctx->regs = regs;
  925. ctx->total_allocation = 0;
  926. INIT_LIST_HEAD(&ctx->buffer_list);
  927. if (context_add_buffer(ctx) < 0)
  928. return -ENOMEM;
  929. ctx->buffer_tail = list_entry(ctx->buffer_list.next,
  930. struct descriptor_buffer, list);
  931. tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
  932. ctx->callback = callback;
  933. /*
  934. * We put a dummy descriptor in the buffer that has a NULL
  935. * branch address and looks like it's been sent. That way we
  936. * have a descriptor to append DMA programs to.
  937. */
  938. memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
  939. ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
  940. ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
  941. ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
  942. ctx->last = ctx->buffer_tail->buffer;
  943. ctx->prev = ctx->buffer_tail->buffer;
  944. return 0;
  945. }
  946. static void context_release(struct context *ctx)
  947. {
  948. struct fw_card *card = &ctx->ohci->card;
  949. struct descriptor_buffer *desc, *tmp;
  950. list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list)
  951. dma_free_coherent(card->device, PAGE_SIZE, desc,
  952. desc->buffer_bus -
  953. ((void *)&desc->buffer - (void *)desc));
  954. }
  955. /* Must be called with ohci->lock held */
  956. static struct descriptor *context_get_descriptors(struct context *ctx,
  957. int z, dma_addr_t *d_bus)
  958. {
  959. struct descriptor *d = NULL;
  960. struct descriptor_buffer *desc = ctx->buffer_tail;
  961. if (z * sizeof(*d) > desc->buffer_size)
  962. return NULL;
  963. if (z * sizeof(*d) > desc->buffer_size - desc->used) {
  964. /* No room for the descriptor in this buffer, so advance to the
  965. * next one. */
  966. if (desc->list.next == &ctx->buffer_list) {
  967. /* If there is no free buffer next in the list,
  968. * allocate one. */
  969. if (context_add_buffer(ctx) < 0)
  970. return NULL;
  971. }
  972. desc = list_entry(desc->list.next,
  973. struct descriptor_buffer, list);
  974. ctx->buffer_tail = desc;
  975. }
  976. d = desc->buffer + desc->used / sizeof(*d);
  977. memset(d, 0, z * sizeof(*d));
  978. *d_bus = desc->buffer_bus + desc->used;
  979. return d;
  980. }
  981. static void context_run(struct context *ctx, u32 extra)
  982. {
  983. struct fw_ohci *ohci = ctx->ohci;
  984. reg_write(ohci, COMMAND_PTR(ctx->regs),
  985. le32_to_cpu(ctx->last->branch_address));
  986. reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
  987. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
  988. ctx->running = true;
  989. flush_writes(ohci);
  990. }
  991. static void context_append(struct context *ctx,
  992. struct descriptor *d, int z, int extra)
  993. {
  994. dma_addr_t d_bus;
  995. struct descriptor_buffer *desc = ctx->buffer_tail;
  996. d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
  997. desc->used += (z + extra) * sizeof(*d);
  998. wmb(); /* finish init of new descriptors before branch_address update */
  999. ctx->prev->branch_address = cpu_to_le32(d_bus | z);
  1000. ctx->prev = find_branch_descriptor(d, z);
  1001. }
  1002. static void context_stop(struct context *ctx)
  1003. {
  1004. u32 reg;
  1005. int i;
  1006. reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  1007. ctx->running = false;
  1008. flush_writes(ctx->ohci);
  1009. for (i = 0; i < 10; i++) {
  1010. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  1011. if ((reg & CONTEXT_ACTIVE) == 0)
  1012. return;
  1013. mdelay(1);
  1014. }
  1015. fw_error("Error: DMA context still active (0x%08x)\n", reg);
  1016. }
  1017. struct driver_data {
  1018. u8 inline_data[8];
  1019. struct fw_packet *packet;
  1020. };
  1021. /*
  1022. * This function apppends a packet to the DMA queue for transmission.
  1023. * Must always be called with the ochi->lock held to ensure proper
  1024. * generation handling and locking around packet queue manipulation.
  1025. */
  1026. static int at_context_queue_packet(struct context *ctx,
  1027. struct fw_packet *packet)
  1028. {
  1029. struct fw_ohci *ohci = ctx->ohci;
  1030. dma_addr_t d_bus, uninitialized_var(payload_bus);
  1031. struct driver_data *driver_data;
  1032. struct descriptor *d, *last;
  1033. __le32 *header;
  1034. int z, tcode;
  1035. d = context_get_descriptors(ctx, 4, &d_bus);
  1036. if (d == NULL) {
  1037. packet->ack = RCODE_SEND_ERROR;
  1038. return -1;
  1039. }
  1040. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  1041. d[0].res_count = cpu_to_le16(packet->timestamp);
  1042. /*
  1043. * The DMA format for asyncronous link packets is different
  1044. * from the IEEE1394 layout, so shift the fields around
  1045. * accordingly.
  1046. */
  1047. tcode = (packet->header[0] >> 4) & 0x0f;
  1048. header = (__le32 *) &d[1];
  1049. switch (tcode) {
  1050. case TCODE_WRITE_QUADLET_REQUEST:
  1051. case TCODE_WRITE_BLOCK_REQUEST:
  1052. case TCODE_WRITE_RESPONSE:
  1053. case TCODE_READ_QUADLET_REQUEST:
  1054. case TCODE_READ_BLOCK_REQUEST:
  1055. case TCODE_READ_QUADLET_RESPONSE:
  1056. case TCODE_READ_BLOCK_RESPONSE:
  1057. case TCODE_LOCK_REQUEST:
  1058. case TCODE_LOCK_RESPONSE:
  1059. header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  1060. (packet->speed << 16));
  1061. header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  1062. (packet->header[0] & 0xffff0000));
  1063. header[2] = cpu_to_le32(packet->header[2]);
  1064. if (TCODE_IS_BLOCK_PACKET(tcode))
  1065. header[3] = cpu_to_le32(packet->header[3]);
  1066. else
  1067. header[3] = (__force __le32) packet->header[3];
  1068. d[0].req_count = cpu_to_le16(packet->header_length);
  1069. break;
  1070. case TCODE_LINK_INTERNAL:
  1071. header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
  1072. (packet->speed << 16));
  1073. header[1] = cpu_to_le32(packet->header[1]);
  1074. header[2] = cpu_to_le32(packet->header[2]);
  1075. d[0].req_count = cpu_to_le16(12);
  1076. if (is_ping_packet(&packet->header[1]))
  1077. d[0].control |= cpu_to_le16(DESCRIPTOR_PING);
  1078. break;
  1079. case TCODE_STREAM_DATA:
  1080. header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  1081. (packet->speed << 16));
  1082. header[1] = cpu_to_le32(packet->header[0] & 0xffff0000);
  1083. d[0].req_count = cpu_to_le16(8);
  1084. break;
  1085. default:
  1086. /* BUG(); */
  1087. packet->ack = RCODE_SEND_ERROR;
  1088. return -1;
  1089. }
  1090. BUILD_BUG_ON(sizeof(struct driver_data) > sizeof(struct descriptor));
  1091. driver_data = (struct driver_data *) &d[3];
  1092. driver_data->packet = packet;
  1093. packet->driver_data = driver_data;
  1094. if (packet->payload_length > 0) {
  1095. if (packet->payload_length > sizeof(driver_data->inline_data)) {
  1096. payload_bus = dma_map_single(ohci->card.device,
  1097. packet->payload,
  1098. packet->payload_length,
  1099. DMA_TO_DEVICE);
  1100. if (dma_mapping_error(ohci->card.device, payload_bus)) {
  1101. packet->ack = RCODE_SEND_ERROR;
  1102. return -1;
  1103. }
  1104. packet->payload_bus = payload_bus;
  1105. packet->payload_mapped = true;
  1106. } else {
  1107. memcpy(driver_data->inline_data, packet->payload,
  1108. packet->payload_length);
  1109. payload_bus = d_bus + 3 * sizeof(*d);
  1110. }
  1111. d[2].req_count = cpu_to_le16(packet->payload_length);
  1112. d[2].data_address = cpu_to_le32(payload_bus);
  1113. last = &d[2];
  1114. z = 3;
  1115. } else {
  1116. last = &d[0];
  1117. z = 2;
  1118. }
  1119. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  1120. DESCRIPTOR_IRQ_ALWAYS |
  1121. DESCRIPTOR_BRANCH_ALWAYS);
  1122. /* FIXME: Document how the locking works. */
  1123. if (ohci->generation != packet->generation) {
  1124. if (packet->payload_mapped)
  1125. dma_unmap_single(ohci->card.device, payload_bus,
  1126. packet->payload_length, DMA_TO_DEVICE);
  1127. packet->ack = RCODE_GENERATION;
  1128. return -1;
  1129. }
  1130. context_append(ctx, d, z, 4 - z);
  1131. if (ctx->running) {
  1132. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  1133. flush_writes(ohci);
  1134. } else {
  1135. context_run(ctx, 0);
  1136. }
  1137. return 0;
  1138. }
  1139. static void at_context_flush(struct context *ctx)
  1140. {
  1141. tasklet_disable(&ctx->tasklet);
  1142. ctx->flushing = true;
  1143. context_tasklet((unsigned long)ctx);
  1144. ctx->flushing = false;
  1145. tasklet_enable(&ctx->tasklet);
  1146. }
  1147. static int handle_at_packet(struct context *context,
  1148. struct descriptor *d,
  1149. struct descriptor *last)
  1150. {
  1151. struct driver_data *driver_data;
  1152. struct fw_packet *packet;
  1153. struct fw_ohci *ohci = context->ohci;
  1154. int evt;
  1155. if (last->transfer_status == 0 && !context->flushing)
  1156. /* This descriptor isn't done yet, stop iteration. */
  1157. return 0;
  1158. driver_data = (struct driver_data *) &d[3];
  1159. packet = driver_data->packet;
  1160. if (packet == NULL)
  1161. /* This packet was cancelled, just continue. */
  1162. return 1;
  1163. if (packet->payload_mapped)
  1164. dma_unmap_single(ohci->card.device, packet->payload_bus,
  1165. packet->payload_length, DMA_TO_DEVICE);
  1166. evt = le16_to_cpu(last->transfer_status) & 0x1f;
  1167. packet->timestamp = le16_to_cpu(last->res_count);
  1168. log_ar_at_event('T', packet->speed, packet->header, evt);
  1169. switch (evt) {
  1170. case OHCI1394_evt_timeout:
  1171. /* Async response transmit timed out. */
  1172. packet->ack = RCODE_CANCELLED;
  1173. break;
  1174. case OHCI1394_evt_flushed:
  1175. /*
  1176. * The packet was flushed should give same error as
  1177. * when we try to use a stale generation count.
  1178. */
  1179. packet->ack = RCODE_GENERATION;
  1180. break;
  1181. case OHCI1394_evt_missing_ack:
  1182. if (context->flushing)
  1183. packet->ack = RCODE_GENERATION;
  1184. else {
  1185. /*
  1186. * Using a valid (current) generation count, but the
  1187. * node is not on the bus or not sending acks.
  1188. */
  1189. packet->ack = RCODE_NO_ACK;
  1190. }
  1191. break;
  1192. case ACK_COMPLETE + 0x10:
  1193. case ACK_PENDING + 0x10:
  1194. case ACK_BUSY_X + 0x10:
  1195. case ACK_BUSY_A + 0x10:
  1196. case ACK_BUSY_B + 0x10:
  1197. case ACK_DATA_ERROR + 0x10:
  1198. case ACK_TYPE_ERROR + 0x10:
  1199. packet->ack = evt - 0x10;
  1200. break;
  1201. case OHCI1394_evt_no_status:
  1202. if (context->flushing) {
  1203. packet->ack = RCODE_GENERATION;
  1204. break;
  1205. }
  1206. /* fall through */
  1207. default:
  1208. packet->ack = RCODE_SEND_ERROR;
  1209. break;
  1210. }
  1211. packet->callback(packet, &ohci->card, packet->ack);
  1212. return 1;
  1213. }
  1214. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  1215. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  1216. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  1217. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  1218. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  1219. static void handle_local_rom(struct fw_ohci *ohci,
  1220. struct fw_packet *packet, u32 csr)
  1221. {
  1222. struct fw_packet response;
  1223. int tcode, length, i;
  1224. tcode = HEADER_GET_TCODE(packet->header[0]);
  1225. if (TCODE_IS_BLOCK_PACKET(tcode))
  1226. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  1227. else
  1228. length = 4;
  1229. i = csr - CSR_CONFIG_ROM;
  1230. if (i + length > CONFIG_ROM_SIZE) {
  1231. fw_fill_response(&response, packet->header,
  1232. RCODE_ADDRESS_ERROR, NULL, 0);
  1233. } else if (!TCODE_IS_READ_REQUEST(tcode)) {
  1234. fw_fill_response(&response, packet->header,
  1235. RCODE_TYPE_ERROR, NULL, 0);
  1236. } else {
  1237. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  1238. (void *) ohci->config_rom + i, length);
  1239. }
  1240. fw_core_handle_response(&ohci->card, &response);
  1241. }
  1242. static void handle_local_lock(struct fw_ohci *ohci,
  1243. struct fw_packet *packet, u32 csr)
  1244. {
  1245. struct fw_packet response;
  1246. int tcode, length, ext_tcode, sel, try;
  1247. __be32 *payload, lock_old;
  1248. u32 lock_arg, lock_data;
  1249. tcode = HEADER_GET_TCODE(packet->header[0]);
  1250. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  1251. payload = packet->payload;
  1252. ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
  1253. if (tcode == TCODE_LOCK_REQUEST &&
  1254. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  1255. lock_arg = be32_to_cpu(payload[0]);
  1256. lock_data = be32_to_cpu(payload[1]);
  1257. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  1258. lock_arg = 0;
  1259. lock_data = 0;
  1260. } else {
  1261. fw_fill_response(&response, packet->header,
  1262. RCODE_TYPE_ERROR, NULL, 0);
  1263. goto out;
  1264. }
  1265. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  1266. reg_write(ohci, OHCI1394_CSRData, lock_data);
  1267. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  1268. reg_write(ohci, OHCI1394_CSRControl, sel);
  1269. for (try = 0; try < 20; try++)
  1270. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000) {
  1271. lock_old = cpu_to_be32(reg_read(ohci,
  1272. OHCI1394_CSRData));
  1273. fw_fill_response(&response, packet->header,
  1274. RCODE_COMPLETE,
  1275. &lock_old, sizeof(lock_old));
  1276. goto out;
  1277. }
  1278. fw_error("swap not done (CSR lock timeout)\n");
  1279. fw_fill_response(&response, packet->header, RCODE_BUSY, NULL, 0);
  1280. out:
  1281. fw_core_handle_response(&ohci->card, &response);
  1282. }
  1283. static void handle_local_request(struct context *ctx, struct fw_packet *packet)
  1284. {
  1285. u64 offset, csr;
  1286. if (ctx == &ctx->ohci->at_request_ctx) {
  1287. packet->ack = ACK_PENDING;
  1288. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1289. }
  1290. offset =
  1291. ((unsigned long long)
  1292. HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
  1293. packet->header[2];
  1294. csr = offset - CSR_REGISTER_BASE;
  1295. /* Handle config rom reads. */
  1296. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  1297. handle_local_rom(ctx->ohci, packet, csr);
  1298. else switch (csr) {
  1299. case CSR_BUS_MANAGER_ID:
  1300. case CSR_BANDWIDTH_AVAILABLE:
  1301. case CSR_CHANNELS_AVAILABLE_HI:
  1302. case CSR_CHANNELS_AVAILABLE_LO:
  1303. handle_local_lock(ctx->ohci, packet, csr);
  1304. break;
  1305. default:
  1306. if (ctx == &ctx->ohci->at_request_ctx)
  1307. fw_core_handle_request(&ctx->ohci->card, packet);
  1308. else
  1309. fw_core_handle_response(&ctx->ohci->card, packet);
  1310. break;
  1311. }
  1312. if (ctx == &ctx->ohci->at_response_ctx) {
  1313. packet->ack = ACK_COMPLETE;
  1314. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1315. }
  1316. }
  1317. static void at_context_transmit(struct context *ctx, struct fw_packet *packet)
  1318. {
  1319. unsigned long flags;
  1320. int ret;
  1321. spin_lock_irqsave(&ctx->ohci->lock, flags);
  1322. if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
  1323. ctx->ohci->generation == packet->generation) {
  1324. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  1325. handle_local_request(ctx, packet);
  1326. return;
  1327. }
  1328. ret = at_context_queue_packet(ctx, packet);
  1329. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  1330. if (ret < 0)
  1331. packet->callback(packet, &ctx->ohci->card, packet->ack);
  1332. }
  1333. static void detect_dead_context(struct fw_ohci *ohci,
  1334. const char *name, unsigned int regs)
  1335. {
  1336. u32 ctl;
  1337. ctl = reg_read(ohci, CONTROL_SET(regs));
  1338. if (ctl & CONTEXT_DEAD) {
  1339. #ifdef CONFIG_FIREWIRE_OHCI_DEBUG
  1340. fw_error("DMA context %s has stopped, error code: %s\n",
  1341. name, evts[ctl & 0x1f]);
  1342. #else
  1343. fw_error("DMA context %s has stopped, error code: %#x\n",
  1344. name, ctl & 0x1f);
  1345. #endif
  1346. }
  1347. }
  1348. static void handle_dead_contexts(struct fw_ohci *ohci)
  1349. {
  1350. unsigned int i;
  1351. char name[8];
  1352. detect_dead_context(ohci, "ATReq", OHCI1394_AsReqTrContextBase);
  1353. detect_dead_context(ohci, "ATRsp", OHCI1394_AsRspTrContextBase);
  1354. detect_dead_context(ohci, "ARReq", OHCI1394_AsReqRcvContextBase);
  1355. detect_dead_context(ohci, "ARRsp", OHCI1394_AsRspRcvContextBase);
  1356. for (i = 0; i < 32; ++i) {
  1357. if (!(ohci->it_context_support & (1 << i)))
  1358. continue;
  1359. sprintf(name, "IT%u", i);
  1360. detect_dead_context(ohci, name, OHCI1394_IsoXmitContextBase(i));
  1361. }
  1362. for (i = 0; i < 32; ++i) {
  1363. if (!(ohci->ir_context_support & (1 << i)))
  1364. continue;
  1365. sprintf(name, "IR%u", i);
  1366. detect_dead_context(ohci, name, OHCI1394_IsoRcvContextBase(i));
  1367. }
  1368. /* TODO: maybe try to flush and restart the dead contexts */
  1369. }
  1370. static u32 cycle_timer_ticks(u32 cycle_timer)
  1371. {
  1372. u32 ticks;
  1373. ticks = cycle_timer & 0xfff;
  1374. ticks += 3072 * ((cycle_timer >> 12) & 0x1fff);
  1375. ticks += (3072 * 8000) * (cycle_timer >> 25);
  1376. return ticks;
  1377. }
  1378. /*
  1379. * Some controllers exhibit one or more of the following bugs when updating the
  1380. * iso cycle timer register:
  1381. * - When the lowest six bits are wrapping around to zero, a read that happens
  1382. * at the same time will return garbage in the lowest ten bits.
  1383. * - When the cycleOffset field wraps around to zero, the cycleCount field is
  1384. * not incremented for about 60 ns.
  1385. * - Occasionally, the entire register reads zero.
  1386. *
  1387. * To catch these, we read the register three times and ensure that the
  1388. * difference between each two consecutive reads is approximately the same, i.e.
  1389. * less than twice the other. Furthermore, any negative difference indicates an
  1390. * error. (A PCI read should take at least 20 ticks of the 24.576 MHz timer to
  1391. * execute, so we have enough precision to compute the ratio of the differences.)
  1392. */
  1393. static u32 get_cycle_time(struct fw_ohci *ohci)
  1394. {
  1395. u32 c0, c1, c2;
  1396. u32 t0, t1, t2;
  1397. s32 diff01, diff12;
  1398. int i;
  1399. c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1400. if (ohci->quirks & QUIRK_CYCLE_TIMER) {
  1401. i = 0;
  1402. c1 = c2;
  1403. c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1404. do {
  1405. c0 = c1;
  1406. c1 = c2;
  1407. c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1408. t0 = cycle_timer_ticks(c0);
  1409. t1 = cycle_timer_ticks(c1);
  1410. t2 = cycle_timer_ticks(c2);
  1411. diff01 = t1 - t0;
  1412. diff12 = t2 - t1;
  1413. } while ((diff01 <= 0 || diff12 <= 0 ||
  1414. diff01 / diff12 >= 2 || diff12 / diff01 >= 2)
  1415. && i++ < 20);
  1416. }
  1417. return c2;
  1418. }
  1419. /*
  1420. * This function has to be called at least every 64 seconds. The bus_time
  1421. * field stores not only the upper 25 bits of the BUS_TIME register but also
  1422. * the most significant bit of the cycle timer in bit 6 so that we can detect
  1423. * changes in this bit.
  1424. */
  1425. static u32 update_bus_time(struct fw_ohci *ohci)
  1426. {
  1427. u32 cycle_time_seconds = get_cycle_time(ohci) >> 25;
  1428. if ((ohci->bus_time & 0x40) != (cycle_time_seconds & 0x40))
  1429. ohci->bus_time += 0x40;
  1430. return ohci->bus_time | cycle_time_seconds;
  1431. }
  1432. static void bus_reset_tasklet(unsigned long data)
  1433. {
  1434. struct fw_ohci *ohci = (struct fw_ohci *)data;
  1435. int self_id_count, i, j, reg;
  1436. int generation, new_generation;
  1437. unsigned long flags;
  1438. void *free_rom = NULL;
  1439. dma_addr_t free_rom_bus = 0;
  1440. bool is_new_root;
  1441. reg = reg_read(ohci, OHCI1394_NodeID);
  1442. if (!(reg & OHCI1394_NodeID_idValid)) {
  1443. fw_notify("node ID not valid, new bus reset in progress\n");
  1444. return;
  1445. }
  1446. if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
  1447. fw_notify("malconfigured bus\n");
  1448. return;
  1449. }
  1450. ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
  1451. OHCI1394_NodeID_nodeNumber);
  1452. is_new_root = (reg & OHCI1394_NodeID_root) != 0;
  1453. if (!(ohci->is_root && is_new_root))
  1454. reg_write(ohci, OHCI1394_LinkControlSet,
  1455. OHCI1394_LinkControl_cycleMaster);
  1456. ohci->is_root = is_new_root;
  1457. reg = reg_read(ohci, OHCI1394_SelfIDCount);
  1458. if (reg & OHCI1394_SelfIDCount_selfIDError) {
  1459. fw_notify("inconsistent self IDs\n");
  1460. return;
  1461. }
  1462. /*
  1463. * The count in the SelfIDCount register is the number of
  1464. * bytes in the self ID receive buffer. Since we also receive
  1465. * the inverted quadlets and a header quadlet, we shift one
  1466. * bit extra to get the actual number of self IDs.
  1467. */
  1468. self_id_count = (reg >> 3) & 0xff;
  1469. if (self_id_count == 0 || self_id_count > 252) {
  1470. fw_notify("inconsistent self IDs\n");
  1471. return;
  1472. }
  1473. generation = (cond_le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  1474. rmb();
  1475. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  1476. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1]) {
  1477. fw_notify("inconsistent self IDs\n");
  1478. return;
  1479. }
  1480. ohci->self_id_buffer[j] =
  1481. cond_le32_to_cpu(ohci->self_id_cpu[i]);
  1482. }
  1483. rmb();
  1484. /*
  1485. * Check the consistency of the self IDs we just read. The
  1486. * problem we face is that a new bus reset can start while we
  1487. * read out the self IDs from the DMA buffer. If this happens,
  1488. * the DMA buffer will be overwritten with new self IDs and we
  1489. * will read out inconsistent data. The OHCI specification
  1490. * (section 11.2) recommends a technique similar to
  1491. * linux/seqlock.h, where we remember the generation of the
  1492. * self IDs in the buffer before reading them out and compare
  1493. * it to the current generation after reading them out. If
  1494. * the two generations match we know we have a consistent set
  1495. * of self IDs.
  1496. */
  1497. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  1498. if (new_generation != generation) {
  1499. fw_notify("recursive bus reset detected, "
  1500. "discarding self ids\n");
  1501. return;
  1502. }
  1503. /* FIXME: Document how the locking works. */
  1504. spin_lock_irqsave(&ohci->lock, flags);
  1505. ohci->generation = -1; /* prevent AT packet queueing */
  1506. context_stop(&ohci->at_request_ctx);
  1507. context_stop(&ohci->at_response_ctx);
  1508. spin_unlock_irqrestore(&ohci->lock, flags);
  1509. /*
  1510. * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent
  1511. * packets in the AT queues and software needs to drain them.
  1512. * Some OHCI 1.1 controllers (JMicron) apparently require this too.
  1513. */
  1514. at_context_flush(&ohci->at_request_ctx);
  1515. at_context_flush(&ohci->at_response_ctx);
  1516. spin_lock_irqsave(&ohci->lock, flags);
  1517. ohci->generation = generation;
  1518. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  1519. if (ohci->quirks & QUIRK_RESET_PACKET)
  1520. ohci->request_generation = generation;
  1521. /*
  1522. * This next bit is unrelated to the AT context stuff but we
  1523. * have to do it under the spinlock also. If a new config rom
  1524. * was set up before this reset, the old one is now no longer
  1525. * in use and we can free it. Update the config rom pointers
  1526. * to point to the current config rom and clear the
  1527. * next_config_rom pointer so a new update can take place.
  1528. */
  1529. if (ohci->next_config_rom != NULL) {
  1530. if (ohci->next_config_rom != ohci->config_rom) {
  1531. free_rom = ohci->config_rom;
  1532. free_rom_bus = ohci->config_rom_bus;
  1533. }
  1534. ohci->config_rom = ohci->next_config_rom;
  1535. ohci->config_rom_bus = ohci->next_config_rom_bus;
  1536. ohci->next_config_rom = NULL;
  1537. /*
  1538. * Restore config_rom image and manually update
  1539. * config_rom registers. Writing the header quadlet
  1540. * will indicate that the config rom is ready, so we
  1541. * do that last.
  1542. */
  1543. reg_write(ohci, OHCI1394_BusOptions,
  1544. be32_to_cpu(ohci->config_rom[2]));
  1545. ohci->config_rom[0] = ohci->next_header;
  1546. reg_write(ohci, OHCI1394_ConfigROMhdr,
  1547. be32_to_cpu(ohci->next_header));
  1548. }
  1549. #ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
  1550. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0);
  1551. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0);
  1552. #endif
  1553. spin_unlock_irqrestore(&ohci->lock, flags);
  1554. if (free_rom)
  1555. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1556. free_rom, free_rom_bus);
  1557. log_selfids(ohci->node_id, generation,
  1558. self_id_count, ohci->self_id_buffer);
  1559. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  1560. self_id_count, ohci->self_id_buffer,
  1561. ohci->csr_state_setclear_abdicate);
  1562. ohci->csr_state_setclear_abdicate = false;
  1563. }
  1564. static irqreturn_t irq_handler(int irq, void *data)
  1565. {
  1566. struct fw_ohci *ohci = data;
  1567. u32 event, iso_event;
  1568. int i;
  1569. event = reg_read(ohci, OHCI1394_IntEventClear);
  1570. if (!event || !~event)
  1571. return IRQ_NONE;
  1572. /*
  1573. * busReset and postedWriteErr must not be cleared yet
  1574. * (OHCI 1.1 clauses 7.2.3.2 and 13.2.8.1)
  1575. */
  1576. reg_write(ohci, OHCI1394_IntEventClear,
  1577. event & ~(OHCI1394_busReset | OHCI1394_postedWriteErr));
  1578. log_irqs(event);
  1579. if (event & OHCI1394_selfIDComplete)
  1580. tasklet_schedule(&ohci->bus_reset_tasklet);
  1581. if (event & OHCI1394_RQPkt)
  1582. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  1583. if (event & OHCI1394_RSPkt)
  1584. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  1585. if (event & OHCI1394_reqTxComplete)
  1586. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  1587. if (event & OHCI1394_respTxComplete)
  1588. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  1589. if (event & OHCI1394_isochRx) {
  1590. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  1591. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  1592. while (iso_event) {
  1593. i = ffs(iso_event) - 1;
  1594. tasklet_schedule(
  1595. &ohci->ir_context_list[i].context.tasklet);
  1596. iso_event &= ~(1 << i);
  1597. }
  1598. }
  1599. if (event & OHCI1394_isochTx) {
  1600. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  1601. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  1602. while (iso_event) {
  1603. i = ffs(iso_event) - 1;
  1604. tasklet_schedule(
  1605. &ohci->it_context_list[i].context.tasklet);
  1606. iso_event &= ~(1 << i);
  1607. }
  1608. }
  1609. if (unlikely(event & OHCI1394_regAccessFail))
  1610. fw_error("Register access failure - "
  1611. "please notify linux1394-devel@lists.sf.net\n");
  1612. if (unlikely(event & OHCI1394_postedWriteErr)) {
  1613. reg_read(ohci, OHCI1394_PostedWriteAddressHi);
  1614. reg_read(ohci, OHCI1394_PostedWriteAddressLo);
  1615. reg_write(ohci, OHCI1394_IntEventClear,
  1616. OHCI1394_postedWriteErr);
  1617. fw_error("PCI posted write error\n");
  1618. }
  1619. if (unlikely(event & OHCI1394_cycleTooLong)) {
  1620. if (printk_ratelimit())
  1621. fw_notify("isochronous cycle too long\n");
  1622. reg_write(ohci, OHCI1394_LinkControlSet,
  1623. OHCI1394_LinkControl_cycleMaster);
  1624. }
  1625. if (unlikely(event & OHCI1394_cycleInconsistent)) {
  1626. /*
  1627. * We need to clear this event bit in order to make
  1628. * cycleMatch isochronous I/O work. In theory we should
  1629. * stop active cycleMatch iso contexts now and restart
  1630. * them at least two cycles later. (FIXME?)
  1631. */
  1632. if (printk_ratelimit())
  1633. fw_notify("isochronous cycle inconsistent\n");
  1634. }
  1635. if (unlikely(event & OHCI1394_unrecoverableError))
  1636. handle_dead_contexts(ohci);
  1637. if (event & OHCI1394_cycle64Seconds) {
  1638. spin_lock(&ohci->lock);
  1639. update_bus_time(ohci);
  1640. spin_unlock(&ohci->lock);
  1641. } else
  1642. flush_writes(ohci);
  1643. return IRQ_HANDLED;
  1644. }
  1645. static int software_reset(struct fw_ohci *ohci)
  1646. {
  1647. int i;
  1648. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1649. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  1650. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  1651. OHCI1394_HCControl_softReset) == 0)
  1652. return 0;
  1653. msleep(1);
  1654. }
  1655. return -EBUSY;
  1656. }
  1657. static void copy_config_rom(__be32 *dest, const __be32 *src, size_t length)
  1658. {
  1659. size_t size = length * 4;
  1660. memcpy(dest, src, size);
  1661. if (size < CONFIG_ROM_SIZE)
  1662. memset(&dest[length], 0, CONFIG_ROM_SIZE - size);
  1663. }
  1664. static int configure_1394a_enhancements(struct fw_ohci *ohci)
  1665. {
  1666. bool enable_1394a;
  1667. int ret, clear, set, offset;
  1668. /* Check if the driver should configure link and PHY. */
  1669. if (!(reg_read(ohci, OHCI1394_HCControlSet) &
  1670. OHCI1394_HCControl_programPhyEnable))
  1671. return 0;
  1672. /* Paranoia: check whether the PHY supports 1394a, too. */
  1673. enable_1394a = false;
  1674. ret = read_phy_reg(ohci, 2);
  1675. if (ret < 0)
  1676. return ret;
  1677. if ((ret & PHY_EXTENDED_REGISTERS) == PHY_EXTENDED_REGISTERS) {
  1678. ret = read_paged_phy_reg(ohci, 1, 8);
  1679. if (ret < 0)
  1680. return ret;
  1681. if (ret >= 1)
  1682. enable_1394a = true;
  1683. }
  1684. if (ohci->quirks & QUIRK_NO_1394A)
  1685. enable_1394a = false;
  1686. /* Configure PHY and link consistently. */
  1687. if (enable_1394a) {
  1688. clear = 0;
  1689. set = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
  1690. } else {
  1691. clear = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
  1692. set = 0;
  1693. }
  1694. ret = update_phy_reg(ohci, 5, clear, set);
  1695. if (ret < 0)
  1696. return ret;
  1697. if (enable_1394a)
  1698. offset = OHCI1394_HCControlSet;
  1699. else
  1700. offset = OHCI1394_HCControlClear;
  1701. reg_write(ohci, offset, OHCI1394_HCControl_aPhyEnhanceEnable);
  1702. /* Clean up: configuration has been taken care of. */
  1703. reg_write(ohci, OHCI1394_HCControlClear,
  1704. OHCI1394_HCControl_programPhyEnable);
  1705. return 0;
  1706. }
  1707. static int ohci_enable(struct fw_card *card,
  1708. const __be32 *config_rom, size_t length)
  1709. {
  1710. struct fw_ohci *ohci = fw_ohci(card);
  1711. struct pci_dev *dev = to_pci_dev(card->device);
  1712. u32 lps, seconds, version, irqs;
  1713. int i, ret;
  1714. if (software_reset(ohci)) {
  1715. fw_error("Failed to reset ohci card.\n");
  1716. return -EBUSY;
  1717. }
  1718. /*
  1719. * Now enable LPS, which we need in order to start accessing
  1720. * most of the registers. In fact, on some cards (ALI M5251),
  1721. * accessing registers in the SClk domain without LPS enabled
  1722. * will lock up the machine. Wait 50msec to make sure we have
  1723. * full link enabled. However, with some cards (well, at least
  1724. * a JMicron PCIe card), we have to try again sometimes.
  1725. */
  1726. reg_write(ohci, OHCI1394_HCControlSet,
  1727. OHCI1394_HCControl_LPS |
  1728. OHCI1394_HCControl_postedWriteEnable);
  1729. flush_writes(ohci);
  1730. for (lps = 0, i = 0; !lps && i < 3; i++) {
  1731. msleep(50);
  1732. lps = reg_read(ohci, OHCI1394_HCControlSet) &
  1733. OHCI1394_HCControl_LPS;
  1734. }
  1735. if (!lps) {
  1736. fw_error("Failed to set Link Power Status\n");
  1737. return -EIO;
  1738. }
  1739. reg_write(ohci, OHCI1394_HCControlClear,
  1740. OHCI1394_HCControl_noByteSwapData);
  1741. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1742. reg_write(ohci, OHCI1394_LinkControlSet,
  1743. OHCI1394_LinkControl_cycleTimerEnable |
  1744. OHCI1394_LinkControl_cycleMaster);
  1745. reg_write(ohci, OHCI1394_ATRetries,
  1746. OHCI1394_MAX_AT_REQ_RETRIES |
  1747. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1748. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8) |
  1749. (200 << 16));
  1750. seconds = lower_32_bits(get_seconds());
  1751. reg_write(ohci, OHCI1394_IsochronousCycleTimer, seconds << 25);
  1752. ohci->bus_time = seconds & ~0x3f;
  1753. version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  1754. if (version >= OHCI_VERSION_1_1) {
  1755. reg_write(ohci, OHCI1394_InitialChannelsAvailableHi,
  1756. 0xfffffffe);
  1757. card->broadcast_channel_auto_allocated = true;
  1758. }
  1759. /* Get implemented bits of the priority arbitration request counter. */
  1760. reg_write(ohci, OHCI1394_FairnessControl, 0x3f);
  1761. ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f;
  1762. reg_write(ohci, OHCI1394_FairnessControl, 0);
  1763. card->priority_budget_implemented = ohci->pri_req_max != 0;
  1764. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1765. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1766. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1767. ret = configure_1394a_enhancements(ohci);
  1768. if (ret < 0)
  1769. return ret;
  1770. /* Activate link_on bit and contender bit in our self ID packets.*/
  1771. ret = ohci_update_phy_reg(card, 4, 0, PHY_LINK_ACTIVE | PHY_CONTENDER);
  1772. if (ret < 0)
  1773. return ret;
  1774. /*
  1775. * When the link is not yet enabled, the atomic config rom
  1776. * update mechanism described below in ohci_set_config_rom()
  1777. * is not active. We have to update ConfigRomHeader and
  1778. * BusOptions manually, and the write to ConfigROMmap takes
  1779. * effect immediately. We tie this to the enabling of the
  1780. * link, so we have a valid config rom before enabling - the
  1781. * OHCI requires that ConfigROMhdr and BusOptions have valid
  1782. * values before enabling.
  1783. *
  1784. * However, when the ConfigROMmap is written, some controllers
  1785. * always read back quadlets 0 and 2 from the config rom to
  1786. * the ConfigRomHeader and BusOptions registers on bus reset.
  1787. * They shouldn't do that in this initial case where the link
  1788. * isn't enabled. This means we have to use the same
  1789. * workaround here, setting the bus header to 0 and then write
  1790. * the right values in the bus reset tasklet.
  1791. */
  1792. if (config_rom) {
  1793. ohci->next_config_rom =
  1794. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1795. &ohci->next_config_rom_bus,
  1796. GFP_KERNEL);
  1797. if (ohci->next_config_rom == NULL)
  1798. return -ENOMEM;
  1799. copy_config_rom(ohci->next_config_rom, config_rom, length);
  1800. } else {
  1801. /*
  1802. * In the suspend case, config_rom is NULL, which
  1803. * means that we just reuse the old config rom.
  1804. */
  1805. ohci->next_config_rom = ohci->config_rom;
  1806. ohci->next_config_rom_bus = ohci->config_rom_bus;
  1807. }
  1808. ohci->next_header = ohci->next_config_rom[0];
  1809. ohci->next_config_rom[0] = 0;
  1810. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  1811. reg_write(ohci, OHCI1394_BusOptions,
  1812. be32_to_cpu(ohci->next_config_rom[2]));
  1813. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  1814. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  1815. if (!(ohci->quirks & QUIRK_NO_MSI))
  1816. pci_enable_msi(dev);
  1817. if (request_irq(dev->irq, irq_handler,
  1818. pci_dev_msi_enabled(dev) ? 0 : IRQF_SHARED,
  1819. ohci_driver_name, ohci)) {
  1820. fw_error("Failed to allocate interrupt %d.\n", dev->irq);
  1821. pci_disable_msi(dev);
  1822. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1823. ohci->config_rom, ohci->config_rom_bus);
  1824. return -EIO;
  1825. }
  1826. irqs = OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1827. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1828. OHCI1394_isochTx | OHCI1394_isochRx |
  1829. OHCI1394_postedWriteErr |
  1830. OHCI1394_selfIDComplete |
  1831. OHCI1394_regAccessFail |
  1832. OHCI1394_cycle64Seconds |
  1833. OHCI1394_cycleInconsistent |
  1834. OHCI1394_unrecoverableError |
  1835. OHCI1394_cycleTooLong |
  1836. OHCI1394_masterIntEnable;
  1837. if (param_debug & OHCI_PARAM_DEBUG_BUSRESETS)
  1838. irqs |= OHCI1394_busReset;
  1839. reg_write(ohci, OHCI1394_IntMaskSet, irqs);
  1840. reg_write(ohci, OHCI1394_HCControlSet,
  1841. OHCI1394_HCControl_linkEnable |
  1842. OHCI1394_HCControl_BIBimageValid);
  1843. reg_write(ohci, OHCI1394_LinkControlSet,
  1844. OHCI1394_LinkControl_rcvSelfID |
  1845. OHCI1394_LinkControl_rcvPhyPkt);
  1846. ar_context_run(&ohci->ar_request_ctx);
  1847. ar_context_run(&ohci->ar_response_ctx); /* also flushes writes */
  1848. /* We are ready to go, reset bus to finish initialization. */
  1849. fw_schedule_bus_reset(&ohci->card, false, true);
  1850. return 0;
  1851. }
  1852. static int ohci_set_config_rom(struct fw_card *card,
  1853. const __be32 *config_rom, size_t length)
  1854. {
  1855. struct fw_ohci *ohci;
  1856. unsigned long flags;
  1857. __be32 *next_config_rom;
  1858. dma_addr_t uninitialized_var(next_config_rom_bus);
  1859. ohci = fw_ohci(card);
  1860. /*
  1861. * When the OHCI controller is enabled, the config rom update
  1862. * mechanism is a bit tricky, but easy enough to use. See
  1863. * section 5.5.6 in the OHCI specification.
  1864. *
  1865. * The OHCI controller caches the new config rom address in a
  1866. * shadow register (ConfigROMmapNext) and needs a bus reset
  1867. * for the changes to take place. When the bus reset is
  1868. * detected, the controller loads the new values for the
  1869. * ConfigRomHeader and BusOptions registers from the specified
  1870. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  1871. * shadow register. All automatically and atomically.
  1872. *
  1873. * Now, there's a twist to this story. The automatic load of
  1874. * ConfigRomHeader and BusOptions doesn't honor the
  1875. * noByteSwapData bit, so with a be32 config rom, the
  1876. * controller will load be32 values in to these registers
  1877. * during the atomic update, even on litte endian
  1878. * architectures. The workaround we use is to put a 0 in the
  1879. * header quadlet; 0 is endian agnostic and means that the
  1880. * config rom isn't ready yet. In the bus reset tasklet we
  1881. * then set up the real values for the two registers.
  1882. *
  1883. * We use ohci->lock to avoid racing with the code that sets
  1884. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  1885. */
  1886. next_config_rom =
  1887. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1888. &next_config_rom_bus, GFP_KERNEL);
  1889. if (next_config_rom == NULL)
  1890. return -ENOMEM;
  1891. spin_lock_irqsave(&ohci->lock, flags);
  1892. /*
  1893. * If there is not an already pending config_rom update,
  1894. * push our new allocation into the ohci->next_config_rom
  1895. * and then mark the local variable as null so that we
  1896. * won't deallocate the new buffer.
  1897. *
  1898. * OTOH, if there is a pending config_rom update, just
  1899. * use that buffer with the new config_rom data, and
  1900. * let this routine free the unused DMA allocation.
  1901. */
  1902. if (ohci->next_config_rom == NULL) {
  1903. ohci->next_config_rom = next_config_rom;
  1904. ohci->next_config_rom_bus = next_config_rom_bus;
  1905. next_config_rom = NULL;
  1906. }
  1907. copy_config_rom(ohci->next_config_rom, config_rom, length);
  1908. ohci->next_header = config_rom[0];
  1909. ohci->next_config_rom[0] = 0;
  1910. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  1911. spin_unlock_irqrestore(&ohci->lock, flags);
  1912. /* If we didn't use the DMA allocation, delete it. */
  1913. if (next_config_rom != NULL)
  1914. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1915. next_config_rom, next_config_rom_bus);
  1916. /*
  1917. * Now initiate a bus reset to have the changes take
  1918. * effect. We clean up the old config rom memory and DMA
  1919. * mappings in the bus reset tasklet, since the OHCI
  1920. * controller could need to access it before the bus reset
  1921. * takes effect.
  1922. */
  1923. fw_schedule_bus_reset(&ohci->card, true, true);
  1924. return 0;
  1925. }
  1926. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  1927. {
  1928. struct fw_ohci *ohci = fw_ohci(card);
  1929. at_context_transmit(&ohci->at_request_ctx, packet);
  1930. }
  1931. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  1932. {
  1933. struct fw_ohci *ohci = fw_ohci(card);
  1934. at_context_transmit(&ohci->at_response_ctx, packet);
  1935. }
  1936. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  1937. {
  1938. struct fw_ohci *ohci = fw_ohci(card);
  1939. struct context *ctx = &ohci->at_request_ctx;
  1940. struct driver_data *driver_data = packet->driver_data;
  1941. int ret = -ENOENT;
  1942. tasklet_disable(&ctx->tasklet);
  1943. if (packet->ack != 0)
  1944. goto out;
  1945. if (packet->payload_mapped)
  1946. dma_unmap_single(ohci->card.device, packet->payload_bus,
  1947. packet->payload_length, DMA_TO_DEVICE);
  1948. log_ar_at_event('T', packet->speed, packet->header, 0x20);
  1949. driver_data->packet = NULL;
  1950. packet->ack = RCODE_CANCELLED;
  1951. packet->callback(packet, &ohci->card, packet->ack);
  1952. ret = 0;
  1953. out:
  1954. tasklet_enable(&ctx->tasklet);
  1955. return ret;
  1956. }
  1957. static int ohci_enable_phys_dma(struct fw_card *card,
  1958. int node_id, int generation)
  1959. {
  1960. #ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
  1961. return 0;
  1962. #else
  1963. struct fw_ohci *ohci = fw_ohci(card);
  1964. unsigned long flags;
  1965. int n, ret = 0;
  1966. /*
  1967. * FIXME: Make sure this bitmask is cleared when we clear the busReset
  1968. * interrupt bit. Clear physReqResourceAllBuses on bus reset.
  1969. */
  1970. spin_lock_irqsave(&ohci->lock, flags);
  1971. if (ohci->generation != generation) {
  1972. ret = -ESTALE;
  1973. goto out;
  1974. }
  1975. /*
  1976. * Note, if the node ID contains a non-local bus ID, physical DMA is
  1977. * enabled for _all_ nodes on remote buses.
  1978. */
  1979. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  1980. if (n < 32)
  1981. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  1982. else
  1983. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  1984. flush_writes(ohci);
  1985. out:
  1986. spin_unlock_irqrestore(&ohci->lock, flags);
  1987. return ret;
  1988. #endif /* CONFIG_FIREWIRE_OHCI_REMOTE_DMA */
  1989. }
  1990. static u32 ohci_read_csr(struct fw_card *card, int csr_offset)
  1991. {
  1992. struct fw_ohci *ohci = fw_ohci(card);
  1993. unsigned long flags;
  1994. u32 value;
  1995. switch (csr_offset) {
  1996. case CSR_STATE_CLEAR:
  1997. case CSR_STATE_SET:
  1998. if (ohci->is_root &&
  1999. (reg_read(ohci, OHCI1394_LinkControlSet) &
  2000. OHCI1394_LinkControl_cycleMaster))
  2001. value = CSR_STATE_BIT_CMSTR;
  2002. else
  2003. value = 0;
  2004. if (ohci->csr_state_setclear_abdicate)
  2005. value |= CSR_STATE_BIT_ABDICATE;
  2006. return value;
  2007. case CSR_NODE_IDS:
  2008. return reg_read(ohci, OHCI1394_NodeID) << 16;
  2009. case CSR_CYCLE_TIME:
  2010. return get_cycle_time(ohci);
  2011. case CSR_BUS_TIME:
  2012. /*
  2013. * We might be called just after the cycle timer has wrapped
  2014. * around but just before the cycle64Seconds handler, so we
  2015. * better check here, too, if the bus time needs to be updated.
  2016. */
  2017. spin_lock_irqsave(&ohci->lock, flags);
  2018. value = update_bus_time(ohci);
  2019. spin_unlock_irqrestore(&ohci->lock, flags);
  2020. return value;
  2021. case CSR_BUSY_TIMEOUT:
  2022. value = reg_read(ohci, OHCI1394_ATRetries);
  2023. return (value >> 4) & 0x0ffff00f;
  2024. case CSR_PRIORITY_BUDGET:
  2025. return (reg_read(ohci, OHCI1394_FairnessControl) & 0x3f) |
  2026. (ohci->pri_req_max << 8);
  2027. default:
  2028. WARN_ON(1);
  2029. return 0;
  2030. }
  2031. }
  2032. static void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value)
  2033. {
  2034. struct fw_ohci *ohci = fw_ohci(card);
  2035. unsigned long flags;
  2036. switch (csr_offset) {
  2037. case CSR_STATE_CLEAR:
  2038. if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
  2039. reg_write(ohci, OHCI1394_LinkControlClear,
  2040. OHCI1394_LinkControl_cycleMaster);
  2041. flush_writes(ohci);
  2042. }
  2043. if (value & CSR_STATE_BIT_ABDICATE)
  2044. ohci->csr_state_setclear_abdicate = false;
  2045. break;
  2046. case CSR_STATE_SET:
  2047. if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
  2048. reg_write(ohci, OHCI1394_LinkControlSet,
  2049. OHCI1394_LinkControl_cycleMaster);
  2050. flush_writes(ohci);
  2051. }
  2052. if (value & CSR_STATE_BIT_ABDICATE)
  2053. ohci->csr_state_setclear_abdicate = true;
  2054. break;
  2055. case CSR_NODE_IDS:
  2056. reg_write(ohci, OHCI1394_NodeID, value >> 16);
  2057. flush_writes(ohci);
  2058. break;
  2059. case CSR_CYCLE_TIME:
  2060. reg_write(ohci, OHCI1394_IsochronousCycleTimer, value);
  2061. reg_write(ohci, OHCI1394_IntEventSet,
  2062. OHCI1394_cycleInconsistent);
  2063. flush_writes(ohci);
  2064. break;
  2065. case CSR_BUS_TIME:
  2066. spin_lock_irqsave(&ohci->lock, flags);
  2067. ohci->bus_time = (ohci->bus_time & 0x7f) | (value & ~0x7f);
  2068. spin_unlock_irqrestore(&ohci->lock, flags);
  2069. break;
  2070. case CSR_BUSY_TIMEOUT:
  2071. value = (value & 0xf) | ((value & 0xf) << 4) |
  2072. ((value & 0xf) << 8) | ((value & 0x0ffff000) << 4);
  2073. reg_write(ohci, OHCI1394_ATRetries, value);
  2074. flush_writes(ohci);
  2075. break;
  2076. case CSR_PRIORITY_BUDGET:
  2077. reg_write(ohci, OHCI1394_FairnessControl, value & 0x3f);
  2078. flush_writes(ohci);
  2079. break;
  2080. default:
  2081. WARN_ON(1);
  2082. break;
  2083. }
  2084. }
  2085. static void copy_iso_headers(struct iso_context *ctx, void *p)
  2086. {
  2087. int i = ctx->header_length;
  2088. if (i + ctx->base.header_size > PAGE_SIZE)
  2089. return;
  2090. /*
  2091. * The iso header is byteswapped to little endian by
  2092. * the controller, but the remaining header quadlets
  2093. * are big endian. We want to present all the headers
  2094. * as big endian, so we have to swap the first quadlet.
  2095. */
  2096. if (ctx->base.header_size > 0)
  2097. *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
  2098. if (ctx->base.header_size > 4)
  2099. *(u32 *) (ctx->header + i + 4) = __swab32(*(u32 *) p);
  2100. if (ctx->base.header_size > 8)
  2101. memcpy(ctx->header + i + 8, p + 8, ctx->base.header_size - 8);
  2102. ctx->header_length += ctx->base.header_size;
  2103. }
  2104. static int handle_ir_packet_per_buffer(struct context *context,
  2105. struct descriptor *d,
  2106. struct descriptor *last)
  2107. {
  2108. struct iso_context *ctx =
  2109. container_of(context, struct iso_context, context);
  2110. struct descriptor *pd;
  2111. __le32 *ir_header;
  2112. void *p;
  2113. for (pd = d; pd <= last; pd++)
  2114. if (pd->transfer_status)
  2115. break;
  2116. if (pd > last)
  2117. /* Descriptor(s) not done yet, stop iteration */
  2118. return 0;
  2119. p = last + 1;
  2120. copy_iso_headers(ctx, p);
  2121. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS) {
  2122. ir_header = (__le32 *) p;
  2123. ctx->base.callback.sc(&ctx->base,
  2124. le32_to_cpu(ir_header[0]) & 0xffff,
  2125. ctx->header_length, ctx->header,
  2126. ctx->base.callback_data);
  2127. ctx->header_length = 0;
  2128. }
  2129. return 1;
  2130. }
  2131. /* d == last because each descriptor block is only a single descriptor. */
  2132. static int handle_ir_buffer_fill(struct context *context,
  2133. struct descriptor *d,
  2134. struct descriptor *last)
  2135. {
  2136. struct iso_context *ctx =
  2137. container_of(context, struct iso_context, context);
  2138. if (!last->transfer_status)
  2139. /* Descriptor(s) not done yet, stop iteration */
  2140. return 0;
  2141. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
  2142. ctx->base.callback.mc(&ctx->base,
  2143. le32_to_cpu(last->data_address) +
  2144. le16_to_cpu(last->req_count) -
  2145. le16_to_cpu(last->res_count),
  2146. ctx->base.callback_data);
  2147. return 1;
  2148. }
  2149. static int handle_it_packet(struct context *context,
  2150. struct descriptor *d,
  2151. struct descriptor *last)
  2152. {
  2153. struct iso_context *ctx =
  2154. container_of(context, struct iso_context, context);
  2155. int i;
  2156. struct descriptor *pd;
  2157. for (pd = d; pd <= last; pd++)
  2158. if (pd->transfer_status)
  2159. break;
  2160. if (pd > last)
  2161. /* Descriptor(s) not done yet, stop iteration */
  2162. return 0;
  2163. i = ctx->header_length;
  2164. if (i + 4 < PAGE_SIZE) {
  2165. /* Present this value as big-endian to match the receive code */
  2166. *(__be32 *)(ctx->header + i) = cpu_to_be32(
  2167. ((u32)le16_to_cpu(pd->transfer_status) << 16) |
  2168. le16_to_cpu(pd->res_count));
  2169. ctx->header_length += 4;
  2170. }
  2171. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS) {
  2172. ctx->base.callback.sc(&ctx->base, le16_to_cpu(last->res_count),
  2173. ctx->header_length, ctx->header,
  2174. ctx->base.callback_data);
  2175. ctx->header_length = 0;
  2176. }
  2177. return 1;
  2178. }
  2179. static void set_multichannel_mask(struct fw_ohci *ohci, u64 channels)
  2180. {
  2181. u32 hi = channels >> 32, lo = channels;
  2182. reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear, ~hi);
  2183. reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear, ~lo);
  2184. reg_write(ohci, OHCI1394_IRMultiChanMaskHiSet, hi);
  2185. reg_write(ohci, OHCI1394_IRMultiChanMaskLoSet, lo);
  2186. mmiowb();
  2187. ohci->mc_channels = channels;
  2188. }
  2189. static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
  2190. int type, int channel, size_t header_size)
  2191. {
  2192. struct fw_ohci *ohci = fw_ohci(card);
  2193. struct iso_context *uninitialized_var(ctx);
  2194. descriptor_callback_t uninitialized_var(callback);
  2195. u64 *uninitialized_var(channels);
  2196. u32 *uninitialized_var(mask), uninitialized_var(regs);
  2197. unsigned long flags;
  2198. int index, ret = -EBUSY;
  2199. spin_lock_irqsave(&ohci->lock, flags);
  2200. switch (type) {
  2201. case FW_ISO_CONTEXT_TRANSMIT:
  2202. mask = &ohci->it_context_mask;
  2203. callback = handle_it_packet;
  2204. index = ffs(*mask) - 1;
  2205. if (index >= 0) {
  2206. *mask &= ~(1 << index);
  2207. regs = OHCI1394_IsoXmitContextBase(index);
  2208. ctx = &ohci->it_context_list[index];
  2209. }
  2210. break;
  2211. case FW_ISO_CONTEXT_RECEIVE:
  2212. channels = &ohci->ir_context_channels;
  2213. mask = &ohci->ir_context_mask;
  2214. callback = handle_ir_packet_per_buffer;
  2215. index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1;
  2216. if (index >= 0) {
  2217. *channels &= ~(1ULL << channel);
  2218. *mask &= ~(1 << index);
  2219. regs = OHCI1394_IsoRcvContextBase(index);
  2220. ctx = &ohci->ir_context_list[index];
  2221. }
  2222. break;
  2223. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2224. mask = &ohci->ir_context_mask;
  2225. callback = handle_ir_buffer_fill;
  2226. index = !ohci->mc_allocated ? ffs(*mask) - 1 : -1;
  2227. if (index >= 0) {
  2228. ohci->mc_allocated = true;
  2229. *mask &= ~(1 << index);
  2230. regs = OHCI1394_IsoRcvContextBase(index);
  2231. ctx = &ohci->ir_context_list[index];
  2232. }
  2233. break;
  2234. default:
  2235. index = -1;
  2236. ret = -ENOSYS;
  2237. }
  2238. spin_unlock_irqrestore(&ohci->lock, flags);
  2239. if (index < 0)
  2240. return ERR_PTR(ret);
  2241. memset(ctx, 0, sizeof(*ctx));
  2242. ctx->header_length = 0;
  2243. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  2244. if (ctx->header == NULL) {
  2245. ret = -ENOMEM;
  2246. goto out;
  2247. }
  2248. ret = context_init(&ctx->context, ohci, regs, callback);
  2249. if (ret < 0)
  2250. goto out_with_header;
  2251. if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL)
  2252. set_multichannel_mask(ohci, 0);
  2253. return &ctx->base;
  2254. out_with_header:
  2255. free_page((unsigned long)ctx->header);
  2256. out:
  2257. spin_lock_irqsave(&ohci->lock, flags);
  2258. switch (type) {
  2259. case FW_ISO_CONTEXT_RECEIVE:
  2260. *channels |= 1ULL << channel;
  2261. break;
  2262. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2263. ohci->mc_allocated = false;
  2264. break;
  2265. }
  2266. *mask |= 1 << index;
  2267. spin_unlock_irqrestore(&ohci->lock, flags);
  2268. return ERR_PTR(ret);
  2269. }
  2270. static int ohci_start_iso(struct fw_iso_context *base,
  2271. s32 cycle, u32 sync, u32 tags)
  2272. {
  2273. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2274. struct fw_ohci *ohci = ctx->context.ohci;
  2275. u32 control = IR_CONTEXT_ISOCH_HEADER, match;
  2276. int index;
  2277. /* the controller cannot start without any queued packets */
  2278. if (ctx->context.last->branch_address == 0)
  2279. return -ENODATA;
  2280. switch (ctx->base.type) {
  2281. case FW_ISO_CONTEXT_TRANSMIT:
  2282. index = ctx - ohci->it_context_list;
  2283. match = 0;
  2284. if (cycle >= 0)
  2285. match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  2286. (cycle & 0x7fff) << 16;
  2287. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  2288. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  2289. context_run(&ctx->context, match);
  2290. break;
  2291. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2292. control |= IR_CONTEXT_BUFFER_FILL|IR_CONTEXT_MULTI_CHANNEL_MODE;
  2293. /* fall through */
  2294. case FW_ISO_CONTEXT_RECEIVE:
  2295. index = ctx - ohci->ir_context_list;
  2296. match = (tags << 28) | (sync << 8) | ctx->base.channel;
  2297. if (cycle >= 0) {
  2298. match |= (cycle & 0x07fff) << 12;
  2299. control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
  2300. }
  2301. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  2302. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  2303. reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
  2304. context_run(&ctx->context, control);
  2305. ctx->sync = sync;
  2306. ctx->tags = tags;
  2307. break;
  2308. }
  2309. return 0;
  2310. }
  2311. static int ohci_stop_iso(struct fw_iso_context *base)
  2312. {
  2313. struct fw_ohci *ohci = fw_ohci(base->card);
  2314. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2315. int index;
  2316. switch (ctx->base.type) {
  2317. case FW_ISO_CONTEXT_TRANSMIT:
  2318. index = ctx - ohci->it_context_list;
  2319. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  2320. break;
  2321. case FW_ISO_CONTEXT_RECEIVE:
  2322. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2323. index = ctx - ohci->ir_context_list;
  2324. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  2325. break;
  2326. }
  2327. flush_writes(ohci);
  2328. context_stop(&ctx->context);
  2329. tasklet_kill(&ctx->context.tasklet);
  2330. return 0;
  2331. }
  2332. static void ohci_free_iso_context(struct fw_iso_context *base)
  2333. {
  2334. struct fw_ohci *ohci = fw_ohci(base->card);
  2335. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2336. unsigned long flags;
  2337. int index;
  2338. ohci_stop_iso(base);
  2339. context_release(&ctx->context);
  2340. free_page((unsigned long)ctx->header);
  2341. spin_lock_irqsave(&ohci->lock, flags);
  2342. switch (base->type) {
  2343. case FW_ISO_CONTEXT_TRANSMIT:
  2344. index = ctx - ohci->it_context_list;
  2345. ohci->it_context_mask |= 1 << index;
  2346. break;
  2347. case FW_ISO_CONTEXT_RECEIVE:
  2348. index = ctx - ohci->ir_context_list;
  2349. ohci->ir_context_mask |= 1 << index;
  2350. ohci->ir_context_channels |= 1ULL << base->channel;
  2351. break;
  2352. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2353. index = ctx - ohci->ir_context_list;
  2354. ohci->ir_context_mask |= 1 << index;
  2355. ohci->ir_context_channels |= ohci->mc_channels;
  2356. ohci->mc_channels = 0;
  2357. ohci->mc_allocated = false;
  2358. break;
  2359. }
  2360. spin_unlock_irqrestore(&ohci->lock, flags);
  2361. }
  2362. static int ohci_set_iso_channels(struct fw_iso_context *base, u64 *channels)
  2363. {
  2364. struct fw_ohci *ohci = fw_ohci(base->card);
  2365. unsigned long flags;
  2366. int ret;
  2367. switch (base->type) {
  2368. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2369. spin_lock_irqsave(&ohci->lock, flags);
  2370. /* Don't allow multichannel to grab other contexts' channels. */
  2371. if (~ohci->ir_context_channels & ~ohci->mc_channels & *channels) {
  2372. *channels = ohci->ir_context_channels;
  2373. ret = -EBUSY;
  2374. } else {
  2375. set_multichannel_mask(ohci, *channels);
  2376. ret = 0;
  2377. }
  2378. spin_unlock_irqrestore(&ohci->lock, flags);
  2379. break;
  2380. default:
  2381. ret = -EINVAL;
  2382. }
  2383. return ret;
  2384. }
  2385. #ifdef CONFIG_PM
  2386. static void ohci_resume_iso_dma(struct fw_ohci *ohci)
  2387. {
  2388. int i;
  2389. struct iso_context *ctx;
  2390. for (i = 0 ; i < ohci->n_ir ; i++) {
  2391. ctx = &ohci->ir_context_list[i];
  2392. if (ctx->context.running)
  2393. ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
  2394. }
  2395. for (i = 0 ; i < ohci->n_it ; i++) {
  2396. ctx = &ohci->it_context_list[i];
  2397. if (ctx->context.running)
  2398. ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
  2399. }
  2400. }
  2401. #endif
  2402. static int queue_iso_transmit(struct iso_context *ctx,
  2403. struct fw_iso_packet *packet,
  2404. struct fw_iso_buffer *buffer,
  2405. unsigned long payload)
  2406. {
  2407. struct descriptor *d, *last, *pd;
  2408. struct fw_iso_packet *p;
  2409. __le32 *header;
  2410. dma_addr_t d_bus, page_bus;
  2411. u32 z, header_z, payload_z, irq;
  2412. u32 payload_index, payload_end_index, next_page_index;
  2413. int page, end_page, i, length, offset;
  2414. p = packet;
  2415. payload_index = payload;
  2416. if (p->skip)
  2417. z = 1;
  2418. else
  2419. z = 2;
  2420. if (p->header_length > 0)
  2421. z++;
  2422. /* Determine the first page the payload isn't contained in. */
  2423. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  2424. if (p->payload_length > 0)
  2425. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  2426. else
  2427. payload_z = 0;
  2428. z += payload_z;
  2429. /* Get header size in number of descriptors. */
  2430. header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
  2431. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  2432. if (d == NULL)
  2433. return -ENOMEM;
  2434. if (!p->skip) {
  2435. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  2436. d[0].req_count = cpu_to_le16(8);
  2437. /*
  2438. * Link the skip address to this descriptor itself. This causes
  2439. * a context to skip a cycle whenever lost cycles or FIFO
  2440. * overruns occur, without dropping the data. The application
  2441. * should then decide whether this is an error condition or not.
  2442. * FIXME: Make the context's cycle-lost behaviour configurable?
  2443. */
  2444. d[0].branch_address = cpu_to_le32(d_bus | z);
  2445. header = (__le32 *) &d[1];
  2446. header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
  2447. IT_HEADER_TAG(p->tag) |
  2448. IT_HEADER_TCODE(TCODE_STREAM_DATA) |
  2449. IT_HEADER_CHANNEL(ctx->base.channel) |
  2450. IT_HEADER_SPEED(ctx->base.speed));
  2451. header[1] =
  2452. cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
  2453. p->payload_length));
  2454. }
  2455. if (p->header_length > 0) {
  2456. d[2].req_count = cpu_to_le16(p->header_length);
  2457. d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
  2458. memcpy(&d[z], p->header, p->header_length);
  2459. }
  2460. pd = d + z - payload_z;
  2461. payload_end_index = payload_index + p->payload_length;
  2462. for (i = 0; i < payload_z; i++) {
  2463. page = payload_index >> PAGE_SHIFT;
  2464. offset = payload_index & ~PAGE_MASK;
  2465. next_page_index = (page + 1) << PAGE_SHIFT;
  2466. length =
  2467. min(next_page_index, payload_end_index) - payload_index;
  2468. pd[i].req_count = cpu_to_le16(length);
  2469. page_bus = page_private(buffer->pages[page]);
  2470. pd[i].data_address = cpu_to_le32(page_bus + offset);
  2471. payload_index += length;
  2472. }
  2473. if (p->interrupt)
  2474. irq = DESCRIPTOR_IRQ_ALWAYS;
  2475. else
  2476. irq = DESCRIPTOR_NO_IRQ;
  2477. last = z == 2 ? d : d + z - 1;
  2478. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  2479. DESCRIPTOR_STATUS |
  2480. DESCRIPTOR_BRANCH_ALWAYS |
  2481. irq);
  2482. context_append(&ctx->context, d, z, header_z);
  2483. return 0;
  2484. }
  2485. static int queue_iso_packet_per_buffer(struct iso_context *ctx,
  2486. struct fw_iso_packet *packet,
  2487. struct fw_iso_buffer *buffer,
  2488. unsigned long payload)
  2489. {
  2490. struct descriptor *d, *pd;
  2491. dma_addr_t d_bus, page_bus;
  2492. u32 z, header_z, rest;
  2493. int i, j, length;
  2494. int page, offset, packet_count, header_size, payload_per_buffer;
  2495. /*
  2496. * The OHCI controller puts the isochronous header and trailer in the
  2497. * buffer, so we need at least 8 bytes.
  2498. */
  2499. packet_count = packet->header_length / ctx->base.header_size;
  2500. header_size = max(ctx->base.header_size, (size_t)8);
  2501. /* Get header size in number of descriptors. */
  2502. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  2503. page = payload >> PAGE_SHIFT;
  2504. offset = payload & ~PAGE_MASK;
  2505. payload_per_buffer = packet->payload_length / packet_count;
  2506. for (i = 0; i < packet_count; i++) {
  2507. /* d points to the header descriptor */
  2508. z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
  2509. d = context_get_descriptors(&ctx->context,
  2510. z + header_z, &d_bus);
  2511. if (d == NULL)
  2512. return -ENOMEM;
  2513. d->control = cpu_to_le16(DESCRIPTOR_STATUS |
  2514. DESCRIPTOR_INPUT_MORE);
  2515. if (packet->skip && i == 0)
  2516. d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  2517. d->req_count = cpu_to_le16(header_size);
  2518. d->res_count = d->req_count;
  2519. d->transfer_status = 0;
  2520. d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
  2521. rest = payload_per_buffer;
  2522. pd = d;
  2523. for (j = 1; j < z; j++) {
  2524. pd++;
  2525. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  2526. DESCRIPTOR_INPUT_MORE);
  2527. if (offset + rest < PAGE_SIZE)
  2528. length = rest;
  2529. else
  2530. length = PAGE_SIZE - offset;
  2531. pd->req_count = cpu_to_le16(length);
  2532. pd->res_count = pd->req_count;
  2533. pd->transfer_status = 0;
  2534. page_bus = page_private(buffer->pages[page]);
  2535. pd->data_address = cpu_to_le32(page_bus + offset);
  2536. offset = (offset + length) & ~PAGE_MASK;
  2537. rest -= length;
  2538. if (offset == 0)
  2539. page++;
  2540. }
  2541. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  2542. DESCRIPTOR_INPUT_LAST |
  2543. DESCRIPTOR_BRANCH_ALWAYS);
  2544. if (packet->interrupt && i == packet_count - 1)
  2545. pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  2546. context_append(&ctx->context, d, z, header_z);
  2547. }
  2548. return 0;
  2549. }
  2550. static int queue_iso_buffer_fill(struct iso_context *ctx,
  2551. struct fw_iso_packet *packet,
  2552. struct fw_iso_buffer *buffer,
  2553. unsigned long payload)
  2554. {
  2555. struct descriptor *d;
  2556. dma_addr_t d_bus, page_bus;
  2557. int page, offset, rest, z, i, length;
  2558. page = payload >> PAGE_SHIFT;
  2559. offset = payload & ~PAGE_MASK;
  2560. rest = packet->payload_length;
  2561. /* We need one descriptor for each page in the buffer. */
  2562. z = DIV_ROUND_UP(offset + rest, PAGE_SIZE);
  2563. if (WARN_ON(offset & 3 || rest & 3 || page + z > buffer->page_count))
  2564. return -EFAULT;
  2565. for (i = 0; i < z; i++) {
  2566. d = context_get_descriptors(&ctx->context, 1, &d_bus);
  2567. if (d == NULL)
  2568. return -ENOMEM;
  2569. d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  2570. DESCRIPTOR_BRANCH_ALWAYS);
  2571. if (packet->skip && i == 0)
  2572. d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  2573. if (packet->interrupt && i == z - 1)
  2574. d->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  2575. if (offset + rest < PAGE_SIZE)
  2576. length = rest;
  2577. else
  2578. length = PAGE_SIZE - offset;
  2579. d->req_count = cpu_to_le16(length);
  2580. d->res_count = d->req_count;
  2581. d->transfer_status = 0;
  2582. page_bus = page_private(buffer->pages[page]);
  2583. d->data_address = cpu_to_le32(page_bus + offset);
  2584. rest -= length;
  2585. offset = 0;
  2586. page++;
  2587. context_append(&ctx->context, d, 1, 0);
  2588. }
  2589. return 0;
  2590. }
  2591. static int ohci_queue_iso(struct fw_iso_context *base,
  2592. struct fw_iso_packet *packet,
  2593. struct fw_iso_buffer *buffer,
  2594. unsigned long payload)
  2595. {
  2596. struct iso_context *ctx = container_of(base, struct iso_context, base);
  2597. unsigned long flags;
  2598. int ret = -ENOSYS;
  2599. spin_lock_irqsave(&ctx->context.ohci->lock, flags);
  2600. switch (base->type) {
  2601. case FW_ISO_CONTEXT_TRANSMIT:
  2602. ret = queue_iso_transmit(ctx, packet, buffer, payload);
  2603. break;
  2604. case FW_ISO_CONTEXT_RECEIVE:
  2605. ret = queue_iso_packet_per_buffer(ctx, packet, buffer, payload);
  2606. break;
  2607. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  2608. ret = queue_iso_buffer_fill(ctx, packet, buffer, payload);
  2609. break;
  2610. }
  2611. spin_unlock_irqrestore(&ctx->context.ohci->lock, flags);
  2612. return ret;
  2613. }
  2614. static void ohci_flush_queue_iso(struct fw_iso_context *base)
  2615. {
  2616. struct context *ctx =
  2617. &container_of(base, struct iso_context, base)->context;
  2618. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  2619. flush_writes(ctx->ohci);
  2620. }
  2621. static const struct fw_card_driver ohci_driver = {
  2622. .enable = ohci_enable,
  2623. .read_phy_reg = ohci_read_phy_reg,
  2624. .update_phy_reg = ohci_update_phy_reg,
  2625. .set_config_rom = ohci_set_config_rom,
  2626. .send_request = ohci_send_request,
  2627. .send_response = ohci_send_response,
  2628. .cancel_packet = ohci_cancel_packet,
  2629. .enable_phys_dma = ohci_enable_phys_dma,
  2630. .read_csr = ohci_read_csr,
  2631. .write_csr = ohci_write_csr,
  2632. .allocate_iso_context = ohci_allocate_iso_context,
  2633. .free_iso_context = ohci_free_iso_context,
  2634. .set_iso_channels = ohci_set_iso_channels,
  2635. .queue_iso = ohci_queue_iso,
  2636. .flush_queue_iso = ohci_flush_queue_iso,
  2637. .start_iso = ohci_start_iso,
  2638. .stop_iso = ohci_stop_iso,
  2639. };
  2640. #ifdef CONFIG_PPC_PMAC
  2641. static void pmac_ohci_on(struct pci_dev *dev)
  2642. {
  2643. if (machine_is(powermac)) {
  2644. struct device_node *ofn = pci_device_to_OF_node(dev);
  2645. if (ofn) {
  2646. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
  2647. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
  2648. }
  2649. }
  2650. }
  2651. static void pmac_ohci_off(struct pci_dev *dev)
  2652. {
  2653. if (machine_is(powermac)) {
  2654. struct device_node *ofn = pci_device_to_OF_node(dev);
  2655. if (ofn) {
  2656. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
  2657. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
  2658. }
  2659. }
  2660. }
  2661. #else
  2662. static inline void pmac_ohci_on(struct pci_dev *dev) {}
  2663. static inline void pmac_ohci_off(struct pci_dev *dev) {}
  2664. #endif /* CONFIG_PPC_PMAC */
  2665. static int __devinit pci_probe(struct pci_dev *dev,
  2666. const struct pci_device_id *ent)
  2667. {
  2668. struct fw_ohci *ohci;
  2669. u32 bus_options, max_receive, link_speed, version;
  2670. u64 guid;
  2671. int i, err;
  2672. size_t size;
  2673. if (dev->vendor == PCI_VENDOR_ID_PINNACLE_SYSTEMS) {
  2674. dev_err(&dev->dev, "Pinnacle MovieBoard is not yet supported\n");
  2675. return -ENOSYS;
  2676. }
  2677. ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
  2678. if (ohci == NULL) {
  2679. err = -ENOMEM;
  2680. goto fail;
  2681. }
  2682. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  2683. pmac_ohci_on(dev);
  2684. err = pci_enable_device(dev);
  2685. if (err) {
  2686. fw_error("Failed to enable OHCI hardware\n");
  2687. goto fail_free;
  2688. }
  2689. pci_set_master(dev);
  2690. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  2691. pci_set_drvdata(dev, ohci);
  2692. spin_lock_init(&ohci->lock);
  2693. mutex_init(&ohci->phy_reg_mutex);
  2694. tasklet_init(&ohci->bus_reset_tasklet,
  2695. bus_reset_tasklet, (unsigned long)ohci);
  2696. err = pci_request_region(dev, 0, ohci_driver_name);
  2697. if (err) {
  2698. fw_error("MMIO resource unavailable\n");
  2699. goto fail_disable;
  2700. }
  2701. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  2702. if (ohci->registers == NULL) {
  2703. fw_error("Failed to remap registers\n");
  2704. err = -ENXIO;
  2705. goto fail_iomem;
  2706. }
  2707. for (i = 0; i < ARRAY_SIZE(ohci_quirks); i++)
  2708. if ((ohci_quirks[i].vendor == dev->vendor) &&
  2709. (ohci_quirks[i].device == (unsigned short)PCI_ANY_ID ||
  2710. ohci_quirks[i].device == dev->device) &&
  2711. (ohci_quirks[i].revision == (unsigned short)PCI_ANY_ID ||
  2712. ohci_quirks[i].revision >= dev->revision)) {
  2713. ohci->quirks = ohci_quirks[i].flags;
  2714. break;
  2715. }
  2716. if (param_quirks)
  2717. ohci->quirks = param_quirks;
  2718. /*
  2719. * Because dma_alloc_coherent() allocates at least one page,
  2720. * we save space by using a common buffer for the AR request/
  2721. * response descriptors and the self IDs buffer.
  2722. */
  2723. BUILD_BUG_ON(AR_BUFFERS * sizeof(struct descriptor) > PAGE_SIZE/4);
  2724. BUILD_BUG_ON(SELF_ID_BUF_SIZE > PAGE_SIZE/2);
  2725. ohci->misc_buffer = dma_alloc_coherent(ohci->card.device,
  2726. PAGE_SIZE,
  2727. &ohci->misc_buffer_bus,
  2728. GFP_KERNEL);
  2729. if (!ohci->misc_buffer) {
  2730. err = -ENOMEM;
  2731. goto fail_iounmap;
  2732. }
  2733. err = ar_context_init(&ohci->ar_request_ctx, ohci, 0,
  2734. OHCI1394_AsReqRcvContextControlSet);
  2735. if (err < 0)
  2736. goto fail_misc_buf;
  2737. err = ar_context_init(&ohci->ar_response_ctx, ohci, PAGE_SIZE/4,
  2738. OHCI1394_AsRspRcvContextControlSet);
  2739. if (err < 0)
  2740. goto fail_arreq_ctx;
  2741. err = context_init(&ohci->at_request_ctx, ohci,
  2742. OHCI1394_AsReqTrContextControlSet, handle_at_packet);
  2743. if (err < 0)
  2744. goto fail_arrsp_ctx;
  2745. err = context_init(&ohci->at_response_ctx, ohci,
  2746. OHCI1394_AsRspTrContextControlSet, handle_at_packet);
  2747. if (err < 0)
  2748. goto fail_atreq_ctx;
  2749. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  2750. ohci->ir_context_channels = ~0ULL;
  2751. ohci->ir_context_support = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  2752. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  2753. ohci->ir_context_mask = ohci->ir_context_support;
  2754. ohci->n_ir = hweight32(ohci->ir_context_mask);
  2755. size = sizeof(struct iso_context) * ohci->n_ir;
  2756. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  2757. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  2758. ohci->it_context_support = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  2759. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  2760. ohci->it_context_mask = ohci->it_context_support;
  2761. ohci->n_it = hweight32(ohci->it_context_mask);
  2762. size = sizeof(struct iso_context) * ohci->n_it;
  2763. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  2764. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  2765. err = -ENOMEM;
  2766. goto fail_contexts;
  2767. }
  2768. ohci->self_id_cpu = ohci->misc_buffer + PAGE_SIZE/2;
  2769. ohci->self_id_bus = ohci->misc_buffer_bus + PAGE_SIZE/2;
  2770. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  2771. max_receive = (bus_options >> 12) & 0xf;
  2772. link_speed = bus_options & 0x7;
  2773. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  2774. reg_read(ohci, OHCI1394_GUIDLo);
  2775. err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  2776. if (err)
  2777. goto fail_contexts;
  2778. version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  2779. fw_notify("Added fw-ohci device %s, OHCI v%x.%x, "
  2780. "%d IR + %d IT contexts, quirks 0x%x\n",
  2781. dev_name(&dev->dev), version >> 16, version & 0xff,
  2782. ohci->n_ir, ohci->n_it, ohci->quirks);
  2783. return 0;
  2784. fail_contexts:
  2785. kfree(ohci->ir_context_list);
  2786. kfree(ohci->it_context_list);
  2787. context_release(&ohci->at_response_ctx);
  2788. fail_atreq_ctx:
  2789. context_release(&ohci->at_request_ctx);
  2790. fail_arrsp_ctx:
  2791. ar_context_release(&ohci->ar_response_ctx);
  2792. fail_arreq_ctx:
  2793. ar_context_release(&ohci->ar_request_ctx);
  2794. fail_misc_buf:
  2795. dma_free_coherent(ohci->card.device, PAGE_SIZE,
  2796. ohci->misc_buffer, ohci->misc_buffer_bus);
  2797. fail_iounmap:
  2798. pci_iounmap(dev, ohci->registers);
  2799. fail_iomem:
  2800. pci_release_region(dev, 0);
  2801. fail_disable:
  2802. pci_disable_device(dev);
  2803. fail_free:
  2804. kfree(ohci);
  2805. pmac_ohci_off(dev);
  2806. fail:
  2807. if (err == -ENOMEM)
  2808. fw_error("Out of memory\n");
  2809. return err;
  2810. }
  2811. static void pci_remove(struct pci_dev *dev)
  2812. {
  2813. struct fw_ohci *ohci;
  2814. ohci = pci_get_drvdata(dev);
  2815. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  2816. flush_writes(ohci);
  2817. fw_core_remove_card(&ohci->card);
  2818. /*
  2819. * FIXME: Fail all pending packets here, now that the upper
  2820. * layers can't queue any more.
  2821. */
  2822. software_reset(ohci);
  2823. free_irq(dev->irq, ohci);
  2824. if (ohci->next_config_rom && ohci->next_config_rom != ohci->config_rom)
  2825. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  2826. ohci->next_config_rom, ohci->next_config_rom_bus);
  2827. if (ohci->config_rom)
  2828. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  2829. ohci->config_rom, ohci->config_rom_bus);
  2830. ar_context_release(&ohci->ar_request_ctx);
  2831. ar_context_release(&ohci->ar_response_ctx);
  2832. dma_free_coherent(ohci->card.device, PAGE_SIZE,
  2833. ohci->misc_buffer, ohci->misc_buffer_bus);
  2834. context_release(&ohci->at_request_ctx);
  2835. context_release(&ohci->at_response_ctx);
  2836. kfree(ohci->it_context_list);
  2837. kfree(ohci->ir_context_list);
  2838. pci_disable_msi(dev);
  2839. pci_iounmap(dev, ohci->registers);
  2840. pci_release_region(dev, 0);
  2841. pci_disable_device(dev);
  2842. kfree(ohci);
  2843. pmac_ohci_off(dev);
  2844. fw_notify("Removed fw-ohci device.\n");
  2845. }
  2846. #ifdef CONFIG_PM
  2847. static int pci_suspend(struct pci_dev *dev, pm_message_t state)
  2848. {
  2849. struct fw_ohci *ohci = pci_get_drvdata(dev);
  2850. int err;
  2851. software_reset(ohci);
  2852. free_irq(dev->irq, ohci);
  2853. pci_disable_msi(dev);
  2854. err = pci_save_state(dev);
  2855. if (err) {
  2856. fw_error("pci_save_state failed\n");
  2857. return err;
  2858. }
  2859. err = pci_set_power_state(dev, pci_choose_state(dev, state));
  2860. if (err)
  2861. fw_error("pci_set_power_state failed with %d\n", err);
  2862. pmac_ohci_off(dev);
  2863. return 0;
  2864. }
  2865. static int pci_resume(struct pci_dev *dev)
  2866. {
  2867. struct fw_ohci *ohci = pci_get_drvdata(dev);
  2868. int err;
  2869. pmac_ohci_on(dev);
  2870. pci_set_power_state(dev, PCI_D0);
  2871. pci_restore_state(dev);
  2872. err = pci_enable_device(dev);
  2873. if (err) {
  2874. fw_error("pci_enable_device failed\n");
  2875. return err;
  2876. }
  2877. /* Some systems don't setup GUID register on resume from ram */
  2878. if (!reg_read(ohci, OHCI1394_GUIDLo) &&
  2879. !reg_read(ohci, OHCI1394_GUIDHi)) {
  2880. reg_write(ohci, OHCI1394_GUIDLo, (u32)ohci->card.guid);
  2881. reg_write(ohci, OHCI1394_GUIDHi, (u32)(ohci->card.guid >> 32));
  2882. }
  2883. err = ohci_enable(&ohci->card, NULL, 0);
  2884. if (err)
  2885. return err;
  2886. ohci_resume_iso_dma(ohci);
  2887. return 0;
  2888. }
  2889. #endif
  2890. static const struct pci_device_id pci_table[] = {
  2891. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  2892. { }
  2893. };
  2894. MODULE_DEVICE_TABLE(pci, pci_table);
  2895. static struct pci_driver fw_ohci_pci_driver = {
  2896. .name = ohci_driver_name,
  2897. .id_table = pci_table,
  2898. .probe = pci_probe,
  2899. .remove = pci_remove,
  2900. #ifdef CONFIG_PM
  2901. .resume = pci_resume,
  2902. .suspend = pci_suspend,
  2903. #endif
  2904. };
  2905. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  2906. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  2907. MODULE_LICENSE("GPL");
  2908. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  2909. #ifndef CONFIG_IEEE1394_OHCI1394_MODULE
  2910. MODULE_ALIAS("ohci1394");
  2911. #endif
  2912. static int __init fw_ohci_init(void)
  2913. {
  2914. return pci_register_driver(&fw_ohci_pci_driver);
  2915. }
  2916. static void __exit fw_ohci_cleanup(void)
  2917. {
  2918. pci_unregister_driver(&fw_ohci_pci_driver);
  2919. }
  2920. module_init(fw_ohci_init);
  2921. module_exit(fw_ohci_cleanup);