atari_NCR5380.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  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. * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
  28. * this file, too:
  29. *
  30. * - Some of the debug statements were incorrect (undefined variables and the
  31. * like). I fixed that.
  32. *
  33. * - In information_transfer(), I think a #ifdef was wrong. Looking at the
  34. * possible DMA transfer size should also happen for REAL_DMA. I added this
  35. * in the #if statement.
  36. *
  37. * - When using real DMA, information_transfer() should return in a DATAOUT
  38. * phase after starting the DMA. It has nothing more to do.
  39. *
  40. * - The interrupt service routine should run main after end of DMA, too (not
  41. * only after RESELECTION interrupts). Additionally, it should _not_ test
  42. * for more interrupts after running main, since a DMA process may have
  43. * been started and interrupts are turned on now. The new int could happen
  44. * inside the execution of NCR5380_intr(), leading to recursive
  45. * calls.
  46. *
  47. * - I've added a function merge_contiguous_buffers() that tries to
  48. * merge scatter-gather buffers that are located at contiguous
  49. * physical addresses and can be processed with the same DMA setup.
  50. * Since most scatter-gather operations work on a page (4K) of
  51. * 4 buffers (1K), in more than 90% of all cases three interrupts and
  52. * DMA setup actions are saved.
  53. *
  54. * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
  55. * and USLEEP, because these were messing up readability and will never be
  56. * needed for Atari SCSI.
  57. *
  58. * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
  59. * stuff), and 'main' is executed in a bottom half if awoken by an
  60. * interrupt.
  61. *
  62. * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
  63. * constructs. In my eyes, this made the source rather unreadable, so I
  64. * finally replaced that by the *_PRINTK() macros.
  65. *
  66. */
  67. /*
  68. * Further development / testing that should be done :
  69. * 1. Test linked command handling code after Eric is ready with
  70. * the high level code.
  71. */
  72. /* Adapted for the sun3 by Sam Creasey. */
  73. #include <scsi/scsi_dbg.h>
  74. #include <scsi/scsi_transport_spi.h>
  75. #if (NDEBUG & NDEBUG_LISTS)
  76. #define LIST(x, y) \
  77. do { \
  78. printk("LINE:%d Adding %p to %p\n", \
  79. __LINE__, (void*)(x), (void*)(y)); \
  80. if ((x) == (y)) \
  81. udelay(5); \
  82. } while (0)
  83. #define REMOVE(w, x, y, z) \
  84. do { \
  85. printk("LINE:%d Removing: %p->%p %p->%p \n", \
  86. __LINE__, (void*)(w), (void*)(x), \
  87. (void*)(y), (void*)(z)); \
  88. if ((x) == (y)) \
  89. udelay(5); \
  90. } while (0)
  91. #else
  92. #define LIST(x,y)
  93. #define REMOVE(w,x,y,z)
  94. #endif
  95. #ifndef notyet
  96. #undef LINKED
  97. #endif
  98. /*
  99. * Design
  100. *
  101. * This is a generic 5380 driver. To use it on a different platform,
  102. * one simply writes appropriate system specific macros (ie, data
  103. * transfer - some PC's will use the I/O bus, 68K's must use
  104. * memory mapped) and drops this file in their 'C' wrapper.
  105. *
  106. * As far as command queueing, two queues are maintained for
  107. * each 5380 in the system - commands that haven't been issued yet,
  108. * and commands that are currently executing. This means that an
  109. * unlimited number of commands may be queued, letting
  110. * more commands propagate from the higher driver levels giving higher
  111. * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
  112. * allowing multiple commands to propagate all the way to a SCSI-II device
  113. * while a command is already executing.
  114. *
  115. *
  116. * Issues specific to the NCR5380 :
  117. *
  118. * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
  119. * piece of hardware that requires you to sit in a loop polling for
  120. * the REQ signal as long as you are connected. Some devices are
  121. * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
  122. * while doing long seek operations.
  123. *
  124. * The workaround for this is to keep track of devices that have
  125. * disconnected. If the device hasn't disconnected, for commands that
  126. * should disconnect, we do something like
  127. *
  128. * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
  129. *
  130. * Some tweaking of N and M needs to be done. An algorithm based
  131. * on "time to data" would give the best results as long as short time
  132. * to datas (ie, on the same track) were considered, however these
  133. * broken devices are the exception rather than the rule and I'd rather
  134. * spend my time optimizing for the normal case.
  135. *
  136. * Architecture :
  137. *
  138. * At the heart of the design is a coroutine, NCR5380_main,
  139. * which is started from a workqueue for each NCR5380 host in the
  140. * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
  141. * removing the commands from the issue queue and calling
  142. * NCR5380_select() if a nexus is not established.
  143. *
  144. * Once a nexus is established, the NCR5380_information_transfer()
  145. * phase goes through the various phases as instructed by the target.
  146. * if the target goes into MSG IN and sends a DISCONNECT message,
  147. * the command structure is placed into the per instance disconnected
  148. * queue, and NCR5380_main tries to find more work. If the target is
  149. * idle for too long, the system will try to sleep.
  150. *
  151. * If a command has disconnected, eventually an interrupt will trigger,
  152. * calling NCR5380_intr() which will in turn call NCR5380_reselect
  153. * to reestablish a nexus. This will run main if necessary.
  154. *
  155. * On command termination, the done function will be called as
  156. * appropriate.
  157. *
  158. * SCSI pointers are maintained in the SCp field of SCSI command
  159. * structures, being initialized after the command is connected
  160. * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
  161. * Note that in violation of the standard, an implicit SAVE POINTERS operation
  162. * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
  163. */
  164. /*
  165. * Using this file :
  166. * This file a skeleton Linux SCSI driver for the NCR 5380 series
  167. * of chips. To use it, you write an architecture specific functions
  168. * and macros and include this file in your driver.
  169. *
  170. * These macros control options :
  171. * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
  172. * for commands that return with a CHECK CONDITION status.
  173. *
  174. * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
  175. * transceivers.
  176. *
  177. * LINKED - if defined, linked commands are supported.
  178. *
  179. * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
  180. *
  181. * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
  182. *
  183. * These macros MUST be defined :
  184. *
  185. * NCR5380_read(register) - read from the specified register
  186. *
  187. * NCR5380_write(register, value) - write to the specific register
  188. *
  189. * NCR5380_implementation_fields - additional fields needed for this
  190. * specific implementation of the NCR5380
  191. *
  192. * Either real DMA *or* pseudo DMA may be implemented
  193. * REAL functions :
  194. * NCR5380_REAL_DMA should be defined if real DMA is to be used.
  195. * Note that the DMA setup functions should return the number of bytes
  196. * that they were able to program the controller for.
  197. *
  198. * Also note that generic i386/PC versions of these macros are
  199. * available as NCR5380_i386_dma_write_setup,
  200. * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
  201. *
  202. * NCR5380_dma_write_setup(instance, src, count) - initialize
  203. * NCR5380_dma_read_setup(instance, dst, count) - initialize
  204. * NCR5380_dma_residual(instance); - residual count
  205. *
  206. * PSEUDO functions :
  207. * NCR5380_pwrite(instance, src, count)
  208. * NCR5380_pread(instance, dst, count);
  209. *
  210. * The generic driver is initialized by calling NCR5380_init(instance),
  211. * after setting the appropriate host specific fields and ID. If the
  212. * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
  213. * possible) function may be used.
  214. */
  215. /* Macros ease life... :-) */
  216. #define SETUP_HOSTDATA(in) \
  217. struct NCR5380_hostdata *hostdata = \
  218. (struct NCR5380_hostdata *)(in)->hostdata
  219. #define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
  220. #define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
  221. #define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
  222. #define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
  223. #define HOSTNO instance->host_no
  224. #define H_NO(cmd) (cmd)->device->host->host_no
  225. #ifdef SUPPORT_TAGS
  226. /*
  227. * Functions for handling tagged queuing
  228. * =====================================
  229. *
  230. * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
  231. *
  232. * Using consecutive numbers for the tags is no good idea in my eyes. There
  233. * could be wrong re-usings if the counter (8 bit!) wraps and some early
  234. * command has been preempted for a long time. My solution: a bitfield for
  235. * remembering used tags.
  236. *
  237. * There's also the problem that each target has a certain queue size, but we
  238. * cannot know it in advance :-( We just see a QUEUE_FULL status being
  239. * returned. So, in this case, the driver internal queue size assumption is
  240. * reduced to the number of active tags if QUEUE_FULL is returned by the
  241. * target. The command is returned to the mid-level, but with status changed
  242. * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
  243. * correctly.
  244. *
  245. * We're also not allowed running tagged commands as long as an untagged
  246. * command is active. And REQUEST SENSE commands after a contingent allegiance
  247. * condition _must_ be untagged. To keep track whether an untagged command has
  248. * been issued, the host->busy array is still employed, as it is without
  249. * support for tagged queuing.
  250. *
  251. * One could suspect that there are possible race conditions between
  252. * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
  253. * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
  254. * which already guaranteed to be running at most once. It is also the only
  255. * place where tags/LUNs are allocated. So no other allocation can slip
  256. * between that pair, there could only happen a reselection, which can free a
  257. * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
  258. * important: the tag bit must be cleared before 'nr_allocated' is decreased.
  259. */
  260. static void __init init_tags(struct NCR5380_hostdata *hostdata)
  261. {
  262. int target, lun;
  263. struct tag_alloc *ta;
  264. if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
  265. return;
  266. for (target = 0; target < 8; ++target) {
  267. for (lun = 0; lun < 8; ++lun) {
  268. ta = &hostdata->TagAlloc[target][lun];
  269. bitmap_zero(ta->allocated, MAX_TAGS);
  270. ta->nr_allocated = 0;
  271. /* At the beginning, assume the maximum queue size we could
  272. * support (MAX_TAGS). This value will be decreased if the target
  273. * returns QUEUE_FULL status.
  274. */
  275. ta->queue_size = MAX_TAGS;
  276. }
  277. }
  278. }
  279. /* Check if we can issue a command to this LUN: First see if the LUN is marked
  280. * busy by an untagged command. If the command should use tagged queuing, also
  281. * check that there is a free tag and the target's queue won't overflow. This
  282. * function should be called with interrupts disabled to avoid race
  283. * conditions.
  284. */
  285. static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
  286. {
  287. u8 lun = cmd->device->lun;
  288. SETUP_HOSTDATA(cmd->device->host);
  289. if (hostdata->busy[cmd->device->id] & (1 << lun))
  290. return 1;
  291. if (!should_be_tagged ||
  292. !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
  293. !cmd->device->tagged_supported)
  294. return 0;
  295. if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
  296. hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
  297. dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n",
  298. H_NO(cmd), cmd->device->id, lun);
  299. return 1;
  300. }
  301. return 0;
  302. }
  303. /* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
  304. * must be called before!), or reserve the LUN in 'busy' if the command is
  305. * untagged.
  306. */
  307. static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
  308. {
  309. u8 lun = cmd->device->lun;
  310. SETUP_HOSTDATA(cmd->device->host);
  311. /* If we or the target don't support tagged queuing, allocate the LUN for
  312. * an untagged command.
  313. */
  314. if (!should_be_tagged ||
  315. !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
  316. !cmd->device->tagged_supported) {
  317. cmd->tag = TAG_NONE;
  318. hostdata->busy[cmd->device->id] |= (1 << lun);
  319. dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged "
  320. "command\n", H_NO(cmd), cmd->device->id, lun);
  321. } else {
  322. struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
  323. cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
  324. set_bit(cmd->tag, ta->allocated);
  325. ta->nr_allocated++;
  326. dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d "
  327. "(now %d tags in use)\n",
  328. H_NO(cmd), cmd->tag, cmd->device->id,
  329. lun, ta->nr_allocated);
  330. }
  331. }
  332. /* Mark the tag of command 'cmd' as free, or in case of an untagged command,
  333. * unlock the LUN.
  334. */
  335. static void cmd_free_tag(struct scsi_cmnd *cmd)
  336. {
  337. u8 lun = cmd->device->lun;
  338. SETUP_HOSTDATA(cmd->device->host);
  339. if (cmd->tag == TAG_NONE) {
  340. hostdata->busy[cmd->device->id] &= ~(1 << lun);
  341. dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n",
  342. H_NO(cmd), cmd->device->id, lun);
  343. } else if (cmd->tag >= MAX_TAGS) {
  344. printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
  345. H_NO(cmd), cmd->tag);
  346. } else {
  347. struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
  348. clear_bit(cmd->tag, ta->allocated);
  349. ta->nr_allocated--;
  350. dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n",
  351. H_NO(cmd), cmd->tag, cmd->device->id, lun);
  352. }
  353. }
  354. static void free_all_tags(struct NCR5380_hostdata *hostdata)
  355. {
  356. int target, lun;
  357. struct tag_alloc *ta;
  358. if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
  359. return;
  360. for (target = 0; target < 8; ++target) {
  361. for (lun = 0; lun < 8; ++lun) {
  362. ta = &hostdata->TagAlloc[target][lun];
  363. bitmap_zero(ta->allocated, MAX_TAGS);
  364. ta->nr_allocated = 0;
  365. }
  366. }
  367. }
  368. #endif /* SUPPORT_TAGS */
  369. /*
  370. * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
  371. *
  372. * Purpose: Try to merge several scatter-gather requests into one DMA
  373. * transfer. This is possible if the scatter buffers lie on
  374. * physical contiguous addresses.
  375. *
  376. * Parameters: struct scsi_cmnd *cmd
  377. * The command to work on. The first scatter buffer's data are
  378. * assumed to be already transferred into ptr/this_residual.
  379. */
  380. static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
  381. {
  382. #if !defined(CONFIG_SUN3)
  383. unsigned long endaddr;
  384. #if (NDEBUG & NDEBUG_MERGING)
  385. unsigned long oldlen = cmd->SCp.this_residual;
  386. int cnt = 1;
  387. #endif
  388. for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
  389. cmd->SCp.buffers_residual &&
  390. virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
  391. dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
  392. page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
  393. #if (NDEBUG & NDEBUG_MERGING)
  394. ++cnt;
  395. #endif
  396. ++cmd->SCp.buffer;
  397. --cmd->SCp.buffers_residual;
  398. cmd->SCp.this_residual += cmd->SCp.buffer->length;
  399. endaddr += cmd->SCp.buffer->length;
  400. }
  401. #if (NDEBUG & NDEBUG_MERGING)
  402. if (oldlen != cmd->SCp.this_residual)
  403. dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
  404. cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
  405. #endif
  406. #endif /* !defined(CONFIG_SUN3) */
  407. }
  408. /**
  409. * initialize_SCp - init the scsi pointer field
  410. * @cmd: command block to set up
  411. *
  412. * Set up the internal fields in the SCSI command.
  413. */
  414. static inline void initialize_SCp(struct scsi_cmnd *cmd)
  415. {
  416. /*
  417. * Initialize the Scsi Pointer field so that all of the commands in the
  418. * various queues are valid.
  419. */
  420. if (scsi_bufflen(cmd)) {
  421. cmd->SCp.buffer = scsi_sglist(cmd);
  422. cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
  423. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  424. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  425. /* ++roman: Try to merge some scatter-buffers if they are at
  426. * contiguous physical addresses.
  427. */
  428. merge_contiguous_buffers(cmd);
  429. } else {
  430. cmd->SCp.buffer = NULL;
  431. cmd->SCp.buffers_residual = 0;
  432. cmd->SCp.ptr = NULL;
  433. cmd->SCp.this_residual = 0;
  434. }
  435. }
  436. #include <linux/delay.h>
  437. #if NDEBUG
  438. static struct {
  439. unsigned char mask;
  440. const char *name;
  441. } signals[] = {
  442. { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
  443. { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
  444. { SR_SEL, "SEL" }, {0, NULL}
  445. }, basrs[] = {
  446. {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
  447. }, icrs[] = {
  448. {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
  449. {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
  450. {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
  451. {0, NULL}
  452. }, mrs[] = {
  453. {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
  454. {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
  455. "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
  456. {MR_MONITOR_BSY, "MODE MONITOR BSY"},
  457. {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
  458. {0, NULL}
  459. };
  460. /**
  461. * NCR5380_print - print scsi bus signals
  462. * @instance: adapter state to dump
  463. *
  464. * Print the SCSI bus signals for debugging purposes
  465. */
  466. static void NCR5380_print(struct Scsi_Host *instance)
  467. {
  468. unsigned char status, data, basr, mr, icr, i;
  469. unsigned long flags;
  470. local_irq_save(flags);
  471. data = NCR5380_read(CURRENT_SCSI_DATA_REG);
  472. status = NCR5380_read(STATUS_REG);
  473. mr = NCR5380_read(MODE_REG);
  474. icr = NCR5380_read(INITIATOR_COMMAND_REG);
  475. basr = NCR5380_read(BUS_AND_STATUS_REG);
  476. local_irq_restore(flags);
  477. printk("STATUS_REG: %02x ", status);
  478. for (i = 0; signals[i].mask; ++i)
  479. if (status & signals[i].mask)
  480. printk(",%s", signals[i].name);
  481. printk("\nBASR: %02x ", basr);
  482. for (i = 0; basrs[i].mask; ++i)
  483. if (basr & basrs[i].mask)
  484. printk(",%s", basrs[i].name);
  485. printk("\nICR: %02x ", icr);
  486. for (i = 0; icrs[i].mask; ++i)
  487. if (icr & icrs[i].mask)
  488. printk(",%s", icrs[i].name);
  489. printk("\nMODE: %02x ", mr);
  490. for (i = 0; mrs[i].mask; ++i)
  491. if (mr & mrs[i].mask)
  492. printk(",%s", mrs[i].name);
  493. printk("\n");
  494. }
  495. static struct {
  496. unsigned char value;
  497. const char *name;
  498. } phases[] = {
  499. {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
  500. {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
  501. {PHASE_UNKNOWN, "UNKNOWN"}
  502. };
  503. /**
  504. * NCR5380_print_phase - show SCSI phase
  505. * @instance: adapter to dump
  506. *
  507. * Print the current SCSI phase for debugging purposes
  508. *
  509. * Locks: none
  510. */
  511. static void NCR5380_print_phase(struct Scsi_Host *instance)
  512. {
  513. unsigned char status;
  514. int i;
  515. status = NCR5380_read(STATUS_REG);
  516. if (!(status & SR_REQ))
  517. printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
  518. else {
  519. for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
  520. (phases[i].value != (status & PHASE_MASK)); ++i)
  521. ;
  522. printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
  523. }
  524. }
  525. #endif
  526. /*
  527. * ++roman: New scheme of calling NCR5380_main()
  528. *
  529. * If we're not in an interrupt, we can call our main directly, it cannot be
  530. * already running. Else, we queue it on a task queue, if not 'main_running'
  531. * tells us that a lower level is already executing it. This way,
  532. * 'main_running' needs not be protected in a special way.
  533. *
  534. * queue_main() is a utility function for putting our main onto the task
  535. * queue, if main_running is false. It should be called only from a
  536. * interrupt or bottom half.
  537. */
  538. #include <linux/gfp.h>
  539. #include <linux/workqueue.h>
  540. #include <linux/interrupt.h>
  541. static inline void queue_main(struct NCR5380_hostdata *hostdata)
  542. {
  543. if (!hostdata->main_running) {
  544. /* If in interrupt and NCR5380_main() not already running,
  545. queue it on the 'immediate' task queue, to be processed
  546. immediately after the current interrupt processing has
  547. finished. */
  548. schedule_work(&hostdata->main_task);
  549. }
  550. /* else: nothing to do: the running NCR5380_main() will pick up
  551. any newly queued command. */
  552. }
  553. /**
  554. * NCR58380_info - report driver and host information
  555. * @instance: relevant scsi host instance
  556. *
  557. * For use as the host template info() handler.
  558. *
  559. * Locks: none
  560. */
  561. static const char *NCR5380_info(struct Scsi_Host *instance)
  562. {
  563. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  564. return hostdata->info;
  565. }
  566. static void prepare_info(struct Scsi_Host *instance)
  567. {
  568. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  569. snprintf(hostdata->info, sizeof(hostdata->info),
  570. "%s, io_port 0x%lx, n_io_port %d, "
  571. "base 0x%lx, irq %d, "
  572. "can_queue %d, cmd_per_lun %d, "
  573. "sg_tablesize %d, this_id %d, "
  574. "flags { %s}, "
  575. "options { %s} ",
  576. instance->hostt->name, instance->io_port, instance->n_io_port,
  577. instance->base, instance->irq,
  578. instance->can_queue, instance->cmd_per_lun,
  579. instance->sg_tablesize, instance->this_id,
  580. hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
  581. #ifdef DIFFERENTIAL
  582. "DIFFERENTIAL "
  583. #endif
  584. #ifdef REAL_DMA
  585. "REAL_DMA "
  586. #endif
  587. #ifdef PARITY
  588. "PARITY "
  589. #endif
  590. #ifdef SUPPORT_TAGS
  591. "SUPPORT_TAGS "
  592. #endif
  593. "");
  594. }
  595. /**
  596. * NCR5380_print_status - dump controller info
  597. * @instance: controller to dump
  598. *
  599. * Print commands in the various queues, called from NCR5380_abort
  600. * to aid debugging.
  601. */
  602. static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd)
  603. {
  604. int i, s;
  605. unsigned char *command;
  606. printk("scsi%d: destination target %d, lun %llu\n",
  607. H_NO(cmd), cmd->device->id, cmd->device->lun);
  608. printk(KERN_CONT " command = ");
  609. command = cmd->cmnd;
  610. printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]);
  611. for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
  612. printk(KERN_CONT " %02x", command[i]);
  613. printk("\n");
  614. }
  615. static void NCR5380_print_status(struct Scsi_Host *instance)
  616. {
  617. struct NCR5380_hostdata *hostdata;
  618. struct scsi_cmnd *ptr;
  619. unsigned long flags;
  620. NCR5380_dprint(NDEBUG_ANY, instance);
  621. NCR5380_dprint_phase(NDEBUG_ANY, instance);
  622. hostdata = (struct NCR5380_hostdata *)instance->hostdata;
  623. local_irq_save(flags);
  624. printk("NCR5380: coroutine is%s running.\n",
  625. hostdata->main_running ? "" : "n't");
  626. if (!hostdata->connected)
  627. printk("scsi%d: no currently connected command\n", HOSTNO);
  628. else
  629. lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected);
  630. printk("scsi%d: issue_queue\n", HOSTNO);
  631. for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
  632. lprint_Scsi_Cmnd(ptr);
  633. printk("scsi%d: disconnected_queue\n", HOSTNO);
  634. for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
  635. ptr = NEXT(ptr))
  636. lprint_Scsi_Cmnd(ptr);
  637. local_irq_restore(flags);
  638. printk("\n");
  639. }
  640. static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
  641. {
  642. int i, s;
  643. unsigned char *command;
  644. seq_printf(m, "scsi%d: destination target %d, lun %llu\n",
  645. H_NO(cmd), cmd->device->id, cmd->device->lun);
  646. seq_puts(m, " command = ");
  647. command = cmd->cmnd;
  648. seq_printf(m, "%2d (0x%02x)", command[0], command[0]);
  649. for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
  650. seq_printf(m, " %02x", command[i]);
  651. seq_putc(m, '\n');
  652. }
  653. static int __maybe_unused NCR5380_show_info(struct seq_file *m,
  654. struct Scsi_Host *instance)
  655. {
  656. struct NCR5380_hostdata *hostdata;
  657. struct scsi_cmnd *ptr;
  658. unsigned long flags;
  659. hostdata = (struct NCR5380_hostdata *)instance->hostdata;
  660. local_irq_save(flags);
  661. seq_printf(m, "NCR5380: coroutine is%s running.\n",
  662. hostdata->main_running ? "" : "n't");
  663. if (!hostdata->connected)
  664. seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO);
  665. else
  666. show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
  667. seq_printf(m, "scsi%d: issue_queue\n", HOSTNO);
  668. for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr))
  669. show_Scsi_Cmnd(ptr, m);
  670. seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO);
  671. for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr;
  672. ptr = NEXT(ptr))
  673. show_Scsi_Cmnd(ptr, m);
  674. local_irq_restore(flags);
  675. return 0;
  676. }
  677. /**
  678. * NCR5380_init - initialise an NCR5380
  679. * @instance: adapter to configure
  680. * @flags: control flags
  681. *
  682. * Initializes *instance and corresponding 5380 chip,
  683. * with flags OR'd into the initial flags value.
  684. *
  685. * Notes : I assume that the host, hostno, and id bits have been
  686. * set correctly. I don't care about the irq and other fields.
  687. *
  688. * Returns 0 for success
  689. */
  690. static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
  691. {
  692. int i;
  693. SETUP_HOSTDATA(instance);
  694. hostdata->host = instance;
  695. hostdata->aborted = 0;
  696. hostdata->id_mask = 1 << instance->this_id;
  697. hostdata->id_higher_mask = 0;
  698. for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
  699. if (i > hostdata->id_mask)
  700. hostdata->id_higher_mask |= i;
  701. for (i = 0; i < 8; ++i)
  702. hostdata->busy[i] = 0;
  703. #ifdef SUPPORT_TAGS
  704. init_tags(hostdata);
  705. #endif
  706. #if defined (REAL_DMA)
  707. hostdata->dma_len = 0;
  708. #endif
  709. hostdata->targets_present = 0;
  710. hostdata->connected = NULL;
  711. hostdata->issue_queue = NULL;
  712. hostdata->disconnected_queue = NULL;
  713. hostdata->flags = flags;
  714. INIT_WORK(&hostdata->main_task, NCR5380_main);
  715. prepare_info(instance);
  716. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  717. NCR5380_write(MODE_REG, MR_BASE);
  718. NCR5380_write(TARGET_COMMAND_REG, 0);
  719. NCR5380_write(SELECT_ENABLE_REG, 0);
  720. return 0;
  721. }
  722. /**
  723. * NCR5380_exit - remove an NCR5380
  724. * @instance: adapter to remove
  725. *
  726. * Assumes that no more work can be queued (e.g. by NCR5380_intr).
  727. */
  728. static void NCR5380_exit(struct Scsi_Host *instance)
  729. {
  730. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  731. cancel_work_sync(&hostdata->main_task);
  732. }
  733. /**
  734. * NCR5380_queue_command - queue a command
  735. * @instance: the relevant SCSI adapter
  736. * @cmd: SCSI command
  737. *
  738. * cmd is added to the per instance issue_queue, with minor
  739. * twiddling done to the host specific fields of cmd. If the
  740. * main coroutine is not running, it is restarted.
  741. */
  742. static int NCR5380_queue_command(struct Scsi_Host *instance,
  743. struct scsi_cmnd *cmd)
  744. {
  745. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  746. struct scsi_cmnd *tmp;
  747. unsigned long flags;
  748. #if (NDEBUG & NDEBUG_NO_WRITE)
  749. switch (cmd->cmnd[0]) {
  750. case WRITE_6:
  751. case WRITE_10:
  752. printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
  753. H_NO(cmd));
  754. cmd->result = (DID_ERROR << 16);
  755. cmd->scsi_done(cmd);
  756. return 0;
  757. }
  758. #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
  759. /*
  760. * We use the host_scribble field as a pointer to the next command
  761. * in a queue
  762. */
  763. SET_NEXT(cmd, NULL);
  764. cmd->result = 0;
  765. /*
  766. * Insert the cmd into the issue queue. Note that REQUEST SENSE
  767. * commands are added to the head of the queue since any command will
  768. * clear the contingent allegiance condition that exists and the
  769. * sense data is only guaranteed to be valid while the condition exists.
  770. */
  771. /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
  772. * Otherwise a running NCR5380_main may steal the lock.
  773. * Lock before actually inserting due to fairness reasons explained in
  774. * atari_scsi.c. If we insert first, then it's impossible for this driver
  775. * to release the lock.
  776. * Stop timer for this command while waiting for the lock, or timeouts
  777. * may happen (and they really do), and it's no good if the command doesn't
  778. * appear in any of the queues.
  779. * ++roman: Just disabling the NCR interrupt isn't sufficient here,
  780. * because also a timer int can trigger an abort or reset, which would
  781. * alter queues and touch the lock.
  782. */
  783. if (!NCR5380_acquire_dma_irq(instance))
  784. return SCSI_MLQUEUE_HOST_BUSY;
  785. local_irq_save(flags);
  786. /*
  787. * Insert the cmd into the issue queue. Note that REQUEST SENSE
  788. * commands are added to the head of the queue since any command will
  789. * clear the contingent allegiance condition that exists and the
  790. * sense data is only guaranteed to be valid while the condition exists.
  791. */
  792. if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  793. LIST(cmd, hostdata->issue_queue);
  794. SET_NEXT(cmd, hostdata->issue_queue);
  795. hostdata->issue_queue = cmd;
  796. } else {
  797. for (tmp = (struct scsi_cmnd *)hostdata->issue_queue;
  798. NEXT(tmp); tmp = NEXT(tmp))
  799. ;
  800. LIST(cmd, tmp);
  801. SET_NEXT(tmp, cmd);
  802. }
  803. local_irq_restore(flags);
  804. dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd),
  805. (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
  806. /* If queue_command() is called from an interrupt (real one or bottom
  807. * half), we let queue_main() do the job of taking care about main. If it
  808. * is already running, this is a no-op, else main will be queued.
  809. *
  810. * If we're not in an interrupt, we can call NCR5380_main()
  811. * unconditionally, because it cannot be already running.
  812. */
  813. if (in_interrupt() || irqs_disabled())
  814. queue_main(hostdata);
  815. else
  816. NCR5380_main(&hostdata->main_task);
  817. return 0;
  818. }
  819. static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
  820. {
  821. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  822. /* Caller does the locking needed to set & test these data atomically */
  823. if (!hostdata->disconnected_queue &&
  824. !hostdata->issue_queue &&
  825. !hostdata->connected &&
  826. !hostdata->retain_dma_intr)
  827. NCR5380_release_dma_irq(instance);
  828. }
  829. /**
  830. * NCR5380_main - NCR state machines
  831. *
  832. * NCR5380_main is a coroutine that runs as long as more work can
  833. * be done on the NCR5380 host adapters in a system. Both
  834. * NCR5380_queue_command() and NCR5380_intr() will try to start it
  835. * in case it is not running.
  836. *
  837. * Locks: called as its own thread with no locks held.
  838. */
  839. static void NCR5380_main(struct work_struct *work)
  840. {
  841. struct NCR5380_hostdata *hostdata =
  842. container_of(work, struct NCR5380_hostdata, main_task);
  843. struct Scsi_Host *instance = hostdata->host;
  844. struct scsi_cmnd *tmp, *prev;
  845. int done;
  846. unsigned long flags;
  847. /*
  848. * We run (with interrupts disabled) until we're sure that none of
  849. * the host adapters have anything that can be done, at which point
  850. * we set main_running to 0 and exit.
  851. *
  852. * Interrupts are enabled before doing various other internal
  853. * instructions, after we've decided that we need to run through
  854. * the loop again.
  855. *
  856. * this should prevent any race conditions.
  857. *
  858. * ++roman: Just disabling the NCR interrupt isn't sufficient here,
  859. * because also a timer int can trigger an abort or reset, which can
  860. * alter queues and touch the Falcon lock.
  861. */
  862. /* Tell int handlers main() is now already executing. Note that
  863. no races are possible here. If an int comes in before
  864. 'main_running' is set here, and queues/executes main via the
  865. task queue, it doesn't do any harm, just this instance of main
  866. won't find any work left to do. */
  867. if (hostdata->main_running)
  868. return;
  869. hostdata->main_running = 1;
  870. local_save_flags(flags);
  871. do {
  872. local_irq_disable(); /* Freeze request queues */
  873. done = 1;
  874. if (!hostdata->connected) {
  875. dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO);
  876. /*
  877. * Search through the issue_queue for a command destined
  878. * for a target that's not busy.
  879. */
  880. #if (NDEBUG & NDEBUG_LISTS)
  881. for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL;
  882. tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
  883. ;
  884. /*printk("%p ", tmp);*/
  885. if ((tmp == prev) && tmp)
  886. printk(" LOOP\n");
  887. /* else printk("\n"); */
  888. #endif
  889. for (tmp = (struct scsi_cmnd *) hostdata->issue_queue,
  890. prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
  891. u8 lun = tmp->device->lun;
  892. dprintk(NDEBUG_LISTS,
  893. "MAIN tmp=%p target=%d busy=%d lun=%d\n",
  894. tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)],
  895. lun);
  896. /* When we find one, remove it from the issue queue. */
  897. /* ++guenther: possible race with Falcon locking */
  898. if (
  899. #ifdef SUPPORT_TAGS
  900. !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
  901. #else
  902. !(hostdata->busy[tmp->device->id] & (1 << lun))
  903. #endif
  904. ) {
  905. /* ++guenther: just to be sure, this must be atomic */
  906. local_irq_disable();
  907. if (prev) {
  908. REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
  909. SET_NEXT(prev, NEXT(tmp));
  910. } else {
  911. REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
  912. hostdata->issue_queue = NEXT(tmp);
  913. }
  914. SET_NEXT(tmp, NULL);
  915. hostdata->retain_dma_intr++;
  916. /* reenable interrupts after finding one */
  917. local_irq_restore(flags);
  918. /*
  919. * Attempt to establish an I_T_L nexus here.
  920. * On success, instance->hostdata->connected is set.
  921. * On failure, we must add the command back to the
  922. * issue queue so we can keep trying.
  923. */
  924. dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d "
  925. "lun %d removed from issue_queue\n",
  926. HOSTNO, tmp->device->id, lun);
  927. /*
  928. * REQUEST SENSE commands are issued without tagged
  929. * queueing, even on SCSI-II devices because the
  930. * contingent allegiance condition exists for the
  931. * entire unit.
  932. */
  933. /* ++roman: ...and the standard also requires that
  934. * REQUEST SENSE command are untagged.
  935. */
  936. #ifdef SUPPORT_TAGS
  937. cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
  938. #endif
  939. if (!NCR5380_select(instance, tmp)) {
  940. local_irq_disable();
  941. hostdata->retain_dma_intr--;
  942. /* release if target did not response! */
  943. maybe_release_dma_irq(instance);
  944. local_irq_restore(flags);
  945. break;
  946. } else {
  947. local_irq_disable();
  948. LIST(tmp, hostdata->issue_queue);
  949. SET_NEXT(tmp, hostdata->issue_queue);
  950. hostdata->issue_queue = tmp;
  951. #ifdef SUPPORT_TAGS
  952. cmd_free_tag(tmp);
  953. #endif
  954. hostdata->retain_dma_intr--;
  955. local_irq_restore(flags);
  956. dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, "
  957. "returned to issue_queue\n", HOSTNO);
  958. if (hostdata->connected)
  959. break;
  960. }
  961. } /* if target/lun/target queue is not busy */
  962. } /* for issue_queue */
  963. } /* if (!hostdata->connected) */
  964. if (hostdata->connected
  965. #ifdef REAL_DMA
  966. && !hostdata->dma_len
  967. #endif
  968. ) {
  969. local_irq_restore(flags);
  970. dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n",
  971. HOSTNO);
  972. NCR5380_information_transfer(instance);
  973. dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO);
  974. done = 0;
  975. }
  976. } while (!done);
  977. /* Better allow ints _after_ 'main_running' has been cleared, else
  978. an interrupt could believe we'll pick up the work it left for
  979. us, but we won't see it anymore here... */
  980. hostdata->main_running = 0;
  981. local_irq_restore(flags);
  982. }
  983. #ifdef REAL_DMA
  984. /*
  985. * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
  986. *
  987. * Purpose : Called by interrupt handler when DMA finishes or a phase
  988. * mismatch occurs (which would finish the DMA transfer).
  989. *
  990. * Inputs : instance - this instance of the NCR5380.
  991. *
  992. */
  993. static void NCR5380_dma_complete(struct Scsi_Host *instance)
  994. {
  995. SETUP_HOSTDATA(instance);
  996. int transferred;
  997. unsigned char **data;
  998. volatile int *count;
  999. int saved_data = 0, overrun = 0;
  1000. unsigned char p;
  1001. if (!hostdata->connected) {
  1002. printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
  1003. "no connected cmd\n", HOSTNO);
  1004. return;
  1005. }
  1006. if (hostdata->read_overruns) {
  1007. p = hostdata->connected->SCp.phase;
  1008. if (p & SR_IO) {
  1009. udelay(10);
  1010. if ((NCR5380_read(BUS_AND_STATUS_REG) &
  1011. (BASR_PHASE_MATCH|BASR_ACK)) ==
  1012. (BASR_PHASE_MATCH|BASR_ACK)) {
  1013. saved_data = NCR5380_read(INPUT_DATA_REG);
  1014. overrun = 1;
  1015. dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO);
  1016. }
  1017. }
  1018. }
  1019. dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
  1020. HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
  1021. NCR5380_read(STATUS_REG));
  1022. #if defined(CONFIG_SUN3)
  1023. if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
  1024. pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
  1025. instance->host_no);
  1026. BUG();
  1027. }
  1028. /* make sure we're not stuck in a data phase */
  1029. if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
  1030. (BASR_PHASE_MATCH | BASR_ACK)) {
  1031. pr_err("scsi%d: BASR %02x\n", instance->host_no,
  1032. NCR5380_read(BUS_AND_STATUS_REG));
  1033. pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
  1034. instance->host_no);
  1035. BUG();
  1036. }
  1037. #endif
  1038. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1039. NCR5380_write(MODE_REG, MR_BASE);
  1040. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1041. transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
  1042. hostdata->dma_len = 0;
  1043. data = (unsigned char **)&hostdata->connected->SCp.ptr;
  1044. count = &hostdata->connected->SCp.this_residual;
  1045. *data += transferred;
  1046. *count -= transferred;
  1047. if (hostdata->read_overruns) {
  1048. int cnt, toPIO;
  1049. if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
  1050. cnt = toPIO = hostdata->read_overruns;
  1051. if (overrun) {
  1052. dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
  1053. *(*data)++ = saved_data;
  1054. (*count)--;
  1055. cnt--;
  1056. toPIO--;
  1057. }
  1058. dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
  1059. NCR5380_transfer_pio(instance, &p, &cnt, data);
  1060. *count -= toPIO - cnt;
  1061. }
  1062. }
  1063. }
  1064. #endif /* REAL_DMA */
  1065. /**
  1066. * NCR5380_intr - generic NCR5380 irq handler
  1067. * @irq: interrupt number
  1068. * @dev_id: device info
  1069. *
  1070. * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
  1071. * from the disconnected queue, and restarting NCR5380_main()
  1072. * as required.
  1073. */
  1074. static irqreturn_t NCR5380_intr(int irq, void *dev_id)
  1075. {
  1076. struct Scsi_Host *instance = dev_id;
  1077. int done = 1, handled = 0;
  1078. unsigned char basr;
  1079. dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO);
  1080. /* Look for pending interrupts */
  1081. basr = NCR5380_read(BUS_AND_STATUS_REG);
  1082. dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr);
  1083. /* dispatch to appropriate routine if found and done=0 */
  1084. if (basr & BASR_IRQ) {
  1085. NCR5380_dprint(NDEBUG_INTR, instance);
  1086. if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
  1087. done = 0;
  1088. dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO);
  1089. NCR5380_reselect(instance);
  1090. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1091. } else if (basr & BASR_PARITY_ERROR) {
  1092. dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO);
  1093. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1094. } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
  1095. dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO);
  1096. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1097. } else {
  1098. /*
  1099. * The rest of the interrupt conditions can occur only during a
  1100. * DMA transfer
  1101. */
  1102. #if defined(REAL_DMA)
  1103. /*
  1104. * We should only get PHASE MISMATCH and EOP interrupts if we have
  1105. * DMA enabled, so do a sanity check based on the current setting
  1106. * of the MODE register.
  1107. */
  1108. if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
  1109. ((basr & BASR_END_DMA_TRANSFER) ||
  1110. !(basr & BASR_PHASE_MATCH))) {
  1111. dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
  1112. NCR5380_dma_complete( instance );
  1113. done = 0;
  1114. } else
  1115. #endif /* REAL_DMA */
  1116. {
  1117. /* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
  1118. if (basr & BASR_PHASE_MATCH)
  1119. dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, "
  1120. "BASR 0x%x, MR 0x%x, SR 0x%x\n",
  1121. HOSTNO, basr, NCR5380_read(MODE_REG),
  1122. NCR5380_read(STATUS_REG));
  1123. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1124. #ifdef SUN3_SCSI_VME
  1125. dregs->csr |= CSR_DMA_ENABLE;
  1126. #endif
  1127. }
  1128. } /* if !(SELECTION || PARITY) */
  1129. handled = 1;
  1130. } /* BASR & IRQ */ else {
  1131. printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
  1132. "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
  1133. NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
  1134. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1135. #ifdef SUN3_SCSI_VME
  1136. dregs->csr |= CSR_DMA_ENABLE;
  1137. #endif
  1138. }
  1139. if (!done) {
  1140. dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO);
  1141. /* Put a call to NCR5380_main() on the queue... */
  1142. queue_main(shost_priv(instance));
  1143. }
  1144. return IRQ_RETVAL(handled);
  1145. }
  1146. /*
  1147. * Function : int NCR5380_select(struct Scsi_Host *instance,
  1148. * struct scsi_cmnd *cmd)
  1149. *
  1150. * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
  1151. * including ARBITRATION, SELECTION, and initial message out for
  1152. * IDENTIFY and queue messages.
  1153. *
  1154. * Inputs : instance - instantiation of the 5380 driver on which this
  1155. * target lives, cmd - SCSI command to execute.
  1156. *
  1157. * Returns : -1 if selection could not execute for some reason,
  1158. * 0 if selection succeeded or failed because the target
  1159. * did not respond.
  1160. *
  1161. * Side effects :
  1162. * If bus busy, arbitration failed, etc, NCR5380_select() will exit
  1163. * with registers as they should have been on entry - ie
  1164. * SELECT_ENABLE will be set appropriately, the NCR5380
  1165. * will cease to drive any SCSI bus signals.
  1166. *
  1167. * If successful : I_T_L or I_T_L_Q nexus will be established,
  1168. * instance->connected will be set to cmd.
  1169. * SELECT interrupt will be disabled.
  1170. *
  1171. * If failed (no target) : cmd->scsi_done() will be called, and the
  1172. * cmd->result host byte set to DID_BAD_TARGET.
  1173. */
  1174. static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
  1175. {
  1176. SETUP_HOSTDATA(instance);
  1177. unsigned char tmp[3], phase;
  1178. unsigned char *data;
  1179. int len;
  1180. unsigned long timeout;
  1181. unsigned long flags;
  1182. hostdata->restart_select = 0;
  1183. NCR5380_dprint(NDEBUG_ARBITRATION, instance);
  1184. dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO,
  1185. instance->this_id);
  1186. /*
  1187. * Set the phase bits to 0, otherwise the NCR5380 won't drive the
  1188. * data bus during SELECTION.
  1189. */
  1190. local_irq_save(flags);
  1191. if (hostdata->connected) {
  1192. local_irq_restore(flags);
  1193. return -1;
  1194. }
  1195. NCR5380_write(TARGET_COMMAND_REG, 0);
  1196. /*
  1197. * Start arbitration.
  1198. */
  1199. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  1200. NCR5380_write(MODE_REG, MR_ARBITRATE);
  1201. local_irq_restore(flags);
  1202. /* Wait for arbitration logic to complete */
  1203. #if defined(NCR_TIMEOUT)
  1204. {
  1205. unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
  1206. while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
  1207. time_before(jiffies, timeout) && !hostdata->connected)
  1208. ;
  1209. if (time_after_eq(jiffies, timeout)) {
  1210. printk("scsi : arbitration timeout at %d\n", __LINE__);
  1211. NCR5380_write(MODE_REG, MR_BASE);
  1212. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1213. return -1;
  1214. }
  1215. }
  1216. #else /* NCR_TIMEOUT */
  1217. while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
  1218. !hostdata->connected)
  1219. ;
  1220. #endif
  1221. dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO);
  1222. if (hostdata->connected) {
  1223. NCR5380_write(MODE_REG, MR_BASE);
  1224. return -1;
  1225. }
  1226. /*
  1227. * The arbitration delay is 2.2us, but this is a minimum and there is
  1228. * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
  1229. * the integral nature of udelay().
  1230. *
  1231. */
  1232. udelay(3);
  1233. /* Check for lost arbitration */
  1234. if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  1235. (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
  1236. (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  1237. hostdata->connected) {
  1238. NCR5380_write(MODE_REG, MR_BASE);
  1239. dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
  1240. HOSTNO);
  1241. return -1;
  1242. }
  1243. /* after/during arbitration, BSY should be asserted.
  1244. IBM DPES-31080 Version S31Q works now */
  1245. /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
  1246. NCR5380_write(INITIATOR_COMMAND_REG,
  1247. ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
  1248. if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  1249. hostdata->connected) {
  1250. NCR5380_write(MODE_REG, MR_BASE);
  1251. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1252. dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
  1253. HOSTNO);
  1254. return -1;
  1255. }
  1256. /*
  1257. * Again, bus clear + bus settle time is 1.2us, however, this is
  1258. * a minimum so we'll udelay ceil(1.2)
  1259. */
  1260. #ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
  1261. /* ++roman: But some targets (see above :-) seem to need a bit more... */
  1262. udelay(15);
  1263. #else
  1264. udelay(2);
  1265. #endif
  1266. if (hostdata->connected) {
  1267. NCR5380_write(MODE_REG, MR_BASE);
  1268. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1269. return -1;
  1270. }
  1271. dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO);
  1272. /*
  1273. * Now that we have won arbitration, start Selection process, asserting
  1274. * the host and target ID's on the SCSI bus.
  1275. */
  1276. NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
  1277. /*
  1278. * Raise ATN while SEL is true before BSY goes false from arbitration,
  1279. * since this is the only way to guarantee that we'll get a MESSAGE OUT
  1280. * phase immediately after selection.
  1281. */
  1282. NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
  1283. ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
  1284. NCR5380_write(MODE_REG, MR_BASE);
  1285. /*
  1286. * Reselect interrupts must be turned off prior to the dropping of BSY,
  1287. * otherwise we will trigger an interrupt.
  1288. */
  1289. if (hostdata->connected) {
  1290. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1291. return -1;
  1292. }
  1293. NCR5380_write(SELECT_ENABLE_REG, 0);
  1294. /*
  1295. * The initiator shall then wait at least two deskew delays and release
  1296. * the BSY signal.
  1297. */
  1298. udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
  1299. /* Reset BSY */
  1300. NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
  1301. ICR_ASSERT_ATN | ICR_ASSERT_SEL));
  1302. /*
  1303. * Something weird happens when we cease to drive BSY - looks
  1304. * like the board/chip is letting us do another read before the
  1305. * appropriate propagation delay has expired, and we're confusing
  1306. * a BSY signal from ourselves as the target's response to SELECTION.
  1307. *
  1308. * A small delay (the 'C++' frontend breaks the pipeline with an
  1309. * unnecessary jump, making it work on my 386-33/Trantor T128, the
  1310. * tighter 'C' code breaks and requires this) solves the problem -
  1311. * the 1 us delay is arbitrary, and only used because this delay will
  1312. * be the same on other platforms and since it works here, it should
  1313. * work there.
  1314. *
  1315. * wingel suggests that this could be due to failing to wait
  1316. * one deskew delay.
  1317. */
  1318. udelay(1);
  1319. dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
  1320. /*
  1321. * The SCSI specification calls for a 250 ms timeout for the actual
  1322. * selection.
  1323. */
  1324. timeout = jiffies + msecs_to_jiffies(250);
  1325. /*
  1326. * XXX very interesting - we're seeing a bounce where the BSY we
  1327. * asserted is being reflected / still asserted (propagation delay?)
  1328. * and it's detecting as true. Sigh.
  1329. */
  1330. #if 0
  1331. /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
  1332. * IO while SEL is true. But again, there are some disks out the in the
  1333. * world that do that nevertheless. (Somebody claimed that this announces
  1334. * reselection capability of the target.) So we better skip that test and
  1335. * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
  1336. */
  1337. while (time_before(jiffies, timeout) &&
  1338. !(NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO)))
  1339. ;
  1340. if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
  1341. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1342. NCR5380_reselect(instance);
  1343. printk(KERN_ERR "scsi%d: reselection after won arbitration?\n",
  1344. HOSTNO);
  1345. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1346. return -1;
  1347. }
  1348. #else
  1349. while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY))
  1350. ;
  1351. #endif
  1352. /*
  1353. * No less than two deskew delays after the initiator detects the
  1354. * BSY signal is true, it shall release the SEL signal and may
  1355. * change the DATA BUS. -wingel
  1356. */
  1357. udelay(1);
  1358. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1359. if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
  1360. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1361. if (hostdata->targets_present & (1 << cmd->device->id)) {
  1362. printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
  1363. if (hostdata->restart_select)
  1364. printk(KERN_NOTICE "\trestart select\n");
  1365. NCR5380_dprint(NDEBUG_ANY, instance);
  1366. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1367. return -1;
  1368. }
  1369. cmd->result = DID_BAD_TARGET << 16;
  1370. #ifdef SUPPORT_TAGS
  1371. cmd_free_tag(cmd);
  1372. #endif
  1373. cmd->scsi_done(cmd);
  1374. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1375. dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO);
  1376. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1377. return 0;
  1378. }
  1379. hostdata->targets_present |= (1 << cmd->device->id);
  1380. /*
  1381. * Since we followed the SCSI spec, and raised ATN while SEL
  1382. * was true but before BSY was false during selection, the information
  1383. * transfer phase should be a MESSAGE OUT phase so that we can send the
  1384. * IDENTIFY message.
  1385. *
  1386. * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
  1387. * message (2 bytes) with a tag ID that we increment with every command
  1388. * until it wraps back to 0.
  1389. *
  1390. * XXX - it turns out that there are some broken SCSI-II devices,
  1391. * which claim to support tagged queuing but fail when more than
  1392. * some number of commands are issued at once.
  1393. */
  1394. /* Wait for start of REQ/ACK handshake */
  1395. while (!(NCR5380_read(STATUS_REG) & SR_REQ))
  1396. ;
  1397. dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
  1398. HOSTNO, cmd->device->id);
  1399. tmp[0] = IDENTIFY(1, cmd->device->lun);
  1400. #ifdef SUPPORT_TAGS
  1401. if (cmd->tag != TAG_NONE) {
  1402. tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
  1403. tmp[2] = cmd->tag;
  1404. len = 3;
  1405. } else
  1406. len = 1;
  1407. #else
  1408. len = 1;
  1409. cmd->tag = 0;
  1410. #endif /* SUPPORT_TAGS */
  1411. /* Send message(s) */
  1412. data = tmp;
  1413. phase = PHASE_MSGOUT;
  1414. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1415. dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO);
  1416. /* XXX need to handle errors here */
  1417. hostdata->connected = cmd;
  1418. #ifndef SUPPORT_TAGS
  1419. hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
  1420. #endif
  1421. #ifdef SUN3_SCSI_VME
  1422. dregs->csr |= CSR_INTR;
  1423. #endif
  1424. initialize_SCp(cmd);
  1425. return 0;
  1426. }
  1427. /*
  1428. * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
  1429. * unsigned char *phase, int *count, unsigned char **data)
  1430. *
  1431. * Purpose : transfers data in given phase using polled I/O
  1432. *
  1433. * Inputs : instance - instance of driver, *phase - pointer to
  1434. * what phase is expected, *count - pointer to number of
  1435. * bytes to transfer, **data - pointer to data pointer.
  1436. *
  1437. * Returns : -1 when different phase is entered without transferring
  1438. * maximum number of bytes, 0 if all bytes are transferred or exit
  1439. * is in same phase.
  1440. *
  1441. * Also, *phase, *count, *data are modified in place.
  1442. *
  1443. * XXX Note : handling for bus free may be useful.
  1444. */
  1445. /*
  1446. * Note : this code is not as quick as it could be, however it
  1447. * IS 100% reliable, and for the actual data transfer where speed
  1448. * counts, we will always do a pseudo DMA or DMA transfer.
  1449. */
  1450. static int NCR5380_transfer_pio(struct Scsi_Host *instance,
  1451. unsigned char *phase, int *count,
  1452. unsigned char **data)
  1453. {
  1454. register unsigned char p = *phase, tmp;
  1455. register int c = *count;
  1456. register unsigned char *d = *data;
  1457. /*
  1458. * The NCR5380 chip will only drive the SCSI bus when the
  1459. * phase specified in the appropriate bits of the TARGET COMMAND
  1460. * REGISTER match the STATUS REGISTER
  1461. */
  1462. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1463. do {
  1464. /*
  1465. * Wait for assertion of REQ, after which the phase bits will be
  1466. * valid
  1467. */
  1468. while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
  1469. ;
  1470. dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO);
  1471. /* Check for phase mismatch */
  1472. if ((tmp & PHASE_MASK) != p) {
  1473. dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO);
  1474. NCR5380_dprint_phase(NDEBUG_PIO, instance);
  1475. break;
  1476. }
  1477. /* Do actual transfer from SCSI bus to / from memory */
  1478. if (!(p & SR_IO))
  1479. NCR5380_write(OUTPUT_DATA_REG, *d);
  1480. else
  1481. *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
  1482. ++d;
  1483. /*
  1484. * The SCSI standard suggests that in MSGOUT phase, the initiator
  1485. * should drop ATN on the last byte of the message phase
  1486. * after REQ has been asserted for the handshake but before
  1487. * the initiator raises ACK.
  1488. */
  1489. if (!(p & SR_IO)) {
  1490. if (!((p & SR_MSG) && c > 1)) {
  1491. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1492. NCR5380_dprint(NDEBUG_PIO, instance);
  1493. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1494. ICR_ASSERT_DATA | ICR_ASSERT_ACK);
  1495. } else {
  1496. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1497. ICR_ASSERT_DATA | ICR_ASSERT_ATN);
  1498. NCR5380_dprint(NDEBUG_PIO, instance);
  1499. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1500. ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1501. }
  1502. } else {
  1503. NCR5380_dprint(NDEBUG_PIO, instance);
  1504. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  1505. }
  1506. while (NCR5380_read(STATUS_REG) & SR_REQ)
  1507. ;
  1508. dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO);
  1509. /*
  1510. * We have several special cases to consider during REQ/ACK handshaking :
  1511. * 1. We were in MSGOUT phase, and we are on the last byte of the
  1512. * message. ATN must be dropped as ACK is dropped.
  1513. *
  1514. * 2. We are in a MSGIN phase, and we are on the last byte of the
  1515. * message. We must exit with ACK asserted, so that the calling
  1516. * code may raise ATN before dropping ACK to reject the message.
  1517. *
  1518. * 3. ACK and ATN are clear and the target may proceed as normal.
  1519. */
  1520. if (!(p == PHASE_MSGIN && c == 1)) {
  1521. if (p == PHASE_MSGOUT && c > 1)
  1522. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1523. else
  1524. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1525. }
  1526. } while (--c);
  1527. dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c);
  1528. *count = c;
  1529. *data = d;
  1530. tmp = NCR5380_read(STATUS_REG);
  1531. /* The phase read from the bus is valid if either REQ is (already)
  1532. * asserted or if ACK hasn't been released yet. The latter is the case if
  1533. * we're in MSGIN and all wanted bytes have been received.
  1534. */
  1535. if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
  1536. *phase = tmp & PHASE_MASK;
  1537. else
  1538. *phase = PHASE_UNKNOWN;
  1539. if (!c || (*phase == p))
  1540. return 0;
  1541. else
  1542. return -1;
  1543. }
  1544. /*
  1545. * Function : do_abort (Scsi_Host *host)
  1546. *
  1547. * Purpose : abort the currently established nexus. Should only be
  1548. * called from a routine which can drop into a
  1549. *
  1550. * Returns : 0 on success, -1 on failure.
  1551. */
  1552. static int do_abort(struct Scsi_Host *instance)
  1553. {
  1554. unsigned char tmp, *msgptr, phase;
  1555. int len;
  1556. /* Request message out phase */
  1557. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1558. /*
  1559. * Wait for the target to indicate a valid phase by asserting
  1560. * REQ. Once this happens, we'll have either a MSGOUT phase
  1561. * and can immediately send the ABORT message, or we'll have some
  1562. * other phase and will have to source/sink data.
  1563. *
  1564. * We really don't care what value was on the bus or what value
  1565. * the target sees, so we just handshake.
  1566. */
  1567. while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ))
  1568. ;
  1569. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1570. if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
  1571. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
  1572. ICR_ASSERT_ACK);
  1573. while (NCR5380_read(STATUS_REG) & SR_REQ)
  1574. ;
  1575. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1576. }
  1577. tmp = ABORT;
  1578. msgptr = &tmp;
  1579. len = 1;
  1580. phase = PHASE_MSGOUT;
  1581. NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
  1582. /*
  1583. * If we got here, and the command completed successfully,
  1584. * we're about to go into bus free state.
  1585. */
  1586. return len ? -1 : 0;
  1587. }
  1588. #if defined(REAL_DMA)
  1589. /*
  1590. * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
  1591. * unsigned char *phase, int *count, unsigned char **data)
  1592. *
  1593. * Purpose : transfers data in given phase using either real
  1594. * or pseudo DMA.
  1595. *
  1596. * Inputs : instance - instance of driver, *phase - pointer to
  1597. * what phase is expected, *count - pointer to number of
  1598. * bytes to transfer, **data - pointer to data pointer.
  1599. *
  1600. * Returns : -1 when different phase is entered without transferring
  1601. * maximum number of bytes, 0 if all bytes or transferred or exit
  1602. * is in same phase.
  1603. *
  1604. * Also, *phase, *count, *data are modified in place.
  1605. *
  1606. */
  1607. static int NCR5380_transfer_dma(struct Scsi_Host *instance,
  1608. unsigned char *phase, int *count,
  1609. unsigned char **data)
  1610. {
  1611. SETUP_HOSTDATA(instance);
  1612. register int c = *count;
  1613. register unsigned char p = *phase;
  1614. unsigned long flags;
  1615. #if defined(CONFIG_SUN3)
  1616. /* sanity check */
  1617. if (!sun3_dma_setup_done) {
  1618. pr_err("scsi%d: transfer_dma without setup!\n",
  1619. instance->host_no);
  1620. BUG();
  1621. }
  1622. hostdata->dma_len = c;
  1623. dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
  1624. instance->host_no, (p & SR_IO) ? "reading" : "writing",
  1625. c, (p & SR_IO) ? "to" : "from", *data);
  1626. /* netbsd turns off ints here, why not be safe and do it too */
  1627. local_irq_save(flags);
  1628. /* send start chain */
  1629. sun3scsi_dma_start(c, *data);
  1630. if (p & SR_IO) {
  1631. NCR5380_write(TARGET_COMMAND_REG, 1);
  1632. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1633. NCR5380_write(INITIATOR_COMMAND_REG, 0);
  1634. NCR5380_write(MODE_REG,
  1635. (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
  1636. NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
  1637. } else {
  1638. NCR5380_write(TARGET_COMMAND_REG, 0);
  1639. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1640. NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
  1641. NCR5380_write(MODE_REG,
  1642. (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR));
  1643. NCR5380_write(START_DMA_SEND_REG, 0);
  1644. }
  1645. #ifdef SUN3_SCSI_VME
  1646. dregs->csr |= CSR_DMA_ENABLE;
  1647. #endif
  1648. local_irq_restore(flags);
  1649. sun3_dma_active = 1;
  1650. #else /* !defined(CONFIG_SUN3) */
  1651. register unsigned char *d = *data;
  1652. unsigned char tmp;
  1653. if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
  1654. *phase = tmp;
  1655. return -1;
  1656. }
  1657. if (hostdata->read_overruns && (p & SR_IO))
  1658. c -= hostdata->read_overruns;
  1659. dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
  1660. HOSTNO, (p & SR_IO) ? "reading" : "writing",
  1661. c, (p & SR_IO) ? "to" : "from", d);
  1662. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1663. #ifdef REAL_DMA
  1664. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
  1665. #endif /* def REAL_DMA */
  1666. if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
  1667. /* On the Medusa, it is a must to initialize the DMA before
  1668. * starting the NCR. This is also the cleaner way for the TT.
  1669. */
  1670. local_irq_save(flags);
  1671. hostdata->dma_len = (p & SR_IO) ?
  1672. NCR5380_dma_read_setup(instance, d, c) :
  1673. NCR5380_dma_write_setup(instance, d, c);
  1674. local_irq_restore(flags);
  1675. }
  1676. if (p & SR_IO)
  1677. NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
  1678. else {
  1679. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1680. NCR5380_write(START_DMA_SEND_REG, 0);
  1681. }
  1682. if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
  1683. /* On the Falcon, the DMA setup must be done after the last */
  1684. /* NCR access, else the DMA setup gets trashed!
  1685. */
  1686. local_irq_save(flags);
  1687. hostdata->dma_len = (p & SR_IO) ?
  1688. NCR5380_dma_read_setup(instance, d, c) :
  1689. NCR5380_dma_write_setup(instance, d, c);
  1690. local_irq_restore(flags);
  1691. }
  1692. #endif /* !defined(CONFIG_SUN3) */
  1693. return 0;
  1694. }
  1695. #endif /* defined(REAL_DMA) */
  1696. /*
  1697. * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
  1698. *
  1699. * Purpose : run through the various SCSI phases and do as the target
  1700. * directs us to. Operates on the currently connected command,
  1701. * instance->connected.
  1702. *
  1703. * Inputs : instance, instance for which we are doing commands
  1704. *
  1705. * Side effects : SCSI things happen, the disconnected queue will be
  1706. * modified if a command disconnects, *instance->connected will
  1707. * change.
  1708. *
  1709. * XXX Note : we need to watch for bus free or a reset condition here
  1710. * to recover from an unexpected bus free condition.
  1711. */
  1712. static void NCR5380_information_transfer(struct Scsi_Host *instance)
  1713. {
  1714. SETUP_HOSTDATA(instance);
  1715. unsigned long flags;
  1716. unsigned char msgout = NOP;
  1717. int sink = 0;
  1718. int len;
  1719. #if defined(REAL_DMA)
  1720. int transfersize;
  1721. #endif
  1722. unsigned char *data;
  1723. unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
  1724. struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
  1725. #ifdef SUN3_SCSI_VME
  1726. dregs->csr |= CSR_INTR;
  1727. #endif
  1728. while (1) {
  1729. tmp = NCR5380_read(STATUS_REG);
  1730. /* We only have a valid SCSI phase when REQ is asserted */
  1731. if (tmp & SR_REQ) {
  1732. phase = (tmp & PHASE_MASK);
  1733. if (phase != old_phase) {
  1734. old_phase = phase;
  1735. NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
  1736. }
  1737. #if defined(CONFIG_SUN3)
  1738. if (phase == PHASE_CMDOUT) {
  1739. #if defined(REAL_DMA)
  1740. void *d;
  1741. unsigned long count;
  1742. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  1743. count = cmd->SCp.buffer->length;
  1744. d = sg_virt(cmd->SCp.buffer);
  1745. } else {
  1746. count = cmd->SCp.this_residual;
  1747. d = cmd->SCp.ptr;
  1748. }
  1749. /* this command setup for dma yet? */
  1750. if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) {
  1751. if (cmd->request->cmd_type == REQ_TYPE_FS) {
  1752. sun3scsi_dma_setup(d, count,
  1753. rq_data_dir(cmd->request));
  1754. sun3_dma_setup_done = cmd;
  1755. }
  1756. }
  1757. #endif
  1758. #ifdef SUN3_SCSI_VME
  1759. dregs->csr |= CSR_INTR;
  1760. #endif
  1761. }
  1762. #endif /* CONFIG_SUN3 */
  1763. if (sink && (phase != PHASE_MSGOUT)) {
  1764. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1765. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
  1766. ICR_ASSERT_ACK);
  1767. while (NCR5380_read(STATUS_REG) & SR_REQ)
  1768. ;
  1769. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1770. ICR_ASSERT_ATN);
  1771. sink = 0;
  1772. continue;
  1773. }
  1774. switch (phase) {
  1775. case PHASE_DATAOUT:
  1776. #if (NDEBUG & NDEBUG_NO_DATAOUT)
  1777. printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
  1778. "aborted\n", HOSTNO);
  1779. sink = 1;
  1780. do_abort(instance);
  1781. cmd->result = DID_ERROR << 16;
  1782. cmd->scsi_done(cmd);
  1783. return;
  1784. #endif
  1785. case PHASE_DATAIN:
  1786. /*
  1787. * If there is no room left in the current buffer in the
  1788. * scatter-gather list, move onto the next one.
  1789. */
  1790. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  1791. ++cmd->SCp.buffer;
  1792. --cmd->SCp.buffers_residual;
  1793. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  1794. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  1795. /* ++roman: Try to merge some scatter-buffers if
  1796. * they are at contiguous physical addresses.
  1797. */
  1798. merge_contiguous_buffers(cmd);
  1799. dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n",
  1800. HOSTNO, cmd->SCp.this_residual,
  1801. cmd->SCp.buffers_residual);
  1802. }
  1803. /*
  1804. * The preferred transfer method is going to be
  1805. * PSEUDO-DMA for systems that are strictly PIO,
  1806. * since we can let the hardware do the handshaking.
  1807. *
  1808. * For this to work, we need to know the transfersize
  1809. * ahead of time, since the pseudo-DMA code will sit
  1810. * in an unconditional loop.
  1811. */
  1812. /* ++roman: I suggest, this should be
  1813. * #if def(REAL_DMA)
  1814. * instead of leaving REAL_DMA out.
  1815. */
  1816. #if defined(REAL_DMA)
  1817. if (
  1818. #if !defined(CONFIG_SUN3)
  1819. !cmd->device->borken &&
  1820. #endif
  1821. (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) {
  1822. len = transfersize;
  1823. cmd->SCp.phase = phase;
  1824. if (NCR5380_transfer_dma(instance, &phase,
  1825. &len, (unsigned char **)&cmd->SCp.ptr)) {
  1826. /*
  1827. * If the watchdog timer fires, all future
  1828. * accesses to this device will use the
  1829. * polled-IO. */
  1830. scmd_printk(KERN_INFO, cmd,
  1831. "switching to slow handshake\n");
  1832. cmd->device->borken = 1;
  1833. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1834. ICR_ASSERT_ATN);
  1835. sink = 1;
  1836. do_abort(instance);
  1837. cmd->result = DID_ERROR << 16;
  1838. cmd->scsi_done(cmd);
  1839. /* XXX - need to source or sink data here, as appropriate */
  1840. } else {
  1841. #ifdef REAL_DMA
  1842. /* ++roman: When using real DMA,
  1843. * information_transfer() should return after
  1844. * starting DMA since it has nothing more to
  1845. * do.
  1846. */
  1847. return;
  1848. #else
  1849. cmd->SCp.this_residual -= transfersize - len;
  1850. #endif
  1851. }
  1852. } else
  1853. #endif /* defined(REAL_DMA) */
  1854. NCR5380_transfer_pio(instance, &phase,
  1855. (int *)&cmd->SCp.this_residual,
  1856. (unsigned char **)&cmd->SCp.ptr);
  1857. #if defined(CONFIG_SUN3) && defined(REAL_DMA)
  1858. /* if we had intended to dma that command clear it */
  1859. if (sun3_dma_setup_done == cmd)
  1860. sun3_dma_setup_done = NULL;
  1861. #endif
  1862. break;
  1863. case PHASE_MSGIN:
  1864. len = 1;
  1865. data = &tmp;
  1866. NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
  1867. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1868. cmd->SCp.Message = tmp;
  1869. switch (tmp) {
  1870. /*
  1871. * Linking lets us reduce the time required to get the
  1872. * next command out to the device, hopefully this will
  1873. * mean we don't waste another revolution due to the delays
  1874. * required by ARBITRATION and another SELECTION.
  1875. *
  1876. * In the current implementation proposal, low level drivers
  1877. * merely have to start the next command, pointed to by
  1878. * next_link, done() is called as with unlinked commands.
  1879. */
  1880. #ifdef LINKED
  1881. case LINKED_CMD_COMPLETE:
  1882. case LINKED_FLG_CMD_COMPLETE:
  1883. /* Accept message by clearing ACK */
  1884. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1885. dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command "
  1886. "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
  1887. /* Enable reselect interrupts */
  1888. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1889. /*
  1890. * Sanity check : A linked command should only terminate
  1891. * with one of these messages if there are more linked
  1892. * commands available.
  1893. */
  1894. if (!cmd->next_link) {
  1895. printk(KERN_NOTICE "scsi%d: target %d lun %llu "
  1896. "linked command complete, no next_link\n",
  1897. HOSTNO, cmd->device->id, cmd->device->lun);
  1898. sink = 1;
  1899. do_abort(instance);
  1900. return;
  1901. }
  1902. initialize_SCp(cmd->next_link);
  1903. /* The next command is still part of this process; copy it
  1904. * and don't free it! */
  1905. cmd->next_link->tag = cmd->tag;
  1906. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1907. dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request "
  1908. "done, calling scsi_done().\n",
  1909. HOSTNO, cmd->device->id, cmd->device->lun);
  1910. cmd->scsi_done(cmd);
  1911. cmd = hostdata->connected;
  1912. break;
  1913. #endif /* def LINKED */
  1914. case ABORT:
  1915. case COMMAND_COMPLETE:
  1916. /* Accept message by clearing ACK */
  1917. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1918. dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu "
  1919. "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
  1920. local_irq_save(flags);
  1921. hostdata->retain_dma_intr++;
  1922. hostdata->connected = NULL;
  1923. #ifdef SUPPORT_TAGS
  1924. cmd_free_tag(cmd);
  1925. if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
  1926. /* Turn a QUEUE FULL status into BUSY, I think the
  1927. * mid level cannot handle QUEUE FULL :-( (The
  1928. * command is retried after BUSY). Also update our
  1929. * queue size to the number of currently issued
  1930. * commands now.
  1931. */
  1932. /* ++Andreas: the mid level code knows about
  1933. QUEUE_FULL now. */
  1934. struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun];
  1935. dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned "
  1936. "QUEUE_FULL after %d commands\n",
  1937. HOSTNO, cmd->device->id, cmd->device->lun,
  1938. ta->nr_allocated);
  1939. if (ta->queue_size > ta->nr_allocated)
  1940. ta->nr_allocated = ta->queue_size;
  1941. }
  1942. #else
  1943. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1944. #endif
  1945. /* Enable reselect interrupts */
  1946. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1947. /*
  1948. * I'm not sure what the correct thing to do here is :
  1949. *
  1950. * If the command that just executed is NOT a request
  1951. * sense, the obvious thing to do is to set the result
  1952. * code to the values of the stored parameters.
  1953. *
  1954. * If it was a REQUEST SENSE command, we need some way to
  1955. * differentiate between the failure code of the original
  1956. * and the failure code of the REQUEST sense - the obvious
  1957. * case is success, where we fall through and leave the
  1958. * result code unchanged.
  1959. *
  1960. * The non-obvious place is where the REQUEST SENSE failed
  1961. */
  1962. if (cmd->cmnd[0] != REQUEST_SENSE)
  1963. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1964. else if (status_byte(cmd->SCp.Status) != GOOD)
  1965. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  1966. if ((cmd->cmnd[0] == REQUEST_SENSE) &&
  1967. hostdata->ses.cmd_len) {
  1968. scsi_eh_restore_cmnd(cmd, &hostdata->ses);
  1969. hostdata->ses.cmd_len = 0 ;
  1970. }
  1971. if ((cmd->cmnd[0] != REQUEST_SENSE) &&
  1972. (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
  1973. scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
  1974. dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO);
  1975. LIST(cmd,hostdata->issue_queue);
  1976. SET_NEXT(cmd, hostdata->issue_queue);
  1977. hostdata->issue_queue = (struct scsi_cmnd *) cmd;
  1978. dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of "
  1979. "issue queue\n", H_NO(cmd));
  1980. } else {
  1981. cmd->scsi_done(cmd);
  1982. }
  1983. local_irq_restore(flags);
  1984. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1985. /*
  1986. * Restore phase bits to 0 so an interrupted selection,
  1987. * arbitration can resume.
  1988. */
  1989. NCR5380_write(TARGET_COMMAND_REG, 0);
  1990. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  1991. barrier();
  1992. local_irq_save(flags);
  1993. hostdata->retain_dma_intr--;
  1994. /* ++roman: For Falcon SCSI, release the lock on the
  1995. * ST-DMA here if no other commands are waiting on the
  1996. * disconnected queue.
  1997. */
  1998. maybe_release_dma_irq(instance);
  1999. local_irq_restore(flags);
  2000. return;
  2001. case MESSAGE_REJECT:
  2002. /* Accept message by clearing ACK */
  2003. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2004. /* Enable reselect interrupts */
  2005. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2006. switch (hostdata->last_message) {
  2007. case HEAD_OF_QUEUE_TAG:
  2008. case ORDERED_QUEUE_TAG:
  2009. case SIMPLE_QUEUE_TAG:
  2010. /* The target obviously doesn't support tagged
  2011. * queuing, even though it announced this ability in
  2012. * its INQUIRY data ?!? (maybe only this LUN?) Ok,
  2013. * clear 'tagged_supported' and lock the LUN, since
  2014. * the command is treated as untagged further on.
  2015. */
  2016. cmd->device->tagged_supported = 0;
  2017. hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
  2018. cmd->tag = TAG_NONE;
  2019. dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected "
  2020. "QUEUE_TAG message; tagged queuing "
  2021. "disabled\n",
  2022. HOSTNO, cmd->device->id, cmd->device->lun);
  2023. break;
  2024. }
  2025. break;
  2026. case DISCONNECT:
  2027. /* Accept message by clearing ACK */
  2028. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2029. local_irq_save(flags);
  2030. cmd->device->disconnect = 1;
  2031. LIST(cmd,hostdata->disconnected_queue);
  2032. SET_NEXT(cmd, hostdata->disconnected_queue);
  2033. hostdata->connected = NULL;
  2034. hostdata->disconnected_queue = cmd;
  2035. local_irq_restore(flags);
  2036. dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was "
  2037. "moved from connected to the "
  2038. "disconnected_queue\n", HOSTNO,
  2039. cmd->device->id, cmd->device->lun);
  2040. /*
  2041. * Restore phase bits to 0 so an interrupted selection,
  2042. * arbitration can resume.
  2043. */
  2044. NCR5380_write(TARGET_COMMAND_REG, 0);
  2045. /* Enable reselect interrupts */
  2046. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2047. /* Wait for bus free to avoid nasty timeouts */
  2048. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2049. barrier();
  2050. #ifdef SUN3_SCSI_VME
  2051. dregs->csr |= CSR_DMA_ENABLE;
  2052. #endif
  2053. return;
  2054. /*
  2055. * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  2056. * operation, in violation of the SCSI spec so we can safely
  2057. * ignore SAVE/RESTORE pointers calls.
  2058. *
  2059. * Unfortunately, some disks violate the SCSI spec and
  2060. * don't issue the required SAVE_POINTERS message before
  2061. * disconnecting, and we have to break spec to remain
  2062. * compatible.
  2063. */
  2064. case SAVE_POINTERS:
  2065. case RESTORE_POINTERS:
  2066. /* Accept message by clearing ACK */
  2067. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2068. /* Enable reselect interrupts */
  2069. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2070. break;
  2071. case EXTENDED_MESSAGE:
  2072. /*
  2073. * Extended messages are sent in the following format :
  2074. * Byte
  2075. * 0 EXTENDED_MESSAGE == 1
  2076. * 1 length (includes one byte for code, doesn't
  2077. * include first two bytes)
  2078. * 2 code
  2079. * 3..length+1 arguments
  2080. *
  2081. * Start the extended message buffer with the EXTENDED_MESSAGE
  2082. * byte, since spi_print_msg() wants the whole thing.
  2083. */
  2084. extended_msg[0] = EXTENDED_MESSAGE;
  2085. /* Accept first byte by clearing ACK */
  2086. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2087. dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO);
  2088. len = 2;
  2089. data = extended_msg + 1;
  2090. phase = PHASE_MSGIN;
  2091. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2092. dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO,
  2093. (int)extended_msg[1], (int)extended_msg[2]);
  2094. if (!len && extended_msg[1] <=
  2095. (sizeof(extended_msg) - 1)) {
  2096. /* Accept third byte by clearing ACK */
  2097. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2098. len = extended_msg[1] - 1;
  2099. data = extended_msg + 3;
  2100. phase = PHASE_MSGIN;
  2101. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2102. dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n",
  2103. HOSTNO, len);
  2104. switch (extended_msg[2]) {
  2105. case EXTENDED_SDTR:
  2106. case EXTENDED_WDTR:
  2107. case EXTENDED_MODIFY_DATA_POINTER:
  2108. case EXTENDED_EXTENDED_IDENTIFY:
  2109. tmp = 0;
  2110. }
  2111. } else if (len) {
  2112. printk(KERN_NOTICE "scsi%d: error receiving "
  2113. "extended message\n", HOSTNO);
  2114. tmp = 0;
  2115. } else {
  2116. printk(KERN_NOTICE "scsi%d: extended message "
  2117. "code %02x length %d is too long\n",
  2118. HOSTNO, extended_msg[2], extended_msg[1]);
  2119. tmp = 0;
  2120. }
  2121. /* Fall through to reject message */
  2122. /*
  2123. * If we get something weird that we aren't expecting,
  2124. * reject it.
  2125. */
  2126. default:
  2127. if (!tmp) {
  2128. printk(KERN_INFO "scsi%d: rejecting message ",
  2129. instance->host_no);
  2130. spi_print_msg(extended_msg);
  2131. printk("\n");
  2132. } else if (tmp != EXTENDED_MESSAGE)
  2133. scmd_printk(KERN_INFO, cmd,
  2134. "rejecting unknown message %02x\n",
  2135. tmp);
  2136. else
  2137. scmd_printk(KERN_INFO, cmd,
  2138. "rejecting unknown extended message code %02x, length %d\n",
  2139. extended_msg[1], extended_msg[0]);
  2140. msgout = MESSAGE_REJECT;
  2141. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  2142. break;
  2143. } /* switch (tmp) */
  2144. break;
  2145. case PHASE_MSGOUT:
  2146. len = 1;
  2147. data = &msgout;
  2148. hostdata->last_message = msgout;
  2149. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2150. if (msgout == ABORT) {
  2151. local_irq_save(flags);
  2152. #ifdef SUPPORT_TAGS
  2153. cmd_free_tag(cmd);
  2154. #else
  2155. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  2156. #endif
  2157. hostdata->connected = NULL;
  2158. cmd->result = DID_ERROR << 16;
  2159. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2160. maybe_release_dma_irq(instance);
  2161. local_irq_restore(flags);
  2162. cmd->scsi_done(cmd);
  2163. return;
  2164. }
  2165. msgout = NOP;
  2166. break;
  2167. case PHASE_CMDOUT:
  2168. len = cmd->cmd_len;
  2169. data = cmd->cmnd;
  2170. /*
  2171. * XXX for performance reasons, on machines with a
  2172. * PSEUDO-DMA architecture we should probably
  2173. * use the dma transfer function.
  2174. */
  2175. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2176. break;
  2177. case PHASE_STATIN:
  2178. len = 1;
  2179. data = &tmp;
  2180. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2181. cmd->SCp.Status = tmp;
  2182. break;
  2183. default:
  2184. printk("scsi%d: unknown phase\n", HOSTNO);
  2185. NCR5380_dprint(NDEBUG_ANY, instance);
  2186. } /* switch(phase) */
  2187. } /* if (tmp * SR_REQ) */
  2188. } /* while (1) */
  2189. }
  2190. /*
  2191. * Function : void NCR5380_reselect (struct Scsi_Host *instance)
  2192. *
  2193. * Purpose : does reselection, initializing the instance->connected
  2194. * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
  2195. * nexus has been reestablished,
  2196. *
  2197. * Inputs : instance - this instance of the NCR5380.
  2198. *
  2199. */
  2200. /* it might eventually prove necessary to do a dma setup on
  2201. reselection, but it doesn't seem to be needed now -- sam */
  2202. static void NCR5380_reselect(struct Scsi_Host *instance)
  2203. {
  2204. SETUP_HOSTDATA(instance);
  2205. unsigned char target_mask;
  2206. unsigned char lun;
  2207. #ifdef SUPPORT_TAGS
  2208. unsigned char tag;
  2209. #endif
  2210. unsigned char msg[3];
  2211. int __maybe_unused len;
  2212. unsigned char __maybe_unused *data, __maybe_unused phase;
  2213. struct scsi_cmnd *tmp = NULL, *prev;
  2214. /*
  2215. * Disable arbitration, etc. since the host adapter obviously
  2216. * lost, and tell an interrupted NCR5380_select() to restart.
  2217. */
  2218. NCR5380_write(MODE_REG, MR_BASE);
  2219. hostdata->restart_select = 1;
  2220. target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
  2221. dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO);
  2222. /*
  2223. * At this point, we have detected that our SCSI ID is on the bus,
  2224. * SEL is true and BSY was false for at least one bus settle delay
  2225. * (400 ns).
  2226. *
  2227. * We must assert BSY ourselves, until the target drops the SEL
  2228. * signal.
  2229. */
  2230. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
  2231. while (NCR5380_read(STATUS_REG) & SR_SEL)
  2232. ;
  2233. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2234. /*
  2235. * Wait for target to go into MSGIN.
  2236. */
  2237. while (!(NCR5380_read(STATUS_REG) & SR_REQ))
  2238. ;
  2239. #if defined(CONFIG_SUN3) && defined(REAL_DMA)
  2240. /* acknowledge toggle to MSGIN */
  2241. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
  2242. /* peek at the byte without really hitting the bus */
  2243. msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
  2244. #else
  2245. len = 1;
  2246. data = msg;
  2247. phase = PHASE_MSGIN;
  2248. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2249. #endif
  2250. if (!(msg[0] & 0x80)) {
  2251. printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
  2252. spi_print_msg(msg);
  2253. do_abort(instance);
  2254. return;
  2255. }
  2256. lun = (msg[0] & 0x07);
  2257. #if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
  2258. /* If the phase is still MSGIN, the target wants to send some more
  2259. * messages. In case it supports tagged queuing, this is probably a
  2260. * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
  2261. */
  2262. tag = TAG_NONE;
  2263. if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
  2264. /* Accept previous IDENTIFY message by clearing ACK */
  2265. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2266. len = 2;
  2267. data = msg + 1;
  2268. if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
  2269. msg[1] == SIMPLE_QUEUE_TAG)
  2270. tag = msg[2];
  2271. dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at "
  2272. "reselection\n", HOSTNO, target_mask, lun, tag);
  2273. }
  2274. #endif
  2275. /*
  2276. * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
  2277. * just reestablished, and remove it from the disconnected queue.
  2278. */
  2279. for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL;
  2280. tmp; prev = tmp, tmp = NEXT(tmp)) {
  2281. if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
  2282. #ifdef SUPPORT_TAGS
  2283. && (tag == tmp->tag)
  2284. #endif
  2285. ) {
  2286. if (prev) {
  2287. REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
  2288. SET_NEXT(prev, NEXT(tmp));
  2289. } else {
  2290. REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
  2291. hostdata->disconnected_queue = NEXT(tmp);
  2292. }
  2293. SET_NEXT(tmp, NULL);
  2294. break;
  2295. }
  2296. }
  2297. if (!tmp) {
  2298. printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
  2299. #ifdef SUPPORT_TAGS
  2300. "tag %d "
  2301. #endif
  2302. "not in disconnected_queue.\n",
  2303. HOSTNO, target_mask, lun
  2304. #ifdef SUPPORT_TAGS
  2305. , tag
  2306. #endif
  2307. );
  2308. /*
  2309. * Since we have an established nexus that we can't do anything
  2310. * with, we must abort it.
  2311. */
  2312. do_abort(instance);
  2313. return;
  2314. }
  2315. #if defined(CONFIG_SUN3) && defined(REAL_DMA)
  2316. /* engage dma setup for the command we just saw */
  2317. {
  2318. void *d;
  2319. unsigned long count;
  2320. if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
  2321. count = tmp->SCp.buffer->length;
  2322. d = sg_virt(tmp->SCp.buffer);
  2323. } else {
  2324. count = tmp->SCp.this_residual;
  2325. d = tmp->SCp.ptr;
  2326. }
  2327. /* setup this command for dma if not already */
  2328. if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) {
  2329. sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request));
  2330. sun3_dma_setup_done = tmp;
  2331. }
  2332. }
  2333. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  2334. #endif
  2335. /* Accept message by clearing ACK */
  2336. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2337. #if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
  2338. /* If the phase is still MSGIN, the target wants to send some more
  2339. * messages. In case it supports tagged queuing, this is probably a
  2340. * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
  2341. */
  2342. tag = TAG_NONE;
  2343. if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
  2344. /* Accept previous IDENTIFY message by clearing ACK */
  2345. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2346. len = 2;
  2347. data = msg + 1;
  2348. if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
  2349. msg[1] == SIMPLE_QUEUE_TAG)
  2350. tag = msg[2];
  2351. dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
  2352. HOSTNO, target_mask, lun, tag);
  2353. }
  2354. #endif
  2355. hostdata->connected = tmp;
  2356. dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
  2357. HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
  2358. }
  2359. /*
  2360. * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
  2361. *
  2362. * Purpose : abort a command
  2363. *
  2364. * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
  2365. * host byte of the result field to, if zero DID_ABORTED is
  2366. * used.
  2367. *
  2368. * Returns : SUCCESS - success, FAILED on failure.
  2369. *
  2370. * XXX - there is no way to abort the command that is currently
  2371. * connected, you have to wait for it to complete. If this is
  2372. * a problem, we could implement longjmp() / setjmp(), setjmp()
  2373. * called where the loop started in NCR5380_main().
  2374. */
  2375. static
  2376. int NCR5380_abort(struct scsi_cmnd *cmd)
  2377. {
  2378. struct Scsi_Host *instance = cmd->device->host;
  2379. SETUP_HOSTDATA(instance);
  2380. struct scsi_cmnd *tmp, **prev;
  2381. unsigned long flags;
  2382. scmd_printk(KERN_NOTICE, cmd, "aborting command\n");
  2383. NCR5380_print_status(instance);
  2384. local_irq_save(flags);
  2385. dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
  2386. NCR5380_read(BUS_AND_STATUS_REG),
  2387. NCR5380_read(STATUS_REG));
  2388. #if 1
  2389. /*
  2390. * Case 1 : If the command is the currently executing command,
  2391. * we'll set the aborted flag and return control so that
  2392. * information transfer routine can exit cleanly.
  2393. */
  2394. if (hostdata->connected == cmd) {
  2395. dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO);
  2396. /*
  2397. * We should perform BSY checking, and make sure we haven't slipped
  2398. * into BUS FREE.
  2399. */
  2400. /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
  2401. /*
  2402. * Since we can't change phases until we've completed the current
  2403. * handshake, we have to source or sink a byte of data if the current
  2404. * phase is not MSGOUT.
  2405. */
  2406. /*
  2407. * Return control to the executing NCR drive so we can clear the
  2408. * aborted flag and get back into our main loop.
  2409. */
  2410. if (do_abort(instance) == 0) {
  2411. hostdata->aborted = 1;
  2412. hostdata->connected = NULL;
  2413. cmd->result = DID_ABORT << 16;
  2414. #ifdef SUPPORT_TAGS
  2415. cmd_free_tag(cmd);
  2416. #else
  2417. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  2418. #endif
  2419. maybe_release_dma_irq(instance);
  2420. local_irq_restore(flags);
  2421. cmd->scsi_done(cmd);
  2422. return SUCCESS;
  2423. } else {
  2424. local_irq_restore(flags);
  2425. printk("scsi%d: abort of connected command failed!\n", HOSTNO);
  2426. return FAILED;
  2427. }
  2428. }
  2429. #endif
  2430. /*
  2431. * Case 2 : If the command hasn't been issued yet, we simply remove it
  2432. * from the issue queue.
  2433. */
  2434. for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue),
  2435. tmp = (struct scsi_cmnd *)hostdata->issue_queue;
  2436. tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
  2437. if (cmd == tmp) {
  2438. REMOVE(5, *prev, tmp, NEXT(tmp));
  2439. (*prev) = NEXT(tmp);
  2440. SET_NEXT(tmp, NULL);
  2441. tmp->result = DID_ABORT << 16;
  2442. maybe_release_dma_irq(instance);
  2443. local_irq_restore(flags);
  2444. dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n",
  2445. HOSTNO);
  2446. /* Tagged queuing note: no tag to free here, hasn't been assigned
  2447. * yet... */
  2448. tmp->scsi_done(tmp);
  2449. return SUCCESS;
  2450. }
  2451. }
  2452. /*
  2453. * Case 3 : If any commands are connected, we're going to fail the abort
  2454. * and let the high level SCSI driver retry at a later time or
  2455. * issue a reset.
  2456. *
  2457. * Timeouts, and therefore aborted commands, will be highly unlikely
  2458. * and handling them cleanly in this situation would make the common
  2459. * case of noresets less efficient, and would pollute our code. So,
  2460. * we fail.
  2461. */
  2462. if (hostdata->connected) {
  2463. local_irq_restore(flags);
  2464. dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO);
  2465. return FAILED;
  2466. }
  2467. /*
  2468. * Case 4: If the command is currently disconnected from the bus, and
  2469. * there are no connected commands, we reconnect the I_T_L or
  2470. * I_T_L_Q nexus associated with it, go into message out, and send
  2471. * an abort message.
  2472. *
  2473. * This case is especially ugly. In order to reestablish the nexus, we
  2474. * need to call NCR5380_select(). The easiest way to implement this
  2475. * function was to abort if the bus was busy, and let the interrupt
  2476. * handler triggered on the SEL for reselect take care of lost arbitrations
  2477. * where necessary, meaning interrupts need to be enabled.
  2478. *
  2479. * When interrupts are enabled, the queues may change - so we
  2480. * can't remove it from the disconnected queue before selecting it
  2481. * because that could cause a failure in hashing the nexus if that
  2482. * device reselected.
  2483. *
  2484. * Since the queues may change, we can't use the pointers from when we
  2485. * first locate it.
  2486. *
  2487. * So, we must first locate the command, and if NCR5380_select()
  2488. * succeeds, then issue the abort, relocate the command and remove
  2489. * it from the disconnected queue.
  2490. */
  2491. for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp;
  2492. tmp = NEXT(tmp)) {
  2493. if (cmd == tmp) {
  2494. local_irq_restore(flags);
  2495. dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO);
  2496. if (NCR5380_select(instance, cmd))
  2497. return FAILED;
  2498. dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO);
  2499. do_abort(instance);
  2500. local_irq_save(flags);
  2501. for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue),
  2502. tmp = (struct scsi_cmnd *)hostdata->disconnected_queue;
  2503. tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) {
  2504. if (cmd == tmp) {
  2505. REMOVE(5, *prev, tmp, NEXT(tmp));
  2506. *prev = NEXT(tmp);
  2507. SET_NEXT(tmp, NULL);
  2508. tmp->result = DID_ABORT << 16;
  2509. /* We must unlock the tag/LUN immediately here, since the
  2510. * target goes to BUS FREE and doesn't send us another
  2511. * message (COMMAND_COMPLETE or the like)
  2512. */
  2513. #ifdef SUPPORT_TAGS
  2514. cmd_free_tag(tmp);
  2515. #else
  2516. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  2517. #endif
  2518. maybe_release_dma_irq(instance);
  2519. local_irq_restore(flags);
  2520. tmp->scsi_done(tmp);
  2521. return SUCCESS;
  2522. }
  2523. }
  2524. }
  2525. }
  2526. /* Maybe it is sufficient just to release the ST-DMA lock... (if
  2527. * possible at all) At least, we should check if the lock could be
  2528. * released after the abort, in case it is kept due to some bug.
  2529. */
  2530. maybe_release_dma_irq(instance);
  2531. local_irq_restore(flags);
  2532. /*
  2533. * Case 5 : If we reached this point, the command was not found in any of
  2534. * the queues.
  2535. *
  2536. * We probably reached this point because of an unlikely race condition
  2537. * between the command completing successfully and the abortion code,
  2538. * so we won't panic, but we will notify the user in case something really
  2539. * broke.
  2540. */
  2541. printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO);
  2542. return FAILED;
  2543. }
  2544. /*
  2545. * Function : int NCR5380_reset (struct scsi_cmnd *cmd)
  2546. *
  2547. * Purpose : reset the SCSI bus.
  2548. *
  2549. * Returns : SUCCESS or FAILURE
  2550. *
  2551. */
  2552. static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
  2553. {
  2554. struct Scsi_Host *instance = cmd->device->host;
  2555. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  2556. int i;
  2557. unsigned long flags;
  2558. NCR5380_print_status(instance);
  2559. /* get in phase */
  2560. NCR5380_write(TARGET_COMMAND_REG,
  2561. PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
  2562. /* assert RST */
  2563. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
  2564. udelay(40);
  2565. /* reset NCR registers */
  2566. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2567. NCR5380_write(MODE_REG, MR_BASE);
  2568. NCR5380_write(TARGET_COMMAND_REG, 0);
  2569. NCR5380_write(SELECT_ENABLE_REG, 0);
  2570. /* ++roman: reset interrupt condition! otherwise no interrupts don't get
  2571. * through anymore ... */
  2572. (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  2573. /* After the reset, there are no more connected or disconnected commands
  2574. * and no busy units; so clear the low-level status here to avoid
  2575. * conflicts when the mid-level code tries to wake up the affected
  2576. * commands!
  2577. */
  2578. if (hostdata->issue_queue)
  2579. dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
  2580. if (hostdata->connected)
  2581. dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd));
  2582. if (hostdata->disconnected_queue)
  2583. dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
  2584. local_irq_save(flags);
  2585. hostdata->issue_queue = NULL;
  2586. hostdata->connected = NULL;
  2587. hostdata->disconnected_queue = NULL;
  2588. #ifdef SUPPORT_TAGS
  2589. free_all_tags(hostdata);
  2590. #endif
  2591. for (i = 0; i < 8; ++i)
  2592. hostdata->busy[i] = 0;
  2593. #ifdef REAL_DMA
  2594. hostdata->dma_len = 0;
  2595. #endif
  2596. maybe_release_dma_irq(instance);
  2597. local_irq_restore(flags);
  2598. return SUCCESS;
  2599. }