mesh.c 53 KB

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