fw-ohci.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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. packet->driver_data = driver_data;
  521. if (packet->payload_length > 0) {
  522. payload_bus =
  523. dma_map_single(ohci->card.device, packet->payload,
  524. packet->payload_length, DMA_TO_DEVICE);
  525. if (dma_mapping_error(payload_bus)) {
  526. packet->ack = RCODE_SEND_ERROR;
  527. return -1;
  528. }
  529. d[2].req_count = cpu_to_le16(packet->payload_length);
  530. d[2].data_address = cpu_to_le32(payload_bus);
  531. last = &d[2];
  532. z = 3;
  533. } else {
  534. last = &d[0];
  535. z = 2;
  536. }
  537. last->control |= cpu_to_le16(descriptor_output_last |
  538. descriptor_irq_always |
  539. descriptor_branch_always);
  540. /* FIXME: Document how the locking works. */
  541. if (ohci->generation != packet->generation) {
  542. packet->ack = RCODE_GENERATION;
  543. return -1;
  544. }
  545. context_append(ctx, d, z, 4 - z);
  546. /* If the context isn't already running, start it up. */
  547. reg = reg_read(ctx->ohci, control_set(ctx->regs));
  548. if ((reg & CONTEXT_ACTIVE) == 0)
  549. context_run(ctx, 0);
  550. return 0;
  551. }
  552. static int handle_at_packet(struct context *context,
  553. struct descriptor *d,
  554. struct descriptor *last)
  555. {
  556. struct driver_data *driver_data;
  557. struct fw_packet *packet;
  558. struct fw_ohci *ohci = context->ohci;
  559. dma_addr_t payload_bus;
  560. int evt;
  561. if (last->transfer_status == 0)
  562. /* This descriptor isn't done yet, stop iteration. */
  563. return 0;
  564. driver_data = (struct driver_data *) &d[3];
  565. packet = driver_data->packet;
  566. if (packet == NULL)
  567. /* This packet was cancelled, just continue. */
  568. return 1;
  569. payload_bus = le32_to_cpu(last->data_address);
  570. if (payload_bus != 0)
  571. dma_unmap_single(ohci->card.device, payload_bus,
  572. packet->payload_length, DMA_TO_DEVICE);
  573. evt = le16_to_cpu(last->transfer_status) & 0x1f;
  574. packet->timestamp = le16_to_cpu(last->res_count);
  575. switch (evt) {
  576. case OHCI1394_evt_timeout:
  577. /* Async response transmit timed out. */
  578. packet->ack = RCODE_CANCELLED;
  579. break;
  580. case OHCI1394_evt_flushed:
  581. /* The packet was flushed should give same error as
  582. * when we try to use a stale generation count. */
  583. packet->ack = RCODE_GENERATION;
  584. break;
  585. case OHCI1394_evt_missing_ack:
  586. /* Using a valid (current) generation count, but the
  587. * node is not on the bus or not sending acks. */
  588. packet->ack = RCODE_NO_ACK;
  589. break;
  590. case ACK_COMPLETE + 0x10:
  591. case ACK_PENDING + 0x10:
  592. case ACK_BUSY_X + 0x10:
  593. case ACK_BUSY_A + 0x10:
  594. case ACK_BUSY_B + 0x10:
  595. case ACK_DATA_ERROR + 0x10:
  596. case ACK_TYPE_ERROR + 0x10:
  597. packet->ack = evt - 0x10;
  598. break;
  599. default:
  600. packet->ack = RCODE_SEND_ERROR;
  601. break;
  602. }
  603. packet->callback(packet, &ohci->card, packet->ack);
  604. return 1;
  605. }
  606. #define header_get_destination(q) (((q) >> 16) & 0xffff)
  607. #define header_get_tcode(q) (((q) >> 4) & 0x0f)
  608. #define header_get_offset_high(q) (((q) >> 0) & 0xffff)
  609. #define header_get_data_length(q) (((q) >> 16) & 0xffff)
  610. #define header_get_extended_tcode(q) (((q) >> 0) & 0xffff)
  611. static void
  612. handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  613. {
  614. struct fw_packet response;
  615. int tcode, length, i;
  616. tcode = header_get_tcode(packet->header[0]);
  617. if (TCODE_IS_BLOCK_PACKET(tcode))
  618. length = header_get_data_length(packet->header[3]);
  619. else
  620. length = 4;
  621. i = csr - CSR_CONFIG_ROM;
  622. if (i + length > CONFIG_ROM_SIZE) {
  623. fw_fill_response(&response, packet->header,
  624. RCODE_ADDRESS_ERROR, NULL, 0);
  625. } else if (!TCODE_IS_READ_REQUEST(tcode)) {
  626. fw_fill_response(&response, packet->header,
  627. RCODE_TYPE_ERROR, NULL, 0);
  628. } else {
  629. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  630. (void *) ohci->config_rom + i, length);
  631. }
  632. fw_core_handle_response(&ohci->card, &response);
  633. }
  634. static void
  635. handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  636. {
  637. struct fw_packet response;
  638. int tcode, length, ext_tcode, sel;
  639. __be32 *payload, lock_old;
  640. u32 lock_arg, lock_data;
  641. tcode = header_get_tcode(packet->header[0]);
  642. length = header_get_data_length(packet->header[3]);
  643. payload = packet->payload;
  644. ext_tcode = header_get_extended_tcode(packet->header[3]);
  645. if (tcode == TCODE_LOCK_REQUEST &&
  646. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  647. lock_arg = be32_to_cpu(payload[0]);
  648. lock_data = be32_to_cpu(payload[1]);
  649. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  650. lock_arg = 0;
  651. lock_data = 0;
  652. } else {
  653. fw_fill_response(&response, packet->header,
  654. RCODE_TYPE_ERROR, NULL, 0);
  655. goto out;
  656. }
  657. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  658. reg_write(ohci, OHCI1394_CSRData, lock_data);
  659. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  660. reg_write(ohci, OHCI1394_CSRControl, sel);
  661. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
  662. lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
  663. else
  664. fw_notify("swap not done yet\n");
  665. fw_fill_response(&response, packet->header,
  666. RCODE_COMPLETE, &lock_old, sizeof lock_old);
  667. out:
  668. fw_core_handle_response(&ohci->card, &response);
  669. }
  670. static void
  671. handle_local_request(struct context *ctx, struct fw_packet *packet)
  672. {
  673. u64 offset;
  674. u32 csr;
  675. if (ctx == &ctx->ohci->at_request_ctx) {
  676. packet->ack = ACK_PENDING;
  677. packet->callback(packet, &ctx->ohci->card, packet->ack);
  678. }
  679. offset =
  680. ((unsigned long long)
  681. header_get_offset_high(packet->header[1]) << 32) |
  682. packet->header[2];
  683. csr = offset - CSR_REGISTER_BASE;
  684. /* Handle config rom reads. */
  685. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  686. handle_local_rom(ctx->ohci, packet, csr);
  687. else switch (csr) {
  688. case CSR_BUS_MANAGER_ID:
  689. case CSR_BANDWIDTH_AVAILABLE:
  690. case CSR_CHANNELS_AVAILABLE_HI:
  691. case CSR_CHANNELS_AVAILABLE_LO:
  692. handle_local_lock(ctx->ohci, packet, csr);
  693. break;
  694. default:
  695. if (ctx == &ctx->ohci->at_request_ctx)
  696. fw_core_handle_request(&ctx->ohci->card, packet);
  697. else
  698. fw_core_handle_response(&ctx->ohci->card, packet);
  699. break;
  700. }
  701. if (ctx == &ctx->ohci->at_response_ctx) {
  702. packet->ack = ACK_COMPLETE;
  703. packet->callback(packet, &ctx->ohci->card, packet->ack);
  704. }
  705. }
  706. static void
  707. at_context_transmit(struct context *ctx, struct fw_packet *packet)
  708. {
  709. unsigned long flags;
  710. int retval;
  711. spin_lock_irqsave(&ctx->ohci->lock, flags);
  712. if (header_get_destination(packet->header[0]) == ctx->ohci->node_id &&
  713. ctx->ohci->generation == packet->generation) {
  714. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  715. handle_local_request(ctx, packet);
  716. return;
  717. }
  718. retval = at_context_queue_packet(ctx, packet);
  719. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  720. if (retval < 0)
  721. packet->callback(packet, &ctx->ohci->card, packet->ack);
  722. }
  723. static void bus_reset_tasklet(unsigned long data)
  724. {
  725. struct fw_ohci *ohci = (struct fw_ohci *)data;
  726. int self_id_count, i, j, reg;
  727. int generation, new_generation;
  728. unsigned long flags;
  729. reg = reg_read(ohci, OHCI1394_NodeID);
  730. if (!(reg & OHCI1394_NodeID_idValid)) {
  731. fw_error("node ID not valid, new bus reset in progress\n");
  732. return;
  733. }
  734. ohci->node_id = reg & 0xffff;
  735. /* The count in the SelfIDCount register is the number of
  736. * bytes in the self ID receive buffer. Since we also receive
  737. * the inverted quadlets and a header quadlet, we shift one
  738. * bit extra to get the actual number of self IDs. */
  739. self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
  740. generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  741. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  742. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
  743. fw_error("inconsistent self IDs\n");
  744. ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
  745. }
  746. /* Check the consistency of the self IDs we just read. The
  747. * problem we face is that a new bus reset can start while we
  748. * read out the self IDs from the DMA buffer. If this happens,
  749. * the DMA buffer will be overwritten with new self IDs and we
  750. * will read out inconsistent data. The OHCI specification
  751. * (section 11.2) recommends a technique similar to
  752. * linux/seqlock.h, where we remember the generation of the
  753. * self IDs in the buffer before reading them out and compare
  754. * it to the current generation after reading them out. If
  755. * the two generations match we know we have a consistent set
  756. * of self IDs. */
  757. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  758. if (new_generation != generation) {
  759. fw_notify("recursive bus reset detected, "
  760. "discarding self ids\n");
  761. return;
  762. }
  763. /* FIXME: Document how the locking works. */
  764. spin_lock_irqsave(&ohci->lock, flags);
  765. ohci->generation = generation;
  766. context_stop(&ohci->at_request_ctx);
  767. context_stop(&ohci->at_response_ctx);
  768. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  769. /* This next bit is unrelated to the AT context stuff but we
  770. * have to do it under the spinlock also. If a new config rom
  771. * was set up before this reset, the old one is now no longer
  772. * in use and we can free it. Update the config rom pointers
  773. * to point to the current config rom and clear the
  774. * next_config_rom pointer so a new udpate can take place. */
  775. if (ohci->next_config_rom != NULL) {
  776. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  777. ohci->config_rom, ohci->config_rom_bus);
  778. ohci->config_rom = ohci->next_config_rom;
  779. ohci->config_rom_bus = ohci->next_config_rom_bus;
  780. ohci->next_config_rom = NULL;
  781. /* Restore config_rom image and manually update
  782. * config_rom registers. Writing the header quadlet
  783. * will indicate that the config rom is ready, so we
  784. * do that last. */
  785. reg_write(ohci, OHCI1394_BusOptions,
  786. be32_to_cpu(ohci->config_rom[2]));
  787. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  788. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  789. }
  790. spin_unlock_irqrestore(&ohci->lock, flags);
  791. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  792. self_id_count, ohci->self_id_buffer);
  793. }
  794. static irqreturn_t irq_handler(int irq, void *data)
  795. {
  796. struct fw_ohci *ohci = data;
  797. u32 event, iso_event, cycle_time;
  798. int i;
  799. event = reg_read(ohci, OHCI1394_IntEventClear);
  800. if (!event)
  801. return IRQ_NONE;
  802. reg_write(ohci, OHCI1394_IntEventClear, event);
  803. if (event & OHCI1394_selfIDComplete)
  804. tasklet_schedule(&ohci->bus_reset_tasklet);
  805. if (event & OHCI1394_RQPkt)
  806. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  807. if (event & OHCI1394_RSPkt)
  808. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  809. if (event & OHCI1394_reqTxComplete)
  810. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  811. if (event & OHCI1394_respTxComplete)
  812. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  813. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  814. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  815. while (iso_event) {
  816. i = ffs(iso_event) - 1;
  817. tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
  818. iso_event &= ~(1 << i);
  819. }
  820. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  821. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  822. while (iso_event) {
  823. i = ffs(iso_event) - 1;
  824. tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
  825. iso_event &= ~(1 << i);
  826. }
  827. if (event & OHCI1394_cycle64Seconds) {
  828. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  829. if ((cycle_time & 0x80000000) == 0)
  830. ohci->bus_seconds++;
  831. }
  832. return IRQ_HANDLED;
  833. }
  834. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  835. {
  836. struct fw_ohci *ohci = fw_ohci(card);
  837. struct pci_dev *dev = to_pci_dev(card->device);
  838. /* When the link is not yet enabled, the atomic config rom
  839. * update mechanism described below in ohci_set_config_rom()
  840. * is not active. We have to update ConfigRomHeader and
  841. * BusOptions manually, and the write to ConfigROMmap takes
  842. * effect immediately. We tie this to the enabling of the
  843. * link, so we have a valid config rom before enabling - the
  844. * OHCI requires that ConfigROMhdr and BusOptions have valid
  845. * values before enabling.
  846. *
  847. * However, when the ConfigROMmap is written, some controllers
  848. * always read back quadlets 0 and 2 from the config rom to
  849. * the ConfigRomHeader and BusOptions registers on bus reset.
  850. * They shouldn't do that in this initial case where the link
  851. * isn't enabled. This means we have to use the same
  852. * workaround here, setting the bus header to 0 and then write
  853. * the right values in the bus reset tasklet.
  854. */
  855. ohci->next_config_rom =
  856. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  857. &ohci->next_config_rom_bus, GFP_KERNEL);
  858. if (ohci->next_config_rom == NULL)
  859. return -ENOMEM;
  860. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  861. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  862. ohci->next_header = config_rom[0];
  863. ohci->next_config_rom[0] = 0;
  864. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  865. reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
  866. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  867. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  868. if (request_irq(dev->irq, irq_handler,
  869. IRQF_SHARED, ohci_driver_name, ohci)) {
  870. fw_error("Failed to allocate shared interrupt %d.\n",
  871. dev->irq);
  872. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  873. ohci->config_rom, ohci->config_rom_bus);
  874. return -EIO;
  875. }
  876. reg_write(ohci, OHCI1394_HCControlSet,
  877. OHCI1394_HCControl_linkEnable |
  878. OHCI1394_HCControl_BIBimageValid);
  879. flush_writes(ohci);
  880. /* We are ready to go, initiate bus reset to finish the
  881. * initialization. */
  882. fw_core_initiate_bus_reset(&ohci->card, 1);
  883. return 0;
  884. }
  885. static int
  886. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  887. {
  888. struct fw_ohci *ohci;
  889. unsigned long flags;
  890. int retval = 0;
  891. __be32 *next_config_rom;
  892. dma_addr_t next_config_rom_bus;
  893. ohci = fw_ohci(card);
  894. /* When the OHCI controller is enabled, the config rom update
  895. * mechanism is a bit tricky, but easy enough to use. See
  896. * section 5.5.6 in the OHCI specification.
  897. *
  898. * The OHCI controller caches the new config rom address in a
  899. * shadow register (ConfigROMmapNext) and needs a bus reset
  900. * for the changes to take place. When the bus reset is
  901. * detected, the controller loads the new values for the
  902. * ConfigRomHeader and BusOptions registers from the specified
  903. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  904. * shadow register. All automatically and atomically.
  905. *
  906. * Now, there's a twist to this story. The automatic load of
  907. * ConfigRomHeader and BusOptions doesn't honor the
  908. * noByteSwapData bit, so with a be32 config rom, the
  909. * controller will load be32 values in to these registers
  910. * during the atomic update, even on litte endian
  911. * architectures. The workaround we use is to put a 0 in the
  912. * header quadlet; 0 is endian agnostic and means that the
  913. * config rom isn't ready yet. In the bus reset tasklet we
  914. * then set up the real values for the two registers.
  915. *
  916. * We use ohci->lock to avoid racing with the code that sets
  917. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  918. */
  919. next_config_rom =
  920. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  921. &next_config_rom_bus, GFP_KERNEL);
  922. if (next_config_rom == NULL)
  923. return -ENOMEM;
  924. spin_lock_irqsave(&ohci->lock, flags);
  925. if (ohci->next_config_rom == NULL) {
  926. ohci->next_config_rom = next_config_rom;
  927. ohci->next_config_rom_bus = next_config_rom_bus;
  928. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  929. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  930. length * 4);
  931. ohci->next_header = config_rom[0];
  932. ohci->next_config_rom[0] = 0;
  933. reg_write(ohci, OHCI1394_ConfigROMmap,
  934. ohci->next_config_rom_bus);
  935. } else {
  936. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  937. next_config_rom, next_config_rom_bus);
  938. retval = -EBUSY;
  939. }
  940. spin_unlock_irqrestore(&ohci->lock, flags);
  941. /* Now initiate a bus reset to have the changes take
  942. * effect. We clean up the old config rom memory and DMA
  943. * mappings in the bus reset tasklet, since the OHCI
  944. * controller could need to access it before the bus reset
  945. * takes effect. */
  946. if (retval == 0)
  947. fw_core_initiate_bus_reset(&ohci->card, 1);
  948. return retval;
  949. }
  950. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  951. {
  952. struct fw_ohci *ohci = fw_ohci(card);
  953. at_context_transmit(&ohci->at_request_ctx, packet);
  954. }
  955. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  956. {
  957. struct fw_ohci *ohci = fw_ohci(card);
  958. at_context_transmit(&ohci->at_response_ctx, packet);
  959. }
  960. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  961. {
  962. struct fw_ohci *ohci = fw_ohci(card);
  963. struct context *ctx = &ohci->at_request_ctx;
  964. struct driver_data *driver_data = packet->driver_data;
  965. int retval = -ENOENT;
  966. tasklet_disable(&ctx->tasklet);
  967. if (packet->ack != 0)
  968. goto out;
  969. driver_data->packet = NULL;
  970. packet->ack = RCODE_CANCELLED;
  971. packet->callback(packet, &ohci->card, packet->ack);
  972. retval = 0;
  973. out:
  974. tasklet_enable(&ctx->tasklet);
  975. return retval;
  976. }
  977. static int
  978. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  979. {
  980. struct fw_ohci *ohci = fw_ohci(card);
  981. unsigned long flags;
  982. int n, retval = 0;
  983. /* FIXME: Make sure this bitmask is cleared when we clear the busReset
  984. * interrupt bit. Clear physReqResourceAllBuses on bus reset. */
  985. spin_lock_irqsave(&ohci->lock, flags);
  986. if (ohci->generation != generation) {
  987. retval = -ESTALE;
  988. goto out;
  989. }
  990. /* NOTE, if the node ID contains a non-local bus ID, physical DMA is
  991. * enabled for _all_ nodes on remote buses. */
  992. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  993. if (n < 32)
  994. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  995. else
  996. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  997. flush_writes(ohci);
  998. out:
  999. spin_unlock_irqrestore(&ohci->lock, flags);
  1000. return retval;
  1001. }
  1002. static u64
  1003. ohci_get_bus_time(struct fw_card *card)
  1004. {
  1005. struct fw_ohci *ohci = fw_ohci(card);
  1006. u32 cycle_time;
  1007. u64 bus_time;
  1008. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1009. bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
  1010. return bus_time;
  1011. }
  1012. static int handle_ir_dualbuffer_packet(struct context *context,
  1013. struct descriptor *d,
  1014. struct descriptor *last)
  1015. {
  1016. struct iso_context *ctx =
  1017. container_of(context, struct iso_context, context);
  1018. struct db_descriptor *db = (struct db_descriptor *) d;
  1019. __le32 *ir_header;
  1020. size_t header_length;
  1021. void *p, *end;
  1022. int i;
  1023. if (db->first_res_count > 0 && db->second_res_count > 0)
  1024. /* This descriptor isn't done yet, stop iteration. */
  1025. return 0;
  1026. header_length = le16_to_cpu(db->first_req_count) -
  1027. le16_to_cpu(db->first_res_count);
  1028. i = ctx->header_length;
  1029. p = db + 1;
  1030. end = p + header_length;
  1031. while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
  1032. memcpy(ctx->header + i, p + 4, ctx->base.header_size);
  1033. i += ctx->base.header_size;
  1034. p += ctx->base.header_size + 4;
  1035. }
  1036. ctx->header_length = i;
  1037. if (le16_to_cpu(db->control) & descriptor_irq_always) {
  1038. ir_header = (__le32 *) (db + 1);
  1039. ctx->base.callback(&ctx->base,
  1040. le32_to_cpu(ir_header[0]) & 0xffff,
  1041. ctx->header_length, ctx->header,
  1042. ctx->base.callback_data);
  1043. ctx->header_length = 0;
  1044. }
  1045. return 1;
  1046. }
  1047. static int handle_it_packet(struct context *context,
  1048. struct descriptor *d,
  1049. struct descriptor *last)
  1050. {
  1051. struct iso_context *ctx =
  1052. container_of(context, struct iso_context, context);
  1053. if (last->transfer_status == 0)
  1054. /* This descriptor isn't done yet, stop iteration. */
  1055. return 0;
  1056. if (le16_to_cpu(last->control) & descriptor_irq_always)
  1057. ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
  1058. 0, NULL, ctx->base.callback_data);
  1059. return 1;
  1060. }
  1061. static struct fw_iso_context *
  1062. ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
  1063. {
  1064. struct fw_ohci *ohci = fw_ohci(card);
  1065. struct iso_context *ctx, *list;
  1066. descriptor_callback_t callback;
  1067. u32 *mask, regs;
  1068. unsigned long flags;
  1069. int index, retval = -ENOMEM;
  1070. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  1071. mask = &ohci->it_context_mask;
  1072. list = ohci->it_context_list;
  1073. callback = handle_it_packet;
  1074. } else {
  1075. mask = &ohci->ir_context_mask;
  1076. list = ohci->ir_context_list;
  1077. callback = handle_ir_dualbuffer_packet;
  1078. }
  1079. /* FIXME: We need a fallback for pre 1.1 OHCI. */
  1080. if (callback == handle_ir_dualbuffer_packet &&
  1081. ohci->version < OHCI_VERSION_1_1)
  1082. return ERR_PTR(-EINVAL);
  1083. spin_lock_irqsave(&ohci->lock, flags);
  1084. index = ffs(*mask) - 1;
  1085. if (index >= 0)
  1086. *mask &= ~(1 << index);
  1087. spin_unlock_irqrestore(&ohci->lock, flags);
  1088. if (index < 0)
  1089. return ERR_PTR(-EBUSY);
  1090. if (type == FW_ISO_CONTEXT_TRANSMIT)
  1091. regs = OHCI1394_IsoXmitContextBase(index);
  1092. else
  1093. regs = OHCI1394_IsoRcvContextBase(index);
  1094. ctx = &list[index];
  1095. memset(ctx, 0, sizeof *ctx);
  1096. ctx->header_length = 0;
  1097. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  1098. if (ctx->header == NULL)
  1099. goto out;
  1100. retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
  1101. regs, callback);
  1102. if (retval < 0)
  1103. goto out_with_header;
  1104. return &ctx->base;
  1105. out_with_header:
  1106. free_page((unsigned long)ctx->header);
  1107. out:
  1108. spin_lock_irqsave(&ohci->lock, flags);
  1109. *mask |= 1 << index;
  1110. spin_unlock_irqrestore(&ohci->lock, flags);
  1111. return ERR_PTR(retval);
  1112. }
  1113. static int ohci_start_iso(struct fw_iso_context *base,
  1114. s32 cycle, u32 sync, u32 tags)
  1115. {
  1116. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1117. struct fw_ohci *ohci = ctx->context.ohci;
  1118. u32 cycle_match = 0;
  1119. int index;
  1120. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1121. index = ctx - ohci->it_context_list;
  1122. if (cycle > 0)
  1123. cycle_match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  1124. (cycle & 0x7fff) << 16;
  1125. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  1126. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  1127. context_run(&ctx->context, cycle_match);
  1128. } else {
  1129. index = ctx - ohci->ir_context_list;
  1130. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  1131. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  1132. reg_write(ohci, context_match(ctx->context.regs),
  1133. (tags << 28) | (sync << 8) | ctx->base.channel);
  1134. context_run(&ctx->context,
  1135. IR_CONTEXT_DUAL_BUFFER_MODE |
  1136. IR_CONTEXT_ISOCH_HEADER);
  1137. }
  1138. return 0;
  1139. }
  1140. static int ohci_stop_iso(struct fw_iso_context *base)
  1141. {
  1142. struct fw_ohci *ohci = fw_ohci(base->card);
  1143. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1144. int index;
  1145. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1146. index = ctx - ohci->it_context_list;
  1147. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  1148. } else {
  1149. index = ctx - ohci->ir_context_list;
  1150. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  1151. }
  1152. flush_writes(ohci);
  1153. context_stop(&ctx->context);
  1154. return 0;
  1155. }
  1156. static void ohci_free_iso_context(struct fw_iso_context *base)
  1157. {
  1158. struct fw_ohci *ohci = fw_ohci(base->card);
  1159. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1160. unsigned long flags;
  1161. int index;
  1162. ohci_stop_iso(base);
  1163. context_release(&ctx->context);
  1164. free_page((unsigned long)ctx->header);
  1165. spin_lock_irqsave(&ohci->lock, flags);
  1166. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1167. index = ctx - ohci->it_context_list;
  1168. ohci->it_context_mask |= 1 << index;
  1169. } else {
  1170. index = ctx - ohci->ir_context_list;
  1171. ohci->ir_context_mask |= 1 << index;
  1172. }
  1173. spin_unlock_irqrestore(&ohci->lock, flags);
  1174. }
  1175. static int
  1176. ohci_queue_iso_transmit(struct fw_iso_context *base,
  1177. struct fw_iso_packet *packet,
  1178. struct fw_iso_buffer *buffer,
  1179. unsigned long payload)
  1180. {
  1181. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1182. struct descriptor *d, *last, *pd;
  1183. struct fw_iso_packet *p;
  1184. __le32 *header;
  1185. dma_addr_t d_bus, page_bus;
  1186. u32 z, header_z, payload_z, irq;
  1187. u32 payload_index, payload_end_index, next_page_index;
  1188. int page, end_page, i, length, offset;
  1189. /* FIXME: Cycle lost behavior should be configurable: lose
  1190. * packet, retransmit or terminate.. */
  1191. p = packet;
  1192. payload_index = payload;
  1193. if (p->skip)
  1194. z = 1;
  1195. else
  1196. z = 2;
  1197. if (p->header_length > 0)
  1198. z++;
  1199. /* Determine the first page the payload isn't contained in. */
  1200. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  1201. if (p->payload_length > 0)
  1202. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  1203. else
  1204. payload_z = 0;
  1205. z += payload_z;
  1206. /* Get header size in number of descriptors. */
  1207. header_z = DIV_ROUND_UP(p->header_length, sizeof *d);
  1208. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  1209. if (d == NULL)
  1210. return -ENOMEM;
  1211. if (!p->skip) {
  1212. d[0].control = cpu_to_le16(descriptor_key_immediate);
  1213. d[0].req_count = cpu_to_le16(8);
  1214. header = (__le32 *) &d[1];
  1215. header[0] = cpu_to_le32(it_header_sy(p->sy) |
  1216. it_header_tag(p->tag) |
  1217. it_header_tcode(TCODE_STREAM_DATA) |
  1218. it_header_channel(ctx->base.channel) |
  1219. it_header_speed(ctx->base.speed));
  1220. header[1] =
  1221. cpu_to_le32(it_header_data_length(p->header_length +
  1222. p->payload_length));
  1223. }
  1224. if (p->header_length > 0) {
  1225. d[2].req_count = cpu_to_le16(p->header_length);
  1226. d[2].data_address = cpu_to_le32(d_bus + z * sizeof *d);
  1227. memcpy(&d[z], p->header, p->header_length);
  1228. }
  1229. pd = d + z - payload_z;
  1230. payload_end_index = payload_index + p->payload_length;
  1231. for (i = 0; i < payload_z; i++) {
  1232. page = payload_index >> PAGE_SHIFT;
  1233. offset = payload_index & ~PAGE_MASK;
  1234. next_page_index = (page + 1) << PAGE_SHIFT;
  1235. length =
  1236. min(next_page_index, payload_end_index) - payload_index;
  1237. pd[i].req_count = cpu_to_le16(length);
  1238. page_bus = page_private(buffer->pages[page]);
  1239. pd[i].data_address = cpu_to_le32(page_bus + offset);
  1240. payload_index += length;
  1241. }
  1242. if (p->interrupt)
  1243. irq = descriptor_irq_always;
  1244. else
  1245. irq = descriptor_no_irq;
  1246. last = z == 2 ? d : d + z - 1;
  1247. last->control |= cpu_to_le16(descriptor_output_last |
  1248. descriptor_status |
  1249. descriptor_branch_always |
  1250. irq);
  1251. context_append(&ctx->context, d, z, header_z);
  1252. return 0;
  1253. }
  1254. static int
  1255. ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
  1256. struct fw_iso_packet *packet,
  1257. struct fw_iso_buffer *buffer,
  1258. unsigned long payload)
  1259. {
  1260. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1261. struct db_descriptor *db = NULL;
  1262. struct descriptor *d;
  1263. struct fw_iso_packet *p;
  1264. dma_addr_t d_bus, page_bus;
  1265. u32 z, header_z, length, rest;
  1266. int page, offset, packet_count, header_size;
  1267. /* FIXME: Cycle lost behavior should be configurable: lose
  1268. * packet, retransmit or terminate.. */
  1269. if (packet->skip) {
  1270. d = context_get_descriptors(&ctx->context, 2, &d_bus);
  1271. if (d == NULL)
  1272. return -ENOMEM;
  1273. db = (struct db_descriptor *) d;
  1274. db->control = cpu_to_le16(descriptor_status |
  1275. descriptor_branch_always |
  1276. descriptor_wait);
  1277. db->first_size = cpu_to_le16(ctx->base.header_size + 4);
  1278. context_append(&ctx->context, d, 2, 0);
  1279. }
  1280. p = packet;
  1281. z = 2;
  1282. /* The OHCI controller puts the status word in the header
  1283. * buffer too, so we need 4 extra bytes per packet. */
  1284. packet_count = p->header_length / ctx->base.header_size;
  1285. header_size = packet_count * (ctx->base.header_size + 4);
  1286. /* Get header size in number of descriptors. */
  1287. header_z = DIV_ROUND_UP(header_size, sizeof *d);
  1288. page = payload >> PAGE_SHIFT;
  1289. offset = payload & ~PAGE_MASK;
  1290. rest = p->payload_length;
  1291. /* FIXME: OHCI 1.0 doesn't support dual buffer receive */
  1292. /* FIXME: make packet-per-buffer/dual-buffer a context option */
  1293. while (rest > 0) {
  1294. d = context_get_descriptors(&ctx->context,
  1295. z + header_z, &d_bus);
  1296. if (d == NULL)
  1297. return -ENOMEM;
  1298. db = (struct db_descriptor *) d;
  1299. db->control = cpu_to_le16(descriptor_status |
  1300. descriptor_branch_always);
  1301. db->first_size = cpu_to_le16(ctx->base.header_size + 4);
  1302. db->first_req_count = cpu_to_le16(header_size);
  1303. db->first_res_count = db->first_req_count;
  1304. db->first_buffer = cpu_to_le32(d_bus + sizeof *db);
  1305. if (offset + rest < PAGE_SIZE)
  1306. length = rest;
  1307. else
  1308. length = PAGE_SIZE - offset;
  1309. db->second_req_count = cpu_to_le16(length);
  1310. db->second_res_count = db->second_req_count;
  1311. page_bus = page_private(buffer->pages[page]);
  1312. db->second_buffer = cpu_to_le32(page_bus + offset);
  1313. if (p->interrupt && length == rest)
  1314. db->control |= cpu_to_le16(descriptor_irq_always);
  1315. context_append(&ctx->context, d, z, header_z);
  1316. offset = (offset + length) & ~PAGE_MASK;
  1317. rest -= length;
  1318. page++;
  1319. }
  1320. return 0;
  1321. }
  1322. static int
  1323. ohci_queue_iso(struct fw_iso_context *base,
  1324. struct fw_iso_packet *packet,
  1325. struct fw_iso_buffer *buffer,
  1326. unsigned long payload)
  1327. {
  1328. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1329. if (base->type == FW_ISO_CONTEXT_TRANSMIT)
  1330. return ohci_queue_iso_transmit(base, packet, buffer, payload);
  1331. else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
  1332. return ohci_queue_iso_receive_dualbuffer(base, packet,
  1333. buffer, payload);
  1334. else
  1335. /* FIXME: Implement fallback for OHCI 1.0 controllers. */
  1336. return -EINVAL;
  1337. }
  1338. static const struct fw_card_driver ohci_driver = {
  1339. .name = ohci_driver_name,
  1340. .enable = ohci_enable,
  1341. .update_phy_reg = ohci_update_phy_reg,
  1342. .set_config_rom = ohci_set_config_rom,
  1343. .send_request = ohci_send_request,
  1344. .send_response = ohci_send_response,
  1345. .cancel_packet = ohci_cancel_packet,
  1346. .enable_phys_dma = ohci_enable_phys_dma,
  1347. .get_bus_time = ohci_get_bus_time,
  1348. .allocate_iso_context = ohci_allocate_iso_context,
  1349. .free_iso_context = ohci_free_iso_context,
  1350. .queue_iso = ohci_queue_iso,
  1351. .start_iso = ohci_start_iso,
  1352. .stop_iso = ohci_stop_iso,
  1353. };
  1354. static int software_reset(struct fw_ohci *ohci)
  1355. {
  1356. int i;
  1357. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1358. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  1359. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  1360. OHCI1394_HCControl_softReset) == 0)
  1361. return 0;
  1362. msleep(1);
  1363. }
  1364. return -EBUSY;
  1365. }
  1366. /* ---------- pci subsystem interface ---------- */
  1367. enum {
  1368. CLEANUP_SELF_ID,
  1369. CLEANUP_REGISTERS,
  1370. CLEANUP_IOMEM,
  1371. CLEANUP_DISABLE,
  1372. CLEANUP_PUT_CARD,
  1373. };
  1374. static int cleanup(struct fw_ohci *ohci, int stage, int code)
  1375. {
  1376. struct pci_dev *dev = to_pci_dev(ohci->card.device);
  1377. switch (stage) {
  1378. case CLEANUP_SELF_ID:
  1379. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1380. ohci->self_id_cpu, ohci->self_id_bus);
  1381. case CLEANUP_REGISTERS:
  1382. kfree(ohci->it_context_list);
  1383. kfree(ohci->ir_context_list);
  1384. pci_iounmap(dev, ohci->registers);
  1385. case CLEANUP_IOMEM:
  1386. pci_release_region(dev, 0);
  1387. case CLEANUP_DISABLE:
  1388. pci_disable_device(dev);
  1389. case CLEANUP_PUT_CARD:
  1390. fw_card_put(&ohci->card);
  1391. }
  1392. return code;
  1393. }
  1394. static int __devinit
  1395. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  1396. {
  1397. struct fw_ohci *ohci;
  1398. u32 bus_options, max_receive, link_speed;
  1399. u64 guid;
  1400. int error_code;
  1401. size_t size;
  1402. ohci = kzalloc(sizeof *ohci, GFP_KERNEL);
  1403. if (ohci == NULL) {
  1404. fw_error("Could not malloc fw_ohci data.\n");
  1405. return -ENOMEM;
  1406. }
  1407. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1408. if (pci_enable_device(dev)) {
  1409. fw_error("Failed to enable OHCI hardware.\n");
  1410. return cleanup(ohci, CLEANUP_PUT_CARD, -ENODEV);
  1411. }
  1412. pci_set_master(dev);
  1413. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1414. pci_set_drvdata(dev, ohci);
  1415. spin_lock_init(&ohci->lock);
  1416. tasklet_init(&ohci->bus_reset_tasklet,
  1417. bus_reset_tasklet, (unsigned long)ohci);
  1418. if (pci_request_region(dev, 0, ohci_driver_name)) {
  1419. fw_error("MMIO resource unavailable\n");
  1420. return cleanup(ohci, CLEANUP_DISABLE, -EBUSY);
  1421. }
  1422. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1423. if (ohci->registers == NULL) {
  1424. fw_error("Failed to remap registers\n");
  1425. return cleanup(ohci, CLEANUP_IOMEM, -ENXIO);
  1426. }
  1427. if (software_reset(ohci)) {
  1428. fw_error("Failed to reset ohci card.\n");
  1429. return cleanup(ohci, CLEANUP_REGISTERS, -EBUSY);
  1430. }
  1431. /* Now enable LPS, which we need in order to start accessing
  1432. * most of the registers. In fact, on some cards (ALI M5251),
  1433. * accessing registers in the SClk domain without LPS enabled
  1434. * will lock up the machine. Wait 50msec to make sure we have
  1435. * full link enabled. */
  1436. reg_write(ohci, OHCI1394_HCControlSet,
  1437. OHCI1394_HCControl_LPS |
  1438. OHCI1394_HCControl_postedWriteEnable);
  1439. flush_writes(ohci);
  1440. msleep(50);
  1441. reg_write(ohci, OHCI1394_HCControlClear,
  1442. OHCI1394_HCControl_noByteSwapData);
  1443. reg_write(ohci, OHCI1394_LinkControlSet,
  1444. OHCI1394_LinkControl_rcvSelfID |
  1445. OHCI1394_LinkControl_cycleTimerEnable |
  1446. OHCI1394_LinkControl_cycleMaster);
  1447. ar_context_init(&ohci->ar_request_ctx, ohci,
  1448. OHCI1394_AsReqRcvContextControlSet);
  1449. ar_context_init(&ohci->ar_response_ctx, ohci,
  1450. OHCI1394_AsRspRcvContextControlSet);
  1451. context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE,
  1452. OHCI1394_AsReqTrContextControlSet, handle_at_packet);
  1453. context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE,
  1454. OHCI1394_AsRspTrContextControlSet, handle_at_packet);
  1455. reg_write(ohci, OHCI1394_ATRetries,
  1456. OHCI1394_MAX_AT_REQ_RETRIES |
  1457. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1458. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1459. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1460. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1461. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1462. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1463. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1464. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1465. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1466. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1467. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1468. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1469. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1470. fw_error("Out of memory for it/ir contexts.\n");
  1471. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1472. }
  1473. /* self-id dma buffer allocation */
  1474. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1475. SELF_ID_BUF_SIZE,
  1476. &ohci->self_id_bus,
  1477. GFP_KERNEL);
  1478. if (ohci->self_id_cpu == NULL) {
  1479. fw_error("Out of memory for self ID buffer.\n");
  1480. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1481. }
  1482. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1483. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1484. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1485. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1486. reg_write(ohci, OHCI1394_IntMaskSet,
  1487. OHCI1394_selfIDComplete |
  1488. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1489. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1490. OHCI1394_isochRx | OHCI1394_isochTx |
  1491. OHCI1394_masterIntEnable |
  1492. OHCI1394_cycle64Seconds);
  1493. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1494. max_receive = (bus_options >> 12) & 0xf;
  1495. link_speed = bus_options & 0x7;
  1496. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1497. reg_read(ohci, OHCI1394_GUIDLo);
  1498. error_code = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1499. if (error_code < 0)
  1500. return cleanup(ohci, CLEANUP_SELF_ID, error_code);
  1501. ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  1502. fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
  1503. dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
  1504. return 0;
  1505. }
  1506. static void pci_remove(struct pci_dev *dev)
  1507. {
  1508. struct fw_ohci *ohci;
  1509. ohci = pci_get_drvdata(dev);
  1510. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1511. flush_writes(ohci);
  1512. fw_core_remove_card(&ohci->card);
  1513. /* FIXME: Fail all pending packets here, now that the upper
  1514. * layers can't queue any more. */
  1515. software_reset(ohci);
  1516. free_irq(dev->irq, ohci);
  1517. cleanup(ohci, CLEANUP_SELF_ID, 0);
  1518. fw_notify("Removed fw-ohci device.\n");
  1519. }
  1520. static struct pci_device_id pci_table[] = {
  1521. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  1522. { }
  1523. };
  1524. MODULE_DEVICE_TABLE(pci, pci_table);
  1525. static struct pci_driver fw_ohci_pci_driver = {
  1526. .name = ohci_driver_name,
  1527. .id_table = pci_table,
  1528. .probe = pci_probe,
  1529. .remove = pci_remove,
  1530. };
  1531. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1532. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  1533. MODULE_LICENSE("GPL");
  1534. static int __init fw_ohci_init(void)
  1535. {
  1536. return pci_register_driver(&fw_ohci_pci_driver);
  1537. }
  1538. static void __exit fw_ohci_cleanup(void)
  1539. {
  1540. pci_unregister_driver(&fw_ohci_pci_driver);
  1541. }
  1542. module_init(fw_ohci_init);
  1543. module_exit(fw_ohci_cleanup);