sun3_NCR5380.c 93 KB

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