NCR5380.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  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.10 1998/9/2 Alan Cox
  31. * (alan@redhat.com)
  32. * Fixed up the timer lockups reported so far. Things still suck. Looking
  33. * forward to 2.3 and per device request queues. Then it'll be possible to
  34. * SMP thread this beast and improve life no end.
  35. * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
  36. * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
  37. * (hopefully) fixed and enhanced USLEEP
  38. * added support for DTC3181E card (for Mustek scanner)
  39. *
  40. * Revision 1.8 Ingmar Baumgart
  41. * (ingmar@gonzo.schwaben.de)
  42. * added support for NCR53C400a card
  43. *
  44. * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
  45. * added proc_info
  46. * added support needed for DTC 3180/3280
  47. * fixed a couple of bugs
  48. *
  49. * Revision 1.5 1994/01/19 09:14:57 drew
  50. * Fixed udelay() hack that was being used on DATAOUT phases
  51. * instead of a proper wait for the final handshake.
  52. *
  53. * Revision 1.4 1994/01/19 06:44:25 drew
  54. * *** empty log message ***
  55. *
  56. * Revision 1.3 1994/01/19 05:24:40 drew
  57. * Added support for TCR LAST_BYTE_SENT bit.
  58. *
  59. * Revision 1.2 1994/01/15 06:14:11 drew
  60. * REAL DMA support, bug fixes.
  61. *
  62. * Revision 1.1 1994/01/15 06:00:54 drew
  63. * Initial revision
  64. *
  65. */
  66. /*
  67. * Further development / testing that should be done :
  68. * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
  69. * code so that everything does the same thing that's done at the
  70. * end of a pseudo-DMA read operation.
  71. *
  72. * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
  73. * basically, transfer size needs to be reduced by one
  74. * and the last byte read as is done with PSEUDO_DMA.
  75. *
  76. * 4. Test SCSI-II tagged queueing (I have no devices which support
  77. * tagged queueing)
  78. *
  79. * 5. Test linked command handling code after Eric is ready with
  80. * the high level code.
  81. */
  82. #include <scsi/scsi_dbg.h>
  83. #ifndef NDEBUG
  84. #define NDEBUG 0
  85. #endif
  86. #ifndef NDEBUG
  87. #define NDEBUG_ABORT 0
  88. #endif
  89. #if (NDEBUG & NDEBUG_LISTS)
  90. #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
  91. #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); }
  92. #else
  93. #define LIST(x,y)
  94. #define REMOVE(w,x,y,z)
  95. #endif
  96. #ifndef notyet
  97. #undef LINKED
  98. #undef REAL_DMA
  99. #endif
  100. #ifdef REAL_DMA_POLL
  101. #undef READ_OVERRUNS
  102. #define READ_OVERRUNS
  103. #endif
  104. #ifdef BOARD_REQUIRES_NO_DELAY
  105. #define io_recovery_delay(x)
  106. #else
  107. #define io_recovery_delay(x) udelay(x)
  108. #endif
  109. /*
  110. * Design
  111. *
  112. * This is a generic 5380 driver. To use it on a different platform,
  113. * one simply writes appropriate system specific macros (ie, data
  114. * transfer - some PC's will use the I/O bus, 68K's must use
  115. * memory mapped) and drops this file in their 'C' wrapper.
  116. *
  117. * (Note from hch: unfortunately it was not enough for the different
  118. * m68k folks and instead of improving this driver they copied it
  119. * and hacked it up for their needs. As a consequence they lost
  120. * most updates to this driver. Maybe someone will fix all these
  121. * drivers to use a common core one day..)
  122. *
  123. * As far as command queueing, two queues are maintained for
  124. * each 5380 in the system - commands that haven't been issued yet,
  125. * and commands that are currently executing. This means that an
  126. * unlimited number of commands may be queued, letting
  127. * more commands propagate from the higher driver levels giving higher
  128. * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
  129. * allowing multiple commands to propagate all the way to a SCSI-II device
  130. * while a command is already executing.
  131. *
  132. *
  133. * Issues specific to the NCR5380 :
  134. *
  135. * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
  136. * piece of hardware that requires you to sit in a loop polling for
  137. * the REQ signal as long as you are connected. Some devices are
  138. * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
  139. * while doing long seek operations.
  140. *
  141. * The workaround for this is to keep track of devices that have
  142. * disconnected. If the device hasn't disconnected, for commands that
  143. * should disconnect, we do something like
  144. *
  145. * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
  146. *
  147. * Some tweaking of N and M needs to be done. An algorithm based
  148. * on "time to data" would give the best results as long as short time
  149. * to datas (ie, on the same track) were considered, however these
  150. * broken devices are the exception rather than the rule and I'd rather
  151. * spend my time optimizing for the normal case.
  152. *
  153. * Architecture :
  154. *
  155. * At the heart of the design is a coroutine, NCR5380_main,
  156. * which is started from a workqueue for each NCR5380 host in the
  157. * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
  158. * removing the commands from the issue queue and calling
  159. * NCR5380_select() if a nexus is not established.
  160. *
  161. * Once a nexus is established, the NCR5380_information_transfer()
  162. * phase goes through the various phases as instructed by the target.
  163. * if the target goes into MSG IN and sends a DISCONNECT message,
  164. * the command structure is placed into the per instance disconnected
  165. * queue, and NCR5380_main tries to find more work. If the target is
  166. * idle for too long, the system will try to sleep.
  167. *
  168. * If a command has disconnected, eventually an interrupt will trigger,
  169. * calling NCR5380_intr() which will in turn call NCR5380_reselect
  170. * to reestablish a nexus. This will run main if necessary.
  171. *
  172. * On command termination, the done function will be called as
  173. * appropriate.
  174. *
  175. * SCSI pointers are maintained in the SCp field of SCSI command
  176. * structures, being initialized after the command is connected
  177. * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
  178. * Note that in violation of the standard, an implicit SAVE POINTERS operation
  179. * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
  180. */
  181. /*
  182. * Using this file :
  183. * This file a skeleton Linux SCSI driver for the NCR 5380 series
  184. * of chips. To use it, you write an architecture specific functions
  185. * and macros and include this file in your driver.
  186. *
  187. * These macros control options :
  188. * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
  189. * defined.
  190. *
  191. * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
  192. * for commands that return with a CHECK CONDITION status.
  193. *
  194. * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
  195. * transceivers.
  196. *
  197. * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
  198. * override-configure an IRQ.
  199. *
  200. * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
  201. * bytes at a time. Since interrupts are disabled by default during
  202. * these transfers, we might need this to give reasonable interrupt
  203. * service time if the transfer size gets too large.
  204. *
  205. * LINKED - if defined, linked commands are supported.
  206. *
  207. * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
  208. *
  209. * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
  210. *
  211. * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
  212. * rely on phase mismatch and EOP interrupts to determine end
  213. * of phase.
  214. *
  215. * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
  216. * only really want to use this if you're having a problem with
  217. * dropped characters during high speed communications, and even
  218. * then, you're going to be better off twiddling with transfersize
  219. * in the high level code.
  220. *
  221. * Defaults for these will be provided although the user may want to adjust
  222. * these to allocate CPU resources to the SCSI driver or "real" code.
  223. *
  224. * USLEEP_SLEEP - amount of time, in jiffies, to sleep
  225. *
  226. * USLEEP_POLL - amount of time, in jiffies, to poll
  227. *
  228. * These macros MUST be defined :
  229. * NCR5380_local_declare() - declare any local variables needed for your
  230. * transfer routines.
  231. *
  232. * NCR5380_setup(instance) - initialize any local variables needed from a given
  233. * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
  234. *
  235. * NCR5380_read(register) - read from the specified register
  236. *
  237. * NCR5380_write(register, value) - write to the specific register
  238. *
  239. * NCR5380_implementation_fields - additional fields needed for this
  240. * specific implementation of the NCR5380
  241. *
  242. * Either real DMA *or* pseudo DMA may be implemented
  243. * REAL functions :
  244. * NCR5380_REAL_DMA should be defined if real DMA is to be used.
  245. * Note that the DMA setup functions should return the number of bytes
  246. * that they were able to program the controller for.
  247. *
  248. * Also note that generic i386/PC versions of these macros are
  249. * available as NCR5380_i386_dma_write_setup,
  250. * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
  251. *
  252. * NCR5380_dma_write_setup(instance, src, count) - initialize
  253. * NCR5380_dma_read_setup(instance, dst, count) - initialize
  254. * NCR5380_dma_residual(instance); - residual count
  255. *
  256. * PSEUDO functions :
  257. * NCR5380_pwrite(instance, src, count)
  258. * NCR5380_pread(instance, dst, count);
  259. *
  260. * The generic driver is initialized by calling NCR5380_init(instance),
  261. * after setting the appropriate host specific fields and ID. If the
  262. * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
  263. * possible) function may be used.
  264. */
  265. static int do_abort(struct Scsi_Host *host);
  266. static void do_reset(struct Scsi_Host *host);
  267. /*
  268. * initialize_SCp - init the scsi pointer field
  269. * @cmd: command block to set up
  270. *
  271. * Set up the internal fields in the SCSI command.
  272. */
  273. static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
  274. {
  275. /*
  276. * Initialize the Scsi Pointer field so that all of the commands in the
  277. * various queues are valid.
  278. */
  279. if (cmd->use_sg) {
  280. cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
  281. cmd->SCp.buffers_residual = cmd->use_sg - 1;
  282. cmd->SCp.ptr = page_address(cmd->SCp.buffer->page)+
  283. cmd->SCp.buffer->offset;
  284. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  285. } else {
  286. cmd->SCp.buffer = NULL;
  287. cmd->SCp.buffers_residual = 0;
  288. cmd->SCp.ptr = (char *) cmd->request_buffer;
  289. cmd->SCp.this_residual = cmd->request_bufflen;
  290. }
  291. }
  292. /**
  293. * NCR5380_poll_politely - wait for NCR5380 status bits
  294. * @instance: controller to poll
  295. * @reg: 5380 register to poll
  296. * @bit: Bitmask to check
  297. * @val: Value required to exit
  298. *
  299. * Polls the NCR5380 in a reasonably efficient manner waiting for
  300. * an event to occur, after a short quick poll we begin giving the
  301. * CPU back in non IRQ contexts
  302. *
  303. * Returns the value of the register or a negative error code.
  304. */
  305. static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t)
  306. {
  307. NCR5380_local_declare();
  308. int n = 500; /* At about 8uS a cycle for the cpu access */
  309. unsigned long end = jiffies + t;
  310. int r;
  311. NCR5380_setup(instance);
  312. while( n-- > 0)
  313. {
  314. r = NCR5380_read(reg);
  315. if((r & bit) == val)
  316. return 0;
  317. cpu_relax();
  318. }
  319. /* t time yet ? */
  320. while(time_before(jiffies, end))
  321. {
  322. r = NCR5380_read(reg);
  323. if((r & bit) == val)
  324. return 0;
  325. if(!in_interrupt())
  326. yield();
  327. else
  328. cpu_relax();
  329. }
  330. return -ETIMEDOUT;
  331. }
  332. static struct {
  333. unsigned char value;
  334. const char *name;
  335. } phases[] = {
  336. {PHASE_DATAOUT, "DATAOUT"},
  337. {PHASE_DATAIN, "DATAIN"},
  338. {PHASE_CMDOUT, "CMDOUT"},
  339. {PHASE_STATIN, "STATIN"},
  340. {PHASE_MSGOUT, "MSGOUT"},
  341. {PHASE_MSGIN, "MSGIN"},
  342. {PHASE_UNKNOWN, "UNKNOWN"}
  343. };
  344. #if NDEBUG
  345. static struct {
  346. unsigned char mask;
  347. const char *name;
  348. } signals[] = {
  349. {SR_DBP, "PARITY"},
  350. {SR_RST, "RST"},
  351. {SR_BSY, "BSY"},
  352. {SR_REQ, "REQ"},
  353. {SR_MSG, "MSG"},
  354. {SR_CD, "CD"},
  355. {SR_IO, "IO"},
  356. {SR_SEL, "SEL"},
  357. {0, NULL}
  358. },
  359. basrs[] = {
  360. {BASR_ATN, "ATN"},
  361. {BASR_ACK, "ACK"},
  362. {0, NULL}
  363. },
  364. icrs[] = {
  365. {ICR_ASSERT_RST, "ASSERT RST"},
  366. {ICR_ASSERT_ACK, "ASSERT ACK"},
  367. {ICR_ASSERT_BSY, "ASSERT BSY"},
  368. {ICR_ASSERT_SEL, "ASSERT SEL"},
  369. {ICR_ASSERT_ATN, "ASSERT ATN"},
  370. {ICR_ASSERT_DATA, "ASSERT DATA"},
  371. {0, NULL}
  372. },
  373. mrs[] = {
  374. {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
  375. {MR_TARGET, "MODE TARGET"},
  376. {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
  377. {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
  378. {MR_MONITOR_BSY, "MODE MONITOR BSY"},
  379. {MR_DMA_MODE, "MODE DMA"},
  380. {MR_ARBITRATE, "MODE ARBITRATION"},
  381. {0, NULL}
  382. };
  383. /**
  384. * NCR5380_print - print scsi bus signals
  385. * @instance: adapter state to dump
  386. *
  387. * Print the SCSI bus signals for debugging purposes
  388. *
  389. * Locks: caller holds hostdata lock (not essential)
  390. */
  391. static void NCR5380_print(struct Scsi_Host *instance)
  392. {
  393. NCR5380_local_declare();
  394. unsigned char status, data, basr, mr, icr, i;
  395. NCR5380_setup(instance);
  396. data = NCR5380_read(CURRENT_SCSI_DATA_REG);
  397. status = NCR5380_read(STATUS_REG);
  398. mr = NCR5380_read(MODE_REG);
  399. icr = NCR5380_read(INITIATOR_COMMAND_REG);
  400. basr = NCR5380_read(BUS_AND_STATUS_REG);
  401. printk("STATUS_REG: %02x ", status);
  402. for (i = 0; signals[i].mask; ++i)
  403. if (status & signals[i].mask)
  404. printk(",%s", signals[i].name);
  405. printk("\nBASR: %02x ", basr);
  406. for (i = 0; basrs[i].mask; ++i)
  407. if (basr & basrs[i].mask)
  408. printk(",%s", basrs[i].name);
  409. printk("\nICR: %02x ", icr);
  410. for (i = 0; icrs[i].mask; ++i)
  411. if (icr & icrs[i].mask)
  412. printk(",%s", icrs[i].name);
  413. printk("\nMODE: %02x ", mr);
  414. for (i = 0; mrs[i].mask; ++i)
  415. if (mr & mrs[i].mask)
  416. printk(",%s", mrs[i].name);
  417. printk("\n");
  418. }
  419. /*
  420. * NCR5380_print_phase - show SCSI phase
  421. * @instance: adapter to dump
  422. *
  423. * Print the current SCSI phase for debugging purposes
  424. *
  425. * Locks: none
  426. */
  427. static void NCR5380_print_phase(struct Scsi_Host *instance)
  428. {
  429. NCR5380_local_declare();
  430. unsigned char status;
  431. int i;
  432. NCR5380_setup(instance);
  433. status = NCR5380_read(STATUS_REG);
  434. if (!(status & SR_REQ))
  435. printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
  436. else {
  437. for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
  438. printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
  439. }
  440. }
  441. #endif
  442. /*
  443. * These need tweaking, and would probably work best as per-device
  444. * flags initialized differently for disk, tape, cd, etc devices.
  445. * People with broken devices are free to experiment as to what gives
  446. * the best results for them.
  447. *
  448. * USLEEP_SLEEP should be a minimum seek time.
  449. *
  450. * USLEEP_POLL should be a maximum rotational latency.
  451. */
  452. #ifndef USLEEP_SLEEP
  453. /* 20 ms (reasonable hard disk speed) */
  454. #define USLEEP_SLEEP (20*HZ/1000)
  455. #endif
  456. /* 300 RPM (floppy speed) */
  457. #ifndef USLEEP_POLL
  458. #define USLEEP_POLL (200*HZ/1000)
  459. #endif
  460. #ifndef USLEEP_WAITLONG
  461. /* RvC: (reasonable time to wait on select error) */
  462. #define USLEEP_WAITLONG USLEEP_SLEEP
  463. #endif
  464. /*
  465. * Function : int should_disconnect (unsigned char cmd)
  466. *
  467. * Purpose : decide weather a command would normally disconnect or
  468. * not, since if it won't disconnect we should go to sleep.
  469. *
  470. * Input : cmd - opcode of SCSI command
  471. *
  472. * Returns : DISCONNECT_LONG if we should disconnect for a really long
  473. * time (ie always, sleep, look for REQ active, sleep),
  474. * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
  475. * time-to-data delay, DISCONNECT_NONE if this command would return
  476. * immediately.
  477. *
  478. * Future sleep algorithms based on time to data can exploit
  479. * something like this so they can differentiate between "normal"
  480. * (ie, read, write, seek) and unusual commands (ie, * format).
  481. *
  482. * Note : We don't deal with commands that handle an immediate disconnect,
  483. *
  484. */
  485. static int should_disconnect(unsigned char cmd)
  486. {
  487. switch (cmd) {
  488. case READ_6:
  489. case WRITE_6:
  490. case SEEK_6:
  491. case READ_10:
  492. case WRITE_10:
  493. case SEEK_10:
  494. return DISCONNECT_TIME_TO_DATA;
  495. case FORMAT_UNIT:
  496. case SEARCH_HIGH:
  497. case SEARCH_LOW:
  498. case SEARCH_EQUAL:
  499. return DISCONNECT_LONG;
  500. default:
  501. return DISCONNECT_NONE;
  502. }
  503. }
  504. static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
  505. {
  506. hostdata->time_expires = jiffies + timeout;
  507. schedule_delayed_work(&hostdata->coroutine, timeout);
  508. }
  509. static int probe_irq __initdata = 0;
  510. /**
  511. * probe_intr - helper for IRQ autoprobe
  512. * @irq: interrupt number
  513. * @dev_id: unused
  514. * @regs: unused
  515. *
  516. * Set a flag to indicate the IRQ in question was received. This is
  517. * used by the IRQ probe code.
  518. */
  519. static irqreturn_t __init probe_intr(int irq, void *dev_id,
  520. struct pt_regs *regs)
  521. {
  522. probe_irq = irq;
  523. return IRQ_HANDLED;
  524. }
  525. /**
  526. * NCR5380_probe_irq - find the IRQ of an NCR5380
  527. * @instance: NCR5380 controller
  528. * @possible: bitmask of ISA IRQ lines
  529. *
  530. * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
  531. * and then looking to see what interrupt actually turned up.
  532. *
  533. * Locks: none, irqs must be enabled on entry
  534. */
  535. static int __init NCR5380_probe_irq(struct Scsi_Host *instance, int possible)
  536. {
  537. NCR5380_local_declare();
  538. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  539. unsigned long timeout;
  540. int trying_irqs, i, mask;
  541. NCR5380_setup(instance);
  542. for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
  543. if ((mask & possible) && (request_irq(i, &probe_intr, SA_INTERRUPT, "NCR-probe", NULL) == 0))
  544. trying_irqs |= mask;
  545. timeout = jiffies + (250 * HZ / 1000);
  546. probe_irq = SCSI_IRQ_NONE;
  547. /*
  548. * A interrupt is triggered whenever BSY = false, SEL = true
  549. * and a bit set in the SELECT_ENABLE_REG is asserted on the
  550. * SCSI bus.
  551. *
  552. * Note that the bus is only driven when the phase control signals
  553. * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
  554. * to zero.
  555. */
  556. NCR5380_write(TARGET_COMMAND_REG, 0);
  557. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  558. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  559. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
  560. while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
  561. {
  562. set_current_state(TASK_UNINTERRUPTIBLE);
  563. schedule_timeout(1);
  564. }
  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_WORK(&hostdata->coroutine, NCR5380_main, hostdata);
  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_work(&hostdata->coroutine);
  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(void *p)
  942. {
  943. struct NCR5380_hostdata *hostdata = p;
  944. struct Scsi_Host *instance = hostdata->host;
  945. Scsi_Cmnd *tmp, *prev;
  946. int done;
  947. spin_lock_irq(instance->host_lock);
  948. do {
  949. /* Lock held here */
  950. done = 1;
  951. if (!hostdata->connected && !hostdata->selecting) {
  952. dprintk(NDEBUG_MAIN, ("scsi%d : not connected\n", instance->host_no));
  953. /*
  954. * Search through the issue_queue for a command destined
  955. * for a target that's not busy.
  956. */
  957. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
  958. {
  959. if (prev != tmp)
  960. dprintk(NDEBUG_LISTS, ("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun));
  961. /* When we find one, remove it from the issue queue. */
  962. if (!(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))) {
  963. if (prev) {
  964. REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
  965. prev->host_scribble = tmp->host_scribble;
  966. } else {
  967. REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
  968. hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
  969. }
  970. tmp->host_scribble = NULL;
  971. /*
  972. * Attempt to establish an I_T_L nexus here.
  973. * On success, instance->hostdata->connected is set.
  974. * On failure, we must add the command back to the
  975. * issue queue so we can keep trying.
  976. */
  977. 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));
  978. /*
  979. * A successful selection is defined as one that
  980. * leaves us with the command connected and
  981. * in hostdata->connected, OR has terminated the
  982. * command.
  983. *
  984. * With successful commands, we fall through
  985. * and see if we can do an information transfer,
  986. * with failures we will restart.
  987. */
  988. hostdata->selecting = NULL;
  989. /* RvC: have to preset this to indicate a new command is being performed */
  990. if (!NCR5380_select(instance, tmp,
  991. /*
  992. * REQUEST SENSE commands are issued without tagged
  993. * queueing, even on SCSI-II devices because the
  994. * contingent allegiance condition exists for the
  995. * entire unit.
  996. */
  997. (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
  998. break;
  999. } else {
  1000. LIST(tmp, hostdata->issue_queue);
  1001. tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
  1002. hostdata->issue_queue = tmp;
  1003. done = 0;
  1004. dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no));
  1005. }
  1006. /* lock held here still */
  1007. } /* if target/lun is not busy */
  1008. } /* for */
  1009. /* exited locked */
  1010. } /* if (!hostdata->connected) */
  1011. if (hostdata->selecting) {
  1012. tmp = (Scsi_Cmnd *) hostdata->selecting;
  1013. /* Selection will drop and retake the lock */
  1014. if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
  1015. /* Ok ?? */
  1016. } else {
  1017. /* RvC: device failed, so we wait a long time
  1018. this is needed for Mustek scanners, that
  1019. do not respond to commands immediately
  1020. after a scan */
  1021. printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id);
  1022. LIST(tmp, hostdata->issue_queue);
  1023. tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
  1024. hostdata->issue_queue = tmp;
  1025. NCR5380_set_timer(hostdata, USLEEP_WAITLONG);
  1026. }
  1027. } /* if hostdata->selecting */
  1028. if (hostdata->connected
  1029. #ifdef REAL_DMA
  1030. && !hostdata->dmalen
  1031. #endif
  1032. && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
  1033. ) {
  1034. dprintk(NDEBUG_MAIN, ("scsi%d : main() : performing information transfer\n", instance->host_no));
  1035. NCR5380_information_transfer(instance);
  1036. dprintk(NDEBUG_MAIN, ("scsi%d : main() : done set false\n", instance->host_no));
  1037. done = 0;
  1038. } else
  1039. break;
  1040. } while (!done);
  1041. spin_unlock_irq(instance->host_lock);
  1042. }
  1043. #ifndef DONT_USE_INTR
  1044. /**
  1045. * NCR5380_intr - generic NCR5380 irq handler
  1046. * @irq: interrupt number
  1047. * @dev_id: device info
  1048. * @regs: registers (unused)
  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, struct pt_regs *regs)
  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_work(&hostdata->coroutine);
  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[cmd->device->id] += (jiffies - hostdata->timebase);
  1134. hostdata->pendingw--;
  1135. break;
  1136. case READ:
  1137. case READ_6:
  1138. case READ_10:
  1139. hostdata->time_read[cmd->device->id] += (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 << cmd->device->id)));
  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, cmd->device->id));
  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 << cmd->device->id)) {
  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 << cmd->device->id);
  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. printk("scsi%d : switching target %d lun %d to slow handshake\n", instance->host_no, cmd->device->id, cmd->device->lun);
  1956. cmd->device->borken = 1;
  1957. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1958. sink = 1;
  1959. do_abort(instance);
  1960. cmd->result = DID_ERROR << 16;
  1961. cmd->done(cmd);
  1962. /* XXX - need to source or sink data here, as appropriate */
  1963. } else
  1964. cmd->SCp.this_residual -= transfersize - len;
  1965. } else
  1966. #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
  1967. NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
  1968. &cmd->SCp.ptr);
  1969. break;
  1970. case PHASE_MSGIN:
  1971. len = 1;
  1972. data = &tmp;
  1973. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1974. cmd->SCp.Message = tmp;
  1975. switch (tmp) {
  1976. /*
  1977. * Linking lets us reduce the time required to get the
  1978. * next command out to the device, hopefully this will
  1979. * mean we don't waste another revolution due to the delays
  1980. * required by ARBITRATION and another SELECTION.
  1981. *
  1982. * In the current implementation proposal, low level drivers
  1983. * merely have to start the next command, pointed to by
  1984. * next_link, done() is called as with unlinked commands.
  1985. */
  1986. #ifdef LINKED
  1987. case LINKED_CMD_COMPLETE:
  1988. case LINKED_FLG_CMD_COMPLETE:
  1989. /* Accept message by clearing ACK */
  1990. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1991. dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun));
  1992. /*
  1993. * Sanity check : A linked command should only terminate with
  1994. * one of these messages if there are more linked commands
  1995. * available.
  1996. */
  1997. if (!cmd->next_link) {
  1998. printk("scsi%d : target %d lun %d linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun);
  1999. sink = 1;
  2000. do_abort(instance);
  2001. return;
  2002. }
  2003. initialize_SCp(cmd->next_link);
  2004. /* The next command is still part of this process */
  2005. cmd->next_link->tag = cmd->tag;
  2006. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  2007. 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));
  2008. collect_stats(hostdata, cmd);
  2009. cmd->scsi_done(cmd);
  2010. cmd = hostdata->connected;
  2011. break;
  2012. #endif /* def LINKED */
  2013. case ABORT:
  2014. case COMMAND_COMPLETE:
  2015. /* Accept message by clearing ACK */
  2016. sink = 1;
  2017. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2018. hostdata->connected = NULL;
  2019. dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d, lun %d completed\n", instance->host_no, cmd->device->id, cmd->device->lun));
  2020. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  2021. /*
  2022. * I'm not sure what the correct thing to do here is :
  2023. *
  2024. * If the command that just executed is NOT a request
  2025. * sense, the obvious thing to do is to set the result
  2026. * code to the values of the stored parameters.
  2027. *
  2028. * If it was a REQUEST SENSE command, we need some way
  2029. * to differentiate between the failure code of the original
  2030. * and the failure code of the REQUEST sense - the obvious
  2031. * case is success, where we fall through and leave the result
  2032. * code unchanged.
  2033. *
  2034. * The non-obvious place is where the REQUEST SENSE failed
  2035. */
  2036. if (cmd->cmnd[0] != REQUEST_SENSE)
  2037. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  2038. else if (status_byte(cmd->SCp.Status) != GOOD)
  2039. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  2040. #ifdef AUTOSENSE
  2041. if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
  2042. dprintk(NDEBUG_AUTOSENSE, ("scsi%d : performing request sense\n", instance->host_no));
  2043. cmd->cmnd[0] = REQUEST_SENSE;
  2044. cmd->cmnd[1] &= 0xe0;
  2045. cmd->cmnd[2] = 0;
  2046. cmd->cmnd[3] = 0;
  2047. cmd->cmnd[4] = sizeof(cmd->sense_buffer);
  2048. cmd->cmnd[5] = 0;
  2049. cmd->SCp.buffer = NULL;
  2050. cmd->SCp.buffers_residual = 0;
  2051. cmd->SCp.ptr = (char *) cmd->sense_buffer;
  2052. cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
  2053. LIST(cmd, hostdata->issue_queue);
  2054. cmd->host_scribble = (unsigned char *)
  2055. hostdata->issue_queue;
  2056. hostdata->issue_queue = (Scsi_Cmnd *) cmd;
  2057. dprintk(NDEBUG_QUEUES, ("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no));
  2058. } else
  2059. #endif /* def AUTOSENSE */
  2060. {
  2061. collect_stats(hostdata, cmd);
  2062. cmd->scsi_done(cmd);
  2063. }
  2064. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2065. /*
  2066. * Restore phase bits to 0 so an interrupted selection,
  2067. * arbitration can resume.
  2068. */
  2069. NCR5380_write(TARGET_COMMAND_REG, 0);
  2070. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2071. barrier();
  2072. return;
  2073. case MESSAGE_REJECT:
  2074. /* Accept message by clearing ACK */
  2075. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2076. switch (hostdata->last_message) {
  2077. case HEAD_OF_QUEUE_TAG:
  2078. case ORDERED_QUEUE_TAG:
  2079. case SIMPLE_QUEUE_TAG:
  2080. cmd->device->simple_tags = 0;
  2081. hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
  2082. break;
  2083. default:
  2084. break;
  2085. }
  2086. case DISCONNECT:{
  2087. /* Accept message by clearing ACK */
  2088. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2089. cmd->device->disconnect = 1;
  2090. LIST(cmd, hostdata->disconnected_queue);
  2091. cmd->host_scribble = (unsigned char *)
  2092. hostdata->disconnected_queue;
  2093. hostdata->connected = NULL;
  2094. hostdata->disconnected_queue = cmd;
  2095. 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));
  2096. /*
  2097. * Restore phase bits to 0 so an interrupted selection,
  2098. * arbitration can resume.
  2099. */
  2100. NCR5380_write(TARGET_COMMAND_REG, 0);
  2101. /* Enable reselect interrupts */
  2102. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2103. /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
  2104. /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
  2105. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2106. barrier();
  2107. return;
  2108. }
  2109. /*
  2110. * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  2111. * operation, in violation of the SCSI spec so we can safely
  2112. * ignore SAVE/RESTORE pointers calls.
  2113. *
  2114. * Unfortunately, some disks violate the SCSI spec and
  2115. * don't issue the required SAVE_POINTERS message before
  2116. * disconnecting, and we have to break spec to remain
  2117. * compatible.
  2118. */
  2119. case SAVE_POINTERS:
  2120. case RESTORE_POINTERS:
  2121. /* Accept message by clearing ACK */
  2122. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2123. break;
  2124. case EXTENDED_MESSAGE:
  2125. /*
  2126. * Extended messages are sent in the following format :
  2127. * Byte
  2128. * 0 EXTENDED_MESSAGE == 1
  2129. * 1 length (includes one byte for code, doesn't
  2130. * include first two bytes)
  2131. * 2 code
  2132. * 3..length+1 arguments
  2133. *
  2134. * Start the extended message buffer with the EXTENDED_MESSAGE
  2135. * byte, since scsi_print_msg() wants the whole thing.
  2136. */
  2137. extended_msg[0] = EXTENDED_MESSAGE;
  2138. /* Accept first byte by clearing ACK */
  2139. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2140. dprintk(NDEBUG_EXTENDED, ("scsi%d : receiving extended message\n", instance->host_no));
  2141. len = 2;
  2142. data = extended_msg + 1;
  2143. phase = PHASE_MSGIN;
  2144. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2145. dprintk(NDEBUG_EXTENDED, ("scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]));
  2146. if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
  2147. /* Accept third byte by clearing ACK */
  2148. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2149. len = extended_msg[1] - 1;
  2150. data = extended_msg + 3;
  2151. phase = PHASE_MSGIN;
  2152. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2153. dprintk(NDEBUG_EXTENDED, ("scsi%d : message received, residual %d\n", instance->host_no, len));
  2154. switch (extended_msg[2]) {
  2155. case EXTENDED_SDTR:
  2156. case EXTENDED_WDTR:
  2157. case EXTENDED_MODIFY_DATA_POINTER:
  2158. case EXTENDED_EXTENDED_IDENTIFY:
  2159. tmp = 0;
  2160. }
  2161. } else if (len) {
  2162. printk("scsi%d: error receiving extended message\n", instance->host_no);
  2163. tmp = 0;
  2164. } else {
  2165. printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
  2166. tmp = 0;
  2167. }
  2168. /* Fall through to reject message */
  2169. /*
  2170. * If we get something weird that we aren't expecting,
  2171. * reject it.
  2172. */
  2173. default:
  2174. if (!tmp) {
  2175. printk("scsi%d: rejecting message ", instance->host_no);
  2176. scsi_print_msg(extended_msg);
  2177. printk("\n");
  2178. } else if (tmp != EXTENDED_MESSAGE)
  2179. printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n", instance->host_no, tmp, cmd->device->id, cmd->device->lun);
  2180. else
  2181. printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n", instance->host_no, extended_msg[1], extended_msg[0], cmd->device->id, cmd->device->lun);
  2182. msgout = MESSAGE_REJECT;
  2183. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  2184. break;
  2185. } /* switch (tmp) */
  2186. break;
  2187. case PHASE_MSGOUT:
  2188. len = 1;
  2189. data = &msgout;
  2190. hostdata->last_message = msgout;
  2191. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2192. if (msgout == ABORT) {
  2193. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  2194. hostdata->connected = NULL;
  2195. cmd->result = DID_ERROR << 16;
  2196. collect_stats(hostdata, cmd);
  2197. cmd->scsi_done(cmd);
  2198. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2199. return;
  2200. }
  2201. msgout = NOP;
  2202. break;
  2203. case PHASE_CMDOUT:
  2204. len = cmd->cmd_len;
  2205. data = cmd->cmnd;
  2206. /*
  2207. * XXX for performance reasons, on machines with a
  2208. * PSEUDO-DMA architecture we should probably
  2209. * use the dma transfer function.
  2210. */
  2211. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2212. if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
  2213. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  2214. dprintk(NDEBUG_USLEEP, ("scsi%d : issued command, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
  2215. return;
  2216. }
  2217. break;
  2218. case PHASE_STATIN:
  2219. len = 1;
  2220. data = &tmp;
  2221. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2222. cmd->SCp.Status = tmp;
  2223. break;
  2224. default:
  2225. printk("scsi%d : unknown phase\n", instance->host_no);
  2226. NCR5380_dprint(NDEBUG_ALL, instance);
  2227. } /* switch(phase) */
  2228. } /* if (tmp * SR_REQ) */
  2229. else {
  2230. /* RvC: go to sleep if polling time expired
  2231. */
  2232. if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
  2233. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  2234. dprintk(NDEBUG_USLEEP, ("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
  2235. return;
  2236. }
  2237. }
  2238. } /* while (1) */
  2239. }
  2240. /*
  2241. * Function : void NCR5380_reselect (struct Scsi_Host *instance)
  2242. *
  2243. * Purpose : does reselection, initializing the instance->connected
  2244. * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
  2245. * nexus has been reestablished,
  2246. *
  2247. * Inputs : instance - this instance of the NCR5380.
  2248. *
  2249. * Locks: io_request_lock held by caller if IRQ driven
  2250. */
  2251. static void NCR5380_reselect(struct Scsi_Host *instance) {
  2252. NCR5380_local_declare();
  2253. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
  2254. instance->hostdata;
  2255. unsigned char target_mask;
  2256. unsigned char lun, phase;
  2257. int len;
  2258. unsigned char msg[3];
  2259. unsigned char *data;
  2260. Scsi_Cmnd *tmp = NULL, *prev;
  2261. int abort = 0;
  2262. NCR5380_setup(instance);
  2263. /*
  2264. * Disable arbitration, etc. since the host adapter obviously
  2265. * lost, and tell an interrupted NCR5380_select() to restart.
  2266. */
  2267. NCR5380_write(MODE_REG, MR_BASE);
  2268. hostdata->restart_select = 1;
  2269. target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
  2270. dprintk(NDEBUG_SELECTION, ("scsi%d : reselect\n", instance->host_no));
  2271. /*
  2272. * At this point, we have detected that our SCSI ID is on the bus,
  2273. * SEL is true and BSY was false for at least one bus settle delay
  2274. * (400 ns).
  2275. *
  2276. * We must assert BSY ourselves, until the target drops the SEL
  2277. * signal.
  2278. */
  2279. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
  2280. /* FIXME: timeout too long, must fail to workqueue */
  2281. if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
  2282. abort = 1;
  2283. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2284. /*
  2285. * Wait for target to go into MSGIN.
  2286. * FIXME: timeout needed and fail to work queeu
  2287. */
  2288. if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
  2289. abort = 1;
  2290. len = 1;
  2291. data = msg;
  2292. phase = PHASE_MSGIN;
  2293. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2294. if (!(msg[0] & 0x80)) {
  2295. printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
  2296. scsi_print_msg(msg);
  2297. abort = 1;
  2298. } else {
  2299. /* Accept message by clearing ACK */
  2300. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2301. lun = (msg[0] & 0x07);
  2302. /*
  2303. * We need to add code for SCSI-II to track which devices have
  2304. * I_T_L_Q nexuses established, and which have simple I_T_L
  2305. * nexuses so we can chose to do additional data transfer.
  2306. */
  2307. /*
  2308. * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
  2309. * just reestablished, and remove it from the disconnected queue.
  2310. */
  2311. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2312. if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
  2313. ) {
  2314. if (prev) {
  2315. REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
  2316. prev->host_scribble = tmp->host_scribble;
  2317. } else {
  2318. REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
  2319. hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
  2320. }
  2321. tmp->host_scribble = NULL;
  2322. break;
  2323. }
  2324. if (!tmp) {
  2325. printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
  2326. /*
  2327. * Since we have an established nexus that we can't do anything with,
  2328. * we must abort it.
  2329. */
  2330. abort = 1;
  2331. }
  2332. }
  2333. if (abort) {
  2334. do_abort(instance);
  2335. } else {
  2336. hostdata->connected = tmp;
  2337. dprintk(NDEBUG_RESELECTION, ("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n", instance->host_no, tmp->target, tmp->lun, tmp->tag));
  2338. }
  2339. }
  2340. /*
  2341. * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
  2342. *
  2343. * Purpose : called by interrupt handler when DMA finishes or a phase
  2344. * mismatch occurs (which would finish the DMA transfer).
  2345. *
  2346. * Inputs : instance - this instance of the NCR5380.
  2347. *
  2348. * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
  2349. * nexus has been reestablished, on failure NULL is returned.
  2350. */
  2351. #ifdef REAL_DMA
  2352. static void NCR5380_dma_complete(NCR5380_instance * instance) {
  2353. NCR5380_local_declare();
  2354. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata * instance->hostdata);
  2355. int transferred;
  2356. NCR5380_setup(instance);
  2357. /*
  2358. * XXX this might not be right.
  2359. *
  2360. * Wait for final byte to transfer, ie wait for ACK to go false.
  2361. *
  2362. * We should use the Last Byte Sent bit, unfortunately this is
  2363. * not available on the 5380/5381 (only the various CMOS chips)
  2364. *
  2365. * FIXME: timeout, and need to handle long timeout/irq case
  2366. */
  2367. NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
  2368. NCR5380_write(MODE_REG, MR_BASE);
  2369. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2370. /*
  2371. * The only places we should see a phase mismatch and have to send
  2372. * data from the same set of pointers will be the data transfer
  2373. * phases. So, residual, requested length are only important here.
  2374. */
  2375. if (!(hostdata->connected->SCp.phase & SR_CD)) {
  2376. transferred = instance->dmalen - NCR5380_dma_residual();
  2377. hostdata->connected->SCp.this_residual -= transferred;
  2378. hostdata->connected->SCp.ptr += transferred;
  2379. }
  2380. }
  2381. #endif /* def REAL_DMA */
  2382. /*
  2383. * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
  2384. *
  2385. * Purpose : abort a command
  2386. *
  2387. * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
  2388. * host byte of the result field to, if zero DID_ABORTED is
  2389. * used.
  2390. *
  2391. * Returns : 0 - success, -1 on failure.
  2392. *
  2393. * XXX - there is no way to abort the command that is currently
  2394. * connected, you have to wait for it to complete. If this is
  2395. * a problem, we could implement longjmp() / setjmp(), setjmp()
  2396. * called where the loop started in NCR5380_main().
  2397. *
  2398. * Locks: host lock taken by caller
  2399. */
  2400. static int NCR5380_abort(Scsi_Cmnd * cmd) {
  2401. NCR5380_local_declare();
  2402. struct Scsi_Host *instance = cmd->device->host;
  2403. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  2404. Scsi_Cmnd *tmp, **prev;
  2405. printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no);
  2406. scsi_print_command(cmd);
  2407. NCR5380_print_status(instance);
  2408. NCR5380_setup(instance);
  2409. dprintk(NDEBUG_ABORT, ("scsi%d : abort called\n", instance->host_no));
  2410. dprintk(NDEBUG_ABORT, (" basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG)));
  2411. #if 0
  2412. /*
  2413. * Case 1 : If the command is the currently executing command,
  2414. * we'll set the aborted flag and return control so that
  2415. * information transfer routine can exit cleanly.
  2416. */
  2417. if (hostdata->connected == cmd) {
  2418. dprintk(NDEBUG_ABORT, ("scsi%d : aborting connected command\n", instance->host_no));
  2419. hostdata->aborted = 1;
  2420. /*
  2421. * We should perform BSY checking, and make sure we haven't slipped
  2422. * into BUS FREE.
  2423. */
  2424. NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
  2425. /*
  2426. * Since we can't change phases until we've completed the current
  2427. * handshake, we have to source or sink a byte of data if the current
  2428. * phase is not MSGOUT.
  2429. */
  2430. /*
  2431. * Return control to the executing NCR drive so we can clear the
  2432. * aborted flag and get back into our main loop.
  2433. */
  2434. return 0;
  2435. }
  2436. #endif
  2437. /*
  2438. * Case 2 : If the command hasn't been issued yet, we simply remove it
  2439. * from the issue queue.
  2440. */
  2441. dprintk(NDEBUG_ABORT, ("scsi%d : abort going into loop.\n", instance->host_no));
  2442. 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)
  2443. if (cmd == tmp) {
  2444. REMOVE(5, *prev, tmp, tmp->host_scribble);
  2445. (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
  2446. tmp->host_scribble = NULL;
  2447. tmp->result = DID_ABORT << 16;
  2448. dprintk(NDEBUG_ABORT, ("scsi%d : abort removed command from issue queue.\n", instance->host_no));
  2449. tmp->done(tmp);
  2450. return SUCCESS;
  2451. }
  2452. #if (NDEBUG & NDEBUG_ABORT)
  2453. /* KLL */
  2454. else if (prev == tmp)
  2455. printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
  2456. #endif
  2457. /*
  2458. * Case 3 : If any commands are connected, we're going to fail the abort
  2459. * and let the high level SCSI driver retry at a later time or
  2460. * issue a reset.
  2461. *
  2462. * Timeouts, and therefore aborted commands, will be highly unlikely
  2463. * and handling them cleanly in this situation would make the common
  2464. * case of noresets less efficient, and would pollute our code. So,
  2465. * we fail.
  2466. */
  2467. if (hostdata->connected) {
  2468. dprintk(NDEBUG_ABORT, ("scsi%d : abort failed, command connected.\n", instance->host_no));
  2469. return FAILED;
  2470. }
  2471. /*
  2472. * Case 4: If the command is currently disconnected from the bus, and
  2473. * there are no connected commands, we reconnect the I_T_L or
  2474. * I_T_L_Q nexus associated with it, go into message out, and send
  2475. * an abort message.
  2476. *
  2477. * This case is especially ugly. In order to reestablish the nexus, we
  2478. * need to call NCR5380_select(). The easiest way to implement this
  2479. * function was to abort if the bus was busy, and let the interrupt
  2480. * handler triggered on the SEL for reselect take care of lost arbitrations
  2481. * where necessary, meaning interrupts need to be enabled.
  2482. *
  2483. * When interrupts are enabled, the queues may change - so we
  2484. * can't remove it from the disconnected queue before selecting it
  2485. * because that could cause a failure in hashing the nexus if that
  2486. * device reselected.
  2487. *
  2488. * Since the queues may change, we can't use the pointers from when we
  2489. * first locate it.
  2490. *
  2491. * So, we must first locate the command, and if NCR5380_select()
  2492. * succeeds, then issue the abort, relocate the command and remove
  2493. * it from the disconnected queue.
  2494. */
  2495. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2496. if (cmd == tmp) {
  2497. dprintk(NDEBUG_ABORT, ("scsi%d : aborting disconnected command.\n", instance->host_no));
  2498. if (NCR5380_select(instance, cmd, (int) cmd->tag))
  2499. return FAILED;
  2500. dprintk(NDEBUG_ABORT, ("scsi%d : nexus reestablished.\n", instance->host_no));
  2501. do_abort(instance);
  2502. 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)
  2503. if (cmd == tmp) {
  2504. REMOVE(5, *prev, tmp, tmp->host_scribble);
  2505. *prev = (Scsi_Cmnd *) tmp->host_scribble;
  2506. tmp->host_scribble = NULL;
  2507. tmp->result = DID_ABORT << 16;
  2508. tmp->done(tmp);
  2509. return SUCCESS;
  2510. }
  2511. }
  2512. /*
  2513. * Case 5 : If we reached this point, the command was not found in any of
  2514. * the queues.
  2515. *
  2516. * We probably reached this point because of an unlikely race condition
  2517. * between the command completing successfully and the abortion code,
  2518. * so we won't panic, but we will notify the user in case something really
  2519. * broke.
  2520. */
  2521. printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
  2522. " before abortion\n", instance->host_no);
  2523. return FAILED;
  2524. }
  2525. /*
  2526. * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
  2527. *
  2528. * Purpose : reset the SCSI bus.
  2529. *
  2530. * Returns : SUCCESS
  2531. *
  2532. * Locks: host lock taken by caller
  2533. */
  2534. static int NCR5380_bus_reset(Scsi_Cmnd * cmd)
  2535. {
  2536. struct Scsi_Host *instance = cmd->device->host;
  2537. NCR5380_local_declare();
  2538. NCR5380_setup(instance);
  2539. NCR5380_print_status(instance);
  2540. spin_lock_irq(instance->host_lock);
  2541. do_reset(instance);
  2542. spin_unlock_irq(instance->host_lock);
  2543. return SUCCESS;
  2544. }