mesh.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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 <linux/pci.h>
  33. #include <asm/dbdma.h>
  34. #include <asm/io.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/prom.h>
  37. #include <asm/irq.h>
  38. #include <asm/hydra.h>
  39. #include <asm/processor.h>
  40. #include <asm/machdep.h>
  41. #include <asm/pmac_feature.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. if (ms->dma_started)
  955. halt_dma(ms);
  956. handle_reset(ms);
  957. /* request_q is empty, no point in mesh_start() */
  958. return;
  959. }
  960. if (err & ERR_UNEXPDISC) {
  961. /* Unexpected disconnect */
  962. if (exc & EXC_RESELECTED) {
  963. reselected(ms);
  964. return;
  965. }
  966. if (!ms->aborting) {
  967. printk(KERN_WARNING "mesh: target %d aborted\n",
  968. ms->conn_tgt);
  969. dumplog(ms, ms->conn_tgt);
  970. dumpslog(ms);
  971. }
  972. out_8(&mr->interrupt, INT_CMDDONE);
  973. ms->stat = DID_ABORT;
  974. mesh_done(ms, 1);
  975. return;
  976. }
  977. if (err & ERR_PARITY) {
  978. if (ms->msgphase == msg_in) {
  979. printk(KERN_ERR "mesh: msg parity error, target %d\n",
  980. ms->conn_tgt);
  981. ms->msgout[0] = MSG_PARITY_ERROR;
  982. ms->n_msgout = 1;
  983. ms->msgphase = msg_in_bad;
  984. cmd_complete(ms);
  985. return;
  986. }
  987. if (ms->stat == DID_OK) {
  988. printk(KERN_ERR "mesh: parity error, target %d\n",
  989. ms->conn_tgt);
  990. ms->stat = DID_PARITY;
  991. }
  992. count = (mr->count_hi << 8) + mr->count_lo;
  993. if (count == 0) {
  994. cmd_complete(ms);
  995. } else {
  996. /* reissue the data transfer command */
  997. out_8(&mr->sequence, mr->sequence);
  998. }
  999. return;
  1000. }
  1001. if (err & ERR_SEQERR) {
  1002. if (exc & EXC_RESELECTED) {
  1003. /* This can happen if we issue a command to
  1004. get the bus just after the target reselects us. */
  1005. static int mesh_resel_seqerr;
  1006. mesh_resel_seqerr++;
  1007. reselected(ms);
  1008. return;
  1009. }
  1010. if (exc == EXC_PHASEMM) {
  1011. static int mesh_phasemm_seqerr;
  1012. mesh_phasemm_seqerr++;
  1013. phase_mismatch(ms);
  1014. return;
  1015. }
  1016. printk(KERN_ERR "mesh: sequence error (err=%x exc=%x)\n",
  1017. err, exc);
  1018. } else {
  1019. printk(KERN_ERR "mesh: unknown error %x (exc=%x)\n", err, exc);
  1020. }
  1021. mesh_dump_regs(ms);
  1022. dumplog(ms, ms->conn_tgt);
  1023. if (ms->phase > selecting && (in_8(&mr->bus_status1) & BS1_BSY)) {
  1024. /* try to do what the target wants */
  1025. do_abort(ms);
  1026. phase_mismatch(ms);
  1027. return;
  1028. }
  1029. ms->stat = DID_ERROR;
  1030. mesh_done(ms, 1);
  1031. }
  1032. static void handle_exception(struct mesh_state *ms)
  1033. {
  1034. int exc;
  1035. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1036. exc = in_8(&mr->exception);
  1037. out_8(&mr->interrupt, INT_EXCEPTION | INT_CMDDONE);
  1038. if (exc & EXC_RESELECTED) {
  1039. static int mesh_resel_exc;
  1040. mesh_resel_exc++;
  1041. reselected(ms);
  1042. } else if (exc == EXC_ARBLOST) {
  1043. printk(KERN_DEBUG "mesh: lost arbitration\n");
  1044. ms->stat = DID_BUS_BUSY;
  1045. mesh_done(ms, 1);
  1046. } else if (exc == EXC_SELTO) {
  1047. /* selection timed out */
  1048. ms->stat = DID_BAD_TARGET;
  1049. mesh_done(ms, 1);
  1050. } else if (exc == EXC_PHASEMM) {
  1051. /* target wants to do something different:
  1052. find out what it wants and do it. */
  1053. phase_mismatch(ms);
  1054. } else {
  1055. printk(KERN_ERR "mesh: can't cope with exception %x\n", exc);
  1056. mesh_dump_regs(ms);
  1057. dumplog(ms, ms->conn_tgt);
  1058. do_abort(ms);
  1059. phase_mismatch(ms);
  1060. }
  1061. }
  1062. static void handle_msgin(struct mesh_state *ms)
  1063. {
  1064. int i, code;
  1065. struct scsi_cmnd *cmd = ms->current_req;
  1066. struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
  1067. if (ms->n_msgin == 0)
  1068. return;
  1069. code = ms->msgin[0];
  1070. if (ALLOW_DEBUG(ms->conn_tgt)) {
  1071. printk(KERN_DEBUG "got %d message bytes:", ms->n_msgin);
  1072. for (i = 0; i < ms->n_msgin; ++i)
  1073. printk(" %x", ms->msgin[i]);
  1074. printk("\n");
  1075. }
  1076. dlog(ms, "msgin msg=%.8x",
  1077. MKWORD(ms->n_msgin, code, ms->msgin[1], ms->msgin[2]));
  1078. ms->expect_reply = 0;
  1079. ms->n_msgout = 0;
  1080. if (ms->n_msgin < msgin_length(ms))
  1081. goto reject;
  1082. if (cmd)
  1083. cmd->SCp.Message = code;
  1084. switch (code) {
  1085. case COMMAND_COMPLETE:
  1086. break;
  1087. case EXTENDED_MESSAGE:
  1088. switch (ms->msgin[2]) {
  1089. case EXTENDED_MODIFY_DATA_POINTER:
  1090. ms->data_ptr += (ms->msgin[3] << 24) + ms->msgin[6]
  1091. + (ms->msgin[4] << 16) + (ms->msgin[5] << 8);
  1092. break;
  1093. case EXTENDED_SDTR:
  1094. if (tp->sdtr_state != sdtr_sent) {
  1095. /* reply with an SDTR */
  1096. add_sdtr_msg(ms);
  1097. /* limit period to at least his value,
  1098. offset to no more than his */
  1099. if (ms->msgout[3] < ms->msgin[3])
  1100. ms->msgout[3] = ms->msgin[3];
  1101. if (ms->msgout[4] > ms->msgin[4])
  1102. ms->msgout[4] = ms->msgin[4];
  1103. set_sdtr(ms, ms->msgout[3], ms->msgout[4]);
  1104. ms->msgphase = msg_out;
  1105. } else {
  1106. set_sdtr(ms, ms->msgin[3], ms->msgin[4]);
  1107. }
  1108. break;
  1109. default:
  1110. goto reject;
  1111. }
  1112. break;
  1113. case SAVE_POINTERS:
  1114. tp->saved_ptr = ms->data_ptr;
  1115. break;
  1116. case RESTORE_POINTERS:
  1117. ms->data_ptr = tp->saved_ptr;
  1118. break;
  1119. case DISCONNECT:
  1120. ms->phase = disconnecting;
  1121. break;
  1122. case ABORT:
  1123. break;
  1124. case MESSAGE_REJECT:
  1125. if (tp->sdtr_state == sdtr_sent)
  1126. set_sdtr(ms, 0, 0);
  1127. break;
  1128. case NOP:
  1129. break;
  1130. default:
  1131. if (IDENTIFY_BASE <= code && code <= IDENTIFY_BASE + 7) {
  1132. if (cmd == NULL) {
  1133. do_abort(ms);
  1134. ms->msgphase = msg_out;
  1135. } else if (code != cmd->device->lun + IDENTIFY_BASE) {
  1136. printk(KERN_WARNING "mesh: lun mismatch "
  1137. "(%d != %llu) on reselection from "
  1138. "target %d\n", code - IDENTIFY_BASE,
  1139. cmd->device->lun, ms->conn_tgt);
  1140. }
  1141. break;
  1142. }
  1143. goto reject;
  1144. }
  1145. return;
  1146. reject:
  1147. printk(KERN_WARNING "mesh: rejecting message from target %d:",
  1148. ms->conn_tgt);
  1149. for (i = 0; i < ms->n_msgin; ++i)
  1150. printk(" %x", ms->msgin[i]);
  1151. printk("\n");
  1152. ms->msgout[0] = MESSAGE_REJECT;
  1153. ms->n_msgout = 1;
  1154. ms->msgphase = msg_out;
  1155. }
  1156. /*
  1157. * Set up DMA commands for transferring data.
  1158. */
  1159. static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd)
  1160. {
  1161. int i, dma_cmd, total, off, dtot;
  1162. struct scatterlist *scl;
  1163. struct dbdma_cmd *dcmds;
  1164. dma_cmd = ms->tgts[ms->conn_tgt].data_goes_out?
  1165. OUTPUT_MORE: INPUT_MORE;
  1166. dcmds = ms->dma_cmds;
  1167. dtot = 0;
  1168. if (cmd) {
  1169. int nseg;
  1170. cmd->SCp.this_residual = scsi_bufflen(cmd);
  1171. nseg = scsi_dma_map(cmd);
  1172. BUG_ON(nseg < 0);
  1173. if (nseg) {
  1174. total = 0;
  1175. off = ms->data_ptr;
  1176. scsi_for_each_sg(cmd, scl, nseg, i) {
  1177. u32 dma_addr = sg_dma_address(scl);
  1178. u32 dma_len = sg_dma_len(scl);
  1179. total += scl->length;
  1180. if (off >= dma_len) {
  1181. off -= dma_len;
  1182. continue;
  1183. }
  1184. if (dma_len > 0xffff)
  1185. panic("mesh: scatterlist element >= 64k");
  1186. dcmds->req_count = cpu_to_le16(dma_len - off);
  1187. dcmds->command = cpu_to_le16(dma_cmd);
  1188. dcmds->phy_addr = cpu_to_le32(dma_addr + off);
  1189. dcmds->xfer_status = 0;
  1190. ++dcmds;
  1191. dtot += dma_len - off;
  1192. off = 0;
  1193. }
  1194. }
  1195. }
  1196. if (dtot == 0) {
  1197. /* Either the target has overrun our buffer,
  1198. or the caller didn't provide a buffer. */
  1199. static char mesh_extra_buf[64];
  1200. dtot = sizeof(mesh_extra_buf);
  1201. dcmds->req_count = cpu_to_le16(dtot);
  1202. dcmds->phy_addr = cpu_to_le32(virt_to_phys(mesh_extra_buf));
  1203. dcmds->xfer_status = 0;
  1204. ++dcmds;
  1205. }
  1206. dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
  1207. dcmds[-1].command = cpu_to_le16(dma_cmd);
  1208. memset(dcmds, 0, sizeof(*dcmds));
  1209. dcmds->command = cpu_to_le16(DBDMA_STOP);
  1210. ms->dma_count = dtot;
  1211. }
  1212. static void halt_dma(struct mesh_state *ms)
  1213. {
  1214. volatile struct dbdma_regs __iomem *md = ms->dma;
  1215. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1216. struct scsi_cmnd *cmd = ms->current_req;
  1217. int t, nb;
  1218. if (!ms->tgts[ms->conn_tgt].data_goes_out) {
  1219. /* wait a little while until the fifo drains */
  1220. t = 50;
  1221. while (t > 0 && in_8(&mr->fifo_count) != 0
  1222. && (in_le32(&md->status) & ACTIVE) != 0) {
  1223. --t;
  1224. udelay(1);
  1225. }
  1226. }
  1227. out_le32(&md->control, RUN << 16); /* turn off RUN bit */
  1228. nb = (mr->count_hi << 8) + mr->count_lo;
  1229. dlog(ms, "halt_dma fc/count=%.6x",
  1230. MKWORD(0, mr->fifo_count, 0, nb));
  1231. if (ms->tgts[ms->conn_tgt].data_goes_out)
  1232. nb += mr->fifo_count;
  1233. /* nb is the number of bytes not yet transferred
  1234. to/from the target. */
  1235. ms->data_ptr -= nb;
  1236. dlog(ms, "data_ptr %x", ms->data_ptr);
  1237. if (ms->data_ptr < 0) {
  1238. printk(KERN_ERR "mesh: halt_dma: data_ptr=%d (nb=%d, ms=%p)\n",
  1239. ms->data_ptr, nb, ms);
  1240. ms->data_ptr = 0;
  1241. #ifdef MESH_DBG
  1242. dumplog(ms, ms->conn_tgt);
  1243. dumpslog(ms);
  1244. #endif /* MESH_DBG */
  1245. } else if (cmd && scsi_bufflen(cmd) &&
  1246. ms->data_ptr > scsi_bufflen(cmd)) {
  1247. printk(KERN_DEBUG "mesh: target %d overrun, "
  1248. "data_ptr=%x total=%x goes_out=%d\n",
  1249. ms->conn_tgt, ms->data_ptr, scsi_bufflen(cmd),
  1250. ms->tgts[ms->conn_tgt].data_goes_out);
  1251. }
  1252. if (cmd)
  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_lck(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. static DEF_SCSI_QCMD(mesh_queue)
  1523. /*
  1524. * Called to handle interrupts, either call by the interrupt
  1525. * handler (do_mesh_interrupt) or by other functions in
  1526. * exceptional circumstances
  1527. */
  1528. static void mesh_interrupt(struct mesh_state *ms)
  1529. {
  1530. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1531. int intr;
  1532. #if 0
  1533. if (ALLOW_DEBUG(ms->conn_tgt))
  1534. printk(KERN_DEBUG "mesh_intr, bs0=%x int=%x exc=%x err=%x "
  1535. "phase=%d msgphase=%d\n", mr->bus_status0,
  1536. mr->interrupt, mr->exception, mr->error,
  1537. ms->phase, ms->msgphase);
  1538. #endif
  1539. while ((intr = in_8(&mr->interrupt)) != 0) {
  1540. dlog(ms, "interrupt intr/err/exc/seq=%.8x",
  1541. MKWORD(intr, mr->error, mr->exception, mr->sequence));
  1542. if (intr & INT_ERROR) {
  1543. handle_error(ms);
  1544. } else if (intr & INT_EXCEPTION) {
  1545. handle_exception(ms);
  1546. } else if (intr & INT_CMDDONE) {
  1547. out_8(&mr->interrupt, INT_CMDDONE);
  1548. cmd_complete(ms);
  1549. }
  1550. }
  1551. }
  1552. /* Todo: here we can at least try to remove the command from the
  1553. * queue if it isn't connected yet, and for pending command, assert
  1554. * ATN until the bus gets freed.
  1555. */
  1556. static int mesh_abort(struct scsi_cmnd *cmd)
  1557. {
  1558. struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
  1559. printk(KERN_DEBUG "mesh_abort(%p)\n", cmd);
  1560. mesh_dump_regs(ms);
  1561. dumplog(ms, cmd->device->id);
  1562. dumpslog(ms);
  1563. return FAILED;
  1564. }
  1565. /*
  1566. * Called by the midlayer with the lock held to reset the
  1567. * SCSI host and bus.
  1568. * The midlayer will wait for devices to come back, we don't need
  1569. * to do that ourselves
  1570. */
  1571. static int mesh_host_reset(struct scsi_cmnd *cmd)
  1572. {
  1573. struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
  1574. volatile struct mesh_regs __iomem *mr = ms->mesh;
  1575. volatile struct dbdma_regs __iomem *md = ms->dma;
  1576. unsigned long flags;
  1577. printk(KERN_DEBUG "mesh_host_reset\n");
  1578. spin_lock_irqsave(ms->host->host_lock, flags);
  1579. if (ms->dma_started)
  1580. halt_dma(ms);
  1581. /* Reset the controller & dbdma channel */
  1582. out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* stop dma */
  1583. out_8(&mr->exception, 0xff); /* clear all exception bits */
  1584. out_8(&mr->error, 0xff); /* clear all error bits */
  1585. out_8(&mr->sequence, SEQ_RESETMESH);
  1586. mesh_flush_io(mr);
  1587. udelay(1);
  1588. out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  1589. out_8(&mr->source_id, ms->host->this_id);
  1590. out_8(&mr->sel_timeout, 25); /* 250ms */
  1591. out_8(&mr->sync_params, ASYNC_PARAMS);
  1592. /* Reset the bus */
  1593. out_8(&mr->bus_status1, BS1_RST); /* assert RST */
  1594. mesh_flush_io(mr);
  1595. udelay(30); /* leave it on for >= 25us */
  1596. out_8(&mr->bus_status1, 0); /* negate RST */
  1597. /* Complete pending commands */
  1598. handle_reset(ms);
  1599. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1600. return SUCCESS;
  1601. }
  1602. static void set_mesh_power(struct mesh_state *ms, int state)
  1603. {
  1604. if (!machine_is(powermac))
  1605. return;
  1606. if (state) {
  1607. pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 1);
  1608. msleep(200);
  1609. } else {
  1610. pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 0);
  1611. msleep(10);
  1612. }
  1613. }
  1614. #ifdef CONFIG_PM
  1615. static int mesh_suspend(struct macio_dev *mdev, pm_message_t mesg)
  1616. {
  1617. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1618. unsigned long flags;
  1619. switch (mesg.event) {
  1620. case PM_EVENT_SUSPEND:
  1621. case PM_EVENT_HIBERNATE:
  1622. case PM_EVENT_FREEZE:
  1623. break;
  1624. default:
  1625. return 0;
  1626. }
  1627. if (ms->phase == sleeping)
  1628. return 0;
  1629. scsi_block_requests(ms->host);
  1630. spin_lock_irqsave(ms->host->host_lock, flags);
  1631. while(ms->phase != idle) {
  1632. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1633. msleep(10);
  1634. spin_lock_irqsave(ms->host->host_lock, flags);
  1635. }
  1636. ms->phase = sleeping;
  1637. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1638. disable_irq(ms->meshintr);
  1639. set_mesh_power(ms, 0);
  1640. return 0;
  1641. }
  1642. static int mesh_resume(struct macio_dev *mdev)
  1643. {
  1644. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1645. unsigned long flags;
  1646. if (ms->phase != sleeping)
  1647. return 0;
  1648. set_mesh_power(ms, 1);
  1649. mesh_init(ms);
  1650. spin_lock_irqsave(ms->host->host_lock, flags);
  1651. mesh_start(ms);
  1652. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1653. enable_irq(ms->meshintr);
  1654. scsi_unblock_requests(ms->host);
  1655. return 0;
  1656. }
  1657. #endif /* CONFIG_PM */
  1658. /*
  1659. * If we leave drives set for synchronous transfers (especially
  1660. * CDROMs), and reboot to MacOS, it gets confused, poor thing.
  1661. * So, on reboot we reset the SCSI bus.
  1662. */
  1663. static int mesh_shutdown(struct macio_dev *mdev)
  1664. {
  1665. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1666. volatile struct mesh_regs __iomem *mr;
  1667. unsigned long flags;
  1668. printk(KERN_INFO "resetting MESH scsi bus(es)\n");
  1669. spin_lock_irqsave(ms->host->host_lock, flags);
  1670. mr = ms->mesh;
  1671. out_8(&mr->intr_mask, 0);
  1672. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  1673. out_8(&mr->bus_status1, BS1_RST);
  1674. mesh_flush_io(mr);
  1675. udelay(30);
  1676. out_8(&mr->bus_status1, 0);
  1677. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1678. return 0;
  1679. }
  1680. static struct scsi_host_template mesh_template = {
  1681. .proc_name = "mesh",
  1682. .name = "MESH",
  1683. .queuecommand = mesh_queue,
  1684. .eh_abort_handler = mesh_abort,
  1685. .eh_host_reset_handler = mesh_host_reset,
  1686. .can_queue = 20,
  1687. .this_id = 7,
  1688. .sg_tablesize = SG_ALL,
  1689. .cmd_per_lun = 2,
  1690. .use_clustering = DISABLE_CLUSTERING,
  1691. };
  1692. static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
  1693. {
  1694. struct device_node *mesh = macio_get_of_node(mdev);
  1695. struct pci_dev* pdev = macio_get_pci_dev(mdev);
  1696. int tgt, minper;
  1697. const int *cfp;
  1698. struct mesh_state *ms;
  1699. struct Scsi_Host *mesh_host;
  1700. void *dma_cmd_space;
  1701. dma_addr_t dma_cmd_bus;
  1702. switch (mdev->bus->chip->type) {
  1703. case macio_heathrow:
  1704. case macio_gatwick:
  1705. case macio_paddington:
  1706. use_active_neg = 0;
  1707. break;
  1708. default:
  1709. use_active_neg = SEQ_ACTIVE_NEG;
  1710. }
  1711. if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
  1712. printk(KERN_ERR "mesh: expected 2 addrs and 2 intrs"
  1713. " (got %d,%d)\n", macio_resource_count(mdev),
  1714. macio_irq_count(mdev));
  1715. return -ENODEV;
  1716. }
  1717. if (macio_request_resources(mdev, "mesh") != 0) {
  1718. printk(KERN_ERR "mesh: unable to request memory resources");
  1719. return -EBUSY;
  1720. }
  1721. mesh_host = scsi_host_alloc(&mesh_template, sizeof(struct mesh_state));
  1722. if (mesh_host == NULL) {
  1723. printk(KERN_ERR "mesh: couldn't register host");
  1724. goto out_release;
  1725. }
  1726. /* Old junk for root discovery, that will die ultimately */
  1727. #if !defined(MODULE)
  1728. note_scsi_host(mesh, mesh_host);
  1729. #endif
  1730. mesh_host->base = macio_resource_start(mdev, 0);
  1731. mesh_host->irq = macio_irq(mdev, 0);
  1732. ms = (struct mesh_state *) mesh_host->hostdata;
  1733. macio_set_drvdata(mdev, ms);
  1734. ms->host = mesh_host;
  1735. ms->mdev = mdev;
  1736. ms->pdev = pdev;
  1737. ms->mesh = ioremap(macio_resource_start(mdev, 0), 0x1000);
  1738. if (ms->mesh == NULL) {
  1739. printk(KERN_ERR "mesh: can't map registers\n");
  1740. goto out_free;
  1741. }
  1742. ms->dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
  1743. if (ms->dma == NULL) {
  1744. printk(KERN_ERR "mesh: can't map registers\n");
  1745. iounmap(ms->mesh);
  1746. goto out_free;
  1747. }
  1748. ms->meshintr = macio_irq(mdev, 0);
  1749. ms->dmaintr = macio_irq(mdev, 1);
  1750. /* Space for dma command list: +1 for stop command,
  1751. * +1 to allow for aligning.
  1752. */
  1753. ms->dma_cmd_size = (mesh_host->sg_tablesize + 2) * sizeof(struct dbdma_cmd);
  1754. /* We use the PCI APIs for now until the generic one gets fixed
  1755. * enough or until we get some macio-specific versions
  1756. */
  1757. dma_cmd_space = pci_zalloc_consistent(macio_get_pci_dev(mdev),
  1758. ms->dma_cmd_size, &dma_cmd_bus);
  1759. if (dma_cmd_space == NULL) {
  1760. printk(KERN_ERR "mesh: can't allocate DMA table\n");
  1761. goto out_unmap;
  1762. }
  1763. ms->dma_cmds = (struct dbdma_cmd *) DBDMA_ALIGN(dma_cmd_space);
  1764. ms->dma_cmd_space = dma_cmd_space;
  1765. ms->dma_cmd_bus = dma_cmd_bus + ((unsigned long)ms->dma_cmds)
  1766. - (unsigned long)dma_cmd_space;
  1767. ms->current_req = NULL;
  1768. for (tgt = 0; tgt < 8; ++tgt) {
  1769. ms->tgts[tgt].sdtr_state = do_sdtr;
  1770. ms->tgts[tgt].sync_params = ASYNC_PARAMS;
  1771. ms->tgts[tgt].current_req = NULL;
  1772. }
  1773. if ((cfp = of_get_property(mesh, "clock-frequency", NULL)))
  1774. ms->clk_freq = *cfp;
  1775. else {
  1776. printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
  1777. ms->clk_freq = 50000000;
  1778. }
  1779. /* The maximum sync rate is clock / 5; increase
  1780. * mesh_sync_period if necessary.
  1781. */
  1782. minper = 1000000000 / (ms->clk_freq / 5); /* ns */
  1783. if (mesh_sync_period < minper)
  1784. mesh_sync_period = minper;
  1785. /* Power up the chip */
  1786. set_mesh_power(ms, 1);
  1787. /* Set it up */
  1788. mesh_init(ms);
  1789. /* Request interrupt */
  1790. if (request_irq(ms->meshintr, do_mesh_interrupt, 0, "MESH", ms)) {
  1791. printk(KERN_ERR "MESH: can't get irq %d\n", ms->meshintr);
  1792. goto out_shutdown;
  1793. }
  1794. /* Add scsi host & scan */
  1795. if (scsi_add_host(mesh_host, &mdev->ofdev.dev))
  1796. goto out_release_irq;
  1797. scsi_scan_host(mesh_host);
  1798. return 0;
  1799. out_release_irq:
  1800. free_irq(ms->meshintr, ms);
  1801. out_shutdown:
  1802. /* shutdown & reset bus in case of error or macos can be confused
  1803. * at reboot if the bus was set to synchronous mode already
  1804. */
  1805. mesh_shutdown(mdev);
  1806. set_mesh_power(ms, 0);
  1807. pci_free_consistent(macio_get_pci_dev(mdev), ms->dma_cmd_size,
  1808. ms->dma_cmd_space, ms->dma_cmd_bus);
  1809. out_unmap:
  1810. iounmap(ms->dma);
  1811. iounmap(ms->mesh);
  1812. out_free:
  1813. scsi_host_put(mesh_host);
  1814. out_release:
  1815. macio_release_resources(mdev);
  1816. return -ENODEV;
  1817. }
  1818. static int mesh_remove(struct macio_dev *mdev)
  1819. {
  1820. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1821. struct Scsi_Host *mesh_host = ms->host;
  1822. scsi_remove_host(mesh_host);
  1823. free_irq(ms->meshintr, ms);
  1824. /* Reset scsi bus */
  1825. mesh_shutdown(mdev);
  1826. /* Shut down chip & termination */
  1827. set_mesh_power(ms, 0);
  1828. /* Unmap registers & dma controller */
  1829. iounmap(ms->mesh);
  1830. iounmap(ms->dma);
  1831. /* Free DMA commands memory */
  1832. pci_free_consistent(macio_get_pci_dev(mdev), ms->dma_cmd_size,
  1833. ms->dma_cmd_space, ms->dma_cmd_bus);
  1834. /* Release memory resources */
  1835. macio_release_resources(mdev);
  1836. scsi_host_put(mesh_host);
  1837. return 0;
  1838. }
  1839. static struct of_device_id mesh_match[] =
  1840. {
  1841. {
  1842. .name = "mesh",
  1843. },
  1844. {
  1845. .type = "scsi",
  1846. .compatible = "chrp,mesh0"
  1847. },
  1848. {},
  1849. };
  1850. MODULE_DEVICE_TABLE (of, mesh_match);
  1851. static struct macio_driver mesh_driver =
  1852. {
  1853. .driver = {
  1854. .name = "mesh",
  1855. .owner = THIS_MODULE,
  1856. .of_match_table = mesh_match,
  1857. },
  1858. .probe = mesh_probe,
  1859. .remove = mesh_remove,
  1860. .shutdown = mesh_shutdown,
  1861. #ifdef CONFIG_PM
  1862. .suspend = mesh_suspend,
  1863. .resume = mesh_resume,
  1864. #endif
  1865. };
  1866. static int __init init_mesh(void)
  1867. {
  1868. /* Calculate sync rate from module parameters */
  1869. if (sync_rate > 10)
  1870. sync_rate = 10;
  1871. if (sync_rate > 0) {
  1872. printk(KERN_INFO "mesh: configured for synchronous %d MB/s\n", sync_rate);
  1873. mesh_sync_period = 1000 / sync_rate; /* ns */
  1874. mesh_sync_offset = 15;
  1875. } else
  1876. printk(KERN_INFO "mesh: configured for asynchronous\n");
  1877. return macio_register_driver(&mesh_driver);
  1878. }
  1879. static void __exit exit_mesh(void)
  1880. {
  1881. return macio_unregister_driver(&mesh_driver);
  1882. }
  1883. module_init(init_mesh);
  1884. module_exit(exit_mesh);