fw-ohci.c 53 KB

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