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