fw-ohci.c 44 KB

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