fw-ohci.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
  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/compiler.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/gfp.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/pci.h>
  30. #include <linux/spinlock.h>
  31. #include <asm/page.h>
  32. #include <asm/system.h>
  33. #ifdef CONFIG_PPC_PMAC
  34. #include <asm/pmac_feature.h>
  35. #endif
  36. #include "fw-ohci.h"
  37. #include "fw-transaction.h"
  38. #define DESCRIPTOR_OUTPUT_MORE 0
  39. #define DESCRIPTOR_OUTPUT_LAST (1 << 12)
  40. #define DESCRIPTOR_INPUT_MORE (2 << 12)
  41. #define DESCRIPTOR_INPUT_LAST (3 << 12)
  42. #define DESCRIPTOR_STATUS (1 << 11)
  43. #define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
  44. #define DESCRIPTOR_PING (1 << 7)
  45. #define DESCRIPTOR_YY (1 << 6)
  46. #define DESCRIPTOR_NO_IRQ (0 << 4)
  47. #define DESCRIPTOR_IRQ_ERROR (1 << 4)
  48. #define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
  49. #define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
  50. #define DESCRIPTOR_WAIT (3 << 0)
  51. struct descriptor {
  52. __le16 req_count;
  53. __le16 control;
  54. __le32 data_address;
  55. __le32 branch_address;
  56. __le16 res_count;
  57. __le16 transfer_status;
  58. } __attribute__((aligned(16)));
  59. struct db_descriptor {
  60. __le16 first_size;
  61. __le16 control;
  62. __le16 second_req_count;
  63. __le16 first_req_count;
  64. __le32 branch_address;
  65. __le16 second_res_count;
  66. __le16 first_res_count;
  67. __le32 reserved0;
  68. __le32 first_buffer;
  69. __le32 second_buffer;
  70. __le32 reserved1;
  71. } __attribute__((aligned(16)));
  72. #define CONTROL_SET(regs) (regs)
  73. #define CONTROL_CLEAR(regs) ((regs) + 4)
  74. #define COMMAND_PTR(regs) ((regs) + 12)
  75. #define CONTEXT_MATCH(regs) ((regs) + 16)
  76. struct ar_buffer {
  77. struct descriptor descriptor;
  78. struct ar_buffer *next;
  79. __le32 data[0];
  80. };
  81. struct ar_context {
  82. struct fw_ohci *ohci;
  83. struct ar_buffer *current_buffer;
  84. struct ar_buffer *last_buffer;
  85. void *pointer;
  86. u32 regs;
  87. struct tasklet_struct tasklet;
  88. };
  89. struct context;
  90. typedef int (*descriptor_callback_t)(struct context *ctx,
  91. struct descriptor *d,
  92. struct descriptor *last);
  93. /*
  94. * A buffer that contains a block of DMA-able coherent memory used for
  95. * storing a portion of a DMA descriptor program.
  96. */
  97. struct descriptor_buffer {
  98. struct list_head list;
  99. dma_addr_t buffer_bus;
  100. size_t buffer_size;
  101. size_t used;
  102. struct descriptor buffer[0];
  103. };
  104. struct context {
  105. struct fw_ohci *ohci;
  106. u32 regs;
  107. int total_allocation;
  108. /*
  109. * List of page-sized buffers for storing DMA descriptors.
  110. * Head of list contains buffers in use and tail of list contains
  111. * free buffers.
  112. */
  113. struct list_head buffer_list;
  114. /*
  115. * Pointer to a buffer inside buffer_list that contains the tail
  116. * end of the current DMA program.
  117. */
  118. struct descriptor_buffer *buffer_tail;
  119. /*
  120. * The descriptor containing the branch address of the first
  121. * descriptor that has not yet been filled by the device.
  122. */
  123. struct descriptor *last;
  124. /*
  125. * The last descriptor in the DMA program. It contains the branch
  126. * address that must be updated upon appending a new descriptor.
  127. */
  128. struct descriptor *prev;
  129. descriptor_callback_t callback;
  130. struct tasklet_struct tasklet;
  131. };
  132. #define IT_HEADER_SY(v) ((v) << 0)
  133. #define IT_HEADER_TCODE(v) ((v) << 4)
  134. #define IT_HEADER_CHANNEL(v) ((v) << 8)
  135. #define IT_HEADER_TAG(v) ((v) << 14)
  136. #define IT_HEADER_SPEED(v) ((v) << 16)
  137. #define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
  138. struct iso_context {
  139. struct fw_iso_context base;
  140. struct context context;
  141. int excess_bytes;
  142. void *header;
  143. size_t header_length;
  144. };
  145. #define CONFIG_ROM_SIZE 1024
  146. struct fw_ohci {
  147. struct fw_card card;
  148. u32 version;
  149. __iomem char *registers;
  150. dma_addr_t self_id_bus;
  151. __le32 *self_id_cpu;
  152. struct tasklet_struct bus_reset_tasklet;
  153. int node_id;
  154. int generation;
  155. int request_generation;
  156. u32 bus_seconds;
  157. bool old_uninorth;
  158. /*
  159. * Spinlock for accessing fw_ohci data. Never call out of
  160. * this driver with this lock held.
  161. */
  162. spinlock_t lock;
  163. u32 self_id_buffer[512];
  164. /* Config rom buffers */
  165. __be32 *config_rom;
  166. dma_addr_t config_rom_bus;
  167. __be32 *next_config_rom;
  168. dma_addr_t next_config_rom_bus;
  169. u32 next_header;
  170. struct ar_context ar_request_ctx;
  171. struct ar_context ar_response_ctx;
  172. struct context at_request_ctx;
  173. struct context at_response_ctx;
  174. u32 it_context_mask;
  175. struct iso_context *it_context_list;
  176. u32 ir_context_mask;
  177. struct iso_context *ir_context_list;
  178. };
  179. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  180. {
  181. return container_of(card, struct fw_ohci, card);
  182. }
  183. #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  184. #define IR_CONTEXT_BUFFER_FILL 0x80000000
  185. #define IR_CONTEXT_ISOCH_HEADER 0x40000000
  186. #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
  187. #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
  188. #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
  189. #define CONTEXT_RUN 0x8000
  190. #define CONTEXT_WAKE 0x1000
  191. #define CONTEXT_DEAD 0x0800
  192. #define CONTEXT_ACTIVE 0x0400
  193. #define OHCI1394_MAX_AT_REQ_RETRIES 0x2
  194. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  195. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  196. #define FW_OHCI_MAJOR 240
  197. #define OHCI1394_REGISTER_SIZE 0x800
  198. #define OHCI_LOOP_COUNT 500
  199. #define OHCI1394_PCI_HCI_Control 0x40
  200. #define SELF_ID_BUF_SIZE 0x800
  201. #define OHCI_TCODE_PHY_PACKET 0x0e
  202. #define OHCI_VERSION_1_1 0x010010
  203. static char ohci_driver_name[] = KBUILD_MODNAME;
  204. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  205. {
  206. writel(data, ohci->registers + offset);
  207. }
  208. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  209. {
  210. return readl(ohci->registers + offset);
  211. }
  212. static inline void flush_writes(const struct fw_ohci *ohci)
  213. {
  214. /* Do a dummy read to flush writes. */
  215. reg_read(ohci, OHCI1394_Version);
  216. }
  217. static int
  218. ohci_update_phy_reg(struct fw_card *card, int addr,
  219. int clear_bits, int set_bits)
  220. {
  221. struct fw_ohci *ohci = fw_ohci(card);
  222. u32 val, old;
  223. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  224. flush_writes(ohci);
  225. msleep(2);
  226. val = reg_read(ohci, OHCI1394_PhyControl);
  227. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  228. fw_error("failed to set phy reg bits.\n");
  229. return -EBUSY;
  230. }
  231. old = OHCI1394_PhyControl_ReadData(val);
  232. old = (old & ~clear_bits) | set_bits;
  233. reg_write(ohci, OHCI1394_PhyControl,
  234. OHCI1394_PhyControl_Write(addr, old));
  235. return 0;
  236. }
  237. static int ar_context_add_page(struct ar_context *ctx)
  238. {
  239. struct device *dev = ctx->ohci->card.device;
  240. struct ar_buffer *ab;
  241. dma_addr_t ab_bus;
  242. size_t offset;
  243. ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
  244. if (ab == NULL)
  245. return -ENOMEM;
  246. ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
  247. if (dma_mapping_error(ab_bus)) {
  248. free_page((unsigned long) ab);
  249. return -ENOMEM;
  250. }
  251. memset(&ab->descriptor, 0, sizeof(ab->descriptor));
  252. ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  253. DESCRIPTOR_STATUS |
  254. DESCRIPTOR_BRANCH_ALWAYS);
  255. offset = offsetof(struct ar_buffer, data);
  256. ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
  257. ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
  258. ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
  259. ab->descriptor.branch_address = 0;
  260. dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
  261. ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
  262. ctx->last_buffer->next = ab;
  263. ctx->last_buffer = ab;
  264. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  265. flush_writes(ctx->ohci);
  266. return 0;
  267. }
  268. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  269. #define cond_le32_to_cpu(v) \
  270. (ohci->old_uninorth ? (__force __u32)(v) : le32_to_cpu(v))
  271. #else
  272. #define cond_le32_to_cpu(v) le32_to_cpu(v)
  273. #endif
  274. static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
  275. {
  276. struct fw_ohci *ohci = ctx->ohci;
  277. struct fw_packet p;
  278. u32 status, length, tcode;
  279. p.header[0] = cond_le32_to_cpu(buffer[0]);
  280. p.header[1] = cond_le32_to_cpu(buffer[1]);
  281. p.header[2] = cond_le32_to_cpu(buffer[2]);
  282. tcode = (p.header[0] >> 4) & 0x0f;
  283. switch (tcode) {
  284. case TCODE_WRITE_QUADLET_REQUEST:
  285. case TCODE_READ_QUADLET_RESPONSE:
  286. p.header[3] = (__force __u32) buffer[3];
  287. p.header_length = 16;
  288. p.payload_length = 0;
  289. break;
  290. case TCODE_READ_BLOCK_REQUEST :
  291. p.header[3] = cond_le32_to_cpu(buffer[3]);
  292. p.header_length = 16;
  293. p.payload_length = 0;
  294. break;
  295. case TCODE_WRITE_BLOCK_REQUEST:
  296. case TCODE_READ_BLOCK_RESPONSE:
  297. case TCODE_LOCK_REQUEST:
  298. case TCODE_LOCK_RESPONSE:
  299. p.header[3] = cond_le32_to_cpu(buffer[3]);
  300. p.header_length = 16;
  301. p.payload_length = p.header[3] >> 16;
  302. break;
  303. case TCODE_WRITE_RESPONSE:
  304. case TCODE_READ_QUADLET_REQUEST:
  305. case OHCI_TCODE_PHY_PACKET:
  306. p.header_length = 12;
  307. p.payload_length = 0;
  308. break;
  309. }
  310. p.payload = (void *) buffer + p.header_length;
  311. /* FIXME: What to do about evt_* errors? */
  312. length = (p.header_length + p.payload_length + 3) / 4;
  313. status = cond_le32_to_cpu(buffer[length]);
  314. p.ack = ((status >> 16) & 0x1f) - 16;
  315. p.speed = (status >> 21) & 0x7;
  316. p.timestamp = status & 0xffff;
  317. p.generation = ohci->request_generation;
  318. /*
  319. * The OHCI bus reset handler synthesizes a phy packet with
  320. * the new generation number when a bus reset happens (see
  321. * section 8.4.2.3). This helps us determine when a request
  322. * was received and make sure we send the response in the same
  323. * generation. We only need this for requests; for responses
  324. * we use the unique tlabel for finding the matching
  325. * request.
  326. */
  327. if (p.ack + 16 == 0x09)
  328. ohci->request_generation = (p.header[2] >> 16) & 0xff;
  329. else if (ctx == &ohci->ar_request_ctx)
  330. fw_core_handle_request(&ohci->card, &p);
  331. else
  332. fw_core_handle_response(&ohci->card, &p);
  333. return buffer + length + 1;
  334. }
  335. static void ar_context_tasklet(unsigned long data)
  336. {
  337. struct ar_context *ctx = (struct ar_context *)data;
  338. struct fw_ohci *ohci = ctx->ohci;
  339. struct ar_buffer *ab;
  340. struct descriptor *d;
  341. void *buffer, *end;
  342. ab = ctx->current_buffer;
  343. d = &ab->descriptor;
  344. if (d->res_count == 0) {
  345. size_t size, rest, offset;
  346. /*
  347. * This descriptor is finished and we may have a
  348. * packet split across this and the next buffer. We
  349. * reuse the page for reassembling the split packet.
  350. */
  351. offset = offsetof(struct ar_buffer, data);
  352. dma_unmap_single(ohci->card.device,
  353. le32_to_cpu(ab->descriptor.data_address) - offset,
  354. PAGE_SIZE, DMA_BIDIRECTIONAL);
  355. buffer = ab;
  356. ab = ab->next;
  357. d = &ab->descriptor;
  358. size = buffer + PAGE_SIZE - ctx->pointer;
  359. rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
  360. memmove(buffer, ctx->pointer, size);
  361. memcpy(buffer + size, ab->data, rest);
  362. ctx->current_buffer = ab;
  363. ctx->pointer = (void *) ab->data + rest;
  364. end = buffer + size + rest;
  365. while (buffer < end)
  366. buffer = handle_ar_packet(ctx, buffer);
  367. free_page((unsigned long)buffer);
  368. ar_context_add_page(ctx);
  369. } else {
  370. buffer = ctx->pointer;
  371. ctx->pointer = end =
  372. (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
  373. while (buffer < end)
  374. buffer = handle_ar_packet(ctx, buffer);
  375. }
  376. }
  377. static int
  378. ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
  379. {
  380. struct ar_buffer ab;
  381. ctx->regs = regs;
  382. ctx->ohci = ohci;
  383. ctx->last_buffer = &ab;
  384. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  385. ar_context_add_page(ctx);
  386. ar_context_add_page(ctx);
  387. ctx->current_buffer = ab.next;
  388. ctx->pointer = ctx->current_buffer->data;
  389. return 0;
  390. }
  391. static void ar_context_run(struct ar_context *ctx)
  392. {
  393. struct ar_buffer *ab = ctx->current_buffer;
  394. dma_addr_t ab_bus;
  395. size_t offset;
  396. offset = offsetof(struct ar_buffer, data);
  397. ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
  398. reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
  399. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
  400. flush_writes(ctx->ohci);
  401. }
  402. static struct descriptor *
  403. find_branch_descriptor(struct descriptor *d, int z)
  404. {
  405. int b, key;
  406. b = (le16_to_cpu(d->control) & DESCRIPTOR_BRANCH_ALWAYS) >> 2;
  407. key = (le16_to_cpu(d->control) & DESCRIPTOR_KEY_IMMEDIATE) >> 8;
  408. /* figure out which descriptor the branch address goes in */
  409. if (z == 2 && (b == 3 || key == 2))
  410. return d;
  411. else
  412. return d + z - 1;
  413. }
  414. static void context_tasklet(unsigned long data)
  415. {
  416. struct context *ctx = (struct context *) data;
  417. struct descriptor *d, *last;
  418. u32 address;
  419. int z;
  420. struct descriptor_buffer *desc;
  421. desc = list_entry(ctx->buffer_list.next,
  422. struct descriptor_buffer, list);
  423. last = ctx->last;
  424. while (last->branch_address != 0) {
  425. struct descriptor_buffer *old_desc = desc;
  426. address = le32_to_cpu(last->branch_address);
  427. z = address & 0xf;
  428. address &= ~0xf;
  429. /* If the branch address points to a buffer outside of the
  430. * current buffer, advance to the next buffer. */
  431. if (address < desc->buffer_bus ||
  432. address >= desc->buffer_bus + desc->used)
  433. desc = list_entry(desc->list.next,
  434. struct descriptor_buffer, list);
  435. d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
  436. last = find_branch_descriptor(d, z);
  437. if (!ctx->callback(ctx, d, last))
  438. break;
  439. if (old_desc != desc) {
  440. /* If we've advanced to the next buffer, move the
  441. * previous buffer to the free list. */
  442. unsigned long flags;
  443. old_desc->used = 0;
  444. spin_lock_irqsave(&ctx->ohci->lock, flags);
  445. list_move_tail(&old_desc->list, &ctx->buffer_list);
  446. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  447. }
  448. ctx->last = last;
  449. }
  450. }
  451. /*
  452. * Allocate a new buffer and add it to the list of free buffers for this
  453. * context. Must be called with ohci->lock held.
  454. */
  455. static int
  456. context_add_buffer(struct context *ctx)
  457. {
  458. struct descriptor_buffer *desc;
  459. dma_addr_t bus_addr;
  460. int offset;
  461. /*
  462. * 16MB of descriptors should be far more than enough for any DMA
  463. * program. This will catch run-away userspace or DoS attacks.
  464. */
  465. if (ctx->total_allocation >= 16*1024*1024)
  466. return -ENOMEM;
  467. desc = dma_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE,
  468. &bus_addr, GFP_ATOMIC);
  469. if (!desc)
  470. return -ENOMEM;
  471. offset = (void *)&desc->buffer - (void *)desc;
  472. desc->buffer_size = PAGE_SIZE - offset;
  473. desc->buffer_bus = bus_addr + offset;
  474. desc->used = 0;
  475. list_add_tail(&desc->list, &ctx->buffer_list);
  476. ctx->total_allocation += PAGE_SIZE;
  477. return 0;
  478. }
  479. static int
  480. context_init(struct context *ctx, struct fw_ohci *ohci,
  481. u32 regs, descriptor_callback_t callback)
  482. {
  483. ctx->ohci = ohci;
  484. ctx->regs = regs;
  485. ctx->total_allocation = 0;
  486. INIT_LIST_HEAD(&ctx->buffer_list);
  487. if (context_add_buffer(ctx) < 0)
  488. return -ENOMEM;
  489. ctx->buffer_tail = list_entry(ctx->buffer_list.next,
  490. struct descriptor_buffer, list);
  491. tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
  492. ctx->callback = callback;
  493. /*
  494. * We put a dummy descriptor in the buffer that has a NULL
  495. * branch address and looks like it's been sent. That way we
  496. * have a descriptor to append DMA programs to.
  497. */
  498. memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
  499. ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
  500. ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
  501. ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
  502. ctx->last = ctx->buffer_tail->buffer;
  503. ctx->prev = ctx->buffer_tail->buffer;
  504. return 0;
  505. }
  506. static void
  507. context_release(struct context *ctx)
  508. {
  509. struct fw_card *card = &ctx->ohci->card;
  510. struct descriptor_buffer *desc, *tmp;
  511. list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list)
  512. dma_free_coherent(card->device, PAGE_SIZE, desc,
  513. desc->buffer_bus -
  514. ((void *)&desc->buffer - (void *)desc));
  515. }
  516. /* Must be called with ohci->lock held */
  517. static struct descriptor *
  518. context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
  519. {
  520. struct descriptor *d = NULL;
  521. struct descriptor_buffer *desc = ctx->buffer_tail;
  522. if (z * sizeof(*d) > desc->buffer_size)
  523. return NULL;
  524. if (z * sizeof(*d) > desc->buffer_size - desc->used) {
  525. /* No room for the descriptor in this buffer, so advance to the
  526. * next one. */
  527. if (desc->list.next == &ctx->buffer_list) {
  528. /* If there is no free buffer next in the list,
  529. * allocate one. */
  530. if (context_add_buffer(ctx) < 0)
  531. return NULL;
  532. }
  533. desc = list_entry(desc->list.next,
  534. struct descriptor_buffer, list);
  535. ctx->buffer_tail = desc;
  536. }
  537. d = desc->buffer + desc->used / sizeof(*d);
  538. memset(d, 0, z * sizeof(*d));
  539. *d_bus = desc->buffer_bus + desc->used;
  540. return d;
  541. }
  542. static void context_run(struct context *ctx, u32 extra)
  543. {
  544. struct fw_ohci *ohci = ctx->ohci;
  545. reg_write(ohci, COMMAND_PTR(ctx->regs),
  546. le32_to_cpu(ctx->last->branch_address));
  547. reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
  548. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
  549. flush_writes(ohci);
  550. }
  551. static void context_append(struct context *ctx,
  552. struct descriptor *d, int z, int extra)
  553. {
  554. dma_addr_t d_bus;
  555. struct descriptor_buffer *desc = ctx->buffer_tail;
  556. d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
  557. desc->used += (z + extra) * sizeof(*d);
  558. ctx->prev->branch_address = cpu_to_le32(d_bus | z);
  559. ctx->prev = find_branch_descriptor(d, z);
  560. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  561. flush_writes(ctx->ohci);
  562. }
  563. static void context_stop(struct context *ctx)
  564. {
  565. u32 reg;
  566. int i;
  567. reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  568. flush_writes(ctx->ohci);
  569. for (i = 0; i < 10; i++) {
  570. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  571. if ((reg & CONTEXT_ACTIVE) == 0)
  572. break;
  573. fw_notify("context_stop: still active (0x%08x)\n", reg);
  574. mdelay(1);
  575. }
  576. }
  577. struct driver_data {
  578. struct fw_packet *packet;
  579. };
  580. /*
  581. * This function apppends a packet to the DMA queue for transmission.
  582. * Must always be called with the ochi->lock held to ensure proper
  583. * generation handling and locking around packet queue manipulation.
  584. */
  585. static int
  586. at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
  587. {
  588. struct fw_ohci *ohci = ctx->ohci;
  589. dma_addr_t d_bus, uninitialized_var(payload_bus);
  590. struct driver_data *driver_data;
  591. struct descriptor *d, *last;
  592. __le32 *header;
  593. int z, tcode;
  594. u32 reg;
  595. d = context_get_descriptors(ctx, 4, &d_bus);
  596. if (d == NULL) {
  597. packet->ack = RCODE_SEND_ERROR;
  598. return -1;
  599. }
  600. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  601. d[0].res_count = cpu_to_le16(packet->timestamp);
  602. /*
  603. * The DMA format for asyncronous link packets is different
  604. * from the IEEE1394 layout, so shift the fields around
  605. * accordingly. If header_length is 8, it's a PHY packet, to
  606. * which we need to prepend an extra quadlet.
  607. */
  608. header = (__le32 *) &d[1];
  609. if (packet->header_length > 8) {
  610. header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  611. (packet->speed << 16));
  612. header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  613. (packet->header[0] & 0xffff0000));
  614. header[2] = cpu_to_le32(packet->header[2]);
  615. tcode = (packet->header[0] >> 4) & 0x0f;
  616. if (TCODE_IS_BLOCK_PACKET(tcode))
  617. header[3] = cpu_to_le32(packet->header[3]);
  618. else
  619. header[3] = (__force __le32) packet->header[3];
  620. d[0].req_count = cpu_to_le16(packet->header_length);
  621. } else {
  622. header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
  623. (packet->speed << 16));
  624. header[1] = cpu_to_le32(packet->header[0]);
  625. header[2] = cpu_to_le32(packet->header[1]);
  626. d[0].req_count = cpu_to_le16(12);
  627. }
  628. driver_data = (struct driver_data *) &d[3];
  629. driver_data->packet = packet;
  630. packet->driver_data = driver_data;
  631. if (packet->payload_length > 0) {
  632. payload_bus =
  633. dma_map_single(ohci->card.device, packet->payload,
  634. packet->payload_length, DMA_TO_DEVICE);
  635. if (dma_mapping_error(payload_bus)) {
  636. packet->ack = RCODE_SEND_ERROR;
  637. return -1;
  638. }
  639. d[2].req_count = cpu_to_le16(packet->payload_length);
  640. d[2].data_address = cpu_to_le32(payload_bus);
  641. last = &d[2];
  642. z = 3;
  643. } else {
  644. last = &d[0];
  645. z = 2;
  646. }
  647. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  648. DESCRIPTOR_IRQ_ALWAYS |
  649. DESCRIPTOR_BRANCH_ALWAYS);
  650. /* FIXME: Document how the locking works. */
  651. if (ohci->generation != packet->generation) {
  652. if (packet->payload_length > 0)
  653. dma_unmap_single(ohci->card.device, payload_bus,
  654. packet->payload_length, DMA_TO_DEVICE);
  655. packet->ack = RCODE_GENERATION;
  656. return -1;
  657. }
  658. context_append(ctx, d, z, 4 - z);
  659. /* If the context isn't already running, start it up. */
  660. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  661. if ((reg & CONTEXT_RUN) == 0)
  662. context_run(ctx, 0);
  663. return 0;
  664. }
  665. static int handle_at_packet(struct context *context,
  666. struct descriptor *d,
  667. struct descriptor *last)
  668. {
  669. struct driver_data *driver_data;
  670. struct fw_packet *packet;
  671. struct fw_ohci *ohci = context->ohci;
  672. dma_addr_t payload_bus;
  673. int evt;
  674. if (last->transfer_status == 0)
  675. /* This descriptor isn't done yet, stop iteration. */
  676. return 0;
  677. driver_data = (struct driver_data *) &d[3];
  678. packet = driver_data->packet;
  679. if (packet == NULL)
  680. /* This packet was cancelled, just continue. */
  681. return 1;
  682. payload_bus = le32_to_cpu(last->data_address);
  683. if (payload_bus != 0)
  684. dma_unmap_single(ohci->card.device, payload_bus,
  685. packet->payload_length, DMA_TO_DEVICE);
  686. evt = le16_to_cpu(last->transfer_status) & 0x1f;
  687. packet->timestamp = le16_to_cpu(last->res_count);
  688. switch (evt) {
  689. case OHCI1394_evt_timeout:
  690. /* Async response transmit timed out. */
  691. packet->ack = RCODE_CANCELLED;
  692. break;
  693. case OHCI1394_evt_flushed:
  694. /*
  695. * The packet was flushed should give same error as
  696. * when we try to use a stale generation count.
  697. */
  698. packet->ack = RCODE_GENERATION;
  699. break;
  700. case OHCI1394_evt_missing_ack:
  701. /*
  702. * Using a valid (current) generation count, but the
  703. * node is not on the bus or not sending acks.
  704. */
  705. packet->ack = RCODE_NO_ACK;
  706. break;
  707. case ACK_COMPLETE + 0x10:
  708. case ACK_PENDING + 0x10:
  709. case ACK_BUSY_X + 0x10:
  710. case ACK_BUSY_A + 0x10:
  711. case ACK_BUSY_B + 0x10:
  712. case ACK_DATA_ERROR + 0x10:
  713. case ACK_TYPE_ERROR + 0x10:
  714. packet->ack = evt - 0x10;
  715. break;
  716. default:
  717. packet->ack = RCODE_SEND_ERROR;
  718. break;
  719. }
  720. packet->callback(packet, &ohci->card, packet->ack);
  721. return 1;
  722. }
  723. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  724. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  725. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  726. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  727. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  728. static void
  729. handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  730. {
  731. struct fw_packet response;
  732. int tcode, length, i;
  733. tcode = HEADER_GET_TCODE(packet->header[0]);
  734. if (TCODE_IS_BLOCK_PACKET(tcode))
  735. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  736. else
  737. length = 4;
  738. i = csr - CSR_CONFIG_ROM;
  739. if (i + length > CONFIG_ROM_SIZE) {
  740. fw_fill_response(&response, packet->header,
  741. RCODE_ADDRESS_ERROR, NULL, 0);
  742. } else if (!TCODE_IS_READ_REQUEST(tcode)) {
  743. fw_fill_response(&response, packet->header,
  744. RCODE_TYPE_ERROR, NULL, 0);
  745. } else {
  746. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  747. (void *) ohci->config_rom + i, length);
  748. }
  749. fw_core_handle_response(&ohci->card, &response);
  750. }
  751. static void
  752. handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  753. {
  754. struct fw_packet response;
  755. int tcode, length, ext_tcode, sel;
  756. __be32 *payload, lock_old;
  757. u32 lock_arg, lock_data;
  758. tcode = HEADER_GET_TCODE(packet->header[0]);
  759. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  760. payload = packet->payload;
  761. ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
  762. if (tcode == TCODE_LOCK_REQUEST &&
  763. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  764. lock_arg = be32_to_cpu(payload[0]);
  765. lock_data = be32_to_cpu(payload[1]);
  766. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  767. lock_arg = 0;
  768. lock_data = 0;
  769. } else {
  770. fw_fill_response(&response, packet->header,
  771. RCODE_TYPE_ERROR, NULL, 0);
  772. goto out;
  773. }
  774. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  775. reg_write(ohci, OHCI1394_CSRData, lock_data);
  776. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  777. reg_write(ohci, OHCI1394_CSRControl, sel);
  778. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
  779. lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
  780. else
  781. fw_notify("swap not done yet\n");
  782. fw_fill_response(&response, packet->header,
  783. RCODE_COMPLETE, &lock_old, sizeof(lock_old));
  784. out:
  785. fw_core_handle_response(&ohci->card, &response);
  786. }
  787. static void
  788. handle_local_request(struct context *ctx, struct fw_packet *packet)
  789. {
  790. u64 offset;
  791. u32 csr;
  792. if (ctx == &ctx->ohci->at_request_ctx) {
  793. packet->ack = ACK_PENDING;
  794. packet->callback(packet, &ctx->ohci->card, packet->ack);
  795. }
  796. offset =
  797. ((unsigned long long)
  798. HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
  799. packet->header[2];
  800. csr = offset - CSR_REGISTER_BASE;
  801. /* Handle config rom reads. */
  802. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  803. handle_local_rom(ctx->ohci, packet, csr);
  804. else switch (csr) {
  805. case CSR_BUS_MANAGER_ID:
  806. case CSR_BANDWIDTH_AVAILABLE:
  807. case CSR_CHANNELS_AVAILABLE_HI:
  808. case CSR_CHANNELS_AVAILABLE_LO:
  809. handle_local_lock(ctx->ohci, packet, csr);
  810. break;
  811. default:
  812. if (ctx == &ctx->ohci->at_request_ctx)
  813. fw_core_handle_request(&ctx->ohci->card, packet);
  814. else
  815. fw_core_handle_response(&ctx->ohci->card, packet);
  816. break;
  817. }
  818. if (ctx == &ctx->ohci->at_response_ctx) {
  819. packet->ack = ACK_COMPLETE;
  820. packet->callback(packet, &ctx->ohci->card, packet->ack);
  821. }
  822. }
  823. static void
  824. at_context_transmit(struct context *ctx, struct fw_packet *packet)
  825. {
  826. unsigned long flags;
  827. int retval;
  828. spin_lock_irqsave(&ctx->ohci->lock, flags);
  829. if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
  830. ctx->ohci->generation == packet->generation) {
  831. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  832. handle_local_request(ctx, packet);
  833. return;
  834. }
  835. retval = at_context_queue_packet(ctx, packet);
  836. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  837. if (retval < 0)
  838. packet->callback(packet, &ctx->ohci->card, packet->ack);
  839. }
  840. static void bus_reset_tasklet(unsigned long data)
  841. {
  842. struct fw_ohci *ohci = (struct fw_ohci *)data;
  843. int self_id_count, i, j, reg;
  844. int generation, new_generation;
  845. unsigned long flags;
  846. void *free_rom = NULL;
  847. dma_addr_t free_rom_bus = 0;
  848. reg = reg_read(ohci, OHCI1394_NodeID);
  849. if (!(reg & OHCI1394_NodeID_idValid)) {
  850. fw_notify("node ID not valid, new bus reset in progress\n");
  851. return;
  852. }
  853. if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
  854. fw_notify("malconfigured bus\n");
  855. return;
  856. }
  857. ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
  858. OHCI1394_NodeID_nodeNumber);
  859. /*
  860. * The count in the SelfIDCount register is the number of
  861. * bytes in the self ID receive buffer. Since we also receive
  862. * the inverted quadlets and a header quadlet, we shift one
  863. * bit extra to get the actual number of self IDs.
  864. */
  865. self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
  866. generation = (cond_le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  867. rmb();
  868. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  869. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
  870. fw_error("inconsistent self IDs\n");
  871. ohci->self_id_buffer[j] =
  872. cond_le32_to_cpu(ohci->self_id_cpu[i]);
  873. }
  874. rmb();
  875. /*
  876. * Check the consistency of the self IDs we just read. The
  877. * problem we face is that a new bus reset can start while we
  878. * read out the self IDs from the DMA buffer. If this happens,
  879. * the DMA buffer will be overwritten with new self IDs and we
  880. * will read out inconsistent data. The OHCI specification
  881. * (section 11.2) recommends a technique similar to
  882. * linux/seqlock.h, where we remember the generation of the
  883. * self IDs in the buffer before reading them out and compare
  884. * it to the current generation after reading them out. If
  885. * the two generations match we know we have a consistent set
  886. * of self IDs.
  887. */
  888. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  889. if (new_generation != generation) {
  890. fw_notify("recursive bus reset detected, "
  891. "discarding self ids\n");
  892. return;
  893. }
  894. /* FIXME: Document how the locking works. */
  895. spin_lock_irqsave(&ohci->lock, flags);
  896. ohci->generation = generation;
  897. context_stop(&ohci->at_request_ctx);
  898. context_stop(&ohci->at_response_ctx);
  899. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  900. /*
  901. * This next bit is unrelated to the AT context stuff but we
  902. * have to do it under the spinlock also. If a new config rom
  903. * was set up before this reset, the old one is now no longer
  904. * in use and we can free it. Update the config rom pointers
  905. * to point to the current config rom and clear the
  906. * next_config_rom pointer so a new udpate can take place.
  907. */
  908. if (ohci->next_config_rom != NULL) {
  909. if (ohci->next_config_rom != ohci->config_rom) {
  910. free_rom = ohci->config_rom;
  911. free_rom_bus = ohci->config_rom_bus;
  912. }
  913. ohci->config_rom = ohci->next_config_rom;
  914. ohci->config_rom_bus = ohci->next_config_rom_bus;
  915. ohci->next_config_rom = NULL;
  916. /*
  917. * Restore config_rom image and manually update
  918. * config_rom registers. Writing the header quadlet
  919. * will indicate that the config rom is ready, so we
  920. * do that last.
  921. */
  922. reg_write(ohci, OHCI1394_BusOptions,
  923. be32_to_cpu(ohci->config_rom[2]));
  924. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  925. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  926. }
  927. spin_unlock_irqrestore(&ohci->lock, flags);
  928. if (free_rom)
  929. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  930. free_rom, free_rom_bus);
  931. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  932. self_id_count, ohci->self_id_buffer);
  933. }
  934. static irqreturn_t irq_handler(int irq, void *data)
  935. {
  936. struct fw_ohci *ohci = data;
  937. u32 event, iso_event, cycle_time;
  938. int i;
  939. event = reg_read(ohci, OHCI1394_IntEventClear);
  940. if (!event || !~event)
  941. return IRQ_NONE;
  942. reg_write(ohci, OHCI1394_IntEventClear, event);
  943. if (event & OHCI1394_selfIDComplete)
  944. tasklet_schedule(&ohci->bus_reset_tasklet);
  945. if (event & OHCI1394_RQPkt)
  946. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  947. if (event & OHCI1394_RSPkt)
  948. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  949. if (event & OHCI1394_reqTxComplete)
  950. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  951. if (event & OHCI1394_respTxComplete)
  952. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  953. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  954. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  955. while (iso_event) {
  956. i = ffs(iso_event) - 1;
  957. tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
  958. iso_event &= ~(1 << i);
  959. }
  960. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  961. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  962. while (iso_event) {
  963. i = ffs(iso_event) - 1;
  964. tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
  965. iso_event &= ~(1 << i);
  966. }
  967. if (unlikely(event & OHCI1394_postedWriteErr))
  968. fw_error("PCI posted write error\n");
  969. if (unlikely(event & OHCI1394_cycleTooLong)) {
  970. if (printk_ratelimit())
  971. fw_notify("isochronous cycle too long\n");
  972. reg_write(ohci, OHCI1394_LinkControlSet,
  973. OHCI1394_LinkControl_cycleMaster);
  974. }
  975. if (event & OHCI1394_cycle64Seconds) {
  976. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  977. if ((cycle_time & 0x80000000) == 0)
  978. ohci->bus_seconds++;
  979. }
  980. return IRQ_HANDLED;
  981. }
  982. static int software_reset(struct fw_ohci *ohci)
  983. {
  984. int i;
  985. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  986. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  987. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  988. OHCI1394_HCControl_softReset) == 0)
  989. return 0;
  990. msleep(1);
  991. }
  992. return -EBUSY;
  993. }
  994. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  995. {
  996. struct fw_ohci *ohci = fw_ohci(card);
  997. struct pci_dev *dev = to_pci_dev(card->device);
  998. if (software_reset(ohci)) {
  999. fw_error("Failed to reset ohci card.\n");
  1000. return -EBUSY;
  1001. }
  1002. /*
  1003. * Now enable LPS, which we need in order to start accessing
  1004. * most of the registers. In fact, on some cards (ALI M5251),
  1005. * accessing registers in the SClk domain without LPS enabled
  1006. * will lock up the machine. Wait 50msec to make sure we have
  1007. * full link enabled.
  1008. */
  1009. reg_write(ohci, OHCI1394_HCControlSet,
  1010. OHCI1394_HCControl_LPS |
  1011. OHCI1394_HCControl_postedWriteEnable);
  1012. flush_writes(ohci);
  1013. msleep(50);
  1014. reg_write(ohci, OHCI1394_HCControlClear,
  1015. OHCI1394_HCControl_noByteSwapData);
  1016. reg_write(ohci, OHCI1394_LinkControlSet,
  1017. OHCI1394_LinkControl_rcvSelfID |
  1018. OHCI1394_LinkControl_cycleTimerEnable |
  1019. OHCI1394_LinkControl_cycleMaster);
  1020. reg_write(ohci, OHCI1394_ATRetries,
  1021. OHCI1394_MAX_AT_REQ_RETRIES |
  1022. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1023. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1024. ar_context_run(&ohci->ar_request_ctx);
  1025. ar_context_run(&ohci->ar_response_ctx);
  1026. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1027. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1028. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1029. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1030. reg_write(ohci, OHCI1394_IntMaskSet,
  1031. OHCI1394_selfIDComplete |
  1032. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1033. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1034. OHCI1394_isochRx | OHCI1394_isochTx |
  1035. OHCI1394_postedWriteErr | OHCI1394_cycleTooLong |
  1036. OHCI1394_cycle64Seconds | OHCI1394_masterIntEnable);
  1037. /* Activate link_on bit and contender bit in our self ID packets.*/
  1038. if (ohci_update_phy_reg(card, 4, 0,
  1039. PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
  1040. return -EIO;
  1041. /*
  1042. * When the link is not yet enabled, the atomic config rom
  1043. * update mechanism described below in ohci_set_config_rom()
  1044. * is not active. We have to update ConfigRomHeader and
  1045. * BusOptions manually, and the write to ConfigROMmap takes
  1046. * effect immediately. We tie this to the enabling of the
  1047. * link, so we have a valid config rom before enabling - the
  1048. * OHCI requires that ConfigROMhdr and BusOptions have valid
  1049. * values before enabling.
  1050. *
  1051. * However, when the ConfigROMmap is written, some controllers
  1052. * always read back quadlets 0 and 2 from the config rom to
  1053. * the ConfigRomHeader and BusOptions registers on bus reset.
  1054. * They shouldn't do that in this initial case where the link
  1055. * isn't enabled. This means we have to use the same
  1056. * workaround here, setting the bus header to 0 and then write
  1057. * the right values in the bus reset tasklet.
  1058. */
  1059. if (config_rom) {
  1060. ohci->next_config_rom =
  1061. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1062. &ohci->next_config_rom_bus,
  1063. GFP_KERNEL);
  1064. if (ohci->next_config_rom == NULL)
  1065. return -ENOMEM;
  1066. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  1067. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  1068. } else {
  1069. /*
  1070. * In the suspend case, config_rom is NULL, which
  1071. * means that we just reuse the old config rom.
  1072. */
  1073. ohci->next_config_rom = ohci->config_rom;
  1074. ohci->next_config_rom_bus = ohci->config_rom_bus;
  1075. }
  1076. ohci->next_header = be32_to_cpu(ohci->next_config_rom[0]);
  1077. ohci->next_config_rom[0] = 0;
  1078. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  1079. reg_write(ohci, OHCI1394_BusOptions,
  1080. be32_to_cpu(ohci->next_config_rom[2]));
  1081. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  1082. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  1083. if (request_irq(dev->irq, irq_handler,
  1084. IRQF_SHARED, ohci_driver_name, ohci)) {
  1085. fw_error("Failed to allocate shared interrupt %d.\n",
  1086. dev->irq);
  1087. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1088. ohci->config_rom, ohci->config_rom_bus);
  1089. return -EIO;
  1090. }
  1091. reg_write(ohci, OHCI1394_HCControlSet,
  1092. OHCI1394_HCControl_linkEnable |
  1093. OHCI1394_HCControl_BIBimageValid);
  1094. flush_writes(ohci);
  1095. /*
  1096. * We are ready to go, initiate bus reset to finish the
  1097. * initialization.
  1098. */
  1099. fw_core_initiate_bus_reset(&ohci->card, 1);
  1100. return 0;
  1101. }
  1102. static int
  1103. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  1104. {
  1105. struct fw_ohci *ohci;
  1106. unsigned long flags;
  1107. int retval = -EBUSY;
  1108. __be32 *next_config_rom;
  1109. dma_addr_t next_config_rom_bus;
  1110. ohci = fw_ohci(card);
  1111. /*
  1112. * When the OHCI controller is enabled, the config rom update
  1113. * mechanism is a bit tricky, but easy enough to use. See
  1114. * section 5.5.6 in the OHCI specification.
  1115. *
  1116. * The OHCI controller caches the new config rom address in a
  1117. * shadow register (ConfigROMmapNext) and needs a bus reset
  1118. * for the changes to take place. When the bus reset is
  1119. * detected, the controller loads the new values for the
  1120. * ConfigRomHeader and BusOptions registers from the specified
  1121. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  1122. * shadow register. All automatically and atomically.
  1123. *
  1124. * Now, there's a twist to this story. The automatic load of
  1125. * ConfigRomHeader and BusOptions doesn't honor the
  1126. * noByteSwapData bit, so with a be32 config rom, the
  1127. * controller will load be32 values in to these registers
  1128. * during the atomic update, even on litte endian
  1129. * architectures. The workaround we use is to put a 0 in the
  1130. * header quadlet; 0 is endian agnostic and means that the
  1131. * config rom isn't ready yet. In the bus reset tasklet we
  1132. * then set up the real values for the two registers.
  1133. *
  1134. * We use ohci->lock to avoid racing with the code that sets
  1135. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  1136. */
  1137. next_config_rom =
  1138. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1139. &next_config_rom_bus, GFP_KERNEL);
  1140. if (next_config_rom == NULL)
  1141. return -ENOMEM;
  1142. spin_lock_irqsave(&ohci->lock, flags);
  1143. if (ohci->next_config_rom == NULL) {
  1144. ohci->next_config_rom = next_config_rom;
  1145. ohci->next_config_rom_bus = next_config_rom_bus;
  1146. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  1147. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  1148. length * 4);
  1149. ohci->next_header = config_rom[0];
  1150. ohci->next_config_rom[0] = 0;
  1151. reg_write(ohci, OHCI1394_ConfigROMmap,
  1152. ohci->next_config_rom_bus);
  1153. retval = 0;
  1154. }
  1155. spin_unlock_irqrestore(&ohci->lock, flags);
  1156. /*
  1157. * Now initiate a bus reset to have the changes take
  1158. * effect. We clean up the old config rom memory and DMA
  1159. * mappings in the bus reset tasklet, since the OHCI
  1160. * controller could need to access it before the bus reset
  1161. * takes effect.
  1162. */
  1163. if (retval == 0)
  1164. fw_core_initiate_bus_reset(&ohci->card, 1);
  1165. else
  1166. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1167. next_config_rom, next_config_rom_bus);
  1168. return retval;
  1169. }
  1170. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  1171. {
  1172. struct fw_ohci *ohci = fw_ohci(card);
  1173. at_context_transmit(&ohci->at_request_ctx, packet);
  1174. }
  1175. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  1176. {
  1177. struct fw_ohci *ohci = fw_ohci(card);
  1178. at_context_transmit(&ohci->at_response_ctx, packet);
  1179. }
  1180. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  1181. {
  1182. struct fw_ohci *ohci = fw_ohci(card);
  1183. struct context *ctx = &ohci->at_request_ctx;
  1184. struct driver_data *driver_data = packet->driver_data;
  1185. int retval = -ENOENT;
  1186. tasklet_disable(&ctx->tasklet);
  1187. if (packet->ack != 0)
  1188. goto out;
  1189. driver_data->packet = NULL;
  1190. packet->ack = RCODE_CANCELLED;
  1191. packet->callback(packet, &ohci->card, packet->ack);
  1192. retval = 0;
  1193. out:
  1194. tasklet_enable(&ctx->tasklet);
  1195. return retval;
  1196. }
  1197. static int
  1198. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  1199. {
  1200. struct fw_ohci *ohci = fw_ohci(card);
  1201. unsigned long flags;
  1202. int n, retval = 0;
  1203. /*
  1204. * FIXME: Make sure this bitmask is cleared when we clear the busReset
  1205. * interrupt bit. Clear physReqResourceAllBuses on bus reset.
  1206. */
  1207. spin_lock_irqsave(&ohci->lock, flags);
  1208. if (ohci->generation != generation) {
  1209. retval = -ESTALE;
  1210. goto out;
  1211. }
  1212. /*
  1213. * Note, if the node ID contains a non-local bus ID, physical DMA is
  1214. * enabled for _all_ nodes on remote buses.
  1215. */
  1216. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  1217. if (n < 32)
  1218. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  1219. else
  1220. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  1221. flush_writes(ohci);
  1222. out:
  1223. spin_unlock_irqrestore(&ohci->lock, flags);
  1224. return retval;
  1225. }
  1226. static u64
  1227. ohci_get_bus_time(struct fw_card *card)
  1228. {
  1229. struct fw_ohci *ohci = fw_ohci(card);
  1230. u32 cycle_time;
  1231. u64 bus_time;
  1232. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1233. bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
  1234. return bus_time;
  1235. }
  1236. static int handle_ir_dualbuffer_packet(struct context *context,
  1237. struct descriptor *d,
  1238. struct descriptor *last)
  1239. {
  1240. struct iso_context *ctx =
  1241. container_of(context, struct iso_context, context);
  1242. struct db_descriptor *db = (struct db_descriptor *) d;
  1243. __le32 *ir_header;
  1244. size_t header_length;
  1245. void *p, *end;
  1246. int i;
  1247. if (db->first_res_count != 0 && db->second_res_count != 0) {
  1248. if (ctx->excess_bytes <= le16_to_cpu(db->second_req_count)) {
  1249. /* This descriptor isn't done yet, stop iteration. */
  1250. return 0;
  1251. }
  1252. ctx->excess_bytes -= le16_to_cpu(db->second_req_count);
  1253. }
  1254. header_length = le16_to_cpu(db->first_req_count) -
  1255. le16_to_cpu(db->first_res_count);
  1256. i = ctx->header_length;
  1257. p = db + 1;
  1258. end = p + header_length;
  1259. while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
  1260. /*
  1261. * The iso header is byteswapped to little endian by
  1262. * the controller, but the remaining header quadlets
  1263. * are big endian. We want to present all the headers
  1264. * as big endian, so we have to swap the first
  1265. * quadlet.
  1266. */
  1267. *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
  1268. memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
  1269. i += ctx->base.header_size;
  1270. ctx->excess_bytes +=
  1271. (le32_to_cpu(*(__le32 *)(p + 4)) >> 16) & 0xffff;
  1272. p += ctx->base.header_size + 4;
  1273. }
  1274. ctx->header_length = i;
  1275. ctx->excess_bytes -= le16_to_cpu(db->second_req_count) -
  1276. le16_to_cpu(db->second_res_count);
  1277. if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
  1278. ir_header = (__le32 *) (db + 1);
  1279. ctx->base.callback(&ctx->base,
  1280. le32_to_cpu(ir_header[0]) & 0xffff,
  1281. ctx->header_length, ctx->header,
  1282. ctx->base.callback_data);
  1283. ctx->header_length = 0;
  1284. }
  1285. return 1;
  1286. }
  1287. static int handle_ir_packet_per_buffer(struct context *context,
  1288. struct descriptor *d,
  1289. struct descriptor *last)
  1290. {
  1291. struct iso_context *ctx =
  1292. container_of(context, struct iso_context, context);
  1293. struct descriptor *pd;
  1294. __le32 *ir_header;
  1295. void *p;
  1296. int i;
  1297. for (pd = d; pd <= last; pd++) {
  1298. if (pd->transfer_status)
  1299. break;
  1300. }
  1301. if (pd > last)
  1302. /* Descriptor(s) not done yet, stop iteration */
  1303. return 0;
  1304. i = ctx->header_length;
  1305. p = last + 1;
  1306. if (ctx->base.header_size > 0 &&
  1307. i + ctx->base.header_size <= PAGE_SIZE) {
  1308. /*
  1309. * The iso header is byteswapped to little endian by
  1310. * the controller, but the remaining header quadlets
  1311. * are big endian. We want to present all the headers
  1312. * as big endian, so we have to swap the first quadlet.
  1313. */
  1314. *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
  1315. memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
  1316. ctx->header_length += ctx->base.header_size;
  1317. }
  1318. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS) {
  1319. ir_header = (__le32 *) p;
  1320. ctx->base.callback(&ctx->base,
  1321. le32_to_cpu(ir_header[0]) & 0xffff,
  1322. ctx->header_length, ctx->header,
  1323. ctx->base.callback_data);
  1324. ctx->header_length = 0;
  1325. }
  1326. return 1;
  1327. }
  1328. static int handle_it_packet(struct context *context,
  1329. struct descriptor *d,
  1330. struct descriptor *last)
  1331. {
  1332. struct iso_context *ctx =
  1333. container_of(context, struct iso_context, context);
  1334. if (last->transfer_status == 0)
  1335. /* This descriptor isn't done yet, stop iteration. */
  1336. return 0;
  1337. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
  1338. ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
  1339. 0, NULL, ctx->base.callback_data);
  1340. return 1;
  1341. }
  1342. static struct fw_iso_context *
  1343. ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
  1344. {
  1345. struct fw_ohci *ohci = fw_ohci(card);
  1346. struct iso_context *ctx, *list;
  1347. descriptor_callback_t callback;
  1348. u32 *mask, regs;
  1349. unsigned long flags;
  1350. int index, retval = -ENOMEM;
  1351. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  1352. mask = &ohci->it_context_mask;
  1353. list = ohci->it_context_list;
  1354. callback = handle_it_packet;
  1355. } else {
  1356. mask = &ohci->ir_context_mask;
  1357. list = ohci->ir_context_list;
  1358. if (ohci->version >= OHCI_VERSION_1_1)
  1359. callback = handle_ir_dualbuffer_packet;
  1360. else
  1361. callback = handle_ir_packet_per_buffer;
  1362. }
  1363. spin_lock_irqsave(&ohci->lock, flags);
  1364. index = ffs(*mask) - 1;
  1365. if (index >= 0)
  1366. *mask &= ~(1 << index);
  1367. spin_unlock_irqrestore(&ohci->lock, flags);
  1368. if (index < 0)
  1369. return ERR_PTR(-EBUSY);
  1370. if (type == FW_ISO_CONTEXT_TRANSMIT)
  1371. regs = OHCI1394_IsoXmitContextBase(index);
  1372. else
  1373. regs = OHCI1394_IsoRcvContextBase(index);
  1374. ctx = &list[index];
  1375. memset(ctx, 0, sizeof(*ctx));
  1376. ctx->header_length = 0;
  1377. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  1378. if (ctx->header == NULL)
  1379. goto out;
  1380. retval = context_init(&ctx->context, ohci, regs, callback);
  1381. if (retval < 0)
  1382. goto out_with_header;
  1383. return &ctx->base;
  1384. out_with_header:
  1385. free_page((unsigned long)ctx->header);
  1386. out:
  1387. spin_lock_irqsave(&ohci->lock, flags);
  1388. *mask |= 1 << index;
  1389. spin_unlock_irqrestore(&ohci->lock, flags);
  1390. return ERR_PTR(retval);
  1391. }
  1392. static int ohci_start_iso(struct fw_iso_context *base,
  1393. s32 cycle, u32 sync, u32 tags)
  1394. {
  1395. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1396. struct fw_ohci *ohci = ctx->context.ohci;
  1397. u32 control, match;
  1398. int index;
  1399. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1400. index = ctx - ohci->it_context_list;
  1401. match = 0;
  1402. if (cycle >= 0)
  1403. match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  1404. (cycle & 0x7fff) << 16;
  1405. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  1406. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  1407. context_run(&ctx->context, match);
  1408. } else {
  1409. index = ctx - ohci->ir_context_list;
  1410. control = IR_CONTEXT_ISOCH_HEADER;
  1411. if (ohci->version >= OHCI_VERSION_1_1)
  1412. control |= IR_CONTEXT_DUAL_BUFFER_MODE;
  1413. match = (tags << 28) | (sync << 8) | ctx->base.channel;
  1414. if (cycle >= 0) {
  1415. match |= (cycle & 0x07fff) << 12;
  1416. control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
  1417. }
  1418. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  1419. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  1420. reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
  1421. context_run(&ctx->context, control);
  1422. }
  1423. return 0;
  1424. }
  1425. static int ohci_stop_iso(struct fw_iso_context *base)
  1426. {
  1427. struct fw_ohci *ohci = fw_ohci(base->card);
  1428. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1429. int index;
  1430. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1431. index = ctx - ohci->it_context_list;
  1432. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  1433. } else {
  1434. index = ctx - ohci->ir_context_list;
  1435. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  1436. }
  1437. flush_writes(ohci);
  1438. context_stop(&ctx->context);
  1439. return 0;
  1440. }
  1441. static void ohci_free_iso_context(struct fw_iso_context *base)
  1442. {
  1443. struct fw_ohci *ohci = fw_ohci(base->card);
  1444. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1445. unsigned long flags;
  1446. int index;
  1447. ohci_stop_iso(base);
  1448. context_release(&ctx->context);
  1449. free_page((unsigned long)ctx->header);
  1450. spin_lock_irqsave(&ohci->lock, flags);
  1451. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1452. index = ctx - ohci->it_context_list;
  1453. ohci->it_context_mask |= 1 << index;
  1454. } else {
  1455. index = ctx - ohci->ir_context_list;
  1456. ohci->ir_context_mask |= 1 << index;
  1457. }
  1458. spin_unlock_irqrestore(&ohci->lock, flags);
  1459. }
  1460. static int
  1461. ohci_queue_iso_transmit(struct fw_iso_context *base,
  1462. struct fw_iso_packet *packet,
  1463. struct fw_iso_buffer *buffer,
  1464. unsigned long payload)
  1465. {
  1466. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1467. struct descriptor *d, *last, *pd;
  1468. struct fw_iso_packet *p;
  1469. __le32 *header;
  1470. dma_addr_t d_bus, page_bus;
  1471. u32 z, header_z, payload_z, irq;
  1472. u32 payload_index, payload_end_index, next_page_index;
  1473. int page, end_page, i, length, offset;
  1474. /*
  1475. * FIXME: Cycle lost behavior should be configurable: lose
  1476. * packet, retransmit or terminate..
  1477. */
  1478. p = packet;
  1479. payload_index = payload;
  1480. if (p->skip)
  1481. z = 1;
  1482. else
  1483. z = 2;
  1484. if (p->header_length > 0)
  1485. z++;
  1486. /* Determine the first page the payload isn't contained in. */
  1487. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  1488. if (p->payload_length > 0)
  1489. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  1490. else
  1491. payload_z = 0;
  1492. z += payload_z;
  1493. /* Get header size in number of descriptors. */
  1494. header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
  1495. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  1496. if (d == NULL)
  1497. return -ENOMEM;
  1498. if (!p->skip) {
  1499. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  1500. d[0].req_count = cpu_to_le16(8);
  1501. header = (__le32 *) &d[1];
  1502. header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
  1503. IT_HEADER_TAG(p->tag) |
  1504. IT_HEADER_TCODE(TCODE_STREAM_DATA) |
  1505. IT_HEADER_CHANNEL(ctx->base.channel) |
  1506. IT_HEADER_SPEED(ctx->base.speed));
  1507. header[1] =
  1508. cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
  1509. p->payload_length));
  1510. }
  1511. if (p->header_length > 0) {
  1512. d[2].req_count = cpu_to_le16(p->header_length);
  1513. d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
  1514. memcpy(&d[z], p->header, p->header_length);
  1515. }
  1516. pd = d + z - payload_z;
  1517. payload_end_index = payload_index + p->payload_length;
  1518. for (i = 0; i < payload_z; i++) {
  1519. page = payload_index >> PAGE_SHIFT;
  1520. offset = payload_index & ~PAGE_MASK;
  1521. next_page_index = (page + 1) << PAGE_SHIFT;
  1522. length =
  1523. min(next_page_index, payload_end_index) - payload_index;
  1524. pd[i].req_count = cpu_to_le16(length);
  1525. page_bus = page_private(buffer->pages[page]);
  1526. pd[i].data_address = cpu_to_le32(page_bus + offset);
  1527. payload_index += length;
  1528. }
  1529. if (p->interrupt)
  1530. irq = DESCRIPTOR_IRQ_ALWAYS;
  1531. else
  1532. irq = DESCRIPTOR_NO_IRQ;
  1533. last = z == 2 ? d : d + z - 1;
  1534. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  1535. DESCRIPTOR_STATUS |
  1536. DESCRIPTOR_BRANCH_ALWAYS |
  1537. irq);
  1538. context_append(&ctx->context, d, z, header_z);
  1539. return 0;
  1540. }
  1541. static int
  1542. ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
  1543. struct fw_iso_packet *packet,
  1544. struct fw_iso_buffer *buffer,
  1545. unsigned long payload)
  1546. {
  1547. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1548. struct db_descriptor *db = NULL;
  1549. struct descriptor *d;
  1550. struct fw_iso_packet *p;
  1551. dma_addr_t d_bus, page_bus;
  1552. u32 z, header_z, length, rest;
  1553. int page, offset, packet_count, header_size;
  1554. /*
  1555. * FIXME: Cycle lost behavior should be configurable: lose
  1556. * packet, retransmit or terminate..
  1557. */
  1558. p = packet;
  1559. z = 2;
  1560. /*
  1561. * The OHCI controller puts the status word in the header
  1562. * buffer too, so we need 4 extra bytes per packet.
  1563. */
  1564. packet_count = p->header_length / ctx->base.header_size;
  1565. header_size = packet_count * (ctx->base.header_size + 4);
  1566. /* Get header size in number of descriptors. */
  1567. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  1568. page = payload >> PAGE_SHIFT;
  1569. offset = payload & ~PAGE_MASK;
  1570. rest = p->payload_length;
  1571. /* FIXME: make packet-per-buffer/dual-buffer a context option */
  1572. while (rest > 0) {
  1573. d = context_get_descriptors(&ctx->context,
  1574. z + header_z, &d_bus);
  1575. if (d == NULL)
  1576. return -ENOMEM;
  1577. db = (struct db_descriptor *) d;
  1578. db->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1579. DESCRIPTOR_BRANCH_ALWAYS);
  1580. db->first_size = cpu_to_le16(ctx->base.header_size + 4);
  1581. if (p->skip && rest == p->payload_length) {
  1582. db->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  1583. db->first_req_count = db->first_size;
  1584. } else {
  1585. db->first_req_count = cpu_to_le16(header_size);
  1586. }
  1587. db->first_res_count = db->first_req_count;
  1588. db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
  1589. if (p->skip && rest == p->payload_length)
  1590. length = 4;
  1591. else if (offset + rest < PAGE_SIZE)
  1592. length = rest;
  1593. else
  1594. length = PAGE_SIZE - offset;
  1595. db->second_req_count = cpu_to_le16(length);
  1596. db->second_res_count = db->second_req_count;
  1597. page_bus = page_private(buffer->pages[page]);
  1598. db->second_buffer = cpu_to_le32(page_bus + offset);
  1599. if (p->interrupt && length == rest)
  1600. db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  1601. context_append(&ctx->context, d, z, header_z);
  1602. offset = (offset + length) & ~PAGE_MASK;
  1603. rest -= length;
  1604. if (offset == 0)
  1605. page++;
  1606. }
  1607. return 0;
  1608. }
  1609. static int
  1610. ohci_queue_iso_receive_packet_per_buffer(struct fw_iso_context *base,
  1611. struct fw_iso_packet *packet,
  1612. struct fw_iso_buffer *buffer,
  1613. unsigned long payload)
  1614. {
  1615. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1616. struct descriptor *d = NULL, *pd = NULL;
  1617. struct fw_iso_packet *p = packet;
  1618. dma_addr_t d_bus, page_bus;
  1619. u32 z, header_z, rest;
  1620. int i, j, length;
  1621. int page, offset, packet_count, header_size, payload_per_buffer;
  1622. /*
  1623. * The OHCI controller puts the status word in the
  1624. * buffer too, so we need 4 extra bytes per packet.
  1625. */
  1626. packet_count = p->header_length / ctx->base.header_size;
  1627. header_size = ctx->base.header_size + 4;
  1628. /* Get header size in number of descriptors. */
  1629. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  1630. page = payload >> PAGE_SHIFT;
  1631. offset = payload & ~PAGE_MASK;
  1632. payload_per_buffer = p->payload_length / packet_count;
  1633. for (i = 0; i < packet_count; i++) {
  1634. /* d points to the header descriptor */
  1635. z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
  1636. d = context_get_descriptors(&ctx->context,
  1637. z + header_z, &d_bus);
  1638. if (d == NULL)
  1639. return -ENOMEM;
  1640. d->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1641. DESCRIPTOR_INPUT_MORE);
  1642. if (p->skip && i == 0)
  1643. d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  1644. d->req_count = cpu_to_le16(header_size);
  1645. d->res_count = d->req_count;
  1646. d->transfer_status = 0;
  1647. d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
  1648. rest = payload_per_buffer;
  1649. for (j = 1; j < z; j++) {
  1650. pd = d + j;
  1651. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1652. DESCRIPTOR_INPUT_MORE);
  1653. if (offset + rest < PAGE_SIZE)
  1654. length = rest;
  1655. else
  1656. length = PAGE_SIZE - offset;
  1657. pd->req_count = cpu_to_le16(length);
  1658. pd->res_count = pd->req_count;
  1659. pd->transfer_status = 0;
  1660. page_bus = page_private(buffer->pages[page]);
  1661. pd->data_address = cpu_to_le32(page_bus + offset);
  1662. offset = (offset + length) & ~PAGE_MASK;
  1663. rest -= length;
  1664. if (offset == 0)
  1665. page++;
  1666. }
  1667. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1668. DESCRIPTOR_INPUT_LAST |
  1669. DESCRIPTOR_BRANCH_ALWAYS);
  1670. if (p->interrupt && i == packet_count - 1)
  1671. pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  1672. context_append(&ctx->context, d, z, header_z);
  1673. }
  1674. return 0;
  1675. }
  1676. static int
  1677. ohci_queue_iso(struct fw_iso_context *base,
  1678. struct fw_iso_packet *packet,
  1679. struct fw_iso_buffer *buffer,
  1680. unsigned long payload)
  1681. {
  1682. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1683. unsigned long flags;
  1684. int retval;
  1685. spin_lock_irqsave(&ctx->context.ohci->lock, flags);
  1686. if (base->type == FW_ISO_CONTEXT_TRANSMIT)
  1687. retval = ohci_queue_iso_transmit(base, packet, buffer, payload);
  1688. else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
  1689. retval = ohci_queue_iso_receive_dualbuffer(base, packet,
  1690. buffer, payload);
  1691. else
  1692. retval = ohci_queue_iso_receive_packet_per_buffer(base, packet,
  1693. buffer,
  1694. payload);
  1695. spin_unlock_irqrestore(&ctx->context.ohci->lock, flags);
  1696. return retval;
  1697. }
  1698. static const struct fw_card_driver ohci_driver = {
  1699. .name = ohci_driver_name,
  1700. .enable = ohci_enable,
  1701. .update_phy_reg = ohci_update_phy_reg,
  1702. .set_config_rom = ohci_set_config_rom,
  1703. .send_request = ohci_send_request,
  1704. .send_response = ohci_send_response,
  1705. .cancel_packet = ohci_cancel_packet,
  1706. .enable_phys_dma = ohci_enable_phys_dma,
  1707. .get_bus_time = ohci_get_bus_time,
  1708. .allocate_iso_context = ohci_allocate_iso_context,
  1709. .free_iso_context = ohci_free_iso_context,
  1710. .queue_iso = ohci_queue_iso,
  1711. .start_iso = ohci_start_iso,
  1712. .stop_iso = ohci_stop_iso,
  1713. };
  1714. static int __devinit
  1715. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  1716. {
  1717. struct fw_ohci *ohci;
  1718. u32 bus_options, max_receive, link_speed;
  1719. u64 guid;
  1720. int err;
  1721. size_t size;
  1722. #ifdef CONFIG_PPC_PMAC
  1723. /* Necessary on some machines if fw-ohci was loaded/ unloaded before */
  1724. if (machine_is(powermac)) {
  1725. struct device_node *ofn = pci_device_to_OF_node(dev);
  1726. if (ofn) {
  1727. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
  1728. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
  1729. }
  1730. }
  1731. #endif /* CONFIG_PPC_PMAC */
  1732. ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
  1733. if (ohci == NULL) {
  1734. fw_error("Could not malloc fw_ohci data.\n");
  1735. return -ENOMEM;
  1736. }
  1737. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1738. err = pci_enable_device(dev);
  1739. if (err) {
  1740. fw_error("Failed to enable OHCI hardware.\n");
  1741. goto fail_put_card;
  1742. }
  1743. pci_set_master(dev);
  1744. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1745. pci_set_drvdata(dev, ohci);
  1746. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  1747. ohci->old_uninorth = dev->vendor == PCI_VENDOR_ID_APPLE &&
  1748. dev->device == PCI_DEVICE_ID_APPLE_UNI_N_FW;
  1749. #endif
  1750. spin_lock_init(&ohci->lock);
  1751. tasklet_init(&ohci->bus_reset_tasklet,
  1752. bus_reset_tasklet, (unsigned long)ohci);
  1753. err = pci_request_region(dev, 0, ohci_driver_name);
  1754. if (err) {
  1755. fw_error("MMIO resource unavailable\n");
  1756. goto fail_disable;
  1757. }
  1758. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1759. if (ohci->registers == NULL) {
  1760. fw_error("Failed to remap registers\n");
  1761. err = -ENXIO;
  1762. goto fail_iomem;
  1763. }
  1764. ar_context_init(&ohci->ar_request_ctx, ohci,
  1765. OHCI1394_AsReqRcvContextControlSet);
  1766. ar_context_init(&ohci->ar_response_ctx, ohci,
  1767. OHCI1394_AsRspRcvContextControlSet);
  1768. context_init(&ohci->at_request_ctx, ohci,
  1769. OHCI1394_AsReqTrContextControlSet, handle_at_packet);
  1770. context_init(&ohci->at_response_ctx, ohci,
  1771. OHCI1394_AsRspTrContextControlSet, handle_at_packet);
  1772. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1773. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1774. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1775. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1776. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1777. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1778. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1779. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1780. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1781. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1782. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1783. fw_error("Out of memory for it/ir contexts.\n");
  1784. err = -ENOMEM;
  1785. goto fail_registers;
  1786. }
  1787. /* self-id dma buffer allocation */
  1788. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1789. SELF_ID_BUF_SIZE,
  1790. &ohci->self_id_bus,
  1791. GFP_KERNEL);
  1792. if (ohci->self_id_cpu == NULL) {
  1793. fw_error("Out of memory for self ID buffer.\n");
  1794. err = -ENOMEM;
  1795. goto fail_registers;
  1796. }
  1797. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1798. max_receive = (bus_options >> 12) & 0xf;
  1799. link_speed = bus_options & 0x7;
  1800. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1801. reg_read(ohci, OHCI1394_GUIDLo);
  1802. err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1803. if (err < 0)
  1804. goto fail_self_id;
  1805. ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  1806. fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
  1807. dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
  1808. return 0;
  1809. fail_self_id:
  1810. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1811. ohci->self_id_cpu, ohci->self_id_bus);
  1812. fail_registers:
  1813. kfree(ohci->it_context_list);
  1814. kfree(ohci->ir_context_list);
  1815. pci_iounmap(dev, ohci->registers);
  1816. fail_iomem:
  1817. pci_release_region(dev, 0);
  1818. fail_disable:
  1819. pci_disable_device(dev);
  1820. fail_put_card:
  1821. fw_card_put(&ohci->card);
  1822. return err;
  1823. }
  1824. static void pci_remove(struct pci_dev *dev)
  1825. {
  1826. struct fw_ohci *ohci;
  1827. ohci = pci_get_drvdata(dev);
  1828. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1829. flush_writes(ohci);
  1830. fw_core_remove_card(&ohci->card);
  1831. /*
  1832. * FIXME: Fail all pending packets here, now that the upper
  1833. * layers can't queue any more.
  1834. */
  1835. software_reset(ohci);
  1836. free_irq(dev->irq, ohci);
  1837. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1838. ohci->self_id_cpu, ohci->self_id_bus);
  1839. kfree(ohci->it_context_list);
  1840. kfree(ohci->ir_context_list);
  1841. pci_iounmap(dev, ohci->registers);
  1842. pci_release_region(dev, 0);
  1843. pci_disable_device(dev);
  1844. fw_card_put(&ohci->card);
  1845. #ifdef CONFIG_PPC_PMAC
  1846. /* On UniNorth, power down the cable and turn off the chip clock
  1847. * to save power on laptops */
  1848. if (machine_is(powermac)) {
  1849. struct device_node *ofn = pci_device_to_OF_node(dev);
  1850. if (ofn) {
  1851. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
  1852. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
  1853. }
  1854. }
  1855. #endif /* CONFIG_PPC_PMAC */
  1856. fw_notify("Removed fw-ohci device.\n");
  1857. }
  1858. #ifdef CONFIG_PM
  1859. static int pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1860. {
  1861. struct fw_ohci *ohci = pci_get_drvdata(pdev);
  1862. int err;
  1863. software_reset(ohci);
  1864. free_irq(pdev->irq, ohci);
  1865. err = pci_save_state(pdev);
  1866. if (err) {
  1867. fw_error("pci_save_state failed\n");
  1868. return err;
  1869. }
  1870. err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1871. if (err)
  1872. fw_error("pci_set_power_state failed with %d\n", err);
  1873. /* PowerMac suspend code comes last */
  1874. #ifdef CONFIG_PPC_PMAC
  1875. if (machine_is(powermac)) {
  1876. struct device_node *ofn = pci_device_to_OF_node(pdev);
  1877. if (ofn)
  1878. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
  1879. }
  1880. #endif /* CONFIG_PPC_PMAC */
  1881. return 0;
  1882. }
  1883. static int pci_resume(struct pci_dev *pdev)
  1884. {
  1885. struct fw_ohci *ohci = pci_get_drvdata(pdev);
  1886. int err;
  1887. /* PowerMac resume code comes first */
  1888. #ifdef CONFIG_PPC_PMAC
  1889. if (machine_is(powermac)) {
  1890. struct device_node *ofn = pci_device_to_OF_node(pdev);
  1891. if (ofn)
  1892. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
  1893. }
  1894. #endif /* CONFIG_PPC_PMAC */
  1895. pci_set_power_state(pdev, PCI_D0);
  1896. pci_restore_state(pdev);
  1897. err = pci_enable_device(pdev);
  1898. if (err) {
  1899. fw_error("pci_enable_device failed\n");
  1900. return err;
  1901. }
  1902. return ohci_enable(&ohci->card, NULL, 0);
  1903. }
  1904. #endif
  1905. static struct pci_device_id pci_table[] = {
  1906. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  1907. { }
  1908. };
  1909. MODULE_DEVICE_TABLE(pci, pci_table);
  1910. static struct pci_driver fw_ohci_pci_driver = {
  1911. .name = ohci_driver_name,
  1912. .id_table = pci_table,
  1913. .probe = pci_probe,
  1914. .remove = pci_remove,
  1915. #ifdef CONFIG_PM
  1916. .resume = pci_resume,
  1917. .suspend = pci_suspend,
  1918. #endif
  1919. };
  1920. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1921. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  1922. MODULE_LICENSE("GPL");
  1923. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  1924. #ifndef CONFIG_IEEE1394_OHCI1394_MODULE
  1925. MODULE_ALIAS("ohci1394");
  1926. #endif
  1927. static int __init fw_ohci_init(void)
  1928. {
  1929. return pci_register_driver(&fw_ohci_pci_driver);
  1930. }
  1931. static void __exit fw_ohci_cleanup(void)
  1932. {
  1933. pci_unregister_driver(&fw_ohci_pci_driver);
  1934. }
  1935. module_init(fw_ohci_init);
  1936. module_exit(fw_ohci_cleanup);