mesh.c 53 KB

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