fas216.c 77 KB

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