fw-ohci.c 44 KB

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