fw-ohci.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-ohci.c - Driver for OHCI 1394 boards
  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/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/poll.h>
  27. #include <linux/dma-mapping.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/semaphore.h>
  30. #include "fw-transaction.h"
  31. #include "fw-ohci.h"
  32. #define descriptor_output_more 0
  33. #define descriptor_output_last (1 << 12)
  34. #define descriptor_input_more (2 << 12)
  35. #define descriptor_input_last (3 << 12)
  36. #define descriptor_status (1 << 11)
  37. #define descriptor_key_immediate (2 << 8)
  38. #define descriptor_ping (1 << 7)
  39. #define descriptor_yy (1 << 6)
  40. #define descriptor_no_irq (0 << 4)
  41. #define descriptor_irq_error (1 << 4)
  42. #define descriptor_irq_always (3 << 4)
  43. #define descriptor_branch_always (3 << 2)
  44. #define descriptor_wait (3 << 0)
  45. struct descriptor {
  46. __le16 req_count;
  47. __le16 control;
  48. __le32 data_address;
  49. __le32 branch_address;
  50. __le16 res_count;
  51. __le16 transfer_status;
  52. } __attribute__((aligned(16)));
  53. struct db_descriptor {
  54. __le16 first_size;
  55. __le16 control;
  56. __le16 second_req_count;
  57. __le16 first_req_count;
  58. __le32 branch_address;
  59. __le16 second_res_count;
  60. __le16 first_res_count;
  61. __le32 reserved0;
  62. __le32 first_buffer;
  63. __le32 second_buffer;
  64. __le32 reserved1;
  65. } __attribute__((aligned(16)));
  66. #define control_set(regs) (regs)
  67. #define control_clear(regs) ((regs) + 4)
  68. #define command_ptr(regs) ((regs) + 12)
  69. #define context_match(regs) ((regs) + 16)
  70. struct ar_buffer {
  71. struct descriptor descriptor;
  72. struct ar_buffer *next;
  73. __le32 data[0];
  74. };
  75. struct ar_context {
  76. struct fw_ohci *ohci;
  77. struct ar_buffer *current_buffer;
  78. struct ar_buffer *last_buffer;
  79. void *pointer;
  80. u32 regs;
  81. struct tasklet_struct tasklet;
  82. };
  83. struct context;
  84. typedef int (*descriptor_callback_t)(struct context *ctx,
  85. struct descriptor *d,
  86. struct descriptor *last);
  87. struct context {
  88. struct fw_ohci *ohci;
  89. u32 regs;
  90. struct descriptor *buffer;
  91. dma_addr_t buffer_bus;
  92. size_t buffer_size;
  93. struct descriptor *head_descriptor;
  94. struct descriptor *tail_descriptor;
  95. struct descriptor *tail_descriptor_last;
  96. struct descriptor *prev_descriptor;
  97. descriptor_callback_t callback;
  98. struct tasklet_struct tasklet;
  99. };
  100. struct at_context {
  101. struct fw_ohci *ohci;
  102. dma_addr_t descriptor_bus;
  103. dma_addr_t buffer_bus;
  104. struct fw_packet *current_packet;
  105. struct list_head list;
  106. struct {
  107. struct descriptor more;
  108. __le32 header[4];
  109. struct descriptor last;
  110. } d;
  111. u32 regs;
  112. struct tasklet_struct tasklet;
  113. };
  114. #define it_header_sy(v) ((v) << 0)
  115. #define it_header_tcode(v) ((v) << 4)
  116. #define it_header_channel(v) ((v) << 8)
  117. #define it_header_tag(v) ((v) << 14)
  118. #define it_header_speed(v) ((v) << 16)
  119. #define it_header_data_length(v) ((v) << 16)
  120. struct iso_context {
  121. struct fw_iso_context base;
  122. struct context context;
  123. void *header;
  124. size_t header_length;
  125. };
  126. #define CONFIG_ROM_SIZE 1024
  127. struct fw_ohci {
  128. struct fw_card card;
  129. u32 version;
  130. __iomem char *registers;
  131. dma_addr_t self_id_bus;
  132. __le32 *self_id_cpu;
  133. struct tasklet_struct bus_reset_tasklet;
  134. int node_id;
  135. int generation;
  136. int request_generation;
  137. /* Spinlock for accessing fw_ohci data. Never call out of
  138. * this driver with this lock held. */
  139. spinlock_t lock;
  140. u32 self_id_buffer[512];
  141. /* Config rom buffers */
  142. __be32 *config_rom;
  143. dma_addr_t config_rom_bus;
  144. __be32 *next_config_rom;
  145. dma_addr_t next_config_rom_bus;
  146. u32 next_header;
  147. struct ar_context ar_request_ctx;
  148. struct ar_context ar_response_ctx;
  149. struct at_context at_request_ctx;
  150. struct at_context at_response_ctx;
  151. u32 it_context_mask;
  152. struct iso_context *it_context_list;
  153. u32 ir_context_mask;
  154. struct iso_context *ir_context_list;
  155. };
  156. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  157. {
  158. return container_of(card, struct fw_ohci, card);
  159. }
  160. #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  161. #define IR_CONTEXT_BUFFER_FILL 0x80000000
  162. #define IR_CONTEXT_ISOCH_HEADER 0x40000000
  163. #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
  164. #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
  165. #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
  166. #define CONTEXT_RUN 0x8000
  167. #define CONTEXT_WAKE 0x1000
  168. #define CONTEXT_DEAD 0x0800
  169. #define CONTEXT_ACTIVE 0x0400
  170. #define OHCI1394_MAX_AT_REQ_RETRIES 0x2
  171. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  172. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  173. #define FW_OHCI_MAJOR 240
  174. #define OHCI1394_REGISTER_SIZE 0x800
  175. #define OHCI_LOOP_COUNT 500
  176. #define OHCI1394_PCI_HCI_Control 0x40
  177. #define SELF_ID_BUF_SIZE 0x800
  178. #define OHCI_TCODE_PHY_PACKET 0x0e
  179. #define OHCI_VERSION_1_1 0x010010
  180. static char ohci_driver_name[] = KBUILD_MODNAME;
  181. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  182. {
  183. writel(data, ohci->registers + offset);
  184. }
  185. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  186. {
  187. return readl(ohci->registers + offset);
  188. }
  189. static inline void flush_writes(const struct fw_ohci *ohci)
  190. {
  191. /* Do a dummy read to flush writes. */
  192. reg_read(ohci, OHCI1394_Version);
  193. }
  194. static int
  195. ohci_update_phy_reg(struct fw_card *card, int addr,
  196. int clear_bits, int set_bits)
  197. {
  198. struct fw_ohci *ohci = fw_ohci(card);
  199. u32 val, old;
  200. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  201. msleep(2);
  202. val = reg_read(ohci, OHCI1394_PhyControl);
  203. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  204. fw_error("failed to set phy reg bits.\n");
  205. return -EBUSY;
  206. }
  207. old = OHCI1394_PhyControl_ReadData(val);
  208. old = (old & ~clear_bits) | set_bits;
  209. reg_write(ohci, OHCI1394_PhyControl,
  210. OHCI1394_PhyControl_Write(addr, old));
  211. return 0;
  212. }
  213. static int ar_context_add_page(struct ar_context *ctx)
  214. {
  215. struct device *dev = ctx->ohci->card.device;
  216. struct ar_buffer *ab;
  217. dma_addr_t ab_bus;
  218. size_t offset;
  219. ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
  220. if (ab == NULL)
  221. return -ENOMEM;
  222. ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
  223. if (dma_mapping_error(ab_bus)) {
  224. free_page((unsigned long) ab);
  225. return -ENOMEM;
  226. }
  227. memset(&ab->descriptor, 0, sizeof ab->descriptor);
  228. ab->descriptor.control = cpu_to_le16(descriptor_input_more |
  229. descriptor_status |
  230. descriptor_branch_always);
  231. offset = offsetof(struct ar_buffer, data);
  232. ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
  233. ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
  234. ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
  235. ab->descriptor.branch_address = 0;
  236. dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
  237. ctx->last_buffer->descriptor.branch_address = ab_bus | 1;
  238. ctx->last_buffer->next = ab;
  239. ctx->last_buffer = ab;
  240. reg_write(ctx->ohci, control_set(ctx->regs), CONTEXT_WAKE);
  241. flush_writes(ctx->ohci);
  242. return 0;
  243. }
  244. static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
  245. {
  246. struct fw_ohci *ohci = ctx->ohci;
  247. struct fw_packet p;
  248. u32 status, length, tcode;
  249. p.header[0] = le32_to_cpu(buffer[0]);
  250. p.header[1] = le32_to_cpu(buffer[1]);
  251. p.header[2] = le32_to_cpu(buffer[2]);
  252. tcode = (p.header[0] >> 4) & 0x0f;
  253. switch (tcode) {
  254. case TCODE_WRITE_QUADLET_REQUEST:
  255. case TCODE_READ_QUADLET_RESPONSE:
  256. p.header[3] = (__force __u32) buffer[3];
  257. p.header_length = 16;
  258. p.payload_length = 0;
  259. break;
  260. case TCODE_READ_BLOCK_REQUEST :
  261. p.header[3] = le32_to_cpu(buffer[3]);
  262. p.header_length = 16;
  263. p.payload_length = 0;
  264. break;
  265. case TCODE_WRITE_BLOCK_REQUEST:
  266. case TCODE_READ_BLOCK_RESPONSE:
  267. case TCODE_LOCK_REQUEST:
  268. case TCODE_LOCK_RESPONSE:
  269. p.header[3] = le32_to_cpu(buffer[3]);
  270. p.header_length = 16;
  271. p.payload_length = p.header[3] >> 16;
  272. break;
  273. case TCODE_WRITE_RESPONSE:
  274. case TCODE_READ_QUADLET_REQUEST:
  275. case OHCI_TCODE_PHY_PACKET:
  276. p.header_length = 12;
  277. p.payload_length = 0;
  278. break;
  279. }
  280. p.payload = (void *) buffer + p.header_length;
  281. /* FIXME: What to do about evt_* errors? */
  282. length = (p.header_length + p.payload_length + 3) / 4;
  283. status = le32_to_cpu(buffer[length]);
  284. p.ack = ((status >> 16) & 0x1f) - 16;
  285. p.speed = (status >> 21) & 0x7;
  286. p.timestamp = status & 0xffff;
  287. p.generation = ohci->request_generation;
  288. /* The OHCI bus reset handler synthesizes a phy packet with
  289. * the new generation number when a bus reset happens (see
  290. * section 8.4.2.3). This helps us determine when a request
  291. * was received and make sure we send the response in the same
  292. * generation. We only need this for requests; for responses
  293. * we use the unique tlabel for finding the matching
  294. * request. */
  295. if (p.ack + 16 == 0x09)
  296. ohci->request_generation = (buffer[2] >> 16) & 0xff;
  297. else if (ctx == &ohci->ar_request_ctx)
  298. fw_core_handle_request(&ohci->card, &p);
  299. else
  300. fw_core_handle_response(&ohci->card, &p);
  301. return buffer + length + 1;
  302. }
  303. static void ar_context_tasklet(unsigned long data)
  304. {
  305. struct ar_context *ctx = (struct ar_context *)data;
  306. struct fw_ohci *ohci = ctx->ohci;
  307. struct ar_buffer *ab;
  308. struct descriptor *d;
  309. void *buffer, *end;
  310. ab = ctx->current_buffer;
  311. d = &ab->descriptor;
  312. if (d->res_count == 0) {
  313. size_t size, rest, offset;
  314. /* This descriptor is finished and we may have a
  315. * packet split across this and the next buffer. We
  316. * reuse the page for reassembling the split packet. */
  317. offset = offsetof(struct ar_buffer, data);
  318. dma_unmap_single(ohci->card.device,
  319. ab->descriptor.data_address - offset,
  320. PAGE_SIZE, DMA_BIDIRECTIONAL);
  321. buffer = ab;
  322. ab = ab->next;
  323. d = &ab->descriptor;
  324. size = buffer + PAGE_SIZE - ctx->pointer;
  325. rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
  326. memmove(buffer, ctx->pointer, size);
  327. memcpy(buffer + size, ab->data, rest);
  328. ctx->current_buffer = ab;
  329. ctx->pointer = (void *) ab->data + rest;
  330. end = buffer + size + rest;
  331. while (buffer < end)
  332. buffer = handle_ar_packet(ctx, buffer);
  333. free_page((unsigned long)buffer);
  334. ar_context_add_page(ctx);
  335. } else {
  336. buffer = ctx->pointer;
  337. ctx->pointer = end =
  338. (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
  339. while (buffer < end)
  340. buffer = handle_ar_packet(ctx, buffer);
  341. }
  342. }
  343. static int
  344. ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
  345. {
  346. struct ar_buffer ab;
  347. ctx->regs = regs;
  348. ctx->ohci = ohci;
  349. ctx->last_buffer = &ab;
  350. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  351. ar_context_add_page(ctx);
  352. ar_context_add_page(ctx);
  353. ctx->current_buffer = ab.next;
  354. ctx->pointer = ctx->current_buffer->data;
  355. reg_write(ctx->ohci, command_ptr(ctx->regs), ab.descriptor.branch_address);
  356. reg_write(ctx->ohci, control_set(ctx->regs), CONTEXT_RUN);
  357. flush_writes(ctx->ohci);
  358. return 0;
  359. }
  360. static void context_tasklet(unsigned long data)
  361. {
  362. struct context *ctx = (struct context *) data;
  363. struct fw_ohci *ohci = ctx->ohci;
  364. struct descriptor *d, *last;
  365. u32 address;
  366. int z;
  367. dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
  368. ctx->buffer_size, DMA_TO_DEVICE);
  369. d = ctx->tail_descriptor;
  370. last = ctx->tail_descriptor_last;
  371. while (last->branch_address != 0) {
  372. address = le32_to_cpu(last->branch_address);
  373. z = address & 0xf;
  374. d = ctx->buffer + (address - ctx->buffer_bus) / sizeof *d;
  375. last = (z == 2) ? d : d + z - 1;
  376. if (!ctx->callback(ctx, d, last))
  377. break;
  378. ctx->tail_descriptor = d;
  379. ctx->tail_descriptor_last = last;
  380. }
  381. }
  382. static int
  383. context_init(struct context *ctx, struct fw_ohci *ohci,
  384. size_t buffer_size, u32 regs,
  385. descriptor_callback_t callback)
  386. {
  387. ctx->ohci = ohci;
  388. ctx->regs = regs;
  389. ctx->buffer_size = buffer_size;
  390. ctx->buffer = kmalloc(buffer_size, GFP_KERNEL);
  391. if (ctx->buffer == NULL)
  392. return -ENOMEM;
  393. tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
  394. ctx->callback = callback;
  395. ctx->buffer_bus =
  396. dma_map_single(ohci->card.device, ctx->buffer,
  397. buffer_size, DMA_TO_DEVICE);
  398. if (dma_mapping_error(ctx->buffer_bus)) {
  399. kfree(ctx->buffer);
  400. return -ENOMEM;
  401. }
  402. ctx->head_descriptor = ctx->buffer;
  403. ctx->prev_descriptor = ctx->buffer;
  404. ctx->tail_descriptor = ctx->buffer;
  405. ctx->tail_descriptor_last = ctx->buffer;
  406. /* We put a dummy descriptor in the buffer that has a NULL
  407. * branch address and looks like it's been sent. That way we
  408. * have a descriptor to append DMA programs to. Also, the
  409. * ring buffer invariant is that it always has at least one
  410. * element so that head == tail means buffer full. */
  411. memset(ctx->head_descriptor, 0, sizeof *ctx->head_descriptor);
  412. ctx->head_descriptor->control = cpu_to_le16(descriptor_output_last);
  413. ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
  414. ctx->head_descriptor++;
  415. return 0;
  416. }
  417. static void
  418. context_release(struct context *ctx)
  419. {
  420. struct fw_card *card = &ctx->ohci->card;
  421. dma_unmap_single(card->device, ctx->buffer_bus,
  422. ctx->buffer_size, DMA_TO_DEVICE);
  423. kfree(ctx->buffer);
  424. }
  425. static struct descriptor *
  426. context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
  427. {
  428. struct descriptor *d, *tail, *end;
  429. d = ctx->head_descriptor;
  430. tail = ctx->tail_descriptor;
  431. end = ctx->buffer + ctx->buffer_size / sizeof(struct descriptor);
  432. if (d + z <= tail) {
  433. goto has_space;
  434. } else if (d > tail && d + z <= end) {
  435. goto has_space;
  436. } else if (d > tail && ctx->buffer + z <= tail) {
  437. d = ctx->buffer;
  438. goto has_space;
  439. }
  440. return NULL;
  441. has_space:
  442. memset(d, 0, z * sizeof *d);
  443. *d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof *d;
  444. return d;
  445. }
  446. static void context_run(struct context *ctx, u32 extra)
  447. {
  448. struct fw_ohci *ohci = ctx->ohci;
  449. reg_write(ohci, command_ptr(ctx->regs),
  450. le32_to_cpu(ctx->tail_descriptor_last->branch_address));
  451. reg_write(ohci, control_clear(ctx->regs), ~0);
  452. reg_write(ohci, control_set(ctx->regs), CONTEXT_RUN | extra);
  453. flush_writes(ohci);
  454. }
  455. static void context_append(struct context *ctx,
  456. struct descriptor *d, int z, int extra)
  457. {
  458. dma_addr_t d_bus;
  459. d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof *d;
  460. ctx->head_descriptor = d + z + extra;
  461. ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
  462. ctx->prev_descriptor = z == 2 ? d : d + z - 1;
  463. dma_sync_single_for_device(ctx->ohci->card.device, ctx->buffer_bus,
  464. ctx->buffer_size, DMA_TO_DEVICE);
  465. reg_write(ctx->ohci, control_set(ctx->regs), CONTEXT_WAKE);
  466. flush_writes(ctx->ohci);
  467. }
  468. static void context_stop(struct context *ctx)
  469. {
  470. u32 reg;
  471. int i;
  472. reg_write(ctx->ohci, control_clear(ctx->regs), CONTEXT_RUN);
  473. flush_writes(ctx->ohci);
  474. for (i = 0; i < 10; i++) {
  475. reg = reg_read(ctx->ohci, control_set(ctx->regs));
  476. if ((reg & CONTEXT_ACTIVE) == 0)
  477. break;
  478. fw_notify("context_stop: still active (0x%08x)\n", reg);
  479. msleep(1);
  480. }
  481. }
  482. static void
  483. do_packet_callbacks(struct fw_ohci *ohci, struct list_head *list)
  484. {
  485. struct fw_packet *p, *next;
  486. list_for_each_entry_safe(p, next, list, link)
  487. p->callback(p, &ohci->card, p->ack);
  488. }
  489. static void
  490. complete_transmission(struct fw_packet *packet,
  491. int ack, struct list_head *list)
  492. {
  493. list_move_tail(&packet->link, list);
  494. packet->ack = ack;
  495. }
  496. /* This function prepares the first packet in the context queue for
  497. * transmission. Must always be called with the ochi->lock held to
  498. * ensure proper generation handling and locking around packet queue
  499. * manipulation. */
  500. static void
  501. at_context_setup_packet(struct at_context *ctx, struct list_head *list)
  502. {
  503. struct fw_packet *packet;
  504. struct fw_ohci *ohci = ctx->ohci;
  505. int z, tcode;
  506. packet = fw_packet(ctx->list.next);
  507. memset(&ctx->d, 0, sizeof ctx->d);
  508. if (packet->payload_length > 0) {
  509. packet->payload_bus = dma_map_single(ohci->card.device,
  510. packet->payload,
  511. packet->payload_length,
  512. DMA_TO_DEVICE);
  513. if (dma_mapping_error(packet->payload_bus)) {
  514. complete_transmission(packet, RCODE_SEND_ERROR, list);
  515. return;
  516. }
  517. ctx->d.more.control =
  518. cpu_to_le16(descriptor_output_more |
  519. descriptor_key_immediate);
  520. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  521. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  522. ctx->d.last.control =
  523. cpu_to_le16(descriptor_output_last |
  524. descriptor_irq_always |
  525. descriptor_branch_always);
  526. ctx->d.last.req_count = cpu_to_le16(packet->payload_length);
  527. ctx->d.last.data_address = cpu_to_le32(packet->payload_bus);
  528. z = 3;
  529. } else {
  530. ctx->d.more.control =
  531. cpu_to_le16(descriptor_output_last |
  532. descriptor_key_immediate |
  533. descriptor_irq_always |
  534. descriptor_branch_always);
  535. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  536. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  537. z = 2;
  538. }
  539. /* The DMA format for asyncronous link packets is different
  540. * from the IEEE1394 layout, so shift the fields around
  541. * accordingly. If header_length is 8, it's a PHY packet, to
  542. * which we need to prepend an extra quadlet. */
  543. if (packet->header_length > 8) {
  544. ctx->d.header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  545. (packet->speed << 16));
  546. ctx->d.header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  547. (packet->header[0] & 0xffff0000));
  548. ctx->d.header[2] = cpu_to_le32(packet->header[2]);
  549. tcode = (packet->header[0] >> 4) & 0x0f;
  550. if (TCODE_IS_BLOCK_PACKET(tcode))
  551. ctx->d.header[3] = cpu_to_le32(packet->header[3]);
  552. else
  553. ctx->d.header[3] = packet->header[3];
  554. } else {
  555. ctx->d.header[0] =
  556. cpu_to_le32((OHCI1394_phy_tcode << 4) |
  557. (packet->speed << 16));
  558. ctx->d.header[1] = cpu_to_le32(packet->header[0]);
  559. ctx->d.header[2] = cpu_to_le32(packet->header[1]);
  560. ctx->d.more.req_count = cpu_to_le16(12);
  561. }
  562. /* FIXME: Document how the locking works. */
  563. if (ohci->generation == packet->generation) {
  564. reg_write(ctx->ohci, command_ptr(ctx->regs),
  565. ctx->descriptor_bus | z);
  566. reg_write(ctx->ohci, control_set(ctx->regs),
  567. CONTEXT_RUN | CONTEXT_WAKE);
  568. ctx->current_packet = packet;
  569. } else {
  570. /* We dont return error codes from this function; all
  571. * transmission errors are reported through the
  572. * callback. */
  573. complete_transmission(packet, RCODE_GENERATION, list);
  574. }
  575. }
  576. static void at_context_stop(struct at_context *ctx)
  577. {
  578. u32 reg;
  579. reg_write(ctx->ohci, control_clear(ctx->regs), CONTEXT_RUN);
  580. reg = reg_read(ctx->ohci, control_set(ctx->regs));
  581. if (reg & CONTEXT_ACTIVE)
  582. fw_notify("Tried to stop context, but it is still active "
  583. "(0x%08x).\n", reg);
  584. }
  585. static void at_context_tasklet(unsigned long data)
  586. {
  587. struct at_context *ctx = (struct at_context *)data;
  588. struct fw_ohci *ohci = ctx->ohci;
  589. struct fw_packet *packet;
  590. LIST_HEAD(list);
  591. unsigned long flags;
  592. int evt;
  593. spin_lock_irqsave(&ohci->lock, flags);
  594. packet = fw_packet(ctx->list.next);
  595. at_context_stop(ctx);
  596. /* If the head of the list isn't the packet that just got
  597. * transmitted, the packet got cancelled before we finished
  598. * transmitting it. */
  599. if (ctx->current_packet != packet)
  600. goto skip_to_next;
  601. if (packet->payload_length > 0) {
  602. dma_unmap_single(ohci->card.device, packet->payload_bus,
  603. packet->payload_length, DMA_TO_DEVICE);
  604. evt = le16_to_cpu(ctx->d.last.transfer_status) & 0x1f;
  605. packet->timestamp = le16_to_cpu(ctx->d.last.res_count);
  606. }
  607. else {
  608. evt = le16_to_cpu(ctx->d.more.transfer_status) & 0x1f;
  609. packet->timestamp = le16_to_cpu(ctx->d.more.res_count);
  610. }
  611. if (evt < 16) {
  612. switch (evt) {
  613. case OHCI1394_evt_timeout:
  614. /* Async response transmit timed out. */
  615. complete_transmission(packet, RCODE_CANCELLED, &list);
  616. break;
  617. case OHCI1394_evt_flushed:
  618. /* The packet was flushed should give same
  619. * error as when we try to use a stale
  620. * generation count. */
  621. complete_transmission(packet,
  622. RCODE_GENERATION, &list);
  623. break;
  624. case OHCI1394_evt_missing_ack:
  625. /* Using a valid (current) generation count,
  626. * but the node is not on the bus or not
  627. * sending acks. */
  628. complete_transmission(packet, RCODE_NO_ACK, &list);
  629. break;
  630. default:
  631. complete_transmission(packet, RCODE_SEND_ERROR, &list);
  632. break;
  633. }
  634. } else
  635. complete_transmission(packet, evt - 16, &list);
  636. skip_to_next:
  637. /* If more packets are queued, set up the next one. */
  638. if (!list_empty(&ctx->list))
  639. at_context_setup_packet(ctx, &list);
  640. spin_unlock_irqrestore(&ohci->lock, flags);
  641. do_packet_callbacks(ohci, &list);
  642. }
  643. static int
  644. at_context_init(struct at_context *ctx, struct fw_ohci *ohci, u32 regs)
  645. {
  646. INIT_LIST_HEAD(&ctx->list);
  647. ctx->descriptor_bus =
  648. dma_map_single(ohci->card.device, &ctx->d,
  649. sizeof ctx->d, DMA_TO_DEVICE);
  650. if (dma_mapping_error(ctx->descriptor_bus))
  651. return -ENOMEM;
  652. ctx->regs = regs;
  653. ctx->ohci = ohci;
  654. tasklet_init(&ctx->tasklet, at_context_tasklet, (unsigned long)ctx);
  655. return 0;
  656. }
  657. #define header_get_destination(q) (((q) >> 16) & 0xffff)
  658. #define header_get_tcode(q) (((q) >> 4) & 0x0f)
  659. #define header_get_offset_high(q) (((q) >> 0) & 0xffff)
  660. #define header_get_data_length(q) (((q) >> 16) & 0xffff)
  661. #define header_get_extended_tcode(q) (((q) >> 0) & 0xffff)
  662. static void
  663. handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  664. {
  665. struct fw_packet response;
  666. int tcode, length, i;
  667. tcode = header_get_tcode(packet->header[0]);
  668. if (TCODE_IS_BLOCK_PACKET(tcode))
  669. length = header_get_data_length(packet->header[3]);
  670. else
  671. length = 4;
  672. i = csr - CSR_CONFIG_ROM;
  673. if (i + length > CONFIG_ROM_SIZE) {
  674. fw_fill_response(&response, packet->header,
  675. RCODE_ADDRESS_ERROR, NULL, 0);
  676. } else if (!TCODE_IS_READ_REQUEST(tcode)) {
  677. fw_fill_response(&response, packet->header,
  678. RCODE_TYPE_ERROR, NULL, 0);
  679. } else {
  680. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  681. (void *) ohci->config_rom + i, length);
  682. }
  683. fw_core_handle_response(&ohci->card, &response);
  684. }
  685. static void
  686. handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  687. {
  688. struct fw_packet response;
  689. int tcode, length, ext_tcode, sel;
  690. __be32 *payload, lock_old;
  691. u32 lock_arg, lock_data;
  692. tcode = header_get_tcode(packet->header[0]);
  693. length = header_get_data_length(packet->header[3]);
  694. payload = packet->payload;
  695. ext_tcode = header_get_extended_tcode(packet->header[3]);
  696. if (tcode == TCODE_LOCK_REQUEST &&
  697. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  698. lock_arg = be32_to_cpu(payload[0]);
  699. lock_data = be32_to_cpu(payload[1]);
  700. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  701. lock_arg = 0;
  702. lock_data = 0;
  703. } else {
  704. fw_fill_response(&response, packet->header,
  705. RCODE_TYPE_ERROR, NULL, 0);
  706. goto out;
  707. }
  708. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  709. reg_write(ohci, OHCI1394_CSRData, lock_data);
  710. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  711. reg_write(ohci, OHCI1394_CSRControl, sel);
  712. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
  713. lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
  714. else
  715. fw_notify("swap not done yet\n");
  716. fw_fill_response(&response, packet->header,
  717. RCODE_COMPLETE, &lock_old, sizeof lock_old);
  718. out:
  719. fw_core_handle_response(&ohci->card, &response);
  720. }
  721. static void
  722. handle_local_request(struct at_context *ctx, struct fw_packet *packet)
  723. {
  724. u64 offset;
  725. u32 csr;
  726. packet->ack = ACK_PENDING;
  727. packet->callback(packet, &ctx->ohci->card, packet->ack);
  728. offset =
  729. ((unsigned long long)
  730. header_get_offset_high(packet->header[1]) << 32) |
  731. packet->header[2];
  732. csr = offset - CSR_REGISTER_BASE;
  733. /* Handle config rom reads. */
  734. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  735. handle_local_rom(ctx->ohci, packet, csr);
  736. else switch (csr) {
  737. case CSR_BUS_MANAGER_ID:
  738. case CSR_BANDWIDTH_AVAILABLE:
  739. case CSR_CHANNELS_AVAILABLE_HI:
  740. case CSR_CHANNELS_AVAILABLE_LO:
  741. handle_local_lock(ctx->ohci, packet, csr);
  742. break;
  743. default:
  744. if (ctx == &ctx->ohci->at_request_ctx)
  745. fw_core_handle_request(&ctx->ohci->card, packet);
  746. else
  747. fw_core_handle_response(&ctx->ohci->card, packet);
  748. break;
  749. }
  750. }
  751. static void
  752. at_context_transmit(struct at_context *ctx, struct fw_packet *packet)
  753. {
  754. LIST_HEAD(list);
  755. unsigned long flags;
  756. spin_lock_irqsave(&ctx->ohci->lock, flags);
  757. if (header_get_destination(packet->header[0]) == ctx->ohci->node_id &&
  758. ctx->ohci->generation == packet->generation) {
  759. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  760. handle_local_request(ctx, packet);
  761. return;
  762. }
  763. list_add_tail(&packet->link, &ctx->list);
  764. if (ctx->list.next == &packet->link)
  765. at_context_setup_packet(ctx, &list);
  766. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  767. do_packet_callbacks(ctx->ohci, &list);
  768. }
  769. static void bus_reset_tasklet(unsigned long data)
  770. {
  771. struct fw_ohci *ohci = (struct fw_ohci *)data;
  772. int self_id_count, i, j, reg;
  773. int generation, new_generation;
  774. unsigned long flags;
  775. reg = reg_read(ohci, OHCI1394_NodeID);
  776. if (!(reg & OHCI1394_NodeID_idValid)) {
  777. fw_error("node ID not valid, new bus reset in progress\n");
  778. return;
  779. }
  780. ohci->node_id = reg & 0xffff;
  781. /* The count in the SelfIDCount register is the number of
  782. * bytes in the self ID receive buffer. Since we also receive
  783. * the inverted quadlets and a header quadlet, we shift one
  784. * bit extra to get the actual number of self IDs. */
  785. self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
  786. generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  787. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  788. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
  789. fw_error("inconsistent self IDs\n");
  790. ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
  791. }
  792. /* Check the consistency of the self IDs we just read. The
  793. * problem we face is that a new bus reset can start while we
  794. * read out the self IDs from the DMA buffer. If this happens,
  795. * the DMA buffer will be overwritten with new self IDs and we
  796. * will read out inconsistent data. The OHCI specification
  797. * (section 11.2) recommends a technique similar to
  798. * linux/seqlock.h, where we remember the generation of the
  799. * self IDs in the buffer before reading them out and compare
  800. * it to the current generation after reading them out. If
  801. * the two generations match we know we have a consistent set
  802. * of self IDs. */
  803. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  804. if (new_generation != generation) {
  805. fw_notify("recursive bus reset detected, "
  806. "discarding self ids\n");
  807. return;
  808. }
  809. /* FIXME: Document how the locking works. */
  810. spin_lock_irqsave(&ohci->lock, flags);
  811. ohci->generation = generation;
  812. at_context_stop(&ohci->at_request_ctx);
  813. at_context_stop(&ohci->at_response_ctx);
  814. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  815. /* This next bit is unrelated to the AT context stuff but we
  816. * have to do it under the spinlock also. If a new config rom
  817. * was set up before this reset, the old one is now no longer
  818. * in use and we can free it. Update the config rom pointers
  819. * to point to the current config rom and clear the
  820. * next_config_rom pointer so a new udpate can take place. */
  821. if (ohci->next_config_rom != NULL) {
  822. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  823. ohci->config_rom, ohci->config_rom_bus);
  824. ohci->config_rom = ohci->next_config_rom;
  825. ohci->config_rom_bus = ohci->next_config_rom_bus;
  826. ohci->next_config_rom = NULL;
  827. /* Restore config_rom image and manually update
  828. * config_rom registers. Writing the header quadlet
  829. * will indicate that the config rom is ready, so we
  830. * do that last. */
  831. reg_write(ohci, OHCI1394_BusOptions,
  832. be32_to_cpu(ohci->config_rom[2]));
  833. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  834. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  835. }
  836. spin_unlock_irqrestore(&ohci->lock, flags);
  837. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  838. self_id_count, ohci->self_id_buffer);
  839. }
  840. static irqreturn_t irq_handler(int irq, void *data)
  841. {
  842. struct fw_ohci *ohci = data;
  843. u32 event, iso_event;
  844. int i;
  845. event = reg_read(ohci, OHCI1394_IntEventClear);
  846. if (!event)
  847. return IRQ_NONE;
  848. reg_write(ohci, OHCI1394_IntEventClear, event);
  849. if (event & OHCI1394_selfIDComplete)
  850. tasklet_schedule(&ohci->bus_reset_tasklet);
  851. if (event & OHCI1394_RQPkt)
  852. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  853. if (event & OHCI1394_RSPkt)
  854. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  855. if (event & OHCI1394_reqTxComplete)
  856. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  857. if (event & OHCI1394_respTxComplete)
  858. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  859. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  860. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  861. while (iso_event) {
  862. i = ffs(iso_event) - 1;
  863. tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
  864. iso_event &= ~(1 << i);
  865. }
  866. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  867. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  868. while (iso_event) {
  869. i = ffs(iso_event) - 1;
  870. tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
  871. iso_event &= ~(1 << i);
  872. }
  873. return IRQ_HANDLED;
  874. }
  875. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  876. {
  877. struct fw_ohci *ohci = fw_ohci(card);
  878. struct pci_dev *dev = to_pci_dev(card->device);
  879. /* When the link is not yet enabled, the atomic config rom
  880. * update mechanism described below in ohci_set_config_rom()
  881. * is not active. We have to update ConfigRomHeader and
  882. * BusOptions manually, and the write to ConfigROMmap takes
  883. * effect immediately. We tie this to the enabling of the
  884. * link, so we have a valid config rom before enabling - the
  885. * OHCI requires that ConfigROMhdr and BusOptions have valid
  886. * values before enabling.
  887. *
  888. * However, when the ConfigROMmap is written, some controllers
  889. * always read back quadlets 0 and 2 from the config rom to
  890. * the ConfigRomHeader and BusOptions registers on bus reset.
  891. * They shouldn't do that in this initial case where the link
  892. * isn't enabled. This means we have to use the same
  893. * workaround here, setting the bus header to 0 and then write
  894. * the right values in the bus reset tasklet.
  895. */
  896. ohci->next_config_rom =
  897. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  898. &ohci->next_config_rom_bus, GFP_KERNEL);
  899. if (ohci->next_config_rom == NULL)
  900. return -ENOMEM;
  901. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  902. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  903. ohci->next_header = config_rom[0];
  904. ohci->next_config_rom[0] = 0;
  905. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  906. reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
  907. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  908. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  909. if (request_irq(dev->irq, irq_handler,
  910. SA_SHIRQ, ohci_driver_name, ohci)) {
  911. fw_error("Failed to allocate shared interrupt %d.\n",
  912. dev->irq);
  913. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  914. ohci->config_rom, ohci->config_rom_bus);
  915. return -EIO;
  916. }
  917. reg_write(ohci, OHCI1394_HCControlSet,
  918. OHCI1394_HCControl_linkEnable |
  919. OHCI1394_HCControl_BIBimageValid);
  920. flush_writes(ohci);
  921. /* We are ready to go, initiate bus reset to finish the
  922. * initialization. */
  923. fw_core_initiate_bus_reset(&ohci->card, 1);
  924. return 0;
  925. }
  926. static int
  927. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  928. {
  929. struct fw_ohci *ohci;
  930. unsigned long flags;
  931. int retval = 0;
  932. __be32 *next_config_rom;
  933. dma_addr_t next_config_rom_bus;
  934. ohci = fw_ohci(card);
  935. /* When the OHCI controller is enabled, the config rom update
  936. * mechanism is a bit tricky, but easy enough to use. See
  937. * section 5.5.6 in the OHCI specification.
  938. *
  939. * The OHCI controller caches the new config rom address in a
  940. * shadow register (ConfigROMmapNext) and needs a bus reset
  941. * for the changes to take place. When the bus reset is
  942. * detected, the controller loads the new values for the
  943. * ConfigRomHeader and BusOptions registers from the specified
  944. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  945. * shadow register. All automatically and atomically.
  946. *
  947. * Now, there's a twist to this story. The automatic load of
  948. * ConfigRomHeader and BusOptions doesn't honor the
  949. * noByteSwapData bit, so with a be32 config rom, the
  950. * controller will load be32 values in to these registers
  951. * during the atomic update, even on litte endian
  952. * architectures. The workaround we use is to put a 0 in the
  953. * header quadlet; 0 is endian agnostic and means that the
  954. * config rom isn't ready yet. In the bus reset tasklet we
  955. * then set up the real values for the two registers.
  956. *
  957. * We use ohci->lock to avoid racing with the code that sets
  958. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  959. */
  960. next_config_rom =
  961. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  962. &next_config_rom_bus, GFP_KERNEL);
  963. if (next_config_rom == NULL)
  964. return -ENOMEM;
  965. spin_lock_irqsave(&ohci->lock, flags);
  966. if (ohci->next_config_rom == NULL) {
  967. ohci->next_config_rom = next_config_rom;
  968. ohci->next_config_rom_bus = next_config_rom_bus;
  969. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  970. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  971. length * 4);
  972. ohci->next_header = config_rom[0];
  973. ohci->next_config_rom[0] = 0;
  974. reg_write(ohci, OHCI1394_ConfigROMmap,
  975. ohci->next_config_rom_bus);
  976. } else {
  977. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  978. next_config_rom, next_config_rom_bus);
  979. retval = -EBUSY;
  980. }
  981. spin_unlock_irqrestore(&ohci->lock, flags);
  982. /* Now initiate a bus reset to have the changes take
  983. * effect. We clean up the old config rom memory and DMA
  984. * mappings in the bus reset tasklet, since the OHCI
  985. * controller could need to access it before the bus reset
  986. * takes effect. */
  987. if (retval == 0)
  988. fw_core_initiate_bus_reset(&ohci->card, 1);
  989. return retval;
  990. }
  991. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  992. {
  993. struct fw_ohci *ohci = fw_ohci(card);
  994. at_context_transmit(&ohci->at_request_ctx, packet);
  995. }
  996. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  997. {
  998. struct fw_ohci *ohci = fw_ohci(card);
  999. at_context_transmit(&ohci->at_response_ctx, packet);
  1000. }
  1001. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  1002. {
  1003. struct fw_ohci *ohci = fw_ohci(card);
  1004. LIST_HEAD(list);
  1005. unsigned long flags;
  1006. spin_lock_irqsave(&ohci->lock, flags);
  1007. if (packet->ack == 0) {
  1008. fw_notify("cancelling packet %p (header[0]=%08x)\n",
  1009. packet, packet->header[0]);
  1010. complete_transmission(packet, RCODE_CANCELLED, &list);
  1011. }
  1012. spin_unlock_irqrestore(&ohci->lock, flags);
  1013. do_packet_callbacks(ohci, &list);
  1014. /* Return success if we actually cancelled something. */
  1015. return list_empty(&list) ? -ENOENT : 0;
  1016. }
  1017. static int
  1018. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  1019. {
  1020. struct fw_ohci *ohci = fw_ohci(card);
  1021. unsigned long flags;
  1022. int n, retval = 0;
  1023. /* FIXME: Make sure this bitmask is cleared when we clear the busReset
  1024. * interrupt bit. Clear physReqResourceAllBuses on bus reset. */
  1025. spin_lock_irqsave(&ohci->lock, flags);
  1026. if (ohci->generation != generation) {
  1027. retval = -ESTALE;
  1028. goto out;
  1029. }
  1030. /* NOTE, if the node ID contains a non-local bus ID, physical DMA is
  1031. * enabled for _all_ nodes on remote buses. */
  1032. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  1033. if (n < 32)
  1034. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  1035. else
  1036. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  1037. flush_writes(ohci);
  1038. out:
  1039. spin_unlock_irqrestore(&ohci->lock, flags);
  1040. return retval;
  1041. }
  1042. static int handle_ir_bufferfill_packet(struct context *context,
  1043. struct descriptor *d,
  1044. struct descriptor *last)
  1045. {
  1046. struct iso_context *ctx =
  1047. container_of(context, struct iso_context, context);
  1048. if (d->res_count > 0)
  1049. return 0;
  1050. if (le16_to_cpu(last->control) & descriptor_irq_always)
  1051. ctx->base.callback(&ctx->base,
  1052. le16_to_cpu(last->res_count),
  1053. 0, NULL, ctx->base.callback_data);
  1054. return 1;
  1055. }
  1056. static int handle_ir_dualbuffer_packet(struct context *context,
  1057. struct descriptor *d,
  1058. struct descriptor *last)
  1059. {
  1060. struct iso_context *ctx =
  1061. container_of(context, struct iso_context, context);
  1062. struct db_descriptor *db = (struct db_descriptor *) d;
  1063. size_t header_length;
  1064. if (db->first_res_count > 0 && db->second_res_count > 0)
  1065. /* This descriptor isn't done yet, stop iteration. */
  1066. return 0;
  1067. header_length = db->first_req_count - db->first_res_count;
  1068. if (ctx->header_length + header_length <= PAGE_SIZE)
  1069. memcpy(ctx->header + ctx->header_length, db + 1, header_length);
  1070. ctx->header_length += header_length;
  1071. if (le16_to_cpu(db->control) & descriptor_irq_always) {
  1072. ctx->base.callback(&ctx->base, 0,
  1073. ctx->header_length, ctx->header,
  1074. ctx->base.callback_data);
  1075. ctx->header_length = 0;
  1076. }
  1077. return 1;
  1078. }
  1079. #define ISO_BUFFER_SIZE (64 * 1024)
  1080. static int handle_it_packet(struct context *context,
  1081. struct descriptor *d,
  1082. struct descriptor *last)
  1083. {
  1084. struct iso_context *ctx =
  1085. container_of(context, struct iso_context, context);
  1086. if (last->transfer_status == 0)
  1087. /* This descriptor isn't done yet, stop iteration. */
  1088. return 0;
  1089. if (le16_to_cpu(last->control) & descriptor_irq_always)
  1090. ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
  1091. 0, NULL, ctx->base.callback_data);
  1092. return 1;
  1093. }
  1094. static struct fw_iso_context *
  1095. ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
  1096. {
  1097. struct fw_ohci *ohci = fw_ohci(card);
  1098. struct iso_context *ctx, *list;
  1099. descriptor_callback_t callback;
  1100. u32 *mask, regs;
  1101. unsigned long flags;
  1102. int index, retval = -ENOMEM;
  1103. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  1104. mask = &ohci->it_context_mask;
  1105. list = ohci->it_context_list;
  1106. callback = handle_it_packet;
  1107. } else {
  1108. mask = &ohci->ir_context_mask;
  1109. list = ohci->ir_context_list;
  1110. if (header_size > 0)
  1111. callback = handle_ir_dualbuffer_packet;
  1112. else
  1113. callback = handle_ir_bufferfill_packet;
  1114. }
  1115. if (callback == handle_ir_dualbuffer_packet &&
  1116. ohci->version < OHCI_VERSION_1_1)
  1117. return ERR_PTR(-EINVAL);
  1118. spin_lock_irqsave(&ohci->lock, flags);
  1119. index = ffs(*mask) - 1;
  1120. if (index >= 0)
  1121. *mask &= ~(1 << index);
  1122. spin_unlock_irqrestore(&ohci->lock, flags);
  1123. if (index < 0)
  1124. return ERR_PTR(-EBUSY);
  1125. if (type == FW_ISO_CONTEXT_TRANSMIT)
  1126. regs = OHCI1394_IsoXmitContextBase(index);
  1127. else
  1128. regs = OHCI1394_IsoRcvContextBase(index);
  1129. ctx = &list[index];
  1130. memset(ctx, 0, sizeof *ctx);
  1131. ctx->header_length = 0;
  1132. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  1133. if (ctx->header == NULL)
  1134. goto out;
  1135. retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
  1136. regs, callback);
  1137. if (retval < 0)
  1138. goto out_with_header;
  1139. return &ctx->base;
  1140. out_with_header:
  1141. free_page((unsigned long)ctx->header);
  1142. out:
  1143. spin_lock_irqsave(&ohci->lock, flags);
  1144. *mask |= 1 << index;
  1145. spin_unlock_irqrestore(&ohci->lock, flags);
  1146. return ERR_PTR(retval);
  1147. }
  1148. static int ohci_start_iso(struct fw_iso_context *base, s32 cycle)
  1149. {
  1150. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1151. struct fw_ohci *ohci = ctx->context.ohci;
  1152. u32 cycle_match = 0, mode;
  1153. int index;
  1154. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1155. index = ctx - ohci->it_context_list;
  1156. if (cycle > 0)
  1157. cycle_match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  1158. (cycle & 0x7fff) << 16;
  1159. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  1160. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  1161. context_run(&ctx->context, cycle_match);
  1162. } else {
  1163. index = ctx - ohci->ir_context_list;
  1164. if (ctx->base.header_size > 0)
  1165. mode = IR_CONTEXT_DUAL_BUFFER_MODE;
  1166. else
  1167. mode = IR_CONTEXT_BUFFER_FILL;
  1168. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  1169. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  1170. reg_write(ohci, context_match(ctx->context.regs),
  1171. 0xf0000000 | ctx->base.channel);
  1172. context_run(&ctx->context, mode);
  1173. }
  1174. return 0;
  1175. }
  1176. static int ohci_stop_iso(struct fw_iso_context *base)
  1177. {
  1178. struct fw_ohci *ohci = fw_ohci(base->card);
  1179. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1180. int index;
  1181. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1182. index = ctx - ohci->it_context_list;
  1183. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  1184. } else {
  1185. index = ctx - ohci->ir_context_list;
  1186. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  1187. }
  1188. flush_writes(ohci);
  1189. context_stop(&ctx->context);
  1190. return 0;
  1191. }
  1192. static void ohci_free_iso_context(struct fw_iso_context *base)
  1193. {
  1194. struct fw_ohci *ohci = fw_ohci(base->card);
  1195. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1196. unsigned long flags;
  1197. int index;
  1198. ohci_stop_iso(base);
  1199. context_release(&ctx->context);
  1200. free_page((unsigned long)ctx->header);
  1201. spin_lock_irqsave(&ohci->lock, flags);
  1202. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1203. index = ctx - ohci->it_context_list;
  1204. ohci->it_context_mask |= 1 << index;
  1205. } else {
  1206. index = ctx - ohci->ir_context_list;
  1207. ohci->ir_context_mask |= 1 << index;
  1208. }
  1209. spin_unlock_irqrestore(&ohci->lock, flags);
  1210. }
  1211. static int
  1212. ohci_queue_iso_transmit(struct fw_iso_context *base,
  1213. struct fw_iso_packet *packet,
  1214. struct fw_iso_buffer *buffer,
  1215. unsigned long payload)
  1216. {
  1217. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1218. struct descriptor *d, *last, *pd;
  1219. struct fw_iso_packet *p;
  1220. __le32 *header;
  1221. dma_addr_t d_bus, page_bus;
  1222. u32 z, header_z, payload_z, irq;
  1223. u32 payload_index, payload_end_index, next_page_index;
  1224. int page, end_page, i, length, offset;
  1225. /* FIXME: Cycle lost behavior should be configurable: lose
  1226. * packet, retransmit or terminate.. */
  1227. p = packet;
  1228. payload_index = payload;
  1229. if (p->skip)
  1230. z = 1;
  1231. else
  1232. z = 2;
  1233. if (p->header_length > 0)
  1234. z++;
  1235. /* Determine the first page the payload isn't contained in. */
  1236. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  1237. if (p->payload_length > 0)
  1238. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  1239. else
  1240. payload_z = 0;
  1241. z += payload_z;
  1242. /* Get header size in number of descriptors. */
  1243. header_z = DIV_ROUND_UP(p->header_length, sizeof *d);
  1244. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  1245. if (d == NULL)
  1246. return -ENOMEM;
  1247. if (!p->skip) {
  1248. d[0].control = cpu_to_le16(descriptor_key_immediate);
  1249. d[0].req_count = cpu_to_le16(8);
  1250. header = (__le32 *) &d[1];
  1251. header[0] = cpu_to_le32(it_header_sy(p->sy) |
  1252. it_header_tag(p->tag) |
  1253. it_header_tcode(TCODE_STREAM_DATA) |
  1254. it_header_channel(ctx->base.channel) |
  1255. it_header_speed(ctx->base.speed));
  1256. header[1] =
  1257. cpu_to_le32(it_header_data_length(p->header_length +
  1258. p->payload_length));
  1259. }
  1260. if (p->header_length > 0) {
  1261. d[2].req_count = cpu_to_le16(p->header_length);
  1262. d[2].data_address = cpu_to_le32(d_bus + z * sizeof *d);
  1263. memcpy(&d[z], p->header, p->header_length);
  1264. }
  1265. pd = d + z - payload_z;
  1266. payload_end_index = payload_index + p->payload_length;
  1267. for (i = 0; i < payload_z; i++) {
  1268. page = payload_index >> PAGE_SHIFT;
  1269. offset = payload_index & ~PAGE_MASK;
  1270. next_page_index = (page + 1) << PAGE_SHIFT;
  1271. length =
  1272. min(next_page_index, payload_end_index) - payload_index;
  1273. pd[i].req_count = cpu_to_le16(length);
  1274. page_bus = page_private(buffer->pages[page]);
  1275. pd[i].data_address = cpu_to_le32(page_bus + offset);
  1276. payload_index += length;
  1277. }
  1278. if (p->interrupt)
  1279. irq = descriptor_irq_always;
  1280. else
  1281. irq = descriptor_no_irq;
  1282. last = z == 2 ? d : d + z - 1;
  1283. last->control |= cpu_to_le16(descriptor_output_last |
  1284. descriptor_status |
  1285. descriptor_branch_always |
  1286. irq);
  1287. context_append(&ctx->context, d, z, header_z);
  1288. return 0;
  1289. }
  1290. static int
  1291. ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
  1292. struct fw_iso_packet *packet,
  1293. struct fw_iso_buffer *buffer,
  1294. unsigned long payload)
  1295. {
  1296. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1297. struct db_descriptor *db = NULL;
  1298. struct descriptor *d;
  1299. struct fw_iso_packet *p;
  1300. dma_addr_t d_bus, page_bus;
  1301. u32 z, header_z, length, rest;
  1302. int page, offset;
  1303. /* FIXME: Cycle lost behavior should be configurable: lose
  1304. * packet, retransmit or terminate.. */
  1305. p = packet;
  1306. z = 2;
  1307. /* Get header size in number of descriptors. */
  1308. header_z = DIV_ROUND_UP(p->header_length, sizeof *d);
  1309. page = payload >> PAGE_SHIFT;
  1310. offset = payload & ~PAGE_MASK;
  1311. rest = p->payload_length;
  1312. /* FIXME: OHCI 1.0 doesn't support dual buffer receive */
  1313. /* FIXME: handle descriptor_wait */
  1314. /* FIXME: make packet-per-buffer/dual-buffer a context option */
  1315. while (rest > 0) {
  1316. d = context_get_descriptors(&ctx->context,
  1317. z + header_z, &d_bus);
  1318. if (d == NULL)
  1319. return -ENOMEM;
  1320. db = (struct db_descriptor *) d;
  1321. db->control = cpu_to_le16(descriptor_status |
  1322. descriptor_branch_always);
  1323. db->first_size = cpu_to_le16(ctx->base.header_size);
  1324. db->first_req_count = cpu_to_le16(p->header_length);
  1325. db->first_res_count = db->first_req_count;
  1326. db->first_buffer = cpu_to_le32(d_bus + sizeof *db);
  1327. if (offset + rest < PAGE_SIZE)
  1328. length = rest;
  1329. else
  1330. length = PAGE_SIZE - offset;
  1331. db->second_req_count = cpu_to_le16(length);
  1332. db->second_res_count = db->second_req_count;
  1333. page_bus = page_private(buffer->pages[page]);
  1334. db->second_buffer = cpu_to_le32(page_bus + offset);
  1335. if (p->interrupt && length == rest)
  1336. db->control |= cpu_to_le16(descriptor_irq_always);
  1337. context_append(&ctx->context, d, z, header_z);
  1338. offset = (offset + length) & ~PAGE_MASK;
  1339. rest -= length;
  1340. page++;
  1341. }
  1342. return 0;
  1343. }
  1344. static int
  1345. ohci_queue_iso_receive_bufferfill(struct fw_iso_context *base,
  1346. struct fw_iso_packet *packet,
  1347. struct fw_iso_buffer *buffer,
  1348. unsigned long payload)
  1349. {
  1350. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1351. struct descriptor *d = NULL;
  1352. dma_addr_t d_bus, page_bus;
  1353. u32 length, rest;
  1354. int page, offset;
  1355. page = payload >> PAGE_SHIFT;
  1356. offset = payload & ~PAGE_MASK;
  1357. rest = packet->payload_length;
  1358. while (rest > 0) {
  1359. d = context_get_descriptors(&ctx->context, 1, &d_bus);
  1360. if (d == NULL)
  1361. return -ENOMEM;
  1362. d->control = cpu_to_le16(descriptor_input_more |
  1363. descriptor_status |
  1364. descriptor_branch_always);
  1365. if (offset + rest < PAGE_SIZE)
  1366. length = rest;
  1367. else
  1368. length = PAGE_SIZE - offset;
  1369. page_bus = page_private(buffer->pages[page]);
  1370. d->data_address = cpu_to_le32(page_bus + offset);
  1371. d->req_count = cpu_to_le16(length);
  1372. d->res_count = cpu_to_le16(length);
  1373. if (packet->interrupt && length == rest)
  1374. d->control |= cpu_to_le16(descriptor_irq_always);
  1375. context_append(&ctx->context, d, 1, 0);
  1376. offset = (offset + length) & ~PAGE_MASK;
  1377. rest -= length;
  1378. page++;
  1379. }
  1380. return 0;
  1381. }
  1382. static int
  1383. ohci_queue_iso(struct fw_iso_context *base,
  1384. struct fw_iso_packet *packet,
  1385. struct fw_iso_buffer *buffer,
  1386. unsigned long payload)
  1387. {
  1388. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1389. if (base->type == FW_ISO_CONTEXT_TRANSMIT)
  1390. return ohci_queue_iso_transmit(base, packet, buffer, payload);
  1391. else if (base->header_size == 0)
  1392. return ohci_queue_iso_receive_bufferfill(base, packet,
  1393. buffer, payload);
  1394. else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
  1395. return ohci_queue_iso_receive_dualbuffer(base, packet,
  1396. buffer, payload);
  1397. else
  1398. /* FIXME: Implement fallback for OHCI 1.0 controllers. */
  1399. return -EINVAL;
  1400. }
  1401. static const struct fw_card_driver ohci_driver = {
  1402. .name = ohci_driver_name,
  1403. .enable = ohci_enable,
  1404. .update_phy_reg = ohci_update_phy_reg,
  1405. .set_config_rom = ohci_set_config_rom,
  1406. .send_request = ohci_send_request,
  1407. .send_response = ohci_send_response,
  1408. .cancel_packet = ohci_cancel_packet,
  1409. .enable_phys_dma = ohci_enable_phys_dma,
  1410. .allocate_iso_context = ohci_allocate_iso_context,
  1411. .free_iso_context = ohci_free_iso_context,
  1412. .queue_iso = ohci_queue_iso,
  1413. .start_iso = ohci_start_iso,
  1414. .stop_iso = ohci_stop_iso,
  1415. };
  1416. static int software_reset(struct fw_ohci *ohci)
  1417. {
  1418. int i;
  1419. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1420. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  1421. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  1422. OHCI1394_HCControl_softReset) == 0)
  1423. return 0;
  1424. msleep(1);
  1425. }
  1426. return -EBUSY;
  1427. }
  1428. /* ---------- pci subsystem interface ---------- */
  1429. enum {
  1430. CLEANUP_SELF_ID,
  1431. CLEANUP_REGISTERS,
  1432. CLEANUP_IOMEM,
  1433. CLEANUP_DISABLE,
  1434. CLEANUP_PUT_CARD,
  1435. };
  1436. static int cleanup(struct fw_ohci *ohci, int stage, int code)
  1437. {
  1438. struct pci_dev *dev = to_pci_dev(ohci->card.device);
  1439. switch (stage) {
  1440. case CLEANUP_SELF_ID:
  1441. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1442. ohci->self_id_cpu, ohci->self_id_bus);
  1443. case CLEANUP_REGISTERS:
  1444. kfree(ohci->it_context_list);
  1445. kfree(ohci->ir_context_list);
  1446. pci_iounmap(dev, ohci->registers);
  1447. case CLEANUP_IOMEM:
  1448. pci_release_region(dev, 0);
  1449. case CLEANUP_DISABLE:
  1450. pci_disable_device(dev);
  1451. case CLEANUP_PUT_CARD:
  1452. fw_card_put(&ohci->card);
  1453. }
  1454. return code;
  1455. }
  1456. static int __devinit
  1457. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  1458. {
  1459. struct fw_ohci *ohci;
  1460. u32 bus_options, max_receive, link_speed;
  1461. u64 guid;
  1462. int error_code;
  1463. size_t size;
  1464. ohci = kzalloc(sizeof *ohci, GFP_KERNEL);
  1465. if (ohci == NULL) {
  1466. fw_error("Could not malloc fw_ohci data.\n");
  1467. return -ENOMEM;
  1468. }
  1469. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1470. if (pci_enable_device(dev)) {
  1471. fw_error("Failed to enable OHCI hardware.\n");
  1472. return cleanup(ohci, CLEANUP_PUT_CARD, -ENODEV);
  1473. }
  1474. pci_set_master(dev);
  1475. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1476. pci_set_drvdata(dev, ohci);
  1477. spin_lock_init(&ohci->lock);
  1478. tasklet_init(&ohci->bus_reset_tasklet,
  1479. bus_reset_tasklet, (unsigned long)ohci);
  1480. if (pci_request_region(dev, 0, ohci_driver_name)) {
  1481. fw_error("MMIO resource unavailable\n");
  1482. return cleanup(ohci, CLEANUP_DISABLE, -EBUSY);
  1483. }
  1484. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1485. if (ohci->registers == NULL) {
  1486. fw_error("Failed to remap registers\n");
  1487. return cleanup(ohci, CLEANUP_IOMEM, -ENXIO);
  1488. }
  1489. if (software_reset(ohci)) {
  1490. fw_error("Failed to reset ohci card.\n");
  1491. return cleanup(ohci, CLEANUP_REGISTERS, -EBUSY);
  1492. }
  1493. /* Now enable LPS, which we need in order to start accessing
  1494. * most of the registers. In fact, on some cards (ALI M5251),
  1495. * accessing registers in the SClk domain without LPS enabled
  1496. * will lock up the machine. Wait 50msec to make sure we have
  1497. * full link enabled. */
  1498. reg_write(ohci, OHCI1394_HCControlSet,
  1499. OHCI1394_HCControl_LPS |
  1500. OHCI1394_HCControl_postedWriteEnable);
  1501. flush_writes(ohci);
  1502. msleep(50);
  1503. reg_write(ohci, OHCI1394_HCControlClear,
  1504. OHCI1394_HCControl_noByteSwapData);
  1505. reg_write(ohci, OHCI1394_LinkControlSet,
  1506. OHCI1394_LinkControl_rcvSelfID |
  1507. OHCI1394_LinkControl_cycleTimerEnable |
  1508. OHCI1394_LinkControl_cycleMaster);
  1509. ar_context_init(&ohci->ar_request_ctx, ohci,
  1510. OHCI1394_AsReqRcvContextControlSet);
  1511. ar_context_init(&ohci->ar_response_ctx, ohci,
  1512. OHCI1394_AsRspRcvContextControlSet);
  1513. at_context_init(&ohci->at_request_ctx, ohci,
  1514. OHCI1394_AsReqTrContextControlSet);
  1515. at_context_init(&ohci->at_response_ctx, ohci,
  1516. OHCI1394_AsRspTrContextControlSet);
  1517. reg_write(ohci, OHCI1394_ATRetries,
  1518. OHCI1394_MAX_AT_REQ_RETRIES |
  1519. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1520. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1521. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1522. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1523. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1524. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1525. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1526. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1527. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1528. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1529. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1530. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1531. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1532. fw_error("Out of memory for it/ir contexts.\n");
  1533. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1534. }
  1535. /* self-id dma buffer allocation */
  1536. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1537. SELF_ID_BUF_SIZE,
  1538. &ohci->self_id_bus,
  1539. GFP_KERNEL);
  1540. if (ohci->self_id_cpu == NULL) {
  1541. fw_error("Out of memory for self ID buffer.\n");
  1542. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1543. }
  1544. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1545. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1546. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1547. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1548. reg_write(ohci, OHCI1394_IntMaskSet,
  1549. OHCI1394_selfIDComplete |
  1550. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1551. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1552. OHCI1394_isochRx | OHCI1394_isochTx |
  1553. OHCI1394_masterIntEnable);
  1554. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1555. max_receive = (bus_options >> 12) & 0xf;
  1556. link_speed = bus_options & 0x7;
  1557. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1558. reg_read(ohci, OHCI1394_GUIDLo);
  1559. error_code = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1560. if (error_code < 0)
  1561. return cleanup(ohci, CLEANUP_SELF_ID, error_code);
  1562. ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  1563. fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
  1564. dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
  1565. return 0;
  1566. }
  1567. static void pci_remove(struct pci_dev *dev)
  1568. {
  1569. struct fw_ohci *ohci;
  1570. ohci = pci_get_drvdata(dev);
  1571. reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_masterIntEnable);
  1572. fw_core_remove_card(&ohci->card);
  1573. /* FIXME: Fail all pending packets here, now that the upper
  1574. * layers can't queue any more. */
  1575. software_reset(ohci);
  1576. free_irq(dev->irq, ohci);
  1577. cleanup(ohci, CLEANUP_SELF_ID, 0);
  1578. fw_notify("Removed fw-ohci device.\n");
  1579. }
  1580. static struct pci_device_id pci_table[] = {
  1581. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  1582. { }
  1583. };
  1584. MODULE_DEVICE_TABLE(pci, pci_table);
  1585. static struct pci_driver fw_ohci_pci_driver = {
  1586. .name = ohci_driver_name,
  1587. .id_table = pci_table,
  1588. .probe = pci_probe,
  1589. .remove = pci_remove,
  1590. };
  1591. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1592. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  1593. MODULE_LICENSE("GPL");
  1594. static int __init fw_ohci_init(void)
  1595. {
  1596. return pci_register_driver(&fw_ohci_pci_driver);
  1597. }
  1598. static void __exit fw_ohci_cleanup(void)
  1599. {
  1600. pci_unregister_driver(&fw_ohci_pci_driver);
  1601. }
  1602. module_init(fw_ohci_init);
  1603. module_exit(fw_ohci_cleanup);