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