fw-ohci.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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 (packet->payload_bus == 0) {
  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 (ctx->descriptor_bus == 0)
  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_IsoRecvIntEventSet);
  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_IsoXmitIntEventSet);
  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. spin_lock_irqsave(&ohci->lock, flags);
  955. *mask |= 1 << index;
  956. spin_unlock_irqrestore(&ohci->lock, flags);
  957. return ERR_PTR(-ENOMEM);
  958. }
  959. ctx->buffer_bus =
  960. dma_map_single(card->device, ctx->buffer,
  961. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  962. ctx->head_descriptor = ctx->buffer;
  963. ctx->prev_descriptor = ctx->buffer;
  964. ctx->tail_descriptor = ctx->buffer;
  965. ctx->tail_descriptor_last = ctx->buffer;
  966. /* We put a dummy descriptor in the buffer that has a NULL
  967. * branch address and looks like it's been sent. That way we
  968. * have a descriptor to append DMA programs to. Also, the
  969. * ring buffer invariant is that it always has at least one
  970. * element so that head == tail means buffer full. */
  971. memset(ctx->head_descriptor, 0, sizeof *ctx->head_descriptor);
  972. ctx->head_descriptor->control = cpu_to_le16(descriptor_output_last);
  973. ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
  974. ctx->head_descriptor++;
  975. return &ctx->base;
  976. }
  977. static int ohci_send_iso(struct fw_iso_context *base, s32 cycle)
  978. {
  979. struct iso_context *ctx = (struct iso_context *)base;
  980. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  981. u32 cycle_match = 0;
  982. int index;
  983. index = ctx - ohci->it_context_list;
  984. if (cycle > 0)
  985. cycle_match = CONTEXT_CYCLE_MATCH_ENABLE |
  986. (cycle & 0x7fff) << 16;
  987. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  988. reg_write(ohci, OHCI1394_IsoXmitCommandPtr(index),
  989. le32_to_cpu(ctx->tail_descriptor_last->branch_address));
  990. reg_write(ohci, OHCI1394_IsoXmitContextControlClear(index), ~0);
  991. reg_write(ohci, OHCI1394_IsoXmitContextControlSet(index),
  992. CONTEXT_RUN | cycle_match);
  993. flush_writes(ohci);
  994. return 0;
  995. }
  996. static void ohci_free_iso_context(struct fw_iso_context *base)
  997. {
  998. struct fw_ohci *ohci = fw_ohci(base->card);
  999. struct iso_context *ctx = (struct iso_context *)base;
  1000. unsigned long flags;
  1001. int index;
  1002. flush_iso_context(ctx);
  1003. spin_lock_irqsave(&ohci->lock, flags);
  1004. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1005. index = ctx - ohci->it_context_list;
  1006. reg_write(ohci, OHCI1394_IsoXmitContextControlClear(index), ~0);
  1007. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  1008. ohci->it_context_mask |= 1 << index;
  1009. } else {
  1010. index = ctx - ohci->ir_context_list;
  1011. reg_write(ohci, OHCI1394_IsoRcvContextControlClear(index), ~0);
  1012. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  1013. ohci->ir_context_mask |= 1 << index;
  1014. }
  1015. flush_writes(ohci);
  1016. dma_unmap_single(ohci->card.device, ctx->buffer_bus,
  1017. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  1018. spin_unlock_irqrestore(&ohci->lock, flags);
  1019. }
  1020. static int
  1021. ohci_queue_iso(struct fw_iso_context *base,
  1022. struct fw_iso_packet *packet, void *payload)
  1023. {
  1024. struct iso_context *ctx = (struct iso_context *)base;
  1025. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  1026. struct descriptor *d, *end, *last, *tail, *pd;
  1027. struct fw_iso_packet *p;
  1028. __le32 *header;
  1029. dma_addr_t d_bus;
  1030. u32 z, header_z, payload_z, irq;
  1031. u32 payload_index, payload_end_index, next_page_index;
  1032. int index, page, end_page, i, length, offset;
  1033. /* FIXME: Cycle lost behavior should be configurable: lose
  1034. * packet, retransmit or terminate.. */
  1035. p = packet;
  1036. payload_index = payload - ctx->base.buffer;
  1037. d = ctx->head_descriptor;
  1038. tail = ctx->tail_descriptor;
  1039. end = ctx->buffer + ISO_BUFFER_SIZE / sizeof(struct descriptor);
  1040. if (p->skip)
  1041. z = 1;
  1042. else
  1043. z = 2;
  1044. if (p->header_length > 0)
  1045. z++;
  1046. /* Determine the first page the payload isn't contained in. */
  1047. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  1048. if (p->payload_length > 0)
  1049. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  1050. else
  1051. payload_z = 0;
  1052. z += payload_z;
  1053. /* Get header size in number of descriptors. */
  1054. header_z = DIV_ROUND_UP(p->header_length, sizeof *d);
  1055. if (d + z + header_z <= tail) {
  1056. goto has_space;
  1057. } else if (d > tail && d + z + header_z <= end) {
  1058. goto has_space;
  1059. } else if (d > tail && ctx->buffer + z + header_z <= tail) {
  1060. d = ctx->buffer;
  1061. goto has_space;
  1062. }
  1063. /* No space in buffer */
  1064. return -1;
  1065. has_space:
  1066. memset(d, 0, (z + header_z) * sizeof *d);
  1067. d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof *d;
  1068. if (!p->skip) {
  1069. d[0].control = cpu_to_le16(descriptor_key_immediate);
  1070. d[0].req_count = cpu_to_le16(8);
  1071. header = (__le32 *) &d[1];
  1072. header[0] = cpu_to_le32(it_header_sy(p->sy) |
  1073. it_header_tag(p->tag) |
  1074. it_header_tcode(TCODE_STREAM_DATA) |
  1075. it_header_channel(ctx->base.channel) |
  1076. it_header_speed(ctx->base.speed));
  1077. header[1] =
  1078. cpu_to_le32(it_header_data_length(p->header_length +
  1079. p->payload_length));
  1080. }
  1081. if (p->header_length > 0) {
  1082. d[2].req_count = cpu_to_le16(p->header_length);
  1083. d[2].data_address = cpu_to_le32(d_bus + z * sizeof *d);
  1084. memcpy(&d[z], p->header, p->header_length);
  1085. }
  1086. pd = d + z - payload_z;
  1087. payload_end_index = payload_index + p->payload_length;
  1088. for (i = 0; i < payload_z; i++) {
  1089. page = payload_index >> PAGE_SHIFT;
  1090. offset = payload_index & ~PAGE_MASK;
  1091. next_page_index = (page + 1) << PAGE_SHIFT;
  1092. length =
  1093. min(next_page_index, payload_end_index) - payload_index;
  1094. pd[i].req_count = cpu_to_le16(length);
  1095. pd[i].data_address = cpu_to_le32(ctx->base.pages[page] + offset);
  1096. payload_index += length;
  1097. }
  1098. if (z == 2)
  1099. last = d;
  1100. else
  1101. last = d + z - 1;
  1102. if (p->interrupt)
  1103. irq = descriptor_irq_always;
  1104. else
  1105. irq = descriptor_no_irq;
  1106. last->control = cpu_to_le16(descriptor_output_last |
  1107. descriptor_status |
  1108. descriptor_branch_always |
  1109. irq);
  1110. dma_sync_single_for_device(ohci->card.device, ctx->buffer_bus,
  1111. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  1112. ctx->head_descriptor = d + z + header_z;
  1113. ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
  1114. ctx->prev_descriptor = last;
  1115. index = ctx - ohci->it_context_list;
  1116. reg_write(ohci, OHCI1394_IsoXmitContextControlSet(index), CONTEXT_WAKE);
  1117. flush_writes(ohci);
  1118. return 0;
  1119. }
  1120. static const struct fw_card_driver ohci_driver = {
  1121. .name = ohci_driver_name,
  1122. .enable = ohci_enable,
  1123. .update_phy_reg = ohci_update_phy_reg,
  1124. .set_config_rom = ohci_set_config_rom,
  1125. .send_request = ohci_send_request,
  1126. .send_response = ohci_send_response,
  1127. .cancel_packet = ohci_cancel_packet,
  1128. .enable_phys_dma = ohci_enable_phys_dma,
  1129. .allocate_iso_context = ohci_allocate_iso_context,
  1130. .free_iso_context = ohci_free_iso_context,
  1131. .queue_iso = ohci_queue_iso,
  1132. .send_iso = ohci_send_iso,
  1133. };
  1134. static int software_reset(struct fw_ohci *ohci)
  1135. {
  1136. int i;
  1137. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1138. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  1139. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  1140. OHCI1394_HCControl_softReset) == 0)
  1141. return 0;
  1142. msleep(1);
  1143. }
  1144. return -EBUSY;
  1145. }
  1146. /* ---------- pci subsystem interface ---------- */
  1147. enum {
  1148. CLEANUP_SELF_ID,
  1149. CLEANUP_REGISTERS,
  1150. CLEANUP_IOMEM,
  1151. CLEANUP_DISABLE,
  1152. CLEANUP_PUT_CARD,
  1153. };
  1154. static int cleanup(struct fw_ohci *ohci, int stage, int code)
  1155. {
  1156. struct pci_dev *dev = to_pci_dev(ohci->card.device);
  1157. switch (stage) {
  1158. case CLEANUP_SELF_ID:
  1159. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1160. ohci->self_id_cpu, ohci->self_id_bus);
  1161. case CLEANUP_REGISTERS:
  1162. kfree(ohci->it_context_list);
  1163. kfree(ohci->ir_context_list);
  1164. pci_iounmap(dev, ohci->registers);
  1165. case CLEANUP_IOMEM:
  1166. pci_release_region(dev, 0);
  1167. case CLEANUP_DISABLE:
  1168. pci_disable_device(dev);
  1169. case CLEANUP_PUT_CARD:
  1170. fw_card_put(&ohci->card);
  1171. }
  1172. return code;
  1173. }
  1174. static int __devinit
  1175. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  1176. {
  1177. struct fw_ohci *ohci;
  1178. u32 bus_options, max_receive, link_speed;
  1179. u64 guid;
  1180. int error_code;
  1181. size_t size;
  1182. ohci = kzalloc(sizeof *ohci, GFP_KERNEL);
  1183. if (ohci == NULL) {
  1184. fw_error("Could not malloc fw_ohci data.\n");
  1185. return -ENOMEM;
  1186. }
  1187. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1188. if (pci_enable_device(dev)) {
  1189. fw_error("Failed to enable OHCI hardware.\n");
  1190. return cleanup(ohci, CLEANUP_PUT_CARD, -ENODEV);
  1191. }
  1192. pci_set_master(dev);
  1193. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1194. pci_set_drvdata(dev, ohci);
  1195. spin_lock_init(&ohci->lock);
  1196. tasklet_init(&ohci->bus_reset_tasklet,
  1197. bus_reset_tasklet, (unsigned long)ohci);
  1198. if (pci_request_region(dev, 0, ohci_driver_name)) {
  1199. fw_error("MMIO resource unavailable\n");
  1200. return cleanup(ohci, CLEANUP_DISABLE, -EBUSY);
  1201. }
  1202. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1203. if (ohci->registers == NULL) {
  1204. fw_error("Failed to remap registers\n");
  1205. return cleanup(ohci, CLEANUP_IOMEM, -ENXIO);
  1206. }
  1207. if (software_reset(ohci)) {
  1208. fw_error("Failed to reset ohci card.\n");
  1209. return cleanup(ohci, CLEANUP_REGISTERS, -EBUSY);
  1210. }
  1211. /* Now enable LPS, which we need in order to start accessing
  1212. * most of the registers. In fact, on some cards (ALI M5251),
  1213. * accessing registers in the SClk domain without LPS enabled
  1214. * will lock up the machine. Wait 50msec to make sure we have
  1215. * full link enabled. */
  1216. reg_write(ohci, OHCI1394_HCControlSet,
  1217. OHCI1394_HCControl_LPS |
  1218. OHCI1394_HCControl_postedWriteEnable);
  1219. flush_writes(ohci);
  1220. msleep(50);
  1221. reg_write(ohci, OHCI1394_HCControlClear,
  1222. OHCI1394_HCControl_noByteSwapData);
  1223. reg_write(ohci, OHCI1394_LinkControlSet,
  1224. OHCI1394_LinkControl_rcvSelfID |
  1225. OHCI1394_LinkControl_cycleTimerEnable |
  1226. OHCI1394_LinkControl_cycleMaster);
  1227. ar_context_init(&ohci->ar_request_ctx, ohci,
  1228. OHCI1394_AsReqRcvContextControlSet);
  1229. ar_context_init(&ohci->ar_response_ctx, ohci,
  1230. OHCI1394_AsRspRcvContextControlSet);
  1231. at_context_init(&ohci->at_request_ctx, ohci,
  1232. OHCI1394_AsReqTrContextControlSet);
  1233. at_context_init(&ohci->at_response_ctx, ohci,
  1234. OHCI1394_AsRspTrContextControlSet);
  1235. reg_write(ohci, OHCI1394_ATRetries,
  1236. OHCI1394_MAX_AT_REQ_RETRIES |
  1237. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1238. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1239. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1240. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1241. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1242. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1243. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1244. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1245. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1246. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1247. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1248. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1249. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1250. fw_error("Out of memory for it/ir contexts.\n");
  1251. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1252. }
  1253. /* self-id dma buffer allocation */
  1254. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1255. SELF_ID_BUF_SIZE,
  1256. &ohci->self_id_bus,
  1257. GFP_KERNEL);
  1258. if (ohci->self_id_cpu == NULL) {
  1259. fw_error("Out of memory for self ID buffer.\n");
  1260. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1261. }
  1262. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1263. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1264. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1265. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1266. reg_write(ohci, OHCI1394_IntMaskSet,
  1267. OHCI1394_selfIDComplete |
  1268. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1269. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1270. OHCI1394_isochRx | OHCI1394_isochTx |
  1271. OHCI1394_masterIntEnable);
  1272. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1273. max_receive = (bus_options >> 12) & 0xf;
  1274. link_speed = bus_options & 0x7;
  1275. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1276. reg_read(ohci, OHCI1394_GUIDLo);
  1277. error_code = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1278. if (error_code < 0)
  1279. return cleanup(ohci, CLEANUP_SELF_ID, error_code);
  1280. fw_notify("Added fw-ohci device %s.\n", dev->dev.bus_id);
  1281. return 0;
  1282. }
  1283. static void pci_remove(struct pci_dev *dev)
  1284. {
  1285. struct fw_ohci *ohci;
  1286. ohci = pci_get_drvdata(dev);
  1287. reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_masterIntEnable);
  1288. fw_core_remove_card(&ohci->card);
  1289. /* FIXME: Fail all pending packets here, now that the upper
  1290. * layers can't queue any more. */
  1291. software_reset(ohci);
  1292. free_irq(dev->irq, ohci);
  1293. cleanup(ohci, CLEANUP_SELF_ID, 0);
  1294. fw_notify("Removed fw-ohci device.\n");
  1295. }
  1296. static struct pci_device_id pci_table[] = {
  1297. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  1298. { }
  1299. };
  1300. MODULE_DEVICE_TABLE(pci, pci_table);
  1301. static struct pci_driver fw_ohci_pci_driver = {
  1302. .name = ohci_driver_name,
  1303. .id_table = pci_table,
  1304. .probe = pci_probe,
  1305. .remove = pci_remove,
  1306. };
  1307. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1308. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  1309. MODULE_LICENSE("GPL");
  1310. static int __init fw_ohci_init(void)
  1311. {
  1312. return pci_register_driver(&fw_ohci_pci_driver);
  1313. }
  1314. static void __exit fw_ohci_cleanup(void)
  1315. {
  1316. pci_unregister_driver(&fw_ohci_pci_driver);
  1317. }
  1318. module_init(fw_ohci_init);
  1319. module_exit(fw_ohci_cleanup);