fas216.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035
  1. /*
  2. * linux/drivers/acorn/scsi/fas216.c
  3. *
  4. * Copyright (C) 1997-2003 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
  11. * other sources, including:
  12. * the AMD Am53CF94 data sheet
  13. * the AMD Am53C94 data sheet
  14. *
  15. * This is a generic driver. To use it, have a look at cumana_2.c. You
  16. * should define your own structure that overlays FAS216_Info, eg:
  17. * struct my_host_data {
  18. * FAS216_Info info;
  19. * ... my host specific data ...
  20. * };
  21. *
  22. * Changelog:
  23. * 30-08-1997 RMK Created
  24. * 14-09-1997 RMK Started disconnect support
  25. * 08-02-1998 RMK Corrected real DMA support
  26. * 15-02-1998 RMK Started sync xfer support
  27. * 06-04-1998 RMK Tightened conditions for printing incomplete
  28. * transfers
  29. * 02-05-1998 RMK Added extra checks in fas216_reset
  30. * 24-05-1998 RMK Fixed synchronous transfers with period >= 200ns
  31. * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
  32. * 26-08-1998 RMK Improved message support wrt MESSAGE_REJECT
  33. * 02-04-2000 RMK Converted to use the new error handling, and
  34. * automatically request sense data upon check
  35. * condition status from targets.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/kernel.h>
  40. #include <linux/string.h>
  41. #include <linux/ioport.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/delay.h>
  44. #include <linux/bitops.h>
  45. #include <linux/init.h>
  46. #include <linux/interrupt.h>
  47. #include <asm/dma.h>
  48. #include <asm/io.h>
  49. #include <asm/irq.h>
  50. #include <asm/ecard.h>
  51. #include "../scsi.h"
  52. #include <scsi/scsi_dbg.h>
  53. #include <scsi/scsi_host.h>
  54. #include "fas216.h"
  55. #include "scsi.h"
  56. /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
  57. * the data sheet. This restriction is crazy, especially when
  58. * you only want to send 16 bytes! What were the guys who
  59. * designed this chip on at that time? Did they read the SCSI2
  60. * spec at all? The following sections are taken from the SCSI2
  61. * standard (s2r10) concerning this:
  62. *
  63. * > IMPLEMENTORS NOTES:
  64. * > (1) Re-negotiation at every selection is not recommended, since a
  65. * > significant performance impact is likely.
  66. *
  67. * > The implied synchronous agreement shall remain in effect until a BUS DEVICE
  68. * > RESET message is received, until a hard reset condition occurs, or until one
  69. * > of the two SCSI devices elects to modify the agreement. The default data
  70. * > transfer mode is asynchronous data transfer mode. The default data transfer
  71. * > mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
  72. * > reset condition.
  73. *
  74. * In total, this means that once you have elected to use synchronous
  75. * transfers, you must always use DMA.
  76. *
  77. * I was thinking that this was a good chip until I found this restriction ;(
  78. */
  79. #define SCSI2_SYNC
  80. #undef SCSI2_TAG
  81. #undef DEBUG_CONNECT
  82. #undef DEBUG_MESSAGES
  83. #undef CHECK_STRUCTURE
  84. #define LOG_CONNECT (1 << 0)
  85. #define LOG_BUSSERVICE (1 << 1)
  86. #define LOG_FUNCTIONDONE (1 << 2)
  87. #define LOG_MESSAGES (1 << 3)
  88. #define LOG_BUFFER (1 << 4)
  89. #define LOG_ERROR (1 << 8)
  90. static int level_mask = LOG_ERROR;
  91. module_param(level_mask, int, 0644);
  92. #ifndef MODULE
  93. static int __init fas216_log_setup(char *str)
  94. {
  95. char *s;
  96. level_mask = 0;
  97. while ((s = strsep(&str, ",")) != NULL) {
  98. switch (s[0]) {
  99. case 'a':
  100. if (strcmp(s, "all") == 0)
  101. level_mask |= -1;
  102. break;
  103. case 'b':
  104. if (strncmp(s, "bus", 3) == 0)
  105. level_mask |= LOG_BUSSERVICE;
  106. if (strncmp(s, "buf", 3) == 0)
  107. level_mask |= LOG_BUFFER;
  108. break;
  109. case 'c':
  110. level_mask |= LOG_CONNECT;
  111. break;
  112. case 'e':
  113. level_mask |= LOG_ERROR;
  114. break;
  115. case 'm':
  116. level_mask |= LOG_MESSAGES;
  117. break;
  118. case 'n':
  119. if (strcmp(s, "none") == 0)
  120. level_mask = 0;
  121. break;
  122. case 's':
  123. level_mask |= LOG_FUNCTIONDONE;
  124. break;
  125. }
  126. }
  127. return 1;
  128. }
  129. __setup("fas216_logging=", fas216_log_setup);
  130. #endif
  131. static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
  132. {
  133. unsigned int off = reg << info->scsi.io_shift;
  134. return readb(info->scsi.io_base + off);
  135. }
  136. static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
  137. {
  138. unsigned int off = reg << info->scsi.io_shift;
  139. writeb(val, info->scsi.io_base + off);
  140. }
  141. static void fas216_dumpstate(FAS216_Info *info)
  142. {
  143. unsigned char is, stat, inst;
  144. is = fas216_readb(info, REG_IS);
  145. stat = fas216_readb(info, REG_STAT);
  146. inst = fas216_readb(info, REG_INST);
  147. printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
  148. " INST=%02X IS=%02X CFIS=%02X",
  149. fas216_readb(info, REG_CTCL),
  150. fas216_readb(info, REG_CTCM),
  151. fas216_readb(info, REG_CMD), stat, inst, is,
  152. fas216_readb(info, REG_CFIS));
  153. printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
  154. fas216_readb(info, REG_CNTL1),
  155. fas216_readb(info, REG_CNTL2),
  156. fas216_readb(info, REG_CNTL3),
  157. fas216_readb(info, REG_CTCH));
  158. }
  159. static void print_SCp(struct scsi_pointer *SCp, const char *prefix, const char *suffix)
  160. {
  161. printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
  162. prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
  163. SCp->buffers_residual, suffix);
  164. }
  165. #ifdef CHECK_STRUCTURE
  166. static void fas216_dumpinfo(FAS216_Info *info)
  167. {
  168. static int used = 0;
  169. int i;
  170. if (used++)
  171. return;
  172. printk("FAS216_Info=\n");
  173. printk(" { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
  174. info->magic_start, info->host, info->SCpnt,
  175. info->origSCpnt);
  176. printk(" scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
  177. info->scsi.io_shift, info->scsi.irq,
  178. info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
  179. info->scsi.cfg[3]);
  180. printk(" type=%p phase=%X\n",
  181. info->scsi.type, info->scsi.phase);
  182. print_SCp(&info->scsi.SCp, " SCp={ ", " }\n");
  183. printk(" msgs async_stp=%X disconnectable=%d aborting=%d }\n",
  184. info->scsi.async_stp,
  185. info->scsi.disconnectable, info->scsi.aborting);
  186. printk(" stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
  187. " disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
  188. info->stats.queues, info->stats.removes, info->stats.fins,
  189. info->stats.reads, info->stats.writes, info->stats.miscs,
  190. info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
  191. info->stats.host_resets);
  192. printk(" ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
  193. info->ifcfg.clockrate, info->ifcfg.select_timeout,
  194. info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
  195. for (i = 0; i < 8; i++) {
  196. printk(" busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
  197. i, info->busyluns[i], i,
  198. info->device[i].disconnect_ok, info->device[i].stp,
  199. info->device[i].sof, info->device[i].sync_state);
  200. }
  201. printk(" dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
  202. info->dma.transfer_type, info->dma.setup,
  203. info->dma.pseudo, info->dma.stop);
  204. printk(" internal_done=%X magic_end=%lX }\n",
  205. info->internal_done, info->magic_end);
  206. }
  207. static void __fas216_checkmagic(FAS216_Info *info, const char *func)
  208. {
  209. int corruption = 0;
  210. if (info->magic_start != MAGIC) {
  211. printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
  212. corruption++;
  213. }
  214. if (info->magic_end != MAGIC) {
  215. printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
  216. corruption++;
  217. }
  218. if (corruption) {
  219. fas216_dumpinfo(info);
  220. panic("scsi memory space corrupted in %s", func);
  221. }
  222. }
  223. #define fas216_checkmagic(info) __fas216_checkmagic((info), __func__)
  224. #else
  225. #define fas216_checkmagic(info)
  226. #endif
  227. static const char *fas216_bus_phase(int stat)
  228. {
  229. static const char *phases[] = {
  230. "DATA OUT", "DATA IN",
  231. "COMMAND", "STATUS",
  232. "MISC OUT", "MISC IN",
  233. "MESG OUT", "MESG IN"
  234. };
  235. return phases[stat & STAT_BUSMASK];
  236. }
  237. static const char *fas216_drv_phase(FAS216_Info *info)
  238. {
  239. static const char *phases[] = {
  240. [PHASE_IDLE] = "idle",
  241. [PHASE_SELECTION] = "selection",
  242. [PHASE_COMMAND] = "command",
  243. [PHASE_DATAOUT] = "data out",
  244. [PHASE_DATAIN] = "data in",
  245. [PHASE_MSGIN] = "message in",
  246. [PHASE_MSGIN_DISCONNECT]= "disconnect",
  247. [PHASE_MSGOUT_EXPECT] = "expect message out",
  248. [PHASE_MSGOUT] = "message out",
  249. [PHASE_STATUS] = "status",
  250. [PHASE_DONE] = "done",
  251. };
  252. if (info->scsi.phase < ARRAY_SIZE(phases) &&
  253. phases[info->scsi.phase])
  254. return phases[info->scsi.phase];
  255. return "???";
  256. }
  257. static char fas216_target(FAS216_Info *info)
  258. {
  259. if (info->SCpnt)
  260. return '0' + info->SCpnt->device->id;
  261. else
  262. return 'H';
  263. }
  264. static void
  265. fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
  266. {
  267. static char buf[1024];
  268. vsnprintf(buf, sizeof(buf), fmt, ap);
  269. printk("scsi%d.%c: %s", info->host->host_no, target, buf);
  270. }
  271. static void fas216_log_command(FAS216_Info *info, int level,
  272. struct scsi_cmnd *SCpnt, char *fmt, ...)
  273. {
  274. va_list args;
  275. if (level != 0 && !(level & level_mask))
  276. return;
  277. va_start(args, fmt);
  278. fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
  279. va_end(args);
  280. scsi_print_command(SCpnt);
  281. }
  282. static void
  283. fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
  284. {
  285. va_list args;
  286. if (level != 0 && !(level & level_mask))
  287. return;
  288. if (target < 0)
  289. target = 'H';
  290. else
  291. target += '0';
  292. va_start(args, fmt);
  293. fas216_do_log(info, target, fmt, args);
  294. va_end(args);
  295. printk("\n");
  296. }
  297. static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
  298. {
  299. va_list args;
  300. if (level != 0 && !(level & level_mask))
  301. return;
  302. va_start(args, fmt);
  303. fas216_do_log(info, fas216_target(info), fmt, args);
  304. va_end(args);
  305. printk("\n");
  306. }
  307. #define PH_SIZE 32
  308. static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
  309. static int ph_ptr;
  310. static void add_debug_list(int stat, int ssr, int isr, int ph)
  311. {
  312. ph_list[ph_ptr].stat = stat;
  313. ph_list[ph_ptr].ssr = ssr;
  314. ph_list[ph_ptr].isr = isr;
  315. ph_list[ph_ptr].ph = ph;
  316. ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
  317. }
  318. static struct { int command; void *from; } cmd_list[8];
  319. static int cmd_ptr;
  320. static void fas216_cmd(FAS216_Info *info, unsigned int command)
  321. {
  322. cmd_list[cmd_ptr].command = command;
  323. cmd_list[cmd_ptr].from = __builtin_return_address(0);
  324. cmd_ptr = (cmd_ptr + 1) & 7;
  325. fas216_writeb(info, REG_CMD, command);
  326. }
  327. static void print_debug_list(void)
  328. {
  329. int i;
  330. i = ph_ptr;
  331. printk(KERN_ERR "SCSI IRQ trail\n");
  332. do {
  333. printk(" %02x:%02x:%02x:%1x",
  334. ph_list[i].stat, ph_list[i].ssr,
  335. ph_list[i].isr, ph_list[i].ph);
  336. i = (i + 1) & (PH_SIZE - 1);
  337. if (((i ^ ph_ptr) & 7) == 0)
  338. printk("\n");
  339. } while (i != ph_ptr);
  340. if ((i ^ ph_ptr) & 7)
  341. printk("\n");
  342. i = cmd_ptr;
  343. printk(KERN_ERR "FAS216 commands: ");
  344. do {
  345. printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
  346. i = (i + 1) & 7;
  347. } while (i != cmd_ptr);
  348. printk("\n");
  349. }
  350. static void fas216_done(FAS216_Info *info, unsigned int result);
  351. /**
  352. * fas216_get_last_msg - retrive last message from the list
  353. * @info: interface to search
  354. * @pos: current fifo position
  355. *
  356. * Retrieve a last message from the list, using position in fifo.
  357. */
  358. static inline unsigned short
  359. fas216_get_last_msg(FAS216_Info *info, int pos)
  360. {
  361. unsigned short packed_msg = NOP;
  362. struct message *msg;
  363. int msgnr = 0;
  364. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  365. if (pos >= msg->fifo)
  366. break;
  367. }
  368. if (msg) {
  369. if (msg->msg[0] == EXTENDED_MESSAGE)
  370. packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
  371. else
  372. packed_msg = msg->msg[0];
  373. }
  374. fas216_log(info, LOG_MESSAGES,
  375. "Message: %04x found at position %02x\n", packed_msg, pos);
  376. return packed_msg;
  377. }
  378. /**
  379. * fas216_syncperiod - calculate STP register value
  380. * @info: state structure for interface connected to device
  381. * @ns: period in ns (between subsequent bytes)
  382. *
  383. * Calculate value to be loaded into the STP register for a given period
  384. * in ns. Returns a value suitable for REG_STP.
  385. */
  386. static int fas216_syncperiod(FAS216_Info *info, int ns)
  387. {
  388. int value = (info->ifcfg.clockrate * ns) / 1000;
  389. fas216_checkmagic(info);
  390. if (value < 4)
  391. value = 4;
  392. else if (value > 35)
  393. value = 35;
  394. return value & 31;
  395. }
  396. /**
  397. * fas216_set_sync - setup FAS216 chip for specified transfer period.
  398. * @info: state structure for interface connected to device
  399. * @target: target
  400. *
  401. * Correctly setup FAS216 chip for specified transfer period.
  402. * Notes : we need to switch the chip out of FASTSCSI mode if we have
  403. * a transfer period >= 200ns - otherwise the chip will violate
  404. * the SCSI timings.
  405. */
  406. static void fas216_set_sync(FAS216_Info *info, int target)
  407. {
  408. unsigned int cntl3;
  409. fas216_writeb(info, REG_SOF, info->device[target].sof);
  410. fas216_writeb(info, REG_STP, info->device[target].stp);
  411. cntl3 = info->scsi.cfg[2];
  412. if (info->device[target].period >= (200 / 4))
  413. cntl3 = cntl3 & ~CNTL3_FASTSCSI;
  414. fas216_writeb(info, REG_CNTL3, cntl3);
  415. }
  416. /* Synchronous transfer support
  417. *
  418. * Note: The SCSI II r10 spec says (5.6.12):
  419. *
  420. * (2) Due to historical problems with early host adapters that could
  421. * not accept an SDTR message, some targets may not initiate synchronous
  422. * negotiation after a power cycle as required by this standard. Host
  423. * adapters that support synchronous mode may avoid the ensuing failure
  424. * modes when the target is independently power cycled by initiating a
  425. * synchronous negotiation on each REQUEST SENSE and INQUIRY command.
  426. * This approach increases the SCSI bus overhead and is not recommended
  427. * for new implementations. The correct method is to respond to an
  428. * SDTR message with a MESSAGE REJECT message if the either the
  429. * initiator or target devices does not support synchronous transfers
  430. * or does not want to negotiate for synchronous transfers at the time.
  431. * Using the correct method assures compatibility with wide data
  432. * transfers and future enhancements.
  433. *
  434. * We will always initiate a synchronous transfer negotiation request on
  435. * every INQUIRY or REQUEST SENSE message, unless the target itself has
  436. * at some point performed a synchronous transfer negotiation request, or
  437. * we have synchronous transfers disabled for this device.
  438. */
  439. /**
  440. * fas216_handlesync - Handle a synchronous transfer message
  441. * @info: state structure for interface
  442. * @msg: message from target
  443. *
  444. * Handle a synchronous transfer message from the target
  445. */
  446. static void fas216_handlesync(FAS216_Info *info, char *msg)
  447. {
  448. struct fas216_device *dev = &info->device[info->SCpnt->device->id];
  449. enum { sync, async, none, reject } res = none;
  450. #ifdef SCSI2_SYNC
  451. switch (msg[0]) {
  452. case MESSAGE_REJECT:
  453. /* Synchronous transfer request failed.
  454. * Note: SCSI II r10:
  455. *
  456. * SCSI devices that are capable of synchronous
  457. * data transfers shall not respond to an SDTR
  458. * message with a MESSAGE REJECT message.
  459. *
  460. * Hence, if we get this condition, we disable
  461. * negotiation for this device.
  462. */
  463. if (dev->sync_state == neg_inprogress) {
  464. dev->sync_state = neg_invalid;
  465. res = async;
  466. }
  467. break;
  468. case EXTENDED_MESSAGE:
  469. switch (dev->sync_state) {
  470. /* We don't accept synchronous transfer requests.
  471. * Respond with a MESSAGE_REJECT to prevent a
  472. * synchronous transfer agreement from being reached.
  473. */
  474. case neg_invalid:
  475. res = reject;
  476. break;
  477. /* We were not negotiating a synchronous transfer,
  478. * but the device sent us a negotiation request.
  479. * Honour the request by sending back a SDTR
  480. * message containing our capability, limited by
  481. * the targets capability.
  482. */
  483. default:
  484. fas216_cmd(info, CMD_SETATN);
  485. if (msg[4] > info->ifcfg.sync_max_depth)
  486. msg[4] = info->ifcfg.sync_max_depth;
  487. if (msg[3] < 1000 / info->ifcfg.clockrate)
  488. msg[3] = 1000 / info->ifcfg.clockrate;
  489. msgqueue_flush(&info->scsi.msgs);
  490. msgqueue_addmsg(&info->scsi.msgs, 5,
  491. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  492. msg[3], msg[4]);
  493. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  494. /* This is wrong. The agreement is not in effect
  495. * until this message is accepted by the device
  496. */
  497. dev->sync_state = neg_targcomplete;
  498. res = sync;
  499. break;
  500. /* We initiated the synchronous transfer negotiation,
  501. * and have successfully received a response from the
  502. * target. The synchronous transfer agreement has been
  503. * reached. Note: if the values returned are out of our
  504. * bounds, we must reject the message.
  505. */
  506. case neg_inprogress:
  507. res = reject;
  508. if (msg[4] <= info->ifcfg.sync_max_depth &&
  509. msg[3] >= 1000 / info->ifcfg.clockrate) {
  510. dev->sync_state = neg_complete;
  511. res = sync;
  512. }
  513. break;
  514. }
  515. }
  516. #else
  517. res = reject;
  518. #endif
  519. switch (res) {
  520. case sync:
  521. dev->period = msg[3];
  522. dev->sof = msg[4];
  523. dev->stp = fas216_syncperiod(info, msg[3] * 4);
  524. fas216_set_sync(info, info->SCpnt->device->id);
  525. break;
  526. case reject:
  527. fas216_cmd(info, CMD_SETATN);
  528. msgqueue_flush(&info->scsi.msgs);
  529. msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
  530. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  531. case async:
  532. dev->period = info->ifcfg.asyncperiod / 4;
  533. dev->sof = 0;
  534. dev->stp = info->scsi.async_stp;
  535. fas216_set_sync(info, info->SCpnt->device->id);
  536. break;
  537. case none:
  538. break;
  539. }
  540. }
  541. /**
  542. * fas216_updateptrs - update data pointers after transfer suspended/paused
  543. * @info: interface's local pointer to update
  544. * @bytes_transferred: number of bytes transferred
  545. *
  546. * Update data pointers after transfer suspended/paused
  547. */
  548. static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
  549. {
  550. struct scsi_pointer *SCp = &info->scsi.SCp;
  551. fas216_checkmagic(info);
  552. BUG_ON(bytes_transferred < 0);
  553. SCp->phase -= bytes_transferred;
  554. while (bytes_transferred != 0) {
  555. if (SCp->this_residual > bytes_transferred)
  556. break;
  557. /*
  558. * We have used up this buffer. Move on to the
  559. * next buffer.
  560. */
  561. bytes_transferred -= SCp->this_residual;
  562. if (!next_SCp(SCp) && bytes_transferred) {
  563. printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
  564. info->host->host_no, '0' + info->SCpnt->device->id);
  565. return;
  566. }
  567. }
  568. SCp->this_residual -= bytes_transferred;
  569. if (SCp->this_residual)
  570. SCp->ptr += bytes_transferred;
  571. else
  572. SCp->ptr = NULL;
  573. }
  574. /**
  575. * fas216_pio - transfer data off of/on to card using programmed IO
  576. * @info: interface to transfer data to/from
  577. * @direction: direction to transfer data (DMA_OUT/DMA_IN)
  578. *
  579. * Transfer data off of/on to card using programmed IO.
  580. * Notes: this is incredibly slow.
  581. */
  582. static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
  583. {
  584. struct scsi_pointer *SCp = &info->scsi.SCp;
  585. fas216_checkmagic(info);
  586. if (direction == DMA_OUT)
  587. fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
  588. else
  589. put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
  590. if (SCp->this_residual == 0)
  591. next_SCp(SCp);
  592. }
  593. static void fas216_set_stc(FAS216_Info *info, unsigned int length)
  594. {
  595. fas216_writeb(info, REG_STCL, length);
  596. fas216_writeb(info, REG_STCM, length >> 8);
  597. fas216_writeb(info, REG_STCH, length >> 16);
  598. }
  599. static unsigned int fas216_get_ctc(FAS216_Info *info)
  600. {
  601. return fas216_readb(info, REG_CTCL) +
  602. (fas216_readb(info, REG_CTCM) << 8) +
  603. (fas216_readb(info, REG_CTCH) << 16);
  604. }
  605. /**
  606. * fas216_cleanuptransfer - clean up after a transfer has completed.
  607. * @info: interface to clean up
  608. *
  609. * Update the data pointers according to the number of bytes transferred
  610. * on the SCSI bus.
  611. */
  612. static void fas216_cleanuptransfer(FAS216_Info *info)
  613. {
  614. unsigned long total, residual, fifo;
  615. fasdmatype_t dmatype = info->dma.transfer_type;
  616. info->dma.transfer_type = fasdma_none;
  617. /*
  618. * PIO transfers do not need to be cleaned up.
  619. */
  620. if (dmatype == fasdma_pio || dmatype == fasdma_none)
  621. return;
  622. if (dmatype == fasdma_real_all)
  623. total = info->scsi.SCp.phase;
  624. else
  625. total = info->scsi.SCp.this_residual;
  626. residual = fas216_get_ctc(info);
  627. fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  628. fas216_log(info, LOG_BUFFER, "cleaning up from previous "
  629. "transfer: length 0x%06x, residual 0x%x, fifo %d",
  630. total, residual, fifo);
  631. /*
  632. * If we were performing Data-Out, the transfer counter
  633. * counts down each time a byte is transferred by the
  634. * host to the FIFO. This means we must include the
  635. * bytes left in the FIFO from the transfer counter.
  636. */
  637. if (info->scsi.phase == PHASE_DATAOUT)
  638. residual += fifo;
  639. fas216_updateptrs(info, total - residual);
  640. }
  641. /**
  642. * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
  643. * @info: interface from which device disconnected from
  644. *
  645. * Start a DMA/PIO transfer off of/on to card
  646. */
  647. static void fas216_transfer(FAS216_Info *info)
  648. {
  649. fasdmadir_t direction;
  650. fasdmatype_t dmatype;
  651. fas216_log(info, LOG_BUFFER,
  652. "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
  653. info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
  654. info->scsi.SCp.phase);
  655. if (!info->scsi.SCp.ptr) {
  656. fas216_log(info, LOG_ERROR, "null buffer passed to "
  657. "fas216_starttransfer");
  658. print_SCp(&info->scsi.SCp, "SCp: ", "\n");
  659. print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
  660. return;
  661. }
  662. /*
  663. * If we have a synchronous transfer agreement in effect, we must
  664. * use DMA mode. If we are using asynchronous transfers, we may
  665. * use DMA mode or PIO mode.
  666. */
  667. if (info->device[info->SCpnt->device->id].sof)
  668. dmatype = fasdma_real_all;
  669. else
  670. dmatype = fasdma_pio;
  671. if (info->scsi.phase == PHASE_DATAOUT)
  672. direction = DMA_OUT;
  673. else
  674. direction = DMA_IN;
  675. if (info->dma.setup)
  676. dmatype = info->dma.setup(info->host, &info->scsi.SCp,
  677. direction, dmatype);
  678. info->dma.transfer_type = dmatype;
  679. if (dmatype == fasdma_real_all)
  680. fas216_set_stc(info, info->scsi.SCp.phase);
  681. else
  682. fas216_set_stc(info, info->scsi.SCp.this_residual);
  683. switch (dmatype) {
  684. case fasdma_pio:
  685. fas216_log(info, LOG_BUFFER, "PIO transfer");
  686. fas216_writeb(info, REG_SOF, 0);
  687. fas216_writeb(info, REG_STP, info->scsi.async_stp);
  688. fas216_cmd(info, CMD_TRANSFERINFO);
  689. fas216_pio(info, direction);
  690. break;
  691. case fasdma_pseudo:
  692. fas216_log(info, LOG_BUFFER, "pseudo transfer");
  693. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  694. info->dma.pseudo(info->host, &info->scsi.SCp,
  695. direction, info->SCpnt->transfersize);
  696. break;
  697. case fasdma_real_block:
  698. fas216_log(info, LOG_BUFFER, "block dma transfer");
  699. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  700. break;
  701. case fasdma_real_all:
  702. fas216_log(info, LOG_BUFFER, "total dma transfer");
  703. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  704. break;
  705. default:
  706. fas216_log(info, LOG_BUFFER | LOG_ERROR,
  707. "invalid FAS216 DMA type");
  708. break;
  709. }
  710. }
  711. /**
  712. * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
  713. * @info: interface from which device disconnected from
  714. *
  715. * Called when we switch away from DATA IN or DATA OUT phases.
  716. */
  717. static void fas216_stoptransfer(FAS216_Info *info)
  718. {
  719. fas216_checkmagic(info);
  720. if (info->dma.transfer_type == fasdma_real_all ||
  721. info->dma.transfer_type == fasdma_real_block)
  722. info->dma.stop(info->host, &info->scsi.SCp);
  723. fas216_cleanuptransfer(info);
  724. if (info->scsi.phase == PHASE_DATAIN) {
  725. unsigned int fifo;
  726. /*
  727. * If we were performing Data-In, then the FIFO counter
  728. * contains the number of bytes not transferred via DMA
  729. * from the on-board FIFO. Read them manually.
  730. */
  731. fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  732. while (fifo && info->scsi.SCp.ptr) {
  733. *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
  734. fas216_updateptrs(info, 1);
  735. fifo--;
  736. }
  737. } else {
  738. /*
  739. * After a Data-Out phase, there may be unsent
  740. * bytes left in the FIFO. Flush them out.
  741. */
  742. fas216_cmd(info, CMD_FLUSHFIFO);
  743. }
  744. }
  745. static void fas216_aborttransfer(FAS216_Info *info)
  746. {
  747. fas216_checkmagic(info);
  748. if (info->dma.transfer_type == fasdma_real_all ||
  749. info->dma.transfer_type == fasdma_real_block)
  750. info->dma.stop(info->host, &info->scsi.SCp);
  751. info->dma.transfer_type = fasdma_none;
  752. fas216_cmd(info, CMD_FLUSHFIFO);
  753. }
  754. static void fas216_kick(FAS216_Info *info);
  755. /**
  756. * fas216_disconnected_intr - handle device disconnection
  757. * @info: interface from which device disconnected from
  758. *
  759. * Handle device disconnection
  760. */
  761. static void fas216_disconnect_intr(FAS216_Info *info)
  762. {
  763. unsigned long flags;
  764. fas216_checkmagic(info);
  765. fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
  766. info->scsi.phase);
  767. msgqueue_flush(&info->scsi.msgs);
  768. switch (info->scsi.phase) {
  769. case PHASE_SELECTION: /* while selecting - no target */
  770. case PHASE_SELSTEPS:
  771. fas216_done(info, DID_NO_CONNECT);
  772. break;
  773. case PHASE_MSGIN_DISCONNECT: /* message in - disconnecting */
  774. info->scsi.disconnectable = 1;
  775. info->scsi.phase = PHASE_IDLE;
  776. info->stats.disconnects += 1;
  777. spin_lock_irqsave(&info->host_lock, flags);
  778. if (info->scsi.phase == PHASE_IDLE)
  779. fas216_kick(info);
  780. spin_unlock_irqrestore(&info->host_lock, flags);
  781. break;
  782. case PHASE_DONE: /* at end of command - complete */
  783. fas216_done(info, DID_OK);
  784. break;
  785. case PHASE_MSGOUT: /* message out - possible ABORT message */
  786. if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
  787. info->scsi.aborting = 0;
  788. fas216_done(info, DID_ABORT);
  789. break;
  790. }
  791. default: /* huh? */
  792. printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
  793. info->host->host_no, fas216_target(info), fas216_drv_phase(info));
  794. print_debug_list();
  795. fas216_stoptransfer(info);
  796. fas216_done(info, DID_ERROR);
  797. break;
  798. }
  799. }
  800. /**
  801. * fas216_reselected_intr - start reconnection of a device
  802. * @info: interface which was reselected
  803. *
  804. * Start reconnection of a device
  805. */
  806. static void
  807. fas216_reselected_intr(FAS216_Info *info)
  808. {
  809. unsigned int cfis, i;
  810. unsigned char msg[4];
  811. unsigned char target, lun, tag;
  812. fas216_checkmagic(info);
  813. WARN_ON(info->scsi.phase == PHASE_SELECTION ||
  814. info->scsi.phase == PHASE_SELSTEPS);
  815. cfis = fas216_readb(info, REG_CFIS);
  816. fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
  817. info->scsi.phase, cfis);
  818. cfis &= CFIS_CF;
  819. if (cfis < 2 || cfis > 4) {
  820. printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
  821. info->host->host_no);
  822. goto bad_message;
  823. }
  824. for (i = 0; i < cfis; i++)
  825. msg[i] = fas216_readb(info, REG_FF);
  826. if (!(msg[0] & (1 << info->host->this_id)) ||
  827. !(msg[1] & 0x80))
  828. goto initiator_error;
  829. target = msg[0] & ~(1 << info->host->this_id);
  830. target = ffs(target) - 1;
  831. lun = msg[1] & 7;
  832. tag = 0;
  833. if (cfis >= 3) {
  834. if (msg[2] != SIMPLE_QUEUE_TAG)
  835. goto initiator_error;
  836. tag = msg[3];
  837. }
  838. /* set up for synchronous transfers */
  839. fas216_writeb(info, REG_SDID, target);
  840. fas216_set_sync(info, target);
  841. msgqueue_flush(&info->scsi.msgs);
  842. fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
  843. target, lun, tag);
  844. if (info->scsi.disconnectable && info->SCpnt) {
  845. info->scsi.disconnectable = 0;
  846. if (info->SCpnt->device->id == target &&
  847. info->SCpnt->device->lun == lun &&
  848. info->SCpnt->tag == tag) {
  849. fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
  850. } else {
  851. queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
  852. fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
  853. info->SCpnt = NULL;
  854. }
  855. }
  856. if (!info->SCpnt) {
  857. info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
  858. target, lun, tag);
  859. fas216_log(info, LOG_CONNECT, "had to get command");
  860. }
  861. if (info->SCpnt) {
  862. /*
  863. * Restore data pointer from SAVED data pointer
  864. */
  865. info->scsi.SCp = info->SCpnt->SCp;
  866. fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
  867. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  868. info->scsi.phase = PHASE_MSGIN;
  869. } else {
  870. /*
  871. * Our command structure not found - abort the
  872. * command on the target. Since we have no
  873. * record of this command, we can't send
  874. * an INITIATOR DETECTED ERROR message.
  875. */
  876. fas216_cmd(info, CMD_SETATN);
  877. #if 0
  878. if (tag)
  879. msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
  880. else
  881. #endif
  882. msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
  883. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  884. info->scsi.aborting = 1;
  885. }
  886. fas216_cmd(info, CMD_MSGACCEPTED);
  887. return;
  888. initiator_error:
  889. printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
  890. info->host->host_no);
  891. for (i = 0; i < cfis; i++)
  892. printk(" %02x", msg[i]);
  893. printk("\n");
  894. bad_message:
  895. fas216_cmd(info, CMD_SETATN);
  896. msgqueue_flush(&info->scsi.msgs);
  897. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  898. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  899. fas216_cmd(info, CMD_MSGACCEPTED);
  900. }
  901. static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
  902. {
  903. int i;
  904. switch (message[0]) {
  905. case COMMAND_COMPLETE:
  906. if (msglen != 1)
  907. goto unrecognised;
  908. printk(KERN_ERR "scsi%d.%c: command complete with no "
  909. "status in MESSAGE_IN?\n",
  910. info->host->host_no, fas216_target(info));
  911. break;
  912. case SAVE_POINTERS:
  913. if (msglen != 1)
  914. goto unrecognised;
  915. /*
  916. * Save current data pointer to SAVED data pointer
  917. * SCSI II standard says that we must not acknowledge
  918. * this until we have really saved pointers.
  919. * NOTE: we DO NOT save the command nor status pointers
  920. * as required by the SCSI II standard. These always
  921. * point to the start of their respective areas.
  922. */
  923. info->SCpnt->SCp = info->scsi.SCp;
  924. info->SCpnt->SCp.sent_command = 0;
  925. fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
  926. "save data pointers: [%p, %X]",
  927. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  928. break;
  929. case RESTORE_POINTERS:
  930. if (msglen != 1)
  931. goto unrecognised;
  932. /*
  933. * Restore current data pointer from SAVED data pointer
  934. */
  935. info->scsi.SCp = info->SCpnt->SCp;
  936. fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
  937. "restore data pointers: [%p, 0x%x]",
  938. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  939. break;
  940. case DISCONNECT:
  941. if (msglen != 1)
  942. goto unrecognised;
  943. info->scsi.phase = PHASE_MSGIN_DISCONNECT;
  944. break;
  945. case MESSAGE_REJECT:
  946. if (msglen != 1)
  947. goto unrecognised;
  948. switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
  949. case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
  950. fas216_handlesync(info, message);
  951. break;
  952. default:
  953. fas216_log(info, 0, "reject, last message 0x%04x",
  954. fas216_get_last_msg(info, info->scsi.msgin_fifo));
  955. }
  956. break;
  957. case NOP:
  958. break;
  959. case EXTENDED_MESSAGE:
  960. if (msglen < 3)
  961. goto unrecognised;
  962. switch (message[2]) {
  963. case EXTENDED_SDTR: /* Sync transfer negotiation request/reply */
  964. fas216_handlesync(info, message);
  965. break;
  966. default:
  967. goto unrecognised;
  968. }
  969. break;
  970. default:
  971. goto unrecognised;
  972. }
  973. return;
  974. unrecognised:
  975. fas216_log(info, 0, "unrecognised message, rejecting");
  976. printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
  977. for (i = 0; i < msglen; i++)
  978. printk("%s%02X", i & 31 ? " " : "\n ", message[i]);
  979. printk("\n");
  980. /*
  981. * Something strange seems to be happening here -
  982. * I can't use SETATN since the chip gives me an
  983. * invalid command interrupt when I do. Weird.
  984. */
  985. fas216_cmd(info, CMD_NOP);
  986. fas216_dumpstate(info);
  987. fas216_cmd(info, CMD_SETATN);
  988. msgqueue_flush(&info->scsi.msgs);
  989. msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
  990. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  991. fas216_dumpstate(info);
  992. }
  993. static int fas216_wait_cmd(FAS216_Info *info, int cmd)
  994. {
  995. int tout;
  996. int stat;
  997. fas216_cmd(info, cmd);
  998. for (tout = 1000; tout; tout -= 1) {
  999. stat = fas216_readb(info, REG_STAT);
  1000. if (stat & (STAT_INT|STAT_PARITYERROR))
  1001. break;
  1002. udelay(1);
  1003. }
  1004. return stat;
  1005. }
  1006. static int fas216_get_msg_byte(FAS216_Info *info)
  1007. {
  1008. unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
  1009. if ((stat & STAT_INT) == 0)
  1010. goto timedout;
  1011. if ((stat & STAT_BUSMASK) != STAT_MESGIN)
  1012. goto unexpected_phase_change;
  1013. fas216_readb(info, REG_INST);
  1014. stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
  1015. if ((stat & STAT_INT) == 0)
  1016. goto timedout;
  1017. if (stat & STAT_PARITYERROR)
  1018. goto parity_error;
  1019. if ((stat & STAT_BUSMASK) != STAT_MESGIN)
  1020. goto unexpected_phase_change;
  1021. fas216_readb(info, REG_INST);
  1022. return fas216_readb(info, REG_FF);
  1023. timedout:
  1024. fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
  1025. return -1;
  1026. unexpected_phase_change:
  1027. fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
  1028. return -2;
  1029. parity_error:
  1030. fas216_log(info, LOG_ERROR, "parity error during message in phase");
  1031. return -3;
  1032. }
  1033. /**
  1034. * fas216_message - handle a function done interrupt from FAS216 chip
  1035. * @info: interface which caused function done interrupt
  1036. *
  1037. * Handle a function done interrupt from FAS216 chip
  1038. */
  1039. static void fas216_message(FAS216_Info *info)
  1040. {
  1041. unsigned char *message = info->scsi.message;
  1042. unsigned int msglen = 1;
  1043. int msgbyte = 0;
  1044. fas216_checkmagic(info);
  1045. message[0] = fas216_readb(info, REG_FF);
  1046. if (message[0] == EXTENDED_MESSAGE) {
  1047. msgbyte = fas216_get_msg_byte(info);
  1048. if (msgbyte >= 0) {
  1049. message[1] = msgbyte;
  1050. for (msglen = 2; msglen < message[1] + 2; msglen++) {
  1051. msgbyte = fas216_get_msg_byte(info);
  1052. if (msgbyte >= 0)
  1053. message[msglen] = msgbyte;
  1054. else
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. if (msgbyte == -3)
  1060. goto parity_error;
  1061. #ifdef DEBUG_MESSAGES
  1062. {
  1063. int i;
  1064. printk("scsi%d.%c: message in: ",
  1065. info->host->host_no, fas216_target(info));
  1066. for (i = 0; i < msglen; i++)
  1067. printk("%02X ", message[i]);
  1068. printk("\n");
  1069. }
  1070. #endif
  1071. fas216_parse_message(info, message, msglen);
  1072. fas216_cmd(info, CMD_MSGACCEPTED);
  1073. return;
  1074. parity_error:
  1075. fas216_cmd(info, CMD_SETATN);
  1076. msgqueue_flush(&info->scsi.msgs);
  1077. msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
  1078. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1079. fas216_cmd(info, CMD_MSGACCEPTED);
  1080. return;
  1081. }
  1082. /**
  1083. * fas216_send_command - send command after all message bytes have been sent
  1084. * @info: interface which caused bus service
  1085. *
  1086. * Send a command to a target after all message bytes have been sent
  1087. */
  1088. static void fas216_send_command(FAS216_Info *info)
  1089. {
  1090. int i;
  1091. fas216_checkmagic(info);
  1092. fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
  1093. fas216_cmd(info, CMD_FLUSHFIFO);
  1094. /* load command */
  1095. for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
  1096. fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
  1097. fas216_cmd(info, CMD_TRANSFERINFO);
  1098. info->scsi.phase = PHASE_COMMAND;
  1099. }
  1100. /**
  1101. * fas216_send_messageout - handle bus service to send a message
  1102. * @info: interface which caused bus service
  1103. *
  1104. * Handle bus service to send a message.
  1105. * Note: We do not allow the device to change the data direction!
  1106. */
  1107. static void fas216_send_messageout(FAS216_Info *info, int start)
  1108. {
  1109. unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
  1110. fas216_checkmagic(info);
  1111. fas216_cmd(info, CMD_FLUSHFIFO);
  1112. if (tot_msglen) {
  1113. struct message *msg;
  1114. int msgnr = 0;
  1115. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1116. int i;
  1117. for (i = start; i < msg->length; i++)
  1118. fas216_writeb(info, REG_FF, msg->msg[i]);
  1119. msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
  1120. start = 0;
  1121. }
  1122. } else
  1123. fas216_writeb(info, REG_FF, NOP);
  1124. fas216_cmd(info, CMD_TRANSFERINFO);
  1125. info->scsi.phase = PHASE_MSGOUT;
  1126. }
  1127. /**
  1128. * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
  1129. * @info: interface which caused bus service interrupt
  1130. * @stat: Status register contents
  1131. * @is: SCSI Status register contents
  1132. *
  1133. * Handle a bus service interrupt from FAS216 chip
  1134. */
  1135. static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
  1136. {
  1137. fas216_checkmagic(info);
  1138. fas216_log(info, LOG_BUSSERVICE,
  1139. "bus service: stat=%02x is=%02x phase=%02x",
  1140. stat, is, info->scsi.phase);
  1141. switch (info->scsi.phase) {
  1142. case PHASE_SELECTION:
  1143. if ((is & IS_BITS) != IS_MSGBYTESENT)
  1144. goto bad_is;
  1145. break;
  1146. case PHASE_SELSTEPS:
  1147. switch (is & IS_BITS) {
  1148. case IS_SELARB:
  1149. case IS_MSGBYTESENT:
  1150. goto bad_is;
  1151. case IS_NOTCOMMAND:
  1152. case IS_EARLYPHASE:
  1153. if ((stat & STAT_BUSMASK) == STAT_MESGIN)
  1154. break;
  1155. goto bad_is;
  1156. case IS_COMPLETE:
  1157. break;
  1158. }
  1159. default:
  1160. break;
  1161. }
  1162. fas216_cmd(info, CMD_NOP);
  1163. #define STATE(st,ph) ((ph) << 3 | (st))
  1164. /* This table describes the legal SCSI state transitions,
  1165. * as described by the SCSI II spec.
  1166. */
  1167. switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
  1168. case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In */
  1169. case STATE(STAT_DATAIN, PHASE_MSGOUT): /* Message Out -> Data In */
  1170. case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command -> Data In */
  1171. case STATE(STAT_DATAIN, PHASE_MSGIN): /* Message In -> Data In */
  1172. info->scsi.phase = PHASE_DATAIN;
  1173. fas216_transfer(info);
  1174. return;
  1175. case STATE(STAT_DATAIN, PHASE_DATAIN): /* Data In -> Data In */
  1176. case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out -> Data Out */
  1177. fas216_cleanuptransfer(info);
  1178. fas216_transfer(info);
  1179. return;
  1180. case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out */
  1181. case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out -> Data Out */
  1182. case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command -> Data Out */
  1183. case STATE(STAT_DATAOUT, PHASE_MSGIN): /* Message In -> Data Out */
  1184. fas216_cmd(info, CMD_FLUSHFIFO);
  1185. info->scsi.phase = PHASE_DATAOUT;
  1186. fas216_transfer(info);
  1187. return;
  1188. case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out -> Status */
  1189. case STATE(STAT_STATUS, PHASE_DATAIN): /* Data In -> Status */
  1190. fas216_stoptransfer(info);
  1191. case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status */
  1192. case STATE(STAT_STATUS, PHASE_MSGOUT): /* Message Out -> Status */
  1193. case STATE(STAT_STATUS, PHASE_COMMAND): /* Command -> Status */
  1194. case STATE(STAT_STATUS, PHASE_MSGIN): /* Message In -> Status */
  1195. fas216_cmd(info, CMD_INITCMDCOMPLETE);
  1196. info->scsi.phase = PHASE_STATUS;
  1197. return;
  1198. case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out -> Message In */
  1199. case STATE(STAT_MESGIN, PHASE_DATAIN): /* Data In -> Message In */
  1200. fas216_stoptransfer(info);
  1201. case STATE(STAT_MESGIN, PHASE_COMMAND): /* Command -> Message In */
  1202. case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In */
  1203. case STATE(STAT_MESGIN, PHASE_MSGOUT): /* Message Out -> Message In */
  1204. info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1205. fas216_cmd(info, CMD_FLUSHFIFO);
  1206. fas216_cmd(info, CMD_TRANSFERINFO);
  1207. info->scsi.phase = PHASE_MSGIN;
  1208. return;
  1209. case STATE(STAT_MESGIN, PHASE_MSGIN):
  1210. info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1211. fas216_cmd(info, CMD_TRANSFERINFO);
  1212. return;
  1213. case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out -> Command */
  1214. case STATE(STAT_COMMAND, PHASE_MSGIN): /* Message In -> Command */
  1215. fas216_send_command(info);
  1216. info->scsi.phase = PHASE_COMMAND;
  1217. return;
  1218. /*
  1219. * Selection -> Message Out
  1220. */
  1221. case STATE(STAT_MESGOUT, PHASE_SELECTION):
  1222. fas216_send_messageout(info, 1);
  1223. return;
  1224. /*
  1225. * Message Out -> Message Out
  1226. */
  1227. case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
  1228. case STATE(STAT_MESGOUT, PHASE_MSGOUT):
  1229. /*
  1230. * If we get another message out phase, this usually
  1231. * means some parity error occurred. Resend complete
  1232. * set of messages. If we have more than one byte to
  1233. * send, we need to assert ATN again.
  1234. */
  1235. if (info->device[info->SCpnt->device->id].parity_check) {
  1236. /*
  1237. * We were testing... good, the device
  1238. * supports parity checking.
  1239. */
  1240. info->device[info->SCpnt->device->id].parity_check = 0;
  1241. info->device[info->SCpnt->device->id].parity_enabled = 1;
  1242. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  1243. }
  1244. if (msgqueue_msglength(&info->scsi.msgs) > 1)
  1245. fas216_cmd(info, CMD_SETATN);
  1246. /*FALLTHROUGH*/
  1247. /*
  1248. * Any -> Message Out
  1249. */
  1250. case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
  1251. fas216_send_messageout(info, 0);
  1252. return;
  1253. /* Error recovery rules.
  1254. * These either attempt to abort or retry the operation.
  1255. * TODO: we need more of these
  1256. */
  1257. case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command -> Command */
  1258. /* error - we've sent out all the command bytes
  1259. * we have.
  1260. * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
  1261. * to include the command bytes sent for this to work
  1262. * correctly.
  1263. */
  1264. printk(KERN_ERR "scsi%d.%c: "
  1265. "target trying to receive more command bytes\n",
  1266. info->host->host_no, fas216_target(info));
  1267. fas216_cmd(info, CMD_SETATN);
  1268. fas216_set_stc(info, 15);
  1269. fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
  1270. msgqueue_flush(&info->scsi.msgs);
  1271. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  1272. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1273. return;
  1274. }
  1275. if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
  1276. printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
  1277. info->host->host_no, fas216_target(info),
  1278. fas216_bus_phase(stat));
  1279. msgqueue_flush(&info->scsi.msgs);
  1280. fas216_cmd(info, CMD_SETATN);
  1281. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  1282. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1283. info->scsi.aborting = 1;
  1284. fas216_cmd(info, CMD_TRANSFERINFO);
  1285. return;
  1286. }
  1287. printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
  1288. info->host->host_no, fas216_target(info),
  1289. fas216_bus_phase(stat),
  1290. fas216_drv_phase(info));
  1291. print_debug_list();
  1292. return;
  1293. bad_is:
  1294. fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
  1295. fas216_dumpstate(info);
  1296. print_debug_list();
  1297. fas216_done(info, DID_ERROR);
  1298. }
  1299. /**
  1300. * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
  1301. * @info: interface which caused function done interrupt
  1302. * @stat: Status register contents
  1303. * @is: SCSI Status register contents
  1304. *
  1305. * Handle a function done interrupt from FAS216 chip
  1306. */
  1307. static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
  1308. {
  1309. unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1310. fas216_checkmagic(info);
  1311. fas216_log(info, LOG_FUNCTIONDONE,
  1312. "function done: stat=%02x is=%02x phase=%02x",
  1313. stat, is, info->scsi.phase);
  1314. switch (info->scsi.phase) {
  1315. case PHASE_STATUS: /* status phase - read status and msg */
  1316. if (fifo_len != 2) {
  1317. fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
  1318. }
  1319. /*
  1320. * Read status then message byte.
  1321. */
  1322. info->scsi.SCp.Status = fas216_readb(info, REG_FF);
  1323. info->scsi.SCp.Message = fas216_readb(info, REG_FF);
  1324. info->scsi.phase = PHASE_DONE;
  1325. fas216_cmd(info, CMD_MSGACCEPTED);
  1326. break;
  1327. case PHASE_IDLE:
  1328. case PHASE_SELECTION:
  1329. case PHASE_SELSTEPS:
  1330. break;
  1331. case PHASE_MSGIN: /* message in phase */
  1332. if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
  1333. info->scsi.msgin_fifo = fifo_len;
  1334. fas216_message(info);
  1335. break;
  1336. }
  1337. default:
  1338. fas216_log(info, 0, "internal phase %s for function done?"
  1339. " What do I do with this?",
  1340. fas216_target(info), fas216_drv_phase(info));
  1341. }
  1342. }
  1343. static void fas216_bus_reset(FAS216_Info *info)
  1344. {
  1345. neg_t sync_state;
  1346. int i;
  1347. msgqueue_flush(&info->scsi.msgs);
  1348. sync_state = neg_invalid;
  1349. #ifdef SCSI2_SYNC
  1350. if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
  1351. sync_state = neg_wait;
  1352. #endif
  1353. info->scsi.phase = PHASE_IDLE;
  1354. info->SCpnt = NULL; /* bug! */
  1355. memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
  1356. for (i = 0; i < 8; i++) {
  1357. info->device[i].disconnect_ok = info->ifcfg.disconnect_ok;
  1358. info->device[i].sync_state = sync_state;
  1359. info->device[i].period = info->ifcfg.asyncperiod / 4;
  1360. info->device[i].stp = info->scsi.async_stp;
  1361. info->device[i].sof = 0;
  1362. info->device[i].wide_xfer = 0;
  1363. }
  1364. info->rst_bus_status = 1;
  1365. wake_up(&info->eh_wait);
  1366. }
  1367. /**
  1368. * fas216_intr - handle interrupts to progress a command
  1369. * @info: interface to service
  1370. *
  1371. * Handle interrupts from the interface to progress a command
  1372. */
  1373. irqreturn_t fas216_intr(FAS216_Info *info)
  1374. {
  1375. unsigned char inst, is, stat;
  1376. int handled = IRQ_NONE;
  1377. fas216_checkmagic(info);
  1378. stat = fas216_readb(info, REG_STAT);
  1379. is = fas216_readb(info, REG_IS);
  1380. inst = fas216_readb(info, REG_INST);
  1381. add_debug_list(stat, is, inst, info->scsi.phase);
  1382. if (stat & STAT_INT) {
  1383. if (inst & INST_BUSRESET) {
  1384. fas216_log(info, 0, "bus reset detected");
  1385. fas216_bus_reset(info);
  1386. scsi_report_bus_reset(info->host, 0);
  1387. } else if (inst & INST_ILLEGALCMD) {
  1388. fas216_log(info, LOG_ERROR, "illegal command given\n");
  1389. fas216_dumpstate(info);
  1390. print_debug_list();
  1391. } else if (inst & INST_DISCONNECT)
  1392. fas216_disconnect_intr(info);
  1393. else if (inst & INST_RESELECTED) /* reselected */
  1394. fas216_reselected_intr(info);
  1395. else if (inst & INST_BUSSERVICE) /* bus service request */
  1396. fas216_busservice_intr(info, stat, is);
  1397. else if (inst & INST_FUNCDONE) /* function done */
  1398. fas216_funcdone_intr(info, stat, is);
  1399. else
  1400. fas216_log(info, 0, "unknown interrupt received:"
  1401. " phase %s inst %02X is %02X stat %02X",
  1402. fas216_drv_phase(info), inst, is, stat);
  1403. handled = IRQ_HANDLED;
  1404. }
  1405. return handled;
  1406. }
  1407. static void __fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1408. {
  1409. int tot_msglen;
  1410. /* following what the ESP driver says */
  1411. fas216_set_stc(info, 0);
  1412. fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
  1413. /* flush FIFO */
  1414. fas216_cmd(info, CMD_FLUSHFIFO);
  1415. /* load bus-id and timeout */
  1416. fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
  1417. fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
  1418. /* synchronous transfers */
  1419. fas216_set_sync(info, SCpnt->device->id);
  1420. tot_msglen = msgqueue_msglength(&info->scsi.msgs);
  1421. #ifdef DEBUG_MESSAGES
  1422. {
  1423. struct message *msg;
  1424. int msgnr = 0, i;
  1425. printk("scsi%d.%c: message out: ",
  1426. info->host->host_no, '0' + SCpnt->device->id);
  1427. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1428. printk("{ ");
  1429. for (i = 0; i < msg->length; i++)
  1430. printk("%02x ", msg->msg[i]);
  1431. printk("} ");
  1432. }
  1433. printk("\n");
  1434. }
  1435. #endif
  1436. if (tot_msglen == 1 || tot_msglen == 3) {
  1437. /*
  1438. * We have an easy message length to send...
  1439. */
  1440. struct message *msg;
  1441. int msgnr = 0, i;
  1442. info->scsi.phase = PHASE_SELSTEPS;
  1443. /* load message bytes */
  1444. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1445. for (i = 0; i < msg->length; i++)
  1446. fas216_writeb(info, REG_FF, msg->msg[i]);
  1447. msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
  1448. }
  1449. /* load command */
  1450. for (i = 0; i < SCpnt->cmd_len; i++)
  1451. fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
  1452. if (tot_msglen == 1)
  1453. fas216_cmd(info, CMD_SELECTATN);
  1454. else
  1455. fas216_cmd(info, CMD_SELECTATN3);
  1456. } else {
  1457. /*
  1458. * We have an unusual number of message bytes to send.
  1459. * Load first byte into fifo, and issue SELECT with ATN and
  1460. * stop steps.
  1461. */
  1462. struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
  1463. fas216_writeb(info, REG_FF, msg->msg[0]);
  1464. msg->fifo = 1;
  1465. fas216_cmd(info, CMD_SELECTATNSTOP);
  1466. }
  1467. }
  1468. /*
  1469. * Decide whether we need to perform a parity test on this device.
  1470. * Can also be used to force parity error conditions during initial
  1471. * information transfer phase (message out) for test purposes.
  1472. */
  1473. static int parity_test(FAS216_Info *info, int target)
  1474. {
  1475. #if 0
  1476. if (target == 3) {
  1477. info->device[target].parity_check = 0;
  1478. return 1;
  1479. }
  1480. #endif
  1481. return info->device[target].parity_check;
  1482. }
  1483. static void fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1484. {
  1485. int disconnect_ok;
  1486. /*
  1487. * claim host busy
  1488. */
  1489. info->scsi.phase = PHASE_SELECTION;
  1490. info->scsi.SCp = SCpnt->SCp;
  1491. info->SCpnt = SCpnt;
  1492. info->dma.transfer_type = fasdma_none;
  1493. if (parity_test(info, SCpnt->device->id))
  1494. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
  1495. else
  1496. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  1497. /*
  1498. * Don't allow request sense commands to disconnect.
  1499. */
  1500. disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
  1501. info->device[SCpnt->device->id].disconnect_ok;
  1502. /*
  1503. * build outgoing message bytes
  1504. */
  1505. msgqueue_flush(&info->scsi.msgs);
  1506. msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
  1507. /*
  1508. * add tag message if required
  1509. */
  1510. if (SCpnt->tag)
  1511. msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
  1512. do {
  1513. #ifdef SCSI2_SYNC
  1514. if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
  1515. info->device[SCpnt->device->id].sync_state == neg_complete) &&
  1516. (SCpnt->cmnd[0] == REQUEST_SENSE ||
  1517. SCpnt->cmnd[0] == INQUIRY)) {
  1518. info->device[SCpnt->device->id].sync_state = neg_inprogress;
  1519. msgqueue_addmsg(&info->scsi.msgs, 5,
  1520. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  1521. 1000 / info->ifcfg.clockrate,
  1522. info->ifcfg.sync_max_depth);
  1523. break;
  1524. }
  1525. #endif
  1526. } while (0);
  1527. __fas216_start_command(info, SCpnt);
  1528. }
  1529. static void fas216_allocate_tag(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1530. {
  1531. #ifdef SCSI2_TAG
  1532. /*
  1533. * tagged queuing - allocate a new tag to this command
  1534. */
  1535. if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
  1536. SCpnt->cmnd[0] != INQUIRY) {
  1537. SCpnt->device->current_tag += 1;
  1538. if (SCpnt->device->current_tag == 0)
  1539. SCpnt->device->current_tag = 1;
  1540. SCpnt->tag = SCpnt->device->current_tag;
  1541. } else
  1542. #endif
  1543. set_bit(SCpnt->device->id * 8 +
  1544. (u8)(SCpnt->device->lun & 0x7), info->busyluns);
  1545. info->stats.removes += 1;
  1546. switch (SCpnt->cmnd[0]) {
  1547. case WRITE_6:
  1548. case WRITE_10:
  1549. case WRITE_12:
  1550. info->stats.writes += 1;
  1551. break;
  1552. case READ_6:
  1553. case READ_10:
  1554. case READ_12:
  1555. info->stats.reads += 1;
  1556. break;
  1557. default:
  1558. info->stats.miscs += 1;
  1559. break;
  1560. }
  1561. }
  1562. static void fas216_do_bus_device_reset(FAS216_Info *info,
  1563. struct scsi_cmnd *SCpnt)
  1564. {
  1565. struct message *msg;
  1566. /*
  1567. * claim host busy
  1568. */
  1569. info->scsi.phase = PHASE_SELECTION;
  1570. info->scsi.SCp = SCpnt->SCp;
  1571. info->SCpnt = SCpnt;
  1572. info->dma.transfer_type = fasdma_none;
  1573. fas216_log(info, LOG_ERROR, "sending bus device reset");
  1574. msgqueue_flush(&info->scsi.msgs);
  1575. msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
  1576. /* following what the ESP driver says */
  1577. fas216_set_stc(info, 0);
  1578. fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
  1579. /* flush FIFO */
  1580. fas216_cmd(info, CMD_FLUSHFIFO);
  1581. /* load bus-id and timeout */
  1582. fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
  1583. fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
  1584. /* synchronous transfers */
  1585. fas216_set_sync(info, SCpnt->device->id);
  1586. msg = msgqueue_getmsg(&info->scsi.msgs, 0);
  1587. fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
  1588. msg->fifo = 1;
  1589. fas216_cmd(info, CMD_SELECTATNSTOP);
  1590. }
  1591. /**
  1592. * fas216_kick - kick a command to the interface
  1593. * @info: our host interface to kick
  1594. *
  1595. * Kick a command to the interface, interface should be idle.
  1596. * Notes: Interrupts are always disabled!
  1597. */
  1598. static void fas216_kick(FAS216_Info *info)
  1599. {
  1600. struct scsi_cmnd *SCpnt = NULL;
  1601. #define TYPE_OTHER 0
  1602. #define TYPE_RESET 1
  1603. #define TYPE_QUEUE 2
  1604. int where_from = TYPE_OTHER;
  1605. fas216_checkmagic(info);
  1606. /*
  1607. * Obtain the next command to process.
  1608. */
  1609. do {
  1610. if (info->rstSCpnt) {
  1611. SCpnt = info->rstSCpnt;
  1612. /* don't remove it */
  1613. where_from = TYPE_RESET;
  1614. break;
  1615. }
  1616. if (info->reqSCpnt) {
  1617. SCpnt = info->reqSCpnt;
  1618. info->reqSCpnt = NULL;
  1619. break;
  1620. }
  1621. if (info->origSCpnt) {
  1622. SCpnt = info->origSCpnt;
  1623. info->origSCpnt = NULL;
  1624. break;
  1625. }
  1626. /* retrieve next command */
  1627. if (!SCpnt) {
  1628. SCpnt = queue_remove_exclude(&info->queues.issue,
  1629. info->busyluns);
  1630. where_from = TYPE_QUEUE;
  1631. break;
  1632. }
  1633. } while (0);
  1634. if (!SCpnt) {
  1635. /*
  1636. * no command pending, so enable reselection.
  1637. */
  1638. fas216_cmd(info, CMD_ENABLESEL);
  1639. return;
  1640. }
  1641. /*
  1642. * We're going to start a command, so disable reselection
  1643. */
  1644. fas216_cmd(info, CMD_DISABLESEL);
  1645. if (info->scsi.disconnectable && info->SCpnt) {
  1646. fas216_log(info, LOG_CONNECT,
  1647. "moved command for %d to disconnected queue",
  1648. info->SCpnt->device->id);
  1649. queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
  1650. info->scsi.disconnectable = 0;
  1651. info->SCpnt = NULL;
  1652. }
  1653. fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
  1654. "starting");
  1655. switch (where_from) {
  1656. case TYPE_QUEUE:
  1657. fas216_allocate_tag(info, SCpnt);
  1658. case TYPE_OTHER:
  1659. fas216_start_command(info, SCpnt);
  1660. break;
  1661. case TYPE_RESET:
  1662. fas216_do_bus_device_reset(info, SCpnt);
  1663. break;
  1664. }
  1665. fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
  1666. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  1667. /*
  1668. * should now get either DISCONNECT or
  1669. * (FUNCTION DONE with BUS SERVICE) interrupt
  1670. */
  1671. }
  1672. /*
  1673. * Clean up from issuing a BUS DEVICE RESET message to a device.
  1674. */
  1675. static void fas216_devicereset_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
  1676. unsigned int result)
  1677. {
  1678. fas216_log(info, LOG_ERROR, "fas216 device reset complete");
  1679. info->rstSCpnt = NULL;
  1680. info->rst_dev_status = 1;
  1681. wake_up(&info->eh_wait);
  1682. }
  1683. /**
  1684. * fas216_rq_sns_done - Finish processing automatic request sense command
  1685. * @info: interface that completed
  1686. * @SCpnt: command that completed
  1687. * @result: driver byte of result
  1688. *
  1689. * Finish processing automatic request sense command
  1690. */
  1691. static void fas216_rq_sns_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
  1692. unsigned int result)
  1693. {
  1694. fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
  1695. "request sense complete, result=0x%04x%02x%02x",
  1696. result, SCpnt->SCp.Message, SCpnt->SCp.Status);
  1697. if (result != DID_OK || SCpnt->SCp.Status != GOOD)
  1698. /*
  1699. * Something went wrong. Make sure that we don't
  1700. * have valid data in the sense buffer that could
  1701. * confuse the higher levels.
  1702. */
  1703. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1704. //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
  1705. //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
  1706. /*
  1707. * Note that we don't set SCpnt->result, since that should
  1708. * reflect the status of the command that we were asked by
  1709. * the upper layers to process. This would have been set
  1710. * correctly by fas216_std_done.
  1711. */
  1712. scsi_eh_restore_cmnd(SCpnt, &info->ses);
  1713. SCpnt->scsi_done(SCpnt);
  1714. }
  1715. /**
  1716. * fas216_std_done - finish processing of standard command
  1717. * @info: interface that completed
  1718. * @SCpnt: command that completed
  1719. * @result: driver byte of result
  1720. *
  1721. * Finish processing of standard command
  1722. */
  1723. static void
  1724. fas216_std_done(FAS216_Info *info, struct scsi_cmnd *SCpnt, unsigned int result)
  1725. {
  1726. info->stats.fins += 1;
  1727. SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
  1728. info->scsi.SCp.Status;
  1729. fas216_log_command(info, LOG_CONNECT, SCpnt,
  1730. "command complete, result=0x%08x", SCpnt->result);
  1731. /*
  1732. * If the driver detected an error, we're all done.
  1733. */
  1734. if (host_byte(SCpnt->result) != DID_OK ||
  1735. msg_byte(SCpnt->result) != COMMAND_COMPLETE)
  1736. goto done;
  1737. /*
  1738. * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
  1739. * status, request the sense information.
  1740. */
  1741. if (status_byte(SCpnt->result) == CHECK_CONDITION ||
  1742. status_byte(SCpnt->result) == COMMAND_TERMINATED)
  1743. goto request_sense;
  1744. /*
  1745. * If the command did not complete with GOOD status,
  1746. * we are all done here.
  1747. */
  1748. if (status_byte(SCpnt->result) != GOOD)
  1749. goto done;
  1750. /*
  1751. * We have successfully completed a command. Make sure that
  1752. * we do not have any buffers left to transfer. The world
  1753. * is not perfect, and we seem to occasionally hit this.
  1754. * It can be indicative of a buggy driver, target or the upper
  1755. * levels of the SCSI code.
  1756. */
  1757. if (info->scsi.SCp.ptr) {
  1758. switch (SCpnt->cmnd[0]) {
  1759. case INQUIRY:
  1760. case START_STOP:
  1761. case MODE_SENSE:
  1762. break;
  1763. default:
  1764. scmd_printk(KERN_ERR, SCpnt,
  1765. "incomplete data transfer detected: res=%08X ptr=%p len=%X\n",
  1766. SCpnt->result, info->scsi.SCp.ptr,
  1767. info->scsi.SCp.this_residual);
  1768. scsi_print_command(SCpnt);
  1769. set_host_byte(SCpnt, DID_ERROR);
  1770. goto request_sense;
  1771. }
  1772. }
  1773. done:
  1774. if (SCpnt->scsi_done) {
  1775. SCpnt->scsi_done(SCpnt);
  1776. return;
  1777. }
  1778. panic("scsi%d.H: null scsi_done function in fas216_done",
  1779. info->host->host_no);
  1780. request_sense:
  1781. if (SCpnt->cmnd[0] == REQUEST_SENSE)
  1782. goto done;
  1783. scsi_eh_prep_cmnd(SCpnt, &info->ses, NULL, 0, ~0);
  1784. fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
  1785. "requesting sense");
  1786. init_SCp(SCpnt);
  1787. SCpnt->SCp.Message = 0;
  1788. SCpnt->SCp.Status = 0;
  1789. SCpnt->tag = 0;
  1790. SCpnt->host_scribble = (void *)fas216_rq_sns_done;
  1791. /*
  1792. * Place this command into the high priority "request
  1793. * sense" slot. This will be the very next command
  1794. * executed, unless a target connects to us.
  1795. */
  1796. if (info->reqSCpnt)
  1797. printk(KERN_WARNING "scsi%d.%c: losing request command\n",
  1798. info->host->host_no, '0' + SCpnt->device->id);
  1799. info->reqSCpnt = SCpnt;
  1800. }
  1801. /**
  1802. * fas216_done - complete processing for current command
  1803. * @info: interface that completed
  1804. * @result: driver byte of result
  1805. *
  1806. * Complete processing for current command
  1807. */
  1808. static void fas216_done(FAS216_Info *info, unsigned int result)
  1809. {
  1810. void (*fn)(FAS216_Info *, struct scsi_cmnd *, unsigned int);
  1811. struct scsi_cmnd *SCpnt;
  1812. unsigned long flags;
  1813. fas216_checkmagic(info);
  1814. if (!info->SCpnt)
  1815. goto no_command;
  1816. SCpnt = info->SCpnt;
  1817. info->SCpnt = NULL;
  1818. info->scsi.phase = PHASE_IDLE;
  1819. if (info->scsi.aborting) {
  1820. fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
  1821. result = DID_ABORT;
  1822. info->scsi.aborting = 0;
  1823. }
  1824. /*
  1825. * Sanity check the completion - if we have zero bytes left
  1826. * to transfer, we should not have a valid pointer.
  1827. */
  1828. if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
  1829. scmd_printk(KERN_INFO, SCpnt,
  1830. "zero bytes left to transfer, but buffer pointer still valid: ptr=%p len=%08x\n",
  1831. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  1832. info->scsi.SCp.ptr = NULL;
  1833. scsi_print_command(SCpnt);
  1834. }
  1835. /*
  1836. * Clear down this command as completed. If we need to request
  1837. * the sense information, fas216_kick will re-assert the busy
  1838. * status.
  1839. */
  1840. info->device[SCpnt->device->id].parity_check = 0;
  1841. clear_bit(SCpnt->device->id * 8 +
  1842. (u8)(SCpnt->device->lun & 0x7), info->busyluns);
  1843. fn = (void (*)(FAS216_Info *, struct scsi_cmnd *, unsigned int))SCpnt->host_scribble;
  1844. fn(info, SCpnt, result);
  1845. if (info->scsi.irq) {
  1846. spin_lock_irqsave(&info->host_lock, flags);
  1847. if (info->scsi.phase == PHASE_IDLE)
  1848. fas216_kick(info);
  1849. spin_unlock_irqrestore(&info->host_lock, flags);
  1850. }
  1851. return;
  1852. no_command:
  1853. panic("scsi%d.H: null command in fas216_done",
  1854. info->host->host_no);
  1855. }
  1856. /**
  1857. * fas216_queue_command - queue a command for adapter to process.
  1858. * @SCpnt: Command to queue
  1859. * @done: done function to call once command is complete
  1860. *
  1861. * Queue a command for adapter to process.
  1862. * Returns: 0 on success, else error.
  1863. * Notes: io_request_lock is held, interrupts are disabled.
  1864. */
  1865. static int fas216_queue_command_lck(struct scsi_cmnd *SCpnt,
  1866. void (*done)(struct scsi_cmnd *))
  1867. {
  1868. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  1869. int result;
  1870. fas216_checkmagic(info);
  1871. fas216_log_command(info, LOG_CONNECT, SCpnt,
  1872. "received command (%p)", SCpnt);
  1873. SCpnt->scsi_done = done;
  1874. SCpnt->host_scribble = (void *)fas216_std_done;
  1875. SCpnt->result = 0;
  1876. init_SCp(SCpnt);
  1877. info->stats.queues += 1;
  1878. SCpnt->tag = 0;
  1879. spin_lock(&info->host_lock);
  1880. /*
  1881. * Add command into execute queue and let it complete under
  1882. * whatever scheme we're using.
  1883. */
  1884. result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
  1885. /*
  1886. * If we successfully added the command,
  1887. * kick the interface to get it moving.
  1888. */
  1889. if (result == 0 && info->scsi.phase == PHASE_IDLE)
  1890. fas216_kick(info);
  1891. spin_unlock(&info->host_lock);
  1892. fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
  1893. result ? "failure" : "success");
  1894. return result;
  1895. }
  1896. DEF_SCSI_QCMD(fas216_queue_command)
  1897. /**
  1898. * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
  1899. * @SCpnt: Command to wake
  1900. *
  1901. * Trigger restart of a waiting thread in fas216_command
  1902. */
  1903. static void fas216_internal_done(struct scsi_cmnd *SCpnt)
  1904. {
  1905. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  1906. fas216_checkmagic(info);
  1907. info->internal_done = 1;
  1908. }
  1909. /**
  1910. * fas216_noqueue_command - process a command for the adapter.
  1911. * @SCpnt: Command to queue
  1912. *
  1913. * Queue a command for adapter to process.
  1914. * Returns: scsi result code.
  1915. * Notes: io_request_lock is held, interrupts are disabled.
  1916. */
  1917. static int fas216_noqueue_command_lck(struct scsi_cmnd *SCpnt,
  1918. void (*done)(struct scsi_cmnd *))
  1919. {
  1920. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  1921. fas216_checkmagic(info);
  1922. /*
  1923. * We should only be using this if we don't have an interrupt.
  1924. * Provide some "incentive" to use the queueing code.
  1925. */
  1926. BUG_ON(info->scsi.irq);
  1927. info->internal_done = 0;
  1928. fas216_queue_command_lck(SCpnt, fas216_internal_done);
  1929. /*
  1930. * This wastes time, since we can't return until the command is
  1931. * complete. We can't sleep either since we may get re-entered!
  1932. * However, we must re-enable interrupts, or else we'll be
  1933. * waiting forever.
  1934. */
  1935. spin_unlock_irq(info->host->host_lock);
  1936. while (!info->internal_done) {
  1937. /*
  1938. * If we don't have an IRQ, then we must poll the card for
  1939. * it's interrupt, and use that to call this driver's
  1940. * interrupt routine. That way, we keep the command
  1941. * progressing. Maybe we can add some intelligence here
  1942. * and go to sleep if we know that the device is going
  1943. * to be some time (eg, disconnected).
  1944. */
  1945. if (fas216_readb(info, REG_STAT) & STAT_INT) {
  1946. spin_lock_irq(info->host->host_lock);
  1947. fas216_intr(info);
  1948. spin_unlock_irq(info->host->host_lock);
  1949. }
  1950. }
  1951. spin_lock_irq(info->host->host_lock);
  1952. done(SCpnt);
  1953. return 0;
  1954. }
  1955. DEF_SCSI_QCMD(fas216_noqueue_command)
  1956. /*
  1957. * Error handler timeout function. Indicate that we timed out,
  1958. * and wake up any error handler process so it can continue.
  1959. */
  1960. static void fas216_eh_timer(unsigned long data)
  1961. {
  1962. FAS216_Info *info = (FAS216_Info *)data;
  1963. fas216_log(info, LOG_ERROR, "error handling timed out\n");
  1964. del_timer(&info->eh_timer);
  1965. if (info->rst_bus_status == 0)
  1966. info->rst_bus_status = -1;
  1967. if (info->rst_dev_status == 0)
  1968. info->rst_dev_status = -1;
  1969. wake_up(&info->eh_wait);
  1970. }
  1971. enum res_find {
  1972. res_failed, /* not found */
  1973. res_success, /* command on issue queue */
  1974. res_hw_abort /* command on disconnected dev */
  1975. };
  1976. /**
  1977. * fas216_do_abort - decide how to abort a command
  1978. * @SCpnt: command to abort
  1979. *
  1980. * Decide how to abort a command.
  1981. * Returns: abort status
  1982. */
  1983. static enum res_find fas216_find_command(FAS216_Info *info,
  1984. struct scsi_cmnd *SCpnt)
  1985. {
  1986. enum res_find res = res_failed;
  1987. if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
  1988. /*
  1989. * The command was on the issue queue, and has not been
  1990. * issued yet. We can remove the command from the queue,
  1991. * and acknowledge the abort. Neither the device nor the
  1992. * interface know about the command.
  1993. */
  1994. printk("on issue queue ");
  1995. res = res_success;
  1996. } else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
  1997. /*
  1998. * The command was on the disconnected queue. We must
  1999. * reconnect with the device if possible, and send it
  2000. * an abort message.
  2001. */
  2002. printk("on disconnected queue ");
  2003. res = res_hw_abort;
  2004. } else if (info->SCpnt == SCpnt) {
  2005. printk("executing ");
  2006. switch (info->scsi.phase) {
  2007. /*
  2008. * If the interface is idle, and the command is 'disconnectable',
  2009. * then it is the same as on the disconnected queue.
  2010. */
  2011. case PHASE_IDLE:
  2012. if (info->scsi.disconnectable) {
  2013. info->scsi.disconnectable = 0;
  2014. info->SCpnt = NULL;
  2015. res = res_hw_abort;
  2016. }
  2017. break;
  2018. default:
  2019. break;
  2020. }
  2021. } else if (info->origSCpnt == SCpnt) {
  2022. /*
  2023. * The command will be executed next, but a command
  2024. * is currently using the interface. This is similar to
  2025. * being on the issue queue, except the busylun bit has
  2026. * been set.
  2027. */
  2028. info->origSCpnt = NULL;
  2029. clear_bit(SCpnt->device->id * 8 +
  2030. (u8)(SCpnt->device->lun & 0x7), info->busyluns);
  2031. printk("waiting for execution ");
  2032. res = res_success;
  2033. } else
  2034. printk("unknown ");
  2035. return res;
  2036. }
  2037. /**
  2038. * fas216_eh_abort - abort this command
  2039. * @SCpnt: command to abort
  2040. *
  2041. * Abort this command.
  2042. * Returns: FAILED if unable to abort
  2043. * Notes: io_request_lock is taken, and irqs are disabled
  2044. */
  2045. int fas216_eh_abort(struct scsi_cmnd *SCpnt)
  2046. {
  2047. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2048. int result = FAILED;
  2049. fas216_checkmagic(info);
  2050. info->stats.aborts += 1;
  2051. scmd_printk(KERN_WARNING, SCpnt, "abort command\n");
  2052. print_debug_list();
  2053. fas216_dumpstate(info);
  2054. switch (fas216_find_command(info, SCpnt)) {
  2055. /*
  2056. * We found the command, and cleared it out. Either
  2057. * the command is still known to be executing on the
  2058. * target, or the busylun bit is not set.
  2059. */
  2060. case res_success:
  2061. scmd_printk(KERN_WARNING, SCpnt, "abort %p success\n", SCpnt);
  2062. result = SUCCESS;
  2063. break;
  2064. /*
  2065. * We need to reconnect to the target and send it an
  2066. * ABORT or ABORT_TAG message. We can only do this
  2067. * if the bus is free.
  2068. */
  2069. case res_hw_abort:
  2070. /*
  2071. * We are unable to abort the command for some reason.
  2072. */
  2073. default:
  2074. case res_failed:
  2075. scmd_printk(KERN_WARNING, SCpnt, "abort %p failed\n", SCpnt);
  2076. break;
  2077. }
  2078. return result;
  2079. }
  2080. /**
  2081. * fas216_eh_device_reset - Reset the device associated with this command
  2082. * @SCpnt: command specifing device to reset
  2083. *
  2084. * Reset the device associated with this command.
  2085. * Returns: FAILED if unable to reset.
  2086. * Notes: We won't be re-entered, so we'll only have one device
  2087. * reset on the go at one time.
  2088. */
  2089. int fas216_eh_device_reset(struct scsi_cmnd *SCpnt)
  2090. {
  2091. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2092. unsigned long flags;
  2093. int i, res = FAILED, target = SCpnt->device->id;
  2094. fas216_log(info, LOG_ERROR, "device reset for target %d", target);
  2095. spin_lock_irqsave(&info->host_lock, flags);
  2096. do {
  2097. /*
  2098. * If we are currently connected to a device, and
  2099. * it is the device we want to reset, there is
  2100. * nothing we can do here. Chances are it is stuck,
  2101. * and we need a bus reset.
  2102. */
  2103. if (info->SCpnt && !info->scsi.disconnectable &&
  2104. info->SCpnt->device->id == SCpnt->device->id)
  2105. break;
  2106. /*
  2107. * We're going to be resetting this device. Remove
  2108. * all pending commands from the driver. By doing
  2109. * so, we guarantee that we won't touch the command
  2110. * structures except to process the reset request.
  2111. */
  2112. queue_remove_all_target(&info->queues.issue, target);
  2113. queue_remove_all_target(&info->queues.disconnected, target);
  2114. if (info->origSCpnt && info->origSCpnt->device->id == target)
  2115. info->origSCpnt = NULL;
  2116. if (info->reqSCpnt && info->reqSCpnt->device->id == target)
  2117. info->reqSCpnt = NULL;
  2118. for (i = 0; i < 8; i++)
  2119. clear_bit(target * 8 + i, info->busyluns);
  2120. /*
  2121. * Hijack this SCSI command structure to send
  2122. * a bus device reset message to this device.
  2123. */
  2124. SCpnt->host_scribble = (void *)fas216_devicereset_done;
  2125. info->rst_dev_status = 0;
  2126. info->rstSCpnt = SCpnt;
  2127. if (info->scsi.phase == PHASE_IDLE)
  2128. fas216_kick(info);
  2129. mod_timer(&info->eh_timer, jiffies + 30 * HZ);
  2130. spin_unlock_irqrestore(&info->host_lock, flags);
  2131. /*
  2132. * Wait up to 30 seconds for the reset to complete.
  2133. */
  2134. wait_event(info->eh_wait, info->rst_dev_status);
  2135. del_timer_sync(&info->eh_timer);
  2136. spin_lock_irqsave(&info->host_lock, flags);
  2137. info->rstSCpnt = NULL;
  2138. if (info->rst_dev_status == 1)
  2139. res = SUCCESS;
  2140. } while (0);
  2141. SCpnt->host_scribble = NULL;
  2142. spin_unlock_irqrestore(&info->host_lock, flags);
  2143. fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
  2144. res == SUCCESS ? "success" : "failed");
  2145. return res;
  2146. }
  2147. /**
  2148. * fas216_eh_bus_reset - Reset the bus associated with the command
  2149. * @SCpnt: command specifing bus to reset
  2150. *
  2151. * Reset the bus associated with the command.
  2152. * Returns: FAILED if unable to reset.
  2153. * Notes: Further commands are blocked.
  2154. */
  2155. int fas216_eh_bus_reset(struct scsi_cmnd *SCpnt)
  2156. {
  2157. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2158. unsigned long flags;
  2159. struct scsi_device *SDpnt;
  2160. fas216_checkmagic(info);
  2161. fas216_log(info, LOG_ERROR, "resetting bus");
  2162. info->stats.bus_resets += 1;
  2163. spin_lock_irqsave(&info->host_lock, flags);
  2164. /*
  2165. * Stop all activity on this interface.
  2166. */
  2167. fas216_aborttransfer(info);
  2168. fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
  2169. /*
  2170. * Clear any pending interrupts.
  2171. */
  2172. while (fas216_readb(info, REG_STAT) & STAT_INT)
  2173. fas216_readb(info, REG_INST);
  2174. info->rst_bus_status = 0;
  2175. /*
  2176. * For each attached hard-reset device, clear out
  2177. * all command structures. Leave the running
  2178. * command in place.
  2179. */
  2180. shost_for_each_device(SDpnt, info->host) {
  2181. int i;
  2182. if (SDpnt->soft_reset)
  2183. continue;
  2184. queue_remove_all_target(&info->queues.issue, SDpnt->id);
  2185. queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
  2186. if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
  2187. info->origSCpnt = NULL;
  2188. if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
  2189. info->reqSCpnt = NULL;
  2190. info->SCpnt = NULL;
  2191. for (i = 0; i < 8; i++)
  2192. clear_bit(SDpnt->id * 8 + i, info->busyluns);
  2193. }
  2194. info->scsi.phase = PHASE_IDLE;
  2195. /*
  2196. * Reset the SCSI bus. Device cleanup happens in
  2197. * the interrupt handler.
  2198. */
  2199. fas216_cmd(info, CMD_RESETSCSI);
  2200. mod_timer(&info->eh_timer, jiffies + HZ);
  2201. spin_unlock_irqrestore(&info->host_lock, flags);
  2202. /*
  2203. * Wait one second for the interrupt.
  2204. */
  2205. wait_event(info->eh_wait, info->rst_bus_status);
  2206. del_timer_sync(&info->eh_timer);
  2207. fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
  2208. info->rst_bus_status == 1 ? "success" : "failed");
  2209. return info->rst_bus_status == 1 ? SUCCESS : FAILED;
  2210. }
  2211. /**
  2212. * fas216_init_chip - Initialise FAS216 state after reset
  2213. * @info: state structure for interface
  2214. *
  2215. * Initialise FAS216 state after reset
  2216. */
  2217. static void fas216_init_chip(FAS216_Info *info)
  2218. {
  2219. unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
  2220. fas216_writeb(info, REG_CLKF, clock);
  2221. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  2222. fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
  2223. fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
  2224. fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
  2225. fas216_writeb(info, REG_SOF, 0);
  2226. fas216_writeb(info, REG_STP, info->scsi.async_stp);
  2227. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  2228. }
  2229. /**
  2230. * fas216_eh_host_reset - Reset the host associated with this command
  2231. * @SCpnt: command specifing host to reset
  2232. *
  2233. * Reset the host associated with this command.
  2234. * Returns: FAILED if unable to reset.
  2235. * Notes: io_request_lock is taken, and irqs are disabled
  2236. */
  2237. int fas216_eh_host_reset(struct scsi_cmnd *SCpnt)
  2238. {
  2239. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2240. spin_lock_irq(info->host->host_lock);
  2241. fas216_checkmagic(info);
  2242. fas216_log(info, LOG_ERROR, "resetting host");
  2243. /*
  2244. * Reset the SCSI chip.
  2245. */
  2246. fas216_cmd(info, CMD_RESETCHIP);
  2247. /*
  2248. * Ugly ugly ugly!
  2249. * We need to release the host_lock and enable
  2250. * IRQs if we sleep, but we must relock and disable
  2251. * IRQs after the sleep.
  2252. */
  2253. spin_unlock_irq(info->host->host_lock);
  2254. msleep(50 * 1000/100);
  2255. spin_lock_irq(info->host->host_lock);
  2256. /*
  2257. * Release the SCSI reset.
  2258. */
  2259. fas216_cmd(info, CMD_NOP);
  2260. fas216_init_chip(info);
  2261. spin_unlock_irq(info->host->host_lock);
  2262. return SUCCESS;
  2263. }
  2264. #define TYPE_UNKNOWN 0
  2265. #define TYPE_NCR53C90 1
  2266. #define TYPE_NCR53C90A 2
  2267. #define TYPE_NCR53C9x 3
  2268. #define TYPE_Am53CF94 4
  2269. #define TYPE_EmFAS216 5
  2270. #define TYPE_QLFAS216 6
  2271. static char *chip_types[] = {
  2272. "unknown",
  2273. "NS NCR53C90",
  2274. "NS NCR53C90A",
  2275. "NS NCR53C9x",
  2276. "AMD Am53CF94",
  2277. "Emulex FAS216",
  2278. "QLogic FAS216"
  2279. };
  2280. static int fas216_detect_type(FAS216_Info *info)
  2281. {
  2282. int family, rev;
  2283. /*
  2284. * Reset the chip.
  2285. */
  2286. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2287. udelay(50);
  2288. fas216_writeb(info, REG_CMD, CMD_NOP);
  2289. /*
  2290. * Check to see if control reg 2 is present.
  2291. */
  2292. fas216_writeb(info, REG_CNTL3, 0);
  2293. fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
  2294. /*
  2295. * If we are unable to read back control reg 2
  2296. * correctly, it is not present, and we have a
  2297. * NCR53C90.
  2298. */
  2299. if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
  2300. return TYPE_NCR53C90;
  2301. /*
  2302. * Now, check control register 3
  2303. */
  2304. fas216_writeb(info, REG_CNTL2, 0);
  2305. fas216_writeb(info, REG_CNTL3, 0);
  2306. fas216_writeb(info, REG_CNTL3, 5);
  2307. /*
  2308. * If we are unable to read the register back
  2309. * correctly, we have a NCR53C90A
  2310. */
  2311. if (fas216_readb(info, REG_CNTL3) != 5)
  2312. return TYPE_NCR53C90A;
  2313. /*
  2314. * Now read the ID from the chip.
  2315. */
  2316. fas216_writeb(info, REG_CNTL3, 0);
  2317. fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
  2318. fas216_writeb(info, REG_CNTL3, 0);
  2319. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2320. udelay(50);
  2321. fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
  2322. fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
  2323. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2324. udelay(50);
  2325. fas216_writeb(info, REG_CMD, CMD_NOP);
  2326. rev = fas216_readb(info, REG_ID);
  2327. family = rev >> 3;
  2328. rev &= 7;
  2329. switch (family) {
  2330. case 0x01:
  2331. if (rev == 4)
  2332. return TYPE_Am53CF94;
  2333. break;
  2334. case 0x02:
  2335. switch (rev) {
  2336. case 2:
  2337. return TYPE_EmFAS216;
  2338. case 3:
  2339. return TYPE_QLFAS216;
  2340. }
  2341. break;
  2342. default:
  2343. break;
  2344. }
  2345. printk("family %x rev %x\n", family, rev);
  2346. return TYPE_NCR53C9x;
  2347. }
  2348. /**
  2349. * fas216_reset_state - Initialise driver internal state
  2350. * @info: state to initialise
  2351. *
  2352. * Initialise driver internal state
  2353. */
  2354. static void fas216_reset_state(FAS216_Info *info)
  2355. {
  2356. int i;
  2357. fas216_checkmagic(info);
  2358. fas216_bus_reset(info);
  2359. /*
  2360. * Clear out all stale info in our state structure
  2361. */
  2362. memset(info->busyluns, 0, sizeof(info->busyluns));
  2363. info->scsi.disconnectable = 0;
  2364. info->scsi.aborting = 0;
  2365. for (i = 0; i < 8; i++) {
  2366. info->device[i].parity_enabled = 0;
  2367. info->device[i].parity_check = 1;
  2368. }
  2369. /*
  2370. * Drain all commands on disconnected queue
  2371. */
  2372. while (queue_remove(&info->queues.disconnected) != NULL);
  2373. /*
  2374. * Remove executing commands.
  2375. */
  2376. info->SCpnt = NULL;
  2377. info->reqSCpnt = NULL;
  2378. info->rstSCpnt = NULL;
  2379. info->origSCpnt = NULL;
  2380. }
  2381. /**
  2382. * fas216_init - initialise FAS/NCR/AMD SCSI structures.
  2383. * @host: a driver-specific filled-out structure
  2384. *
  2385. * Initialise FAS/NCR/AMD SCSI structures.
  2386. * Returns: 0 on success
  2387. */
  2388. int fas216_init(struct Scsi_Host *host)
  2389. {
  2390. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2391. info->magic_start = MAGIC;
  2392. info->magic_end = MAGIC;
  2393. info->host = host;
  2394. info->scsi.cfg[0] = host->this_id | CNTL1_PERE;
  2395. info->scsi.cfg[1] = CNTL2_ENF | CNTL2_S2FE;
  2396. info->scsi.cfg[2] = info->ifcfg.cntl3 |
  2397. CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
  2398. info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
  2399. info->rst_dev_status = -1;
  2400. info->rst_bus_status = -1;
  2401. init_waitqueue_head(&info->eh_wait);
  2402. init_timer(&info->eh_timer);
  2403. info->eh_timer.data = (unsigned long)info;
  2404. info->eh_timer.function = fas216_eh_timer;
  2405. spin_lock_init(&info->host_lock);
  2406. memset(&info->stats, 0, sizeof(info->stats));
  2407. msgqueue_initialise(&info->scsi.msgs);
  2408. if (!queue_initialise(&info->queues.issue))
  2409. return -ENOMEM;
  2410. if (!queue_initialise(&info->queues.disconnected)) {
  2411. queue_free(&info->queues.issue);
  2412. return -ENOMEM;
  2413. }
  2414. return 0;
  2415. }
  2416. /**
  2417. * fas216_add - initialise FAS/NCR/AMD SCSI ic.
  2418. * @host: a driver-specific filled-out structure
  2419. * @dev: parent device
  2420. *
  2421. * Initialise FAS/NCR/AMD SCSI ic.
  2422. * Returns: 0 on success
  2423. */
  2424. int fas216_add(struct Scsi_Host *host, struct device *dev)
  2425. {
  2426. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2427. int type, ret;
  2428. if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
  2429. printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
  2430. info->ifcfg.clockrate);
  2431. return -EINVAL;
  2432. }
  2433. fas216_reset_state(info);
  2434. type = fas216_detect_type(info);
  2435. info->scsi.type = chip_types[type];
  2436. udelay(300);
  2437. /*
  2438. * Initialise the chip correctly.
  2439. */
  2440. fas216_init_chip(info);
  2441. /*
  2442. * Reset the SCSI bus. We don't want to see
  2443. * the resulting reset interrupt, so mask it
  2444. * out.
  2445. */
  2446. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
  2447. fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
  2448. /*
  2449. * scsi standard says wait 250ms
  2450. */
  2451. spin_unlock_irq(info->host->host_lock);
  2452. msleep(100*1000/100);
  2453. spin_lock_irq(info->host->host_lock);
  2454. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  2455. fas216_readb(info, REG_INST);
  2456. fas216_checkmagic(info);
  2457. ret = scsi_add_host(host, dev);
  2458. if (ret)
  2459. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2460. else
  2461. scsi_scan_host(host);
  2462. return ret;
  2463. }
  2464. void fas216_remove(struct Scsi_Host *host)
  2465. {
  2466. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2467. fas216_checkmagic(info);
  2468. scsi_remove_host(host);
  2469. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2470. scsi_host_put(host);
  2471. }
  2472. /**
  2473. * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
  2474. * @host: a driver-specific filled-out structure
  2475. *
  2476. * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
  2477. */
  2478. void fas216_release(struct Scsi_Host *host)
  2479. {
  2480. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2481. queue_free(&info->queues.disconnected);
  2482. queue_free(&info->queues.issue);
  2483. }
  2484. void fas216_print_host(FAS216_Info *info, struct seq_file *m)
  2485. {
  2486. seq_printf(m,
  2487. "\n"
  2488. "Chip : %s\n"
  2489. " Address: 0x%p\n"
  2490. " IRQ : %d\n"
  2491. " DMA : %d\n",
  2492. info->scsi.type, info->scsi.io_base,
  2493. info->scsi.irq, info->scsi.dma);
  2494. }
  2495. void fas216_print_stats(FAS216_Info *info, struct seq_file *m)
  2496. {
  2497. seq_printf(m, "\n"
  2498. "Command Statistics:\n"
  2499. " Queued : %u\n"
  2500. " Issued : %u\n"
  2501. " Completed : %u\n"
  2502. " Reads : %u\n"
  2503. " Writes : %u\n"
  2504. " Others : %u\n"
  2505. " Disconnects: %u\n"
  2506. " Aborts : %u\n"
  2507. " Bus resets : %u\n"
  2508. " Host resets: %u\n",
  2509. info->stats.queues, info->stats.removes,
  2510. info->stats.fins, info->stats.reads,
  2511. info->stats.writes, info->stats.miscs,
  2512. info->stats.disconnects, info->stats.aborts,
  2513. info->stats.bus_resets, info->stats.host_resets);
  2514. }
  2515. void fas216_print_devices(FAS216_Info *info, struct seq_file *m)
  2516. {
  2517. struct fas216_device *dev;
  2518. struct scsi_device *scd;
  2519. seq_puts(m, "Device/Lun TaggedQ Parity Sync\n");
  2520. shost_for_each_device(scd, info->host) {
  2521. dev = &info->device[scd->id];
  2522. seq_printf(m, " %d/%llu ", scd->id, scd->lun);
  2523. if (scd->tagged_supported)
  2524. seq_printf(m, "%3sabled(%3d) ",
  2525. scd->simple_tags ? "en" : "dis",
  2526. scd->current_tag);
  2527. else
  2528. seq_puts(m, "unsupported ");
  2529. seq_printf(m, "%3sabled ", dev->parity_enabled ? "en" : "dis");
  2530. if (dev->sof)
  2531. seq_printf(m, "offset %d, %d ns\n",
  2532. dev->sof, dev->period * 4);
  2533. else
  2534. seq_puts(m, "async\n");
  2535. }
  2536. }
  2537. EXPORT_SYMBOL(fas216_init);
  2538. EXPORT_SYMBOL(fas216_add);
  2539. EXPORT_SYMBOL(fas216_queue_command);
  2540. EXPORT_SYMBOL(fas216_noqueue_command);
  2541. EXPORT_SYMBOL(fas216_intr);
  2542. EXPORT_SYMBOL(fas216_remove);
  2543. EXPORT_SYMBOL(fas216_release);
  2544. EXPORT_SYMBOL(fas216_eh_abort);
  2545. EXPORT_SYMBOL(fas216_eh_device_reset);
  2546. EXPORT_SYMBOL(fas216_eh_bus_reset);
  2547. EXPORT_SYMBOL(fas216_eh_host_reset);
  2548. EXPORT_SYMBOL(fas216_print_host);
  2549. EXPORT_SYMBOL(fas216_print_stats);
  2550. EXPORT_SYMBOL(fas216_print_devices);
  2551. MODULE_AUTHOR("Russell King");
  2552. MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
  2553. MODULE_LICENSE("GPL");