fw-ohci.c 62 KB

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