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