fas216.c 77 KB

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