fw-ohci.c 43 KB

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