NCR5380.c 91 KB

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