fw-ohci.c 62 KB

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