NCR5380.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831
  1. /*
  2. * NCR 5380 generic driver routines. These should make it *trivial*
  3. * to implement 5380 SCSI drivers under Linux with a non-trantor
  4. * architecture.
  5. *
  6. * Note that these routines also work with NR53c400 family chips.
  7. *
  8. * Copyright 1993, Drew Eckhardt
  9. * Visionary Computing
  10. * (Unix and Linux consulting and custom programming)
  11. * drew@colorado.edu
  12. * +1 (303) 666-5836
  13. *
  14. * DISTRIBUTION RELEASE 6.
  15. *
  16. * For more information, please consult
  17. *
  18. * NCR 5380 Family
  19. * SCSI Protocol Controller
  20. * Databook
  21. *
  22. * NCR Microelectronics
  23. * 1635 Aeroplaza Drive
  24. * Colorado Springs, CO 80916
  25. * 1+ (719) 578-3400
  26. * 1+ (800) 334-5454
  27. */
  28. /*
  29. * Revision 1.10 1998/9/2 Alan Cox
  30. * (alan@lxorguk.ukuu.org.uk)
  31. * Fixed up the timer lockups reported so far. Things still suck. Looking
  32. * forward to 2.3 and per device request queues. Then it'll be possible to
  33. * SMP thread this beast and improve life no end.
  34. * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
  35. * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
  36. * (hopefully) fixed and enhanced USLEEP
  37. * added support for DTC3181E card (for Mustek scanner)
  38. *
  39. * Revision 1.8 Ingmar Baumgart
  40. * (ingmar@gonzo.schwaben.de)
  41. * added support for NCR53C400a card
  42. *
  43. * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
  44. * added proc_info
  45. * added support needed for DTC 3180/3280
  46. * fixed a couple of bugs
  47. *
  48. * Revision 1.5 1994/01/19 09:14:57 drew
  49. * Fixed udelay() hack that was being used on DATAOUT phases
  50. * instead of a proper wait for the final handshake.
  51. *
  52. * Revision 1.4 1994/01/19 06:44:25 drew
  53. * *** empty log message ***
  54. *
  55. * Revision 1.3 1994/01/19 05:24:40 drew
  56. * Added support for TCR LAST_BYTE_SENT bit.
  57. *
  58. * Revision 1.2 1994/01/15 06:14:11 drew
  59. * REAL DMA support, bug fixes.
  60. *
  61. * Revision 1.1 1994/01/15 06:00:54 drew
  62. * Initial revision
  63. *
  64. */
  65. /*
  66. * Further development / testing that should be done :
  67. * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
  68. * code so that everything does the same thing that's done at the
  69. * end of a pseudo-DMA read operation.
  70. *
  71. * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
  72. * basically, transfer size needs to be reduced by one
  73. * and the last byte read as is done with PSEUDO_DMA.
  74. *
  75. * 4. Test SCSI-II tagged queueing (I have no devices which support
  76. * tagged queueing)
  77. *
  78. * 5. Test linked command handling code after Eric is ready with
  79. * the high level code.
  80. */
  81. #include <scsi/scsi_dbg.h>
  82. #include <scsi/scsi_transport_spi.h>
  83. #if (NDEBUG & NDEBUG_LISTS)
  84. #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
  85. #define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
  86. #else
  87. #define LIST(x,y)
  88. #define REMOVE(w,x,y,z)
  89. #endif
  90. #ifndef notyet
  91. #undef LINKED
  92. #undef REAL_DMA
  93. #endif
  94. #ifdef REAL_DMA_POLL
  95. #undef READ_OVERRUNS
  96. #define READ_OVERRUNS
  97. #endif
  98. #ifdef BOARD_REQUIRES_NO_DELAY
  99. #define io_recovery_delay(x)
  100. #else
  101. #define io_recovery_delay(x) udelay(x)
  102. #endif
  103. /*
  104. * Design
  105. *
  106. * This is a generic 5380 driver. To use it on a different platform,
  107. * one simply writes appropriate system specific macros (ie, data
  108. * transfer - some PC's will use the I/O bus, 68K's must use
  109. * memory mapped) and drops this file in their 'C' wrapper.
  110. *
  111. * (Note from hch: unfortunately it was not enough for the different
  112. * m68k folks and instead of improving this driver they copied it
  113. * and hacked it up for their needs. As a consequence they lost
  114. * most updates to this driver. Maybe someone will fix all these
  115. * drivers to use a common core one day..)
  116. *
  117. * As far as command queueing, two queues are maintained for
  118. * each 5380 in the system - commands that haven't been issued yet,
  119. * and commands that are currently executing. This means that an
  120. * unlimited number of commands may be queued, letting
  121. * more commands propagate from the higher driver levels giving higher
  122. * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
  123. * allowing multiple commands to propagate all the way to a SCSI-II device
  124. * while a command is already executing.
  125. *
  126. *
  127. * Issues specific to the NCR5380 :
  128. *
  129. * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
  130. * piece of hardware that requires you to sit in a loop polling for
  131. * the REQ signal as long as you are connected. Some devices are
  132. * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
  133. * while doing long seek operations.
  134. *
  135. * The workaround for this is to keep track of devices that have
  136. * disconnected. If the device hasn't disconnected, for commands that
  137. * should disconnect, we do something like
  138. *
  139. * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
  140. *
  141. * Some tweaking of N and M needs to be done. An algorithm based
  142. * on "time to data" would give the best results as long as short time
  143. * to datas (ie, on the same track) were considered, however these
  144. * broken devices are the exception rather than the rule and I'd rather
  145. * spend my time optimizing for the normal case.
  146. *
  147. * Architecture :
  148. *
  149. * At the heart of the design is a coroutine, NCR5380_main,
  150. * which is started from a workqueue for each NCR5380 host in the
  151. * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
  152. * removing the commands from the issue queue and calling
  153. * NCR5380_select() if a nexus is not established.
  154. *
  155. * Once a nexus is established, the NCR5380_information_transfer()
  156. * phase goes through the various phases as instructed by the target.
  157. * if the target goes into MSG IN and sends a DISCONNECT message,
  158. * the command structure is placed into the per instance disconnected
  159. * queue, and NCR5380_main tries to find more work. If the target is
  160. * idle for too long, the system will try to sleep.
  161. *
  162. * If a command has disconnected, eventually an interrupt will trigger,
  163. * calling NCR5380_intr() which will in turn call NCR5380_reselect
  164. * to reestablish a nexus. This will run main if necessary.
  165. *
  166. * On command termination, the done function will be called as
  167. * appropriate.
  168. *
  169. * SCSI pointers are maintained in the SCp field of SCSI command
  170. * structures, being initialized after the command is connected
  171. * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
  172. * Note that in violation of the standard, an implicit SAVE POINTERS operation
  173. * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
  174. */
  175. /*
  176. * Using this file :
  177. * This file a skeleton Linux SCSI driver for the NCR 5380 series
  178. * of chips. To use it, you write an architecture specific functions
  179. * and macros and include this file in your driver.
  180. *
  181. * These macros control options :
  182. * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
  183. * defined.
  184. *
  185. * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
  186. * for commands that return with a CHECK CONDITION status.
  187. *
  188. * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
  189. * transceivers.
  190. *
  191. * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
  192. * override-configure an IRQ.
  193. *
  194. * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
  195. * bytes at a time. Since interrupts are disabled by default during
  196. * these transfers, we might need this to give reasonable interrupt
  197. * service time if the transfer size gets too large.
  198. *
  199. * LINKED - if defined, linked commands are supported.
  200. *
  201. * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
  202. *
  203. * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
  204. *
  205. * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
  206. * rely on phase mismatch and EOP interrupts to determine end
  207. * of phase.
  208. *
  209. * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
  210. * only really want to use this if you're having a problem with
  211. * dropped characters during high speed communications, and even
  212. * then, you're going to be better off twiddling with transfersize
  213. * in the high level code.
  214. *
  215. * Defaults for these will be provided although the user may want to adjust
  216. * these to allocate CPU resources to the SCSI driver or "real" code.
  217. *
  218. * USLEEP_SLEEP - amount of time, in jiffies, to sleep
  219. *
  220. * USLEEP_POLL - amount of time, in jiffies, to poll
  221. *
  222. * These macros MUST be defined :
  223. * NCR5380_local_declare() - declare any local variables needed for your
  224. * transfer routines.
  225. *
  226. * NCR5380_setup(instance) - initialize any local variables needed from a given
  227. * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
  228. *
  229. * NCR5380_read(register) - read from the specified register
  230. *
  231. * NCR5380_write(register, value) - write to the specific register
  232. *
  233. * NCR5380_implementation_fields - additional fields needed for this
  234. * specific implementation of the NCR5380
  235. *
  236. * Either real DMA *or* pseudo DMA may be implemented
  237. * REAL functions :
  238. * NCR5380_REAL_DMA should be defined if real DMA is to be used.
  239. * Note that the DMA setup functions should return the number of bytes
  240. * that they were able to program the controller for.
  241. *
  242. * Also note that generic i386/PC versions of these macros are
  243. * available as NCR5380_i386_dma_write_setup,
  244. * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
  245. *
  246. * NCR5380_dma_write_setup(instance, src, count) - initialize
  247. * NCR5380_dma_read_setup(instance, dst, count) - initialize
  248. * NCR5380_dma_residual(instance); - residual count
  249. *
  250. * PSEUDO functions :
  251. * NCR5380_pwrite(instance, src, count)
  252. * NCR5380_pread(instance, dst, count);
  253. *
  254. * The generic driver is initialized by calling NCR5380_init(instance),
  255. * after setting the appropriate host specific fields and ID. If the
  256. * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
  257. * possible) function may be used.
  258. */
  259. static int do_abort(struct Scsi_Host *host);
  260. static void do_reset(struct Scsi_Host *host);
  261. /*
  262. * initialize_SCp - init the scsi pointer field
  263. * @cmd: command block to set up
  264. *
  265. * Set up the internal fields in the SCSI command.
  266. */
  267. static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
  268. {
  269. /*
  270. * Initialize the Scsi Pointer field so that all of the commands in the
  271. * various queues are valid.
  272. */
  273. if (scsi_bufflen(cmd)) {
  274. cmd->SCp.buffer = scsi_sglist(cmd);
  275. cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
  276. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  277. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  278. } else {
  279. cmd->SCp.buffer = NULL;
  280. cmd->SCp.buffers_residual = 0;
  281. cmd->SCp.ptr = NULL;
  282. cmd->SCp.this_residual = 0;
  283. }
  284. }
  285. /**
  286. * NCR5380_poll_politely - wait for NCR5380 status bits
  287. * @instance: controller to poll
  288. * @reg: 5380 register to poll
  289. * @bit: Bitmask to check
  290. * @val: Value required to exit
  291. *
  292. * Polls the NCR5380 in a reasonably efficient manner waiting for
  293. * an event to occur, after a short quick poll we begin giving the
  294. * CPU back in non IRQ contexts
  295. *
  296. * Returns the value of the register or a negative error code.
  297. */
  298. static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t)
  299. {
  300. NCR5380_local_declare();
  301. int n = 500; /* At about 8uS a cycle for the cpu access */
  302. unsigned long end = jiffies + t;
  303. int r;
  304. NCR5380_setup(instance);
  305. while( n-- > 0)
  306. {
  307. r = NCR5380_read(reg);
  308. if((r & bit) == val)
  309. return 0;
  310. cpu_relax();
  311. }
  312. /* t time yet ? */
  313. while(time_before(jiffies, end))
  314. {
  315. r = NCR5380_read(reg);
  316. if((r & bit) == val)
  317. return 0;
  318. if(!in_interrupt())
  319. cond_resched();
  320. else
  321. cpu_relax();
  322. }
  323. return -ETIMEDOUT;
  324. }
  325. static struct {
  326. unsigned char value;
  327. const char *name;
  328. } phases[] __maybe_unused = {
  329. {PHASE_DATAOUT, "DATAOUT"},
  330. {PHASE_DATAIN, "DATAIN"},
  331. {PHASE_CMDOUT, "CMDOUT"},
  332. {PHASE_STATIN, "STATIN"},
  333. {PHASE_MSGOUT, "MSGOUT"},
  334. {PHASE_MSGIN, "MSGIN"},
  335. {PHASE_UNKNOWN, "UNKNOWN"}
  336. };
  337. #if NDEBUG
  338. static struct {
  339. unsigned char mask;
  340. const char *name;
  341. } signals[] = {
  342. {SR_DBP, "PARITY"},
  343. {SR_RST, "RST"},
  344. {SR_BSY, "BSY"},
  345. {SR_REQ, "REQ"},
  346. {SR_MSG, "MSG"},
  347. {SR_CD, "CD"},
  348. {SR_IO, "IO"},
  349. {SR_SEL, "SEL"},
  350. {0, NULL}
  351. },
  352. basrs[] = {
  353. {BASR_ATN, "ATN"},
  354. {BASR_ACK, "ACK"},
  355. {0, NULL}
  356. },
  357. icrs[] = {
  358. {ICR_ASSERT_RST, "ASSERT RST"},
  359. {ICR_ASSERT_ACK, "ASSERT ACK"},
  360. {ICR_ASSERT_BSY, "ASSERT BSY"},
  361. {ICR_ASSERT_SEL, "ASSERT SEL"},
  362. {ICR_ASSERT_ATN, "ASSERT ATN"},
  363. {ICR_ASSERT_DATA, "ASSERT DATA"},
  364. {0, NULL}
  365. },
  366. mrs[] = {
  367. {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
  368. {MR_TARGET, "MODE TARGET"},
  369. {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
  370. {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
  371. {MR_MONITOR_BSY, "MODE MONITOR BSY"},
  372. {MR_DMA_MODE, "MODE DMA"},
  373. {MR_ARBITRATE, "MODE ARBITRATION"},
  374. {0, NULL}
  375. };
  376. /**
  377. * NCR5380_print - print scsi bus signals
  378. * @instance: adapter state to dump
  379. *
  380. * Print the SCSI bus signals for debugging purposes
  381. *
  382. * Locks: caller holds hostdata lock (not essential)
  383. */
  384. static void NCR5380_print(struct Scsi_Host *instance)
  385. {
  386. NCR5380_local_declare();
  387. unsigned char status, data, basr, mr, icr, i;
  388. NCR5380_setup(instance);
  389. data = NCR5380_read(CURRENT_SCSI_DATA_REG);
  390. status = NCR5380_read(STATUS_REG);
  391. mr = NCR5380_read(MODE_REG);
  392. icr = NCR5380_read(INITIATOR_COMMAND_REG);
  393. basr = NCR5380_read(BUS_AND_STATUS_REG);
  394. printk("STATUS_REG: %02x ", status);
  395. for (i = 0; signals[i].mask; ++i)
  396. if (status & signals[i].mask)
  397. printk(",%s", signals[i].name);
  398. printk("\nBASR: %02x ", basr);
  399. for (i = 0; basrs[i].mask; ++i)
  400. if (basr & basrs[i].mask)
  401. printk(",%s", basrs[i].name);
  402. printk("\nICR: %02x ", icr);
  403. for (i = 0; icrs[i].mask; ++i)
  404. if (icr & icrs[i].mask)
  405. printk(",%s", icrs[i].name);
  406. printk("\nMODE: %02x ", mr);
  407. for (i = 0; mrs[i].mask; ++i)
  408. if (mr & mrs[i].mask)
  409. printk(",%s", mrs[i].name);
  410. printk("\n");
  411. }
  412. /*
  413. * NCR5380_print_phase - show SCSI phase
  414. * @instance: adapter to dump
  415. *
  416. * Print the current SCSI phase for debugging purposes
  417. *
  418. * Locks: none
  419. */
  420. static void NCR5380_print_phase(struct Scsi_Host *instance)
  421. {
  422. NCR5380_local_declare();
  423. unsigned char status;
  424. int i;
  425. NCR5380_setup(instance);
  426. status = NCR5380_read(STATUS_REG);
  427. if (!(status & SR_REQ))
  428. printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
  429. else {
  430. for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
  431. printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
  432. }
  433. }
  434. #endif
  435. /*
  436. * These need tweaking, and would probably work best as per-device
  437. * flags initialized differently for disk, tape, cd, etc devices.
  438. * People with broken devices are free to experiment as to what gives
  439. * the best results for them.
  440. *
  441. * USLEEP_SLEEP should be a minimum seek time.
  442. *
  443. * USLEEP_POLL should be a maximum rotational latency.
  444. */
  445. #ifndef USLEEP_SLEEP
  446. /* 20 ms (reasonable hard disk speed) */
  447. #define USLEEP_SLEEP (20*HZ/1000)
  448. #endif
  449. /* 300 RPM (floppy speed) */
  450. #ifndef USLEEP_POLL
  451. #define USLEEP_POLL (200*HZ/1000)
  452. #endif
  453. #ifndef USLEEP_WAITLONG
  454. /* RvC: (reasonable time to wait on select error) */
  455. #define USLEEP_WAITLONG USLEEP_SLEEP
  456. #endif
  457. /*
  458. * Function : int should_disconnect (unsigned char cmd)
  459. *
  460. * Purpose : decide whether a command would normally disconnect or
  461. * not, since if it won't disconnect we should go to sleep.
  462. *
  463. * Input : cmd - opcode of SCSI command
  464. *
  465. * Returns : DISCONNECT_LONG if we should disconnect for a really long
  466. * time (ie always, sleep, look for REQ active, sleep),
  467. * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
  468. * time-to-data delay, DISCONNECT_NONE if this command would return
  469. * immediately.
  470. *
  471. * Future sleep algorithms based on time to data can exploit
  472. * something like this so they can differentiate between "normal"
  473. * (ie, read, write, seek) and unusual commands (ie, * format).
  474. *
  475. * Note : We don't deal with commands that handle an immediate disconnect,
  476. *
  477. */
  478. static int should_disconnect(unsigned char cmd)
  479. {
  480. switch (cmd) {
  481. case READ_6:
  482. case WRITE_6:
  483. case SEEK_6:
  484. case READ_10:
  485. case WRITE_10:
  486. case SEEK_10:
  487. return DISCONNECT_TIME_TO_DATA;
  488. case FORMAT_UNIT:
  489. case SEARCH_HIGH:
  490. case SEARCH_LOW:
  491. case SEARCH_EQUAL:
  492. return DISCONNECT_LONG;
  493. default:
  494. return DISCONNECT_NONE;
  495. }
  496. }
  497. static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
  498. {
  499. hostdata->time_expires = jiffies + timeout;
  500. schedule_delayed_work(&hostdata->coroutine, timeout);
  501. }
  502. static int probe_irq __initdata = 0;
  503. /**
  504. * probe_intr - helper for IRQ autoprobe
  505. * @irq: interrupt number
  506. * @dev_id: unused
  507. * @regs: unused
  508. *
  509. * Set a flag to indicate the IRQ in question was received. This is
  510. * used by the IRQ probe code.
  511. */
  512. static irqreturn_t __init probe_intr(int irq, void *dev_id)
  513. {
  514. probe_irq = irq;
  515. return IRQ_HANDLED;
  516. }
  517. /**
  518. * NCR5380_probe_irq - find the IRQ of an NCR5380
  519. * @instance: NCR5380 controller
  520. * @possible: bitmask of ISA IRQ lines
  521. *
  522. * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
  523. * and then looking to see what interrupt actually turned up.
  524. *
  525. * Locks: none, irqs must be enabled on entry
  526. */
  527. static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
  528. int possible)
  529. {
  530. NCR5380_local_declare();
  531. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  532. unsigned long timeout;
  533. int trying_irqs, i, mask;
  534. NCR5380_setup(instance);
  535. for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
  536. if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
  537. trying_irqs |= mask;
  538. timeout = jiffies + (250 * HZ / 1000);
  539. probe_irq = SCSI_IRQ_NONE;
  540. /*
  541. * A interrupt is triggered whenever BSY = false, SEL = true
  542. * and a bit set in the SELECT_ENABLE_REG is asserted on the
  543. * SCSI bus.
  544. *
  545. * Note that the bus is only driven when the phase control signals
  546. * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
  547. * to zero.
  548. */
  549. NCR5380_write(TARGET_COMMAND_REG, 0);
  550. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  551. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  552. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
  553. while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
  554. schedule_timeout_uninterruptible(1);
  555. NCR5380_write(SELECT_ENABLE_REG, 0);
  556. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  557. for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
  558. if (trying_irqs & mask)
  559. free_irq(i, NULL);
  560. return probe_irq;
  561. }
  562. /**
  563. * NCR58380_print_options - show options
  564. * @instance: unused for now
  565. *
  566. * Called by probe code indicating the NCR5380 driver options that
  567. * were selected. At some point this will switch to runtime options
  568. * read from the adapter in question
  569. *
  570. * Locks: none
  571. */
  572. static void __init __maybe_unused
  573. NCR5380_print_options(struct Scsi_Host *instance)
  574. {
  575. printk(" generic options"
  576. #ifdef AUTOPROBE_IRQ
  577. " AUTOPROBE_IRQ"
  578. #endif
  579. #ifdef AUTOSENSE
  580. " AUTOSENSE"
  581. #endif
  582. #ifdef DIFFERENTIAL
  583. " DIFFERENTIAL"
  584. #endif
  585. #ifdef REAL_DMA
  586. " REAL DMA"
  587. #endif
  588. #ifdef REAL_DMA_POLL
  589. " REAL DMA POLL"
  590. #endif
  591. #ifdef PARITY
  592. " PARITY"
  593. #endif
  594. #ifdef PSEUDO_DMA
  595. " PSEUDO DMA"
  596. #endif
  597. #ifdef UNSAFE
  598. " UNSAFE "
  599. #endif
  600. );
  601. printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
  602. printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
  603. if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
  604. printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
  605. }
  606. }
  607. /**
  608. * NCR5380_print_status - dump controller info
  609. * @instance: controller to dump
  610. *
  611. * Print commands in the various queues, called from NCR5380_abort
  612. * and NCR5380_debug to aid debugging.
  613. *
  614. * Locks: called functions disable irqs
  615. */
  616. static void NCR5380_print_status(struct Scsi_Host *instance)
  617. {
  618. NCR5380_dprint(NDEBUG_ANY, instance);
  619. NCR5380_dprint_phase(NDEBUG_ANY, instance);
  620. }
  621. /******************************************/
  622. /*
  623. * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
  624. *
  625. * *buffer: I/O buffer
  626. * **start: if inout == FALSE pointer into buffer where user read should start
  627. * offset: current offset
  628. * length: length of buffer
  629. * hostno: Scsi_Host host_no
  630. * inout: TRUE - user is writing; FALSE - user is reading
  631. *
  632. * Return the number of bytes read from or written
  633. */
  634. static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
  635. char *buffer, int length)
  636. {
  637. #ifdef DTC_PUBLIC_RELEASE
  638. dtc_wmaxi = dtc_maxi = 0;
  639. #endif
  640. #ifdef PAS16_PUBLIC_RELEASE
  641. pas_wmaxi = pas_maxi = 0;
  642. #endif
  643. return (-ENOSYS); /* Currently this is a no-op */
  644. }
  645. #undef SPRINTF
  646. #define SPRINTF(args...) seq_printf(m, ## args)
  647. static
  648. void lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, struct seq_file *m);
  649. static
  650. void lprint_command(unsigned char *cmd, struct seq_file *m);
  651. static
  652. void lprint_opcode(int opcode, struct seq_file *m);
  653. static int __maybe_unused NCR5380_show_info(struct seq_file *m,
  654. struct Scsi_Host *instance)
  655. {
  656. struct NCR5380_hostdata *hostdata;
  657. Scsi_Cmnd *ptr;
  658. hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  659. SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
  660. if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
  661. SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
  662. #ifdef DTC_PUBLIC_RELEASE
  663. SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
  664. #endif
  665. #ifdef T128_PUBLIC_RELEASE
  666. SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
  667. #endif
  668. #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
  669. SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
  670. #endif
  671. #ifdef PAS16_PUBLIC_RELEASE
  672. SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
  673. #endif
  674. SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
  675. SPRINTF("io_port: %04x ", (int) instance->io_port);
  676. if (instance->irq == SCSI_IRQ_NONE)
  677. SPRINTF("IRQ: None.\n");
  678. else
  679. SPRINTF("IRQ: %d.\n", instance->irq);
  680. #ifdef DTC_PUBLIC_RELEASE
  681. SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", dtc_wmaxi, dtc_maxi);
  682. #endif
  683. #ifdef PAS16_PUBLIC_RELEASE
  684. SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi, pas_maxi);
  685. #endif
  686. spin_lock_irq(instance->host_lock);
  687. if (!hostdata->connected)
  688. SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
  689. else
  690. lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, m);
  691. SPRINTF("scsi%d: issue_queue\n", instance->host_no);
  692. for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
  693. lprint_Scsi_Cmnd(ptr, m);
  694. SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
  695. for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
  696. lprint_Scsi_Cmnd(ptr, m);
  697. spin_unlock_irq(instance->host_lock);
  698. return 0;
  699. }
  700. static void lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, struct seq_file *m)
  701. {
  702. SPRINTF("scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
  703. SPRINTF(" command = ");
  704. lprint_command(cmd->cmnd, m);
  705. }
  706. static void lprint_command(unsigned char *command, struct seq_file *m)
  707. {
  708. int i, s;
  709. lprint_opcode(command[0], m);
  710. for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
  711. SPRINTF("%02x ", command[i]);
  712. SPRINTF("\n");
  713. }
  714. static void lprint_opcode(int opcode, struct seq_file *m)
  715. {
  716. SPRINTF("%2d (0x%02x)", opcode, opcode);
  717. }
  718. /**
  719. * NCR5380_init - initialise an NCR5380
  720. * @instance: adapter to configure
  721. * @flags: control flags
  722. *
  723. * Initializes *instance and corresponding 5380 chip,
  724. * with flags OR'd into the initial flags value.
  725. *
  726. * Notes : I assume that the host, hostno, and id bits have been
  727. * set correctly. I don't care about the irq and other fields.
  728. *
  729. * Returns 0 for success
  730. *
  731. * Locks: interrupts must be enabled when we are called
  732. */
  733. static int NCR5380_init(struct Scsi_Host *instance, int flags)
  734. {
  735. NCR5380_local_declare();
  736. int i, pass;
  737. unsigned long timeout;
  738. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  739. if(in_interrupt())
  740. printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
  741. /*
  742. * On NCR53C400 boards, NCR5380 registers are mapped 8 past
  743. * the base address.
  744. */
  745. #ifdef NCR53C400
  746. if (flags & FLAG_NCR53C400)
  747. instance->NCR5380_instance_name += NCR53C400_address_adjust;
  748. #endif
  749. NCR5380_setup(instance);
  750. hostdata->aborted = 0;
  751. hostdata->id_mask = 1 << instance->this_id;
  752. for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
  753. if (i > hostdata->id_mask)
  754. hostdata->id_higher_mask |= i;
  755. for (i = 0; i < 8; ++i)
  756. hostdata->busy[i] = 0;
  757. #ifdef REAL_DMA
  758. hostdata->dmalen = 0;
  759. #endif
  760. hostdata->targets_present = 0;
  761. hostdata->connected = NULL;
  762. hostdata->issue_queue = NULL;
  763. hostdata->disconnected_queue = NULL;
  764. INIT_DELAYED_WORK(&hostdata->coroutine, NCR5380_main);
  765. #ifdef NCR5380_STATS
  766. for (i = 0; i < 8; ++i) {
  767. hostdata->time_read[i] = 0;
  768. hostdata->time_write[i] = 0;
  769. hostdata->bytes_read[i] = 0;
  770. hostdata->bytes_write[i] = 0;
  771. }
  772. hostdata->timebase = 0;
  773. hostdata->pendingw = 0;
  774. hostdata->pendingr = 0;
  775. #endif
  776. /* The CHECK code seems to break the 53C400. Will check it later maybe */
  777. if (flags & FLAG_NCR53C400)
  778. hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
  779. else
  780. hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
  781. hostdata->host = instance;
  782. hostdata->time_expires = 0;
  783. #ifndef AUTOSENSE
  784. if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
  785. printk(KERN_WARNING "scsi%d : WARNING : support for multiple outstanding commands enabled\n" " without AUTOSENSE option, contingent allegiance conditions may\n"
  786. " be incorrectly cleared.\n", instance->host_no);
  787. #endif /* def AUTOSENSE */
  788. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  789. NCR5380_write(MODE_REG, MR_BASE);
  790. NCR5380_write(TARGET_COMMAND_REG, 0);
  791. NCR5380_write(SELECT_ENABLE_REG, 0);
  792. #ifdef NCR53C400
  793. if (hostdata->flags & FLAG_NCR53C400) {
  794. NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
  795. }
  796. #endif
  797. /*
  798. * Detect and correct bus wedge problems.
  799. *
  800. * If the system crashed, it may have crashed in a state
  801. * where a SCSI command was still executing, and the
  802. * SCSI bus is not in a BUS FREE STATE.
  803. *
  804. * If this is the case, we'll try to abort the currently
  805. * established nexus which we know nothing about, and that
  806. * failing, do a hard reset of the SCSI bus
  807. */
  808. for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
  809. switch (pass) {
  810. case 1:
  811. case 3:
  812. case 5:
  813. printk(KERN_INFO "scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no);
  814. timeout = jiffies + 5 * HZ;
  815. NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, 0, 5*HZ);
  816. break;
  817. case 2:
  818. printk(KERN_WARNING "scsi%d: bus busy, attempting abort\n", instance->host_no);
  819. do_abort(instance);
  820. break;
  821. case 4:
  822. printk(KERN_WARNING "scsi%d: bus busy, attempting reset\n", instance->host_no);
  823. do_reset(instance);
  824. break;
  825. case 6:
  826. printk(KERN_ERR "scsi%d: bus locked solid or invalid override\n", instance->host_no);
  827. return -ENXIO;
  828. }
  829. }
  830. return 0;
  831. }
  832. /**
  833. * NCR5380_exit - remove an NCR5380
  834. * @instance: adapter to remove
  835. */
  836. static void NCR5380_exit(struct Scsi_Host *instance)
  837. {
  838. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  839. cancel_delayed_work_sync(&hostdata->coroutine);
  840. }
  841. /**
  842. * NCR5380_queue_command - queue a command
  843. * @cmd: SCSI command
  844. * @done: completion handler
  845. *
  846. * cmd is added to the per instance issue_queue, with minor
  847. * twiddling done to the host specific fields of cmd. If the
  848. * main coroutine is not running, it is restarted.
  849. *
  850. * Locks: host lock taken by caller
  851. */
  852. static int NCR5380_queue_command_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
  853. {
  854. struct Scsi_Host *instance = cmd->device->host;
  855. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  856. Scsi_Cmnd *tmp;
  857. #if (NDEBUG & NDEBUG_NO_WRITE)
  858. switch (cmd->cmnd[0]) {
  859. case WRITE_6:
  860. case WRITE_10:
  861. printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
  862. cmd->result = (DID_ERROR << 16);
  863. done(cmd);
  864. return 0;
  865. }
  866. #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
  867. #ifdef NCR5380_STATS
  868. switch (cmd->cmnd[0]) {
  869. case WRITE:
  870. case WRITE_6:
  871. case WRITE_10:
  872. hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
  873. hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
  874. hostdata->pendingw++;
  875. break;
  876. case READ:
  877. case READ_6:
  878. case READ_10:
  879. hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
  880. hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
  881. hostdata->pendingr++;
  882. break;
  883. }
  884. #endif
  885. /*
  886. * We use the host_scribble field as a pointer to the next command
  887. * in a queue
  888. */
  889. cmd->host_scribble = NULL;
  890. cmd->scsi_done = done;
  891. cmd->result = 0;
  892. /*
  893. * Insert the cmd into the issue queue. Note that REQUEST SENSE
  894. * commands are added to the head of the queue since any command will
  895. * clear the contingent allegiance condition that exists and the
  896. * sense data is only guaranteed to be valid while the condition exists.
  897. */
  898. if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  899. LIST(cmd, hostdata->issue_queue);
  900. cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
  901. hostdata->issue_queue = cmd;
  902. } else {
  903. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
  904. LIST(cmd, tmp);
  905. tmp->host_scribble = (unsigned char *) cmd;
  906. }
  907. dprintk(NDEBUG_QUEUES, "scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
  908. /* Run the coroutine if it isn't already running. */
  909. /* Kick off command processing */
  910. schedule_delayed_work(&hostdata->coroutine, 0);
  911. return 0;
  912. }
  913. static DEF_SCSI_QCMD(NCR5380_queue_command)
  914. /**
  915. * NCR5380_main - NCR state machines
  916. *
  917. * NCR5380_main is a coroutine that runs as long as more work can
  918. * be done on the NCR5380 host adapters in a system. Both
  919. * NCR5380_queue_command() and NCR5380_intr() will try to start it
  920. * in case it is not running.
  921. *
  922. * Locks: called as its own thread with no locks held. Takes the
  923. * host lock and called routines may take the isa dma lock.
  924. */
  925. static void NCR5380_main(struct work_struct *work)
  926. {
  927. struct NCR5380_hostdata *hostdata =
  928. container_of(work, struct NCR5380_hostdata, coroutine.work);
  929. struct Scsi_Host *instance = hostdata->host;
  930. Scsi_Cmnd *tmp, *prev;
  931. int done;
  932. spin_lock_irq(instance->host_lock);
  933. do {
  934. /* Lock held here */
  935. done = 1;
  936. if (!hostdata->connected && !hostdata->selecting) {
  937. dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
  938. /*
  939. * Search through the issue_queue for a command destined
  940. * for a target that's not busy.
  941. */
  942. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
  943. {
  944. if (prev != tmp)
  945. dprintk(NDEBUG_LISTS, "MAIN tmp=%p target=%d busy=%d lun=%llu\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun);
  946. /* When we find one, remove it from the issue queue. */
  947. if (!(hostdata->busy[tmp->device->id] &
  948. (1 << (u8)(tmp->device->lun & 0xff)))) {
  949. if (prev) {
  950. REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
  951. prev->host_scribble = tmp->host_scribble;
  952. } else {
  953. REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
  954. hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
  955. }
  956. tmp->host_scribble = NULL;
  957. /*
  958. * Attempt to establish an I_T_L nexus here.
  959. * On success, instance->hostdata->connected is set.
  960. * On failure, we must add the command back to the
  961. * issue queue so we can keep trying.
  962. */
  963. dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main() : command for target %d lun %llu removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun);
  964. /*
  965. * A successful selection is defined as one that
  966. * leaves us with the command connected and
  967. * in hostdata->connected, OR has terminated the
  968. * command.
  969. *
  970. * With successful commands, we fall through
  971. * and see if we can do an information transfer,
  972. * with failures we will restart.
  973. */
  974. hostdata->selecting = NULL;
  975. /* RvC: have to preset this to indicate a new command is being performed */
  976. if (!NCR5380_select(instance, tmp,
  977. /*
  978. * REQUEST SENSE commands are issued without tagged
  979. * queueing, even on SCSI-II devices because the
  980. * contingent allegiance condition exists for the
  981. * entire unit.
  982. */
  983. (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
  984. break;
  985. } else {
  986. LIST(tmp, hostdata->issue_queue);
  987. tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
  988. hostdata->issue_queue = tmp;
  989. done = 0;
  990. dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no);
  991. }
  992. /* lock held here still */
  993. } /* if target/lun is not busy */
  994. } /* for */
  995. /* exited locked */
  996. } /* if (!hostdata->connected) */
  997. if (hostdata->selecting) {
  998. tmp = (Scsi_Cmnd *) hostdata->selecting;
  999. /* Selection will drop and retake the lock */
  1000. if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
  1001. /* Ok ?? */
  1002. } else {
  1003. /* RvC: device failed, so we wait a long time
  1004. this is needed for Mustek scanners, that
  1005. do not respond to commands immediately
  1006. after a scan */
  1007. printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id);
  1008. LIST(tmp, hostdata->issue_queue);
  1009. tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
  1010. hostdata->issue_queue = tmp;
  1011. NCR5380_set_timer(hostdata, USLEEP_WAITLONG);
  1012. }
  1013. } /* if hostdata->selecting */
  1014. if (hostdata->connected
  1015. #ifdef REAL_DMA
  1016. && !hostdata->dmalen
  1017. #endif
  1018. && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
  1019. ) {
  1020. dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
  1021. NCR5380_information_transfer(instance);
  1022. dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
  1023. done = 0;
  1024. } else
  1025. break;
  1026. } while (!done);
  1027. spin_unlock_irq(instance->host_lock);
  1028. }
  1029. #ifndef DONT_USE_INTR
  1030. /**
  1031. * NCR5380_intr - generic NCR5380 irq handler
  1032. * @irq: interrupt number
  1033. * @dev_id: device info
  1034. *
  1035. * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
  1036. * from the disconnected queue, and restarting NCR5380_main()
  1037. * as required.
  1038. *
  1039. * Locks: takes the needed instance locks
  1040. */
  1041. static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
  1042. {
  1043. NCR5380_local_declare();
  1044. struct Scsi_Host *instance = dev_id;
  1045. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1046. int done;
  1047. unsigned char basr;
  1048. unsigned long flags;
  1049. dprintk(NDEBUG_INTR, "scsi : NCR5380 irq %d triggered\n",
  1050. instance->irq);
  1051. do {
  1052. done = 1;
  1053. spin_lock_irqsave(instance->host_lock, flags);
  1054. /* Look for pending interrupts */
  1055. NCR5380_setup(instance);
  1056. basr = NCR5380_read(BUS_AND_STATUS_REG);
  1057. /* XXX dispatch to appropriate routine if found and done=0 */
  1058. if (basr & BASR_IRQ) {
  1059. NCR5380_dprint(NDEBUG_INTR, instance);
  1060. if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
  1061. done = 0;
  1062. dprintk(NDEBUG_INTR, "scsi%d : SEL interrupt\n", instance->host_no);
  1063. NCR5380_reselect(instance);
  1064. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1065. } else if (basr & BASR_PARITY_ERROR) {
  1066. dprintk(NDEBUG_INTR, "scsi%d : PARITY interrupt\n", instance->host_no);
  1067. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1068. } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
  1069. dprintk(NDEBUG_INTR, "scsi%d : RESET interrupt\n", instance->host_no);
  1070. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1071. } else {
  1072. #if defined(REAL_DMA)
  1073. /*
  1074. * We should only get PHASE MISMATCH and EOP interrupts
  1075. * if we have DMA enabled, so do a sanity check based on
  1076. * the current setting of the MODE register.
  1077. */
  1078. if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
  1079. int transferred;
  1080. if (!hostdata->connected)
  1081. panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
  1082. transferred = (hostdata->dmalen - NCR5380_dma_residual(instance));
  1083. hostdata->connected->SCp.this_residual -= transferred;
  1084. hostdata->connected->SCp.ptr += transferred;
  1085. hostdata->dmalen = 0;
  1086. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1087. /* FIXME: we need to poll briefly then defer a workqueue task ! */
  1088. NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
  1089. NCR5380_write(MODE_REG, MR_BASE);
  1090. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1091. }
  1092. #else
  1093. dprintk(NDEBUG_INTR, "scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
  1094. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1095. #endif
  1096. }
  1097. } /* if BASR_IRQ */
  1098. spin_unlock_irqrestore(instance->host_lock, flags);
  1099. if(!done)
  1100. schedule_delayed_work(&hostdata->coroutine, 0);
  1101. } while (!done);
  1102. return IRQ_HANDLED;
  1103. }
  1104. #endif
  1105. /**
  1106. * collect_stats - collect stats on a scsi command
  1107. * @hostdata: adapter
  1108. * @cmd: command being issued
  1109. *
  1110. * Update the statistical data by parsing the command in question
  1111. */
  1112. static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd)
  1113. {
  1114. #ifdef NCR5380_STATS
  1115. switch (cmd->cmnd[0]) {
  1116. case WRITE:
  1117. case WRITE_6:
  1118. case WRITE_10:
  1119. hostdata->time_write[scmd_id(cmd)] += (jiffies - hostdata->timebase);
  1120. hostdata->pendingw--;
  1121. break;
  1122. case READ:
  1123. case READ_6:
  1124. case READ_10:
  1125. hostdata->time_read[scmd_id(cmd)] += (jiffies - hostdata->timebase);
  1126. hostdata->pendingr--;
  1127. break;
  1128. }
  1129. #endif
  1130. }
  1131. /*
  1132. * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
  1133. * int tag);
  1134. *
  1135. * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
  1136. * including ARBITRATION, SELECTION, and initial message out for
  1137. * IDENTIFY and queue messages.
  1138. *
  1139. * Inputs : instance - instantiation of the 5380 driver on which this
  1140. * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
  1141. * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
  1142. * the command that is presently connected.
  1143. *
  1144. * Returns : -1 if selection could not execute for some reason,
  1145. * 0 if selection succeeded or failed because the target
  1146. * did not respond.
  1147. *
  1148. * Side effects :
  1149. * If bus busy, arbitration failed, etc, NCR5380_select() will exit
  1150. * with registers as they should have been on entry - ie
  1151. * SELECT_ENABLE will be set appropriately, the NCR5380
  1152. * will cease to drive any SCSI bus signals.
  1153. *
  1154. * If successful : I_T_L or I_T_L_Q nexus will be established,
  1155. * instance->connected will be set to cmd.
  1156. * SELECT interrupt will be disabled.
  1157. *
  1158. * If failed (no target) : cmd->scsi_done() will be called, and the
  1159. * cmd->result host byte set to DID_BAD_TARGET.
  1160. *
  1161. * Locks: caller holds hostdata lock in IRQ mode
  1162. */
  1163. static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
  1164. {
  1165. NCR5380_local_declare();
  1166. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1167. unsigned char tmp[3], phase;
  1168. unsigned char *data;
  1169. int len;
  1170. unsigned long timeout;
  1171. unsigned char value;
  1172. int err;
  1173. NCR5380_setup(instance);
  1174. if (hostdata->selecting)
  1175. goto part2;
  1176. hostdata->restart_select = 0;
  1177. NCR5380_dprint(NDEBUG_ARBITRATION, instance);
  1178. dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
  1179. /*
  1180. * Set the phase bits to 0, otherwise the NCR5380 won't drive the
  1181. * data bus during SELECTION.
  1182. */
  1183. NCR5380_write(TARGET_COMMAND_REG, 0);
  1184. /*
  1185. * Start arbitration.
  1186. */
  1187. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  1188. NCR5380_write(MODE_REG, MR_ARBITRATE);
  1189. /* We can be relaxed here, interrupts are on, we are
  1190. in workqueue context, the birds are singing in the trees */
  1191. spin_unlock_irq(instance->host_lock);
  1192. err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
  1193. spin_lock_irq(instance->host_lock);
  1194. if (err < 0) {
  1195. printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
  1196. NCR5380_write(MODE_REG, MR_BASE);
  1197. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1198. goto failed;
  1199. }
  1200. dprintk(NDEBUG_ARBITRATION, "scsi%d : arbitration complete\n", instance->host_no);
  1201. /*
  1202. * The arbitration delay is 2.2us, but this is a minimum and there is
  1203. * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
  1204. * the integral nature of udelay().
  1205. *
  1206. */
  1207. udelay(3);
  1208. /* Check for lost arbitration */
  1209. if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
  1210. NCR5380_write(MODE_REG, MR_BASE);
  1211. dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
  1212. goto failed;
  1213. }
  1214. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
  1215. if (!(hostdata->flags & FLAG_DTC3181E) &&
  1216. /* RvC: DTC3181E has some trouble with this
  1217. * so we simply removed it. Seems to work with
  1218. * only Mustek scanner attached
  1219. */
  1220. (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
  1221. NCR5380_write(MODE_REG, MR_BASE);
  1222. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1223. dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
  1224. goto failed;
  1225. }
  1226. /*
  1227. * Again, bus clear + bus settle time is 1.2us, however, this is
  1228. * a minimum so we'll udelay ceil(1.2)
  1229. */
  1230. udelay(2);
  1231. dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
  1232. /*
  1233. * Now that we have won arbitration, start Selection process, asserting
  1234. * the host and target ID's on the SCSI bus.
  1235. */
  1236. NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
  1237. /*
  1238. * Raise ATN while SEL is true before BSY goes false from arbitration,
  1239. * since this is the only way to guarantee that we'll get a MESSAGE OUT
  1240. * phase immediately after selection.
  1241. */
  1242. NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
  1243. NCR5380_write(MODE_REG, MR_BASE);
  1244. /*
  1245. * Reselect interrupts must be turned off prior to the dropping of BSY,
  1246. * otherwise we will trigger an interrupt.
  1247. */
  1248. NCR5380_write(SELECT_ENABLE_REG, 0);
  1249. /*
  1250. * The initiator shall then wait at least two deskew delays and release
  1251. * the BSY signal.
  1252. */
  1253. udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
  1254. /* Reset BSY */
  1255. NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
  1256. /*
  1257. * Something weird happens when we cease to drive BSY - looks
  1258. * like the board/chip is letting us do another read before the
  1259. * appropriate propagation delay has expired, and we're confusing
  1260. * a BSY signal from ourselves as the target's response to SELECTION.
  1261. *
  1262. * A small delay (the 'C++' frontend breaks the pipeline with an
  1263. * unnecessary jump, making it work on my 386-33/Trantor T128, the
  1264. * tighter 'C' code breaks and requires this) solves the problem -
  1265. * the 1 us delay is arbitrary, and only used because this delay will
  1266. * be the same on other platforms and since it works here, it should
  1267. * work there.
  1268. *
  1269. * wingel suggests that this could be due to failing to wait
  1270. * one deskew delay.
  1271. */
  1272. udelay(1);
  1273. dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
  1274. /*
  1275. * The SCSI specification calls for a 250 ms timeout for the actual
  1276. * selection.
  1277. */
  1278. timeout = jiffies + (250 * HZ / 1000);
  1279. /*
  1280. * XXX very interesting - we're seeing a bounce where the BSY we
  1281. * asserted is being reflected / still asserted (propagation delay?)
  1282. * and it's detecting as true. Sigh.
  1283. */
  1284. hostdata->select_time = 0; /* we count the clock ticks at which we polled */
  1285. hostdata->selecting = cmd;
  1286. part2:
  1287. /* RvC: here we enter after a sleeping period, or immediately after
  1288. execution of part 1
  1289. we poll only once ech clock tick */
  1290. value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
  1291. if (!value && (hostdata->select_time < HZ/4)) {
  1292. /* RvC: we still must wait for a device response */
  1293. hostdata->select_time++; /* after 25 ticks the device has failed */
  1294. NCR5380_set_timer(hostdata, 1);
  1295. return 0; /* RvC: we return here with hostdata->selecting set,
  1296. to go to sleep */
  1297. }
  1298. hostdata->selecting = NULL;/* clear this pointer, because we passed the
  1299. waiting period */
  1300. if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
  1301. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1302. NCR5380_reselect(instance);
  1303. printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
  1304. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1305. return -1;
  1306. }
  1307. /*
  1308. * No less than two deskew delays after the initiator detects the
  1309. * BSY signal is true, it shall release the SEL signal and may
  1310. * change the DATA BUS. -wingel
  1311. */
  1312. udelay(1);
  1313. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1314. if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
  1315. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1316. if (hostdata->targets_present & (1 << scmd_id(cmd))) {
  1317. printk(KERN_DEBUG "scsi%d : weirdness\n", instance->host_no);
  1318. if (hostdata->restart_select)
  1319. printk(KERN_DEBUG "\trestart select\n");
  1320. NCR5380_dprint(NDEBUG_SELECTION, instance);
  1321. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1322. return -1;
  1323. }
  1324. cmd->result = DID_BAD_TARGET << 16;
  1325. collect_stats(hostdata, cmd);
  1326. cmd->scsi_done(cmd);
  1327. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1328. dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n", instance->host_no);
  1329. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1330. return 0;
  1331. }
  1332. hostdata->targets_present |= (1 << scmd_id(cmd));
  1333. /*
  1334. * Since we followed the SCSI spec, and raised ATN while SEL
  1335. * was true but before BSY was false during selection, the information
  1336. * transfer phase should be a MESSAGE OUT phase so that we can send the
  1337. * IDENTIFY message.
  1338. *
  1339. * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
  1340. * message (2 bytes) with a tag ID that we increment with every command
  1341. * until it wraps back to 0.
  1342. *
  1343. * XXX - it turns out that there are some broken SCSI-II devices,
  1344. * which claim to support tagged queuing but fail when more than
  1345. * some number of commands are issued at once.
  1346. */
  1347. /* Wait for start of REQ/ACK handshake */
  1348. spin_unlock_irq(instance->host_lock);
  1349. err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
  1350. spin_lock_irq(instance->host_lock);
  1351. if(err) {
  1352. printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
  1353. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1354. goto failed;
  1355. }
  1356. dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
  1357. tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->device->lun);
  1358. len = 1;
  1359. cmd->tag = 0;
  1360. /* Send message(s) */
  1361. data = tmp;
  1362. phase = PHASE_MSGOUT;
  1363. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1364. dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
  1365. /* XXX need to handle errors here */
  1366. hostdata->connected = cmd;
  1367. hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
  1368. initialize_SCp(cmd);
  1369. return 0;
  1370. /* Selection failed */
  1371. failed:
  1372. return -1;
  1373. }
  1374. /*
  1375. * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
  1376. * unsigned char *phase, int *count, unsigned char **data)
  1377. *
  1378. * Purpose : transfers data in given phase using polled I/O
  1379. *
  1380. * Inputs : instance - instance of driver, *phase - pointer to
  1381. * what phase is expected, *count - pointer to number of
  1382. * bytes to transfer, **data - pointer to data pointer.
  1383. *
  1384. * Returns : -1 when different phase is entered without transferring
  1385. * maximum number of bytes, 0 if all bytes or transferred or exit
  1386. * is in same phase.
  1387. *
  1388. * Also, *phase, *count, *data are modified in place.
  1389. *
  1390. * XXX Note : handling for bus free may be useful.
  1391. */
  1392. /*
  1393. * Note : this code is not as quick as it could be, however it
  1394. * IS 100% reliable, and for the actual data transfer where speed
  1395. * counts, we will always do a pseudo DMA or DMA transfer.
  1396. */
  1397. static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
  1398. NCR5380_local_declare();
  1399. unsigned char p = *phase, tmp;
  1400. int c = *count;
  1401. unsigned char *d = *data;
  1402. /*
  1403. * RvC: some administrative data to process polling time
  1404. */
  1405. int break_allowed = 0;
  1406. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1407. NCR5380_setup(instance);
  1408. if (!(p & SR_IO))
  1409. dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
  1410. else
  1411. dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
  1412. /*
  1413. * The NCR5380 chip will only drive the SCSI bus when the
  1414. * phase specified in the appropriate bits of the TARGET COMMAND
  1415. * REGISTER match the STATUS REGISTER
  1416. */
  1417. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1418. /* RvC: don't know if this is necessary, but other SCSI I/O is short
  1419. * so breaks are not necessary there
  1420. */
  1421. if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
  1422. break_allowed = 1;
  1423. }
  1424. do {
  1425. /*
  1426. * Wait for assertion of REQ, after which the phase bits will be
  1427. * valid
  1428. */
  1429. /* RvC: we simply poll once, after that we stop temporarily
  1430. * and let the device buffer fill up
  1431. * if breaking is not allowed, we keep polling as long as needed
  1432. */
  1433. /* FIXME */
  1434. while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
  1435. if (!(tmp & SR_REQ)) {
  1436. /* timeout condition */
  1437. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  1438. break;
  1439. }
  1440. dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
  1441. /* Check for phase mismatch */
  1442. if ((tmp & PHASE_MASK) != p) {
  1443. dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
  1444. NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
  1445. break;
  1446. }
  1447. /* Do actual transfer from SCSI bus to / from memory */
  1448. if (!(p & SR_IO))
  1449. NCR5380_write(OUTPUT_DATA_REG, *d);
  1450. else
  1451. *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
  1452. ++d;
  1453. /*
  1454. * The SCSI standard suggests that in MSGOUT phase, the initiator
  1455. * should drop ATN on the last byte of the message phase
  1456. * after REQ has been asserted for the handshake but before
  1457. * the initiator raises ACK.
  1458. */
  1459. if (!(p & SR_IO)) {
  1460. if (!((p & SR_MSG) && c > 1)) {
  1461. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1462. NCR5380_dprint(NDEBUG_PIO, instance);
  1463. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
  1464. } else {
  1465. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
  1466. NCR5380_dprint(NDEBUG_PIO, instance);
  1467. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1468. }
  1469. } else {
  1470. NCR5380_dprint(NDEBUG_PIO, instance);
  1471. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  1472. }
  1473. /* FIXME - if this fails bus reset ?? */
  1474. NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ);
  1475. dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
  1476. /*
  1477. * We have several special cases to consider during REQ/ACK handshaking :
  1478. * 1. We were in MSGOUT phase, and we are on the last byte of the
  1479. * message. ATN must be dropped as ACK is dropped.
  1480. *
  1481. * 2. We are in a MSGIN phase, and we are on the last byte of the
  1482. * message. We must exit with ACK asserted, so that the calling
  1483. * code may raise ATN before dropping ACK to reject the message.
  1484. *
  1485. * 3. ACK and ATN are clear and the target may proceed as normal.
  1486. */
  1487. if (!(p == PHASE_MSGIN && c == 1)) {
  1488. if (p == PHASE_MSGOUT && c > 1)
  1489. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1490. else
  1491. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1492. }
  1493. } while (--c);
  1494. dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
  1495. *count = c;
  1496. *data = d;
  1497. tmp = NCR5380_read(STATUS_REG);
  1498. if (tmp & SR_REQ)
  1499. *phase = tmp & PHASE_MASK;
  1500. else
  1501. *phase = PHASE_UNKNOWN;
  1502. if (!c || (*phase == p))
  1503. return 0;
  1504. else
  1505. return -1;
  1506. }
  1507. /**
  1508. * do_reset - issue a reset command
  1509. * @host: adapter to reset
  1510. *
  1511. * Issue a reset sequence to the NCR5380 and try and get the bus
  1512. * back into sane shape.
  1513. *
  1514. * Locks: caller holds queue lock
  1515. */
  1516. static void do_reset(struct Scsi_Host *host) {
  1517. NCR5380_local_declare();
  1518. NCR5380_setup(host);
  1519. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
  1520. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
  1521. udelay(25);
  1522. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1523. }
  1524. /*
  1525. * Function : do_abort (Scsi_Host *host)
  1526. *
  1527. * Purpose : abort the currently established nexus. Should only be
  1528. * called from a routine which can drop into a
  1529. *
  1530. * Returns : 0 on success, -1 on failure.
  1531. *
  1532. * Locks: queue lock held by caller
  1533. * FIXME: sort this out and get new_eh running
  1534. */
  1535. static int do_abort(struct Scsi_Host *host) {
  1536. NCR5380_local_declare();
  1537. unsigned char *msgptr, phase, tmp;
  1538. int len;
  1539. int rc;
  1540. NCR5380_setup(host);
  1541. /* Request message out phase */
  1542. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1543. /*
  1544. * Wait for the target to indicate a valid phase by asserting
  1545. * REQ. Once this happens, we'll have either a MSGOUT phase
  1546. * and can immediately send the ABORT message, or we'll have some
  1547. * other phase and will have to source/sink data.
  1548. *
  1549. * We really don't care what value was on the bus or what value
  1550. * the target sees, so we just handshake.
  1551. */
  1552. rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
  1553. if(rc < 0)
  1554. return -1;
  1555. tmp = (unsigned char)rc;
  1556. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1557. if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
  1558. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1559. rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, 0, 3*HZ);
  1560. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1561. if(rc == -1)
  1562. return -1;
  1563. }
  1564. tmp = ABORT;
  1565. msgptr = &tmp;
  1566. len = 1;
  1567. phase = PHASE_MSGOUT;
  1568. NCR5380_transfer_pio(host, &phase, &len, &msgptr);
  1569. /*
  1570. * If we got here, and the command completed successfully,
  1571. * we're about to go into bus free state.
  1572. */
  1573. return len ? -1 : 0;
  1574. }
  1575. #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
  1576. /*
  1577. * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
  1578. * unsigned char *phase, int *count, unsigned char **data)
  1579. *
  1580. * Purpose : transfers data in given phase using either real
  1581. * or pseudo DMA.
  1582. *
  1583. * Inputs : instance - instance of driver, *phase - pointer to
  1584. * what phase is expected, *count - pointer to number of
  1585. * bytes to transfer, **data - pointer to data pointer.
  1586. *
  1587. * Returns : -1 when different phase is entered without transferring
  1588. * maximum number of bytes, 0 if all bytes or transferred or exit
  1589. * is in same phase.
  1590. *
  1591. * Also, *phase, *count, *data are modified in place.
  1592. *
  1593. * Locks: io_request lock held by caller
  1594. */
  1595. static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
  1596. NCR5380_local_declare();
  1597. register int c = *count;
  1598. register unsigned char p = *phase;
  1599. register unsigned char *d = *data;
  1600. unsigned char tmp;
  1601. int foo;
  1602. #if defined(REAL_DMA_POLL)
  1603. int cnt, toPIO;
  1604. unsigned char saved_data = 0, overrun = 0, residue;
  1605. #endif
  1606. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1607. NCR5380_setup(instance);
  1608. if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
  1609. *phase = tmp;
  1610. return -1;
  1611. }
  1612. #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
  1613. #ifdef READ_OVERRUNS
  1614. if (p & SR_IO) {
  1615. c -= 2;
  1616. }
  1617. #endif
  1618. dprintk(NDEBUG_DMA, "scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
  1619. hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
  1620. #endif
  1621. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1622. #ifdef REAL_DMA
  1623. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
  1624. #elif defined(REAL_DMA_POLL)
  1625. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
  1626. #else
  1627. /*
  1628. * Note : on my sample board, watch-dog timeouts occurred when interrupts
  1629. * were not disabled for the duration of a single DMA transfer, from
  1630. * before the setting of DMA mode to after transfer of the last byte.
  1631. */
  1632. #if defined(PSEUDO_DMA) && defined(UNSAFE)
  1633. spin_unlock_irq(instance->host_lock);
  1634. #endif
  1635. /* KLL May need eop and parity in 53c400 */
  1636. if (hostdata->flags & FLAG_NCR53C400)
  1637. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
  1638. MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
  1639. MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
  1640. else
  1641. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
  1642. #endif /* def REAL_DMA */
  1643. dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
  1644. /*
  1645. * On the PAS16 at least I/O recovery delays are not needed here.
  1646. * Everyone else seems to want them.
  1647. */
  1648. if (p & SR_IO) {
  1649. io_recovery_delay(1);
  1650. NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
  1651. } else {
  1652. io_recovery_delay(1);
  1653. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1654. io_recovery_delay(1);
  1655. NCR5380_write(START_DMA_SEND_REG, 0);
  1656. io_recovery_delay(1);
  1657. }
  1658. #if defined(REAL_DMA_POLL)
  1659. do {
  1660. tmp = NCR5380_read(BUS_AND_STATUS_REG);
  1661. } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
  1662. /*
  1663. At this point, either we've completed DMA, or we have a phase mismatch,
  1664. or we've unexpectedly lost BUSY (which is a real error).
  1665. For write DMAs, we want to wait until the last byte has been
  1666. transferred out over the bus before we turn off DMA mode. Alas, there
  1667. seems to be no terribly good way of doing this on a 5380 under all
  1668. conditions. For non-scatter-gather operations, we can wait until REQ
  1669. and ACK both go false, or until a phase mismatch occurs. Gather-writes
  1670. are nastier, since the device will be expecting more data than we
  1671. are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
  1672. could test LAST BIT SENT to assure transfer (I imagine this is precisely
  1673. why this signal was added to the newer chips) but on the older 538[01]
  1674. this signal does not exist. The workaround for this lack is a watchdog;
  1675. we bail out of the wait-loop after a modest amount of wait-time if
  1676. the usual exit conditions are not met. Not a terribly clean or
  1677. correct solution :-%
  1678. Reads are equally tricky due to a nasty characteristic of the NCR5380.
  1679. If the chip is in DMA mode for an READ, it will respond to a target's
  1680. REQ by latching the SCSI data into the INPUT DATA register and asserting
  1681. ACK, even if it has _already_ been notified by the DMA controller that
  1682. the current DMA transfer has completed! If the NCR5380 is then taken
  1683. out of DMA mode, this already-acknowledged byte is lost.
  1684. This is not a problem for "one DMA transfer per command" reads, because
  1685. the situation will never arise... either all of the data is DMA'ed
  1686. properly, or the target switches to MESSAGE IN phase to signal a
  1687. disconnection (either operation bringing the DMA to a clean halt).
  1688. However, in order to handle scatter-reads, we must work around the
  1689. problem. The chosen fix is to DMA N-2 bytes, then check for the
  1690. condition before taking the NCR5380 out of DMA mode. One or two extra
  1691. bytes are transferred via PIO as necessary to fill out the original
  1692. request.
  1693. */
  1694. if (p & SR_IO) {
  1695. #ifdef READ_OVERRUNS
  1696. udelay(10);
  1697. if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
  1698. saved_data = NCR5380_read(INPUT_DATA_REGISTER);
  1699. overrun = 1;
  1700. }
  1701. #endif
  1702. } else {
  1703. int limit = 100;
  1704. while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
  1705. if (!(tmp & BASR_PHASE_MATCH))
  1706. break;
  1707. if (--limit < 0)
  1708. break;
  1709. }
  1710. }
  1711. dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG));
  1712. NCR5380_write(MODE_REG, MR_BASE);
  1713. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1714. residue = NCR5380_dma_residual(instance);
  1715. c -= residue;
  1716. *count -= c;
  1717. *data += c;
  1718. *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
  1719. #ifdef READ_OVERRUNS
  1720. if (*phase == p && (p & SR_IO) && residue == 0) {
  1721. if (overrun) {
  1722. dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
  1723. **data = saved_data;
  1724. *data += 1;
  1725. *count -= 1;
  1726. cnt = toPIO = 1;
  1727. } else {
  1728. printk("No overrun??\n");
  1729. cnt = toPIO = 2;
  1730. }
  1731. dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
  1732. NCR5380_transfer_pio(instance, phase, &cnt, data);
  1733. *count -= toPIO - cnt;
  1734. }
  1735. #endif
  1736. dprintk(NDEBUG_DMA, "Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count));
  1737. return 0;
  1738. #elif defined(REAL_DMA)
  1739. return 0;
  1740. #else /* defined(REAL_DMA_POLL) */
  1741. if (p & SR_IO) {
  1742. #ifdef DMA_WORKS_RIGHT
  1743. foo = NCR5380_pread(instance, d, c);
  1744. #else
  1745. int diff = 1;
  1746. if (hostdata->flags & FLAG_NCR53C400) {
  1747. diff = 0;
  1748. }
  1749. if (!(foo = NCR5380_pread(instance, d, c - diff))) {
  1750. /*
  1751. * We can't disable DMA mode after successfully transferring
  1752. * what we plan to be the last byte, since that would open up
  1753. * a race condition where if the target asserted REQ before
  1754. * we got the DMA mode reset, the NCR5380 would have latched
  1755. * an additional byte into the INPUT DATA register and we'd
  1756. * have dropped it.
  1757. *
  1758. * The workaround was to transfer one fewer bytes than we
  1759. * intended to with the pseudo-DMA read function, wait for
  1760. * the chip to latch the last byte, read it, and then disable
  1761. * pseudo-DMA mode.
  1762. *
  1763. * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
  1764. * REQ is deasserted when ACK is asserted, and not reasserted
  1765. * until ACK goes false. Since the NCR5380 won't lower ACK
  1766. * until DACK is asserted, which won't happen unless we twiddle
  1767. * the DMA port or we take the NCR5380 out of DMA mode, we
  1768. * can guarantee that we won't handshake another extra
  1769. * byte.
  1770. */
  1771. if (!(hostdata->flags & FLAG_NCR53C400)) {
  1772. while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
  1773. /* Wait for clean handshake */
  1774. while (NCR5380_read(STATUS_REG) & SR_REQ);
  1775. d[c - 1] = NCR5380_read(INPUT_DATA_REG);
  1776. }
  1777. }
  1778. #endif
  1779. } else {
  1780. #ifdef DMA_WORKS_RIGHT
  1781. foo = NCR5380_pwrite(instance, d, c);
  1782. #else
  1783. int timeout;
  1784. dprintk(NDEBUG_C400_PWRITE, "About to pwrite %d bytes\n", c);
  1785. if (!(foo = NCR5380_pwrite(instance, d, c))) {
  1786. /*
  1787. * Wait for the last byte to be sent. If REQ is being asserted for
  1788. * the byte we're interested, we'll ACK it and it will go false.
  1789. */
  1790. if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
  1791. timeout = 20000;
  1792. while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
  1793. if (!timeout)
  1794. dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : timed out on last byte\n", instance->host_no);
  1795. if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
  1796. hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
  1797. if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
  1798. hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
  1799. dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : last byte sent works\n", instance->host_no);
  1800. }
  1801. }
  1802. } else {
  1803. dprintk(NDEBUG_C400_PWRITE, "Waiting for LASTBYTE\n");
  1804. while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
  1805. dprintk(NDEBUG_C400_PWRITE, "Got LASTBYTE\n");
  1806. }
  1807. }
  1808. #endif
  1809. }
  1810. NCR5380_write(MODE_REG, MR_BASE);
  1811. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1812. if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
  1813. dprintk(NDEBUG_C400_PWRITE, "53C400w: Checking for IRQ\n");
  1814. if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
  1815. dprintk(NDEBUG_C400_PWRITE, "53C400w: got it, reading reset interrupt reg\n");
  1816. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1817. } else {
  1818. printk("53C400w: IRQ NOT THERE!\n");
  1819. }
  1820. }
  1821. *data = d + c;
  1822. *count = 0;
  1823. *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
  1824. #if defined(PSEUDO_DMA) && defined(UNSAFE)
  1825. spin_lock_irq(instance->host_lock);
  1826. #endif /* defined(REAL_DMA_POLL) */
  1827. return foo;
  1828. #endif /* def REAL_DMA */
  1829. }
  1830. #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
  1831. /*
  1832. * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
  1833. *
  1834. * Purpose : run through the various SCSI phases and do as the target
  1835. * directs us to. Operates on the currently connected command,
  1836. * instance->connected.
  1837. *
  1838. * Inputs : instance, instance for which we are doing commands
  1839. *
  1840. * Side effects : SCSI things happen, the disconnected queue will be
  1841. * modified if a command disconnects, *instance->connected will
  1842. * change.
  1843. *
  1844. * XXX Note : we need to watch for bus free or a reset condition here
  1845. * to recover from an unexpected bus free condition.
  1846. *
  1847. * Locks: io_request_lock held by caller in IRQ mode
  1848. */
  1849. static void NCR5380_information_transfer(struct Scsi_Host *instance) {
  1850. NCR5380_local_declare();
  1851. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
  1852. unsigned char msgout = NOP;
  1853. int sink = 0;
  1854. int len;
  1855. #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
  1856. int transfersize;
  1857. #endif
  1858. unsigned char *data;
  1859. unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
  1860. Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
  1861. /* RvC: we need to set the end of the polling time */
  1862. unsigned long poll_time = jiffies + USLEEP_POLL;
  1863. NCR5380_setup(instance);
  1864. while (1) {
  1865. tmp = NCR5380_read(STATUS_REG);
  1866. /* We only have a valid SCSI phase when REQ is asserted */
  1867. if (tmp & SR_REQ) {
  1868. phase = (tmp & PHASE_MASK);
  1869. if (phase != old_phase) {
  1870. old_phase = phase;
  1871. NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
  1872. }
  1873. if (sink && (phase != PHASE_MSGOUT)) {
  1874. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1875. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1876. while (NCR5380_read(STATUS_REG) & SR_REQ);
  1877. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1878. sink = 0;
  1879. continue;
  1880. }
  1881. switch (phase) {
  1882. case PHASE_DATAIN:
  1883. case PHASE_DATAOUT:
  1884. #if (NDEBUG & NDEBUG_NO_DATAOUT)
  1885. printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
  1886. sink = 1;
  1887. do_abort(instance);
  1888. cmd->result = DID_ERROR << 16;
  1889. cmd->scsi_done(cmd);
  1890. return;
  1891. #endif
  1892. /*
  1893. * If there is no room left in the current buffer in the
  1894. * scatter-gather list, move onto the next one.
  1895. */
  1896. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  1897. ++cmd->SCp.buffer;
  1898. --cmd->SCp.buffers_residual;
  1899. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  1900. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  1901. dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual);
  1902. }
  1903. /*
  1904. * The preferred transfer method is going to be
  1905. * PSEUDO-DMA for systems that are strictly PIO,
  1906. * since we can let the hardware do the handshaking.
  1907. *
  1908. * For this to work, we need to know the transfersize
  1909. * ahead of time, since the pseudo-DMA code will sit
  1910. * in an unconditional loop.
  1911. */
  1912. #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
  1913. /* KLL
  1914. * PSEUDO_DMA is defined here. If this is the g_NCR5380
  1915. * driver then it will always be defined, so the
  1916. * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
  1917. * NCR5380 case. I think this is a fairly clean solution.
  1918. * We supplement these 2 if's with the flag.
  1919. */
  1920. #ifdef NCR5380_dma_xfer_len
  1921. if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
  1922. #else
  1923. transfersize = cmd->transfersize;
  1924. #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
  1925. if (transfersize > 512)
  1926. transfersize = 512;
  1927. #endif /* LIMIT_TRANSFERSIZE */
  1928. if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
  1929. /* Limit transfers to 32K, for xx400 & xx406
  1930. * pseudoDMA that transfers in 128 bytes blocks. */
  1931. if (transfersize > 32 * 1024)
  1932. transfersize = 32 * 1024;
  1933. #endif
  1934. len = transfersize;
  1935. if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
  1936. /*
  1937. * If the watchdog timer fires, all future accesses to this
  1938. * device will use the polled-IO.
  1939. */
  1940. scmd_printk(KERN_INFO, cmd,
  1941. "switching to slow handshake\n");
  1942. cmd->device->borken = 1;
  1943. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1944. sink = 1;
  1945. do_abort(instance);
  1946. cmd->result = DID_ERROR << 16;
  1947. cmd->scsi_done(cmd);
  1948. /* XXX - need to source or sink data here, as appropriate */
  1949. } else
  1950. cmd->SCp.this_residual -= transfersize - len;
  1951. } else
  1952. #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
  1953. NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
  1954. &cmd->SCp.ptr);
  1955. break;
  1956. case PHASE_MSGIN:
  1957. len = 1;
  1958. data = &tmp;
  1959. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1960. cmd->SCp.Message = tmp;
  1961. switch (tmp) {
  1962. /*
  1963. * Linking lets us reduce the time required to get the
  1964. * next command out to the device, hopefully this will
  1965. * mean we don't waste another revolution due to the delays
  1966. * required by ARBITRATION and another SELECTION.
  1967. *
  1968. * In the current implementation proposal, low level drivers
  1969. * merely have to start the next command, pointed to by
  1970. * next_link, done() is called as with unlinked commands.
  1971. */
  1972. #ifdef LINKED
  1973. case LINKED_CMD_COMPLETE:
  1974. case LINKED_FLG_CMD_COMPLETE:
  1975. /* Accept message by clearing ACK */
  1976. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1977. dprintk(NDEBUG_LINKED, "scsi%d : target %d lun %llu linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun);
  1978. /*
  1979. * Sanity check : A linked command should only terminate with
  1980. * one of these messages if there are more linked commands
  1981. * available.
  1982. */
  1983. if (!cmd->next_link) {
  1984. printk("scsi%d : target %d lun %llu linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun);
  1985. sink = 1;
  1986. do_abort(instance);
  1987. return;
  1988. }
  1989. initialize_SCp(cmd->next_link);
  1990. /* The next command is still part of this process */
  1991. cmd->next_link->tag = cmd->tag;
  1992. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1993. dprintk(NDEBUG_LINKED, "scsi%d : target %d lun %llu linked request done, calling scsi_done().\n", instance->host_no, cmd->device->id, cmd->device->lun);
  1994. collect_stats(hostdata, cmd);
  1995. cmd->scsi_done(cmd);
  1996. cmd = hostdata->connected;
  1997. break;
  1998. #endif /* def LINKED */
  1999. case ABORT:
  2000. case COMMAND_COMPLETE:
  2001. /* Accept message by clearing ACK */
  2002. sink = 1;
  2003. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2004. hostdata->connected = NULL;
  2005. dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
  2006. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
  2007. /*
  2008. * I'm not sure what the correct thing to do here is :
  2009. *
  2010. * If the command that just executed is NOT a request
  2011. * sense, the obvious thing to do is to set the result
  2012. * code to the values of the stored parameters.
  2013. *
  2014. * If it was a REQUEST SENSE command, we need some way
  2015. * to differentiate between the failure code of the original
  2016. * and the failure code of the REQUEST sense - the obvious
  2017. * case is success, where we fall through and leave the result
  2018. * code unchanged.
  2019. *
  2020. * The non-obvious place is where the REQUEST SENSE failed
  2021. */
  2022. if (cmd->cmnd[0] != REQUEST_SENSE)
  2023. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  2024. else if (status_byte(cmd->SCp.Status) != GOOD)
  2025. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  2026. #ifdef AUTOSENSE
  2027. if ((cmd->cmnd[0] == REQUEST_SENSE) &&
  2028. hostdata->ses.cmd_len) {
  2029. scsi_eh_restore_cmnd(cmd, &hostdata->ses);
  2030. hostdata->ses.cmd_len = 0 ;
  2031. }
  2032. if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
  2033. scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
  2034. dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
  2035. LIST(cmd, hostdata->issue_queue);
  2036. cmd->host_scribble = (unsigned char *)
  2037. hostdata->issue_queue;
  2038. hostdata->issue_queue = (Scsi_Cmnd *) cmd;
  2039. dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
  2040. } else
  2041. #endif /* def AUTOSENSE */
  2042. {
  2043. collect_stats(hostdata, cmd);
  2044. cmd->scsi_done(cmd);
  2045. }
  2046. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2047. /*
  2048. * Restore phase bits to 0 so an interrupted selection,
  2049. * arbitration can resume.
  2050. */
  2051. NCR5380_write(TARGET_COMMAND_REG, 0);
  2052. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2053. barrier();
  2054. return;
  2055. case MESSAGE_REJECT:
  2056. /* Accept message by clearing ACK */
  2057. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2058. switch (hostdata->last_message) {
  2059. case HEAD_OF_QUEUE_TAG:
  2060. case ORDERED_QUEUE_TAG:
  2061. case SIMPLE_QUEUE_TAG:
  2062. cmd->device->simple_tags = 0;
  2063. hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
  2064. break;
  2065. default:
  2066. break;
  2067. }
  2068. case DISCONNECT:{
  2069. /* Accept message by clearing ACK */
  2070. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2071. cmd->device->disconnect = 1;
  2072. LIST(cmd, hostdata->disconnected_queue);
  2073. cmd->host_scribble = (unsigned char *)
  2074. hostdata->disconnected_queue;
  2075. hostdata->connected = NULL;
  2076. hostdata->disconnected_queue = cmd;
  2077. dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d lun %llu was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun);
  2078. /*
  2079. * Restore phase bits to 0 so an interrupted selection,
  2080. * arbitration can resume.
  2081. */
  2082. NCR5380_write(TARGET_COMMAND_REG, 0);
  2083. /* Enable reselect interrupts */
  2084. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2085. /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
  2086. /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
  2087. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2088. barrier();
  2089. return;
  2090. }
  2091. /*
  2092. * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  2093. * operation, in violation of the SCSI spec so we can safely
  2094. * ignore SAVE/RESTORE pointers calls.
  2095. *
  2096. * Unfortunately, some disks violate the SCSI spec and
  2097. * don't issue the required SAVE_POINTERS message before
  2098. * disconnecting, and we have to break spec to remain
  2099. * compatible.
  2100. */
  2101. case SAVE_POINTERS:
  2102. case RESTORE_POINTERS:
  2103. /* Accept message by clearing ACK */
  2104. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2105. break;
  2106. case EXTENDED_MESSAGE:
  2107. /*
  2108. * Extended messages are sent in the following format :
  2109. * Byte
  2110. * 0 EXTENDED_MESSAGE == 1
  2111. * 1 length (includes one byte for code, doesn't
  2112. * include first two bytes)
  2113. * 2 code
  2114. * 3..length+1 arguments
  2115. *
  2116. * Start the extended message buffer with the EXTENDED_MESSAGE
  2117. * byte, since spi_print_msg() wants the whole thing.
  2118. */
  2119. extended_msg[0] = EXTENDED_MESSAGE;
  2120. /* Accept first byte by clearing ACK */
  2121. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2122. dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
  2123. len = 2;
  2124. data = extended_msg + 1;
  2125. phase = PHASE_MSGIN;
  2126. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2127. dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]);
  2128. if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
  2129. /* Accept third byte by clearing ACK */
  2130. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2131. len = extended_msg[1] - 1;
  2132. data = extended_msg + 3;
  2133. phase = PHASE_MSGIN;
  2134. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2135. dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
  2136. switch (extended_msg[2]) {
  2137. case EXTENDED_SDTR:
  2138. case EXTENDED_WDTR:
  2139. case EXTENDED_MODIFY_DATA_POINTER:
  2140. case EXTENDED_EXTENDED_IDENTIFY:
  2141. tmp = 0;
  2142. }
  2143. } else if (len) {
  2144. printk("scsi%d: error receiving extended message\n", instance->host_no);
  2145. tmp = 0;
  2146. } else {
  2147. printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
  2148. tmp = 0;
  2149. }
  2150. /* Fall through to reject message */
  2151. /*
  2152. * If we get something weird that we aren't expecting,
  2153. * reject it.
  2154. */
  2155. default:
  2156. if (!tmp) {
  2157. printk("scsi%d: rejecting message ", instance->host_no);
  2158. spi_print_msg(extended_msg);
  2159. printk("\n");
  2160. } else if (tmp != EXTENDED_MESSAGE)
  2161. scmd_printk(KERN_INFO, cmd,
  2162. "rejecting unknown message %02x\n",tmp);
  2163. else
  2164. scmd_printk(KERN_INFO, cmd,
  2165. "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
  2166. msgout = MESSAGE_REJECT;
  2167. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  2168. break;
  2169. } /* switch (tmp) */
  2170. break;
  2171. case PHASE_MSGOUT:
  2172. len = 1;
  2173. data = &msgout;
  2174. hostdata->last_message = msgout;
  2175. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2176. if (msgout == ABORT) {
  2177. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
  2178. hostdata->connected = NULL;
  2179. cmd->result = DID_ERROR << 16;
  2180. collect_stats(hostdata, cmd);
  2181. cmd->scsi_done(cmd);
  2182. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2183. return;
  2184. }
  2185. msgout = NOP;
  2186. break;
  2187. case PHASE_CMDOUT:
  2188. len = cmd->cmd_len;
  2189. data = cmd->cmnd;
  2190. /*
  2191. * XXX for performance reasons, on machines with a
  2192. * PSEUDO-DMA architecture we should probably
  2193. * use the dma transfer function.
  2194. */
  2195. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2196. if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
  2197. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  2198. dprintk(NDEBUG_USLEEP, "scsi%d : issued command, sleeping until %lu\n", instance->host_no, hostdata->time_expires);
  2199. return;
  2200. }
  2201. break;
  2202. case PHASE_STATIN:
  2203. len = 1;
  2204. data = &tmp;
  2205. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2206. cmd->SCp.Status = tmp;
  2207. break;
  2208. default:
  2209. printk("scsi%d : unknown phase\n", instance->host_no);
  2210. NCR5380_dprint(NDEBUG_ANY, instance);
  2211. } /* switch(phase) */
  2212. } /* if (tmp * SR_REQ) */
  2213. else {
  2214. /* RvC: go to sleep if polling time expired
  2215. */
  2216. if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
  2217. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  2218. dprintk(NDEBUG_USLEEP, "scsi%d : poll timed out, sleeping until %lu\n", instance->host_no, hostdata->time_expires);
  2219. return;
  2220. }
  2221. }
  2222. } /* while (1) */
  2223. }
  2224. /*
  2225. * Function : void NCR5380_reselect (struct Scsi_Host *instance)
  2226. *
  2227. * Purpose : does reselection, initializing the instance->connected
  2228. * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
  2229. * nexus has been reestablished,
  2230. *
  2231. * Inputs : instance - this instance of the NCR5380.
  2232. *
  2233. * Locks: io_request_lock held by caller if IRQ driven
  2234. */
  2235. static void NCR5380_reselect(struct Scsi_Host *instance) {
  2236. NCR5380_local_declare();
  2237. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
  2238. instance->hostdata;
  2239. unsigned char target_mask;
  2240. unsigned char lun, phase;
  2241. int len;
  2242. unsigned char msg[3];
  2243. unsigned char *data;
  2244. Scsi_Cmnd *tmp = NULL, *prev;
  2245. int abort = 0;
  2246. NCR5380_setup(instance);
  2247. /*
  2248. * Disable arbitration, etc. since the host adapter obviously
  2249. * lost, and tell an interrupted NCR5380_select() to restart.
  2250. */
  2251. NCR5380_write(MODE_REG, MR_BASE);
  2252. hostdata->restart_select = 1;
  2253. target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
  2254. dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
  2255. /*
  2256. * At this point, we have detected that our SCSI ID is on the bus,
  2257. * SEL is true and BSY was false for at least one bus settle delay
  2258. * (400 ns).
  2259. *
  2260. * We must assert BSY ourselves, until the target drops the SEL
  2261. * signal.
  2262. */
  2263. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
  2264. /* FIXME: timeout too long, must fail to workqueue */
  2265. if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
  2266. abort = 1;
  2267. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2268. /*
  2269. * Wait for target to go into MSGIN.
  2270. * FIXME: timeout needed and fail to work queeu
  2271. */
  2272. if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
  2273. abort = 1;
  2274. len = 1;
  2275. data = msg;
  2276. phase = PHASE_MSGIN;
  2277. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2278. if (!(msg[0] & 0x80)) {
  2279. printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
  2280. spi_print_msg(msg);
  2281. abort = 1;
  2282. } else {
  2283. /* Accept message by clearing ACK */
  2284. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2285. lun = (msg[0] & 0x07);
  2286. /*
  2287. * We need to add code for SCSI-II to track which devices have
  2288. * I_T_L_Q nexuses established, and which have simple I_T_L
  2289. * nexuses so we can chose to do additional data transfer.
  2290. */
  2291. /*
  2292. * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
  2293. * just reestablished, and remove it from the disconnected queue.
  2294. */
  2295. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2296. if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
  2297. ) {
  2298. if (prev) {
  2299. REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
  2300. prev->host_scribble = tmp->host_scribble;
  2301. } else {
  2302. REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
  2303. hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
  2304. }
  2305. tmp->host_scribble = NULL;
  2306. break;
  2307. }
  2308. if (!tmp) {
  2309. printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
  2310. /*
  2311. * Since we have an established nexus that we can't do anything with,
  2312. * we must abort it.
  2313. */
  2314. abort = 1;
  2315. }
  2316. }
  2317. if (abort) {
  2318. do_abort(instance);
  2319. } else {
  2320. hostdata->connected = tmp;
  2321. dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n", instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
  2322. }
  2323. }
  2324. /*
  2325. * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
  2326. *
  2327. * Purpose : called by interrupt handler when DMA finishes or a phase
  2328. * mismatch occurs (which would finish the DMA transfer).
  2329. *
  2330. * Inputs : instance - this instance of the NCR5380.
  2331. *
  2332. * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
  2333. * nexus has been reestablished, on failure NULL is returned.
  2334. */
  2335. #ifdef REAL_DMA
  2336. static void NCR5380_dma_complete(NCR5380_instance * instance) {
  2337. NCR5380_local_declare();
  2338. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  2339. int transferred;
  2340. NCR5380_setup(instance);
  2341. /*
  2342. * XXX this might not be right.
  2343. *
  2344. * Wait for final byte to transfer, ie wait for ACK to go false.
  2345. *
  2346. * We should use the Last Byte Sent bit, unfortunately this is
  2347. * not available on the 5380/5381 (only the various CMOS chips)
  2348. *
  2349. * FIXME: timeout, and need to handle long timeout/irq case
  2350. */
  2351. NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
  2352. NCR5380_write(MODE_REG, MR_BASE);
  2353. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2354. /*
  2355. * The only places we should see a phase mismatch and have to send
  2356. * data from the same set of pointers will be the data transfer
  2357. * phases. So, residual, requested length are only important here.
  2358. */
  2359. if (!(hostdata->connected->SCp.phase & SR_CD)) {
  2360. transferred = instance->dmalen - NCR5380_dma_residual();
  2361. hostdata->connected->SCp.this_residual -= transferred;
  2362. hostdata->connected->SCp.ptr += transferred;
  2363. }
  2364. }
  2365. #endif /* def REAL_DMA */
  2366. /*
  2367. * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
  2368. *
  2369. * Purpose : abort a command
  2370. *
  2371. * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
  2372. * host byte of the result field to, if zero DID_ABORTED is
  2373. * used.
  2374. *
  2375. * Returns : SUCCESS - success, FAILED on failure.
  2376. *
  2377. * XXX - there is no way to abort the command that is currently
  2378. * connected, you have to wait for it to complete. If this is
  2379. * a problem, we could implement longjmp() / setjmp(), setjmp()
  2380. * called where the loop started in NCR5380_main().
  2381. *
  2382. * Locks: host lock taken by caller
  2383. */
  2384. static int NCR5380_abort(Scsi_Cmnd * cmd) {
  2385. NCR5380_local_declare();
  2386. struct Scsi_Host *instance = cmd->device->host;
  2387. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  2388. Scsi_Cmnd *tmp, **prev;
  2389. printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no);
  2390. scsi_print_command(cmd);
  2391. NCR5380_print_status(instance);
  2392. NCR5380_setup(instance);
  2393. dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
  2394. dprintk(NDEBUG_ABORT, " basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
  2395. #if 0
  2396. /*
  2397. * Case 1 : If the command is the currently executing command,
  2398. * we'll set the aborted flag and return control so that
  2399. * information transfer routine can exit cleanly.
  2400. */
  2401. if (hostdata->connected == cmd) {
  2402. dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
  2403. hostdata->aborted = 1;
  2404. /*
  2405. * We should perform BSY checking, and make sure we haven't slipped
  2406. * into BUS FREE.
  2407. */
  2408. NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
  2409. /*
  2410. * Since we can't change phases until we've completed the current
  2411. * handshake, we have to source or sink a byte of data if the current
  2412. * phase is not MSGOUT.
  2413. */
  2414. /*
  2415. * Return control to the executing NCR drive so we can clear the
  2416. * aborted flag and get back into our main loop.
  2417. */
  2418. return SUCCESS;
  2419. }
  2420. #endif
  2421. /*
  2422. * Case 2 : If the command hasn't been issued yet, we simply remove it
  2423. * from the issue queue.
  2424. */
  2425. dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
  2426. for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue), tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2427. if (cmd == tmp) {
  2428. REMOVE(5, *prev, tmp, tmp->host_scribble);
  2429. (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
  2430. tmp->host_scribble = NULL;
  2431. tmp->result = DID_ABORT << 16;
  2432. dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
  2433. tmp->scsi_done(tmp);
  2434. return SUCCESS;
  2435. }
  2436. #if (NDEBUG & NDEBUG_ABORT)
  2437. /* KLL */
  2438. else if (prev == tmp)
  2439. printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
  2440. #endif
  2441. /*
  2442. * Case 3 : If any commands are connected, we're going to fail the abort
  2443. * and let the high level SCSI driver retry at a later time or
  2444. * issue a reset.
  2445. *
  2446. * Timeouts, and therefore aborted commands, will be highly unlikely
  2447. * and handling them cleanly in this situation would make the common
  2448. * case of noresets less efficient, and would pollute our code. So,
  2449. * we fail.
  2450. */
  2451. if (hostdata->connected) {
  2452. dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
  2453. return FAILED;
  2454. }
  2455. /*
  2456. * Case 4: If the command is currently disconnected from the bus, and
  2457. * there are no connected commands, we reconnect the I_T_L or
  2458. * I_T_L_Q nexus associated with it, go into message out, and send
  2459. * an abort message.
  2460. *
  2461. * This case is especially ugly. In order to reestablish the nexus, we
  2462. * need to call NCR5380_select(). The easiest way to implement this
  2463. * function was to abort if the bus was busy, and let the interrupt
  2464. * handler triggered on the SEL for reselect take care of lost arbitrations
  2465. * where necessary, meaning interrupts need to be enabled.
  2466. *
  2467. * When interrupts are enabled, the queues may change - so we
  2468. * can't remove it from the disconnected queue before selecting it
  2469. * because that could cause a failure in hashing the nexus if that
  2470. * device reselected.
  2471. *
  2472. * Since the queues may change, we can't use the pointers from when we
  2473. * first locate it.
  2474. *
  2475. * So, we must first locate the command, and if NCR5380_select()
  2476. * succeeds, then issue the abort, relocate the command and remove
  2477. * it from the disconnected queue.
  2478. */
  2479. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2480. if (cmd == tmp) {
  2481. dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
  2482. if (NCR5380_select(instance, cmd, (int) cmd->tag))
  2483. return FAILED;
  2484. dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
  2485. do_abort(instance);
  2486. for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue), tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2487. if (cmd == tmp) {
  2488. REMOVE(5, *prev, tmp, tmp->host_scribble);
  2489. *prev = (Scsi_Cmnd *) tmp->host_scribble;
  2490. tmp->host_scribble = NULL;
  2491. tmp->result = DID_ABORT << 16;
  2492. tmp->scsi_done(tmp);
  2493. return SUCCESS;
  2494. }
  2495. }
  2496. /*
  2497. * Case 5 : If we reached this point, the command was not found in any of
  2498. * the queues.
  2499. *
  2500. * We probably reached this point because of an unlikely race condition
  2501. * between the command completing successfully and the abortion code,
  2502. * so we won't panic, but we will notify the user in case something really
  2503. * broke.
  2504. */
  2505. printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
  2506. " before abortion\n", instance->host_no);
  2507. return FAILED;
  2508. }
  2509. /*
  2510. * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
  2511. *
  2512. * Purpose : reset the SCSI bus.
  2513. *
  2514. * Returns : SUCCESS
  2515. *
  2516. * Locks: host lock taken by caller
  2517. */
  2518. static int NCR5380_bus_reset(Scsi_Cmnd * cmd)
  2519. {
  2520. struct Scsi_Host *instance = cmd->device->host;
  2521. NCR5380_local_declare();
  2522. NCR5380_setup(instance);
  2523. NCR5380_print_status(instance);
  2524. spin_lock_irq(instance->host_lock);
  2525. do_reset(instance);
  2526. spin_unlock_irq(instance->host_lock);
  2527. return SUCCESS;
  2528. }