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