ohci.c 95 KB

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