mesh.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  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);
  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);
  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);
  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)
  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);
  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)
  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 mesg)
  1625. {
  1626. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1627. unsigned long flags;
  1628. switch (mesg.event) {
  1629. case PM_EVENT_SUSPEND:
  1630. case PM_EVENT_FREEZE:
  1631. break;
  1632. default:
  1633. return 0;
  1634. }
  1635. if (mesg.event == mdev->ofdev.dev.power.power_state.event)
  1636. return 0;
  1637. scsi_block_requests(ms->host);
  1638. spin_lock_irqsave(ms->host->host_lock, flags);
  1639. while(ms->phase != idle) {
  1640. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1641. msleep(10);
  1642. spin_lock_irqsave(ms->host->host_lock, flags);
  1643. }
  1644. ms->phase = sleeping;
  1645. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1646. disable_irq(ms->meshintr);
  1647. set_mesh_power(ms, 0);
  1648. mdev->ofdev.dev.power.power_state = mesg;
  1649. return 0;
  1650. }
  1651. static int mesh_resume(struct macio_dev *mdev)
  1652. {
  1653. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1654. unsigned long flags;
  1655. if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
  1656. return 0;
  1657. set_mesh_power(ms, 1);
  1658. mesh_init(ms);
  1659. spin_lock_irqsave(ms->host->host_lock, flags);
  1660. mesh_start(ms);
  1661. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1662. enable_irq(ms->meshintr);
  1663. scsi_unblock_requests(ms->host);
  1664. mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
  1665. return 0;
  1666. }
  1667. #endif /* CONFIG_PM */
  1668. /*
  1669. * If we leave drives set for synchronous transfers (especially
  1670. * CDROMs), and reboot to MacOS, it gets confused, poor thing.
  1671. * So, on reboot we reset the SCSI bus.
  1672. */
  1673. static int mesh_shutdown(struct macio_dev *mdev)
  1674. {
  1675. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1676. volatile struct mesh_regs __iomem *mr;
  1677. unsigned long flags;
  1678. printk(KERN_INFO "resetting MESH scsi bus(es)\n");
  1679. spin_lock_irqsave(ms->host->host_lock, flags);
  1680. mr = ms->mesh;
  1681. out_8(&mr->intr_mask, 0);
  1682. out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
  1683. out_8(&mr->bus_status1, BS1_RST);
  1684. mesh_flush_io(mr);
  1685. udelay(30);
  1686. out_8(&mr->bus_status1, 0);
  1687. spin_unlock_irqrestore(ms->host->host_lock, flags);
  1688. return 0;
  1689. }
  1690. static struct scsi_host_template mesh_template = {
  1691. .proc_name = "mesh",
  1692. .name = "MESH",
  1693. .queuecommand = mesh_queue,
  1694. .eh_abort_handler = mesh_abort,
  1695. .eh_host_reset_handler = mesh_host_reset,
  1696. .can_queue = 20,
  1697. .this_id = 7,
  1698. .sg_tablesize = SG_ALL,
  1699. .cmd_per_lun = 2,
  1700. .use_clustering = DISABLE_CLUSTERING,
  1701. };
  1702. static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
  1703. {
  1704. struct device_node *mesh = macio_get_of_node(mdev);
  1705. struct pci_dev* pdev = macio_get_pci_dev(mdev);
  1706. int tgt, minper;
  1707. const int *cfp;
  1708. struct mesh_state *ms;
  1709. struct Scsi_Host *mesh_host;
  1710. void *dma_cmd_space;
  1711. dma_addr_t dma_cmd_bus;
  1712. switch (mdev->bus->chip->type) {
  1713. case macio_heathrow:
  1714. case macio_gatwick:
  1715. case macio_paddington:
  1716. use_active_neg = 0;
  1717. break;
  1718. default:
  1719. use_active_neg = SEQ_ACTIVE_NEG;
  1720. }
  1721. if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
  1722. printk(KERN_ERR "mesh: expected 2 addrs and 2 intrs"
  1723. " (got %d,%d)\n", macio_resource_count(mdev),
  1724. macio_irq_count(mdev));
  1725. return -ENODEV;
  1726. }
  1727. if (macio_request_resources(mdev, "mesh") != 0) {
  1728. printk(KERN_ERR "mesh: unable to request memory resources");
  1729. return -EBUSY;
  1730. }
  1731. mesh_host = scsi_host_alloc(&mesh_template, sizeof(struct mesh_state));
  1732. if (mesh_host == NULL) {
  1733. printk(KERN_ERR "mesh: couldn't register host");
  1734. goto out_release;
  1735. }
  1736. /* Old junk for root discovery, that will die ultimately */
  1737. #if !defined(MODULE)
  1738. note_scsi_host(mesh, mesh_host);
  1739. #endif
  1740. mesh_host->base = macio_resource_start(mdev, 0);
  1741. mesh_host->irq = macio_irq(mdev, 0);
  1742. ms = (struct mesh_state *) mesh_host->hostdata;
  1743. macio_set_drvdata(mdev, ms);
  1744. ms->host = mesh_host;
  1745. ms->mdev = mdev;
  1746. ms->pdev = pdev;
  1747. ms->mesh = ioremap(macio_resource_start(mdev, 0), 0x1000);
  1748. if (ms->mesh == NULL) {
  1749. printk(KERN_ERR "mesh: can't map registers\n");
  1750. goto out_free;
  1751. }
  1752. ms->dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
  1753. if (ms->dma == NULL) {
  1754. printk(KERN_ERR "mesh: can't map registers\n");
  1755. iounmap(ms->mesh);
  1756. goto out_free;
  1757. }
  1758. ms->meshintr = macio_irq(mdev, 0);
  1759. ms->dmaintr = macio_irq(mdev, 1);
  1760. /* Space for dma command list: +1 for stop command,
  1761. * +1 to allow for aligning.
  1762. */
  1763. ms->dma_cmd_size = (mesh_host->sg_tablesize + 2) * sizeof(struct dbdma_cmd);
  1764. /* We use the PCI APIs for now until the generic one gets fixed
  1765. * enough or until we get some macio-specific versions
  1766. */
  1767. dma_cmd_space = pci_alloc_consistent(macio_get_pci_dev(mdev),
  1768. ms->dma_cmd_size,
  1769. &dma_cmd_bus);
  1770. if (dma_cmd_space == NULL) {
  1771. printk(KERN_ERR "mesh: can't allocate DMA table\n");
  1772. goto out_unmap;
  1773. }
  1774. memset(dma_cmd_space, 0, ms->dma_cmd_size);
  1775. ms->dma_cmds = (struct dbdma_cmd *) DBDMA_ALIGN(dma_cmd_space);
  1776. ms->dma_cmd_space = dma_cmd_space;
  1777. ms->dma_cmd_bus = dma_cmd_bus + ((unsigned long)ms->dma_cmds)
  1778. - (unsigned long)dma_cmd_space;
  1779. ms->current_req = NULL;
  1780. for (tgt = 0; tgt < 8; ++tgt) {
  1781. ms->tgts[tgt].sdtr_state = do_sdtr;
  1782. ms->tgts[tgt].sync_params = ASYNC_PARAMS;
  1783. ms->tgts[tgt].current_req = NULL;
  1784. }
  1785. if ((cfp = get_property(mesh, "clock-frequency", NULL)))
  1786. ms->clk_freq = *cfp;
  1787. else {
  1788. printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
  1789. ms->clk_freq = 50000000;
  1790. }
  1791. /* The maximum sync rate is clock / 5; increase
  1792. * mesh_sync_period if necessary.
  1793. */
  1794. minper = 1000000000 / (ms->clk_freq / 5); /* ns */
  1795. if (mesh_sync_period < minper)
  1796. mesh_sync_period = minper;
  1797. /* Power up the chip */
  1798. set_mesh_power(ms, 1);
  1799. /* Set it up */
  1800. mesh_init(ms);
  1801. /* Request interrupt */
  1802. if (request_irq(ms->meshintr, do_mesh_interrupt, 0, "MESH", ms)) {
  1803. printk(KERN_ERR "MESH: can't get irq %d\n", ms->meshintr);
  1804. goto out_shutdown;
  1805. }
  1806. /* Add scsi host & scan */
  1807. if (scsi_add_host(mesh_host, &mdev->ofdev.dev))
  1808. goto out_release_irq;
  1809. scsi_scan_host(mesh_host);
  1810. return 0;
  1811. out_release_irq:
  1812. free_irq(ms->meshintr, ms);
  1813. out_shutdown:
  1814. /* shutdown & reset bus in case of error or macos can be confused
  1815. * at reboot if the bus was set to synchronous mode already
  1816. */
  1817. mesh_shutdown(mdev);
  1818. set_mesh_power(ms, 0);
  1819. pci_free_consistent(macio_get_pci_dev(mdev), ms->dma_cmd_size,
  1820. ms->dma_cmd_space, ms->dma_cmd_bus);
  1821. out_unmap:
  1822. iounmap(ms->dma);
  1823. iounmap(ms->mesh);
  1824. out_free:
  1825. scsi_host_put(mesh_host);
  1826. out_release:
  1827. macio_release_resources(mdev);
  1828. return -ENODEV;
  1829. }
  1830. static int mesh_remove(struct macio_dev *mdev)
  1831. {
  1832. struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
  1833. struct Scsi_Host *mesh_host = ms->host;
  1834. scsi_remove_host(mesh_host);
  1835. free_irq(ms->meshintr, ms);
  1836. /* Reset scsi bus */
  1837. mesh_shutdown(mdev);
  1838. /* Shut down chip & termination */
  1839. set_mesh_power(ms, 0);
  1840. /* Unmap registers & dma controller */
  1841. iounmap(ms->mesh);
  1842. iounmap(ms->dma);
  1843. /* Free DMA commands memory */
  1844. pci_free_consistent(macio_get_pci_dev(mdev), ms->dma_cmd_size,
  1845. ms->dma_cmd_space, ms->dma_cmd_bus);
  1846. /* Release memory resources */
  1847. macio_release_resources(mdev);
  1848. scsi_host_put(mesh_host);
  1849. return 0;
  1850. }
  1851. static struct of_device_id mesh_match[] =
  1852. {
  1853. {
  1854. .name = "mesh",
  1855. },
  1856. {
  1857. .type = "scsi",
  1858. .compatible = "chrp,mesh0"
  1859. },
  1860. {},
  1861. };
  1862. MODULE_DEVICE_TABLE (of, mesh_match);
  1863. static struct macio_driver mesh_driver =
  1864. {
  1865. .name = "mesh",
  1866. .match_table = mesh_match,
  1867. .probe = mesh_probe,
  1868. .remove = mesh_remove,
  1869. .shutdown = mesh_shutdown,
  1870. #ifdef CONFIG_PM
  1871. .suspend = mesh_suspend,
  1872. .resume = mesh_resume,
  1873. #endif
  1874. };
  1875. static int __init init_mesh(void)
  1876. {
  1877. /* Calculate sync rate from module parameters */
  1878. if (sync_rate > 10)
  1879. sync_rate = 10;
  1880. if (sync_rate > 0) {
  1881. printk(KERN_INFO "mesh: configured for synchronous %d MB/s\n", sync_rate);
  1882. mesh_sync_period = 1000 / sync_rate; /* ns */
  1883. mesh_sync_offset = 15;
  1884. } else
  1885. printk(KERN_INFO "mesh: configured for asynchronous\n");
  1886. return macio_register_driver(&mesh_driver);
  1887. }
  1888. static void __exit exit_mesh(void)
  1889. {
  1890. return macio_unregister_driver(&mesh_driver);
  1891. }
  1892. module_init(init_mesh);
  1893. module_exit(exit_mesh);