mesh.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. /*
  2. * SCSI low-level driver for the MESH (Macintosh Enhanced SCSI Hardware)
  3. * bus adaptor found on Power Macintosh computers.
  4. * We assume the MESH is connected to a DBDMA (descriptor-based DMA)
  5. * controller.
  6. *
  7. * Paul Mackerras, August 1996.
  8. * Copyright (C) 1996 Paul Mackerras.
  9. *
  10. * Apr. 21 2002 - BenH Rework bus reset code for new error handler
  11. * Add delay after initial bus reset
  12. * Add module parameters
  13. *
  14. * Sep. 27 2003 - BenH Move to new driver model, fix some write posting
  15. * issues
  16. * To do:
  17. * - handle aborts correctly
  18. * - retry arbitration if lost (unless higher levels do this for us)
  19. * - power down the chip when no device is detected
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/types.h>
  25. #include <linux/string.h>
  26. #include <linux/slab.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/stat.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/reboot.h>
  32. #include <linux/spinlock.h>
  33. #include <asm/dbdma.h>
  34. #include <asm/io.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/prom.h>
  37. #include <asm/system.h>
  38. #include <asm/irq.h>
  39. #include <asm/hydra.h>
  40. #include <asm/processor.h>
  41. #include <asm/machdep.h>
  42. #include <asm/pmac_feature.h>
  43. #include <asm/pci-bridge.h>
  44. #include <asm/macio.h>
  45. #include <scsi/scsi.h>
  46. #include <scsi/scsi_cmnd.h>
  47. #include <scsi/scsi_device.h>
  48. #include <scsi/scsi_host.h>
  49. #include "mesh.h"
  50. #if 1
  51. #undef KERN_DEBUG
  52. #define KERN_DEBUG KERN_WARNING
  53. #endif
  54. MODULE_AUTHOR("Paul Mackerras (paulus@samba.org)");
  55. MODULE_DESCRIPTION("PowerMac MESH SCSI driver");
  56. MODULE_LICENSE("GPL");
  57. static int sync_rate = CONFIG_SCSI_MESH_SYNC_RATE;
  58. static int sync_targets = 0xff;
  59. static int resel_targets = 0xff;
  60. static int debug_targets = 0; /* print debug for these targets */
  61. static int init_reset_delay = CONFIG_SCSI_MESH_RESET_DELAY_MS;
  62. module_param(sync_rate, int, 0);
  63. MODULE_PARM_DESC(sync_rate, "Synchronous rate (0..10, 0=async)");
  64. module_param(sync_targets, int, 0);
  65. MODULE_PARM_DESC(sync_targets, "Bitmask of targets allowed to set synchronous");
  66. module_param(resel_targets, int, 0);
  67. MODULE_PARM_DESC(resel_targets, "Bitmask of targets allowed to set disconnect");
  68. module_param(debug_targets, int, 0644);
  69. MODULE_PARM_DESC(debug_targets, "Bitmask of debugged targets");
  70. module_param(init_reset_delay, int, 0);
  71. MODULE_PARM_DESC(init_reset_delay, "Initial bus reset delay (0=no reset)");
  72. static int mesh_sync_period = 100;
  73. static int mesh_sync_offset = 0;
  74. static unsigned char use_active_neg = 0; /* bit mask for SEQ_ACTIVE_NEG if used */
  75. #define ALLOW_SYNC(tgt) ((sync_targets >> (tgt)) & 1)
  76. #define ALLOW_RESEL(tgt) ((resel_targets >> (tgt)) & 1)
  77. #define ALLOW_DEBUG(tgt) ((debug_targets >> (tgt)) & 1)
  78. #define DEBUG_TARGET(cmd) ((cmd) && ALLOW_DEBUG((cmd)->device->id))
  79. #undef MESH_DBG
  80. #define N_DBG_LOG 50
  81. #define N_DBG_SLOG 20
  82. #define NUM_DBG_EVENTS 13
  83. #undef DBG_USE_TB /* bombs on 601 */
  84. struct dbglog {
  85. char *fmt;
  86. u32 tb;
  87. u8 phase;
  88. u8 bs0;
  89. u8 bs1;
  90. u8 tgt;
  91. int d;
  92. };
  93. enum mesh_phase {
  94. idle,
  95. arbitrating,
  96. selecting,
  97. commanding,
  98. dataing,
  99. statusing,
  100. busfreeing,
  101. disconnecting,
  102. reselecting,
  103. sleeping
  104. };
  105. enum msg_phase {
  106. msg_none,
  107. msg_out,
  108. msg_out_xxx,
  109. msg_out_last,
  110. msg_in,
  111. msg_in_bad,
  112. };
  113. enum sdtr_phase {
  114. do_sdtr,
  115. sdtr_sent,
  116. sdtr_done
  117. };
  118. struct mesh_target {
  119. enum sdtr_phase sdtr_state;
  120. int sync_params;
  121. int data_goes_out; /* guess as to data direction */
  122. struct scsi_cmnd *current_req;
  123. u32 saved_ptr;
  124. #ifdef MESH_DBG
  125. int log_ix;
  126. int n_log;
  127. struct dbglog log[N_DBG_LOG];
  128. #endif
  129. };
  130. struct mesh_state {
  131. volatile struct mesh_regs __iomem *mesh;
  132. int meshintr;
  133. volatile struct dbdma_regs __iomem *dma;
  134. int dmaintr;
  135. struct Scsi_Host *host;
  136. struct mesh_state *next;
  137. struct scsi_cmnd *request_q;
  138. struct scsi_cmnd *request_qtail;
  139. enum mesh_phase phase; /* what we're currently trying to do */
  140. enum msg_phase msgphase;
  141. int conn_tgt; /* target we're connected to */
  142. struct scsi_cmnd *current_req; /* req we're currently working on */
  143. int data_ptr;
  144. int dma_started;
  145. int dma_count;
  146. int stat;
  147. int aborting;
  148. int expect_reply;
  149. int n_msgin;
  150. u8 msgin[16];
  151. int n_msgout;
  152. int last_n_msgout;
  153. u8 msgout[16];
  154. struct dbdma_cmd *dma_cmds; /* space for dbdma commands, aligned */
  155. dma_addr_t dma_cmd_bus;
  156. void *dma_cmd_space;
  157. int dma_cmd_size;
  158. int clk_freq;
  159. struct mesh_target tgts[8];
  160. struct macio_dev *mdev;
  161. struct pci_dev* pdev;
  162. #ifdef MESH_DBG
  163. int log_ix;
  164. int n_log;
  165. struct dbglog log[N_DBG_SLOG];
  166. #endif
  167. };
  168. /*
  169. * Driver is too messy, we need a few prototypes...
  170. */
  171. static void mesh_done(struct mesh_state *ms, int start_next);
  172. static void mesh_interrupt(struct mesh_state *ms);
  173. static void cmd_complete(struct mesh_state *ms);
  174. static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd);
  175. static void halt_dma(struct mesh_state *ms);
  176. static void phase_mismatch(struct mesh_state *ms);
  177. /*
  178. * Some debugging & logging routines
  179. */
  180. #ifdef MESH_DBG
  181. static inline u32 readtb(void)
  182. {
  183. u32 tb;
  184. #ifdef DBG_USE_TB
  185. /* Beware: if you enable this, it will crash on 601s. */
  186. asm ("mftb %0" : "=r" (tb) : );
  187. #else
  188. tb = 0;
  189. #endif
  190. return tb;
  191. }
  192. static void dlog(struct mesh_state *ms, char *fmt, int a)
  193. {
  194. struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
  195. struct dbglog *tlp, *slp;
  196. tlp = &tp->log[tp->log_ix];
  197. slp = &ms->log[ms->log_ix];
  198. tlp->fmt = fmt;
  199. tlp->tb = readtb();
  200. tlp->phase = (ms->msgphase << 4) + ms->phase;
  201. tlp->bs0 = ms->mesh->bus_status0;
  202. tlp->bs1 = ms->mesh->bus_status1;
  203. tlp->tgt = ms->conn_tgt;
  204. tlp->d = a;
  205. *slp = *tlp;
  206. if (++tp->log_ix >= N_DBG_LOG)
  207. tp->log_ix = 0;
  208. if (tp->n_log < N_DBG_LOG)
  209. ++tp->n_log;
  210. if (++ms->log_ix >= N_DBG_SLOG)
  211. ms->log_ix = 0;
  212. if (ms->n_log < N_DBG_SLOG)
  213. ++ms->n_log;
  214. }
  215. static void dumplog(struct mesh_state *ms, int t)
  216. {
  217. struct mesh_target *tp = &ms->tgts[t];
  218. struct dbglog *lp;
  219. int i;
  220. if (tp->n_log == 0)
  221. return;
  222. i = tp->log_ix - tp->n_log;
  223. if (i < 0)
  224. i += N_DBG_LOG;
  225. tp->n_log = 0;
  226. do {
  227. lp = &tp->log[i];
  228. printk(KERN_DEBUG "mesh log %d: bs=%.2x%.2x ph=%.2x ",
  229. t, lp->bs1, lp->bs0, lp->phase);
  230. #ifdef DBG_USE_TB
  231. printk("tb=%10u ", lp->tb);
  232. #endif
  233. printk(lp->fmt, lp->d);
  234. printk("\n");
  235. if (++i >= N_DBG_LOG)
  236. i = 0;
  237. } while (i != tp->log_ix);
  238. }
  239. static void dumpslog(struct mesh_state *ms)
  240. {
  241. struct dbglog *lp;
  242. int i;
  243. if (ms->n_log == 0)
  244. return;
  245. i = ms->log_ix - ms->n_log;
  246. if (i < 0)
  247. i += N_DBG_SLOG;
  248. ms->n_log = 0;
  249. do {
  250. lp = &ms->log[i];
  251. printk(KERN_DEBUG "mesh log: bs=%.2x%.2x ph=%.2x t%d ",
  252. lp->bs1, lp->bs0, lp->phase, lp->tgt);
  253. #ifdef DBG_USE_TB
  254. printk("tb=%10u ", lp->tb);
  255. #endif
  256. printk(lp->fmt, lp->d);
  257. printk("\n");
  258. if (++i >= N_DBG_SLOG)
  259. i = 0;
  260. } while (i != ms->log_ix);
  261. }
  262. #else
  263. static inline void dlog(struct mesh_state *ms, char *fmt, int a)
  264. {}
  265. static inline void dumplog(struct mesh_state *ms, int tgt)
  266. {}
  267. static inline void dumpslog(struct mesh_state *ms)
  268. {}
  269. #endif /* MESH_DBG */
  270. #define MKWORD(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
  271. static void
  272. mesh_dump_regs(struct mesh_state *ms)
  273. {
  274. volatile struct mesh_regs __iomem *mr = ms->mesh;
  275. volatile struct dbdma_regs __iomem *md = ms->dma;
  276. int t;
  277. struct mesh_target *tp;
  278. printk(KERN_DEBUG "mesh: state at %p, regs at %p, dma at %p\n",
  279. ms, mr, md);
  280. printk(KERN_DEBUG " ct=%4x seq=%2x bs=%4x fc=%2x "
  281. "exc=%2x err=%2x im=%2x int=%2x sp=%2x\n",
  282. (mr->count_hi << 8) + mr->count_lo, mr->sequence,
  283. (mr->bus_status1 << 8) + mr->bus_status0, mr->fifo_count,
  284. mr->exception, mr->error, mr->intr_mask, mr->interrupt,
  285. mr->sync_params);
  286. while(in_8(&mr->fifo_count))
  287. printk(KERN_DEBUG " fifo data=%.2x\n",in_8(&mr->fifo));
  288. printk(KERN_DEBUG " dma stat=%x cmdptr=%x\n",
  289. in_le32(&md->status), in_le32(&md->cmdptr));
  290. printk(KERN_DEBUG " phase=%d msgphase=%d conn_tgt=%d data_ptr=%d\n",
  291. ms->phase, ms->msgphase, ms->conn_tgt, ms->data_ptr);
  292. printk(KERN_DEBUG " dma_st=%d dma_ct=%d n_msgout=%d\n",
  293. ms->dma_started, ms->dma_count, ms->n_msgout);
  294. for (t = 0; t < 8; ++t) {
  295. tp = &ms->tgts[t];
  296. if (tp->current_req == NULL)
  297. continue;
  298. printk(KERN_DEBUG " target %d: req=%p goes_out=%d saved_ptr=%d\n",
  299. t, tp->current_req, tp->data_goes_out, tp->saved_ptr);
  300. }
  301. }
  302. /*
  303. * Flush write buffers on the bus path to the mesh
  304. */
  305. static inline void mesh_flush_io(volatile struct mesh_regs __iomem *mr)
  306. {
  307. (void)in_8(&mr->mesh_id);
  308. }
  309. /*
  310. * Complete a SCSI command
  311. */
  312. static void mesh_completed(struct mesh_state *ms, struct scsi_cmnd *cmd)
  313. {
  314. (*cmd->scsi_done)(cmd);
  315. }
  316. /* Called with meshinterrupt disabled, initialize the chipset
  317. * and eventually do the initial bus reset. The lock must not be
  318. * held since we can schedule.
  319. */
  320. static void mesh_init(struct mesh_state *ms)
  321. {
  322. volatile struct mesh_regs __iomem *mr = ms->mesh;
  323. volatile struct dbdma_regs __iomem *md = ms->dma;
  324. mesh_flush_io(mr);
  325. udelay(100);
  326. /* Reset controller */
  327. out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* stop dma */
  328. out_8(&mr->exception, 0xff); /* clear all exception bits */
  329. out_8(&mr->error, 0xff); /* clear all error bits */
  330. out_8(&mr->sequence, SEQ_RESETMESH);
  331. mesh_flush_io(mr);
  332. udelay(10);
  333. out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  334. out_8(&mr->source_id, ms->host->this_id);
  335. out_8(&mr->sel_timeout, 25); /* 250ms */
  336. out_8(&mr->sync_params, ASYNC_PARAMS);
  337. if (init_reset_delay) {
  338. printk(KERN_INFO "mesh: performing initial bus reset...\n");
  339. /* Reset bus */
  340. out_8(&mr->bus_status1, BS1_RST); /* assert RST */
  341. mesh_flush_io(mr);
  342. udelay(30); /* leave it on for >= 25us */
  343. out_8(&mr->bus_status1, 0); /* negate RST */
  344. mesh_flush_io(mr);
  345. /* Wait for bus to come back */
  346. msleep(init_reset_delay);
  347. }
  348. /* Reconfigure controller */
  349. out_8(&mr->interrupt, 0xff); /* clear all interrupt bits */
  350. out_8(&mr->sequence, SEQ_FLUSHFIFO);
  351. mesh_flush_io(mr);
  352. udelay(1);
  353. out_8(&mr->sync_params, ASYNC_PARAMS);
  354. out_8(&mr->sequence, SEQ_ENBRESEL);
  355. ms->phase = idle;
  356. ms->msgphase = msg_none;
  357. }
  358. static void mesh_start_cmd(struct mesh_state *ms, struct scsi_cmnd *cmd)
  359. {
  360. volatile struct mesh_regs __iomem *mr = ms->mesh;
  361. int t, id;
  362. id = cmd->device->id;
  363. ms->current_req = cmd;
  364. ms->tgts[id].data_goes_out = cmd->sc_data_direction == DMA_TO_DEVICE;
  365. ms->tgts[id].current_req = cmd;
  366. #if 1
  367. if (DEBUG_TARGET(cmd)) {
  368. int i;
  369. printk(KERN_DEBUG "mesh_start: %p ser=%lu tgt=%d cmd=",
  370. cmd, cmd->serial_number, id);
  371. for (i = 0; i < cmd->cmd_len; ++i)
  372. printk(" %x", cmd->cmnd[i]);
  373. printk(" use_sg=%d buffer=%p bufflen=%u\n",
  374. scsi_sg_count(cmd), scsi_sglist(cmd), scsi_bufflen(cmd));
  375. }
  376. #endif
  377. if (ms->dma_started)
  378. panic("mesh: double DMA start !\n");
  379. ms->phase = arbitrating;
  380. ms->msgphase = msg_none;
  381. ms->data_ptr = 0;
  382. ms->dma_started = 0;
  383. ms->n_msgout = 0;
  384. ms->last_n_msgout = 0;
  385. ms->expect_reply = 0;
  386. ms->conn_tgt = id;
  387. ms->tgts[id].saved_ptr = 0;
  388. ms->stat = DID_OK;
  389. ms->aborting = 0;
  390. #ifdef MESH_DBG
  391. ms->tgts[id].n_log = 0;
  392. dlog(ms, "start cmd=%x", (int) cmd);
  393. #endif
  394. /* Off we go */
  395. dlog(ms, "about to arb, intr/exc/err/fc=%.8x",
  396. MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
  397. out_8(&mr->interrupt, INT_CMDDONE);
  398. out_8(&mr->sequence, SEQ_ENBRESEL);
  399. mesh_flush_io(mr);
  400. udelay(1);
  401. if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
  402. /*
  403. * Some other device has the bus or is arbitrating for it -
  404. * probably a target which is about to reselect us.
  405. */
  406. dlog(ms, "busy b4 arb, intr/exc/err/fc=%.8x",
  407. MKWORD(mr->interrupt, mr->exception,
  408. mr->error, mr->fifo_count));
  409. for (t = 100; t > 0; --t) {
  410. if ((in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) == 0)
  411. break;
  412. if (in_8(&mr->interrupt) != 0) {
  413. dlog(ms, "intr b4 arb, intr/exc/err/fc=%.8x",
  414. MKWORD(mr->interrupt, mr->exception,
  415. mr->error, mr->fifo_count));
  416. mesh_interrupt(ms);
  417. if (ms->phase != arbitrating)
  418. return;
  419. }
  420. udelay(1);
  421. }
  422. if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
  423. /* XXX should try again in a little while */
  424. ms->stat = DID_BUS_BUSY;
  425. ms->phase = idle;
  426. mesh_done(ms, 0);
  427. return;
  428. }
  429. }
  430. /*
  431. * Apparently the mesh has a bug where it will assert both its
  432. * own bit and the target's bit on the bus during arbitration.
  433. */
  434. out_8(&mr->dest_id, mr->source_id);
  435. /*
  436. * There appears to be a race with reselection sometimes,
  437. * where a target reselects us just as we issue the
  438. * arbitrate command. It seems that then the arbitrate
  439. * command just hangs waiting for the bus to be free
  440. * without giving us a reselection exception.
  441. * The only way I have found to get it to respond correctly
  442. * is this: disable reselection before issuing the arbitrate
  443. * command, then after issuing it, if it looks like a target
  444. * is trying to reselect us, reset the mesh and then enable
  445. * reselection.
  446. */
  447. out_8(&mr->sequence, SEQ_DISRESEL);
  448. if (in_8(&mr->interrupt) != 0) {
  449. dlog(ms, "intr after disresel, intr/exc/err/fc=%.8x",
  450. MKWORD(mr->interrupt, mr->exception,
  451. mr->error, mr->fifo_count));
  452. mesh_interrupt(ms);
  453. if (ms->phase != arbitrating)
  454. return;
  455. dlog(ms, "after intr after disresel, intr/exc/err/fc=%.8x",
  456. MKWORD(mr->interrupt, mr->exception,
  457. mr->error, mr->fifo_count));
  458. }
  459. out_8(&mr->sequence, SEQ_ARBITRATE);
  460. for (t = 230; t > 0; --t) {
  461. if (in_8(&mr->interrupt) != 0)
  462. break;
  463. udelay(1);
  464. }
  465. dlog(ms, "after arb, intr/exc/err/fc=%.8x",
  466. MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
  467. if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
  468. && (in_8(&mr->bus_status0) & BS0_IO)) {
  469. /* looks like a reselection - try resetting the mesh */
  470. dlog(ms, "resel? after arb, intr/exc/err/fc=%.8x",
  471. MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
  472. out_8(&mr->sequence, SEQ_RESETMESH);
  473. mesh_flush_io(mr);
  474. udelay(10);
  475. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  476. out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  477. out_8(&mr->sequence, SEQ_ENBRESEL);
  478. mesh_flush_io(mr);
  479. for (t = 10; t > 0 && in_8(&mr->interrupt) == 0; --t)
  480. udelay(1);
  481. dlog(ms, "tried reset after arb, intr/exc/err/fc=%.8x",
  482. MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
  483. #ifndef MESH_MULTIPLE_HOSTS
  484. if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
  485. && (in_8(&mr->bus_status0) & BS0_IO)) {
  486. printk(KERN_ERR "mesh: controller not responding"
  487. " to reselection!\n");
  488. /*
  489. * If this is a target reselecting us, and the
  490. * mesh isn't responding, the higher levels of
  491. * the scsi code will eventually time out and
  492. * reset the bus.
  493. */
  494. }
  495. #endif
  496. }
  497. }
  498. /*
  499. * Start the next command for a MESH.
  500. * Should be called with interrupts disabled.
  501. */
  502. static void mesh_start(struct mesh_state *ms)
  503. {
  504. struct scsi_cmnd *cmd, *prev, *next;
  505. if (ms->phase != idle || ms->current_req != NULL) {
  506. printk(KERN_ERR "inappropriate mesh_start (phase=%d, ms=%p)",
  507. ms->phase, ms);
  508. return;
  509. }
  510. while (ms->phase == idle) {
  511. prev = NULL;
  512. for (cmd = ms->request_q; ; cmd = (struct scsi_cmnd *) cmd->host_scribble) {
  513. if (cmd == NULL)
  514. return;
  515. if (ms->tgts[cmd->device->id].current_req == NULL)
  516. break;
  517. prev = cmd;
  518. }
  519. next = (struct scsi_cmnd *) cmd->host_scribble;
  520. if (prev == NULL)
  521. ms->request_q = next;
  522. else
  523. prev->host_scribble = (void *) next;
  524. if (next == NULL)
  525. ms->request_qtail = prev;
  526. mesh_start_cmd(ms, cmd);
  527. }
  528. }
  529. static void mesh_done(struct mesh_state *ms, int start_next)
  530. {
  531. struct scsi_cmnd *cmd;
  532. struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
  533. cmd = ms->current_req;
  534. ms->current_req = NULL;
  535. tp->current_req = NULL;
  536. if (cmd) {
  537. cmd->result = (ms->stat << 16) + cmd->SCp.Status;
  538. if (ms->stat == DID_OK)
  539. cmd->result += (cmd->SCp.Message << 8);
  540. if (DEBUG_TARGET(cmd)) {
  541. printk(KERN_DEBUG "mesh_done: result = %x, data_ptr=%d, buflen=%d\n",
  542. cmd->result, ms->data_ptr, scsi_bufflen(cmd));
  543. #if 0
  544. /* needs to use sg? */
  545. if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12 || cmd->cmnd[0] == 3)
  546. && cmd->request_buffer != 0) {
  547. unsigned char *b = cmd->request_buffer;
  548. printk(KERN_DEBUG "buffer = %x %x %x %x %x %x %x %x\n",
  549. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
  550. }
  551. #endif
  552. }
  553. cmd->SCp.this_residual -= ms->data_ptr;
  554. mesh_completed(ms, cmd);
  555. }
  556. if (start_next) {
  557. out_8(&ms->mesh->sequence, SEQ_ENBRESEL);
  558. mesh_flush_io(ms->mesh);
  559. udelay(1);
  560. ms->phase = idle;
  561. mesh_start(ms);
  562. }
  563. }
  564. static inline void add_sdtr_msg(struct mesh_state *ms)
  565. {
  566. int i = ms->n_msgout;
  567. ms->msgout[i] = EXTENDED_MESSAGE;
  568. ms->msgout[i+1] = 3;
  569. ms->msgout[i+2] = EXTENDED_SDTR;
  570. ms->msgout[i+3] = mesh_sync_period/4;
  571. ms->msgout[i+4] = (ALLOW_SYNC(ms->conn_tgt)? mesh_sync_offset: 0);
  572. ms->n_msgout = i + 5;
  573. }
  574. static void set_sdtr(struct mesh_state *ms, int period, int offset)
  575. {
  576. struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
  577. volatile struct mesh_regs __iomem *mr = ms->mesh;
  578. int v, tr;
  579. tp->sdtr_state = sdtr_done;
  580. if (offset == 0) {
  581. /* asynchronous */
  582. if (SYNC_OFF(tp->sync_params))
  583. printk(KERN_INFO "mesh: target %d now asynchronous\n",
  584. ms->conn_tgt);
  585. tp->sync_params = ASYNC_PARAMS;
  586. out_8(&mr->sync_params, ASYNC_PARAMS);
  587. return;
  588. }
  589. /*
  590. * We need to compute ceil(clk_freq * period / 500e6) - 2
  591. * without incurring overflow.
  592. */
  593. v = (ms->clk_freq / 5000) * period;
  594. if (v <= 250000) {
  595. /* special case: sync_period == 5 * clk_period */
  596. v = 0;
  597. /* units of tr are 100kB/s */
  598. tr = (ms->clk_freq + 250000) / 500000;
  599. } else {
  600. /* sync_period == (v + 2) * 2 * clk_period */
  601. v = (v + 99999) / 100000 - 2;
  602. if (v > 15)
  603. v = 15; /* oops */
  604. tr = ((ms->clk_freq / (v + 2)) + 199999) / 200000;
  605. }
  606. if (offset > 15)
  607. offset = 15; /* can't happen */
  608. tp->sync_params = SYNC_PARAMS(offset, v);
  609. out_8(&mr->sync_params, tp->sync_params);
  610. printk(KERN_INFO "mesh: target %d synchronous at %d.%d MB/s\n",
  611. ms->conn_tgt, tr/10, tr%10);
  612. }
  613. static void start_phase(struct mesh_state *ms)
  614. {
  615. int i, seq, nb;
  616. volatile struct mesh_regs __iomem *mr = ms->mesh;
  617. volatile struct dbdma_regs __iomem *md = ms->dma;
  618. struct scsi_cmnd *cmd = ms->current_req;
  619. struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
  620. dlog(ms, "start_phase nmo/exc/fc/seq = %.8x",
  621. MKWORD(ms->n_msgout, mr->exception, mr->fifo_count, mr->sequence));
  622. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  623. seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
  624. switch (ms->msgphase) {
  625. case msg_none:
  626. break;
  627. case msg_in:
  628. out_8(&mr->count_hi, 0);
  629. out_8(&mr->count_lo, 1);
  630. out_8(&mr->sequence, SEQ_MSGIN + seq);
  631. ms->n_msgin = 0;
  632. return;
  633. case msg_out:
  634. /*
  635. * To make sure ATN drops before we assert ACK for
  636. * the last byte of the message, we have to do the
  637. * last byte specially.
  638. */
  639. if (ms->n_msgout <= 0) {
  640. printk(KERN_ERR "mesh: msg_out but n_msgout=%d\n",
  641. ms->n_msgout);
  642. mesh_dump_regs(ms);
  643. ms->msgphase = msg_none;
  644. break;
  645. }
  646. if (ALLOW_DEBUG(ms->conn_tgt)) {
  647. printk(KERN_DEBUG "mesh: sending %d msg bytes:",
  648. ms->n_msgout);
  649. for (i = 0; i < ms->n_msgout; ++i)
  650. printk(" %x", ms->msgout[i]);
  651. printk("\n");
  652. }
  653. dlog(ms, "msgout msg=%.8x", MKWORD(ms->n_msgout, ms->msgout[0],
  654. ms->msgout[1], ms->msgout[2]));
  655. out_8(&mr->count_hi, 0);
  656. out_8(&mr->sequence, SEQ_FLUSHFIFO);
  657. mesh_flush_io(mr);
  658. udelay(1);
  659. /*
  660. * If ATN is not already asserted, we assert it, then
  661. * issue a SEQ_MSGOUT to get the mesh to drop ACK.
  662. */
  663. if ((in_8(&mr->bus_status0) & BS0_ATN) == 0) {
  664. dlog(ms, "bus0 was %.2x explicitly asserting ATN", mr->bus_status0);
  665. out_8(&mr->bus_status0, BS0_ATN); /* explicit ATN */
  666. mesh_flush_io(mr);
  667. udelay(1);
  668. out_8(&mr->count_lo, 1);
  669. out_8(&mr->sequence, SEQ_MSGOUT + seq);
  670. out_8(&mr->bus_status0, 0); /* release explicit ATN */
  671. dlog(ms,"hace: after explicit ATN bus0=%.2x",mr->bus_status0);
  672. }
  673. if (ms->n_msgout == 1) {
  674. /*
  675. * We can't issue the SEQ_MSGOUT without ATN
  676. * until the target has asserted REQ. The logic
  677. * in cmd_complete handles both situations:
  678. * REQ already asserted or not.
  679. */
  680. cmd_complete(ms);
  681. } else {
  682. out_8(&mr->count_lo, ms->n_msgout - 1);
  683. out_8(&mr->sequence, SEQ_MSGOUT + seq);
  684. for (i = 0; i < ms->n_msgout - 1; ++i)
  685. out_8(&mr->fifo, ms->msgout[i]);
  686. }
  687. return;
  688. default:
  689. printk(KERN_ERR "mesh bug: start_phase msgphase=%d\n",
  690. ms->msgphase);
  691. }
  692. switch (ms->phase) {
  693. case selecting:
  694. out_8(&mr->dest_id, ms->conn_tgt);
  695. out_8(&mr->sequence, SEQ_SELECT + SEQ_ATN);
  696. break;
  697. case commanding:
  698. out_8(&mr->sync_params, tp->sync_params);
  699. out_8(&mr->count_hi, 0);
  700. if (cmd) {
  701. out_8(&mr->count_lo, cmd->cmd_len);
  702. out_8(&mr->sequence, SEQ_COMMAND + seq);
  703. for (i = 0; i < cmd->cmd_len; ++i)
  704. out_8(&mr->fifo, cmd->cmnd[i]);
  705. } else {
  706. out_8(&mr->count_lo, 6);
  707. out_8(&mr->sequence, SEQ_COMMAND + seq);
  708. for (i = 0; i < 6; ++i)
  709. out_8(&mr->fifo, 0);
  710. }
  711. break;
  712. case dataing:
  713. /* transfer data, if any */
  714. if (!ms->dma_started) {
  715. set_dma_cmds(ms, cmd);
  716. out_le32(&md->cmdptr, virt_to_phys(ms->dma_cmds));
  717. out_le32(&md->control, (RUN << 16) | RUN);
  718. ms->dma_started = 1;
  719. }
  720. nb = ms->dma_count;
  721. if (nb > 0xfff0)
  722. nb = 0xfff0;
  723. ms->dma_count -= nb;
  724. ms->data_ptr += nb;
  725. out_8(&mr->count_lo, nb);
  726. out_8(&mr->count_hi, nb >> 8);
  727. out_8(&mr->sequence, (tp->data_goes_out?
  728. SEQ_DATAOUT: SEQ_DATAIN) + SEQ_DMA_MODE + seq);
  729. break;
  730. case statusing:
  731. out_8(&mr->count_hi, 0);
  732. out_8(&mr->count_lo, 1);
  733. out_8(&mr->sequence, SEQ_STATUS + seq);
  734. break;
  735. case busfreeing:
  736. case disconnecting:
  737. out_8(&mr->sequence, SEQ_ENBRESEL);
  738. mesh_flush_io(mr);
  739. udelay(1);
  740. dlog(ms, "enbresel intr/exc/err/fc=%.8x",
  741. MKWORD(mr->interrupt, mr->exception, mr->error,
  742. mr->fifo_count));
  743. out_8(&mr->sequence, SEQ_BUSFREE);
  744. break;
  745. default:
  746. printk(KERN_ERR "mesh: start_phase called with phase=%d\n",
  747. ms->phase);
  748. dumpslog(ms);
  749. }
  750. }
  751. static inline void get_msgin(struct mesh_state *ms)
  752. {
  753. volatile struct mesh_regs __iomem *mr = ms->mesh;
  754. int i, n;
  755. n = mr->fifo_count;
  756. if (n != 0) {
  757. i = ms->n_msgin;
  758. ms->n_msgin = i + n;
  759. for (; n > 0; --n)
  760. ms->msgin[i++] = in_8(&mr->fifo);
  761. }
  762. }
  763. static inline int msgin_length(struct mesh_state *ms)
  764. {
  765. int b, n;
  766. n = 1;
  767. if (ms->n_msgin > 0) {
  768. b = ms->msgin[0];
  769. if (b == 1) {
  770. /* extended message */
  771. n = ms->n_msgin < 2? 2: ms->msgin[1] + 2;
  772. } else if (0x20 <= b && b <= 0x2f) {
  773. /* 2-byte message */
  774. n = 2;
  775. }
  776. }
  777. return n;
  778. }
  779. static void reselected(struct mesh_state *ms)
  780. {
  781. volatile struct mesh_regs __iomem *mr = ms->mesh;
  782. struct scsi_cmnd *cmd;
  783. struct mesh_target *tp;
  784. int b, t, prev;
  785. switch (ms->phase) {
  786. case idle:
  787. break;
  788. case arbitrating:
  789. if ((cmd = ms->current_req) != NULL) {
  790. /* put the command back on the queue */
  791. cmd->host_scribble = (void *) ms->request_q;
  792. if (ms->request_q == NULL)
  793. ms->request_qtail = cmd;
  794. ms->request_q = cmd;
  795. tp = &ms->tgts[cmd->device->id];
  796. tp->current_req = NULL;
  797. }
  798. break;
  799. case busfreeing:
  800. ms->phase = reselecting;
  801. mesh_done(ms, 0);
  802. break;
  803. case disconnecting:
  804. break;
  805. default:
  806. printk(KERN_ERR "mesh: reselected in phase %d/%d tgt %d\n",
  807. ms->msgphase, ms->phase, ms->conn_tgt);
  808. dumplog(ms, ms->conn_tgt);
  809. dumpslog(ms);
  810. }
  811. if (ms->dma_started) {
  812. printk(KERN_ERR "mesh: reselected with DMA started !\n");
  813. halt_dma(ms);
  814. }
  815. ms->current_req = NULL;
  816. ms->phase = dataing;
  817. ms->msgphase = msg_in;
  818. ms->n_msgout = 0;
  819. ms->last_n_msgout = 0;
  820. prev = ms->conn_tgt;
  821. /*
  822. * We seem to get abortive reselections sometimes.
  823. */
  824. while ((in_8(&mr->bus_status1) & BS1_BSY) == 0) {
  825. static int mesh_aborted_resels;
  826. mesh_aborted_resels++;
  827. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  828. mesh_flush_io(mr);
  829. udelay(1);
  830. out_8(&mr->sequence, SEQ_ENBRESEL);
  831. mesh_flush_io(mr);
  832. udelay(5);
  833. dlog(ms, "extra resel err/exc/fc = %.6x",
  834. MKWORD(0, mr->error, mr->exception, mr->fifo_count));
  835. }
  836. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  837. mesh_flush_io(mr);
  838. udelay(1);
  839. out_8(&mr->sequence, SEQ_ENBRESEL);
  840. mesh_flush_io(mr);
  841. udelay(1);
  842. out_8(&mr->sync_params, ASYNC_PARAMS);
  843. /*
  844. * Find out who reselected us.
  845. */
  846. if (in_8(&mr->fifo_count) == 0) {
  847. printk(KERN_ERR "mesh: reselection but nothing in fifo?\n");
  848. ms->conn_tgt = ms->host->this_id;
  849. goto bogus;
  850. }
  851. /* get the last byte in the fifo */
  852. do {
  853. b = in_8(&mr->fifo);
  854. dlog(ms, "reseldata %x", b);
  855. } while (in_8(&mr->fifo_count));
  856. for (t = 0; t < 8; ++t)
  857. if ((b & (1 << t)) != 0 && t != ms->host->this_id)
  858. break;
  859. if (b != (1 << t) + (1 << ms->host->this_id)) {
  860. printk(KERN_ERR "mesh: bad reselection data %x\n", b);
  861. ms->conn_tgt = ms->host->this_id;
  862. goto bogus;
  863. }
  864. /*
  865. * Set up to continue with that target's transfer.
  866. */
  867. ms->conn_tgt = t;
  868. tp = &ms->tgts[t];
  869. out_8(&mr->sync_params, tp->sync_params);
  870. if (ALLOW_DEBUG(t)) {
  871. printk(KERN_DEBUG "mesh: reselected by target %d\n", t);
  872. printk(KERN_DEBUG "mesh: saved_ptr=%x goes_out=%d cmd=%p\n",
  873. tp->saved_ptr, tp->data_goes_out, tp->current_req);
  874. }
  875. ms->current_req = tp->current_req;
  876. if (tp->current_req == NULL) {
  877. printk(KERN_ERR "mesh: reselected by tgt %d but no cmd!\n", t);
  878. goto bogus;
  879. }
  880. ms->data_ptr = tp->saved_ptr;
  881. dlog(ms, "resel prev tgt=%d", prev);
  882. dlog(ms, "resel err/exc=%.4x", MKWORD(0, 0, mr->error, mr->exception));
  883. start_phase(ms);
  884. return;
  885. bogus:
  886. dumplog(ms, ms->conn_tgt);
  887. dumpslog(ms);
  888. ms->data_ptr = 0;
  889. ms->aborting = 1;
  890. start_phase(ms);
  891. }
  892. static void do_abort(struct mesh_state *ms)
  893. {
  894. ms->msgout[0] = ABORT;
  895. ms->n_msgout = 1;
  896. ms->aborting = 1;
  897. ms->stat = DID_ABORT;
  898. dlog(ms, "abort", 0);
  899. }
  900. static void handle_reset(struct mesh_state *ms)
  901. {
  902. int tgt;
  903. struct mesh_target *tp;
  904. struct scsi_cmnd *cmd;
  905. volatile struct mesh_regs __iomem *mr = ms->mesh;
  906. for (tgt = 0; tgt < 8; ++tgt) {
  907. tp = &ms->tgts[tgt];
  908. if ((cmd = tp->current_req) != NULL) {
  909. cmd->result = DID_RESET << 16;
  910. tp->current_req = NULL;
  911. mesh_completed(ms, cmd);
  912. }
  913. ms->tgts[tgt].sdtr_state = do_sdtr;
  914. ms->tgts[tgt].sync_params = ASYNC_PARAMS;
  915. }
  916. ms->current_req = NULL;
  917. while ((cmd = ms->request_q) != NULL) {
  918. ms->request_q = (struct scsi_cmnd *) cmd->host_scribble;
  919. cmd->result = DID_RESET << 16;
  920. mesh_completed(ms, cmd);
  921. }
  922. ms->phase = idle;
  923. ms->msgphase = msg_none;
  924. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  925. out_8(&mr->sequence, SEQ_FLUSHFIFO);
  926. mesh_flush_io(mr);
  927. udelay(1);
  928. out_8(&mr->sync_params, ASYNC_PARAMS);
  929. out_8(&mr->sequence, SEQ_ENBRESEL);
  930. }
  931. static irqreturn_t do_mesh_interrupt(int irq, void *dev_id)
  932. {
  933. unsigned long flags;
  934. struct mesh_state *ms = dev_id;
  935. struct Scsi_Host *dev = ms->host;
  936. spin_lock_irqsave(dev->host_lock, flags);
  937. mesh_interrupt(ms);
  938. spin_unlock_irqrestore(dev->host_lock, flags);
  939. return IRQ_HANDLED;
  940. }
  941. static void handle_error(struct mesh_state *ms)
  942. {
  943. int err, exc, count;
  944. volatile struct mesh_regs __iomem *mr = ms->mesh;
  945. err = in_8(&mr->error);
  946. exc = in_8(&mr->exception);
  947. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  948. dlog(ms, "error err/exc/fc/cl=%.8x",
  949. MKWORD(err, exc, mr->fifo_count, mr->count_lo));
  950. if (err & ERR_SCSIRESET) {
  951. /* SCSI bus was reset */
  952. printk(KERN_INFO "mesh: SCSI bus reset detected: "
  953. "waiting for end...");
  954. while ((in_8(&mr->bus_status1) & BS1_RST) != 0)
  955. udelay(1);
  956. printk("done\n");
  957. handle_reset(ms);
  958. /* request_q is empty, no point in mesh_start() */
  959. return;
  960. }
  961. if (err & ERR_UNEXPDISC) {
  962. /* Unexpected disconnect */
  963. if (exc & EXC_RESELECTED) {
  964. reselected(ms);
  965. return;
  966. }
  967. if (!ms->aborting) {
  968. printk(KERN_WARNING "mesh: target %d aborted\n",
  969. ms->conn_tgt);
  970. dumplog(ms, ms->conn_tgt);
  971. dumpslog(ms);
  972. }
  973. out_8(&mr->interrupt, INT_CMDDONE);
  974. ms->stat = DID_ABORT;
  975. mesh_done(ms, 1);
  976. return;
  977. }
  978. if (err & ERR_PARITY) {
  979. if (ms->msgphase == msg_in) {
  980. printk(KERN_ERR "mesh: msg parity error, target %d\n",
  981. ms->conn_tgt);
  982. ms->msgout[0] = MSG_PARITY_ERROR;
  983. ms->n_msgout = 1;
  984. ms->msgphase = msg_in_bad;
  985. cmd_complete(ms);
  986. return;
  987. }
  988. if (ms->stat == DID_OK) {
  989. printk(KERN_ERR "mesh: parity error, target %d\n",
  990. ms->conn_tgt);
  991. ms->stat = DID_PARITY;
  992. }
  993. count = (mr->count_hi << 8) + mr->count_lo;
  994. if (count == 0) {
  995. cmd_complete(ms);
  996. } else {
  997. /* reissue the data transfer command */
  998. out_8(&mr->sequence, mr->sequence);
  999. }
  1000. return;
  1001. }
  1002. if (err & ERR_SEQERR) {
  1003. if (exc & EXC_RESELECTED) {
  1004. /* This can happen if we issue a command to
  1005. get the bus just after the target reselects us. */
  1006. static int mesh_resel_seqerr;
  1007. mesh_resel_seqerr++;
  1008. reselected(ms);
  1009. return;
  1010. }
  1011. if (exc == EXC_PHASEMM) {
  1012. static int mesh_phasemm_seqerr;
  1013. mesh_phasemm_seqerr++;
  1014. phase_mismatch(ms);
  1015. return;
  1016. }
  1017. printk(KERN_ERR "mesh: sequence error (err=%x exc=%x)\n",
  1018. err, exc);
  1019. } else {
  1020. printk(KERN_ERR "mesh: unknown error %x (exc=%x)\n", err, exc);
  1021. }
  1022. mesh_dump_regs(ms);
  1023. dumplog(ms, ms->conn_tgt);
  1024. if (ms->phase > selecting && (in_8(&mr->bus_status1) & BS1_BSY)) {
  1025. /* try to do what the target wants */
  1026. do_abort(ms);
  1027. phase_mismatch(ms);
  1028. return;
  1029. }
  1030. ms->stat = DID_ERROR;
  1031. mesh_done(ms, 1);
  1032. }
  1033. static void handle_exception(struct mesh_state *ms)
  1034. {
  1035. int exc;
  1036. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1037. exc = in_8(&mr->exception);
  1038. out_8(&mr->interrupt, INT_EXCEPTION | INT_CMDDONE);
  1039. if (exc & EXC_RESELECTED) {
  1040. static int mesh_resel_exc;
  1041. mesh_resel_exc++;
  1042. reselected(ms);
  1043. } else if (exc == EXC_ARBLOST) {
  1044. printk(KERN_DEBUG "mesh: lost arbitration\n");
  1045. ms->stat = DID_BUS_BUSY;
  1046. mesh_done(ms, 1);
  1047. } else if (exc == EXC_SELTO) {
  1048. /* selection timed out */
  1049. ms->stat = DID_BAD_TARGET;
  1050. mesh_done(ms, 1);
  1051. } else if (exc == EXC_PHASEMM) {
  1052. /* target wants to do something different:
  1053. find out what it wants and do it. */
  1054. phase_mismatch(ms);
  1055. } else {
  1056. printk(KERN_ERR "mesh: can't cope with exception %x\n", exc);
  1057. mesh_dump_regs(ms);
  1058. dumplog(ms, ms->conn_tgt);
  1059. do_abort(ms);
  1060. phase_mismatch(ms);
  1061. }
  1062. }
  1063. static void handle_msgin(struct mesh_state *ms)
  1064. {
  1065. int i, code;
  1066. struct scsi_cmnd *cmd = ms->current_req;
  1067. struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
  1068. if (ms->n_msgin == 0)
  1069. return;
  1070. code = ms->msgin[0];
  1071. if (ALLOW_DEBUG(ms->conn_tgt)) {
  1072. printk(KERN_DEBUG "got %d message bytes:", ms->n_msgin);
  1073. for (i = 0; i < ms->n_msgin; ++i)
  1074. printk(" %x", ms->msgin[i]);
  1075. printk("\n");
  1076. }
  1077. dlog(ms, "msgin msg=%.8x",
  1078. MKWORD(ms->n_msgin, code, ms->msgin[1], ms->msgin[2]));
  1079. ms->expect_reply = 0;
  1080. ms->n_msgout = 0;
  1081. if (ms->n_msgin < msgin_length(ms))
  1082. goto reject;
  1083. if (cmd)
  1084. cmd->SCp.Message = code;
  1085. switch (code) {
  1086. case COMMAND_COMPLETE:
  1087. break;
  1088. case EXTENDED_MESSAGE:
  1089. switch (ms->msgin[2]) {
  1090. case EXTENDED_MODIFY_DATA_POINTER:
  1091. ms->data_ptr += (ms->msgin[3] << 24) + ms->msgin[6]
  1092. + (ms->msgin[4] << 16) + (ms->msgin[5] << 8);
  1093. break;
  1094. case EXTENDED_SDTR:
  1095. if (tp->sdtr_state != sdtr_sent) {
  1096. /* reply with an SDTR */
  1097. add_sdtr_msg(ms);
  1098. /* limit period to at least his value,
  1099. offset to no more than his */
  1100. if (ms->msgout[3] < ms->msgin[3])
  1101. ms->msgout[3] = ms->msgin[3];
  1102. if (ms->msgout[4] > ms->msgin[4])
  1103. ms->msgout[4] = ms->msgin[4];
  1104. set_sdtr(ms, ms->msgout[3], ms->msgout[4]);
  1105. ms->msgphase = msg_out;
  1106. } else {
  1107. set_sdtr(ms, ms->msgin[3], ms->msgin[4]);
  1108. }
  1109. break;
  1110. default:
  1111. goto reject;
  1112. }
  1113. break;
  1114. case SAVE_POINTERS:
  1115. tp->saved_ptr = ms->data_ptr;
  1116. break;
  1117. case RESTORE_POINTERS:
  1118. ms->data_ptr = tp->saved_ptr;
  1119. break;
  1120. case DISCONNECT:
  1121. ms->phase = disconnecting;
  1122. break;
  1123. case ABORT:
  1124. break;
  1125. case MESSAGE_REJECT:
  1126. if (tp->sdtr_state == sdtr_sent)
  1127. set_sdtr(ms, 0, 0);
  1128. break;
  1129. case NOP:
  1130. break;
  1131. default:
  1132. if (IDENTIFY_BASE <= code && code <= IDENTIFY_BASE + 7) {
  1133. if (cmd == NULL) {
  1134. do_abort(ms);
  1135. ms->msgphase = msg_out;
  1136. } else if (code != cmd->device->lun + IDENTIFY_BASE) {
  1137. printk(KERN_WARNING "mesh: lun mismatch "
  1138. "(%d != %d) on reselection from "
  1139. "target %d\n", code - IDENTIFY_BASE,
  1140. cmd->device->lun, ms->conn_tgt);
  1141. }
  1142. break;
  1143. }
  1144. goto reject;
  1145. }
  1146. return;
  1147. reject:
  1148. printk(KERN_WARNING "mesh: rejecting message from target %d:",
  1149. ms->conn_tgt);
  1150. for (i = 0; i < ms->n_msgin; ++i)
  1151. printk(" %x", ms->msgin[i]);
  1152. printk("\n");
  1153. ms->msgout[0] = MESSAGE_REJECT;
  1154. ms->n_msgout = 1;
  1155. ms->msgphase = msg_out;
  1156. }
  1157. /*
  1158. * Set up DMA commands for transferring data.
  1159. */
  1160. static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd)
  1161. {
  1162. int i, dma_cmd, total, off, dtot;
  1163. struct scatterlist *scl;
  1164. struct dbdma_cmd *dcmds;
  1165. dma_cmd = ms->tgts[ms->conn_tgt].data_goes_out?
  1166. OUTPUT_MORE: INPUT_MORE;
  1167. dcmds = ms->dma_cmds;
  1168. dtot = 0;
  1169. if (cmd) {
  1170. int nseg;
  1171. cmd->SCp.this_residual = scsi_bufflen(cmd);
  1172. nseg = scsi_dma_map(cmd);
  1173. BUG_ON(nseg < 0);
  1174. if (nseg) {
  1175. total = 0;
  1176. off = ms->data_ptr;
  1177. scsi_for_each_sg(cmd, scl, nseg, i) {
  1178. u32 dma_addr = sg_dma_address(scl);
  1179. u32 dma_len = sg_dma_len(scl);
  1180. total += scl->length;
  1181. if (off >= dma_len) {
  1182. off -= dma_len;
  1183. continue;
  1184. }
  1185. if (dma_len > 0xffff)
  1186. panic("mesh: scatterlist element >= 64k");
  1187. st_le16(&dcmds->req_count, dma_len - off);
  1188. st_le16(&dcmds->command, dma_cmd);
  1189. st_le32(&dcmds->phy_addr, dma_addr + off);
  1190. dcmds->xfer_status = 0;
  1191. ++dcmds;
  1192. dtot += dma_len - off;
  1193. off = 0;
  1194. }
  1195. }
  1196. }
  1197. if (dtot == 0) {
  1198. /* Either the target has overrun our buffer,
  1199. or the caller didn't provide a buffer. */
  1200. static char mesh_extra_buf[64];
  1201. dtot = sizeof(mesh_extra_buf);
  1202. st_le16(&dcmds->req_count, dtot);
  1203. st_le32(&dcmds->phy_addr, virt_to_phys(mesh_extra_buf));
  1204. dcmds->xfer_status = 0;
  1205. ++dcmds;
  1206. }
  1207. dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
  1208. st_le16(&dcmds[-1].command, dma_cmd);
  1209. memset(dcmds, 0, sizeof(*dcmds));
  1210. st_le16(&dcmds->command, DBDMA_STOP);
  1211. ms->dma_count = dtot;
  1212. }
  1213. static void halt_dma(struct mesh_state *ms)
  1214. {
  1215. volatile struct dbdma_regs __iomem *md = ms->dma;
  1216. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1217. struct scsi_cmnd *cmd = ms->current_req;
  1218. int t, nb;
  1219. if (!ms->tgts[ms->conn_tgt].data_goes_out) {
  1220. /* wait a little while until the fifo drains */
  1221. t = 50;
  1222. while (t > 0 && in_8(&mr->fifo_count) != 0
  1223. && (in_le32(&md->status) & ACTIVE) != 0) {
  1224. --t;
  1225. udelay(1);
  1226. }
  1227. }
  1228. out_le32(&md->control, RUN << 16); /* turn off RUN bit */
  1229. nb = (mr->count_hi << 8) + mr->count_lo;
  1230. dlog(ms, "halt_dma fc/count=%.6x",
  1231. MKWORD(0, mr->fifo_count, 0, nb));
  1232. if (ms->tgts[ms->conn_tgt].data_goes_out)
  1233. nb += mr->fifo_count;
  1234. /* nb is the number of bytes not yet transferred
  1235. to/from the target. */
  1236. ms->data_ptr -= nb;
  1237. dlog(ms, "data_ptr %x", ms->data_ptr);
  1238. if (ms->data_ptr < 0) {
  1239. printk(KERN_ERR "mesh: halt_dma: data_ptr=%d (nb=%d, ms=%p)\n",
  1240. ms->data_ptr, nb, ms);
  1241. ms->data_ptr = 0;
  1242. #ifdef MESH_DBG
  1243. dumplog(ms, ms->conn_tgt);
  1244. dumpslog(ms);
  1245. #endif /* MESH_DBG */
  1246. } else if (cmd && scsi_bufflen(cmd) &&
  1247. ms->data_ptr > scsi_bufflen(cmd)) {
  1248. printk(KERN_DEBUG "mesh: target %d overrun, "
  1249. "data_ptr=%x total=%x goes_out=%d\n",
  1250. ms->conn_tgt, ms->data_ptr, scsi_bufflen(cmd),
  1251. ms->tgts[ms->conn_tgt].data_goes_out);
  1252. }
  1253. scsi_dma_unmap(cmd);
  1254. ms->dma_started = 0;
  1255. }
  1256. static void phase_mismatch(struct mesh_state *ms)
  1257. {
  1258. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1259. int phase;
  1260. dlog(ms, "phasemm ch/cl/seq/fc=%.8x",
  1261. MKWORD(mr->count_hi, mr->count_lo, mr->sequence, mr->fifo_count));
  1262. phase = in_8(&mr->bus_status0) & BS0_PHASE;
  1263. if (ms->msgphase == msg_out_xxx && phase == BP_MSGOUT) {
  1264. /* output the last byte of the message, without ATN */
  1265. out_8(&mr->count_lo, 1);
  1266. out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
  1267. mesh_flush_io(mr);
  1268. udelay(1);
  1269. out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
  1270. ms->msgphase = msg_out_last;
  1271. return;
  1272. }
  1273. if (ms->msgphase == msg_in) {
  1274. get_msgin(ms);
  1275. if (ms->n_msgin)
  1276. handle_msgin(ms);
  1277. }
  1278. if (ms->dma_started)
  1279. halt_dma(ms);
  1280. if (mr->fifo_count) {
  1281. out_8(&mr->sequence, SEQ_FLUSHFIFO);
  1282. mesh_flush_io(mr);
  1283. udelay(1);
  1284. }
  1285. ms->msgphase = msg_none;
  1286. switch (phase) {
  1287. case BP_DATAIN:
  1288. ms->tgts[ms->conn_tgt].data_goes_out = 0;
  1289. ms->phase = dataing;
  1290. break;
  1291. case BP_DATAOUT:
  1292. ms->tgts[ms->conn_tgt].data_goes_out = 1;
  1293. ms->phase = dataing;
  1294. break;
  1295. case BP_COMMAND:
  1296. ms->phase = commanding;
  1297. break;
  1298. case BP_STATUS:
  1299. ms->phase = statusing;
  1300. break;
  1301. case BP_MSGIN:
  1302. ms->msgphase = msg_in;
  1303. ms->n_msgin = 0;
  1304. break;
  1305. case BP_MSGOUT:
  1306. ms->msgphase = msg_out;
  1307. if (ms->n_msgout == 0) {
  1308. if (ms->aborting) {
  1309. do_abort(ms);
  1310. } else {
  1311. if (ms->last_n_msgout == 0) {
  1312. printk(KERN_DEBUG
  1313. "mesh: no msg to repeat\n");
  1314. ms->msgout[0] = NOP;
  1315. ms->last_n_msgout = 1;
  1316. }
  1317. ms->n_msgout = ms->last_n_msgout;
  1318. }
  1319. }
  1320. break;
  1321. default:
  1322. printk(KERN_DEBUG "mesh: unknown scsi phase %x\n", phase);
  1323. ms->stat = DID_ERROR;
  1324. mesh_done(ms, 1);
  1325. return;
  1326. }
  1327. start_phase(ms);
  1328. }
  1329. static void cmd_complete(struct mesh_state *ms)
  1330. {
  1331. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1332. struct scsi_cmnd *cmd = ms->current_req;
  1333. struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
  1334. int seq, n, t;
  1335. dlog(ms, "cmd_complete fc=%x", mr->fifo_count);
  1336. seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
  1337. switch (ms->msgphase) {
  1338. case msg_out_xxx:
  1339. /* huh? we expected a phase mismatch */
  1340. ms->n_msgin = 0;
  1341. ms->msgphase = msg_in;
  1342. /* fall through */
  1343. case msg_in:
  1344. /* should have some message bytes in fifo */
  1345. get_msgin(ms);
  1346. n = msgin_length(ms);
  1347. if (ms->n_msgin < n) {
  1348. out_8(&mr->count_lo, n - ms->n_msgin);
  1349. out_8(&mr->sequence, SEQ_MSGIN + seq);
  1350. } else {
  1351. ms->msgphase = msg_none;
  1352. handle_msgin(ms);
  1353. start_phase(ms);
  1354. }
  1355. break;
  1356. case msg_in_bad:
  1357. out_8(&mr->sequence, SEQ_FLUSHFIFO);
  1358. mesh_flush_io(mr);
  1359. udelay(1);
  1360. out_8(&mr->count_lo, 1);
  1361. out_8(&mr->sequence, SEQ_MSGIN + SEQ_ATN + use_active_neg);
  1362. break;
  1363. case msg_out:
  1364. /*
  1365. * To get the right timing on ATN wrt ACK, we have
  1366. * to get the MESH to drop ACK, wait until REQ gets
  1367. * asserted, then drop ATN. To do this we first
  1368. * issue a SEQ_MSGOUT with ATN and wait for REQ,
  1369. * then change the command to a SEQ_MSGOUT w/o ATN.
  1370. * If we don't see REQ in a reasonable time, we
  1371. * change the command to SEQ_MSGIN with ATN,
  1372. * wait for the phase mismatch interrupt, then
  1373. * issue the SEQ_MSGOUT without ATN.
  1374. */
  1375. out_8(&mr->count_lo, 1);
  1376. out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg + SEQ_ATN);
  1377. t = 30; /* wait up to 30us */
  1378. while ((in_8(&mr->bus_status0) & BS0_REQ) == 0 && --t >= 0)
  1379. udelay(1);
  1380. dlog(ms, "last_mbyte err/exc/fc/cl=%.8x",
  1381. MKWORD(mr->error, mr->exception,
  1382. mr->fifo_count, mr->count_lo));
  1383. if (in_8(&mr->interrupt) & (INT_ERROR | INT_EXCEPTION)) {
  1384. /* whoops, target didn't do what we expected */
  1385. ms->last_n_msgout = ms->n_msgout;
  1386. ms->n_msgout = 0;
  1387. if (in_8(&mr->interrupt) & INT_ERROR) {
  1388. printk(KERN_ERR "mesh: error %x in msg_out\n",
  1389. in_8(&mr->error));
  1390. handle_error(ms);
  1391. return;
  1392. }
  1393. if (in_8(&mr->exception) != EXC_PHASEMM)
  1394. printk(KERN_ERR "mesh: exc %x in msg_out\n",
  1395. in_8(&mr->exception));
  1396. else
  1397. printk(KERN_DEBUG "mesh: bs0=%x in msg_out\n",
  1398. in_8(&mr->bus_status0));
  1399. handle_exception(ms);
  1400. return;
  1401. }
  1402. if (in_8(&mr->bus_status0) & BS0_REQ) {
  1403. out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
  1404. mesh_flush_io(mr);
  1405. udelay(1);
  1406. out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
  1407. ms->msgphase = msg_out_last;
  1408. } else {
  1409. out_8(&mr->sequence, SEQ_MSGIN + use_active_neg + SEQ_ATN);
  1410. ms->msgphase = msg_out_xxx;
  1411. }
  1412. break;
  1413. case msg_out_last:
  1414. ms->last_n_msgout = ms->n_msgout;
  1415. ms->n_msgout = 0;
  1416. ms->msgphase = ms->expect_reply? msg_in: msg_none;
  1417. start_phase(ms);
  1418. break;
  1419. case msg_none:
  1420. switch (ms->phase) {
  1421. case idle:
  1422. printk(KERN_ERR "mesh: interrupt in idle phase?\n");
  1423. dumpslog(ms);
  1424. return;
  1425. case selecting:
  1426. dlog(ms, "Selecting phase at command completion",0);
  1427. ms->msgout[0] = IDENTIFY(ALLOW_RESEL(ms->conn_tgt),
  1428. (cmd? cmd->device->lun: 0));
  1429. ms->n_msgout = 1;
  1430. ms->expect_reply = 0;
  1431. if (ms->aborting) {
  1432. ms->msgout[0] = ABORT;
  1433. ms->n_msgout++;
  1434. } else if (tp->sdtr_state == do_sdtr) {
  1435. /* add SDTR message */
  1436. add_sdtr_msg(ms);
  1437. ms->expect_reply = 1;
  1438. tp->sdtr_state = sdtr_sent;
  1439. }
  1440. ms->msgphase = msg_out;
  1441. /*
  1442. * We need to wait for REQ before dropping ATN.
  1443. * We wait for at most 30us, then fall back to
  1444. * a scheme where we issue a SEQ_COMMAND with ATN,
  1445. * which will give us a phase mismatch interrupt
  1446. * when REQ does come, and then we send the message.
  1447. */
  1448. t = 230; /* wait up to 230us */
  1449. while ((in_8(&mr->bus_status0) & BS0_REQ) == 0) {
  1450. if (--t < 0) {
  1451. dlog(ms, "impatient for req", ms->n_msgout);
  1452. ms->msgphase = msg_none;
  1453. break;
  1454. }
  1455. udelay(1);
  1456. }
  1457. break;
  1458. case dataing:
  1459. if (ms->dma_count != 0) {
  1460. start_phase(ms);
  1461. return;
  1462. }
  1463. /*
  1464. * We can get a phase mismatch here if the target
  1465. * changes to the status phase, even though we have
  1466. * had a command complete interrupt. Then, if we
  1467. * issue the SEQ_STATUS command, we'll get a sequence
  1468. * error interrupt. Which isn't so bad except that
  1469. * occasionally the mesh actually executes the
  1470. * SEQ_STATUS *as well as* giving us the sequence
  1471. * error and phase mismatch exception.
  1472. */
  1473. out_8(&mr->sequence, 0);
  1474. out_8(&mr->interrupt,
  1475. INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  1476. halt_dma(ms);
  1477. break;
  1478. case statusing:
  1479. if (cmd) {
  1480. cmd->SCp.Status = mr->fifo;
  1481. if (DEBUG_TARGET(cmd))
  1482. printk(KERN_DEBUG "mesh: status is %x\n",
  1483. cmd->SCp.Status);
  1484. }
  1485. ms->msgphase = msg_in;
  1486. break;
  1487. case busfreeing:
  1488. mesh_done(ms, 1);
  1489. return;
  1490. case disconnecting:
  1491. ms->current_req = NULL;
  1492. ms->phase = idle;
  1493. mesh_start(ms);
  1494. return;
  1495. default:
  1496. break;
  1497. }
  1498. ++ms->phase;
  1499. start_phase(ms);
  1500. break;
  1501. }
  1502. }
  1503. /*
  1504. * Called by midlayer with host locked to queue a new
  1505. * request
  1506. */
  1507. static int mesh_queue(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  1508. {
  1509. struct mesh_state *ms;
  1510. cmd->scsi_done = done;
  1511. cmd->host_scribble = NULL;
  1512. ms = (struct mesh_state *) cmd->device->host->hostdata;
  1513. if (ms->request_q == NULL)
  1514. ms->request_q = cmd;
  1515. else
  1516. ms->request_qtail->host_scribble = (void *) cmd;
  1517. ms->request_qtail = cmd;
  1518. if (ms->phase == idle)
  1519. mesh_start(ms);
  1520. return 0;
  1521. }
  1522. /*
  1523. * Called to handle interrupts, either call by the interrupt
  1524. * handler (do_mesh_interrupt) or by other functions in
  1525. * exceptional circumstances
  1526. */
  1527. static void mesh_interrupt(struct mesh_state *ms)
  1528. {
  1529. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1530. int intr;
  1531. #if 0
  1532. if (ALLOW_DEBUG(ms->conn_tgt))
  1533. printk(KERN_DEBUG "mesh_intr, bs0=%x int=%x exc=%x err=%x "
  1534. "phase=%d msgphase=%d\n", mr->bus_status0,
  1535. mr->interrupt, mr->exception, mr->error,
  1536. ms->phase, ms->msgphase);
  1537. #endif
  1538. while ((intr = in_8(&mr->interrupt)) != 0) {
  1539. dlog(ms, "interrupt intr/err/exc/seq=%.8x",
  1540. MKWORD(intr, mr->error, mr->exception, mr->sequence));
  1541. if (intr & INT_ERROR) {
  1542. handle_error(ms);
  1543. } else if (intr & INT_EXCEPTION) {
  1544. handle_exception(ms);
  1545. } else if (intr & INT_CMDDONE) {
  1546. out_8(&mr->interrupt, INT_CMDDONE);
  1547. cmd_complete(ms);
  1548. }
  1549. }
  1550. }
  1551. /* Todo: here we can at least try to remove the command from the
  1552. * queue if it isn't connected yet, and for pending command, assert
  1553. * ATN until the bus gets freed.
  1554. */
  1555. static int mesh_abort(struct scsi_cmnd *cmd)
  1556. {
  1557. struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
  1558. printk(KERN_DEBUG "mesh_abort(%p)\n", cmd);
  1559. mesh_dump_regs(ms);
  1560. dumplog(ms, cmd->device->id);
  1561. dumpslog(ms);
  1562. return FAILED;
  1563. }
  1564. /*
  1565. * Called by the midlayer with the lock held to reset the
  1566. * SCSI host and bus.
  1567. * The midlayer will wait for devices to come back, we don't need
  1568. * to do that ourselves
  1569. */
  1570. static int mesh_host_reset(struct scsi_cmnd *cmd)
  1571. {
  1572. struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
  1573. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1574. volatile struct dbdma_regs __iomem *md = ms->dma;
  1575. unsigned long flags;
  1576. printk(KERN_DEBUG "mesh_host_reset\n");
  1577. spin_lock_irqsave(ms->host->host_lock, flags);
  1578. /* Reset the controller & dbdma channel */
  1579. out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* stop dma */
  1580. out_8(&mr->exception, 0xff); /* clear all exception bits */
  1581. out_8(&mr->error, 0xff); /* clear all error bits */
  1582. out_8(&mr->sequence, SEQ_RESETMESH);
  1583. mesh_flush_io(mr);
  1584. udelay(1);
  1585. out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  1586. out_8(&mr->source_id, ms->host->this_id);
  1587. out_8(&mr->sel_timeout, 25); /* 250ms */
  1588. out_8(&mr->sync_params, ASYNC_PARAMS);
  1589. /* Reset the bus */
  1590. out_8(&mr->bus_status1, BS1_RST); /* assert RST */
  1591. mesh_flush_io(mr);
  1592. udelay(30); /* leave it on for >= 25us */
  1593. out_8(&mr->bus_status1, 0); /* negate RST */
  1594. /* Complete pending commands */
  1595. handle_reset(ms);
  1596. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1597. return SUCCESS;
  1598. }
  1599. static void set_mesh_power(struct mesh_state *ms, int state)
  1600. {
  1601. if (!machine_is(powermac))
  1602. return;
  1603. if (state) {
  1604. pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 1);
  1605. msleep(200);
  1606. } else {
  1607. pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 0);
  1608. msleep(10);
  1609. }
  1610. }
  1611. #ifdef CONFIG_PM
  1612. static int mesh_suspend(struct macio_dev *mdev, pm_message_t mesg)
  1613. {
  1614. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1615. unsigned long flags;
  1616. switch (mesg.event) {
  1617. case PM_EVENT_SUSPEND:
  1618. case PM_EVENT_HIBERNATE:
  1619. case PM_EVENT_FREEZE:
  1620. break;
  1621. default:
  1622. return 0;
  1623. }
  1624. if (ms->phase == sleeping)
  1625. return 0;
  1626. scsi_block_requests(ms->host);
  1627. spin_lock_irqsave(ms->host->host_lock, flags);
  1628. while(ms->phase != idle) {
  1629. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1630. msleep(10);
  1631. spin_lock_irqsave(ms->host->host_lock, flags);
  1632. }
  1633. ms->phase = sleeping;
  1634. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1635. disable_irq(ms->meshintr);
  1636. set_mesh_power(ms, 0);
  1637. return 0;
  1638. }
  1639. static int mesh_resume(struct macio_dev *mdev)
  1640. {
  1641. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1642. unsigned long flags;
  1643. if (ms->phase != sleeping)
  1644. return 0;
  1645. set_mesh_power(ms, 1);
  1646. mesh_init(ms);
  1647. spin_lock_irqsave(ms->host->host_lock, flags);
  1648. mesh_start(ms);
  1649. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1650. enable_irq(ms->meshintr);
  1651. scsi_unblock_requests(ms->host);
  1652. return 0;
  1653. }
  1654. #endif /* CONFIG_PM */
  1655. /*
  1656. * If we leave drives set for synchronous transfers (especially
  1657. * CDROMs), and reboot to MacOS, it gets confused, poor thing.
  1658. * So, on reboot we reset the SCSI bus.
  1659. */
  1660. static int mesh_shutdown(struct macio_dev *mdev)
  1661. {
  1662. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1663. volatile struct mesh_regs __iomem *mr;
  1664. unsigned long flags;
  1665. printk(KERN_INFO "resetting MESH scsi bus(es)\n");
  1666. spin_lock_irqsave(ms->host->host_lock, flags);
  1667. mr = ms->mesh;
  1668. out_8(&mr->intr_mask, 0);
  1669. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  1670. out_8(&mr->bus_status1, BS1_RST);
  1671. mesh_flush_io(mr);
  1672. udelay(30);
  1673. out_8(&mr->bus_status1, 0);
  1674. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1675. return 0;
  1676. }
  1677. static struct scsi_host_template mesh_template = {
  1678. .proc_name = "mesh",
  1679. .name = "MESH",
  1680. .queuecommand = mesh_queue,
  1681. .eh_abort_handler = mesh_abort,
  1682. .eh_host_reset_handler = mesh_host_reset,
  1683. .can_queue = 20,
  1684. .this_id = 7,
  1685. .sg_tablesize = SG_ALL,
  1686. .cmd_per_lun = 2,
  1687. .use_clustering = DISABLE_CLUSTERING,
  1688. };
  1689. static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
  1690. {
  1691. struct device_node *mesh = macio_get_of_node(mdev);
  1692. struct pci_dev* pdev = macio_get_pci_dev(mdev);
  1693. int tgt, minper;
  1694. const int *cfp;
  1695. struct mesh_state *ms;
  1696. struct Scsi_Host *mesh_host;
  1697. void *dma_cmd_space;
  1698. dma_addr_t dma_cmd_bus;
  1699. switch (mdev->bus->chip->type) {
  1700. case macio_heathrow:
  1701. case macio_gatwick:
  1702. case macio_paddington:
  1703. use_active_neg = 0;
  1704. break;
  1705. default:
  1706. use_active_neg = SEQ_ACTIVE_NEG;
  1707. }
  1708. if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
  1709. printk(KERN_ERR "mesh: expected 2 addrs and 2 intrs"
  1710. " (got %d,%d)\n", macio_resource_count(mdev),
  1711. macio_irq_count(mdev));
  1712. return -ENODEV;
  1713. }
  1714. if (macio_request_resources(mdev, "mesh") != 0) {
  1715. printk(KERN_ERR "mesh: unable to request memory resources");
  1716. return -EBUSY;
  1717. }
  1718. mesh_host = scsi_host_alloc(&mesh_template, sizeof(struct mesh_state));
  1719. if (mesh_host == NULL) {
  1720. printk(KERN_ERR "mesh: couldn't register host");
  1721. goto out_release;
  1722. }
  1723. /* Old junk for root discovery, that will die ultimately */
  1724. #if !defined(MODULE)
  1725. note_scsi_host(mesh, mesh_host);
  1726. #endif
  1727. mesh_host->base = macio_resource_start(mdev, 0);
  1728. mesh_host->irq = macio_irq(mdev, 0);
  1729. ms = (struct mesh_state *) mesh_host->hostdata;
  1730. macio_set_drvdata(mdev, ms);
  1731. ms->host = mesh_host;
  1732. ms->mdev = mdev;
  1733. ms->pdev = pdev;
  1734. ms->mesh = ioremap(macio_resource_start(mdev, 0), 0x1000);
  1735. if (ms->mesh == NULL) {
  1736. printk(KERN_ERR "mesh: can't map registers\n");
  1737. goto out_free;
  1738. }
  1739. ms->dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
  1740. if (ms->dma == NULL) {
  1741. printk(KERN_ERR "mesh: can't map registers\n");
  1742. iounmap(ms->mesh);
  1743. goto out_free;
  1744. }
  1745. ms->meshintr = macio_irq(mdev, 0);
  1746. ms->dmaintr = macio_irq(mdev, 1);
  1747. /* Space for dma command list: +1 for stop command,
  1748. * +1 to allow for aligning.
  1749. */
  1750. ms->dma_cmd_size = (mesh_host->sg_tablesize + 2) * sizeof(struct dbdma_cmd);
  1751. /* We use the PCI APIs for now until the generic one gets fixed
  1752. * enough or until we get some macio-specific versions
  1753. */
  1754. dma_cmd_space = pci_alloc_consistent(macio_get_pci_dev(mdev),
  1755. ms->dma_cmd_size,
  1756. &dma_cmd_bus);
  1757. if (dma_cmd_space == NULL) {
  1758. printk(KERN_ERR "mesh: can't allocate DMA table\n");
  1759. goto out_unmap;
  1760. }
  1761. memset(dma_cmd_space, 0, ms->dma_cmd_size);
  1762. ms->dma_cmds = (struct dbdma_cmd *) DBDMA_ALIGN(dma_cmd_space);
  1763. ms->dma_cmd_space = dma_cmd_space;
  1764. ms->dma_cmd_bus = dma_cmd_bus + ((unsigned long)ms->dma_cmds)
  1765. - (unsigned long)dma_cmd_space;
  1766. ms->current_req = NULL;
  1767. for (tgt = 0; tgt < 8; ++tgt) {
  1768. ms->tgts[tgt].sdtr_state = do_sdtr;
  1769. ms->tgts[tgt].sync_params = ASYNC_PARAMS;
  1770. ms->tgts[tgt].current_req = NULL;
  1771. }
  1772. if ((cfp = of_get_property(mesh, "clock-frequency", NULL)))
  1773. ms->clk_freq = *cfp;
  1774. else {
  1775. printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
  1776. ms->clk_freq = 50000000;
  1777. }
  1778. /* The maximum sync rate is clock / 5; increase
  1779. * mesh_sync_period if necessary.
  1780. */
  1781. minper = 1000000000 / (ms->clk_freq / 5); /* ns */
  1782. if (mesh_sync_period < minper)
  1783. mesh_sync_period = minper;
  1784. /* Power up the chip */
  1785. set_mesh_power(ms, 1);
  1786. /* Set it up */
  1787. mesh_init(ms);
  1788. /* Request interrupt */
  1789. if (request_irq(ms->meshintr, do_mesh_interrupt, 0, "MESH", ms)) {
  1790. printk(KERN_ERR "MESH: can't get irq %d\n", ms->meshintr);
  1791. goto out_shutdown;
  1792. }
  1793. /* Add scsi host & scan */
  1794. if (scsi_add_host(mesh_host, &mdev->ofdev.dev))
  1795. goto out_release_irq;
  1796. scsi_scan_host(mesh_host);
  1797. return 0;
  1798. out_release_irq:
  1799. free_irq(ms->meshintr, ms);
  1800. out_shutdown:
  1801. /* shutdown & reset bus in case of error or macos can be confused
  1802. * at reboot if the bus was set to synchronous mode already
  1803. */
  1804. mesh_shutdown(mdev);
  1805. set_mesh_power(ms, 0);
  1806. pci_free_consistent(macio_get_pci_dev(mdev), ms->dma_cmd_size,
  1807. ms->dma_cmd_space, ms->dma_cmd_bus);
  1808. out_unmap:
  1809. iounmap(ms->dma);
  1810. iounmap(ms->mesh);
  1811. out_free:
  1812. scsi_host_put(mesh_host);
  1813. out_release:
  1814. macio_release_resources(mdev);
  1815. return -ENODEV;
  1816. }
  1817. static int mesh_remove(struct macio_dev *mdev)
  1818. {
  1819. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1820. struct Scsi_Host *mesh_host = ms->host;
  1821. scsi_remove_host(mesh_host);
  1822. free_irq(ms->meshintr, ms);
  1823. /* Reset scsi bus */
  1824. mesh_shutdown(mdev);
  1825. /* Shut down chip & termination */
  1826. set_mesh_power(ms, 0);
  1827. /* Unmap registers & dma controller */
  1828. iounmap(ms->mesh);
  1829. iounmap(ms->dma);
  1830. /* Free DMA commands memory */
  1831. pci_free_consistent(macio_get_pci_dev(mdev), ms->dma_cmd_size,
  1832. ms->dma_cmd_space, ms->dma_cmd_bus);
  1833. /* Release memory resources */
  1834. macio_release_resources(mdev);
  1835. scsi_host_put(mesh_host);
  1836. return 0;
  1837. }
  1838. static struct of_device_id mesh_match[] =
  1839. {
  1840. {
  1841. .name = "mesh",
  1842. },
  1843. {
  1844. .type = "scsi",
  1845. .compatible = "chrp,mesh0"
  1846. },
  1847. {},
  1848. };
  1849. MODULE_DEVICE_TABLE (of, mesh_match);
  1850. static struct macio_driver mesh_driver =
  1851. {
  1852. .name = "mesh",
  1853. .match_table = mesh_match,
  1854. .probe = mesh_probe,
  1855. .remove = mesh_remove,
  1856. .shutdown = mesh_shutdown,
  1857. #ifdef CONFIG_PM
  1858. .suspend = mesh_suspend,
  1859. .resume = mesh_resume,
  1860. #endif
  1861. };
  1862. static int __init init_mesh(void)
  1863. {
  1864. /* Calculate sync rate from module parameters */
  1865. if (sync_rate > 10)
  1866. sync_rate = 10;
  1867. if (sync_rate > 0) {
  1868. printk(KERN_INFO "mesh: configured for synchronous %d MB/s\n", sync_rate);
  1869. mesh_sync_period = 1000 / sync_rate; /* ns */
  1870. mesh_sync_offset = 15;
  1871. } else
  1872. printk(KERN_INFO "mesh: configured for asynchronous\n");
  1873. return macio_register_driver(&mesh_driver);
  1874. }
  1875. static void __exit exit_mesh(void)
  1876. {
  1877. return macio_unregister_driver(&mesh_driver);
  1878. }
  1879. module_init(init_mesh);
  1880. module_exit(exit_mesh);