fw-ohci.c 45 KB

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