fw-ohci.c 54 KB

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