fw-ohci.c 62 KB

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