atari_NCR5380.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928
  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. }