NCR5380.c 88 KB

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