fw-ohci.c 61 KB

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