fw-ohci.c 45 KB

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